Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmComputeLinkInformation.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmComputeLinkInformation.h"
4
5
#include <algorithm>
6
#include <sstream>
7
#include <utility>
8
9
#include <cm/memory>
10
#include <cm/optional>
11
#include <cm/string_view>
12
#include <cmext/algorithm>
13
#include <cmext/string_view>
14
15
#include "cmsys/String.h"
16
17
#include "cmComputeLinkDepends.h"
18
#include "cmGeneratorTarget.h"
19
#include "cmGlobalGenerator.h"
20
#include "cmLinkItem.h"
21
#include "cmList.h"
22
#include "cmListFileCache.h"
23
#include "cmLocalGenerator.h"
24
#include "cmMakefile.h"
25
#include "cmMessageType.h"
26
#include "cmOrderDirectories.h"
27
#include "cmPlaceholderExpander.h"
28
#include "cmSourceFile.h"
29
#include "cmState.h"
30
#include "cmStateTypes.h"
31
#include "cmStringAlgorithms.h"
32
#include "cmSystemTools.h"
33
#include "cmTarget.h"
34
#include "cmValue.h"
35
#include "cmXcFramework.h"
36
#include "cmake.h"
37
38
// #define CM_COMPUTE_LINK_INFO_DEBUG
39
40
/*
41
Notes about linking on various platforms:
42
43
------------------------------------------------------------------------------
44
45
Linux, FreeBSD, macOS, Sun, Windows:
46
47
Linking to libraries using the full path works fine.
48
49
------------------------------------------------------------------------------
50
51
On AIX, more work is needed.
52
53
  The "-bnoipath" option is needed.  From "man ld":
54
55
    Note: If you specify a shared object, or an archive file
56
    containing a shared object, with an absolute or relative path
57
    name, instead of with the -lName flag, the path name is
58
    included in the import file ID string in the loader section of
59
    the output file. You can override this behavior with the
60
    -bnoipath option.
61
62
      noipath
63
64
        For shared objects listed on the command-line, rather than
65
        specified with the -l flag, use a null path component when
66
        listing the shared object in the loader section of the
67
        output file. A null path component is always used for
68
        shared objects specified with the -l flag. This option
69
        does not affect the specification of a path component by
70
        using a line beginning with #! in an import file. The
71
        default is the ipath option.
72
73
  This prevents the full path specified on the compile line from being
74
  compiled directly into the binary.
75
76
  By default the linker places -L paths in the embedded runtime path.
77
  In order to implement CMake's RPATH interface correctly, we need the
78
  -blibpath:Path option.  From "man ld":
79
80
      libpath:Path
81
82
        Uses Path as the library path when writing the loader section
83
        of the output file. Path is neither checked for validity nor
84
        used when searching for libraries specified by the -l flag.
85
        Path overrides any library paths generated when the -L flag is
86
        used.
87
88
        If you do not specify any -L flags, or if you specify the
89
        nolibpath option, the default library path information is
90
        written in the loader section of the output file. The default
91
        library path information is the value of the LIBPATH
92
        environment variable if it is defined, and /usr/lib:/lib,
93
        otherwise.
94
95
  We can pass -Wl,-blibpath:/usr/lib:/lib always to avoid the -L stuff
96
  and not break when the user sets LIBPATH.  Then if we want to add an
97
  rpath we insert it into the option before /usr/lib.
98
99
------------------------------------------------------------------------------
100
101
On HP-UX, more work is needed.  There are differences between
102
versions.
103
104
ld: 92453-07 linker linker ld B.10.33 990520
105
106
  Linking with a full path works okay for static and shared libraries.
107
  The linker seems to always put the full path to where the library
108
  was found in the binary whether using a full path or -lfoo syntax.
109
  Transitive link dependencies work just fine due to the full paths.
110
111
  It has the "-l:libfoo.sl" option.  The +nodefaultrpath is accepted
112
  but not documented and does not seem to do anything.  There is no
113
  +forceload option.
114
115
ld: 92453-07 linker ld HP Itanium(R) B.12.41  IPF/IPF
116
117
  Linking with a full path works okay for static libraries.
118
119
  Linking with a full path works okay for shared libraries.  However
120
  dependent (transitive) libraries of those linked directly must be
121
  either found with an rpath stored in the direct dependencies or
122
  found in -L paths as if they were specified with "-l:libfoo.sl"
123
  (really "-l:<soname>").  The search matches that of the dynamic
124
  loader but only with -L paths.  In other words, if we have an
125
  executable that links to shared library bar which links to shared
126
  library foo, the link line for the exe must contain
127
128
    /dir/with/bar/libbar.sl -L/dir/with/foo
129
130
  It does not matter whether the exe wants to link to foo directly or
131
  whether /dir/with/foo/libfoo.sl is listed.  The -L path must still
132
  be present.  It should match the runtime path computed for the
133
  executable taking all directly and transitively linked libraries
134
  into account.
135
136
  The "+nodefaultrpath" option should be used to avoid getting -L
137
  paths in the rpath unless we add our own rpath with +b.  This means
138
  that skip-build-rpath should use this option.
139
140
  See documentation in "man ld", "man dld.so", and
141
  http://docs.hp.com/en/B2355-90968/creatingandusinglibraries.htm
142
143
    +[no]defaultrpath
144
      +defaultrpath is the default.  Include any paths that are
145
      specified with -L in the embedded path, unless you specify the
146
      +b option.  If you use +b, only the path list specified by +b is
147
      in the embedded path.
148
149
      The +nodefaultrpath option removes all library paths that were
150
      specified with the -L option from the embedded path.  The linker
151
      searches the library paths specified by the -L option at link
152
      time.  At run time, the only library paths searched are those
153
      specified by the environment variables LD_LIBRARY_PATH and
154
      SHLIB_PATH, library paths specified by the +b linker option, and
155
      finally the default library paths.
156
157
    +rpathfirst
158
      This option will cause the paths specified in RPATH (embedded
159
      path) to be used before the paths specified in LD_LIBRARY_PATH
160
      or SHLIB_PATH, in searching for shared libraries.  This changes
161
      the default search order of LD_LIBRARY_PATH, SHLIB_PATH, and
162
      RPATH (embedded path).
163
164
------------------------------------------------------------------------------
165
Notes about dependent (transitive) shared libraries:
166
167
On non-Windows systems shared libraries may have transitive
168
dependencies.  In order to support LINK_INTERFACE_LIBRARIES we must
169
support linking to a shared library without listing all the libraries
170
to which it links.  Some linkers want to be able to find the
171
transitive dependencies (dependent libraries) of shared libraries
172
listed on the command line.
173
174
  - On Windows, DLLs are not directly linked, and the import libraries
175
    have no transitive dependencies.
176
177
  - On Mac OS X 10.5 and above transitive dependencies are not needed.
178
179
  - On Mac OS X 10.4 and below we need to actually list the dependencies.
180
    Otherwise when using -isysroot for universal binaries it cannot
181
    find the dependent libraries.  Listing them on the command line
182
    tells the linker where to find them, but unfortunately also links
183
    the library.
184
185
  - On HP-UX, the linker wants to find the transitive dependencies of
186
    shared libraries in the -L paths even if the dependent libraries
187
    are given on the link line.
188
189
  - On AIX the transitive dependencies are not needed.
190
191
  - On SGI, the linker wants to find the transitive dependencies of
192
    shared libraries in the -L paths if they are not given on the link
193
    line.  Transitive linking can be disabled using the options
194
195
      -no_transitive_link -Wl,-no_transitive_link
196
197
    which disable it.  Both options must be given when invoking the
198
    linker through the compiler.
199
200
  - On Sun, the linker wants to find the transitive dependencies of
201
    shared libraries in the -L paths if they are not given on the link
202
    line.
203
204
  - On Linux, FreeBSD, and QNX:
205
206
    The linker wants to find the transitive dependencies of shared
207
    libraries in the "-rpath-link" paths option if they have not been
208
    given on the link line.  The option is like rpath but just for
209
    link time:
210
211
      -Wl,-rpath-link,"/path1:/path2"
212
213
For -rpath-link, we need a separate runtime path ordering pass
214
including just the dependent libraries that are not linked.
215
216
For -L paths on non-HP, we can do the same thing as with rpath-link
217
but put the results in -L paths.  The paths should be listed at the
218
end to avoid conflicting with user search paths (?).
219
220
For -L paths on HP, we should do a runtime path ordering pass with
221
all libraries, both linked and non-linked.  Even dependent
222
libraries that are also linked need to be listed in -L paths.
223
224
In our implementation we add all dependent libraries to the runtime
225
path computation.  Then the auto-generated RPATH will find everything.
226
227
------------------------------------------------------------------------------
228
Notes about shared libraries with not builtin soname:
229
230
Some UNIX shared libraries may be created with no builtin soname.  On
231
some platforms such libraries cannot be linked using the path to their
232
location because the linker will copy the path into the field used to
233
find the library at runtime.
234
235
  Apple:    ../libfoo.dylib  ==>  libfoo.dylib  # ok, uses install_name
236
  SGI:      ../libfoo.so     ==>  libfoo.so     # ok
237
  AIX:      ../libfoo.so     ==>  libfoo.so     # ok
238
  Linux:    ../libfoo.so     ==>  ../libfoo.so  # bad
239
  HP-UX:    ../libfoo.so     ==>  ../libfoo.so  # bad
240
  Sun:      ../libfoo.so     ==>  ../libfoo.so  # bad
241
  FreeBSD:  ../libfoo.so     ==>  ../libfoo.so  # bad
242
243
In order to link these libraries we need to use the old-style split
244
into -L.. and -lfoo options.  This should be fairly safe because most
245
problems with -lfoo options were related to selecting shared libraries
246
instead of static but in this case we want the shared lib.  Link
247
directory ordering needs to be done to make sure these shared
248
libraries are found first.  There should be very few restrictions
249
because this need be done only for shared libraries without soname-s.
250
251
*/
252
253
cmComputeLinkInformation::cmComputeLinkInformation(
254
  cmGeneratorTarget const* target, std::string const& config)
255
  // Store context information.
256
0
  : Target(target)
257
0
  , Makefile(target->Target->GetMakefile())
258
0
  , GlobalGenerator(target->GetLocalGenerator()->GetGlobalGenerator())
259
0
  , CMakeInstance(this->GlobalGenerator->GetCMakeInstance())
260
  // The configuration being linked.
261
0
  , Config(config)
