From 5e9cb0128d1b600ff74505429269fd4dff367f4a Mon Sep 17 00:00:00 2001 From: Isabella Muerte <63051+slurps-mad-rips@users.noreply.github.com> Date: Fri, 27 Sep 2019 07:59:53 -0700 Subject: [PATCH] Refactor CMake to use more modern paradigms (#741) Remove 2.6-isms Remove 2.8-isms Bump CMake minimum version to 3.4 Disable some options when used as a subdirectory Use `CONFIGURE_DEPENDS` with `file(GLOB)` when possible Backport CMake 3.15's MSVC_RUNTIME_LIBRARY setting. Set all compile options as generator expressions. Set all find-package files to be installed to the correct file. Remove `export(PACKAGE)`, as this has been deprecated. Remove fat binary support Remove manual setting of iPhone settings. These should be set by parent projects. Remove use of ExternalProject for a local use Conditionally remove format target unless clang-format is found --- .travis.yml | 6 +- CMakeLists.txt | 438 ++++++++-------------------- test/CMakeLists.txt | 100 +++---- test/ostream_wrapper_test.cpp | 2 +- util/CMakeLists.txt | 44 +-- yaml-cpp-config-version.cmake.in | 11 - yaml-cpp.pc.cmake => yaml-cpp.pc.in | 4 +- 7 files changed, 189 insertions(+), 416 deletions(-) delete mode 100644 yaml-cpp-config-version.cmake.in rename yaml-cpp.pc.cmake => yaml-cpp.pc.in (68%) diff --git a/.travis.yml b/.travis.yml index d0b6a04ef..6a7ab8515 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,10 +18,10 @@ before_script: - mkdir build - cd build - cmake .. + - cd .. script: - - make - - test/run-tests - + - cmake --build build + - cmake --build build --target test matrix: exclude: - os: linux diff --git a/CMakeLists.txt b/CMakeLists.txt index 4732a453f..b9759b0fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,367 +1,165 @@ -### -### CMake settings -### -# see http://www.cmake.org/Wiki/CMake_Policies -cmake_minimum_required(VERSION 3.1) +# 3.5 is actually available almost everywhere, but this a good minimum +cmake_minimum_required(VERSION 3.4) +project(YAML_CPP VERSION 0.6.3 LANGUAGES CXX) +include(CMakePackageConfigHelpers) +include(CMakeDependentOption) include(CheckCXXCompilerFlag) +include(GNUInstallDirs) +include(CTest) -### -### Project settings -### -project(YAML_CPP VERSION 0.6.3) +find_program(YAML_CPP_CLANG_FORMAT_EXE NAMES clang-format) -### -### Project options -### -## Project stuff -option(YAML_CPP_BUILD_TESTS "Enable testing" ON) +option(YAML_CPP_BUILD_CONTRIB "Enable yaml-cpp contrib in library" ON) option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON) -option(YAML_CPP_BUILD_CONTRIB "Enable contrib stuff in library" ON) -option(YAML_CPP_INSTALL "Enable generation of install target" ON) -## Build options -# --> General -# see http://www.cmake.org/cmake/help/cmake2.6docs.html#variable:BUILD_SHARED_LIBS -# http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_library -option(YAML_BUILD_SHARED_LIBS "Build Shared Libraries" OFF) +cmake_dependent_option(YAML_CPP_BUILD_TESTS + "Enable yaml-cpp tests" ON + "BUILD_TESTING;CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF) +cmake_dependent_option(YAML_CPP_INSTALL + "Enable generation of yaml-cpp install targets" ON + "CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF) +cmake_dependent_option(YAML_BUILD_SHARED_LIBS + "Build yaml-cpp shared library" OFF + "BUILD_SHARED_LIBS" OFF) -# --> Apple -if(APPLE) - option(YAML_APPLE_UNIVERSAL_BIN "Apple: Build universal binary" OFF) -endif() +cmake_dependent_option(YAML_MSVC_SHARED_RT + "MSVC: Build yaml-cpp with shared runtime libs (/MD)" ON + "MSVC" OFF) +cmake_dependent_option(YAML_APPLE_UNIVERSAL_BIN + "Apple: Build yaml-cpp universal binary" OFF + "APPLE" OFF) -# --> Microsoft Visual C++ -# see http://msdn.microsoft.com/en-us/library/aa278396(v=VS.60).aspx -# http://msdn.microsoft.com/en-us/library/2kzt1wy3(v=VS.71).aspx -if(MSVC) - option(YAML_MSVC_SHARED_RT "MSVC: Build with shared runtime libs (/MD)" ON) - option(YAML_MSVC_STHREADED_RT "MSVC: Build with single-threaded static runtime libs (/ML until VS .NET 2003)" OFF) +set(yaml-cpp-type STATIC) +if (YAML_BUILD_SHARED_LIBS) + set(yaml-cpp-type SHARED) endif() -### -### Sources, headers, directories and libs -### - -# From http://www.cmake.org/pipermail/cmake/2010-March/035992.html: -# function to collect all the sources from sub-directories -# into a single list -function(add_sources) - get_property(is_defined GLOBAL PROPERTY SRCS_LIST DEFINED) - if(NOT is_defined) - define_property(GLOBAL PROPERTY SRCS_LIST - BRIEF_DOCS "List of source files" - FULL_DOCS "List of all source files in the entire project") - endif() - # make absolute paths - set(SRCS) - foreach(s IN LISTS ARGN) - if(NOT IS_ABSOLUTE "${s}") - get_filename_component(s "${s}" ABSOLUTE) - endif() - list(APPEND SRCS "${s}") - endforeach() - # append to global list - set_property(GLOBAL APPEND PROPERTY SRCS_LIST "${SRCS}") -endfunction(add_sources) - -set(header_directory "include/yaml-cpp/") +set(build-shared $) +set(build-windows-dll $,${build-shared}>) +set(not-msvc $>) -file(GLOB sources "src/[a-zA-Z]*.cpp") -file(GLOB_RECURSE public_headers "include/yaml-cpp/[a-zA-Z]*.h") -file(GLOB private_headers "src/[a-zA-Z]*.h") - -if(YAML_CPP_BUILD_CONTRIB) - file(GLOB contrib_sources "src/contrib/[a-zA-Z]*.cpp") - file(GLOB contrib_public_headers "include/yaml-cpp/contrib/[a-zA-Z]*.h") - file(GLOB contrib_private_headers "src/contrib/[a-zA-Z]*.h") -else() - add_definitions(-DYAML_CPP_NO_CONTRIB) +if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) + set(CMAKE_MSVC_RUNTIME_LIBRARY + MultiThreaded$<$:Debug>$<${build-shared}:DLL>) endif() -set(library_sources - ${sources} - ${public_headers} - ${private_headers} - ${contrib_sources} - ${contrib_public_headers} - ${contrib_private_headers} -) -add_sources(${library_sources}) - -if(VERBOSE) - message(STATUS "sources: ${sources}") - message(STATUS "public_headers: ${public_headers}") - message(STATUS "private_headers: ${private_headers}") - message(STATUS "contrib_sources: ${contrib_sources}") - message(STATUS "contrib_public_headers: ${contrib_public_headers}") - message(STATUS "contrib_private_headers: ${contrib_private_headers}") +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) endif() -if (CMAKE_VERSION VERSION_LESS 2.8.12) - include_directories(${YAML_CPP_SOURCE_DIR}/src) - include_directories(${YAML_CPP_SOURCE_DIR}/include) -endif() +file(GLOB yaml-cpp-contrib-sources ${contrib-pattern}) +file(GLOB yaml-cpp-sources ${src-pattern}) +set(msvc-rt $) -### -### General compilation settings -### -set(yaml_c_flags ${CMAKE_C_FLAGS}) -set(yaml_cxx_flags ${CMAKE_CXX_FLAGS}) +set(msvc-rt-mtd-static $) +set(msvc-rt-mt-static $) -if(YAML_BUILD_SHARED_LIBS) - set(LABEL_SUFFIX "shared") -else() - set(LABEL_SUFFIX "static") -endif() +set(msvc-rt-mtd-dll $) +set(msvc-rt-mt-dll $) -if(APPLE) - if(YAML_APPLE_UNIVERSAL_BIN) - set(CMAKE_OSX_ARCHITECTURES ppc;i386) - endif() -endif() +set(backport-msvc-runtime $) -if(IPHONE) - set(CMAKE_OSX_SYSROOT "iphoneos4.2") - set(CMAKE_OSX_ARCHITECTURES "armv6;armv7") -endif() - -if(WIN32) - if(YAML_BUILD_SHARED_LIBS) - add_definitions(-D${PROJECT_NAME}_DLL) # use or build Windows DLL - endif() - if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) - set(CMAKE_INSTALL_PREFIX "C:/") - endif() -endif() +add_library(yaml-cpp ${yaml-cpp-type}) +add_library(yaml::yaml ALIAS yaml-cpp) -# GCC or Clang or Intel Compiler specialities -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR - (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC") OR - CMAKE_CXX_COMPILER_ID MATCHES "Intel") +set_property(TARGET yaml-cpp + PROPERTY + MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY}) +set_property(TARGET yaml-cpp + PROPERTY + CXX_STANDARD_REQUIRED ON) - ### General stuff - if(WIN32) - set(CMAKE_SHARED_LIBRARY_PREFIX "") # DLLs do not have a "lib" prefix - set(CMAKE_IMPORT_LIBRARY_PREFIX "") # same for DLL import libs - set(CMAKE_LINK_DEF_FILE_FLAG "") # CMake workaround (2.8.3) - endif() +target_include_directories(yaml-cpp + PUBLIC + $ + $ + PRIVATE + $) - ### Project stuff - if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) - endif() - # - set(GCC_EXTRA_OPTIONS "") - # - if(YAML_BUILD_SHARED_LIBS) - set(GCC_EXTRA_OPTIONS "${GCC_EXTRA_OPTIONS} -fPIC") - endif() - # - set(FLAG_TESTED "-Wextra -Wshadow -Weffc++ -pedantic -pedantic-errors") - check_cxx_compiler_flag(${FLAG_TESTED} FLAG_WEXTRA) - if(FLAG_WEXTRA) - set(GCC_EXTRA_OPTIONS "${GCC_EXTRA_OPTIONS} ${FLAG_TESTED}") - endif() - # - set(yaml_cxx_flags "-Wall ${GCC_EXTRA_OPTIONS} -pedantic -Wno-long-long ${yaml_cxx_flags}") - - ### Make specific - if(${CMAKE_BUILD_TOOL} MATCHES make OR ${CMAKE_BUILD_TOOL} MATCHES gmake) - add_custom_target(debuggable ${CMAKE_MAKE_PROGRAM} clean - COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR} - COMMENT "Adjusting settings for debug compilation" - VERBATIM) - add_custom_target(releasable ${CMAKE_MAKE_PROGRAM} clean - COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR} - COMMENT "Adjusting settings for release compilation" - VERBATIM) - endif() -endif() - -# Microsoft VisualC++ specialities -if(MSVC) - ### General stuff - # a) Change MSVC runtime library settings (/MD[d], /MT[d], /ML[d] (single-threaded until VS 2003)) - # plus set lib suffix for later use and project label accordingly - # see http://msdn.microsoft.com/en-us/library/aa278396(v=VS.60).aspx - # http://msdn.microsoft.com/en-us/library/2kzt1wy3(v=VS.71).aspx - set(LIB_RT_SUFFIX "md") # CMake defaults to /MD for MSVC - set(LIB_RT_OPTION "/MD") - # - if(NOT YAML_MSVC_SHARED_RT) # User wants to have static runtime libraries (/MT, /ML) - if(YAML_MSVC_STHREADED_RT) # User wants to have old single-threaded static runtime libraries - set(LIB_RT_SUFFIX "ml") - set(LIB_RT_OPTION "/ML") - if(NOT ${MSVC_VERSION} LESS 1400) - message(FATAL_ERROR "Single-threaded static runtime libraries (/ML) only available until VS .NET 2003 (7.1).") - endif() - else() - set(LIB_RT_SUFFIX "mt") - set(LIB_RT_OPTION "/MT") - endif() - - # correct linker options - foreach(flag_var CMAKE_C_FLAGS CMAKE_CXX_FLAGS) - foreach(config_name "" DEBUG RELEASE MINSIZEREL RELWITHDEBINFO) - set(var_name "${flag_var}") - if(NOT "${config_name}" STREQUAL "") - set(var_name "${var_name}_${config_name}") - endif() - string(REPLACE "/MD" "${LIB_RT_OPTION}" ${var_name} "${${var_name}}") - set(${var_name} "${${var_name}}" CACHE STRING "" FORCE) - endforeach() - endforeach() - endif() - # - set(LABEL_SUFFIX "${LABEL_SUFFIX} ${LIB_RT_SUFFIX}") - - # b) Change prefix for static libraries - set(CMAKE_STATIC_LIBRARY_PREFIX "lib") # to distinguish static libraries from DLL import libs - - # c) Correct suffixes for static libraries - if(NOT YAML_BUILD_SHARED_LIBS) - ### General stuff - set(LIB_TARGET_SUFFIX "${LIB_SUFFIX}${LIB_RT_SUFFIX}") - endif() - - ### Project stuff - # /W3 = set warning level; see http://msdn.microsoft.com/en-us/library/thxezb7y.aspx - # /wd4127 = disable warning C4127 "conditional expression is constant"; see 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 - set(yaml_cxx_flags "/W3 /wd4127 /wd4355 ${yaml_cxx_flags}") +if (NOT DEFINED CMAKE_CXX_STANDARD) + set_target_properties(yaml-cpp + PROPERTIES + CXX_STANDARD 11) endif() +target_compile_options(yaml-cpp + PRIVATE + $<${not-msvc}:-Wall -Wextra -Wshadow -Weffc++ -Wno-long-long> + $<${not-msvc}:-pedantic -pedantic-errors> -### -### General install settings -### -set(INCLUDE_INSTALL_ROOT_DIR include) - -set(INCLUDE_INSTALL_DIR ${INCLUDE_INSTALL_ROOT_DIR}/yaml-cpp) -set(LIB_INSTALL_DIR "lib${LIB_SUFFIX}") - -set(_INSTALL_DESTINATIONS - RUNTIME DESTINATION bin - LIBRARY DESTINATION ${LIB_INSTALL_DIR} - ARCHIVE DESTINATION ${LIB_INSTALL_DIR} -) - + $<$:-MTd> + $<$:-MT> + $<$:-MDd> + $<$:-MD> -### -### Library -### -if(YAML_BUILD_SHARED_LIBS) - add_library(yaml-cpp SHARED ${library_sources}) -else() - add_library(yaml-cpp STATIC ${library_sources}) -endif() + # /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 + $<$:/W3 /wd4127 /wd4355>) -if (NOT CMAKE_VERSION VERSION_LESS 2.8.12) - target_include_directories(yaml-cpp - PUBLIC $ - $ - PRIVATE $) -endif() +target_compile_definitions(yaml-cpp + PRIVATE + $<${build-windows-dll}:${PROJECT_NAME}_DLL> + $<$>:YAML_CPP_NO_CONTRIB>) -set_target_properties(yaml-cpp PROPERTIES - CXX_STANDARD 11 - CXX_STANDARD_REQUIRED ON -) +target_sources(yaml-cpp + PRIVATE + $<$:${yaml-cpp-contrib-sources}> + ${yaml-cpp-sources}) set_target_properties(yaml-cpp PROPERTIES - COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags}" -) + VERSION "${PROJECT_VERSION}" + SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}" + PROJECT_LABEL "yaml-cpp $" + DEBUG_POSTFIX $<$,$>:d>) -set_target_properties(yaml-cpp PROPERTIES - VERSION "${YAML_CPP_VERSION}" - SOVERSION "${YAML_CPP_VERSION_MAJOR}.${YAML_CPP_VERSION_MINOR}" - PROJECT_LABEL "yaml-cpp ${LABEL_SUFFIX}" -) +configure_package_config_file( + "${PROJECT_SOURCE_DIR}/yaml-cpp-config.cmake.in" + "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp") -if(IPHONE) - set_target_properties(yaml-cpp PROPERTIES - XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "3.0" - ) -endif() +write_basic_package_version_file( + "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake" + COMPATIBILITY AnyNewerVersion) -if(MSVC) - if(NOT YAML_BUILD_SHARED_LIBS) - # correct library names - set_target_properties(yaml-cpp PROPERTIES - DEBUG_POSTFIX "${LIB_TARGET_SUFFIX}d" - RELEASE_POSTFIX "${LIB_TARGET_SUFFIX}" - MINSIZEREL_POSTFIX "${LIB_TARGET_SUFFIX}" - RELWITHDEBINFO_POSTFIX "${LIB_TARGET_SUFFIX}" - ) - endif() -endif() - -if (YAML_CPP_INSTALL) - install(TARGETS yaml-cpp EXPORT yaml-cpp-targets ${_INSTALL_DESTINATIONS}) - install( - DIRECTORY ${header_directory} - DESTINATION ${INCLUDE_INSTALL_DIR} - FILES_MATCHING PATTERN "*.h" - ) -endif() - -export( - TARGETS yaml-cpp - FILE "${PROJECT_BINARY_DIR}/yaml-cpp-targets.cmake") -export(PACKAGE yaml-cpp) -set(EXPORT_TARGETS yaml-cpp CACHE INTERNAL "export targets") - -set(CONFIG_INCLUDE_DIRS "${YAML_CPP_SOURCE_DIR}/include") -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/yaml-cpp-config.cmake.in - "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake" @ONLY) - -if(WIN32 AND NOT CYGWIN) - set(INSTALL_CMAKE_DIR CMake) -else() - set(INSTALL_CMAKE_DIR ${LIB_INSTALL_DIR}/cmake/yaml-cpp) -endif() - - -file(RELATIVE_PATH REL_INCLUDE_DIR "${CMAKE_INSTALL_PREFIX}/${INSTALL_CMAKE_DIR}" "${CMAKE_INSTALL_PREFIX}/${INCLUDE_INSTALL_ROOT_DIR}") -set(CONFIG_INCLUDE_DIRS "\${YAML_CPP_CMAKE_DIR}/${REL_INCLUDE_DIR}") -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/yaml-cpp-config.cmake.in - "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/yaml-cpp-config.cmake" @ONLY) - -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/yaml-cpp-config-version.cmake.in - "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake" @ONLY) +configure_file(yaml-cpp.pc.in yaml-cpp.pc @ONLY) if (YAML_CPP_INSTALL) + install(TARGETS yaml-cpp + EXPORT yaml-cpp-targets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + FILES_MATCHING PATTERN "*.h") + install(EXPORT yaml-cpp-targets + DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp") install(FILES - "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/yaml-cpp-config.cmake" + "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake" "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake" - DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev) - install(EXPORT yaml-cpp-targets DESTINATION ${INSTALL_CMAKE_DIR}) - - if(UNIX) - set(PC_FILE ${CMAKE_BINARY_DIR}/yaml-cpp.pc) - configure_file("yaml-cpp.pc.cmake" ${PC_FILE} @ONLY) - install(FILES ${PC_FILE} DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) - endif() - + DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp") + install(FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc" + DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig) endif() - -### -### Extras -### if(YAML_CPP_BUILD_TESTS) - enable_testing() add_subdirectory(test) endif() + if(YAML_CPP_BUILD_TOOLS) add_subdirectory(util) endif() -### Formatting -get_property(all_sources GLOBAL PROPERTY SRCS_LIST) -add_custom_target(format - COMMAND clang-format --style=file -i ${all_sources} - COMMENT "Running clang-format" - VERBATIM) +if (YAML_CPP_CLANG_FORMAT_EXE) + add_custom_target(format + COMMAND clang-format --style=file -i $ + COMMENT "Running clang-format" + VERBATIM) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0a669d5cc..33015844a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,69 +1,49 @@ -include(ExternalProject) +find_package(Threads REQUIRED) -if(MSVC) - # MS Visual Studio expects lib prefix on static libraries, - # but CMake compiles them without prefix - # See https://gitlab.kitware.com/cmake/cmake/issues/17338 - set(CMAKE_STATIC_LIBRARY_PREFIX "") -endif() - -ExternalProject_Add( - googletest_project - SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.8.0" - INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/prefix" - CMAKE_ARGS - -DCMAKE_INSTALL_PREFIX:PATH= - -DBUILD_GMOCK=ON - -Dgtest_force_shared_crt=ON -) - -add_library(gmock UNKNOWN IMPORTED) -set_target_properties(gmock PROPERTIES - IMPORTED_LOCATION - ${PROJECT_BINARY_DIR}/test/prefix/lib/${CMAKE_STATIC_LIBRARY_PREFIX}gmock${CMAKE_STATIC_LIBRARY_SUFFIX} -) +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +set(BUILD_MOCK ON CACHE BOOL "" FORCE) +set(CMAKE_POLICY_DEFAULT_CMP0048 NEW) -find_package(Threads) +add_subdirectory( + "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.8.0" + "${CMAKE_CURRENT_BINARY_DIR}/prefix") -include_directories(SYSTEM "${PROJECT_BINARY_DIR}/test/prefix/include") +include_directories(SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.8.0/googletest/include") -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR - CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(yaml_test_flags "-Wno-variadic-macros -Wno-sign-compare") - - if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(yaml_test_flags "${yaml_test_flags} -Wno-c99-extensions") - endif() +set(test-new-api-pattern "new-api/*.cpp") +set(test-source-pattern "*.cpp" "integration/*.cpp" "node/*.cpp") +if (CMAKE_VERSION VERSION_GREATER 3.11) + list(INSERT test-new-api-pattern 0 CONFIGURE_DEPENDS) + list(INSERT test-source-pattern 0 CONFIGURE_DEPENDS) endif() -file(GLOB test_headers [a-z_]*.h) -file(GLOB test_sources [a-z_]*.cpp integration/[a-z_]*.cpp node/[a-z_]*.cpp) -file(GLOB test_new_api_sources new-api/[a-z]*.cpp) - -list(APPEND test_sources ${test_new_api_sources}) -add_sources(${test_sources} ${test_headers}) - -include_directories(${YAML_CPP_SOURCE_DIR}/src) -include_directories(${YAML_CPP_SOURCE_DIR}/test) - -add_executable(run-tests - ${test_sources} - ${test_headers} -) - -set_target_properties(run-tests PROPERTIES - CXX_STANDARD 11 - CXX_STANDARD_REQUIRED ON -) +file(GLOB test-new-api-sources ${test-new-api-pattern}) +file(GLOB test-sources ${test-source-pattern}) + +add_executable(yaml-cpp-tests) +target_sources(yaml-cpp-tests + PRIVATE + ${test-new-api-sources} + ${test-sources}) +target_include_directories(yaml-cpp-tests + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/integration + ${CMAKE_CURRENT_SOURCE_DIR} + ${PROJECT_SOURCE_DIR}/src) +target_compile_options(yaml-cpp-tests + PRIVATE + $<$:-Wno-c99-extensions -Wno-variadic-macros -Wno-sign-compare> + $<$:-Wno-variadic-macros -Wno-sign-compare>) +target_link_libraries(yaml-cpp-tests + PRIVATE + Threads::Threads + yaml-cpp + gmock) -add_dependencies(run-tests googletest_project) +set_property(TARGET yaml-cpp-tests PROPERTY CXX_STANDARD_REQUIRED ON) +if (NOT DEFINED CMAKE_CXX_STANDARD) + set_target_properties(yaml-cpp-tests PROPERTIES CXX_STANDARD 11) +endif() -set_target_properties(run-tests PROPERTIES - COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags} ${yaml_test_flags}" -) -target_link_libraries(run-tests - yaml-cpp - gmock - ${CMAKE_THREAD_LIBS_INIT}) -add_test(yaml-test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/run-tests) +add_test(yaml-cpp::test yaml-cpp-tests) diff --git a/test/ostream_wrapper_test.cpp b/test/ostream_wrapper_test.cpp index cdc1f0508..ff4f635a5 100644 --- a/test/ostream_wrapper_test.cpp +++ b/test/ostream_wrapper_test.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include "gtest/gtest.h" #include "yaml-cpp/ostream_wrapper.h" diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 09dafa24c..87ea4f91e 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -1,26 +1,32 @@ -cmake_minimum_required(VERSION 3.5) +add_executable(yaml-cpp-sandbox sandbox.cpp) +add_executable(yaml-cpp-parse parse.cpp) +add_executable(yaml-cpp-read read.cpp) -add_sources(parse.cpp) -add_executable(parse parse.cpp) -set_target_properties(parse PROPERTIES - CXX_STANDARD 11 +target_link_libraries(yaml-cpp-sandbox PRIVATE yaml-cpp) +target_link_libraries(yaml-cpp-parse PRIVATE yaml-cpp) +target_link_libraries(yaml-cpp-read PRIVATE yaml-cpp) + +set_property(TARGET yaml-cpp-sandbox PROPERTY OUTPUT_NAME sandbox) +set_property(TARGET yaml-cpp-parse PROPERTY OUTPUT_NAME parse) +set_property(TARGET yaml-cpp-read PROPERTY OUTPUT_NAME read) + +set_target_properties(yaml-cpp-sandbox + PROPERTIES CXX_STANDARD_REQUIRED ON -) -target_link_libraries(parse yaml-cpp) + OUTPUT_NAME sandbox) -add_sources(sandbox.cpp) -add_executable(sandbox sandbox.cpp) -set_target_properties(sandbox PROPERTIES - CXX_STANDARD 11 +set_target_properties(yaml-cpp-parse + PROPERTIES CXX_STANDARD_REQUIRED ON -) -target_link_libraries(sandbox yaml-cpp) + OUTPUT_NAME parse) -add_sources(read.cpp) -add_executable(read read.cpp) -set_target_properties(read PROPERTIES - CXX_STANDARD 11 +set_target_properties(yaml-cpp-read + PROPERTIES CXX_STANDARD_REQUIRED ON -) -target_link_libraries(read yaml-cpp) + OUTPUT_NAME read) +if (NOT DEFINED CMAKE_CXX_STANDARD) + set_target_properties(yaml-cpp-sandbox yaml-cpp-parse yaml-cpp-read + PROPERTIES + CXX_STANDARD 11) +endif() diff --git a/yaml-cpp-config-version.cmake.in b/yaml-cpp-config-version.cmake.in deleted file mode 100644 index 80b9c79ad..000000000 --- a/yaml-cpp-config-version.cmake.in +++ /dev/null @@ -1,11 +0,0 @@ -set(PACKAGE_VERSION "@YAML_CPP_VERSION@") - -# Check whether the requested PACKAGE_FIND_VERSION is compatible -if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") - set(PACKAGE_VERSION_COMPATIBLE FALSE) -else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") - set(PACKAGE_VERSION_EXACT TRUE) - endif() -endif() diff --git a/yaml-cpp.pc.cmake b/yaml-cpp.pc.in similarity index 68% rename from yaml-cpp.pc.cmake rename to yaml-cpp.pc.in index 3db7962ea..f713de636 100644 --- a/yaml-cpp.pc.cmake +++ b/yaml-cpp.pc.in @@ -1,7 +1,7 @@ prefix=@CMAKE_INSTALL_PREFIX@ exec_prefix=${prefix} -includedir=${prefix}/@INCLUDE_INSTALL_ROOT_DIR@ -libdir=${exec_prefix}/@LIB_INSTALL_DIR@ +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ +libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@ Name: Yaml-cpp Description: A YAML parser and emitter for C++