Coverage Report

Created: 2026-03-12 06:35

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
void cmComputeLinkInformation::AddImplicitLinkInfo(std::string const& lang)
1088
0
{
1089
  // Add libraries for this language that are not implied by the
1090
  // linker language.
1091
0
  std::string libVar = cmStrCat("CMAKE_", lang, "_IMPLICIT_LINK_LIBRARIES");
1092
0
  if (cmValue libs = this->Makefile->GetDefinition(libVar)) {
1093
0
    cmList libsList{ *libs };
1094
0
    for (auto const& i : libsList) {
1095
0
      if (!cm::contains(this->ImplicitLinkLibs, i)) {
1096
0
        this->AddItem({ i });
1097
0
      }
1098
0
    }
1099
0
  }
1100
1101
  // Add linker search paths for this language that are not
1102
  // implied by the linker language.
1103
0
  std::string dirVar = cmStrCat("CMAKE_", lang, "_IMPLICIT_LINK_DIRECTORIES");
1104
0
  if (cmValue dirs = this->Makefile->GetDefinition(dirVar)) {
1105
0
    cmList dirsList{ *dirs };
1106
0
    this->OrderLinkerSearchPath->AddLanguageDirectories(dirsList);
1107
0
  }
1108
0
}
1109
1110
void cmComputeLinkInformation::AddItem(LinkEntry const& entry)
1111
0
{
1112
0
  cmGeneratorTarget const* tgt = entry.Target;
1113
0
  BT<std::string> const& item = entry.Item;
1114
1115
  // Compute the proper name to use to link this library.
1116
0
  std::string const& config = this->Config;
1117
0
  bool impexe = (tgt && tgt->IsExecutableWithExports());
1118
0
  if (impexe && !tgt->HasImportLibrary(config) && !this->LoaderFlag) {
1119
    // Skip linking to executables on platforms with no import
1120
    // libraries or loader flags.
1121
0
    return;
1122
0
  }
1123
1124
0
  if (tgt && tgt->IsLinkable()) {
1125
    // This is a CMake target.  Ask the target for its real name.
1126
0
    if (impexe && this->LoaderFlag) {
1127
      // This link item is an executable that may provide symbols
1128
      // used by this target.  A special flag is needed on this
1129
      // platform.  Add it now using a special feature.
1130
0
      cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(config)
1131
0
        ? cmStateEnums::ImportLibraryArtifact
1132
0
        : cmStateEnums::RuntimeBinaryArtifact;
1133
0
      std::string exe = tgt->GetFullPath(config, artifact, true);
1134
0
      this->Items.emplace_back(
1135
0
        BT<std::string>(exe, item.Backtrace), ItemIsPath::Yes, tgt, nullptr,
1136
0
        this->FindLibraryFeature(entry.Feature == DEFAULT
1137
0
                                   ? "__CMAKE_LINK_EXECUTABLE"
1138
0
                                   : entry.Feature));
1139
0
      this->Depends.push_back(std::move(exe));
1140
0
    } else if (tgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
1141
      // Add the interface library as an item so it can be considered as part
1142
      // of COMPATIBLE_INTERFACE_ enforcement.  The generators will ignore
1143
      // this for the actual link line.
1144
0
      this->Items.emplace_back(std::string(), ItemIsPath::No, tgt);
1145
1146
      // Also add the item the interface specifies to be used in its place.
1147
0
      std::string const& libName = tgt->GetImportedLibName(config);
1148
0
      if (!libName.empty()) {
1149
0
        this->AddItem(BT<std::string>(libName, item.Backtrace));
1150
0
      }
1151
0
    } else if (tgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
1152
0
      this->Items.emplace_back(item, ItemIsPath::No, tgt);
1153
0
    } else if (this->GlobalGenerator->IsXcode() &&
1154
0
               !tgt->GetImportedXcFrameworkPath(config).empty()) {
1155
0
      this->Items.emplace_back(
1156
0
        tgt->GetImportedXcFrameworkPath(config), ItemIsPath::Yes, tgt, nullptr,
1157
0
        this->FindLibraryFeature(entry.Feature == DEFAULT
1158
0
                                   ? "__CMAKE_LINK_XCFRAMEWORK"
1159
0
                                   : entry.Feature));
1160
0
    } else {
1161
      // Decide whether to use an import library.
1162
0
      cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(config)
1163
0
        ? cmStateEnums::ImportLibraryArtifact
1164
0
        : cmStateEnums::RuntimeBinaryArtifact;
1165
1166
      // Pass the full path to the target file.
1167
0
      BT<std::string> lib = BT<std::string>(
1168
0
        tgt->GetFullPath(config, artifact, true), item.Backtrace);
1169
0
      if (tgt->IsAIX() && cmHasLiteralSuffix(lib.Value, "-NOTFOUND") &&
1170
0
          artifact == cmStateEnums::ImportLibraryArtifact) {
1171
        // This is an imported executable on AIX that has ENABLE_EXPORTS
1172
        // but not IMPORTED_IMPLIB.  CMake used to produce and accept such
1173
        // imported executables on AIX before we taught it to use linker
1174
        // import files.  For compatibility, simply skip linking to this
1175
        // executable as we did before.  It works with runtime linking.
1176
0
        return;
1177
0
      }
1178
0
      if (!this->LinkDependsNoShared ||
1179
0
          tgt->GetType() != cmStateEnums::SHARED_LIBRARY) {
1180
0
        this->Depends.push_back(lib.Value);
1181
0
      }
1182
1183
0
      LinkEntry libEntry{ entry };
1184
0
      libEntry.Item = lib;
1185
0
      this->AddTargetItem(libEntry);
1186
0
      if (tgt->IsApple() && tgt->HasImportLibrary(config)) {
1187
        // Use the library rather than the tbd file for runpath computation
1188
0
        this->AddLibraryRuntimeInfo(
1189
0
          tgt->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact, true),
1190
0
          tgt);
1191
0
      } else {
1192
0
        this->AddLibraryRuntimeInfo(lib.Value, tgt);
1193
0
      }
1194
0
      if (tgt && tgt->GetType() == cmStateEnums::SHARED_LIBRARY &&
1195
0
          this->Target->IsDLLPlatform()) {
1196
0
        this->AddRuntimeDLL(tgt);
1197
0
      }
1198
0
    }
1199
1200
0
    auto xcFrameworkPath = tgt->GetImportedXcFrameworkPath(config);
1201
0
    if (!xcFrameworkPath.empty()) {
1202
0
      auto plist = cmParseXcFrameworkPlist(xcFrameworkPath, *this->Makefile,
1203
0
                                           item.Backtrace);
1204
0
      if (!plist) {
1205
0
        return;
1206
0
      }
1207
0
      if (auto const* library =
1208
0
            plist->SelectSuitableLibrary(*this->Makefile, item.Backtrace)) {
1209
0
        if (!library->HeadersPath.empty()) {
1210
0
          this->AddXcFrameworkHeaderPath(cmStrCat(xcFrameworkPath, '/',
1211
0
                                                  library->LibraryIdentifier,
1212
0
                                                  '/', library->HeadersPath));
1213
0
        }
1214
0
      } else {
1215
0
        return;
1216
0
      }
1217
0
    }
1218
0
  } else {
1219
    // This is not a CMake target.  Use the name given.
1220
0
    if (cmHasSuffix(entry.Feature, "FRAMEWORK"_s) ||
1221
0
        (entry.Feature == DEFAULT &&
1222
0
         cmSystemTools::IsPathToFramework(item.Value) &&
1223
0
         this->Target->IsApple())) {
1224
      // This is a framework.
1225
0
      this->AddFrameworkItem(entry);
1226
0
    } else if (cmHasSuffix(entry.Feature, "XCFRAMEWORK"_s) ||
1227
0
               (entry.Feature == DEFAULT &&
1228
0
                cmSystemTools::IsPathToXcFramework(item.Value) &&
1229
0
                this->Target->IsApple())) {
1230
      // This is a framework.
1231
0
      this->AddXcFrameworkItem(entry);
1232
0
    } else if (cmSystemTools::FileIsFullPath(item.Value)) {
1233
0
      if (cmSystemTools::FileIsDirectory(item.Value)) {
1234
        // This is a directory.
1235
0
        this->DropDirectoryItem(item);
1236
0
      } else {
1237
        // Use the full path given to the library file.
1238
0
        this->Depends.push_back(item.Value);
1239
0
        this->AddFullItem(entry);
1240
0
        this->AddLibraryRuntimeInfo(item.Value);
1241
0
      }
1242
0
    } else if (entry.Kind != cmComputeLinkDepends::LinkEntry::Object) {
1243
      // This is a library or option specified by the user.
1244
0
      this->AddUserItem(entry);
1245
0
    }
1246
0
  }
