cmInstallTargetGenerator.cxx 28 KB

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