cmComputeLinkInformation.cxx 55 KB

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