cmInstallTargetGenerator.cxx 27 KB

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