cmInstallTargetGenerator.cxx 32 KB

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