This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
80 lines (67 loc) · 2.41 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
cmake_minimum_required(VERSION 3.12)
# ---- Project ----
project(
cpphw
VERSION 1.0
LANGUAGES C CXX)
# ---- Env var for SonarLint ----
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# ---- C++ standard ----
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# ---- Include guards ----
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(
FATAL_ERROR
"In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there."
)
endif()
# ---- Function to create executables from source files in a directory ----
function(create_executables DIRECTORY)
file(GLOB_RECURSE SRC_FILES "${DIRECTORY}/*.cpp")
foreach(SRC_FILE ${SRC_FILES})
get_filename_component(EXE_NAME ${SRC_FILE} NAME_WE)
add_executable(${DIRECTORY}${EXE_NAME} ${SRC_FILE})
set_target_properties(${DIRECTORY}${EXE_NAME} PROPERTIES CXX_STANDARD 20)
endforeach()
endfunction()
# ---- Create executables for subdirectories ----
set(SUBDIRECTORIES loops functions arrays)
foreach(SUBDIR ${SUBDIRECTORIES})
create_executables(${SUBDIR})
endforeach()
# ---- Add googletest ----
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
# ---- Enable testing ----
enable_testing()
# ---- Special case for 'date' directory ----
add_executable(date date/date.cpp date/tests.cpp)
target_include_directories(date PRIVATE date)
target_link_libraries(date PRIVATE GTest::gtest_main)
set_target_properties(date PROPERTIES CXX_STANDARD 20)
include(GoogleTest)
gtest_discover_tests(date)
# ---- Special case for 'shapes' directory ----
file(GLOB_RECURSE SHAPES_SRC_FILES "shapes/*.cpp")
add_executable(shapes ${SHAPES_SRC_FILES})
target_include_directories(shapes PRIVATE shapes)
target_link_libraries(shapes PRIVATE GTest::gtest_main)
set_target_properties(shapes PROPERTIES CXX_STANDARD 20)
include(GoogleTest)
gtest_discover_tests(shapes)
# ---- Special case for 'matrix' directory ----
add_executable(matrix matrix/tests.cpp)
target_include_directories(matrix PRIVATE matrix)
target_link_libraries(matrix PRIVATE GTest::gtest_main)
set_target_properties(matrix PROPERTIES CXX_STANDARD 20)
include(GoogleTest)
gtest_discover_tests(matrix)