cmComputeLinkInformation.cxx 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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 "cmGlobalGenerator.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmTarget.h"
  19. #include <algorithm>
  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. //----------------------------------------------------------------------------
  116. cmComputeLinkInformation
  117. ::cmComputeLinkInformation(cmTarget* target, const char* config)
  118. {
  119. // Store context information.
  120. this->Target = target;
  121. this->Makefile = this->Target->GetMakefile();
  122. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  123. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  124. // The configuration being linked.
  125. this->Config = config;
  126. // Get the language used for linking this target.
  127. this->LinkLanguage =
  128. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  129. if(!this->LinkLanguage)
  130. {
  131. // The Compute method will do nothing, so skip the rest of the
  132. // initialization.
  133. return;
  134. }
  135. // Check whether we should use an import library for linking a target.
  136. this->UseImportLibrary =
  137. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")?true:false;
  138. // On platforms without import libraries there may be a special flag
  139. // to use when creating a plugin (module) that obtains symbols from
  140. // the program that will load it.
  141. this->LoaderFlag = 0;
  142. if(!this->UseImportLibrary &&
  143. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  144. {
  145. std::string loader_flag_var = "CMAKE_SHARED_MODULE_LOADER_";
  146. loader_flag_var += this->LinkLanguage;
  147. loader_flag_var += "_FLAG";
  148. this->LoaderFlag = this->Makefile->GetDefinition(loader_flag_var.c_str());
  149. }
  150. // Get options needed to link libraries.
  151. this->LibLinkFlag =
  152. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  153. this->LibLinkFileFlag =
  154. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FILE_FLAG");
  155. this->LibLinkSuffix =
  156. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  157. // Get options needed to specify RPATHs.
  158. this->RuntimeUseChrpath = false;
  159. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  160. {
  161. std::string rtVar = "CMAKE_";
  162. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  163. {
  164. rtVar += "EXECUTABLE";
  165. }
  166. else
  167. {
  168. rtVar += "SHARED_LIBRARY";
  169. }
  170. rtVar += "_RUNTIME_";
  171. rtVar += this->LinkLanguage;
  172. rtVar += "_FLAG";
  173. std::string rtSepVar = rtVar + "_SEP";
  174. this->RuntimeFlag = this->Makefile->GetSafeDefinition(rtVar.c_str());
  175. this->RuntimeSep = this->Makefile->GetSafeDefinition(rtSepVar.c_str());
  176. this->RuntimeAlways =
  177. (this->Makefile->
  178. GetSafeDefinition("CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH"));
  179. this->RuntimeUseChrpath = this->Target->IsChrpathUsed();
  180. }
  181. // Get link type information.
  182. this->ComputeLinkTypeInfo();
  183. // Setup the link item parser.
  184. this->ComputeItemParserInfo();
  185. // Setup framework support.
  186. this->ComputeFrameworkInfo();
  187. // Get the implicit link directories for this platform.
  188. if(const char* implicitLinks =
  189. (this->Makefile->GetDefinition
  190. ("CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES")))
  191. {
  192. std::vector<std::string> implicitLinkVec;
  193. cmSystemTools::ExpandListArgument(implicitLinks, implicitLinkVec);
  194. for(std::vector<std::string>::const_iterator
  195. i = implicitLinkVec.begin();
  196. i != implicitLinkVec.end(); ++i)
  197. {
  198. this->ImplicitLinkDirs.insert(*i);
  199. }
  200. }
  201. // Initial state.
  202. this->RuntimeSearchPathComputed = false;
  203. this->HaveUserFlagItem = false;
  204. // Decide whether to enable compatible library search path mode.
  205. // There exists code that effectively does
  206. //
  207. // /path/to/libA.so -lB
  208. //
  209. // where -lB is meant to link to /path/to/libB.so. This is broken
  210. // because it specified -lB without specifying a link directory (-L)
  211. // in which to search for B. This worked in CMake 2.4 and below
  212. // because -L/path/to would be added by the -L/-l split for A. In
  213. // order to support such projects we need to add the directories
  214. // containing libraries linked with a full path to the -L path.
  215. this->OldLinkDirMode = false;
  216. if(this->Makefile->IsOn("CMAKE_LINK_OLD_PATHS") ||
  217. this->Makefile->GetLocalGenerator()
  218. ->NeedBackwardsCompatibility(2, 4))
  219. {
  220. this->OldLinkDirMode = true;
  221. }
  222. }
  223. //----------------------------------------------------------------------------
  224. cmComputeLinkInformation::ItemVector const&
  225. cmComputeLinkInformation::GetItems()
  226. {
  227. return this->Items;
  228. }
  229. //----------------------------------------------------------------------------
  230. std::vector<std::string> const& cmComputeLinkInformation::GetDirectories()
  231. {
  232. return this->Directories;
  233. }
  234. //----------------------------------------------------------------------------
  235. std::vector<std::string> const& cmComputeLinkInformation::GetDepends()
  236. {
  237. return this->Depends;
  238. }
  239. //----------------------------------------------------------------------------
  240. std::vector<std::string> const& cmComputeLinkInformation::GetFrameworkPaths()
  241. {
  242. return this->FrameworkPaths;
  243. }
  244. //----------------------------------------------------------------------------
  245. std::set<cmTarget*> const&
  246. cmComputeLinkInformation::GetSharedLibrariesLinked()
  247. {
  248. return this->SharedLibrariesLinked;
  249. }
  250. //----------------------------------------------------------------------------
  251. bool cmComputeLinkInformation::Compute()
  252. {
  253. // Skip targets that do not link.
  254. if(!(this->Target->GetType() == cmTarget::EXECUTABLE ||
  255. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  256. this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
  257. this->Target->GetType() == cmTarget::STATIC_LIBRARY))
  258. {
  259. return false;
  260. }
  261. // We require a link language for the target.
  262. if(!this->LinkLanguage)
  263. {
  264. cmSystemTools::
  265. Error("CMake can not determine linker language for target:",
  266. this->Target->GetName());
  267. return false;
  268. }
  269. // Compute the ordered link line items.
  270. cmComputeLinkDepends cld(this->Target, this->Config);
  271. cmComputeLinkDepends::EntryVector const& linkEntries = cld.Compute();
  272. // Add the link line items.
  273. for(cmComputeLinkDepends::EntryVector::const_iterator
  274. lei = linkEntries.begin();
  275. lei != linkEntries.end(); ++lei)
  276. {
  277. this->AddItem(lei->Item, lei->Target);
  278. }
  279. // Restore the target link type so the correct system runtime
  280. // libraries are found.
  281. this->SetCurrentLinkType(this->StartLinkType);
  282. // Compute the linker search path.
  283. this->ComputeLinkerSearchDirectories();
  284. return true;
  285. }
  286. //----------------------------------------------------------------------------
  287. void cmComputeLinkInformation::AddItem(std::string const& item,
  288. cmTarget* tgt)
  289. {
  290. // Compute the proper name to use to link this library.
  291. const char* config = this->Config;
  292. bool impexe = (tgt && tgt->IsExecutableWithExports());
  293. if(impexe && !this->UseImportLibrary && !this->LoaderFlag)
  294. {
  295. // Skip linking to executables on platforms with no import
  296. // libraries or loader flags.
  297. return;
  298. }
  299. // Keep track of shared libraries linked.
  300. if(tgt && tgt->GetType() == cmTarget::SHARED_LIBRARY)
  301. {
  302. this->SharedLibrariesLinked.insert(tgt);
  303. }
  304. if(tgt && (tgt->GetType() == cmTarget::STATIC_LIBRARY ||
  305. tgt->GetType() == cmTarget::SHARED_LIBRARY ||
  306. tgt->GetType() == cmTarget::MODULE_LIBRARY ||
  307. impexe))
  308. {
  309. // This is a CMake target. Ask the target for its real name.
  310. if(impexe && this->LoaderFlag)
  311. {
  312. // This link item is an executable that may provide symbols
  313. // used by this target. A special flag is needed on this
  314. // platform. Add it now.
  315. std::string linkItem;
  316. linkItem = this->LoaderFlag;
  317. std::string exe = tgt->GetFullPath(config, this->UseImportLibrary);
  318. linkItem += exe;
  319. this->Items.push_back(Item(linkItem, true));
  320. this->Depends.push_back(exe);
  321. }
  322. else
  323. {
  324. // Decide whether to use an import library.
  325. bool implib =
  326. (this->UseImportLibrary &&
  327. (impexe || tgt->GetType() == cmTarget::SHARED_LIBRARY));
  328. // Pass the full path to the target file.
  329. std::string lib = tgt->GetFullPath(config, implib);
  330. this->Depends.push_back(lib);
  331. if(tgt->IsFrameworkOnApple())
  332. {
  333. // Frameworks on OS X need only the framework directory to
  334. // link.
  335. std::string fw = tgt->GetDirectory(config, implib);
  336. this->AddFrameworkItem(fw);
  337. }
  338. else
  339. {
  340. this->AddTargetItem(lib, tgt);
  341. this->AddLibraryRuntimeInfo(lib, tgt);
  342. }
  343. }
  344. }
  345. else
  346. {
  347. // This is not a CMake target. Use the name given.
  348. if(cmSystemTools::FileIsFullPath(item.c_str()))
  349. {
  350. if(cmSystemTools::FileIsDirectory(item.c_str()))
  351. {
  352. // This is a directory.
  353. this->AddDirectoryItem(item);
  354. }
  355. else
  356. {
  357. // Use the full path given to the library file.
  358. this->Depends.push_back(item);
  359. this->AddFullItem(item);
  360. this->AddLibraryRuntimeInfo(item);
  361. }
  362. }
  363. else
  364. {
  365. // This is a library or option specified by the user.
  366. this->AddUserItem(item);
  367. }
  368. }
  369. }
  370. //----------------------------------------------------------------------------
  371. void cmComputeLinkInformation::ComputeLinkTypeInfo()
  372. {
  373. // First assume we cannot do link type stuff.
  374. this->LinkTypeEnabled = false;
  375. // Lookup link type selection flags.
  376. const char* static_link_type_flag = 0;
  377. const char* shared_link_type_flag = 0;
  378. const char* target_type_str = 0;
  379. switch(this->Target->GetType())
  380. {
  381. case cmTarget::EXECUTABLE: target_type_str = "EXE"; break;
  382. case cmTarget::SHARED_LIBRARY: target_type_str = "SHARED_LIBRARY"; break;
  383. case cmTarget::MODULE_LIBRARY: target_type_str = "SHARED_MODULE"; break;
  384. default: break;
  385. }
  386. if(target_type_str)
  387. {
  388. std::string static_link_type_flag_var = "CMAKE_";
  389. static_link_type_flag_var += target_type_str;
  390. static_link_type_flag_var += "_LINK_STATIC_";
  391. static_link_type_flag_var += this->LinkLanguage;
  392. static_link_type_flag_var += "_FLAGS";
  393. static_link_type_flag =
  394. this->Makefile->GetDefinition(static_link_type_flag_var.c_str());
  395. std::string shared_link_type_flag_var = "CMAKE_";
  396. shared_link_type_flag_var += target_type_str;
  397. shared_link_type_flag_var += "_LINK_DYNAMIC_";
  398. shared_link_type_flag_var += this->LinkLanguage;
  399. shared_link_type_flag_var += "_FLAGS";
  400. shared_link_type_flag =
  401. this->Makefile->GetDefinition(shared_link_type_flag_var.c_str());
  402. }
  403. // We can support link type switching only if all needed flags are
  404. // known.
  405. if(static_link_type_flag && *static_link_type_flag &&
  406. shared_link_type_flag && *shared_link_type_flag)
  407. {
  408. this->LinkTypeEnabled = true;
  409. this->StaticLinkTypeFlag = static_link_type_flag;
  410. this->SharedLinkTypeFlag = shared_link_type_flag;
  411. }
  412. // TODO: Lookup the starting link type from the target (is it being
  413. // linked statically?).
  414. this->StartLinkType = LinkShared;
  415. this->CurrentLinkType = this->StartLinkType;
  416. }
  417. //----------------------------------------------------------------------------
  418. void cmComputeLinkInformation::ComputeItemParserInfo()
  419. {
  420. // Get possible library name prefixes.
  421. cmMakefile* mf = this->Makefile;
  422. this->AddLinkPrefix(mf->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  423. this->AddLinkPrefix(mf->GetDefinition("CMAKE_SHARED_LIBRARY_PREFIX"));
  424. // Import library names should be matched and treated as shared
  425. // libraries for the purposes of linking.
  426. this->AddLinkExtension(mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
  427. LinkShared);
  428. this->AddLinkExtension(mf->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
  429. LinkStatic);
  430. this->AddLinkExtension(mf->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
  431. LinkShared);
  432. this->AddLinkExtension(mf->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"),
  433. LinkUnknown);
  434. if(const char* linkSuffixes =
  435. mf->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS"))
  436. {
  437. std::vector<std::string> linkSuffixVec;
  438. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  439. for(std::vector<std::string>::iterator i = linkSuffixVec.begin();
  440. i != linkSuffixVec.end(); ++i)
  441. {
  442. this->AddLinkExtension(i->c_str(), LinkUnknown);
  443. }
  444. }
  445. // Compute a regex to match link extensions.
  446. std::string libext = this->CreateExtensionRegex(this->LinkExtensions);
  447. // Create regex to remove any library extension.
  448. std::string reg("(.*)");
  449. reg += libext;
  450. this->RemoveLibraryExtension.compile(reg.c_str());
  451. // Create a regex to match a library name. Match index 1 will be
  452. // the prefix if it exists and empty otherwise. Match index 2 will
  453. // be the library name. Match index 3 will be the library
  454. // extension.
  455. reg = "^(";
  456. for(std::set<cmStdString>::iterator p = this->LinkPrefixes.begin();
  457. p != this->LinkPrefixes.end(); ++p)
  458. {
  459. reg += *p;
  460. reg += "|";
  461. }
  462. reg += ")";
  463. reg += "([^/]*)";
  464. // Create a regex to match any library name.
  465. std::string reg_any = reg;
  466. reg_any += libext;
  467. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  468. fprintf(stderr, "any regex [%s]\n", reg_any.c_str());
  469. #endif
  470. this->ExtractAnyLibraryName.compile(reg_any.c_str());
  471. // Create a regex to match static library names.
  472. if(!this->StaticLinkExtensions.empty())
  473. {
  474. std::string reg_static = reg;
  475. reg_static += this->CreateExtensionRegex(this->StaticLinkExtensions);
  476. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  477. fprintf(stderr, "static regex [%s]\n", reg_static.c_str());
  478. #endif
  479. this->ExtractStaticLibraryName.compile(reg_static.c_str());
  480. }
  481. // Create a regex to match shared library names.
  482. if(!this->SharedLinkExtensions.empty())
  483. {
  484. std::string reg_shared = reg;
  485. reg_shared += this->CreateExtensionRegex(this->SharedLinkExtensions);
  486. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  487. fprintf(stderr, "shared regex [%s]\n", reg_shared.c_str());
  488. #endif
  489. this->ExtractSharedLibraryName.compile(reg_shared.c_str());
  490. }
  491. }
  492. //----------------------------------------------------------------------------
  493. void cmComputeLinkInformation::AddLinkPrefix(const char* p)
  494. {
  495. if(p)
  496. {
  497. this->LinkPrefixes.insert(p);
  498. }
  499. }
  500. //----------------------------------------------------------------------------
  501. void cmComputeLinkInformation::AddLinkExtension(const char* e, LinkType type)
  502. {
  503. if(e && *e)
  504. {
  505. if(type == LinkStatic)
  506. {
  507. this->StaticLinkExtensions.push_back(e);
  508. }
  509. if(type == LinkShared)
  510. {
  511. this->SharedLinkExtensions.push_back(e);
  512. }
  513. this->LinkExtensions.push_back(e);
  514. }
  515. }
  516. //----------------------------------------------------------------------------
  517. std::string
  518. cmComputeLinkInformation
  519. ::CreateExtensionRegex(std::vector<std::string> const& exts)
  520. {
  521. // Build a list of extension choices.
  522. std::string libext = "(";
  523. const char* sep = "";
  524. for(std::vector<std::string>::const_iterator i = exts.begin();
  525. i != exts.end(); ++i)
  526. {
  527. // Separate this choice from the previous one.
  528. libext += sep;
  529. sep = "|";
  530. // Store this extension choice with the "." escaped.
  531. libext += "\\";
  532. #if defined(_WIN32) && !defined(__CYGWIN__)
  533. libext += this->NoCaseExpression(i->c_str());
  534. #else
  535. libext += *i;
  536. #endif
  537. }
  538. // Finish the list.
  539. libext += ").*";
  540. return libext;
  541. }
  542. //----------------------------------------------------------------------------
  543. std::string cmComputeLinkInformation::NoCaseExpression(const char* str)
  544. {
  545. std::string ret;
  546. const char* s = str;
  547. while(*s)
  548. {
  549. if(*s == '.')
  550. {
  551. ret += *s;
  552. }
  553. else
  554. {
  555. ret += "[";
  556. ret += tolower(*s);
  557. ret += toupper(*s);
  558. ret += "]";
  559. }
  560. s++;
  561. }
  562. return ret;
  563. }
  564. //-------------------------------------------------------------------
  565. void cmComputeLinkInformation::SetCurrentLinkType(LinkType lt)
  566. {
  567. // If we are changing the current link type add the flag to tell the
  568. // linker about it.
  569. if(this->CurrentLinkType != lt)
  570. {
  571. this->CurrentLinkType = lt;
  572. if(this->LinkTypeEnabled)
  573. {
  574. switch(this->CurrentLinkType)
  575. {
  576. case LinkStatic:
  577. this->Items.push_back(Item(this->StaticLinkTypeFlag, false));
  578. break;
  579. case LinkShared:
  580. this->Items.push_back(Item(this->SharedLinkTypeFlag, false));
  581. break;
  582. default:
  583. break;
  584. }
  585. }
  586. }
  587. }
  588. //----------------------------------------------------------------------------
  589. void cmComputeLinkInformation::AddTargetItem(std::string const& item,
  590. cmTarget* target)
  591. {
  592. // This is called to handle a link item that is a full path to a target.
  593. // If the target is not a static library make sure the link type is
  594. // shared. This is because dynamic-mode linking can handle both
  595. // shared and static libraries but static-mode can handle only
  596. // static libraries. If a previous user item changed the link type
  597. // to static we need to make sure it is back to shared.
  598. if(target->GetType() != cmTarget::STATIC_LIBRARY)
  599. {
  600. this->SetCurrentLinkType(LinkShared);
  601. }
  602. // If this platform wants a flag before the full path, add it.
  603. if(!this->LibLinkFileFlag.empty())
  604. {
  605. this->Items.push_back(Item(this->LibLinkFileFlag, false));
  606. }
  607. // Now add the full path to the library.
  608. this->Items.push_back(Item(item, true));
  609. }
  610. //----------------------------------------------------------------------------
  611. void cmComputeLinkInformation::AddFullItem(std::string const& item)
  612. {
  613. // Check for the implicit link directory special case.
  614. if(this->CheckImplicitDirItem(item))
  615. {
  616. return;
  617. }
  618. // This is called to handle a link item that is a full path.
  619. // If the target is not a static library make sure the link type is
  620. // shared. This is because dynamic-mode linking can handle both
  621. // shared and static libraries but static-mode can handle only
  622. // static libraries. If a previous user item changed the link type
  623. // to static we need to make sure it is back to shared.
  624. if(this->LinkTypeEnabled)
  625. {
  626. std::string name = cmSystemTools::GetFilenameName(item);
  627. if(this->ExtractSharedLibraryName.find(name))
  628. {
  629. this->SetCurrentLinkType(LinkShared);
  630. }
  631. else if(!this->ExtractStaticLibraryName.find(item))
  632. {
  633. // We cannot determine the type. Assume it is the target's
  634. // default type.
  635. this->SetCurrentLinkType(this->StartLinkType);
  636. }
  637. }
  638. // Record the directory in which the library appears because CMake
  639. // 2.4 in below added these as -L paths.
  640. if(this->OldLinkDirMode)
  641. {
  642. this->OldLinkDirs.push_back(cmSystemTools::GetFilenamePath(item));
  643. }
  644. // If this platform wants a flag before the full path, add it.
  645. if(!this->LibLinkFileFlag.empty())
  646. {
  647. this->Items.push_back(Item(this->LibLinkFileFlag, false));
  648. }
  649. // Now add the full path to the library.
  650. this->Items.push_back(Item(item, true));
  651. }
  652. //----------------------------------------------------------------------------
  653. bool cmComputeLinkInformation::CheckImplicitDirItem(std::string const& item)
  654. {
  655. // We only switch to a pathless item if the link type may be
  656. // enforced. Fortunately only platforms that support link types
  657. // seem to have magic per-architecture implicit link directories.
  658. if(!this->LinkTypeEnabled)
  659. {
  660. return false;
  661. }
  662. // Check if this item is in an implicit link directory.
  663. std::string dir = cmSystemTools::GetFilenamePath(item);
  664. if(this->ImplicitLinkDirs.find(dir) == this->ImplicitLinkDirs.end())
  665. {
  666. // Only libraries in implicit link directories are converted to
  667. // pathless items.
  668. return false;
  669. }
  670. // Many system linkers support multiple architectures by
  671. // automatically selecting the implicit linker search path for the
  672. // current architecture. If the library appears in an implicit link
  673. // directory then just report the file name without the directory
  674. // portion. This will allow the system linker to locate the proper
  675. // library for the architecture at link time.
  676. std::string file = cmSystemTools::GetFilenameName(item);
  677. this->AddUserItem(file);
  678. return true;
  679. }
  680. //----------------------------------------------------------------------------
  681. void cmComputeLinkInformation::AddUserItem(std::string const& item)
  682. {
  683. // This is called to handle a link item that does not match a CMake
  684. // target and is not a full path. We check here if it looks like a
  685. // library file name to automatically request the proper link type
  686. // from the linker. For example:
  687. //
  688. // foo ==> -lfoo
  689. // libfoo.a ==> -Wl,-Bstatic -lfoo
  690. std::string lib;
  691. // Parse out the prefix, base, and suffix components of the
  692. // library name. If the name matches that of a shared or static
  693. // library then set the link type accordingly.
  694. //
  695. // Search for shared library names first because some platforms
  696. // have shared libraries with names that match the static library
  697. // pattern. For example cygwin and msys use the convention
  698. // libfoo.dll.a for import libraries and libfoo.a for static
  699. // libraries. On AIX a library with the name libfoo.a can be
  700. // shared!
  701. if(this->ExtractSharedLibraryName.find(item))
  702. {
  703. // This matches a shared library file name.
  704. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  705. fprintf(stderr, "shared regex matched [%s] [%s] [%s]\n",
  706. this->ExtractSharedLibraryName.match(1).c_str(),
  707. this->ExtractSharedLibraryName.match(2).c_str(),
  708. this->ExtractSharedLibraryName.match(3).c_str());
  709. #endif
  710. // Set the link type to shared.
  711. this->SetCurrentLinkType(LinkShared);
  712. // Use just the library name so the linker will search.
  713. lib = this->ExtractSharedLibraryName.match(2);
  714. }
  715. else if(this->ExtractStaticLibraryName.find(item))
  716. {
  717. // This matches a static library file name.
  718. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  719. fprintf(stderr, "static regex matched [%s] [%s] [%s]\n",
  720. this->ExtractStaticLibraryName.match(1).c_str(),
  721. this->ExtractStaticLibraryName.match(2).c_str(),
  722. this->ExtractStaticLibraryName.match(3).c_str());
  723. #endif
  724. // Set the link type to static.
  725. this->SetCurrentLinkType(LinkStatic);
  726. // Use just the library name so the linker will search.
  727. lib = this->ExtractStaticLibraryName.match(2);
  728. }
  729. else if(this->ExtractAnyLibraryName.find(item))
  730. {
  731. // This matches a library file name.
  732. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  733. fprintf(stderr, "any regex matched [%s] [%s] [%s]\n",
  734. this->ExtractAnyLibraryName.match(1).c_str(),
  735. this->ExtractAnyLibraryName.match(2).c_str(),
  736. this->ExtractAnyLibraryName.match(3).c_str());
  737. #endif
  738. // Restore the target link type since this item does not specify
  739. // one.
  740. this->SetCurrentLinkType(this->StartLinkType);
  741. // Use just the library name so the linker will search.
  742. lib = this->ExtractAnyLibraryName.match(2);
  743. }
  744. else if(item[0] == '-' || item[0] == '$' || item[0] == '`')
  745. {
  746. // This is a linker option provided by the user.
  747. this->HaveUserFlagItem = true;
  748. // Restore the target link type since this item does not specify
  749. // one.
  750. this->SetCurrentLinkType(this->StartLinkType);
  751. // Use the item verbatim.
  752. this->Items.push_back(Item(item, false));
  753. return;
  754. }
  755. else
  756. {
  757. // This is a name specified by the user.
  758. this->HaveUserFlagItem = true;
  759. // We must ask the linker to search for a library with this name.
  760. // Restore the target link type since this item does not specify
  761. // one.
  762. this->SetCurrentLinkType(this->StartLinkType);
  763. lib = item;
  764. }
  765. // Create an option to ask the linker to search for the library.
  766. std::string out = this->LibLinkFlag;
  767. out += lib;
  768. out += this->LibLinkSuffix;
  769. this->Items.push_back(Item(out, false));
  770. // Here we could try to find the library the linker will find and
  771. // add a runtime information entry for it. It would probably not be
  772. // reliable and we want to encourage use of full paths for library
  773. // specification.
  774. }
  775. //----------------------------------------------------------------------------
  776. void cmComputeLinkInformation::AddFrameworkItem(std::string const& item)
  777. {
  778. // Try to separate the framework name and path.
  779. if(!this->SplitFramework.find(item.c_str()))
  780. {
  781. cmOStringStream e;
  782. e << "Could not parse framework path \"" << item << "\" "
  783. << "linked by target " << this->Target->GetName() << ".";
  784. cmSystemTools::Error(e.str().c_str());
  785. return;
  786. }
  787. // Add the directory portion to the framework search path.
  788. this->AddFrameworkPath(this->SplitFramework.match(1));
  789. // Add the item using the -framework option.
  790. std::string fw = "-framework ";
  791. fw += this->SplitFramework.match(2);
  792. this->Items.push_back(Item(fw, false));
  793. }
  794. //----------------------------------------------------------------------------
  795. void cmComputeLinkInformation::AddDirectoryItem(std::string const& item)
  796. {
  797. #ifdef __APPLE__
  798. if(cmSystemTools::IsPathToFramework(item.c_str()))
  799. {
  800. this->AddFrameworkItem(item);
  801. }
  802. else
  803. #endif
  804. {
  805. this->DropDirectoryItem(item);
  806. }
  807. }
  808. //----------------------------------------------------------------------------
  809. void cmComputeLinkInformation::DropDirectoryItem(std::string const& item)
  810. {
  811. // A full path to a directory was found as a link item. Warn the
  812. // user.
  813. cmOStringStream e;
  814. e << "WARNING: Target \"" << this->Target->GetName()
  815. << "\" requests linking to directory \"" << item << "\". "
  816. << "Targets may link only to libraries. "
  817. << "CMake is dropping the item.";
  818. cmSystemTools::Message(e.str().c_str());
  819. }
  820. //----------------------------------------------------------------------------
  821. void cmComputeLinkInformation::ComputeFrameworkInfo()
  822. {
  823. // Avoid adding system framework paths. See "man ld" on OS X.
  824. this->FrameworkPathsEmmitted.insert("/Library/Frameworks");
  825. this->FrameworkPathsEmmitted.insert("/Network/Library/Frameworks");
  826. this->FrameworkPathsEmmitted.insert("/System/Library/Frameworks");
  827. // Regular expression to extract a framework path and name.
  828. this->SplitFramework.compile("(.*)/(.*)\\.framework$");
  829. }
  830. //----------------------------------------------------------------------------
  831. void cmComputeLinkInformation::AddFrameworkPath(std::string const& p)
  832. {
  833. if(this->FrameworkPathsEmmitted.insert(p).second)
  834. {
  835. this->FrameworkPaths.push_back(p);
  836. }
  837. }
  838. //----------------------------------------------------------------------------
  839. void cmComputeLinkInformation::ComputeLinkerSearchDirectories()
  840. {
  841. // Some search paths should never be emitted.
  842. this->DirectoriesEmmitted = this->ImplicitLinkDirs;
  843. this->DirectoriesEmmitted.insert("");
  844. // Check if we need to include the runtime search path at link time.
  845. std::string var = "CMAKE_SHARED_LIBRARY_LINK_";
  846. var += this->LinkLanguage;
  847. var += "_WITH_RUNTIME_PATH";
  848. if(this->Makefile->IsOn(var.c_str()))
  849. {
  850. // This platform requires the runtime library path for shared
  851. // libraries to be specified at link time as -L paths. It needs
  852. // them so that transitive dependencies of the libraries linked
  853. // may be found by the linker.
  854. this->AddLinkerSearchDirectories(this->GetRuntimeSearchPath());
  855. }
  856. // Get the search path entries requested by the user.
  857. this->AddLinkerSearchDirectories(this->Target->GetLinkDirectories());
  858. // Support broken projects if necessary.
  859. if(this->HaveUserFlagItem && this->OldLinkDirMode)
  860. {
  861. this->AddLinkerSearchDirectories(this->OldLinkDirs);
  862. }
  863. }
  864. //----------------------------------------------------------------------------
  865. void
  866. cmComputeLinkInformation
  867. ::AddLinkerSearchDirectories(std::vector<std::string> const& dirs)
  868. {
  869. for(std::vector<std::string>::const_iterator i = dirs.begin();
  870. i != dirs.end(); ++i)
  871. {
  872. if(this->DirectoriesEmmitted.insert(*i).second)
  873. {
  874. this->Directories.push_back(*i);
  875. }
  876. }
  877. }
  878. //----------------------------------------------------------------------------
  879. std::vector<std::string> const&
  880. cmComputeLinkInformation::GetRuntimeSearchPath()
  881. {
  882. if(!this->RuntimeSearchPathComputed)
  883. {
  884. this->RuntimeSearchPathComputed = true;
  885. this->CollectRuntimeDirectories();
  886. this->FindConflictingLibraries();
  887. this->OrderRuntimeSearchPath();
  888. }
  889. return this->RuntimeSearchPath;
  890. }
  891. //============================================================================
  892. // Directory ordering computation.
  893. // - Useful to compute a safe runtime library path order
  894. // - Need runtime path for supporting INSTALL_RPATH_USE_LINK_PATH
  895. // - Need runtime path at link time to pickup transitive link dependencies
  896. // for shared libraries (in future when we do not always add them).
  897. //----------------------------------------------------------------------------
  898. void
  899. cmComputeLinkInformation::AddLibraryRuntimeInfo(std::string const& fullPath,
  900. cmTarget* target)
  901. {
  902. // Skip targets that are not shared libraries (modules cannot be linked).
  903. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  904. {
  905. return;
  906. }
  907. // Try to get the soname of the library. Only files with this name
  908. // could possibly conflict.
  909. std::string soName = target->GetSOName(this->Config);
  910. const char* soname = soName.empty()? 0 : soName.c_str();
  911. // Add the library runtime entry.
  912. this->AddLibraryRuntimeInfo(fullPath, soname);
  913. }
  914. //----------------------------------------------------------------------------
  915. void
  916. cmComputeLinkInformation::AddLibraryRuntimeInfo(std::string const& fullPath,
  917. const char* soname)
  918. {
  919. // Get the name of the library from the file name.
  920. std::string file = cmSystemTools::GetFilenameName(fullPath);
  921. if(!this->ExtractSharedLibraryName.find(file.c_str()))
  922. {
  923. // This is not the name of a shared library.
  924. return;
  925. }
  926. // Add the runtime information at most once.
  927. if(this->LibraryRuntimeInfoEmmitted.insert(fullPath).second)
  928. {
  929. // Construct the runtime information entry for this library.
  930. LibraryRuntimeEntry entry;
  931. entry.FileName = cmSystemTools::GetFilenameName(fullPath);
  932. entry.SOName = soname? soname : "";
  933. entry.Directory = cmSystemTools::GetFilenamePath(fullPath);
  934. this->LibraryRuntimeInfo.push_back(entry);
  935. }
  936. else
  937. {
  938. // This can happen if the same library is linked multiple times.
  939. // In that case the runtime information check need be done only
  940. // once anyway. For shared libs we could add a check in AddItem
  941. // to not repeat them.
  942. }
  943. }
  944. //----------------------------------------------------------------------------
  945. void cmComputeLinkInformation::CollectRuntimeDirectories()
  946. {
  947. // Get all directories that should be in the runtime search path.
  948. // Add directories containing libraries linked with full path.
  949. for(std::vector<LibraryRuntimeEntry>::iterator
  950. ei = this->LibraryRuntimeInfo.begin();
  951. ei != this->LibraryRuntimeInfo.end(); ++ei)
  952. {
  953. ei->DirectoryIndex = this->AddRuntimeDirectory(ei->Directory);
  954. }
  955. // Add link directories specified for the target.
  956. std::vector<std::string> const& dirs = this->Target->GetLinkDirectories();
  957. for(std::vector<std::string>::const_iterator di = dirs.begin();
  958. di != dirs.end(); ++di)
  959. {
  960. this->AddRuntimeDirectory(*di);
  961. }
  962. }
  963. //----------------------------------------------------------------------------
  964. int cmComputeLinkInformation::AddRuntimeDirectory(std::string const& dir)
  965. {
  966. // Add the runtime directory with a unique index.
  967. std::map<cmStdString, int>::iterator i =
  968. this->RuntimeDirectoryIndex.find(dir);
  969. if(i == this->RuntimeDirectoryIndex.end())
  970. {
  971. std::map<cmStdString, int>::value_type
  972. entry(dir, static_cast<int>(this->RuntimeDirectories.size()));
  973. i = this->RuntimeDirectoryIndex.insert(entry).first;
  974. this->RuntimeDirectories.push_back(dir);
  975. }
  976. return i->second;
  977. }
  978. //----------------------------------------------------------------------------
  979. struct cmCLIRuntimeConflictCompare
  980. {
  981. typedef std::pair<int, int> RuntimeConflictPair;
  982. // The conflict pair is unique based on just the directory
  983. // (first). The second element is only used for displaying
  984. // information about why the entry is present.
  985. bool operator()(RuntimeConflictPair const& l,
  986. RuntimeConflictPair const& r)
  987. {
  988. return l.first == r.first;
  989. }
  990. };
  991. //----------------------------------------------------------------------------
  992. void cmComputeLinkInformation::FindConflictingLibraries()
  993. {
  994. // Allocate the conflict graph.
  995. this->RuntimeConflictGraph.resize(this->RuntimeDirectories.size());
  996. this->RuntimeDirectoryVisited.resize(this->RuntimeDirectories.size(), 0);
  997. // Find all runtime directories providing each library.
  998. for(unsigned int lri = 0; lri < this->LibraryRuntimeInfo.size(); ++lri)
  999. {
  1000. this->FindDirectoriesForLib(lri);
  1001. }
  1002. // Clean up the conflict graph representation.
  1003. for(std::vector<RuntimeConflictList>::iterator
  1004. i = this->RuntimeConflictGraph.begin();
  1005. i != this->RuntimeConflictGraph.end(); ++i)
  1006. {
  1007. // Sort the outgoing edges for each graph node so that the
  1008. // original order will be preserved as much as possible.
  1009. std::sort(i->begin(), i->end());
  1010. // Make the edge list unique so cycle detection will be reliable.
  1011. RuntimeConflictList::iterator last =
  1012. std::unique(i->begin(), i->end(), cmCLIRuntimeConflictCompare());
  1013. i->erase(last, i->end());
  1014. }
  1015. }
  1016. //----------------------------------------------------------------------------
  1017. void cmComputeLinkInformation::FindDirectoriesForLib(unsigned int lri)
  1018. {
  1019. // Search through the runtime directories to find those providing
  1020. // this library.
  1021. LibraryRuntimeEntry& re = this->LibraryRuntimeInfo[lri];
  1022. for(unsigned int i = 0; i < this->RuntimeDirectories.size(); ++i)
  1023. {
  1024. // Skip the directory that is supposed to provide the library.
  1025. if(this->RuntimeDirectories[i] == re.Directory)
  1026. {
  1027. continue;
  1028. }
  1029. // Determine which type of check to do.
  1030. if(!re.SOName.empty())
  1031. {
  1032. // We have the library soname. Check if it will be found.
  1033. std::string file = this->RuntimeDirectories[i];
  1034. file += "/";
  1035. file += re.SOName;
  1036. std::set<cmStdString> const& files =
  1037. (this->GlobalGenerator
  1038. ->GetDirectoryContent(this->RuntimeDirectories[i], false));
  1039. if((std::set<cmStdString>::const_iterator(files.find(re.SOName)) !=
  1040. files.end()) ||
  1041. cmSystemTools::FileExists(file.c_str(), true))
  1042. {
  1043. // The library will be found in this directory but this is not
  1044. // the directory named for it. Add an entry to make sure the
  1045. // desired directory comes before this one.
  1046. RuntimeConflictPair p(re.DirectoryIndex, lri);
  1047. this->RuntimeConflictGraph[i].push_back(p);
  1048. }
  1049. }
  1050. else
  1051. {
  1052. // We do not have the soname. Look for files in the directory
  1053. // that may conflict.
  1054. std::set<cmStdString> const& files =
  1055. (this->GlobalGenerator
  1056. ->GetDirectoryContent(this->RuntimeDirectories[i], true));
  1057. // Get the set of files that might conflict. Since we do not
  1058. // know the soname just look at all files that start with the
  1059. // file name. Usually the soname starts with the library name.
  1060. std::string base = re.FileName;
  1061. std::set<cmStdString>::const_iterator first = files.lower_bound(base);
  1062. ++base[base.size()-1];
  1063. std::set<cmStdString>::const_iterator last = files.upper_bound(base);
  1064. bool found = false;
  1065. for(std::set<cmStdString>::const_iterator fi = first;
  1066. !found && fi != last; ++fi)
  1067. {
  1068. found = true;
  1069. }
  1070. if(found)
  1071. {
  1072. // The library may be found in this directory but this is not
  1073. // the directory named for it. Add an entry to make sure the
  1074. // desired directory comes before this one.
  1075. RuntimeConflictPair p(re.DirectoryIndex, lri);
  1076. this->RuntimeConflictGraph[i].push_back(p);
  1077. }
  1078. }
  1079. }
  1080. }
  1081. //----------------------------------------------------------------------------
  1082. void cmComputeLinkInformation::OrderRuntimeSearchPath()
  1083. {
  1084. // Allow a cycle to be diagnosed once.
  1085. this->CycleDiagnosed = false;
  1086. this->WalkId = 0;
  1087. // Iterate through the directories in the original order.
  1088. for(unsigned int i=0; i < this->RuntimeDirectories.size(); ++i)
  1089. {
  1090. // Start a new DFS from this node.
  1091. ++this->WalkId;
  1092. this->VisitRuntimeDirectory(i);
  1093. }
  1094. }
  1095. //----------------------------------------------------------------------------
  1096. void cmComputeLinkInformation::VisitRuntimeDirectory(unsigned int i)
  1097. {
  1098. // Skip nodes already visited.
  1099. if(this->RuntimeDirectoryVisited[i])
  1100. {
  1101. if(this->RuntimeDirectoryVisited[i] == this->WalkId)
  1102. {
  1103. // We have reached a node previously visited on this DFS.
  1104. // There is a cycle.
  1105. this->DiagnoseCycle();
  1106. }
  1107. return;
  1108. }
  1109. // We are now visiting this node so mark it.
  1110. this->RuntimeDirectoryVisited[i] = this->WalkId;
  1111. // Visit the neighbors of the node first.
  1112. RuntimeConflictList const& clist = this->RuntimeConflictGraph[i];
  1113. for(RuntimeConflictList::const_iterator j = clist.begin();
  1114. j != clist.end(); ++j)
  1115. {
  1116. this->VisitRuntimeDirectory(j->first);
  1117. }
  1118. // Now that all directories required to come before this one have
  1119. // been emmitted, emit this directory.
  1120. this->RuntimeSearchPath.push_back(this->RuntimeDirectories[i]);
  1121. }
  1122. //----------------------------------------------------------------------------
  1123. void cmComputeLinkInformation::DiagnoseCycle()
  1124. {
  1125. // Report the cycle at most once.
  1126. if(this->CycleDiagnosed)
  1127. {
  1128. return;
  1129. }
  1130. this->CycleDiagnosed = true;
  1131. // Construct the message.
  1132. cmOStringStream e;
  1133. e << "WARNING: Cannot generate a safe runtime path for target "
  1134. << this->Target->GetName()
  1135. << " because there is a cycle in the constraint graph:\n";
  1136. // Display the conflict graph.
  1137. for(unsigned int i=0; i < this->RuntimeConflictGraph.size(); ++i)
  1138. {
  1139. RuntimeConflictList const& clist = this->RuntimeConflictGraph[i];
  1140. e << "dir " << i << " is [" << this->RuntimeDirectories[i] << "]\n";
  1141. for(RuntimeConflictList::const_iterator j = clist.begin();
  1142. j != clist.end(); ++j)
  1143. {
  1144. e << " dir " << j->first << " must precede it due to [";
  1145. LibraryRuntimeEntry const& re = this->LibraryRuntimeInfo[j->second];
  1146. if(re.SOName.empty())
  1147. {
  1148. e << re.FileName;
  1149. }
  1150. else
  1151. {
  1152. e << re.SOName;
  1153. }
  1154. e << "]\n";
  1155. }
  1156. }
  1157. cmSystemTools::Message(e.str().c_str());
  1158. }
  1159. //----------------------------------------------------------------------------
  1160. void cmComputeLinkInformation::GetRPath(std::vector<std::string>& runtimeDirs,
  1161. bool for_install)
  1162. {
  1163. // Select whether to generate runtime search directories.
  1164. bool outputRuntime =
  1165. !this->Makefile->IsOn("CMAKE_SKIP_RPATH") && !this->RuntimeFlag.empty();
  1166. // Select whether to generate an rpath for the install tree or the
  1167. // build tree.
  1168. bool linking_for_install =
  1169. (for_install ||
  1170. this->Target->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"));
  1171. bool use_install_rpath =
  1172. (outputRuntime && this->Target->HaveInstallTreeRPATH() &&
  1173. linking_for_install);
  1174. bool use_build_rpath =
  1175. (outputRuntime && this->Target->HaveBuildTreeRPATH() &&
  1176. !linking_for_install);
  1177. bool use_link_rpath =
  1178. outputRuntime && linking_for_install &&
  1179. this->Target->GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
  1180. // Construct the RPATH.
  1181. if(use_install_rpath)
  1182. {
  1183. const char* install_rpath = this->Target->GetProperty("INSTALL_RPATH");
  1184. cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
  1185. }
  1186. if(use_build_rpath || use_link_rpath)
  1187. {
  1188. std::vector<std::string> const& rdirs = this->GetRuntimeSearchPath();
  1189. for(std::vector<std::string>::const_iterator ri = rdirs.begin();
  1190. ri != rdirs.end(); ++ri)
  1191. {
  1192. // Put this directory in the rpath if using build-tree rpath
  1193. // support or if using the link path as an rpath.
  1194. if(use_build_rpath)
  1195. {
  1196. runtimeDirs.push_back(*ri);
  1197. }
  1198. else if(use_link_rpath)
  1199. {
  1200. // Do not add any path inside the source or build tree.
  1201. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1202. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1203. if(!cmSystemTools::ComparePath(ri->c_str(), topSourceDir) &&
  1204. !cmSystemTools::ComparePath(ri->c_str(), topBinaryDir) &&
  1205. !cmSystemTools::IsSubDirectory(ri->c_str(), topSourceDir) &&
  1206. !cmSystemTools::IsSubDirectory(ri->c_str(), topBinaryDir))
  1207. {
  1208. runtimeDirs.push_back(*ri);
  1209. }
  1210. }
  1211. }
  1212. }
  1213. // Add runtime paths required by the platform to always be
  1214. // present. This is done even when skipping rpath support.
  1215. cmSystemTools::ExpandListArgument(this->RuntimeAlways.c_str(), runtimeDirs);
  1216. }
  1217. //----------------------------------------------------------------------------
  1218. std::string cmComputeLinkInformation::GetRPathString(bool for_install)
  1219. {
  1220. // Get the directories to use.
  1221. std::vector<std::string> runtimeDirs;
  1222. this->GetRPath(runtimeDirs, for_install);
  1223. // Concatenate the paths.
  1224. std::string rpath;
  1225. const char* sep = "";
  1226. for(std::vector<std::string>::const_iterator ri = runtimeDirs.begin();
  1227. ri != runtimeDirs.end(); ++ri)
  1228. {
  1229. // Separate from previous path.
  1230. rpath += sep;
  1231. sep = this->GetRuntimeSep().c_str();
  1232. // Add this path.
  1233. rpath += *ri;
  1234. }
  1235. return rpath;
  1236. }
  1237. //----------------------------------------------------------------------------
  1238. std::string cmComputeLinkInformation::GetChrpathString()
  1239. {
  1240. if(!this->RuntimeUseChrpath)
  1241. {
  1242. return "";
  1243. }
  1244. return this->GetRPathString(true);
  1245. }
  1246. //----------------------------------------------------------------------------
  1247. std::string cmComputeLinkInformation::GetChrpathTool()
  1248. {
  1249. return this->Makefile->GetSafeDefinition("CMAKE_CHRPATH");
  1250. }