cmInstallTargetGenerator.cxx 30 KB

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