Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmExportCommand.cxx
blob4b2e36d8ecf099ab7c13c663ae6446decd8ee7ff
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmExportCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/30 22:25:52 $
7 Version: $Revision: 1.10 $
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 "cmExportBuildFileGenerator.h"
25 cmExportCommand::cmExportCommand()
26 :cmCommand()
27 ,ArgumentGroup()
28 ,Targets(&Helper, "TARGETS")
29 ,Append(&Helper, "APPEND", &ArgumentGroup)
30 ,Namespace(&Helper, "NAMESPACE", &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 return false;
59 if (this->Targets.WasFound() == false)
61 this->SetError("TARGETS option missing.");
62 return false;
65 if(!this->Filename.WasFound())
67 this->SetError("FILE <filename> option missing.");
68 return false;
71 // Make sure the file has a .cmake extension.
72 if(cmSystemTools::GetFilenameLastExtension(this->Filename.GetCString())
73 != ".cmake")
75 cmOStringStream e;
76 e << "FILE option given filename \"" << this->Filename.GetString()
77 << "\" which does not have an extension of \".cmake\".\n";
78 this->SetError(e.str().c_str());
79 return false;
82 // Get the file to write.
83 std::string fname = this->Filename.GetString();
84 if(cmSystemTools::FileIsFullPath(fname.c_str()))
86 if(!this->Makefile->CanIWriteThisFile(fname.c_str()))
88 cmOStringStream e;
89 e << "FILE option given filename \"" << fname
90 << "\" which is in the source tree.\n";
91 this->SetError(e.str().c_str());
92 return false;
95 else
97 // Interpret relative paths with respect to the current build dir.
98 fname = this->Makefile->GetCurrentOutputDirectory();
99 fname += "/";
100 fname += this->Filename.GetString();
103 // Collect the targets to be exported.
104 std::vector<cmTarget*> targets;
105 for(std::vector<std::string>::const_iterator
106 currentTarget = this->Targets.GetVector().begin();
107 currentTarget != this->Targets.GetVector().end();
108 ++currentTarget)
110 if(cmTarget* target =
111 this->Makefile->GetLocalGenerator()->
112 GetGlobalGenerator()->FindTarget(0, currentTarget->c_str()))
114 if((target->GetType() == cmTarget::EXECUTABLE) ||
115 (target->GetType() == cmTarget::STATIC_LIBRARY) ||
116 (target->GetType() == cmTarget::SHARED_LIBRARY) ||
117 (target->GetType() == cmTarget::MODULE_LIBRARY))
119 targets.push_back(target);
121 else
123 cmOStringStream e;
124 e << "given target \"" << *currentTarget
125 << "\" which is not an executable or library.";
126 this->SetError(e.str().c_str());
127 return false;
130 else
132 cmOStringStream e;
133 e << "given target \"" << *currentTarget
134 << "\" which is not built by this project.";
135 this->SetError(e.str().c_str());
136 return false;
140 // Setup export file generation.
141 cmExportBuildFileGenerator ebfg;
142 ebfg.SetExportFile(fname.c_str());
143 ebfg.SetNamespace(this->Namespace.GetCString());
144 ebfg.SetAppendMode(this->Append.IsEnabled());
145 ebfg.SetExports(&targets);
146 ebfg.SetCommand(this);
148 // Compute the set of configurations exported.
149 if(const char* types =
150 this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
152 std::vector<std::string> configurationTypes;
153 cmSystemTools::ExpandListArgument(types, configurationTypes);
154 for(std::vector<std::string>::const_iterator
155 ci = configurationTypes.begin();
156 ci != configurationTypes.end(); ++ci)
158 ebfg.AddConfiguration(ci->c_str());
161 else if(const char* config =
162 this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
164 ebfg.AddConfiguration(config);
166 else
168 ebfg.AddConfiguration("");
171 // Generate the import file.
172 if(!ebfg.GenerateImportFile())
174 this->SetError("could not write export file.");
175 return false;
178 // Report generated error message if any.
179 if(!this->ErrorMessage.empty())
181 this->SetError(this->ErrorMessage.c_str());
182 return false;
185 return true;