Skip to content

Commit cc3a7cc

Browse files
committed
Add CMakeLists.txt, defining a Catch CMake target, and documenting its usage
1 parent f666f5f commit cc3a7cc

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

CMakeLists.txt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#[[
2+
3+
Catch, Formlabs fork
4+
====================
5+
6+
Usage
7+
-----
8+
9+
* Include the directory containing this CMakeLists.txt into your project.
10+
* Link your test binaries against the build target, `Catch`.
11+
* The header `catch.hpp` should be visible in the sources of those bianries.
12+
* Use Catch as normal.
13+
14+
More details
15+
------------
16+
17+
This CMakeLists.txt will check whether there is already a target called `Catch`
18+
defined. If so, it will try to determine whether that version of Catch is
19+
compatible with this one. The way this is done is that the `_CATCH_GIT_REVISION`
20+
property of the target is queried, and compared for equality with the Git
21+
revision of this repository. If that target property is not found, or does not
22+
match this repository's Git revision, then a warning message is printed.
23+
24+
If no `Catch` target exists, one will be defined as normal.
25+
26+
This is all done to support, for example, the scenario that your repository uses
27+
submodules A and B, and both A and B use Catch. If both A and B include Catch
28+
using this repository, and their Catch revisions are in sync, there will be no
29+
problem. If the revisions are out-of-sync, then you will get a warning message,
30+
and if the Catch versions are actually incompatible then your test code may
31+
break.
32+
33+
]]
34+
35+
cmake_minimum_required (VERSION 3.4)
36+
37+
find_package (Git REQUIRED)
38+
39+
execute_process (
40+
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
41+
OUTPUT_VARIABLE _CATCH_GIT_REVISION
42+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
43+
)
44+
45+
string (STRIP "${_CATCH_GIT_REVISION}" _CATCH_GIT_REVISION)
46+
47+
if (TARGET Catch)
48+
get_target_property (_OTHER_CATCH_INCLUDE_DIRECTORIES
49+
Catch INTERFACE_INCLUDE_DIRECTORIES
50+
)
51+
52+
list (GET _OTHER_CATCH_INCLUDE_DIRECTORIES 0 _OTHER_CATCH_INCLUDE_DIRECTORY)
53+
54+
execute_process (
55+
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
56+
OUTPUT_VARIABLE _OTHER_CATCH_GIT_REVISION
57+
WORKING_DIRECTORY ${_OTHER_CATCH_INCLUDE_DIRECTORY}
58+
)
59+
60+
string (STRIP "${_OTHER_CATCH_GIT_REVISION}" _OTHER_CATCH_GIT_REVISION)
61+
62+
if (NOT ${_CATCH_GIT_REVISION} STREQUAL ${_OTHER_CATCH_GIT_REVISION})
63+
message (WARNING "
64+
Potentially incompatible versions of Catch found!
65+
The revision at ${CMAKE_CURRENT_SOURCE_DIR} is ${_CATCH_GIT_REVISION}.
66+
The revision of the existing Catch target is ${_OTHER_CATCH_GIT_REVISION}.
67+
The include directories of the existing Catch target are ${_OTHER_CATCH_INCLUDE_DIRECTORIES}.
68+
The other Catch target is the one that will be used.
69+
")
70+
endif ()
71+
else ()
72+
add_library (Catch INTERFACE)
73+
target_include_directories (Catch INTERFACE single_include)
74+
endif ()

0 commit comments

Comments
 (0)