cmExportFileGenerator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 "cmExportFileGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. #include "cmVersion.h"
  19. #include <cmsys/auto_ptr.hxx>
  20. //----------------------------------------------------------------------------
  21. cmExportFileGenerator::cmExportFileGenerator()
  22. {
  23. this->AppendMode = false;
  24. }
  25. //----------------------------------------------------------------------------
  26. void cmExportFileGenerator::AddConfiguration(const char* config)
  27. {
  28. this->Configurations.push_back(config);
  29. }
  30. //----------------------------------------------------------------------------
  31. void cmExportFileGenerator::SetExportFile(const char* mainFile)
  32. {
  33. this->MainImportFile = mainFile;
  34. this->FileDir =
  35. cmSystemTools::GetFilenamePath(this->MainImportFile);
  36. this->FileBase =
  37. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  38. this->FileExt =
  39. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  40. }
  41. //----------------------------------------------------------------------------
  42. bool cmExportFileGenerator::GenerateImportFile()
  43. {
  44. // Open the output file to generate it.
  45. cmsys::auto_ptr<std::ofstream> foutPtr;
  46. if(this->AppendMode)
  47. {
  48. // Open for append.
  49. cmsys::auto_ptr<std::ofstream>
  50. ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app));
  51. foutPtr = ap;
  52. }
  53. else
  54. {
  55. // Generate atomically and with copy-if-different.
  56. cmsys::auto_ptr<cmGeneratedFileStream>
  57. ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
  58. ap->SetCopyIfDifferent(true);
  59. foutPtr = ap;
  60. }
  61. if(!foutPtr.get() || !*foutPtr)
  62. {
  63. std::string se = cmSystemTools::GetLastSystemError();
  64. cmOStringStream e;
  65. e << "cannot write to file \"" << this->MainImportFile
  66. << "\": " << se;
  67. cmSystemTools::Error(e.str().c_str());
  68. return false;
  69. }
  70. std::ostream& os = *foutPtr;
  71. // Isolate the file policy level.
  72. // We use 2.6 here instead of the current version because newer
  73. // versions of CMake should be able to export files imported by 2.6
  74. // until the import format changes.
  75. os << "CMAKE_POLICY(PUSH)\n"
  76. << "CMAKE_POLICY(VERSION 2.6)\n";
  77. // Start with the import file header.
  78. this->GenerateImportHeaderCode(os);
  79. // Create all the imported targets.
  80. bool result = this->GenerateMainFile(os);
  81. // End with the import file footer.
  82. this->GenerateImportFooterCode(os);
  83. os << "CMAKE_POLICY(POP)\n";
  84. return result;
  85. }
  86. //----------------------------------------------------------------------------
  87. void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
  88. const char* config)
  89. {
  90. // Construct the property configuration suffix.
  91. std::string suffix = "_";
  92. if(config && *config)
  93. {
  94. suffix += cmSystemTools::UpperCase(config);
  95. }
  96. else
  97. {
  98. suffix += "NOCONFIG";
  99. }
  100. // Generate the per-config target information.
  101. this->GenerateImportTargetsConfig(os, config, suffix);
  102. }
  103. //----------------------------------------------------------------------------
  104. void
  105. cmExportFileGenerator
  106. ::SetImportDetailProperties(const char* config, std::string const& suffix,
  107. cmTarget* target, ImportPropertyMap& properties)
  108. {
  109. // Get the makefile in which to lookup target information.
  110. cmMakefile* mf = target->GetMakefile();
  111. // Add the soname for unix shared libraries.
  112. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  113. target->GetType() == cmTarget::MODULE_LIBRARY)
  114. {
  115. // Check whether this is a DLL platform.
  116. bool dll_platform =
  117. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  118. if(!dll_platform)
  119. {
  120. std::string soname = target->GetSOName(config);
  121. std::string prop = "IMPORTED_SONAME";
  122. prop += suffix;
  123. properties[prop] = soname;
  124. }
  125. }
  126. // Add the transitive link dependencies for this configuration.
  127. if(cmTargetLinkInterface const* iface =
  128. target->GetLinkInterface(config))
  129. {
  130. // This target provides a link interface, so use it.
  131. this->SetImportLinkProperty(suffix, target,
  132. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  133. iface->Libraries, properties);
  134. this->SetImportLinkProperty(suffix, target,
  135. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  136. iface->SharedDeps, properties);
  137. }
  138. else if(target->GetType() == cmTarget::STATIC_LIBRARY ||
  139. target->GetType() == cmTarget::SHARED_LIBRARY)
  140. {
  141. // The default link interface for static and shared libraries is
  142. // their link implementation library list.
  143. this->SetImportLinkProperties(config, suffix, target, properties);
  144. }
  145. }
  146. //----------------------------------------------------------------------------
  147. void
  148. cmExportFileGenerator
  149. ::SetImportLinkProperties(const char* config, std::string const& suffix,
  150. cmTarget* target, ImportPropertyMap& properties)
  151. {
  152. // Compute which library configuration to link.
  153. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  154. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  155. {
  156. linkType = cmTarget::DEBUG;
  157. }
  158. // Construct the list of libs linked for this configuration.
  159. std::vector<std::string> actual_libs;
  160. cmTarget::LinkLibraryVectorType const& libs =
  161. target->GetOriginalLinkLibraries();
  162. for(cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
  163. li != libs.end(); ++li)
  164. {
  165. // Skip entries that will resolve to the target itself, are empty,
  166. // or are not meant for this configuration.
  167. if(li->first == target->GetName() || li->first.empty() ||
  168. !(li->second == cmTarget::GENERAL || li->second == linkType))
  169. {
  170. continue;
  171. }
  172. // Store this entry.
  173. actual_libs.push_back(li->first);
  174. }
  175. // Store the entries in the property.
  176. this->SetImportLinkProperty(suffix, target,
  177. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  178. actual_libs, properties);
  179. }
  180. //----------------------------------------------------------------------------
  181. void
  182. cmExportFileGenerator
  183. ::SetImportLinkProperty(std::string const& suffix,
  184. cmTarget* target,
  185. const char* propName,
  186. std::vector<std::string> const& libs,
  187. ImportPropertyMap& properties)
  188. {
  189. // Skip the property if there are no libraries.
  190. if(libs.empty())
  191. {
  192. return;
  193. }
  194. // Get the makefile in which to lookup target information.
  195. cmMakefile* mf = target->GetMakefile();
  196. // Construct the property value.
  197. std::string link_libs;
  198. const char* sep = "";
  199. for(std::vector<std::string>::const_iterator li = libs.begin();
  200. li != libs.end(); ++li)
  201. {
  202. // Separate this from the previous entry.
  203. link_libs += sep;
  204. sep = ";";
  205. // Append this entry.
  206. if(cmTarget* tgt = mf->FindTargetToUse(li->c_str()))
  207. {
  208. // This is a target.
  209. if(tgt->IsImported())
  210. {
  211. // The target is imported (and therefore is not in the
  212. // export). Append the raw name.
  213. link_libs += *li;
  214. }
  215. else if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
  216. {
  217. // The target is in the export. Append it with the export
  218. // namespace.
  219. link_libs += this->Namespace;
  220. link_libs += *li;
  221. }
  222. else
  223. {
  224. // The target is not in the export.
  225. if(!this->AppendMode)
  226. {
  227. // We are not appending, so all exported targets should be
  228. // known here. This is probably user-error.
  229. this->ComplainAboutMissingTarget(target, tgt);
  230. }
  231. // Assume the target will be exported by another command.
  232. // Append it with the export namespace.
  233. link_libs += this->Namespace;
  234. link_libs += *li;
  235. }
  236. }
  237. else
  238. {
  239. // Append the raw name.
  240. link_libs += *li;
  241. }
  242. }
  243. // Store the property.
  244. std::string prop = propName;
  245. prop += suffix;
  246. properties[prop] = link_libs;
  247. }
  248. //----------------------------------------------------------------------------
  249. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  250. const char* config)
  251. {
  252. os << "#----------------------------------------------------------------\n"
  253. << "# Generated CMake target import file";
  254. if(config)
  255. {
  256. os << " for configuration \"" << config << "\".\n";
  257. }
  258. else
  259. {
  260. os << ".\n";
  261. }
  262. os << "#----------------------------------------------------------------\n"
  263. << "\n";
  264. this->GenerateImportVersionCode(os);
  265. }
  266. //----------------------------------------------------------------------------
  267. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  268. {
  269. os << "# Commands beyond this point should not need to know the version.\n"
  270. << "SET(CMAKE_IMPORT_FILE_VERSION)\n";
  271. }
  272. //----------------------------------------------------------------------------
  273. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  274. {
  275. // Store an import file format version. This will let us change the
  276. // format later while still allowing old import files to work.
  277. os << "# Commands may need to know the format version.\n"
  278. << "SET(CMAKE_IMPORT_FILE_VERSION 1)\n"
  279. << "\n";
  280. }
  281. //----------------------------------------------------------------------------
  282. void
  283. cmExportFileGenerator
  284. ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
  285. {
  286. // Construct the imported target name.
  287. std::string targetName = this->Namespace;
  288. targetName += target->GetName();
  289. // Create the imported target.
  290. os << "# Create imported target " << targetName << "\n";
  291. switch(target->GetType())
  292. {
  293. case cmTarget::EXECUTABLE:
  294. os << "ADD_EXECUTABLE(" << targetName << " IMPORTED)\n";
  295. break;
  296. case cmTarget::STATIC_LIBRARY:
  297. os << "ADD_LIBRARY(" << targetName << " STATIC IMPORTED)\n";
  298. break;
  299. case cmTarget::SHARED_LIBRARY:
  300. os << "ADD_LIBRARY(" << targetName << " SHARED IMPORTED)\n";
  301. break;
  302. case cmTarget::MODULE_LIBRARY:
  303. os << "ADD_LIBRARY(" << targetName << " MODULE IMPORTED)\n";
  304. break;
  305. default: // should never happen
  306. break;
  307. }
  308. // Mark the imported executable if it has exports.
  309. if(target->IsExecutableWithExports())
  310. {
  311. os << "SET_PROPERTY(TARGET " << targetName
  312. << " PROPERTY ENABLE_EXPORTS 1)\n";
  313. }
  314. // Mark the imported library if it is a framework.
  315. if(target->IsFrameworkOnApple())
  316. {
  317. os << "SET_PROPERTY(TARGET " << targetName
  318. << " PROPERTY FRAMEWORK 1)\n";
  319. }
  320. // Mark the imported executable if it is an application bundle.
  321. if(target->IsAppBundleOnApple())
  322. {
  323. os << "SET_PROPERTY(TARGET " << targetName
  324. << " PROPERTY MACOSX_BUNDLE 1)\n";
  325. }
  326. os << "\n";
  327. }
  328. //----------------------------------------------------------------------------
  329. void
  330. cmExportFileGenerator
  331. ::GenerateImportPropertyCode(std::ostream& os, const char* config,
  332. cmTarget* target,
  333. ImportPropertyMap const& properties)
  334. {
  335. // Construct the imported target name.
  336. std::string targetName = this->Namespace;
  337. targetName += target->GetName();
  338. // Set the import properties.
  339. os << "# Import target \"" << targetName << "\" for configuration \""
  340. << config << "\"\n";
  341. os << "SET_PROPERTY(TARGET " << targetName
  342. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  343. if(config && *config)
  344. {
  345. os << cmSystemTools::UpperCase(config);
  346. }
  347. else
  348. {
  349. os << "NOCONFIG";
  350. }
  351. os << ")\n";
  352. os << "SET_TARGET_PROPERTIES(" << targetName << " PROPERTIES\n";
  353. for(ImportPropertyMap::const_iterator pi = properties.begin();
  354. pi != properties.end(); ++pi)
  355. {
  356. os << " " << pi->first << " \"" << pi->second << "\"\n";
  357. }
  358. os << " )\n"
  359. << "\n";
  360. }