2023-08-17 08:54:05 +10:00
# 3.5 is actually available almost everywhere, but this a good minimum.
# 3.14 as the upper policy limit avoids CMake deprecation warnings.
cmake_minimum_required ( VERSION 3.4...3.14 )
2020-07-07 18:38:10 -07:00
# enable MSVC_RUNTIME_LIBRARY target property
# see https://cmake.org/cmake/help/latest/policy/CMP0091.html
if ( POLICY CMP0091 )
cmake_policy ( SET CMP0091 NEW )
endif ()
2023-08-10 14:27:56 -05:00
project ( YAML_CPP VERSION 0.8.0 LANGUAGES CXX )
2011-03-17 02:06:10 +00:00
2022-04-03 23:57:28 +02:00
set ( YAML_CPP_MAIN_PROJECT OFF )
if ( CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR )
set ( YAML_CPP_MAIN_PROJECT ON )
endif ()
2019-09-27 07:59:53 -07:00
include ( CMakePackageConfigHelpers )
include ( CMakeDependentOption )
2011-03-17 02:06:10 +00:00
include ( CheckCXXCompilerFlag )
2019-09-27 07:59:53 -07:00
include ( GNUInstallDirs )
2023-03-22 19:14:49 -05:00
include ( CTest )
2011-03-17 02:06:10 +00:00
2019-09-27 07:59:53 -07:00
option ( YAML_CPP_BUILD_CONTRIB "Enable yaml-cpp contrib in library" ON )
2017-11-09 15:30:25 -05:00
option ( YAML_CPP_BUILD_TOOLS "Enable parse tools" ON )
2019-10-13 13:50:40 -07:00
option ( YAML_BUILD_SHARED_LIBS "Build yaml-cpp shared library" ${ BUILD_SHARED_LIBS } )
2022-04-03 23:57:28 +02:00
option ( YAML_CPP_INSTALL "Enable generation of yaml-cpp install targets" ${ YAML_CPP_MAIN_PROJECT } )
2022-04-01 11:36:43 +08:00
option ( YAML_CPP_FORMAT_SOURCE "Format source" ON )
2015-03-31 12:32:09 +01:00
option ( YAML_CPP_SUPPORT_MERGE_KEYS "Support YAML merge keys ('<<') in yaml-cpp's executable targets. Use '#define YAML_CPP_SUPPORT_MERGE_KEYS' instead when linking from another project." OFF )
2019-09-27 07:59:53 -07:00
cmake_dependent_option ( YAML_CPP_BUILD_TESTS
2023-03-04 21:06:01 +01:00
"Enable yaml-cpp tests" OFF
"BUILD_TESTING;YAML_CPP_MAIN_PROJECT" OFF )
2019-09-27 07:59:53 -07:00
cmake_dependent_option ( YAML_MSVC_SHARED_RT
"MSVC: Build yaml-cpp with shared runtime libs (/MD)" ON
2022-03-31 20:13:19 -07:00
"CMAKE_SYSTEM_NAME MATCHES Windows" OFF )
2015-03-31 12:32:09 +01:00
2023-03-04 21:06:01 +01:00
if ( YAML_CPP_FORMAT_SOURCE )
find_program ( YAML_CPP_CLANG_FORMAT_EXE NAMES clang-format )
endif ()
2019-09-27 07:59:53 -07:00
if ( YAML_BUILD_SHARED_LIBS )
set ( yaml-cpp-type SHARED )
2019-10-31 15:35:12 +01:00
set ( yaml-cpp-label-postfix "shared" )
2021-11-23 07:40:08 +03:00
else ()
set ( yaml-cpp-type STATIC )
set ( yaml-cpp-label-postfix "static" )
2019-09-10 13:00:07 -04:00
endif ()
2011-03-17 02:06:10 +00:00
2019-09-27 07:59:53 -07:00
set ( build-shared $< BOOL:${YAML_BUILD_SHARED_LIBS} > )
set ( build-windows-dll $< AND:$<BOOL:${CMAKE_HOST_WIN32} > , ${ build-shared } > )
set ( not-msvc $< NOT:$<CXX_COMPILER_ID:MSVC > > )
2020-05-24 19:55:52 +02:00
set ( msvc-shared_rt $< BOOL:${YAML_MSVC_SHARED_RT} > )
2019-09-27 07:59:53 -07:00
if ( NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY )
set ( CMAKE_MSVC_RUNTIME_LIBRARY
2020-05-24 19:55:52 +02:00
MultiThreaded $< $<CONFIG:Debug > :Debug> $< ${msvc-shared_rt}:DLL > )
2019-09-10 13:00:07 -04:00
endif ()
2011-03-17 02:06:10 +00:00
2019-09-27 07:59:53 -07:00
set ( contrib-pattern "src/contrib/*.cpp" )
set ( src-pattern "src/*.cpp" )
if ( CMAKE_VERSION VERSION_GREATER 3.12 )
list ( INSERT contrib-pattern 0 CONFIGURE_DEPENDS )
list ( INSERT src-pattern 0 CONFIGURE_DEPENDS )
2011-03-17 02:06:10 +00:00
endif ()
2019-09-27 07:59:53 -07:00
file ( GLOB yaml-cpp-contrib-sources ${ contrib-pattern } )
file ( GLOB yaml-cpp-sources ${ src-pattern } )
2014-03-24 23:34:26 -05:00
2019-09-27 07:59:53 -07:00
set ( msvc-rt $< TARGET_PROPERTY:MSVC_RUNTIME_LIBRARY > )
set ( msvc-rt-mtd-static $< STREQUAL:${msvc-rt},MultiThreadedDebug > )
set ( msvc-rt-mt-static $< STREQUAL:${msvc-rt},MultiThreaded > )
set ( msvc-rt-mtd-dll $< STREQUAL:${msvc-rt},MultiThreadedDebugDLL > )
set ( msvc-rt-mt-dll $< STREQUAL:${msvc-rt},MultiThreadedDLL > )
set ( backport-msvc-runtime $< VERSION_LESS:${CMAKE_VERSION},3.15 > )
2019-10-04 22:46:18 +03:00
add_library ( yaml-cpp ${ yaml-cpp-type } "" )
2019-11-30 00:02:21 +01:00
add_library ( yaml-cpp::yaml-cpp ALIAS yaml-cpp )
2019-09-27 07:59:53 -07:00
set_property ( TARGET yaml-cpp
PROPERTY
MSVC_RUNTIME_LIBRARY ${ CMAKE_MSVC_RUNTIME_LIBRARY } )
set_property ( TARGET yaml-cpp
PROPERTY
CXX_STANDARD_REQUIRED ON )
2022-01-27 23:03:10 +00:00
if ( NOT YAML_BUILD_SHARED_LIBS )
set_property ( TARGET yaml-cpp PROPERTY POSITION_INDEPENDENT_CODE ON )
endif ()
2019-09-27 07:59:53 -07:00
target_include_directories ( yaml-cpp
PUBLIC
$< BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include >
$< INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >
PRIVATE
$< BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src > )
if ( NOT DEFINED CMAKE_CXX_STANDARD )
set_target_properties ( yaml-cpp
PROPERTIES
CXX_STANDARD 11 )
2011-03-17 02:06:10 +00:00
endif ()
2022-09-20 08:22:11 +03:00
if ( YAML_CPP_MAIN_PROJECT )
target_compile_options ( yaml-cpp
PRIVATE
$< ${not-msvc}:-Wall -Wextra -Wshadow -Weffc++ -Wno-long-long >
$< ${not-msvc}:-pedantic -pedantic-errors > )
endif ()
2019-09-27 07:59:53 -07:00
target_compile_options ( yaml-cpp
PRIVATE
$< $<AND:${backport-msvc-runtime},${msvc-rt-mtd-static} > :-MTd>
$< $<AND:${backport-msvc-runtime},${msvc-rt-mt-static} > :-MT>
$< $<AND:${backport-msvc-runtime},${msvc-rt-mtd-dll} > :-MDd>
$< $<AND:${backport-msvc-runtime},${msvc-rt-mt-dll} > :-MD>
2011-03-17 02:06:10 +00:00
2019-09-27 07:59:53 -07:00
# /wd4127 = disable warning C4127 "conditional expression is constant"
# http://msdn.microsoft.com/en-us/library/6t66728h.aspx
# /wd4355 = disable warning C4355 "'this' : used in base member initializer list
# http://msdn.microsoft.com/en-us/library/3c594ae3.aspx
$< $<CXX_COMPILER_ID:MSVC > :/W3 /wd4127 /wd4355> )
2014-03-24 23:34:26 -05:00
2019-09-27 07:59:53 -07:00
target_compile_definitions ( yaml-cpp
2022-09-20 15:24:35 +10:00
PUBLIC
$< $<NOT:$<BOOL:${YAML_BUILD_SHARED_LIBS} > >:YAML_CPP_STATIC_DEFINE>
2019-09-27 07:59:53 -07:00
PRIVATE
$< ${build-windows-dll}:${PROJECT_NAME}_DLL >
2015-03-31 12:32:09 +01:00
$< $<NOT:$<BOOL:${YAML_CPP_BUILD_CONTRIB} > >:YAML_CPP_NO_CONTRIB>
$< $<BOOL:${YAML_CPP_SUPPORT_MERGE_KEYS} > :YAML_CPP_SUPPORT_MERGE_KEYS> )
2011-03-17 02:06:10 +00:00
2019-09-27 07:59:53 -07:00
target_sources ( yaml-cpp
PRIVATE
$< $<BOOL:${YAML_CPP_BUILD_CONTRIB} > : ${ yaml-cpp-contrib-sources } >
${ yaml-cpp-sources } )
2020-05-24 19:55:52 +02:00
2020-04-29 22:58:05 +08:00
if ( NOT DEFINED CMAKE_DEBUG_POSTFIX )
set ( CMAKE_DEBUG_POSTFIX "d" )
endif ()
2018-05-05 18:03:13 +02:00
2019-09-10 10:51:13 -04:00
set_target_properties ( yaml-cpp PROPERTIES
2019-09-27 07:59:53 -07:00
VERSION "${PROJECT_VERSION}"
SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
2019-10-31 15:35:12 +01:00
PROJECT_LABEL "yaml-cpp ${yaml-cpp-label-postfix}"
2020-04-29 22:58:05 +08:00
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}" )
2019-09-10 10:51:13 -04:00
2022-09-20 07:17:13 +02:00
set ( CONFIG_EXPORT_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/yaml-cpp" )
2022-04-01 05:26:47 +02:00
set ( EXPORT_TARGETS yaml-cpp )
2019-09-27 07:59:53 -07:00
configure_package_config_file (
"${PROJECT_SOURCE_DIR}/yaml-cpp-config.cmake.in"
"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
2022-04-01 05:26:47 +02:00
INSTALL_DESTINATION "${CONFIG_EXPORT_DIR}"
2022-09-20 08:26:59 +03:00
PATH_VARS CMAKE_INSTALL_INCLUDEDIR CMAKE_INSTALL_LIBDIR CONFIG_EXPORT_DIR YAML_BUILD_SHARED_LIBS )
2022-04-01 05:26:47 +02:00
unset ( EXPORT_TARGETS )
2011-03-17 02:06:10 +00:00
2019-09-27 07:59:53 -07:00
write_basic_package_version_file (
"${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
COMPATIBILITY AnyNewerVersion )
2011-03-17 02:06:10 +00:00
2019-09-27 07:59:53 -07:00
configure_file ( yaml-cpp.pc.in yaml-cpp.pc @ONLY )
2015-01-24 11:21:26 -06:00
2018-11-18 19:27:15 +02:00
if ( YAML_CPP_INSTALL )
2022-04-01 05:26:47 +02:00
install ( TARGETS yaml-cpp
2019-09-27 07:59:53 -07:00
EXPORT yaml-cpp-targets
RUNTIME DESTINATION ${ CMAKE_INSTALL_BINDIR }
LIBRARY DESTINATION ${ CMAKE_INSTALL_LIBDIR }
ARCHIVE DESTINATION ${ CMAKE_INSTALL_LIBDIR } )
2022-04-01 05:26:47 +02:00
install ( DIRECTORY ${ PROJECT_SOURCE_DIR } /include/
2019-09-27 07:59:53 -07:00
DESTINATION ${ CMAKE_INSTALL_INCLUDEDIR }
2022-04-01 05:26:47 +02:00
FILES_MATCHING PATTERN "*.h" )
2019-09-27 07:59:53 -07:00
install ( EXPORT yaml-cpp-targets
2023-06-28 10:37:09 -06:00
NAMESPACE yaml-cpp::
2022-04-01 05:26:47 +02:00
DESTINATION "${CONFIG_EXPORT_DIR}" )
install ( FILES
"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
"${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
DESTINATION "${CONFIG_EXPORT_DIR}" )
2019-09-27 07:59:53 -07:00
install ( FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc"
2022-09-20 07:17:13 +02:00
DESTINATION ${ CMAKE_INSTALL_LIBDIR } /pkgconfig )
2011-03-17 02:06:10 +00:00
endif ()
2022-04-01 05:26:47 +02:00
unset ( CONFIG_EXPORT_DIR )
2011-03-17 02:06:10 +00:00
2017-11-09 15:30:25 -05:00
if ( YAML_CPP_BUILD_TESTS )
2022-04-01 05:26:47 +02:00
add_subdirectory ( test )
2017-11-09 15:30:25 -05:00
endif ()
2019-09-27 07:59:53 -07:00
2017-11-09 15:30:25 -05:00
if ( YAML_CPP_BUILD_TOOLS )
2022-04-01 05:26:47 +02:00
add_subdirectory ( util )
2011-03-17 02:06:10 +00:00
endif ()
2014-03-25 00:02:16 -05:00
2022-04-01 11:36:43 +08:00
if ( YAML_CPP_FORMAT_SOURCE AND YAML_CPP_CLANG_FORMAT_EXE )
2019-09-27 07:59:53 -07:00
add_custom_target ( format
COMMAND clang-format --style=file -i $< TARGET_PROPERTY:yaml-cpp,SOURCES >
2020-04-29 10:40:33 -04:00
COMMAND_EXPAND_LISTS
2019-09-27 07:59:53 -07:00
COMMENT "Running clang-format"
VERBATIM )
endif ()
2022-09-20 13:03:40 +08:00
# uninstall target
if ( NOT TARGET uninstall )
configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY )
add_custom_target ( uninstall
COMMAND ${ CMAKE_COMMAND } -P ${ CMAKE_CURRENT_BINARY_DIR } /cmake_uninstall.cmake )
endif ()