1247
0
}
1248
1249
void cmComputeLinkInformation::AddSharedDepItem(LinkEntry const& entry)
1250
0
{
1251
0
  BT<std::string> const& item = entry.Item;
1252
0
  cmGeneratorTarget const* tgt = entry.Target;
1253
1254
  // Record dependencies on DLLs.
1255
0
  if (tgt && tgt->GetType() == cmStateEnums::SHARED_LIBRARY &&
1256
0
      this->Target->IsDLLPlatform() &&
1257
0
      this->SharedDependencyMode != SharedDepModeLink) {
1258
0
    this->AddRuntimeDLL(tgt);
1259
0
  }
1260
1261
  // If dropping shared library dependencies, ignore them.
1262
0
  if (this->SharedDependencyMode == SharedDepModeNone) {
1263
0
    return;
1264
0
  }
1265
1266
  // The user may have incorrectly named an item.  Skip items that are
1267
  // not full paths to shared libraries.
1268
0
  if (tgt) {
1269
    // The target will provide a full path.  Make sure it is a shared
1270
    // library.
1271
0
    if (tgt->GetType() != cmStateEnums::SHARED_LIBRARY) {
1272
0
      return;
1273
0
    }
1274
0
  } else {
1275
    // Skip items that are not full paths.  We will not be able to
1276
    // reliably specify them.
1277
0
    if (!cmSystemTools::FileIsFullPath(item.Value)) {
1278
0
      return;
1279
0
    }
1280
1281
    // Get the name of the library from the file name.
1282
0
    std::string file = cmSystemTools::GetFilenameName(item.Value);
1283
0
    if (!this->ExtractSharedLibraryName.find(file)) {
1284
      // This is not the name of a shared library.
1285
0
      return;
1286
0
    }
1287
0
  }
1288
1289
  // If in linking mode, just link to the shared library.
1290
0
  if (this->SharedDependencyMode == SharedDepModeLink ||
1291
      // For an imported shared library without a known runtime artifact,
1292
      // such as a CUDA stub, a library file named with the real soname
1293
      // may not be available at all, so '-rpath-link' cannot help linkers
1294
      // find it to satisfy '--no-allow-shlib-undefined' recursively.
1295
      // Pass this dependency to the linker explicitly just in case.
1296
      // If the linker also uses '--as-needed' behavior, this will not
1297
      // add an unnecessary direct dependency.
1298
0
      (tgt && tgt->IsImported() &&
1299
0
       !tgt->HasKnownRuntimeArtifactLocation(this->Config) &&
1300
0
       this->Target->LinkerEnforcesNoAllowShLibUndefined(this->Config))) {
1301
0
    this->AddItem(entry);
1302
0
    return;
1303
0
  }
1304
1305
  // Get a full path to the dependent shared library.
1306
  // Add it to the runtime path computation so that the target being
1307
  // linked will be able to find it.
1308
0
  std::string lib;
1309
0
  if (tgt) {
1310
0
    cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(this->Config)
1311
0
      ? cmStateEnums::ImportLibraryArtifact
1312
0
      : cmStateEnums::RuntimeBinaryArtifact;
1313
0
    lib = tgt->GetFullPath(this->Config, artifact);
1314
0
    if (tgt->IsApple() && tgt->HasImportLibrary(this->Config)) {
1315
      // Use the library rather than the tbd file for runpath computation
1316
0
      this->AddLibraryRuntimeInfo(
1317
0
        tgt->GetFullPath(this->Config, cmStateEnums::RuntimeBinaryArtifact,
1318
0
                         true),
1319
0
        tgt);
1320
0
    } else {
1321
0
      this->AddLibraryRuntimeInfo(lib, tgt);
1322
0
    }
1323
0
  } else {
1324
0
    lib = item.Value;
1325
0
    this->AddLibraryRuntimeInfo(lib);
1326
0
  }
1327
1328
  // Check if we need to include the dependent shared library in other
1329
  // path ordering.
1330
0
  cmOrderDirectories* order = nullptr;
1331
0
  if (this->SharedDependencyMode == SharedDepModeLibDir &&
1332
0
      !this->LinkWithRuntimePath /* AddLibraryRuntimeInfo adds it */) {
1333
    // Add the item to the linker search path.
1334
0
    order = this->OrderLinkerSearchPath.get();
1335
0
  } else if (this->SharedDependencyMode == SharedDepModeDir) {
1336
    // Add the item to the separate dependent library search path.
1337
0
    order = this->OrderDependentRPath.get();
1338
0
  }
1339
0
  if (order) {
1340
0
    if (tgt) {
1341
0
      std::string soName = tgt->GetSOName(this->Config);
1342
0
      char const* soname = soName.empty() ? nullptr : soName.c_str();
1343
0
      order->AddRuntimeLibrary(lib, soname);
1344
0
    } else {
1345
0
      order->AddRuntimeLibrary(lib);
1346
0
    }
1347
0
  }
1348
0
}
1349
1350
void cmComputeLinkInformation::AddRuntimeDLL(cmGeneratorTarget const* tgt)
1351
0
{
1352
0
  if (std::find(this->RuntimeDLLs.begin(), this->RuntimeDLLs.end(), tgt) ==
1353
0
      this->RuntimeDLLs.end()) {
1354
0
    this->RuntimeDLLs.emplace_back(tgt);
1355
0
  }
1356
0
}
1357
1358
void cmComputeLinkInformation::ComputeLinkTypeInfo()
1359
0
{
1360
  // Check whether archives may actually be shared libraries.
1361
0
  this->ArchivesMayBeShared =
1362
0
    this->CMakeInstance->GetState()->GetGlobalPropertyAsBool(
1363
0
      "TARGET_ARCHIVES_MAY_BE_SHARED_LIBS");
1364
1365
  // First assume we cannot do link type stuff.
1366
0
  this->LinkTypeEnabled = false;
1367
1368
  // Lookup link type selection flags.
1369
0
  cmValue static_link_type_flag = nullptr;
1370
0
  cmValue shared_link_type_flag = nullptr;
1371
0
  char const* target_type_str = nullptr;
1372
0
  switch (this->Target->GetType()) {
1373
0
    case cmStateEnums::EXECUTABLE:
1374
0
      target_type_str = "EXE";
1375
0
      break;
1376
0
    case cmStateEnums::SHARED_LIBRARY:
1377
0
      target_type_str = "SHARED_LIBRARY";
1378
0
      break;
1379
0
    case cmStateEnums::MODULE_LIBRARY:
1380
0
      target_type_str = "SHARED_MODULE";
1381
0
      break;
1382
0
    default:
1383
0
      break;
1384
0
  }
1385
0
  if (target_type_str) {
1386
0
    std::string static_link_type_flag_var =
1387
0
      cmStrCat("CMAKE_", target_type_str, "_LINK_STATIC_", this->LinkLanguage,
1388
0
               "_FLAGS");
1389
0
    static_link_type_flag =
1390
0
      this->Makefile->GetDefinition(static_link_type_flag_var);
1391
1392
0
    std::string shared_link_type_flag_var =
1393
0
      cmStrCat("CMAKE_", target_type_str, "_LINK_DYNAMIC_", this->LinkLanguage,
1394
0
               "_FLAGS");
1395
0
    shared_link_type_flag =
1396
0
      this->Makefile->GetDefinition(shared_link_type_flag_var);
1397
0
  }
1398
1399
  // We can support link type switching only if all needed flags are
1400
  // known.
1401
0
  if (cmNonempty(static_link_type_flag) && cmNonempty(shared_link_type_flag)) {
1402
0
    this->LinkTypeEnabled = true;
1403
0
    this->StaticLinkTypeFlag = *static_link_type_flag;
1404
0
    this->SharedLinkTypeFlag = *shared_link_type_flag;
1405
0
  }
1406
1407
  // Lookup the starting link type from the target (linked statically?).
1408
0
  cmValue lss = this->Target->GetProperty("LINK_SEARCH_START_STATIC");
1409
0
  this->StartLinkType = lss.IsOn() ? LinkStatic : LinkShared;
1410
0
  this->CurrentLinkType = this->StartLinkType;
1411
0
}
1412
1413
void cmComputeLinkInformation::ComputeItemParserInfo()
1414
0
{
1415
  // Get possible library name prefixes.
1416
0
  cmMakefile* mf = this->Makefile;
1417
0
  this->AddLinkPrefix(mf->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
1418
0
  this->AddLinkPrefix(mf->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX"));
1419
1420
  // Import library names should be matched and treated as shared
1421
  // libraries for the purposes of linking.
1422
0
  this->AddLinkExtension(mf->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
1423
0
                         LinkShared);
1424
0
  this->AddLinkExtension(mf->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
1425
0
                         LinkStatic);
1426
0
  this->AddLinkExtension(mf->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
1427
0
                         LinkShared);
1428
0
  this->AddLinkExtension(mf->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX"),
1429
0
                         LinkUnknown);
1430
0
  if (cmValue linkSuffixes =
1431
0
        mf->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS")) {
1432
0
    cmList linkSuffixList{ *linkSuffixes };
1433
0
    for (auto const& i : linkSuffixList) {
1434
0
      this->AddLinkExtension(i, LinkUnknown);
1435
0
    }
1436
0
  }
1437
0
  if (cmValue sharedSuffixes =
1438
0
        mf->GetDefinition("CMAKE_EXTRA_SHARED_LIBRARY_SUFFIXES")) {
1439
0
    cmList sharedSuffixList{ *sharedSuffixes };
1440
0
    for (std::string const& i : sharedSuffixList) {
1441
0
      this->AddLinkExtension(i, LinkShared);
1442
0
    }
1443
0
  }
1444
1445
  // Compute a regex to match link extensions.
1446
0
  std::string libext =
1447
0
    this->CreateExtensionRegex(this->LinkExtensions, LinkUnknown);
1448
1449
  // Create regex to remove any library extension.
1450
0
  std::string reg("(.*)");
1451
0
  reg += libext;
1452
0
  this->OrderLinkerSearchPath->SetLinkExtensionInfo(this->LinkExtensions, reg);
1453
1454
  // Create a regex to match a library name.  Match index 1 will be
1455
  // the prefix if it exists and empty otherwise.  Match index 2 will
1456
  // be the library name.  Match index 3 will be the library
1457
  // extension.
1458
0
  reg = "^(";
1459
0
  for (std::string const& p : this->LinkPrefixes) {
1460
0
    reg += p;
1461
0
    reg += '|';
1462
0
  }
1463
0
  reg += ")([^/:]*)";
1464
1465
  // Create a regex to match any library name.
1466
0
  std::string reg_any = cmStrCat(reg, libext);
1467
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1468
  fprintf(stderr, "any regex [%s]\n", reg_any.c_str());
1469
#endif
1470
0
  this->ExtractAnyLibraryName.compile(reg_any);
1471
1472
  // Create a regex to match static library names.
1473
0
  if (!this->StaticLinkExtensions.empty()) {
1474
0
    std::string reg_static = cmStrCat(
1475
0
      reg, this->CreateExtensionRegex(this->StaticLinkExtensions, LinkStatic));
1476
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1477
    fprintf(stderr, "static regex [%s]\n", reg_static.c_str());
1478
#endif
1479
0
    this->ExtractStaticLibraryName.compile(reg_static);
1480
0
  }
1481
1482
  // Create a regex to match shared library names.
1483
0
  if (!this->SharedLinkExtensions.empty()) {
1484
0
    std::string reg_shared = reg;
1485
0
    this->SharedRegexString =
1486
0
      this->CreateExtensionRegex(this->SharedLinkExtensions, LinkShared);
1487
0
    reg_shared += this->SharedRegexString;
1488
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1489
    fprintf(stderr, "shared regex [%s]\n", reg_shared.c_str());
1490
#endif
1491
0
    this->ExtractSharedLibraryName.compile(reg_shared);
1492
0
  }
1493
0
}
1494
1495
void cmComputeLinkInformation::AddLinkPrefix(std::string const& p)
1496
0
{
1497
0
  if (!p.empty()) {
1498
0
    this->LinkPrefixes.insert(p);
1499
0
  }
1500
0
}
1501
1502
void cmComputeLinkInformation::AddLinkExtension(std::string const& e,
1503
                                                LinkType type)
1504
0
{
1505
0
  if (!e.empty()) {
1506
0
    if (type == LinkStatic) {
1507
0
      this->StaticLinkExtensions.emplace_back(e);
1508
0
    }
1509
0
    if (type == LinkShared) {
1510
0
      this->SharedLinkExtensions.emplace_back(e);
1511
0
    }
1512
0
    this->LinkExtensions.emplace_back(e);
1513
0
  }
1514
0
}
1515
1516
// XXX(clang-tidy): This method's const-ness is platform dependent, so we
1517
// cannot make it `const` as `clang-tidy` wants us to.
1518
// NOLINTNEXTLINE(readability-make-member-function-const)
1519
std::string cmComputeLinkInformation::CreateExtensionRegex(
1520
  std::vector<std::string> const& exts, LinkType type)
1521
0
{
1522
  // Build a list of extension choices.
1523
0
  std::string libext = "(";
1524
0
  char const* sep = "";
1525
0
  for (std::string const& i : exts) {
1526
    // Separate this choice from the previous one.
1527
0
    libext += sep;
1528
0
    sep = "|";
1529
1530
    // Store this extension choice with the "." escaped.
1531
0
    libext += "\\";
1532
#if defined(_WIN32) && !defined(__CYGWIN__)
1533
    libext += this->NoCaseExpression(i);
1534
#else
1535
0
    libext += i;
1536
0
#endif
1537
0
  }
1538
1539
  // Finish the list.
1540
0
  libext += ')';
1541
1542
  // Add an optional OpenBSD-style version or major.minor.version component.
1543
0
  if (this->IsOpenBSD || type == LinkShared) {
1544
0
    libext += "(\\.[0-9]+)*";
1545
0
  }
1546
1547
0
  libext += '$';
1548
0
  return libext;
1549
0
}
1550
1551
std::string cmComputeLinkInformation::NoCaseExpression(std::string const& str)
1552
0
{
1553
0
  std::string ret;
1554
0
  ret.reserve(str.size() * 4);
1555
0
  for (char c : str) {
1556
0
    if (c == '.') {
1557
0
      ret += c;
1558
0
    } else {
1559
0
      ret += '[';
1560
0
      ret += static_cast<char>(cmsysString_tolower(c));
1561
0
      ret += static_cast<char>(cmsysString_toupper(c));
1562
0
      ret += ']';
1563
0
    }
1564
0
  }
1565
0
  return ret;
1566
0
}
1567
1568
void cmComputeLinkInformation::SetCurrentLinkType(LinkType lt)
1569
0
{
1570
  // If we are changing the current link type add the flag to tell the
1571
  // linker about it.
1572
0
  if (this->CurrentLinkType != lt) {
1573
0
    this->CurrentLinkType = lt;
1574
1575
0
    if (this->LinkTypeEnabled) {
1576
0
      switch (this->CurrentLinkType) {
1577
0
        case LinkStatic:
1578
0
          this->Items.emplace_back(this->StaticLinkTypeFlag, ItemIsPath::No);
1579
0
          break;
1580
0
        case LinkShared:
1581
0
          this->Items.emplace_back(this->SharedLinkTypeFlag, ItemIsPath::No);
1582
0
          break;
1583
0
        default:
1584
0
          break;
1585
0
      }
1586
0
    }
1587
0
  }
1588
0
}
1589
1590
void cmComputeLinkInformation::AddTargetItem(LinkEntry const& entry)
1591
0
{
1592
  // This is called to handle a link item that is a full path to a target.
1593
  // If the target is not a static library make sure the link type is
1594
  // shared.  This is because dynamic-mode linking can handle both
1595
  // shared and static libraries but static-mode can handle only
1596
  // static libraries.  If a previous user item changed the link type
1597
  // to static we need to make sure it is back to shared.
1598
0
  BT<std::string> const& item = entry.Item;
1599
0
  cmGeneratorTarget const* target = entry.Target;
1600
1601
0
  if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
1602
0
    this->SetCurrentLinkType(LinkShared);
1603
0
  }
1604
1605
  // Keep track of shared library targets linked.
1606
0
  if (target->GetType() == cmStateEnums::SHARED_LIBRARY) {
1607
0
    this->SharedLibrariesLinked.insert(target);
1608
0
  }
1609
1610
  // Handle case of an imported shared library with no soname.
1611
0
  if (this->NoSONameUsesPath &&
1612
0
      target->IsImportedSharedLibWithoutSOName(this->Config)) {
1613
0
    this->AddSharedLibNoSOName(entry);
1614
0
    return;
1615
0
  }
1616
1617
0
  bool const isImportedFrameworkFolderOnApple =
1618
0
    target->IsImportedFrameworkFolderOnApple(this->Config);
1619
0
  if (target->IsFrameworkOnApple() || isImportedFrameworkFolderOnApple) {
1620
    // Add the framework directory and the framework item itself
1621
0
    auto fwDescriptor = this->GlobalGenerator->SplitFrameworkPath(
1622
0
      item.Value, cmGlobalGenerator::FrameworkFormat::Extended);
1623
0
    if (!fwDescriptor) {
1624
0
      this->CMakeInstance->IssueMessage(
1625
0
        MessageType::FATAL_ERROR,
1626
0
        cmStrCat("Could not parse framework path \"", item.Value,
1627
0
                 "\" linked by target ", this->Target->GetName(), '.'),
1628
0
        item.Backtrace);
1629
0
      return;
1630
0
    }
1631
0
    if (!fwDescriptor->Directory.empty()) {
1632
      // Add the directory portion to the framework search path.
1633
0
      this->AddFrameworkPath(fwDescriptor->Directory);
1634
0
    }
1635
1636
0
    if (this->GlobalGenerator->IsXcode()) {
1637
0
      if (isImportedFrameworkFolderOnApple) {
1638
0
        if (entry.Feature == DEFAULT) {
1639
0
          this->AddLibraryFeature("FRAMEWORK");
1640
0
          this->Items.emplace_back(item, ItemIsPath::Yes, target, nullptr,
1641
0
                                   this->FindLibraryFeature("FRAMEWORK"));
1642
0
        } else {
1643
0
          this->Items.emplace_back(item, ItemIsPath::Yes, target, nullptr,
1644
0
                                   this->FindLibraryFeature(entry.Feature));
1645
0
        }
1646
0
      } else {
1647
0
        this->Items.emplace_back(
1648
0
          item, ItemIsPath::Yes, target, nullptr,
1649
0
          this->FindLibraryFeature(entry.Feature == DEFAULT
1650
0
                                     ? "__CMAKE_LINK_FRAMEWORK"
1651
0
                                     : entry.Feature));
1652
0
      }
1653
0
    } else {
1654
0
      if (cmHasSuffix(entry.Feature, "FRAMEWORK"_s)) {
1655
0
        this->Items.emplace_back(fwDescriptor->GetLinkName(), ItemIsPath::Yes,
1656
0
                                 target, nullptr,
1657
0
                                 this->FindLibraryFeature(entry.Feature));
1658
0
      } else if (entry.Feature == DEFAULT &&
1659
0
                 isImportedFrameworkFolderOnApple) {
1660
0
        this->AddLibraryFeature("FRAMEWORK");
1661
0
        this->Items.emplace_back(fwDescriptor->GetLinkName(), ItemIsPath::Yes,
1662
0
                                 target, nullptr,
1663
0
                                 this->FindLibraryFeature("FRAMEWORK"));
1664
0
      } else {
1665
0
        this->Items.emplace_back(
1666
0
          item, ItemIsPath::Yes, target, nullptr,
1667
0
          this->FindLibraryFeature(entry.Feature == DEFAULT
1668
0
                                     ? "__CMAKE_LINK_LIBRARY"
1669
0
                                     : entry.Feature));
1670
0
      }
1671
0
    }
1672
0
  } else {
1673
    // Now add the full path to the library.
1674
0
    this->Items.emplace_back(
1675
0
      item, ItemIsPath::Yes, target, nullptr,
1676
0
      this->FindLibraryFeature(
1677
0
        entry.Feature == DEFAULT ? "__CMAKE_LINK_LIBRARY" : entry.Feature));
1678
0
  }
1679
0
}
1680
1681
void cmComputeLinkInformation::AddFullItem(LinkEntry const& entry)
1682
0
{
1683
0
  BT<std::string> const& item = entry.Item;
1684
1685
  // Check for the implicit link directory special case.
1686
0
  if (this->CheckImplicitDirItem(entry)) {
1687
0
    return;
1688
0
  }
1689
1690
  // Check for case of shared library with no builtin soname.
1691
0
  if (this->NoSONameUsesPath && this->CheckSharedLibNoSOName(entry)) {
1692
0
    return;
1693
0
  }
1694
1695
  // This is called to handle a link item that is a full path.
1696
  // If the target is not a static library make sure the link type is
1697
  // shared.  This is because dynamic-mode linking can handle both
1698
  // shared and static libraries but static-mode can handle only
1699
  // static libraries.  If a previous user item changed the link type
1700
  // to static we need to make sure it is back to shared.
1701
0
  if (this->LinkTypeEnabled) {
1702
0
    std::string name = cmSystemTools::GetFilenameName(item.Value);
1703
0
    if (this->ExtractSharedLibraryName.find(name)) {
1704
0
      this->SetCurrentLinkType(LinkShared);
1705
0
    } else if (!this->ExtractStaticLibraryName.find(item.Value)) {
1706
      // We cannot determine the type.  Assume it is the target's
1707
      // default type.
1708
0
      this->SetCurrentLinkType(this->StartLinkType);
1709
0
    }
1710
0
  }
1711
1712
  // Now add the full path to the library.
1713
0
  this->Items.emplace_back(
1714
0
    item, ItemIsPath::Yes, nullptr, entry.ObjectSource,
1715
0
    this->FindLibraryFeature(
1716
0
      entry.Feature == DEFAULT
1717
0
        ? (entry.Kind == cmComputeLinkDepends::LinkEntry::Object
1718
0
             ? "__CMAKE_LINK_OBJECT"
1719
0
             : "__CMAKE_LINK_LIBRARY")
1720
0
        : entry.Feature));
1721
0
}
1722
1723
bool cmComputeLinkInformation::CheckImplicitDirItem(LinkEntry const& entry)
1724
0
{
1725
0
  BT<std::string> const& item = entry.Item;
1726
1727
  // We only switch to a pathless item if the link type may be
1728
  // enforced.  Fortunately only platforms that support link types
1729
  // seem to have magic per-architecture implicit link directories.
1730
0
  if (!this->LinkTypeEnabled) {
1731
0
    return false;
1732
0
  }
1733
1734
  // Check if this item is in an implicit link directory.
1735
0
  std::string dir = cmSystemTools::GetFilenamePath(item.Value);
1736
0
  if (!cm::contains(this->ImplicitLinkDirs, dir)) {
1737
    // Only libraries in implicit link directories are converted to
1738
    // pathless items.
1739
0
    return false;
1740
0
  }
1741
1742
  // Only apply the policy below if the library file is one that can
1743
  // be found by the linker.
1744
0
  std::string file = cmSystemTools::GetFilenameName(item.Value);
1745
0
  if (!this->ExtractAnyLibraryName.find(file)) {
1746
0
    return false;
1747
0
  }
1748
1749
0
  return false;
1750
0
}
1751
1752
void cmComputeLinkInformation::AddUserItem(LinkEntry const& entry)
1753
0
{
1754
  // This is called to handle a link item that does not match a CMake
1755
  // target and is not a full path.  We check here if it looks like a
1756
  // library file name to automatically request the proper link type
1757
  // from the linker.  For example:
1758
  //
1759
  //   foo       ==>  -lfoo
1760
  //   libfoo.a  ==>  -Wl,-Bstatic -lfoo
1761
1762
0
  cm::string_view const LINKER{ "LINKER:" };
1763
0
  BT<std::string> const& item = entry.Item;
1764
1765
0
  if (item.Value[0] == '-' || item.Value[0] == '$' || item.Value[0] == '`') {
1766
    // Pass flags through untouched.
1767
1768
    // Restore the target link type since this item does not specify
1769
    // one.
1770
0
    this->SetCurrentLinkType(this->StartLinkType);
1771
1772
    // Use the item verbatim.
1773
0
    this->Items.emplace_back(item, ItemIsPath::No);
1774
0
    return;
1775
0
  }
1776
1777
0
  if (cmHasPrefix(item.Value, LINKER)) {
1778
0
    std::vector<BT<std::string>> linkerFlag{ 1, item };
1779
0
    this->Target->ResolveLinkerWrapper(linkerFlag, this->GetLinkLanguage(),
1780
0
                                       /* joinItems = */ true);
1781
0
    if (!linkerFlag.empty()) {
1782
0
      this->Items.emplace_back(linkerFlag.front(), ItemIsPath::No);
1783
0
    }
1784
0
    return;
1785
0
  }
1786
1787
  // Parse out the prefix, base, and suffix components of the
1788
  // library name.  If the name matches that of a shared or static
1789
  // library then set the link type accordingly.
1790
  //
1791
  // Search for shared library names first because some platforms
1792
  // have shared libraries with names that match the static library
1793
  // pattern.  For example cygwin and msys use the convention
1794
  // libfoo.dll.a for import libraries and libfoo.a for static
1795
  // libraries.  On AIX a library with the name libfoo.a can be
1796
  // shared!
1797
0
  std::string lib;
1798
0
  if (this->ExtractSharedLibraryName.find(item.Value)) {
1799
// This matches a shared library file name.
1800
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1801
    fprintf(stderr, "shared regex matched [%s] [%s] [%s]\n",
1802
            this->ExtractSharedLibraryName.match(1).c_str(),
1803
            this->ExtractSharedLibraryName.match(2).c_str(),
1804
            this->ExtractSharedLibraryName.match(3).c_str());
1805
#endif
1806
    // Set the link type to shared.
1807
0
    this->SetCurrentLinkType(LinkShared);
1808
1809
    // Use just the library name so the linker will search.
1810
0
    lib = this->ExtractSharedLibraryName.match(2);
1811
0
  } else if (this->ExtractStaticLibraryName.find(item.Value)) {
1812
// This matches a static library file name.
1813
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1814
    fprintf(stderr, "static regex matched [%s] [%s] [%s]\n",
1815
            this->ExtractStaticLibraryName.match(1).c_str(),
1816
            this->ExtractStaticLibraryName.match(2).c_str(),
1817
            this->ExtractStaticLibraryName.match(3).c_str());
1818
#endif
1819
    // Set the link type to static.
1820
0
    this->SetCurrentLinkType(LinkStatic);
1821
1822
    // Use just the library name so the linker will search.
1823
0
    lib = this->ExtractStaticLibraryName.match(2);
1824
0
  } else if (this->ExtractAnyLibraryName.find(item.Value)) {
1825
// This matches a library file name.
1826
#ifdef CM_COMPUTE_LINK_INFO_DEBUG
1827
    fprintf(stderr, "any regex matched [%s] [%s] [%s]\n",
1828
            this->ExtractAnyLibraryName.match(1).c_str(),
1829
            this->ExtractAnyLibraryName.match(2).c_str(),
1830
            this->ExtractAnyLibraryName.match(3).c_str());
1831
#endif
1832
    // Restore the target link type since this item does not specify
1833
    // one.
1834
0
    this->SetCurrentLinkType(this->StartLinkType);
1835
1836
    // Use just the library name so the linker will search.
1837
0
    lib = this->ExtractAnyLibraryName.match(2);
1838
0
  } else {
1839
    // We must ask the linker to search for a library with this name.
1840
    // Restore the target link type since this item does not specify
1841
    // one.
1842
0
    this->SetCurrentLinkType(this->StartLinkType);
1843
0
    lib = item.Value;
1844
0
  }
1845
1846
  // Create an option to ask the linker to search for the library.
1847
0
  auto out = cmStrCat(this->LibLinkFlag, lib, this->LibLinkSuffix);
1848
1849
0
  if (entry.Feature != DEFAULT) {
1850
0
    auto const& feature = this->GetLibraryFeature(entry.Feature);
1851
0
    this->Items.emplace_back(
1852
0
      BT<std::string>(
1853
0
        feature.GetDecoratedItem(cmStrCat(lib, this->LibLinkSuffix),
1854
0
                                 item.Value, out, ItemIsPath::No),
1855
0
        item.Backtrace),
1856
0
      ItemIsPath::No);
1857
0
  } else {
1858
0
    this->Items.emplace_back(BT<std::string>(out, item.Backtrace),
1859
0
                             ItemIsPath::No);
1860
0
  }
1861
1862
  // Here we could try to find the library the linker will find and
1863
  // add a runtime information entry for it.  It would probably not be
1864
  // reliable and we want to encourage use of full paths for library
1865
  // specification.
1866
0
}
1867
1868
void cmComputeLinkInformation::AddFrameworkItem(LinkEntry const& entry)
1869
0
{
1870
0
  std::string const& item = entry.Item.Value;
1871
1872
  // Try to separate the framework name and path.
1873
0
  auto fwDescriptor = this->GlobalGenerator->SplitFrameworkPath(
1874
0
    item,
1875
0
    entry.Feature == DEFAULT ? cmGlobalGenerator::FrameworkFormat::Relaxed
1876
0
                             : cmGlobalGenerator::FrameworkFormat::Extended);
1877
0
  if (!fwDescriptor) {
1878
0
    std::ostringstream e;
1879
0
    e << "Could not parse framework path \"" << item << "\" linked by target "
1880
0
      << this->Target->GetName() << '.';
1881
0
    cmSystemTools::Error(e.str());
1882
0
    return;
1883
0
  }
1884
1885
0
  std::string const& fw_path = fwDescriptor->Directory;
1886
0
  if (!fw_path.empty()) {
1887
    // Add the directory portion to the framework search path.
1888
0
    this->AddFrameworkPath(fw_path);
1889
0
  }
1890
1891
  // add runtime information
1892
0
  this->AddLibraryRuntimeInfo(fwDescriptor->GetFullPath());
1893
1894
0
  if (entry.Feature == DEFAULT) {
1895
    // ensure FRAMEWORK feature is loaded
1896
0
    this->AddLibraryFeature("FRAMEWORK");
1897
0
  }
1898
1899
0
  if (this->GlobalGenerator->IsXcode()) {
1900
    // Add framework path - it will be handled by Xcode after it's added to
1901
    // "Link Binary With Libraries" build phase
1902
0
    this->Items.emplace_back(item, ItemIsPath::Yes, nullptr, nullptr,
1903
0
                             this->FindLibraryFeature(entry.Feature == DEFAULT
1904
0
                                                        ? "FRAMEWORK"
1905
0
                                                        : entry.Feature));
1906
0
  } else {
1907
0
    this->Items.emplace_back(
1908
0
      fwDescriptor->GetLinkName(), ItemIsPath::Yes, nullptr, nullptr,
1909
0
      this->FindLibraryFeature(entry.Feature == DEFAULT ? "FRAMEWORK"
1910
0
                                                        : entry.Feature));
1911
0
  }
1912
0
}
1913
1914
void cmComputeLinkInformation::AddXcFrameworkItem(LinkEntry const& entry)
1915
0
{
1916
0
  auto plist = cmParseXcFrameworkPlist(entry.Item.Value, *this->Makefile,
1917
0
                                       entry.Item.Backtrace);
1918
0
  if (!plist) {
1919
0
    return;
1920
0
  }
1921
1922
0
  if (auto const* lib =
1923
0
        plist->SelectSuitableLibrary(*this->Makefile, entry.Item.Backtrace)) {
1924
0
    if (this->GlobalGenerator->IsXcode()) {
1925
0
      this->Items.emplace_back(
1926
0
        entry.Item.Value, ItemIsPath::Yes, nullptr, nullptr,
1927
0
        this->FindLibraryFeature(entry.Feature == DEFAULT
1928
0
                                   ? "__CMAKE_LINK_XCFRAMEWORK"
1929
0
                                   : entry.Feature));
1930
0
    } else {
1931
0
      auto libraryPath = cmStrCat(
1932
0
        entry.Item.Value, '/', lib->LibraryIdentifier, '/', lib->LibraryPath);
1933
0
      LinkEntry libraryEntry(
1934
0
        BT<std::string>(libraryPath, entry.Item.Backtrace), entry.Target);
1935
1936
0
      if (cmSystemTools::IsPathToFramework(libraryPath) &&
1937
0
          this->Target->IsApple()) {
1938
        // This is a framework.
1939
0
        this->AddFrameworkItem(libraryEntry);
1940
0
      } else {
1941
0
        this->Depends.push_back(libraryPath);
1942
0
        this->AddFullItem(libraryEntry);
1943
0
        this->AddLibraryRuntimeInfo(libraryPath);
1944
0
        if (!lib->HeadersPath.empty()) {
1945
0
          this->AddXcFrameworkHeaderPath(cmStrCat(entry.Item.Value, '/',
1946
0
                                                  lib->LibraryIdentifier, '/',
1947
0
                                                  lib->HeadersPath));
1948
0
        }
1949
0
      }
1950
0
    }
1951
0
  }
1952
0
}
1953
1954
void cmComputeLinkInformation::DropDirectoryItem(BT<std::string> const& item)
1955
0
{
1956
  // A full path to a directory was found as a link item.  Warn the
1957
  // user.
1958
0
  this->CMakeInstance->IssueMessage(
1959
0
    MessageType::WARNING,
1960
0
    cmStrCat("Target \"", this->Target->GetName(),
1961
0
             "\" requests linking to directory \"", item.Value,
1962
0
             "\".  Targets may link only to libraries.  CMake is dropping "
1963
0
             "the item."),
1964
0
    item.Backtrace);
1965
0
}
1966
1967
void cmComputeLinkInformation::ComputeFrameworkInfo()
1968
0
{
1969
  // Avoid adding implicit framework paths.
1970
0
  cmList implicitDirs;
1971
1972
  // Get platform-wide implicit directories.
1973
0
  implicitDirs.assign(this->Makefile->GetDefinition(
1974
0
    "CMAKE_PLATFORM_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES"));
1975
1976
  // Get language-specific implicit directories.
1977
0
  std::string implicitDirVar = cmStrCat(
1978
0
    "CMAKE_", this->LinkLanguage, "_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES");
1979
0
  implicitDirs.append(this->Makefile->GetDefinition(implicitDirVar));
1980
1981
0
  this->FrameworkPathsEmitted.insert(implicitDirs.begin(), implicitDirs.end());
1982
0
}
1983
1984
void cmComputeLinkInformation::AddFrameworkPath(std::string const& p)
1985
0
{
1986
0
  if (this->FrameworkPathsEmitted.insert(p).second) {
1987
0
    this->FrameworkPaths.push_back(p);
1988
0
  }
1989
0
}
1990
1991
void cmComputeLinkInformation::AddXcFrameworkHeaderPath(std::string const& p)
1992
0
{
1993
0
  this->XcFrameworkHeaderPaths.push_back(p);
1994
0
}
1995
1996
bool cmComputeLinkInformation::CheckSharedLibNoSOName(LinkEntry const& entry)
1997
0
{
1998
  // This platform will use the path to a library as its soname if the
1999
  // library is given via path and was not built with an soname.  If
2000
  // this is a shared library that might be the case.
2001
0
  std::string file = cmSystemTools::GetFilenameName(entry.Item.Value);
2002
0
  if (this->ExtractSharedLibraryName.find(file)) {
2003
    // If we can guess the soname fairly reliably then assume the
2004
    // library has one.  Otherwise assume the library has no builtin
2005
    // soname.
2006
0
    std::string soname;
2007
0
    if (!cmSystemTools::GuessLibrarySOName(entry.Item.Value, soname)) {
2008
0
      this->AddSharedLibNoSOName(entry);
2009
0
      return true;
2010
0
    }
2011
0
  }
2012
0
  return false;
2013
0
}
2014
2015
void cmComputeLinkInformation::AddSharedLibNoSOName(LinkEntry const& entry)
2016
0
{
2017
  // We have a full path to a shared library with no soname.  We need
2018
  // to ask the linker to locate the item because otherwise the path
2019
  // we give to it will be embedded in the target linked.  Then at
2020
  // runtime the dynamic linker will search for the library using the
2021
  // path instead of just the name.
2022
0
  LinkEntry fileEntry{ entry };
2023
0
  fileEntry.Item = cmSystemTools::GetFilenameName(entry.Item.Value);
2024
0
  this->AddUserItem(fileEntry);
2025
2026
  // Make sure the link directory ordering will find the library.
2027
0
  this->OrderLinkerSearchPath->AddLinkLibrary(entry.Item.Value);
2028
0
}
2029
2030
void cmComputeLinkInformation::LoadImplicitLinkInfo()
2031
0
{
2032
  // Get platform-wide implicit directories.
2033
0
  cmList implicitDirs{ this->Makefile->GetDefinition(
2034
0
    "CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES") };
2035
2036
  // Append library architecture to all implicit platform directories
2037
  // and add them to the set
2038
0
  if (cmValue libraryArch =
2039
0
        this->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE")) {
2040
0
    for (auto const& i : implicitDirs) {
2041
0
      this->ImplicitLinkDirs.insert(cmStrCat(i, '/', *libraryArch));
2042
0
    }
2043
0
  }
2044
2045
  // Get language-specific implicit directories.
2046
0
  std::string implicitDirVar =
2047
0
    cmStrCat("CMAKE_", this->LinkLanguage, "_IMPLICIT_LINK_DIRECTORIES");
2048
0
  implicitDirs.append(this->Makefile->GetDefinition(implicitDirVar));
2049
2050
  // Store implicit link directories.
2051
0
  this->ImplicitLinkDirs.insert(implicitDirs.begin(), implicitDirs.end());
2052
2053
  // Get language-specific implicit libraries.
2054
0
  std::string implicitLibVar =
2055
0
    cmStrCat("CMAKE_", this->LinkLanguage, "_IMPLICIT_LINK_LIBRARIES");
2056
0
  cmList implicitLibs{ this->Makefile->GetDefinition(implicitLibVar) };
2057
2058
  // Store implicit link libraries.
2059
0
  for (auto const& item : implicitLibs) {
2060
    // Items starting in '-' but not '-l' are flags, not libraries,
2061
    // and should not be filtered by this implicit list.
2062
0
    if (item[0] != '-' || item[1] == 'l') {
2063
0
      this->ImplicitLinkLibs.insert(item);
2064
0
    }
2065
0
  }
2066
2067
  // Get platform specific rpath link directories
2068
0
  cmList::append(this->RuntimeLinkDirs,
2069
0
                 this->Makefile->GetDefinition("CMAKE_PLATFORM_RUNTIME_PATH"));
2070
0
}
2071
2072
std::vector<std::string> const&
2073
cmComputeLinkInformation::GetRuntimeSearchPath() const
2074
0
{
2075
0
  return this->OrderRuntimeSearchPath->GetOrderedDirectories();
2076
0
}
2077
2078
void cmComputeLinkInformation::AddLibraryRuntimeInfo(
2079
  std::string const& fullPath, cmGeneratorTarget const* target)
2080
0
{
2081
  // Ignore targets on Apple where install_name is not @rpath.
2082
  // The dependenty library can be found with other means such as
2083
  // @loader_path or full paths.
2084
0
  if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
2085
0
    if (!target->HasMacOSXRpathInstallNameDir(this->Config)) {
2086
0
      return;
2087
0
    }
2088
0
  }
2089
2090
  // Libraries with unknown type must be handled using just the file
2091
  // on disk.
2092
0
  if (target->GetType() == cmStateEnums::UNKNOWN_LIBRARY) {
2093
0
    this->AddLibraryRuntimeInfo(fullPath);
2094
0
    return;
2095
0
  }
2096
2097
  // Skip targets that are not shared libraries (modules cannot be linked).
2098
0
  if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
2099
0
    return;
2100
0
  }
