cmComputeLinkInformation.cxx 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmComputeLinkInformation.h"
  14. #include "cmComputeLinkDepends.h"
  15. #include "cmOrderRuntimeDirectories.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmMakefile.h"
  19. #include "cmTarget.h"
  20. #include <ctype.h>
  21. /*
  22. Notes about linking on various platforms:
  23. ------------------------------------------------------------------------------
  24. Linux, FreeBSD, Mac OS X, IRIX, Sun, Windows:
  25. Linking to libraries using the full path works fine.
  26. ------------------------------------------------------------------------------
  27. On AIX, more work is needed.
  28. The "-bnoipath" option is needed. From "man ld":
  29. Note: If you specify a shared object, or an archive file
  30. containing a shared object, with an absolute or relative path
  31. name, instead of with the -lName flag, the path name is
  32. included in the import file ID string in the loader section of
  33. the output file. You can override this behavior with the
  34. -bnoipath option.
  35. noipath
  36. For shared objects listed on the command-line, rather than
  37. specified with the -l flag, use a null path component when
  38. listing the shared object in the loader section of the
  39. output file. A null path component is always used for
  40. shared objects specified with the -l flag. This option
  41. does not affect the specification of a path component by
  42. using a line beginning with #! in an import file. The
  43. default is the ipath option.
  44. This prevents the full path specified on the compile line from being
  45. compiled directly into the binary.
  46. By default the linker places -L paths in the embedded runtime path.
  47. In order to implement CMake's RPATH interface correctly, we need the
  48. -blibpath:Path option. From "man ld":
  49. libpath:Path
  50. Uses Path as the library path when writing the loader section
  51. of the output file. Path is neither checked for validity nor
  52. used when searching for libraries specified by the -l flag.
  53. Path overrides any library paths generated when the -L flag is
  54. used.
  55. If you do not specify any -L flags, or if you specify the
  56. nolibpath option, the default library path information is
  57. written in the loader section of the output file. The default
  58. library path information is the value of the LIBPATH
  59. environment variable if it is defined, and /usr/lib:/lib,
  60. otherwise.
  61. We can pass -Wl,-blibpath:/usr/lib:/lib always to avoid the -L stuff
  62. and not break when the user sets LIBPATH. Then if we want to add an
  63. rpath we insert it into the option before /usr/lib.
  64. ------------------------------------------------------------------------------
  65. On HP-UX, more work is needed. There are differences between
  66. versions.
  67. ld: 92453-07 linker linker ld B.10.33 990520
  68. Linking with a full path works okay for static and shared libraries.
  69. The linker seems to always put the full path to where the library
  70. was found in the binary whether using a full path or -lfoo syntax.
  71. Transitive link dependencies work just fine due to the full paths.
  72. It has the "-l:libfoo.sl" option. The +nodefaultrpath is accepted
  73. but not documented and does not seem to do anything. There is no
  74. +forceload option.
  75. ld: 92453-07 linker ld HP Itanium(R) B.12.41 IPF/IPF
  76. Linking with a full path works okay for static libraries.
  77. Linking with a full path works okay for shared libraries. However
  78. dependent (transitive) libraries of those linked directly must be
  79. either found with an rpath stored in the direct dependencies or
  80. found in -L paths as if they were specified with "-l:libfoo.sl"
  81. (really "-l:<soname>"). The search matches that of the dynamic
  82. loader but only with -L paths. In other words, if we have an
  83. executable that links to shared library bar which links to shared
  84. library foo, the link line for the exe must contain
  85. /dir/with/bar/libbar.sl -L/dir/with/foo
  86. It does not matter whether the exe wants to link to foo directly or
  87. whether /dir/with/foo/libfoo.sl is listed. The -L path must still
  88. be present. It should match the runtime path computed for the
  89. executable taking all directly and transitively linked libraries
  90. into account.
  91. The "+nodefaultrpath" option should be used to avoid getting -L
  92. paths in the rpath unless we add our own rpath with +b. This means
  93. that skip-build-rpath should use this option.
  94. See documentation in "man ld", "man dld.so", and
  95. http://docs.hp.com/en/B2355-90968/creatingandusinglibraries.htm
  96. +[no]defaultrpath
  97. +defaultrpath is the default. Include any paths that are
  98. specified with -L in the embedded path, unless you specify the
  99. +b option. If you use +b, only the path list specified by +b is
  100. in the embedded path.
  101. The +nodefaultrpath option removes all library paths that were
  102. specified with the -L option from the embedded path. The linker
  103. searches the library paths specified by the -L option at link
  104. time. At run time, the only library paths searched are those
  105. specified by the environment variables LD_LIBRARY_PATH and
  106. SHLIB_PATH, library paths specified by the +b linker option, and
  107. finally the default library paths.
  108. +rpathfirst
  109. This option will cause the paths specified in RPATH (embedded
  110. path) to be used before the paths specified in LD_LIBRARY_PATH
  111. or SHLIB_PATH, in searching for shared libraries. This changes
  112. the default search order of LD_LIBRARY_PATH, SHLIB_PATH, and
  113. RPATH (embedded path).
  114. ------------------------------------------------------------------------------
  115. Notes about dependent (transitive) shared libraries:
  116. On non-Windows systems shared libraries may have transitive
  117. dependencies. In order to support LINK_INTERFACE_LIBRARIES we must
  118. support linking to a shared library without listing all the libraries
  119. to which it links. Some linkers want to be able to find the
  120. transitive dependencies (dependent libraries) of shared libraries
  121. listed on the command line.
  122. - On Windows, DLLs are not directly linked, and the import libraries
  123. have no transitive dependencies.
  124. - On Mac, we need to actually list the transitive dependencies.
  125. Otherwise when using -isysroot for universal binaries it cannot
  126. find the dependent libraries. Listing them on the command line
  127. tells the linker where to find them, but unfortunately also links
  128. the library.
  129. - On HP-UX, the linker wants to find the transitive dependencies of
  130. shared libraries in the -L paths even if the dependent libraries
  131. are given on the link line.
  132. - On AIX the transitive dependencies are not needed.
  133. - On SGI, the linker wants to find the transitive dependencies of
  134. shared libraries in the -L paths if they are not given on the link
  135. line. Transitive linking can be disabled using the options
  136. -no_transitive_link -Wl,-no_transitive_link
  137. which disable it. Both options must be given when invoking the
  138. linker through the compiler.
  139. - On Sun, the linker wants to find the transitive dependencies of
  140. shared libraries in the -L paths if they are not given on the link
  141. line.
  142. - On Linux, FreeBSD, and QNX:
  143. The linker wants to find the transitive dependencies of shared
  144. libraries in the "-rpath-link" paths option if they have not been
  145. given on the link line. The option is like rpath but just for
  146. link time:
  147. -Wl,-rpath-link,"/path1:/path2"
  148. For -rpath-link, we need a separate runtime path ordering pass
  149. including just the dependent libraries that are not linked.
  150. For -L paths on non-HP, we can do the same thing as with rpath-link
  151. but put the results in -L paths. The paths should be listed at the
  152. end to avoid conflicting with user search paths (?).
  153. For -L paths on HP, we should do a runtime path ordering pass with
  154. all libraries, both linked and non-linked. Even dependent
  155. libraries that are also linked need to be listed in -L paths.
  156. In our implementation we add all dependent libraries to the runtime
  157. path computation. Then the auto-generated RPATH will find everything.
  158. */
  159. //----------------------------------------------------------------------------
  160. cmComputeLinkInformation
  161. ::cmComputeLinkInformation(cmTarget* target, const char* config)
  162. {
  163. // Store context information.
  164. this->Target = target;
  165. this->Makefile = this->Target->GetMakefile();
  166. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  167. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  168. // The configuration being linked.
  169. this->Config = config;
  170. // Allocate internals.
  171. this->OrderRuntimeSearchPath =
  172. new cmOrderRuntimeDirectories(this->GlobalGenerator, target->GetName(),
  173. "runtime path");
  174. this->OrderDependentRPath = 0;
  175. // Get the language used for linking this target.
  176. this->LinkLanguage =
  177. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  178. if(!this->LinkLanguage)
  179. {
  180. // The Compute method will do nothing, so skip the rest of the
  181. // initialization.
  182. return;
  183. }
  184. // Check whether we should use an import library for linking a target.
  185. this->UseImportLibrary =
  186. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")?true:false;
  187. // On platforms without import libraries there may be a special flag
  188. // to use when creating a plugin (module) that obtains symbols from
  189. // the program that will load it.
  190. this->LoaderFlag = 0;
  191. if(!this->UseImportLibrary &&
  192. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  193. {
  194. std::string loader_flag_var = "CMAKE_SHARED_MODULE_LOADER_";
  195. loader_flag_var += this->LinkLanguage;
  196. loader_flag_var += "_FLAG";
  197. this->LoaderFlag = this->Makefile->GetDefinition(loader_flag_var.c_str());
  198. }
  199. // Get options needed to link libraries.
  200. this->LibLinkFlag =
  201. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  202. this->LibLinkFileFlag =
  203. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FILE_FLAG");
  204. this->LibLinkSuffix =
  205. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  206. // Get options needed to specify RPATHs.
  207. this->RuntimeUseChrpath = false;
  208. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  209. {
  210. const char* tType =
  211. ((this->Target->GetType() == cmTarget::EXECUTABLE)?
  212. "EXECUTABLE" : "SHARED_LIBRARY");
  213. std::string rtVar = "CMAKE_";
  214. rtVar += tType;
  215. rtVar += "_RUNTIME_";
  216. rtVar += this->LinkLanguage;
  217. rtVar += "_FLAG";
  218. std::string rtSepVar = rtVar + "_SEP";
  219. this->RuntimeFlag = this->Makefile->GetSafeDefinition(rtVar.c_str());
  220. this->RuntimeSep = this->Makefile->GetSafeDefinition(rtSepVar.c_str());
  221. this->RuntimeAlways =
  222. (this->Makefile->
  223. GetSafeDefinition("CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH"));
  224. this->RuntimeUseChrpath = this->Target->IsChrpathUsed();
  225. // Get options needed to help find dependent libraries.
  226. std::string rlVar = "CMAKE_";
  227. rlVar += tType;
  228. rlVar += "_RPATH_LINK_";
  229. rlVar += this->LinkLanguage;
  230. rlVar += "_FLAG";
  231. this->RPathLinkFlag = this->Makefile->GetSafeDefinition(rlVar.c_str());
  232. }
  233. // Get link type information.
  234. this->ComputeLinkTypeInfo();
  235. // Setup the link item parser.
  236. this->ComputeItemParserInfo();
  237. // Setup framework support.
  238. this->ComputeFrameworkInfo();
  239. // Choose a mode for dealing with shared library dependencies.
  240. this->SharedDependencyMode = SharedDepModeNone;
  241. if(this->Makefile->IsOn("CMAKE_LINK_DEPENDENT_LIBRARY_FILES"))
  242. {
  243. this->SharedDependencyMode = SharedDepModeLink;
  244. }
  245. else if(this->Makefile->IsOn("CMAKE_LINK_DEPENDENT_LIBRARY_DIRS"))
  246. {
  247. this->SharedDependencyMode = SharedDepModeDir;
  248. }
  249. else if(!this->RPathLinkFlag.empty())
  250. {
  251. this->SharedDependencyMode = SharedDepModeDir;
  252. }
  253. if(this->SharedDependencyMode == SharedDepModeDir)
  254. {
  255. this->OrderDependentRPath =
  256. new cmOrderRuntimeDirectories(this->GlobalGenerator, target->GetName(),
  257. "dependent library path");
  258. }
  259. // Get the implicit link directories for this platform.
  260. if(const char* implicitLinks =
  261. (this->Makefile->GetDefinition
  262. ("CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES")))
  263. {
  264. std::vector<std::string> implicitLinkVec;
  265. cmSystemTools::ExpandListArgument(implicitLinks, implicitLinkVec);
  266. for(std::vector<std::string>::const_iterator
  267. i = implicitLinkVec.begin();
  268. i != implicitLinkVec.end(); ++i)
  269. {
  270. this->ImplicitLinkDirs.insert(*i);
  271. }
  272. }
  273. // Initial state.
  274. this->HaveUserFlagItem = false;
  275. // Decide whether to enable compatible library search path mode.
  276. // There exists code that effectively does
  277. //
  278. // /path/to/libA.so -lB
  279. //
  280. // where -lB is meant to link to /path/to/libB.so. This is broken
  281. // because it specified -lB without specifying a link directory (-L)
  282. // in which to search for B. This worked in CMake 2.4 and below
  283. // because -L/path/to would be added by the -L/-l split for A. In
  284. // order to support such projects we need to add the directories
  285. // containing libraries linked with a full path to the -L path.
  286. this->OldLinkDirMode = false;
  287. if(this->Makefile->IsOn("CMAKE_LINK_OLD_PATHS") ||
  288. this->Makefile->GetLocalGenerator()
  289. ->NeedBackwardsCompatibility(2, 4))
  290. {
  291. this->OldLinkDirMode = true;
  292. }
  293. }
  294. //----------------------------------------------------------------------------
  295. cmComputeLinkInformation::~cmComputeLinkInformation()
  296. {
  297. delete this->OrderRuntimeSearchPath;
  298. delete this->OrderDependentRPath;
  299. }
  300. //----------------------------------------------------------------------------
  301. cmComputeLinkInformation::ItemVector const&
  302. cmComputeLinkInformation::GetItems()
  303. {
  304. return this->Items;
  305. }
  306. //----------------------------------------------------------------------------
  307. std::vector<std::string> const& cmComputeLinkInformation::GetDirectories()
  308. {
  309. return this->Directories;
  310. }
  311. //----------------------------------------------------------------------------
  312. std::string cmComputeLinkInformation::GetRPathLinkString()
  313. {
  314. // If there is no separate linker runtime search flag (-rpath-link)
  315. // there is no reason to compute a string.
  316. if(!this->OrderDependentRPath || this->RPathLinkFlag.empty())
  317. {
  318. return "";
  319. }
  320. // Construct the linker runtime search path.
  321. std::string rpath_link;
  322. const char* sep = "";
  323. std::vector<std::string> const& dirs =
  324. this->OrderDependentRPath->GetRuntimePath();
  325. for(std::vector<std::string>::const_iterator di = dirs.begin();
  326. di != dirs.end(); ++di)
  327. {
  328. rpath_link += sep;
  329. sep = ":";
  330. rpath_link += *di;
  331. }
  332. return rpath_link;
  333. }
  334. //----------------------------------------------------------------------------
  335. std::vector<std::string> const& cmComputeLinkInformation::GetDepends()
  336. {
  337. return this->Depends;
  338. }
  339. //----------------------------------------------------------------------------
  340. std::vector<std::string> const& cmComputeLinkInformation::GetFrameworkPaths()
  341. {
  342. return this->FrameworkPaths;
  343. }
  344. //----------------------------------------------------------------------------
  345. std::set<cmTarget*> const&
  346. cmComputeLinkInformation::GetSharedLibrariesLinked()
  347. {
  348. return this->SharedLibrariesLinked;
  349. }
  350. //----------------------------------------------------------------------------
  351. bool cmComputeLinkInformation::Compute()
  352. {
  353. // Skip targets that do not link.
  354. if(!(this->Target->GetType() == cmTarget::EXECUTABLE ||
  355. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  356. this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
  357. this->Target->GetType() == cmTarget::STATIC_LIBRARY))
  358. {
  359. return false;
  360. }
  361. // We require a link language for the target.
  362. if(!this->LinkLanguage)
  363. {
  364. cmSystemTools::
  365. Error("CMake can not determine linker language for target:",
  366. this->Target->GetName());
  367. return false;
  368. }
  369. // Compute the ordered link line items.
  370. cmComputeLinkDepends cld(this->Target, this->Config);
  371. cmComputeLinkDepends::EntryVector const& linkEntries = cld.Compute();
  372. // Add the link line items.
  373. for(cmComputeLinkDepends::EntryVector::const_iterator
  374. lei = linkEntries.begin();
  375. lei != linkEntries.end(); ++lei)
  376. {
  377. if(lei->IsSharedDep)
  378. {
  379. this->AddSharedDepItem(lei->Item, lei->Target);
  380. }
  381. else
  382. {
  383. this->AddItem(lei->Item, lei->Target);
  384. }
  385. }
  386. // Restore the target link type so the correct system runtime
  387. // libraries are found.
  388. const char* lss = this->Target->GetProperty("LINK_SEARCH_END_STATIC");
  389. if(cmSystemTools::IsOn(lss))
  390. {
  391. this->SetCurrentLinkType(LinkStatic);
  392. }
  393. else
  394. {
  395. this->SetCurrentLinkType(this->StartLinkType);
  396. }
  397. // Compute the linker search path.
  398. this->ComputeLinkerSearchDirectories();
  399. return true;
  400. }
  401. //----------------------------------------------------------------------------
  402. void cmComputeLinkInformation::AddItem(std::string const& item, cmTarget* tgt)
  403. {
  404. // Compute the proper name to use to link this library.
  405. const char* config = this->Config;
  406. bool impexe = (tgt && tgt->IsExecutableWithExports());
  407. if(impexe && !this->UseImportLibrary && !this->LoaderFlag)
  408. {
  409. // Skip linking to executables on platforms with no import
  410. // libraries or loader flags.
  411. return;
  412. }
  413. if(tgt && (tgt->GetType() == cmTarget::STATIC_LIBRARY ||
  414. tgt->GetType() == cmTarget::SHARED_LIBRARY ||
  415. tgt->GetType() == cmTarget::MODULE_LIBRARY ||
  416. impexe))
  417. {
  418. // This is a CMake target. Ask the target for its real name.
  419. if(impexe && this->LoaderFlag)
  420. {
  421. // This link item is an executable that may provide symbols
  422. // used by this target. A special flag is needed on this
  423. // platform. Add it now.
  424. std::string linkItem;
  425. linkItem = this->LoaderFlag;
  426. std::string exe = tgt->GetFullPath(config, this->UseImportLibrary);
  427. linkItem += exe;
  428. this->Items.push_back(Item(linkItem, true));
  429. this->Depends.push_back(exe);
  430. }
  431. else
  432. {
  433. // Decide whether to use an import library.
  434. bool implib =
  435. (this->UseImportLibrary &&
  436. (impexe || tgt->GetType() == cmTarget::SHARED_LIBRARY));
  437. // Pass the full path to the target file.
  438. std::string lib = tgt->GetFullPath(config, implib);
  439. this->Depends.push_back(lib);
  440. if(tgt->IsFrameworkOnApple())
  441. {
  442. // Frameworks on OS X need only the framework directory to
  443. // link.
  444. std::string fw = tgt->GetDirectory(config, implib);
  445. this->AddFrameworkItem(fw);
  446. }
  447. else
  448. {
  449. this->AddTargetItem(lib, tgt);
  450. this->AddLibraryRuntimeInfo(lib, tgt);
  451. }
  452. }
  453. }
  454. else
  455. {
  456. // This is not a CMake target. Use the name given.
  457. if(cmSystemTools::FileIsFullPath(item.c_str()))
  458. {
  459. if(cmSystemTools::FileIsDirectory(item.c_str()))
  460. {
  461. // This is a directory.
  462. this->AddDirectoryItem(item);
  463. }
  464. else
  465. {
  466. // Use the full path given to the library file.
  467. this->Depends.push_back(item);
  468. this->AddFullItem(item);
  469. this->AddLibraryRuntimeInfo(item);
  470. }
  471. }
  472. else
  473. {
  474. // This is a library or option specified by the user.
  475. this->AddUserItem(item);
  476. }
  477. }
  478. }
  479. //----------------------------------------------------------------------------
  480. void cmComputeLinkInformation::AddSharedDepItem(std::string const& item,
  481. cmTarget* tgt)
  482. {
  483. // If dropping shared library dependencies, ignore them.
  484. if(this->SharedDependencyMode == SharedDepModeNone)
  485. {
  486. return;
  487. }
  488. // The user may have incorrectly named an item. Skip items that are
  489. // not full paths to shared libraries.
  490. if(tgt)
  491. {
  492. // The target will provide a full path. Make sure it is a shared
  493. // library.
  494. if(tgt->GetType() != cmTarget::SHARED_LIBRARY)
  495. {
  496. return;
  497. }
  498. }
  499. else
  500. {
  501. // Skip items that are not full paths. We will not be able to
  502. // reliably specify them.
  503. if(!cmSystemTools::FileIsFullPath(item.c_str()))
  504. {
  505. return;
  506. }
  507. // Get the name of the library from the file name.
  508. std::string file = cmSystemTools::GetFilenameName(item);
  509. if(!this->ExtractSharedLibraryName.find(file.c_str()))
  510. {
  511. // This is not the name of a shared library.
  512. return;
  513. }
  514. }
  515. // If in linking mode, just link to the shared library.
  516. if(this->SharedDependencyMode == SharedDepModeLink)
  517. {
  518. this->AddItem(item, tgt);
  519. return;
  520. }
  521. // Get a full path to the dependent shared library.
  522. // Add it to the runtime path computation so that the target being
  523. // linked will be able to find it.
  524. std::string lib;
  525. if(tgt)
  526. {
  527. lib = tgt->GetFullPath(this->Config, this->UseImportLibrary);
  528. this->AddLibraryRuntimeInfo(lib, tgt);
  529. }
  530. else
  531. {
  532. lib = item;
  533. this->AddLibraryRuntimeInfo(lib);
  534. }
  535. // Add the item to the separate dependent library search path if
  536. // this platform wants one.
  537. if(this->OrderDependentRPath)
  538. {
  539. if(tgt)
  540. {
  541. std::string soName = tgt->GetSOName(this->Config);
  542. const char* soname = soName.empty()? 0 : soName.c_str();
  543. this->OrderDependentRPath->AddLibrary(lib, soname);
  544. }
  545. else
  546. {
  547. this->OrderDependentRPath->AddLibrary(lib);
  548. }
  549. }
  550. }
  551. //----------------------------------------------------------------------------
  552. void cmComputeLinkInformation::ComputeLinkTypeInfo()
  553. {
  554. // First assume we cannot do link type stuff.
  555. this->LinkTypeEnabled = false;
  556. // Lookup link type selection flags.
  557. const char* static_link_type_flag = 0;
  558. const char* shared_link_type_flag = 0;
  559. const char* target_type_str = 0;
  560. switch(this->Target->GetType())
  561. {
  562. case cmTarget::EXECUTABLE: target_type_str = "EXE"; break;
  563. case cmTarget::SHARED_LIBRARY: target_type_str = "SHARED_LIBRARY"; break;
  564. case cmTarget::MODULE_LIBRARY: target_type_str = "SHARED_MODULE"; break;
  565. default: break;
  566. }
  567. if(target_type_str)
  568. {
  569. std::string static_link_type_flag_var = "CMAKE_";
  570. static_link_type_flag_var += target_type_str;
  571. static_link_type_flag_var += "_LINK_STATIC_";
  572. static_link_type_flag_var += this->LinkLanguage;
  573. static_link_type_flag_var += "_FLAGS";
  574. static_link_type_flag =
  575. this->Makefile->GetDefinition(static_link_type_flag_var.c_str());
  576. std::string shared_link_type_flag_var = "CMAKE_";
  577. shared_link_type_flag_var += target_type_str;
  578. shared_link_type_flag_var += "_LINK_DYNAMIC_";
  579. shared_link_type_flag_var += this->LinkLanguage;
  580. shared_link_type_flag_var += "_FLAGS";
  581. shared_link_type_flag =
  582. this->Makefile->GetDefinition(shared_link_type_flag_var.c_str());
  583. }
  584. // We can support link type switching only if all needed flags are
  585. // known.
  586. if(static_link_type_flag && *static_link_type_flag &&
  587. shared_link_type_flag && *shared_link_type_flag)
  588. {
  589. this->LinkTypeEnabled = true;
  590. this->StaticLinkTypeFlag = static_link_type_flag;
  591. this->SharedLinkTypeFlag = shared_link_type_flag;
  592. }
  593. // TODO: Lookup the starting link type from the target (is it being
  594. // linked statically?).
  595. this->StartLinkType = LinkShared;
  596. this->CurrentLinkType = this->StartLinkType;
  597. }
  598. //----------------------------------------------------------------------------
  599. void cmComputeLinkInformation::ComputeItemParserInfo()
  600. {
  601. // Get possible library name prefixes.
  602. cmMakefile* mf = this->Makefile;
  603. this->AddLinkPrefix(mf->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  604. this->AddLinkPrefix(mf->GetDefinition("CMAKE_SHARED_LIBRARY_PREFIX"));
  605. // Import library names should be matched and treated as shared
  606. // libraries for the purposes of linking.
  607. this->AddLinkExtension(mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
  608. LinkShared);
  609. this->AddLinkExtension(mf->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
  610. LinkStatic);
  611. this->AddLinkExtension(mf->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
  612. LinkShared);
  613. this->AddLinkExtension(mf->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"),
  614. LinkUnknown);
  615. if(const char* linkSuffixes =
  616. mf->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS"))
  617. {
  618. std::vector<std::string> linkSuffixVec;
  619. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  620. for(std::vector<std::string>::iterator i = linkSuffixVec.begin();
  621. i != linkSuffixVec.end(); ++i)
  622. {
  623. this->AddLinkExtension(i->c_str(), LinkUnknown);
  624. }
  625. }
  626. // Compute a regex to match link extensions.
  627. std::string libext = this->CreateExtensionRegex(this->LinkExtensions);
  628. // Create regex to remove any library extension.
  629. std::string reg("(.*)");
  630. reg += libext;
  631. this->RemoveLibraryExtension.compile(reg.c_str());
  632. // Create a regex to match a library name. Match index 1 will be
  633. // the prefix if it exists and empty otherwise. Match index 2 will
  634. // be the library name. Match index 3 will be the library
  635. // extension.
  636. reg = "^(";
  637. for(std::set<cmStdString>::iterator p = this->LinkPrefixes.begin();
  638. p != this->LinkPrefixes.end(); ++p)
  639. {
  640. reg += *p;
  641. reg += "|";
  642. }
  643. reg += ")";
  644. reg += "([^/]*)";
  645. // Create a regex to match any library name.
  646. std::string reg_any = reg;
  647. reg_any += libext;
  648. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  649. fprintf(stderr, "any regex [%s]\n", reg_any.c_str());
  650. #endif
  651. this->ExtractAnyLibraryName.compile(reg_any.c_str());
  652. // Create a regex to match static library names.
  653. if(!this->StaticLinkExtensions.empty())
  654. {
  655. std::string reg_static = reg;
  656. reg_static += this->CreateExtensionRegex(this->StaticLinkExtensions);
  657. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  658. fprintf(stderr, "static regex [%s]\n", reg_static.c_str());
  659. #endif
  660. this->ExtractStaticLibraryName.compile(reg_static.c_str());
  661. }
  662. // Create a regex to match shared library names.
  663. if(!this->SharedLinkExtensions.empty())
  664. {
  665. std::string reg_shared = reg;
  666. reg_shared += this->CreateExtensionRegex(this->SharedLinkExtensions);
  667. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  668. fprintf(stderr, "shared regex [%s]\n", reg_shared.c_str());
  669. #endif
  670. this->ExtractSharedLibraryName.compile(reg_shared.c_str());
  671. }
  672. }
  673. //----------------------------------------------------------------------------
  674. void cmComputeLinkInformation::AddLinkPrefix(const char* p)
  675. {
  676. if(p)
  677. {
  678. this->LinkPrefixes.insert(p);
  679. }
  680. }
  681. //----------------------------------------------------------------------------
  682. void cmComputeLinkInformation::AddLinkExtension(const char* e, LinkType type)
  683. {
  684. if(e && *e)
  685. {
  686. if(type == LinkStatic)
  687. {
  688. this->StaticLinkExtensions.push_back(e);
  689. }
  690. if(type == LinkShared)
  691. {
  692. this->SharedLinkExtensions.push_back(e);
  693. }
  694. this->LinkExtensions.push_back(e);
  695. }
  696. }
  697. //----------------------------------------------------------------------------
  698. std::string
  699. cmComputeLinkInformation
  700. ::CreateExtensionRegex(std::vector<std::string> const& exts)
  701. {
  702. // Build a list of extension choices.
  703. std::string libext = "(";
  704. const char* sep = "";
  705. for(std::vector<std::string>::const_iterator i = exts.begin();
  706. i != exts.end(); ++i)
  707. {
  708. // Separate this choice from the previous one.
  709. libext += sep;
  710. sep = "|";
  711. // Store this extension choice with the "." escaped.
  712. libext += "\\";
  713. #if defined(_WIN32) && !defined(__CYGWIN__)
  714. libext += this->NoCaseExpression(i->c_str());
  715. #else
  716. libext += *i;
  717. #endif
  718. }
  719. // Finish the list.
  720. libext += ").*";
  721. return libext;
  722. }
  723. //----------------------------------------------------------------------------
  724. std::string cmComputeLinkInformation::NoCaseExpression(const char* str)
  725. {
  726. std::string ret;
  727. const char* s = str;
  728. while(*s)
  729. {
  730. if(*s == '.')
  731. {
  732. ret += *s;
  733. }
  734. else
  735. {
  736. ret += "[";
  737. ret += tolower(*s);
  738. ret += toupper(*s);
  739. ret += "]";
  740. }
  741. s++;
  742. }
  743. return ret;
  744. }
  745. //-------------------------------------------------------------------
  746. void cmComputeLinkInformation::SetCurrentLinkType(LinkType lt)
  747. {
  748. // If we are changing the current link type add the flag to tell the
  749. // linker about it.
  750. if(this->CurrentLinkType != lt)
  751. {
  752. this->CurrentLinkType = lt;
  753. if(this->LinkTypeEnabled)
  754. {
  755. switch(this->CurrentLinkType)
  756. {
  757. case LinkStatic:
  758. this->Items.push_back(Item(this->StaticLinkTypeFlag, false));
  759. break;
  760. case LinkShared:
  761. this->Items.push_back(Item(this->SharedLinkTypeFlag, false));
  762. break;
  763. default:
  764. break;
  765. }
  766. }
  767. }
  768. }
  769. //----------------------------------------------------------------------------
  770. void cmComputeLinkInformation::AddTargetItem(std::string const& item,
  771. cmTarget* target)
  772. {
  773. // This is called to handle a link item that is a full path to a target.
  774. // If the target is not a static library make sure the link type is
  775. // shared. This is because dynamic-mode linking can handle both
  776. // shared and static libraries but static-mode can handle only
  777. // static libraries. If a previous user item changed the link type
  778. // to static we need to make sure it is back to shared.
  779. if(target->GetType() != cmTarget::STATIC_LIBRARY)
  780. {
  781. this->SetCurrentLinkType(LinkShared);
  782. }
  783. // If this platform wants a flag before the full path, add it.
  784. if(!this->LibLinkFileFlag.empty())
  785. {
  786. this->Items.push_back(Item(this->LibLinkFileFlag, false));
  787. }
  788. // Keep track of shared library targets linked.
  789. if(target->GetType() == cmTarget::SHARED_LIBRARY)
  790. {
  791. this->SharedLibrariesLinked.insert(target);
  792. }
  793. // Now add the full path to the library.
  794. this->Items.push_back(Item(item, true));
  795. }
  796. //----------------------------------------------------------------------------
  797. void cmComputeLinkInformation::AddFullItem(std::string const& item)
  798. {
  799. // Check for the implicit link directory special case.
  800. if(this->CheckImplicitDirItem(item))
  801. {
  802. return;
  803. }
  804. // This is called to handle a link item that is a full path.
  805. // If the target is not a static library make sure the link type is
  806. // shared. This is because dynamic-mode linking can handle both
  807. // shared and static libraries but static-mode can handle only
  808. // static libraries. If a previous user item changed the link type
  809. // to static we need to make sure it is back to shared.
  810. if(this->LinkTypeEnabled)
  811. {
  812. std::string name = cmSystemTools::GetFilenameName(item);
  813. if(this->ExtractSharedLibraryName.find(name))
  814. {
  815. this->SetCurrentLinkType(LinkShared);
  816. }
  817. else if(!this->ExtractStaticLibraryName.find(item))
  818. {
  819. // We cannot determine the type. Assume it is the target's
  820. // default type.
  821. this->SetCurrentLinkType(this->StartLinkType);
  822. }
  823. }
  824. // Record the directory in which the library appears because CMake
  825. // 2.4 in below added these as -L paths.
  826. if(this->OldLinkDirMode)
  827. {
  828. this->OldLinkDirs.push_back(cmSystemTools::GetFilenamePath(item));
  829. }
  830. // If this platform wants a flag before the full path, add it.
  831. if(!this->LibLinkFileFlag.empty())
  832. {
  833. this->Items.push_back(Item(this->LibLinkFileFlag, false));
  834. }
  835. // Now add the full path to the library.
  836. this->Items.push_back(Item(item, true));
  837. }
  838. //----------------------------------------------------------------------------
  839. bool cmComputeLinkInformation::CheckImplicitDirItem(std::string const& item)
  840. {
  841. // We only switch to a pathless item if the link type may be
  842. // enforced. Fortunately only platforms that support link types
  843. // seem to have magic per-architecture implicit link directories.
  844. if(!this->LinkTypeEnabled)
  845. {
  846. return false;
  847. }
  848. // Check if this item is in an implicit link directory.
  849. std::string dir = cmSystemTools::GetFilenamePath(item);
  850. if(this->ImplicitLinkDirs.find(dir) == this->ImplicitLinkDirs.end())
  851. {
  852. // Only libraries in implicit link directories are converted to
  853. // pathless items.
  854. return false;
  855. }
  856. // Many system linkers support multiple architectures by
  857. // automatically selecting the implicit linker search path for the
  858. // current architecture. If the library appears in an implicit link
  859. // directory then just report the file name without the directory
  860. // portion. This will allow the system linker to locate the proper
  861. // library for the architecture at link time.
  862. std::string file = cmSystemTools::GetFilenameName(item);
  863. this->AddUserItem(file);
  864. return true;
  865. }
  866. //----------------------------------------------------------------------------
  867. void cmComputeLinkInformation::AddUserItem(std::string const& item)
  868. {
  869. // This is called to handle a link item that does not match a CMake
  870. // target and is not a full path. We check here if it looks like a
  871. // library file name to automatically request the proper link type
  872. // from the linker. For example:
  873. //
  874. // foo ==> -lfoo
  875. // libfoo.a ==> -Wl,-Bstatic -lfoo
  876. std::string lib;
  877. // Parse out the prefix, base, and suffix components of the
  878. // library name. If the name matches that of a shared or static
  879. // library then set the link type accordingly.
  880. //
  881. // Search for shared library names first because some platforms
  882. // have shared libraries with names that match the static library
  883. // pattern. For example cygwin and msys use the convention
  884. // libfoo.dll.a for import libraries and libfoo.a for static
  885. // libraries. On AIX a library with the name libfoo.a can be
  886. // shared!
  887. if(this->ExtractSharedLibraryName.find(item))
  888. {
  889. // This matches a shared library file name.
  890. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  891. fprintf(stderr, "shared regex matched [%s] [%s] [%s]\n",
  892. this->ExtractSharedLibraryName.match(1).c_str(),
  893. this->ExtractSharedLibraryName.match(2).c_str(),
  894. this->ExtractSharedLibraryName.match(3).c_str());
  895. #endif
  896. // Set the link type to shared.
  897. this->SetCurrentLinkType(LinkShared);
  898. // Use just the library name so the linker will search.
  899. lib = this->ExtractSharedLibraryName.match(2);
  900. }
  901. else if(this->ExtractStaticLibraryName.find(item))
  902. {
  903. // This matches a static library file name.
  904. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  905. fprintf(stderr, "static regex matched [%s] [%s] [%s]\n",
  906. this->ExtractStaticLibraryName.match(1).c_str(),
  907. this->ExtractStaticLibraryName.match(2).c_str(),
  908. this->ExtractStaticLibraryName.match(3).c_str());
  909. #endif
  910. // Set the link type to static.
  911. this->SetCurrentLinkType(LinkStatic);
  912. // Use just the library name so the linker will search.
  913. lib = this->ExtractStaticLibraryName.match(2);
  914. }
  915. else if(this->ExtractAnyLibraryName.find(item))
  916. {
  917. // This matches a library file name.
  918. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  919. fprintf(stderr, "any regex matched [%s] [%s] [%s]\n",
  920. this->ExtractAnyLibraryName.match(1).c_str(),
  921. this->ExtractAnyLibraryName.match(2).c_str(),
  922. this->ExtractAnyLibraryName.match(3).c_str());
  923. #endif
  924. // Restore the target link type since this item does not specify
  925. // one.
  926. this->SetCurrentLinkType(this->StartLinkType);
  927. // Use just the library name so the linker will search.
  928. lib = this->ExtractAnyLibraryName.match(2);
  929. }
  930. else if(item[0] == '-' || item[0] == '$' || item[0] == '`')
  931. {
  932. // This is a linker option provided by the user.
  933. this->HaveUserFlagItem = true;
  934. // Restore the target link type since this item does not specify
  935. // one.
  936. this->SetCurrentLinkType(this->StartLinkType);
  937. // Use the item verbatim.
  938. this->Items.push_back(Item(item, false));
  939. return;
  940. }
  941. else
  942. {
  943. // This is a name specified by the user.
  944. this->HaveUserFlagItem = true;
  945. // We must ask the linker to search for a library with this name.
  946. // Restore the target link type since this item does not specify
  947. // one.
  948. this->SetCurrentLinkType(this->StartLinkType);
  949. lib = item;
  950. }
  951. // Create an option to ask the linker to search for the library.
  952. std::string out = this->LibLinkFlag;
  953. out += lib;
  954. out += this->LibLinkSuffix;
  955. this->Items.push_back(Item(out, false));
  956. // Here we could try to find the library the linker will find and
  957. // add a runtime information entry for it. It would probably not be
  958. // reliable and we want to encourage use of full paths for library
  959. // specification.
  960. }
  961. //----------------------------------------------------------------------------
  962. void cmComputeLinkInformation::AddFrameworkItem(std::string const& item)
  963. {
  964. // Try to separate the framework name and path.
  965. if(!this->SplitFramework.find(item.c_str()))
  966. {
  967. cmOStringStream e;
  968. e << "Could not parse framework path \"" << item << "\" "
  969. << "linked by target " << this->Target->GetName() << ".";
  970. cmSystemTools::Error(e.str().c_str());
  971. return;
  972. }
  973. // Add the directory portion to the framework search path.
  974. this->AddFrameworkPath(this->SplitFramework.match(1));
  975. // Add the item using the -framework option.
  976. std::string fw = "-framework ";
  977. fw += this->SplitFramework.match(2);
  978. this->Items.push_back(Item(fw, false));
  979. }
  980. //----------------------------------------------------------------------------
  981. void cmComputeLinkInformation::AddDirectoryItem(std::string const& item)
  982. {
  983. #ifdef __APPLE__
  984. if(cmSystemTools::IsPathToFramework(item.c_str()))
  985. {
  986. this->AddFrameworkItem(item);
  987. }
  988. else
  989. #endif
  990. {
  991. this->DropDirectoryItem(item);
  992. }
  993. }
  994. //----------------------------------------------------------------------------
  995. void cmComputeLinkInformation::DropDirectoryItem(std::string const& item)
  996. {
  997. // A full path to a directory was found as a link item. Warn the
  998. // user.
  999. cmOStringStream e;
  1000. e << "WARNING: Target \"" << this->Target->GetName()
  1001. << "\" requests linking to directory \"" << item << "\". "
  1002. << "Targets may link only to libraries. "
  1003. << "CMake is dropping the item.";
  1004. cmSystemTools::Message(e.str().c_str());
  1005. }
  1006. //----------------------------------------------------------------------------
  1007. void cmComputeLinkInformation::ComputeFrameworkInfo()
  1008. {
  1009. // Avoid adding system framework paths. See "man ld" on OS X.
  1010. this->FrameworkPathsEmmitted.insert("/Library/Frameworks");
  1011. this->FrameworkPathsEmmitted.insert("/Network/Library/Frameworks");
  1012. this->FrameworkPathsEmmitted.insert("/System/Library/Frameworks");
  1013. // Regular expression to extract a framework path and name.
  1014. this->SplitFramework.compile("(.*)/(.*)\\.framework$");
  1015. }
  1016. //----------------------------------------------------------------------------
  1017. void cmComputeLinkInformation::AddFrameworkPath(std::string const& p)
  1018. {
  1019. if(this->FrameworkPathsEmmitted.insert(p).second)
  1020. {
  1021. this->FrameworkPaths.push_back(p);
  1022. }
  1023. }
  1024. //----------------------------------------------------------------------------
  1025. void cmComputeLinkInformation::ComputeLinkerSearchDirectories()
  1026. {
  1027. // Some search paths should never be emitted.
  1028. this->DirectoriesEmmitted = this->ImplicitLinkDirs;
  1029. this->DirectoriesEmmitted.insert("");
  1030. // Check if we need to include the runtime search path at link time.
  1031. std::string var = "CMAKE_SHARED_LIBRARY_LINK_";
  1032. var += this->LinkLanguage;
  1033. var += "_WITH_RUNTIME_PATH";
  1034. if(this->Makefile->IsOn(var.c_str()))
  1035. {
  1036. // This platform requires the runtime library path for shared
  1037. // libraries to be specified at link time as -L paths. It needs
  1038. // them so that transitive dependencies of the libraries linked
  1039. // may be found by the linker.
  1040. this->AddLinkerSearchDirectories(this->GetRuntimeSearchPath());
  1041. }
  1042. // Get the search path entries requested by the user.
  1043. this->AddLinkerSearchDirectories(this->Target->GetLinkDirectories());
  1044. // Support broken projects if necessary.
  1045. if(this->HaveUserFlagItem && this->OldLinkDirMode)
  1046. {
  1047. this->AddLinkerSearchDirectories(this->OldLinkDirs);
  1048. }
  1049. // If there is no separate linker runtime search flag (-rpath-link)
  1050. // and we have a search path for dependent libraries add it to the
  1051. // link directories.
  1052. if(this->OrderDependentRPath && this->RPathLinkFlag.empty())
  1053. {
  1054. this->AddLinkerSearchDirectories
  1055. (this->OrderDependentRPath->GetRuntimePath());
  1056. }
  1057. }
  1058. //----------------------------------------------------------------------------
  1059. void
  1060. cmComputeLinkInformation
  1061. ::AddLinkerSearchDirectories(std::vector<std::string> const& dirs)
  1062. {
  1063. for(std::vector<std::string>::const_iterator i = dirs.begin();
  1064. i != dirs.end(); ++i)
  1065. {
  1066. if(this->DirectoriesEmmitted.insert(*i).second)
  1067. {
  1068. this->Directories.push_back(*i);
  1069. }
  1070. }
  1071. }
  1072. //----------------------------------------------------------------------------
  1073. std::vector<std::string> const&
  1074. cmComputeLinkInformation::GetRuntimeSearchPath()
  1075. {
  1076. return this->OrderRuntimeSearchPath->GetRuntimePath();
  1077. }
  1078. //----------------------------------------------------------------------------
  1079. void
  1080. cmComputeLinkInformation::AddLibraryRuntimeInfo(std::string const& fullPath,
  1081. cmTarget* target)
  1082. {
  1083. // Skip targets that are not shared libraries (modules cannot be linked).
  1084. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1085. {
  1086. return;
  1087. }
  1088. // Try to get the soname of the library. Only files with this name
  1089. // could possibly conflict.
  1090. std::string soName = target->GetSOName(this->Config);
  1091. const char* soname = soName.empty()? 0 : soName.c_str();
  1092. // Add the library runtime entry.
  1093. this->AddLibraryRuntimeInfo(fullPath, soname);
  1094. }
  1095. //----------------------------------------------------------------------------
  1096. void
  1097. cmComputeLinkInformation::AddLibraryRuntimeInfo(std::string const& fullPath,
  1098. const char* soname)
  1099. {
  1100. // Get the name of the library from the file name.
  1101. std::string file = cmSystemTools::GetFilenameName(fullPath);
  1102. if(!this->ExtractSharedLibraryName.find(file.c_str()))
  1103. {
  1104. // This is not the name of a shared library.
  1105. return;
  1106. }
  1107. // Include this library in the runtime path ordering.
  1108. this->OrderRuntimeSearchPath->AddLibrary(fullPath, soname);
  1109. }
  1110. //----------------------------------------------------------------------------
  1111. void cmComputeLinkInformation::GetRPath(std::vector<std::string>& runtimeDirs,
  1112. bool for_install)
  1113. {
  1114. // Select whether to generate runtime search directories.
  1115. bool outputRuntime =
  1116. !this->Makefile->IsOn("CMAKE_SKIP_RPATH") && !this->RuntimeFlag.empty();
  1117. // Select whether to generate an rpath for the install tree or the
  1118. // build tree.
  1119. bool linking_for_install =
  1120. (for_install ||
  1121. this->Target->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"));
  1122. bool use_install_rpath =
  1123. (outputRuntime && this->Target->HaveInstallTreeRPATH() &&
  1124. linking_for_install);
  1125. bool use_build_rpath =
  1126. (outputRuntime && this->Target->HaveBuildTreeRPATH() &&
  1127. !linking_for_install);
  1128. bool use_link_rpath =
  1129. outputRuntime && linking_for_install &&
  1130. this->Target->GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
  1131. // Construct the RPATH.
  1132. if(use_install_rpath)
  1133. {
  1134. const char* install_rpath = this->Target->GetProperty("INSTALL_RPATH");
  1135. cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
  1136. }
  1137. if(use_build_rpath || use_link_rpath)
  1138. {
  1139. std::vector<std::string> const& rdirs = this->GetRuntimeSearchPath();
  1140. for(std::vector<std::string>::const_iterator ri = rdirs.begin();
  1141. ri != rdirs.end(); ++ri)
  1142. {
  1143. // Put this directory in the rpath if using build-tree rpath
  1144. // support or if using the link path as an rpath.
  1145. if(use_build_rpath)
  1146. {
  1147. runtimeDirs.push_back(*ri);
  1148. }
  1149. else if(use_link_rpath)
  1150. {
  1151. // Do not add any path inside the source or build tree.
  1152. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1153. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1154. if(!cmSystemTools::ComparePath(ri->c_str(), topSourceDir) &&
  1155. !cmSystemTools::ComparePath(ri->c_str(), topBinaryDir) &&
  1156. !cmSystemTools::IsSubDirectory(ri->c_str(), topSourceDir) &&
  1157. !cmSystemTools::IsSubDirectory(ri->c_str(), topBinaryDir))
  1158. {
  1159. runtimeDirs.push_back(*ri);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. // Add runtime paths required by the platform to always be
  1165. // present. This is done even when skipping rpath support.
  1166. cmSystemTools::ExpandListArgument(this->RuntimeAlways.c_str(), runtimeDirs);
  1167. }
  1168. //----------------------------------------------------------------------------
  1169. std::string cmComputeLinkInformation::GetRPathString(bool for_install)
  1170. {
  1171. // Get the directories to use.
  1172. std::vector<std::string> runtimeDirs;
  1173. this->GetRPath(runtimeDirs, for_install);
  1174. // Concatenate the paths.
  1175. std::string rpath;
  1176. const char* sep = "";
  1177. for(std::vector<std::string>::const_iterator ri = runtimeDirs.begin();
  1178. ri != runtimeDirs.end(); ++ri)
  1179. {
  1180. // Separate from previous path.
  1181. rpath += sep;
  1182. sep = this->GetRuntimeSep().c_str();
  1183. // Add this path.
  1184. rpath += *ri;
  1185. }
  1186. return rpath;
  1187. }
  1188. //----------------------------------------------------------------------------
  1189. std::string cmComputeLinkInformation::GetChrpathString()
  1190. {
  1191. if(!this->RuntimeUseChrpath)
  1192. {
  1193. return "";
  1194. }
  1195. return this->GetRPathString(true);
  1196. }
  1197. //----------------------------------------------------------------------------
  1198. std::string cmComputeLinkInformation::GetChrpathTool()
  1199. {
  1200. return this->Makefile->GetSafeDefinition("CMAKE_CHRPATH");
  1201. }