cmInstallTargetGenerator.cxx 28 KB

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