262
0
{
263
  // Check whether to recognize OpenBSD-style library versioned names.
264
0
  this->IsOpenBSD = this->Makefile->GetState()->GetGlobalPropertyAsBool(
265
0
    "FIND_LIBRARY_USE_OPENBSD_VERSIONING");
266
267
  // Allocate internals.
268
0
  this->OrderLinkerSearchPath = cm::make_unique<cmOrderDirectories>(
269
0
    this->GlobalGenerator, target, "linker search path");
270
0
  this->OrderRuntimeSearchPath = cm::make_unique<cmOrderDirectories>(
271
0
    this->GlobalGenerator, target, "runtime search path");
272
273
  // Get the language used for linking this target.
274
0
  this->LinkLanguage = this->Target->GetLinkerLanguage(config);
275
0
  if (this->LinkLanguage.empty()) {
276
    // The Compute method will do nothing, so skip the rest of the
277
    // initialization.
278
0
    return;
279
0
  }
280
281
  // Check whether we should skip dependencies on shared library files.
282
0
  this->LinkDependsNoShared =
283
0
    this->Target->GetPropertyAsBool("LINK_DEPENDS_NO_SHARED");
284
285
  // On platforms without import libraries there may be a special flag
286
  // to use when creating a plugin (module) that obtains symbols from
287
  // the program that will load it.
288
0
  if (!this->Target->IsDLLPlatform() &&
289
0
      this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) {
290
0
    std::string loader_flag_var =
291
0
      cmStrCat("CMAKE_SHARED_MODULE_LOADER_", this->LinkLanguage, "_FLAG");
292
0
    this->LoaderFlag = this->Makefile->GetDefinition(loader_flag_var);
293
0
  }
294
295
  // Get options needed to link libraries.
296
0
  if (cmValue flag = this->Makefile->GetDefinition(
297
0
        cmStrCat("CMAKE_", this->LinkLanguage, "_LINK_LIBRARY_FLAG"))) {
298
0
    this->LibLinkFlag = *flag;
299
0
  } else {
300
0
    this->LibLinkFlag =
301
0
      this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
302
0
  }
303
0
  if (cmValue flag = this->Makefile->GetDefinition(
304
0
        cmStrCat("CMAKE_", this->LinkLanguage, "_LINK_LIBRARY_FILE_FLAG"))) {
305
0
    this->LibLinkFileFlag = *flag;
306
0
  } else {
307
0
    this->LibLinkFileFlag =
308
0
      this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FILE_FLAG");
309
0
  }
310
0
  if (cmValue suffix = this->Makefile->GetDefinition(
311
0
        cmStrCat("CMAKE_", this->LinkLanguage, "_LINK_LIBRARY_SUFFIX"))) {
312
0
    this->LibLinkSuffix = *suffix;
313
0
  } else {
314
0
    this->LibLinkSuffix =
315
0
      this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
316
0
  }
317
0
  if (cmValue flag = this->Makefile->GetDefinition(
318
0
        cmStrCat("CMAKE_", this->LinkLanguage, "_LINK_OBJECT_FILE_FLAG"))) {
319
0
    this->ObjLinkFileFlag = *flag;
320
0
  } else {
321
0
    this->ObjLinkFileFlag =
322
0
      this->Makefile->GetSafeDefinition("CMAKE_LINK_OBJECT_FILE_FLAG");
323
0
  }
324
325
  // Get options needed to specify RPATHs.
326
0
  this->RuntimeUseChrpath = false;
327
0
  if (this->Target->GetType() != cmStateEnums::STATIC_LIBRARY) {
328
0
    char const* tType = ((this->Target->GetType() == cmStateEnums::EXECUTABLE)
329
0
                           ? "EXECUTABLE"
330
0
                           : "SHARED_LIBRARY");
331
0
    std::string rtVar =
332
0
      cmStrCat("CMAKE_", tType, "_RUNTIME_", this->LinkLanguage, "_FLAG");
333
0
    std::string rtSepVar = cmStrCat(rtVar, "_SEP");
334
0
    this->RuntimeFlag = this->Makefile->GetSafeDefinition(rtVar);
335
0
    this->RuntimeSep = this->Makefile->GetSafeDefinition(rtSepVar);
336
0
    this->RuntimeAlways = (this->Makefile->GetSafeDefinition(
337
0
      "CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH"));
338
339
0
    this->RuntimeUseChrpath = this->Target->IsChrpathUsed(config);
340
341
    // Get options needed to help find dependent libraries.
342
0
    std::string rlVar =
343
0
      cmStrCat("CMAKE_", tType, "_RPATH_LINK_", this->LinkLanguage, "_FLAG");
344
0
    this->RPathLinkFlag = this->Makefile->GetSafeDefinition(rlVar);
345
0
  }
346
347
  // Check if we need to include the runtime search path at link time.
348
0
  {
349
0
    std::string var = cmStrCat("CMAKE_SHARED_LIBRARY_LINK_",
350
0
                               this->LinkLanguage, "_WITH_RUNTIME_PATH");
351
0
    this->LinkWithRuntimePath = this->Makefile->IsOn(var);
352
0
  }
353
354
  // Define some Feature descriptors to handle standard library and object link
355
0
  if (!this->GetLibLinkFileFlag().empty()) {
356
0
    this->LibraryFeatureDescriptors.emplace(
357
0
      "__CMAKE_LINK_LIBRARY",
358
0
      LibraryFeatureDescriptor{
359
0
        "__CMAKE_LINK_LIBRARY",
360
0
        cmStrCat(this->GetLibLinkFileFlag(), "<LIBRARY>") });
361
0
  }
362
0
  if (!this->GetObjLinkFileFlag().empty()) {
363
0
    this->LibraryFeatureDescriptors.emplace(
364
0
      "__CMAKE_LINK_OBJECT",
365
0
      LibraryFeatureDescriptor{
366
0
        "__CMAKE_LINK_OBJECT",
367
0
        cmStrCat(this->GetObjLinkFileFlag(), "<LIBRARY>") });
368
0
  }
369
0
  if (!this->LoaderFlag->empty()) {
370
    // Define a Feature descriptor for the link of an executable with exports
371
0
    this->LibraryFeatureDescriptors.emplace(
372
0
      "__CMAKE_LINK_EXECUTABLE",
373
0
      LibraryFeatureDescriptor{ "__CMAKE_LINK_EXECUTABLE",
374
0
                                cmStrCat(*this->LoaderFlag, "<LIBRARY>") });
375
0
  }
376
  // To link framework using a full path
377
0
  this->LibraryFeatureDescriptors.emplace(
378
0
    "__CMAKE_LINK_FRAMEWORK",
379
0
    LibraryFeatureDescriptor{ "__CMAKE_LINK_FRAMEWORK", "<LIBRARY>" });
380
  // To link xcframework using a full path
381
0
  this->LibraryFeatureDescriptors.emplace(
382
0
    "__CMAKE_LINK_XCFRAMEWORK",
383
0
    LibraryFeatureDescriptor{ "__CMAKE_LINK_XCFRAMEWORK", "<LIBRARY>" });
384
385
  // Check the platform policy for missing soname case.
386
0
  this->NoSONameUsesPath =
387
0
    this->Makefile->IsOn("CMAKE_PLATFORM_USES_PATH_WHEN_NO_SONAME");
388
389
  // Get link type information.
390
0
  this->ComputeLinkTypeInfo();
391
392
  // Setup the link item parser.
393
0
  this->ComputeItemParserInfo();
394
395
  // Setup framework support.
396
0
  this->ComputeFrameworkInfo();
397
398
  // Choose a mode for dealing with shared library dependencies.
399
0
  this->SharedDependencyMode = SharedDepModeNone;
400
0
  if (this->Makefile->IsOn("CMAKE_LINK_DEPENDENT_LIBRARY_FILES")) {
401
0
    this->SharedDependencyMode = SharedDepModeLink;
402
0
  } else if (this->Makefile->IsOn("CMAKE_LINK_DEPENDENT_LIBRARY_DIRS")) {
403
0
    this->SharedDependencyMode = SharedDepModeLibDir;
404
0
  } else if (!this->RPathLinkFlag.empty()) {
405
0
    this->SharedDependencyMode = SharedDepModeDir;
406
0
    this->OrderDependentRPath = cm::make_unique<cmOrderDirectories>(
407
0
      this->GlobalGenerator, target, "dependent library path");
408
0
  }
409
410
  // Add the search path entries requested by the user to path ordering.
411
0
  std::vector<std::string> directories;
412
0
  this->Target->GetLinkDirectories(directories, config, this->LinkLanguage);
413
0
  this->OrderLinkerSearchPath->AddUserDirectories(directories);
414
0
  this->OrderRuntimeSearchPath->AddUserDirectories(directories);
415
416
  // Set up the implicit link directories.
417
0
  this->LoadImplicitLinkInfo();
418
0
  this->OrderLinkerSearchPath->SetImplicitDirectories(this->ImplicitLinkDirs);
419
0
  this->OrderRuntimeSearchPath->SetImplicitDirectories(this->ImplicitLinkDirs);
420
0
  if (this->OrderDependentRPath) {
421
0
    this->OrderDependentRPath->SetImplicitDirectories(this->ImplicitLinkDirs);
422
0
    this->OrderDependentRPath->AddLanguageDirectories(this->RuntimeLinkDirs);
423
0
  }
424
0
}
425
426
0
cmComputeLinkInformation::~cmComputeLinkInformation() = default;
427
428
namespace {
429
std::string const& DEFAULT = cmComputeLinkDepends::LinkEntry::DEFAULT;
430
}
431
432
void cmComputeLinkInformation::AppendValues(
433
  std::string& result, std::vector<BT<std::string>>& values)
434
0
{
435
0
  for (BT<std::string>& p : values) {
436
0
    if (result.empty()) {
437
0
      result.append(" ");
438
0
    }
439
440
0
    result.append(p.Value);
441
0
  }
442
0
}
443
444
cmComputeLinkInformation::ItemVector const&
445
cmComputeLinkInformation::GetItems() const
446
0
{
447
0
  return this->Items;
448
0
}
449
450
std::vector<std::string> const& cmComputeLinkInformation::GetDirectories()
451
  const
452
0
{
453
0
  return this->OrderLinkerSearchPath->GetOrderedDirectories();
454
0
}
455
456
std::vector<BT<std::string>>
457
cmComputeLinkInformation::GetDirectoriesWithBacktraces()
458
0
{
459
0
  std::vector<BT<std::string>> directoriesWithBacktraces;
460
461
0
  std::vector<BT<std::string>> targetLinkDirectories =
462
0
    this->Target->GetLinkDirectories(this->Config, this->LinkLanguage);
463
464
0
  std::vector<std::string> const& orderedDirectories = this->GetDirectories();
465
0
  for (std::string const& dir : orderedDirectories) {
466
0
    auto result = std::find(targetLinkDirectories.begin(),
467
0
                            targetLinkDirectories.end(), dir);
468
0
    if (result != targetLinkDirectories.end()) {
469
0
      directoriesWithBacktraces.emplace_back(std::move(*result));
470
0
    } else {
471
0
      directoriesWithBacktraces.emplace_back(dir);
472
0
    }
473
0
  }
474
475
0
  return directoriesWithBacktraces;
476
0
}
477
478
std::string cmComputeLinkInformation::GetRPathLinkString() const
479
0
{
480
  // If there is no separate linker runtime search flag (-rpath-link)
481
  // there is no reason to compute a string.
482
0
  if (!this->OrderDependentRPath) {
483
0
    return "";
484
0
  }
485
486
  // Construct the linker runtime search path. These MUST NOT contain tokens
487
  // such as $ORIGIN, see https://sourceware.org/bugzilla/show_bug.cgi?id=16936
488
0
  return cmJoin(this->OrderDependentRPath->GetOrderedDirectories(), ":");
489
0
}
490
491
std::vector<std::string> const& cmComputeLinkInformation::GetDepends() const
492
0
{
493
0
  return this->Depends;
494
0
}
495
496
std::vector<std::string> const& cmComputeLinkInformation::GetFrameworkPaths()
497
  const
498
0
{
499
0
  return this->FrameworkPaths;
500
0
}
501
502
std::set<std::string> const&
503
cmComputeLinkInformation::GetFrameworkPathsEmitted() const
504
0
{
505
0
  return this->FrameworkPathsEmitted;
506
0
}
507
508
std::vector<std::string> const&
509
cmComputeLinkInformation::GetXcFrameworkHeaderPaths() const
510
0
{
511
0
  return this->XcFrameworkHeaderPaths;
512
0
}
513
514
std::set<cmGeneratorTarget const*> const&
515
cmComputeLinkInformation::GetSharedLibrariesLinked() const
516
0
{
517
0
  return this->SharedLibrariesLinked;
518
0
}
519
520
std::vector<cmGeneratorTarget const*> const&
521
cmComputeLinkInformation::GetExternalObjectTargets() const
522
0
{
523
0
  return this->ExternalObjectTargets;
524
0
}
525
526
bool cmComputeLinkInformation::Compute()
527
0
{
528
  // Skip targets that do not link or have link-like information consumers may
529
  // need (namely modules).
530
0
  if (!(this->Target->GetType() == cmStateEnums::EXECUTABLE ||
531
0
        this->Target->GetType() == cmStateEnums::SHARED_LIBRARY ||
532
0
        this->Target->GetType() == cmStateEnums::MODULE_LIBRARY ||
533
0
        this->Target->GetType() == cmStateEnums::STATIC_LIBRARY ||
534
0
        (this->Target->CanCompileSources() &&
535
0
         (this->Target->HaveCxxModuleSupport(this->Config) ==
536
0
            cmGeneratorTarget::Cxx20SupportLevel::Supported ||
537
0
          this->Target->HaveFortranSources())))) {
538
0
    return false;
539
0
  }
540
541
  // We require a link language for the target.
542
0
  if (this->LinkLanguage.empty()) {
543
0
    cmSystemTools::Error(
544
0
      "CMake can not determine linker language for target: " +
545
0
      this->Target->GetName());
546
0
    return false;
547
0
  }
548
549
0
  LinkLibrariesStrategy strategy = LinkLibrariesStrategy::REORDER_MINIMALLY;
550
0
  if (cmValue s = this->Target->GetProperty("LINK_LIBRARIES_STRATEGY")) {
551
0
    if (*s == "REORDER_MINIMALLY"_s) {
552
0
      strategy = LinkLibrariesStrategy::REORDER_MINIMALLY;
553
0
    } else if (*s == "REORDER_FREELY"_s) {
554
0
      strategy = LinkLibrariesStrategy::REORDER_FREELY;
555
0
    } else {
556
0
      this->CMakeInstance->IssueMessage(
557
0
        MessageType::FATAL_ERROR,
558
0
        cmStrCat("LINK_LIBRARIES_STRATEGY value '", *s,
559
0
                 "' is not recognized."),
560
0
        this->Target->GetBacktrace());
561
0
      return false;
562
0
    }
563
0
  }
564
565
  // Compute the ordered link line items.
566
0
  cmComputeLinkDepends cld(this->Target, this->Config, this->LinkLanguage,
567
0
                           strategy);
568
0
  cmComputeLinkDepends::EntryVector const& linkEntries = cld.Compute();
569
0
  FeatureDescriptor const* currentFeature = nullptr;
570
571
  // Add the link line items.
572
0
  for (cmComputeLinkDepends::LinkEntry const& linkEntry : linkEntries) {
573
0
    if (linkEntry.Kind == cmComputeLinkDepends::LinkEntry::Group) {
574
0
      auto const& groupFeature = this->GetGroupFeature(linkEntry.Feature);
575
0
      if (groupFeature.Supported) {
576
0
        if (linkEntry.Item.Value == "</LINK_GROUP>" && currentFeature) {
577
          // emit feature suffix, if any
578
0
          if (!currentFeature->Suffix.empty()) {
579
0
            this->Items.emplace_back(
580
0
              BT<std::string>{ currentFeature->Suffix,
581
0
                               this->Items.back().Value.Backtrace },
582
0
              ItemIsPath::No);
583
0
          }
584
0
          currentFeature = nullptr;
585
0
        }
586
0
        this->Items.emplace_back(
587
0
          BT<std::string>{ linkEntry.Item.Value == "<LINK_GROUP>"
588
0
                             ? groupFeature.Prefix
589
0
                             : groupFeature.Suffix,
590
0
                           linkEntry.Item.Backtrace },
591
0
          ItemIsPath::No);
592
0
      }
593
0
      continue;
594
0
    }
595
596
0
    if (currentFeature && linkEntry.Feature != currentFeature->Name) {
597
      // emit feature suffix, if any
598
0
      if (!currentFeature->Suffix.empty()) {
599
0
        this->Items.emplace_back(
600
0
          BT<std::string>{ currentFeature->Suffix,
601
0
                           this->Items.back().Value.Backtrace },
602
0
          ItemIsPath::No);
603
0
      }
604
0
      currentFeature = nullptr;
605
0
    }
606
607
0
    if (linkEntry.Feature != DEFAULT &&
608
0
        (!currentFeature || linkEntry.Feature != currentFeature->Name)) {
609
0
      if (!this->AddLibraryFeature(linkEntry.Feature)) {
610
0
        continue;
611
0
      }
612
0
      currentFeature = this->FindLibraryFeature(linkEntry.Feature);
613
      // emit feature prefix, if any
614
0
      if (!currentFeature->Prefix.empty()) {
615
0
        this->Items.emplace_back(
616
0
          BT<std::string>{ currentFeature->Prefix, linkEntry.Item.Backtrace },
617
0
          ItemIsPath::No);
618
0
      }
619
0
    }
620
621
0
    if (linkEntry.Kind == cmComputeLinkDepends::LinkEntry::SharedDep) {
622
0
      this->AddSharedDepItem(linkEntry);
623
0
    } else {
624
0
      this->AddItem(linkEntry);
625
0
    }
626
0
  }
627
628
0
  if (currentFeature) {
629
    // emit feature suffix, if any
630
0
    if (!currentFeature->Suffix.empty()) {
631
0
      this->Items.emplace_back(
632
0
        BT<std::string>{ currentFeature->Suffix,
633
0
                         this->Items.back().Value.Backtrace },
634
0
        ItemIsPath::No);
635
0
    }
636
0
  }
637
638
  // Restore the target link type so the correct system runtime
639
  // libraries are found.
640
0
  cmValue lss = this->Target->GetProperty("LINK_SEARCH_END_STATIC");
641
0
  if (lss.IsOn()) {
642
0
    this->SetCurrentLinkType(LinkStatic);
643
0
  } else {
644
0
    this->SetCurrentLinkType(this->StartLinkType);
645
0
  }
646
647
  // Add implicit language runtime libraries and directories.
648
0
  this->AddImplicitLinkInfo();
649
650
  // Record targets referenced by $<TARGET_OBJECTS:...> sources.
651
0
  this->AddExternalObjectTargets();
652
653
0
  return true;
654
0
}
655
656
namespace {
657
void FinalizeFeatureFormat(std::string& format, std::string const& activeTag,
658
                           std::string const& otherTag)
659
0
{
660
0
  auto pos = format.find(otherTag);
661
0
  if (pos != std::string::npos) {
662
0
    format.erase(pos, format.find('}', pos) - pos + 1);
663
0
  }
664
0
  pos = format.find(activeTag);
665
0
  if (pos != std::string::npos) {
666
0
    format.erase(pos, activeTag.length());
667
0
    pos = format.find('}', pos);
668
0
    if (pos != std::string::npos) {
669
0
      format.erase(pos, 1);
670
0
    }
671
0
  }
672
0
}
673
674
bool IsValidFeatureFormat(std::string const& format)
675
0
{
676
0
  return format.find("<LIBRARY>") != std::string::npos ||
677
0
    format.find("<LIB_ITEM>") != std::string::npos ||
678
0
    format.find("<LINK_ITEM>") != std::string::npos;
679
0
}
680
681
class FeaturePlaceHolderExpander : public cmPlaceholderExpander
682
{
683
public:
684
  FeaturePlaceHolderExpander(std::string const* library,
685
                             std::string const* libItem = nullptr,
686
                             std::string const* linkItem = nullptr)
687
0
    : Library(library)
688
0
    , LibItem(libItem)
689
0
    , LinkItem(linkItem)
690
0
  {
691
0
  }
692
693
private:
694
  std::string ExpandVariable(std::string const& variable) override
695
0
  {
696
0
    if (this->Library && variable == "LIBRARY") {
697
0
      return *this->Library;
698
0
    }
699
0
    if (this->LibItem && variable == "LIB_ITEM") {
700
0
      return *this->LibItem;
701
0
    }
702
0
    if (this->LinkItem && variable == "LINK_ITEM") {
703
0
      return *this->LinkItem;
704
0
    }
705
706
0
    return variable;
707
0
  }
708
709
  std::string const* Library = nullptr;
710
  std::string const* LibItem = nullptr;
711
  std::string const* LinkItem = nullptr;
712
};
713
}
714
715
cmComputeLinkInformation::FeatureDescriptor::FeatureDescriptor(
716
  std::string name, std::string itemFormat)
