-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCMakeLists.txt
151 lines (118 loc) · 5.07 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
cmake_minimum_required(VERSION 3.17)
# # for vcpkg int ./tools/vcpkg
# set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_LIST_DIR}/tools/vcpkg/scripts/buildsystems/vcpkg.cmake)
include(./tools/function_script.cmake)
project(CoderbyteChallenges)
set(CMAKE_CXX_STANDARD 17) # was 20 but my current environment does not cope well (7.10.24), should make no difference whatsoever
set(CMAKE_CXX_STANDARD_REQUIRED True)
# ======================== testing ============================
# now using FetchContent
include(FetchContent)
FetchContent_Declare(
googletest
# link from googleTest docs https://google.github.io/googletest/quickstart-cmake.html
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()
set(test_flag_ "CODERBYTE_CHALLENGES_TEST_CPP_FLAG")
include(GoogleTest)
# ======================== testing =============================
# DummyInputLib
add_subdirectory(tools/DummyInputLib)
# ================== Cpp files globing ==========================
FILE(GLOB_RECURSE cpp_exe_paths_ "src/*.cpp")
prepare_exe_list(4 3 ${cpp_exe_paths_})
set(cpp_exes_ ${exes_})
set(exes_ "") # clear variable just in case
check_length(cpp_exe_paths_ cpp_exes_)
# message(DEBUG " cpp_exes_: ${cpp_exes_}")
# message(DEBUG " cpp_exe_paths_: ${cpp_exe_paths_}")
# ================= C files globing =============================
FILE(GLOB_RECURSE c_exe_paths_ "src/*.c")
prepare_exe_list(2 3 ${c_exe_paths_})
set(c_exes_ ${exes_})
set(exes_ "") # clear variable just in case
check_length(c_exe_paths_ c_exes_)
# message(DEBUG " c_exes_: ${c_exes_}")
# message(DEBUG " c_exe_paths_: ${c_exe_paths_}")
# test names should be the same as the name of the file in which the
# tested function exists
# ================ Cpp tests globing ============================
FILE(GLOB_RECURSE cpp_test_paths_ "tests/*_tests.cpp")
prepare_exe_list(4 5 ${cpp_test_paths_})
set(cpp_test_exes_ ${exes_})
set(exes_ "") # clear variable just in case
check_length(cpp_test_paths_ cpp_test_exes_)
message(DEBUG " cpp_test_exes_: ${cpp_test_exes_}")
message(DEBUG " cpp_test_paths_: ${cpp_test_paths_}")
# == add executables and set linking properties for c and cpp ===
# concatenate lists with executable names and source files' paths
# (append to empty lists)
list(APPEND concat_exe_paths_ ${cpp_exe_paths_} ${c_exe_paths_})
list(APPEND concat_exes_ ${cpp_exes_} ${c_exes_})
check_length(concat_exes_ concat_exe_paths_)
message(DEBUG " number of targets in src dir: ${iter_max_}")
# iterate over c and cpp targets
set(iter_ 0)
list(LENGTH concat_exes_ iter_max_)
while(iter_ LESS iter_max_)
# get exe name and source file path
list(GET concat_exes_ ${iter_} exe_name_)
list(GET concat_exe_paths_ ${iter_} src_path_)
# ==================== executables =========================
add_executable(${exe_name_} ${src_path_})
message(STATUS " ${exe_name_}: ${src_path_}")
# ==================== executables =========================
if(${src_path_} MATCHES "^.*\.(c)$")
# ======================.c linking =========================
target_link_libraries( ${exe_name_} PRIVATE ${input_lib_name_}C )
# ============= .c files compiler options ==================
target_compile_options( ${exe_name_} PRIVATE -include ${lib_header_name_})
# message(STATUS " Including: ${lib_header_name_}")
# ==========================================================
else()
# =====================.cpp linking ========================
target_link_libraries( ${exe_name_} PRIVATE ${input_lib_name_}Cpp )
# ============ .cpp files compiler options =================
target_compile_options( ${exe_name_} PRIVATE -include ${lib_header_name_})
# message(STATUS " Including: ${lib_header_name_}")
# ==========================================================
endif()
math(EXPR iter_ ${iter_}+1)
endwhile()
# === add executables and set linking properties for tests ====
set(iter_ 0)
list(LENGTH cpp_test_paths_ tests_num_)
if (tests_num_ GREATER 0)
message(STATUS "TESTS:")
endif()
while(iter_ LESS tests_num_)
list(GET cpp_test_paths_ ${iter_} path_)
list(GET cpp_test_exes_ ${iter_} exe_)
# under the assumption that thel ${exe} is program_name_tests.cpp
# find program_name executable
string(LENGTH ${exe_} len_)
math(EXPR len_ ${len_}-6 )
string(SUBSTRING ${exe_} 0 ${len_} program_name_)
# test executable, linking and compiler options
add_executable(
${exe_}
${CMAKE_CURRENT_LIST_DIR}/src/${program_name_}.cpp
${path_}
)
message(STATUS " ${exe_}: ${path_}")
target_link_libraries(
${exe_}
PRIVATE gtest_main
)
target_compile_definitions(
${exe_}
PRIVATE ${test_flag_}
)
gtest_discover_tests(${exe_})
# add_test(NAME ${exe_}_test COMMAND ${exe_})
math(EXPR iter_ ${iter_}+1)
endwhile()