cmComputeLinkInformation.cxx 68 KB

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