2101
2102
  // Skip targets that do not have a known runtime artifact.
2103
0
  if (!target->HasKnownRuntimeArtifactLocation(this->Config)) {
2104
0
    return;
2105
0
  }
2106
2107
  // Try to get the soname of the library.  Only files with this name
2108
  // could possibly conflict.
2109
0
  std::string soName = target->GetSOName(this->Config);
2110
0
  char const* soname = soName.empty() ? nullptr : soName.c_str();
2111
2112
  // Include this library in the runtime path ordering.
2113
0
  this->OrderRuntimeSearchPath->AddRuntimeLibrary(fullPath, soname);
2114
0
  if (this->LinkWithRuntimePath) {
2115
0
    this->OrderLinkerSearchPath->AddRuntimeLibrary(fullPath, soname);
2116
0
  }
2117
0
}
2118
2119
void cmComputeLinkInformation::AddLibraryRuntimeInfo(
2120
  std::string const& fullPath)
2121
0
{
2122
  // Get the name of the library from the file name.
2123
0
  bool is_shared_library = false;
2124
0
  std::string file = cmSystemTools::GetFilenameName(fullPath);
2125
2126
0
  if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
2127
    // Check that @rpath is part of the install name.
2128
    // If it isn't, return.
2129
0
    std::string soname;
2130
0
    if (!cmSystemTools::GuessLibraryInstallName(fullPath, soname)) {
2131
0
      return;
2132
0
    }
2133
2134
0
    if (soname.find("@rpath") == std::string::npos) {
2135
0
      return;
2136
0
    }
2137
0
  }
