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. Target(&t), Destination(dest), ImportLibrary(implib),
  23. Permissions(permissions)
  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->GetPropertyAsBool("MACOSX_BUNDLE"))
  107. {
  108. // Compute the source locations of the bundle executable and
  109. // Info.plist file.
  110. this->PrepareScriptReference(os, this->Target, "INSTALL",
  111. false, false);
  112. std::string plist = fromFile;
  113. plist += ".app/Contents/Info.plist";
  114. fromFile += ".app/Contents/MacOS/";
  115. fromFile += this->GetScriptReference(this->Target, "INSTALL",
  116. false);
  117. // Compute the destination locations of the bundle Info.plist file.
  118. destination += "/";
  119. destination += this->GetScriptReference(this->Target, "INSTALL",
  120. false);
  121. destination += ".app/Contents";
  122. // Install the Info.plist file.
  123. this->AddInstallRule(os, destination.c_str(), cmTarget::INSTALL_FILES,
  124. plist.c_str());
  125. // Compute the destination locations of the bundle executable file.
  126. destination += "/MacOS";
  127. }
  128. }
  129. break;
  130. case cmTarget::STATIC_LIBRARY:
  131. case cmTarget::MODULE_LIBRARY:
  132. // Nothing special for modules or static libraries.
  133. break;
  134. default:
  135. break;
  136. }
  137. // An import library looks like a static library.
  138. if(this->ImportLibrary)
  139. {
  140. type = cmTarget::STATIC_LIBRARY;
  141. }
  142. // Write code to install the target file.
  143. this->AddInstallRule(os, destination.c_str(), type, fromFile.c_str(),
  144. this->ImportLibrary, properties,
  145. this->Permissions.c_str());
  146. // Fix the install_name settings in installed binaries.
  147. if(type == cmTarget::SHARED_LIBRARY ||
  148. type == cmTarget::MODULE_LIBRARY ||
  149. type == cmTarget::EXECUTABLE)
  150. {
  151. this->AddInstallNamePatchRule(os, destination.c_str());
  152. }
  153. }
  154. //----------------------------------------------------------------------------
  155. void
  156. cmInstallTargetGenerator
  157. ::PrepareScriptReference(std::ostream& os, cmTarget* target,
  158. const char* place, bool useConfigDir,
  159. bool useSOName)
  160. {
  161. // If the target name may vary with the configuration type then
  162. // store all possible names ahead of time in variables.
  163. std::string fname;
  164. for(std::vector<std::string>::const_iterator i =
  165. this->ConfigurationTypes->begin();
  166. i != this->ConfigurationTypes->end(); ++i)
  167. {
  168. // Initialize the name.
  169. fname = "";
  170. if(useConfigDir)
  171. {
  172. // Start with the configuration's subdirectory.
  173. target->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()->
  174. AppendDirectoryForConfig("", i->c_str(), "/", fname);
  175. }
  176. // Compute the name of the library.
  177. std::string targetName;
  178. std::string targetNameSO;
  179. std::string targetNameReal;
  180. std::string targetNameImport;
  181. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  182. targetNameImport, i->c_str());
  183. if(this->ImportLibrary)
  184. {
  185. // Use the import library name.
  186. fname += targetNameImport;
  187. }
  188. else if(useSOName)
  189. {
  190. // Use the soname.
  191. fname += targetNameSO;
  192. }
  193. else
  194. {
  195. // Use the canonical name.
  196. fname += targetName;
  197. }
  198. // Set a variable with the target name for this configuration.
  199. os << "SET(" << target->GetName() << "_" << place
  200. << (this->ImportLibrary? "_IMPNAME_" : "_NAME_") << *i
  201. << " \"" << fname << "\")\n";
  202. }
  203. }
  204. //----------------------------------------------------------------------------
  205. std::string cmInstallTargetGenerator::GetScriptReference(cmTarget* target,
  206. const char* place,
  207. bool useSOName)
  208. {
  209. if(this->ConfigurationTypes->empty())
  210. {
  211. // Reference the target by its one configuration name.
  212. std::string targetName;
  213. std::string targetNameSO;
  214. std::string targetNameReal;
  215. std::string targetNameImport;
  216. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  217. targetNameImport, this->ConfigurationName);
  218. if(this->ImportLibrary)
  219. {
  220. // Use the import library name.
  221. return targetNameImport;
  222. }
  223. else if(useSOName)
  224. {
  225. // Use the soname.
  226. return targetNameSO;
  227. }
  228. else
  229. {
  230. // Use the canonical name.
  231. return targetName;
  232. }
  233. }
  234. else
  235. {
  236. // Reference the target using the per-configuration variable.
  237. std::string ref = "${";
  238. ref += target->GetName();
  239. if(this->ImportLibrary)
  240. {
  241. ref += "_";
  242. ref += place;
  243. ref += "_IMPNAME_";
  244. }
  245. else
  246. {
  247. ref += "_";
  248. ref += place;
  249. ref += "_NAME_";
  250. }
  251. ref += "${CMAKE_INSTALL_CONFIG_NAME}}";
  252. return ref;
  253. }
  254. }
  255. //----------------------------------------------------------------------------
  256. void cmInstallTargetGenerator::AddInstallNamePatchRule(std::ostream& os,
  257. const char* destination)
  258. {
  259. // Build a map of build-tree install_name to install-tree install_name for
  260. // shared libraries linked to this target.
  261. std::map<cmStdString, cmStdString> install_name_remap;
  262. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  263. const char* config = this->ConfigurationName;
  264. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  265. {
  266. linkType = cmTarget::DEBUG;
  267. }
  268. // TODO: Merge with ComputeLinkInformation.
  269. const cmTarget::LinkLibraryVectorType& inLibs =
  270. this->Target->GetLinkLibraries();
  271. for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
  272. j != inLibs.end(); ++j)
  273. {
  274. std::string lib = j->first;
  275. if((this->Target->GetType() == cmTarget::EXECUTABLE ||
  276. lib != this->Target->GetName()) &&
  277. (j->second == cmTarget::GENERAL || j->second == linkType))
  278. {
  279. if(cmTarget* tgt = this->Target->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()->FindTarget(0, lib.c_str()))
  280. {
  281. if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
  282. {
  283. // If the build tree and install tree use different path components
  284. // of the install_name field then we need to create a mapping to be
  285. // applied after installation.
  286. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  287. std::string for_install =
  288. tgt->GetInstallNameDirForInstallTree(config);
  289. if(for_build != for_install)
  290. {
  291. // Map from the build-tree install_name.
  292. this->PrepareScriptReference(os, tgt, "REMAP_FROM",
  293. !for_build.empty(), true);
  294. for_build += this->GetScriptReference(tgt, "REMAP_FROM", true);
  295. // Map to the install-tree install_name.
  296. this->PrepareScriptReference(os, tgt, "REMAP_TO",
  297. false, true);
  298. for_install += this->GetScriptReference(tgt, "REMAP_TO", true);
  299. // Store the mapping entry.
  300. install_name_remap[for_build] = for_install;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. // Edit the install_name of the target itself if necessary.
  307. this->PrepareScriptReference(os, this->Target, "REMAPPED", false, true);
  308. std::string new_id;
  309. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  310. {
  311. std::string for_build = this->Target->GetInstallNameDirForBuildTree(config);
  312. std::string for_install = this->Target->GetInstallNameDirForInstallTree(config);
  313. if(for_build != for_install)
  314. {
  315. // Prepare to refer to the install-tree install_name.
  316. new_id = for_install;
  317. new_id += this->GetScriptReference(this->Target, "REMAPPED", true);
  318. }
  319. }
  320. // Write a rule to run install_name_tool to set the install-tree
  321. // install_name value and references.
  322. if(!new_id.empty() || !install_name_remap.empty())
  323. {
  324. os << "EXECUTE_PROCESS(COMMAND install_name_tool";
  325. if(!new_id.empty())
  326. {
  327. os << "\n -id \"" << new_id << "\"";
  328. }
  329. for(std::map<cmStdString, cmStdString>::const_iterator
  330. i = install_name_remap.begin();
  331. i != install_name_remap.end(); ++i)
  332. {
  333. os << "\n -change \"" << i->first << "\" \"" << i->second << "\"";
  334. }
  335. os << "\n \"" << destination << "/"
  336. << this->GetScriptReference(this->Target, "REMAPPED", true) << "\")\n";
  337. }
  338. }