1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmExportCommand.cxx,v $
6 Date: $Date: 2008-01-23 15:27:59 $
7 Version: $Revision: 1.7 $
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 "cmExportCommand.h"
18 #include "cmGlobalGenerator.h"
19 #include "cmLocalGenerator.h"
20 #include "cmGeneratedFileStream.h"
23 #include <cmsys/auto_ptr.hxx>
25 cmExportCommand::cmExportCommand()
28 ,Targets(&Helper
, "TARGETS")
29 ,Append(&Helper
, "APPEND", &ArgumentGroup
)
30 ,Prefix(&Helper
, "PREFIX", &ArgumentGroup
)
31 ,Filename(&Helper
, "FILE", &ArgumentGroup
)
34 this->Targets
.Follows(0);
35 // and after that the other options in any order
36 this->ArgumentGroup
.Follows(&this->Targets
);
42 ::InitialPass(std::vector
<std::string
> const& args
, cmExecutionStatus
&)
46 this->SetError("called with too few arguments");
50 std::vector
<std::string
> unknownArgs
;
51 this->Helper
.Parse(&args
, &unknownArgs
);
53 if (!unknownArgs
.empty())
55 this->SetError("Unknown arguments.");
56 cmSystemTools::SetFatalErrorOccured();
60 if (this->Targets
.WasFound() == false)
62 this->SetError("TARGETS option missing.");
63 cmSystemTools::SetFatalErrorOccured();
68 if ( !this->Makefile
->CanIWriteThisFile(this->Filename
.GetString().c_str()) )
70 std::string e
= "attempted to write a file: " + this->Filename
.GetString()
71 + " into a source directory.";
72 this->SetError(e
.c_str());
73 cmSystemTools::SetFatalErrorOccured();
77 if((this->Targets
.GetVector().empty())||(this->Filename
.GetString().empty()))
82 // Use copy-if-different if not appending.
83 cmsys::auto_ptr
<std::ofstream
> foutPtr
;
84 if(this->Append
.IsEnabled())
86 cmsys::auto_ptr
<std::ofstream
> ap(
87 new std::ofstream(this->Filename
.GetString().c_str(), std::ios::app
));
92 cmsys::auto_ptr
<cmGeneratedFileStream
> ap(
93 new cmGeneratedFileStream(this->Filename
.GetString().c_str(), true));
94 ap
->SetCopyIfDifferent(true);
97 std::ostream
& fout
= *foutPtr
.get();
101 cmSystemTools::Error("Error Writing ", this->Filename
.GetString().c_str());
102 cmSystemTools::ReportLastSystemError("");
106 // the following code may move into an "export generator"
107 // Compute the set of configurations.
108 std::vector
<std::string
> configurationTypes
;
109 if(const char* types
=
110 this->Makefile
->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
112 cmSystemTools::ExpandListArgument(types
, configurationTypes
);
114 if(configurationTypes
.empty())
116 const char* config
= this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE");
119 configurationTypes
.push_back(config
);
123 for(std::vector
<std::string
>::const_iterator
124 currentTarget
= this->Targets
.GetVector().begin();
125 currentTarget
!= this->Targets
.GetVector().end();
128 cmTarget
* target
= this->Makefile
->GetLocalGenerator()->
129 GetGlobalGenerator()->FindTarget(0, currentTarget
->c_str(), true);
132 std::string e
= "detected unknown target: " + *currentTarget
;
133 this->SetError(e
.c_str());
134 cmSystemTools::SetFatalErrorOccured();
139 for(std::vector
<std::string
>::const_iterator
140 currentTarget
= this->Targets
.GetVector().begin();
141 currentTarget
!= this->Targets
.GetVector().end();
144 // Look for a CMake target with the given name, which is an executable
145 // and which can be run
146 cmTarget
* target
= this->Makefile
->GetLocalGenerator()->
147 GetGlobalGenerator()->FindTarget(0, currentTarget
->c_str(), true);
149 && ((target
->GetType() == cmTarget::EXECUTABLE
)
150 || (target
->GetType() == cmTarget::STATIC_LIBRARY
)
151 || (target
->GetType() == cmTarget::SHARED_LIBRARY
)
152 || (target
->GetType() == cmTarget::MODULE_LIBRARY
)))
154 switch (target
->GetType())
156 case cmTarget::EXECUTABLE
:
157 fout
<< "ADD_EXECUTABLE("
158 << this->Prefix
.GetString().c_str() << currentTarget
->c_str()
161 case cmTarget::STATIC_LIBRARY
:
162 fout
<< "ADD_LIBRARY("
163 << this->Prefix
.GetString().c_str() << currentTarget
->c_str()
164 << " STATIC IMPORT )\n";
166 case cmTarget::SHARED_LIBRARY
:
167 fout
<< "ADD_LIBRARY("
168 << this->Prefix
.GetString().c_str() << currentTarget
->c_str()
169 << " SHARED IMPORT )\n";
171 case cmTarget::MODULE_LIBRARY
:
172 fout
<< "ADD_LIBRARY("
173 << this->Prefix
.GetString().c_str() << currentTarget
->c_str()
174 << " MODULE IMPORT )\n";
176 default: // should never happen
180 fout
<< "SET_TARGET_PROPERTIES(" << this->Prefix
.GetString().c_str()
181 << currentTarget
->c_str() << " PROPERTIES \n"
182 <<" LOCATION \""<< target
->GetLocation(0)<<"\"\n";
183 for(std::vector
<std::string
>::const_iterator
184 currentConfig
= configurationTypes
.begin();
185 currentConfig
!= configurationTypes
.end();
188 if (!currentConfig
->empty())
190 const char* loc
= target
->GetLocation(currentConfig
->c_str());
193 fout
<< " " << currentConfig
->c_str()
194 << "_LOCATION \"" << loc
<< "\"\n";