717
0
  : Name(std::move(name))
718
0
  , Supported(true)
719
0
  , ItemPathFormat(std::move(itemFormat))
720
0
  , ItemNameFormat(this->ItemPathFormat)
721
0
{
722
0
}
723
cmComputeLinkInformation::FeatureDescriptor::FeatureDescriptor(
724
  std::string name, std::string itemPathFormat, std::string itemNameFormat)
725
0
  : Name(std::move(name))
726
0
  , Supported(true)
727
0
  , ItemPathFormat(std::move(itemPathFormat))
728
0
  , ItemNameFormat(std::move(itemNameFormat))
729
0
{
730
0
}
731
cmComputeLinkInformation::FeatureDescriptor::FeatureDescriptor(
732
  std::string name, std::string prefix, std::string itemPathFormat,
733
  std::string itemNameFormat, std::string suffix)
734
0
  : Name(std::move(name))
735
0
  , Supported(true)
736
0
  , Prefix(std::move(prefix))
737
0
  , Suffix(std::move(suffix))
738
0
  , ItemPathFormat(std::move(itemPathFormat))
739
0
  , ItemNameFormat(std::move(itemNameFormat))
740
0
{
741
0
}
742
cmComputeLinkInformation::FeatureDescriptor::FeatureDescriptor(
743
  std::string name, std::string prefix, std::string suffix, bool)
744
0
  : Name(std::move(name))
745
0
  , Supported(true)
746
0
  , Prefix(std::move(prefix))
747
0
  , Suffix(std::move(suffix))
748
0
{
749
0
}
750
751
std::string cmComputeLinkInformation::FeatureDescriptor::GetDecoratedItem(
752
  std::string const& library, ItemIsPath isPath) const
753
0
{
754
0
  auto format =
755
0
    isPath == ItemIsPath::Yes ? this->ItemPathFormat : this->ItemNameFormat;
756
757
  // replace <LIBRARY>, <LIB_ITEM> and <LINK_ITEM> patterns with library path
758
0
  FeaturePlaceHolderExpander expander(&library, &library, &library);
759
0
  return expander.ExpandVariables(format);
760
0
}
761
std::string cmComputeLinkInformation::FeatureDescriptor::GetDecoratedItem(
762
  std::string const& library, std::string const& libItem,
763
  std::string const& linkItem, ItemIsPath isPath) const
764
0
{
765
0
  auto format =
766
0
    isPath == ItemIsPath::Yes ? this->ItemPathFormat : this->ItemNameFormat;
767
768
  // replace <LIBRARY>, <LIB_ITEM> and <LINK_ITEM> patterns
769
0
  FeaturePlaceHolderExpander expander(&library, &libItem, &linkItem);
770
0
  return expander.ExpandVariables(format);
771
0
}
772
773
cmComputeLinkInformation::LibraryFeatureDescriptor::LibraryFeatureDescriptor(
774
  std::string name, std::string itemFormat)
775
0
  : FeatureDescriptor(std::move(name), std::move(itemFormat))
776
0
{
777
0
}
778
cmComputeLinkInformation::LibraryFeatureDescriptor::LibraryFeatureDescriptor(
779
  std::string name, std::string itemPathFormat, std::string itemNameFormat)
780
0
  : FeatureDescriptor(std::move(name), std::move(itemPathFormat),
781
0
                      std::move(itemNameFormat))
782
0
{
783
0
}
784
cmComputeLinkInformation::LibraryFeatureDescriptor::LibraryFeatureDescriptor(
785
  std::string name, std::string prefix, std::string itemPathFormat,
786
  std::string itemNameFormat, std::string suffix)
787
0
  : FeatureDescriptor(std::move(name), std::move(prefix),
788
0
                      std::move(itemPathFormat), std::move(itemNameFormat),
789
0
                      std::move(suffix))
790
0
{
791
0
}
792
793
bool cmComputeLinkInformation::AddLibraryFeature(std::string const& feature)
794
0
{
795
0
  auto it = this->LibraryFeatureDescriptors.find(feature);
796
0
  if (it != this->LibraryFeatureDescriptors.end()) {
797
0
    return it->second.Supported;
798
0
  }
799
800
0
  auto featureName =
801
0
    cmStrCat("CMAKE_", this->LinkLanguage, "_LINK_LIBRARY_USING_", feature);
802
0
  cmValue featureSupported =
803
0
    this->Makefile->GetDefinition(cmStrCat(featureName, "_SUPPORTED"));
804
0
  if (!featureSupported) {
805
    // language specific variable is not defined, fallback to the more generic
806
    // one
807
0
    featureName = cmStrCat("CMAKE_LINK_LIBRARY_USING_", feature);
808
0
    featureSupported =
809
0
      this->Makefile->GetDefinition(cmStrCat(featureName, "_SUPPORTED"));
810
0
  }
811
0
  if (!featureSupported.IsOn()) {
812
0
    this->LibraryFeatureDescriptors.emplace(feature, FeatureDescriptor{});
813
814
0
    this->CMakeInstance->IssueMessage(
815
0
      MessageType::FATAL_ERROR,
816
0
      cmStrCat(
817
0
        "Feature '", feature,
818
0
        "', specified through generator-expression '$<LINK_LIBRARY>' to "
819
0
        "link target '",
820
0
        this->Target->GetName(), "', is not supported for the '",
821
0
        this->LinkLanguage, "' link language."),
822
0
      this->Target->GetBacktrace());
823
824
0
    return false;
825
0
  }
826
827
0
  cmValue langFeature = this->Makefile->GetDefinition(featureName);
828
0
  if (!langFeature) {
829
0
    this->LibraryFeatureDescriptors.emplace(feature, FeatureDescriptor{});
830
831
0
    this->CMakeInstance->IssueMessage(
832
0
      MessageType::FATAL_ERROR,
833
0
      cmStrCat(
834
0
        "Feature '", feature,
835
0
        "', specified through generator-expression '$<LINK_LIBRARY>' to "
836
0
        "link target '",
837
0
        this->Target->GetName(), "', is not defined for the '",
838
0
        this->LinkLanguage, "' link language."),
839
0
      this->Target->GetBacktrace());
840
841
0
    return false;
842
0
  }
843
844
0
  auto items = cmExpandListWithBacktrace(
845
0
    *langFeature, this->Target->GetBacktrace(), cmList::EmptyElements::Yes);
846
847
0
  if ((items.size() == 1 && !IsValidFeatureFormat(items.front().Value)) ||
848
0
      (items.size() == 3 && !IsValidFeatureFormat(items[1].Value))) {
849
0
    this->LibraryFeatureDescriptors.emplace(feature, FeatureDescriptor{});
850
0
    this->CMakeInstance->IssueMessage(
851
0
      MessageType::FATAL_ERROR,
852
0
      cmStrCat("Feature '", feature, "', specified by variable '", featureName,
853
0
               "', is malformed (\"<LIBRARY>\", \"<LIB_ITEM>\", or "
854
0
               "\"<LINK_ITEM>\" patterns "
855
0
               "are missing) and cannot be used to link target '",
856
0
               this->Target->GetName(), "'."),
857
0
      this->Target->GetBacktrace());
858
859
0
    return false;
860
0
  }
861
862
  // now, handle possible "PATH{}" and "NAME{}" patterns
863
0
  if (items.size() == 1) {
864
0
    items.push_back(items.front());
865
0
    FinalizeFeatureFormat(items[0].Value, "PATH{", "NAME{");
866
0
    FinalizeFeatureFormat(items[1].Value, "NAME{", "PATH{");
867
0
  } else if (items.size() == 3) {
868
0
    items.insert(items.begin() + 1, items[1]);
869
0
    FinalizeFeatureFormat(items[1].Value, "PATH{", "NAME{");
870
0
    FinalizeFeatureFormat(items[2].Value, "NAME{", "PATH{");
871
0
  } else {
872
0
    this->LibraryFeatureDescriptors.emplace(feature, FeatureDescriptor{});
873
0
    this->CMakeInstance->IssueMessage(
874
0
      MessageType::FATAL_ERROR,
875
0
      cmStrCat("Feature '", feature, "', specified by variable '", featureName,
876
0
               "', is malformed (wrong number of elements) and cannot be used "
877
0
               "to link target '",
878
0
               this->Target->GetName(), "'."),
879
0
      this->Target->GetBacktrace());
880
881
0
    return false;
882
0
  }
883
0
  if ((items.size() == 2 && !IsValidFeatureFormat(items[0].Value)) ||
884
0
      (items.size() == 4 && !IsValidFeatureFormat(items[1].Value))) {
885
    // PATH{} has wrong format
886
0
    this->LibraryFeatureDescriptors.emplace(feature, FeatureDescriptor{});
887
0
    this->CMakeInstance->IssueMessage(
888
0
      MessageType::FATAL_ERROR,
889
0
      cmStrCat("Feature '", feature, "', specified by variable '", featureName,
890
0
               "', is malformed (\"<LIBRARY>\", \"<LIB_ITEM>\", or "
891
0
               "\"<LINK_ITEM>\" patterns "
892
0
               "are missing for \"PATH{}\" alternative) and cannot be used to "
893
0
               "link target '",
894
0
               this->Target->GetName(), "'."),
895
0
      this->Target->GetBacktrace());
896
897
0
    return false;
898
0
  }
899
0
  if ((items.size() == 2 && !IsValidFeatureFormat(items[1].Value)) ||
900
0
      (items.size() == 4 && !IsValidFeatureFormat(items[2].Value))) {
901
    // NAME{} has wrong format
902
0
    this->LibraryFeatureDescriptors.emplace(feature, FeatureDescriptor{});
903
0
    this->CMakeInstance->IssueMessage(
904
0
      MessageType::FATAL_ERROR,
905
0
      cmStrCat("Feature '", feature, "', specified by variable '", featureName,
906
0
               "', is malformed (\"<LIBRARY>\", \"<LIB_ITEM>\", or "
907
0
               "\"<LINK_ITEM>\" patterns "
908
0
               "are missing for \"NAME{}\" alternative) and cannot be used to "
909
0
               "link target '",
910
0
               this->Target->GetName(), "'."),
911
0
      this->Target->GetBacktrace());
912
913
0
    return false;
914
0
  }
915
916
  // replace LINKER: pattern
917
0
  this->Target->ResolveLinkerWrapper(items, this->LinkLanguage, true);
918
919
0
  if (items.size() == 2) {
920
0
    this->LibraryFeatureDescriptors.emplace(
921
0
      feature,
922
0
      LibraryFeatureDescriptor{ feature, items[0].Value, items[1].Value });
923
0
  } else {
924
0
    this->LibraryFeatureDescriptors.emplace(
925
0
      feature,
926
0
      LibraryFeatureDescriptor{ feature, items[0].Value, items[1].Value,
927
0
                                items[2].Value, items[3].Value });
928
0
  }
929
930
0
  return true;
931
0
}
932
933
cmComputeLinkInformation::FeatureDescriptor const&
934
cmComputeLinkInformation::GetLibraryFeature(std::string const& feature) const
935
0
{
936
0
  return this->LibraryFeatureDescriptors.find(feature)->second;
937
0
}
938
cmComputeLinkInformation::FeatureDescriptor const*
939
cmComputeLinkInformation::FindLibraryFeature(std::string const& feature) const
940
0
{
941
0
  auto it = this->LibraryFeatureDescriptors.find(feature);
942
0
  if (it == this->LibraryFeatureDescriptors.end()) {
943
0
    return nullptr;
944
0
  }
945
946
0
  return &it->second;
947
0
}
948
949
cmComputeLinkInformation::GroupFeatureDescriptor::GroupFeatureDescriptor(
950
  std::string name, std::string prefix, std::string suffix)
