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
12if(NOT Catch_FIND_VERSION)
13  message(FATAL_ERROR "A version number must be specified.")
14elseif(Catch_FIND_REQUIRED)
15  message(FATAL_ERROR "This module assumes Catch is not required.")
16elseif(Catch_FIND_VERSION_EXACT)
17  message(FATAL_ERROR "Exact version numbers are not supported, only minimum.")
18endif()
19
20# Extract the version number from catch.hpp
21function(_get_catch_version)
22  file(STRINGS "${CATCH_INCLUDE_DIR}/catch.hpp" version_line REGEX "Catch v.*" LIMIT_COUNT 1)
23  if(version_line MATCHES "Catch v([0-9]+)\\.([0-9]+)\\.([0-9]+)")
24    set(CATCH_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" PARENT_SCOPE)
25  endif()
26endfunction()
27
28# Download the single-header version of Catch
29function(_download_catch version destination_dir)
30  message(STATUS "Downloading catch v${version}...")
31  set(url https://github.com/philsquared/Catch/releases/download/v${version}/catch.hpp)
32  file(DOWNLOAD ${url} "${destination_dir}/catch.hpp" STATUS status)
33  list(GET status 0 error)
34  if(error)
35    message(FATAL_ERROR "Could not download ${url}")
36  endif()
37  set(CATCH_INCLUDE_DIR "${destination_dir}" CACHE INTERNAL "")
38endfunction()
39
40# Look for catch locally
41find_path(CATCH_INCLUDE_DIR NAMES catch.hpp PATH_SUFFIXES catch)
42if(CATCH_INCLUDE_DIR)
43  _get_catch_version()
44endif()
45
46# Download the header if it wasn't found or if it's outdated
47if(NOT CATCH_VERSION OR CATCH_VERSION VERSION_LESS ${Catch_FIND_VERSION})
48  if(DOWNLOAD_CATCH)
49    _download_catch(${Catch_FIND_VERSION} "${PROJECT_BINARY_DIR}/catch/")
50    _get_catch_version()
51  else()
52    set(CATCH_FOUND FALSE)
53    return()
54  endif()
55endif()
56
57set(CATCH_FOUND TRUE)
58