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