diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f31c61..2f3d3d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,15 +10,13 @@ set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH}) include(Commons) if (NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) + set(CMAKE_BUILD_TYPE Release) endif () set_common_cxx_flags_for_sparta() add_dependent_packages_for_sparta() -include_directories( - ${Boost_INCLUDE_DIRS} - ) +include_directories(${Boost_INCLUDE_DIRS}) ################################################### # Add sparta interface library @@ -26,9 +24,9 @@ include_directories( add_library(sparta INTERFACE) target_include_directories(sparta INTERFACE - $ - $ - ) + $ + $ +) target_link_libraries(sparta INTERFACE ${Boost_LIBRARIES}) @@ -41,9 +39,10 @@ include(GNUInstallDirs) # 'make install' to the correct locations (provided by GNUInstallDirs). install(TARGETS sparta EXPORT sparta_target - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) # This is for Windows + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # This is for Windows +) install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(EXPORT sparta_target DESTINATION cmake) @@ -54,20 +53,17 @@ export(TARGETS sparta FILE sparta_target.cmake) ################################################### # test ################################################### -file(GLOB test - "test/*.cpp" - ) +file(GLOB test "test/*.cpp") include(CTest) # ${test} contains all paths to the test cpps foreach(testfile ${test}) - # ${testfile} is in the format of test/SomeTest.cpp - string(REPLACE ".cpp" "_test" no_ext_name ${testfile}) - # ${no_ext_name} is in the format of test/SomeTest_test - get_filename_component(test_bin ${no_ext_name} NAME) - # ${test_bin} is in the format of SomeTest_test - add_executable(${test_bin} ${testfile}) - target_link_libraries(${test_bin} PRIVATE sparta gmock_main) - add_test(NAME ${testfile} COMMAND ${test_bin}) + # ${testfile} is in the format of test/SomeTest.cpp + string(REPLACE ".cpp" "_test" no_ext_name ${testfile}) + # ${no_ext_name} is in the format of test/SomeTest_test + get_filename_component(test_bin ${no_ext_name} NAME) + # ${test_bin} is in the format of SomeTest_test + add_executable(${test_bin} ${testfile}) + target_link_libraries(${test_bin} PRIVATE sparta gmock_main) + add_test(NAME ${testfile} COMMAND ${test_bin}) endforeach() - diff --git a/cmake_modules/Commons.cmake b/cmake_modules/Commons.cmake index 79cc798..97b39c1 100644 --- a/cmake_modules/Commons.cmake +++ b/cmake_modules/Commons.cmake @@ -4,86 +4,90 @@ # LICENSE file in the root directory of this source tree. function(print_dirs var name) - foreach (path ${var}) - message(STATUS "${name}: " ${path}) - endforeach () + foreach (path ${var}) + message(STATUS "${name}: " ${path}) + endforeach () endfunction() macro(set_common_cxx_flags_for_sparta) - message(STATUS "Using C++17") - set(CMAKE_CXX_STANDARD 17) - set(CMAKE_CXX_STANDARD_REQUIRED ON) - set(CMAKE_CXX_EXTENSIONS OFF) - if (MSVC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /W1 /D_SCL_SECURE_NO_WARNINGS") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") - set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT") - set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT") - else () - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -Wall") - set(COMMON_CXX_FLAGS_NODBG, "-O3") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${COMMON_CXX_FLAGS_NODBG}") - set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${COMMON_CXX_FLAGS_NODBG}") - set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${COMMON_CXX_FLAGS_NODBG}") - endif () + message(STATUS "Using C++17") + set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + set(CMAKE_CXX_EXTENSIONS OFF) + if (MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /W1 /D_SCL_SECURE_NO_WARNINGS") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT") + set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT") + else () + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -Wall") + set(COMMON_CXX_FLAGS_NODBG, "-O3") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${COMMON_CXX_FLAGS_NODBG}") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${COMMON_CXX_FLAGS_NODBG}") + set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${COMMON_CXX_FLAGS_NODBG}") + endif () endmacro() macro(add_dependent_packages_for_sparta) - find_package(Boost 1.71.0 REQUIRED COMPONENTS thread) - print_dirs("${Boost_INCLUDE_DIRS}" "Boost_INCLUDE_DIRS") - print_dirs("${Boost_LIBRARIES}" "Boost_LIBRARIES") + find_package(Boost 1.71.0 REQUIRED COMPONENTS thread) + print_dirs("${Boost_INCLUDE_DIRS}" "Boost_INCLUDE_DIRS") + print_dirs("${Boost_LIBRARIES}" "Boost_LIBRARIES") - set(Boost_USE_STATIC_LIBS ON) - set(Boost_USE_STATIC_RUNTIME ON) - set(Boost_USE_MULTITHREADED ON) + set(Boost_USE_STATIC_LIBS ON) + set(Boost_USE_STATIC_RUNTIME ON) + set(Boost_USE_MULTITHREADED ON) - project(googletest-download NONE) + project(googletest-download NONE) - # Download and unpack googletest at configure time - configure_file(cmake_modules/gtest.cmake.in googletest-download/CMakeLists.txt) - execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) - if(result) - message(FATAL_ERROR "CMake step for googletest failed: ${result}") - endif() - execute_process(COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) - if(result) - message(FATAL_ERROR "Build step for googletest failed: ${result}") - endif() + # Download and unpack googletest at configure time + configure_file(cmake_modules/gtest.cmake.in googletest-download/CMakeLists.txt) + execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download + ) + if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") + endif() + execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download + ) + if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") + endif() - # Prevent overriding the parent project's compiler/linker - # settings on Windows - set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + # Prevent overriding the parent project's compiler/linker + # settings on Windows + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - # Add googletest directly to our build. This defines - # the gtest and gtest_main targets. - add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src - ${CMAKE_BINARY_DIR}/googletest-build - EXCLUDE_FROM_ALL) + # Add googletest directly to our build. This defines + # the gtest and gtest_main targets. + add_subdirectory( + ${CMAKE_BINARY_DIR}/googletest-src + ${CMAKE_BINARY_DIR}/googletest-build + EXCLUDE_FROM_ALL + ) - # The gtest/gtest_main targets carry header search path - # dependencies automatically when using CMake 2.8.11 or - # later. Otherwise we have to add them here ourselves. - if (CMAKE_VERSION VERSION_LESS 2.8.11) - include_directories("${gtest_SOURCE_DIR}/include") - endif() + # The gtest/gtest_main targets carry header search path + # dependencies automatically when using CMake 2.8.11 or + # later. Otherwise we have to add them here ourselves. + if (CMAKE_VERSION VERSION_LESS 2.8.11) + include_directories("${gtest_SOURCE_DIR}/include") + endif() endmacro() function(set_link_whole target_name lib_name) - set(libpath "${LIBRARY_OUTPUT_DIRECTORY}${CMAKE_STATIC_LIBRARY_PREFIX}${lib_name}${CMAKE_STATIC_LIBRARY_SUFFIX}") - message(STATUS "${target_name} will link ${libpath} wholly") - string(TOLOWER ${CMAKE_CXX_COMPILER_ID} compiler_id) - if (${compiler_id} MATCHES ".*clang") - set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS "-Wl,-force_load ${libpath} ") - elseif (${compiler_id} STREQUAL "gnu") - set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS "-Wl,--whole-archive ${libpath} ") - elseif (${compiler_id} STREQUAL "msvc") - set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS "/WHOLEARCHIVE:${libpath} ") - else () - message(WARNING "Unknown compiler: skipping whole link option: " ${compiler_id}) - endif () + set(libpath "${LIBRARY_OUTPUT_DIRECTORY}${CMAKE_STATIC_LIBRARY_PREFIX}${lib_name}${CMAKE_STATIC_LIBRARY_SUFFIX}") + message(STATUS "${target_name} will link ${libpath} wholly") + string(TOLOWER ${CMAKE_CXX_COMPILER_ID} compiler_id) + if (${compiler_id} MATCHES ".*clang") + set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS "-Wl,-force_load ${libpath} ") + elseif (${compiler_id} STREQUAL "gnu") + set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS "-Wl,--whole-archive ${libpath} ") + elseif (${compiler_id} STREQUAL "msvc") + set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS "/WHOLEARCHIVE:${libpath} ") + else () + message(WARNING "Unknown compiler: skipping whole link option: " ${compiler_id}) + endif () endfunction()