1
0

cmInstallTargetGenerator.cxx 12 KB

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