-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
62 lines (50 loc) · 1.69 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
cmake_minimum_required(VERSION 3.11.0)
project(my_project)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# include and configure catch2
include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.0-preview3
)
FetchContent_MakeAvailable(Catch2)
# macro to find subdirectories
# https://stackoverflow.com/a/7788165
MACRO(SUBDIRLIST result curdir)
FILE(GLOB children CONFIGURE_DEPENDS ${curdir}/*)
SET(dirlist "")
FOREACH(child ${children})
IF(IS_DIRECTORY ${child})
LIST(APPEND dirlist ${child})
ENDIF()
ENDFOREACH()
SET(${result} ${dirlist})
ENDMACRO()
# find subdirectories in src that contain code for classes
SUBDIRLIST(SOURCE_DIRECTORIES src)
# in each subdirectory, find cpp files and add them to the source list
set(SOURCES "")
set(TEST_SOURCES "")
FOREACH(subdir ${SOURCE_DIRECTORIES})
FILE(GLOB sources CONFIGURE_DEPENDS ${subdir}/*.cpp)
FILE(GLOB test_sources CONFIGURE_DEPENDS ${subdir}/test*.cpp)
LIST(APPEND SOURCES ${sources})
IF(test_sources)
LIST(REMOVE_ITEM SOURCES ${test_sources})
ENDIF()
LIST(APPEND TEST_SOURCES ${test_sources})
ENDFOREACH()
# include the source directories
include_directories(${SOURCE_DIRECTORIES})
# create a build target for the main project file
add_executable(main src/main.cpp ${SOURCES})
# create a build target for the testing file
add_executable(tests src/tests.cpp ${TEST_SOURCES} ${SOURCES})
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
# initialize catch2
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(tests)