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
This commit is contained in:
Isabella Muerte
2019-09-27 07:59:53 -07:00
committed by Jesse Beder
parent 9a3624205e
commit 5e9cb0128d
7 changed files with 201 additions and 428 deletions

View File

@@ -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 "")
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(BUILD_MOCK ON CACHE BOOL "" FORCE)
set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
add_subdirectory(
"${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.8.0"
"${CMAKE_CURRENT_BINARY_DIR}/prefix")
include_directories(SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.8.0/googletest/include")
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()
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=<INSTALL_DIR>
-DBUILD_GMOCK=ON
-Dgtest_force_shared_crt=ON
)
file(GLOB test-new-api-sources ${test-new-api-pattern})
file(GLOB test-sources ${test-source-pattern})
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}
)
find_package(Threads)
include_directories(SYSTEM "${PROJECT_BINARY_DIR}/test/prefix/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()
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
)
add_dependencies(run-tests googletest_project)
set_target_properties(run-tests PROPERTIES
COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags} ${yaml_test_flags}"
)
target_link_libraries(run-tests
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
$<$<CXX_COMPILER_ID:Clang>:-Wno-c99-extensions -Wno-variadic-macros -Wno-sign-compare>
$<$<CXX_COMPILER_ID:GNU>:-Wno-variadic-macros -Wno-sign-compare>)
target_link_libraries(yaml-cpp-tests
PRIVATE
Threads::Threads
yaml-cpp
gmock
${CMAKE_THREAD_LIBS_INIT})
gmock)
add_test(yaml-test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/run-tests)
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()
add_test(yaml-cpp::test yaml-cpp-tests)