From cd7346ff3d94618b10a9e79489580c7eafe63f91 Mon Sep 17 00:00:00 2001 From: Tamas Kenez Date: Tue, 6 Oct 2015 14:32:40 +0200 Subject: [PATCH 1/2] cmake improvements (install target) - add install target to install config-module for which creates an INTERFACE library - add unittest to test config-module - add option to skip tests and install only - fix MSVC compatibility problem in CMakeLists.txt --- .gitignore | 1 + CMakeLists.txt | 30 ++++++- unittests/CMakeLists.txt | 8 +- unittests/cmake-config-module.cmake | 123 ++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 unittests/cmake-config-module.cmake diff --git a/.gitignore b/.gitignore index 5d4370d13..acb63c308 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ out.xml cereal_version.out xml_ordering.out build +/out/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 4838b97b0..3e11544c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,36 @@ cmake_minimum_required (VERSION 2.6.2) project (cereal) option(SKIP_PORTABILITY_TEST "Skip portability tests" OFF) +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() -set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Werror -g -Wextra -Wshadow -pedantic ${CMAKE_CXX_FLAGS}") +if(NOT MSVC) + set(CMAKE_CXX_FLAGS "-Wall -Werror -g -Wextra -Wshadow -pedantic ${CMAKE_CXX_FLAGS}") + if(CMAKE_VERSION VERSION_LESS 3.1) + set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + endif() +endif() + +if(NOT CMAKE_VERSION VERSION_LESS 3.0) + add_library(cereal INTERFACE) + target_include_directories(cereal INTERFACE + $ + $ + ) + install(TARGETS cereal EXPORT cereal + DESTINATION lib) # ignored + install(EXPORT cereal FILE cereal-config.cmake + DESTINATION share/cmake/cereal) + install(DIRECTORY include/cereal DESTINATION include) +endif() + +if(JUST_INSTALL_CEREAL) + return() +endif() include_directories(./include) diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index 622d49ecf..71aa70b28 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -35,7 +35,7 @@ foreach(TEST_SOURCE ${TESTS}) ## If we are on a 64-bit machine, create an extra 32-bit version of the test #if(${CMAKE_SIZEOF_VOID_P} EQUAL 8) # add_executable(${TEST_TARGET}_32 ${TEST_SOURCE}) - # set_target_properties(${TEST_TARGET}_32 PROPERTIES + # set_target_properties(${TEST_TARGET}_32 PROPERTIES # COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK;BOOST_TEST_MODULE=${TEST_TARGET}" # COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") # target_link_libraries(${TEST_TARGET}_32 ${Boost_LIBRARIES}) @@ -66,7 +66,7 @@ foreach(TEST_SOURCE ${TESTS}) if(IS_SPECIAL_TEST EQUAL -1) add_dependencies(coverage ${COVERAGE_TARGET}) - + add_executable(${COVERAGE_TARGET} EXCLUDE_FROM_ALL ${TEST_SOURCE}) set_target_properties(${COVERAGE_TARGET} PROPERTIES COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK;BOOST_TEST_MODULE=${COVERAGE_TARGET}") set_target_properties(${COVERAGE_TARGET} PROPERTIES COMPILE_FLAGS "-coverage") @@ -75,3 +75,7 @@ foreach(TEST_SOURCE ${TESTS}) target_link_libraries(${COVERAGE_TARGET} ${Boost_LIBRARIES}) endif() endforeach() + +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) +endif() diff --git a/unittests/cmake-config-module.cmake b/unittests/cmake-config-module.cmake new file mode 100644 index 000000000..1b2f46420 --- /dev/null +++ b/unittests/cmake-config-module.cmake @@ -0,0 +1,123 @@ +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) + +# cmake configure step for cereal +file(MAKE_DIRECTORY ${BINARY_DIR}/cereal) +execute_process( + COMMAND ${CMAKE_COMMAND} + -DJUST_INSTALL_CEREAL=1 + -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} + ${CMAKE_CURRENT_LIST_DIR}/.. + WORKING_DIRECTORY ${BINARY_DIR}/cereal + RESULT_VARIABLE result +) +if(result) + message(FATAL_ERROR "Cereal cmake configure-step failed") +endif() + +# cmake install cereal +execute_process( + COMMAND ${CMAKE_COMMAND} + --build ${BINARY_DIR}/cereal + --target install + RESULT_VARIABLE result +) +if(result) + message(FATAL_ERROR "Cereal cmake install-step failed") +endif() + +# create test project sources +file(WRITE ${BINARY_DIR}/test_source/CMakeLists.txt " + cmake_minimum_required(VERSION ${CMAKE_VERSION}) + project(cereal-test-config-module) + if(NOT MSVC) + if(CMAKE_VERSION VERSION_LESS 3.1) + set(CMAKE_CXX_FLAGS \"-std=c++11 \${CMAKE_CXX_FLAGS}\") + else() + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + endif() + endif() + find_package(cereal REQUIRED) + add_executable(cereal-test-config-module main.cpp) + target_link_libraries(cereal-test-config-module cereal) + enable_testing() + add_test(test-cereal-test-config-module cereal-test-config-module) +") +file(WRITE ${BINARY_DIR}/test_source/main.cpp " + #include + #include + #include + struct MyData + { + int x = 0, y = 0, z = 0; + void set() { x = 1; y = 2; z = 3; } + bool is_set() const { return x == 1 && y == 2 && z == 3; } + + // This method lets cereal know which data members to serialize + template + void serialize(Archive & archive) + { + archive( x, y, z ); // serialize things by passing them to the archive + } + }; + int main() + { + std::stringstream ss; // any stream can be used + + { + cereal::BinaryOutputArchive oarchive(ss); // Create an output archive + + MyData m1, m2, m3; + m1.set(); + m2.set(); + m3.set(); + oarchive(m1, m2, m3); // Write the data to the archive + } + + { + cereal::BinaryInputArchive iarchive(ss); // Create an input archive + + MyData m1, m2, m3; + iarchive(m1, m2, m3); // Read the data from the archive + + return (m1.is_set() && m2.is_set() && m3.is_set()) + ? EXIT_SUCCESS : EXIT_FAILURE; + } + }" +) +file(MAKE_DIRECTORY ${BINARY_DIR}/test) +execute_process( + COMMAND ${CMAKE_COMMAND} + -DCMAKE_PREFIX_PATH=${INSTALL_DIR} + ${BINARY_DIR}/test_source + WORKING_DIRECTORY ${BINARY_DIR}/test + RESULT_VARIABLE result +) +if(result) + message(FATAL_ERROR "Test cmake configure-step failed") +endif() + +# cmake install cereal +execute_process( + COMMAND ${CMAKE_COMMAND} + --build ${BINARY_DIR}/test + RESULT_VARIABLE result +) +if(result) + message(FATAL_ERROR "Test cmake build-step failed") +endif() + +execute_process( + COMMAND ${CMAKE_CTEST_COMMAND} + WORKING_DIRECTORY ${BINARY_DIR}/test + RESULT_VARIABLE result +) + +if(result) + message(FATAL_ERROR "Test run failed") +endif() From f2ccfea1f8f65b7d0531058b422813441cce9cbb Mon Sep 17 00:00:00 2001 From: Tamas Kenez Date: Wed, 7 Oct 2015 11:02:34 +0200 Subject: [PATCH 2/2] cmake: don't overwrite CMAKE_CXX_STANDARD if it's already higher --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e11544c8..ff95f6dcc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,9 @@ if(NOT MSVC) if(CMAKE_VERSION VERSION_LESS 3.1) set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") else() - set(CMAKE_CXX_STANDARD 11) + if(NOT DEFINED CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD STREQUAL "98") + set(CMAKE_CXX_STANDARD 11) + endif() set(CMAKE_CXX_STANDARD_REQUIRED ON) endif() endif()