1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmExportFileGenerator.cxx,v $
6 Date: $Date: 2009-09-01 14:37:36 $
7 Version: $Revision: 1.19 $
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"
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
;
44 cmSystemTools::GetFilenamePath(this->MainImportFile
);
46 cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile
);
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
;
59 cmsys::auto_ptr
<std::ofstream
>
60 ap(new std::ofstream(this->MainImportFile
.c_str(), std::ios::app
));
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);
71 if(!foutPtr
.get() || !*foutPtr
)
73 std::string se
= cmSystemTools::GetLastSystemError();
75 e
<< "cannot write to file \"" << this->MainImportFile
77 cmSystemTools::Error(e
.str().c_str());
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";
108 //----------------------------------------------------------------------------
109 void cmExportFileGenerator::GenerateImportConfig(std::ostream
& os
,
112 // Construct the property configuration suffix.
113 std::string suffix
= "_";
114 if(config
&& *config
)
116 suffix
+= cmSystemTools::UpperCase(config
);
120 suffix
+= "NOCONFIG";
123 // Generate the per-config target information.
124 this->GenerateImportTargetsConfig(os
, config
, suffix
);
127 //----------------------------------------------------------------------------
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.
142 (mf
->IsOn("WIN32") || mf
->IsOn("CYGWIN") || mf
->IsOn("MINGW"));
145 std::string soname
= target
->GetSOName(config
);
146 std::string prop
= "IMPORTED_SONAME";
148 properties
[prop
] = soname
;
152 // Add the transitive link dependencies for this configuration.
153 if(cmTarget::LinkInterface
const* iface
= target
->GetLinkInterface(config
))
155 this->SetImportLinkProperty(suffix
, target
,
156 "IMPORTED_LINK_INTERFACE_LANGUAGES",
157 iface
->Languages
, properties
);
158 this->SetImportLinkProperty(suffix
, target
,
159 "IMPORTED_LINK_INTERFACE_LIBRARIES",
160 iface
->Libraries
, properties
);
161 this->SetImportLinkProperty(suffix
, target
,
162 "IMPORTED_LINK_DEPENDENT_LIBRARIES",
163 iface
->SharedDeps
, properties
);
164 if(iface
->Multiplicity
> 0)
166 std::string prop
= "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
169 m
<< iface
->Multiplicity
;
170 properties
[prop
] = m
.str();
175 //----------------------------------------------------------------------------
177 cmExportFileGenerator
178 ::SetImportLinkProperty(std::string
const& suffix
,
180 const char* propName
,
181 std::vector
<std::string
> const& libs
,
182 ImportPropertyMap
& properties
)
184 // Skip the property if there are no libraries.
190 // Get the makefile in which to lookup target information.
191 cmMakefile
* mf
= target
->GetMakefile();
193 // Construct the property value.
194 std::string link_libs
;
195 const char* sep
= "";
196 for(std::vector
<std::string
>::const_iterator li
= libs
.begin();
197 li
!= libs
.end(); ++li
)
199 // Separate this from the previous entry.
203 // Append this entry.
204 if(cmTarget
* tgt
= mf
->FindTargetToUse(li
->c_str()))
207 if(tgt
->IsImported())
209 // The target is imported (and therefore is not in the
210 // export). Append the raw name.
213 else if(this->ExportedTargets
.find(tgt
) != this->ExportedTargets
.end())
215 // The target is in the export. Append it with the export
217 link_libs
+= this->Namespace
;
222 // The target is not in the export.
223 if(!this->AppendMode
)
225 // We are not appending, so all exported targets should be
226 // known here. This is probably user-error.
227 this->ComplainAboutMissingTarget(target
, tgt
);
229 // Assume the target will be exported by another command.
230 // Append it with the export namespace.
231 link_libs
+= this->Namespace
;
237 // Append the raw name.
242 // Store the property.
243 std::string prop
= propName
;
245 properties
[prop
] = link_libs
;
248 //----------------------------------------------------------------------------
249 void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream
& os
,
252 os
<< "#----------------------------------------------------------------\n"
253 << "# Generated CMake target import file";
256 os
<< " for configuration \"" << config
<< "\".\n";
262 os
<< "#----------------------------------------------------------------\n"
264 this->GenerateImportVersionCode(os
);
267 //----------------------------------------------------------------------------
268 void cmExportFileGenerator::GenerateImportFooterCode(std::ostream
& os
)
270 os
<< "# Commands beyond this point should not need to know the version.\n"
271 << "SET(CMAKE_IMPORT_FILE_VERSION)\n";
274 //----------------------------------------------------------------------------
275 void cmExportFileGenerator::GenerateImportVersionCode(std::ostream
& os
)
277 // Store an import file format version. This will let us change the
278 // format later while still allowing old import files to work.
279 os
<< "# Commands may need to know the format version.\n"
280 << "SET(CMAKE_IMPORT_FILE_VERSION 1)\n"
284 //----------------------------------------------------------------------------
286 cmExportFileGenerator
287 ::GenerateImportTargetCode(std::ostream
& os
, cmTarget
* target
)
289 // Construct the imported target name.
290 std::string targetName
= this->Namespace
;
291 targetName
+= target
->GetName();
293 // Create the imported target.
294 os
<< "# Create imported target " << targetName
<< "\n";
295 switch(target
->GetType())
297 case cmTarget::EXECUTABLE
:
298 os
<< "ADD_EXECUTABLE(" << targetName
<< " IMPORTED)\n";
300 case cmTarget::STATIC_LIBRARY
:
301 os
<< "ADD_LIBRARY(" << targetName
<< " STATIC IMPORTED)\n";
303 case cmTarget::SHARED_LIBRARY
:
304 os
<< "ADD_LIBRARY(" << targetName
<< " SHARED IMPORTED)\n";
306 case cmTarget::MODULE_LIBRARY
:
307 os
<< "ADD_LIBRARY(" << targetName
<< " MODULE IMPORTED)\n";
309 default: // should never happen
313 // Mark the imported executable if it has exports.
314 if(target
->IsExecutableWithExports())
316 os
<< "SET_PROPERTY(TARGET " << targetName
317 << " PROPERTY ENABLE_EXPORTS 1)\n";
320 // Mark the imported library if it is a framework.
321 if(target
->IsFrameworkOnApple())
323 os
<< "SET_PROPERTY(TARGET " << targetName
324 << " PROPERTY FRAMEWORK 1)\n";
327 // Mark the imported executable if it is an application bundle.
328 if(target
->IsAppBundleOnApple())
330 os
<< "SET_PROPERTY(TARGET " << targetName
331 << " PROPERTY MACOSX_BUNDLE 1)\n";
336 //----------------------------------------------------------------------------
338 cmExportFileGenerator
339 ::GenerateImportPropertyCode(std::ostream
& os
, const char* config
,
341 ImportPropertyMap
const& properties
)
343 // Construct the imported target name.
344 std::string targetName
= this->Namespace
;
345 targetName
+= target
->GetName();
347 // Set the import properties.
348 os
<< "# Import target \"" << targetName
<< "\" for configuration \""
350 os
<< "SET_PROPERTY(TARGET " << targetName
351 << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
352 if(config
&& *config
)
354 os
<< cmSystemTools::UpperCase(config
);
361 os
<< "SET_TARGET_PROPERTIES(" << targetName
<< " PROPERTIES\n";
362 for(ImportPropertyMap::const_iterator pi
= properties
.begin();
363 pi
!= properties
.end(); ++pi
)
365 os
<< " " << pi
->first
<< " \"" << pi
->second
<< "\"\n";