951
0
  : FeatureDescriptor(std::move(name), std::move(prefix), std::move(suffix),
952
0
                      true)
953
0
{
954
0
}
955
956
cmComputeLinkInformation::FeatureDescriptor const&
957
cmComputeLinkInformation::GetGroupFeature(std::string const& feature)
958
0
{
959
0
  auto it = this->GroupFeatureDescriptors.find(feature);
960
0
  if (it != this->GroupFeatureDescriptors.end()) {
961
0
    return it->second;
962
0
  }
963
964
0
  auto featureName =
965
0
    cmStrCat("CMAKE_", this->LinkLanguage, "_LINK_GROUP_USING_", feature);
966
0
  cmValue featureSupported =
967
0
    this->Makefile->GetDefinition(cmStrCat(featureName, "_SUPPORTED"));
968
0
  if (!featureSupported) {
969
    // language specific variable is not defined, fallback to the more generic
970
    // one
971
0
    featureName = cmStrCat("CMAKE_LINK_GROUP_USING_", feature);
972
0
    featureSupported =
973
0
      this->Makefile->GetDefinition(cmStrCat(featureName, "_SUPPORTED"));
974
0
  }
975
0
  if (!featureSupported.IsOn()) {
976
0
    this->CMakeInstance->IssueMessage(
977
0
      MessageType::FATAL_ERROR,
978
0
      cmStrCat("Feature '", feature,
979
0
               "', specified through generator-expression '$<LINK_GROUP>' to "
980
0
               "link target '",
981
0
               this->Target->GetName(), "', is not supported for the '",
982
0
               this->LinkLanguage, "' link language."),
983
0
      this->Target->GetBacktrace());
984
0
    return this->GroupFeatureDescriptors.emplace(feature, FeatureDescriptor{})
985
0
      .first->second;
986
0
  }
987
988
0
  cmValue langFeature = this->Makefile->GetDefinition(featureName);
989
0
  if (!langFeature) {
990
0
    this->CMakeInstance->IssueMessage(
991
0
      MessageType::FATAL_ERROR,
992
0
      cmStrCat("Feature '", feature,
993
0
               "', specified through generator-expression '$<LINK_GROUP>' to "
994
0
               "link target '",
995
0
               this->Target->GetName(), "', is not defined for the '",
996
0
               this->LinkLanguage, "' link language."),
997
0
      this->Target->GetBacktrace());
998
0
    return this->GroupFeatureDescriptors.emplace(feature, FeatureDescriptor{})
999
0
      .first->second;
1000
0
  }
1001
1002
0
  auto items = cmExpandListWithBacktrace(
1003
0
    *langFeature, this->Target->GetBacktrace(), cmList::EmptyElements::Yes);
1004
1005
  // replace LINKER: pattern
1006
0
  this->Target->ResolveLinkerWrapper(items, this->LinkLanguage, true);
1007
1008
0
  if (items.size() == 2) {
1009
0
    return this->GroupFeatureDescriptors
1010
0
      .emplace(
1011
0
        feature,
1012
0
        GroupFeatureDescriptor{ feature, items[0].Value, items[1].Value })
1013
0
      .first->second;
1014
0
  }
1015
1016
0
  this->CMakeInstance->IssueMessage(
1017
0
    MessageType::FATAL_ERROR,
1018
0
    cmStrCat("Feature '", feature, "', specified by variable '", featureName,
1019
0
             "', is malformed (wrong number of elements) and cannot be used "
1020
0
             "to link target '",
1021
0
             this->Target->GetName(), "'."),
1022
0
    this->Target->GetBacktrace());
1023
0
  return this->GroupFeatureDescriptors.emplace(feature, FeatureDescriptor{})
1024
0
    .first->second;
1025
0
}
1026
1027
void cmComputeLinkInformation::AddExternalObjectTargets()
1028
0
{
1029
0
  std::vector<cmSourceFile const*> externalObjects;
1030
0
  this->Target->GetExternalObjects(externalObjects, this->Config);
1031
0
  std::set<std::string> emitted;
1032
0
  for (auto const* externalObject : externalObjects) {
1033
0
    std::string const& objLib = externalObject->GetObjectLibrary();
1034
0
    if (objLib.empty()) {
1035
0
      continue;
1036
0
    }
1037
0
    if (emitted.insert(objLib).second) {
1038
0
      cmLinkItem const& objItem =
1039
0
        this->Target->ResolveLinkItem(BT<std::string>(objLib));
1040
0
      if (objItem.Target) {
1041
0
        this->ExternalObjectTargets.emplace_back(objItem.Target);
1042
0
      }
1043
0
    }
1044
0
  }
1045
0
}
1046
1047
void cmComputeLinkInformation::AddImplicitLinkInfo()
1048
0
{
1049
  // The link closure lists all languages whose implicit info is needed.
1050
0
  cmGeneratorTarget::LinkClosure const* lc =
1051
0
    this->Target->GetLinkClosure(this->Config);
1052
0
  for (std::string const& li : lc->Languages) {
1053
1054
0
    if (li == "CUDA" || li == "HIP") {
1055
      // These need to go before the other implicit link information
1056
      // as they could require symbols from those other library
1057
      // Currently restricted as CUDA and HIP are the only languages
1058
      // we have documented runtime behavior controls for
1059
0
      this->AddRuntimeLinkLibrary(li);
1060
0
    }
1061
1062
    // Skip those of the linker language.  They are implicit.
1063
0
    if (li != this->LinkLanguage) {
1064
0
      this->AddImplicitLinkInfo(li);
1065
0
    }
1066
0
  }
1067
0
}
1068
1069
void cmComputeLinkInformation::AddRuntimeLinkLibrary(std::string const& lang)
1070
0
{
1071
0
  std::string const& runtimeLibrary =
1072
0
    this->Target->GetRuntimeLinkLibrary(lang, this->Config);
1073
0
  if (runtimeLibrary.empty()) {
1074
0
    return;
1075
0
  }
1076
0
  if (cmValue runtimeLinkOptions = this->Makefile->GetDefinition(cmStrCat(
1077
0
        "CMAKE_", lang, "_RUNTIME_LIBRARY_LINK_OPTIONS_", runtimeLibrary))) {
1078
0
    cmList libs{ *runtimeLinkOptions };
1079
0
    for (auto const& i : libs) {
1080
0
      if (!cm::contains(this->ImplicitLinkLibs, i)) {
1081
0
        this->AddItem({ i });
1082
0
      }
1083
0
    }
1084
0
  }
1085
0
}
1086
1087
namespace {
1088
std::string const gcc_s = "gcc_s";
1089
std::string const gcc_s_asneeded = "gcc_s_asneeded";
1090
}
1091
1092
void cmComputeLinkInformation::AddImplicitLinkInfo(std::string const& lang)
1093
0
{
1094
0
  auto const impliedByLinkerLanguage = [this](std::string const& lib) -> bool {
1095
0
    if (cm::contains(this->ImplicitLinkLibs, lib)) {
1096
0
      return true;
1097
0
    }
1098
    // As of GCC 16, `gcc` implies `gcc_s_asneeded` but `g++` implies `gcc_s`.
1099
    // Accept them interchangeably when linking mixed C and C++ binaries.
1100
0
    if ((lib == gcc_s_asneeded &&
1101
0
         cm::contains(this->ImplicitLinkLibs, gcc_s)) ||
1102
0
        (lib == gcc_s &&
1103
0
         cm::contains(this->ImplicitLinkLibs, gcc_s_asneeded))) {
1104
0
      return true;
1105
0
    }
1106
0
    return false;
1107
0
  };
1108
1109
  // Add libraries for this language that are not implied by the
1110
  // linker language.
1111
0
  std::string libVar = cmStrCat("CMAKE_", lang, "_IMPLICIT_LINK_LIBRARIES");
1112
0
  if (cmValue libs = this->Makefile->GetDefinition(libVar)) {
1113
0
    cmList libsList{ *libs };
1114
0
    for (std::string const& i : libsList) {
1115
0
      if (!impliedByLinkerLanguage(i)) {
1116
0
        this->AddItem({ i });
1117
0
      }
1118
0
    }
1119
0
  }
1120
1121
  // Add linker search paths for this language that are not
1122
  // implied by the linker language.
1123
0
  std::string dirVar = cmStrCat("CMAKE_", lang, "_IMPLICIT_LINK_DIRECTORIES");
1124
0
  if (cmValue dirs = this->Makefile->GetDefinition(dirVar)) {
1125
0
    cmList dirsList{ *dirs };
1126
0
    this->OrderLinkerSearchPath->AddLanguageDirectories(dirsList);
1127
0
  }
1128
0
}
1129
1130
void cmComputeLinkInformation::AddItem(LinkEntry const& entry)
1131
0
{
1132
0
  cmGeneratorTarget const* tgt = entry.Target;
1133
0
  BT<std::string> const& item = entry.Item;
1134
1135
  // Compute the proper name to use to link this library.
1136
0
  std::string const& config = this->Config;
1137
0
  bool impexe = (tgt && tgt->IsExecutableWithExports());
1138
0
  if (impexe && !tgt->HasImportLibrary(config) && !this->LoaderFlag) {
1139
    // Skip linking to executables on platforms with no import
1140
    // libraries or loader flags.
1141
0
    return;
1142
0
  }
1143
1144
0
  if (tgt && tgt->IsLinkable()) {
1145
    // This is a CMake target.  Ask the target for its real name.
1146
0
    if (impexe && this->LoaderFlag) {
1147
      // This link item is an executable that may provide symbols
1148
      // used by this target.  A special flag is needed on this
1149
      // platform.  Add it now using a special feature.
1150
0
      cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(config)
1151
0
        ? cmStateEnums::ImportLibraryArtifact
1152
0
        : cmStateEnums::RuntimeBinaryArtifact;
1153
0
      std::string exe = tgt->GetFullPath(config, artifact, true);
1154
0
      this->Items.emplace_back(
1155
0
        BT<std::string>(exe, item.Backtrace), ItemIsPath::Yes, tgt, nullptr,
1156
0
        this->FindLibraryFeature(entry.Feature == DEFAULT
1157
0
                                   ? "__CMAKE_LINK_EXECUTABLE"
1158
0
                                   : entry.Feature));
1159
0
      this->Depends.push_back(std::move(exe));
1160
0
    } else if (tgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
1161
      // Add the interface library as an item so it can be considered as part
1162
      // of COMPATIBLE_INTERFACE_ enforcement.  The generators will ignore
1163
      // this for the actual link line.
1164
0
      this->Items.emplace_back(std::string(), ItemIsPath::No, tgt);
1165
1166
      // Also add the item the interface specifies to be used in its place.
1167
0
      std::string const& libName = tgt->GetImportedLibName(config);
1168
0
      if (!libName.empty()) {
1169
0
        this->AddItem(BT<std::string>(libName, item.Backtrace));
1170
0
      }
1171
0
    } else if (tgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
1172
0
      this->Items.emplace_back(item, ItemIsPath::No, tgt);
1173
0
    } else if (this->GlobalGenerator->IsXcode() &&
1174
0
               !tgt->GetImportedXcFrameworkPath(config).empty()) {
1175
0
      this->Items.emplace_back(
1176
0
        tgt->GetImportedXcFrameworkPath(config), ItemIsPath::Yes, tgt, nullptr,
1177
0
        this->FindLibraryFeature(entry.Feature == DEFAULT
1178
0
                                   ? "__CMAKE_LINK_XCFRAMEWORK"
1179
0
                                   : entry.Feature));
1180
0
    } else {
1181
      // Decide whether to use an import library.
1182
0
      cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(config)
1183
0
        ? cmStateEnums::ImportLibraryArtifact
1184
0
        : cmStateEnums::RuntimeBinaryArtifact;
1185
1186
      // Pass the full path to the target file.
1187
0
      BT<std::string> lib = BT<std::string>(
1188
0
        tgt->GetFullPath(config, artifact, true), item.Backtrace);
1189
0
      if (tgt->IsAIX() && cmHasLiteralSuffix(lib.Value, "-NOTFOUND") &&
1190
0
          artifact == cmStateEnums::ImportLibraryArtifact) {
1191
        // This is an imported executable on AIX that has ENABLE_EXPORTS
1192
        // but not IMPORTED_IMPLIB.  CMake used to produce and accept such
1193
        // imported executables on AIX before we taught it to use linker
1194
        // import files.  For compatibility, simply skip linking to this
1195
        // executable as we did before.  It works with runtime linking.
1196
0
        return;
1197
0
      }
1198
0
      if (!this->LinkDependsNoShared ||
1199
0
          tgt->GetType() != cmStateEnums::SHARED_LIBRARY) {
1200
0
        this->Depends.push_back(lib.Value);
1201
0
      }
1202
1203
0
      LinkEntry libEntry{ entry };
1204
0
      libEntry.Item = lib;
1205
0
      this->AddTargetItem(libEntry);
1206
0
      if (tgt->IsApple() && tgt->HasImportLibrary(config)) {
1207
        // Use the library rather than the tbd file for runpath computation
1208
0
        this->AddLibraryRuntimeInfo(
1209
0
          tgt->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact, true),
1210
0
          tgt);
1211
0
      } else {
1212
0
        this->AddLibraryRuntimeInfo(lib.Value, tgt);
1213
0
      }
1214
0
      if (tgt && tgt->GetType() == cmStateEnums::SHARED_LIBRARY &&
1215
0
          this->Target->IsDLLPlatform()) {
1216
0
        this->AddRuntimeDLL(tgt);
1217
0
      }
1218
0
    }
1219
1220
0
    auto xcFrameworkPath = tgt->GetImportedXcFrameworkPath(config);
1221
0
    if (!xcFrameworkPath.empty()) {
1222
0
      auto plist = cmParseXcFrameworkPlist(xcFrameworkPath, *this->Makefile,
1223
0
                                           item.Backtrace);
1224
0
      if (!plist) {
1225
0
        return;
1226
0
      }
1227
0
      if (auto const* library =
1228
0
            plist->SelectSuitableLibrary(*this->Makefile, item.Backtrace)) {
1229
0
        if (!library->HeadersPath.empty()) {
1230
0
          this->AddXcFrameworkHeaderPath(cmStrCat(xcFrameworkPath, '/',
1231
0
                                                  library->LibraryIdentifier,
1232
0
                                                  '/', library->HeadersPath));
1233
0
        }
1234
0
      } else {
1235
0
        return;
1236
0
      }
1237
0
    }
1238
0
  } else {
1239
    // This is not a CMake target.  Use the name given.
1240
0
    if (cmHasSuffix(entry.Feature, "FRAMEWORK"_s) ||
1241
0
        (entry.Feature == DEFAULT &&
1242
0
         cmSystemTools::IsPathToFramework(item.Value) &&
1243
0
         this->Target->IsApple())) {
1244
      // This is a framework.
1245
0
      this->AddFrameworkItem(entry);
1246
0
    } else if (cmHasSuffix(entry.Feature, "XCFRAMEWORK"_s) ||
1247
0
               (entry.Feature == DEFAULT &&
1248
0
                cmSystemTools::IsPathToXcFramework(item.Value) &&
1249
0
                this->Target->IsApple())) {
1250
      // This is a framework.
1251
0
      this->AddXcFrameworkItem(entry);
1252
0
    } else if (cmSystemTools::FileIsFullPath(item.Value)) {
1253
0
      if (cmSystemTools::FileIsDirectory(item.Value)) {
1254
        // This is a directory.
1255
0
        this->DropDirectoryItem(item);
1256
0
      } else {
1257
        // Use the full path given to the library file.
1258
0
        this->Depends.push_back(item.Value);
1259
0
        this->AddFullItem(entry);
1260
0
        this->AddLibraryRuntimeInfo(item.Value);
1261
0
      }
1262
0
    } else if (entry.Kind != cmComputeLinkDepends::LinkEntry::Object) {
1263
      // This is a library or option specified by the user.
1264
0
      this->AddUserItem(entry);
1265
0
    }
1266
0
  }
