-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_target.cmake
271 lines (230 loc) · 7.71 KB
/
add_target.cmake
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
cmake_minimum_required(VERSION 3.18)
if(COMMAND add_target)
message(
WARNING
"add_target function already exists, breaking early instead of overriding."
)
return()
endif()
function(add_target target_name)
if(TARGET ${target_name})
message(
FATAL_ERROR
"${CMAKE_CURRENT_FUNCTION}: Cannot add target ${target_name} because it already exists."
)
endif()
set(options "STATIC;SHARED;EXECUTALBE;HEADER_ONLY")
set(multivalue_keywords
"INCLUDES;DEPENDENCIES;OPTIONS;DEFINITIONS;FEATURES;BOOST_TEST;GOOGLE_TEST"
)
cmake_parse_arguments(PARSE_ARGV 1 io1 "${options}" ""
"${multivalue_keywords}")
# options are mutually exclusive the found_options list must have length one
# at the end of this loop
foreach(option IN LISTS options)
if(io1_${option})
list(APPEND found_option ${option})
endif()
endforeach()
list(LENGTH found_option found_option_count)
if(found_option_count EQUAL 0)
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION}: missing target type.")
elseif(NOT found_option_count EQUAL 1)
message(
FATAL_ERROR
"${CMAKE_CURRENT_FUNCTION}: cannot define multiple target types.")
endif()
# target sources are the leftover values
fetch_source_files(sources ${io1_UNPARSED_ARGUMENTS})
# create the target with the type detected by found_option
if(io1_STATIC)
add_library(${target_name} STATIC ${sources})
elseif(io1_SHARED)
add_library(${target_name} SHARED ${sources})
elseif(io1_EXECUTABLE)
add_executable(${target_name} ${sources})
elseif(io1_HEADER_ONLY)
add_library(${target_name} INTERFACE ${sources})
endif()
# apply source files properties and groups
apply_source_groups(${io1_UNPARSED_ARGUMENTS})
apply_source_files_properties(${io1_UNPARSED_ARGUMENTS})
message(
STATUS
"${CMAKE_CURRENT_FUNCTION}: created ${found_option} target ${target_name}."
)
# configure the target from the multi-value keywords
if(DEFINED io1_INCLUDES)
target_include_directories(${target_name} ${io1_INCLUDES})
endif()
if(DEFINED io1_DEPENDENCIES)
target_link_libraries(${target_name} ${io1_DEPENDENCIES})
endif()
if(DEFINED io1_OPTIONS)
target_compile_options(${target_name} ${io1_OPTIONS})
endif()
if(DEFINED io1_DEFINITIONS)
target_compile_definitions(${target_name} ${io1_DEFINITIONS})
endif()
if(DEFINED io1_FEATURES)
target_compile_features(${target_name} ${io1_FEATURES})
endif()
if(DEFINED io1_BOOST_TEST)
set(test_name "boost-test-${target_name}")
if(TARGET ${test_name})
message(
FATAL_ERROR
"${CMAKE_CURRENT_FUNCTION}: cannot create boost test target ${test_name} because it already exists."
)
endif()
find_package(
Boost REQUIRED
COMPONENTS unit_test_framework
QUIET)
fetch_source_files(test_sources ${io1_BOOST_TEST})
add_executable(${test_name} ${test_sources})
apply_source_groups(${io1_BOOST_TEST})
apply_source_files_properties(${io1_BOOST_TEST})
target_link_libraries(${test_name} PRIVATE ${target_name}
Boost::unit_test_framework)
message(
STATUS
"${CMAKE_CURRENT_FUNCTION}: created boost test target ${test_name}.")
if(BUILD_TESTING)
add_test(
NAME ${test_name}
COMMAND ${test_name} --catch_system_error=yes --detect_memory_leaks
--logger=JUNIT,all,junit_${test_name}.xml
WORKING_DIRECTORY $<TARGET_FILE_DIR:${test_name}>)
else()
message(
STATUS
"${CMAKE_CURRENT_FUNCTION}: skipped declaring ${test_name} to CTest because BUILD_TESTING is false."
)
endif()
endif()
if(DEFINED io1_GOOGLE_TEST)
if(NOT COMMAND gtest_discover_tests)
include(GoogleTest)
endif()
set(test_name "google-test-${target_name}")
if(TARGET ${test_name})
message(
FATAL_ERROR
"${CMAKE_CURRENT_FUNCTION}: cannot create google test target ${test_name} because it already exists."
)
endif()
find_package(GTest REQUIRED QUIET)
fetch_source_files(test_sources ${io1_GOOGLE_TEST})
add_executable(${test_name} ${test_sources})
apply_source_groups(${io1_GOOGLE_TEST})
apply_source_files_properties(${io1_GOOGLE_TEST})
target_link_libraries(${test_name} PRIVATE ${target_name} GTest::Main)
message(
STATUS
"${CMAKE_CURRENT_FUNCTION}: created google test target ${test_name}.")
if(BUILD_TESTING)
gtest_discover_tests(${test_name}
WORKING_DIRECTORY $<TARGET_FILE_DIR:${test_name}>)
else()
message(
STATUS
"${CMAKE_CURRENT_FUNCTION}: skipped declaring ${test_name} to CTest because BUILD_TESTING is false."
)
endif()
endif()
endfunction()
set(source_group_regex "^(.*)//$")
set(source_file_properties_regex "^(.*):(.*)$")
function(apply_source_groups)
fetch_source_groups(sources groups ${ARGN})
foreach(source group IN ZIP_LISTS sources groups)
source_group(NAME "${group}" FILES "${source}")
endforeach()
endfunction()
function(fetch_source_groups out_sources out_groups)
set(current_group_name ".")
set(current_group_path "/.")
foreach(file IN LISTS ARGN)
if("${file}" MATCHES "${source_group_regex}")
set(new_group_path "${CMAKE_MATCH_1}")
if(IS_ABSOLUTE "${new_group_path}")
set(current_group_path "${new_group_path}")
else()
get_filename_component(current_group_path "${new_group_path}" ABSOLUTE
BASE_DIR "${current_group_path}")
endif()
string(SUBSTRING "${current_group_path}" 1 -1 current_group_name)
elseif(NOT "${current_group_name}" STREQUAL ".")
list(APPEND temp_out_groups "${current_group_name}")
if("${file}" MATCHES "${source_file_properties_regex}")
list(APPEND temp_out_sources "${CMAKE_MATCH_1}")
else()
list(APPEND temp_out_sources "${file}")
endif()
endif()
endforeach()
set(${out_sources}
"${temp_out_sources}"
PARENT_SCOPE)
set(${out_groups}
"${temp_out_groups}"
PARENT_SCOPE)
endfunction()
# strip out groups strings and source file properties
function(fetch_source_files out_sources)
foreach(str IN LISTS ARGN)
if("${str}" MATCHES "${source_group_regex}")
continue()
endif()
if("${str}" MATCHES "${source_file_properties_regex}")
list(APPEND temp_out_sources "${CMAKE_MATCH_1}")
else()
list(APPEND temp_out_sources "${str}")
endif()
endforeach()
set(${out_sources}
"${temp_out_sources}"
PARENT_SCOPE)
endfunction()
function(apply_source_files_properties)
foreach(str IN LISTS ARGN)
if("${str}" MATCHES "${source_group_regex}")
continue()
endif()
parse_file_options("${str}" file options)
cmake_parse_arguments(io1 "cpp;header" "" "" ${options})
if(io1_cpp)
set_source_files_properties("${file}" PROPERTIES LANGUAGE CXX)
endif()
if(io1_header)
set_source_files_properties("${file}" PROPERTIES HEADER_FILE_ONLY ON)
endif()
endforeach()
endfunction()
# expects "file.c[:opt1,opt2,opt3,...]" and parse it into "file.c" and the list
# "opt1;opt2;opt3"
function(parse_file_options str out_file out_options)
if("${str}" MATCHES "${source_file_properties_regex}")
set(${out_file}
"${CMAKE_MATCH_1}"
PARENT_SCOPE)
if("${CMAKE_MATCH_1}" STREQUAL "")
set(${out_options}
""
PARENT_SCOPE)
else()
string(REPLACE "," ";" temp_out_options "${CMAKE_MATCH_2}")
set(${out_options}
"${temp_out_options}"
PARENT_SCOPE)
endif()
else()
set(${out_file}
"${str}"
PARENT_SCOPE)
set(${out_options}
""
PARENT_SCOPE)
endif()
endfunction()