cmComputeLinkInformation.cxx 69 KB

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