1267
0
}
1268
1269
void cmComputeLinkInformation::AddSharedDepItem(LinkEntry const& entry)
1270
0
{
1271
0
  BT<std::string> const& item = entry.Item;
1272
0
  cmGeneratorTarget const* tgt = entry.Target;
1273
1274
  // Record dependencies on DLLs.
1275
0
  if (tgt && tgt->GetType() == cmStateEnums::SHARED_LIBRARY &&
1276
0
      this->Target->IsDLLPlatform() &&
1277
0
      this->SharedDependencyMode != SharedDepModeLink) {
1278
0
    this->AddRuntimeDLL(tgt);
1279
0
  }
1280
1281
  // If dropping shared library dependencies, ignore them.
1282
0
  if (this->SharedDependencyMode == SharedDepModeNone) {
1283
0
    return;
1284
0
  }
1285
1286
  // The user may have incorrectly named an item.  Skip items that are
1287
  // not full paths to shared libraries.
1288
0
  if (tgt) {
1289
    // The target will provide a full path.  Make sure it is a shared
1290
    // library.
1291
0
    if (tgt->GetType() != cmStateEnums::SHARED_LIBRARY) {
1292
0
      return;
1293
0
    }
1294
0
  } else {
1295
    // Skip items that are not full paths.  We will not be able to
1296
    // reliably specify them.
1297
0
    if (!cmSystemTools::FileIsFullPath(item.Value)) {
1298
0
      return;
1299
0
    }
1300
1301
    // Get the name of the library from the file name.
1302
0
    std::string file = cmSystemTools::GetFilenameName(item.Value);
1303
0
    if (!this->ExtractSharedLibraryName.find(file)) {
1304
      // This is not the name of a shared library.
1305
0
      return;
1306
0
    }
1307
0
  }
1308
1309
  // If in linking mode, just link to the shared library.
1310
0
  if (this->SharedDependencyMode == SharedDepModeLink ||
1311
      // For an imported shared library without a known runtime artifact,
1312
      // such as a CUDA stub, a library file named with the real soname
1313
      // may not be available at all, so '-rpath-link' cannot help linkers
1314
      // find it to satisfy '--no-allow-shlib-undefined' recursively.
1315
      // Pass this dependency to the linker explicitly just in case.
1316
      // If the linker also uses '--as-needed' behavior, this will not
1317
      // add an unnecessary direct dependency.
1318
0
      (tgt && tgt->IsImported() &&
1319
0
       !tgt->HasKnownRuntimeArtifactLocation(this->Config) &&
1320
0
       this->Target->LinkerEnforcesNoAllowShLibUndefined(this->Config))) {
1321
0
    this->AddItem(entry);
1322
0
    return;
1323
0
  }
1324
1325
  // Get a full path to the dependent shared library.
1326
  // Add it to the runtime path computation so that the target being
1327
  // linked will be able to find it.
1328
0
  std::string lib;
1329
0
  if (tgt) {
1330
0
    cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(this->Config)
1331
0
      ? cmStateEnums::ImportLibraryArtifact
1332
0
      : cmStateEnums::RuntimeBinaryArtifact;
1333
0
    lib = tgt->GetFullPath(this->Config, artifact);
1334
0
    if (tgt->IsApple() && tgt->HasImportLibrary(this->Config)) {
1335
      // Use the library rather than the tbd file for runpath computation
1336
0
      this->AddLibraryRuntimeInfo(
1337
0
        tgt->GetFullPath(this->Config, cmStateEnums::RuntimeBinaryArtifact,
1338
0
                         true),
1339
0
        tgt);
1340
0
    } else {
1341
0
      this->AddLibraryRuntimeInfo(lib, tgt);
1342
0
    }
1343
0
  } else {
1344
0
    lib = item.Value;
1345
0
    this->AddLibraryRuntimeInfo(lib);
1346
0
  }
1347
1348
  // Check if we need to include the dependent shared library in other
1349
  // path ordering.
1350
0
  cmOrderDirectories* order = nullptr;
1351
0
  if (this->SharedDependencyMode == SharedDepModeLibDir &&
1352
0
      !this->LinkWithRuntimePath /* AddLibraryRuntimeInfo adds it */) {
1353
    // Add the item to the linker search path.
1354
0
    order = this->OrderLinkerSearchPath.get();
1355
0
  } else if (this->SharedDependencyMode == SharedDepModeDir) {
1356
    // Add the item to the separate dependent library search path.
1357
0
    order = this->OrderDependentRPath.get();
1358
0
  }
1359
0
  if (order) {
1360
0
    if (tgt) {
1361
0
      std::string soName = tgt->GetSOName(this->Config);
1362
0
      char const* soname = soName.empty() ? nullptr : soName.c_str();
1363
0
      order->AddRuntimeLibrary(lib, soname);
1364
0
    } else {
1365
0
      order->AddRuntimeLibrary(lib);
1366
0
    }
1367
0
  }
1368
0
}
1369
1370
void cmComputeLinkInformation::AddRuntimeDLL(cmGeneratorTarget const* tgt)
1371
0
{
1372
0
  if (std::find(this->RuntimeDLLs.begin(), this->RuntimeDLLs.end(), tgt) ==
1373
0
      this->RuntimeDLLs.end()) {
1374
0
    this->RuntimeDLLs.emplace_back(tgt);
1375
0
  }
1376
0
}
1377
1378
void cmComputeLinkInformation::ComputeLinkTypeInfo()
1379
0
{
1380
  // Check whether archives may actually be shared libraries.
1381
0
  this->ArchivesMayBeShared =
1382
0
    this->CMakeInstance->GetState()->GetGlobalPropertyAsBool(
1383
0
      "TARGET_ARCHIVES_MAY_BE_SHARED_LIBS");
1384
1385
  // First assume we cannot do link type stuff.
1386
0
  this->LinkTypeEnabled = false;
1387
1388
  // Lookup link type selection flags.
1389
0
  cmValue static_link_type_flag = nullptr;
1390
0
  cmValue shared_link_type_flag = nullptr;
1391
0
  char const* target_type_str = nullptr;
1392
0
  switch (this->Target->GetType()) {
1393
0
    case cmStateEnums::EXECUTABLE:
1394
0
      target_type_str = "EXE";
1395
0
      break;
1396
0
    case cmStateEnums::SHARED_LIBRARY:
1397
0
      target_type_str = "SHARED_LIBRARY";
1398
0
      break;
1399
0
    case cmStateEnums::MODULE_LIBRARY:
1400
0
      target_type_str = "SHARED_MODULE";
1401
0
      break;
1402
0
    default:
1403
0
      break;
1404
0
  }
1405
0
  if (target_type_str) {
1406
0
    std::string static_link_type_flag_var =
1407
0
      cmStrCat("CMAKE_", target_type_str, "_LINK_STATIC_", this->LinkLanguage,
1408
0
               "_FLAGS");
1409
0
    static_link_type_flag =
1410
0
      this->Makefile->GetDefinition(static_link_type_flag_var);
1411
1412
0
    std::string shared_link_type_flag_var =
1413
0
      cmStrCat("CMAKE_", target_type_str, "_LINK_DYNAMIC_", this->LinkLanguage,
1414
0
               "_FLAGS");
1415
0
    shared_link_type_flag =
1416
0
      this->Makefile->GetDefinition(shared_link_type_flag_var);
1417
0
  }
1418
1419
  // We can support link type switching only if all needed flags are
1420
  // known.
1421
0
  if (cmNonempty(static_link_type_flag) && cmNonempty(shared_link_type_flag)) {
1422
0
    this->LinkTypeEnabled = true;
1423
0
    this->StaticLinkTypeFlag = *static_link_type_flag;
1424
0
    this->SharedLinkTypeFlag = *shared_link_type_flag;
1425
0
  }
1426
1427
  // Lookup the starting link type from the target (linked statically?).
1428
0
  cmValue lss = this->Target->GetProperty("LINK_SEARCH_START_STATIC");
1429
0
  this->StartLinkType = lss.IsOn() ? LinkStatic : LinkShared;
1430
0
  this->CurrentLinkType = this->StartLinkType;
1431
0
}
1432
1433
void cmComputeLinkInformation::ComputeItemParserInfo()
1434
0
{
1435
  // Get possible library name prefixes.
1436
0
  cmMakefile* mf = this->Makefile;
1437
0
  this->AddLinkPrefix(mf->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
1438
0
  this->AddLinkPrefix(mf->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX"));
1439
1440
  // Import library names should be matched and treated as shared
1441
  // libraries for the purposes of linking.
1442
0
  this->AddLinkExtension(mf->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
1443
0
                         LinkShared);
1444
0
  this->AddLinkExtension(mf->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
1445
0
                         LinkStatic);
1446
0
  this->AddLinkExtension(mf->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
1447
0
                         LinkShared);
1448
0
  this->AddLinkExtension(mf->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX"),
1449
0
                         LinkUnknown);
1450
0
  if (cmValue linkSuffixes =
1451
0
        mf->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS")) {
1452
0
    cmList linkSuffixList{ *linkSuffixes };
1453
0
    for (auto const& i : linkSuffixList) {
1454
0
      this->AddLinkExtension(i, LinkUnknown);
1455
0
    }
1456
0
  }
1457
0
  if (cmValue sharedSuffixes =
1458
0
        mf->GetDefinition("CMAKE_EXTRA_SHARED_LIBRARY_SUFFIXES")) {
1459
0
    cmList sharedSuffixList{ *sharedSuffixes };
1460
0
    for (std::string const& i : sharedSuffixList) {
1461
0
      this->AddLinkExtension(i, LinkShared);
1462
0
    }
1463
0
  }
1464
1465
  // Compute a regex to match link extensions.
1466
0
  std::string libext =
1467
0
    this->CreateExtensionRegex(this->LinkExtensions, LinkUnknown);
1468
1469
  // Create regex to remove any library extension.
1470
0
  std::string reg("(.*)");
1471
0
  reg += libext;
1472
0
  this->OrderLinkerSearchPath->SetLinkExtensionInfo(this->LinkExtensions, reg);
1473
1474
  // Create a regex to match a library name.  Match index 1 will be
1475
  // the prefix if it exists and empty otherwise.  Match index 2 will
1476
  // be the library name.  Match index 3 will be the library
1477
  // extension.
1478
0
  reg = "^(";
1479
0
  for (std::string const& p : this->LinkPrefixes) {
1480
0
    reg += p;
1481
0
    reg += '|';
1482
0
  }
1483
0
  reg += ")([^/:]*)";
1484
1485
  // Create a regex to match any library name.
1486
0
  std::string reg_any = cmStrCat(reg, libext);
1487
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1488
  fprintf(stderr, "any regex [%s]\n", reg_any.c_str());
1489
#endif
1490
0
  this->ExtractAnyLibraryName.compile(reg_any);
1491
1492
  // Create a regex to match static library names.
1493
0
  if (!this->StaticLinkExtensions.empty()) {
1494
0
    std::string reg_static = cmStrCat(
1495
0
      reg, this->CreateExtensionRegex(this->StaticLinkExtensions, LinkStatic));
1496
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1497
    fprintf(stderr, "static regex [%s]\n", reg_static.c_str());
1498
#endif
1499
0
    this->ExtractStaticLibraryName.compile(reg_static);
1500
0
  }
1501
1502
  // Create a regex to match shared library names.
1503
0
  if (!this->SharedLinkExtensions.empty()) {
1504
0
    std::string reg_shared = reg;
1505
0
    this->SharedRegexString =
1506
0
      this->CreateExtensionRegex(this->SharedLinkExtensions, LinkShared);
1507
0
    reg_shared += this->SharedRegexString;
1508
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1509
    fprintf(stderr, "shared regex [%s]\n", reg_shared.c_str());
1510
#endif
1511
0
    this->ExtractSharedLibraryName.compile(reg_shared);
1512
0
  }
1513
0
}
1514
1515
void cmComputeLinkInformation::AddLinkPrefix(std::string const& p)
1516
0
{
1517
0
  if (!p.empty()) {
1518
0
    this->LinkPrefixes.insert(p);
1519
0
  }
1520
0
}
1521
1522
void cmComputeLinkInformation::AddLinkExtension(std::string const& e,
1523
                                                LinkType type)
1524
0
{
1525
0
  if (!e.empty()) {
1526
0
    if (type == LinkStatic) {
1527
0
      this->StaticLinkExtensions.emplace_back(e);
1528
0
    }
1529
0
    if (type == LinkShared) {
1530
0
      this->SharedLinkExtensions.emplace_back(e);
1531
0
    }
1532
0
    this->LinkExtensions.emplace_back(e);
1533
0
  }
1534
0
}
1535
1536
// XXX(clang-tidy): This method's const-ness is platform dependent, so we
1537
// cannot make it `const` as `clang-tidy` wants us to.
1538
// NOLINTNEXTLINE(readability-make-member-function-const)
1539
std::string cmComputeLinkInformation::CreateExtensionRegex(
1540
  std::vector<std::string> const& exts, LinkType type)
1541
0
{
1542
  // Build a list of extension choices.
1543
0
  std::string libext = "(";
1544
0
  char const* sep = "";
1545
0
  for (std::string const& i : exts) {
1546
    // Separate this choice from the previous one.
1547
0
    libext += sep;
1548
0
    sep = "|";
1549
1550
    // Store this extension choice with the "." escaped.
1551
0
    libext += "\\";
1552
#if defined(_WIN32) && !defined(__CYGWIN__)
1553
    libext += this->NoCaseExpression(i);
1554
#else
1555
0
    libext += i;
1556
0
#endif
1557
0
  }
1558
1559
  // Finish the list.
1560
0
  libext += ')';
1561
1562
  // Add an optional OpenBSD-style version or major.minor.version component.
1563
0
  if (this->IsOpenBSD || type == LinkShared) {
1564
0
    libext += "(\\.[0-9]+)*";
1565
0
  }
1566
1567
0
  libext += '$';
1568
0
  return libext;
1569
0
}
1570
1571
std::string cmComputeLinkInformation::NoCaseExpression(std::string const& str)
1572
0
{
1573
0
  std::string ret;
1574
0
  ret.reserve(str.size() * 4);
1575
0
  for (char c : str) {
1576
0
    if (c == '.') {
1577
0
      ret += c;
1578
0
    } else {
1579
0
      ret += '[';
1580
0
      ret += static_cast<char>(cmsysString_tolower(c));
1581
0
      ret += static_cast<char>(cmsysString_toupper(c));
1582
0
      ret += ']';
1583
0
    }
1584
0
  }
1585
0
  return ret;
1586
0
}
1587
1588
void cmComputeLinkInformation::SetCurrentLinkType(LinkType lt)
1589
0
{
1590
  // If we are changing the current link type add the flag to tell the
1591
  // linker about it.
1592
0
  if (this->CurrentLinkType != lt) {
1593
0
    this->CurrentLinkType = lt;
1594
1595
0
    if (this->LinkTypeEnabled) {
1596
0
      switch (this->CurrentLinkType) {
1597
0
        case LinkStatic:
1598
0
          this->Items.emplace_back(this->StaticLinkTypeFlag, ItemIsPath::No);
1599
0
          break;
1600
0
        case LinkShared:
1601
0
          this->Items.emplace_back(this->SharedLinkTypeFlag, ItemIsPath::No);
1602
0
          break;
1603
0
        default:
1604
0
          break;
1605
0
      }
1606
0
    }
1607
0
  }
1608
0
}
1609
1610
void cmComputeLinkInformation::AddTargetItem(LinkEntry const& entry)
1611
0
{
1612
  // This is called to handle a link item that is a full path to a target.
1613
  // If the target is not a static library make sure the link type is
1614
  // shared.  This is because dynamic-mode linking can handle both
1615
  // shared and static libraries but static-mode can handle only
1616
  // static libraries.  If a previous user item changed the link type
1617
  // to static we need to make sure it is back to shared.
1618
0
  BT<std::string> const& item = entry.Item;
1619
0
  cmGeneratorTarget const* target = entry.Target;
1620
1621
0
  if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
1622
0
    this->SetCurrentLinkType(LinkShared);
1623
0
  }
1624
1625
  // Keep track of shared library targets linked.
1626
0
  if (target->GetType() == cmStateEnums::SHARED_LIBRARY) {
1627
0
    this->SharedLibrariesLinked.insert(target);
1628
0
  }
1629
1630
  // Handle case of an imported shared library with no soname.
1631
0
  if (this->NoSONameUsesPath &&
1632
0
      target->IsImportedSharedLibWithoutSOName(this->Config)) {
1633
0
    this->AddSharedLibNoSOName(entry);
1634
0
    return;
1635
0
  }
1636
1637
0
  bool const isImportedFrameworkFolderOnApple =
1638
0
    target->IsImportedFrameworkFolderOnApple(this->Config);
1639
0
  if (target->IsFrameworkOnApple() || isImportedFrameworkFolderOnApple) {
1640
    // Add the framework directory and the framework item itself
1641
0
    auto fwDescriptor = this->GlobalGenerator->SplitFrameworkPath(
1642
0
      item.Value, cmGlobalGenerator::FrameworkFormat::Extended);
1643
0
    if (!fwDescriptor) {
1644
0
      this->CMakeInstance->IssueMessage(
1645
0
        MessageType::FATAL_ERROR,
1646
0
        cmStrCat("Could not parse framework path \"", item.Value,
1647
0
                 "\" linked by target ", this->Target->GetName(), '.'),
1648
0
        item.Backtrace);
1649
0
      return;
1650
0
    }
1651
0
    if (!fwDescriptor->Directory.empty()) {
1652
      // Add the directory portion to the framework search path.
1653
0
      this->AddFrameworkPath(fwDescriptor->Directory);
1654
0
    }
1655
1656
0
    if (this->GlobalGenerator->IsXcode()) {
1657
0
      if (isImportedFrameworkFolderOnApple) {
1658
0
        if (entry.Feature == DEFAULT) {
1659
0
          this->AddLibraryFeature("FRAMEWORK");
1660
0
          this->Items.emplace_back(item, ItemIsPath::Yes, target, nullptr,
1661
0
                                   this->FindLibraryFeature("FRAMEWORK"));
1662
0
        } else {
1663
0
          this->Items.emplace_back(item, ItemIsPath::Yes, target, nullptr,
1664
0
                                   this->FindLibraryFeature(entry.Feature));
1665
0
        }
1666
0
      } else {
1667
0
        this->Items.emplace_back(
1668
0
          item, ItemIsPath::Yes, target, nullptr,
1669
0
          this->FindLibraryFeature(entry.Feature == DEFAULT
1670
0
                                     ? "__CMAKE_LINK_FRAMEWORK"
1671
0
                                     : entry.Feature));
1672
0
      }
1673
0
    } else {
1674
0
      if (cmHasSuffix(entry.Feature, "FRAMEWORK"_s)) {
1675
0
        this->Items.emplace_back(fwDescriptor->GetLinkName(), ItemIsPath::Yes,
1676
0
                                 target, nullptr,
1677
0
                                 this->FindLibraryFeature(entry.Feature));
1678
0
      } else if (entry.Feature == DEFAULT &&
1679
0
                 isImportedFrameworkFolderOnApple) {
1680
0
        this->AddLibraryFeature("FRAMEWORK");
1681
0
        this->Items.emplace_back(fwDescriptor->GetLinkName(), ItemIsPath::Yes,
1682
0
                                 target, nullptr,
1683
0
                                 this->FindLibraryFeature("FRAMEWORK"));
1684
0
      } else {
1685
0
        this->Items.emplace_back(
1686
0
          item, ItemIsPath::Yes, target, nullptr,
1687
0
          this->FindLibraryFeature(entry.Feature == DEFAULT
1688
0
                                     ? "__CMAKE_LINK_LIBRARY"
1689
0
                                     : entry.Feature));
1690
0
      }
1691
0
    }
1692
0
  } else {
1693
    // Now add the full path to the library.
1694
0
    this->Items.emplace_back(
1695
0
      item, ItemIsPath::Yes, target, nullptr,
1696
0
      this->FindLibraryFeature(
1697
0
        entry.Feature == DEFAULT ? "__CMAKE_LINK_LIBRARY" : entry.Feature));
1698
0
  }
1699
0
}
1700
1701
void cmComputeLinkInformation::AddFullItem(LinkEntry const& entry)
1702
0
{
1703
0
  BT<std::string> const& item = entry.Item;
1704
1705
  // Check for the implicit link directory special case.
1706
0
  if (this->CheckImplicitDirItem(entry)) {
1707
0
    return;
1708
0
  }
1709
1710
  // Check for case of shared library with no builtin soname.
1711
0
  if (this->NoSONameUsesPath && this->CheckSharedLibNoSOName(entry)) {
1712
0
    return;
1713
0
  }
1714
1715
  // This is called to handle a link item that is a full path.
1716
  // If the target is not a static library make sure the link type is
1717
  // shared.  This is because dynamic-mode linking can handle both
1718
  // shared and static libraries but static-mode can handle only
1719
  // static libraries.  If a previous user item changed the link type
1720
  // to static we need to make sure it is back to shared.
1721
0
  if (this->LinkTypeEnabled) {
1722
0
    std::string name = cmSystemTools::GetFilenameName(item.Value);
1723
0
    if (this->ExtractSharedLibraryName.find(name)) {
1724
0
      this->SetCurrentLinkType(LinkShared);
1725
0
    } else if (!this->ExtractStaticLibraryName.find(item.Value)) {
1726
      // We cannot determine the type.  Assume it is the target's
1727
      // default type.
1728
0
      this->SetCurrentLinkType(this->StartLinkType);
1729
0
    }
1730
0
  }
1731
1732
  // Now add the full path to the library.
1733
0
  this->Items.emplace_back(
1734
0
    item, ItemIsPath::Yes, nullptr, entry.ObjectSource,
1735
0
    this->FindLibraryFeature(
1736
0
      entry.Feature == DEFAULT
1737
0
        ? (entry.Kind == cmComputeLinkDepends::LinkEntry::Object
1738
0
             ? "__CMAKE_LINK_OBJECT"
1739
0
             : "__CMAKE_LINK_LIBRARY")
1740
0
        : entry.Feature));
1741
0
}
1742
1743
bool cmComputeLinkInformation::CheckImplicitDirItem(LinkEntry const& entry)
1744
0
{
1745
0
  BT<std::string> const& item = entry.Item;
1746
1747
  // We only switch to a pathless item if the link type may be
1748
  // enforced.  Fortunately only platforms that support link types
1749
  // seem to have magic per-architecture implicit link directories.
1750
0
  if (!this->LinkTypeEnabled) {
1751
0
    return false;
1752
0
  }
1753
1754
  // Check if this item is in an implicit link directory.
1755
0
  std::string dir = cmSystemTools::GetFilenamePath(item.Value);
1756
0
  if (!cm::contains(this->ImplicitLinkDirs, dir)) {
1757
    // Only libraries in implicit link directories are converted to
1758
    // pathless items.
1759
0
    return false;
1760
0
  }
1761
1762
  // Only apply the policy below if the library file is one that can
1763
  // be found by the linker.
1764
0
  std::string file = cmSystemTools::GetFilenameName(item.Value);
1765
0
  if (!this->ExtractAnyLibraryName.find(file)) {
1766
0
    return false;
1767
0
  }
1768
1769
0
  return false;
1770
0
}
1771
1772
void cmComputeLinkInformation::AddUserItem(LinkEntry const& entry)
1773
0
{
1774
  // This is called to handle a link item that does not match a CMake
1775
  // target and is not a full path.  We check here if it looks like a
1776
  // library file name to automatically request the proper link type
1777
  // from the linker.  For example:
1778
  //
1779
  //   foo       ==>  -lfoo
1780
  //   libfoo.a  ==>  -Wl,-Bstatic -lfoo
1781
1782
0
  cm::string_view const LINKER{ "LINKER:" };
1783
0
  BT<std::string> const& item = entry.Item;
1784
1785
0
  if (item.Value[0] == '-' || item.Value[0] == '$' || item.Value[0] == '`') {
1786
    // Pass flags through untouched.
1787
1788
    // Restore the target link type since this item does not specify
1789
    // one.
1790
0
    this->SetCurrentLinkType(this->StartLinkType);
1791
1792
    // Use the item verbatim.
1793
0
    this->Items.emplace_back(item, ItemIsPath::No);
1794
0
    return;
1795
0
  }
1796
1797
0
  if (cmHasPrefix(item.Value, LINKER)) {
1798
0
    std::vector<BT<std::string>> linkerFlag{ 1, item };
1799
0
    this->Target->ResolveLinkerWrapper(linkerFlag, this->GetLinkLanguage(),
1800
0
                                       /* joinItems = */ true);
1801
0
    if (!linkerFlag.empty()) {
1802
0
      this->Items.emplace_back(linkerFlag.front(), ItemIsPath::No);
1803
0
    }
1804
0
    return;
1805
0
  }
1806
1807
  // Parse out the prefix, base, and suffix components of the
1808
  // library name.  If the name matches that of a shared or static
1809
  // library then set the link type accordingly.
1810
  //
1811
  // Search for shared library names first because some platforms
1812
  // have shared libraries with names that match the static library
1813
  // pattern.  For example cygwin and msys use the convention
1814
  // libfoo.dll.a for import libraries and libfoo.a for static
1815
  // libraries.  On AIX a library with the name libfoo.a can be
1816
  // shared!
1817
0
  std::string lib;
1818
0
  if (this->ExtractSharedLibraryName.find(item.Value)) {
1819
// This matches a shared library file name.
1820
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1821
    fprintf(stderr, "shared regex matched [%s] [%s] [%s]\n",
1822
            this->ExtractSharedLibraryName.match(1).c_str(),
1823
            this->ExtractSharedLibraryName.match(2).c_str(),
1824
            this->ExtractSharedLibraryName.match(3).c_str());
1825
#endif
1826
    // Set the link type to shared.
1827
0
    this->SetCurrentLinkType(LinkShared);
1828
1829
    // Use just the library name so the linker will search.
1830
0
    lib = this->ExtractSharedLibraryName.match(2);
1831
0
  } else if (this->ExtractStaticLibraryName.find(item.Value)) {
1832
// This matches a static library file name.
1833
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1834
    fprintf(stderr, "static regex matched [%s] [%s] [%s]\n",
1835
            this->ExtractStaticLibraryName.match(1).c_str(),
1836
            this->ExtractStaticLibraryName.match(2).c_str(),
1837
            this->ExtractStaticLibraryName.match(3).c_str());
1838
#endif
1839
    // Set the link type to static.
1840
0
    this->SetCurrentLinkType(LinkStatic);
1841
1842
    // Use just the library name so the linker will search.
1843
0
    lib = this->ExtractStaticLibraryName.match(2);
1844
0
  } else if (this->ExtractAnyLibraryName.find(item.Value)) {
1845
// This matches a library file name.
1846
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1847
    fprintf(stderr, "any regex matched [%s] [%s] [%s]\n",
1848
            this->ExtractAnyLibraryName.match(1).c_str(),
1849
            this->ExtractAnyLibraryName.match(2).c_str(),
1850
            this->ExtractAnyLibraryName.match(3).c_str());
1851
#endif
1852
    // Restore the target link type since this item does not specify
1853
    // one.
1854
0
    this->SetCurrentLinkType(this->StartLinkType);
1855
1856
    // Use just the library name so the linker will search.
1857
0
    lib = this->ExtractAnyLibraryName.match(2);
1858
0
  } else {
1859
    // We must ask the linker to search for a library with this name.
1860
    // Restore the target link type since this item does not specify
1861
    // one.
1862
0
    this->SetCurrentLinkType(this->StartLinkType);
1863
0
    lib = item.Value;
1864
0
  }
1865
1866
  // Create an option to ask the linker to search for the library.
1867
0
  auto out = cmStrCat(this->LibLinkFlag, lib, this->LibLinkSuffix);
1868
1869
0
  if (entry.Feature != DEFAULT) {
1870
0
    auto const& feature = this->GetLibraryFeature(entry.Feature);
1871
0
    this->Items.emplace_back(
1872
0
      BT<std::string>(
1873
0
        feature.GetDecoratedItem(cmStrCat(lib, this->LibLinkSuffix),
1874
0
                                 item.Value, out, ItemIsPath::No),
1875
0
        item.Backtrace),
1876
0
      ItemIsPath::No);
1877
0
  } else {
1878
0
    this->Items.emplace_back(BT<std::string>(out, item.Backtrace),
1879
0
                             ItemIsPath::No);
1880
0
  }
1881
1882
  // Here we could try to find the library the linker will find and
1883
  // add a runtime information entry for it.  It would probably not be
1884
  // reliable and we want to encourage use of full paths for library
1885
  // specification.
1886
0
}
1887
1888
void cmComputeLinkInformation::AddFrameworkItem(LinkEntry const& entry)
1889
0
{
1890
0
  std::string const& item = entry.Item.Value;
1891
1892
  // Try to separate the framework name and path.
1893
0
  auto fwDescriptor = this->GlobalGenerator->SplitFrameworkPath(
1894
0
    item,
1895
0
    entry.Feature == DEFAULT ? cmGlobalGenerator::FrameworkFormat::Relaxed
1896
0
                             : cmGlobalGenerator::FrameworkFormat::Extended);
1897
0
  if (!fwDescriptor) {
1898
0
    std::ostringstream e;
1899
0
    e << "Could not parse framework path \"" << item << "\" linked by target "
1900
0
      << this->Target->GetName() << '.';
1901
0
    cmSystemTools::Error(e.str());
1902
0
    return;
1903
0
  }
1904
1905
0
  std::string const& fw_path = fwDescriptor->Directory;
1906
0
  if (!fw_path.empty()) {
1907
    // Add the directory portion to the framework search path.
1908
0
    this->AddFrameworkPath(fw_path);
1909
0
  }
1910
1911
  // add runtime information
1912
0
  this->AddLibraryRuntimeInfo(fwDescriptor->GetFullPath());
1913
1914
0
  if (entry.Feature == DEFAULT) {
1915
    // ensure FRAMEWORK feature is loaded
1916
0
    this->AddLibraryFeature("FRAMEWORK");
1917
0
  }
1918
1919
0
  if (this->GlobalGenerator->IsXcode()) {
1920
    // Add framework path - it will be handled by Xcode after it's added to
1921
    // "Link Binary With Libraries" build phase
1922
0
    this->Items.emplace_back(item, ItemIsPath::Yes, nullptr, nullptr,
1923
0
                             this->FindLibraryFeature(entry.Feature == DEFAULT
1924
0
                                                        ? "FRAMEWORK"
1925
0
                                                        : entry.Feature));
1926
0
  } else {
1927
0
    this->Items.emplace_back(
1928
0
      fwDescriptor->GetLinkName(), ItemIsPath::Yes, nullptr, nullptr,
1929
0
      this->FindLibraryFeature(entry.Feature == DEFAULT ? "FRAMEWORK"
1930
0
                                                        : entry.Feature));
1931
0
  }
1932
0
}
1933
1934
void cmComputeLinkInformation::AddXcFrameworkItem(LinkEntry const& entry)
1935
0
{
1936
0
  auto plist = cmParseXcFrameworkPlist(entry.Item.Value, *this->Makefile,
1937
0
                                       entry.Item.Backtrace);
1938
0
  if (!plist) {
1939
0
    return;
1940
0
  }
1941
1942
0
  if (auto const* lib =
1943
0
        plist->SelectSuitableLibrary(*this->Makefile, entry.Item.Backtrace)) {
1944
0
    if (this->GlobalGenerator->IsXcode()) {
1945
0
      this->Items.emplace_back(
1946
0
        entry.Item.Value, ItemIsPath::Yes, nullptr, nullptr,
1947
0
        this->FindLibraryFeature(entry.Feature == DEFAULT
1948
0
                                   ? "__CMAKE_LINK_XCFRAMEWORK"
1949
0
                                   : entry.Feature));
1950
0
    } else {
1951
0
      auto libraryPath = cmStrCat(
1952
0
        entry.Item.Value, '/', lib->LibraryIdentifier, '/', lib->LibraryPath);
1953
0
      LinkEntry libraryEntry(
1954
0
        BT<std::string>(libraryPath, entry.Item.Backtrace), entry.Target);
1955
1956
0
      if (cmSystemTools::IsPathToFramework(libraryPath) &&
1957
0
          this->Target->IsApple()) {
1958
        // This is a framework.
1959
0
        this->AddFrameworkItem(libraryEntry);
1960
0
      } else {
1961
0
        this->Depends.push_back(libraryPath);
1962
0
        this->AddFullItem(libraryEntry);
1963
0
        this->AddLibraryRuntimeInfo(libraryPath);
1964
0
        if (!lib->HeadersPath.empty()) {
1965
0
          this->AddXcFrameworkHeaderPath(cmStrCat(entry.Item.Value, '/',
1966
0
                                                  lib->LibraryIdentifier, '/',
1967
0
                                                  lib->HeadersPath));
1968
0
        }
1969
0
      }
1970
0
    }
1971
0
  }
1972
0
}
1973
1974
void cmComputeLinkInformation::DropDirectoryItem(BT<std::string> const& item)
1975
0
{
1976
  // A full path to a directory was found as a link item.  Warn the
1977
  // user.
1978
0
  this->CMakeInstance->IssueMessage(
1979
0
    MessageType::WARNING,
1980
0
    cmStrCat("Target \"", this->Target->GetName(),
1981
0
             "\" requests linking to directory \"", item.Value,
1982
0
             "\".  Targets may link only to libraries.  CMake is dropping "
1983
0
             "the item."),
1984
0
    item.Backtrace);
1985
0
}
1986
1987
void cmComputeLinkInformation::ComputeFrameworkInfo()
1988
0
{
1989
  // Avoid adding implicit framework paths.
1990
0
  cmList implicitDirs;
1991
1992
  // Get platform-wide implicit directories.
1993
0
  implicitDirs.assign(this->Makefile->GetDefinition(
1994
0
    "CMAKE_PLATFORM_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES"));
1995
1996
  // Get language-specific implicit directories.
1997
0
  std::string implicitDirVar = cmStrCat(
1998
0
    "CMAKE_", this->LinkLanguage, "_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES");
1999
0
  implicitDirs.append(this->Makefile->GetDefinition(implicitDirVar));
2000
2001
0
  this->FrameworkPathsEmitted.insert(implicitDirs.begin(), implicitDirs.end());
2002
0
}
2003
2004
void cmComputeLinkInformation::AddFrameworkPath(std::string const& p)
2005
0
{
2006
0
  if (this->FrameworkPathsEmitted.insert(p).second) {
2007
0
    this->FrameworkPaths.push_back(p);
2008
0
  }
2009
0
}
2010
2011
void cmComputeLinkInformation::AddXcFrameworkHeaderPath(std::string const& p)
2012
0
{
2013
0
  this->XcFrameworkHeaderPaths.push_back(p);
2014
0
}
2015
2016
bool cmComputeLinkInformation::CheckSharedLibNoSOName(LinkEntry const& entry)
2017
0
{
2018
  // This platform will use the path to a library as its soname if the
2019
  // library is given via path and was not built with an soname.  If
2020
  // this is a shared library that might be the case.
2021
0
  std::string file = cmSystemTools::GetFilenameName(entry.Item.Value);
2022
0
  if (this->ExtractSharedLibraryName.find(file)) {
2023
    // If we can guess the soname fairly reliably then assume the
2024
    // library has one.  Otherwise assume the library has no builtin
2025
    // soname.
2026
0
    std::string soname;
2027
0
    if (!cmSystemTools::GuessLibrarySOName(entry.Item.Value, soname)) {
2028
0
      this->AddSharedLibNoSOName(entry);
2029
0
      return true;
2030
0
    }
2031
0
  }
2032
0
  return false;
2033
0
}
2034
2035
void cmComputeLinkInformation::AddSharedLibNoSOName(LinkEntry const& entry)
2036
0
{
2037
  // We have a full path to a shared library with no soname.  We need
2038
  // to ask the linker to locate the item because otherwise the path
2039
  // we give to it will be embedded in the target linked.  Then at
2040
  // runtime the dynamic linker will search for the library using the
2041
  // path instead of just the name.
2042
0
  LinkEntry fileEntry{ entry };
2043
0
  fileEntry.Item = cmSystemTools::GetFilenameName(entry.Item.Value);
2044
0
  this->AddUserItem(fileEntry);
2045
2046
  // Make sure the link directory ordering will find the library.
2047
0
  this->OrderLinkerSearchPath->AddLinkLibrary(entry.Item.Value);
2048
0
}
2049
2050
void cmComputeLinkInformation::LoadImplicitLinkInfo()
2051
0
{
2052
  // Get platform-wide implicit directories.
2053
0
  cmList implicitDirs{ this->Makefile->GetDefinition(
2054
0
    "CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES") };
2055
2056
  // Append library architecture to all implicit platform directories
2057
  // and add them to the set
2058
0
  if (cmValue libraryArch =
2059
0
        this->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE")) {
2060
0
    for (auto const& i : implicitDirs) {
2061
0
      this->ImplicitLinkDirs.insert(cmStrCat(i, '/', *libraryArch));
2062
0
    }
2063
0
  }
2064
2065
  // Get language-specific implicit directories.
2066
0
  std::string implicitDirVar =
2067
0
    cmStrCat("CMAKE_", this->LinkLanguage, "_IMPLICIT_LINK_DIRECTORIES");
2068
0
  implicitDirs.append(this->Makefile->GetDefinition(implicitDirVar));
2069
2070
  // Store implicit link directories.
2071
0
  this->ImplicitLinkDirs.insert(implicitDirs.begin(), implicitDirs.end());
2072
2073
  // Get language-specific implicit libraries.
2074
0
  std::string implicitLibVar =
2075
0
    cmStrCat("CMAKE_", this->LinkLanguage, "_IMPLICIT_LINK_LIBRARIES");
2076
0
  cmList implicitLibs{ this->Makefile->GetDefinition(implicitLibVar) };
2077
2078
  // Store implicit link libraries.
2079
0
  for (auto const& item : implicitLibs) {
2080
    // Items starting in '-' but not '-l' are flags, not libraries,
2081
    // and should not be filtered by this implicit list.
2082
0
    if (item[0] != '-' || item[1] == 'l') {
2083
0
      this->ImplicitLinkLibs.insert(item);
2084
0
    }
2085
0
  }
2086
2087
  // Get platform specific rpath link directories
2088
0
  cmList::append(this->RuntimeLinkDirs,
2089
0
                 this->Makefile->GetDefinition("CMAKE_PLATFORM_RUNTIME_PATH"));
2090
0
}
2091
2092
std::vector<std::string> const&
2093
cmComputeLinkInformation::GetRuntimeSearchPath() const
2094
0
{
2095
0
  return this->OrderRuntimeSearchPath->GetOrderedDirectories();
2096
0
}
2097
2098
void cmComputeLinkInformation::AddLibraryRuntimeInfo(
2099
  std::string const& fullPath, cmGeneratorTarget const* target)
2100
0
{
2101
  // Ignore targets on Apple where install_name is not @rpath.
2102
  // The dependenty library can be found with other means such as
2103
  // @loader_path or full paths.
2104
0
  if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
2105
0
    if (!target->HasMacOSXRpathInstallNameDir(this->Config)) {
2106
0
      return;
2107
0
    }
2108
0
  }
2109
2110
  // Libraries with unknown type must be handled using just the file
2111
  // on disk.
2112
0
  if (target->GetType() == cmStateEnums::UNKNOWN_LIBRARY) {
2113
0
    this->AddLibraryRuntimeInfo(fullPath);
2114
0
    return;
2115
0
  }
2116
2117
  // Skip targets that are not shared libraries (modules cannot be linked).
2118
0
  if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
2119
0
    return;
2120
0
  }
