ENH: keep cleaning up Tcl/Tk modules
[cmake.git] / Source / cmExportCommand.cxx
blob88090fae6bd421e8a60988e9b250d83060a09bfc
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmExportCommand.cxx,v $
5 Language: C++
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"
21 #include "cmake.h"
23 #include <cmsys/auto_ptr.hxx>
25 cmExportCommand::cmExportCommand()
26 :cmCommand()
27 ,ArgumentGroup()
28 ,Targets(&Helper, "TARGETS")
29 ,Append(&Helper, "APPEND", &ArgumentGroup)
30 ,Prefix(&Helper, "PREFIX", &ArgumentGroup)
31 ,Filename(&Helper, "FILE", &ArgumentGroup)
33 // at first TARGETS
34 this->Targets.Follows(0);
35 // and after that the other options in any order
36 this->ArgumentGroup.Follows(&this->Targets);
40 // cmExportCommand
41 bool cmExportCommand
42 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
44 if(args.size() < 2 )
46 this->SetError("called with too few arguments");
47 return false;
50 std::vector<std::string> unknownArgs;
51 this->Helper.Parse(&args, &unknownArgs);
53 if (!unknownArgs.empty())
55 this->SetError("Unknown arguments.");
56 cmSystemTools::SetFatalErrorOccured();
57 return false;
60 if (this->Targets.WasFound() == false)
62 this->SetError("TARGETS option missing.");
63 cmSystemTools::SetFatalErrorOccured();
64 return false;
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();
74 return false;
77 if((this->Targets.GetVector().empty())||(this->Filename.GetString().empty()))
79 return true;
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));
88 foutPtr = ap;
90 else
92 cmsys::auto_ptr<cmGeneratedFileStream> ap(
93 new cmGeneratedFileStream(this->Filename.GetString().c_str(), true));
94 ap->SetCopyIfDifferent(true);
95 foutPtr = ap;
97 std::ostream& fout = *foutPtr.get();
99 if (!fout)
101 cmSystemTools::Error("Error Writing ", this->Filename.GetString().c_str());
102 cmSystemTools::ReportLastSystemError("");
103 return true;
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");
117 if (config!=0)
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();
126 ++currentTarget)
128 cmTarget* target = this->Makefile->GetLocalGenerator()->
129 GetGlobalGenerator()->FindTarget(0, currentTarget->c_str(), true);
130 if (target == 0)
132 std::string e = "detected unknown target: " + *currentTarget;
133 this->SetError(e.c_str());
134 cmSystemTools::SetFatalErrorOccured();
135 return false;
139 for(std::vector<std::string>::const_iterator
140 currentTarget = this->Targets.GetVector().begin();
141 currentTarget != this->Targets.GetVector().end();
142 ++currentTarget)
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);
148 if ((target != 0)
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()
159 << " IMPORT )\n";
160 break;
161 case cmTarget::STATIC_LIBRARY:
162 fout << "ADD_LIBRARY("
163 << this->Prefix.GetString().c_str() << currentTarget->c_str()
164 << " STATIC IMPORT )\n";
165 break;
166 case cmTarget::SHARED_LIBRARY:
167 fout << "ADD_LIBRARY("
168 << this->Prefix.GetString().c_str() << currentTarget->c_str()
169 << " SHARED IMPORT )\n";
170 break;
171 case cmTarget::MODULE_LIBRARY:
172 fout << "ADD_LIBRARY("
173 << this->Prefix.GetString().c_str() << currentTarget->c_str()
174 << " MODULE IMPORT )\n";
175 break;
176 default: // should never happen
177 break;
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();
186 ++currentConfig)
188 if (!currentConfig->empty())
190 const char* loc = target->GetLocation(currentConfig->c_str());
191 if (loc && *loc)
193 fout << " " << currentConfig->c_str()
194 << "_LOCATION \"" << loc << "\"\n";
198 fout << " )\n\n";
202 return true;