1
0

cmInstallTargetGenerator.cxx 13 KB

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