Debian Codegraph @

cmake/tribits/core/package_arch/TribitsAddExecutable.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
11 include("${CMAKE_CURRENT_LIST_DIR}/../test_support/TribitsAddExecutableTestHelpers.cmake")
12 include("${CMAKE_CURRENT_LIST_DIR}/../test_support/TribitsAddTestHelpers.cmake")
13 include(TribitsCommonArgsHelpers)
14 include(TribitsGeneralMacros)
15 include(TribitsLibIsTestOnly)
16 include(TribitsReportInvalidTribitsUsage)
17 include(TribitsDeprecatedHelpers)
18
19 include(PrintVar)
20 include(AppendSet)
21 include(CMakeParseArguments)
22
23
24 # @FUNCTION: tribits_add_executable()
25 #
26 # Function used to create an executable (typically for a test or example),
27 # using the built-in CMake command ``add_executable()``.
28 #
29 # Usage::
30 #
31 # tribits_add_executable(
32 # <exeRootName> [NOEXEPREFIX] [NOEXESUFFIX] [ADD_DIR_TO_NAME]
33 # SOURCES <src0> <src1> ...
34 # [CATEGORIES <category0> <category1> ...]
35 # [HOST <host0> <host1> ...]
36 # [XHOST <host0> <host1> ...]
37 # [HOSTTYPE <hosttype0> <hosttype1> ...]
38 # [XHOSTTYPE <hosttype0> <hosttype1> ...]
39 # [EXCLUDE_IF_NOT_TRUE <varname0> <varname1> ...]
40 # [DIRECTORY <dir>]
41 # [TESTONLYLIBS <lib0> <lib1> ...]
42 # [IMPORTEDLIBS <lib0> <lib1> ...]
43 # [COMM [serial] [mpi]]
44 # [LINKER_LANGUAGE (C|CXX|Fortran)]
45 # [TARGET_DEFINES -D<define0> -D<define1> ...]
46 # [INSTALLABLE]
47 # [ADDED_EXE_TARGET_NAME_OUT <exeTargetName>]
48 # )
49 #
50 # *Sections:*
51 #
52 # * `Formal Arguments (tribits_add_executable())`_
53 # * `Executable and Target Name (tribits_add_executable())`_
54 # * `Additional Executable and Source File Properties (tribits_add_executable())`_
55 # * `Install Target (tribits_add_executable())`_
56 #
57 # .. _Formal Arguments (tribits_add_executable()):
58 #
59 # **Formal Arguments (tribits_add_executable())**
60 #
61 # ``<exeRootName>``
62 #
63 # The root name of the executable (and CMake target) (see `Executable and
64 # Target Name (tribits_add_executable())`_). This must be the first
65 # argument.
66 #
67 # ``NOEXEPREFIX``
68 #
69 # If passed in, then ``${PACKAGE_NAME}_`` is not added the beginning of
70 # the executable name (see `Executable and Target Name
71 # (tribits_add_executable())`_).
72 #
73 # ``NOEXESUFFIX``
74 #
75 # If passed in, then ``${${PROJECT_NAME}_CMAKE_EXECUTABLE_SUFFIX}`` and
76 # not added to the end of the executable name (except for native Windows
77 # builds, see `Executable and Target Name (tribits_add_executable())`_).
78 #
79 # ``ADD_DIR_TO_NAME``
80 #
81 # If passed in, the directory path relative to the package's base
82 # directory (with "/" replaced by "_") is added to the executable name
83 # (see `Executable and Target Name (tribits_add_executable())`_). This
84 # provides a simple way to create unique test executable names inside of a
85 # given TriBITS package. Only test executables in the same directory
86 # would need to have unique ``<execRootName>`` passed in.
87 #
88 # ``SOURCES <src0> <src1> ...``
89 #
90 # Gives the source files that will be compiled into the built executable.
91 # By default, these sources are assumed to be in the current working
92 # directory (or can contain the relative path or absolute path). If
93 # ``<srci>`` is an absolute path, then that full file path is used. This
94 # list of sources (with adjusted directory path) are passed into
95 # ``add_executable(<exeTargetName> ... )``. After calling this function,
96 # the properties of the source files can be altered using the built-in
97 # CMake command ``set_source_file_properties()``.
98 #
99 # ``DIRECTORY <dir>``
100 #
101 # If specified, then the generated executable ``<exeTargetName>`` is
102 # placed in the relative or absolute directory ``<dir>``. If ``<dir>`` is
103 # not an absolute path, then the generated executable is placed in the
104 # directory ``${CMAKE_CURRENT_BINARY_DIR}/<dir>/``. Also, the sources for
105 # the executable listed in ``SOURCES <src0> <src1> ...`` are assumed to be
106 # in the relative or absolute directory ``<dir>`` instead of the current
107 # source directory. This directory path is prepended to each source file
108 # name ``<srci>`` unless ``<srci>`` is an absolute path. If ``<dir>`` is
109 # not an absolute path, then source files listed in ``SOURCES`` are
110 # assumed to be in the directory ``${CMAKE_CURRENT_SOURCE_DIR}/<dir>/``.
111 #
112 # ``CATEGORIES <category0> <category1> ...``
113 #
114 # Gives the `Test Test Categories`_ for which this test will be added.
115 # See `tribits_add_test()`_ for more details.
116 #
117 # ``HOST <host0> <host1> ...``
118 #
119 # The list of hosts for which to enable the test (see `tribits_add_test()`_).
120 #
121 # ``XHOST <host0> <host1> ...``
122 #
123 # The list of hosts for which **not** to enable the test (see
124 # `tribits_add_test()`_).
125 #
126 # ``HOSTTYPE <hosttype0> <hosttype1> ...``
127 #
128 # The list of host types for which to enable the test (see
129 # `tribits_add_test()`_).
130 #
131 # ``XHOSTTYPE <hosttype0> <hosttype1> ...``
132 #
133 # The list of host types for which **not** to enable the test (see
134 # `tribits_add_test()`_).
135 #
136 # ``EXCLUDE_IF_NOT_TRUE <varname0> <varname1> ...``
137 #
138 # If specified, gives the names of CMake variables that must evaluate to
139 # true, or the test will not be added (see `tribits_add_test()`_).
140 #
141 # ``TESTONLYLIBS <lib0> <lib1> ...``
142 #
143 # Specifies extra test-only libraries defined in this CMake project that
144 # will be linked to the executable using ``target_link_libraries()``.
145 # Note that regular libraries (i.e. not ``TESTONLY``) defined in the
146 # current package or any upstream packages can *NOT* be listed! TriBITS
147 # automatically links non ``TESTONLY`` libraries in this package and
148 # upstream packages to the executable. The only libraries that should be
149 # listed in this argument are either ``TESTONLY`` libraries.
150 #
151 # ``IMPORTEDLIBS <lib0> <lib1> ...``
152 #
153 # Specifies extra external libraries that will be linked to the executable
154 # using ``target_link_libraries()``. This can only be used for libraries
155 # that are built external from this CMake project and are not provided
156 # through a proper `TriBITS TPL`_. The latter usage of passing in
157 # external libraries is not recommended. External libraries should be
158 # handled as declared `TriBITS TPLs`_. So far, the only case where
159 # ``IMPORTEDLIBS`` has been shown to be necessary is to pass in the
160 # standard C math library ``m``. In every other case, a TriBITS TPL
161 # should be used instead.
162 #
163 # ``COMM [serial] [mpi]``
164 #
165 # If specified, selects if the test will be added in serial and/or MPI
166 # mode. See the ``COMM`` argument in the script
167 # `tribits_add_test()`_ for more details.
168 #
169 # ``LINKER_LANGUAGE (C|CXX|Fortran)``
170 #
171 # If specified, overrides the linker language used by setting the built-in
172 # CMake target property ``LINKER_LANGUAGE``. TriBITS sets the default
173 # linker language as follows::
174 #
175 # if (${PROJECT_NAME}_ENABLE_CXX)
176 # set(LINKER_LANGUAGE CXX)
177 # elseif (${PROJECT_NAME}_ENABLE_C)
178 # set(LINKER_LANGUAGE C)
179 # else()
180 # # Let CMake set the default linker language it wants based
181 # # on source file extensions passed into ``add_executable()``.
182 # endif()
183 #
184 # The reason for this logic is that on some platform if you have a Fortran
185 # or C main that links to C++ libraries, then you need the C++ compiler to
186 # do the final linking. CMake does not seem to automatically know that it
187 # is pulling in C++ libraries and therefore needs to be told use C++ for
188 # linking. This is the correct default behavior for mixed-language
189 # projects. However, this argument allows the developer to override this
190 # logic and use any linker language desired based on other considerations.
191 #
192 # ``TARGET_DEFINES -D<define0> -D<define1> ...``
193 #
194 # Add the listed defines using
195 # ``target_compile_definitions(<exeTargetName> ...)``. These should only
196 # affect the listed sources for the built executable and not other
197 # targets.
198 #
199 # ``INSTALLABLE``
200 #
201 # If passed in, then an install target will be added to install the built
202 # executable into the ``${CMAKE_INSTALL_PREFIX}/bin/`` directory (see
203 # `Install Target (tribits_add_executable())`_).
204 #
205 # ``ADDED_EXE_TARGET_NAME_OUT <exeTargetName>``
206 #
207 # If specified, then on output the variable ``<exeTargetName>`` will be
208 # set with the name of the executable target passed to
209 # ``add_executable(<exeTargetName> ... )``. Having this name allows the
210 # calling ``CMakeLists.txt`` file access and set additional target
211 # properties (see `Additional Executable and Source File Properties
212 # (tribits_add_executable())`_).
213 #
214 # .. _Executable and Target Name (tribits_add_executable()):
215 #
216 # **Executable and Target Name (tribits_add_executable())**
217 #
218 # By default, the full name of the executable and target name
219 # is::
220 #
221 # <exeTargetName> = ${PACKAGE_NAME}_<exeRootName>
222 #
223 # If ``ADD_DIR_TO_NAME`` is set, then the directory path relative to the
224 # package base directory (with "/" replaced with "_"), or ``<relDirName>``, is
225 # added to the executable name to form::
226 #
227 # <exeTargetName> = ${PACKAGE_NAME}_<relDirName>_<exeRootName>
228 #
229 # If the option ``NOEXEPREFIX`` is passed in, then the prefix
230 # ``${PACKAGE_NAME}_`` is removed.
231 #
232 # The executable suffix ``${${PROJECT_NAME}_CMAKE_EXECUTABLE_SUFFIX}`` will be
233 # added to the actual executable file name if the option ``NOEXESUFFIX`` is
234 # *not* passed in but this suffix is never added to the target name.
235 # (However, note that on Windows platforms, the default ``*.exe`` extension is
236 # always added because windows will not run an executable in many contexts
237 # unless it has the ``*.exe`` extension.)
238 #
239 # The reason that a default prefix is prepended to the executable and target
240 # name is because the primary reason to create an executable is typically to
241 # create a test or an example that is private to the package. This prefix
242 # helps to namespace the executable and its target so as to avoid name clashes
243 # with targets in other packages. It also helps to avoid clashes if the
244 # executable gets installed into the install directory (if ``INSTALLABLE`` is
245 # specified). For general utility executables on Linux/Unix systems,
246 # ``NOEXEPREFIX`` and ``NOEXESUFFIX`` should be passed in. In this case, one
247 # must be careful to pick ``<exeRootName>`` that will be sufficiently globally
248 # unique. Please use common sense when picking non-namespaced names.
249 #
250 # .. _Additional Executable and Source File Properties (tribits_add_executable()):
251 #
252 # **Additional Executable and Source File Properties (tribits_add_executable())**
253 #
254 # Once ``add_executable(<exeTargetName> ... )`` is called and this function
255 # exists, one can set and change properties on the ``<exeTargetName>``
256 # executable target using the built-in ``set_target_properties()`` command as
257 # well as properties on any of the source files listed in ``SOURCES`` using
258 # the built-in ``set_source_file_properties()`` command just like in any CMake
259 # project. IF the executable is added, its name will be returned by the
260 # argument ``ADDED_EXE_TARGET_NAME_OUT <exeTargetName>``. For example::
261 #
262 # tribits_add_executable( someExe ...
263 # ADDED_EXE_TARGET_NAME_OUT someExe_TARGET_NAME )
264 #
265 # if (someExe_TARGET_NAME)
266 # set_target_properties( ${someExe_TARGET_NAME}
267 # PROPERTIES LINKER_LANGUAGE CXX )
268 # endif()
269 #
270 # The ``if(someExe_TARGET_NAME)`` is needed in case the executable does not
271 # get added for some reason (see `Formal Arguments
272 # (tribits_add_executable())`_ for logic that can result in the executable
273 # target not getting added).
274 #
275 # .. _Install Target (tribits_add_executable()):
276 #
277 # **Install Target (tribits_add_executable())**
278 #
279 # If ``INSTALLABLE`` is passed in, then an install target using the built-in
280 # CMake command ``install(TARGETS <exeTargetName> ...)`` is added to install
281 # the built executable into the ``${CMAKE_INSTALL_PREFIX}/bin/`` directory
282 # (actual install directory path is determined by
283 # ``${PROJECT_NAME}_INSTALL_RUNTIME_DIR``, see `Setting the install prefix`_).
284 #
285 function(tribits_add_executable EXE_NAME)
286
287 if(${PROJECT_NAME}_VERBOSE_CONFIGURE)
288 message("")
289 message("TRIBITS_ADD_EXECUTABLE: ${EXE_NAME} ${ARGN}")
290 endif()
291
292 tribits_add_executable_assert_correct_call_context()
293
294 #
295 # A) Parse the input arguments
296 #
297
298 cmake_parse_arguments(
299 #prefix
300 PARSE
301 #options
302 "NOEXEPREFIX;NOEXESUFFIX;ADD_DIR_TO_NAME;INSTALLABLE"
303 #one_value_keywords
304 ""
305 #multi_value_keywords
306 "SOURCES;CATEGORIES;HOST;XHOST;HOSTTYPE;XHOSTTYPE;EXCLUDE_IF_NOT_TRUE;DIRECTORY;TESTONLYLIBS;IMPORTEDLIBS;DEPLIBS;COMM;LINKER_LANGUAGE;TARGET_DEFINES;DEFINES;ADDED_EXE_TARGET_NAME_OUT"
307 ${ARGN}
308 )
309
310 tribits_check_for_unparsed_arguments()
311
312 # Executable not added by default!
313 if(PARSE_ADDED_EXE_TARGET_NAME_OUT)
314 set(${PARSE_ADDED_EXE_TARGET_NAME_OUT} "" PARENT_SCOPE)
315 endif()
316
317 set(EXE_BINARY_NAME ${EXE_NAME})
318 tribits_add_executable_adjust_exe_name(EXE_BINARY_NAME)
319
320 tribits_add_executable_is_skipped(skipAddExecutable)
321 if (skipAddExecutable)
322 return()
323 endif()
324
325 tribits_add_executable_get_adjusted_sources_list(EXE_SOURCES)
326
327 tribits_add_executable_assert_testonlylibs()
328
329 tribits_add_executable_assert_importedlibs()
330
331 tribits_add_executable_convert_from_deplibs()
332
333 #
334 # B) Add the executable and set its properties
335 #
336
337 if(${PROJECT_NAME}_VERBOSE_CONFIGURE)
338 message("TRIBITS_ADD_EXECUTABLE: add_executable(${EXE_BINARY_NAME} ${EXE_SOURCES})")
339 endif()
340 add_executable(${EXE_BINARY_NAME} ${EXE_SOURCES})
341 append_global_set(${PARENT_PACKAGE_NAME}_ALL_TARGETS ${EXE_BINARY_NAME})
342
343 if(PARSE_ADDED_EXE_TARGET_NAME_OUT)
344 set(${PARSE_ADDED_EXE_TARGET_NAME_OUT} ${EXE_BINARY_NAME} PARENT_SCOPE)
345 endif()
346
347 if (PARSE_DEFINES)
348 tribits_deprecated("Passing extra defines through 'DEFINES' ${PARSE_DEFINES}"
349 " is deprecated. Instead, pass them through 'TARGET_DEFINES'. The 'DEFINES'"
350 " argument was incorrectly implemented by calling add_definitions() which has"
351 " directory scope and not function scope as was documented. This resulted in"
352 " confusing behavior. If one wishes to set defines at the directly level,"
353 " just call add_definitions() directly.")
354 add_definitions(${PARSE_DEFINES})
355 endif()
356
357 if (PARSE_TARGET_DEFINES)
358 target_compile_definitions(${EXE_BINARY_NAME} PUBLIC ${PARSE_TARGET_DEFINES})
359 endif()
360
361 if(PARSE_NOEXESUFFIX AND NOT WIN32)
362 set_target_properties(${EXE_BINARY_NAME} PROPERTIES SUFFIX "")
363 else()
364 set_target_properties(${EXE_BINARY_NAME} PROPERTIES SUFFIX
365 ${${PROJECT_NAME}_CMAKE_EXECUTABLE_SUFFIX})
366 endif()
367
368 tribits_set_linker_language_from_arg( ${EXE_BINARY_NAME}
369 "${PARSE_LINKER_LANGUAGE}" )
370
371 assert_defined(${PROJECT_NAME}_LINK_SEARCH_START_STATIC)
372 if (${PROJECT_NAME}_LINK_SEARCH_START_STATIC)
373 #message("${EXE_BINARY_NAME}: Adding property LINK_SEARCH_START_STATIC")
374 set_property(TARGET ${EXE_BINARY_NAME} PROPERTY LINK_SEARCH_START_STATIC 1)
375 endif()
376
377 if(PARSE_DIRECTORY)
378 set_target_properties( ${EXE_BINARY_NAME} PROPERTIES
379 RUNTIME_OUTPUT_DIRECTORY ${PARSE_DIRECTORY} )
380 endif()
381
382 set_property(TARGET ${EXE_BINARY_NAME} APPEND PROPERTY
383 LABELS ${PACKAGE_NAME}Exes ${PARENT_PACKAGE_NAME}Exes)
384
385 #
386 # C) Link ${EXE_BINARY_NAME} to direct upstream libraries
387 #
388
389 target_link_libraries(${EXE_BINARY_NAME} PUBLIC ${${PACKAGE_NAME}_LIBRARIES})
390 foreach(depPkg IN LISTS ${PACKAGE_NAME}_LIB_ENABLED_DEPENDENCIES
391 ${PACKAGE_NAME}_TEST_ENABLED_DEPENDENCIES
392 )
393 target_link_libraries(${EXE_BINARY_NAME} PUBLIC ${depPkg}::all_libs)
394 endforeach()
395 foreach(testOnlyLib ${PARSE_TESTONLYLIBS})
396 target_link_libraries(${EXE_BINARY_NAME} PUBLIC
397 "${${PROJECT_NAME}_LIBRARY_NAME_PREFIX}${testOnlyLib}")
398 endforeach()
399
400 #
401 # D) Install if asked
402 #
403
404 if(${PROJECT_NAME}_INSTALL_EXECUTABLES AND PARSE_INSTALLABLE)
405 install(
406 TARGETS ${EXE_BINARY_NAME}
407 EXPORT ${PROJECT_NAME}
408 DESTINATION ${${PROJECT_NAME}_INSTALL_RUNTIME_DIR}
409 COMPONENT ${PACKAGE_NAME}
410 )
411 endif()
412
413 endfunction()
414
415
416 # Assert tribits_add_executable() is called in the correct context
417 #
418 # NOTE: This read the variables from the enclosing tribits_add_executable()
419 # function call scope.
420 #
421 function(tribits_add_executable_assert_correct_call_context)
422
423 if (CURRENTLY_PROCESSING_SUBPACKAGE)
424
425 # This is a subpackage being processed
426
427 if(NOT ${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_CALLED)
428 tribits_report_invalid_tribits_usage(
429 "Must call tribits_subpackage() before tribits_add_executable()"
430 " in ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
431 endif()
432
433 if(${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED)
434 tribits_report_invalid_tribits_usage(
435 "Must call tribits_add_executable() before "
436 " tribits_subpackage_postprocess() in ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
437 endif()
438
439 else()
440
441 # This is a package being processed
442
443 if(NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED)
444 tribits_report_invalid_tribits_usage(
445 "Must call tribits_package() or tribits_package_decl() before"
446 " tribits_add_executable() in ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
447 endif()
448
449 if(${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS_CALLED)
450 tribits_report_invalid_tribits_usage(
451 "Must call tribits_add_executable() before "
452 " tribits_package_postprocess() in ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
453 endif()
454
455 endif()
456
457 endfunction()
458
459
460 # Modify EXE_BINARY_NAME based in passed-in args
461 #
462 # NOTE: This read the variables from the enclosing tribits_add_executable()
463 # function call scope.
464 #
465 function(tribits_add_executable_adjust_exe_name exeBinaryNameInOut)
466
467 set(exeBinaryName ${${exeBinaryNameInOut}})
468 if (PARSE_ADD_DIR_TO_NAME)
469 set(dirName "")
470 tribits_create_name_from_current_source_directory(dirName)
471 set(exeBinaryName ${dirName}_${exeBinaryName})
472 endif()
473
474 if (DEFINED PACKAGE_NAME AND NOT PARSE_NOEXEPREFIX)
475 set(exeBinaryName ${PACKAGE_NAME}_${exeBinaryName})
476 endif()
477 set(${exeBinaryNameInOut} ${exeBinaryName} PARENT_SCOPE)
478
479 endfunction()
480
481
482 # Check if to skip adding the executable based on different criteria
483 #
484 # NOTE: This read the variables from the enclosing tribits_add_executable()
485 # function call scope.
486 #
487 function(tribits_add_executable_is_skipped skipAddExecutableOut)
488
489 set(skipAddExecutable FALSE)
490
491 set(ADD_THE_TEST FALSE)
492 set(TEST_NAME ${EXE_NAME}) # For error message
493 tribits_add_test_process_categories(ADD_THE_TEST)
494 if (NOT ADD_THE_TEST)
495 set(skipAddExecutable TRUE)
496 endif()
497 set(TEST_NAME "")
498
499 set(ADD_THE_TEST FALSE)
500 tribits_add_test_process_host_hosttype(ADD_THE_TEST)
501 if (NOT ADD_THE_TEST)
502 set(skipAddExecutable TRUE)
503 endif()
504
505 tribits_process_comm_args(ADD_SERIAL_EXE ADD_MPI_EXE ${PARSE_COMM})
506 if (NOT ADD_SERIAL_EXE AND NOT ADD_MPI_EXE)
507 set(skipAddExecutable TRUE)
508 endif()
509
510 if (${EXE_BINARY_NAME}_EXE_DISABLE)
511 message("-- "
512 "${EXE_BINARY_NAME} EXE NOT being built due to ${EXE_BINARY_NAME}_EXE_DISABLE="
513 "'${${EXE_BINARY_NAME}_EXE_DISABLE}'")
514 set(skipAddExecutable TRUE)
515 endif()
516
517 set(${skipAddExecutableOut} ${skipAddExecutable} PARENT_SCOPE)
518
519 endfunction()
520
521
522 # Get adjusted list of source files
523 #
524 # NOTE: This read the variables from the enclosing tribits_add_executable()
525 # function call scope.
526 #
527 function(tribits_add_executable_get_adjusted_sources_list exeSourcesOut)
528
529 set(exeSources "")
530 if(PARSE_DIRECTORY )
531 foreach( srcFile ${PARSE_SOURCES} )
532 if(IS_ABSOLUTE ${srcFile})
533 list(APPEND exeSources "${srcFile}")
534 else()
535 list(APPEND exeSources "${PARSE_DIRECTORY}/${srcFile}")
536 endif()
537 endforeach( )
538 else()
539 foreach( srcFile ${PARSE_SOURCES} )
540 list(APPEND exeSources "${srcFile}")
541 endforeach( )
542 endif()
543
544 set(${exeSourcesOut} ${exeSources} PARENT_SCOPE)
545
546 endfunction()
547
548
549 # Assert tribits_add_executable() TESTONLYLIBS
550 #
551 # NOTE: This read the variables from the enclosing tribits_add_executable()
552 # function call scope.
553 #
554 function(tribits_add_executable_assert_testonlylibs)
555 # Assert that TESTONLYLIBS only contains TESTONLY libs!
556 foreach(testOnlyLib ${PARSE_TESTONLYLIBS})
557 set(prefixedTestOnlyLib "${${PROJECT_NAME}_LIBRARY_NAME_PREFIX}${testOnlyLib}")
558 tribits_lib_is_testonly(${prefixedTestOnlyLib} libIsTestOnlyLib)
559 if (NOT libIsTestOnlyLib)
560 message(FATAL_ERROR "ERROR: '${testOnlyLib}' in TESTONLYLIBS not a TESTONLY lib!"
561 " If this a regular library in this package or in an dependent upstream"
562 " package then TriBITS will link automatically to it. If you remove this and it"
563 " does not link, then you need to add a new package dependency to"
564 " this package's dependencies file"
565 " ${${PACKAGE_NAME}_SOURCE_DIR}/cmake/Dependencies.cmake")
566 elseif(PARSE_INSTALLABLE)
567 message(FATAL_ERROR "ERROR: TESTONLY lib '${testOnlyLib}' not allowed with"
568 " INSTALLABLE executable! An INSTALLABLE executable can only depend on"
569 " non-TESTONLY libraries! Otherwise, when shared libs are used, and"
570 " TESTONLY library would not be installed and the installed executable"
571 " would be unusable!" )
572 endif()
573 endforeach()
574 endfunction()
575
576
577 # Assert tribits_add_executable() IMPORTEDLIBS
578 #
579 # NOTE: This read the variables from the enclosing tribits_add_executable()
580 # function call scope.
581 #
582 function(tribits_add_executable_assert_importedlibs)
583
584 # Assert that IMPORTEDLIBS are not TESTONLY libs are not regular package
585 # libs!
586 foreach(importedLib ${PARSE_IMPORTEDLIBS})
587 set(prefixedImportedLib "${${PROJECT_NAME}_LIBRARY_NAME_PREFIX}${importedLib}")
588 tribits_lib_is_testonly(${prefixedImportedLib} importedLibIsTestOnlyLib)
589 if (libIsTestOnly)
590 message(FATAL_ERROR
591 "ERROR: Lib '${importedLib}' being passed through"
592 " IMPORTEDLIBS is not allowed to be a TESTONLY lib!"
593 " Use TESTONLYLIBS instead!" )
594 endif()
595 list(FIND ${PACKAGE_NAME}_LIBRARIES "${PACKAGE_NAME}::${prefixedImportedLib}"
596 foundPrefixedImportedLibInPkgLibs_idx)
597 if (NOT foundPrefixedImportedLibInPkgLibs_idx EQUAL -1)
598 message(FATAL_ERROR
599 "ERROR: Lib '${importedLib}' in IMPORTEDLIBS is in"
600 " this package and is *not* an external lib!"
601 " TriBITS takes care of linking against libs the current"
602 " package automatically. Please remove '${importedLib}' from IMPORTEDLIBS!")
603 elseif (TARGET ${prefixedImportedLib})
604 message(FATAL_ERROR
605 "ERROR: Lib '${importedLib}' being passed through"
606 " IMPORTEDLIBS is *not* an external library but instead is a library"
607 " defined in this CMake project!"
608 " TriBITS takes care of linking against libraries in dependent upstream"
609 " packages. If you want to link to a library in an upstream"
610 " package then add the package name for that library to the appropriate"
611 " list in this package's dependencies file"
612 " ${${PACKAGE_NAME}_SOURCE_DIR}/cmake/Dependencies.cmake")
613 endif()
614 endforeach()
615
616 endfunction()
617
618
619 # Convert from tribits_add_executable() DEPLIBS to IMPORTEDLIBS and TESTONLYLIBS
620 #
621 # NOTE: This is a macro as it updates local variables in the
622 # tribits_add_executable() scope!
623 #
624 macro(tribits_add_executable_convert_from_deplibs)
625
626 # Convert from old DEPLIBS to TESTONLYLIBS and IMPORTEDLIBS
627 foreach(depLib ${PARSE_DEPLIBS})
628 set(prefixedDepLib "${${PROJECT_NAME}_LIBRARY_NAME_PREFIX}${depLib}")
629 tribits_lib_is_testonly(${prefixedDepLib} depLibIsTestOnlyLib)
630 if (depLibIsTestOnlyLib)
631 tribits_deprecated("Passing TESTONLY lib '${depLib}' through DEPLIBS"
632 " is deprecated! Instead, please pass through TESTONLYLIBS instead!"
633 " DEPLIBS is deprecated!")
634 list(APPEND PARSE_TESTONLYLIBS ${depLib})
635 elseif (TARGET ${prefixedDepLib})
636 tribits_deprecated("Passing non-TESTONLY lib '${depLib}' through DEPLIBS"
637 " is deprecated! The library '${depLib}' appears to be a"
638 " library defined in this CMake project."
639 " TriBITS takes care of linking against libraries in dependent upstream"
640 " packages. Therefore, please remove '${depLib}' from this list."
641 " If you want to link to a library from an upstream"
642 " package, then add the package name to the appropriate category"
643 " in this package's dependencies file: "
644 " ${${PACKAGE_NAME}_SOURCE_DIR}/cmake/Dependencies.cmake")
645 # ToDo: Convert the above 'WARNING' to 'SEND_ERROR'
646 else()
647 message(WARNING "WARNING: Passing external lib '${depLib}' through"
648 " DEPLIBS is deprecated! Instead, pass through IMPORTEDLIBS!"
649 " DEPLIBS is deprecated!"
650 " Please note that only external libs are allowed to be passed through"
651 " IMPORTEDLIBS.")
652 list(APPEND PARSE_IMPORTEDLIBS ${depLib})
653 endif()
654 endforeach()
655
656 endmacro()