cmInstallTargetGenerator.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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->NamelinkMode = NamelinkModeNone;
  30. this->Target->SetHaveInstallRule(true);
  31. }
  32. //----------------------------------------------------------------------------
  33. cmInstallTargetGenerator
  34. ::~cmInstallTargetGenerator()
  35. {
  36. }
  37. //----------------------------------------------------------------------------
  38. void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
  39. {
  40. // Warn if installing an exclude-from-all target.
  41. if(this->Target->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  42. {
  43. cmOStringStream 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. // Track indentation.
  51. Indent indent;
  52. // Begin this block of installation.
  53. std::string component_test =
  54. this->CreateComponentTest(this->Component.c_str());
  55. os << indent << "IF(" << component_test << ")\n";
  56. // Compute the build tree directory from which to copy the target.
  57. std::string fromDir;
  58. if(this->Target->NeedRelinkBeforeInstall())
  59. {
  60. fromDir = this->Target->GetMakefile()->GetStartOutputDirectory();
  61. fromDir += cmake::GetCMakeFilesDirectory();
  62. fromDir += "/CMakeRelink.dir/";
  63. }
  64. else
  65. {
  66. fromDir = this->Target->GetDirectory(0, this->ImportLibrary);
  67. fromDir += "/";
  68. }
  69. // Generate a portion of the script for each configuration.
  70. if(this->ConfigurationTypes->empty())
  71. {
  72. this->GenerateScriptForConfig(os, fromDir.c_str(),
  73. this->ConfigurationName,
  74. indent.Next());
  75. }
  76. else
  77. {
  78. for(std::vector<std::string>::const_iterator i =
  79. this->ConfigurationTypes->begin();
  80. i != this->ConfigurationTypes->end(); ++i)
  81. {
  82. this->GenerateScriptForConfig(os, fromDir.c_str(), i->c_str(),
  83. indent.Next());
  84. }
  85. }
  86. // End this block of installation.
  87. os << indent << "ENDIF(" << component_test << ")\n\n";
  88. }
  89. //----------------------------------------------------------------------------
  90. void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
  91. const char* fromDir,
  92. const char* config,
  93. Indent const& indent)
  94. {
  95. // Compute the per-configuration directory containing the files.
  96. std::string fromDirConfig = fromDir;
  97. this->Target->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()
  98. ->AppendDirectoryForConfig("", config, "/", fromDirConfig);
  99. if(config && *config)
  100. {
  101. // Skip this configuration for config-specific installation that
  102. // does not match it.
  103. if(!this->InstallsForConfig(config))
  104. {
  105. return;
  106. }
  107. // Generate a per-configuration block.
  108. std::string config_test = this->CreateConfigTest(config);
  109. os << indent << "IF(" << config_test << ")\n";
  110. this->GenerateScriptForConfigDir(os, fromDirConfig.c_str(), config,
  111. indent.Next());
  112. os << indent << "ENDIF(" << config_test << ")\n";
  113. }
  114. else
  115. {
  116. this->GenerateScriptForConfigDir(os, fromDirConfig.c_str(), config,
  117. indent);
  118. }
  119. }
  120. //----------------------------------------------------------------------------
  121. void
  122. cmInstallTargetGenerator
  123. ::GenerateScriptForConfigDir(std::ostream& os,
  124. const char* fromDirConfig,
  125. const char* config,
  126. Indent const& indent)
  127. {
  128. // Compute the full path to the main installed file for this target.
  129. std::string toInstallPath = this->GetInstallDestination();
  130. toInstallPath += "/";
  131. toInstallPath += this->GetInstallFilename(this->Target, config,
  132. this->ImportLibrary, false);
  133. // Track whether post-install operations should be added to the
  134. // script.
  135. bool tweakInstalledFile = true;
  136. // Compute the list of files to install for this target.
  137. std::vector<std::string> files;
  138. std::string literal_args;
  139. cmTarget::TargetType type = this->Target->GetType();
  140. if(type == cmTarget::EXECUTABLE)
  141. {
  142. // There is a bug in cmInstallCommand if this fails.
  143. assert(this->NamelinkMode == NamelinkModeNone);
  144. std::string targetName;
  145. std::string targetNameReal;
  146. std::string targetNameImport;
  147. std::string targetNamePDB;
  148. this->Target->GetExecutableNames(targetName, targetNameReal,
  149. targetNameImport, targetNamePDB,
  150. config);
  151. if(this->ImportLibrary)
  152. {
  153. std::string from1 = fromDirConfig;
  154. from1 += targetNameImport;
  155. files.push_back(from1);
  156. // An import library looks like a static library.
  157. type = cmTarget::STATIC_LIBRARY;
  158. }
  159. else
  160. {
  161. std::string from1 = fromDirConfig;
  162. from1 += targetName;
  163. // Handle OSX Bundles.
  164. if(this->Target->IsAppBundleOnApple())
  165. {
  166. // Compute the source locations of the bundle executable and
  167. // Info.plist file.
  168. from1 += ".app";
  169. files.push_back(from1);
  170. type = cmTarget::INSTALL_DIRECTORY;
  171. // Need to apply install_name_tool and stripping to binary
  172. // inside bundle.
  173. toInstallPath += ".app/Contents/MacOS/";
  174. toInstallPath += this->GetInstallFilename(this->Target, config,
  175. this->ImportLibrary, false);
  176. literal_args += " USE_SOURCE_PERMISSIONS";
  177. }
  178. else
  179. {
  180. files.push_back(from1);
  181. if(targetNameReal != targetName)
  182. {
  183. std::string from2 = fromDirConfig;
  184. from2 += targetNameReal;
  185. files.push_back(from2);
  186. }
  187. }
  188. }
  189. }
  190. else
  191. {
  192. std::string targetName;
  193. std::string targetNameSO;
  194. std::string targetNameReal;
  195. std::string targetNameImport;
  196. std::string targetNamePDB;
  197. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  198. targetNameImport, targetNamePDB,
  199. config);
  200. if(this->ImportLibrary)
  201. {
  202. // There is a bug in cmInstallCommand if this fails.
  203. assert(this->NamelinkMode == NamelinkModeNone);
  204. std::string from1 = fromDirConfig;
  205. from1 += targetNameImport;
  206. files.push_back(from1);
  207. // An import library looks like a static library.
  208. type = cmTarget::STATIC_LIBRARY;
  209. }
  210. else if(this->Target->IsFrameworkOnApple())
  211. {
  212. // There is a bug in cmInstallCommand if this fails.
  213. assert(this->NamelinkMode == NamelinkModeNone);
  214. // Compute the build tree location of the framework directory
  215. std::string from1 = fromDirConfig;
  216. // Remove trailing slashes... so that from1 ends with ".framework":
  217. //
  218. cmSystemTools::ConvertToUnixSlashes(from1);
  219. files.push_back(from1);
  220. type = cmTarget::INSTALL_DIRECTORY;
  221. // Need to apply install_name_tool and stripping to binary
  222. // inside framework.
  223. toInstallPath += ".framework/";
  224. toInstallPath += this->GetInstallFilename(this->Target, config,
  225. this->ImportLibrary, false);
  226. literal_args += " USE_SOURCE_PERMISSIONS";
  227. }
  228. else
  229. {
  230. // Operations done at install time on the installed file should
  231. // be done on the real file and not any of the symlinks.
  232. toInstallPath = this->GetInstallDestination();
  233. toInstallPath += "/";
  234. toInstallPath += targetNameReal;
  235. // Construct the list of file names to install for this library.
  236. bool haveNamelink = false;
  237. std::string fromName;
  238. std::string fromSOName;
  239. std::string fromRealName;
  240. fromName = fromDirConfig;
  241. fromName += targetName;
  242. if(targetNameSO != targetName)
  243. {
  244. haveNamelink = true;
  245. fromSOName = fromDirConfig;
  246. fromSOName += targetNameSO;
  247. }
  248. if(targetNameReal != targetName &&
  249. targetNameReal != targetNameSO)
  250. {
  251. haveNamelink = true;
  252. fromRealName = fromDirConfig;
  253. fromRealName += targetNameReal;
  254. }
  255. // Add the names based on the current namelink mode.
  256. if(haveNamelink)
  257. {
  258. // With a namelink we need to check the mode.
  259. if(this->NamelinkMode == NamelinkModeOnly)
  260. {
  261. // Install the namelink only.
  262. files.push_back(fromName);
  263. tweakInstalledFile = false;
  264. }
  265. else
  266. {
  267. // Install the real file if it has its own name.
  268. if(!fromRealName.empty())
  269. {
  270. files.push_back(fromRealName);
  271. }
  272. // Install the soname link if it has its own name.
  273. if(!fromSOName.empty())
  274. {
  275. files.push_back(fromSOName);
  276. }
  277. // Install the namelink if it is not to be skipped.
  278. if(this->NamelinkMode != NamelinkModeSkip)
  279. {
  280. files.push_back(fromName);
  281. }
  282. }
  283. }
  284. else
  285. {
  286. // Without a namelink there will be only one file. Install it
  287. // if this is not a namelink-only rule.
  288. if(this->NamelinkMode != NamelinkModeOnly)
  289. {
  290. files.push_back(fromName);
  291. }
  292. }
  293. }
  294. }
  295. // Skip this rule if no files are to be installed for the target.
  296. if(files.empty())
  297. {
  298. return;
  299. }
  300. // Write code to install the target file.
  301. const char* no_dir_permissions = 0;
  302. const char* no_rename = 0;
  303. const char* no_properties = 0;
  304. bool optional = this->Optional || this->ImportLibrary;
  305. this->AddInstallRule(os, type, files,
  306. optional, no_properties,
  307. this->FilePermissions.c_str(), no_dir_permissions,
  308. no_rename, literal_args.c_str(),
  309. indent);
  310. // Construct the path of the file on disk after installation on
  311. // which tweaks may be performed.
  312. std::string toDestDirPath = "$ENV{DESTDIR}";
  313. if(toInstallPath[0] != '/' && toInstallPath[0] != '$')
  314. {
  315. toDestDirPath += "/";
  316. }
  317. toDestDirPath += toInstallPath;
  318. // TODO:
  319. // - Skip IF(EXISTS) checks if nothing is done with the installed file
  320. if(tweakInstalledFile)
  321. {
  322. os << indent << "IF(EXISTS \"" << toDestDirPath << "\")\n";
  323. this->AddInstallNamePatchRule(os, indent.Next(), config, toDestDirPath);
  324. this->AddChrpathPatchRule(os, indent.Next(), config, toDestDirPath);
  325. this->AddRanlibRule(os, indent.Next(), type, toDestDirPath);
  326. this->AddStripRule(os, indent.Next(), type, toDestDirPath);
  327. os << indent << "ENDIF(EXISTS \"" << toDestDirPath << "\")\n";
  328. }
  329. }
  330. //----------------------------------------------------------------------------
  331. std::string
  332. cmInstallTargetGenerator::GetInstallFilename(const char* config) const
  333. {
  334. return
  335. cmInstallTargetGenerator::GetInstallFilename(this->Target, config,
  336. this->ImportLibrary, false);
  337. }
  338. //----------------------------------------------------------------------------
  339. std::string cmInstallTargetGenerator::GetInstallFilename(cmTarget* target,
  340. const char* config,
  341. bool implib,
  342. bool useSOName)
  343. {
  344. std::string fname;
  345. // Compute the name of the library.
  346. if(target->GetType() == cmTarget::EXECUTABLE)
  347. {
  348. std::string targetName;
  349. std::string targetNameReal;
  350. std::string targetNameImport;
  351. std::string targetNamePDB;
  352. target->GetExecutableNames(targetName, targetNameReal,
  353. targetNameImport, targetNamePDB,
  354. config);
  355. if(implib)
  356. {
  357. // Use the import library name.
  358. fname = targetNameImport;
  359. }
  360. else
  361. {
  362. // Use the canonical name.
  363. fname = targetName;
  364. }
  365. }
  366. else
  367. {
  368. std::string targetName;
  369. std::string targetNameSO;
  370. std::string targetNameReal;
  371. std::string targetNameImport;
  372. std::string targetNamePDB;
  373. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  374. targetNameImport, targetNamePDB, config);
  375. if(implib)
  376. {
  377. // Use the import library name.
  378. fname = targetNameImport;
  379. }
  380. else if(useSOName)
  381. {
  382. // Use the soname.
  383. fname = targetNameSO;
  384. }
  385. else
  386. {
  387. // Use the canonical name.
  388. fname = targetName;
  389. }
  390. }
  391. return fname;
  392. }
  393. //----------------------------------------------------------------------------
  394. void
  395. cmInstallTargetGenerator
  396. ::AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
  397. const char* config, std::string const& toDestDirPath)
  398. {
  399. if(this->ImportLibrary ||
  400. !(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  401. this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
  402. this->Target->GetType() == cmTarget::EXECUTABLE))
  403. {
  404. return;
  405. }
  406. // Fix the install_name settings in installed binaries.
  407. std::string installNameTool =
  408. this->Target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  409. if(!installNameTool.size())
  410. {
  411. return;
  412. }
  413. // Build a map of build-tree install_name to install-tree install_name for
  414. // shared libraries linked to this target.
  415. std::map<cmStdString, cmStdString> install_name_remap;
  416. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config))
  417. {
  418. std::set<cmTarget*> const& sharedLibs = cli->GetSharedLibrariesLinked();
  419. for(std::set<cmTarget*>::const_iterator j = sharedLibs.begin();
  420. j != sharedLibs.end(); ++j)
  421. {
  422. // If the build tree and install tree use different path
  423. // components of the install_name field then we need to create a
  424. // mapping to be applied after installation.
  425. cmTarget* tgt = *j;
  426. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  427. std::string for_install = tgt->GetInstallNameDirForInstallTree(config);
  428. if(for_build != for_install)
  429. {
  430. // The directory portions differ. Append the filename to
  431. // create the mapping.
  432. std::string fname =
  433. this->GetInstallFilename(tgt, config, false, true);
  434. // Map from the build-tree install_name.
  435. for_build += fname;
  436. // Map to the install-tree install_name.
  437. for_install += fname;
  438. // Store the mapping entry.
  439. install_name_remap[for_build] = for_install;
  440. }
  441. }
  442. }
  443. // Edit the install_name of the target itself if necessary.
  444. std::string new_id;
  445. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  446. {
  447. std::string for_build =
  448. this->Target->GetInstallNameDirForBuildTree(config);
  449. std::string for_install =
  450. this->Target->GetInstallNameDirForInstallTree(config);
  451. if(this->Target->IsFrameworkOnApple() && for_install.empty())
  452. {
  453. // Frameworks seem to have an id corresponding to their own full
  454. // path.
  455. // ...
  456. // for_install = fullDestPath_without_DESTDIR_or_name;
  457. }
  458. // If the install name will change on installation set the new id
  459. // on the installed file.
  460. if(for_build != for_install)
  461. {
  462. // Prepare to refer to the install-tree install_name.
  463. new_id = for_install;
  464. new_id += this->GetInstallFilename(this->Target, config,
  465. this->ImportLibrary, true);
  466. }
  467. }
  468. // Write a rule to run install_name_tool to set the install-tree
  469. // install_name value and references.
  470. if(!new_id.empty() || !install_name_remap.empty())
  471. {
  472. os << indent << "EXECUTE_PROCESS(COMMAND \"" << installNameTool;
  473. os << "\"";
  474. if(!new_id.empty())
  475. {
  476. os << "\n" << indent << " -id \"" << new_id << "\"";
  477. }
  478. for(std::map<cmStdString, cmStdString>::const_iterator
  479. i = install_name_remap.begin();
  480. i != install_name_remap.end(); ++i)
  481. {
  482. os << "\n" << indent << " -change \""
  483. << i->first << "\" \"" << i->second << "\"";
  484. }
  485. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  486. }
  487. }
  488. //----------------------------------------------------------------------------
  489. void
  490. cmInstallTargetGenerator
  491. ::AddChrpathPatchRule(std::ostream& os, Indent const& indent,
  492. const char* config, std::string const& toDestDirPath)
  493. {
  494. if(this->ImportLibrary ||
  495. !(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  496. this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
  497. this->Target->GetType() == cmTarget::EXECUTABLE))
  498. {
  499. return;
  500. }
  501. if(!this->Target->IsChrpathUsed())
  502. {
  503. return;
  504. }
  505. // Get the link information for this target.
  506. // It can provide the RPATH.
  507. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  508. if(!cli)
  509. {
  510. return;
  511. }
  512. // Get the install RPATH from the link information.
  513. std::string newRpath = cli->GetChrpathString();
  514. // Fix the RPATH in installed ELF binaries using chrpath.
  515. std::string chrpathTool = cli->GetChrpathTool();
  516. // Write a rule to run chrpath to set the install-tree RPATH
  517. os << indent << "EXECUTE_PROCESS(COMMAND \"" << chrpathTool;
  518. os << "\" -r \"" << newRpath << "\" \"" << toDestDirPath << "\")\n";
  519. }
  520. //----------------------------------------------------------------------------
  521. void
  522. cmInstallTargetGenerator::AddStripRule(std::ostream& os,
  523. Indent const& indent,
  524. cmTarget::TargetType type,
  525. const std::string& toDestDirPath)
  526. {
  527. // don't strip static libraries, because it removes the only symbol table
  528. // they have so you can't link to them anymore
  529. if(type == cmTarget::STATIC_LIBRARY)
  530. {
  531. return;
  532. }
  533. // Don't handle OSX Bundles.
  534. if(this->Target->GetMakefile()->IsOn("APPLE") &&
  535. this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  536. {
  537. return;
  538. }
  539. if(! this->Target->GetMakefile()->IsSet("CMAKE_STRIP"))
  540. {
  541. return;
  542. }
  543. os << indent << "IF(CMAKE_INSTALL_DO_STRIP)\n";
  544. os << indent << " EXECUTE_PROCESS(COMMAND \""
  545. << this->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
  546. << "\" \"" << toDestDirPath << "\")\n";
  547. os << indent << "ENDIF(CMAKE_INSTALL_DO_STRIP)\n";
  548. }
  549. //----------------------------------------------------------------------------
  550. void
  551. cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
  552. Indent const& indent,
  553. cmTarget::TargetType type,
  554. const std::string& toDestDirPath)
  555. {
  556. // Static libraries need ranlib on this platform.
  557. if(type != cmTarget::STATIC_LIBRARY)
  558. {
  559. return;
  560. }
  561. // Perform post-installation processing on the file depending
  562. // on its type.
  563. if(!this->Target->GetMakefile()->IsOn("APPLE"))
  564. {
  565. return;
  566. }
  567. std::string ranlib =
  568. this->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  569. if(ranlib.empty())
  570. {
  571. return;
  572. }
  573. os << indent << "EXECUTE_PROCESS(COMMAND \""
  574. << ranlib << "\" \"" << toDestDirPath << "\")\n";
  575. }