cmInstallTargetGenerator.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmInstallTargetGenerator.h"
  11. #include "cmComputeLinkInformation.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmake.h"
  16. #include <assert.h>
  17. //----------------------------------------------------------------------------
  18. cmInstallTargetGenerator
  19. ::cmInstallTargetGenerator(cmTarget& t, const char* dest, bool implib,
  20. const char* file_permissions,
  21. std::vector<std::string> const& configurations,
  22. const char* component, bool optional):
  23. cmInstallGenerator(dest, configurations, component), Target(&t),
  24. ImportLibrary(implib), FilePermissions(file_permissions), Optional(optional)
  25. {
  26. this->ActionsPerConfig = true;
  27. this->NamelinkMode = NamelinkModeNone;
  28. this->Target->SetHaveInstallRule(true);
  29. }
  30. //----------------------------------------------------------------------------
  31. cmInstallTargetGenerator
  32. ::~cmInstallTargetGenerator()
  33. {
  34. }
  35. //----------------------------------------------------------------------------
  36. void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
  37. {
  38. // Warn if installing an exclude-from-all target.
  39. if(this->Target->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  40. {
  41. cmOStringStream msg;
  42. msg << "WARNING: Target \"" << this->Target->GetName()
  43. << "\" has EXCLUDE_FROM_ALL set and will not be built by default "
  44. << "but an install rule has been provided for it. CMake does "
  45. << "not define behavior for this case.";
  46. cmSystemTools::Message(msg.str().c_str(), "Warning");
  47. }
  48. // Perform the main install script generation.
  49. this->cmInstallGenerator::GenerateScript(os);
  50. }
  51. //----------------------------------------------------------------------------
  52. void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
  53. const char* config,
  54. Indent const& indent)
  55. {
  56. // Compute the build tree directory from which to copy the target.
  57. std::string fromDirConfig;
  58. if(this->Target->NeedRelinkBeforeInstall(config))
  59. {
  60. fromDirConfig = this->Target->GetMakefile()->GetStartOutputDirectory();
  61. fromDirConfig += cmake::GetCMakeFilesDirectory();
  62. fromDirConfig += "/CMakeRelink.dir/";
  63. }
  64. else
  65. {
  66. fromDirConfig = this->Target->GetDirectory(config, this->ImportLibrary);
  67. fromDirConfig += "/";
  68. }
  69. std::string toDir = this->GetInstallDestination();
  70. toDir += "/";
  71. // Track whether post-install operations should be added to the
  72. // script.
  73. bool tweakInstalledFile = true;
  74. // Compute the list of files to install for this target.
  75. std::vector<std::string> filesFrom;
  76. std::vector<std::string> filesTo;
  77. std::string literal_args;
  78. cmTarget::TargetType type = this->Target->GetType();
  79. if(type == cmTarget::EXECUTABLE)
  80. {
  81. // There is a bug in cmInstallCommand if this fails.
  82. assert(this->NamelinkMode == NamelinkModeNone);
  83. std::string targetName;
  84. std::string targetNameReal;
  85. std::string targetNameImport;
  86. std::string targetNamePDB;
  87. this->Target->GetExecutableNames(targetName, targetNameReal,
  88. targetNameImport, targetNamePDB,
  89. config);
  90. if(this->ImportLibrary)
  91. {
  92. std::string from1 = fromDirConfig + targetNameImport;
  93. std::string to1 = toDir + targetNameImport;
  94. filesFrom.push_back(from1);
  95. filesTo.push_back(to1);
  96. // An import library looks like a static library.
  97. type = cmTarget::STATIC_LIBRARY;
  98. }
  99. else
  100. {
  101. std::string from1 = fromDirConfig + targetName;
  102. std::string to1 = toDir + targetName;
  103. // Handle OSX Bundles.
  104. if(this->Target->IsAppBundleOnApple())
  105. {
  106. // Install the whole app bundle directory.
  107. type = cmTarget::INSTALL_DIRECTORY;
  108. literal_args += " USE_SOURCE_PERMISSIONS";
  109. from1 += ".app";
  110. // Tweaks apply to the binary inside the bundle.
  111. to1 += ".app/Contents/MacOS/";
  112. to1 += targetName;
  113. }
  114. else
  115. {
  116. // Tweaks apply to the real file, so list it first.
  117. if(targetNameReal != targetName)
  118. {
  119. std::string from2 = fromDirConfig + targetNameReal;
  120. std::string to2 = toDir += targetNameReal;
  121. filesFrom.push_back(from2);
  122. filesTo.push_back(to2);
  123. }
  124. }
  125. filesFrom.push_back(from1);
  126. filesTo.push_back(to1);
  127. }
  128. }
  129. else
  130. {
  131. std::string targetName;
  132. std::string targetNameSO;
  133. std::string targetNameReal;
  134. std::string targetNameImport;
  135. std::string targetNamePDB;
  136. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  137. targetNameImport, targetNamePDB,
  138. config);
  139. if(this->ImportLibrary)
  140. {
  141. // There is a bug in cmInstallCommand if this fails.
  142. assert(this->NamelinkMode == NamelinkModeNone);
  143. std::string from1 = fromDirConfig + targetNameImport;
  144. std::string to1 = toDir + targetNameImport;
  145. filesFrom.push_back(from1);
  146. filesTo.push_back(to1);
  147. // An import library looks like a static library.
  148. type = cmTarget::STATIC_LIBRARY;
  149. }
  150. else if(this->Target->IsFrameworkOnApple())
  151. {
  152. // There is a bug in cmInstallCommand if this fails.
  153. assert(this->NamelinkMode == NamelinkModeNone);
  154. // Install the whole framework directory.
  155. type = cmTarget::INSTALL_DIRECTORY;
  156. literal_args += " USE_SOURCE_PERMISSIONS";
  157. std::string from1 = fromDirConfig + targetName + ".framework";
  158. // Tweaks apply to the binary inside the bundle.
  159. std::string to1 = toDir + targetName;
  160. to1 += ".framework/Versions/";
  161. to1 += this->Target->GetFrameworkVersion();
  162. to1 += "/";
  163. to1 += targetName;
  164. filesFrom.push_back(from1);
  165. filesTo.push_back(to1);
  166. }
  167. else
  168. {
  169. bool haveNamelink = false;
  170. // Library link name.
  171. std::string fromName = fromDirConfig + targetName;
  172. std::string toName = toDir + targetName;
  173. // Library interface name.
  174. std::string fromSOName;
  175. std::string toSOName;
  176. if(targetNameSO != targetName)
  177. {
  178. haveNamelink = true;
  179. fromSOName = fromDirConfig + targetNameSO;
  180. toSOName = toDir + targetNameSO;
  181. }
  182. // Library implementation name.
  183. std::string fromRealName;
  184. std::string toRealName;
  185. if(targetNameReal != targetName &&
  186. targetNameReal != targetNameSO)
  187. {
  188. haveNamelink = true;
  189. fromRealName = fromDirConfig + targetNameReal;
  190. toRealName = toDir + targetNameReal;
  191. }
  192. // Add the names based on the current namelink mode.
  193. if(haveNamelink)
  194. {
  195. // With a namelink we need to check the mode.
  196. if(this->NamelinkMode == NamelinkModeOnly)
  197. {
  198. // Install the namelink only.
  199. filesFrom.push_back(fromName);
  200. filesTo.push_back(toName);
  201. tweakInstalledFile = false;
  202. }
  203. else
  204. {
  205. // Install the real file if it has its own name.
  206. if(!fromRealName.empty())
  207. {
  208. filesFrom.push_back(fromRealName);
  209. filesTo.push_back(toRealName);
  210. }
  211. // Install the soname link if it has its own name.
  212. if(!fromSOName.empty())
  213. {
  214. filesFrom.push_back(fromSOName);
  215. filesTo.push_back(toSOName);
  216. }
  217. // Install the namelink if it is not to be skipped.
  218. if(this->NamelinkMode != NamelinkModeSkip)
  219. {
  220. filesFrom.push_back(fromName);
  221. filesTo.push_back(toName);
  222. }
  223. }
  224. }
  225. else
  226. {
  227. // Without a namelink there will be only one file. Install it
  228. // if this is not a namelink-only rule.
  229. if(this->NamelinkMode != NamelinkModeOnly)
  230. {
  231. filesFrom.push_back(fromName);
  232. filesTo.push_back(toName);
  233. }
  234. }
  235. }
  236. }
  237. // If this fails the above code is buggy.
  238. assert(filesFrom.size() == filesTo.size());
  239. // Skip this rule if no files are to be installed for the target.
  240. if(filesFrom.empty())
  241. {
  242. return;
  243. }
  244. // Construct the path of the file on disk after installation on
  245. // which tweaks may be performed.
  246. std::string const& toInstallPath = filesTo[0];
  247. std::string toDestDirPath = "$ENV{DESTDIR}";
  248. if(toInstallPath[0] != '/' && toInstallPath[0] != '$')
  249. {
  250. toDestDirPath += "/";
  251. }
  252. toDestDirPath += toInstallPath;
  253. // Add pre-installation tweaks.
  254. if(tweakInstalledFile)
  255. {
  256. // Collect tweaking rules.
  257. cmOStringStream tw;
  258. this->AddRPathCheckRule(tw, indent.Next(), config, toDestDirPath);
  259. std::string tws = tw.str();
  260. // Add the rules, if any.
  261. if(!tws.empty())
  262. {
  263. os << indent << "IF(EXISTS \"" << toDestDirPath << "\")\n";
  264. os << tws;
  265. os << indent << "ENDIF(EXISTS \"" << toDestDirPath << "\")\n";
  266. }
  267. }
  268. // Write code to install the target file.
  269. const char* no_dir_permissions = 0;
  270. const char* no_rename = 0;
  271. bool optional = this->Optional || this->ImportLibrary;
  272. this->AddInstallRule(os, type, filesFrom,
  273. optional,
  274. this->FilePermissions.c_str(), no_dir_permissions,
  275. no_rename, literal_args.c_str(),
  276. indent);
  277. // Add post-installation tweaks.
  278. if(tweakInstalledFile)
  279. {
  280. // Collect tweaking rules.
  281. cmOStringStream tw;
  282. this->AddInstallNamePatchRule(tw, indent.Next(), config, toDestDirPath);
  283. this->AddChrpathPatchRule(tw, indent.Next(), config, toDestDirPath);
  284. this->AddRanlibRule(tw, indent.Next(), toDestDirPath);
  285. this->AddStripRule(tw, indent.Next(), toDestDirPath);
  286. std::string tws = tw.str();
  287. // Add the rules, if any.
  288. if(!tws.empty())
  289. {
  290. os << indent << "IF(EXISTS \"" << toDestDirPath << "\")\n";
  291. os << tws;
  292. os << indent << "ENDIF(EXISTS \"" << toDestDirPath << "\")\n";
  293. }
  294. }
  295. }
  296. //----------------------------------------------------------------------------
  297. std::string
  298. cmInstallTargetGenerator::GetInstallFilename(const char* config) const
  299. {
  300. NameType nameType = this->ImportLibrary? NameImplib : NameNormal;
  301. return
  302. cmInstallTargetGenerator::GetInstallFilename(this->Target, config,
  303. nameType);
  304. }
  305. //----------------------------------------------------------------------------
  306. std::string cmInstallTargetGenerator::GetInstallFilename(cmTarget* target,
  307. const char* config,
  308. NameType nameType)
  309. {
  310. std::string fname;
  311. // Compute the name of the library.
  312. if(target->GetType() == cmTarget::EXECUTABLE)
  313. {
  314. std::string targetName;
  315. std::string targetNameReal;
  316. std::string targetNameImport;
  317. std::string targetNamePDB;
  318. target->GetExecutableNames(targetName, targetNameReal,
  319. targetNameImport, targetNamePDB,
  320. config);
  321. if(nameType == NameImplib)
  322. {
  323. // Use the import library name.
  324. fname = targetNameImport;
  325. }
  326. else if(nameType == NameReal)
  327. {
  328. // Use the canonical name.
  329. fname = targetNameReal;
  330. }
  331. else
  332. {
  333. // Use the canonical name.
  334. fname = targetName;
  335. }
  336. }
  337. else
  338. {
  339. std::string targetName;
  340. std::string targetNameSO;
  341. std::string targetNameReal;
  342. std::string targetNameImport;
  343. std::string targetNamePDB;
  344. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  345. targetNameImport, targetNamePDB, config);
  346. if(nameType == NameImplib)
  347. {
  348. // Use the import library name.
  349. fname = targetNameImport;
  350. }
  351. else if(nameType == NameSO)
  352. {
  353. // Use the soname.
  354. fname = targetNameSO;
  355. }
  356. else if(nameType == NameReal)
  357. {
  358. // Use the real name.
  359. fname = targetNameReal;
  360. }
  361. else
  362. {
  363. // Use the canonical name.
  364. fname = targetName;
  365. }
  366. }
  367. return fname;
  368. }
  369. //----------------------------------------------------------------------------
  370. void
  371. cmInstallTargetGenerator
  372. ::AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
  373. const char* config, std::string const& toDestDirPath)
  374. {
  375. if(this->ImportLibrary ||
  376. !(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  377. this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
  378. this->Target->GetType() == cmTarget::EXECUTABLE))
  379. {
  380. return;
  381. }
  382. // Fix the install_name settings in installed binaries.
  383. std::string installNameTool =
  384. this->Target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  385. if(!installNameTool.size())
  386. {
  387. return;
  388. }
  389. // Build a map of build-tree install_name to install-tree install_name for
  390. // shared libraries linked to this target.
  391. std::map<cmStdString, cmStdString> install_name_remap;
  392. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config))
  393. {
  394. std::set<cmTarget*> const& sharedLibs = cli->GetSharedLibrariesLinked();
  395. for(std::set<cmTarget*>::const_iterator j = sharedLibs.begin();
  396. j != sharedLibs.end(); ++j)
  397. {
  398. cmTarget* tgt = *j;
  399. // The install_name of an imported target does not change.
  400. if(tgt->IsImported())
  401. {
  402. continue;
  403. }
  404. // If the build tree and install tree use different path
  405. // components of the install_name field then we need to create a
  406. // mapping to be applied after installation.
  407. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  408. std::string for_install = tgt->GetInstallNameDirForInstallTree(config);
  409. if(for_build != for_install)
  410. {
  411. // The directory portions differ. Append the filename to
  412. // create the mapping.
  413. std::string fname =
  414. this->GetInstallFilename(tgt, config, NameSO);
  415. // Map from the build-tree install_name.
  416. for_build += fname;
  417. // Map to the install-tree install_name.
  418. for_install += fname;
  419. // Store the mapping entry.
  420. install_name_remap[for_build] = for_install;
  421. }
  422. }
  423. }
  424. // Edit the install_name of the target itself if necessary.
  425. std::string new_id;
  426. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  427. {
  428. std::string for_build =
  429. this->Target->GetInstallNameDirForBuildTree(config);
  430. std::string for_install =
  431. this->Target->GetInstallNameDirForInstallTree(config);
  432. if(this->Target->IsFrameworkOnApple() && for_install.empty())
  433. {
  434. // Frameworks seem to have an id corresponding to their own full
  435. // path.
  436. // ...
  437. // for_install = fullDestPath_without_DESTDIR_or_name;
  438. }
  439. // If the install name will change on installation set the new id
  440. // on the installed file.
  441. if(for_build != for_install)
  442. {
  443. // Prepare to refer to the install-tree install_name.
  444. new_id = for_install;
  445. new_id += this->GetInstallFilename(this->Target, config, NameSO);
  446. }
  447. }
  448. // Write a rule to run install_name_tool to set the install-tree
  449. // install_name value and references.
  450. if(!new_id.empty() || !install_name_remap.empty())
  451. {
  452. os << indent << "EXECUTE_PROCESS(COMMAND \"" << installNameTool;
  453. os << "\"";
  454. if(!new_id.empty())
  455. {
  456. os << "\n" << indent << " -id \"" << new_id << "\"";
  457. }
  458. for(std::map<cmStdString, cmStdString>::const_iterator
  459. i = install_name_remap.begin();
  460. i != install_name_remap.end(); ++i)
  461. {
  462. os << "\n" << indent << " -change \""
  463. << i->first << "\" \"" << i->second << "\"";
  464. }
  465. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  466. }
  467. }
  468. //----------------------------------------------------------------------------
  469. void
  470. cmInstallTargetGenerator
  471. ::AddRPathCheckRule(std::ostream& os, Indent const& indent,
  472. const char* config, std::string const& toDestDirPath)
  473. {
  474. // Skip the chrpath if the target does not need it.
  475. if(this->ImportLibrary || !this->Target->IsChrpathUsed(config))
  476. {
  477. return;
  478. }
  479. // Get the link information for this target.
  480. // It can provide the RPATH.
  481. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  482. if(!cli)
  483. {
  484. return;
  485. }
  486. // Get the install RPATH from the link information.
  487. std::string newRpath = cli->GetChrpathString();
  488. // Write a rule to remove the installed file if its rpath is not the
  489. // new rpath. This is needed for existing build/install trees when
  490. // the installed rpath changes but the file is not rebuilt.
  491. os << indent << "FILE(RPATH_CHECK\n"
  492. << indent << " FILE \"" << toDestDirPath << "\"\n"
  493. << indent << " RPATH \"" << newRpath << "\")\n";
  494. }
  495. //----------------------------------------------------------------------------
  496. void
  497. cmInstallTargetGenerator
  498. ::AddChrpathPatchRule(std::ostream& os, Indent const& indent,
  499. const char* config, std::string const& toDestDirPath)
  500. {
  501. // Skip the chrpath if the target does not need it.
  502. if(this->ImportLibrary || !this->Target->IsChrpathUsed(config))
  503. {
  504. return;
  505. }
  506. // Get the link information for this target.
  507. // It can provide the RPATH.
  508. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  509. if(!cli)
  510. {
  511. return;
  512. }
  513. // Construct the original rpath string to be replaced.
  514. std::string oldRpath = cli->GetRPathString(false);
  515. // Get the install RPATH from the link information.
  516. std::string newRpath = cli->GetChrpathString();
  517. // Skip the rule if the paths are identical
  518. if(oldRpath == newRpath)
  519. {
  520. return;
  521. }
  522. // Write a rule to run chrpath to set the install-tree RPATH
  523. if(newRpath.empty())
  524. {
  525. os << indent << "FILE(RPATH_REMOVE\n"
  526. << indent << " FILE \"" << toDestDirPath << "\")\n";
  527. }
  528. else
  529. {
  530. os << indent << "FILE(RPATH_CHANGE\n"
  531. << indent << " FILE \"" << toDestDirPath << "\"\n"
  532. << indent << " OLD_RPATH \"" << oldRpath << "\"\n"
  533. << indent << " NEW_RPATH \"" << newRpath << "\")\n";
  534. }
  535. }
  536. //----------------------------------------------------------------------------
  537. void
  538. cmInstallTargetGenerator::AddStripRule(std::ostream& os,
  539. Indent const& indent,
  540. const std::string& toDestDirPath)
  541. {
  542. // don't strip static libraries, because it removes the only symbol table
  543. // they have so you can't link to them anymore
  544. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  545. {
  546. return;
  547. }
  548. // Don't handle OSX Bundles.
  549. if(this->Target->GetMakefile()->IsOn("APPLE") &&
  550. this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  551. {
  552. return;
  553. }
  554. if(! this->Target->GetMakefile()->IsSet("CMAKE_STRIP"))
  555. {
  556. return;
  557. }
  558. os << indent << "IF(CMAKE_INSTALL_DO_STRIP)\n";
  559. os << indent << " EXECUTE_PROCESS(COMMAND \""
  560. << this->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
  561. << "\" \"" << toDestDirPath << "\")\n";
  562. os << indent << "ENDIF(CMAKE_INSTALL_DO_STRIP)\n";
  563. }
  564. //----------------------------------------------------------------------------
  565. void
  566. cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
  567. Indent const& indent,
  568. const std::string& toDestDirPath)
  569. {
  570. // Static libraries need ranlib on this platform.
  571. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  572. {
  573. return;
  574. }
  575. // Perform post-installation processing on the file depending
  576. // on its type.
  577. if(!this->Target->GetMakefile()->IsOn("APPLE"))
  578. {
  579. return;
  580. }
  581. std::string ranlib =
  582. this->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  583. if(ranlib.empty())
  584. {
  585. return;
  586. }
  587. os << indent << "EXECUTE_PROCESS(COMMAND \""
  588. << ranlib << "\" \"" << toDestDirPath << "\")\n";
  589. }