cmInstallTargetGenerator.cxx 13 KB

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