cmComputeLinkInformation.cxx 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  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.empty())
  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*> 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.empty())
  410. {
  411. cmSystemTools::
  412. Error("CMake can not determine linker language for target: ",
  413. this->Target->GetName().c_str());
  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*> const& wrongItems = cld.GetOldWrongConfigItems();
  452. for(std::set<cmTarget const*>::const_iterator i = wrongItems.begin();
  453. i != wrongItems.end(); ++i)
  454. {
  455. cmTarget const* 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,
  523. cmTarget const* tgt)
  524. {
  525. // Compute the proper name to use to link this library.
  526. const char* config = this->Config;
  527. bool impexe = (tgt && tgt->IsExecutableWithExports());
  528. if(impexe && !this->UseImportLibrary && !this->LoaderFlag)
  529. {
  530. // Skip linking to executables on platforms with no import
  531. // libraries or loader flags.
  532. return;
  533. }
  534. if(tgt && tgt->IsLinkable())
  535. {
  536. // This is a CMake target. Ask the target for its real name.
  537. if(impexe && this->LoaderFlag)
  538. {
  539. // This link item is an executable that may provide symbols
  540. // used by this target. A special flag is needed on this
  541. // platform. Add it now.
  542. std::string linkItem;
  543. linkItem = this->LoaderFlag;
  544. std::string exe = tgt->GetFullPath(config, this->UseImportLibrary,
  545. true);
  546. linkItem += exe;
  547. this->Items.push_back(Item(linkItem, true, tgt));
  548. this->Depends.push_back(exe);
  549. }
  550. else
  551. {
  552. // Decide whether to use an import library.
  553. bool implib =
  554. (this->UseImportLibrary &&
  555. (impexe || tgt->GetType() == cmTarget::SHARED_LIBRARY));
  556. if(tgt->GetType() == cmTarget::INTERFACE_LIBRARY)
  557. {
  558. this->Items.push_back(Item(std::string(), true, tgt));
  559. return;
  560. }
  561. // Pass the full path to the target file.
  562. std::string lib = tgt->GetFullPath(config, implib, true);
  563. if(!this->LinkDependsNoShared ||
  564. tgt->GetType() != cmTarget::SHARED_LIBRARY)
  565. {
  566. this->Depends.push_back(lib);
  567. }
  568. this->AddTargetItem(lib, tgt);
  569. this->AddLibraryRuntimeInfo(lib, tgt);
  570. }
  571. }
  572. else
  573. {
  574. // This is not a CMake target. Use the name given.
  575. if(cmSystemTools::FileIsFullPath(item.c_str()))
  576. {
  577. if(cmSystemTools::FileIsDirectory(item.c_str()))
  578. {
  579. // This is a directory.
  580. this->AddDirectoryItem(item);
  581. }
  582. else
  583. {
  584. // Use the full path given to the library file.
  585. this->Depends.push_back(item);
  586. this->AddFullItem(item);
  587. this->AddLibraryRuntimeInfo(item);
  588. }
  589. }
  590. else
  591. {
  592. // This is a library or option specified by the user.
  593. this->AddUserItem(item, true);
  594. }
  595. }
  596. }
  597. //----------------------------------------------------------------------------
  598. void cmComputeLinkInformation::AddSharedDepItem(std::string const& item,
  599. cmTarget const* tgt)
  600. {
  601. // If dropping shared library dependencies, ignore them.
  602. if(this->SharedDependencyMode == SharedDepModeNone)
  603. {
  604. return;
  605. }
  606. // The user may have incorrectly named an item. Skip items that are
  607. // not full paths to shared libraries.
  608. if(tgt)
  609. {
  610. // The target will provide a full path. Make sure it is a shared
  611. // library.
  612. if(tgt->GetType() != cmTarget::SHARED_LIBRARY)
  613. {
  614. return;
  615. }
  616. }
  617. else
  618. {
  619. // Skip items that are not full paths. We will not be able to
  620. // reliably specify them.
  621. if(!cmSystemTools::FileIsFullPath(item.c_str()))
  622. {
  623. return;
  624. }
  625. // Get the name of the library from the file name.
  626. std::string file = cmSystemTools::GetFilenameName(item);
  627. if(!this->ExtractSharedLibraryName.find(file.c_str()))
  628. {
  629. // This is not the name of a shared library.
  630. return;
  631. }
  632. }
  633. // If in linking mode, just link to the shared library.
  634. if(this->SharedDependencyMode == SharedDepModeLink)
  635. {
  636. this->AddItem(item, tgt);
  637. return;
  638. }
  639. // Get a full path to the dependent shared library.
  640. // Add it to the runtime path computation so that the target being
  641. // linked will be able to find it.
  642. std::string lib;
  643. if(tgt)
  644. {
  645. lib = tgt->GetFullPath(this->Config, this->UseImportLibrary);
  646. this->AddLibraryRuntimeInfo(lib, tgt);
  647. }
  648. else
  649. {
  650. lib = item;
  651. this->AddLibraryRuntimeInfo(lib);
  652. }
  653. // Check if we need to include the dependent shared library in other
  654. // path ordering.
  655. cmOrderDirectories* order = 0;
  656. if(this->SharedDependencyMode == SharedDepModeLibDir &&
  657. !this->LinkWithRuntimePath /* AddLibraryRuntimeInfo adds it */)
  658. {
  659. // Add the item to the linker search path.
  660. order = this->OrderLinkerSearchPath;
  661. }
  662. else if(this->SharedDependencyMode == SharedDepModeDir)
  663. {
  664. // Add the item to the separate dependent library search path.
  665. order = this->OrderDependentRPath;
  666. }
  667. if(order)
  668. {
  669. if(tgt)
  670. {
  671. std::string soName = tgt->GetSOName(this->Config);
  672. const char* soname = soName.empty()? 0 : soName.c_str();
  673. order->AddRuntimeLibrary(lib, soname);
  674. }
  675. else
  676. {
  677. order->AddRuntimeLibrary(lib);
  678. }
  679. }
  680. }
  681. //----------------------------------------------------------------------------
  682. void cmComputeLinkInformation::ComputeLinkTypeInfo()
  683. {
  684. // Check whether archives may actually be shared libraries.
  685. this->ArchivesMayBeShared =
  686. this->CMakeInstance->GetPropertyAsBool(
  687. "TARGET_ARCHIVES_MAY_BE_SHARED_LIBS");
  688. // First assume we cannot do link type stuff.
  689. this->LinkTypeEnabled = false;
  690. // Lookup link type selection flags.
  691. const char* static_link_type_flag = 0;
  692. const char* shared_link_type_flag = 0;
  693. const char* target_type_str = 0;
  694. switch(this->Target->GetType())
  695. {
  696. case cmTarget::EXECUTABLE: target_type_str = "EXE"; break;
  697. case cmTarget::SHARED_LIBRARY: target_type_str = "SHARED_LIBRARY"; break;
  698. case cmTarget::MODULE_LIBRARY: target_type_str = "SHARED_MODULE"; break;
  699. default: break;
  700. }
  701. if(target_type_str)
  702. {
  703. std::string static_link_type_flag_var = "CMAKE_";
  704. static_link_type_flag_var += target_type_str;
  705. static_link_type_flag_var += "_LINK_STATIC_";
  706. static_link_type_flag_var += this->LinkLanguage;
  707. static_link_type_flag_var += "_FLAGS";
  708. static_link_type_flag =
  709. this->Makefile->GetDefinition(static_link_type_flag_var.c_str());
  710. std::string shared_link_type_flag_var = "CMAKE_";
  711. shared_link_type_flag_var += target_type_str;
  712. shared_link_type_flag_var += "_LINK_DYNAMIC_";
  713. shared_link_type_flag_var += this->LinkLanguage;
  714. shared_link_type_flag_var += "_FLAGS";
  715. shared_link_type_flag =
  716. this->Makefile->GetDefinition(shared_link_type_flag_var.c_str());
  717. }
  718. // We can support link type switching only if all needed flags are
  719. // known.
  720. if(static_link_type_flag && *static_link_type_flag &&
  721. shared_link_type_flag && *shared_link_type_flag)
  722. {
  723. this->LinkTypeEnabled = true;
  724. this->StaticLinkTypeFlag = static_link_type_flag;
  725. this->SharedLinkTypeFlag = shared_link_type_flag;
  726. }
  727. // Lookup the starting link type from the target (linked statically?).
  728. const char* lss = this->Target->GetProperty("LINK_SEARCH_START_STATIC");
  729. this->StartLinkType = cmSystemTools::IsOn(lss)? LinkStatic : LinkShared;
  730. this->CurrentLinkType = this->StartLinkType;
  731. }
  732. //----------------------------------------------------------------------------
  733. void cmComputeLinkInformation::ComputeItemParserInfo()
  734. {
  735. // Get possible library name prefixes.
  736. cmMakefile* mf = this->Makefile;
  737. this->AddLinkPrefix(mf->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  738. this->AddLinkPrefix(mf->GetDefinition("CMAKE_SHARED_LIBRARY_PREFIX"));
  739. // Import library names should be matched and treated as shared
  740. // libraries for the purposes of linking.
  741. this->AddLinkExtension(mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
  742. LinkShared);
  743. this->AddLinkExtension(mf->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
  744. LinkStatic);
  745. this->AddLinkExtension(mf->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
  746. LinkShared);
  747. this->AddLinkExtension(mf->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"),
  748. LinkUnknown);
  749. if(const char* linkSuffixes =
  750. mf->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS"))
  751. {
  752. std::vector<std::string> linkSuffixVec;
  753. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  754. for(std::vector<std::string>::iterator i = linkSuffixVec.begin();
  755. i != linkSuffixVec.end(); ++i)
  756. {
  757. this->AddLinkExtension(i->c_str(), LinkUnknown);
  758. }
  759. }
  760. if(const char* sharedSuffixes =
  761. mf->GetDefinition("CMAKE_EXTRA_SHARED_LIBRARY_SUFFIXES"))
  762. {
  763. std::vector<std::string> sharedSuffixVec;
  764. cmSystemTools::ExpandListArgument(sharedSuffixes, sharedSuffixVec);
  765. for(std::vector<std::string>::iterator i = sharedSuffixVec.begin();
  766. i != sharedSuffixVec.end(); ++i)
  767. {
  768. this->AddLinkExtension(i->c_str(), LinkShared);
  769. }
  770. }
  771. // Compute a regex to match link extensions.
  772. std::string libext = this->CreateExtensionRegex(this->LinkExtensions,
  773. LinkUnknown);
  774. // Create regex to remove any library extension.
  775. std::string reg("(.*)");
  776. reg += libext;
  777. this->OrderLinkerSearchPath->SetLinkExtensionInfo(this->LinkExtensions,
  778. reg);
  779. // Create a regex to match a library name. Match index 1 will be
  780. // the prefix if it exists and empty otherwise. Match index 2 will
  781. // be the library name. Match index 3 will be the library
  782. // extension.
  783. reg = "^(";
  784. for(std::set<cmStdString>::iterator p = this->LinkPrefixes.begin();
  785. p != this->LinkPrefixes.end(); ++p)
  786. {
  787. reg += *p;
  788. reg += "|";
  789. }
  790. reg += ")";
  791. reg += "([^/:]*)";
  792. // Create a regex to match any library name.
  793. std::string reg_any = reg;
  794. reg_any += libext;
  795. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  796. fprintf(stderr, "any regex [%s]\n", reg_any.c_str());
  797. #endif
  798. this->ExtractAnyLibraryName.compile(reg_any.c_str());
  799. // Create a regex to match static library names.
  800. if(!this->StaticLinkExtensions.empty())
  801. {
  802. std::string reg_static = reg;
  803. reg_static += this->CreateExtensionRegex(this->StaticLinkExtensions,
  804. LinkStatic);
  805. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  806. fprintf(stderr, "static regex [%s]\n", reg_static.c_str());
  807. #endif
  808. this->ExtractStaticLibraryName.compile(reg_static.c_str());
  809. }
  810. // Create a regex to match shared library names.
  811. if(!this->SharedLinkExtensions.empty())
  812. {
  813. std::string reg_shared = reg;
  814. this->SharedRegexString =
  815. this->CreateExtensionRegex(this->SharedLinkExtensions, LinkShared);
  816. reg_shared += this->SharedRegexString;
  817. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  818. fprintf(stderr, "shared regex [%s]\n", reg_shared.c_str());
  819. #endif
  820. this->ExtractSharedLibraryName.compile(reg_shared.c_str());
  821. }
  822. }
  823. //----------------------------------------------------------------------------
  824. void cmComputeLinkInformation::AddLinkPrefix(const char* p)
  825. {
  826. if(p && *p)
  827. {
  828. this->LinkPrefixes.insert(p);
  829. }
  830. }
  831. //----------------------------------------------------------------------------
  832. void cmComputeLinkInformation::AddLinkExtension(const char* e, LinkType type)
  833. {
  834. if(e && *e)
  835. {
  836. if(type == LinkStatic)
  837. {
  838. this->StaticLinkExtensions.push_back(e);
  839. }
  840. if(type == LinkShared)
  841. {
  842. this->SharedLinkExtensions.push_back(e);
  843. }
  844. this->LinkExtensions.push_back(e);
  845. }
  846. }
  847. //----------------------------------------------------------------------------
  848. std::string
  849. cmComputeLinkInformation
  850. ::CreateExtensionRegex(std::vector<std::string> const& exts, LinkType type)
  851. {
  852. // Build a list of extension choices.
  853. std::string libext = "(";
  854. const char* sep = "";
  855. for(std::vector<std::string>::const_iterator i = exts.begin();
  856. i != exts.end(); ++i)
  857. {
  858. // Separate this choice from the previous one.
  859. libext += sep;
  860. sep = "|";
  861. // Store this extension choice with the "." escaped.
  862. libext += "\\";
  863. #if defined(_WIN32) && !defined(__CYGWIN__)
  864. libext += this->NoCaseExpression(i->c_str());
  865. #else
  866. libext += *i;
  867. #endif
  868. }
  869. // Finish the list.
  870. libext += ")";
  871. // Add an optional OpenBSD version component.
  872. if(this->OpenBSD)
  873. {
  874. libext += "(\\.[0-9]+\\.[0-9]+)?";
  875. }
  876. else if(type == LinkShared)
  877. {
  878. libext += "(\\.[0-9]+)?";
  879. }
  880. libext += "$";
  881. return libext;
  882. }
  883. //----------------------------------------------------------------------------
  884. std::string cmComputeLinkInformation::NoCaseExpression(const char* str)
  885. {
  886. std::string ret;
  887. const char* s = str;
  888. while(*s)
  889. {
  890. if(*s == '.')
  891. {
  892. ret += *s;
  893. }
  894. else
  895. {
  896. ret += "[";
  897. ret += static_cast<char>(tolower(*s));
  898. ret += static_cast<char>(toupper(*s));
  899. ret += "]";
  900. }
  901. s++;
  902. }
  903. return ret;
  904. }
  905. //-------------------------------------------------------------------
  906. void cmComputeLinkInformation::SetCurrentLinkType(LinkType lt)
  907. {
  908. // If we are changing the current link type add the flag to tell the
  909. // linker about it.
  910. if(this->CurrentLinkType != lt)
  911. {
  912. this->CurrentLinkType = lt;
  913. if(this->LinkTypeEnabled)
  914. {
  915. switch(this->CurrentLinkType)
  916. {
  917. case LinkStatic:
  918. this->Items.push_back(Item(this->StaticLinkTypeFlag, false));
  919. break;
  920. case LinkShared:
  921. this->Items.push_back(Item(this->SharedLinkTypeFlag, false));
  922. break;
  923. default:
  924. break;
  925. }
  926. }
  927. }
  928. }
  929. //----------------------------------------------------------------------------
  930. void cmComputeLinkInformation::AddTargetItem(std::string const& item,
  931. cmTarget const* target)
  932. {
  933. // This is called to handle a link item that is a full path to a target.
  934. // If the target is not a static library make sure the link type is
  935. // shared. This is because dynamic-mode linking can handle both
  936. // shared and static libraries but static-mode can handle only
  937. // static libraries. If a previous user item changed the link type
  938. // to static we need to make sure it is back to shared.
  939. if(target->GetType() != cmTarget::STATIC_LIBRARY)
  940. {
  941. this->SetCurrentLinkType(LinkShared);
  942. }
  943. // Keep track of shared library targets linked.
  944. if(target->GetType() == cmTarget::SHARED_LIBRARY)
  945. {
  946. this->SharedLibrariesLinked.insert(target);
  947. }
  948. // Handle case of an imported shared library with no soname.
  949. if(this->NoSONameUsesPath &&
  950. target->IsImportedSharedLibWithoutSOName(this->Config))
  951. {
  952. this->AddSharedLibNoSOName(item);
  953. return;
  954. }
  955. // If this platform wants a flag before the full path, add it.
  956. if(!this->LibLinkFileFlag.empty())
  957. {
  958. this->Items.push_back(Item(this->LibLinkFileFlag, false));
  959. }
  960. // For compatibility with CMake 2.4 include the item's directory in
  961. // the linker search path.
  962. if(this->OldLinkDirMode && !target->IsFrameworkOnApple() &&
  963. this->OldLinkDirMask.find(cmSystemTools::GetFilenamePath(item)) ==
  964. this->OldLinkDirMask.end())
  965. {
  966. this->OldLinkDirItems.push_back(item);
  967. }
  968. // Now add the full path to the library.
  969. this->Items.push_back(Item(item, true, target));
  970. }
  971. //----------------------------------------------------------------------------
  972. void cmComputeLinkInformation::AddFullItem(std::string const& item)
  973. {
  974. // Check for the implicit link directory special case.
  975. if(this->CheckImplicitDirItem(item))
  976. {
  977. return;
  978. }
  979. // Check for case of shared library with no builtin soname.
  980. if(this->NoSONameUsesPath && this->CheckSharedLibNoSOName(item))
  981. {
  982. return;
  983. }
  984. // Full path libraries should specify a valid library file name.
  985. // See documentation of CMP0008.
  986. if(this->Target->GetPolicyStatusCMP0008() != cmPolicies::NEW &&
  987. (strstr(this->GlobalGenerator->GetName(), "Visual Studio") ||
  988. strstr(this->GlobalGenerator->GetName(), "Xcode")))
  989. {
  990. std::string file = cmSystemTools::GetFilenameName(item);
  991. if(!this->ExtractAnyLibraryName.find(file.c_str()))
  992. {
  993. this->HandleBadFullItem(item, file);
  994. return;
  995. }
  996. }
  997. // This is called to handle a link item that is a full path.
  998. // If the target is not a static library make sure the link type is
  999. // shared. This is because dynamic-mode linking can handle both
  1000. // shared and static libraries but static-mode can handle only
  1001. // static libraries. If a previous user item changed the link type
  1002. // to static we need to make sure it is back to shared.
  1003. if(this->LinkTypeEnabled)
  1004. {
  1005. std::string name = cmSystemTools::GetFilenameName(item);
  1006. if(this->ExtractSharedLibraryName.find(name))
  1007. {
  1008. this->SetCurrentLinkType(LinkShared);
  1009. }
  1010. else if(!this->ExtractStaticLibraryName.find(item))
  1011. {
  1012. // We cannot determine the type. Assume it is the target's
  1013. // default type.
  1014. this->SetCurrentLinkType(this->StartLinkType);
  1015. }
  1016. }
  1017. // For compatibility with CMake 2.4 include the item's directory in
  1018. // the linker search path.
  1019. if(this->OldLinkDirMode &&
  1020. this->OldLinkDirMask.find(cmSystemTools::GetFilenamePath(item)) ==
  1021. this->OldLinkDirMask.end())
  1022. {
  1023. this->OldLinkDirItems.push_back(item);
  1024. }
  1025. // If this platform wants a flag before the full path, add it.
  1026. if(!this->LibLinkFileFlag.empty())
  1027. {
  1028. this->Items.push_back(Item(this->LibLinkFileFlag, false));
  1029. }
  1030. // Now add the full path to the library.
  1031. this->Items.push_back(Item(item, true));
  1032. }
  1033. //----------------------------------------------------------------------------
  1034. bool cmComputeLinkInformation::CheckImplicitDirItem(std::string const& item)
  1035. {
  1036. // We only switch to a pathless item if the link type may be
  1037. // enforced. Fortunately only platforms that support link types
  1038. // seem to have magic per-architecture implicit link directories.
  1039. if(!this->LinkTypeEnabled)
  1040. {
  1041. return false;
  1042. }
  1043. // Check if this item is in an implicit link directory.
  1044. std::string dir = cmSystemTools::GetFilenamePath(item);
  1045. if(this->ImplicitLinkDirs.find(dir) == this->ImplicitLinkDirs.end())
  1046. {
  1047. // Only libraries in implicit link directories are converted to
  1048. // pathless items.
  1049. return false;
  1050. }
  1051. // Only apply the policy below if the library file is one that can
  1052. // be found by the linker.
  1053. std::string file = cmSystemTools::GetFilenameName(item);
  1054. if(!this->ExtractAnyLibraryName.find(file))
  1055. {
  1056. return false;
  1057. }
  1058. // Many system linkers support multiple architectures by
  1059. // automatically selecting the implicit linker search path for the
  1060. // current architecture. If the library appears in an implicit link
  1061. // directory then just report the file name without the directory
  1062. // portion. This will allow the system linker to locate the proper
  1063. // library for the architecture at link time.
  1064. this->AddUserItem(file, false);
  1065. // Make sure the link directory ordering will find the library.
  1066. this->OrderLinkerSearchPath->AddLinkLibrary(item);
  1067. return true;
  1068. }
  1069. //----------------------------------------------------------------------------
  1070. void cmComputeLinkInformation::AddUserItem(std::string const& item,
  1071. bool pathNotKnown)
  1072. {
  1073. // This is called to handle a link item that does not match a CMake
  1074. // target and is not a full path. We check here if it looks like a
  1075. // library file name to automatically request the proper link type
  1076. // from the linker. For example:
  1077. //
  1078. // foo ==> -lfoo
  1079. // libfoo.a ==> -Wl,-Bstatic -lfoo
  1080. // Pass flags through untouched.
  1081. if(item[0] == '-' || item[0] == '$' || item[0] == '`')
  1082. {
  1083. // if this is a -l option then we might need to warn about
  1084. // CMP0003 so put it in OldUserFlagItems, if it is not a -l
  1085. // or -Wl,-l (-framework -pthread), then allow it without a
  1086. // CMP0003 as -L will not affect those other linker flags
  1087. if(item.find("-l") == 0 || item.find("-Wl,-l") == 0)
  1088. {
  1089. // This is a linker option provided by the user.
  1090. this->OldUserFlagItems.push_back(item);
  1091. }
  1092. // Restore the target link type since this item does not specify
  1093. // one.
  1094. this->SetCurrentLinkType(this->StartLinkType);
  1095. // Use the item verbatim.
  1096. this->Items.push_back(Item(item, false));
  1097. return;
  1098. }
  1099. // Parse out the prefix, base, and suffix components of the
  1100. // library name. If the name matches that of a shared or static
  1101. // library then set the link type accordingly.
  1102. //
  1103. // Search for shared library names first because some platforms
  1104. // have shared libraries with names that match the static library
  1105. // pattern. For example cygwin and msys use the convention
  1106. // libfoo.dll.a for import libraries and libfoo.a for static
  1107. // libraries. On AIX a library with the name libfoo.a can be
  1108. // shared!
  1109. std::string lib;
  1110. if(this->ExtractSharedLibraryName.find(item))
  1111. {
  1112. // This matches a shared library file name.
  1113. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  1114. fprintf(stderr, "shared regex matched [%s] [%s] [%s]\n",
  1115. this->ExtractSharedLibraryName.match(1).c_str(),
  1116. this->ExtractSharedLibraryName.match(2).c_str(),
  1117. this->ExtractSharedLibraryName.match(3).c_str());
  1118. #endif
  1119. // Set the link type to shared.
  1120. this->SetCurrentLinkType(LinkShared);
  1121. // Use just the library name so the linker will search.
  1122. lib = this->ExtractSharedLibraryName.match(2);
  1123. }
  1124. else if(this->ExtractStaticLibraryName.find(item))
  1125. {
  1126. // This matches a static library file name.
  1127. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  1128. fprintf(stderr, "static regex matched [%s] [%s] [%s]\n",
  1129. this->ExtractStaticLibraryName.match(1).c_str(),
  1130. this->ExtractStaticLibraryName.match(2).c_str(),
  1131. this->ExtractStaticLibraryName.match(3).c_str());
  1132. #endif
  1133. // Set the link type to static.
  1134. this->SetCurrentLinkType(LinkStatic);
  1135. // Use just the library name so the linker will search.
  1136. lib = this->ExtractStaticLibraryName.match(2);
  1137. }
  1138. else if(this->ExtractAnyLibraryName.find(item))
  1139. {
  1140. // This matches a library file name.
  1141. #ifdef CM_COMPUTE_LINK_INFO_DEBUG
  1142. fprintf(stderr, "any regex matched [%s] [%s] [%s]\n",
  1143. this->ExtractAnyLibraryName.match(1).c_str(),
  1144. this->ExtractAnyLibraryName.match(2).c_str(),
  1145. this->ExtractAnyLibraryName.match(3).c_str());
  1146. #endif
  1147. // Restore the target link type since this item does not specify
  1148. // one.
  1149. this->SetCurrentLinkType(this->StartLinkType);
  1150. // Use just the library name so the linker will search.
  1151. lib = this->ExtractAnyLibraryName.match(2);
  1152. }
  1153. else
  1154. {
  1155. // This is a name specified by the user.
  1156. if(pathNotKnown)
  1157. {
  1158. this->OldUserFlagItems.push_back(item);
  1159. }
  1160. // We must ask the linker to search for a library with this name.
  1161. // Restore the target link type since this item does not specify
  1162. // one.
  1163. this->SetCurrentLinkType(this->StartLinkType);
  1164. lib = item;
  1165. }
  1166. // Create an option to ask the linker to search for the library.
  1167. std::string out = this->LibLinkFlag;
  1168. out += lib;
  1169. out += this->LibLinkSuffix;
  1170. this->Items.push_back(Item(out, false));
  1171. // Here we could try to find the library the linker will find and
  1172. // add a runtime information entry for it. It would probably not be
  1173. // reliable and we want to encourage use of full paths for library
  1174. // specification.
  1175. }
  1176. //----------------------------------------------------------------------------
  1177. void cmComputeLinkInformation::AddFrameworkItem(std::string const& item)
  1178. {
  1179. // Try to separate the framework name and path.
  1180. if(!this->SplitFramework.find(item.c_str()))
  1181. {
  1182. cmOStringStream e;
  1183. e << "Could not parse framework path \"" << item << "\" "
  1184. << "linked by target " << this->Target->GetName() << ".";
  1185. cmSystemTools::Error(e.str().c_str());
  1186. return;
  1187. }
  1188. std::string fw_path = this->SplitFramework.match(1);
  1189. std::string fw = this->SplitFramework.match(2);
  1190. std::string full_fw = fw_path;
  1191. full_fw += "/";
  1192. full_fw += fw;
  1193. full_fw += ".framework";
  1194. full_fw += "/";
  1195. full_fw += fw;
  1196. // Add the directory portion to the framework search path.
  1197. this->AddFrameworkPath(fw_path);
  1198. // add runtime information
  1199. this->AddLibraryRuntimeInfo(full_fw);
  1200. // Add the item using the -framework option.
  1201. this->Items.push_back(Item("-framework", false));
  1202. fw = this->LocalGenerator->EscapeForShell(fw.c_str());
  1203. this->Items.push_back(Item(fw, false));
  1204. }
  1205. //----------------------------------------------------------------------------
  1206. void cmComputeLinkInformation::AddDirectoryItem(std::string const& item)
  1207. {
  1208. if(this->Makefile->IsOn("APPLE")
  1209. && cmSystemTools::IsPathToFramework(item.c_str()))
  1210. {
  1211. this->AddFrameworkItem(item);
  1212. }
  1213. else
  1214. {
  1215. this->DropDirectoryItem(item);
  1216. }
  1217. }
  1218. //----------------------------------------------------------------------------
  1219. void cmComputeLinkInformation::DropDirectoryItem(std::string const& item)
  1220. {
  1221. // A full path to a directory was found as a link item. Warn the
  1222. // user.
  1223. cmOStringStream e;
  1224. e << "WARNING: Target \"" << this->Target->GetName()
  1225. << "\" requests linking to directory \"" << item << "\". "
  1226. << "Targets may link only to libraries. "
  1227. << "CMake is dropping the item.";
  1228. cmSystemTools::Message(e.str().c_str());
  1229. }
  1230. //----------------------------------------------------------------------------
  1231. void cmComputeLinkInformation::ComputeFrameworkInfo()
  1232. {
  1233. // Avoid adding implicit framework paths.
  1234. std::vector<std::string> implicitDirVec;
  1235. // Get platform-wide implicit directories.
  1236. if(const char* implicitLinks = this->Makefile->GetDefinition
  1237. ("CMAKE_PLATFORM_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES"))
  1238. {
  1239. cmSystemTools::ExpandListArgument(implicitLinks, implicitDirVec);
  1240. }
  1241. // Get language-specific implicit directories.
  1242. std::string implicitDirVar = "CMAKE_";
  1243. implicitDirVar += this->LinkLanguage;
  1244. implicitDirVar += "_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES";
  1245. if(const char* implicitDirs =
  1246. this->Makefile->GetDefinition(implicitDirVar.c_str()))
  1247. {
  1248. cmSystemTools::ExpandListArgument(implicitDirs, implicitDirVec);
  1249. }
  1250. for(std::vector<std::string>::const_iterator i = implicitDirVec.begin();
  1251. i != implicitDirVec.end(); ++i)
  1252. {
  1253. this->FrameworkPathsEmmitted.insert(*i);
  1254. }
  1255. // Regular expression to extract a framework path and name.
  1256. this->SplitFramework.compile("(.*)/(.*)\\.framework$");
  1257. }
  1258. //----------------------------------------------------------------------------
  1259. void cmComputeLinkInformation::AddFrameworkPath(std::string const& p)
  1260. {
  1261. if(this->FrameworkPathsEmmitted.insert(p).second)
  1262. {
  1263. this->FrameworkPaths.push_back(p);
  1264. }
  1265. }
  1266. //----------------------------------------------------------------------------
  1267. bool cmComputeLinkInformation::CheckSharedLibNoSOName(std::string const& item)
  1268. {
  1269. // This platform will use the path to a library as its soname if the
  1270. // library is given via path and was not built with an soname. If
  1271. // this is a shared library that might be the case.
  1272. std::string file = cmSystemTools::GetFilenameName(item);
  1273. if(this->ExtractSharedLibraryName.find(file))
  1274. {
  1275. // If we can guess the soname fairly reliably then assume the
  1276. // library has one. Otherwise assume the library has no builtin
  1277. // soname.
  1278. std::string soname;
  1279. if(!cmSystemTools::GuessLibrarySOName(item, soname))
  1280. {
  1281. this->AddSharedLibNoSOName(item);
  1282. return true;
  1283. }
  1284. }
  1285. return false;
  1286. }
  1287. //----------------------------------------------------------------------------
  1288. void cmComputeLinkInformation::AddSharedLibNoSOName(std::string const& item)
  1289. {
  1290. // We have a full path to a shared library with no soname. We need
  1291. // to ask the linker to locate the item because otherwise the path
  1292. // we give to it will be embedded in the target linked. Then at
  1293. // runtime the dynamic linker will search for the library using the
  1294. // path instead of just the name.
  1295. std::string file = cmSystemTools::GetFilenameName(item);
  1296. this->AddUserItem(file, false);
  1297. // Make sure the link directory ordering will find the library.
  1298. this->OrderLinkerSearchPath->AddLinkLibrary(item);
  1299. }
  1300. //----------------------------------------------------------------------------
  1301. void cmComputeLinkInformation::HandleBadFullItem(std::string const& item,
  1302. std::string const& file)
  1303. {
  1304. // Do not depend on things that do not exist.
  1305. std::vector<std::string>::iterator i =
  1306. std::find(this->Depends.begin(), this->Depends.end(), item);
  1307. if(i != this->Depends.end())
  1308. {
  1309. this->Depends.erase(i);
  1310. }
  1311. // Tell the linker to search for the item and provide the proper
  1312. // path for it. Do not contribute to any CMP0003 warning (do not
  1313. // put in OldLinkDirItems or OldUserFlagItems).
  1314. this->AddUserItem(file, false);
  1315. this->OrderLinkerSearchPath->AddLinkLibrary(item);
  1316. // Produce any needed message.
  1317. switch(this->Target->GetPolicyStatusCMP0008())
  1318. {
  1319. case cmPolicies::WARN:
  1320. {
  1321. // Print the warning at most once for this item.
  1322. std::string wid = "CMP0008-WARNING-GIVEN-";
  1323. wid += item;
  1324. if(!this->CMakeInstance->GetPropertyAsBool(wid.c_str()))
  1325. {
  1326. this->CMakeInstance->SetProperty(wid.c_str(), "1");
  1327. cmOStringStream w;
  1328. w << (this->Makefile->GetPolicies()
  1329. ->GetPolicyWarning(cmPolicies::CMP0008)) << "\n"
  1330. << "Target \"" << this->Target->GetName() << "\" links to item\n"
  1331. << " " << item << "\n"
  1332. << "which is a full-path but not a valid library file name.";
  1333. this->CMakeInstance->IssueMessage(cmake::AUTHOR_WARNING, w.str(),
  1334. this->Target->GetBacktrace());
  1335. }
  1336. }
  1337. case cmPolicies::OLD:
  1338. // OLD behavior does not warn.
  1339. break;
  1340. case cmPolicies::NEW:
  1341. // NEW behavior will not get here.
  1342. break;
  1343. case cmPolicies::REQUIRED_IF_USED:
  1344. case cmPolicies::REQUIRED_ALWAYS:
  1345. {
  1346. cmOStringStream e;
  1347. e << (this->Makefile->GetPolicies()->
  1348. GetRequiredPolicyError(cmPolicies::CMP0008)) << "\n"
  1349. << "Target \"" << this->Target->GetName() << "\" links to item\n"
  1350. << " " << item << "\n"
  1351. << "which is a full-path but not a valid library file name.";
  1352. this->CMakeInstance->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1353. this->Target->GetBacktrace());
  1354. }
  1355. break;
  1356. }
  1357. }
  1358. //----------------------------------------------------------------------------
  1359. bool cmComputeLinkInformation::FinishLinkerSearchDirectories()
  1360. {
  1361. // Support broken projects if necessary.
  1362. if(this->OldLinkDirItems.empty() || this->OldUserFlagItems.empty() ||
  1363. !this->OldLinkDirMode)
  1364. {
  1365. return true;
  1366. }
  1367. // Enforce policy constraints.
  1368. switch(this->Target->GetPolicyStatusCMP0003())
  1369. {
  1370. case cmPolicies::WARN:
  1371. if(!this->CMakeInstance->GetPropertyAsBool("CMP0003-WARNING-GIVEN"))
  1372. {
  1373. this->CMakeInstance->SetProperty("CMP0003-WARNING-GIVEN", "1");
  1374. cmOStringStream w;
  1375. this->PrintLinkPolicyDiagnosis(w);
  1376. this->CMakeInstance->IssueMessage(cmake::AUTHOR_WARNING, w.str(),
  1377. this->Target->GetBacktrace());
  1378. }
  1379. case cmPolicies::OLD:
  1380. // OLD behavior is to add the paths containing libraries with
  1381. // known full paths as link directories.
  1382. break;
  1383. case cmPolicies::NEW:
  1384. // Should never happen due to assignment of OldLinkDirMode
  1385. return true;
  1386. case cmPolicies::REQUIRED_IF_USED:
  1387. case cmPolicies::REQUIRED_ALWAYS:
  1388. {
  1389. cmOStringStream e;
  1390. e << (this->Makefile->GetPolicies()->
  1391. GetRequiredPolicyError(cmPolicies::CMP0003)) << "\n";
  1392. this->PrintLinkPolicyDiagnosis(e);
  1393. this->CMakeInstance->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1394. this->Target->GetBacktrace());
  1395. return false;
  1396. }
  1397. }
  1398. // Add the link directories for full path items.
  1399. for(std::vector<std::string>::const_iterator
  1400. i = this->OldLinkDirItems.begin();
  1401. i != this->OldLinkDirItems.end(); ++i)
  1402. {
  1403. this->OrderLinkerSearchPath->AddLinkLibrary(*i);
  1404. }
  1405. return true;
  1406. }
  1407. //----------------------------------------------------------------------------
  1408. void cmComputeLinkInformation::PrintLinkPolicyDiagnosis(std::ostream& os)
  1409. {
  1410. // Tell the user what to do.
  1411. os << "Policy CMP0003 should be set before this line. "
  1412. << "Add code such as\n"
  1413. << " if(COMMAND cmake_policy)\n"
  1414. << " cmake_policy(SET CMP0003 NEW)\n"
  1415. << " endif(COMMAND cmake_policy)\n"
  1416. << "as early as possible but after the most recent call to "
  1417. << "cmake_minimum_required or cmake_policy(VERSION). ";
  1418. // List the items that might need the old-style paths.
  1419. os << "This warning appears because target \""
  1420. << this->Target->GetName() << "\" "
  1421. << "links to some libraries for which the linker must search:\n";
  1422. {
  1423. // Format the list of unknown items to be as short as possible while
  1424. // still fitting in the allowed width (a true solution would be the
  1425. // bin packing problem if we were allowed to change the order).
  1426. std::string::size_type max_size = 76;
  1427. std::string line;
  1428. const char* sep = " ";
  1429. for(std::vector<std::string>::const_iterator
  1430. i = this->OldUserFlagItems.begin();
  1431. i != this->OldUserFlagItems.end(); ++i)
  1432. {
  1433. // If the addition of another item will exceed the limit then
  1434. // output the current line and reset it. Note that the separator
  1435. // is either " " or ", " which is always 2 characters.
  1436. if(!line.empty() && (line.size() + i->size() + 2) > max_size)
  1437. {
  1438. os << line << "\n";
  1439. sep = " ";
  1440. line = "";
  1441. }
  1442. line += sep;
  1443. line += *i;
  1444. // Convert to the other separator.
  1445. sep = ", ";
  1446. }
  1447. if(!line.empty())
  1448. {
  1449. os << line << "\n";
  1450. }
  1451. }
  1452. // List the paths old behavior is adding.
  1453. os << "and other libraries with known full path:\n";
  1454. std::set<cmStdString> emitted;
  1455. for(std::vector<std::string>::const_iterator
  1456. i = this->OldLinkDirItems.begin();
  1457. i != this->OldLinkDirItems.end(); ++i)
  1458. {
  1459. if(emitted.insert(cmSystemTools::GetFilenamePath(*i)).second)
  1460. {
  1461. os << " " << *i << "\n";
  1462. }
  1463. }
  1464. // Explain.
  1465. os << "CMake is adding directories in the second list to the linker "
  1466. << "search path in case they are needed to find libraries from the "
  1467. << "first list (for backwards compatibility with CMake 2.4). "
  1468. << "Set policy CMP0003 to OLD or NEW to enable or disable this "
  1469. << "behavior explicitly. "
  1470. << "Run \"cmake --help-policy CMP0003\" for more information.";
  1471. }
  1472. //----------------------------------------------------------------------------
  1473. void cmComputeLinkInformation::LoadImplicitLinkInfo()
  1474. {
  1475. std::vector<std::string> implicitDirVec;
  1476. // Get platform-wide implicit directories.
  1477. if(const char* implicitLinks =
  1478. (this->Makefile->GetDefinition
  1479. ("CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES")))
  1480. {
  1481. cmSystemTools::ExpandListArgument(implicitLinks, implicitDirVec);
  1482. }
  1483. // Append library architecture to all implicit platform directories
  1484. // and add them to the set
  1485. if(const char* libraryArch =
  1486. this->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE"))
  1487. {
  1488. for (std::vector<std::string>::const_iterator i = implicitDirVec.begin();
  1489. i != implicitDirVec.end(); ++i)
  1490. {
  1491. this->ImplicitLinkDirs.insert(*i + "/" + libraryArch);
  1492. }
  1493. }
  1494. // Get language-specific implicit directories.
  1495. std::string implicitDirVar = "CMAKE_";
  1496. implicitDirVar += this->LinkLanguage;
  1497. implicitDirVar += "_IMPLICIT_LINK_DIRECTORIES";
  1498. if(const char* implicitDirs =
  1499. this->Makefile->GetDefinition(implicitDirVar.c_str()))
  1500. {
  1501. cmSystemTools::ExpandListArgument(implicitDirs, implicitDirVec);
  1502. }
  1503. // Store implicit link directories.
  1504. for(std::vector<std::string>::const_iterator i = implicitDirVec.begin();
  1505. i != implicitDirVec.end(); ++i)
  1506. {
  1507. this->ImplicitLinkDirs.insert(*i);
  1508. }
  1509. // Get language-specific implicit libraries.
  1510. std::vector<std::string> implicitLibVec;
  1511. std::string implicitLibVar = "CMAKE_";
  1512. implicitLibVar += this->LinkLanguage;
  1513. implicitLibVar += "_IMPLICIT_LINK_LIBRARIES";
  1514. if(const char* implicitLibs =
  1515. this->Makefile->GetDefinition(implicitLibVar.c_str()))
  1516. {
  1517. cmSystemTools::ExpandListArgument(implicitLibs, implicitLibVec);
  1518. }
  1519. // Store implicit link libraries.
  1520. for(std::vector<std::string>::const_iterator i = implicitLibVec.begin();
  1521. i != implicitLibVec.end(); ++i)
  1522. {
  1523. // Items starting in '-' but not '-l' are flags, not libraries,
  1524. // and should not be filtered by this implicit list.
  1525. std::string const& item = *i;
  1526. if(item[0] != '-' || item[1] == 'l')
  1527. {
  1528. this->ImplicitLinkLibs.insert(item);
  1529. }
  1530. }
  1531. // Get platform specific rpath link directories
  1532. if(const char *rpathDirs =
  1533. (this->Makefile->GetDefinition
  1534. ("CMAKE_PLATFORM_RUNTIME_PATH")))
  1535. {
  1536. cmSystemTools::ExpandListArgument(rpathDirs, this->RuntimeLinkDirs);
  1537. }
  1538. }
  1539. //----------------------------------------------------------------------------
  1540. std::vector<std::string> const&
  1541. cmComputeLinkInformation::GetRuntimeSearchPath()
  1542. {
  1543. return this->OrderRuntimeSearchPath->GetOrderedDirectories();
  1544. }
  1545. //----------------------------------------------------------------------------
  1546. void
  1547. cmComputeLinkInformation::AddLibraryRuntimeInfo(std::string const& fullPath,
  1548. cmTarget const* target)
  1549. {
  1550. // Ignore targets on Apple where install_name is not @rpath.
  1551. // The dependenty library can be found with other means such as
  1552. // @loader_path or full paths.
  1553. if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  1554. {
  1555. if(!target->HasMacOSXRpathInstallNameDir(this->Config))
  1556. {
  1557. return;
  1558. }
  1559. }
  1560. // Libraries with unknown type must be handled using just the file
  1561. // on disk.
  1562. if(target->GetType() == cmTarget::UNKNOWN_LIBRARY)
  1563. {
  1564. this->AddLibraryRuntimeInfo(fullPath);
  1565. return;
  1566. }
  1567. // Skip targets that are not shared libraries (modules cannot be linked).
  1568. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1569. {
  1570. return;
  1571. }
  1572. // Try to get the soname of the library. Only files with this name
  1573. // could possibly conflict.
  1574. std::string soName = target->GetSOName(this->Config);
  1575. const char* soname = soName.empty()? 0 : soName.c_str();
  1576. // Include this library in the runtime path ordering.
  1577. this->OrderRuntimeSearchPath->AddRuntimeLibrary(fullPath, soname);
  1578. if(this->LinkWithRuntimePath)
  1579. {
  1580. this->OrderLinkerSearchPath->AddRuntimeLibrary(fullPath, soname);
  1581. }
  1582. }
  1583. //----------------------------------------------------------------------------
  1584. void
  1585. cmComputeLinkInformation::AddLibraryRuntimeInfo(std::string const& fullPath)
  1586. {
  1587. // Get the name of the library from the file name.
  1588. bool is_shared_library = false;
  1589. std::string file = cmSystemTools::GetFilenameName(fullPath);
  1590. if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  1591. {
  1592. // Check that @rpath is part of the install name.
  1593. // If it isn't, return.
  1594. std::string soname;
  1595. if(!cmSystemTools::GuessLibraryInstallName(fullPath, soname))
  1596. {
  1597. return;
  1598. }
  1599. if(soname.find("@rpath") == std::string::npos)
  1600. {
  1601. return;
  1602. }
  1603. }
  1604. is_shared_library = this->ExtractSharedLibraryName.find(file.c_str());
  1605. if(!is_shared_library)
  1606. {
  1607. // On some platforms (AIX) a shared library may look static.
  1608. if(this->ArchivesMayBeShared)
  1609. {
  1610. if(this->ExtractStaticLibraryName.find(file.c_str()))
  1611. {
  1612. // This is the name of a shared library or archive.
  1613. is_shared_library = true;
  1614. }
  1615. }
  1616. }
  1617. // It could be an Apple framework
  1618. if(!is_shared_library)
  1619. {
  1620. if(fullPath.find(".framework") != std::string::npos)
  1621. {
  1622. cmsys::RegularExpression splitFramework;
  1623. splitFramework.compile("^(.*)/(.*).framework/(.*)$");
  1624. if(splitFramework.find(fullPath) &&
  1625. (std::string::npos !=
  1626. splitFramework.match(3).find(splitFramework.match(2))))
  1627. {
  1628. is_shared_library = true;
  1629. }
  1630. }
  1631. }
  1632. if(!is_shared_library)
  1633. {
  1634. return;
  1635. }
  1636. // Include this library in the runtime path ordering.
  1637. this->OrderRuntimeSearchPath->AddRuntimeLibrary(fullPath);
  1638. if(this->LinkWithRuntimePath)
  1639. {
  1640. this->OrderLinkerSearchPath->AddRuntimeLibrary(fullPath);
  1641. }
  1642. }
  1643. //----------------------------------------------------------------------------
  1644. static void cmCLI_ExpandListUnique(const char* str,
  1645. std::vector<std::string>& out,
  1646. std::set<cmStdString>& emitted)
  1647. {
  1648. std::vector<std::string> tmp;
  1649. cmSystemTools::ExpandListArgument(str, tmp);
  1650. for(std::vector<std::string>::iterator i = tmp.begin(); i != tmp.end(); ++i)
  1651. {
  1652. if(emitted.insert(*i).second)
  1653. {
  1654. out.push_back(*i);
  1655. }
  1656. }
  1657. }
  1658. //----------------------------------------------------------------------------
  1659. void cmComputeLinkInformation::GetRPath(std::vector<std::string>& runtimeDirs,
  1660. bool for_install)
  1661. {
  1662. // Select whether to generate runtime search directories.
  1663. bool outputRuntime =
  1664. !this->Makefile->IsOn("CMAKE_SKIP_RPATH") && !this->RuntimeFlag.empty();
  1665. // Select whether to generate an rpath for the install tree or the
  1666. // build tree.
  1667. bool linking_for_install =
  1668. (for_install ||
  1669. this->Target->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"));
  1670. bool use_install_rpath =
  1671. (outputRuntime && this->Target->HaveInstallTreeRPATH() &&
  1672. linking_for_install);
  1673. bool use_build_rpath =
  1674. (outputRuntime && this->Target->HaveBuildTreeRPATH(this->Config) &&
  1675. !linking_for_install);
  1676. bool use_link_rpath =
  1677. outputRuntime && linking_for_install &&
  1678. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH") &&
  1679. this->Target->GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
  1680. // Construct the RPATH.
  1681. std::set<cmStdString> emitted;
  1682. if(use_install_rpath)
  1683. {
  1684. const char* install_rpath = this->Target->GetProperty("INSTALL_RPATH");
  1685. cmCLI_ExpandListUnique(install_rpath, runtimeDirs, emitted);
  1686. }
  1687. if(use_build_rpath || use_link_rpath)
  1688. {
  1689. std::string rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  1690. const char *stagePath
  1691. = this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
  1692. const char *installPrefix
  1693. = this->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  1694. cmSystemTools::ConvertToUnixSlashes(rootPath);
  1695. std::vector<std::string> const& rdirs = this->GetRuntimeSearchPath();
  1696. for(std::vector<std::string>::const_iterator ri = rdirs.begin();
  1697. ri != rdirs.end(); ++ri)
  1698. {
  1699. // Put this directory in the rpath if using build-tree rpath
  1700. // support or if using the link path as an rpath.
  1701. if(use_build_rpath)
  1702. {
  1703. std::string d = *ri;
  1704. if (!rootPath.empty() && d.find(rootPath) == 0)
  1705. {
  1706. d = d.substr(rootPath.size());
  1707. }
  1708. else if (stagePath && *stagePath && d.find(stagePath) == 0)
  1709. {
  1710. std::string suffix = d.substr(strlen(stagePath));
  1711. d = installPrefix;
  1712. d += "/";
  1713. d += suffix;
  1714. cmSystemTools::ConvertToUnixSlashes(d);
  1715. }
  1716. if(emitted.insert(d).second)
  1717. {
  1718. runtimeDirs.push_back(d);
  1719. }
  1720. }
  1721. else if(use_link_rpath)
  1722. {
  1723. // Do not add any path inside the source or build tree.
  1724. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1725. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1726. if(!cmSystemTools::ComparePath(ri->c_str(), topSourceDir) &&
  1727. !cmSystemTools::ComparePath(ri->c_str(), topBinaryDir) &&
  1728. !cmSystemTools::IsSubDirectory(ri->c_str(), topSourceDir) &&
  1729. !cmSystemTools::IsSubDirectory(ri->c_str(), topBinaryDir))
  1730. {
  1731. std::string d = *ri;
  1732. if (!rootPath.empty() && d.find(rootPath) == 0)
  1733. {
  1734. d = d.substr(rootPath.size());
  1735. }
  1736. else if (stagePath && *stagePath && d.find(stagePath) == 0)
  1737. {
  1738. std::string suffix = d.substr(strlen(stagePath));
  1739. d = installPrefix;
  1740. d += "/";
  1741. d += suffix;
  1742. cmSystemTools::ConvertToUnixSlashes(d);
  1743. }
  1744. if(emitted.insert(d).second)
  1745. {
  1746. runtimeDirs.push_back(d);
  1747. }
  1748. }
  1749. }
  1750. }
  1751. }
  1752. // Add runtime paths required by the languages to always be
  1753. // present. This is done even when skipping rpath support.
  1754. {
  1755. cmTarget::LinkClosure const* lc =
  1756. this->Target->GetLinkClosure(this->Config, this->HeadTarget);
  1757. for(std::vector<std::string>::const_iterator li = lc->Languages.begin();
  1758. li != lc->Languages.end(); ++li)
  1759. {
  1760. std::string useVar = "CMAKE_" + *li +
  1761. "_USE_IMPLICIT_LINK_DIRECTORIES_IN_RUNTIME_PATH";
  1762. if(this->Makefile->IsOn(useVar.c_str()))
  1763. {
  1764. std::string dirVar = "CMAKE_" + *li +
  1765. "_IMPLICIT_LINK_DIRECTORIES";
  1766. if(const char* dirs = this->Makefile->GetDefinition(dirVar.c_str()))
  1767. {
  1768. cmCLI_ExpandListUnique(dirs, runtimeDirs, emitted);
  1769. }
  1770. }
  1771. }
  1772. }
  1773. // Add runtime paths required by the platform to always be
  1774. // present. This is done even when skipping rpath support.
  1775. cmCLI_ExpandListUnique(this->RuntimeAlways.c_str(), runtimeDirs, emitted);
  1776. }
  1777. //----------------------------------------------------------------------------
  1778. std::string cmComputeLinkInformation::GetRPathString(bool for_install)
  1779. {
  1780. // Get the directories to use.
  1781. std::vector<std::string> runtimeDirs;
  1782. this->GetRPath(runtimeDirs, for_install);
  1783. // Concatenate the paths.
  1784. std::string rpath;
  1785. const char* sep = "";
  1786. for(std::vector<std::string>::const_iterator ri = runtimeDirs.begin();
  1787. ri != runtimeDirs.end(); ++ri)
  1788. {
  1789. // Separate from previous path.
  1790. rpath += sep;
  1791. sep = this->GetRuntimeSep().c_str();
  1792. // Add this path.
  1793. rpath += *ri;
  1794. }
  1795. // If the rpath will be replaced at install time, prepare space.
  1796. if(!for_install && this->RuntimeUseChrpath)
  1797. {
  1798. if(!rpath.empty())
  1799. {
  1800. // Add one trailing separator so the linker does not re-use the
  1801. // rpath .dynstr entry for a symbol name that happens to match
  1802. // the end of the rpath string.
  1803. rpath += this->GetRuntimeSep();
  1804. }
  1805. // Make sure it is long enough to hold the replacement value.
  1806. std::string::size_type minLength = this->GetChrpathString().length();
  1807. while(rpath.length() < minLength)
  1808. {
  1809. rpath += this->GetRuntimeSep();
  1810. }
  1811. }
  1812. return rpath;
  1813. }
  1814. //----------------------------------------------------------------------------
  1815. std::string cmComputeLinkInformation::GetChrpathString()
  1816. {
  1817. if(!this->RuntimeUseChrpath)
  1818. {
  1819. return "";
  1820. }
  1821. return this->GetRPathString(true);
  1822. }