CMakeLists.txt (6159B)
1 cmake_minimum_required(VERSION 3.0) 2 3 if(POLICY CMP0077) 4 cmake_policy(SET CMP0077 NEW) 5 endif() 6 7 ################################################################################ 8 ## DOCTEST 9 ################################################################################ 10 11 file(READ ${CMAKE_CURRENT_SOURCE_DIR}/scripts/version.txt ver) 12 project(doctest VERSION ${ver} LANGUAGES CXX) 13 14 # Determine if doctest is built as a subproject (using add_subdirectory) or if it is the main project. 15 set(MAIN_PROJECT OFF) 16 if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) 17 set(MAIN_PROJECT ON) 18 endif() 19 20 option(DOCTEST_WITH_TESTS "Build tests/examples" ${MAIN_PROJECT}) 21 option(DOCTEST_WITH_MAIN_IN_STATIC_LIB "Build a static lib (cmake target) with a default main entry point" ON) 22 option(DOCTEST_NO_INSTALL "Skip the installation process" OFF) 23 option(DOCTEST_USE_STD_HEADERS "Use std headers" OFF) 24 25 add_library(${PROJECT_NAME} INTERFACE) 26 add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) 27 28 if(NOT CMAKE_VERSION VERSION_LESS 3.8) 29 target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_11) 30 endif() 31 32 set(doctest_parts_folder "${CMAKE_CURRENT_SOURCE_DIR}/doctest/parts") 33 set(doctest_folder "${CMAKE_CURRENT_SOURCE_DIR}/") # in order to have the mpi extension files, not included into the doctest.h single header 34 35 if(MAIN_PROJECT) 36 # use a special hidden version of the header which directly includes the 2 parts - proper reporting of file/line locations during dev 37 target_include_directories(${PROJECT_NAME} INTERFACE 38 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/scripts/development_only/> 39 $<BUILD_INTERFACE:${doctest_parts_folder}> 40 $<BUILD_INTERFACE:${doctest_folder}>) 41 42 # add a custom target that assembles the single header when any of the parts are touched 43 add_custom_command( 44 OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/doctest/doctest.h 45 DEPENDS 46 ${doctest_parts_folder}/doctest_fwd.h 47 ${doctest_parts_folder}/doctest.cpp 48 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/assemble_single_header.cmake 49 COMMENT "assembling the single header") 50 51 add_custom_target(assemble_single_header ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/doctest/doctest.h) 52 else() 53 target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>) 54 endif() 55 56 # hack to support building on XCode 6 and 7 - propagate the definition to everything 57 if(DEFINED DOCTEST_THREAD_LOCAL) 58 target_compile_definitions(${PROJECT_NAME} INTERFACE 59 DOCTEST_THREAD_LOCAL=${DOCTEST_THREAD_LOCAL}) 60 endif() 61 62 if(DOCTEST_USE_STD_HEADERS) 63 target_compile_definitions(${PROJECT_NAME} INTERFACE DOCTEST_CONFIG_USE_STD_HEADERS) 64 endif() 65 66 ################################################################################ 67 ## TESTS/EXAMPLES/HELPERS 68 ################################################################################ 69 70 if(${DOCTEST_WITH_MAIN_IN_STATIC_LIB}) 71 add_library(${PROJECT_NAME}_with_main STATIC EXCLUDE_FROM_ALL ${doctest_parts_folder}/doctest.cpp) 72 add_library(${PROJECT_NAME}::${PROJECT_NAME}_with_main ALIAS ${PROJECT_NAME}_with_main) 73 target_compile_definitions(${PROJECT_NAME}_with_main PRIVATE 74 DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN) 75 set_target_properties(${PROJECT_NAME}_with_main PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON) 76 target_link_libraries(${PROJECT_NAME}_with_main PUBLIC ${PROJECT_NAME}) 77 endif() 78 79 if(MAIN_PROJECT AND DOCTEST_WITH_TESTS) 80 include(scripts/cmake/common.cmake) 81 82 add_subdirectory(examples/all_features) 83 add_subdirectory(examples/exe_with_static_libs) 84 add_subdirectory(examples/executable_dll_and_plugin) 85 add_subdirectory(examples/combining_the_same_tests_built_differently_in_multiple_shared_objects) 86 add_subdirectory(scripts/playground) 87 add_subdirectory(examples/mpi) 88 endif() 89 90 ################################################################################ 91 ## PACKAGE SUPPORT 92 ################################################################################ 93 94 set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") 95 96 if(CMAKE_SYSTEM_NAME STREQUAL Linux) 97 include(GNUInstallDirs) 98 set(include_install_dir ${CMAKE_INSTALL_INCLUDEDIR}) 99 set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") 100 else() 101 set(include_install_dir "include") 102 set(config_install_dir "lib/cmake/${PROJECT_NAME}") 103 endif() 104 105 106 set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake") 107 set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake") 108 set(targets_export_name "${PROJECT_NAME}Targets") 109 set(namespace "${PROJECT_NAME}::") 110 111 include(CMakePackageConfigHelpers) 112 113 # CMake automatically adds an architecture compatibility check to make sure 114 # 32 and 64 bit code is not accidentally mixed. For a header-only library this 115 # is not required. The check can be disabled by temporarily unsetting 116 # CMAKE_SIZEOF_VOID_P. In CMake 3.14 and later this can be achieved more cleanly 117 # with write_basic_package_version_file(ARCH_INDEPENDENT). 118 # TODO: Use this once a newer CMake can be required. 119 set(DOCTEST_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) 120 unset(CMAKE_SIZEOF_VOID_P) 121 write_basic_package_version_file( 122 "${version_config}" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion 123 ) 124 set(CMAKE_SIZEOF_VOID_P ${DOCTEST_SIZEOF_VOID_P}) 125 126 configure_file("scripts/cmake/Config.cmake.in" "${project_config}" @ONLY) 127 128 if(NOT ${DOCTEST_NO_INSTALL}) 129 install( 130 TARGETS ${PROJECT_NAME} 131 EXPORT "${targets_export_name}" 132 INCLUDES DESTINATION "${include_install_dir}" 133 ) 134 135 install( 136 FILES "doctest/doctest.h" 137 DESTINATION "${include_install_dir}/doctest" 138 ) 139 140 install( 141 FILES "${project_config}" "${version_config}" 142 DESTINATION "${config_install_dir}" 143 ) 144 145 install( 146 FILES "scripts/cmake/doctest.cmake" "scripts/cmake/doctestAddTests.cmake" 147 DESTINATION "${config_install_dir}" 148 ) 149 150 install( 151 EXPORT "${targets_export_name}" 152 NAMESPACE "${namespace}" 153 DESTINATION "${config_install_dir}" 154 ) 155 endif()