cmInstallTargetGenerator.cxx 21 KB

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