FindCatch.cmake 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # - Find the Catch test framework or download it (single header)
  2. #
  3. # This is a quick module for internal use. It assumes that Catch is
  4. # REQUIRED and that a minimum version is provided (not EXACT). If
  5. # a suitable version isn't found locally, the single header file
  6. # will be downloaded and placed in the build dir: PROJECT_BINARY_DIR.
  7. #
  8. # This code sets the following variables:
  9. # CATCH_INCLUDE_DIR - path to catch.hpp
  10. # CATCH_VERSION - version number
  11. if(NOT Catch_FIND_VERSION)
  12. message(FATAL_ERROR "A version number must be specified.")
  13. elseif(Catch_FIND_REQUIRED)
  14. message(FATAL_ERROR "This module assumes Catch is not required.")
  15. elseif(Catch_FIND_VERSION_EXACT)
  16. message(FATAL_ERROR "Exact version numbers are not supported, only minimum.")
  17. endif()
  18. # Extract the version number from catch.hpp
  19. function(_get_catch_version)
  20. file(
  21. STRINGS "${CATCH_INCLUDE_DIR}/catch.hpp" version_line
  22. REGEX "Catch v.*"
  23. LIMIT_COUNT 1)
  24. if(version_line MATCHES "Catch v([0-9]+)\\.([0-9]+)\\.([0-9]+)")
  25. set(CATCH_VERSION
  26. "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}"
  27. PARENT_SCOPE)
  28. endif()
  29. endfunction()
  30. # Download the single-header version of Catch
  31. function(_download_catch version destination_dir)
  32. message(STATUS "Downloading catch v${version}...")
  33. set(url https://github.com/philsquared/Catch/releases/download/v${version}/catch.hpp)
  34. file(DOWNLOAD ${url} "${destination_dir}/catch.hpp" STATUS status)
  35. list(GET status 0 error)
  36. if(error)
  37. message(FATAL_ERROR "Could not download ${url}")
  38. endif()
  39. set(CATCH_INCLUDE_DIR
  40. "${destination_dir}"
  41. CACHE INTERNAL "")
  42. endfunction()
  43. # Look for catch locally
  44. find_path(
  45. CATCH_INCLUDE_DIR
  46. NAMES catch.hpp
  47. PATH_SUFFIXES catch2)
  48. if(CATCH_INCLUDE_DIR)
  49. _get_catch_version()
  50. endif()
  51. # Download the header if it wasn't found or if it's outdated
  52. if(NOT CATCH_VERSION OR CATCH_VERSION VERSION_LESS ${Catch_FIND_VERSION})
  53. if(DOWNLOAD_CATCH)
  54. _download_catch(${Catch_FIND_VERSION} "${PROJECT_BINARY_DIR}/catch/")
  55. _get_catch_version()
  56. else()
  57. set(CATCH_FOUND FALSE)
  58. return()
  59. endif()
  60. endif()
  61. add_library(Catch2::Catch2 IMPORTED INTERFACE)
  62. set_property(TARGET Catch2::Catch2 PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CATCH_INCLUDE_DIR}")
  63. set(CATCH_FOUND TRUE)