cmComputeLinkInformation.cxx 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmComputeLinkInformation.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmComputeLinkDepends.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmOrderDirectories.h"
  11. #include "cmOutputConverter.h"
  12. #include "cmPolicies.h"
  13. #include "cmState.h"
  14. #include "cmStateTypes.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include "cmake.h"
  18. #include <algorithm>
  19. #include <ctype.h>
  20. #include <sstream>
  21. #include <string.h>
  22. #include <utility>
  23. //#define CM_COMPUTE_LINK_INFO_DEBUG
  24. /*
  25. Notes about linking on various platforms:
  26. ------------------------------------------------------------------------------
  27. Linux, FreeBSD, Mac OS X, IRIX, Sun, Windows:
  28. Linking to libraries using the full path works fine.
  29. ------------------------------------------------------------------------------
  30. On AIX, more work is needed.
  31. The "-bnoipath" option is needed. From "man ld":
  32. Note: If you specify a shared object, or an archive file
  33. containing a shared object, with an absolute or relative path
  34. name, instead of with the -lName flag, the path name is
  35. included in the import file ID string in the loader section of
  36. the output file. You can override this behavior with the
  37. -bnoipath option.
  38. noipath
  39. For shared objects listed on the command-line, rather than
  40. specified with the -l flag, use a null path component when
  41. listing the shared object in the loader section of the
  42. output file. A null path component is always used for
  43. shared objects specified with the -l flag. This option
  44. does not affect the specification of a path component by
  45. using a line beginning with #! in an import file. The
  46. default is the ipath option.
  47. This prevents the full path specified on the compile line from being
  48. compiled directly into the binary.
  49. By default the linker places -L paths in the embedded runtime path.
  50. In order to implement CMake's RPATH interface correctly, we need the
  51. -blibpath:Path option. From "man ld":
  52. libpath:Path
  53. Uses Path as the library path when writing the loader section
  54. of the output file. Path is neither checked for validity nor
  55. used when searching for libraries specified by the -l flag.
  56. Path overrides any library paths generated when the -L flag is
  57. used.
  58. If you do not specify any -L flags, or if you specify the
  59. nolibpath option, the default library path information is
  60. written in the loader section of the output file. The default
  61. library path information is the value of the LIBPATH
  62. environment variable if it is defined, and /usr/lib:/lib,
  63. otherwise.
  64. We can pass -Wl,-blibpath:/usr/lib:/lib always to avoid the -L stuff
  65. and not break when the user sets LIBPATH. Then if we want to add an
  66. rpath we insert it into the option before /usr/lib.
  67. ------------------------------------------------------------------------------
  68. On HP-UX, more work is needed. There are differences between
  69. versions.
  70. ld: 92453-07 linker linker ld B.10.33 990520
  71. Linking with a full path works okay for static and shared libraries.
  72. The linker seems to always put the full path to where the library
  73. was found in the binary whether using a full path or -lfoo syntax.
  74. Transitive link dependencies work just fine due to the full paths.
  75. It has the "-l:libfoo.sl" option. The +nodefaultrpath is accepted
  76. but not documented and does not seem to do anything. There is no
  77. +forceload option.
  78. ld: 92453-07 linker ld HP Itanium(R) B.12.41 IPF/IPF
  79. Linking with a full path works okay for static libraries.
  80. Linking with a full path works okay for shared libraries. However
  81. dependent (transitive) libraries of those linked directly must be
  82. either found with an rpath stored in the direct dependencies or
  83. found in -L paths as if they were specified with "-l:libfoo.sl"
  84. (really "-l:<soname>"). The search matches that of the dynamic
  85. loader but only with -L paths. In other words, if we have an
  86. executable that links to shared library bar which links to shared
  87. library foo, the link line for the exe must contain
  88. /dir/with/bar/libbar.sl -L/dir/with/foo
  89. It does not matter whether the exe wants to link to foo directly or
  90. whether /dir/with/foo/libfoo.sl is listed. The -L path must still
  91. be present. It should match the runtime path computed for the
  92. executable taking all directly and transitively linked libraries
  93. into account.
  94. The "+nodefaultrpath" option should be used to avoid getting -L
  95. paths in the rpath unless we add our own rpath with +b. This means
  96. that skip-build-rpath should use this option.
  97. See documentation in "man ld", "man dld.so", and
  98. http://docs.hp.com/en/B2355-90968/creatingandusinglibraries.htm
  99. +[no]defaultrpath
  100. +defaultrpath is the default. Include any paths that are
  101. specified with -L in the embedded path, unless you specify the
  102. +b option. If you use +b, only the path list specified by +b is
  103. in the embedded path.
  104. The +nodefaultrpath option removes all library paths that were
  105. specified with the -L option from the embedded path. The linker
  106. searches the library paths specified by the -L option at link
  107. time. At run time, the only library paths searched are those
  108. specified by the environment variables LD_LIBRARY_PATH and
  109. SHLIB_PATH, library paths specified by the +b linker option, and
  110. finally the default library paths.
  111. +rpathfirst
  112. This option will cause the paths specified in RPATH (embedded
  113. path) to be used before the paths specified in LD_LIBRARY_PATH
  114. or SHLIB_PATH, in searching for shared libraries. This changes
  115. the default search order of LD_LIBRARY_PATH, SHLIB_PATH, and
  116. RPATH (embedded path).
  117. ------------------------------------------------------------------------------
  118. Notes about dependent (transitive) shared libraries:
  119. On non-Windows systems shared libraries may have transitive
  120. dependencies. In order to support LINK_INTERFACE_LIBRARIES we must
  121. support linking to a shared library without listing all the libraries
  122. to which it links. Some linkers want to be able to find the
  123. transitive dependencies (dependent libraries) of shared libraries
  124. listed on the command line.
  125. - On Windows, DLLs are not directly linked, and the import libraries
  126. have no transitive dependencies.
  127. - On Mac OS X 10.5 and above transitive dependencies are not needed.
  128. - On Mac OS X 10.4 and below we need to actually list the dependencies.
  129. Otherwise when using -isysroot for universal binaries it cannot
  130. find the dependent libraries. Listing them on the command line
  131. tells the linker where to find them, but unfortunately also links
  132. the library.
  133. - On HP-UX, the linker wants to find the transitive dependencies of
  134. shared libraries in the -L paths even if the dependent libraries
  135. are given on the link line.
  136. - On AIX the transitive dependencies are not needed.
  137. - On SGI, the linker wants to find the transitive dependencies of
  138. shared libraries in the -L paths if they are not given on the link
  139. line. Transitive linking can be disabled using the options
  140. -no_transitive_link -Wl,-no_transitive_link
  141. which disable it. Both options must be given when invoking the
  142. linker through the compiler.
  143. - On Sun, the linker wants to find the transitive dependencies of
  144. shared libraries in the -L paths if they are not given on the link
  145. line.
  146. - On Linux, FreeBSD, and QNX:
  147. The linker wants to find the transitive dependencies of shared
  148. libraries in the "-rpath-link" paths option if they have not been
  149. given on the link line. The option is like rpath but just for
  150. link time:
  151. -Wl,-rpath-link,"/path1:/path2"
  152. For -rpath-link, we need a separate runtime path ordering pass
  153. including just the dependent libraries that are not linked.
  154. For -L paths on non-HP, we can do the same thing as with rpath-link
  155. but put the results in -L paths. The paths should be listed at the
  156. end to avoid conflicting with user search paths (?).
  157. For -L paths on HP, we should do a runtime path ordering pass with
  158. all libraries, both linked and non-linked. Even dependent
  159. libraries that are also linked need to be listed in -L paths.
  160. In our implementation we add all dependent libraries to the runtime
  161. path computation. Then the auto-generated RPATH will find everything.
  162. ------------------------------------------------------------------------------
  163. Notes about shared libraries with not builtin soname:
  164. Some UNIX shared libraries may be created with no builtin soname. On
  165. some platforms such libraries cannot be linked using the path to their
  166. location because the linker will copy the path into the field used to
  167. find the library at runtime.
  168. Apple: ../libfoo.dylib ==> libfoo.dylib # ok, uses install_name
  169. SGI: ../libfoo.so ==> libfoo.so # ok
  170. AIX: ../libfoo.so ==> libfoo.so # ok
  171. Linux: ../libfoo.so ==> ../libfoo.so # bad
  172. HP-UX: ../libfoo.so ==> ../libfoo.so # bad
  173. Sun: ../libfoo.so ==> ../libfoo.so # bad
  174. FreeBSD: ../libfoo.so ==> ../libfoo.so # bad
  175. In order to link these libraries we need to use the old-style split
  176. into -L.. and -lfoo options. This should be fairly safe because most
  177. problems with -lfoo options were related to selecting shared libraries
  178. instead of static but in this case we want the shared lib. Link
  179. directory ordering needs to be done to make sure these shared
  180. libraries are found first. There should be very few restrictions
  181. because this need be done only for shared libraries without soname-s.
  182. */
  183. cmComputeLinkInformation::cmComputeLinkInformation(
  184. const cmGeneratorTarget* target, const std::string& config)
  185. {
  186. // Store context information.
  187. this->Target = target;
  188. this->Makefile = this->Target->Target->GetMakefile();
  189. this->GlobalGenerator =
  190. this->Target->GetLocalGenerator()->GetGlobalGenerator();
  191. this->CMakeInstance = this->GlobalGenerator->GetCMakeInstance();
  192. // Check whether to recognize OpenBSD-style library versioned names.
  193. this->OpenBSD = this->Makefile->GetState()->GetGlobalPropertyAsBool(
  194. "FIND_LIBRARY_USE_OPENBSD_VERSIONING");
  195. // The configuration being linked.
  196. this->Config = config;
  197. // Allocate internals.
  198. this->OrderLinkerSearchPath = new cmOrderDirectories(
  199. this->GlobalGenerator, target, "linker search path");
  200. this->OrderRuntimeSearchPath = new cmOrderDirectories(
  201. this->GlobalGenerator, target, "runtime search path");
  202. this->OrderDependentRPath = nullptr;
  203. // Get the language used for linking this target.
  204. this->LinkLanguage = this->Target->GetLinkerLanguage(config);
  205. if (this->LinkLanguage.empty()) {
  206. // The Compute method will do nothing, so skip the rest of the
  207. // initialization.
  208. return;
  209. }
  210. // Check whether we should use an import library for linking a target.
  211. this->UseImportLibrary =
  212. this->Makefile->IsDefinitionSet("CMAKE_IMPORT_LIBRARY_SUFFIX");
  213. // Check whether we should skip dependencies on shared library files.
  214. this->LinkDependsNoShared =
  215. this->Target->GetPropertyAsBool("LINK_DEPENDS_NO_SHARED");
  216. // On platforms without import libraries there may be a special flag
  217. // to use when creating a plugin (module) that obtains symbols from
  218. // the program that will load it.
  219. this->LoaderFlag = nullptr;
  220. if (!this->UseImportLibrary &&
  221. this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  222. std::string loader_flag_var = "CMAKE_SHARED_MODULE_LOADER_";
  223. loader_flag_var += this->LinkLanguage;
  224. loader_flag_var += "_FLAG";
  225. this->LoaderFlag = this->Makefile->GetDefinition(loader_flag_var);
  226. }
  227. // Get options needed to link libraries.
  228. this->LibLinkFlag =
  229. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  230. this->LibLinkFileFlag =
  231. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FILE_FLAG");
  232. this->LibLinkSuffix =
  233. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  234. // Get options needed to specify RPATHs.
  235. this->RuntimeUseChrpath = false;
  236. if (this->Target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  237. const char* tType = ((this->Target->GetType() == cmStateEnums::EXECUTABLE)
  238. ? "EXECUTABLE"
  239. : "SHARED_LIBRARY");
  240. std::string rtVar = "CMAKE_";
  241. rtVar += tType;
  242. rtVar += "_RUNTIME_";
  243. rtVar += this->LinkLanguage;
  244. rtVar += "_FLAG";
  245. std::string rtSepVar = rtVar + "_SEP";
  246. this->RuntimeFlag = this->Makefile->GetSafeDefinition(rtVar);
  247. this->RuntimeSep = this->Makefile->GetSafeDefinition(rtSepVar);
  248. this->RuntimeAlways = (this->Makefile->GetSafeDefinition(
  249. "CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH"));
  250. this->RuntimeUseChrpath = this->Target->IsChrpathUsed(config);
  251. // Get options needed to help find dependent libraries.
  252. std::string rlVar = "CMAKE_";
  253. rlVar += tType;
  254. rlVar += "_RPATH_LINK_";
  255. rlVar += this->LinkLanguage;
  256. rlVar += "_FLAG";
  257. this->RPathLinkFlag = this->Makefile->GetSafeDefinition(rlVar);
  258. }
  259. // Check if we need to include the runtime search path at link time.
  260. {
  261. std::string var = "CMAKE_SHARED_LIBRARY_LINK_";
  262. var += this->LinkLanguage;
  263. var += "_WITH_RUNTIME_PATH";
  264. this->LinkWithRuntimePath = this->Makefile->IsOn(var);
  265. }
  266. // Check the platform policy for missing soname case.
  267. this->NoSONameUsesPath =
  268. this->Makefile->IsOn("CMAKE_PLATFORM_USES_PATH_WHEN_NO_SONAME");
  269. // Get link type information.
  270. this->ComputeLinkTypeInfo();
  271. // Setup the link item parser.
  272. this->ComputeItemParserInfo();
  273. // Setup framework support.
  274. this->ComputeFrameworkInfo();
  275. // Choose a mode for dealing with shared library dependencies.
  276. this->SharedDependencyMode = SharedDepModeNone;
  277. if (this->Makefile->IsOn("CMAKE_LINK_DEPENDENT_LIBRARY_FILES")) {
  278. this->SharedDependencyMode = SharedDepModeLink;
  279. } else if (this->Makefile->IsOn("CMAKE_LINK_DEPENDENT_LIBRARY_DIRS")) {
  280. this->SharedDependencyMode = SharedDepModeLibDir;
  281. } else if (!this->RPathLinkFlag.empty()) {
  282. this->SharedDependencyMode = SharedDepModeDir;
  283. this->OrderDependentRPath = new cmOrderDirectories(
  284. this->GlobalGenerator, target, "dependent library path");
  285. }
  286. // Add the search path entries requested by the user to path ordering.
  287. this->OrderLinkerSearchPath->AddUserDirectories(
  288. this->Target->GetLinkDirectories());
  289. this->OrderRuntimeSearchPath->AddUserDirectories(
  290. this->Target->GetLinkDirectories());
  291. // Set up the implicit link directories.
  292. this->LoadImplicitLinkInfo();
  293. this->OrderLinkerSearchPath->SetImplicitDirectories(this->ImplicitLinkDirs);
  294. this->OrderRuntimeSearchPath->SetImplicitDirectories(this->ImplicitLinkDirs);
  295. if (this->OrderDependentRPath) {
  296. this->OrderDependentRPath->SetImplicitDirectories(this->ImplicitLinkDirs);
  297. this->OrderDependentRPath->AddLanguageDirectories(this->RuntimeLinkDirs);
  298. }
  299. // Decide whether to enable compatible library search path mode.
  300. // There exists code that effectively does
  301. //
  302. // /path/to/libA.so -lB
  303. //
  304. // where -lB is meant to link to /path/to/libB.so. This is broken
  305. // because it specified -lB without specifying a link directory (-L)
  306. // in which to search for B. This worked in CMake 2.4 and below
  307. // because -L/path/to would be added by the -L/-l split for A. In
  308. // order to support such projects we need to add the directories
  309. // containing libraries linked with a full path to the -L path.
  310. this->OldLinkDirMode =
  311. this->Target->GetPolicyStatusCMP0003() != cmPolicies::NEW;
  312. if (this->OldLinkDirMode) {
  313. // Construct a mask to not bother with this behavior for link
  314. // directories already specified by the user.
  315. std::vector<std::string> const& dirs = this->Target->GetLinkDirectories();
  316. this->OldLinkDirMask.insert(dirs.begin(), dirs.end());
  317. }
  318. this->CMP0060Warn = this->Makefile->PolicyOptionalWarningEnabled(
  319. "CMAKE_POLICY_WARNING_CMP0060");
  320. }
  321. cmComputeLinkInformation::~cmComputeLinkInformation()
  322. {
  323. delete this->OrderLinkerSearchPath;
  324. delete this->OrderRuntimeSearchPath;
  325. delete this->OrderDependentRPath;
  326. }
  327. cmComputeLinkInformation::ItemVector const&
  328. cmComputeLinkInformation::GetItems()
  329. {
  330. return this->Items;
  331. }
  332. std::vector<std::string> const& cmComputeLinkInformation::GetDirectories()
  333. {
  334. return this->OrderLinkerSearchPath->GetOrderedDirectories();
  335. }
  336. std::string cmComputeLinkInformation::GetRPathLinkString()
  337. {
  338. // If there is no separate linker runtime search flag (-rpath-link)
  339. // there is no reason to compute a string.
  340. if (!this->OrderDependentRPath) {
  341. return "";
  342. }
  343. // Construct the linker runtime search path.
  344. return cmJoin(this->OrderDependentRPath->GetOrderedDirectories(), ":");
  345. }
  346. std::vector<std::string> const& cmComputeLinkInformation::GetDepends()
  347. {
  348. return this->Depends;
  349. }
  350. std::vector<std::string> const& cmComputeLinkInformation::GetFrameworkPaths()
  351. {
  352. return this->FrameworkPaths;
  353. }
  354. const std::set<const cmGeneratorTarget*>&
  355. cmComputeLinkInformation::GetSharedLibrariesLinked()
  356. {
  357. return this->SharedLibrariesLinked;
  358. }
  359. bool cmComputeLinkInformation::Compute()
  360. {
  361. // Skip targets that do not link.
  362. if (!(this->Target->GetType() == cmStateEnums::EXECUTABLE ||
  363. this->Target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  364. this->Target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  365. this->Target->GetType() == cmStateEnums::STATIC_LIBRARY)) {
  366. return false;
  367. }
  368. // We require a link language for the target.
  369. if (this->LinkLanguage.empty()) {
  370. cmSystemTools::Error(
  371. "CMake can not determine linker language for target: ",
  372. this->Target->GetName().c_str());
  373. return false;
  374. }
  375. // Compute the ordered link line items.
  376. cmComputeLinkDepends cld(this->Target, this->Config);
  377. cld.SetOldLinkDirMode(this->OldLinkDirMode);
  378. cmComputeLinkDepends::EntryVector const& linkEntries = cld.Compute();
  379. // Add the link line items.
  380. for (cmComputeLinkDepends::LinkEntry const& linkEntry : linkEntries) {
  381. if (linkEntry.IsSharedDep) {
  382. this->AddSharedDepItem(linkEntry.Item, linkEntry.Target);
  383. } else {
  384. this->AddItem(linkEntry.Item, linkEntry.Target);
  385. }
  386. }
  387. // Restore the target link type so the correct system runtime
  388. // libraries are found.
  389. const char* lss = this->Target->GetProperty("LINK_SEARCH_END_STATIC");
  390. if (cmSystemTools::IsOn(lss)) {
  391. this->SetCurrentLinkType(LinkStatic);
  392. } else {
  393. this->SetCurrentLinkType(this->StartLinkType);
  394. }
  395. // Finish listing compatibility paths.
  396. if (this->OldLinkDirMode) {
  397. // For CMake 2.4 bug-compatibility we need to consider the output
  398. // directories of targets linked in another configuration as link
  399. // directories.
  400. std::set<cmGeneratorTarget const*> const& wrongItems =
  401. cld.GetOldWrongConfigItems();
  402. for (cmGeneratorTarget const* tgt : wrongItems) {
  403. bool implib = (this->UseImportLibrary &&
  404. (tgt->GetType() == cmStateEnums::SHARED_LIBRARY));
  405. cmStateEnums::ArtifactType artifact = implib
  406. ? cmStateEnums::ImportLibraryArtifact
  407. : cmStateEnums::RuntimeBinaryArtifact;
  408. this->OldLinkDirItems.push_back(
  409. tgt->GetFullPath(this->Config, artifact, true));
  410. }
  411. }
  412. // Finish setting up linker search directories.
  413. if (!this->FinishLinkerSearchDirectories()) {
  414. return false;
  415. }
  416. // Add implicit language runtime libraries and directories.
  417. this->AddImplicitLinkInfo();
  418. if (!this->CMP0060WarnItems.empty()) {
  419. std::ostringstream w;
  420. /* clang-format off */
  421. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0060) << "\n"
  422. "Some library files are in directories implicitly searched by "
  423. "the linker when invoked for " << this->LinkLanguage << ":\n"
  424. " " << cmJoin(this->CMP0060WarnItems, "\n ") << "\n"
  425. "For compatibility with older versions of CMake, the generated "
  426. "link line will ask the linker to search for these by library "
  427. "name."
  428. ;
  429. /* clang-format on */
  430. this->CMakeInstance->IssueMessage(cmake::AUTHOR_WARNING, w.str(),
  431. this->Target->GetBacktrace());
  432. }
  433. return true;
  434. }
  435. void cmComputeLinkInformation::AddImplicitLinkInfo()
  436. {
  437. // The link closure lists all languages whose implicit info is needed.
  438. cmGeneratorTarget::LinkClosure const* lc =
  439. this->Target->GetLinkClosure(this->Config);
  440. for (std::string const& li : lc->Languages) {
  441. // Skip those of the linker language. They are implicit.
  442. if (li != this->LinkLanguage) {
  443. this->AddImplicitLinkInfo(li);
  444. }
  445. }
  446. }
  447. void cmComputeLinkInformation::AddImplicitLinkInfo(std::string const& lang)
  448. {
  449. // Add libraries for this language that are not implied by the
  450. // linker language.
  451. std::string libVar = "CMAKE_";
  452. libVar += lang;
  453. libVar += "_IMPLICIT_LINK_LIBRARIES";
  454. if (const char* libs = this->Makefile->GetDefinition(libVar)) {
  455. std::vector<std::string> libsVec;
  456. cmSystemTools::ExpandListArgument(libs, libsVec);
  457. for (std::string const& i : libsVec) {
  458. if (this->ImplicitLinkLibs.find(i) == this->ImplicitLinkLibs.end()) {
  459. this->AddItem(i, nullptr);
  460. }
  461. }
  462. }
  463. // Add linker search paths for this language that are not
  464. // implied by the linker language.
  465. std::string dirVar = "CMAKE_";
  466. dirVar += lang;
  467. dirVar += "_IMPLICIT_LINK_DIRECTORIES";
  468. if (const char* dirs = this->Makefile->GetDefinition(dirVar)) {
  469. std::vector<std::string> dirsVec;
  470. cmSystemTools::ExpandListArgument(dirs, dirsVec);
  471. this->OrderLinkerSearchPath->AddLanguageDirectories(dirsVec);
  472. }
  473. }
  474. void cmComputeLinkInformation::AddItem(std::string const& item,
  475. cmGeneratorTarget const* tgt)
  476. {
  477. // Compute the proper name to use to link this library.
  478. const std::string& config = this->Config;
  479. bool impexe = (tgt && tgt->IsExecutableWithExports());
  480. if (impexe && !this->UseImportLibrary && !this->LoaderFlag) {
  481. // Skip linking to executables on platforms with no import
  482. // libraries or loader flags.
  483. return;
  484. }
  485. if (tgt && tgt->IsLinkable()) {
  486. // This is a CMake target. Ask the target for its real name.
  487. if (impexe && this->LoaderFlag) {
  488. // This link item is an executable that may provide symbols
  489. // used by this target. A special flag is needed on this
  490. // platform. Add it now.
  491. std::string linkItem;
  492. linkItem = this->LoaderFlag;
  493. cmStateEnums::ArtifactType artifact = this->UseImportLibrary
  494. ? cmStateEnums::ImportLibraryArtifact
  495. : cmStateEnums::RuntimeBinaryArtifact;
  496. std::string exe = tgt->GetFullPath(config, artifact, true);
  497. linkItem += exe;
  498. this->Items.emplace_back(linkItem, true, tgt);
  499. this->Depends.push_back(std::move(exe));
  500. } else if (tgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  501. // Add the interface library as an item so it can be considered as part
  502. // of COMPATIBLE_INTERFACE_ enforcement. The generators will ignore
  503. // this for the actual link line.
  504. this->Items.emplace_back(std::string(), false, tgt);
  505. // Also add the item the interface specifies to be used in its place.
  506. std::string const& libName = tgt->GetImportedLibName(config);
  507. if (!libName.empty()) {
  508. this->AddItem(libName, nullptr);
  509. }
  510. } else {
  511. // Decide whether to use an import library.
  512. bool implib =
  513. (this->UseImportLibrary &&
  514. (impexe || tgt->GetType() == cmStateEnums::SHARED_LIBRARY));
  515. cmStateEnums::ArtifactType artifact = implib
  516. ? cmStateEnums::ImportLibraryArtifact
  517. : cmStateEnums::RuntimeBinaryArtifact;
  518. // Pass the full path to the target file.
  519. std::string lib = tgt->GetFullPath(config, artifact, true);
  520. if (!this->LinkDependsNoShared ||
  521. tgt->GetType() != cmStateEnums::SHARED_LIBRARY) {
  522. this->Depends.push_back(lib);
  523. }
  524. this->AddTargetItem(lib, tgt);
  525. this->AddLibraryRuntimeInfo(lib, tgt);
  526. }
  527. } else {
  528. // This is not a CMake target. Use the name given.
  529. if (cmSystemTools::FileIsFullPath(item)) {
  530. if (cmSystemTools::FileIsDirectory(item)) {
  531. // This is a directory.
  532. this->AddDirectoryItem(item);
  533. } else {
  534. // Use the full path given to the library file.
  535. this->Depends.push_back(item);
  536. this->AddFullItem(item);
  537. this->AddLibraryRuntimeInfo(item);
  538. }
  539. } else {
  540. // This is a library or option specified by the user.
  541. this->AddUserItem(item, true);
  542. }
  543. }
  544. }
  545. void cmComputeLinkInformation::AddSharedDepItem(std::string const& item,
  546. const cmGeneratorTarget* tgt)
  547. {
  548. // If dropping shared library dependencies, ignore them.
  549. if (this->SharedDependencyMode == SharedDepModeNone) {
  550. return;
  551. }
  552. // The user may have incorrectly named an item. Skip items that are
  553. // not full paths to shared libraries.
  554. if (tgt) {
  555. // The target will provide a full path. Make sure it is a shared
  556. // library.
  557. if (tgt->GetType() != cmStateEnums::SHARED_LIBRARY) {
  558. return;
  559. }
  560. } else {
  561. // Skip items that are not full paths. We will not be able to
  562. // reliably specify them.
  563. if (!cmSystemTools::FileIsFullPath(item)) {
  564. return;
  565. }
  566. // Get the name of the library from the file name.
  567. std::string file = cmSystemTools::GetFilenameName(item);
  568. if (!this->ExtractSharedLibraryName.find(file)) {
  569. // This is not the name of a shared library.
  570. return;
  571. }
  572. }
  573. // If in linking mode, just link to the shared library.
  574. if (this->SharedDependencyMode == SharedDepModeLink) {
  575. this->AddItem(item, tgt);
  576. return;
  577. }
  578. // Get a full path to the dependent shared library.
  579. // Add it to the runtime path computation so that the target being
  580. // linked will be able to find it.
  581. std::string lib;
  582. if (tgt) {
  583. cmStateEnums::ArtifactType artifact = this->UseImportLibrary
  584. ? cmStateEnums::ImportLibraryArtifact
  585. : cmStateEnums::RuntimeBinaryArtifact;
  586. lib = tgt->GetFullPath(this->Config, artifact);
  587. this->AddLibraryRuntimeInfo(lib, tgt);
  588. } else {
  589. lib = item;
  590. this->AddLibraryRuntimeInfo(lib);
  591. }
  592. // Check if we need to include the dependent shared library in other
  593. // path ordering.
  594. cmOrderDirectories* order = nullptr;
  595. if (this->SharedDependencyMode == SharedDepModeLibDir &&
  596. !this->LinkWithRuntimePath /* AddLibraryRuntimeInfo adds it */) {
  597. // Add the item to the linker search path.
  598. order = this->OrderLinkerSearchPath;
  599. } else if (this->SharedDependencyMode == SharedDepModeDir) {
  600. // Add the item to the separate dependent library search path.
  601. order = this->OrderDependentRPath;
  602. }
  603. if (order) {
  604. if (tgt) {
  605. std::string soName = tgt->GetSOName(this->Config);
  606. const char* soname = soName.empty() ? nullptr : soName.c_str();
  607. order->AddRuntimeLibrary(lib, soname);
  608. } else {
  609. order->AddRuntimeLibrary(lib);
  610. }
  611. }
  612. }
  613. void cmComputeLinkInformation::ComputeLinkTypeInfo()
  614. {
  615. // Check whether archives may actually be shared libraries.
  616. this->ArchivesMayBeShared =
  617. this->CMakeInstance->GetState()->GetGlobalPropertyAsBool(
  618. "TARGET_ARCHIVES_MAY_BE_SHARED_LIBS");
  619. // First assume we cannot do link type stuff.
  620. this->LinkTypeEnabled = false;
  621. // Lookup link type selection flags.
  622. const char* static_link_type_flag = nullptr;
  623. const char* shared_link_type_flag = nullptr;
  624. const char* target_type_str = nullptr;
  625. switch (this->Target->GetType()) {
  626. case cmStateEnums::EXECUTABLE:
  627. target_type_str = "EXE";
  628. break;
  629. case cmStateEnums::SHARED_LIBRARY:
  630. target_type_str = "SHARED_LIBRARY";
  631. break;
  632. case cmStateEnums::MODULE_LIBRARY:
  633. target_type_str = "SHARED_MODULE";
  634. break;
  635. default:
  636. break;
  637. }
  638. if (target_type_str) {
  639. std::string static_link_type_flag_var = "CMAKE_";
  640. static_link_type_flag_var += target_type_str;
  641. static_link_type_flag_var += "_LINK_STATIC_";
  642. static_link_type_flag_var += this->LinkLanguage;
  643. static_link_type_flag_var += "_FLAGS";
  644. static_link_type_flag =
  645. this->Makefile->GetDefinition(static_link_type_flag_var);
  646. std::string shared_link_type_flag_var = "CMAKE_";
  647. shared_link_type_flag_var += target_type_str;
  648. shared_link_type_flag_var += "_LINK_DYNAMIC_";
  649. shared_link_type_flag_var += this->LinkLanguage;
  650. shared_link_type_flag_var += "_FLAGS";
  651. shared_link_type_flag =
  652. this->Makefile->GetDefinition(shared_link_type_flag_var);
  653. }
  654. // We can support link type switching only if all needed flags are
  655. // known.
  656. if (static_link_type_flag && *static_link_type_flag &&
  657. shared_link_type_flag && *shared_link_type_flag) {
  658. this->LinkTypeEnabled = true;
  659. this->StaticLinkTypeFlag = static_link_type_flag;
  660. this->SharedLinkTypeFlag = shared_link_type_flag;
  661. }
  662. // Lookup the starting link type from the target (linked statically?).
  663. const char* lss = this->Target->GetProperty("LINK_SEARCH_START_STATIC");
  664. this->StartLinkType = cmSystemTools::IsOn(lss) ? LinkStatic : LinkShared;
  665. this->CurrentLinkType = this->StartLinkType;
  666. }
  667. void cmComputeLinkInformation::ComputeItemParserInfo()
  668. {
  669. // Get possible library name prefixes.
  670. cmMakefile* mf = this->Makefile;
  671. this->AddLinkPrefix(mf->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  672. this->AddLinkPrefix(mf->GetDefinition("CMAKE_SHARED_LIBRARY_PREFIX"));
  673. // Import library names should be matched and treated as shared
  674. // libraries for the purposes of linking.
  675. this->AddLinkExtension(mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
  676. LinkShared);
  677. this->AddLinkExtension(mf->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
  678. LinkStatic);
  679. this->AddLinkExtension(mf->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
  680. LinkShared);
  681. this->AddLinkExtension(mf->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"),
  682. LinkUnknown);
  683. if (const char* linkSuffixes =
  684. mf->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS")) {
  685. std::vector<std::string> linkSuffixVec;
  686. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  687. for (std::string const& i : linkSuffixVec) {
  688. this->AddLinkExtension(i.c_str(), LinkUnknown);
  689. }
  690. }
  691. if (const char* sharedSuffixes =
  692. mf->GetDefinition("CMAKE_EXTRA_SHARED_LIBRARY_SUFFIXES")) {
  693. std::vector<std::string> sharedSuffixVec;
  694. cmSystemTools::ExpandListArgument(sharedSuffixes, sharedSuffixVec);
  695. for (std::string const& i : sharedSuffixVec) {
  696. this->AddLinkExtension(i.c_str(), LinkShared);
  697. }
  698. }
  699. // Compute a regex to match link extensions.
  700. std::string libext =
  701. this->CreateExtensionRegex(this->LinkExtensions, LinkUnknown);
  702. // Create regex to remove any library extension.
  703. std::string reg("(.*)");
  704. reg += libext;
  705. this->OrderLinkerSearchPath->SetLinkExtensionInfo(this->LinkExtensions, reg);
  706. // Create a regex to match a library name. Match index 1 will be
  707. // the prefix if it exists and empty otherwise. Match index 2 will
  708. // be the library name. Match index 3 will be the library
  709. // extension.
  710. reg = "^(";
  711. for (std::string const& p : this->LinkPrefixes) {
  712. reg += p;
  713. reg += "|";
  714. }
  715. reg += ")";
  716. reg += "([^/:]*)";
  717. // Create a regex to match any library name.
  718. std::string reg_any = reg;
  719. reg_any += libext;
  720. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  721. fprintf(stderr, "any regex [%s]\n", reg_any.c_str());
  722. #endif
  723. this->ExtractAnyLibraryName.compile(reg_any.c_str());
  724. // Create a regex to match static library names.
  725. if (!this->StaticLinkExtensions.empty()) {
  726. std::string reg_static = reg;
  727. reg_static +=
  728. this->CreateExtensionRegex(this->StaticLinkExtensions, LinkStatic);
  729. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  730. fprintf(stderr, "static regex [%s]\n", reg_static.c_str());
  731. #endif
  732. this->ExtractStaticLibraryName.compile(reg_static.c_str());
  733. }
  734. // Create a regex to match shared library names.
  735. if (!this->SharedLinkExtensions.empty()) {
  736. std::string reg_shared = reg;
  737. this->SharedRegexString =
  738. this->CreateExtensionRegex(this->SharedLinkExtensions, LinkShared);
  739. reg_shared += this->SharedRegexString;
  740. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  741. fprintf(stderr, "shared regex [%s]\n", reg_shared.c_str());
  742. #endif
  743. this->ExtractSharedLibraryName.compile(reg_shared.c_str());
  744. }
  745. }
  746. void cmComputeLinkInformation::AddLinkPrefix(const char* p)
  747. {
  748. if (p && *p) {
  749. this->LinkPrefixes.insert(p);
  750. }
  751. }
  752. void cmComputeLinkInformation::AddLinkExtension(const char* e, LinkType type)
  753. {
  754. if (e && *e) {
  755. if (type == LinkStatic) {
  756. this->StaticLinkExtensions.push_back(e);
  757. }
  758. if (type == LinkShared) {
  759. this->SharedLinkExtensions.push_back(e);
  760. }
  761. this->LinkExtensions.push_back(e);
  762. }
  763. }
  764. std::string cmComputeLinkInformation::CreateExtensionRegex(
  765. std::vector<std::string> const& exts, LinkType type)
  766. {
  767. // Build a list of extension choices.
  768. std::string libext = "(";
  769. const char* sep = "";
  770. for (std::string const& i : exts) {
  771. // Separate this choice from the previous one.
  772. libext += sep;
  773. sep = "|";
  774. // Store this extension choice with the "." escaped.
  775. libext += "\\";
  776. #if defined(_WIN32) && !defined(__CYGWIN__)
  777. libext += this->NoCaseExpression(i.c_str());
  778. #else
  779. libext += i;
  780. #endif
  781. }
  782. // Finish the list.
  783. libext += ")";
  784. // Add an optional OpenBSD-style version or major.minor.version component.
  785. if (this->OpenBSD || type == LinkShared) {
  786. libext += "(\\.[0-9]+)*";
  787. }
  788. libext += "$";
  789. return libext;
  790. }
  791. std::string cmComputeLinkInformation::NoCaseExpression(const char* str)
  792. {
  793. std::string ret;
  794. const char* s = str;
  795. while (*s) {
  796. if (*s == '.') {
  797. ret += *s;
  798. } else {
  799. ret += "[";
  800. ret += static_cast<char>(tolower(*s));
  801. ret += static_cast<char>(toupper(*s));
  802. ret += "]";
  803. }
  804. s++;
  805. }
  806. return ret;
  807. }
  808. void cmComputeLinkInformation::SetCurrentLinkType(LinkType lt)
  809. {
  810. // If we are changing the current link type add the flag to tell the
  811. // linker about it.
  812. if (this->CurrentLinkType != lt) {
  813. this->CurrentLinkType = lt;
  814. if (this->LinkTypeEnabled) {
  815. switch (this->CurrentLinkType) {
  816. case LinkStatic:
  817. this->Items.emplace_back(this->StaticLinkTypeFlag, false);
  818. break;
  819. case LinkShared:
  820. this->Items.emplace_back(this->SharedLinkTypeFlag, false);
  821. break;
  822. default:
  823. break;
  824. }
  825. }
  826. }
  827. }
  828. void cmComputeLinkInformation::AddTargetItem(std::string const& item,
  829. cmGeneratorTarget const* target)
  830. {
  831. // This is called to handle a link item that is a full path to a target.
  832. // If the target is not a static library make sure the link type is
  833. // shared. This is because dynamic-mode linking can handle both
  834. // shared and static libraries but static-mode can handle only
  835. // static libraries. If a previous user item changed the link type
  836. // to static we need to make sure it is back to shared.
  837. if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  838. this->SetCurrentLinkType(LinkShared);
  839. }
  840. // Keep track of shared library targets linked.
  841. if (target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  842. this->SharedLibrariesLinked.insert(target);
  843. }
  844. // Handle case of an imported shared library with no soname.
  845. if (this->NoSONameUsesPath &&
  846. target->IsImportedSharedLibWithoutSOName(this->Config)) {
  847. this->AddSharedLibNoSOName(item);
  848. return;
  849. }
  850. // If this platform wants a flag before the full path, add it.
  851. if (!this->LibLinkFileFlag.empty()) {
  852. this->Items.emplace_back(this->LibLinkFileFlag, false);
  853. }
  854. // For compatibility with CMake 2.4 include the item's directory in
  855. // the linker search path.
  856. if (this->OldLinkDirMode && !target->IsFrameworkOnApple() &&
  857. this->OldLinkDirMask.find(cmSystemTools::GetFilenamePath(item)) ==
  858. this->OldLinkDirMask.end()) {
  859. this->OldLinkDirItems.push_back(item);
  860. }
  861. // Now add the full path to the library.
  862. this->Items.emplace_back(item, true, target);
  863. }
  864. void cmComputeLinkInformation::AddFullItem(std::string const& item)
  865. {
  866. // Check for the implicit link directory special case.
  867. if (this->CheckImplicitDirItem(item)) {
  868. return;
  869. }
  870. // Check for case of shared library with no builtin soname.
  871. if (this->NoSONameUsesPath && this->CheckSharedLibNoSOName(item)) {
  872. return;
  873. }
  874. // Full path libraries should specify a valid library file name.
  875. // See documentation of CMP0008.
  876. std::string generator = this->GlobalGenerator->GetName();
  877. if (this->Target->GetPolicyStatusCMP0008() != cmPolicies::NEW &&
  878. (generator.find("Visual Studio") != std::string::npos ||
  879. generator.find("Xcode") != std::string::npos)) {
  880. std::string file = cmSystemTools::GetFilenameName(item);
  881. if (!this->ExtractAnyLibraryName.find(file.c_str())) {
  882. this->HandleBadFullItem(item, file);
  883. return;
  884. }
  885. }
  886. // This is called to handle a link item that is a full path.
  887. // If the target is not a static library make sure the link type is
  888. // shared. This is because dynamic-mode linking can handle both
  889. // shared and static libraries but static-mode can handle only
  890. // static libraries. If a previous user item changed the link type
  891. // to static we need to make sure it is back to shared.
  892. if (this->LinkTypeEnabled) {
  893. std::string name = cmSystemTools::GetFilenameName(item);
  894. if (this->ExtractSharedLibraryName.find(name)) {
  895. this->SetCurrentLinkType(LinkShared);
  896. } else if (!this->ExtractStaticLibraryName.find(item)) {
  897. // We cannot determine the type. Assume it is the target's
  898. // default type.
  899. this->SetCurrentLinkType(this->StartLinkType);
  900. }
  901. }
  902. // For compatibility with CMake 2.4 include the item's directory in
  903. // the linker search path.
  904. if (this->OldLinkDirMode &&
  905. this->OldLinkDirMask.find(cmSystemTools::GetFilenamePath(item)) ==
  906. this->OldLinkDirMask.end()) {
  907. this->OldLinkDirItems.push_back(item);
  908. }
  909. // If this platform wants a flag before the full path, add it.
  910. if (!this->LibLinkFileFlag.empty()) {
  911. this->Items.emplace_back(this->LibLinkFileFlag, false);
  912. }
  913. // Now add the full path to the library.
  914. this->Items.emplace_back(item, true);
  915. }
  916. bool cmComputeLinkInformation::CheckImplicitDirItem(std::string const& item)
  917. {
  918. // We only switch to a pathless item if the link type may be
  919. // enforced. Fortunately only platforms that support link types
  920. // seem to have magic per-architecture implicit link directories.
  921. if (!this->LinkTypeEnabled) {
  922. return false;
  923. }
  924. // Check if this item is in an implicit link directory.
  925. std::string dir = cmSystemTools::GetFilenamePath(item);
  926. if (this->ImplicitLinkDirs.find(dir) == this->ImplicitLinkDirs.end()) {
  927. // Only libraries in implicit link directories are converted to
  928. // pathless items.
  929. return false;
  930. }
  931. // Only apply the policy below if the library file is one that can
  932. // be found by the linker.
  933. std::string file = cmSystemTools::GetFilenameName(item);
  934. if (!this->ExtractAnyLibraryName.find(file)) {
  935. return false;
  936. }
  937. // Check the policy for whether we should use the approach below.
  938. switch (this->Target->GetPolicyStatusCMP0060()) {
  939. case cmPolicies::WARN:
  940. if (this->CMP0060Warn) {
  941. // Print the warning at most once for this item.
  942. std::string const& wid = "CMP0060-WARNING-GIVEN-" + item;
  943. if (!this->CMakeInstance->GetPropertyAsBool(wid)) {
  944. this->CMakeInstance->SetProperty(wid, "1");
  945. this->CMP0060WarnItems.insert(item);
  946. }
  947. }
  948. case cmPolicies::OLD:
  949. break;
  950. case cmPolicies::REQUIRED_ALWAYS:
  951. case cmPolicies::REQUIRED_IF_USED:
  952. case cmPolicies::NEW:
  953. return false;
  954. }
  955. // Many system linkers support multiple architectures by
  956. // automatically selecting the implicit linker search path for the
  957. // current architecture. If the library appears in an implicit link
  958. // directory then just report the file name without the directory
  959. // portion. This will allow the system linker to locate the proper
  960. // library for the architecture at link time.
  961. this->AddUserItem(file, false);
  962. // Make sure the link directory ordering will find the library.
  963. this->OrderLinkerSearchPath->AddLinkLibrary(item);
  964. return true;
  965. }
  966. void cmComputeLinkInformation::AddUserItem(std::string const& item,
  967. bool pathNotKnown)
  968. {
  969. // This is called to handle a link item that does not match a CMake
  970. // target and is not a full path. We check here if it looks like a
  971. // library file name to automatically request the proper link type
  972. // from the linker. For example:
  973. //
  974. // foo ==> -lfoo
  975. // libfoo.a ==> -Wl,-Bstatic -lfoo
  976. // Pass flags through untouched.
  977. if (item[0] == '-' || item[0] == '$' || item[0] == '`') {
  978. // if this is a -l option then we might need to warn about
  979. // CMP0003 so put it in OldUserFlagItems, if it is not a -l
  980. // or -Wl,-l (-framework -pthread), then allow it without a
  981. // CMP0003 as -L will not affect those other linker flags
  982. if (item.find("-l") == 0 || item.find("-Wl,-l") == 0) {
  983. // This is a linker option provided by the user.
  984. this->OldUserFlagItems.push_back(item);
  985. }
  986. // Restore the target link type since this item does not specify
  987. // one.
  988. this->SetCurrentLinkType(this->StartLinkType);
  989. // Use the item verbatim.
  990. this->Items.emplace_back(item, false);
  991. return;
  992. }
  993. // Parse out the prefix, base, and suffix components of the
  994. // library name. If the name matches that of a shared or static
  995. // library then set the link type accordingly.
  996. //
  997. // Search for shared library names first because some platforms
  998. // have shared libraries with names that match the static library
  999. // pattern. For example cygwin and msys use the convention
  1000. // libfoo.dll.a for import libraries and libfoo.a for static
  1001. // libraries. On AIX a library with the name libfoo.a can be
  1002. // shared!
  1003. std::string lib;
  1004. if (this->ExtractSharedLibraryName.find(item)) {
  1005. // This matches a shared library file name.
  1006. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  1007. fprintf(stderr, "shared regex matched [%s] [%s] [%s]\n",
  1008. this->ExtractSharedLibraryName.match(1).c_str(),
  1009. this->ExtractSharedLibraryName.match(2).c_str(),
  1010. this->ExtractSharedLibraryName.match(3).c_str());
  1011. #endif
  1012. // Set the link type to shared.
  1013. this->SetCurrentLinkType(LinkShared);
  1014. // Use just the library name so the linker will search.
  1015. lib = this->ExtractSharedLibraryName.match(2);
  1016. } else if (this->ExtractStaticLibraryName.find(item)) {
  1017. // This matches a static library file name.
  1018. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  1019. fprintf(stderr, "static regex matched [%s] [%s] [%s]\n",
  1020. this->ExtractStaticLibraryName.match(1).c_str(),
  1021. this->ExtractStaticLibraryName.match(2).c_str(),
  1022. this->ExtractStaticLibraryName.match(3).c_str());
  1023. #endif
  1024. // Set the link type to static.
  1025. this->SetCurrentLinkType(LinkStatic);
  1026. // Use just the library name so the linker will search.
  1027. lib = this->ExtractStaticLibraryName.match(2);
  1028. } else if (this->ExtractAnyLibraryName.find(item)) {
  1029. // This matches a library file name.
  1030. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  1031. fprintf(stderr, "any regex matched [%s] [%s] [%s]\n",
  1032. this->ExtractAnyLibraryName.match(1).c_str(),
  1033. this->ExtractAnyLibraryName.match(2).c_str(),
  1034. this->ExtractAnyLibraryName.match(3).c_str());
  1035. #endif
  1036. // Restore the target link type since this item does not specify
  1037. // one.
  1038. this->SetCurrentLinkType(this->StartLinkType);
  1039. // Use just the library name so the linker will search.
  1040. lib = this->ExtractAnyLibraryName.match(2);
  1041. } else {
  1042. // This is a name specified by the user.
  1043. if (pathNotKnown) {
  1044. this->OldUserFlagItems.push_back(item);
  1045. }
  1046. // We must ask the linker to search for a library with this name.
  1047. // Restore the target link type since this item does not specify
  1048. // one.
  1049. this->SetCurrentLinkType(this->StartLinkType);
  1050. lib = item;
  1051. }
  1052. // Create an option to ask the linker to search for the library.
  1053. std::string out = this->LibLinkFlag;
  1054. out += lib;
  1055. out += this->LibLinkSuffix;
  1056. this->Items.emplace_back(out, false);
  1057. // Here we could try to find the library the linker will find and
  1058. // add a runtime information entry for it. It would probably not be
  1059. // reliable and we want to encourage use of full paths for library
  1060. // specification.
  1061. }
  1062. void cmComputeLinkInformation::AddFrameworkItem(std::string const& item)
  1063. {
  1064. // Try to separate the framework name and path.
  1065. if (!this->SplitFramework.find(item.c_str())) {
  1066. std::ostringstream e;
  1067. e << "Could not parse framework path \"" << item << "\" "
  1068. << "linked by target " << this->Target->GetName() << ".";
  1069. cmSystemTools::Error(e.str().c_str());
  1070. return;
  1071. }
  1072. std::string fw_path = this->SplitFramework.match(1);
  1073. std::string fw = this->SplitFramework.match(2);
  1074. std::string full_fw = fw_path;
  1075. full_fw += "/";
  1076. full_fw += fw;
  1077. full_fw += ".framework";
  1078. full_fw += "/";
  1079. full_fw += fw;
  1080. // Add the directory portion to the framework search path.
  1081. this->AddFrameworkPath(fw_path);
  1082. // add runtime information
  1083. this->AddLibraryRuntimeInfo(full_fw);
  1084. // Add the item using the -framework option.
  1085. this->Items.emplace_back("-framework", false);
  1086. cmOutputConverter converter(this->Makefile->GetStateSnapshot());
  1087. fw = converter.EscapeForShell(fw);
  1088. this->Items.emplace_back(fw, false);
  1089. }
  1090. void cmComputeLinkInformation::AddDirectoryItem(std::string const& item)
  1091. {
  1092. if (this->Makefile->IsOn("APPLE") &&
  1093. cmSystemTools::IsPathToFramework(item.c_str())) {
  1094. this->AddFrameworkItem(item);
  1095. } else {
  1096. this->DropDirectoryItem(item);
  1097. }
  1098. }
  1099. void cmComputeLinkInformation::DropDirectoryItem(std::string const& item)
  1100. {
  1101. // A full path to a directory was found as a link item. Warn the
  1102. // user.
  1103. std::ostringstream e;
  1104. e << "WARNING: Target \"" << this->Target->GetName()
  1105. << "\" requests linking to directory \"" << item << "\". "
  1106. << "Targets may link only to libraries. "
  1107. << "CMake is dropping the item.";
  1108. cmSystemTools::Message(e.str().c_str());
  1109. }
  1110. void cmComputeLinkInformation::ComputeFrameworkInfo()
  1111. {
  1112. // Avoid adding implicit framework paths.
  1113. std::vector<std::string> implicitDirVec;
  1114. // Get platform-wide implicit directories.
  1115. if (const char* implicitLinks = this->Makefile->GetDefinition(
  1116. "CMAKE_PLATFORM_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES")) {
  1117. cmSystemTools::ExpandListArgument(implicitLinks, implicitDirVec);
  1118. }
  1119. // Get language-specific implicit directories.
  1120. std::string implicitDirVar = "CMAKE_";
  1121. implicitDirVar += this->LinkLanguage;
  1122. implicitDirVar += "_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES";
  1123. if (const char* implicitDirs =
  1124. this->Makefile->GetDefinition(implicitDirVar)) {
  1125. cmSystemTools::ExpandListArgument(implicitDirs, implicitDirVec);
  1126. }
  1127. this->FrameworkPathsEmmitted.insert(implicitDirVec.begin(),
  1128. implicitDirVec.end());
  1129. // Regular expression to extract a framework path and name.
  1130. this->SplitFramework.compile("(.*)/(.*)\\.framework$");
  1131. }
  1132. void cmComputeLinkInformation::AddFrameworkPath(std::string const& p)
  1133. {
  1134. if (this->FrameworkPathsEmmitted.insert(p).second) {
  1135. this->FrameworkPaths.push_back(p);
  1136. }
  1137. }
  1138. bool cmComputeLinkInformation::CheckSharedLibNoSOName(std::string const& item)
  1139. {
  1140. // This platform will use the path to a library as its soname if the
  1141. // library is given via path and was not built with an soname. If
  1142. // this is a shared library that might be the case.
  1143. std::string file = cmSystemTools::GetFilenameName(item);
  1144. if (this->ExtractSharedLibraryName.find(file)) {
  1145. // If we can guess the soname fairly reliably then assume the
  1146. // library has one. Otherwise assume the library has no builtin
  1147. // soname.
  1148. std::string soname;
  1149. if (!cmSystemTools::GuessLibrarySOName(item, soname)) {
  1150. this->AddSharedLibNoSOName(item);
  1151. return true;
  1152. }
  1153. }
  1154. return false;
  1155. }
  1156. void cmComputeLinkInformation::AddSharedLibNoSOName(std::string const& item)
  1157. {
  1158. // We have a full path to a shared library with no soname. We need
  1159. // to ask the linker to locate the item because otherwise the path
  1160. // we give to it will be embedded in the target linked. Then at
  1161. // runtime the dynamic linker will search for the library using the
  1162. // path instead of just the name.
  1163. std::string file = cmSystemTools::GetFilenameName(item);
  1164. this->AddUserItem(file, false);
  1165. // Make sure the link directory ordering will find the library.
  1166. this->OrderLinkerSearchPath->AddLinkLibrary(item);
  1167. }
  1168. void cmComputeLinkInformation::HandleBadFullItem(std::string const& item,
  1169. std::string const& file)
  1170. {
  1171. // Do not depend on things that do not exist.
  1172. std::vector<std::string>::iterator i =
  1173. std::find(this->Depends.begin(), this->Depends.end(), item);
  1174. if (i != this->Depends.end()) {
  1175. this->Depends.erase(i);
  1176. }
  1177. // Tell the linker to search for the item and provide the proper
  1178. // path for it. Do not contribute to any CMP0003 warning (do not
  1179. // put in OldLinkDirItems or OldUserFlagItems).
  1180. this->AddUserItem(file, false);
  1181. this->OrderLinkerSearchPath->AddLinkLibrary(item);
  1182. // Produce any needed message.
  1183. switch (this->Target->GetPolicyStatusCMP0008()) {
  1184. case cmPolicies::WARN: {
  1185. // Print the warning at most once for this item.
  1186. std::string wid = "CMP0008-WARNING-GIVEN-";
  1187. wid += item;
  1188. if (!this->CMakeInstance->GetState()->GetGlobalPropertyAsBool(wid)) {
  1189. this->CMakeInstance->GetState()->SetGlobalProperty(wid, "1");
  1190. std::ostringstream w;
  1191. /* clang-format off */
  1192. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0008) << "\n"
  1193. << "Target \"" << this->Target->GetName() << "\" links to item\n"
  1194. << " " << item << "\n"
  1195. << "which is a full-path but not a valid library file name.";
  1196. /* clang-format on */
  1197. this->CMakeInstance->IssueMessage(cmake::AUTHOR_WARNING, w.str(),
  1198. this->Target->GetBacktrace());
  1199. }
  1200. }
  1201. case cmPolicies::OLD:
  1202. // OLD behavior does not warn.
  1203. break;
  1204. case cmPolicies::NEW:
  1205. // NEW behavior will not get here.
  1206. break;
  1207. case cmPolicies::REQUIRED_IF_USED:
  1208. case cmPolicies::REQUIRED_ALWAYS: {
  1209. std::ostringstream e;
  1210. /* clang-format off */
  1211. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0008) << "\n"
  1212. << "Target \"" << this->Target->GetName() << "\" links to item\n"
  1213. << " " << item << "\n"
  1214. << "which is a full-path but not a valid library file name.";
  1215. /* clang-format on */
  1216. this->CMakeInstance->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1217. this->Target->GetBacktrace());
  1218. } break;
  1219. }
  1220. }
  1221. bool cmComputeLinkInformation::FinishLinkerSearchDirectories()
  1222. {
  1223. // Support broken projects if necessary.
  1224. if (this->OldLinkDirItems.empty() || this->OldUserFlagItems.empty() ||
  1225. !this->OldLinkDirMode) {
  1226. return true;
  1227. }
  1228. // Enforce policy constraints.
  1229. switch (this->Target->GetPolicyStatusCMP0003()) {
  1230. case cmPolicies::WARN:
  1231. if (!this->CMakeInstance->GetState()->GetGlobalPropertyAsBool(
  1232. "CMP0003-WARNING-GIVEN")) {
  1233. this->CMakeInstance->GetState()->SetGlobalProperty(
  1234. "CMP0003-WARNING-GIVEN", "1");
  1235. std::ostringstream w;
  1236. this->PrintLinkPolicyDiagnosis(w);
  1237. this->CMakeInstance->IssueMessage(cmake::AUTHOR_WARNING, w.str(),
  1238. this->Target->GetBacktrace());
  1239. }
  1240. case cmPolicies::OLD:
  1241. // OLD behavior is to add the paths containing libraries with
  1242. // known full paths as link directories.
  1243. break;
  1244. case cmPolicies::NEW:
  1245. // Should never happen due to assignment of OldLinkDirMode
  1246. return true;
  1247. case cmPolicies::REQUIRED_IF_USED:
  1248. case cmPolicies::REQUIRED_ALWAYS: {
  1249. std::ostringstream e;
  1250. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0003) << "\n";
  1251. this->PrintLinkPolicyDiagnosis(e);
  1252. this->CMakeInstance->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1253. this->Target->GetBacktrace());
  1254. return false;
  1255. }
  1256. }
  1257. // Add the link directories for full path items.
  1258. for (std::string const& i : this->OldLinkDirItems) {
  1259. this->OrderLinkerSearchPath->AddLinkLibrary(i);
  1260. }
  1261. return true;
  1262. }
  1263. void cmComputeLinkInformation::PrintLinkPolicyDiagnosis(std::ostream& os)
  1264. {
  1265. // Tell the user what to do.
  1266. /* clang-format off */
  1267. os << "Policy CMP0003 should be set before this line. "
  1268. << "Add code such as\n"
  1269. << " if(COMMAND cmake_policy)\n"
  1270. << " cmake_policy(SET CMP0003 NEW)\n"
  1271. << " endif(COMMAND cmake_policy)\n"
  1272. << "as early as possible but after the most recent call to "
  1273. << "cmake_minimum_required or cmake_policy(VERSION). ";
  1274. /* clang-format on */
  1275. // List the items that might need the old-style paths.
  1276. os << "This warning appears because target \"" << this->Target->GetName()
  1277. << "\" "
  1278. << "links to some libraries for which the linker must search:\n";
  1279. {
  1280. // Format the list of unknown items to be as short as possible while
  1281. // still fitting in the allowed width (a true solution would be the
  1282. // bin packing problem if we were allowed to change the order).
  1283. std::string::size_type max_size = 76;
  1284. std::string line;
  1285. const char* sep = " ";
  1286. for (std::string const& i : this->OldUserFlagItems) {
  1287. // If the addition of another item will exceed the limit then
  1288. // output the current line and reset it. Note that the separator
  1289. // is either " " or ", " which is always 2 characters.
  1290. if (!line.empty() && (line.size() + i.size() + 2) > max_size) {
  1291. os << line << "\n";
  1292. sep = " ";
  1293. line.clear();
  1294. }
  1295. line += sep;
  1296. line += i;
  1297. // Convert to the other separator.
  1298. sep = ", ";
  1299. }
  1300. if (!line.empty()) {
  1301. os << line << "\n";
  1302. }
  1303. }
  1304. // List the paths old behavior is adding.
  1305. os << "and other libraries with known full path:\n";
  1306. std::set<std::string> emitted;
  1307. for (std::string const& i : this->OldLinkDirItems) {
  1308. if (emitted.insert(cmSystemTools::GetFilenamePath(i)).second) {
  1309. os << " " << i << "\n";
  1310. }
  1311. }
  1312. // Explain.
  1313. os << "CMake is adding directories in the second list to the linker "
  1314. << "search path in case they are needed to find libraries from the "
  1315. << "first list (for backwards compatibility with CMake 2.4). "
  1316. << "Set policy CMP0003 to OLD or NEW to enable or disable this "
  1317. << "behavior explicitly. "
  1318. << "Run \"cmake --help-policy CMP0003\" for more information.";
  1319. }
  1320. void cmComputeLinkInformation::LoadImplicitLinkInfo()
  1321. {
  1322. std::vector<std::string> implicitDirVec;
  1323. // Get platform-wide implicit directories.
  1324. if (const char* implicitLinks = (this->Makefile->GetDefinition(
  1325. "CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES"))) {
  1326. cmSystemTools::ExpandListArgument(implicitLinks, implicitDirVec);
  1327. }
  1328. // Append library architecture to all implicit platform directories
  1329. // and add them to the set
  1330. if (const char* libraryArch =
  1331. this->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE")) {
  1332. for (std::string const& i : implicitDirVec) {
  1333. this->ImplicitLinkDirs.insert(i + "/" + libraryArch);
  1334. }
  1335. }
  1336. // Get language-specific implicit directories.
  1337. std::string implicitDirVar = "CMAKE_";
  1338. implicitDirVar += this->LinkLanguage;
  1339. implicitDirVar += "_IMPLICIT_LINK_DIRECTORIES";
  1340. if (const char* implicitDirs =
  1341. this->Makefile->GetDefinition(implicitDirVar)) {
  1342. cmSystemTools::ExpandListArgument(implicitDirs, implicitDirVec);
  1343. }
  1344. // Store implicit link directories.
  1345. this->ImplicitLinkDirs.insert(implicitDirVec.begin(), implicitDirVec.end());
  1346. // Get language-specific implicit libraries.
  1347. std::vector<std::string> implicitLibVec;
  1348. std::string implicitLibVar = "CMAKE_";
  1349. implicitLibVar += this->LinkLanguage;
  1350. implicitLibVar += "_IMPLICIT_LINK_LIBRARIES";
  1351. if (const char* implicitLibs =
  1352. this->Makefile->GetDefinition(implicitLibVar)) {
  1353. cmSystemTools::ExpandListArgument(implicitLibs, implicitLibVec);
  1354. }
  1355. // Store implicit link libraries.
  1356. for (std::string const& item : implicitLibVec) {
  1357. // Items starting in '-' but not '-l' are flags, not libraries,
  1358. // and should not be filtered by this implicit list.
  1359. if (item[0] != '-' || item[1] == 'l') {
  1360. this->ImplicitLinkLibs.insert(item);
  1361. }
  1362. }
  1363. // Get platform specific rpath link directories
  1364. if (const char* rpathDirs =
  1365. (this->Makefile->GetDefinition("CMAKE_PLATFORM_RUNTIME_PATH"))) {
  1366. cmSystemTools::ExpandListArgument(rpathDirs, this->RuntimeLinkDirs);
  1367. }
  1368. }
  1369. std::vector<std::string> const&
  1370. cmComputeLinkInformation::GetRuntimeSearchPath()
  1371. {
  1372. return this->OrderRuntimeSearchPath->GetOrderedDirectories();
  1373. }
  1374. void cmComputeLinkInformation::AddLibraryRuntimeInfo(
  1375. std::string const& fullPath, cmGeneratorTarget const* target)
  1376. {
  1377. // Ignore targets on Apple where install_name is not @rpath.
  1378. // The dependenty library can be found with other means such as
  1379. // @loader_path or full paths.
  1380. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1381. if (!target->HasMacOSXRpathInstallNameDir(this->Config)) {
  1382. return;
  1383. }
  1384. }
  1385. // Libraries with unknown type must be handled using just the file
  1386. // on disk.
  1387. if (target->GetType() == cmStateEnums::UNKNOWN_LIBRARY) {
  1388. this->AddLibraryRuntimeInfo(fullPath);
  1389. return;
  1390. }
  1391. // Skip targets that are not shared libraries (modules cannot be linked).
  1392. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1393. return;
  1394. }
  1395. // Try to get the soname of the library. Only files with this name
  1396. // could possibly conflict.
  1397. std::string soName = target->GetSOName(this->Config);
  1398. const char* soname = soName.empty() ? nullptr : soName.c_str();
  1399. // Include this library in the runtime path ordering.
  1400. this->OrderRuntimeSearchPath->AddRuntimeLibrary(fullPath, soname);
  1401. if (this->LinkWithRuntimePath) {
  1402. this->OrderLinkerSearchPath->AddRuntimeLibrary(fullPath, soname);
  1403. }
  1404. }
  1405. void cmComputeLinkInformation::AddLibraryRuntimeInfo(
  1406. std::string const& fullPath)
  1407. {
  1408. // Get the name of the library from the file name.
  1409. bool is_shared_library = false;
  1410. std::string file = cmSystemTools::GetFilenameName(fullPath);
  1411. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1412. // Check that @rpath is part of the install name.
  1413. // If it isn't, return.
  1414. std::string soname;
  1415. if (!cmSystemTools::GuessLibraryInstallName(fullPath, soname)) {
  1416. return;
  1417. }
  1418. if (soname.find("@rpath") == std::string::npos) {
  1419. return;
  1420. }
  1421. }
  1422. is_shared_library = this->ExtractSharedLibraryName.find(file);
  1423. if (!is_shared_library) {
  1424. // On some platforms (AIX) a shared library may look static.
  1425. if (this->ArchivesMayBeShared) {
  1426. if (this->ExtractStaticLibraryName.find(file.c_str())) {
  1427. // This is the name of a shared library or archive.
  1428. is_shared_library = true;
  1429. }
  1430. }
  1431. }
  1432. // It could be an Apple framework
  1433. if (!is_shared_library) {
  1434. if (fullPath.find(".framework") != std::string::npos) {
  1435. static cmsys::RegularExpression splitFramework(
  1436. "^(.*)/(.*).framework/(.*)$");
  1437. if (splitFramework.find(fullPath) &&
  1438. (std::string::npos !=
  1439. splitFramework.match(3).find(splitFramework.match(2)))) {
  1440. is_shared_library = true;
  1441. }
  1442. }
  1443. }
  1444. if (!is_shared_library) {
  1445. return;
  1446. }
  1447. // Include this library in the runtime path ordering.
  1448. this->OrderRuntimeSearchPath->AddRuntimeLibrary(fullPath);
  1449. if (this->LinkWithRuntimePath) {
  1450. this->OrderLinkerSearchPath->AddRuntimeLibrary(fullPath);
  1451. }
  1452. }
  1453. static void cmCLI_ExpandListUnique(const char* str,
  1454. std::vector<std::string>& out,
  1455. std::set<std::string>& emitted)
  1456. {
  1457. std::vector<std::string> tmp;
  1458. cmSystemTools::ExpandListArgument(str, tmp);
  1459. for (std::string const& i : tmp) {
  1460. if (emitted.insert(i).second) {
  1461. out.push_back(i);
  1462. }
  1463. }
  1464. }
  1465. void cmComputeLinkInformation::GetRPath(std::vector<std::string>& runtimeDirs,
  1466. bool for_install)
  1467. {
  1468. // Select whether to generate runtime search directories.
  1469. bool outputRuntime =
  1470. !this->Makefile->IsOn("CMAKE_SKIP_RPATH") && !this->RuntimeFlag.empty();
  1471. // Select whether to generate an rpath for the install tree or the
  1472. // build tree.
  1473. bool linking_for_install =
  1474. (for_install ||
  1475. this->Target->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"));
  1476. bool use_install_rpath =
  1477. (outputRuntime && this->Target->HaveInstallTreeRPATH() &&
  1478. linking_for_install);
  1479. bool use_build_rpath =
  1480. (outputRuntime && this->Target->HaveBuildTreeRPATH(this->Config) &&
  1481. !linking_for_install);
  1482. bool use_link_rpath = outputRuntime && linking_for_install &&
  1483. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH") &&
  1484. this->Target->GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
  1485. // Construct the RPATH.
  1486. std::set<std::string> emitted;
  1487. if (use_install_rpath) {
  1488. const char* install_rpath = this->Target->GetProperty("INSTALL_RPATH");
  1489. cmCLI_ExpandListUnique(install_rpath, runtimeDirs, emitted);
  1490. }
  1491. if (use_build_rpath) {
  1492. // Add directories explicitly specified by user
  1493. if (const char* build_rpath = this->Target->GetProperty("BUILD_RPATH")) {
  1494. cmCLI_ExpandListUnique(build_rpath, runtimeDirs, emitted);
  1495. }
  1496. }
  1497. if (use_build_rpath || use_link_rpath) {
  1498. std::string rootPath;
  1499. if (const char* sysrootLink =
  1500. this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  1501. rootPath = sysrootLink;
  1502. } else {
  1503. rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  1504. }
  1505. const char* stagePath =
  1506. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
  1507. const char* installPrefix =
  1508. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  1509. cmSystemTools::ConvertToUnixSlashes(rootPath);
  1510. std::vector<std::string> const& rdirs = this->GetRuntimeSearchPath();
  1511. for (std::string const& ri : rdirs) {
  1512. // Put this directory in the rpath if using build-tree rpath
  1513. // support or if using the link path as an rpath.
  1514. if (use_build_rpath) {
  1515. std::string d = ri;
  1516. if (!rootPath.empty() && d.find(rootPath) == 0) {
  1517. d = d.substr(rootPath.size());
  1518. } else if (stagePath && *stagePath && d.find(stagePath) == 0) {
  1519. std::string suffix = d.substr(strlen(stagePath));
  1520. d = installPrefix;
  1521. d += "/";
  1522. d += suffix;
  1523. cmSystemTools::ConvertToUnixSlashes(d);
  1524. }
  1525. if (emitted.insert(d).second) {
  1526. runtimeDirs.push_back(std::move(d));
  1527. }
  1528. } else if (use_link_rpath) {
  1529. // Do not add any path inside the source or build tree.
  1530. std::string const& topSourceDir =
  1531. this->CMakeInstance->GetHomeDirectory();
  1532. std::string const& topBinaryDir =
  1533. this->CMakeInstance->GetHomeOutputDirectory();
  1534. if (!cmSystemTools::ComparePath(ri, topSourceDir) &&
  1535. !cmSystemTools::ComparePath(ri, topBinaryDir) &&
  1536. !cmSystemTools::IsSubDirectory(ri, topSourceDir) &&
  1537. !cmSystemTools::IsSubDirectory(ri, topBinaryDir)) {
  1538. std::string d = ri;
  1539. if (!rootPath.empty() && d.find(rootPath) == 0) {
  1540. d = d.substr(rootPath.size());
  1541. } else if (stagePath && *stagePath && d.find(stagePath) == 0) {
  1542. std::string suffix = d.substr(strlen(stagePath));
  1543. d = installPrefix;
  1544. d += "/";
  1545. d += suffix;
  1546. cmSystemTools::ConvertToUnixSlashes(d);
  1547. }
  1548. if (emitted.insert(d).second) {
  1549. runtimeDirs.push_back(std::move(d));
  1550. }
  1551. }
  1552. }
  1553. }
  1554. }
  1555. // Add runtime paths required by the languages to always be
  1556. // present. This is done even when skipping rpath support.
  1557. {
  1558. cmGeneratorTarget::LinkClosure const* lc =
  1559. this->Target->GetLinkClosure(this->Config);
  1560. for (std::string const& li : lc->Languages) {
  1561. std::string useVar =
  1562. "CMAKE_" + li + "_USE_IMPLICIT_LINK_DIRECTORIES_IN_RUNTIME_PATH";
  1563. if (this->Makefile->IsOn(useVar)) {
  1564. std::string dirVar = "CMAKE_" + li + "_IMPLICIT_LINK_DIRECTORIES";
  1565. if (const char* dirs = this->Makefile->GetDefinition(dirVar)) {
  1566. cmCLI_ExpandListUnique(dirs, runtimeDirs, emitted);
  1567. }
  1568. }
  1569. }
  1570. }
  1571. // Add runtime paths required by the platform to always be
  1572. // present. This is done even when skipping rpath support.
  1573. cmCLI_ExpandListUnique(this->RuntimeAlways.c_str(), runtimeDirs, emitted);
  1574. }
  1575. std::string cmComputeLinkInformation::GetRPathString(bool for_install)
  1576. {
  1577. // Get the directories to use.
  1578. std::vector<std::string> runtimeDirs;
  1579. this->GetRPath(runtimeDirs, for_install);
  1580. // Concatenate the paths.
  1581. std::string rpath = cmJoin(runtimeDirs, this->GetRuntimeSep());
  1582. // If the rpath will be replaced at install time, prepare space.
  1583. if (!for_install && this->RuntimeUseChrpath) {
  1584. if (!rpath.empty()) {
  1585. // Add one trailing separator so the linker does not re-use the
  1586. // rpath .dynstr entry for a symbol name that happens to match
  1587. // the end of the rpath string.
  1588. rpath += this->GetRuntimeSep();
  1589. }
  1590. // Make sure it is long enough to hold the replacement value.
  1591. std::string::size_type minLength = this->GetChrpathString().length();
  1592. while (rpath.length() < minLength) {
  1593. rpath += this->GetRuntimeSep();
  1594. }
  1595. }
  1596. return rpath;
  1597. }
  1598. std::string cmComputeLinkInformation::GetChrpathString()
  1599. {
  1600. if (!this->RuntimeUseChrpath) {
  1601. return "";
  1602. }
  1603. return this->GetRPathString(true);
  1604. }