cmInstallTargetGenerator.cxx 27 KB

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