blob: 558d208168c4faf25df40d3f89d38133b9c91c76 [file] [log] [blame]
Joshua Goins3a01ec32024-01-08 09:16:29 +00001cmake_minimum_required(VERSION 3.5)
onqtam4a655632016-05-26 14:20:52 +03002
Denilson das Mercês Amorimb2d16772020-04-29 17:57:53 -03003if(POLICY CMP0077)
4 cmake_policy(SET CMP0077 NEW)
5endif()
6
onqtambff67f72017-04-16 20:04:32 +03007################################################################################
8## DOCTEST
9################################################################################
10
11file(READ ${CMAKE_CURRENT_SOURCE_DIR}/scripts/version.txt ver)
zhihaoy7df06442019-08-05 06:44:23 -050012project(doctest VERSION ${ver} LANGUAGES CXX)
onqtam4a655632016-05-26 14:20:52 +030013
Martin Stump4d345d82020-12-15 11:36:37 +010014# Determine if doctest is built as a subproject (using add_subdirectory) or if it is the main project.
15set(MAIN_PROJECT OFF)
16if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
17 set(MAIN_PROJECT ON)
18endif()
19
20option(DOCTEST_WITH_TESTS "Build tests/examples" ${MAIN_PROJECT})
onqtam36e37452019-03-02 20:10:05 +020021option(DOCTEST_WITH_MAIN_IN_STATIC_LIB "Build a static lib (cmake target) with a default main entry point" ON)
NeverMine17b63bf852017-10-28 16:06:43 +030022option(DOCTEST_NO_INSTALL "Skip the installation process" OFF)
avostrik51e4a0d2020-09-04 17:49:49 +030023option(DOCTEST_USE_STD_HEADERS "Use std headers" OFF)
Giuseppe Robertif3425ae2017-03-28 02:03:50 +020024
onqtam61954c32017-04-16 17:22:44 +030025add_library(${PROJECT_NAME} INTERFACE)
Trond H Emausde045762019-06-24 00:57:25 +020026add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
onqtam36e37452019-03-02 20:10:05 +020027
BerengerBerthoul8aeb7982020-10-29 14:49:02 +010028if(NOT CMAKE_VERSION VERSION_LESS 3.8)
Viktor Kirilov10101a12019-08-11 18:16:08 +030029 target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_11)
30endif()
31
onqtamf80b3f02019-03-24 10:19:09 +020032set(doctest_parts_folder "${CMAKE_CURRENT_SOURCE_DIR}/doctest/parts")
BerengerBerthoul8aeb7982020-10-29 14:49:02 +010033set(doctest_folder "${CMAKE_CURRENT_SOURCE_DIR}/") # in order to have the mpi extension files, not included into the doctest.h single header
onqtamf80b3f02019-03-24 10:19:09 +020034
Martin Stump4d345d82020-12-15 11:36:37 +010035if(MAIN_PROJECT)
onqtamb22d8782019-03-24 13:29:57 +020036 # use a special hidden version of the header which directly includes the 2 parts - proper reporting of file/line locations during dev
onqtam36e37452019-03-02 20:10:05 +020037 target_include_directories(${PROJECT_NAME} INTERFACE
38 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/scripts/development_only/>
BerengerBerthoul8aeb7982020-10-29 14:49:02 +010039 $<BUILD_INTERFACE:${doctest_parts_folder}>
40 $<BUILD_INTERFACE:${doctest_folder}>)
onqtamf80b3f02019-03-24 10:19:09 +020041
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)
onqtam36e37452019-03-02 20:10:05 +020052else()
53 target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>)
54endif()
onqtam4a655632016-05-26 14:20:52 +030055
Navin P55c930c2023-03-15 11:42:11 +053056if(CMAKE_BUILD_TYPE STREQUAL "Debug")
57 if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
58 if (NOT WIN32)
59 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gdwarf-4 ")
60 endif()
61 endif()
62endif()
63
Dimitrij Mijoski0dbbd4f2019-03-02 17:37:34 +010064# hack to support building on XCode 6 and 7 - propagate the definition to everything
65if(DEFINED DOCTEST_THREAD_LOCAL)
66 target_compile_definitions(${PROJECT_NAME} INTERFACE
67 DOCTEST_THREAD_LOCAL=${DOCTEST_THREAD_LOCAL})
68endif()
69
avostrik51e4a0d2020-09-04 17:49:49 +030070if(DOCTEST_USE_STD_HEADERS)
71 target_compile_definitions(${PROJECT_NAME} INTERFACE DOCTEST_CONFIG_USE_STD_HEADERS)
72endif()
73
onqtamb759fb42017-08-01 18:38:26 +030074################################################################################
75## TESTS/EXAMPLES/HELPERS
76################################################################################
Yordan Madzhunkov7d8af912017-06-26 12:20:57 +030077
Dimitrij Mijoski02d4c9b2019-03-14 15:09:37 +010078if(${DOCTEST_WITH_MAIN_IN_STATIC_LIB})
onqtamf80b3f02019-03-24 10:19:09 +020079 add_library(${PROJECT_NAME}_with_main STATIC EXCLUDE_FROM_ALL ${doctest_parts_folder}/doctest.cpp)
Jesse Stricker302f1582022-02-17 20:28:20 +010080 add_library(${PROJECT_NAME}::${PROJECT_NAME}_with_main ALIAS ${PROJECT_NAME}_with_main)
Dimitrij Mijoski0dbbd4f2019-03-02 17:37:34 +010081 target_compile_definitions(${PROJECT_NAME}_with_main PRIVATE
82 DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)
Viktor Kirilov10101a12019-08-11 18:16:08 +030083 set_target_properties(${PROJECT_NAME}_with_main PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON)
Dimitrij Mijoski02d4c9b2019-03-14 15:09:37 +010084 target_link_libraries(${PROJECT_NAME}_with_main PUBLIC ${PROJECT_NAME})
onqtamb759fb42017-08-01 18:38:26 +030085endif()
onqtamcc9e8652016-08-02 14:23:38 +030086
Martin Stump4d345d82020-12-15 11:36:37 +010087if(MAIN_PROJECT AND DOCTEST_WITH_TESTS)
onqtam61954c32017-04-16 17:22:44 +030088 include(scripts/cmake/common.cmake)
onqtamf6d1a512017-05-01 13:56:12 +030089
90 add_subdirectory(examples/all_features)
Stefanbf037c12022-01-20 13:06:51 +010091 add_subdirectory(examples/exe_with_static_libs)
92 add_subdirectory(examples/executable_dll_and_plugin)
93 add_subdirectory(examples/combining_the_same_tests_built_differently_in_multiple_shared_objects)
94 add_subdirectory(scripts/playground)
95 add_subdirectory(examples/mpi)
Giuseppe Robertif3425ae2017-03-28 02:03:50 +020096endif()
Giuseppe Robertie01299b2017-03-26 14:18:59 +020097
Chris Apple66ff54a2023-03-24 05:36:22 -070098set(DOCTEST_CMAKE_HELPER "${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/doctest.cmake" CACHE PATH "Absolute path to to doctest helper file. `include(${DOCTEST_CMAKE_HELPER})`")
99
onqtam61954c32017-04-16 17:22:44 +0300100################################################################################
101## PACKAGE SUPPORT
102################################################################################
103
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200104set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
105
Haraldc123a692019-04-01 18:00:31 +0200106if(CMAKE_SYSTEM_NAME STREQUAL Linux)
107 include(GNUInstallDirs)
108 set(include_install_dir ${CMAKE_INSTALL_INCLUDEDIR})
109 set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
110else()
111 set(include_install_dir "include")
112 set(config_install_dir "lib/cmake/${PROJECT_NAME}")
113endif()
114
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200115
116set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
117set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
118set(targets_export_name "${PROJECT_NAME}Targets")
119set(namespace "${PROJECT_NAME}::")
120
121include(CMakePackageConfigHelpers)
Morris Hafnerf9d5d6a2019-04-16 06:34:07 +0100122
123# CMake automatically adds an architecture compatibility check to make sure
124# 32 and 64 bit code is not accidentally mixed. For a header-only library this
125# is not required. The check can be disabled by temporarily unsetting
126# CMAKE_SIZEOF_VOID_P. In CMake 3.14 and later this can be achieved more cleanly
127# with write_basic_package_version_file(ARCH_INDEPENDENT).
128# TODO: Use this once a newer CMake can be required.
129set(DOCTEST_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
130unset(CMAKE_SIZEOF_VOID_P)
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200131write_basic_package_version_file(
Giuseppe Robertic89cf822017-04-12 17:14:17 +0200132 "${version_config}" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200133)
Morris Hafnerf9d5d6a2019-04-16 06:34:07 +0100134set(CMAKE_SIZEOF_VOID_P ${DOCTEST_SIZEOF_VOID_P})
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200135
onqtam08cf7dc2017-04-16 11:37:22 +0300136configure_file("scripts/cmake/Config.cmake.in" "${project_config}" @ONLY)
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200137
onqtam17c97382019-03-24 09:24:02 +0200138if(NOT ${DOCTEST_NO_INSTALL})
NeverMine17b63bf852017-10-28 16:06:43 +0300139 install(
140 TARGETS ${PROJECT_NAME}
141 EXPORT "${targets_export_name}"
142 INCLUDES DESTINATION "${include_install_dir}"
143 )
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200144
NeverMine17b63bf852017-10-28 16:06:43 +0300145 install(
onqtam36e37452019-03-02 20:10:05 +0200146 FILES "doctest/doctest.h"
ncihnegnbc43fe42019-01-27 05:28:54 -0800147 DESTINATION "${include_install_dir}/doctest"
NeverMine17b63bf852017-10-28 16:06:43 +0300148 )
BerengerBerthoul3860c122023-03-07 16:36:40 +0100149 install(
150 DIRECTORY "doctest/extensions"
151 DESTINATION "${include_install_dir}/doctest"
152 FILES_MATCHING PATTERN "*.h"
153 )
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200154
NeverMine17b63bf852017-10-28 16:06:43 +0300155 install(
156 FILES "${project_config}" "${version_config}"
157 DESTINATION "${config_install_dir}"
158 )
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200159
NeverMine17b63bf852017-10-28 16:06:43 +0300160 install(
Cristian Morales Vega35b339f2019-05-19 20:12:41 +0100161 FILES "scripts/cmake/doctest.cmake" "scripts/cmake/doctestAddTests.cmake"
162 DESTINATION "${config_install_dir}"
163 )
164
165 install(
NeverMine17b63bf852017-10-28 16:06:43 +0300166 EXPORT "${targets_export_name}"
167 NAMESPACE "${namespace}"
168 DESTINATION "${config_install_dir}"
169 )
170endif()