cmInstallTargetGenerator.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. cmInstallTargetGenerator::cmInstallTargetGenerator(
  26. std::string targetName, std::string const& dest, bool implib,
  27. std::string file_permissions, std::vector<std::string> const& configurations,
  28. std::string const& 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(std::move(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. cmStrCat(this->Target->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  83. "/CMakeFiles/CMakeRelink.dir/");
  84. } else {
  85. cmStateEnums::ArtifactType artifact = this->ImportLibrary
  86. ? cmStateEnums::ImportLibraryArtifact
  87. : cmStateEnums::RuntimeBinaryArtifact;
  88. fromDirConfig =
  89. cmStrCat(this->Target->GetDirectory(config, artifact), '/');
  90. }
  91. std::string toDir = cmStrCat(
  92. this->ConvertToAbsoluteDestination(this->GetDestination(config)), '/');
  93. // Compute the list of files to install for this target.
  94. std::vector<std::string> filesFrom;
  95. std::vector<std::string> filesTo;
  96. std::string literal_args;
  97. if (targetType == cmStateEnums::EXECUTABLE) {
  98. // There is a bug in cmInstallCommand if this fails.
  99. assert(this->NamelinkMode == NamelinkModeNone);
  100. cmGeneratorTarget::Names targetNames =
  101. this->Target->GetExecutableNames(config);
  102. if (this->ImportLibrary) {
  103. std::string from1 = fromDirConfig + targetNames.ImportLibrary;
  104. std::string to1 = toDir + targetNames.ImportLibrary;
  105. filesFrom.push_back(std::move(from1));
  106. filesTo.push_back(std::move(to1));
  107. std::string targetNameImportLib;
  108. if (this->Target->GetImplibGNUtoMS(config, targetNames.ImportLibrary,
  109. targetNameImportLib)) {
  110. filesFrom.push_back(fromDirConfig + targetNameImportLib);
  111. filesTo.push_back(toDir + targetNameImportLib);
  112. }
  113. // An import library looks like a static library.
  114. type = cmInstallType_STATIC_LIBRARY;
  115. } else {
  116. std::string from1 = fromDirConfig + targetNames.Output;
  117. std::string to1 = toDir + targetNames.Output;
  118. // Handle OSX Bundles.
  119. if (this->Target->IsAppBundleOnApple()) {
  120. cmMakefile const* mf = this->Target->Target->GetMakefile();
  121. // Get App Bundle Extension
  122. std::string ext;
  123. if (cmProp p = this->Target->GetProperty("BUNDLE_EXTENSION")) {
  124. ext = *p;
  125. } else {
  126. ext = "app";
  127. }
  128. // Install the whole app bundle directory.
  129. type = cmInstallType_DIRECTORY;
  130. literal_args += " USE_SOURCE_PERMISSIONS";
  131. from1 += ".";
  132. from1 += ext;
  133. // Tweaks apply to the binary inside the bundle.
  134. to1 += ".";
  135. to1 += ext;
  136. to1 += "/";
  137. if (!mf->PlatformIsAppleEmbedded()) {
  138. to1 += "Contents/MacOS/";
  139. }
  140. to1 += targetNames.Output;
  141. } else {
  142. // Tweaks apply to the real file, so list it first.
  143. if (targetNames.Real != targetNames.Output) {
  144. std::string from2 = fromDirConfig + targetNames.Real;
  145. std::string to2 = toDir += targetNames.Real;
  146. filesFrom.push_back(std::move(from2));
  147. filesTo.push_back(std::move(to2));
  148. }
  149. }
  150. filesFrom.push_back(std::move(from1));
  151. filesTo.push_back(std::move(to1));
  152. }
  153. } else {
  154. cmGeneratorTarget::Names targetNames =
  155. this->Target->GetLibraryNames(config);
  156. if (this->ImportLibrary) {
  157. // There is a bug in cmInstallCommand if this fails.
  158. assert(this->NamelinkMode == NamelinkModeNone);
  159. std::string from1 = fromDirConfig + targetNames.ImportLibrary;
  160. std::string to1 = toDir + targetNames.ImportLibrary;
  161. filesFrom.push_back(std::move(from1));
  162. filesTo.push_back(std::move(to1));
  163. std::string targetNameImportLib;
  164. if (this->Target->GetImplibGNUtoMS(config, targetNames.ImportLibrary,
  165. targetNameImportLib)) {
  166. filesFrom.push_back(fromDirConfig + targetNameImportLib);
  167. filesTo.push_back(toDir + targetNameImportLib);
  168. }
  169. // An import library looks like a static library.
  170. type = cmInstallType_STATIC_LIBRARY;
  171. } else if (this->Target->IsFrameworkOnApple()) {
  172. // FIXME: In principle we should be able to
  173. // assert(this->NamelinkMode == NamelinkModeNone);
  174. // but since the current install() command implementation checks
  175. // the FRAMEWORK property immediately instead of delaying until
  176. // generate time, it is possible for project code to set the
  177. // property after calling install(). In such a case, the install()
  178. // command will use the LIBRARY code path and create two install
  179. // generators, one for the namelink component (NamelinkModeOnly)
  180. // and one for the primary artifact component (NamelinkModeSkip).
  181. // Historically this was not diagnosed and resulted in silent
  182. // installation of a framework to the LIBRARY destination.
  183. // Retain that behavior and warn about the case.
  184. switch (this->NamelinkMode) {
  185. case NamelinkModeNone:
  186. // Normal case.
  187. break;
  188. case NamelinkModeOnly:
  189. // Assume the NamelinkModeSkip instance will warn and install.
  190. return;
  191. case NamelinkModeSkip: {
  192. std::string e = "Target '" + this->Target->GetName() +
  193. "' was changed to a FRAMEWORK sometime after install(). "
  194. "This may result in the wrong install DESTINATION. "
  195. "Set the FRAMEWORK property earlier.";
  196. this->Target->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  197. MessageType::AUTHOR_WARNING, e, this->GetBacktrace());
  198. } break;
  199. }
  200. // Install the whole framework directory.
  201. type = cmInstallType_DIRECTORY;
  202. literal_args += " USE_SOURCE_PERMISSIONS";
  203. std::string from1 = fromDirConfig + targetNames.Output;
  204. from1 = cmSystemTools::GetFilenamePath(from1);
  205. // Tweaks apply to the binary inside the bundle.
  206. std::string to1 = toDir + targetNames.Real;
  207. filesFrom.push_back(std::move(from1));
  208. filesTo.push_back(std::move(to1));
  209. } else if (this->Target->IsCFBundleOnApple()) {
  210. // Install the whole app bundle directory.
  211. type = cmInstallType_DIRECTORY;
  212. literal_args += " USE_SOURCE_PERMISSIONS";
  213. std::string targetNameBase =
  214. targetNames.Output.substr(0, targetNames.Output.find('/'));
  215. std::string from1 = fromDirConfig + targetNameBase;
  216. std::string to1 = toDir + targetNames.Output;
  217. filesFrom.push_back(std::move(from1));
  218. filesTo.push_back(std::move(to1));
  219. } else {
  220. bool haveNamelink = false;
  221. // Library link name.
  222. std::string fromName = fromDirConfig + targetNames.Output;
  223. std::string toName = toDir + targetNames.Output;
  224. // Library interface name.
  225. std::string fromSOName;
  226. std::string toSOName;
  227. if (targetNames.SharedObject != targetNames.Output) {
  228. haveNamelink = true;
  229. fromSOName = fromDirConfig + targetNames.SharedObject;
  230. toSOName = toDir + targetNames.SharedObject;
  231. }
  232. // Library implementation name.
  233. std::string fromRealName;
  234. std::string toRealName;
  235. if (targetNames.Real != targetNames.Output &&
  236. targetNames.Real != targetNames.SharedObject) {
  237. haveNamelink = true;
  238. fromRealName = fromDirConfig + targetNames.Real;
  239. toRealName = toDir + targetNames.Real;
  240. }
  241. // Add the names based on the current namelink mode.
  242. if (haveNamelink) {
  243. // With a namelink we need to check the mode.
  244. if (this->NamelinkMode == NamelinkModeOnly) {
  245. // Install the namelink only.
  246. filesFrom.push_back(fromName);
  247. filesTo.push_back(toName);
  248. } else {
  249. // Install the real file if it has its own name.
  250. if (!fromRealName.empty()) {
  251. filesFrom.push_back(fromRealName);
  252. filesTo.push_back(toRealName);
  253. }
  254. // Install the soname link if it has its own name.
  255. if (!fromSOName.empty()) {
  256. filesFrom.push_back(fromSOName);
  257. filesTo.push_back(toSOName);
  258. }
  259. // Install the namelink if it is not to be skipped.
  260. if (this->NamelinkMode != NamelinkModeSkip) {
  261. filesFrom.push_back(fromName);
  262. filesTo.push_back(toName);
  263. }
  264. }
  265. } else {
  266. // Without a namelink there will be only one file. Install it
  267. // if this is not a namelink-only rule.
  268. if (this->NamelinkMode != NamelinkModeOnly) {
  269. filesFrom.push_back(fromName);
  270. filesTo.push_back(toName);
  271. }
  272. }
  273. }
  274. }
  275. // If this fails the above code is buggy.
  276. assert(filesFrom.size() == filesTo.size());
  277. // Skip this rule if no files are to be installed for the target.
  278. if (filesFrom.empty()) {
  279. return;
  280. }
  281. // Add pre-installation tweaks.
  282. this->AddTweak(os, indent, config, filesTo,
  283. &cmInstallTargetGenerator::PreReplacementTweaks);
  284. // Write code to install the target file.
  285. const char* no_dir_permissions = nullptr;
  286. const char* no_rename = nullptr;
  287. bool optional = this->Optional || this->ImportLibrary;
  288. this->AddInstallRule(os, this->GetDestination(config), type, filesFrom,
  289. optional, this->FilePermissions.c_str(),
  290. no_dir_permissions, no_rename, literal_args.c_str(),
  291. indent);
  292. // Add post-installation tweaks.
  293. this->AddTweak(os, indent, config, filesTo,
  294. &cmInstallTargetGenerator::PostReplacementTweaks);
  295. }
  296. static std::string computeInstallObjectDir(cmGeneratorTarget* gt,
  297. std::string const& config)
  298. {
  299. std::string objectDir = "objects";
  300. if (!config.empty()) {
  301. objectDir += "-";
  302. objectDir += config;
  303. }
  304. objectDir += "/";
  305. objectDir += gt->GetName();
  306. return objectDir;
  307. }
  308. void cmInstallTargetGenerator::GenerateScriptForConfigObjectLibrary(
  309. std::ostream& os, const std::string& config, Indent indent)
  310. {
  311. // Compute all the object files inside this target
  312. std::vector<std::string> objects;
  313. this->Target->GetTargetObjectNames(config, objects);
  314. std::string const dest = this->GetDestination(config) + "/" +
  315. computeInstallObjectDir(this->Target, config);
  316. std::string const obj_dir = this->Target->GetObjectDirectory(config);
  317. std::string const literal_args = " FILES_FROM_DIR \"" + obj_dir + "\"";
  318. const char* no_dir_permissions = nullptr;
  319. const char* no_rename = nullptr;
  320. this->AddInstallRule(os, dest, cmInstallType_FILES, objects, this->Optional,
  321. this->FilePermissions.c_str(), no_dir_permissions,
  322. no_rename, literal_args.c_str(), indent);
  323. }
  324. void cmInstallTargetGenerator::GetInstallObjectNames(
  325. std::string const& config, std::vector<std::string>& objects) const
  326. {
  327. this->Target->GetTargetObjectNames(config, objects);
  328. for (std::string& o : objects) {
  329. o = cmStrCat(computeInstallObjectDir(this->Target, config), "/", o);
  330. }
  331. }
  332. std::string cmInstallTargetGenerator::GetDestination(
  333. std::string const& config) const
  334. {
  335. return cmGeneratorExpression::Evaluate(
  336. this->Destination, 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. config, "${CMAKE_INSTALL_PREFIX}");
  502. if (for_build != for_install) {
  503. // The directory portions differ. Append the filename to
  504. // create the mapping.
  505. std::string fname = this->GetInstallFilename(tgt, config, NameSO);
  506. // Map from the build-tree install_name.
  507. for_build += fname;
  508. // Map to the install-tree install_name.
  509. for_install += fname;
  510. // Store the mapping entry.
  511. install_name_remap[for_build] = for_install;
  512. }
  513. }
  514. }
  515. // Edit the install_name of the target itself if necessary.
  516. std::string new_id;
  517. if (this->Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  518. std::string for_build =
  519. this->Target->GetInstallNameDirForBuildTree(config);
  520. std::string for_install = this->Target->GetInstallNameDirForInstallTree(
  521. config, "${CMAKE_INSTALL_PREFIX}");
  522. if (this->Target->IsFrameworkOnApple() && for_install.empty()) {
  523. // Frameworks seem to have an id corresponding to their own full
  524. // path.
  525. // ...
  526. // for_install = fullDestPath_without_DESTDIR_or_name;
  527. }
  528. // If the install name will change on installation set the new id
  529. // on the installed file.
  530. if (for_build != for_install) {
  531. // Prepare to refer to the install-tree install_name.
  532. new_id = cmStrCat(
  533. for_install, this->GetInstallFilename(this->Target, config, NameSO));
  534. }
  535. }
  536. // Write a rule to run install_name_tool to set the install-tree
  537. // install_name value and references.
  538. if (!new_id.empty() || !install_name_remap.empty()) {
  539. os << indent << "execute_process(COMMAND \"" << installNameTool;
  540. os << "\"";
  541. if (!new_id.empty()) {
  542. os << "\n" << indent << " -id \"" << new_id << "\"";
  543. }
  544. for (auto const& i : install_name_remap) {
  545. os << "\n"
  546. << indent << " -change \"" << i.first << "\" \"" << i.second << "\"";
  547. }
  548. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  549. }
  550. }
  551. void cmInstallTargetGenerator::AddRPathCheckRule(
  552. std::ostream& os, Indent indent, const std::string& config,
  553. std::string const& toDestDirPath)
  554. {
  555. // Skip the chrpath if the target does not need it.
  556. if (this->ImportLibrary || !this->Target->IsChrpathUsed(config)) {
  557. return;
  558. }
  559. // Skip if on Apple
  560. if (this->Target->Target->GetMakefile()->IsOn(
  561. "CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  562. return;
  563. }
  564. // Get the link information for this target.
  565. // It can provide the RPATH.
  566. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  567. if (!cli) {
  568. return;
  569. }
  570. // Write a rule to remove the installed file if its rpath is not the
  571. // new rpath. This is needed for existing build/install trees when
  572. // the installed rpath changes but the file is not rebuilt.
  573. os << indent << "file(RPATH_CHECK\n"
  574. << indent << " FILE \"" << toDestDirPath << "\"\n";
  575. // CMP0095: ``RPATH`` entries are properly escaped in the intermediary
  576. // CMake install script.
  577. switch (this->Target->GetPolicyStatusCMP0095()) {
  578. case cmPolicies::WARN:
  579. // No author warning needed here, we warn later in
  580. // cmInstallTargetGenerator::AddChrpathPatchRule().
  581. CM_FALLTHROUGH;
  582. case cmPolicies::OLD: {
  583. // Get the install RPATH from the link information.
  584. std::string newRpath = cli->GetChrpathString();
  585. os << indent << " RPATH \"" << newRpath << "\")\n";
  586. break;
  587. }
  588. default: {
  589. // Get the install RPATH from the link information and
  590. // escape any CMake syntax in the install RPATH.
  591. std::string escapedNewRpath =
  592. cmOutputConverter::EscapeForCMake(cli->GetChrpathString());
  593. os << indent << " RPATH " << escapedNewRpath << ")\n";
  594. break;
  595. }
  596. }
  597. }
  598. void cmInstallTargetGenerator::AddChrpathPatchRule(
  599. std::ostream& os, Indent indent, const std::string& config,
  600. std::string const& toDestDirPath)
  601. {
  602. // Skip the chrpath if the target does not need it.
  603. if (this->ImportLibrary || !this->Target->IsChrpathUsed(config)) {
  604. return;
  605. }
  606. // Get the link information for this target.
  607. // It can provide the RPATH.
  608. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  609. if (!cli) {
  610. return;
  611. }
  612. cmMakefile* mf = this->Target->Target->GetMakefile();
  613. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  614. // If using install_name_tool, set up the rules to modify the rpaths.
  615. std::string installNameTool =
  616. mf->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  617. std::vector<std::string> oldRuntimeDirs;
  618. std::vector<std::string> newRuntimeDirs;
  619. cli->GetRPath(oldRuntimeDirs, false);
  620. cli->GetRPath(newRuntimeDirs, true);
  621. std::string darwin_major_version_s =
  622. mf->GetSafeDefinition("DARWIN_MAJOR_VERSION");
  623. std::istringstream ss(darwin_major_version_s);
  624. int darwin_major_version;
  625. ss >> darwin_major_version;
  626. if (!ss.fail() && darwin_major_version <= 9 &&
  627. (!oldRuntimeDirs.empty() || !newRuntimeDirs.empty())) {
  628. std::ostringstream msg;
  629. msg
  630. << "WARNING: Target \"" << this->Target->GetName()
  631. << "\" has runtime paths which cannot be changed during install. "
  632. << "To change runtime paths, OS X version 10.6 or newer is required. "
  633. << "Therefore, runtime paths will not be changed when installing. "
  634. << "CMAKE_BUILD_WITH_INSTALL_RPATH may be used to work around"
  635. " this limitation.";
  636. mf->IssueMessage(MessageType::WARNING, msg.str());
  637. } else {
  638. // Note: These paths are kept unique to avoid
  639. // install_name_tool corruption.
  640. std::set<std::string> runpaths;
  641. for (std::string const& i : oldRuntimeDirs) {
  642. std::string runpath =
  643. mf->GetGlobalGenerator()->ExpandCFGIntDir(i, config);
  644. if (runpaths.find(runpath) == runpaths.end()) {
  645. runpaths.insert(runpath);
  646. os << indent << "execute_process(COMMAND " << installNameTool
  647. << "\n";
  648. os << indent << " -delete_rpath \"" << runpath << "\"\n";
  649. os << indent << " \"" << toDestDirPath << "\")\n";
  650. }
  651. }
  652. runpaths.clear();
  653. for (std::string const& i : newRuntimeDirs) {
  654. std::string runpath =
  655. mf->GetGlobalGenerator()->ExpandCFGIntDir(i, config);
  656. if (runpaths.find(runpath) == runpaths.end()) {
  657. os << indent << "execute_process(COMMAND " << installNameTool
  658. << "\n";
  659. os << indent << " -add_rpath \"" << runpath << "\"\n";
  660. os << indent << " \"" << toDestDirPath << "\")\n";
  661. }
  662. }
  663. }
  664. } else {
  665. // Construct the original rpath string to be replaced.
  666. std::string oldRpath = cli->GetRPathString(false);
  667. // Get the install RPATH from the link information.
  668. std::string newRpath = cli->GetChrpathString();
  669. // Skip the rule if the paths are identical
  670. if (oldRpath == newRpath) {
  671. return;
  672. }
  673. // Escape any CMake syntax in the RPATHs.
  674. std::string escapedOldRpath = cmOutputConverter::EscapeForCMake(oldRpath);
  675. std::string escapedNewRpath = cmOutputConverter::EscapeForCMake(newRpath);
  676. // Write a rule to run chrpath to set the install-tree RPATH
  677. os << indent << "file(RPATH_CHANGE\n"
  678. << indent << " FILE \"" << toDestDirPath << "\"\n"
  679. << indent << " OLD_RPATH " << escapedOldRpath << "\n";
  680. // CMP0095: ``RPATH`` entries are properly escaped in the intermediary
  681. // CMake install script.
  682. switch (this->Target->GetPolicyStatusCMP0095()) {
  683. case cmPolicies::WARN:
  684. this->IssueCMP0095Warning(newRpath);
  685. CM_FALLTHROUGH;
  686. case cmPolicies::OLD:
  687. os << indent << " NEW_RPATH \"" << newRpath << "\"";
  688. break;
  689. default:
  690. os << indent << " NEW_RPATH " << escapedNewRpath;
  691. break;
  692. }
  693. if (this->Target->GetPropertyAsBool("INSTALL_REMOVE_ENVIRONMENT_RPATH")) {
  694. os << "\n" << indent << " INSTALL_REMOVE_ENVIRONMENT_RPATH)\n";
  695. } else {
  696. os << ")\n";
  697. }
  698. }
  699. }
  700. void cmInstallTargetGenerator::AddStripRule(std::ostream& os, Indent indent,
  701. const std::string& toDestDirPath)
  702. {
  703. // don't strip static and import libraries, because it removes the only
  704. // symbol table they have so you can't link to them anymore
  705. if (this->Target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  706. this->ImportLibrary) {
  707. return;
  708. }
  709. // Don't handle OSX Bundles.
  710. if (this->Target->Target->GetMakefile()->IsOn("APPLE") &&
  711. this->Target->GetPropertyAsBool("MACOSX_BUNDLE")) {
  712. return;
  713. }
  714. if (!this->Target->Target->GetMakefile()->IsSet("CMAKE_STRIP")) {
  715. return;
  716. }
  717. std::string stripArgs;
  718. // macOS 'strip' is picky, executables need '-u -r' and dylibs need '-x'.
  719. if (this->Target->Target->GetMakefile()->IsOn("APPLE")) {
  720. if (this->Target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  721. this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  722. stripArgs = "-x ";
  723. } else if (this->Target->GetType() == cmStateEnums::EXECUTABLE) {
  724. stripArgs = "-u -r ";
  725. }
  726. }
  727. os << indent << "if(CMAKE_INSTALL_DO_STRIP)\n";
  728. os << indent << " execute_process(COMMAND \""
  729. << this->Target->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
  730. << "\" " << stripArgs << "\"" << toDestDirPath << "\")\n";
  731. os << indent << "endif()\n";
  732. }
  733. void cmInstallTargetGenerator::AddRanlibRule(std::ostream& os, Indent indent,
  734. const std::string& toDestDirPath)
  735. {
  736. // Static libraries need ranlib on this platform.
  737. if (this->Target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  738. return;
  739. }
  740. // Perform post-installation processing on the file depending
  741. // on its type.
  742. if (!this->Target->Target->GetMakefile()->IsOn("APPLE")) {
  743. return;
  744. }
  745. const std::string& ranlib =
  746. this->Target->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  747. if (ranlib.empty()) {
  748. return;
  749. }
  750. os << indent << "execute_process(COMMAND \"" << ranlib << "\" \""
  751. << toDestDirPath << "\")\n";
  752. }
  753. void cmInstallTargetGenerator::AddUniversalInstallRule(
  754. std::ostream& os, Indent indent, const std::string& toDestDirPath)
  755. {
  756. cmMakefile const* mf = this->Target->Target->GetMakefile();
  757. if (!mf->PlatformIsAppleEmbedded() || !mf->IsOn("XCODE")) {
  758. return;
  759. }
  760. const char* xcodeVersion = mf->GetDefinition("XCODE_VERSION");
  761. if (!xcodeVersion ||
  762. cmSystemTools::VersionCompareGreater("6", xcodeVersion)) {
  763. return;
  764. }
  765. switch (this->Target->GetType()) {
  766. case cmStateEnums::EXECUTABLE:
  767. case cmStateEnums::STATIC_LIBRARY:
  768. case cmStateEnums::SHARED_LIBRARY:
  769. case cmStateEnums::MODULE_LIBRARY:
  770. break;
  771. default:
  772. return;
  773. }
  774. if (!this->Target->Target->GetPropertyAsBool("IOS_INSTALL_COMBINED")) {
  775. return;
  776. }
  777. os << indent << "include(CMakeIOSInstallCombined)\n";
  778. os << indent << "ios_install_combined("
  779. << "\"" << this->Target->Target->GetName() << "\" "
  780. << "\"" << toDestDirPath << "\")\n";
  781. }
  782. void cmInstallTargetGenerator::IssueCMP0095Warning(
  783. const std::string& unescapedRpath)
  784. {
  785. // Reduce warning noise to cases where used RPATHs may actually be affected
  786. // by CMP0095. This filter is meant to skip warnings in cases when
  787. // non-curly-braces syntax (e.g. $ORIGIN) or no keyword is used which has
  788. // worked already before CMP0095. We intend to issue a warning in all cases
  789. // with curly-braces syntax, even if the workaround of double-escaping is in
  790. // place, since we deprecate the need for it with CMP0095.
  791. const bool potentially_affected(unescapedRpath.find("${") !=
  792. std::string::npos);
  793. if (potentially_affected) {
  794. std::ostringstream w;
  795. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0095) << "\n";
  796. w << "RPATH entries for target '" << this->Target->GetName() << "' "
  797. << "will not be escaped in the intermediary "
  798. << "cmake_install.cmake script.";
  799. this->Target->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  800. MessageType::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  801. }
  802. }