2121
2122
  // Skip targets that do not have a known runtime artifact.
2123
0
  if (!target->HasKnownRuntimeArtifactLocation(this->Config)) {
2124
0
    return;
2125
0
  }
2126
2127
  // Try to get the soname of the library.  Only files with this name
2128
  // could possibly conflict.
2129
0
  std::string soName = target->GetSOName(this->Config);
2130
0
  char const* soname = soName.empty() ? nullptr : soName.c_str();
2131
2132
  // Include this library in the runtime path ordering.
2133
0
  this->OrderRuntimeSearchPath->AddRuntimeLibrary(fullPath, soname);
2134
0
  if (this->LinkWithRuntimePath) {
2135
0
    this->OrderLinkerSearchPath->AddRuntimeLibrary(fullPath, soname);
2136
0
  }
2137
0
}
2138
2139
void cmComputeLinkInformation::AddLibraryRuntimeInfo(
2140
  std::string const& fullPath)
2141
0
{
2142
  // Get the name of the library from the file name.
2143
0
  bool is_shared_library = false;
2144
0
  std::string file = cmSystemTools::GetFilenameName(fullPath);
2145
2146
0
  if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
2147
    // Check that @rpath is part of the install name.
2148
    // If it isn't, return.
2149
0
    std::string soname;
2150
0
    if (!cmSystemTools::GuessLibraryInstallName(fullPath, soname)) {
2151
0
      return;
2152
0
    }
2153
2154
0
    if (soname.find("@rpath") == std::string::npos) {
2155
0
      return;
2156
0
    }
2157
0
  }
