cmInstallTargetGenerator.cxx 21 KB

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