Skip to content

Commit

Permalink
cleanup cmake files to be a little more moderen (USCiLab#659)
Browse files Browse the repository at this point in the history
* cleanup cmake files to be a little more moderen

keep the source tree free of build artifacts
cmakelint the cmake files too

* fix cmake setup errors on CI

fix APPLE clang builds too

* CI needs support for realy history cmake V3.6

fix typo in cmake files using add_test() commnds

* One step more to use modern cmake

Prevent to modifiy compile and linker FLAGS and to set global includes
pathes

* fix CI build problems with older cmake versions

prepare cleanup cmake list file

* final cleanup

use Config.cmake.in and install hole cmake config files

* Fix cpp17 PORTABILITY_TEST linker problem

add missed target_link_libraries()

* hopefully prevent windows test problems
  • Loading branch information
ClausKlein authored and AzothAmmo committed Nov 28, 2021
1 parent d660791 commit 6e6fd8d
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 67 deletions.
128 changes: 82 additions & 46 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,83 +1,119 @@
cmake_minimum_required (VERSION 2.6.2)
project (cereal)
cmake_minimum_required(VERSION 3.6...3.15)

project(cereal LANGUAGES CXX VERSION 1.3.0)

if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(CEREAL_MASTER_PROJECT ON)
endif()


if(APPLE)
option(SKIP_PORTABILITY_TEST "Skip portability (32 bit) tests" ON)
endif()

option(SKIP_PORTABILITY_TEST "Skip portability (32 bit) tests" OFF)
option(SKIP_PERFORMANCE_COMPARISON "Skip building performance comparison (requires boost)" OFF)

# TODO: should not be needed! CK
if(NOT CMAKE_VERSION VERSION_LESS 3.0) # installing cereal requires INTERFACE lib
option(JUST_INSTALL_CEREAL "Don't do anything besides installing the library" OFF)
endif()

option(THREAD_SAFE "Use mutexes to ensure thread safety" OFF)
if(THREAD_SAFE)
add_definitions(-DCEREAL_THREAD_SAFE=1)
set(CEREAL_THREAD_LIBS "pthread")
else()
set(CEREAL_THREAD_LIBS "")

set(CEREAL_THREAD_LIBS)
if(UNIX)
option(THREAD_SAFE "Use mutexes to ensure thread safety" OFF)
if(THREAD_SAFE)
message(STATUS "Use mutexes")
add_definitions(-DCEREAL_THREAD_SAFE=1)
set(CEREAL_THREAD_LIBS pthread)
endif()
endif()


if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /W3 /WX")
add_compile_options(/bigobj /W3 /WX)
else()
set(CMAKE_CXX_FLAGS "-Wall -g -Wextra -Wshadow -pedantic -Wold-style-cast ${CMAKE_CXX_FLAGS}")
add_compile_options(-Wall -Wextra -pedantic -Wshadow -Wold-style-cast)
option(WITH_WERROR "Compile with '-Werror' C++ compiler flag" ON)
if(WITH_WERROR)
set(CMAKE_CXX_FLAGS "-Werror ${CMAKE_CXX_FLAGS}")
endif(WITH_WERROR)
add_compile_options(-Werror)
endif()

option(CLANG_USE_LIBCPP "Use libc++ for clang compilation" OFF)
if(CLANG_USE_LIBCPP)
set(CMAKE_CXX_FLAGS "-stdlib=libc++ ${CMAKE_CXX_FLAGS}")
if(APPLE OR CLANG_USE_LIBCPP)
message(STATUS "Use libc++")
add_compile_options(-stdlib=libc++)
# TODO: use add_link_options(-stdlib=libc++ -lc++abi") bud this needs cmake 3.13! CK
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
endif()

if(CMAKE_VERSION VERSION_LESS 3.1)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
else()
if(NOT DEFINED CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD STREQUAL "98")
if(NOT DEFINED CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD STREQUAL "98")
set(CMAKE_CXX_STANDARD 11)
endif()
endif()

if(CMAKE_CXX_STANDARD GREATER 14)
cmake_minimum_required(VERSION 3.8)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

add_library(cereal INTERFACE)
add_library(cereal::cereal ALIAS cereal)
target_include_directories(cereal INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
list(APPEND CEREAL_THREAD_LIBS cereal::cereal)

if(NOT CMAKE_VERSION VERSION_LESS 3.8)
target_compile_features(cereal INTERFACE cxx_std_11)
endif()

if(NOT CMAKE_VERSION VERSION_LESS 3.0)
add_library(cereal INTERFACE)
target_include_directories(cereal INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
install(TARGETS cereal EXPORT cereal
DESTINATION lib) # ignored
install(EXPORT cereal FILE cereal-config.cmake
DESTINATION share/cmake/cereal)

option(CEREAL_INSTALL "Generate the install target" ${CEREAL_MASTER_PROJECT})
if(CEREAL_INSTALL)
include(CMakePackageConfigHelpers)

install(TARGETS cereal EXPORT ${PROJECT_NAME}Targets)
install(DIRECTORY include/cereal DESTINATION include)

set(configFile ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake)
set(versionFile ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake)
set(configInstallDestination lib/cmake/${PROJECT_NAME})

configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
${configFile}
INSTALL_DESTINATION ${configInstallDestination}
)
write_basic_package_version_file(
${versionFile}
COMPATIBILITY SameMajorVersion
)

install(FILES ${configFile} ${versionFile} DESTINATION ${configInstallDestination})
install(
EXPORT ${PROJECT_NAME}Targets
NAMESPACE "cereal::"
DESTINATION ${configInstallDestination}
)
endif()


if(JUST_INSTALL_CEREAL)
return()
endif()

include_directories(./include)

if(NOT CMAKE_VERSION VERSION_LESS 3.12)
cmake_policy(VERSION 3.12)
if(NOT SKIP_PERFORMANCE_COMPARISON)
# Boost serialization for performance sandbox
find_package(Boost REQUIRED COMPONENTS serialization)
endif()

# Boost serialization for performance sandbox
find_package(Boost COMPONENTS serialization)

if(Boost_FOUND)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
endif(Boost_FOUND)

enable_testing()
add_subdirectory(unittests)
option(BUILD_TESTS "Build tests" ${CEREAL_MASTER_PROJECT})
if(BUILD_TESTS)
enable_testing()
add_subdirectory(unittests)
endif()

add_subdirectory(sandbox)

Expand Down
3 changes: 3 additions & 0 deletions Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
2 changes: 1 addition & 1 deletion doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ if(DOXYGEN_FOUND)
COMMENT "Copying documentation to gh-pages branch" VERBATIM
)

endif(DOXYGEN_FOUND)
endif()
11 changes: 8 additions & 3 deletions sandbox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
add_subdirectory(sandbox_shared_lib)

add_executable(sandbox sandbox.cpp)
target_link_libraries(sandbox ${CEREAL_THREAD_LIBS})

add_executable(sandbox_json sandbox_json.cpp)
target_link_libraries(sandbox_json ${CEREAL_THREAD_LIBS})

add_executable(sandbox_rtti sandbox_rtti.cpp)
target_link_libraries(sandbox_rtti ${CEREAL_THREAD_LIBS})

add_executable(sandbox_vs sandbox_vs.cpp)
target_link_libraries(sandbox_vs sandbox_vs_dll)
include_directories(sandbox_shared_lib)

if((Boost_FOUND) AND NOT SKIP_PERFORMANCE_COMPARISON)
if(Boost_FOUND AND NOT SKIP_PERFORMANCE_COMPARISON)
add_executable(performance performance.cpp)
if(MSVC)
set_target_properties(performance PROPERTIES COMPILE_DEFINITIONS "BOOST_SERIALIZATION_DYN_LINK")
endif()
target_link_libraries(performance ${Boost_LIBRARIES})
target_include_directories(performance PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(performance ${CEREAL_THREAD_LIBS} ${Boost_LIBRARIES})
endif()
5 changes: 5 additions & 0 deletions sandbox/sandbox_shared_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
add_library(sandbox_vs_dll SHARED base.cpp derived.cpp)
target_link_libraries(sandbox_vs_dll ${CEREAL_THREAD_LIBS})
target_include_directories(sandbox_vs_dll PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
14 changes: 9 additions & 5 deletions unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)

# A semi-colon separated list of test sources that should not be automatically built with doctest
# A semi-colon separated list of test sources that should not be automatically built with doctest
set(SPECIAL_TESTS "portability_test.cpp")

if(CMAKE_VERSION VERSION_LESS 2.8)
Expand All @@ -13,11 +13,13 @@ if(NOT SKIP_PORTABILITY_TEST)
if((${CMAKE_SIZEOF_VOID_P} EQUAL 8))
if(NOT MSVC)
add_executable(portability_test32 portability_test.cpp)
target_link_libraries(portability_test32 ${CEREAL_THREAD_LIBS})
set_target_properties(portability_test32 PROPERTIES COMPILE_FLAGS "-m32")
set_target_properties(portability_test32 PROPERTIES LINK_FLAGS "-m32")
endif()

add_executable(portability_test64 portability_test.cpp)
target_link_libraries(portability_test64 ${CEREAL_THREAD_LIBS})

add_test(NAME portability_test
COMMAND ${CMAKE_COMMAND}
Expand All @@ -26,6 +28,7 @@ if(NOT SKIP_PORTABILITY_TEST)

elseif(MSVC)
add_executable(portability_test32 portability_test.cpp)
target_link_libraries(portability_test32 cereal::cereal)
endif()
endif()

Expand All @@ -42,14 +45,15 @@ foreach(TEST_SOURCE ${TESTS})

add_executable(${TEST_TARGET} ${TEST_SOURCE})
target_link_libraries(${TEST_TARGET} ${CEREAL_THREAD_LIBS})
add_test("${TEST_TARGET}" "${TEST_TARGET}")
add_test(NAME "${TEST_TARGET}" COMMAND "${TEST_TARGET}")

# If we are on a 64-bit machine, create an extra 32-bit version of the test if portability testing is enabled
if((NOT MSVC) AND (${CMAKE_SIZEOF_VOID_P} EQUAL 8) AND (NOT SKIP_PORTABILITY_TEST))
add_executable(${TEST_TARGET}_32 ${TEST_SOURCE})
set_target_properties(${TEST_TARGET}_32 PROPERTIES
COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
add_test("${TEST_TARGET}_32" "${TEST_TARGET}_32")
target_link_libraries(${TEST_TARGET}_32 ${CEREAL_THREAD_LIBS})
add_test(NAME "${TEST_TARGET}_32" COMMAND "${TEST_TARGET}_32")
endif()

endif()
Expand Down Expand Up @@ -85,7 +89,7 @@ if(NOT MSVC)
target_link_libraries(${COVERAGE_TARGET} ${CEREAL_THREAD_LIBS})
endif()
endforeach()
endif(NOT MSVC)
endif()

if(CMAKE_CXX_STANDARD GREATER 14)
add_subdirectory(cpp17)
Expand All @@ -96,5 +100,5 @@ if(Boost_FOUND)
endif()

if(NOT CMAKE_VERSION VERSION_LESS 3.0)
add_test(test_cmake_config_module ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake-config-module.cmake)
add_test(NAME test_cmake_config_module COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake-config-module.cmake)
endif()
11 changes: 7 additions & 4 deletions unittests/boost/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)

include_directories(SYSTEM ${Boost_INCLUDE_DIRS})

# Build all of the non-special tests
foreach(TEST_SOURCE ${TESTS})
message(STATUS ${TEST_SOURCE})
Expand All @@ -9,14 +11,15 @@ foreach(TEST_SOURCE ${TESTS})

add_executable(${TEST_TARGET} ${TEST_SOURCE})
target_link_libraries(${TEST_TARGET} ${CEREAL_THREAD_LIBS})
add_test("${TEST_TARGET}" "${TEST_TARGET}")
add_test(NAME "${TEST_TARGET}" COMMAND "${TEST_TARGET}")

# If we are on a 64-bit machine, create an extra 32-bit version of the test if portability testing is enabled
if((NOT MSVC) AND (${CMAKE_SIZEOF_VOID_P} EQUAL 8) AND (NOT SKIP_PORTABILITY_TEST))
add_executable(${TEST_TARGET}_32 ${TEST_SOURCE})
target_link_libraries(${TEST_TARGET}_32 ${CEREAL_THREAD_LIBS})
set_target_properties(${TEST_TARGET}_32 PROPERTIES
COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
add_test("${TEST_TARGET}_32" "${TEST_TARGET}_32")
add_test(NAME "${TEST_TARGET}_32" COMMAND "${TEST_TARGET}_32")
endif()

endforeach()
Expand All @@ -26,7 +29,7 @@ if(NOT MSVC)
foreach(TEST_SOURCE ${TESTS})
string(REPLACE ".cpp" "" COVERAGE_TARGET "${TEST_SOURCE}")
set(COVERAGE_TARGET "coverage_${COVERAGE_TARGET}")

add_dependencies(coverage ${COVERAGE_TARGET})

add_executable(${COVERAGE_TARGET} EXCLUDE_FROM_ALL ${TEST_SOURCE})
Expand All @@ -35,4 +38,4 @@ if(NOT MSVC)
set_target_properties(${COVERAGE_TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/coverage")
target_link_libraries(${COVERAGE_TARGET} ${CEREAL_THREAD_LIBS})
endforeach()
endif(NOT MSVC)
endif()
10 changes: 6 additions & 4 deletions unittests/cmake-config-module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ if(CMAKE_VERSION LESS 3.0)
message(FATAL_ERROR "Cereal can't be installed with CMake < 3.0")
endif()

get_filename_component(BINARY_DIR ${CMAKE_CURRENT_LIST_DIR}/../build ABSOLUTE)
get_filename_component(INSTALL_DIR ${CMAKE_CURRENT_LIST_DIR}/../out ABSOLUTE)
get_filename_component(BINARY_DIR ${CMAKE_BINARY_DIR}/build ABSOLUTE)
get_filename_component(INSTALL_DIR ${CMAKE_BINARY_DIR}/out ABSOLUTE)

# cmake configure step for cereal
file(MAKE_DIRECTORY ${BINARY_DIR}/cereal)
Expand Down Expand Up @@ -44,10 +44,11 @@ file(WRITE ${BINARY_DIR}/test_source/CMakeLists.txt "
endif()
find_package(cereal REQUIRED)
add_executable(cereal-test-config-module main.cpp)
target_link_libraries(cereal-test-config-module cereal)
target_link_libraries(cereal-test-config-module cereal::cereal)
enable_testing()
add_test(test-cereal-test-config-module cereal-test-config-module)
add_test(NAME test-cereal-test-config-module COMMAND cereal-test-config-module)
")

file(WRITE ${BINARY_DIR}/test_source/main.cpp "
#include <cereal/archives/binary.hpp>
#include <sstream>
Expand Down Expand Up @@ -90,6 +91,7 @@ file(WRITE ${BINARY_DIR}/test_source/main.cpp "
}
}"
)

file(MAKE_DIRECTORY ${BINARY_DIR}/test)
execute_process(
COMMAND ${CMAKE_COMMAND}
Expand Down
9 changes: 5 additions & 4 deletions unittests/cpp17/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ foreach(TEST_SOURCE ${TESTS})

add_executable(${TEST_TARGET} ${TEST_SOURCE})
target_link_libraries(${TEST_TARGET} ${CEREAL_THREAD_LIBS})
add_test("${TEST_TARGET}" "${TEST_TARGET}")
add_test(NAME "${TEST_TARGET}" COMMAND "${TEST_TARGET}")

# If we are on a 64-bit machine, create an extra 32-bit version of the test if portability testing is enabled
if((NOT MSVC) AND (${CMAKE_SIZEOF_VOID_P} EQUAL 8) AND (NOT SKIP_PORTABILITY_TEST))
add_executable(${TEST_TARGET}_32 ${TEST_SOURCE})
target_link_libraries(${TEST_TARGET}_32 ${CEREAL_THREAD_LIBS})
set_target_properties(${TEST_TARGET}_32 PROPERTIES
COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
add_test("${TEST_TARGET}_32" "${TEST_TARGET}_32")
add_test(NAME "${TEST_TARGET}_32" COMMAND "${TEST_TARGET}_32")
endif()

endforeach()
Expand All @@ -26,7 +27,7 @@ if(NOT MSVC)
foreach(TEST_SOURCE ${TESTS})
string(REPLACE ".cpp" "" COVERAGE_TARGET "${TEST_SOURCE}")
set(COVERAGE_TARGET "coverage_cpp17_${COVERAGE_TARGET}")

add_dependencies(coverage ${COVERAGE_TARGET})

add_executable(${COVERAGE_TARGET} EXCLUDE_FROM_ALL ${TEST_SOURCE})
Expand All @@ -35,4 +36,4 @@ if(NOT MSVC)
set_target_properties(${COVERAGE_TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/coverage")
target_link_libraries(${COVERAGE_TARGET} ${CEREAL_THREAD_LIBS})
endforeach()
endif(NOT MSVC)
endif()

0 comments on commit 6e6fd8d

Please sign in to comment.