112391Sjason@lowepower.com# - Find the Catch test framework or download it (single header)
212391Sjason@lowepower.com#
312391Sjason@lowepower.com# This is a quick module for internal use. It assumes that Catch is
412391Sjason@lowepower.com# REQUIRED and that a minimum version is provided (not EXACT). If
512391Sjason@lowepower.com# a suitable version isn't found locally, the single header file
612391Sjason@lowepower.com# will be downloaded and placed in the build dir: PROJECT_BINARY_DIR.
712391Sjason@lowepower.com#
812391Sjason@lowepower.com# This code sets the following variables:
912391Sjason@lowepower.com#  CATCH_INCLUDE_DIR      - path to catch.hpp
1012391Sjason@lowepower.com#  CATCH_VERSION          - version number
1112391Sjason@lowepower.com
1212391Sjason@lowepower.comif(NOT Catch_FIND_VERSION)
1312391Sjason@lowepower.com  message(FATAL_ERROR "A version number must be specified.")
1412391Sjason@lowepower.comelseif(Catch_FIND_REQUIRED)
1512391Sjason@lowepower.com  message(FATAL_ERROR "This module assumes Catch is not required.")
1612391Sjason@lowepower.comelseif(Catch_FIND_VERSION_EXACT)
1712391Sjason@lowepower.com  message(FATAL_ERROR "Exact version numbers are not supported, only minimum.")
1812391Sjason@lowepower.comendif()
1912391Sjason@lowepower.com
2012391Sjason@lowepower.com# Extract the version number from catch.hpp
2112391Sjason@lowepower.comfunction(_get_catch_version)
2212391Sjason@lowepower.com  file(STRINGS "${CATCH_INCLUDE_DIR}/catch.hpp" version_line REGEX "Catch v.*" LIMIT_COUNT 1)
2312391Sjason@lowepower.com  if(version_line MATCHES "Catch v([0-9]+)\\.([0-9]+)\\.([0-9]+)")
2412391Sjason@lowepower.com    set(CATCH_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" PARENT_SCOPE)
2512391Sjason@lowepower.com  endif()
2612391Sjason@lowepower.comendfunction()
2712391Sjason@lowepower.com
2812391Sjason@lowepower.com# Download the single-header version of Catch
2912391Sjason@lowepower.comfunction(_download_catch version destination_dir)
3012391Sjason@lowepower.com  message(STATUS "Downloading catch v${version}...")
3112391Sjason@lowepower.com  set(url https://github.com/philsquared/Catch/releases/download/v${version}/catch.hpp)
3212391Sjason@lowepower.com  file(DOWNLOAD ${url} "${destination_dir}/catch.hpp" STATUS status)
3312391Sjason@lowepower.com  list(GET status 0 error)
3412391Sjason@lowepower.com  if(error)
3512391Sjason@lowepower.com    message(FATAL_ERROR "Could not download ${url}")
3612391Sjason@lowepower.com  endif()
3712391Sjason@lowepower.com  set(CATCH_INCLUDE_DIR "${destination_dir}" CACHE INTERNAL "")
3812391Sjason@lowepower.comendfunction()
3912391Sjason@lowepower.com
4012391Sjason@lowepower.com# Look for catch locally
4112391Sjason@lowepower.comfind_path(CATCH_INCLUDE_DIR NAMES catch.hpp PATH_SUFFIXES catch)
4212391Sjason@lowepower.comif(CATCH_INCLUDE_DIR)
4312391Sjason@lowepower.com  _get_catch_version()
4412391Sjason@lowepower.comendif()
4512391Sjason@lowepower.com
4612391Sjason@lowepower.com# Download the header if it wasn't found or if it's outdated
4712391Sjason@lowepower.comif(NOT CATCH_VERSION OR CATCH_VERSION VERSION_LESS ${Catch_FIND_VERSION})
4812391Sjason@lowepower.com  if(DOWNLOAD_CATCH)
4912391Sjason@lowepower.com    _download_catch(${Catch_FIND_VERSION} "${PROJECT_BINARY_DIR}/catch/")
5012391Sjason@lowepower.com    _get_catch_version()
5112391Sjason@lowepower.com  else()
5212391Sjason@lowepower.com    set(CATCH_FOUND FALSE)
5312391Sjason@lowepower.com    return()
5412391Sjason@lowepower.com  endif()
5512391Sjason@lowepower.comendif()
5612391Sjason@lowepower.com
5712391Sjason@lowepower.comset(CATCH_FOUND TRUE)
58