cmInstallTargetGenerator.cxx 13 KB

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