Debian Codegraph @

cmake/tribits/core/package_arch/TribitsExternalPackageWriteConfigFile.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_guard()
12
13 include(TribitsGeneralMacros)
14 include(TribitsPackageDependencies)
15 include(TribitsWritePackageConfigFileHelpers)
16
17 include(MessageWrapper)
18
19
20 ################################################################################
21 #
22 # User-level functions
23 #
24 ################################################################################
25
26
27 # @FUNCTION: tribits_extpkg_write_config_file()
28 #
29 # Write out a ``<tplName>Config.cmake`` file for a TriBITS TPL given the list
30 # of include directories and libraries for an external package/TPL.
31 #
32 # Usage::
33 #
34 # tribits_write_external_package_config_file(
35 # <tplName> <tplConfigFile> )
36 #
37 # The arguments are:
38 #
39 # ``<tplName>``: Name of the external package/TPL
40 #
41 # ``<tplConfigFile>``: Full file path for the ``<tplName>Config.cmake``
42 # file that will be written out.
43 #
44 # This function just calls `tribits_extpkg_write_config_file_str()`_
45 # and writes that text to the file ``<tplConfigFile>`` so see that function
46 # for more details.
47 #
48 # NOTE: This is used for a classic TriBITS TPL that does **not** use
49 # ``find_package(<externalPkg>)`` with modern IMPORTED targets.
50 #
51 function(tribits_extpkg_write_config_file tplName tplConfigFile)
52 tribits_extpkg_write_config_file_str(${tplName} tplConfigFileStr)
53 file(WRITE "${tplConfigFile}" "${tplConfigFileStr}")
54 endfunction()
55
56
57 # @FUNCTION: tribits_extpkg_write_config_version_file()
58 #
59 # Write out a ``<tplName>ConfigVersion.cmake`` file.
60 #
61 # Usage::
62 #
63 # tribits_write_external_package_config_version_file(
64 # <tplName> <tplConfigVersionFile> )
65 #
66 # ToDo: Add version arguments!
67 #
68 # The arguments are:
69 #
70 # ``<tplName>``: Name of the external package/TPL
71 #
72 # ``<tplConfigVersionFile>``: Full file path for the
73 # ``<tplName>ConfigVersion.cmake`` file that will be written out.
74 #
75 function(tribits_extpkg_write_config_version_file tplName tplConfigVersionFile)
76 set(tplConfigVersionFileStr "")
77 string(APPEND tplConfigVersionFileStr
78 "# Package config file for external package/TPL '${tplName}'\n"
79 "#\n"
80 "# Generated by CMake, do not edit!\n"
81 "\n"
82 "if (TRIBITS_FINDING_RAW_${tplName}_PACKAGE_FIRST)\n"
83 " set(PACKAGE_VERSION_COMPATIBLE FALSE)\n"
84 " set(PACKAGE_VERSION_UNSUITABLE TRUE)\n"
85 "else()\n"
86 " set(PACKAGE_VERSION_COMPATIBLE TRUE)\n"
87 "endif()\n"
88 "\n"
89 "# Currently there is no version information\n"
90 "set(PACKAGE_VERSION UNKNOWN)\n"
91 "set(PACKAGE_VERSION_EXACT FALSE)\n"
92 )
93 file(WRITE "${tplConfigVersionFile}" "${tplConfigVersionFileStr}")
94 endfunction()
95
96
97 # @FUNCTION: tribits_extpkg_install_config_file()
98 #
99 # Install an already-generated ``<tplName>Config.cmake`` file.
100 #
101 # Usage::
102 #
103 # tribits_write_external_package_install_config_file(
104 # <tplName> <tplConfigFile> )
105 #
106 # The arguments are:
107 #
108 # ``<tplName>``: Name of the external package/TPL
109 #
110 # ``<tplConfigFile>``: Full file path for the ``<tplName>Config.cmake``
111 # file that will be installed into the correct location.
112 #
113 function(tribits_extpkg_install_config_file tplName tplConfigFile)
114 install(
115 FILES "${tplConfigFile}"
116 DESTINATION "${${PROJECT_NAME}_INSTALL_LIB_DIR}/external_packages/${tplName}"
117 )
118 endfunction()
119
120
121 # @FUNCTION: tribits_extpkg_install_config_version_file()
122 #
123 # Install an already-generated ``<tplName>ConfigVersion.cmake`` file.
124 #
125 # Usage::
126 #
127 # tribits_write_external_package_install_config_version_file(
128 # <tplName> <tplConfigVersionFile> )
129 #
130 # The arguments are:
131 #
132 # ``<tplName>``: Name of the external package/TPL
133 #
134 # ``<tplConfigVersionFile>``: Full file path for the
135 # ``<tplName>ConfigVersion.cmake`` file that will be installed into the
136 # correct location ``${${PROJECT_NAME}_INSTALL_LIB_DIR}/external_packages/``
137 #
138 function(tribits_extpkg_install_config_version_file tplName
139 tplConfigVersionFile
140 )
141 install(
142 FILES "${tplConfigVersionFile}"
143 DESTINATION "${${PROJECT_NAME}_INSTALL_LIB_DIR}/external_packages/${tplName}"
144 )
145 endfunction()
146
147
148 ################################################################################
149 #
150 # TriBITS Implementation functions
151 #
152 ################################################################################
153
154
155 # @FUNCTION: tribits_extpkgwit_create_package_config_file()
156 #
157 # Create the ``<tplName>Config.cmake`` file for a TriBITS external package/TPL
158 # that is defined by a set of IMPORTED targets by call to
159 # ``find_package(<externalPkg>)``
160 #
161 # Usage::
162 #
163 # tribits_extpkgwit_create_package_config_file( <tplName>
164 # INNER_FIND_PACKAGE_NAME <externalPkg>
165 # IMPORTED_TARGETS_FOR_ALL_LIBS <importedTarget0> <importedTarget1> ... )
166 #
167 function(tribits_extpkgwit_create_package_config_file tplName)
168 # Parse arguments
169 cmake_parse_arguments(
170 PARSE_ARGV 1
171 PARSE "" "" # prefix, options, one_value_keywords
172 "INNER_FIND_PACKAGE_NAME;IMPORTED_TARGETS_FOR_ALL_LIBS" #multi_value_keywords
173 )
174 tribits_check_for_unparsed_arguments(PARSE)
175 tribits_assert_parse_arg_one_value(PARSE INNER_FIND_PACKAGE_NAME)
176 tribits_assert_parse_arg_one_or_more_values(PARSE IMPORTED_TARGETS_FOR_ALL_LIBS)
177 set(externalPkg ${PARSE_INNER_FIND_PACKAGE_NAME})
178
179 # Create header for <tplName>Config.cmake file
180 set(configFileStr "")
181 tribits_extpkgwit_append_package_config_file_header_str(
182 ${tplName} ${externalPkg} configFileStr)
183
184 # Get ${externalPkg} from where you found it before (see note below)
185 tribits_extpkgwit_append_find_dependency_external_package_str(
186 ${tplName} ${externalPkg} configFileStr)
187
188 # Pull in upstream <UpstreamPkg>Config.cmake files
189 tribits_extpkg_append_find_upstream_dependencies_str(${tplName} configFileStr)
190
191 # Add the ${tplName}::all_libs target and link to this ${externalPkg}
192 # package's native IMPORTED targets
193 tribits_extpkg_append_create_all_libs_target_str( ${tplName}
194 LIB_TARGETS_LIST ${PARSE_IMPORTED_TARGETS_FOR_ALL_LIBS}
195 CONFIG_FILE_STR_INOUT configFileStr )
196
197 # Also link against upstream package's `<UpstreamPkg>::all_libs` targets
198 tribits_extpkg_append_target_link_libraries_to_upstream_all_libs_targets_str(${tplName}
199 ${tplName}::all_libs configFileStr)
200
201 tribits_extpkg_append_tribits_compliant_package_config_vars_str(${tplName}
202 configFileStr)
203
204 tribits_extpkg_write_package_config_file_from_str(${tplName} "${configFileStr}")
205 endfunction()
206
207
208 function(tribits_extpkgwit_append_find_dependency_external_package_str
209 tplName externalPkg configFileStrVarInOut
210 )
211 set(configFileStr "${${configFileStrVarInOut}}")
212 if (${externalPkg}_DIR)
213 string(APPEND configFileStr
214 "set(${externalPkg}_DIR \"${${externalPkg}_DIR}\")\n" )
215 endif()
216 string(APPEND configFileStr
217 "find_dependency(${externalPkg})\n\n")
218 set(${configFileStrVarInOut} "${configFileStr}" PARENT_SCOPE)
219 endfunction()
220 #
221 # NOTE: Above, ${externalPkg}_DIR is only set when
222 # find_package(${externalPkg}) finds a package configure file
223 # ${externalPkg}Config.cmake and **not** when it uses a
224 # Find${externalPkg}.cmake module. Therefore, there is no reason to set
225 # ${externalPkg}_DIR in this file if it will not be used.
226
227
228 function(tribits_extpkgwit_append_package_config_file_header_str
229 tplName externalPkg configFileStrVarInOut
230 )
231 set(configFileStr "${${configFileStrVarInOut}}")
232 string(APPEND configFileStr
233 "# TriBITS-compliant Package config file for external package/TPL '${tplName}'\n"
234 "# based on non TriBITS-compliant external package '${externalPkg}' that uses\n"
235 "# modern IMPORTED targets\n"
236 "#\n"
237 "# Generated by CMake, do not edit!\n"
238 "\n"
239 "# Guard against multiple inclusion\n"
240 "if (TARGET ${tplName}::all_libs)\n"
241 " return()\n"
242 "endif()\n"
243 "\n"
244 "include(CMakeFindDependencyMacro)\n\n"
245 )
246 set(${configFileStrVarInOut} "${configFileStr}" PARENT_SCOPE)
247 endfunction()
248
249
250 # @FUNCTION: tribits_extpkg_write_config_file_str()
251 #
252 # Create the text string for a ``<tplName>Config.cmake`` file given the list
253 # of include directories and libraries for an external package/TPL from the
254 # legacy TriBITS TPL specification.
255 #
256 # Usage::
257 #
258 # tribits_extpkg_write_config_file_str(
259 # <tplName> <tplConfigFileStrOut> )
260 #
261 # The function arguments are:
262 #
263 # ``<tplName>``: Name of the external package/TPL
264 #
265 # ``<tplConfigFileStrOut>``: Name of variable that will contain the string
266 # for the config file on output.
267 #
268 # This function reads from the (cache) variables
269 #
270 # * ``TPL_<tplName>_INCLUDE_DIRS``
271 # * ``TPL_<tplName>_LIBRARIES``
272 # * ``<tplName>_LIB_ENABLED_DEPENDENCIES``
273 #
274 # (which must already be set) and uses that information to produce the
275 # contents of the ``<tplName>Config.cmake`` which is returned as a string
276 # variable that contains IMPORTED targets to represent these libraries and
277 # include directories as well as ``find_dependency()`` calls for upstream
278 # packages listed in ``<tplName>_LIB_ENABLED_DEPENDENCIES``.
279 #
280 # The arguments in ``TPL_<tplName>_LIBRARIES`` are handled in special ways in
281 # order to create the namespaced IMPORTED targets
282 # ``tribits::<tplName>::<libname>`` and the ``<tplName>::all_libs`` target
283 # that depends on these.
284 #
285 # The types of arguments that are handled and how the are interpreted:
286 #
287 # ``<abs-base-path>/[lib]<libname>.<longest-ext>``
288 #
289 # Arguments that are absolute file paths are treated as libraries and an
290 # imported target name ``<libname>`` is derived from the file name (of the
291 # form ``lib<libname>.<longest-ext>`` removing beginning ``lib`` and file
292 # extension ``.<longest-ext>``). The IMPORTED target
293 # ``tribits::<tplName>::<libname>`` is created and the file path is set
294 # using the ``IMPORTED_LOCATION`` target property.
295 #
296 # ``-l<libname>``
297 #
298 # Arguments of the form ``-l<libname>`` are used to create IMPORTED
299 # targets with the name ``tribits::<tplName>::<libname>`` using the
300 # ``IMPORTED_LIBNAME`` target property.
301 #
302 # ``<libname>``
303 #
304 # Arguments that are a raw name that matches the regex
305 # ``^[a-zA-Z_][a-zA-Z0-9_-]*$`` are interpreted to be a library name
306 # ``<libname>`` and is used to create an IMPORTED targets
307 # ``<tplName>::<libname>`` using the ``IMPORTED_LIBNAME`` target property.
308 #
309 # ``-L<dir>``
310 #
311 # Link directories. These are pulled off and added to the
312 # ``<tplName>::all_libs`` using ``target_link_options()``. (The order of
313 # these options is maintained.)
314 #
315 # ``-<any-option>``
316 #
317 # Any other option that starts with ``-`` is assumed to
318 # be a link argument where the order does not matter in relation to the
319 # libraries (but the order of these extra options are maintained w.r.t. each
320 # other).
321 #
322 # ``<unrecognized>``
323 #
324 # Any other argument that does not match one of the above patterns is
325 # regarded as an error.
326 #
327 # For more details on the handling of individual ``TPL_<tplName>_LIBRARIES``
328 # arguments, see `tribits_extpkg_tpl_libraries_entry_type()`_.
329 #
330 # The list of directories given in ``TPL_<tplName>_INCLUDE_DIRS`` is added to
331 # the ``<tplName>::all_libs`` target using ``target_include_directories()``.
332 #
333 # Finally, for every ``<upstreamTplName>`` listed in
334 # ``<tplName>_LIB_ENABLED_DEPENDENCIES``, a link dependency is created using
335 # ``target_link_library(<tplName>::all_libs INTERFACE <upstreamTplName>)``.
336 #
337 # NOTE: The IMPORTED targets generated for each library argument
338 # ``<tplName>::<libname>`` are prefixed with ``tribits::`` to give
339 # ``tribits::<tplName>::<libname>``. This is to avoid clashing with IMPORTED
340 # targets ``<tplName>::<libname>`` from other package config files
341 # ``<tplName>Config.cmake`` or find modules ``Find<tplName>.cmake`` that may
342 # clash (see TriBITSPub/TriBITS#548). But the generated INTERFACE IMPORTED
343 # target ``<tplName>::all_libs`` is **not** namespaced with ``tribits::``
344 # since the ``all_libs`` target is unlikely to clash. The targets
345 # ``tribits::<tplName>::<libname>`` are not directly used in downstream
346 # ``target_link_library()`` calls so the names of these targets are really
347 # just an implementation detail. (The reason we give these a name based of
348 # the library name they represent ``<libname>`` is to make it more clear what
349 # the matching library is and to make the name unique.)
350 #
351 function(tribits_extpkg_write_config_file_str tplName tplConfigFileStrOut)
352
353 # A) Set up beginning of config file text
354 set(configFileStr "")
355 string(APPEND configFileStr
356 "# Package config file for external package/TPL '${tplName}'\n"
357 "#\n"
358 "# Generated by CMake, do not edit!\n"
359 "\n"
360 "# Guard against multiple inclusion\n"
361 "if (TARGET ${tplName}::all_libs)\n"
362 " return()\n"
363 "endif()\n"
364 "\n"
365 )
366
367 # B) Pull in upstream packages
368 tribits_extpkg_append_find_upstream_dependencies_str(${tplName}
369 configFileStr)
370
371 # C) Create IMPORTED library targets from TPL_${tplName}_LIBRARIES
372 tribits_extpkg_process_libraries_list(
373 ${tplName}
374 LIB_TARGETS_LIST_OUT libTargets
375 LIB_LINK_FLAGS_LIST_OUT libLinkFlags
376 CONFIG_FILE_STR_INOUT configFileStr
377 )
378
379 # D) Create the <tplName>::all_libs target
380 tribits_extpkg_append_create_all_libs_target_str(
381 ${tplName}
382 LIB_TARGETS_LIST ${libTargets}
383 LIB_LINK_FLAGS_LIST ${libLinkFlags}
384 CONFIG_FILE_STR_INOUT configFileStr
385 )
386
387 # E) Add standard TriBITS-compliant external package vars
388 tribits_extpkg_append_tribits_compliant_package_config_vars_str(${tplName} configFileStr)
389
390 # F) Set the output
391 set(${tplConfigFileStrOut} "${configFileStr}" PARENT_SCOPE)
392
393 endfunction()
394
395
396 # @FUNCTION: tribits_extpkg_append_find_upstream_dependencies_str()
397 #
398 # Add includes for all upstream external packages/TPLs listed in
399 # ``<tplName>_LIB_ENABLED_DEPENDENCIES``.
400 #
401 # Usage::
402 #
403 # tribits_extpkg_append_find_upstream_dependencies_str(tplName
404 # configFileFragStrInOut)
405 #
406 # NOTE: This also requires that
407 # ``<upstreamTplName>_TRIBITS_COMPLIANT_PACKAGE_CONFIG_FILE`` be set for each
408 # external package/TPL listed in ``<tplName>_LIB_ENABLED_DEPENDENCIES``.
409 #
410 function(tribits_extpkg_append_find_upstream_dependencies_str
411 tplName configFileFragStrInOut
412 )
413 if (NOT "${${tplName}_LIB_ENABLED_DEPENDENCIES}" STREQUAL "")
414 set(configFileFragStr "${${configFileFragStrInOut}}")
415 foreach (upstreamTplDepEntry IN LISTS ${tplName}_LIB_ENABLED_DEPENDENCIES)
416 tribits_extpkg_get_dep_name_and_vis(
417 "${upstreamTplDepEntry}" upstreamTplDepName upstreamTplDepVis)
418 if (NOT "${${upstreamTplDepName}_TRIBITS_COMPLIANT_PACKAGE_CONFIG_FILE_DIR}"
419 STREQUAL ""
420 )
421 set(upstreamTplPackageConfigFileDir
422 "${${upstreamTplDepName}_TRIBITS_COMPLIANT_PACKAGE_CONFIG_FILE_DIR}")
423 else()
424 set(upstreamTplPackageConfigFileDir
425 "\${CMAKE_CURRENT_LIST_DIR}/../${upstreamTplDepName}")
426 endif()
427 string(APPEND configFileFragStr
428 "if (NOT TARGET ${upstreamTplDepName}::all_libs)\n"
429 " include(\"${upstreamTplPackageConfigFileDir}/${upstreamTplDepName}Config.cmake\")\n"
430 "endif()\n"
431 "\n"
432 )
433 endforeach()
434 set(${configFileFragStrInOut} "${configFileFragStr}" PARENT_SCOPE)
435 endif()
436 endfunction()
437 #
438 # NOTE: Above, we include the upstream ${upstreamTplDepName}Config.cmake file
439 # from either beside the current location (in the build tree or the install
440 # tree) or we use the location set in
441 # ${upstreamTplDepName}_TRIBITS_COMPLIANT_PACKAGE_CONFIG_FILE_DIR which would
442 # have been set by the ${upstreamTplDepName}Config.cmake file itself from an
443 # upstream install as pulled in from a TriBITS-compliant external package.
444
445
446 # @FUNCTION: tribits_extpkg_process_libraries_list()
447 #
448 # Read the ``TPL_<tplName>_LIBRARIES`` and
449 # ``<tplName>_LIB_ENABLED_DEPENDENCIES`` list variables and produce the string
450 # for the IMPORTED targets commands with upstream linkages and return list of
451 # targets and left over linker flags.
452 #
453 # Usage::
454 #
455 # tribits_extpkg_process_libraries_list(
456 # <tplName>
457 # LIB_TARGETS_LIST_OUT <libTargetsListOut>
458 # LIB_LINK_FLAGS_LIST_OUT <libLinkFlagsListOut>
459 # CONFIG_FILE_STR_INOUT <configFileFragStrInOut>
460 # )
461 #
462 # The arguments are:
463 #
464 # ``<tplName>``: [In] Name of the external package/TPL
465 #
466 # ``<libTargetsListOut>``: [Out] Name of list variable that will be set with
467 # the list of IMPORTED library targets generated from this list.
468 #
469 # ``<libLinkFlagsListOut>``: [Out] Name of list variable that will be set
470 # with the list of ``-L<dir>`` library directory paths.
471 #
472 # ``<configFileFragStrInOut>``: [Inout] A string variable that will be
473 # appended with the IMPORTED library commands for the list of targets given
474 # in ``<libTargetsList>``.
475 #
476 function(tribits_extpkg_process_libraries_list tplName)
477
478 # A) Parse commandline arguments
479
480 cmake_parse_arguments(
481 PARSE #prefix
482 "" #options
483 "LIB_TARGETS_LIST_OUT;LIB_LINK_FLAGS_LIST_OUT;CONFIG_FILE_STR_INOUT" #one_value_keywords
484 "" #multi_value_keywords
485 ${ARGN}
486 )
487 tribits_check_for_unparsed_arguments()
488
489 # Capture the initial input string in case the name of the var
490 # 'configFileStr' is the same in the parent scope.
491 set(configFileStrInit "${${PARSE_CONFIG_FILE_STR_INOUT}}")
492
493 # B) Create IMPORTED library targets from TPL_${tplName}_LIBRARIES
494
495 set(configFileStr "")
496 set(libTargets "")
497 set(previousLibProcessed "")
498
499 # Iterate through libs in reverse order setting dependencies on the libs
500 # that came before them so CMake will put in right order on the link line.
501
502 set(libLinkFlagsList "") # Will be filled in reverse order below
503
504 set(reverseLibraries ${TPL_${tplName}_LIBRARIES})
505 list(REVERSE reverseLibraries)
506
507 foreach (libentry IN LISTS reverseLibraries)
508 tribits_extpkg_tpl_libraries_entry_type(${libentry} libEntryType)
509 if (libEntryType STREQUAL "UNSUPPORTED_LIB_ENTRY")
510 message_wrapper(SEND_ERROR
511 "ERROR: Can't handle argument '${libentry}' in list TPL_${tplName}_LIBRARIES")
512 elseif (libEntryType STREQUAL "LIB_DIR_LINK_OPTION")
513 list(APPEND libLinkFlagsList "${libentry}")
514 elseif (libEntryType STREQUAL "GENERAL_LINK_OPTION")
515 message_wrapper("-- NOTE: Moving the general link argument '${libentry}' in TPL_${tplName}_LIBRARIES forward on the link line which may change the link and break the link!")
516 list(APPEND libLinkFlagsList "${libentry}")
517 else()
518 tribits_extpkg_process_libraries_list_library_entry(
519 ${tplName} "${libentry}" ${libEntryType} libTargets previousLibProcessed
520 configFileStr )
521 endif()
522 endforeach()
523
524 list(REVERSE libLinkFlagsList) # Put back in original order
525
526 # C) Set output arguments:
527 set(${PARSE_LIB_TARGETS_LIST_OUT} "${libTargets}" PARENT_SCOPE)
528 set(${PARSE_LIB_LINK_FLAGS_LIST_OUT} "${libLinkFlagsList}" PARENT_SCOPE)
529 set(${PARSE_CONFIG_FILE_STR_INOUT} "${configFileStrInit}${configFileStr}"
530 PARENT_SCOPE)
531
532 endfunction()
533
534
535 # @FUNCTION: tribits_extpkg_tpl_libraries_entry_type()
536 #
537 # Returns the type of the library entry in the list TPL_<tplName>_LIBRARIES
538 #
539 # Usage::
540 #
541 # tribits_extpkg_tpl_libraries_entry_type(<libentry> <libEntryTypeOut>)
542 #
543 # Arguments:
544 #
545 # ``<libentry>`` [in]: Element of ``TPL_<tplName>_LIBRARIES``
546 #
547 # ``<libEntryTypeOut>`` [out]: Variable set on output to the type of entry.
548 #
549 # The types of entries set on ``libEntryTypeOut`` include:
550 #
551 # * ``FULL_LIB_PATH``: A full library path
552 #
553 # * ``LIB_NAME_LINK_OPTION``: A library name link option of the form
554 # ``-l<libname>``
555 #
556 # * ``LIB_NAME``: A library name of the form ``<libname>``
557 #
558 # * ``LIB_DIR_LINK_OPTION``: A library directory search option of the form
559 # ``-L<dir>``
560 #
561 # * ``GENERAL_LINK_OPTION``: Some other general link option that starts with
562 # ``-`` but is not ``-l`` or ``-L``.
563 #
564 # * ``UNSUPPORTED_LIB_ENTRY``: An unsupported lib option
565 #
566 function(tribits_extpkg_tpl_libraries_entry_type libentry libEntryTypeOut)
567 string(SUBSTRING "${libentry}" 0 1 firstCharLibEntry)
568 string(SUBSTRING "${libentry}" 0 2 firstTwoCharsLibEntry)
569 if (firstTwoCharsLibEntry STREQUAL "-l")
570 set(libEntryType LIB_NAME_LINK_OPTION)
571 elseif (firstTwoCharsLibEntry STREQUAL "-L")
572 set(libEntryType LIB_DIR_LINK_OPTION)
573 elseif (firstCharLibEntry STREQUAL "-")
574 set(libEntryType GENERAL_LINK_OPTION)
575 elseif (IS_ABSOLUTE "${libentry}")
576 set(libEntryType FULL_LIB_PATH)
577 elseif (libentry MATCHES "^[a-zA-Z_][a-zA-Z0-9_-]*$")
578 set(libEntryType LIB_NAME)
579 else()
580 set(libEntryType UNSUPPORTED_LIB_ENTRY)
581 endif()
582 set(${libEntryTypeOut} ${libEntryType} PARENT_SCOPE)
583 endfunction()
584 # NOTE: Above, if libentry is only 1 char long, then firstTwoCharsLibEntry is
585 # also 1 char long and the above logic still works.
586
587
588 # Function to process a library inside of loop over
589 # ``TPL_<tplName>_LIBRARIES`` in the function
590 # tribits_extpkg_process_libraries_list().
591 #
592 # This also puts in linkages to upstream TPLs ``<tplName>::all_libs`` listed
593 # in ``<tplName>_LIB_ENABLED_DEPENDENCIES``.
594 #
595 function(tribits_extpkg_process_libraries_list_library_entry
596 tplName libentry libEntryType
597 libTargetsInOut previousLibProcessedInOut configFileStrInOut
598 )
599 # Set local vars for inout vars
600 set(libTargets ${${libTargetsInOut}})
601 set(previousLibProcessed ${${previousLibProcessedInOut}})
602 set(configFileStr ${${configFileStrInOut}})
603 # Get libname
604 tribits_extpkg_get_libname_and_path_from_libentry(
605 "${libentry}" ${libEntryType} libname libpath)
606 # Create IMPORTED library target
607 set(prefixed_libname "tribits::${tplName}::${libname}")
608 if (NOT (prefixed_libname IN_LIST libTargets))
609 tribits_extpkg_append_add_library_str (${libname} ${prefixed_libname}
610 ${libEntryType} "${libpath}" configFileStr)
611 if (previousLibProcessed)
612 # This is not the first lib so we only need to link to the previous lib
613 string(APPEND configFileStr
614 "target_link_libraries(${prefixed_libname}\n"
615 " INTERFACE tribits::${tplName}::${previousLibProcessed})\n"
616 )
617 else()
618 # Only on the first lib do we add dependencies on all of the
619 # `<UpstreamPkg>::all_libs` targets
620 tribits_extpkg_append_target_link_libraries_to_upstream_all_libs_targets_str( ${tplName}
621 ${prefixed_libname} configFileStr )
622 endif()
623 string(APPEND configFileStr
624 "\n")
625 # Update for next loop
626 set(previousLibProcessed ${libname})
627 list(APPEND libTargets ${prefixed_libname})
628 endif()
629 # Set output vars
630 set(${libTargetsInOut} ${libTargets} PARENT_SCOPE)
631 set(${previousLibProcessedInOut} ${previousLibProcessed} PARENT_SCOPE)
632 set(${configFileStrInOut} ${configFileStr} PARENT_SCOPE)
633 endfunction()
634 # NOTE: Above, we only need to link the first library
635 # tribits::<tplName>::<libname0> against the upstream TPL libraries
636 # <upstreamTpl>::all_libs. The other imported targets
637 # tribits::<tplName>::<libnamei> for this TPL are linked to this first
638 # tribits::<tplName>::<libname0> which has the needed dependencies.
639
640
641 function(tribits_extpkg_get_libname_and_path_from_libentry
642 libentry libEntryType libnameOut libpathOut
643 )
644 if (libEntryType STREQUAL "FULL_LIB_PATH")
645 tribits_extpkg_get_libname_from_full_lib_path("${libentry}" libname)
646 set(libpath "${libentry}")
647 elseif (libEntryType STREQUAL "LIB_NAME_LINK_OPTION")
648 tribits_extpkg_get_libname_from_lib_name_link_option("${libentry}" libname)
649 set(libpath "")
650 elseif (libEntryType STREQUAL "LIB_NAME")
651 set(libname "${libentry}")
652 set(libpath "")
653 else()
654 message(FATAL_ERROR "Error libEntryType='${libEntryType}' not supported here!")
655 endif()
656 set(${libnameOut} ${libname} PARENT_SCOPE)
657 set(${libpathOut} ${libpath} PARENT_SCOPE)
658 endfunction()
659
660
661 function(tribits_extpkg_append_add_library_str
662 libname prefix_libname libEntryType libpath
663 configFileStrInOut
664 )
665 set(configFileStr "${${configFileStrInOut}}")
666 if (libEntryType STREQUAL "FULL_LIB_PATH")
667 get_filename_component(libExt "${libpath}" LAST_EXT)
668 if (libExt STREQUAL ".a")
669 set(libType STATIC)
670 else()
671 set(libType UNKNOWN)
672 endif()
673 string(APPEND configFileStr
674 "add_library(${prefixed_libname} IMPORTED ${libType})\n"
675 "set_target_properties(${prefixed_libname} PROPERTIES\n"
676 " IMPORTED_LOCATION \"${libpath}\")\n"
677 )
678 elseif (
679 (libEntryType STREQUAL "LIB_NAME_LINK_OPTION")
680 OR (libEntryType STREQUAL "LIB_NAME")
681 )
682 string(APPEND configFileStr
683 "add_library(${prefixed_libname} IMPORTED INTERFACE)\n"
684 "set_target_properties(${prefixed_libname} PROPERTIES\n"
685 " IMPORTED_LIBNAME \"${libname}\")\n"
686 )
687 else()
688 message(FATAL_ERROR "Error libEntryType='${libEntryType}' not supported here!")
689 endif()
690 set(${configFileStrInOut} "${configFileStr}" PARENT_SCOPE)
691 endfunction()
692
693
694 function(tribits_extpkg_get_libname_from_full_lib_path full_lib_path
695 libnameOut
696 )
697 # Should be an absolute library path
698 get_filename_component(full_libname "${full_lib_path}" NAME_WE)
699 # Begins with 'lib'?
700 tribits_extpkg_libname_begins_with_lib("${full_libname}" beginsWithLib)
701 # Assert is a valid lib name and get lib name
702 set(libname "")
703 string(LENGTH "${full_libname}" full_libname_len)
704 if (full_libname_len LESS 0)
705 tribits_extpkg_print_invalid_lib_name(${tplName} "${full_lib_path}")
706 endif()
707 if (WIN32)
708 # Native windows compilers does not prepend library names with 'lib'
709 set(libname "${full_libname}")
710 elseif (APPLE)
711 # On MacOSX, CMake allows using frameworks that *don't* begin with 'lib'
712 # so we have to allow for that
713 if (beginsWithLib)
714 string(SUBSTRING "${full_libname}" 3 -1 libname)
715 else()
716 # Must be a framework dir with extension .framework
717 get_filename_component(last_ext "${full_lib_path}" LAST_EXT)
718 if (last_ext STREQUAL ".framework")
719 set(libname "${full_libname}")
720 else()
721 tribits_extpkg_print_invalid_lib_name(${tplName} "${full_lib_path}")
722 endif()
723 endif()
724 else() # I.e. Linux
725 # Every other system (i.e. Linux) prepends the library name with 'lib' so
726 # assert for that
727 if (NOT beginsWithLib)
728 tribits_extpkg_print_invalid_lib_name(${tplName} "${full_lib_path}")
729 else()
730 string(SUBSTRING "${full_libname}" 3 -1 libname)
731 endif()
732 endif()
733 # Set output
734 set(${libnameOut} ${libname} PARENT_SCOPE)
735 endfunction()
736
737
738 function(tribits_extpkg_libname_begins_with_lib full_libname
739 libnameBeginsWithLibOut
740 )
741 string(SUBSTRING "${full_libname}" 0 3 libPart)
742 if (libPart STREQUAL "lib")
743 set(libnameBeginsWithLib TRUE)
744 else()
745 set(libnameBeginsWithLib FALSE)
746 endif()
747 set(${libnameBeginsWithLibOut} ${libnameBeginsWithLib} PARENT_SCOPE)
748 endfunction()
749
750
751 function(tribits_extpkg_get_libname_from_lib_name_link_option
752 lib_name_link_option libnameOut
753 )
754 # Assert begging part '-l'
755 string(SUBSTRING "${lib_name_link_option}" 0 2 firstTwoCharsLibEntry)
756 if ( )
757 tribits_extpkg_print_invalid_lib_link_option(${tplName} "${lib_name_link_option}")
758 endif()
759 # Get <libname> from -l<libname>
760 string(SUBSTRING "${lib_name_link_option}" 2 -1 libname)
761 # Set output
762 set(${libnameOut} ${libname} PARENT_SCOPE)
763 endfunction()
764
765
766 function(tribits_extpkg_print_invalid_lib_name tplName full_libname)
767 message_wrapper(SEND_ERROR
768 "ERROR: TPL_${tplName}_LIBRARIES entry '${full_libname}' not a valid lib file name!")
769 endfunction()
770
771
772 function(tribits_extpkg_print_invalid_lib_link_option tplName liblinkoption)
773 message(SEND_ERROR
774 "ERROR: TPL_${tplName}_LIBRARIES entry '${liblinkoption}' not a valid lib name link option!")
775 endfunction()
776
777
778 # @FUNCTION: tribits_extpkg_append_target_link_libraries_to_upstream_all_libs_targets_str()
779 #
780 # Append text calling `target_link_libraries(<prefix_libname> ... )` against
781 # the `<UpstreamPkg>::all_libs` targets for all of the direct enabled upstream
782 # dependencies listed in '<tplName>_LIB_ENABLED_DEPENDENCIES` (taking into
783 # account `PUBLIC` and `PRIVATE` dependencies).
784 #
785 function(tribits_extpkg_append_target_link_libraries_to_upstream_all_libs_targets_str
786 tplName prefix_libname configFileStrInOut
787 )
788 set(configFileStr "${${configFileStrInOut}}")
789 if (${tplName}_LIB_ENABLED_DEPENDENCIES)
790 string(APPEND configFileStr
791 "target_link_libraries(${prefix_libname}\n")
792 foreach (upstreamTplDepEntry IN LISTS ${tplName}_LIB_ENABLED_DEPENDENCIES)
793 tribits_extpkg_get_dep_name_and_vis(
794 "${upstreamTplDepEntry}" upstreamTplDepName upstreamTplDepVis)
795 if (upstreamTplDepVis STREQUAL "PUBLIC")
796 string(APPEND configFileStr
797 " INTERFACE ${upstreamTplDepName}::all_libs # i.e. PUBLIC\n")
798 elseif(upstreamTplDepVis STREQUAL "PRIVATE")
799 string(APPEND configFileStr
800 " INTERFACE $<LINK_ONLY:${upstreamTplDepName}::all_libs> # i.e. PRIVATE\n")
801 else()
802 message(FATAL_ERROR "ERROR: Invalid visibility in entry '${upstreamTplDepEntry}'")
803 endif()
804 endforeach()
805 string(APPEND configFileStr
806 " )\n")
807 endif()
808 set(${configFileStrInOut} "${configFileStr}" PARENT_SCOPE)
809 endfunction()
810 #
811 # NOTE: Above, the syntax for a private dependency is:
812 #
813 # INTERFACE $<LINK_ONLY:upstreamLib>
814 #
815 # This has the effect of not bringing along the INTERFACE_INCLUDE_DIRECTORIES
816 # for upstreamLib so the include dirs for upstreamLib will not be listed on
817 # the compile lines of downstream object builds. But it will result in the
818 # libraries being listed on link lines for downstsream library and exec links.
819
820
821 # @FUNCTION: tribits_extpkg_append_create_all_libs_target_str()
822 #
823 # Creates the ``<tplName>::all_libs`` target command text using input info and
824 # from ``TPL_<tplName>_INCLUDE_DIRS``.
825 #
826 # Usage::
827 #
828 # tribits_extpkg_append_create_all_libs_target_str(
829 # <tplName>
830 # LIB_TARGETS_LIST <libTargetsList>
831 # LIB_LINK_FLAGS_LIST <libLinkFlagsList>
832 # CONFIG_FILE_STR_INOUT <configFileFragStrInOut>
833 # )
834 #
835 # The arguments are:
836 #
837 # ``<tplName>``: [in] Name of the external package/TPL
838 #
839 # ``<libTargetsList>``: [in] List of targets created from processing
840 # ``TPL_<tplName>_LIBRARIES``.
841 #
842 # ``<libLinkFlagsList>``: [in] List of of ``-L<dir>`` library directory
843 # paths entries found while processing ``TPL_<tplName>_LIBRARIES``.
844 #
845 # ``<configFileFragStrInOut>``: [out] A string variable that will be
846 # appended with the ``<tplName>::all_libs`` target statements.
847 #
848 function(tribits_extpkg_append_create_all_libs_target_str tplName)
849
850 # Parse commandline arguments
851
852 cmake_parse_arguments(
853 PARSE #prefix
854 "" #options
855 "CONFIG_FILE_STR_INOUT" #one_value_keywords
856 "LIB_TARGETS_LIST;LIB_LINK_FLAGS_LIST" #multi_value_keywords
857 ${ARGN}
858 )
859 tribits_check_for_unparsed_arguments()
860
861 # Set short-hand local vars
862 set(libTargets "${PARSE_LIB_TARGETS_LIST}")
863 set(libLinkFlags "${PARSE_LIB_LINK_FLAGS_LIST}")
864
865 # Capture the initial input string in case the name of the var
866 # 'configFileStr' is the same in the parent scope.
867 set(configFileStrInit "${${PARSE_CONFIG_FILE_STR_INOUT}}")
868
869 set(configFileStr "")
870
871 # add_library()
872 string(APPEND configFileStr
873 "add_library(${tplName}::all_libs INTERFACE IMPORTED)\n")
874 # target_link_libraries()
875 if (libTargets)
876 string(APPEND configFileStr
877 "target_link_libraries(${tplName}::all_libs\n")
878 foreach (libTarget IN LISTS libTargets)
879 string(APPEND configFileStr
880 " INTERFACE ${libTarget}\n")
881 endforeach()
882 string(APPEND configFileStr
883 " )\n")
884 endif()
885 # target_include_directories()
886 if (TPL_${tplName}_INCLUDE_DIRS)
887 string(APPEND configFileStr
888 "target_include_directories(${tplName}::all_libs SYSTEM\n")
889 foreach (inclDir IN LISTS TPL_${tplName}_INCLUDE_DIRS)
890 string(APPEND configFileStr
891 " INTERFACE \"${inclDir}\"\n")
892 endforeach()
893 string(APPEND configFileStr
894 " )\n")
895 endif()
896 # target_link_options()
897 if (libLinkFlags)
898 string(APPEND configFileStr
899 "target_link_options(${tplName}::all_libs\n")
900 foreach (likLinkFlag IN LISTS libLinkFlags)
901 string(APPEND configFileStr
902 " INTERFACE \"${likLinkFlag}\"\n")
903 endforeach()
904 string(APPEND configFileStr
905 " )\n")
906 endif()
907 # Add trailing newline
908 string(APPEND configFileStr
909 "\n")
910
911 # C) Set output arguments
912 set(${PARSE_CONFIG_FILE_STR_INOUT} "${configFileStrInit}${configFileStr}"
913 PARENT_SCOPE)
914
915 endfunction()