2138
2139
0
  is_shared_library = this->ExtractSharedLibraryName.find(file);
2140
2141
0
  if (!is_shared_library) {
2142
    // On some platforms (AIX) a shared library may look static.
2143
0
    if (this->ArchivesMayBeShared) {
2144
0
      if (this->ExtractStaticLibraryName.find(file)) {
2145
        // This is the name of a shared library or archive.
2146
0
        is_shared_library = true;
2147
0
      }
2148
0
    }
2149
0
  }
2150
2151
  // It could be an Apple framework
2152
0
  if (!is_shared_library) {
2153
0
    is_shared_library =
2154
0
      this->GlobalGenerator
2155
0
        ->SplitFrameworkPath(fullPath,
2156
0
                             cmGlobalGenerator::FrameworkFormat::Strict)
2157
0
        .has_value();
2158
0
  }
2159
2160
0
  if (!is_shared_library) {
2161
0
    return;
2162
0
  }
2163
2164
  // Include this library in the runtime path ordering.
2165
0
  this->OrderRuntimeSearchPath->AddRuntimeLibrary(fullPath);
2166
0
  if (this->LinkWithRuntimePath) {
2167
0
    this->OrderLinkerSearchPath->AddRuntimeLibrary(fullPath);
2168
0
  }
2169
0
}
2170
2171
static void cmCLI_ExpandListUnique(std::string const& str,
2172
                                   std::vector<std::string>& out,
2173
                                   std::set<std::string>& emitted)
