cmInstallTargetGenerator.cxx 35 KB

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