cmInstallTargetGenerator.cxx 29 KB

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