cmInstallTargetGenerator.cxx 28 KB

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