Debian Codegraph @

cmake/tribits/core/package_arch/TribitsPackageMacros.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10 include(TribitsPackageSetupCompilerFlags)
11 include(TribitsInternalPackageWriteConfigFile)
12 include(TribitsGeneralMacros)
13 include(TribitsLibIsTestOnly)
14
15 include(CMakeParseArguments)
16 include(GlobalNullSet)
17 include(AppendGlobalSet)
18 include(PrintVar)
19 include(PrependSet)
20 include(PrependGlobalSet)
21 include(RemoveGlobalDuplicates)
22 include(TribitsGatherBuildTargets)
23
24 include("${CMAKE_CURRENT_LIST_DIR}/../test_support/TribitsAddTest.cmake")
25 include("${CMAKE_CURRENT_LIST_DIR}/../test_support/TribitsAddAdvancedTest.cmake")
26
27 include(TribitsAddOptionAndDefine)
28 include(TribitsPkgExportCacheVars)
29 include(TribitsLibraryMacros)
30 include(TribitsAddExecutable)
31 include(TribitsAddExecutableAndTest)
32 include(TribitsCopyFilesToBinaryDir)
33 include(TribitsReportInvalidTribitsUsage)
34
35
36 #
37 # Utility macros
38 #
39
40
41 # Macro that defines the package architecture system variables used to link
42 # different packages together
43 #
44 # See README.DEPENDENCIES for information on what these variables mean and how
45 # they are used.
46 #
47 macro(tribits_define_linkage_vars PACKAGE_NAME_IN)
48 global_null_set(${PACKAGE_NAME_IN}_LIBRARIES "")
49 global_set(${PACKAGE_NAME_IN}_HAS_NATIVE_LIBRARIES_TO_INSTALL FALSE)
50 endmacro()
51
52
53 # Macro that defines variables that create global targets
54 #
55 macro(tribits_define_target_vars PARENT_PACKAGE_NAME_IN)
56 global_null_set(${PARENT_PACKAGE_NAME_IN}_LIB_TARGETS)
57 global_null_set(${PARENT_PACKAGE_NAME_IN}_ALL_TARGETS)
58 endmacro()
59
60
61 # Set up some common variables used in the creation of an package
62 #
63 macro(tribits_set_common_vars PACKAGE_NAME_IN)
64
65 string(TOUPPER ${PACKAGE_NAME_IN} PACKAGE_NAME_UC)
66
67 # Write TRIBITS_PACKAGE versions of common variables
68 set(PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
69 set(PACKAGE_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
70
71 # Get the name of the directory this ${PROJECT_NAME} package is in
72 file(TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR} STANDARD_PACKAGE_SOURCE_DIR)
73 string(REGEX REPLACE "/.+/(.+)" "\\1" PACKAGE_DIR_NAME "${STANDARD_PACKAGE_SOURCE_DIR}")
74
75 endmacro()
76
77
78 # @MACRO: tribits_package_decl()
79 #
80 # Macro called at the very beginning of a package's top-level
81 # `<packageDir>/CMakeLists.txt`_ file when a package has subpackages.
82 #
83 # Usage::
84 #
85 # tribits_package_decl(
86 # <packageName>
87 # [ENABLE_SHADOWING_WARNINGS]
88 # [DISABLE_STRONG_WARNINGS]
89 # [CLEANED]
90 # [DISABLE_CIRCULAR_REF_DETECTION_FAILURE]
91 # )
92 #
93 # The arguments are:
94 #
95 # ``<packageName>``
96 #
97 # Gives the name of the Package, mostly just for checking and
98 # documentation purposes. This must match the name of the package
99 # provided in the `<repoDir>/PackagesList.cmake`_ or an error is issued.
100 #
101 # ``ENABLE_SHADOWING_WARNINGS``
102 #
103 # If specified, then shadowing warnings for the package's sources will be
104 # turned on for supported platforms/compilers. The default is for
105 # shadowing warnings to be turned off. Note that this can be overridden
106 # globally by setting the cache variable
107 # ``${PROJECT_NAME}_ENABLE_SHADOWING_WARNINGS``.
108 #
109 # ``DISABLE_STRONG_WARNINGS``
110 #
111 # If specified, then all strong warnings for the package's sources will be
112 # turned off, if they are not already turned off by global cache
113 # variables. Strong warnings are turned on by default in development
114 # mode.
115 #
116 # ``CLEANED``
117 #
118 # If specified, then warnings will be promoted to errors for compiling the
119 # package's sources for all defined warnings.
120 #
121 # ``DISABLE_CIRCULAR_REF_DETECTION_FAILURE``
122 #
123 # If specified, then the standard grep looking for RCPNode circular
124 # references in `tribits_add_test()`_ and `tribits_add_advanced_test()`_
125 # that causes tests to fail will be disabled. Note that if these warnings
126 # are being produced then it means that the test is leaking memory and
127 # user like may also be leaking memory.
128 #
129 # There are several side-effects of calling this macro:
130 #
131 # * The variables ``${PACKAGE_NAME}_LIB_TARGETS`` (lists all of the package's
132 # targets) and ``${PACKAGE_NAME}_ALL_TARGETS`` (lists all of the package's
133 # libraries) and are initialized to empty.
134 #
135 # * The local variables ``PACKAGE_SOURCE_DIR`` and ``PACKAGE_BINARY_DIR`` are
136 # set for this package's use in its CMakeLists.txt files.
137 #
138 # * Package-specific compiler options are set up in package-scope (i.e., the
139 # package's subdirs) in ``CMAKE_<LANG>_FLAG``.
140 #
141 # * This packages's cmake subdir ``${PACKAGE_SOURCE_DIR}/cmake`` is added to
142 # ``CMAKE_MODULE_PATH`` locally so that the package's try-compile modules
143 # can be read in with just a raw ``include()`` leaving off the full path and
144 # the ``*.cmake`` extension.
145 #
146 # If the package does not have subpackages, just call `tribits_package()`_
147 # which calls this macro.
148 #
149 macro(tribits_package_decl PACKAGE_NAME_IN)
150
151 if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
152 message("\nTRIBITS_PACKAGE_DECL: ${PACKAGE_NAME_IN}")
153 endif()
154
155 tribits_package_decl_assert_call_context()
156
157 #
158 # A) Parse the input arguments
159 #
160
161 cmake_parse_arguments(
162 #prefix
163 PARSE
164 #options
165 "CLEANED;ENABLE_SHADOWING_WARNINGS;DISABLE_STRONG_WARNINGS;DISABLE_CIRCULAR_REF_DETECTION_FAILURE"
166 #one_value_keywords
167 ""
168 #multi_value_keywords
169 ""
170 ${ARGN}
171 )
172
173 tribits_check_for_unparsed_arguments()
174
175 #
176 # B) Assert that the global and local package names are the same!
177 #
178
179 if (DEFINED PACKAGE_NAME)
180 if (NOT ${PACKAGE_NAME_IN} STREQUAL ${PACKAGE_NAME})
181 message(FATAL_ERROR "Error, the package-defined package name"
182 " '${PACKAGE_NAME_IN}' is not the same as the package name"
183 " defined at the global level '${PACKAGE_NAME}'")
184 endif()
185 endif()
186
187 #
188 # C) Set up the CMake support for this ${PROJECT_NAME} package and define some
189 # top-level variables.
190 #
191
192 tribits_set_common_vars(${PACKAGE_NAME_IN})
193 tribits_pkg_init_exported_vars(${PACKAGE_NAME_IN})
194
195 set(${PACKAGE_NAME_IN}_DISABLE_STRONG_WARNINGS OFF
196 CACHE BOOL
197 "If set to true, then strong warnings for package ${PACKAGE_NAME_IN} will be disabled."
198 )
199
200 # Set up the compile flags for the package
201 tribits_setup_compiler_flags(${PACKAGE_NAME_IN})
202
203 # Set up circular reference detection test failure
204 if (PARSE_DISABLE_CIRCULAR_REF_DETECTION_FAILURE)
205 set(${PACKAGE_NAME}_ENABLE_CIRCULAR_REF_DETECTION_FAILURE OFF)
206 else()
207 set(${PACKAGE_NAME}_ENABLE_CIRCULAR_REF_DETECTION_FAILURE ON)
208 endif()
209
210 # Set up parent package linkage variables
211 tribits_define_target_vars(${PACKAGE_NAME})
212
213 # Define this as a CMake/CTest "Subproject"
214 set_directory_properties(PROPERTIES LABELS "${PACKAGE_NAME}")
215
216 #
217 # Append the local package's cmake directory in order to help pull in
218 # configure-time testing macros
219 #
220
221 prepend_set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
222
223 endmacro()
224
225
226 macro(tribits_package_decl_assert_call_context)
227
228 if (CURRENTLY_PROCESSING_SUBPACKAGE)
229 tribits_report_invalid_tribits_usage(
230 "Cannot call tribits_package_decl() in a subpackage."
231 " Use tribits_subpackage() instead"
232 " error in ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
233 endif()
234
235 if(${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED)
236 tribits_report_invalid_tribits_usage(
237 "tribits_package_decl() called more than once in Package ${PACKAGE_NAME}"
238 " This may be because tribits_package_decl() was explicitly called more than once or"
239 " TRIBITS_PACKAGE_DECL was called after TRIBITS_PACKAGE. You do not need both."
240 " If your package has subpackages then do not call tribits_package() instead call:"
241 " tribits_pacakge_decl() then tribits_process_subpackages() then tribits package_def()"
242 )
243 endif()
244
245 # Set flag to check that macros are called in the correct order
246 set(${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED TRUE)
247
248 endmacro()
249
250
251 # @MACRO: tribits_package_def()
252 #
253 # Macro called in `<packageDir>/CMakeLists.txt`_ after subpackages are
254 # processed in order to handle the libraries, tests, and examples of the
255 # parent package.
256 #
257 # Usage::
258 #
259 # tribits_package_def()
260 #
261 # If the package does not have subpackages, just call `tribits_package()`_
262 # which calls this macro.
263 #
264 # This macro has several side effects:
265 #
266 # * The variable ``PACKAGE_NAME`` is set in the local scope for usage by the
267 # package's ``CMakeLists.txt`` files.
268 #
269 # * The intra-package dependency variables (i.e. list of include directories,
270 # list of libraries, etc.) are initialized to empty.
271 #
272 macro(tribits_package_def)
273
274 if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
275 message("\nTRIBITS_PACKAGE_DEF: ${PACKAGE_NAME}")
276 endif()
277
278 tribits_package_def_assert_call_context()
279
280 if (NOT ${PROJECT_NAME}_ENABLE_${PACKAGE_NAME})
281 if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
282 message("\n${PACKAGE_NAME} not enabled so exiting package processing")
283 endif()
284 return()
285 endif()
286
287 # Reset in case were changed by subpackages
288 tribits_set_common_vars(${PACKAGE_NAME})
289
290 # Define package linkage variables
291 tribits_define_linkage_vars(${PACKAGE_NAME})
292
293 endmacro()
294
295
296 macro(tribits_package_def_assert_call_context)
297
298 # check that this is not being called from a subpackage
299 if(NOT ${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED)
300 if (CURRENTLY_PROCESSING_SUBPACKAGE)
301 tribits_report_invalid_tribits_usage(
302 "Cannot call tribits_package_def() in a subpackage."
303 " Use tribits_subpackage() instead"
304 " error in ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
305 endif()
306 endif()
307
308 # Reset since it was changed by the subpackages
309 set(PACKAGE_NAME ${PARENT_PACKAGE_NAME})
310
311 # check that this is not called morethan once in a package
312 if (${PACKAGE_NAME}_TRIBITS_PACKAGE_DEF_CALLED)
313 tribits_report_invalid_tribits_usage(
314 "tribits_package_def() was called more than once in"
315 "${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
316 endif()
317
318 set(${PACKAGE_NAME}_TRIBITS_PACKAGE_DEF_CALLED TRUE)
319
320 endmacro()
321
322
323 # @MACRO: tribits_package()
324 #
325 # Macro called at the very beginning of a package's top-level
326 # `<packageDir>/CMakeLists.txt`_ file.
327 #
328 # Usage::
329 #
330 # tribits_package(
331 # <packageName>
332 # [ENABLE_SHADOWING_WARNINGS]
333 # [DISABLE_STRONG_WARNINGS]
334 # [CLEANED]
335 # [DISABLE_CIRCULAR_REF_DETECTION_FAILURE]
336 # )
337 #
338 # See `tribits_package_decl()`_ for the documentation for the arguments and
339 # `tribits_package_decl()`_ and `tribits_package()`_ for a description the
340 # side-effects (and variables set) after calling this macro.
341 #
342 macro(tribits_package PACKAGE_NAME_IN)
343 tribits_package_assert_call_context()
344 tribits_package_decl(${PACKAGE_NAME_IN} ${ARGN})
345 tribits_package_def()
346 endmacro()
347
348
349 macro(tribits_package_assert_call_context)
350
351 if (CURRENTLY_PROCESSING_SUBPACKAGE)
352 if (NOT ${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED)
353 tribits_report_invalid_tribits_usage(
354 "Cannot call tribits_package() in a subpackage."
355 " Use tribits_subpackage() instead"
356 " error in ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
357 endif()
358 endif()
359
360 if(${PACKAGE_NAME}_SUBPACKAGES)
361 tribits_report_invalid_tribits_usage(
362 "This package has subpackages so you cannot use tribits_package()"
363 "\n Instead use the following call order:"
364 "\n tribits_project_decl(${PACKAGE_NAME})"
365 "\n tribits_process_subpackages()"
366 "\n [do other things you want to do]"
367 "\n tribits_package_def()"
368 "\n tribits_package_postprocess()" )
369 endif()
370
371 if(${PACKAGE_NAME}_TRIBITS_PACKAGE_CALLED)
372 tribits_report_invalid_tribits_usage(
373 "Package ${PACKAGE_NAME} declared more than once!")
374 endif()
375
376 set(${PACKAGE_NAME}_TRIBITS_PACKAGE_CALLED TRUE)
377
378 endmacro()
379
380
381 # @MACRO: tribits_disable_optional_dependency()
382 #
383 # Macro called to disable an optional dependency in the current package for an
384 # optional (internal or external) upstream package.
385 #
386 # Usage::
387 #
388 # tribits_disable_optional_dependency(<upstreamPackageName> "<reasonStr>")
389 #
390 # This macro can be called from a top-level package's
391 # ``<packageDir>/CMakeLists.txt`` file to disable an optional dependency that
392 # may have been enabled by the user or through automated enable/disable logic.
393 #
394 # This is most useful in cases where multiple criteria must be considered
395 # before support for some upstream dependency can really be supported. In
396 # that case, the dependency can be disabled in the current package and
397 # telegraphed to all downstream packages. See `How to tweak downstream
398 # TriBITS "ENABLE" variables during package configuration`_ for more details.
399 #
400 macro(tribits_disable_optional_dependency upstreamPackageName reasonStr)
401 # Assert called in the correct context
402 if (NOT "${${PACKAGE_NAME}_PARENT_PACKAGE}" STREQUAL "")
403 message(FATAL_ERROR "ERROR: Calling tribits_disable_optional_dependency() from"
404 " a subpackage it not allowed. It must be called from the parent package's"
405 " ${${PACKAGE_NAME}_PARENT_PACKAGE} CMakeLists.txt file")
406 endif()
407 if (NOT "${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${${PACKAGE_NAME}_SOURCE_DIR}")
408 message(FATAL_ERROR "ERROR: Calling tribits_disable_optional_dependency() from"
409 " a subdirectory CMakeLists.txt file under '${CMAKE_CURRENT_SOURCE_DIR}' is not allowed."
410 " Instead, please call this from the package's base CMakeLists.txt file"
411 " '${${PACKAGE_NAME}_SOURCE_DIR}/CMakeLists.txt'" )
412 endif()
413 # Get the variable names that are going to be set
414 set(packageEnableVarName ${PACKAGE_NAME}_ENABLE_${upstreamPackageName})
415 string(TOUPPER ${upstreamPackageName} upstreamPackageName_UC)
416 set(havePackageUpstreamPackageMacroVarName
417 HAVE_${PACKAGE_NAME_UC}_${upstreamPackageName_UC})
418 # Assert that the vars already exist (to make sure the package and dependency exist)
419 if (${PROJECT_NAME}_ASSERT_DEFINED_DEPENDENCIES IN_LIST
420 ${PROJECT_NAME}_ASSERT_DEFINED_DEPENDENCIES_ERROR_VALUES_LIST
421 )
422 # We only assert if all packages have to exist, which is not true in a reduced source tree
423 assert_defined(${packageEnableVarName})
424 assert_defined(${havePackageUpstreamPackageMacroVarName})
425 endif()
426 # Set the variables to OFF in local and project-level scopes
427 if (NOT "${reasonStr}" STREQUAL "")
428 message("-- ${reasonStr}")
429 endif()
430 dual_scope_set(${packageEnableVarName} OFF)
431 dual_scope_set(${havePackageUpstreamPackageMacroVarName} OFF)
432 endmacro()
433
434
435 # @MACRO: tribits_add_test_directories()
436 #
437 # Macro called to add a set of test directories for an package.
438 #
439 # Usage::
440 #
441 # tribits_add_test_directories(<dir1> <dir2> ...)
442 #
443 # This macro only needs to be called from the top most ``CMakeLists.txt`` file
444 # for which all subdirectories are all "tests".
445 #
446 # This macro can be called several times within a package and it will have the
447 # right effect.
448 #
449 # Currently, all this macro does macro is to call ``add_subdirectory(<diri>)``
450 # if ``${PACKAGE_NAME}_ENABLE_TESTS`` is ``TRUE``.
451 #
452 macro(tribits_add_test_directories)
453
454 tribits_add_test_example_directories_assert_call_context(
455 tribits_add_test_directories)
456
457 if(${PACKAGE_NAME}_ENABLE_TESTS)
458 foreach(TEST_DIR ${ARGN})
459 tribits_trace_file_processing(PACKAGE ADD_SUBDIR
460 "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_DIR}/CMakeLists.txt")
461 add_subdirectory(${TEST_DIR})
462 endforeach()
463 endif()
464
465 endmacro()
466
467
468 #
469 # Macros to add common options to add to an package
470 #
471
472
473 # @MACRO: tribits_add_debug_option()
474 #
475 # Add the standard cache variable option ``${PACKAGE_NAME}_ENABLE_DEBUG`` for
476 # the package.
477 #
478 # Usage::
479 #
480 # tribits_add_debug_option()
481 #
482 # This option is given the default value ``${${PROJECT_NAME}_ENABLE_DEBUG}``,
483 # and if true, this macro will set the variable
484 # ``HAVE_${PACKAGE_NAME_UC}_DEBUG`` (to be used in the package's configured
485 # header file `<packageDir>/cmake/<packageName>_config.h.in`_). This macro is
486 # typically called in the package's `<packageDir>/CMakeLists.txt`_ file (see
487 # the example ``SimpleCxx/CMakeLists.txt``).
488 #
489 # NOTE: This also calls `tribits_pkg_export_cache_var()`_ to export the
490 # variable ``${PACKAGE_NAME}_ENABLE_DEBUG``.
491 #
492 macro(tribits_add_debug_option)
493 tribits_add_option_and_define(
494 ${PACKAGE_NAME}_ENABLE_DEBUG
495 HAVE_${PACKAGE_NAME_UC}_DEBUG
496 "Enable a host of runtime debug checking."
497 ${${PROJECT_NAME}_ENABLE_DEBUG}
498 )
499 endmacro()
500
501
502 macro(tribits_add_enable_teuchos_time_monitor_option)
503 option(
504 ${PACKAGE_NAME}_ENABLE_TEUCHOS_TIME_MONITOR
505 "Enable Teuchos time monitors for package ${PACKAGE_NAME}"
506 ${${PROJECT_NAME}_ENABLE_TEUCHOS_TIME_MONITOR}
507 )
508 endmacro()
509
510
511 # @MACRO: tribits_add_show_deprecated_warnings_option()
512 #
513 # Add the standard option ``${PACKAGE_NAME}_SHOW_DEPRECATED_WARNINGS`` for the
514 # package.
515 #
516 # Usage::
517 #
518 # tribits_add_show_deprecated_warnings_option()
519 #
520 # This macro should be called in the package's <packageDir>/CMakeLists.txt`_
521 # file. This option is given the default value
522 # ``${${PROJECT_NAME}_SHOW_DEPRECATED_WARNINGS}``. This option is then looked
523 # for in `tribits_configure_file()`_ to add macros to add deprecated warnings
524 # to deprecated parts of a package.
525 #
526 macro(tribits_add_show_deprecated_warnings_option)
527 advanced_set(
528 ${PACKAGE_NAME}_SHOW_DEPRECATED_WARNINGS ${${PROJECT_NAME}_SHOW_DEPRECATED_WARNINGS}
529 CACHE BOOL
530 "Show warnings about deprecated code in ${PACKAGE_NAME}"
531 )
532 advanced_set(
533 ${PACKAGE_NAME}_HIDE_DEPRECATED_CODE ${${PROJECT_NAME}_HIDE_DEPRECATED_CODE}
534 CACHE BOOL
535 "Fully exclude deprecated code in ${PACKAGE_NAME}"
536 )
537 endmacro()
538
539
540 macro(tribits_add_explicit_instantiation_option)
541 tribits_add_option_and_define(
542 ${PACKAGE_NAME}_ENABLE_EXPLICIT_INSTANTIATION
543 HAVE_${PACKAGE_NAME_UC}_EXPLICIT_INSTANTIATION
544 "Enable the use of explicit template instantiation."
545 ${${PROJECT_NAME}_ENABLE_EXPLICIT_INSTANTIATION}
546 )
547 endmacro()
548
549
550 macro(tribits_add_eti_support)
551 append_global_set(${PROJECT_NAME}_ETI_PACKAGES ${PACKAGE_NAME})
552 global_null_set(${PACKAGE_NAME}_ETI_LIBRARYSET)
553 endmacro()
554
555
556 # @MACRO: tribits_add_example_directories()
557 #
558 # Macro called to conditionally add a set of example directories for an
559 # package.
560 #
561 # Usage::
562 #
563 # tribits_add_example_directories(<dir1> <dir2> ...)
564 #
565 # This macro typically is called from the top-level
566 # `<packageDir>/CMakeLists.txt`_ file for which all subdirectories are all
567 # "examples" according to standard package layout.
568 #
569 # This macro can be called several times within a package as desired to break
570 # up example directories any way one would like.
571 #
572 # Currently, all it does macro does is to call ``add_subdirectory(<diri>)`` if
573 # `${PACKAGE_NAME}_ENABLE_EXAMPLES`_ ``= TRUE``.
574 #
575 macro(tribits_add_example_directories)
576
577 tribits_add_test_example_directories_assert_call_context(
578 tribits_add_example_directories)
579
580 if(${PACKAGE_NAME}_ENABLE_EXAMPLES)
581 foreach(EXAMPLE_DIR ${ARGN})
582 tribits_trace_file_processing(PACKAGE ADD_SUBDIR
583 "${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_DIR}/CMakeLists.txt")
584 add_subdirectory(${EXAMPLE_DIR})
585 endforeach()
586 endif()
587
588 endmacro()
589
590
591 macro(tribits_add_test_example_directories_assert_call_context macroName)
592
593 if (CURRENTLY_PROCESSING_SUBPACKAGE)
594
595 # This is a subpackage being processed
596
597 if(NOT ${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_CALLED)
598 tribits_report_invalid_tribits_usage(
599 "Must call tribits_subpackage() before ${macroName}()"
600 " in ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
601 endif()
602
603 if(${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED)
604 tribits_report_invalid_tribits_usage(
605 "Must call ${macroName}() before"
606 " tribits_subpackage_postprocess() in"
607 " ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
608 endif()
609
610 else()
611
612 # This is a package being processed
613
614 if(NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED)
615 tribits_report_invalid_tribits_usage(
616 "Must call tribits_package() or tribits_package_decl() before"
617 " ${macroName}() in ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
618 endif()
619
620 if(${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS_CALLED)
621 tribits_report_invalid_tribits_usage(
622 "Must call ${macroName}() before "
623 " tribits_package_postprocess() in ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
624 endif()
625
626 endif()
627
628 endmacro()
629
630
631 # Utility function that sets up package linkage linkage variables in case the
632 # package has no libraries.
633 #
634 function(tribits_package_finalize_dependency_vars)
635
636 if(${PACKAGE_NAME}_SUBPACKAGES)
637
638 # A package with subpackages should get all of its dependency vars from
639 # its enabled subpackages.
640
641 set(PARENT_PACKAGE_LIBRARIES "")
642
643 set(SUBPACKAGE_IDX 0)
644 foreach(TRIBITS_SUBPACKAGE ${${PARENT_PACKAGE_NAME}_SUBPACKAGES})
645
646 set(SUBPACKAGE_NAME ${TRIBITS_SUBPACKAGE})
647 set(SUBPACKAGE_FULLNAME ${PARENT_PACKAGE_NAME}${TRIBITS_SUBPACKAGE})
648
649 if (${PROJECT_NAME}_ENABLE_${SUBPACKAGE_FULLNAME})
650 prepend_set(PARENT_PACKAGE_LIBRARIES
651 ${${SUBPACKAGE_FULLNAME}_LIBRARIES})
652 endif()
653
654 math(EXPR SUBPACKAGE_IDX "${SUBPACKAGE_IDX}+1")
655
656 endforeach()
657
658 # NOTE: There can't be any duplicate libraries in PARENT_PACKAGE_LIBRARIES
659 # so no need to remove them.
660 global_set(${PACKAGE_NAME}_LIBRARIES "${PARENT_PACKAGE_LIBRARIES}")
661
662 endif()
663
664 endfunction()
665
666
667 # Helper macro for [SUB]tribits_package_postprocess()
668 #
669 macro(tribits_package_postprocess_common)
670
671 if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
672 message("\nTRIBITS_PACKAGE_POSTPROCESS_COMMON: ${PACKAGE_NAME}")
673 print_var(${PACKAGE_NAME}_LIBRARIES)
674 endif()
675
676 tribits_package_create_all_libs_interface_library()
677
678 if (${PROJECT_NAME}_ENABLE_INSTALL_CMAKE_CONFIG_FILES
679 AND (NOT ${PROJECT_NAME}_ENABLE_INSTALLATION_TESTING)
680 )
681 tribits_write_package_client_export_files(${PACKAGE_NAME})
682 endif()
683
684 set(${PACKAGE_NAME}_FINISHED_FIRST_CONFIGURE TRUE
685 CACHE INTERNAL "")
686
687 endmacro()
688
689
690 # Macro to create the ${PACKAGE_NAME}::all_libs INTERFACE target
691 #
692 macro(tribits_package_create_all_libs_interface_library)
693
694 if (NOT TARGET ${PACKAGE_NAME}_all_libs)
695
696 # Find all of the non-TESTONLY library targets
697 tribits_get_all_build_targets_including_in_subdirs("${CMAKE_CURRENT_SOURCE_DIR}"
698 "STATIC_LIBRARY;SHARED_LIBRARY;INTERFACE_LIBRARY"
699 allPackageBuildableLibTargetsList )
700 #print_var(allPackageBuildableLibTargetsList)
701 set(packageLibsInAllLibsList)
702 foreach (libTarget IN LISTS allPackageBuildableLibTargetsList)
703 tribits_lib_is_testonly(${libTarget} isTestOnlyLib)
704 #print_var(isTestOnlyLib)
705 if (NOT isTestOnlyLib)
706 list(APPEND packageLibsInAllLibsList ${libTarget})
707 endif()
708 endforeach()
709 global_set(${PACKAGE_NAME}_EXPORTED_PACKAGE_LIBS_NAMES
710 ${packageLibsInAllLibsList})
711
712 # Create the ${PACKAGE_NAME}_all_libs INTERFACE interface target
713 add_library(${PACKAGE_NAME}_all_libs INTERFACE)
714 target_link_libraries(${PACKAGE_NAME}_all_libs
715 INTERFACE ${packageLibsInAllLibsList} )
716 set_target_properties(${PACKAGE_NAME}_all_libs PROPERTIES
717 EXPORT_NAME all_libs)
718 if (${PROJECT_NAME}_IMPORTED_NO_SYSTEM)
719 set_target_properties(${PACKAGE_NAME}_all_libs PROPERTIES IMPORTED_NO_SYSTEM TRUE)
720 endif()
721
722 # Install the interface target (makes sure it gets put in
723 # <Package>Targets.cmake file)
724 install(
725 TARGETS ${PACKAGE_NAME}_all_libs
726 EXPORT ${PACKAGE_NAME}
727 INCLUDES DESTINATION "${${PROJECT_NAME}_INSTALL_INCLUDE_DIR}"
728 RUNTIME DESTINATION "${${PROJECT_NAME}_INSTALL_RUNTIME_DIR}"
729 LIBRARY DESTINATION "${${PROJECT_NAME}_INSTALL_LIB_DIR}"
730 ARCHIVE DESTINATION "${${PROJECT_NAME}_INSTALL_LIB_DIR}"
731 COMPONENT ${PACKAGE_NAME}
732 )
733
734 if (NOT TARGET ${PACKAGE_NAME}::all_libs)
735 # Create ALIAS ${PACKAGE_NAME}::all_libs target
736 add_library(${PACKAGE_NAME}::all_libs ALIAS ${PACKAGE_NAME}_all_libs)
737 endif()
738
739 endif()
740
741 #include(CMakePrintHelpers)
742 #cmake_print_properties(TARGETS ${PACKAGE_NAME}_all_libs ${PACKAGE_NAME}::all_libs
743 # PROPERTIES TYPE ALIASED_TARGET INTERFACE_LINK_LIBRARIES)
744
745 endmacro()
746
747
748 # @MACRO: tribits_package_postprocess()
749 #
750 # Macro called at the very end of a package's top-level
751 # `<packageDir>/CMakeLists.txt`_ file that performs some critical
752 # post-processing activities.
753 #
754 # Usage::
755 #
756 # tribits_package_postprocess()
757 #
758 # NOTE: This creates the aliased target ``${PACKAGE_NAME}::all_libs`` for all
759 # libraries in all subdirectories that don't have the TRIBITS_TESTONLY_LIB
760 # target property set on them.
761 #
762 # NOTE: It is unfortunate that this macro must be called in a package's
763 # top-level ``CMakeLists.txt`` file but limitations of the CMake language make
764 # it necessary to do so.
765 #
766 macro(tribits_package_postprocess)
767
768 # check that this is not being called from inside a subpackage
769 if (CURRENTLY_PROCESSING_SUBPACKAGE)
770 if(NOT ${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED)
771 tribits_report_invalid_tribits_usage(
772 "Cannot call tribits_package_postprocess() in a subpackage."
773 " Use tribits_subpackage_postprocess() instead"
774 " ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
775 endif()
776 endif()
777
778 if(${PACKAGE_NAME}_SUBPACKAGES)
779
780 # This is a package that has subpackages
781 if(NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED OR
782 NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_DEF_CALLED OR
783 NOT ${PACKAGE_NAME}_TRIBITS_PROCESS_SUBPACKAGES_CALLED )
784
785 tribits_report_invalid_tribits_usage(
786 "Must call tribits_package_decl(), tribits_process_subpackages()"
787 " and tribits_package_def() before tribits_package_postprocess()."
788 " Because this package has subpackages you cannot use tribits_package()"
789 " you must call these in the following order:"
790 " tribits_package_decl()"
791 " tribits_process_subpackages()"
792 " tribits_package_def()"
793 " tribits_package_postprocess()"
794 " in: "
795 "${TRIBITS_PACKAGE_CMAKELIST_FILE}"
796 )
797 endif()
798
799 else()
800
801 # This is a package without subpackages
802
803 if (
804 (NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_CALLED)
805 AND
806 (NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_DEF_CALLED)
807 )
808 tribits_report_invalid_tribits_usage(
809 "Must call tribits_package() or tribits_package_def() before"
810 " tribits_package_postprocess()"
811 " at the top of the file:\n"
812 " ${TRIBITS_PACKAGE_CMAKELIST_FILE}"
813 )
814 endif()
815
816 endif()
817
818 if(NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_DEF_CALLED)
819 tribits_report_invalid_tribits_usage(
820 "Must call tribits_package() before tribits_package_postprocess()."
821 " Or if your package has subpackages you must first call tribits_package_decl(),"
822 " then tribits_process_subpackages(), then tribits_package_def(), then"
823 " tribits_package_postprocess() in"
824 " ${TRIBITS_PACKAGE_CMAKELIST_FILE}"
825 )
826 endif()
827
828 if(${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS_CALLED)
829 tribits_report_invalid_tribits_usage(
830 "tribits_package_postprocess() has been called more than once in"
831 " ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
832 endif()
833
834 # Only parent packages have the targets (${PACKAGE_NAME}_libs and
835 # (${PACKAGE_NAME}_all
836 if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
837 message("\nTRIBITS_PACKAGE_POSTPROCESS: ${PACKAGE_NAME}")
838 print_var(${PACKAGE_NAME}_LIB_TARGETS)
839 print_var(${PACKAGE_NAME}_ALL_TARGETS)
840 endif()
841 add_custom_target(${PACKAGE_NAME}_libs DEPENDS ${${PACKAGE_NAME}_LIB_TARGETS})
842 add_custom_target(${PACKAGE_NAME}_all DEPENDS ${${PACKAGE_NAME}_ALL_TARGETS})
843
844 tribits_package_finalize_dependency_vars()
845 tribits_package_postprocess_common()
846
847 if (${PACKAGE_NAME}_SOURCE_DIR STREQUAL ${PROJECT_NAME}_SOURCE_DIR)
848 set(${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS TRUE)
849 else()
850 set(${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS TRUE PARENT_SCOPE)
851 endif()
852
853 set(${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS_CALLED TRUE)
854
855 endmacro()
856
857
858 # @MACRO: tribits_process_subpackages()
859 #
860 # Macro that processes the `TriBITS Subpackages`_ for a parent `TriBITS
861 # package`_ for packages that are broken down into subpackages. This is
862 # called in the parent packages top-level `<packageDir>/CMakeLists.txt`_ file.
863 #
864 # Usage::
865 #
866 # tribits_process_subpackages()
867 #
868 # This macro must be called after `tribits_package_decl()`_ but before
869 # `tribits_package_def()`_.
870 #
871 macro(tribits_process_subpackages)
872
873 if (CURRENTLY_PROCESSING_SUBPACKAGE)
874 tribits_report_invalid_tribits_usage(
875 "Cannot call tribits_process_subpackages() in a subpackage."
876 " subpackages cannot contain other subpackages"
877 " ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
878 endif()
879
880 if (${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS_CALLED)
881 tribits_report_invalid_tribits_usage(
882 "Must call tribits_process_subpackages() before tribits_package_postprocess()"
883 " in ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
884 endif()
885
886 if (NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED)
887 tribits_report_invalid_tribits_usage(
888 "Must call tribits_package_decl() before tribits_process_subpackages()"
889 " in ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
890 endif()
891
892 if (${PACKAGE_NAME}_TRIBITS_PACKAGE_DEF_CALLED)
893 tribits_report_invalid_tribits_usage(
894 "Must call tribits_package_def() after tribits_process_subpackages()"
895 " in ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
896 endif()
897
898 if (NOT ${PARENT_PACKAGE_NAME}_SUBPACKAGES)
899 tribits_report_invalid_tribits_usage(
900 "The TriBITS Package '${PACKAGE_NAME}' does not have any subpackages."
901 " Therefore, you are not allowed to call tribits_process_subpackages()!")
902 endif()
903
904 set(SUBPACKAGE_IDX 0)
905 foreach(TRIBITS_SUBPACKAGE ${${PARENT_PACKAGE_NAME}_SUBPACKAGES})
906
907 #message("")
908 #print_var(SUBPACKAGE_IDX)
909 #print_var(TRIBITS_SUBPACKAGE)
910
911 set(SUBPACKAGE_NAME ${TRIBITS_SUBPACKAGE})
912 set(SUBPACKAGE_FULLNAME ${PARENT_PACKAGE_NAME}${TRIBITS_SUBPACKAGE})
913 #print_var(SUBPACKAGE_FULLNAME)
914
915 if (${PROJECT_NAME}_ENABLE_${SUBPACKAGE_FULLNAME})
916
917 list(GET ${PARENT_PACKAGE_NAME}_SUBPACKAGE_DIRS ${SUBPACKAGE_IDX} SUBPACKAGE_DIR)
918 #print_var(SUBPACKAGE_DIR)
919
920 if (NOT ${PROJECT_NAME}_BINARY_DIR STREQUAL ${PARENT_PACKAGE_NAME}_BINARY_DIR)
921 dual_scope_set(${SUBPACKAGE_FULLNAME}_BINARY_DIR
922 ${${PARENT_PACKAGE_NAME}_BINARY_DIR}/${SUBPACKAGE_DIR})
923 else()
924 set(${SUBPACKAGE_FULLNAME}_BINARY_DIR
925 ${${PARENT_PACKAGE_NAME}_BINARY_DIR}/${SUBPACKAGE_DIR})
926 endif()
927
928 set(CURRENT_SUBPACKAGE_CMAKELIST_FILE
929 "${${SUBPACKAGE_FULLNAME}_SOURCE_DIR}/CMakeLists.txt")
930 tribits_trace_file_processing(PACKAGE ADD_SUBDIR
931 ${CURRENT_SUBPACKAGE_CMAKELIST_FILE} )
932 set(CURRENTLY_PROCESSING_SUBPACKAGE ${SUBPACKAGE_FULLNAME})
933 add_subdirectory(${${SUBPACKAGE_FULLNAME}_SOURCE_DIR}
934 ${${SUBPACKAGE_FULLNAME}_BINARY_DIR})
935
936 endif()
937
938 math(EXPR SUBPACKAGE_IDX "${SUBPACKAGE_IDX}+1")
939
940 endforeach()
941
942 set(CURRENTLY_PROCESSING_SUBPACKAGE FALSE)
943 set(${PACKAGE_NAME}_TRIBITS_PROCESS_SUBPACKAGES_CALLED TRUE)
944
945 endmacro()