2158
2159
0
  is_shared_library = this->ExtractSharedLibraryName.find(file);
2160
2161
0
  if (!is_shared_library) {
2162
    // On some platforms (AIX) a shared library may look static.
2163
0
    if (this->ArchivesMayBeShared) {
2164
0
      if (this->ExtractStaticLibraryName.find(file)) {
2165
        // This is the name of a shared library or archive.
2166
0
        is_shared_library = true;
2167
0
      }
2168
0
    }
2169
0
  }
2170
2171
  // It could be an Apple framework
2172
0
  if (!is_shared_library) {
2173
0
    is_shared_library =
2174
0
      this->GlobalGenerator
2175
0
        ->SplitFrameworkPath(fullPath,
2176
0
                             cmGlobalGenerator::FrameworkFormat::Strict)
2177
0
        .has_value();
2178
0
  }
2179
2180
0
  if (!is_shared_library) {
2181
0
    return;
2182
0
  }
2183
2184
  // Include this library in the runtime path ordering.
2185
0
  this->OrderRuntimeSearchPath->AddRuntimeLibrary(fullPath);
2186
0
  if (this->LinkWithRuntimePath) {
2187
0
    this->OrderLinkerSearchPath->AddRuntimeLibrary(fullPath);
2188
0
  }
2189
0
}
2190
2191
static void cmCLI_ExpandListUnique(std::string const& str,
2192
                                   std::vector<std::string>& out,
2193
                                   std::set<std::string>& emitted)
