cmInstallTargetGenerator.cxx 32 KB

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