cmInstallTargetGenerator.cxx 29 KB

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