cmComputeLinkInformation.cxx 65 KB

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