Skip to content

Commit

Permalink
Merge pull request UCL#65 from dpshelio/catchVersion
Browse files Browse the repository at this point in the history
Catch version selector
  • Loading branch information
dpshelio authored Sep 11, 2018
2 parents 13bc9b6 + fe5cd50 commit ca83198
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 31 deletions.
13 changes: 10 additions & 3 deletions modules/FindCatch.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ if(Catch_FOUND)
return()
endif()

find_path(CATCH_INCLUDE_DIR catch.hpp PATHS ${EXTERNAL_ROOT}/include)
find_path(CATCH_INCLUDE_DIR catch.hpp PATHS /usr/include ${EXTERNAL_ROOT}/include PATH_SUFFIXES catch2)
# <package>_FIND_VERSION var dessapears after the first time this runs
set(Catch_WANTED_VERSION ${Catch_FIND_VERSION})

if(CATCH_INCLUDE_DIR)
file(
STRINGS ${CATCH_INCLUDE_DIR}/catch.hpp
Expand All @@ -32,8 +35,12 @@ if(CATCH_INCLUDE_DIR)
)
endif()

set(CATCH_INCLUDE_DIRS ${CATCH_INCLUDE_DIR} )
set(Catch_INCLUDE_DIRS ${CATCH_INCLUDE_DIR} )
if(Catch_FIND_VERSION AND (NOT "${CATCH_VERSION_STRING}" STREQUAL "${Catch_FIND_VERSION}"))
set(CATCH_INCLUDE_DIR "")
else()
set(CATCH_INCLUDE_DIRS ${CATCH_INCLUDE_DIR} )
set(Catch_INCLUDE_DIRS ${CATCH_INCLUDE_DIR} )
endif()

if(CATCH_INCLUDE_DIR)
try_run(CATCH_RUNS CATCH_COMPILES
Expand Down
69 changes: 69 additions & 0 deletions modules/LookUpCatch.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Installs catchorg/Catch2 into build directory
#
# - URL: defaults to latest single_include
# - VERSION: defaults to latest

if(Catch_ARGUMENTS)
cmake_parse_arguments(Catch "" "URL;VERSION" ""
${Catch_ARGUMENTS})
endif()

if(NOT Catch_VERSION)
set(Catch_URL_VERSION master)
else()
set (Catch_URL_VERSION "v${Catch_VERSION}")
endif()

if(NOT Catch_URL)
set(Catch_URL https://raw.githubusercontent.com/catchorg/Catch2/${Catch_URL_VERSION}/single_include/catch2/catch.hpp)
endif()

set(Catch_FILE "${EXTERNAL_ROOT}/include/catch.hpp")
file(MAKE_DIRECTORY "${EXTERNAL_ROOT}/include")
file(DOWNLOAD ${Catch_URL} "${Catch_FILE}")

# I imagine this is checking whether the file download
# is long enough. This could be the case that cmake hasn't
# been built with ssl support and a https download will fail.
file(READ "${Catch_FILE}" CATCHSTRING LIMIT 1000)
string(LENGTH "${CATCHSTRING}" CATCHLENGTH)

# In case the download fails with cmake then try with wget/curl
if(NOT CATCHLENGTH GREATER 500)
find_package(Wget)
if(WGET_FOUND)
execute_process(COMMAND ${WGET_EXECUTABLE}
${Catch_URL}
-O ${Catch_FILE}
)
else()
find_program(CURL_EXECUTABLE curl)
execute_process(COMMAND ${CURL_EXECUTABLE}
-L ${Catch_URL}
-o ${Catch_FILE}
)
endif()
endif()

file(READ "${Catch_FILE}" CATCHSTRING LIMIT 1000)
string(LENGTH "${CATCHSTRING}" CATCHLENGTH)
if(NOT CATCHLENGTH GREATER 500)
file(REMOVE "${Catch_FILE}")
message(FATAL_ERROR "Failed to download Catch ${CATCHSTRING} ${CATCHLENGTH}")
endif()


ExternalProject_Add(
Lookup-Catch
PREFIX "${EXTERNAL_ROOT}"
DOWNLOAD_COMMAND ""
# ARGUMENTS
# identification of correct lib in subsequent TARGET_LINK_LIBRARIES commands
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
# Wrap download, configure and build steps in a script to log output
UPDATE_COMMAND ""
LOG_DOWNLOAD ON LOG_CONFIGURE ON LOG_BUILD ON LOG_INSTALL ON
)

32 changes: 4 additions & 28 deletions scripts/AddCatchTest.cmake
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
# First finds or downloads catch
find_package(Catch)

if(NOT CATCH_FOUND)
# setups things so include can be found
include(PackageLookup)
set(catch_url
https://raw.githubusercontent.com/catchorg/Catch2/master/single_include/catch2/catch.hpp)
set(catch_file "${EXTERNAL_ROOT}/include/catch.hpp")
file(MAKE_DIRECTORY "${EXTERNAL_ROOT}/include")
file(DOWNLOAD ${catch_url} "${catch_file}")
file(READ "${catch_file}" CATCHSTRING LIMIT 1000)
string(LENGTH "${CATCHSTRING}" CATCHLENGTH)
if(NOT CATCHLENGTH GREATER 500)
# CMake can't download over https if build lacks ssl. So use wget or curl
find_package(Wget)
if(WGET_FOUND)
execute_process(COMMAND ${WGET_EXECUTABLE} ${catch_url} -O "${catch_file}")
else()
find_program(CURL_EXECUTABLE curl)
execute_process(COMMAND ${CURL_EXECUTABLE} -L ${catch_url} -o "${catch_file}")
endif()
file(READ "${catch_file}" CATCHSTRING LIMIT 1000)
string(LENGTH "${CATCHSTRING}" CATCHLENGTH)
if(NOT CATCHLENGTH GREATER 500)
file(REMOVE "${catch_file}")
message(FATAL_ERROR "Failed to download Catch ${CATCHSTRING} ${CATCHLENGTH}")
endif()
endif()
find_package(Catch REQUIRED)
if(NOT Catch_FOUND AND Catch_WANTED_VERSION)
lookup_package(Catch REQUIRED ARGUMENTS VERSION ${Catch_WANTED_VERSION})
elseif(NOT Catch_FOUND)
lookup_package(Catch REQUIRED)
endif()

# Function to create a common main
Expand Down

0 comments on commit ca83198

Please sign in to comment.