cmInstallTargetGenerator.cxx 21 KB

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