cmInstallTargetGenerator.cxx 12 KB

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