cmInstallTargetGenerator.cxx 29 KB

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