CMakeLists.txt 7.6 KB

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