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