cmInstallTargetGenerator.cxx 30 KB

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