CMakeLists.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. cmake_minimum_required(VERSION 3.10)
  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. option(CATCH_INSTALL_DOCS "Install documentation alongside library" ON)
  10. option(CATCH_INSTALL_EXTRAS "Install extras (CMake scripts, debugger helpers) alongside library" ON)
  11. option(CATCH_DEVELOPMENT_BUILD "Build tests, enable warnings, enable Werror, etc" OFF)
  12. include(CMakeDependentOption)
  13. cmake_dependent_option(CATCH_BUILD_TESTING "Build the SelfTest project" ON "CATCH_DEVELOPMENT_BUILD" OFF)
  14. cmake_dependent_option(CATCH_BUILD_EXAMPLES "Build code examples" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
  15. cmake_dependent_option(CATCH_BUILD_EXTRA_TESTS "Build extra tests" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
  16. cmake_dependent_option(CATCH_BUILD_FUZZERS "Build fuzzers" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
  17. cmake_dependent_option(CATCH_ENABLE_COVERAGE "Generate coverage for codecov.io" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
  18. cmake_dependent_option(CATCH_ENABLE_WERROR "Enables Werror during build" ON "CATCH_DEVELOPMENT_BUILD" OFF)
  19. cmake_dependent_option(CATCH_BUILD_SURROGATES "Enable generating and building surrogate TUs for the main headers" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
  20. cmake_dependent_option(CATCH_ENABLE_CONFIGURE_TESTS "Enable CMake configuration tests. WARNING: VERY EXPENSIVE" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
  21. # Catch2's build breaks if done in-tree. You probably should not build
  22. # things in tree anyway, but we can allow projects that include Catch2
  23. # as a subproject to build in-tree as long as it is not in our tree.
  24. if (CMAKE_BINARY_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  25. message(FATAL_ERROR "Building in-source is not supported! Create a build dir and remove ${CMAKE_SOURCE_DIR}/CMakeCache.txt")
  26. endif()
  27. project(Catch2
  28. VERSION 3.1.1 # CML version placeholder, don't delete
  29. LANGUAGES CXX
  30. # HOMEPAGE_URL is not supported until CMake version 3.12, which
  31. # we do not target yet.
  32. # HOMEPAGE_URL "https://github.com/catchorg/Catch2"
  33. DESCRIPTION "A modern, C++-native, unit test framework."
  34. )
  35. # Provide path for scripts. We first add path to the scripts we don't use,
  36. # but projects including us might, and set the path up to parent scope.
  37. # Then we also add path that we use to configure the project, but is of
  38. # no use to top level projects.
  39. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/extras")
  40. if (NOT NOT_SUBPROJECT)
  41. set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" PARENT_SCOPE)
  42. endif()
  43. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
  44. include(GNUInstallDirs)
  45. include(CMakePackageConfigHelpers)
  46. include(CatchConfigOptions)
  47. if(CATCH_DEVELOPMENT_BUILD)
  48. include(CTest)
  49. endif()
  50. # This variable is used in some subdirectories, so we need it here, rather
  51. # than later in the install block
  52. set(CATCH_CMAKE_CONFIG_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/Catch2")
  53. # We have some Windows builds that test `wmain` entry point,
  54. # and we need this change to be present in all binaries that
  55. # are built during these tests, so this is required here, before
  56. # the subdirectories are added.
  57. if(CATCH_TEST_USE_WMAIN)
  58. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:wmainCRTStartup")
  59. endif()
  60. # Basic paths
  61. set(CATCH_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  62. set(SOURCES_DIR ${CATCH_DIR}/src/catch2)
  63. set(SELF_TEST_DIR ${CATCH_DIR}/tests/SelfTest)
  64. set(BENCHMARK_DIR ${CATCH_DIR}/tests/Benchmark)
  65. set(EXAMPLES_DIR ${CATCH_DIR}/examples)
  66. # We need to bring-in the variables defined there to this scope
  67. add_subdirectory(src)
  68. # Build tests only if requested
  69. if (BUILD_TESTING AND CATCH_BUILD_TESTING AND NOT_SUBPROJECT)
  70. find_package(PythonInterp 3 REQUIRED)
  71. if (NOT PYTHONINTERP_FOUND)
  72. message(FATAL_ERROR "Python not found, but required for tests")
  73. endif()
  74. add_subdirectory(tests)
  75. endif()
  76. if(CATCH_BUILD_EXAMPLES)
  77. add_subdirectory(examples)
  78. endif()
  79. if(CATCH_BUILD_EXTRA_TESTS)
  80. add_subdirectory(tests/ExtraTests)
  81. endif()
  82. if(CATCH_BUILD_FUZZERS)
  83. add_subdirectory(fuzzing)
  84. endif()
  85. if (CATCH_DEVELOPMENT_BUILD)
  86. add_warnings_to_targets("${CATCH_WARNING_TARGETS}")
  87. endif()
  88. # Only perform the installation steps when Catch is not being used as
  89. # a subproject via `add_subdirectory`, or the destinations will break,
  90. # see https://github.com/catchorg/Catch2/issues/1373
  91. if (NOT_SUBPROJECT)
  92. configure_package_config_file(
  93. ${CMAKE_CURRENT_LIST_DIR}/CMake/Catch2Config.cmake.in
  94. ${CMAKE_CURRENT_BINARY_DIR}/Catch2Config.cmake
  95. INSTALL_DESTINATION
  96. ${CATCH_CMAKE_CONFIG_DESTINATION}
  97. )
  98. write_basic_package_version_file(
  99. "${CMAKE_CURRENT_BINARY_DIR}/Catch2ConfigVersion.cmake"
  100. COMPATIBILITY
  101. SameMajorVersion
  102. )
  103. install(
  104. FILES
  105. "${CMAKE_CURRENT_BINARY_DIR}/Catch2Config.cmake"
  106. "${CMAKE_CURRENT_BINARY_DIR}/Catch2ConfigVersion.cmake"
  107. DESTINATION
  108. ${CATCH_CMAKE_CONFIG_DESTINATION}
  109. )
  110. # Install documentation
  111. if(CATCH_INSTALL_DOCS)
  112. install(
  113. DIRECTORY
  114. docs/
  115. DESTINATION
  116. "${CMAKE_INSTALL_DOCDIR}"
  117. PATTERN "doxygen" EXCLUDE
  118. )
  119. endif()
  120. if(CATCH_INSTALL_EXTRAS)
  121. # Install CMake scripts
  122. install(
  123. FILES
  124. "extras/ParseAndAddCatchTests.cmake"
  125. "extras/Catch.cmake"
  126. "extras/CatchAddTests.cmake"
  127. DESTINATION
  128. ${CATCH_CMAKE_CONFIG_DESTINATION}
  129. )
  130. # Install debugger helpers
  131. install(
  132. FILES
  133. "extras/gdbinit"
  134. "extras/lldbinit"
  135. DESTINATION
  136. ${CMAKE_INSTALL_DATAROOTDIR}/Catch2
  137. )
  138. endif()
  139. ## Provide some pkg-config integration
  140. set(PKGCONFIG_INSTALL_DIR
  141. "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig"
  142. CACHE PATH "Path where catch2.pc is installed"
  143. )
  144. configure_file(
  145. ${CMAKE_CURRENT_SOURCE_DIR}/CMake/catch2.pc.in
  146. ${CMAKE_CURRENT_BINARY_DIR}/catch2.pc
  147. @ONLY
  148. )
  149. configure_file(
  150. ${CMAKE_CURRENT_SOURCE_DIR}/CMake/catch2-with-main.pc.in
  151. ${CMAKE_CURRENT_BINARY_DIR}/catch2-with-main.pc
  152. @ONLY
  153. )
  154. install(
  155. FILES
  156. "${CMAKE_CURRENT_BINARY_DIR}/catch2.pc"
  157. "${CMAKE_CURRENT_BINARY_DIR}/catch2-with-main.pc"
  158. DESTINATION
  159. ${PKGCONFIG_INSTALL_DIR}
  160. )
  161. # CPack/CMake started taking the package version from project version 3.12
  162. # So we need to set the version manually for older CMake versions
  163. if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
  164. set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
  165. endif()
  166. set(CPACK_PACKAGE_CONTACT "https://github.com/catchorg/Catch2/")
  167. include( CPack )
  168. endif(NOT_SUBPROJECT)