2194
0
{
2195
0
  cmList tmp{ str };
2196
0
  for (std::string const& i : tmp) {
2197
0
    if (emitted.insert(i).second) {
2198
0
      out.push_back(i);
2199
0
    }
2200
0
  }
2201
0
}
2202
2203
void cmComputeLinkInformation::GetRPath(std::vector<std::string>& runtimeDirs,
2204
                                        bool for_install) const
2205
0
{
2206
  // Select whether to generate runtime search directories.
2207
0
  bool outputRuntime =
2208
0
    !this->Makefile->IsOn("CMAKE_SKIP_RPATH") && !this->RuntimeFlag.empty();
2209
2210
  // Select whether to generate an rpath for the install tree or the
2211
  // build tree.
2212
0
  bool linking_for_install =
2213
0
    (for_install ||
2214
0
     this->Target->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"));
2215
0
  bool use_install_rpath =
2216
0
    (outputRuntime && this->Target->HaveInstallTreeRPATH(this->Config) &&
2217
0
     linking_for_install);
2218
0
  bool use_build_rpath =
2219
0
    (outputRuntime && this->Target->HaveBuildTreeRPATH(this->Config) &&
2220
0
     !linking_for_install);
2221
0
  bool use_link_rpath = outputRuntime && linking_for_install &&
2222
0
    !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH") &&
2223
0
    this->Target->GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
2224
2225
  // Select whether to use $ORIGIN in RPATHs for artifacts in the build tree.
2226
0
  std::string const& originToken = this->Makefile->GetSafeDefinition(
2227
0
    "CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN");
2228
0
  std::string targetOutputDir = this->Target->GetDirectory(this->Config);
2229
0
  bool use_relative_build_rpath =
2230
0
    this->Target->GetPropertyAsBool("BUILD_RPATH_USE_ORIGIN") &&
2231
0
    !originToken.empty() && !targetOutputDir.empty();
2232
2233
  // Construct the RPATH.
2234
0
  std::set<std::string> emitted;
2235
0
  if (use_install_rpath) {
2236
0
    std::string install_rpath;
2237
0
    this->Target->GetInstallRPATH(this->Config, install_rpath);
2238
0
    cmCLI_ExpandListUnique(install_rpath, runtimeDirs, emitted);
2239
0
  }
2240
0
  if (use_build_rpath) {
2241
    // Add directories explicitly specified by user
2242
0
    std::string build_rpath;
2243
0
    if (this->Target->GetBuildRPATH(this->Config, build_rpath)) {
2244
      // This will not resolve entries to use $ORIGIN, the user is expected
2245
      // to do that if necessary.
2246
0
      cmCLI_ExpandListUnique(build_rpath, runtimeDirs, emitted);
2247
0
    }
2248
0
  }
2249
0
  if (use_build_rpath || use_link_rpath) {
2250
0
    std::string rootPath;
2251
0
    if (cmValue sysrootLink =
2252
0
          this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
2253
0
      rootPath = *sysrootLink;
2254
0
    } else {
2255
0
      rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
2256
0
    }
2257
0
    cmValue stagePath = this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
2258
0
    std::string const& installPrefix =
2259
0
      this->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
2260
0
    cmSystemTools::ConvertToUnixSlashes(rootPath);
2261
0
    std::vector<std::string> const& rdirs = this->GetRuntimeSearchPath();
2262
0
    std::string const& topBinaryDir =
2263
0
      this->CMakeInstance->GetHomeOutputDirectory();
2264
0
    for (std::string const& ri : rdirs) {
2265
      // Put this directory in the rpath if using build-tree rpath
2266
      // support or if using the link path as an rpath.
2267
0
      if (use_build_rpath) {
2268
0
        std::string d = ri;
2269
0
        if (!rootPath.empty() && cmHasPrefix(d, rootPath)) {
2270
0
          d.erase(0, rootPath.size());
2271
0
        } else if (cmNonempty(stagePath) && cmHasPrefix(d, *stagePath)) {
2272
0
          d.erase(0, (*stagePath).size());
2273
0
          d = cmStrCat(installPrefix, '/', d);
2274
0
          cmSystemTools::ConvertToUnixSlashes(d);
2275
0
        } else if (use_relative_build_rpath) {
2276
          // If expansion of the $ORIGIN token is supported and permitted per
2277
          // policy, use relative paths in the RPATH.
2278
0
          if (cmSystemTools::ComparePath(d, topBinaryDir) ||
2279
0
              cmSystemTools::IsSubDirectory(d, topBinaryDir)) {
2280
0
            d = cmSystemTools::RelativePath(targetOutputDir, d);
2281
0
            if (!d.empty()) {
2282
0
              d = cmStrCat(originToken, '/', d);
2283
0
            } else {
2284
0
              d = originToken;
2285
0
            }
2286
0
          }
2287
0
        }
2288
0
        if (emitted.insert(d).second) {
2289
0
          runtimeDirs.push_back(std::move(d));
2290
0
        }
2291
0
      } else if (use_link_rpath) {
2292
        // Do not add any path inside the source or build tree.
2293
0
        std::string const& topSourceDir =
2294
0
          this->CMakeInstance->GetHomeDirectory();
2295
0
        if (!cmSystemTools::ComparePath(ri, topSourceDir) &&
2296
0
            !cmSystemTools::ComparePath(ri, topBinaryDir) &&
2297
0
            !cmSystemTools::IsSubDirectory(ri, topSourceDir) &&
2298
0
            !cmSystemTools::IsSubDirectory(ri, topBinaryDir)) {
2299
0
          std::string d = ri;
2300
0
          if (!rootPath.empty() && cmHasPrefix(d, rootPath)) {
2301
0
            d.erase(0, rootPath.size());
2302
0
          } else if (cmNonempty(stagePath) && cmHasPrefix(d, *stagePath)) {
2303
0
            d.erase(0, (*stagePath).size());
2304
0
            d = cmStrCat(installPrefix, '/', d);
2305
0
            cmSystemTools::ConvertToUnixSlashes(d);
2306
0
          }
2307
0
          if (emitted.insert(d).second) {
2308
0
            runtimeDirs.push_back(std::move(d));
2309
0
          }
2310
0
        }
2311
0
      }
2312
0
    }
2313
0
  }
2314
2315
  // Add runtime paths required by the toolchains' runtime libraries.
2316
  // This is done even when skipping RPATH entries modeled by CMake.
2317
  // However, INSTALL_REMOVE_ENVIRONMENT_RPATH tells us to exclude
2318
  // toolchain-defined RPATH entries from installation.
2319
0
  bool const removeEnvironmentRPath = for_install &&
2320
0
    this->Target->GetPropertyAsBool("INSTALL_REMOVE_ENVIRONMENT_RPATH");
2321
0
  if (!removeEnvironmentRPath) {
2322
0
    cmGeneratorTarget::LinkClosure const* lc =
2323
0
      this->Target->GetLinkClosure(this->Config);
2324
0
    for (std::string const& li : lc->Languages) {
2325
0
      std::string useVar = cmStrCat(
2326
0
        "CMAKE_", li, "_USE_IMPLICIT_LINK_DIRECTORIES_IN_RUNTIME_PATH");
2327
0
      if (this->Makefile->IsOn(useVar)) {
2328
0
        std::string dirVar =
2329
0
          cmStrCat("CMAKE_", li, "_IMPLICIT_LINK_DIRECTORIES");
2330
0
        if (cmValue dirs = this->Makefile->GetDefinition(dirVar)) {
2331
0
          cmCLI_ExpandListUnique(*dirs, runtimeDirs, emitted);
2332
0
        }
2333
0
      }
2334
0
    }
2335
0
  }
2336
2337
  // Add runtime paths required by the platform to always be present.
2338
  // This is done even when skipping RPATH entries modeled by CMake.
2339
0
  cmCLI_ExpandListUnique(this->RuntimeAlways, runtimeDirs, emitted);
2340
0
}
2341
2342
std::string cmComputeLinkInformation::GetRPathString(bool for_install) const
2343
0
{
2344
  // Get the directories to use.
2345
0
  std::vector<std::string> runtimeDirs;
2346
0
  this->GetRPath(runtimeDirs, for_install);
2347
2348
  // Concatenate the paths.
2349
0
  std::string rpath = cmJoin(runtimeDirs, this->GetRuntimeSep());
2350
2351
  // If the rpath will be replaced at install time, prepare space.
2352
0
  if (!for_install && this->RuntimeUseChrpath) {
2353
0
    if (!rpath.empty()) {
2354
      // Add one trailing separator so the linker does not reuse the
2355
      // rpath .dynstr entry for a symbol name that happens to match
2356
      // the end of the rpath string.
2357
0
      rpath += this->GetRuntimeSep();
2358
0
    }
2359
2360
    // Make sure it is long enough to hold the replacement value.
2361
0
    std::string::size_type minLength = this->GetChrpathString().length();
2362
0
    while (rpath.length() < minLength) {
2363
0
      rpath += this->GetRuntimeSep();
2364
0
    }
2365
0
  }
2366
2367
0
  return rpath;
2368
0
}
2369
2370
std::string cmComputeLinkInformation::GetChrpathString() const
2371
0
{
2372
0
  if (!this->RuntimeUseChrpath) {
2373
0
    return "";
2374
0
  }
2375
2376
0
  return this->GetRPathString(true);
2377
0
}