cmExportFileGenerator.cxx 13 KB

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