Debian Codegraph @

build_tooling/cmake/tangent/wrappers.cmake

raw
1 # Split a list into named arguments in the current scope.
2 #
3 # Example:
4 # ~~~
5 # set(_mylist foo bar baz 1 2 3)
6 # EXPLODE(_mylist _var1 _var2 _var3)
7 # message(" var1: ${_var1}\n" " var2: ${_var2}\n" " var3: ${_var3}\n")
8 # ~~~
9 #
10 # Which will output:
11 # ~~~
12 # var1: foo
13 # var2: bar
14 # var3: baz
15 # ~~~
16 macro(EXPLODE listname)
17 set(_args ${ARGN})
18 list(LENGTH "${listname}" _list_len)
19 list(LENGTH _args _args_len)
20
21 if("${_args_len}" LESS "${_list_len}")
22 message(
23 FATAL_ERROR
24 " Can't EXPLODE ${_args_len} elements into ${_list_len} elements:\n"
25 " ${_args}")
26 endif()
27
28 set(_idx "0")
29 foreach(varname ${ARGN})
30 list(GET ${listname} ${_idx} ${varname})
31 math(EXPR _idx "${_idx} + 1}")
32 endforeach()
33 endmacro()
34
35 # Join a list of strings into a single string
36 #
37 # usage:
38 # ~~~
39 # join(<outvar> [GLUE <glue>] [arg1 [arg2 [arg3 [...]]]])
40 # ~~~
41 function(join outvar)
42 cmake_parse_arguments(args "" "GLUE" "" ${ARGN})
43 set(joined)
44 foreach(part ${args_UNPARSED_ARGUMENTS})
45 set(joined "${joined}${args_GLUE}${part}")
46 endforeach()
47 set(${outvar}
48 "${joined}"
49 PARENT_SCOPE)
50 endfunction()
51
52 # Backport list(FILTER ...) to cmake < 3.6
53 function(list_filter listname)
54 set(localcopy ${${listname}})
55 if("${CMAKE_VERSION}" VERSION_GREATER "3.5.999")
56 # cmake-lint: disable=E1120
57 list(FILTER localcopy ${ARGN})
58 else()
59 cmake_parse_arguments(_args "" "REGEX" "" ${ARGN})
60 explode(_args_UNPARSED_ARGUMENTS _mode)
61
62 set(buffer)
63 if("${_mode}" STREQUAL "INCLUDE")
64 foreach(elem ${localcopy})
65 if("${elem}" MATCHES "${_args_REGEX}")
66 list(APPEND buffer "${elem}")
67 endif()
68 endforeach()
69 else()
70 foreach(elem ${localcopy})
71 if(NOT "${elem}" MATCHES "${_args_REGEX}")
72 list(APPEND buffer "${elem}")
73 endif()
74 endforeach()
75 endif()
76 set(localcopy ${buffer})
77 endif()
78 set(${listname}
79 ${localcopy}
80 PARENT_SCOPE)
81 endfunction()
82
83 # Wraps `execute_process` and reports error if any command in the list of
84 # commands fails.
85 function(check_call)
86 cmake_parse_arguments(_args "" "OUTPUT_VARIABLE;ERROR_VARIABLE" "" ${ARGN})
87
88 set(request_results_list)
89 if("${CMAKE_VERSION}" VERSION_GREATER "3.9.999")
90 set(request_results_list RESULTS_VARIABLE results)
91 endif()
92
93 set(results)
94 execute_process(
95 ${_args_UNPARSED_ARGUMENTS}
96 OUTPUT_VARIABLE stdout
97 ERROR_VARIABLE stderr
98 RESULT_VARIABLE result ${request_results_list})
99
100 set(kwargs
101 COMMAND
102 WORKING_DIRECTORY
103 TIMEOUT
104 RESULT_VARIABLE
105 RESULTS_VARIABLE
106 OUTPUT_VARIABLE
107 ERROR_VARIABLE
108 INPUT_FILE
109 OUTPUT_FILE
110 ERROR_FILE
111 OUTPUT_QUIET
112 ERROR_QUIET
113 OUTPUT_STRIP_TRAILING_WHITESPACE
114 ERROR_STRIP_TRAILING_WHITESPACE
115 ENCODING)
116 string(REPLACE ";" "|" kwargs "${kwargs}")
117 set(buffer ${_args_UNPARSED_ARGUMENTS})
118
119 # Turn the cmake list into a regular string with '%' as an item separator
120 string(REPLACE ";" "%" buffer "${buffer}")
121 # Split the string at keyword boundaries
122 string(REGEX REPLACE "\\%(${kwargs})\\%" ";\\1%" buffer "${buffer}")
123 # Remove list items that aren't COMMAND
124 set(buffer2 ${buffer})
125 list_filter(buffer2 INCLUDE REGEX "COMMAND%.*")
126 # Save the list of commands
127 set(cmds "${buffer2}")
128
129 set(idx "0")
130 foreach(result ${results})
131 if(NOT "${result}" EQUAL "0")
132 list(GET cmds ${idx} cmdstr)
133 string(REPLACE "COMMAND%" "" cmdstr "${cmdstr}")
134 string(REPLACE "%" " " cmdstr "${cmdstr}")
135 message(FATAL_ERROR " Failed to execute command ${idx}:\n"
136 " ${cmdstr}\n ${stderr}")
137 endif()
138 math(EXPR idx "${idx} + 1}")
139 endforeach()
140
141 if(NOT "${result}" EQUAL "0")
142 string(REPLACE "COMMAND%" "\n " cmdlines "${cmds}")
143 string(REPLACE "%" " " cmdlines "${cmdlines}")
144 message(FATAL_ERROR " Failed to execute one or more command:\n"
145 " ${cmdlines}\n" " ${stderr}")
146 endif()
147
148 if(_args_OUTPUT_VARIABLE)
149 set(${_args_OUTPUT_VARIABLE}
150 ${stdout}
151 PARENT_SCOPE)
152 endif()
153 if(_args_ERROR_VARIABLE)
154 set(${_args_ERROR_VARIABLE}
155 ${stderr}
156 PARENT_SCOPE)
157 endif()
158 endfunction()
159
160 # Executes a python oneline using `execute_process` and reports error if the
161 # command failed
162 function(check_pyoneline)
163 cmake_parse_arguments(_args "" "OUTPUT_VARIABLE;ERROR_VARIABLE" "" ${ARGN})
164
165 # Escape semicolons in the cmake list, the resulting string will contain \\\;
166 # for each semicolon. When we pass this into check_call the escape sequences
167 # will be interpreted, resulting in \; within the `COMMAND` argument to
168 # execute_process.
169 string(REPLACE ";" "\\\\\\;" _oneline "${_args_UNPARSED_ARGUMENTS}")
170
171 set(_pyonline ${ARGN})
172 check_call(
173 # NOTE(josh): cmake concats lines using semicolon, which is the same
174 # separator that python expects, so we don't need to do any special
175 # processing. The cmake list-concat gives us a python oneliner for free.
176 COMMAND python -Bc "${_oneline}"
177 OUTPUT_VARIABLE stdout
178 ERROR_VARIABLE stderr
179 OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE)
180
181 if(_args_OUTPUT_VARIABLE)
182 set(${_args_OUTPUT_VARIABLE}
183 ${stdout}
184 PARENT_SCOPE)
185 endif()
186 if(_args_ERROR_VARIABLE)
187 set(${_args_ERROR_VARIABLE}
188 ${stderr}
189 PARENT_SCOPE)
190 endif()
191 endfunction()
192
193 # Get a list of all known properties
194 check_call(COMMAND ${CMAKE_COMMAND} --help-property-list
195 OUTPUT_VARIABLE _propslist)
196 # Replace newlines with list separator
197 string(REGEX REPLACE "[\n ]+" ";" _propslist "${_propslist}")
198 set(KNOWN_PROPERTIES ${_propslist})
199
200 function(tangent_addtest)
201 cmake_parse_arguments(_args "" "NAME;WORKING_DIRECTORY"
202 "COMMAND;CONFIGURATIONS;DEPENDS" ${ARGN})
203
204 add_test(
205 NAME ${_args_NAME}
206 COMMAND ${_args_COMMAND}
207 WORKING_DIRECTORY ${_args_WORKING_DIRECTORY}
208 CONFIGURATIONS ${_args_CONFIGURATIONS})
209 add_custom_target(
210 runtest.${_args_NAME}
211 DEPENDS ${_args_DEPENDS}
212 COMMAND ${_args_COMMAND}
213 WORKING_DIRECTORY ${_args_WORKING_DIRECTORY})
214 add_dependencies(best-test runtest.${_args_NAME})
215
216 endfunction()
217
218 # Wraps add_library and provides additional keyword options that translate into
219 # additional calls to target_link_libraries, target_set_properties, etc. Usage:
220 # ~~~
221 # cc_library(<target-name> [STATIC|SHARED]
222 # SRCS src1.cc src2.cc src3.cc
223 # DEPS lib1 lib2 lib3)
224 # ~~~
225 #
226 # Keyword Arguments:
227 #
228 # *INCS*: a list of include directories that should be added using
229 # target_include_dirs
230 #
231 # *SRCS*: a list of source files that go into compilation. These translate to
232 # the positional arguments of add_library().
233 #
234 # *DEPS*: a list of libraries to link against. These should be names that cmake
235 # understands. They are passed as the positional arguments to
236 # target_link_libraires
237 #
238 # *PKGDEPS*: A list of pkg-config names that are dependencies of this
239 # executable. They are passed as positional arguments to target_pkg_depends
240 function(cc_library target_name)
241 set(flags)
242 set(oneargs)
243 set(multiargs INC SRCS DEPS PKGDEPS PROPERTIES)
244 cmake_parse_arguments(_args "${flags}" "${oneargs}" "${multiargs}" ${ARGN})
245
246 add_library(${target_name} ${_args_UNPARSED_ARGUMENTS} ${_args_SRCS})
247 if(_args_INC)
248 target_include_directories(${target_name} ${_args_INC})
249 endif()
250 if(_args_DEPS)
251 list(GET _args_DEPS 0 dep0)
252 if(NOT "${_dep0}" MATCHES "(PUBLIC)|(PRIVATE)|(INTERFACE)")
253 list(INSERT _args_DEPS 0 "PUBLIC")
254 endif()
255 target_link_libraries(${target_name} ${_args_DEPS})
256 endif()
257 if(_args_PKGDEPS)
258 target_pkg_depends(${target_name} ${_args_PKGDEPS})
259 endif()
260 if(_args_PROPERTIES)
261 # cmake-lint: disable=E1120
262 set_target_properties(${target_name} PROPERTIES ${_args_PROPERTIES})
263 endif()
264 endfunction()
265
266 # Wraps add_executable and provides additional keyword options that translate
267 # into additional calls to target_link_libraries, target_set_properties, etc.
268 # Usage:
269 # ~~~
270 # cc_binary(<target-name>
271 # SRCS src1.cc src2.cc src3.cc
272 # DEPS lib1 lib2 lib3)
273 # ~~~
274 #
275 # Keyword Arguments:
276 #
277 # *SRCS*: a list of source files that go into compilation. These translate to
278 # the positional arguments of add_executable().
279 #
280 # *DEPS*: a list of libraries to link against. These should be names that cmake
281 # understands. They are passed as the positional arguments to
282 # target_link_libraires
283 #
284 # *PKGDEPS*: A list of pkg-config names that are dependencies of this
285 # executable. They are passed as positional arguments to target_pkg_depends
286 function(cc_binary target_name)
287 set(flags ADD_RUNTARGET)
288 set(oneargs)
289 set(multiargs SRCS DEFS DEPS PKGDEPS PROPERTIES)
290 cmake_parse_arguments(_args "${flags}" "${oneargs}" "${multiargs}" ${ARGN})
291
292 add_executable(${target_name} ${_args_UNPARSED_ARGUMENTS} ${_args_SRCS})
293 if(_args_DEPS)
294 target_link_libraries(${target_name} PUBLIC ${_args_DEPS})
295 endif()
296 if(_args_PKGDEPS)
297 target_pkg_depends(${target_name} ${_args_PKGDEPS})
298 endif()
299 if(_args_PROPERTIES)
300 # cmake-lint: disable=E1120
301 set_target_properties(${target_name} PROPERTIES ${_args_PROPERTIES})
302 endif()
303 if(_args_DEFS)
304 target_compile_definitions(${target_name} ${_args_DEFS})
305 endif()
306
307 if(_args_ADD_RUNTARGET)
308 add_custom_target(
309 run.${target_name}
310 COMMAND ${target_name}
311 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
312 endif()
313 endfunction()
314
315 # Wraps a pair of calls to cc_binary and add_test and provides additional
316 # keyword options that translate to additional calls to set_property
317 #
318 # Usage:
319 # ~~~
320 # cc_test(<cc_binary()-spec>
321 # ARGV --flag1 --flag2
322 # TEST_DEPS file1 file2
323 # WORKING_DIRECTORY <workdir>
324 # LABELS label1 label2)
325 # ~~~
326 #
327 # In addition to creating the executable, this command will also create a target
328 # `run.<test-name>` which can be specified as a target to the build system. When
329 # specified as a build target, will ensure that all the depenencies are up-to-
330 # date before executing the test.
331 #
332 # The name of the test will match the name of the executable target.
333 #
334 # Keyword Arguments:
335 #
336 # *ARGV*: a list of additional command line options added to the command used
337 # for the test
338 #
339 # *TEST_DEPS*: additional file-level dependencies required to execute the test,
340 # such as golden data or resource files.
341 #
342 # *WORKING_DIRECTORY*: working directory where to execute the command
343 #
344 # *LABELS*: a list of ctest labels pased to set_property(TEST ...)
345 function(cc_test target_name)
346 set(flags)
347 set(oneargs WORKING_DIRECTORY)
348 set(multiargs ARGV TEST_DEPS LABELS)
349 cmake_parse_arguments(args "${flags}" "${oneargs}" "${multiargs}" ${ARGN})
350
351 set(cmd $<TARGET_FILE:${target_name}> ${args_ARGV})
352 cc_binary(${target_name} ${args_UNPARSED_ARGUMENTS})
353
354 if(NOT args_WORKING_DIRECTORY)
355 set(args_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
356 endif()
357
358 if(args_TEST_DEPS)
359 add_custom_target(testdeps.${target_name} DEPENDS ${TEST_DEPS})
360 add_dependencies(testdeps testdeps.${target_name})
361 endif()
362
363 add_custom_target(
364 run.${target_name}
365 COMMAND ${cmd}
366 DEPENDS ${args_TEST_DEPS}
367 WORKING_DIRECTORY ${args_WORKING_DIRECTORY})
368
369 add_test(
370 NAME ${target_name}
371 COMMAND ${cmd}
372 WORKING_DIRECTORY ${args_WORKING_DIRECTORY})
373
374 if(args_LABELS)
375 set_property(
376 TEST ${target_name}
377 APPEND
378 PROPERTY LABELS ${args_LABELS})
379 endif()
380 endfunction()
381
382 # Given a list of variables in the current scope, assign their value to a
383 # variable of the same name in the parent scope
384 macro(RETURNVARS)
385 foreach(varname ${ARGN})
386 set(${varname}
387 "${${varname}}"
388 PARENT_SCOPE)
389 endforeach()
390 endmacro()
391
392 set_property(GLOBAL PROPERTY SEMVER_INCLUDENO 0)
393 # Get the version string by parsing a C++ header file. The version is expected
394 # to be in the form of:
395 #
396 # ~~~
397 # define FOO_VERSION \
398 # { 0, 1, 0, "dev", 0 }
399 # ~~~
400 #
401 # Usage:
402 # ~~~
403 # get_version_from_header(<header-path> <macro>)
404 # ~~~
405 #
406 # Assigns into the calling scope a variable with the same name as `${macro}` the
407 # value of the version
408 function(get_version_from_header headerpath macro)
409 get_property(_includeno GLOBAL PROPERTY SEMVER_INCLUDENO)
410 math(EXPR _includeno "${_includeno} + 1")
411 set_property(GLOBAL PROPERTY SEMVER_INCLUDENO ${_includeno})
412 set(stubfile ${CMAKE_CURRENT_BINARY_DIR}/semver-${_includeno}.cmake)
413
414 if(NOT IS_ABSOLUTE ${headerpath})
415 set(headerpath ${CMAKE_CURRENT_SOURCE_DIR}/${headerpath})
416 endif()
417
418 execute_process(
419 COMMAND python -B ${TANGENT_TOOLING}/get_version_from_header.py #
420 --outfile ${stubfile} ${macro} ${headerpath}
421 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
422 RESULT_VARIABLE _retcode
423 OUTPUT_VARIABLE _stdout
424 ERROR_VARIABLE _stderr)
425 if(NOT "${_retcode}" EQUAL "0")
426 message(
427 FATAL_ERROR " Failed to extract version number from"
428 " ${headerpath}: ${_retcode}\n" " ${_stdout}\n" " ${_stderr}")
429 endif()
430 include(${stubfile} RESULT_VARIABLE _result)
431 if(_result STREQUAL NOTFOUND)
432 message(FATAL_ERROR "Failed to include stubfile ${stubfile}")
433 endif()
434
435 string(REGEX MATCH "(.*)_VERSION" _match "${macro}")
436 if(_match)
437 set(prefix ${CMAKE_MATCH_1})
438 else()
439 message(
440 FATAL_ERROR "Header version macro '${macro}' does not end in _VERSION")
441 endif()
442
443 returnvars(${prefix}_VERSION ${prefix}_API_VERSION ${prefix}_SO_VERSION)
444 endfunction()
445
446 # map package name to source directory
447 set(_package_pairs
448 "argue\;argue" #
449 "libtangent-util\;tangent/util" #
450 "libtangent-json\;tangent/json" #
451 "gtangent\;gtangent" #
452 "gtangentmm\;gtangentmm" #
453 )
454 foreach(pair ${_package_pairs})
455 explode(pair _package_name _repodir)
456 set(TANGENT_PACKAGE_SOURCEDIR_${_package_name} ${_repodir})
457 endforeach()
458
459 # Wraps a call to find_package() for a tangent supplied cmake package and
460 # pre-empt the call to find_package() if the package exists locally in the
461 # sparse checkout,
462 function(find_tangent_package package_name)
463 set(repodir ${TANGENT_PACKAGE_SOURCEDIR_${package_name}})
464 if(EXISTS ${CMAKE_SOURCE_DIR}/${repodir})
465 return()
466 endif()
467 find_package(${package_name} ${ARGN})
468 endfunction()
469
470 # Tunable verbose logging
471 function(vlog level)
472 if(NOT DEFINED TANGENT_VERBOSE_LEVEL)
473 return()
474 endif()
475 if("${level}" GREATER ${TANGENT_VERBOSE_LEVEL})
476 return()
477 endif()
478 message(STATUS ${ARGN})
479 endfunction()
480
481 # Glob the current source directory for any children that have listfiles, and
482 # add them.
483 function(glob_subdirs)
484 # NOTE(josh): search through the list of child directories and add any that
485 # actually contain a listfile. While globs are evil, this is necessary for
486 # sparse checkouts. We can and should correctly add dependencies for this glob
487 # in order to retrigger cmake.
488 file(
489 GLOB children
490 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
491 ${CMAKE_CURRENT_SOURCE_DIR}/*)
492 foreach(child ${children})
493 set(candidate ${CMAKE_CURRENT_SOURCE_DIR}/${child}/CMakeLists.txt)
494 if(EXISTS ${candidate})
495 file(RELATIVE_PATH relpath_candidate "${CMAKE_SOURCE_DIR}" "${candidate}")
496 vlog(1 "Enabling subdirectory ${relpath_candidate}")
497 add_subdirectory(${child})
498 endif()
499 endforeach()
500 endfunction()
501
502 # If this build system was generated on travis, the include a rule to decrypt
503 # the given file to the given location.
504 function(travis_decrypt tgtfile srcfile keyid)
505 get_filename_component(_filename ${tgtfile} NAME)
506 if(IS_TRAVIS_CI)
507 add_custom_command(
508 OUTPUT ${tgtfile}
509 DEPENDS ${srcfile}
510 COMMAND
511 # cmake-format: off
512 openssl aes-256-cbc
513 # NOTE(josh): we could use $ENV{...} but then we end up with the
514 # actual encryption key in the command line... which might show up
515 # in logs or error messages.
516 -K "\$\${encrypted_${keyid}_key}"
517 -iv "\$\${encrypted_${keyid}_iv}"
518 -in ${srcfile}
519 -out ${tgtfile}
520 -d
521 # cmake-format: on
522 COMMAND chmod 0600 ${tgtfile}
523 COMMENT "Decrypting ${_filename}")
524 endif()
525 endfunction()