cmInstallTargetGenerator.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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, false, 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. files.NamelinkMode = this->NamelinkMode;
  303. // With a namelink we need to check the mode.
  304. if (this->NamelinkMode == NamelinkModeOnly) {
  305. // Install the namelink only.
  306. files.From.emplace_back(fromName);
  307. files.To.emplace_back(toName);
  308. } else {
  309. // Install the real file if it has its own name.
  310. if (!fromRealName.empty()) {
  311. files.From.emplace_back(fromRealName);
  312. files.To.emplace_back(toRealName);
  313. }
  314. // Install the soname link if it has its own name.
  315. if (!fromSOName.empty()) {
  316. files.From.emplace_back(fromSOName);
  317. files.To.emplace_back(toSOName);
  318. }
  319. // Install the namelink if it is not to be skipped.
  320. if (this->NamelinkMode != NamelinkModeSkip) {
  321. files.From.emplace_back(fromName);
  322. files.To.emplace_back(toName);
  323. }
  324. }
  325. } else {
  326. // Without a namelink there will be only one file. Install it
  327. // if this is not a namelink-only rule.
  328. if (this->NamelinkMode != NamelinkModeOnly) {
  329. files.From.emplace_back(fromName);
  330. files.To.emplace_back(toName);
  331. }
  332. }
  333. }
  334. }
  335. // If this fails the above code is buggy.
  336. assert(files.From.size() == files.To.size());
  337. return files;
  338. }
  339. void cmInstallTargetGenerator::GetInstallObjectNames(
  340. std::string const& config, std::vector<std::string>& objects) const
  341. {
  342. this->Target->GetTargetObjectNames(config, objects);
  343. for (std::string& o : objects) {
  344. o = cmStrCat(computeInstallObjectDir(this->Target, config), "/", o);
  345. }
  346. }
  347. std::string cmInstallTargetGenerator::GetDestination(
  348. std::string const& config) const
  349. {
  350. return cmGeneratorExpression::Evaluate(
  351. this->Destination, this->Target->GetLocalGenerator(), config);
  352. }
  353. std::string cmInstallTargetGenerator::GetInstallFilename(
  354. const std::string& config) const
  355. {
  356. NameType nameType = this->ImportLibrary ? NameImplib : NameNormal;
  357. return cmInstallTargetGenerator::GetInstallFilename(this->Target, config,
  358. nameType);
  359. }
  360. std::string cmInstallTargetGenerator::GetInstallFilename(
  361. cmGeneratorTarget const* target, const std::string& config,
  362. NameType nameType)
  363. {
  364. std::string fname;
  365. // Compute the name of the library.
  366. if (target->GetType() == cmStateEnums::EXECUTABLE) {
  367. cmGeneratorTarget::Names targetNames = target->GetExecutableNames(config);
  368. if (nameType == NameImplib) {
  369. // Use the import library name.
  370. if (!target->GetImplibGNUtoMS(config, targetNames.ImportLibrary, fname,
  371. "${CMAKE_IMPORT_LIBRARY_SUFFIX}")) {
  372. fname = targetNames.ImportLibrary;
  373. }
  374. } else if (nameType == NameReal) {
  375. // Use the canonical name.
  376. fname = targetNames.Real;
  377. } else {
  378. // Use the canonical name.
  379. fname = targetNames.Output;
  380. }
  381. } else {
  382. cmGeneratorTarget::Names targetNames = target->GetLibraryNames(config);
  383. if (nameType == NameImplib) {
  384. // Use the import library name.
  385. if (!target->GetImplibGNUtoMS(config, targetNames.ImportLibrary, fname,
  386. "${CMAKE_IMPORT_LIBRARY_SUFFIX}")) {
  387. fname = targetNames.ImportLibrary;
  388. }
  389. } else if (nameType == NameSO) {
  390. // Use the soname.
  391. fname = targetNames.SharedObject;
  392. } else if (nameType == NameReal) {
  393. // Use the real name.
  394. fname = targetNames.Real;
  395. } else {
  396. // Use the canonical name.
  397. fname = targetNames.Output;
  398. }
  399. }
  400. return fname;
  401. }
  402. bool cmInstallTargetGenerator::Compute(cmLocalGenerator* lg)
  403. {
  404. // Lookup this target in the current directory.
  405. this->Target = lg->FindLocalNonAliasGeneratorTarget(this->TargetName);
  406. if (!this->Target) {
  407. // If no local target has been found, find it in the global scope.
  408. this->Target =
  409. lg->GetGlobalGenerator()->FindGeneratorTarget(this->TargetName);
  410. }
  411. return true;
  412. }
  413. void cmInstallTargetGenerator::AddTweak(std::ostream& os, Indent indent,
  414. const std::string& config,
  415. std::string const& file,
  416. TweakMethod tweak)
  417. {
  418. std::ostringstream tw;
  419. (this->*tweak)(tw, indent.Next(), config, file);
  420. std::string tws = tw.str();
  421. if (!tws.empty()) {
  422. os << indent << "if(EXISTS \"" << file << "\" AND\n"
  423. << indent << " NOT IS_SYMLINK \"" << file << "\")\n";
  424. os << tws;
  425. os << indent << "endif()\n";
  426. }
  427. }
  428. void cmInstallTargetGenerator::AddTweak(std::ostream& os, Indent indent,
  429. const std::string& config,
  430. std::string const& dir,
  431. std::vector<std::string> const& files,
  432. TweakMethod tweak)
  433. {
  434. if (files.size() == 1) {
  435. // Tweak a single file.
  436. this->AddTweak(os, indent, config,
  437. this->GetDestDirPath(cmStrCat(dir, files[0])), tweak);
  438. } else {
  439. // Generate a foreach loop to tweak multiple files.
  440. std::ostringstream tw;
  441. this->AddTweak(tw, indent.Next(), config, "${file}", tweak);
  442. std::string tws = tw.str();
  443. if (!tws.empty()) {
  444. Indent indent2 = indent.Next().Next();
  445. os << indent << "foreach(file\n";
  446. for (std::string const& f : files) {
  447. os << indent2 << "\"" << this->GetDestDirPath(cmStrCat(dir, f))
  448. << "\"\n";
  449. }
  450. os << indent2 << ")\n";
  451. os << tws;
  452. os << indent << "endforeach()\n";
  453. }
  454. }
  455. }
  456. std::string cmInstallTargetGenerator::GetDestDirPath(std::string const& file)
  457. {
  458. // Construct the path of the file on disk after installation on
  459. // which tweaks may be performed.
  460. std::string toDestDirPath = "$ENV{DESTDIR}";
  461. if (file[0] != '/' && file[0] != '$') {
  462. toDestDirPath += "/";
  463. }
  464. toDestDirPath += file;
  465. return toDestDirPath;
  466. }
  467. void cmInstallTargetGenerator::PreReplacementTweaks(std::ostream& os,
  468. Indent indent,
  469. const std::string& config,
  470. std::string const& file)
  471. {
  472. this->AddRPathCheckRule(os, indent, config, file);
  473. }
  474. void cmInstallTargetGenerator::PostReplacementTweaks(std::ostream& os,
  475. Indent indent,
  476. const std::string& config,
  477. std::string const& file)
  478. {
  479. this->AddInstallNamePatchRule(os, indent, config, file);
  480. this->AddChrpathPatchRule(os, indent, config, file);
  481. this->AddUniversalInstallRule(os, indent, file);
  482. this->AddRanlibRule(os, indent, file);
  483. this->AddStripRule(os, indent, file);
  484. }
  485. void cmInstallTargetGenerator::AddInstallNamePatchRule(
  486. std::ostream& os, Indent indent, const std::string& config,
  487. std::string const& toDestDirPath)
  488. {
  489. if (this->ImportLibrary ||
  490. !(this->Target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  491. this->Target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  492. this->Target->GetType() == cmStateEnums::EXECUTABLE)) {
  493. return;
  494. }
  495. // Fix the install_name settings in installed binaries.
  496. std::string installNameTool =
  497. this->Target->Target->GetMakefile()->GetSafeDefinition(
  498. "CMAKE_INSTALL_NAME_TOOL");
  499. if (installNameTool.empty()) {
  500. return;
  501. }
  502. // Build a map of build-tree install_name to install-tree install_name for
  503. // shared libraries linked to this target.
  504. std::map<std::string, std::string> install_name_remap;
  505. if (cmComputeLinkInformation* cli =
  506. this->Target->GetLinkInformation(config)) {
  507. std::set<cmGeneratorTarget const*> const& sharedLibs =
  508. cli->GetSharedLibrariesLinked();
  509. for (cmGeneratorTarget const* tgt : sharedLibs) {
  510. // The install_name of an imported target does not change.
  511. if (tgt->IsImported()) {
  512. continue;
  513. }
  514. // If the build tree and install tree use different path
  515. // components of the install_name field then we need to create a
  516. // mapping to be applied after installation.
  517. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  518. std::string for_install = tgt->GetInstallNameDirForInstallTree(
  519. config, "${CMAKE_INSTALL_PREFIX}");
  520. if (for_build != for_install) {
  521. // The directory portions differ. Append the filename to
  522. // create the mapping.
  523. std::string fname = this->GetInstallFilename(tgt, config, NameSO);
  524. // Map from the build-tree install_name.
  525. for_build += fname;
  526. // Map to the install-tree install_name.
  527. for_install += fname;
  528. // Store the mapping entry.
  529. install_name_remap[for_build] = for_install;
  530. }
  531. }
  532. }
  533. // Edit the install_name of the target itself if necessary.
  534. std::string new_id;
  535. if (this->Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  536. std::string for_build =
  537. this->Target->GetInstallNameDirForBuildTree(config);
  538. std::string for_install = this->Target->GetInstallNameDirForInstallTree(
  539. config, "${CMAKE_INSTALL_PREFIX}");
  540. if (this->Target->IsFrameworkOnApple() && for_install.empty()) {
  541. // Frameworks seem to have an id corresponding to their own full
  542. // path.
  543. // ...
  544. // for_install = fullDestPath_without_DESTDIR_or_name;
  545. }
  546. // If the install name will change on installation set the new id
  547. // on the installed file.
  548. if (for_build != for_install) {
  549. // Prepare to refer to the install-tree install_name.
  550. new_id = cmStrCat(
  551. for_install, this->GetInstallFilename(this->Target, config, NameSO));
  552. }
  553. }
  554. // Write a rule to run install_name_tool to set the install-tree
  555. // install_name value and references.
  556. if (!new_id.empty() || !install_name_remap.empty()) {
  557. os << indent << "execute_process(COMMAND \"" << installNameTool;
  558. os << "\"";
  559. if (!new_id.empty()) {
  560. os << "\n" << indent << " -id \"" << new_id << "\"";
  561. }
  562. for (auto const& i : install_name_remap) {
  563. os << "\n"
  564. << indent << " -change \"" << i.first << "\" \"" << i.second << "\"";
  565. }
  566. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  567. }
  568. }
  569. void cmInstallTargetGenerator::AddRPathCheckRule(
  570. std::ostream& os, Indent indent, const std::string& config,
  571. std::string const& toDestDirPath)
  572. {
  573. // Skip the chrpath if the target does not need it.
  574. if (this->ImportLibrary || !this->Target->IsChrpathUsed(config)) {
  575. return;
  576. }
  577. // Skip if on Apple
  578. if (this->Target->Target->GetMakefile()->IsOn(
  579. "CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  580. return;
  581. }
  582. // Get the link information for this target.
  583. // It can provide the RPATH.
  584. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  585. if (!cli) {
  586. return;
  587. }
  588. // Write a rule to remove the installed file if its rpath is not the
  589. // new rpath. This is needed for existing build/install trees when
  590. // the installed rpath changes but the file is not rebuilt.
  591. os << indent << "file(RPATH_CHECK\n"
  592. << indent << " FILE \"" << toDestDirPath << "\"\n";
  593. // CMP0095: ``RPATH`` entries are properly escaped in the intermediary
  594. // CMake install script.
  595. switch (this->Target->GetPolicyStatusCMP0095()) {
  596. case cmPolicies::WARN:
  597. // No author warning needed here, we warn later in
  598. // cmInstallTargetGenerator::AddChrpathPatchRule().
  599. CM_FALLTHROUGH;
  600. case cmPolicies::OLD: {
  601. // Get the install RPATH from the link information.
  602. std::string newRpath = cli->GetChrpathString();
  603. os << indent << " RPATH \"" << newRpath << "\")\n";
  604. break;
  605. }
  606. default: {
  607. // Get the install RPATH from the link information and
  608. // escape any CMake syntax in the install RPATH.
  609. std::string escapedNewRpath =
  610. cmOutputConverter::EscapeForCMake(cli->GetChrpathString());
  611. os << indent << " RPATH " << escapedNewRpath << ")\n";
  612. break;
  613. }
  614. }
  615. }
  616. void cmInstallTargetGenerator::AddChrpathPatchRule(
  617. std::ostream& os, Indent indent, const std::string& config,
  618. std::string const& toDestDirPath)
  619. {
  620. // Skip the chrpath if the target does not need it.
  621. if (this->ImportLibrary || !this->Target->IsChrpathUsed(config)) {
  622. return;
  623. }
  624. // Get the link information for this target.
  625. // It can provide the RPATH.
  626. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  627. if (!cli) {
  628. return;
  629. }
  630. cmMakefile* mf = this->Target->Target->GetMakefile();
  631. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  632. // If using install_name_tool, set up the rules to modify the rpaths.
  633. std::string installNameTool =
  634. mf->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  635. std::vector<std::string> oldRuntimeDirs;
  636. std::vector<std::string> newRuntimeDirs;
  637. cli->GetRPath(oldRuntimeDirs, false);
  638. cli->GetRPath(newRuntimeDirs, true);
  639. std::string darwin_major_version_s =
  640. mf->GetSafeDefinition("DARWIN_MAJOR_VERSION");
  641. std::istringstream ss(darwin_major_version_s);
  642. int darwin_major_version;
  643. ss >> darwin_major_version;
  644. if (!ss.fail() && darwin_major_version <= 9 &&
  645. (!oldRuntimeDirs.empty() || !newRuntimeDirs.empty())) {
  646. std::ostringstream msg;
  647. msg
  648. << "WARNING: Target \"" << this->Target->GetName()
  649. << "\" has runtime paths which cannot be changed during install. "
  650. << "To change runtime paths, OS X version 10.6 or newer is required. "
  651. << "Therefore, runtime paths will not be changed when installing. "
  652. << "CMAKE_BUILD_WITH_INSTALL_RPATH may be used to work around"
  653. " this limitation.";
  654. mf->IssueMessage(MessageType::WARNING, msg.str());
  655. } else {
  656. // Note: These paths are kept unique to avoid
  657. // install_name_tool corruption.
  658. std::set<std::string> runpaths;
  659. for (std::string const& i : oldRuntimeDirs) {
  660. std::string runpath =
  661. mf->GetGlobalGenerator()->ExpandCFGIntDir(i, config);
  662. if (runpaths.find(runpath) == runpaths.end()) {
  663. runpaths.insert(runpath);
  664. os << indent << "execute_process(COMMAND " << installNameTool
  665. << "\n";
  666. os << indent << " -delete_rpath \"" << runpath << "\"\n";
  667. os << indent << " \"" << toDestDirPath << "\")\n";
  668. }
  669. }
  670. runpaths.clear();
  671. for (std::string const& i : newRuntimeDirs) {
  672. std::string runpath =
  673. mf->GetGlobalGenerator()->ExpandCFGIntDir(i, config);
  674. if (runpaths.find(runpath) == runpaths.end()) {
  675. os << indent << "execute_process(COMMAND " << installNameTool
  676. << "\n";
  677. os << indent << " -add_rpath \"" << runpath << "\"\n";
  678. os << indent << " \"" << toDestDirPath << "\")\n";
  679. }
  680. }
  681. }
  682. } else {
  683. // Construct the original rpath string to be replaced.
  684. std::string oldRpath = cli->GetRPathString(false);
  685. // Get the install RPATH from the link information.
  686. std::string newRpath = cli->GetChrpathString();
  687. // Skip the rule if the paths are identical
  688. if (oldRpath == newRpath) {
  689. return;
  690. }
  691. // Escape any CMake syntax in the RPATHs.
  692. std::string escapedOldRpath = cmOutputConverter::EscapeForCMake(oldRpath);
  693. std::string escapedNewRpath = cmOutputConverter::EscapeForCMake(newRpath);
  694. // Write a rule to run chrpath to set the install-tree RPATH
  695. os << indent << "file(RPATH_CHANGE\n"
  696. << indent << " FILE \"" << toDestDirPath << "\"\n"
  697. << indent << " OLD_RPATH " << escapedOldRpath << "\n";
  698. // CMP0095: ``RPATH`` entries are properly escaped in the intermediary
  699. // CMake install script.
  700. switch (this->Target->GetPolicyStatusCMP0095()) {
  701. case cmPolicies::WARN:
  702. this->IssueCMP0095Warning(newRpath);
  703. CM_FALLTHROUGH;
  704. case cmPolicies::OLD:
  705. os << indent << " NEW_RPATH \"" << newRpath << "\"";
  706. break;
  707. default:
  708. os << indent << " NEW_RPATH " << escapedNewRpath;
  709. break;
  710. }
  711. if (this->Target->GetPropertyAsBool("INSTALL_REMOVE_ENVIRONMENT_RPATH")) {
  712. os << "\n" << indent << " INSTALL_REMOVE_ENVIRONMENT_RPATH)\n";
  713. } else {
  714. os << ")\n";
  715. }
  716. }
  717. }
  718. void cmInstallTargetGenerator::AddStripRule(std::ostream& os, Indent indent,
  719. const std::string& toDestDirPath)
  720. {
  721. // don't strip static and import libraries, because it removes the only
  722. // symbol table they have so you can't link to them anymore
  723. if (this->Target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  724. this->ImportLibrary) {
  725. return;
  726. }
  727. // Don't handle OSX Bundles.
  728. if (this->Target->Target->GetMakefile()->IsOn("APPLE") &&
  729. this->Target->GetPropertyAsBool("MACOSX_BUNDLE")) {
  730. return;
  731. }
  732. if (!this->Target->Target->GetMakefile()->IsSet("CMAKE_STRIP")) {
  733. return;
  734. }
  735. std::string stripArgs;
  736. // macOS 'strip' is picky, executables need '-u -r' and dylibs need '-x'.
  737. if (this->Target->Target->GetMakefile()->IsOn("APPLE")) {
  738. if (this->Target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  739. this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  740. stripArgs = "-x ";
  741. } else if (this->Target->GetType() == cmStateEnums::EXECUTABLE) {
  742. stripArgs = "-u -r ";
  743. }
  744. }
  745. os << indent << "if(CMAKE_INSTALL_DO_STRIP)\n";
  746. os << indent << " execute_process(COMMAND \""
  747. << this->Target->Target->GetMakefile()->GetSafeDefinition("CMAKE_STRIP")
  748. << "\" " << stripArgs << "\"" << toDestDirPath << "\")\n";
  749. os << indent << "endif()\n";
  750. }
  751. void cmInstallTargetGenerator::AddRanlibRule(std::ostream& os, Indent indent,
  752. const std::string& toDestDirPath)
  753. {
  754. // Static libraries need ranlib on this platform.
  755. if (this->Target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  756. return;
  757. }
  758. // Perform post-installation processing on the file depending
  759. // on its type.
  760. if (!this->Target->Target->GetMakefile()->IsOn("APPLE")) {
  761. return;
  762. }
  763. const std::string& ranlib =
  764. this->Target->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  765. if (ranlib.empty()) {
  766. return;
  767. }
  768. os << indent << "execute_process(COMMAND \"" << ranlib << "\" \""
  769. << toDestDirPath << "\")\n";
  770. }
  771. void cmInstallTargetGenerator::AddUniversalInstallRule(
  772. std::ostream& os, Indent indent, const std::string& toDestDirPath)
  773. {
  774. cmMakefile const* mf = this->Target->Target->GetMakefile();
  775. if (!mf->PlatformIsAppleEmbedded() || !mf->IsOn("XCODE")) {
  776. return;
  777. }
  778. cmProp xcodeVersion = mf->GetDefinition("XCODE_VERSION");
  779. if (!xcodeVersion ||
  780. cmSystemTools::VersionCompareGreater("6", *xcodeVersion)) {
  781. return;
  782. }
  783. switch (this->Target->GetType()) {
  784. case cmStateEnums::EXECUTABLE:
  785. case cmStateEnums::STATIC_LIBRARY:
  786. case cmStateEnums::SHARED_LIBRARY:
  787. case cmStateEnums::MODULE_LIBRARY:
  788. break;
  789. default:
  790. return;
  791. }
  792. if (!this->Target->Target->GetPropertyAsBool("IOS_INSTALL_COMBINED")) {
  793. return;
  794. }
  795. os << indent << "include(CMakeIOSInstallCombined)\n";
  796. os << indent << "ios_install_combined("
  797. << "\"" << this->Target->Target->GetName() << "\" "
  798. << "\"" << toDestDirPath << "\")\n";
  799. }
  800. void cmInstallTargetGenerator::IssueCMP0095Warning(
  801. const std::string& unescapedRpath)
  802. {
  803. // Reduce warning noise to cases where used RPATHs may actually be affected
  804. // by CMP0095. This filter is meant to skip warnings in cases when
  805. // non-curly-braces syntax (e.g. $ORIGIN) or no keyword is used which has
  806. // worked already before CMP0095. We intend to issue a warning in all cases
  807. // with curly-braces syntax, even if the workaround of double-escaping is in
  808. // place, since we deprecate the need for it with CMP0095.
  809. const bool potentially_affected(unescapedRpath.find("${") !=
  810. std::string::npos);
  811. if (potentially_affected) {
  812. std::ostringstream w;
  813. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0095) << "\n";
  814. w << "RPATH entries for target '" << this->Target->GetName() << "' "
  815. << "will not be escaped in the intermediary "
  816. << "cmake_install.cmake script.";
  817. this->Target->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  818. MessageType::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  819. }
  820. }