cmInstallTargetGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmake.h"
  18. // TODO:
  19. // - Skip IF(EXISTS) checks if nothing is done with the installed file
  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->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. // Compute the list of files to install for this target.
  133. std::vector<std::string> files;
  134. std::string literal_args;
  135. cmTarget::TargetType type = this->Target->GetType();
  136. if(type == cmTarget::EXECUTABLE)
  137. {
  138. std::string targetName;
  139. std::string targetNameReal;
  140. std::string targetNameImport;
  141. std::string targetNamePDB;
  142. this->Target->GetExecutableNames(targetName, targetNameReal,
  143. targetNameImport, targetNamePDB,
  144. config);
  145. if(this->ImportLibrary)
  146. {
  147. std::string from1 = fromDirConfig;
  148. from1 += targetNameImport;
  149. files.push_back(from1);
  150. // An import library looks like a static library.
  151. type = cmTarget::STATIC_LIBRARY;
  152. }
  153. else
  154. {
  155. std::string from1 = fromDirConfig;
  156. from1 += targetName;
  157. // Handle OSX Bundles.
  158. if(this->Target->IsAppBundleOnApple())
  159. {
  160. // Compute the source locations of the bundle executable and
  161. // Info.plist file.
  162. from1 += ".app";
  163. files.push_back(from1);
  164. type = cmTarget::INSTALL_DIRECTORY;
  165. // Need to apply install_name_tool and stripping to binary
  166. // inside bundle.
  167. toInstallPath += ".app/Contents/MacOS/";
  168. toInstallPath += this->GetInstallFilename(this->Target, config,
  169. this->ImportLibrary, false);
  170. literal_args += " USE_SOURCE_PERMISSIONS";
  171. }
  172. else
  173. {
  174. files.push_back(from1);
  175. if(targetNameReal != targetName)
  176. {
  177. std::string from2 = fromDirConfig;
  178. from2 += targetNameReal;
  179. files.push_back(from2);
  180. }
  181. }
  182. }
  183. }
  184. else
  185. {
  186. std::string targetName;
  187. std::string targetNameSO;
  188. std::string targetNameReal;
  189. std::string targetNameImport;
  190. std::string targetNamePDB;
  191. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  192. targetNameImport, targetNamePDB,
  193. config);
  194. if(this->ImportLibrary)
  195. {
  196. std::string from1 = fromDirConfig;
  197. from1 += targetNameImport;
  198. files.push_back(from1);
  199. // An import library looks like a static library.
  200. type = cmTarget::STATIC_LIBRARY;
  201. }
  202. else if(this->Target->IsFrameworkOnApple())
  203. {
  204. // Compute the build tree location of the framework directory
  205. std::string from1 = fromDirConfig;
  206. // Remove trailing slashes... so that from1 ends with ".framework":
  207. //
  208. cmSystemTools::ConvertToUnixSlashes(from1);
  209. files.push_back(from1);
  210. type = cmTarget::INSTALL_DIRECTORY;
  211. // Need to apply install_name_tool and stripping to binary
  212. // inside framework.
  213. toInstallPath += ".framework/";
  214. toInstallPath += this->GetInstallFilename(this->Target, config,
  215. this->ImportLibrary, false);
  216. literal_args += " USE_SOURCE_PERMISSIONS";
  217. }
  218. else
  219. {
  220. std::string from1 = fromDirConfig;
  221. from1 += targetName;
  222. files.push_back(from1);
  223. if(targetNameSO != targetName)
  224. {
  225. std::string from2 = fromDirConfig;
  226. from2 += targetNameSO;
  227. files.push_back(from2);
  228. }
  229. if(targetNameReal != targetName &&
  230. targetNameReal != targetNameSO)
  231. {
  232. std::string from3 = fromDirConfig;
  233. from3 += targetNameReal;
  234. files.push_back(from3);
  235. }
  236. }
  237. }
  238. // Write code to install the target file.
  239. const char* no_dir_permissions = 0;
  240. const char* no_rename = 0;
  241. const char* no_properties = 0;
  242. bool optional = this->Optional || this->ImportLibrary;
  243. this->AddInstallRule(os, type, files,
  244. optional, no_properties,
  245. this->FilePermissions.c_str(), no_dir_permissions,
  246. no_rename, literal_args.c_str(),
  247. indent);
  248. std::string toDestDirPath = "$ENV{DESTDIR}";
  249. if(toInstallPath[0] != '/')
  250. {
  251. toDestDirPath += "/";
  252. }
  253. toDestDirPath += toInstallPath;
  254. this->Target->SetInstallNameFixupPath(toInstallPath.c_str());
  255. os << indent << "IF(EXISTS \"" << toDestDirPath << "\")\n";
  256. this->AddInstallNamePatchRule(os, indent.Next(), config, toDestDirPath);
  257. this->AddChrpathPatchRule(os, indent.Next(), toDestDirPath);
  258. this->AddRanlibRule(os, indent.Next(), type, toDestDirPath);
  259. this->AddStripRule(os, indent.Next(), type, toDestDirPath);
  260. os << indent << "ENDIF(EXISTS \"" << toDestDirPath << "\")\n";
  261. }
  262. //----------------------------------------------------------------------------
  263. std::string
  264. cmInstallTargetGenerator::GetInstallFilename(const char* config) const
  265. {
  266. return
  267. cmInstallTargetGenerator::GetInstallFilename(this->Target, config,
  268. this->ImportLibrary, false);
  269. }
  270. //----------------------------------------------------------------------------
  271. std::string cmInstallTargetGenerator::GetInstallFilename(cmTarget* target,
  272. const char* config,
  273. bool implib,
  274. bool useSOName)
  275. {
  276. std::string fname;
  277. // Compute the name of the library.
  278. if(target->GetType() == cmTarget::EXECUTABLE)
  279. {
  280. std::string targetName;
  281. std::string targetNameReal;
  282. std::string targetNameImport;
  283. std::string targetNamePDB;
  284. target->GetExecutableNames(targetName, targetNameReal,
  285. targetNameImport, targetNamePDB,
  286. config);
  287. if(implib)
  288. {
  289. // Use the import library name.
  290. fname = targetNameImport;
  291. }
  292. else
  293. {
  294. // Use the canonical name.
  295. fname = targetName;
  296. }
  297. }
  298. else
  299. {
  300. std::string targetName;
  301. std::string targetNameSO;
  302. std::string targetNameReal;
  303. std::string targetNameImport;
  304. std::string targetNamePDB;
  305. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  306. targetNameImport, targetNamePDB, config);
  307. if(implib)
  308. {
  309. // Use the import library name.
  310. fname = targetNameImport;
  311. }
  312. else if(useSOName)
  313. {
  314. // Use the soname.
  315. fname = targetNameSO;
  316. }
  317. else
  318. {
  319. // Use the canonical name.
  320. fname = targetName;
  321. }
  322. }
  323. return fname;
  324. }
  325. //----------------------------------------------------------------------------
  326. void
  327. cmInstallTargetGenerator
  328. ::AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
  329. const char* config, std::string const& toDestDirPath)
  330. {
  331. if(this->ImportLibrary ||
  332. !(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  333. this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
  334. this->Target->GetType() == cmTarget::EXECUTABLE))
  335. {
  336. return;
  337. }
  338. // Fix the install_name settings in installed binaries.
  339. std::string installNameTool =
  340. this->Target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  341. if(!installNameTool.size())
  342. {
  343. return;
  344. }
  345. // Build a map of build-tree install_name to install-tree install_name for
  346. // shared libraries linked to this target.
  347. std::map<cmStdString, cmStdString> install_name_remap;
  348. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  349. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  350. {
  351. linkType = cmTarget::DEBUG;
  352. }
  353. // TODO: Merge with ComputeLinkInformation.
  354. const cmTarget::LinkLibraryVectorType& inLibs =
  355. this->Target->GetLinkLibraries();
  356. for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
  357. j != inLibs.end(); ++j)
  358. {
  359. std::string lib = j->first;
  360. if((this->Target->GetType() == cmTarget::EXECUTABLE ||
  361. lib != this->Target->GetName()) &&
  362. (j->second == cmTarget::GENERAL || j->second == linkType))
  363. {
  364. if(cmTarget* tgt = this->Target->GetMakefile()->
  365. GetLocalGenerator()->GetGlobalGenerator()->
  366. FindTarget(0, lib.c_str()))
  367. {
  368. if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
  369. {
  370. // If the build tree and install tree use different path
  371. // components of the install_name field then we need to create a
  372. // mapping to be applied after installation.
  373. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  374. std::string for_install =
  375. tgt->GetInstallNameDirForInstallTree(config);
  376. std::string fname =
  377. this->GetInstallFilename(tgt, config, false, true);
  378. // Map from the build-tree install_name.
  379. for_build += fname;
  380. // Map to the install-tree install_name.
  381. if (!for_install.empty())
  382. {
  383. for_install += fname;
  384. }
  385. else
  386. {
  387. for_install = tgt->GetInstallNameFixupPath();
  388. }
  389. if(for_build != for_install)
  390. {
  391. // Store the mapping entry.
  392. install_name_remap[for_build] = for_install;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. // Edit the install_name of the target itself if necessary.
  399. std::string new_id;
  400. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  401. {
  402. std::string for_build =
  403. this->Target->GetInstallNameDirForBuildTree(config);
  404. std::string for_install =
  405. this->Target->GetInstallNameDirForInstallTree(config);
  406. std::string fname =
  407. this->GetInstallFilename(this->Target, config, this->ImportLibrary,
  408. true);
  409. for_build += fname;
  410. if (!for_install.empty())
  411. {
  412. for_install += fname;
  413. }
  414. else
  415. {
  416. for_install = this->Target->GetInstallNameFixupPath();
  417. }
  418. if(for_build != for_install)
  419. {
  420. // Prepare to refer to the install-tree install_name.
  421. new_id = for_install;
  422. }
  423. }
  424. // Write a rule to run install_name_tool to set the install-tree
  425. // install_name value and references.
  426. if(!new_id.empty() || !install_name_remap.empty())
  427. {
  428. os << indent << "EXECUTE_PROCESS(COMMAND \"" << installNameTool;
  429. os << "\"";
  430. if(!new_id.empty())
  431. {
  432. os << "\n" << indent << " -id \"" << new_id << "\"";
  433. }
  434. for(std::map<cmStdString, cmStdString>::const_iterator
  435. i = install_name_remap.begin();
  436. i != install_name_remap.end(); ++i)
  437. {
  438. os << "\n" << indent << " -change \""
  439. << i->first << "\" \"" << i->second << "\"";
  440. }
  441. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  442. }
  443. }
  444. //----------------------------------------------------------------------------
  445. void
  446. cmInstallTargetGenerator
  447. ::AddChrpathPatchRule(std::ostream& os, Indent const& indent,
  448. std::string const& toDestDirPath)
  449. {
  450. if(this->ImportLibrary ||
  451. !(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  452. this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
  453. this->Target->GetType() == cmTarget::EXECUTABLE))
  454. {
  455. return;
  456. }
  457. if((this->Target->GetMakefile()->IsOn("CMAKE_USE_CHRPATH")==false)
  458. || (this->Target->IsChrpathAvailable()==false))
  459. {
  460. return;
  461. }
  462. // Fix the RPATH in installed ELF binaries using chrpath.
  463. std::string chrpathTool =
  464. this->Target->GetMakefile()->GetSafeDefinition("CMAKE_CHRPATH");
  465. std::string installRpath;
  466. std::string dummy;
  467. this->Target->GetMakefile()->GetLocalGenerator()->GetLinkerArgs(
  468. installRpath, dummy, *this->Target, true, 0);
  469. const char* linkLanguage = this->Target->GetLinkerLanguage(this->Target->
  470. GetMakefile()->GetLocalGenerator()->GetGlobalGenerator());
  471. if (linkLanguage==0)
  472. {
  473. return;
  474. }
  475. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  476. runTimeFlagVar += linkLanguage;
  477. runTimeFlagVar += "_FLAG";
  478. std::string runtimeFlag =
  479. this->Target->GetMakefile()->GetSafeDefinition(runTimeFlagVar.c_str());
  480. const char* newRpath=installRpath.c_str();
  481. if (strstr(installRpath.c_str(), runtimeFlag.c_str())==installRpath.c_str())
  482. {
  483. newRpath = installRpath.c_str()+strlen(runtimeFlag.c_str());
  484. }
  485. // Write a rule to run chrpath to set the install-tree RPATH
  486. os << indent << "EXECUTE_PROCESS(COMMAND \"" << chrpathTool;
  487. os << "\" -r \"" << newRpath << "\" \"" << toDestDirPath << "\")\n";
  488. }
  489. //----------------------------------------------------------------------------
  490. void
  491. cmInstallTargetGenerator::AddStripRule(std::ostream& os,
  492. Indent const& indent,
  493. cmTarget::TargetType type,
  494. const std::string& toDestDirPath)
  495. {
  496. // don't strip static libraries, because it removes the only symbol table
  497. // they have so you can't link to them anymore
  498. if(type == cmTarget::STATIC_LIBRARY)
  499. {
  500. return;
  501. }
  502. // Don't handle OSX Bundles.
  503. if(this->Target->GetMakefile()->IsOn("APPLE") &&
  504. this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  505. {
  506. return;
  507. }
  508. if(! this->Target->GetMakefile()->IsSet("CMAKE_STRIP"))
  509. {
  510. return;
  511. }
  512. os << indent << "IF(CMAKE_INSTALL_DO_STRIP)\n";
  513. os << indent << " EXECUTE_PROCESS(COMMAND \""
  514. << this->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
  515. << "\" \"" << toDestDirPath << "\")\n";
  516. os << indent << "ENDIF(CMAKE_INSTALL_DO_STRIP)\n";
  517. }
  518. //----------------------------------------------------------------------------
  519. void
  520. cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
  521. Indent const& indent,
  522. cmTarget::TargetType type,
  523. const std::string& toDestDirPath)
  524. {
  525. // Static libraries need ranlib on this platform.
  526. if(type != cmTarget::STATIC_LIBRARY)
  527. {
  528. return;
  529. }
  530. // Perform post-installation processing on the file depending
  531. // on its type.
  532. if(!this->Target->GetMakefile()->IsOn("APPLE"))
  533. {
  534. return;
  535. }
  536. std::string ranlib =
  537. this->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  538. if(ranlib.empty())
  539. {
  540. return;
  541. }
  542. os << indent << "EXECUTE_PROCESS(COMMAND \""
  543. << ranlib << "\" \"" << toDestDirPath << "\")\n";
  544. }