blob: c5b83c0199a4608de42c168d14d507f6e5ad0bb7 [file] [log] [blame]
onqtamc7aaa962016-09-10 02:16:57 +03001cmake_minimum_required(VERSION 3.0)
onqtam4a655632016-05-26 14:20:52 +03002
onqtambff67f72017-04-16 20:04:32 +03003################################################################################
4## DOCTEST
5################################################################################
6
7file(READ ${CMAKE_CURRENT_SOURCE_DIR}/scripts/version.txt ver)
zhihaoy7df06442019-08-05 06:44:23 -05008project(doctest VERSION ${ver} LANGUAGES CXX)
onqtam4a655632016-05-26 14:20:52 +03009
onqtamb22d8782019-03-24 13:29:57 +020010option(DOCTEST_WITH_TESTS "Build tests/examples" ON)
onqtam36e37452019-03-02 20:10:05 +020011option(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 +030012option(DOCTEST_NO_INSTALL "Skip the installation process" OFF)
Giuseppe Robertif3425ae2017-03-28 02:03:50 +020013
onqtam61954c32017-04-16 17:22:44 +030014add_library(${PROJECT_NAME} INTERFACE)
Trond H Emausde045762019-06-24 00:57:25 +020015add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
onqtam36e37452019-03-02 20:10:05 +020016
Viktor Kirilov10101a12019-08-11 18:16:08 +030017if(NOT CMAKE_VERSION VERSION_LESS 3.8)
18 target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_11)
19endif()
20
onqtamf80b3f02019-03-24 10:19:09 +020021set(doctest_parts_folder "${CMAKE_CURRENT_SOURCE_DIR}/doctest/parts")
22
onqtam36e37452019-03-02 20:10:05 +020023if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
onqtamb22d8782019-03-24 13:29:57 +020024 # 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 +020025 target_include_directories(${PROJECT_NAME} INTERFACE
26 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/scripts/development_only/>
onqtamf80b3f02019-03-24 10:19:09 +020027 $<BUILD_INTERFACE:${doctest_parts_folder}>)
28
29 # add a custom target that assembles the single header when any of the parts are touched
30 add_custom_command(
31 OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/doctest/doctest.h
32 DEPENDS
33 ${doctest_parts_folder}/doctest_fwd.h
34 ${doctest_parts_folder}/doctest.cpp
35 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/assemble_single_header.cmake
36 COMMENT "assembling the single header")
37
38 add_custom_target(assemble_single_header ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/doctest/doctest.h)
onqtam36e37452019-03-02 20:10:05 +020039else()
40 target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>)
41endif()
onqtam4a655632016-05-26 14:20:52 +030042
Dimitrij Mijoski0dbbd4f2019-03-02 17:37:34 +010043# hack to support building on XCode 6 and 7 - propagate the definition to everything
44if(DEFINED DOCTEST_THREAD_LOCAL)
45 target_compile_definitions(${PROJECT_NAME} INTERFACE
46 DOCTEST_THREAD_LOCAL=${DOCTEST_THREAD_LOCAL})
47endif()
48
onqtamb759fb42017-08-01 18:38:26 +030049################################################################################
50## TESTS/EXAMPLES/HELPERS
51################################################################################
Yordan Madzhunkov7d8af912017-06-26 12:20:57 +030052
Dimitrij Mijoski02d4c9b2019-03-14 15:09:37 +010053if(${DOCTEST_WITH_MAIN_IN_STATIC_LIB})
onqtamf80b3f02019-03-24 10:19:09 +020054 add_library(${PROJECT_NAME}_with_main STATIC EXCLUDE_FROM_ALL ${doctest_parts_folder}/doctest.cpp)
Dimitrij Mijoski0dbbd4f2019-03-02 17:37:34 +010055 target_compile_definitions(${PROJECT_NAME}_with_main PRIVATE
56 DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)
Viktor Kirilov10101a12019-08-11 18:16:08 +030057 set_target_properties(${PROJECT_NAME}_with_main PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON)
Dimitrij Mijoski02d4c9b2019-03-14 15:09:37 +010058 target_link_libraries(${PROJECT_NAME}_with_main PUBLIC ${PROJECT_NAME})
onqtamb759fb42017-08-01 18:38:26 +030059endif()
onqtamcc9e8652016-08-02 14:23:38 +030060
onqtamb22d8782019-03-24 13:29:57 +020061if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR AND DOCTEST_WITH_TESTS)
onqtam61954c32017-04-16 17:22:44 +030062 include(scripts/cmake/common.cmake)
onqtamf6d1a512017-05-01 13:56:12 +030063
64 add_subdirectory(examples/all_features)
Claus Klein6f1241c2019-08-11 15:35:44 +020065
onqtamc6cc9ff2019-03-15 17:22:50 +020066 # for code coverage we want exactly one binary to be produced and exercised
onqtamaec53d22017-05-03 06:29:47 +030067 if(NOT DEFINED ENV{CODE_COVERAGE})
68 add_subdirectory(examples/exe_with_static_libs)
69 add_subdirectory(examples/executable_dll_and_plugin)
70 add_subdirectory(scripts/playground)
71 endif()
Giuseppe Robertif3425ae2017-03-28 02:03:50 +020072endif()
Giuseppe Robertie01299b2017-03-26 14:18:59 +020073
onqtam61954c32017-04-16 17:22:44 +030074################################################################################
75## PACKAGE SUPPORT
76################################################################################
77
Giuseppe Robertie01299b2017-03-26 14:18:59 +020078set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
79
Haraldc123a692019-04-01 18:00:31 +020080if(CMAKE_SYSTEM_NAME STREQUAL Linux)
81 include(GNUInstallDirs)
82 set(include_install_dir ${CMAKE_INSTALL_INCLUDEDIR})
83 set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
84else()
85 set(include_install_dir "include")
86 set(config_install_dir "lib/cmake/${PROJECT_NAME}")
87endif()
88
Giuseppe Robertie01299b2017-03-26 14:18:59 +020089
90set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
91set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
92set(targets_export_name "${PROJECT_NAME}Targets")
93set(namespace "${PROJECT_NAME}::")
94
95include(CMakePackageConfigHelpers)
Morris Hafnerf9d5d6a2019-04-16 06:34:07 +010096
97# CMake automatically adds an architecture compatibility check to make sure
98# 32 and 64 bit code is not accidentally mixed. For a header-only library this
99# is not required. The check can be disabled by temporarily unsetting
100# CMAKE_SIZEOF_VOID_P. In CMake 3.14 and later this can be achieved more cleanly
101# with write_basic_package_version_file(ARCH_INDEPENDENT).
102# TODO: Use this once a newer CMake can be required.
103set(DOCTEST_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
104unset(CMAKE_SIZEOF_VOID_P)
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200105write_basic_package_version_file(
Giuseppe Robertic89cf822017-04-12 17:14:17 +0200106 "${version_config}" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200107)
Morris Hafnerf9d5d6a2019-04-16 06:34:07 +0100108set(CMAKE_SIZEOF_VOID_P ${DOCTEST_SIZEOF_VOID_P})
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200109
onqtam08cf7dc2017-04-16 11:37:22 +0300110configure_file("scripts/cmake/Config.cmake.in" "${project_config}" @ONLY)
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200111
onqtam17c97382019-03-24 09:24:02 +0200112if(NOT ${DOCTEST_NO_INSTALL})
NeverMine17b63bf852017-10-28 16:06:43 +0300113 install(
114 TARGETS ${PROJECT_NAME}
115 EXPORT "${targets_export_name}"
116 INCLUDES DESTINATION "${include_install_dir}"
117 )
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200118
NeverMine17b63bf852017-10-28 16:06:43 +0300119 install(
onqtam36e37452019-03-02 20:10:05 +0200120 FILES "doctest/doctest.h"
ncihnegnbc43fe42019-01-27 05:28:54 -0800121 DESTINATION "${include_install_dir}/doctest"
NeverMine17b63bf852017-10-28 16:06:43 +0300122 )
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200123
NeverMine17b63bf852017-10-28 16:06:43 +0300124 install(
125 FILES "${project_config}" "${version_config}"
126 DESTINATION "${config_install_dir}"
127 )
Giuseppe Robertie01299b2017-03-26 14:18:59 +0200128
NeverMine17b63bf852017-10-28 16:06:43 +0300129 install(
Cristian Morales Vega35b339f2019-05-19 20:12:41 +0100130 FILES "scripts/cmake/doctest.cmake" "scripts/cmake/doctestAddTests.cmake"
131 DESTINATION "${config_install_dir}"
132 )
133
134 install(
NeverMine17b63bf852017-10-28 16:06:43 +0300135 EXPORT "${targets_export_name}"
136 NAMESPACE "${namespace}"
137 DESTINATION "${config_install_dir}"
138 )
139endif()