cmInstallTargetGenerator.cxx 33 KB

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