You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
7.0 KiB

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