cmInstallTargetGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. //----------------------------------------------------------------------------
  19. cmInstallTargetGenerator
  20. ::cmInstallTargetGenerator(cmTarget& t, const char* dest, bool implib,
  21. const char* file_permissions,
  22. std::vector<std::string> const& configurations,
  23. const char* component, bool optional):
  24. Target(&t), Destination(dest), ImportLibrary(implib),
  25. FilePermissions(file_permissions), Configurations(configurations),
  26. Component(component), Optional(optional)
  27. {
  28. this->Target->SetHaveInstallRule(true);
  29. }
  30. //----------------------------------------------------------------------------
  31. cmInstallTargetGenerator
  32. ::~cmInstallTargetGenerator()
  33. {
  34. }
  35. //----------------------------------------------------------------------------
  36. void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
  37. {
  38. // Compute the build tree directory from which to copy the target.
  39. std::string fromDir;
  40. if(this->Target->NeedRelinkBeforeInstall())
  41. {
  42. fromDir = this->Target->GetMakefile()->GetStartOutputDirectory();
  43. fromDir += cmake::GetCMakeFilesDirectory();
  44. fromDir += "/CMakeRelink.dir/";
  45. }
  46. else
  47. {
  48. fromDir = this->Target->GetDirectory(0, this->ImportLibrary);
  49. fromDir += "/";
  50. }
  51. // Write variable settings to do per-configuration references.
  52. this->PrepareScriptReference(os, this->Target, "BUILD", true, false);
  53. // Create the per-configuration reference.
  54. std::string fromName = this->GetScriptReference(this->Target, "BUILD",
  55. false);
  56. std::string fromFile = fromDir;
  57. fromFile += fromName;
  58. // Choose the final destination. This may be modified for certain
  59. // target types.
  60. std::string destination = this->Destination;
  61. // Setup special properties for some target types.
  62. std::string literal_args;
  63. std::string props;
  64. const char* properties = 0;
  65. cmTarget::TargetType type = this->Target->GetType();
  66. switch(type)
  67. {
  68. case cmTarget::SHARED_LIBRARY:
  69. {
  70. // Add shared library installation properties if this platform
  71. // supports them.
  72. const char* lib_version = 0;
  73. const char* lib_soversion = 0;
  74. // Versioning is supported only for shared libraries and modules,
  75. // and then only when the platform supports an soname flag.
  76. cmGlobalGenerator* gg =
  77. (this->Target->GetMakefile()
  78. ->GetLocalGenerator()->GetGlobalGenerator());
  79. if(const char* linkLanguage = this->Target->GetLinkerLanguage(gg))
  80. {
  81. std::string sonameFlagVar = "CMAKE_SHARED_LIBRARY_SONAME_";
  82. sonameFlagVar += linkLanguage;
  83. sonameFlagVar += "_FLAG";
  84. if(this->Target->GetMakefile()->GetDefinition(sonameFlagVar.c_str()))
  85. {
  86. lib_version = this->Target->GetProperty("VERSION");
  87. lib_soversion = this->Target->GetProperty("SOVERSION");
  88. }
  89. }
  90. if(lib_version)
  91. {
  92. props += " VERSION ";
  93. props += lib_version;
  94. }
  95. if(lib_soversion)
  96. {
  97. props += " SOVERSION ";
  98. props += lib_soversion;
  99. }
  100. properties = props.c_str();
  101. }
  102. break;
  103. case cmTarget::EXECUTABLE:
  104. {
  105. // Add executable installation properties if this platform
  106. // supports them.
  107. #if defined(_WIN32) && !defined(__CYGWIN__)
  108. const char* exe_version = 0;
  109. #else
  110. const char* exe_version = this->Target->GetProperty("VERSION");
  111. #endif
  112. if(exe_version)
  113. {
  114. props += " VERSION ";
  115. props += exe_version;
  116. properties = props.c_str();
  117. }
  118. // Handle OSX Bundles.
  119. if(this->Target->GetMakefile()->IsOn("APPLE") &&
  120. this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  121. {
  122. // Compute the source locations of the bundle executable and
  123. // Info.plist file.
  124. this->PrepareScriptReference(os, this->Target, "INSTALL",
  125. false, false);
  126. fromFile += ".app";
  127. type = cmTarget::INSTALL_DIRECTORY;
  128. literal_args += " USE_SOURCE_PERMISSIONS";
  129. }
  130. }
  131. break;
  132. case cmTarget::STATIC_LIBRARY:
  133. case cmTarget::MODULE_LIBRARY:
  134. // Nothing special for modules or static libraries.
  135. break;
  136. default:
  137. break;
  138. }
  139. // An import library looks like a static library.
  140. if(this->ImportLibrary)
  141. {
  142. type = cmTarget::STATIC_LIBRARY;
  143. }
  144. // Write code to install the target file.
  145. const char* no_dir_permissions = 0;
  146. const char* no_rename = 0;
  147. bool optional = this->Optional | this->ImportLibrary;
  148. this->AddInstallRule(os, destination.c_str(), type, fromFile.c_str(),
  149. optional, properties,
  150. this->FilePermissions.c_str(), no_dir_permissions,
  151. this->Configurations,
  152. this->Component.c_str(),
  153. no_rename, literal_args.c_str());
  154. // Fix the install_name settings in installed binaries.
  155. if((type == cmTarget::SHARED_LIBRARY ||
  156. type == cmTarget::MODULE_LIBRARY ||
  157. type == cmTarget::EXECUTABLE))
  158. {
  159. this->AddInstallNamePatchRule(os, destination.c_str());
  160. }
  161. std::string destinationFilename = destination;
  162. destinationFilename += "/";
  163. destinationFilename += cmSystemTools::GetFilenameName(fromFile);
  164. this->AddRanlibRule(os, type, destinationFilename);
  165. this->AddStripRule(os, destinationFilename);
  166. }
  167. //----------------------------------------------------------------------------
  168. void
  169. cmInstallTargetGenerator
  170. ::PrepareScriptReference(std::ostream& os, cmTarget* target,
  171. const char* place, bool useConfigDir,
  172. bool useSOName)
  173. {
  174. // If the target name may vary with the configuration type then
  175. // store all possible names ahead of time in variables.
  176. std::string fname;
  177. for(std::vector<std::string>::const_iterator i =
  178. this->ConfigurationTypes->begin();
  179. i != this->ConfigurationTypes->end(); ++i)
  180. {
  181. // Initialize the name.
  182. fname = "";
  183. if(useConfigDir)
  184. {
  185. // Start with the configuration's subdirectory.
  186. target->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()->
  187. AppendDirectoryForConfig("", i->c_str(), "/", fname);
  188. }
  189. // Compute the name of the library.
  190. if(target->GetType() == cmTarget::EXECUTABLE)
  191. {
  192. std::string targetName;
  193. std::string targetNameReal;
  194. std::string targetNameImport;
  195. std::string targetNamePDB;
  196. target->GetExecutableNames(targetName, targetNameReal,
  197. targetNameImport, targetNamePDB,
  198. i->c_str());
  199. if(this->ImportLibrary)
  200. {
  201. // Use the import library name.
  202. fname += targetNameImport;
  203. }
  204. else
  205. {
  206. // Use the canonical name.
  207. fname += targetName;
  208. }
  209. }
  210. else
  211. {
  212. std::string targetName;
  213. std::string targetNameSO;
  214. std::string targetNameReal;
  215. std::string targetNameImport;
  216. std::string targetNamePDB;
  217. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  218. targetNameImport, targetNamePDB, i->c_str());
  219. if(this->ImportLibrary)
  220. {
  221. // Use the import library name.
  222. fname += targetNameImport;
  223. }
  224. else if(useSOName)
  225. {
  226. // Use the soname.
  227. fname += targetNameSO;
  228. }
  229. else
  230. {
  231. // Use the canonical name.
  232. fname += targetName;
  233. }
  234. }
  235. // Set a variable with the target name for this configuration.
  236. os << "SET(" << target->GetName() << "_" << place
  237. << (this->ImportLibrary? "_IMPNAME_" : "_NAME_") << *i
  238. << " \"" << fname << "\")\n";
  239. }
  240. }
  241. //----------------------------------------------------------------------------
  242. std::string cmInstallTargetGenerator::GetScriptReference(cmTarget* target,
  243. const char* place,
  244. bool useSOName)
  245. {
  246. if(this->ConfigurationTypes->empty())
  247. {
  248. // Reference the target by its one configuration name.
  249. if(target->GetType() == cmTarget::EXECUTABLE)
  250. {
  251. std::string targetName;
  252. std::string targetNameReal;
  253. std::string targetNameImport;
  254. std::string targetNamePDB;
  255. target->GetExecutableNames(targetName, targetNameReal,
  256. targetNameImport, targetNamePDB,
  257. this->ConfigurationName);
  258. if(this->ImportLibrary)
  259. {
  260. // Use the import library name.
  261. return targetNameImport;
  262. }
  263. else
  264. {
  265. // Use the canonical name.
  266. return targetName;
  267. }
  268. }
  269. else
  270. {
  271. std::string targetName;
  272. std::string targetNameSO;
  273. std::string targetNameReal;
  274. std::string targetNameImport;
  275. std::string targetNamePDB;
  276. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  277. targetNameImport, targetNamePDB,
  278. this->ConfigurationName);
  279. if(this->ImportLibrary)
  280. {
  281. // Use the import library name.
  282. return targetNameImport;
  283. }
  284. else if(useSOName)
  285. {
  286. // Use the soname.
  287. return targetNameSO;
  288. }
  289. else
  290. {
  291. // Use the canonical name.
  292. return targetName;
  293. }
  294. }
  295. }
  296. else
  297. {
  298. // Reference the target using the per-configuration variable.
  299. std::string ref = "${";
  300. ref += target->GetName();
  301. if(this->ImportLibrary)
  302. {
  303. ref += "_";
  304. ref += place;
  305. ref += "_IMPNAME_";
  306. }
  307. else
  308. {
  309. ref += "_";
  310. ref += place;
  311. ref += "_NAME_";
  312. }
  313. ref += "${CMAKE_INSTALL_CONFIG_NAME}}";
  314. return ref;
  315. }
  316. }
  317. //----------------------------------------------------------------------------
  318. void cmInstallTargetGenerator
  319. ::AddInstallNamePatchRule(std::ostream& os,
  320. const char* destination)
  321. {
  322. std::string installNameTool =
  323. this->Target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  324. if(!installNameTool.size())
  325. {
  326. return;
  327. }
  328. // Build a map of build-tree install_name to install-tree install_name for
  329. // shared libraries linked to this target.
  330. std::map<cmStdString, cmStdString> install_name_remap;
  331. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  332. const char* config = this->ConfigurationName;
  333. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  334. {
  335. linkType = cmTarget::DEBUG;
  336. }
  337. // TODO: Merge with ComputeLinkInformation.
  338. const cmTarget::LinkLibraryVectorType& inLibs =
  339. this->Target->GetLinkLibraries();
  340. for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
  341. j != inLibs.end(); ++j)
  342. {
  343. std::string lib = j->first;
  344. if((this->Target->GetType() == cmTarget::EXECUTABLE ||
  345. lib != this->Target->GetName()) &&
  346. (j->second == cmTarget::GENERAL || j->second == linkType))
  347. {
  348. if(cmTarget* tgt = this->Target->GetMakefile()->
  349. GetLocalGenerator()->GetGlobalGenerator()->
  350. FindTarget(0, lib.c_str()))
  351. {
  352. if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
  353. {
  354. // If the build tree and install tree use different path
  355. // components of the install_name field then we need to create a
  356. // mapping to be applied after installation.
  357. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  358. std::string for_install =
  359. tgt->GetInstallNameDirForInstallTree(config);
  360. if(for_build != for_install)
  361. {
  362. // Map from the build-tree install_name.
  363. this->PrepareScriptReference(os, tgt, "REMAP_FROM",
  364. !for_build.empty(), true);
  365. for_build += this->GetScriptReference(tgt, "REMAP_FROM", true);
  366. // Map to the install-tree install_name.
  367. this->PrepareScriptReference(os, tgt, "REMAP_TO",
  368. false, true);
  369. for_install += this->GetScriptReference(tgt, "REMAP_TO", true);
  370. // Store the mapping entry.
  371. install_name_remap[for_build] = for_install;
  372. }
  373. }
  374. }
  375. }
  376. }
  377. // Edit the install_name of the target itself if necessary.
  378. this->PrepareScriptReference(os, this->Target, "REMAPPED", false, true);
  379. std::string new_id;
  380. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  381. {
  382. std::string for_build =
  383. this->Target->GetInstallNameDirForBuildTree(config);
  384. std::string for_install =
  385. this->Target->GetInstallNameDirForInstallTree(config);
  386. if(for_build != for_install)
  387. {
  388. // Prepare to refer to the install-tree install_name.
  389. new_id = for_install;
  390. new_id += this->GetScriptReference(this->Target, "REMAPPED", true);
  391. }
  392. }
  393. // Write a rule to run install_name_tool to set the install-tree
  394. // install_name value and references.
  395. if(!new_id.empty() || !install_name_remap.empty())
  396. {
  397. std::string component_test = "NOT CMAKE_INSTALL_COMPONENT OR "
  398. "\"${CMAKE_INSTALL_COMPONENT}\" MATCHES \"^(";
  399. component_test += this->Component;
  400. component_test += ")$\"";
  401. os << "IF(" << component_test << ")\n";
  402. os << " EXECUTE_PROCESS(COMMAND \"" << installNameTool;
  403. os << "\"";
  404. if(!new_id.empty())
  405. {
  406. os << "\n -id \"" << new_id << "\"";
  407. }
  408. for(std::map<cmStdString, cmStdString>::const_iterator
  409. i = install_name_remap.begin();
  410. i != install_name_remap.end(); ++i)
  411. {
  412. os << "\n -change \"" << i->first << "\" \"" << i->second << "\"";
  413. }
  414. os << "\n \"$ENV{DESTDIR}" << destination << "/"
  415. << this->GetScriptReference(this->Target, "REMAPPED", true) << "\")\n";
  416. os << "ENDIF(" << component_test << ")\n";
  417. }
  418. }
  419. void cmInstallTargetGenerator::AddStripRule(std::ostream& os,
  420. const std::string& destinationFilename)
  421. {
  422. // Don't handle OSX Bundles.
  423. if(this->Target->GetMakefile()->IsOn("APPLE") &&
  424. this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  425. {
  426. return;
  427. }
  428. if(! this->Target->GetMakefile()->IsSet("CMAKE_STRIP"))
  429. {
  430. return;
  431. }
  432. os << "IF(CMAKE_INSTALL_DO_STRIP)\n";
  433. os << " EXECUTE_PROCESS(COMMAND \"";
  434. os << this->Target->GetMakefile()->GetDefinition("CMAKE_STRIP");
  435. os << "\" \"$ENV{DESTDIR}" << destinationFilename << "\" )\n";
  436. os << "ENDIF(CMAKE_INSTALL_DO_STRIP)\n";
  437. }
  438. void cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
  439. cmTarget::TargetType type,
  440. const std::string& destinationFilename)
  441. {
  442. // Static libraries need ranlib on this platform.
  443. if(type != cmTarget::STATIC_LIBRARY)
  444. {
  445. return;
  446. }
  447. // Perform post-installation processing on the file depending
  448. // on its type.
  449. if(!this->Target->GetMakefile()->IsOn("APPLE"))
  450. {
  451. return;
  452. }
  453. std::string ranlib = this->Target->GetMakefile()->GetRequiredDefinition(
  454. "CMAKE_RANLIB");
  455. if (!ranlib.size())
  456. {
  457. return;
  458. }
  459. os << "EXECUTE_PROCESS(COMMAND \"";
  460. os << ranlib;
  461. os << "\" \"$ENV{DESTDIR}" << destinationFilename << "\" )\n";
  462. }