cmExportFileGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <cmsys/auto_ptr.hxx>
  19. //----------------------------------------------------------------------------
  20. cmExportFileGenerator::cmExportFileGenerator()
  21. {
  22. this->AppendMode = false;
  23. }
  24. //----------------------------------------------------------------------------
  25. void cmExportFileGenerator::AddConfiguration(const char* config)
  26. {
  27. this->Configurations.push_back(config);
  28. }
  29. //----------------------------------------------------------------------------
  30. void cmExportFileGenerator::SetExportFile(const char* mainFile)
  31. {
  32. this->MainImportFile = mainFile;
  33. this->FileDir =
  34. cmSystemTools::GetFilenamePath(this->MainImportFile);
  35. this->FileBase =
  36. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  37. this->FileExt =
  38. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  39. }
  40. //----------------------------------------------------------------------------
  41. bool cmExportFileGenerator::GenerateImportFile()
  42. {
  43. // Open the output file to generate it.
  44. cmsys::auto_ptr<std::ofstream> foutPtr;
  45. if(this->AppendMode)
  46. {
  47. // Open for append.
  48. cmsys::auto_ptr<std::ofstream>
  49. ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app));
  50. foutPtr = ap;
  51. }
  52. else
  53. {
  54. // Generate atomically and with copy-if-different.
  55. cmsys::auto_ptr<cmGeneratedFileStream>
  56. ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
  57. ap->SetCopyIfDifferent(true);
  58. foutPtr = ap;
  59. }
  60. if(!foutPtr.get() || !*foutPtr)
  61. {
  62. std::string se = cmSystemTools::GetLastSystemError();
  63. cmOStringStream e;
  64. e << "cannot write to file \"" << this->MainImportFile
  65. << "\": " << se;
  66. cmSystemTools::Error(e.str().c_str());
  67. return false;
  68. }
  69. std::ostream& os = *foutPtr;
  70. // Start with the import file header.
  71. this->GenerateImportHeaderCode(os);
  72. // Create all the imported targets.
  73. bool result = this->GenerateMainFile(os);
  74. // End with the import file footer.
  75. this->GenerateImportFooterCode(os);
  76. return result;
  77. }
  78. //----------------------------------------------------------------------------
  79. void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
  80. const char* config)
  81. {
  82. // Construct the property configuration suffix.
  83. std::string suffix = "_";
  84. if(config && *config)
  85. {
  86. suffix += cmSystemTools::UpperCase(config);
  87. }
  88. else
  89. {
  90. suffix += "NOCONFIG";
  91. }
  92. // Generate the per-config target information.
  93. this->GenerateImportTargetsConfig(os, config, suffix);
  94. }
  95. //----------------------------------------------------------------------------
  96. void
  97. cmExportFileGenerator
  98. ::SetImportDetailProperties(const char* config, std::string const& suffix,
  99. cmTarget* target, ImportPropertyMap& properties)
  100. {
  101. // Get the makefile in which to lookup target information.
  102. cmMakefile* mf = target->GetMakefile();
  103. // Add the soname for unix shared libraries.
  104. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  105. target->GetType() == cmTarget::MODULE_LIBRARY)
  106. {
  107. // Check whether this is a DLL platform.
  108. bool dll_platform =
  109. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  110. if(!dll_platform)
  111. {
  112. std::string soname = target->GetSOName(config);
  113. std::string prop = "IMPORTED_SONAME";
  114. prop += suffix;
  115. properties[prop] = soname;
  116. }
  117. }
  118. // Add the transitive link dependencies for this configuration.
  119. if(target->GetType() == cmTarget::STATIC_LIBRARY ||
  120. target->GetType() == cmTarget::SHARED_LIBRARY)
  121. {
  122. this->SetImportLinkProperties(config, suffix, target, properties);
  123. }
  124. }
  125. //----------------------------------------------------------------------------
  126. void
  127. cmExportFileGenerator
  128. ::SetImportLinkProperties(const char* config, std::string const& suffix,
  129. cmTarget* target, ImportPropertyMap& properties)
  130. {
  131. // Get the makefile in which to lookup target information.
  132. cmMakefile* mf = target->GetMakefile();
  133. // Compute which library configuration to link.
  134. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  135. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  136. {
  137. linkType = cmTarget::DEBUG;
  138. }
  139. // Construct the property value.
  140. cmTarget::LinkLibraryVectorType const& libs =
  141. target->GetOriginalLinkLibraries();
  142. std::string link_libs;
  143. const char* sep = "";
  144. for(cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
  145. li != libs.end(); ++li)
  146. {
  147. // Skip entries that will resolve to the target itself, are empty,
  148. // or are not meant for this configuration.
  149. if(li->first == target->GetName() || li->first.empty() ||
  150. !(li->second == cmTarget::GENERAL || li->second == linkType))
  151. {
  152. continue;
  153. }
  154. // Separate this from the previous entry.
  155. link_libs += sep;
  156. sep = ";";
  157. // Append this entry.
  158. if(cmTarget* tgt = mf->FindTargetToUse(li->first.c_str()))
  159. {
  160. // This is a target. Make sure it is included in the export.
  161. if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
  162. {
  163. // The target is in the export. Append it with the export
  164. // namespace.
  165. link_libs += this->Namespace;
  166. link_libs += li->first;
  167. }
  168. else
  169. {
  170. // The target is not in the export. This is probably
  171. // user-error. Warn but add it anyway.
  172. this->ComplainAboutMissingTarget(target, li->first.c_str());
  173. link_libs += li->first;
  174. }
  175. }
  176. else
  177. {
  178. // Append the raw name.
  179. link_libs += li->first;
  180. }
  181. }
  182. // Store the property.
  183. std::string prop = "IMPORTED_LINK_LIBRARIES";
  184. prop += suffix;
  185. properties[prop] = link_libs;
  186. }
  187. //----------------------------------------------------------------------------
  188. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  189. const char* config)
  190. {
  191. os << "#----------------------------------------------------------------\n"
  192. << "# Generated CMake target import file";
  193. if(config)
  194. {
  195. os << " for configuration \"" << config << "\".\n";
  196. }
  197. else
  198. {
  199. os << ".\n";
  200. }
  201. os << "#----------------------------------------------------------------\n"
  202. << "\n";
  203. this->GenerateImportVersionCode(os);
  204. }
  205. //----------------------------------------------------------------------------
  206. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  207. {
  208. os << "# Commands beyond this point should not need to know the version.\n"
  209. << "SET(CMAKE_IMPORT_FILE_VERSION)\n";
  210. }
  211. //----------------------------------------------------------------------------
  212. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  213. {
  214. // Store an import file format version. This will let us change the
  215. // format later while still allowing old import files to work.
  216. os << "# Commands may need to know the format version.\n"
  217. << "SET(CMAKE_IMPORT_FILE_VERSION 1)\n"
  218. << "\n";
  219. }
  220. //----------------------------------------------------------------------------
  221. void
  222. cmExportFileGenerator
  223. ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
  224. {
  225. // Construct the imported target name.
  226. std::string targetName = this->Namespace;
  227. targetName += target->GetName();
  228. // Create the imported target.
  229. os << "# Create imported target " << targetName << "\n";
  230. switch(target->GetType())
  231. {
  232. case cmTarget::EXECUTABLE:
  233. os << "ADD_EXECUTABLE(" << targetName << " IMPORTED)\n";
  234. break;
  235. case cmTarget::STATIC_LIBRARY:
  236. os << "ADD_LIBRARY(" << targetName << " STATIC IMPORTED)\n";
  237. break;
  238. case cmTarget::SHARED_LIBRARY:
  239. os << "ADD_LIBRARY(" << targetName << " SHARED IMPORTED)\n";
  240. break;
  241. case cmTarget::MODULE_LIBRARY:
  242. os << "ADD_LIBRARY(" << targetName << " MODULE IMPORTED)\n";
  243. break;
  244. default: // should never happen
  245. break;
  246. }
  247. // Mark the imported executable if it has exports.
  248. if(target->IsExecutableWithExports())
  249. {
  250. os << "SET_PROPERTY(TARGET " << targetName
  251. << " PROPERTY ENABLE_EXPORTS 1)\n";
  252. }
  253. // Mark the imported framework. This is done even on non-Apple
  254. // platforms for reference and consistency purposes.
  255. if(target->GetType() == cmTarget::SHARED_LIBRARY &&
  256. target->GetPropertyAsBool("FRAMEWORK"))
  257. {
  258. os << "SET_PROPERTY(TARGET " << targetName
  259. << " PROPERTY FRAMEWORK 1)\n";
  260. }
  261. os << "\n";
  262. }
  263. //----------------------------------------------------------------------------
  264. void
  265. cmExportFileGenerator
  266. ::GenerateImportPropertyCode(std::ostream& os, const char* config,
  267. cmTarget* target,
  268. ImportPropertyMap const& properties)
  269. {
  270. // Construct the imported target name.
  271. std::string targetName = this->Namespace;
  272. targetName += target->GetName();
  273. // Set the import properties.
  274. os << "# Import target \"" << targetName << "\" for configuration \""
  275. << config << "\"\n";
  276. os << "SET_PROPERTY(TARGET " << targetName
  277. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  278. if(config && *config)
  279. {
  280. os << cmSystemTools::UpperCase(config);
  281. }
  282. else
  283. {
  284. os << "NOCONFIG";
  285. }
  286. os << ")\n";
  287. os << "SET_TARGET_PROPERTIES(" << targetName << " PROPERTIES\n";
  288. for(ImportPropertyMap::const_iterator pi = properties.begin();
  289. pi != properties.end(); ++pi)
  290. {
  291. os << " " << pi->first << " \"" << pi->second << "\"\n";
  292. }
  293. os << " )\n"
  294. << "\n";
  295. }