• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.5)
2
3# detect if Catch is being bundled,
4# disable testsuite in that case
5if(NOT DEFINED PROJECT_NAME)
6  set(NOT_SUBPROJECT ON)
7endif()
8
9project(Catch2 LANGUAGES CXX VERSION 2.7.0)
10
11# Provide path for scripts
12list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
13
14include(GNUInstallDirs)
15include(CMakePackageConfigHelpers)
16include(CTest)
17
18option(CATCH_USE_VALGRIND "Perform SelfTests with Valgrind" OFF)
19option(CATCH_BUILD_TESTING "Build SelfTest project" ON)
20option(CATCH_BUILD_EXAMPLES "Build documentation examples" OFF)
21option(CATCH_BUILD_EXTRA_TESTS "Build extra tests" OFF)
22option(CATCH_ENABLE_COVERAGE "Generate coverage for codecov.io" OFF)
23option(CATCH_ENABLE_WERROR "Enable all warnings as errors" ON)
24option(CATCH_INSTALL_DOCS "Install documentation alongside library" ON)
25option(CATCH_INSTALL_HELPERS "Install contrib alongside library" ON)
26
27
28set_property(GLOBAL PROPERTY USE_FOLDERS ON)
29
30# define some folders
31set(CATCH_DIR ${CMAKE_CURRENT_SOURCE_DIR})
32set(SELF_TEST_DIR ${CATCH_DIR}/projects/SelfTest)
33set(BENCHMARK_DIR ${CATCH_DIR}/projects/Benchmark)
34set(HEADER_DIR ${CATCH_DIR}/include)
35
36if(USE_WMAIN)
37    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:wmainCRTStartup")
38endif()
39
40if (BUILD_TESTING AND CATCH_BUILD_TESTING AND NOT_SUBPROJECT)
41    find_package(PythonInterp)
42    if (NOT PYTHONINTERP_FOUND)
43        message(FATAL_ERROR "Python not found, but required for tests")
44    endif()
45    add_subdirectory(projects)
46endif()
47
48if(CATCH_BUILD_EXAMPLES)
49    add_subdirectory(examples)
50endif()
51
52if(CATCH_BUILD_EXTRA_TESTS)
53    add_subdirectory(projects/ExtraTests)
54endif()
55
56# add catch as a 'linkable' target
57add_library(Catch2 INTERFACE)
58
59
60
61# depend on some obvious c++11 features so the dependency is transitively added dependents
62target_compile_features(Catch2
63  INTERFACE
64    cxx_alignas
65    cxx_alignof
66    cxx_attributes
67    cxx_auto_type
68    cxx_constexpr
69    cxx_defaulted_functions
70    cxx_deleted_functions
71    cxx_final
72    cxx_lambdas
73    cxx_noexcept
74    cxx_override
75    cxx_range_for
76    cxx_rvalue_references
77    cxx_static_assert
78    cxx_strong_enums
79    cxx_trailing_return_types
80    cxx_unicode_literals
81    cxx_user_literals
82    cxx_variadic_macros
83)
84
85target_include_directories(Catch2
86  INTERFACE
87    $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/single_include>
88    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
89)
90
91# provide a namespaced alias for clients to 'link' against if catch is included as a sub-project
92add_library(Catch2::Catch2 ALIAS Catch2)
93
94# Only perform the installation steps when Catch is not being used as
95# a subproject via `add_subdirectory`, or the destinations will break,
96# see https://github.com/catchorg/Catch2/issues/1373
97if (NOT_SUBPROJECT)
98    set(CATCH_CMAKE_CONFIG_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/Catch2")
99
100    configure_package_config_file(
101        ${CMAKE_CURRENT_LIST_DIR}/CMake/Catch2Config.cmake.in
102        ${CMAKE_CURRENT_BINARY_DIR}/Catch2Config.cmake
103        INSTALL_DESTINATION
104          ${CATCH_CMAKE_CONFIG_DESTINATION}
105    )
106
107
108    # create and install an export set for catch target as Catch2::Catch
109    install(
110      TARGETS
111        Catch2
112      EXPORT
113        Catch2Targets
114      DESTINATION
115        ${CMAKE_INSTALL_LIBDIR}
116    )
117
118
119    install(
120      EXPORT
121        Catch2Targets
122      NAMESPACE
123        Catch2::
124      DESTINATION
125        ${CATCH_CMAKE_CONFIG_DESTINATION}
126    )
127
128    # By default, FooConfigVersion is tied to architecture that it was
129    # generated on. Because Catch2 is header-only, it is arch-independent
130    # and thus Catch2ConfigVersion should not be tied to the architecture
131    # it was generated on.
132    #
133    # CMake does not provide a direct customization point for this in
134    # `write_basic_package_version_file`, but it can be accomplished
135    # indirectly by temporarily undefining `CMAKE_SIZEOF_VOID_P`.
136    set(CATCH2_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
137    unset(CMAKE_SIZEOF_VOID_P)
138    write_basic_package_version_file(
139      "${CMAKE_CURRENT_BINARY_DIR}/Catch2ConfigVersion.cmake"
140      COMPATIBILITY
141        SameMajorVersion
142    )
143    set(CMAKE_SIZEOF_VOID_P ${CATCH2_CMAKE_SIZEOF_VOID_P})
144
145    install(
146      DIRECTORY
147        "single_include/"
148      DESTINATION
149        "${CMAKE_INSTALL_INCLUDEDIR}"
150    )
151
152    install(
153      FILES
154        "${CMAKE_CURRENT_BINARY_DIR}/Catch2Config.cmake"
155        "${CMAKE_CURRENT_BINARY_DIR}/Catch2ConfigVersion.cmake"
156      DESTINATION
157        ${CATCH_CMAKE_CONFIG_DESTINATION}
158    )
159
160    # Install documentation
161    if(CATCH_INSTALL_DOCS)
162      install(
163        DIRECTORY
164          docs/
165        DESTINATION
166          "${CMAKE_INSTALL_DOCDIR}"
167      )
168    endif()
169
170    if(CATCH_INSTALL_HELPERS)
171    # Install CMake scripts
172    install(
173      FILES
174        "contrib/ParseAndAddCatchTests.cmake"
175        "contrib/Catch.cmake"
176        "contrib/CatchAddTests.cmake"
177      DESTINATION
178        ${CATCH_CMAKE_CONFIG_DESTINATION}
179    )
180
181    # Install debugger helpers
182    install(
183      FILES
184        "contrib/gdbinit"
185        "contrib/lldbinit"
186      DESTINATION
187        ${CMAKE_INSTALL_DATAROOTDIR}/Catch2
188    )
189    endif()
190
191    ## Provide some pkg-config integration
192    set(PKGCONFIG_INSTALL_DIR
193        "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig"
194        CACHE PATH "Path where catch2.pc is installed"
195    )
196    configure_file(
197      ${CMAKE_CURRENT_SOURCE_DIR}/CMake/catch2.pc.in
198      ${CMAKE_CURRENT_BINARY_DIR}/catch2.pc
199      @ONLY
200    )
201    install(
202      FILES
203        "${CMAKE_CURRENT_BINARY_DIR}/catch2.pc"
204      DESTINATION
205        ${PKGCONFIG_INSTALL_DIR}
206    )
207
208endif(NOT_SUBPROJECT)
209