cmInstallTargetGenerator.cxx 12 KB

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