cmInstallTargetGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 "cmInstallTargetGenerator.h"
  11. #include "cmComputeLinkInformation.h"
  12. #include "cmGeneratorExpression.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmake.h"
  18. #include "cmGeneratorTarget.h"
  19. #include <assert.h>
  20. //----------------------------------------------------------------------------
  21. cmInstallTargetGenerator
  22. ::cmInstallTargetGenerator(const std::string& targetName,
  23. const char* dest, bool implib,
  24. const char* file_permissions,
  25. std::vector<std::string> const& configurations,
  26. const char* component,
  27. MessageLevel message,
  28. bool optional):
  29. cmInstallGenerator(dest, configurations, component, message),
  30. TargetName(targetName),
  31. Target(0),
  32. FilePermissions(file_permissions),
  33. ImportLibrary(implib),
  34. Optional(optional)
  35. {
  36. this->ActionsPerConfig = true;
  37. this->NamelinkMode = NamelinkModeNone;
  38. }
  39. //----------------------------------------------------------------------------
  40. cmInstallTargetGenerator
  41. ::~cmInstallTargetGenerator()
  42. {
  43. }
  44. //----------------------------------------------------------------------------
  45. void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
  46. {
  47. // Warn if installing an exclude-from-all target.
  48. if(this->Target->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  49. {
  50. std::ostringstream msg;
  51. msg << "WARNING: Target \"" << this->Target->GetName()
  52. << "\" has EXCLUDE_FROM_ALL set and will not be built by default "
  53. << "but an install rule has been provided for it. CMake does "
  54. << "not define behavior for this case.";
  55. cmSystemTools::Message(msg.str().c_str(), "Warning");
  56. }
  57. // Perform the main install script generation.
  58. this->cmInstallGenerator::GenerateScript(os);
  59. }
  60. //----------------------------------------------------------------------------
  61. void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
  62. const std::string& config,
  63. Indent const& indent)
  64. {
  65. // Compute the build tree directory from which to copy the target.
  66. std::string fromDirConfig;
  67. if(this->Target->NeedRelinkBeforeInstall(config))
  68. {
  69. fromDirConfig =
  70. this->Target->Target->GetMakefile()->GetCurrentBinaryDirectory();
  71. fromDirConfig += cmake::GetCMakeFilesDirectory();
  72. fromDirConfig += "/CMakeRelink.dir/";
  73. }
  74. else
  75. {
  76. fromDirConfig =
  77. this->Target->GetDirectory(config, this->ImportLibrary);
  78. fromDirConfig += "/";
  79. }
  80. std::string toDir =
  81. this->ConvertToAbsoluteDestination(this->GetDestination(config));
  82. toDir += "/";
  83. // Compute the list of files to install for this target.
  84. std::vector<std::string> filesFrom;
  85. std::vector<std::string> filesTo;
  86. std::string literal_args;
  87. cmState::TargetType targetType = this->Target->GetType();
  88. cmInstallType type = cmInstallType();
  89. switch(targetType)
  90. {
  91. case cmState::EXECUTABLE: type = cmInstallType_EXECUTABLE; break;
  92. case cmState::STATIC_LIBRARY: type = cmInstallType_STATIC_LIBRARY; break;
  93. case cmState::SHARED_LIBRARY: type = cmInstallType_SHARED_LIBRARY; break;
  94. case cmState::MODULE_LIBRARY: type = cmInstallType_MODULE_LIBRARY; break;
  95. case cmState::INTERFACE_LIBRARY:
  96. // Not reachable. We never create a cmInstallTargetGenerator for
  97. // an INTERFACE_LIBRARY.
  98. assert(0 && "INTERFACE_LIBRARY targets have no installable outputs.");
  99. break;
  100. case cmState::OBJECT_LIBRARY:
  101. case cmState::UTILITY:
  102. case cmState::GLOBAL_TARGET:
  103. case cmState::UNKNOWN_LIBRARY:
  104. this->Target->Target->GetMakefile()->IssueMessage(cmake::INTERNAL_ERROR,
  105. "cmInstallTargetGenerator created with non-installable target.");
  106. return;
  107. }
  108. if(targetType == cmState::EXECUTABLE)
  109. {
  110. // There is a bug in cmInstallCommand if this fails.
  111. assert(this->NamelinkMode == NamelinkModeNone);
  112. std::string targetName;
  113. std::string targetNameReal;
  114. std::string targetNameImport;
  115. std::string targetNamePDB;
  116. this->Target->GetExecutableNames(targetName, targetNameReal,
  117. targetNameImport, targetNamePDB,
  118. config);
  119. if(this->ImportLibrary)
  120. {
  121. std::string from1 = fromDirConfig + targetNameImport;
  122. std::string to1 = toDir + targetNameImport;
  123. filesFrom.push_back(from1);
  124. filesTo.push_back(to1);
  125. std::string targetNameImportLib;
  126. if(this->Target->GetImplibGNUtoMS(targetNameImport,
  127. targetNameImportLib))
  128. {
  129. filesFrom.push_back(fromDirConfig + targetNameImportLib);
  130. filesTo.push_back(toDir + targetNameImportLib);
  131. }
  132. // An import library looks like a static library.
  133. type = cmInstallType_STATIC_LIBRARY;
  134. }
  135. else
  136. {
  137. std::string from1 = fromDirConfig + targetName;
  138. std::string to1 = toDir + targetName;
  139. // Handle OSX Bundles.
  140. if(this->Target->Target->IsAppBundleOnApple())
  141. {
  142. // Install the whole app bundle directory.
  143. type = cmInstallType_DIRECTORY;
  144. literal_args += " USE_SOURCE_PERMISSIONS";
  145. from1 += ".app";
  146. // Tweaks apply to the binary inside the bundle.
  147. to1 += ".app/Contents/MacOS/";
  148. to1 += targetName;
  149. }
  150. else
  151. {
  152. // Tweaks apply to the real file, so list it first.
  153. if(targetNameReal != targetName)
  154. {
  155. std::string from2 = fromDirConfig + targetNameReal;
  156. std::string to2 = toDir += targetNameReal;
  157. filesFrom.push_back(from2);
  158. filesTo.push_back(to2);
  159. }
  160. }
  161. filesFrom.push_back(from1);
  162. filesTo.push_back(to1);
  163. }
  164. }
  165. else
  166. {
  167. std::string targetName;
  168. std::string targetNameSO;
  169. std::string targetNameReal;
  170. std::string targetNameImport;
  171. std::string targetNamePDB;
  172. this->Target->GetLibraryNames(targetName, targetNameSO,
  173. targetNameReal,
  174. targetNameImport, targetNamePDB,
  175. config);
  176. if(this->ImportLibrary)
  177. {
  178. // There is a bug in cmInstallCommand if this fails.
  179. assert(this->NamelinkMode == NamelinkModeNone);
  180. std::string from1 = fromDirConfig + targetNameImport;
  181. std::string to1 = toDir + targetNameImport;
  182. filesFrom.push_back(from1);
  183. filesTo.push_back(to1);
  184. std::string targetNameImportLib;
  185. if(this->Target->GetImplibGNUtoMS(targetNameImport,
  186. targetNameImportLib))
  187. {
  188. filesFrom.push_back(fromDirConfig + targetNameImportLib);
  189. filesTo.push_back(toDir + targetNameImportLib);
  190. }
  191. // An import library looks like a static library.
  192. type = cmInstallType_STATIC_LIBRARY;
  193. }
  194. else if(this->Target->IsFrameworkOnApple())
  195. {
  196. // There is a bug in cmInstallCommand if this fails.
  197. assert(this->NamelinkMode == NamelinkModeNone);
  198. // Install the whole framework directory.
  199. type = cmInstallType_DIRECTORY;
  200. literal_args += " USE_SOURCE_PERMISSIONS";
  201. std::string from1 = fromDirConfig + targetName;
  202. from1 = cmSystemTools::GetFilenamePath(from1);
  203. // Tweaks apply to the binary inside the bundle.
  204. std::string to1 = toDir + targetNameReal;
  205. filesFrom.push_back(from1);
  206. filesTo.push_back(to1);
  207. }
  208. else if(this->Target->Target->IsCFBundleOnApple())
  209. {
  210. // Install the whole app bundle directory.
  211. type = cmInstallType_DIRECTORY;
  212. literal_args += " USE_SOURCE_PERMISSIONS";
  213. std::string targetNameBase = targetName.substr(0, targetName.find('/'));
  214. std::string from1 = fromDirConfig + targetNameBase;
  215. std::string to1 = toDir + targetName;
  216. filesFrom.push_back(from1);
  217. filesTo.push_back(to1);
  218. }
  219. else
  220. {
  221. bool haveNamelink = false;
  222. // Library link name.
  223. std::string fromName = fromDirConfig + targetName;
  224. std::string toName = toDir + targetName;
  225. // Library interface name.
  226. std::string fromSOName;
  227. std::string toSOName;
  228. if(targetNameSO != targetName)
  229. {
  230. haveNamelink = true;
  231. fromSOName = fromDirConfig + targetNameSO;
  232. toSOName = toDir + targetNameSO;
  233. }
  234. // Library implementation name.
  235. std::string fromRealName;
  236. std::string toRealName;
  237. if(targetNameReal != targetName &&
  238. targetNameReal != targetNameSO)
  239. {
  240. haveNamelink = true;
  241. fromRealName = fromDirConfig + targetNameReal;
  242. toRealName = toDir + targetNameReal;
  243. }
  244. // Add the names based on the current namelink mode.
  245. if(haveNamelink)
  246. {
  247. // With a namelink we need to check the mode.
  248. if(this->NamelinkMode == NamelinkModeOnly)
  249. {
  250. // Install the namelink only.
  251. filesFrom.push_back(fromName);
  252. filesTo.push_back(toName);
  253. }
  254. else
  255. {
  256. // Install the real file if it has its own name.
  257. if(!fromRealName.empty())
  258. {
  259. filesFrom.push_back(fromRealName);
  260. filesTo.push_back(toRealName);
  261. }
  262. // Install the soname link if it has its own name.
  263. if(!fromSOName.empty())
  264. {
  265. filesFrom.push_back(fromSOName);
  266. filesTo.push_back(toSOName);
  267. }
  268. // Install the namelink if it is not to be skipped.
  269. if(this->NamelinkMode != NamelinkModeSkip)
  270. {
  271. filesFrom.push_back(fromName);
  272. filesTo.push_back(toName);
  273. }
  274. }
  275. }
  276. else
  277. {
  278. // Without a namelink there will be only one file. Install it
  279. // if this is not a namelink-only rule.
  280. if(this->NamelinkMode != NamelinkModeOnly)
  281. {
  282. filesFrom.push_back(fromName);
  283. filesTo.push_back(toName);
  284. }
  285. }
  286. }
  287. }
  288. // If this fails the above code is buggy.
  289. assert(filesFrom.size() == filesTo.size());
  290. // Skip this rule if no files are to be installed for the target.
  291. if(filesFrom.empty())
  292. {
  293. return;
  294. }
  295. // Add pre-installation tweaks.
  296. this->AddTweak(os, indent, config, filesTo,
  297. &cmInstallTargetGenerator::PreReplacementTweaks);
  298. // Write code to install the target file.
  299. const char* no_dir_permissions = 0;
  300. const char* no_rename = 0;
  301. bool optional = this->Optional || this->ImportLibrary;
  302. this->AddInstallRule(os, this->GetDestination(config),
  303. type, filesFrom, optional,
  304. this->FilePermissions.c_str(), no_dir_permissions,
  305. no_rename, literal_args.c_str(),
  306. indent);
  307. // Add post-installation tweaks.
  308. this->AddTweak(os, indent, config, filesTo,
  309. &cmInstallTargetGenerator::PostReplacementTweaks);
  310. }
  311. //----------------------------------------------------------------------------
  312. std::string
  313. cmInstallTargetGenerator::GetDestination(std::string const& config) const
  314. {
  315. cmGeneratorExpression ge;
  316. return ge.Parse(this->Destination)
  317. ->Evaluate(this->Target->GetLocalGenerator(), config);
  318. }
  319. //----------------------------------------------------------------------------
  320. std::string
  321. cmInstallTargetGenerator::GetInstallFilename(const std::string& config) const
  322. {
  323. NameType nameType = this->ImportLibrary? NameImplib : NameNormal;
  324. return
  325. cmInstallTargetGenerator::GetInstallFilename(this->Target, config,
  326. nameType);
  327. }
  328. //----------------------------------------------------------------------------
  329. std::string
  330. cmInstallTargetGenerator::GetInstallFilename(cmGeneratorTarget const* target,
  331. const std::string& config,
  332. NameType nameType)
  333. {
  334. std::string fname;
  335. // Compute the name of the library.
  336. if(target->GetType() == cmState::EXECUTABLE)
  337. {
  338. std::string targetName;
  339. std::string targetNameReal;
  340. std::string targetNameImport;
  341. std::string targetNamePDB;
  342. target->GetExecutableNames(targetName, targetNameReal,
  343. targetNameImport, targetNamePDB,
  344. config);
  345. if(nameType == NameImplib)
  346. {
  347. // Use the import library name.
  348. if(!target->GetImplibGNUtoMS(targetNameImport, fname,
  349. "${CMAKE_IMPORT_LIBRARY_SUFFIX}"))
  350. {
  351. fname = targetNameImport;
  352. }
  353. }
  354. else if(nameType == NameReal)
  355. {
  356. // Use the canonical name.
  357. fname = targetNameReal;
  358. }
  359. else
  360. {
  361. // Use the canonical name.
  362. fname = targetName;
  363. }
  364. }
  365. else
  366. {
  367. std::string targetName;
  368. std::string targetNameSO;
  369. std::string targetNameReal;
  370. std::string targetNameImport;
  371. std::string targetNamePDB;
  372. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  373. targetNameImport, targetNamePDB, config);
  374. if(nameType == NameImplib)
  375. {
  376. // Use the import library name.
  377. if(!target->GetImplibGNUtoMS(targetNameImport, fname,
  378. "${CMAKE_IMPORT_LIBRARY_SUFFIX}"))
  379. {
  380. fname = targetNameImport;
  381. }
  382. }
  383. else if(nameType == NameSO)
  384. {
  385. // Use the soname.
  386. fname = targetNameSO;
  387. }
  388. else if(nameType == NameReal)
  389. {
  390. // Use the real name.
  391. fname = targetNameReal;
  392. }
  393. else
  394. {
  395. // Use the canonical name.
  396. fname = targetName;
  397. }
  398. }
  399. return fname;
  400. }
  401. void cmInstallTargetGenerator::Compute(cmLocalGenerator* lg)
  402. {
  403. this->Target = lg->GetGlobalGenerator()->GetGeneratorTarget(
  404. lg->GetMakefile()->FindTarget(this->TargetName));
  405. }
  406. //----------------------------------------------------------------------------
  407. void
  408. cmInstallTargetGenerator
  409. ::AddTweak(std::ostream& os, Indent const& indent, const std::string& config,
  410. std::string const& file, TweakMethod tweak)
  411. {
  412. std::ostringstream tw;
  413. (this->*tweak)(tw, indent.Next(), config, file);
  414. std::string tws = tw.str();
  415. if(!tws.empty())
  416. {
  417. os << indent << "if(EXISTS \"" << file << "\" AND\n"
  418. << indent << " NOT IS_SYMLINK \"" << file << "\")\n";
  419. os << tws;
  420. os << indent << "endif()\n";
  421. }
  422. }
  423. //----------------------------------------------------------------------------
  424. void
  425. cmInstallTargetGenerator
  426. ::AddTweak(std::ostream& os, Indent const& indent, const std::string& config,
  427. std::vector<std::string> const& files, TweakMethod tweak)
  428. {
  429. if(files.size() == 1)
  430. {
  431. // Tweak a single file.
  432. this->AddTweak(os, indent, config, this->GetDestDirPath(files[0]), tweak);
  433. }
  434. else
  435. {
  436. // Generate a foreach loop to tweak multiple files.
  437. std::ostringstream tw;
  438. this->AddTweak(tw, indent.Next(), config, "${file}", tweak);
  439. std::string tws = tw.str();
  440. if(!tws.empty())
  441. {
  442. Indent indent2 = indent.Next().Next();
  443. os << indent << "foreach(file\n";
  444. for(std::vector<std::string>::const_iterator i = files.begin();
  445. i != files.end(); ++i)
  446. {
  447. os << indent2 << "\"" << this->GetDestDirPath(*i) << "\"\n";
  448. }
  449. os << indent2 << ")\n";
  450. os << tws;
  451. os << indent << "endforeach()\n";
  452. }
  453. }
  454. }
  455. //----------------------------------------------------------------------------
  456. std::string cmInstallTargetGenerator::GetDestDirPath(std::string const& file)
  457. {
  458. // Construct the path of the file on disk after installation on
  459. // which tweaks may be performed.
  460. std::string toDestDirPath = "$ENV{DESTDIR}";
  461. if(file[0] != '/' && file[0] != '$')
  462. {
  463. toDestDirPath += "/";
  464. }
  465. toDestDirPath += file;
  466. return toDestDirPath;
  467. }
  468. //----------------------------------------------------------------------------
  469. void cmInstallTargetGenerator::PreReplacementTweaks(std::ostream& os,
  470. Indent const& indent,
  471. const std::string& config,
  472. std::string const& file)
  473. {
  474. this->AddRPathCheckRule(os, indent, config, file);
  475. }
  476. //----------------------------------------------------------------------------
  477. void cmInstallTargetGenerator::PostReplacementTweaks(std::ostream& os,
  478. Indent const& indent,
  479. const std::string& config,
  480. std::string const& file)
  481. {
  482. this->AddInstallNamePatchRule(os, indent, config, file);
  483. this->AddChrpathPatchRule(os, indent, config, file);
  484. this->AddRanlibRule(os, indent, file);
  485. this->AddStripRule(os, indent, file);
  486. }
  487. //----------------------------------------------------------------------------
  488. void
  489. cmInstallTargetGenerator
  490. ::AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
  491. const std::string& config,
  492. std::string const& toDestDirPath)
  493. {
  494. if(this->ImportLibrary ||
  495. !(this->Target->GetType() == cmState::SHARED_LIBRARY ||
  496. this->Target->GetType() == cmState::MODULE_LIBRARY ||
  497. this->Target->GetType() == cmState::EXECUTABLE))
  498. {
  499. return;
  500. }
  501. // Fix the install_name settings in installed binaries.
  502. std::string installNameTool = this->Target->Target->GetMakefile()
  503. ->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  504. if(installNameTool.empty())
  505. {
  506. return;
  507. }
  508. // Build a map of build-tree install_name to install-tree install_name for
  509. // shared libraries linked to this target.
  510. std::map<std::string, std::string> install_name_remap;
  511. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config))
  512. {
  513. std::set<cmGeneratorTarget const*> const& sharedLibs
  514. = cli->GetSharedLibrariesLinked();
  515. for(std::set<cmGeneratorTarget const*>::const_iterator j
  516. = sharedLibs.begin(); j != sharedLibs.end(); ++j)
  517. {
  518. cmGeneratorTarget const* tgt = *j;
  519. // The install_name of an imported target does not change.
  520. if(tgt->IsImported())
  521. {
  522. continue;
  523. }
  524. // If the build tree and install tree use different path
  525. // components of the install_name field then we need to create a
  526. // mapping to be applied after installation.
  527. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  528. std::string for_install = tgt->GetInstallNameDirForInstallTree();
  529. if(for_build != for_install)
  530. {
  531. // The directory portions differ. Append the filename to
  532. // create the mapping.
  533. std::string fname =
  534. this->GetInstallFilename(tgt, config, NameSO);
  535. // Map from the build-tree install_name.
  536. for_build += fname;
  537. // Map to the install-tree install_name.
  538. for_install += fname;
  539. // Store the mapping entry.
  540. install_name_remap[for_build] = for_install;
  541. }
  542. }
  543. }
  544. // Edit the install_name of the target itself if necessary.
  545. std::string new_id;
  546. if(this->Target->GetType() == cmState::SHARED_LIBRARY)
  547. {
  548. std::string for_build =
  549. this->Target->GetInstallNameDirForBuildTree(config);
  550. std::string for_install =
  551. this->Target->GetInstallNameDirForInstallTree();
  552. if(this->Target->IsFrameworkOnApple() && for_install.empty())
  553. {
  554. // Frameworks seem to have an id corresponding to their own full
  555. // path.
  556. // ...
  557. // for_install = fullDestPath_without_DESTDIR_or_name;
  558. }
  559. // If the install name will change on installation set the new id
  560. // on the installed file.
  561. if(for_build != for_install)
  562. {
  563. // Prepare to refer to the install-tree install_name.
  564. new_id = for_install;
  565. new_id += this->GetInstallFilename(this->Target, config, NameSO);
  566. }
  567. }
  568. // Write a rule to run install_name_tool to set the install-tree
  569. // install_name value and references.
  570. if(!new_id.empty() || !install_name_remap.empty())
  571. {
  572. os << indent << "execute_process(COMMAND \"" << installNameTool;
  573. os << "\"";
  574. if(!new_id.empty())
  575. {
  576. os << "\n" << indent << " -id \"" << new_id << "\"";
  577. }
  578. for(std::map<std::string, std::string>::const_iterator
  579. i = install_name_remap.begin();
  580. i != install_name_remap.end(); ++i)
  581. {
  582. os << "\n" << indent << " -change \""
  583. << i->first << "\" \"" << i->second << "\"";
  584. }
  585. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  586. }
  587. }
  588. //----------------------------------------------------------------------------
  589. void
  590. cmInstallTargetGenerator
  591. ::AddRPathCheckRule(std::ostream& os, Indent const& indent,
  592. const std::string& config,
  593. std::string const& toDestDirPath)
  594. {
  595. // Skip the chrpath if the target does not need it.
  596. if(this->ImportLibrary || !this->Target->IsChrpathUsed(config))
  597. {
  598. return;
  599. }
  600. // Skip if on Apple
  601. if(this->Target->Target->GetMakefile()
  602. ->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  603. {
  604. return;
  605. }
  606. // Get the link information for this target.
  607. // It can provide the RPATH.
  608. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  609. if(!cli)
  610. {
  611. return;
  612. }
  613. // Get the install RPATH from the link information.
  614. std::string newRpath = cli->GetChrpathString();
  615. // Write a rule to remove the installed file if its rpath is not the
  616. // new rpath. This is needed for existing build/install trees when
  617. // the installed rpath changes but the file is not rebuilt.
  618. os << indent << "file(RPATH_CHECK\n"
  619. << indent << " FILE \"" << toDestDirPath << "\"\n"
  620. << indent << " RPATH \"" << newRpath << "\")\n";
  621. }
  622. //----------------------------------------------------------------------------
  623. void
  624. cmInstallTargetGenerator
  625. ::AddChrpathPatchRule(std::ostream& os, Indent const& indent,
  626. const std::string& config,
  627. std::string const& toDestDirPath)
  628. {
  629. // Skip the chrpath if the target does not need it.
  630. if(this->ImportLibrary || !this->Target->IsChrpathUsed(config))
  631. {
  632. return;
  633. }
  634. // Get the link information for this target.
  635. // It can provide the RPATH.
  636. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  637. if(!cli)
  638. {
  639. return;
  640. }
  641. cmMakefile* mf = this->Target->Target->GetMakefile();
  642. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  643. {
  644. // If using install_name_tool, set up the rules to modify the rpaths.
  645. std::string installNameTool =
  646. mf->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  647. std::vector<std::string> oldRuntimeDirs, newRuntimeDirs;
  648. cli->GetRPath(oldRuntimeDirs, false);
  649. cli->GetRPath(newRuntimeDirs, true);
  650. std::string darwin_major_version_s =
  651. mf->GetSafeDefinition("DARWIN_MAJOR_VERSION");
  652. std::stringstream ss(darwin_major_version_s);
  653. int darwin_major_version;
  654. ss >> darwin_major_version;
  655. if(!ss.fail() && darwin_major_version <= 9 &&
  656. (!oldRuntimeDirs.empty() || !newRuntimeDirs.empty())
  657. )
  658. {
  659. std::ostringstream msg;
  660. msg << "WARNING: Target \"" << this->Target->GetName()
  661. << "\" has runtime paths which cannot be changed during install. "
  662. << "To change runtime paths, OS X version 10.6 or newer is required. "
  663. << "Therefore, runtime paths will not be changed when installing. "
  664. << "CMAKE_BUILD_WITH_INSTALL_RPATH may be used to work around"
  665. " this limitation.";
  666. mf->IssueMessage(cmake::WARNING, msg.str());
  667. }
  668. else
  669. {
  670. // Note: These paths are kept unique to avoid
  671. // install_name_tool corruption.
  672. std::set<std::string> runpaths;
  673. for(std::vector<std::string>::const_iterator i = oldRuntimeDirs.begin();
  674. i != oldRuntimeDirs.end(); ++i)
  675. {
  676. std::string runpath =
  677. mf->GetGlobalGenerator()->ExpandCFGIntDir(*i, config);
  678. if(runpaths.find(runpath) == runpaths.end())
  679. {
  680. runpaths.insert(runpath);
  681. os << indent << "execute_process(COMMAND " << installNameTool <<"\n";
  682. os << indent << " -delete_rpath \"" << runpath << "\"\n";
  683. os << indent << " \"" << toDestDirPath << "\")\n";
  684. }
  685. }
  686. runpaths.clear();
  687. for(std::vector<std::string>::const_iterator i = newRuntimeDirs.begin();
  688. i != newRuntimeDirs.end(); ++i)
  689. {
  690. std::string runpath =
  691. mf->GetGlobalGenerator()->ExpandCFGIntDir(*i, config);
  692. if(runpaths.find(runpath) == runpaths.end())
  693. {
  694. os << indent << "execute_process(COMMAND " << installNameTool <<"\n";
  695. os << indent << " -add_rpath \"" << runpath << "\"\n";
  696. os << indent << " \"" << toDestDirPath << "\")\n";
  697. }
  698. }
  699. }
  700. }
  701. else
  702. {
  703. // Construct the original rpath string to be replaced.
  704. std::string oldRpath = cli->GetRPathString(false);
  705. // Get the install RPATH from the link information.
  706. std::string newRpath = cli->GetChrpathString();
  707. // Skip the rule if the paths are identical
  708. if(oldRpath == newRpath)
  709. {
  710. return;
  711. }
  712. // Write a rule to run chrpath to set the install-tree RPATH
  713. if(newRpath.empty())
  714. {
  715. os << indent << "file(RPATH_REMOVE\n"
  716. << indent << " FILE \"" << toDestDirPath << "\")\n";
  717. }
  718. else
  719. {
  720. os << indent << "file(RPATH_CHANGE\n"
  721. << indent << " FILE \"" << toDestDirPath << "\"\n"
  722. << indent << " OLD_RPATH \"" << oldRpath << "\"\n"
  723. << indent << " NEW_RPATH \"" << newRpath << "\")\n";
  724. }
  725. }
  726. }
  727. //----------------------------------------------------------------------------
  728. void
  729. cmInstallTargetGenerator::AddStripRule(std::ostream& os,
  730. Indent const& indent,
  731. const std::string& toDestDirPath)
  732. {
  733. // don't strip static and import libraries, because it removes the only
  734. // symbol table they have so you can't link to them anymore
  735. if(this->Target->GetType()==cmState::STATIC_LIBRARY || this->ImportLibrary)
  736. {
  737. return;
  738. }
  739. // Don't handle OSX Bundles.
  740. if(this->Target->Target->GetMakefile()->IsOn("APPLE") &&
  741. this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  742. {
  743. return;
  744. }
  745. if(! this->Target->Target->GetMakefile()->IsSet("CMAKE_STRIP"))
  746. {
  747. return;
  748. }
  749. os << indent << "if(CMAKE_INSTALL_DO_STRIP)\n";
  750. os << indent << " execute_process(COMMAND \""
  751. << this->Target->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
  752. << "\" \"" << toDestDirPath << "\")\n";
  753. os << indent << "endif()\n";
  754. }
  755. //----------------------------------------------------------------------------
  756. void
  757. cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
  758. Indent const& indent,
  759. const std::string& toDestDirPath)
  760. {
  761. // Static libraries need ranlib on this platform.
  762. if(this->Target->GetType() != cmState::STATIC_LIBRARY)
  763. {
  764. return;
  765. }
  766. // Perform post-installation processing on the file depending
  767. // on its type.
  768. if(!this->Target->Target->GetMakefile()->IsOn("APPLE"))
  769. {
  770. return;
  771. }
  772. std::string ranlib =
  773. this->Target->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  774. if(ranlib.empty())
  775. {
  776. return;
  777. }
  778. os << indent << "execute_process(COMMAND \""
  779. << ranlib << "\" \"" << toDestDirPath << "\")\n";
  780. }