cmInstallTargetGenerator.cxx 32 KB

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