cmInstallTargetGenerator.cxx 31 KB

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