2174
0
{
2175
0
  cmList tmp{ str };
2176
0
  for (std::string const& i : tmp) {
2177
0
    if (emitted.insert(i).second) {
2178
0
      out.push_back(i);
2179
0
    }
2180
0
  }
2181
0
}
2182
2183
void cmComputeLinkInformation::GetRPath(std::vector<std::string>& runtimeDirs,
2184
                                        bool for_install) const
2185
0
{
2186
  // Select whether to generate runtime search directories.
2187
0
  bool outputRuntime =
2188
0
    !this->Makefile->IsOn("CMAKE_SKIP_RPATH") && !this->RuntimeFlag.empty();
2189
2190
  // Select whether to generate an rpath for the install tree or the
2191
  // build tree.
2192
0
  bool linking_for_install =
2193
0
    (for_install ||
2194
0
     this->Target->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"));
2195
0
  bool use_install_rpath =
2196
0
    (outputRuntime && this->Target->HaveInstallTreeRPATH(this->Config) &&
2197
0
     linking_for_install);
2198
0
  bool use_build_rpath =
2199
0
    (outputRuntime && this->Target->HaveBuildTreeRPATH(this->Config) &&
2200
0
     !linking_for_install);
2201
0
  bool use_link_rpath = outputRuntime && linking_for_install &&
2202
0
    !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH") &&
2203
0
    this->Target->GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
2204
2205
  // Select whether to use $ORIGIN in RPATHs for artifacts in the build tree.
2206
0
  std::string const& originToken = this->Makefile->GetSafeDefinition(
2207
0
    "CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN");
2208
0
  std::string targetOutputDir = this->Target->GetDirectory(this->Config);
2209
0
  bool use_relative_build_rpath =
2210
0
    this->Target->GetPropertyAsBool("BUILD_RPATH_USE_ORIGIN") &&
2211
0
    !originToken.empty() && !targetOutputDir.empty();
2212
2213
  // Construct the RPATH.
2214
0
  std::set<std::string> emitted;
2215
0
  if (use_install_rpath) {
2216
0
    std::string install_rpath;
2217
0
    this->Target->GetInstallRPATH(this->Config, install_rpath);
2218
0
    cmCLI_ExpandListUnique(install_rpath, runtimeDirs, emitted);
2219
0
  }
2220
0
  if (use_build_rpath) {
2221
    // Add directories explicitly specified by user
2222
0
    std::string build_rpath;
2223
0
    if (this->Target->GetBuildRPATH(this->Config, build_rpath)) {
2224
      // This will not resolve entries to use $ORIGIN, the user is expected
2225
      // to do that if necessary.
2226
0
      cmCLI_ExpandListUnique(build_rpath, runtimeDirs, emitted);
2227
0
    }
2228
0
  }
2229
0
  if (use_build_rpath || use_link_rpath) {
2230
0
    std::string rootPath;
2231
0
    if (cmValue sysrootLink =
2232
0
          this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
2233
0
      rootPath = *sysrootLink;
2234
0
    } else {
2235
0
      rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
2236
0
    }
2237
0
    cmValue stagePath = this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
2238
0
    std::string const& installPrefix =
2239
0
      this->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
2240
0
    cmSystemTools::ConvertToUnixSlashes(rootPath);
2241
0
    std::vector<std::string> const& rdirs = this->GetRuntimeSearchPath();
2242
0
    std::string const& topBinaryDir =
2243
0
      this->CMakeInstance->GetHomeOutputDirectory();
2244
0
    for (std::string const& ri : rdirs) {
2245
      // Put this directory in the rpath if using build-tree rpath
2246
      // support or if using the link path as an rpath.
2247
0
      if (use_build_rpath) {
2248
0
        std::string d = ri;
2249
0
        if (!rootPath.empty() && cmHasPrefix(d, rootPath)) {
2250
0
          d.erase(0, rootPath.size());
2251
0
        } else if (cmNonempty(stagePath) && cmHasPrefix(d, *stagePath)) {
2252
0
          d.erase(0, (*stagePath).size());
2253
0
          d = cmStrCat(installPrefix, '/', d);
2254
0
          cmSystemTools::ConvertToUnixSlashes(d);
2255
0
        } else if (use_relative_build_rpath) {
2256
          // If expansion of the $ORIGIN token is supported and permitted per
2257
          // policy, use relative paths in the RPATH.
2258
0
          if (cmSystemTools::ComparePath(d, topBinaryDir) ||
2259
0
              cmSystemTools::IsSubDirectory(d, topBinaryDir)) {
2260
0
            d = cmSystemTools::RelativePath(targetOutputDir, d);
2261
0
            if (!d.empty()) {
2262
0
              d = cmStrCat(originToken, '/', d);
2263
0
            } else {
2264
0
              d = originToken;
2265
0
            }
2266
0
          }
2267
0
        }
2268
0
        if (emitted.insert(d).second) {
2269
0
          runtimeDirs.push_back(std::move(d));
2270
0
        }
2271
0
      } else if (use_link_rpath) {
2272
        // Do not add any path inside the source or build tree.
2273
0
        std::string const& topSourceDir =
2274
0
          this->CMakeInstance->GetHomeDirectory();
2275
0
        if (!cmSystemTools::ComparePath(ri, topSourceDir) &&
2276
0
            !cmSystemTools::ComparePath(ri, topBinaryDir) &&
2277
0
            !cmSystemTools::IsSubDirectory(ri, topSourceDir) &&
2278
0
            !cmSystemTools::IsSubDirectory(ri, topBinaryDir)) {
2279
0
          std::string d = ri;
2280
0
          if (!rootPath.empty() && cmHasPrefix(d, rootPath)) {
2281
0
            d.erase(0, rootPath.size());
2282
0
          } else if (cmNonempty(stagePath) && cmHasPrefix(d, *stagePath)) {
2283
0
            d.erase(0, (*stagePath).size());
2284
0
            d = cmStrCat(installPrefix, '/', d);
2285
0
            cmSystemTools::ConvertToUnixSlashes(d);
2286
0
          }
2287
0
          if (emitted.insert(d).second) {
2288
0
            runtimeDirs.push_back(std::move(d));
2289
0
          }
2290
0
        }
2291
0
      }
2292
0
    }
2293
0
  }
2294
2295
  // Add runtime paths required by the languages to always be
2296
  // present.  This is done even when skipping rpath support.
2297
0
  {
2298
0
    cmGeneratorTarget::LinkClosure const* lc =
2299
0
      this->Target->GetLinkClosure(this->Config);
2300
0
    for (std::string const& li : lc->Languages) {
2301
0
      std::string useVar = cmStrCat(
2302
0
        "CMAKE_", li, "_USE_IMPLICIT_LINK_DIRECTORIES_IN_RUNTIME_PATH");
2303
0
      if (this->Makefile->IsOn(useVar)) {
2304
0
        std::string dirVar =
2305
0
          cmStrCat("CMAKE_", li, "_IMPLICIT_LINK_DIRECTORIES");
2306
0
        if (cmValue dirs = this->Makefile->GetDefinition(dirVar)) {
2307
0
          cmCLI_ExpandListUnique(*dirs, runtimeDirs, emitted);
2308
0
        }
2309
0
      }
2310
0
    }
2311
0
  }
2312
2313
  // Add runtime paths required by the platform to always be
2314
  // present.  This is done even when skipping rpath support.
2315
0
  cmCLI_ExpandListUnique(this->RuntimeAlways, runtimeDirs, emitted);
2316
0
}
2317
2318
std::string cmComputeLinkInformation::GetRPathString(bool for_install) const
2319
0
{
2320
  // Get the directories to use.
2321
0
  std::vector<std::string> runtimeDirs;
2322
0
  this->GetRPath(runtimeDirs, for_install);
2323
2324
  // Concatenate the paths.
2325
0
  std::string rpath = cmJoin(runtimeDirs, this->GetRuntimeSep());
2326
2327
  // If the rpath will be replaced at install time, prepare space.
2328
0
  if (!for_install && this->RuntimeUseChrpath) {
2329
0
    if (!rpath.empty()) {
2330
      // Add one trailing separator so the linker does not reuse the
2331
      // rpath .dynstr entry for a symbol name that happens to match
2332
      // the end of the rpath string.
2333
0
      rpath += this->GetRuntimeSep();
2334
0
    }
2335
2336
    // Make sure it is long enough to hold the replacement value.
2337
0
    std::string::size_type minLength = this->GetChrpathString().length();
2338
0
    while (rpath.length() < minLength) {
2339
0
      rpath += this->GetRuntimeSep();
2340
0
    }
2341
0
  }
2342
2343
0
  return rpath;
2344
0
}
2345
2346
std::string cmComputeLinkInformation::GetChrpathString() const
2347
0
{
2348
0
  if (!this->RuntimeUseChrpath) {
2349
0
    return "";
2350
0
  }
2351
2352
0
  return this->GetRPathString(true);
2353
0
}