cmInstallTargetGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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(CM_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 const& 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(from1);
  122. filesTo.push_back(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->PlatformIsAppleIos()) {
  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(from2);
  161. filesTo.push_back(to2);
  162. }
  163. }
  164. filesFrom.push_back(from1);
  165. filesTo.push_back(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(from1);
  181. filesTo.push_back(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(from1);
  201. filesTo.push_back(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(from1);
  210. filesTo.push_back(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 = CM_NULLPTR;
  277. const char* no_rename = CM_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 const& 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 = CM_NULLPTR;
  310. const char* no_rename = CM_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::vector<std::string>::iterator i = objects.begin();
  320. i != objects.end(); ++i) {
  321. *i = computeInstallObjectDir(this->Target, config) + "/" + *i;
  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(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(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. this->Target = lg->FindLocalNonAliasGeneratorTarget(this->TargetName);
  394. }
  395. void cmInstallTargetGenerator::AddTweak(std::ostream& os, Indent const& indent,
  396. const std::string& config,
  397. std::string const& file,
  398. TweakMethod tweak)
  399. {
  400. std::ostringstream tw;
  401. (this->*tweak)(tw, indent.Next(), config, file);
  402. std::string tws = tw.str();
  403. if (!tws.empty()) {
  404. os << indent << "if(EXISTS \"" << file << "\" AND\n"
  405. << indent << " NOT IS_SYMLINK \"" << file << "\")\n";
  406. os << tws;
  407. os << indent << "endif()\n";
  408. }
  409. }
  410. void cmInstallTargetGenerator::AddTweak(std::ostream& os, Indent const& indent,
  411. const std::string& config,
  412. std::vector<std::string> const& files,
  413. TweakMethod tweak)
  414. {
  415. if (files.size() == 1) {
  416. // Tweak a single file.
  417. this->AddTweak(os, indent, config, this->GetDestDirPath(files[0]), tweak);
  418. } else {
  419. // Generate a foreach loop to tweak multiple files.
  420. std::ostringstream tw;
  421. this->AddTweak(tw, indent.Next(), config, "${file}", tweak);
  422. std::string tws = tw.str();
  423. if (!tws.empty()) {
  424. Indent indent2 = indent.Next().Next();
  425. os << indent << "foreach(file\n";
  426. for (std::vector<std::string>::const_iterator i = files.begin();
  427. i != files.end(); ++i) {
  428. os << indent2 << "\"" << this->GetDestDirPath(*i) << "\"\n";
  429. }
  430. os << indent2 << ")\n";
  431. os << tws;
  432. os << indent << "endforeach()\n";
  433. }
  434. }
  435. }
  436. std::string cmInstallTargetGenerator::GetDestDirPath(std::string const& file)
  437. {
  438. // Construct the path of the file on disk after installation on
  439. // which tweaks may be performed.
  440. std::string toDestDirPath = "$ENV{DESTDIR}";
  441. if (file[0] != '/' && file[0] != '$') {
  442. toDestDirPath += "/";
  443. }
  444. toDestDirPath += file;
  445. return toDestDirPath;
  446. }
  447. void cmInstallTargetGenerator::PreReplacementTweaks(std::ostream& os,
  448. Indent const& indent,
  449. const std::string& config,
  450. std::string const& file)
  451. {
  452. this->AddRPathCheckRule(os, indent, config, file);
  453. }
  454. void cmInstallTargetGenerator::PostReplacementTweaks(std::ostream& os,
  455. Indent const& indent,
  456. const std::string& config,
  457. std::string const& file)
  458. {
  459. this->AddInstallNamePatchRule(os, indent, config, file);
  460. this->AddChrpathPatchRule(os, indent, config, file);
  461. this->AddUniversalInstallRule(os, indent, file);
  462. this->AddRanlibRule(os, indent, file);
  463. this->AddStripRule(os, indent, file);
  464. }
  465. void cmInstallTargetGenerator::AddInstallNamePatchRule(
  466. std::ostream& os, Indent const& indent, const std::string& config,
  467. std::string const& toDestDirPath)
  468. {
  469. if (this->ImportLibrary ||
  470. !(this->Target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  471. this->Target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  472. this->Target->GetType() == cmStateEnums::EXECUTABLE)) {
  473. return;
  474. }
  475. // Fix the install_name settings in installed binaries.
  476. std::string installNameTool =
  477. this->Target->Target->GetMakefile()->GetSafeDefinition(
  478. "CMAKE_INSTALL_NAME_TOOL");
  479. if (installNameTool.empty()) {
  480. return;
  481. }
  482. // Build a map of build-tree install_name to install-tree install_name for
  483. // shared libraries linked to this target.
  484. std::map<std::string, std::string> install_name_remap;
  485. if (cmComputeLinkInformation* cli =
  486. this->Target->GetLinkInformation(config)) {
  487. std::set<cmGeneratorTarget const*> const& sharedLibs =
  488. cli->GetSharedLibrariesLinked();
  489. for (std::set<cmGeneratorTarget const*>::const_iterator j =
  490. sharedLibs.begin();
  491. j != sharedLibs.end(); ++j) {
  492. cmGeneratorTarget const* tgt = *j;
  493. // The install_name of an imported target does not change.
  494. if (tgt->IsImported()) {
  495. continue;
  496. }
  497. // If the build tree and install tree use different path
  498. // components of the install_name field then we need to create a
  499. // mapping to be applied after installation.
  500. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  501. std::string for_install = tgt->GetInstallNameDirForInstallTree();
  502. if (for_build != for_install) {
  503. // The directory portions differ. Append the filename to
  504. // create the mapping.
  505. std::string fname = this->GetInstallFilename(tgt, config, NameSO);
  506. // Map from the build-tree install_name.
  507. for_build += fname;
  508. // Map to the install-tree install_name.
  509. for_install += fname;
  510. // Store the mapping entry.
  511. install_name_remap[for_build] = for_install;
  512. }
  513. }
  514. }
  515. // Edit the install_name of the target itself if necessary.
  516. std::string new_id;
  517. if (this->Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  518. std::string for_build =
  519. this->Target->GetInstallNameDirForBuildTree(config);
  520. std::string for_install = this->Target->GetInstallNameDirForInstallTree();
  521. if (this->Target->IsFrameworkOnApple() && for_install.empty()) {
  522. // Frameworks seem to have an id corresponding to their own full
  523. // path.
  524. // ...
  525. // for_install = fullDestPath_without_DESTDIR_or_name;
  526. }
  527. // If the install name will change on installation set the new id
  528. // on the installed file.
  529. if (for_build != for_install) {
  530. // Prepare to refer to the install-tree install_name.
  531. new_id = for_install;
  532. new_id += this->GetInstallFilename(this->Target, config, NameSO);
  533. }
  534. }
  535. // Write a rule to run install_name_tool to set the install-tree
  536. // install_name value and references.
  537. if (!new_id.empty() || !install_name_remap.empty()) {
  538. os << indent << "execute_process(COMMAND \"" << installNameTool;
  539. os << "\"";
  540. if (!new_id.empty()) {
  541. os << "\n" << indent << " -id \"" << new_id << "\"";
  542. }
  543. for (std::map<std::string, std::string>::const_iterator i =
  544. install_name_remap.begin();
  545. i != install_name_remap.end(); ++i) {
  546. os << "\n"
  547. << indent << " -change \"" << i->first << "\" \"" << i->second
  548. << "\"";
  549. }
  550. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  551. }
  552. }
  553. void cmInstallTargetGenerator::AddRPathCheckRule(
  554. std::ostream& os, Indent const& 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 const& 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(cmake::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::vector<std::string>::const_iterator i = oldRuntimeDirs.begin();
  626. i != oldRuntimeDirs.end(); ++i) {
  627. std::string runpath =
  628. mf->GetGlobalGenerator()->ExpandCFGIntDir(*i, config);
  629. if (runpaths.find(runpath) == runpaths.end()) {
  630. runpaths.insert(runpath);
  631. os << indent << "execute_process(COMMAND " << installNameTool
  632. << "\n";
  633. os << indent << " -delete_rpath \"" << runpath << "\"\n";
  634. os << indent << " \"" << toDestDirPath << "\")\n";
  635. }
  636. }
  637. runpaths.clear();
  638. for (std::vector<std::string>::const_iterator i = newRuntimeDirs.begin();
  639. i != newRuntimeDirs.end(); ++i) {
  640. std::string runpath =
  641. mf->GetGlobalGenerator()->ExpandCFGIntDir(*i, config);
  642. if (runpaths.find(runpath) == runpaths.end()) {
  643. os << indent << "execute_process(COMMAND " << installNameTool
  644. << "\n";
  645. os << indent << " -add_rpath \"" << runpath << "\"\n";
  646. os << indent << " \"" << toDestDirPath << "\")\n";
  647. }
  648. }
  649. }
  650. } else {
  651. // Construct the original rpath string to be replaced.
  652. std::string oldRpath = cli->GetRPathString(false);
  653. // Get the install RPATH from the link information.
  654. std::string newRpath = cli->GetChrpathString();
  655. // Skip the rule if the paths are identical
  656. if (oldRpath == newRpath) {
  657. return;
  658. }
  659. // Write a rule to run chrpath to set the install-tree RPATH
  660. os << indent << "file(RPATH_CHANGE\n"
  661. << indent << " FILE \"" << toDestDirPath << "\"\n"
  662. << indent << " OLD_RPATH \"" << oldRpath << "\"\n"
  663. << indent << " NEW_RPATH \"" << newRpath << "\")\n";
  664. }
  665. }
  666. void cmInstallTargetGenerator::AddStripRule(std::ostream& os,
  667. Indent const& indent,
  668. const std::string& toDestDirPath)
  669. {
  670. // don't strip static and import libraries, because it removes the only
  671. // symbol table they have so you can't link to them anymore
  672. if (this->Target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  673. this->ImportLibrary) {
  674. return;
  675. }
  676. // Don't handle OSX Bundles.
  677. if (this->Target->Target->GetMakefile()->IsOn("APPLE") &&
  678. this->Target->GetPropertyAsBool("MACOSX_BUNDLE")) {
  679. return;
  680. }
  681. if (!this->Target->Target->GetMakefile()->IsSet("CMAKE_STRIP")) {
  682. return;
  683. }
  684. os << indent << "if(CMAKE_INSTALL_DO_STRIP)\n";
  685. os << indent << " execute_process(COMMAND \""
  686. << this->Target->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
  687. << "\" \"" << toDestDirPath << "\")\n";
  688. os << indent << "endif()\n";
  689. }
  690. void cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
  691. Indent const& indent,
  692. const std::string& toDestDirPath)
  693. {
  694. // Static libraries need ranlib on this platform.
  695. if (this->Target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  696. return;
  697. }
  698. // Perform post-installation processing on the file depending
  699. // on its type.
  700. if (!this->Target->Target->GetMakefile()->IsOn("APPLE")) {
  701. return;
  702. }
  703. std::string ranlib =
  704. this->Target->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  705. if (ranlib.empty()) {
  706. return;
  707. }
  708. os << indent << "execute_process(COMMAND \"" << ranlib << "\" \""
  709. << toDestDirPath << "\")\n";
  710. }
  711. void cmInstallTargetGenerator::AddUniversalInstallRule(
  712. std::ostream& os, Indent const& indent, const std::string& toDestDirPath)
  713. {
  714. cmMakefile const* mf = this->Target->Target->GetMakefile();
  715. if (!mf->PlatformIsAppleIos() || !mf->IsOn("XCODE")) {
  716. return;
  717. }
  718. const char* xcodeVersion = mf->GetDefinition("XCODE_VERSION");
  719. if (!xcodeVersion ||
  720. cmSystemTools::VersionCompareGreater("6", xcodeVersion)) {
  721. return;
  722. }
  723. switch (this->Target->GetType()) {
  724. case cmStateEnums::EXECUTABLE:
  725. case cmStateEnums::STATIC_LIBRARY:
  726. case cmStateEnums::SHARED_LIBRARY:
  727. case cmStateEnums::MODULE_LIBRARY:
  728. break;
  729. default:
  730. return;
  731. }
  732. if (!this->Target->Target->GetPropertyAsBool("IOS_INSTALL_COMBINED")) {
  733. return;
  734. }
  735. os << indent << "include(CMakeIOSInstallCombined)\n";
  736. os << indent << "ios_install_combined("
  737. << "\"" << this->Target->Target->GetName() << "\" "
  738. << "\"" << toDestDirPath << "\")\n";
  739. }