CMakeLists.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # 3.5 is actually available almost everywhere, but this a good minimum
  2. cmake_minimum_required(VERSION 3.4)
  3. # enable MSVC_RUNTIME_LIBRARY target property
  4. # see https://cmake.org/cmake/help/latest/policy/CMP0091.html
  5. if(POLICY CMP0091)
  6. cmake_policy(SET CMP0091 NEW)
  7. endif()
  8. project(YAML_CPP VERSION 0.7.0 LANGUAGES CXX)
  9. set(YAML_CPP_MAIN_PROJECT OFF)
  10. if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
  11. set(YAML_CPP_MAIN_PROJECT ON)
  12. endif()
  13. include(CMakePackageConfigHelpers)
  14. include(CMakeDependentOption)
  15. include(CheckCXXCompilerFlag)
  16. include(GNUInstallDirs)
  17. include(CTest)
  18. option(YAML_CPP_BUILD_CONTRIB "Enable yaml-cpp contrib in library" ON)
  19. option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON)
  20. option(YAML_BUILD_SHARED_LIBS "Build yaml-cpp shared library" ${BUILD_SHARED_LIBS})
  21. option(YAML_CPP_INSTALL "Enable generation of yaml-cpp install targets" ${YAML_CPP_MAIN_PROJECT})
  22. option(YAML_CPP_FORMAT_SOURCE "Format source" ON)
  23. cmake_dependent_option(YAML_CPP_BUILD_TESTS
  24. "Enable yaml-cpp tests" OFF
  25. "BUILD_TESTING;YAML_CPP_MAIN_PROJECT" OFF)
  26. cmake_dependent_option(YAML_MSVC_SHARED_RT
  27. "MSVC: Build yaml-cpp with shared runtime libs (/MD)" ON
  28. "CMAKE_SYSTEM_NAME MATCHES Windows" OFF)
  29. if (YAML_CPP_FORMAT_SOURCE)
  30. find_program(YAML_CPP_CLANG_FORMAT_EXE NAMES clang-format)
  31. endif()
  32. if (YAML_BUILD_SHARED_LIBS)
  33. set(yaml-cpp-type SHARED)
  34. set(yaml-cpp-label-postfix "shared")
  35. else()
  36. set(yaml-cpp-type STATIC)
  37. set(yaml-cpp-label-postfix "static")
  38. endif()
  39. set(build-shared $<BOOL:${YAML_BUILD_SHARED_LIBS}>)
  40. set(build-windows-dll $<AND:$<BOOL:${CMAKE_HOST_WIN32}>,${build-shared}>)
  41. set(not-msvc $<NOT:$<CXX_COMPILER_ID:MSVC>>)
  42. set(msvc-shared_rt $<BOOL:${YAML_MSVC_SHARED_RT}>)
  43. if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
  44. set(CMAKE_MSVC_RUNTIME_LIBRARY
  45. MultiThreaded$<$<CONFIG:Debug>:Debug>$<${msvc-shared_rt}:DLL>)
  46. endif()
  47. set(contrib-pattern "src/contrib/*.cpp")
  48. set(src-pattern "src/*.cpp")
  49. if (CMAKE_VERSION VERSION_GREATER 3.12)
  50. list(INSERT contrib-pattern 0 CONFIGURE_DEPENDS)
  51. list(INSERT src-pattern 0 CONFIGURE_DEPENDS)
  52. endif()
  53. file(GLOB yaml-cpp-contrib-sources ${contrib-pattern})
  54. file(GLOB yaml-cpp-sources ${src-pattern})
  55. set(msvc-rt $<TARGET_PROPERTY:MSVC_RUNTIME_LIBRARY>)
  56. set(msvc-rt-mtd-static $<STREQUAL:${msvc-rt},MultiThreadedDebug>)
  57. set(msvc-rt-mt-static $<STREQUAL:${msvc-rt},MultiThreaded>)
  58. set(msvc-rt-mtd-dll $<STREQUAL:${msvc-rt},MultiThreadedDebugDLL>)
  59. set(msvc-rt-mt-dll $<STREQUAL:${msvc-rt},MultiThreadedDLL>)
  60. set(backport-msvc-runtime $<VERSION_LESS:${CMAKE_VERSION},3.15>)
  61. add_library(yaml-cpp ${yaml-cpp-type} "")
  62. add_library(yaml-cpp::yaml-cpp ALIAS yaml-cpp)
  63. set_property(TARGET yaml-cpp
  64. PROPERTY
  65. MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY})
  66. set_property(TARGET yaml-cpp
  67. PROPERTY
  68. CXX_STANDARD_REQUIRED ON)
  69. if (NOT YAML_BUILD_SHARED_LIBS)
  70. set_property(TARGET yaml-cpp PROPERTY POSITION_INDEPENDENT_CODE ON)
  71. endif()
  72. target_include_directories(yaml-cpp
  73. PUBLIC
  74. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  75. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  76. PRIVATE
  77. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
  78. if (NOT DEFINED CMAKE_CXX_STANDARD)
  79. set_target_properties(yaml-cpp
  80. PROPERTIES
  81. CXX_STANDARD 11)
  82. endif()
  83. if(YAML_CPP_MAIN_PROJECT)
  84. target_compile_options(yaml-cpp
  85. PRIVATE
  86. $<${not-msvc}:-Wall -Wextra -Wshadow -Weffc++ -Wno-long-long>
  87. $<${not-msvc}:-pedantic -pedantic-errors>)
  88. endif()
  89. target_compile_options(yaml-cpp
  90. PRIVATE
  91. $<$<AND:${backport-msvc-runtime},${msvc-rt-mtd-static}>:-MTd>
  92. $<$<AND:${backport-msvc-runtime},${msvc-rt-mt-static}>:-MT>
  93. $<$<AND:${backport-msvc-runtime},${msvc-rt-mtd-dll}>:-MDd>
  94. $<$<AND:${backport-msvc-runtime},${msvc-rt-mt-dll}>:-MD>
  95. # /wd4127 = disable warning C4127 "conditional expression is constant"
  96. # http://msdn.microsoft.com/en-us/library/6t66728h.aspx
  97. # /wd4355 = disable warning C4355 "'this' : used in base member initializer list
  98. # http://msdn.microsoft.com/en-us/library/3c594ae3.aspx
  99. $<$<CXX_COMPILER_ID:MSVC>:/W3 /wd4127 /wd4355>)
  100. target_compile_definitions(yaml-cpp
  101. PUBLIC
  102. $<$<NOT:$<BOOL:${YAML_BUILD_SHARED_LIBS}>>:YAML_CPP_STATIC_DEFINE>
  103. PRIVATE
  104. $<${build-windows-dll}:${PROJECT_NAME}_DLL>
  105. $<$<NOT:$<BOOL:${YAML_CPP_BUILD_CONTRIB}>>:YAML_CPP_NO_CONTRIB>)
  106. target_sources(yaml-cpp
  107. PRIVATE
  108. $<$<BOOL:${YAML_CPP_BUILD_CONTRIB}>:${yaml-cpp-contrib-sources}>
  109. ${yaml-cpp-sources})
  110. if (NOT DEFINED CMAKE_DEBUG_POSTFIX)
  111. set(CMAKE_DEBUG_POSTFIX "d")
  112. endif()
  113. set_target_properties(yaml-cpp PROPERTIES
  114. VERSION "${PROJECT_VERSION}"
  115. SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
  116. PROJECT_LABEL "yaml-cpp ${yaml-cpp-label-postfix}"
  117. DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
  118. set(CONFIG_EXPORT_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/yaml-cpp")
  119. set(EXPORT_TARGETS yaml-cpp)
  120. configure_package_config_file(
  121. "${PROJECT_SOURCE_DIR}/yaml-cpp-config.cmake.in"
  122. "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
  123. INSTALL_DESTINATION "${CONFIG_EXPORT_DIR}"
  124. PATH_VARS CMAKE_INSTALL_INCLUDEDIR CMAKE_INSTALL_LIBDIR CONFIG_EXPORT_DIR YAML_BUILD_SHARED_LIBS)
  125. unset(EXPORT_TARGETS)
  126. write_basic_package_version_file(
  127. "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
  128. COMPATIBILITY AnyNewerVersion)
  129. configure_file(yaml-cpp.pc.in yaml-cpp.pc @ONLY)
  130. if (YAML_CPP_INSTALL)
  131. install(TARGETS yaml-cpp
  132. EXPORT yaml-cpp-targets
  133. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  134. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  135. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
  136. install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
  137. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  138. FILES_MATCHING PATTERN "*.h")
  139. install(EXPORT yaml-cpp-targets
  140. DESTINATION "${CONFIG_EXPORT_DIR}")
  141. install(FILES
  142. "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
  143. "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
  144. DESTINATION "${CONFIG_EXPORT_DIR}")
  145. install(FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc"
  146. DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
  147. endif()
  148. unset(CONFIG_EXPORT_DIR)
  149. if(YAML_CPP_BUILD_TESTS)
  150. add_subdirectory(test)
  151. endif()
  152. if(YAML_CPP_BUILD_TOOLS)
  153. add_subdirectory(util)
  154. endif()
  155. if (YAML_CPP_FORMAT_SOURCE AND YAML_CPP_CLANG_FORMAT_EXE)
  156. add_custom_target(format
  157. COMMAND clang-format --style=file -i $<TARGET_PROPERTY:yaml-cpp,SOURCES>
  158. COMMAND_EXPAND_LISTS
  159. COMMENT "Running clang-format"
  160. VERBATIM)
  161. endif()
  162. # uninstall target
  163. if(NOT TARGET uninstall)
  164. configure_file(
  165. "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
  166. "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
  167. IMMEDIATE @ONLY)
  168. add_custom_target(uninstall
  169. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
  170. endif()