cmInstallTargetGenerator.cxx 32 KB

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