STYLE: Nightly Date Stamp
[cmake.git] / Source / cmExportFileGenerator.cxx
blob346455415d8cf3cb6b00fb9bbaef1a077ea2fe4a
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmExportFileGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2008-09-04 21:34:24 $
7 Version: $Revision: 1.15 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmExportFileGenerator.h"
19 #include "cmGeneratedFileStream.h"
20 #include "cmMakefile.h"
21 #include "cmSystemTools.h"
22 #include "cmTarget.h"
23 #include "cmVersion.h"
25 #include <cmsys/auto_ptr.hxx>
27 //----------------------------------------------------------------------------
28 cmExportFileGenerator::cmExportFileGenerator()
30 this->AppendMode = false;
33 //----------------------------------------------------------------------------
34 void cmExportFileGenerator::AddConfiguration(const char* config)
36 this->Configurations.push_back(config);
39 //----------------------------------------------------------------------------
40 void cmExportFileGenerator::SetExportFile(const char* mainFile)
42 this->MainImportFile = mainFile;
43 this->FileDir =
44 cmSystemTools::GetFilenamePath(this->MainImportFile);
45 this->FileBase =
46 cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
47 this->FileExt =
48 cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
51 //----------------------------------------------------------------------------
52 bool cmExportFileGenerator::GenerateImportFile()
54 // Open the output file to generate it.
55 cmsys::auto_ptr<std::ofstream> foutPtr;
56 if(this->AppendMode)
58 // Open for append.
59 cmsys::auto_ptr<std::ofstream>
60 ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app));
61 foutPtr = ap;
63 else
65 // Generate atomically and with copy-if-different.
66 cmsys::auto_ptr<cmGeneratedFileStream>
67 ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
68 ap->SetCopyIfDifferent(true);
69 foutPtr = ap;
71 if(!foutPtr.get() || !*foutPtr)
73 std::string se = cmSystemTools::GetLastSystemError();
74 cmOStringStream e;
75 e << "cannot write to file \"" << this->MainImportFile
76 << "\": " << se;
77 cmSystemTools::Error(e.str().c_str());
78 return false;
80 std::ostream& os = *foutPtr;
82 // Protect that file against use with older CMake versions.
83 os << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
84 os << "IF(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
85 << " MESSAGE(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
86 << "ENDIF(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n";
88 // Isolate the file policy level.
89 // We use 2.6 here instead of the current version because newer
90 // versions of CMake should be able to export files imported by 2.6
91 // until the import format changes.
92 os << "CMAKE_POLICY(PUSH)\n"
93 << "CMAKE_POLICY(VERSION 2.6)\n";
95 // Start with the import file header.
96 this->GenerateImportHeaderCode(os);
98 // Create all the imported targets.
99 bool result = this->GenerateMainFile(os);
101 // End with the import file footer.
102 this->GenerateImportFooterCode(os);
103 os << "CMAKE_POLICY(POP)\n";
105 return result;
108 //----------------------------------------------------------------------------
109 void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
110 const char* config)
112 // Construct the property configuration suffix.
113 std::string suffix = "_";
114 if(config && *config)
116 suffix += cmSystemTools::UpperCase(config);
118 else
120 suffix += "NOCONFIG";
123 // Generate the per-config target information.
124 this->GenerateImportTargetsConfig(os, config, suffix);
127 //----------------------------------------------------------------------------
128 void
129 cmExportFileGenerator
130 ::SetImportDetailProperties(const char* config, std::string const& suffix,
131 cmTarget* target, ImportPropertyMap& properties)
133 // Get the makefile in which to lookup target information.
134 cmMakefile* mf = target->GetMakefile();
136 // Add the soname for unix shared libraries.
137 if(target->GetType() == cmTarget::SHARED_LIBRARY ||
138 target->GetType() == cmTarget::MODULE_LIBRARY)
140 // Check whether this is a DLL platform.
141 bool dll_platform =
142 (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
143 if(!dll_platform)
145 std::string soname = target->GetSOName(config);
146 std::string prop = "IMPORTED_SONAME";
147 prop += suffix;
148 properties[prop] = soname;
152 // Add the transitive link dependencies for this configuration.
153 if(cmTargetLinkInterface const* iface =
154 target->GetLinkInterface(config))
156 // This target provides a link interface, so use it.
157 this->SetImportLinkProperty(suffix, target,
158 "IMPORTED_LINK_INTERFACE_LIBRARIES",
159 iface->Libraries, properties);
160 this->SetImportLinkProperty(suffix, target,
161 "IMPORTED_LINK_DEPENDENT_LIBRARIES",
162 iface->SharedDeps, properties);
164 else if(target->GetType() == cmTarget::STATIC_LIBRARY ||
165 target->GetType() == cmTarget::SHARED_LIBRARY)
167 // The default link interface for static and shared libraries is
168 // their link implementation library list.
169 this->SetImportLinkProperties(config, suffix, target, properties);
173 //----------------------------------------------------------------------------
174 void
175 cmExportFileGenerator
176 ::SetImportLinkProperties(const char* config, std::string const& suffix,
177 cmTarget* target, ImportPropertyMap& properties)
179 // Compute which library configuration to link.
180 cmTarget::LinkLibraryType linkType = target->ComputeLinkType(config);
182 // Construct the list of libs linked for this configuration.
183 std::vector<std::string> actual_libs;
184 cmTarget::LinkLibraryVectorType const& libs =
185 target->GetOriginalLinkLibraries();
186 for(cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
187 li != libs.end(); ++li)
189 // Skip entries that will resolve to the target itself, are empty,
190 // or are not meant for this configuration.
191 if(li->first == target->GetName() || li->first.empty() ||
192 !(li->second == cmTarget::GENERAL || li->second == linkType))
194 continue;
197 // Store this entry.
198 actual_libs.push_back(li->first);
201 // Store the entries in the property.
202 this->SetImportLinkProperty(suffix, target,
203 "IMPORTED_LINK_INTERFACE_LIBRARIES",
204 actual_libs, properties);
207 //----------------------------------------------------------------------------
208 void
209 cmExportFileGenerator
210 ::SetImportLinkProperty(std::string const& suffix,
211 cmTarget* target,
212 const char* propName,
213 std::vector<std::string> const& libs,
214 ImportPropertyMap& properties)
216 // Skip the property if there are no libraries.
217 if(libs.empty())
219 return;
222 // Get the makefile in which to lookup target information.
223 cmMakefile* mf = target->GetMakefile();
225 // Construct the property value.
226 std::string link_libs;
227 const char* sep = "";
228 for(std::vector<std::string>::const_iterator li = libs.begin();
229 li != libs.end(); ++li)
231 // Separate this from the previous entry.
232 link_libs += sep;
233 sep = ";";
235 // Append this entry.
236 if(cmTarget* tgt = mf->FindTargetToUse(li->c_str()))
238 // This is a target.
239 if(tgt->IsImported())
241 // The target is imported (and therefore is not in the
242 // export). Append the raw name.
243 link_libs += *li;
245 else if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
247 // The target is in the export. Append it with the export
248 // namespace.
249 link_libs += this->Namespace;
250 link_libs += *li;
252 else
254 // The target is not in the export.
255 if(!this->AppendMode)
257 // We are not appending, so all exported targets should be
258 // known here. This is probably user-error.
259 this->ComplainAboutMissingTarget(target, tgt);
261 // Assume the target will be exported by another command.
262 // Append it with the export namespace.
263 link_libs += this->Namespace;
264 link_libs += *li;
267 else
269 // Append the raw name.
270 link_libs += *li;
274 // Store the property.
275 std::string prop = propName;
276 prop += suffix;
277 properties[prop] = link_libs;
280 //----------------------------------------------------------------------------
281 void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
282 const char* config)
284 os << "#----------------------------------------------------------------\n"
285 << "# Generated CMake target import file";
286 if(config)
288 os << " for configuration \"" << config << "\".\n";
290 else
292 os << ".\n";
294 os << "#----------------------------------------------------------------\n"
295 << "\n";
296 this->GenerateImportVersionCode(os);
299 //----------------------------------------------------------------------------
300 void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
302 os << "# Commands beyond this point should not need to know the version.\n"
303 << "SET(CMAKE_IMPORT_FILE_VERSION)\n";
306 //----------------------------------------------------------------------------
307 void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
309 // Store an import file format version. This will let us change the
310 // format later while still allowing old import files to work.
311 os << "# Commands may need to know the format version.\n"
312 << "SET(CMAKE_IMPORT_FILE_VERSION 1)\n"
313 << "\n";
316 //----------------------------------------------------------------------------
317 void
318 cmExportFileGenerator
319 ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
321 // Construct the imported target name.
322 std::string targetName = this->Namespace;
323 targetName += target->GetName();
325 // Create the imported target.
326 os << "# Create imported target " << targetName << "\n";
327 switch(target->GetType())
329 case cmTarget::EXECUTABLE:
330 os << "ADD_EXECUTABLE(" << targetName << " IMPORTED)\n";
331 break;
332 case cmTarget::STATIC_LIBRARY:
333 os << "ADD_LIBRARY(" << targetName << " STATIC IMPORTED)\n";
334 break;
335 case cmTarget::SHARED_LIBRARY:
336 os << "ADD_LIBRARY(" << targetName << " SHARED IMPORTED)\n";
337 break;
338 case cmTarget::MODULE_LIBRARY:
339 os << "ADD_LIBRARY(" << targetName << " MODULE IMPORTED)\n";
340 break;
341 default: // should never happen
342 break;
345 // Mark the imported executable if it has exports.
346 if(target->IsExecutableWithExports())
348 os << "SET_PROPERTY(TARGET " << targetName
349 << " PROPERTY ENABLE_EXPORTS 1)\n";
352 // Mark the imported library if it is a framework.
353 if(target->IsFrameworkOnApple())
355 os << "SET_PROPERTY(TARGET " << targetName
356 << " PROPERTY FRAMEWORK 1)\n";
359 // Mark the imported executable if it is an application bundle.
360 if(target->IsAppBundleOnApple())
362 os << "SET_PROPERTY(TARGET " << targetName
363 << " PROPERTY MACOSX_BUNDLE 1)\n";
365 os << "\n";
368 //----------------------------------------------------------------------------
369 void
370 cmExportFileGenerator
371 ::GenerateImportPropertyCode(std::ostream& os, const char* config,
372 cmTarget* target,
373 ImportPropertyMap const& properties)
375 // Construct the imported target name.
376 std::string targetName = this->Namespace;
377 targetName += target->GetName();
379 // Set the import properties.
380 os << "# Import target \"" << targetName << "\" for configuration \""
381 << config << "\"\n";
382 os << "SET_PROPERTY(TARGET " << targetName
383 << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
384 if(config && *config)
386 os << cmSystemTools::UpperCase(config);
388 else
390 os << "NOCONFIG";
392 os << ")\n";
393 os << "SET_TARGET_PROPERTIES(" << targetName << " PROPERTIES\n";
394 for(ImportPropertyMap::const_iterator pi = properties.begin();
395 pi != properties.end(); ++pi)
397 os << " " << pi->first << " \"" << pi->second << "\"\n";
399 os << " )\n"
400 << "\n";