ENH: keep cleaning up Tcl/Tk modules
[cmake.git] / Source / cmAddCustomCommandCommand.cxx
blob9bc8bb951429e21bff9d42eb7dfa9e4bf7121195
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmAddCustomCommandCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-01-23 15:27:59 $
7 Version: $Revision: 1.36 $
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 "cmAddCustomCommandCommand.h"
19 #include "cmTarget.h"
21 #include "cmSourceFile.h"
23 // cmAddCustomCommandCommand
24 bool cmAddCustomCommandCommand
25 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
27 /* Let's complain at the end of this function about the lack of a particular
28 arg. For the moment, let's say that COMMAND, and either TARGET or SOURCE
29 are required.
31 if (args.size() < 4)
33 this->SetError("called with wrong number of arguments.");
34 return false;
37 std::string source, target, main_dependency, working;
38 std::string comment_buffer;
39 const char* comment = 0;
40 std::vector<std::string> depends, outputs, output;
41 bool verbatim = false;
42 bool append = false;
43 std::string implicit_depends_lang;
44 cmCustomCommand::ImplicitDependsList implicit_depends;
46 // Accumulate one command line at a time.
47 cmCustomCommandLine currentLine;
49 // Save all command lines.
50 cmCustomCommandLines commandLines;
52 cmTarget::CustomCommandType cctype = cmTarget::POST_BUILD;
54 enum tdoing {
55 doing_source,
56 doing_command,
57 doing_target,
58 doing_depends,
59 doing_implicit_depends_lang,
60 doing_implicit_depends_file,
61 doing_main_dependency,
62 doing_output,
63 doing_outputs,
64 doing_comment,
65 doing_working_directory,
66 doing_nothing
69 tdoing doing = doing_nothing;
71 for (unsigned int j = 0; j < args.size(); ++j)
73 std::string const& copy = args[j];
75 if(copy == "SOURCE")
77 doing = doing_source;
79 else if(copy == "COMMAND")
81 doing = doing_command;
83 // Save the current command before starting the next command.
84 if(!currentLine.empty())
86 commandLines.push_back(currentLine);
87 currentLine.clear();
90 else if(copy == "PRE_BUILD")
92 cctype = cmTarget::PRE_BUILD;
94 else if(copy == "PRE_LINK")
96 cctype = cmTarget::PRE_LINK;
98 else if(copy == "POST_BUILD")
100 cctype = cmTarget::POST_BUILD;
102 else if(copy == "VERBATIM")
104 verbatim = true;
106 else if(copy == "APPEND")
108 append = true;
110 else if(copy == "TARGET")
112 doing = doing_target;
114 else if(copy == "ARGS")
116 // Ignore this old keyword.
118 else if (copy == "DEPENDS")
120 doing = doing_depends;
122 else if (copy == "OUTPUTS")
124 doing = doing_outputs;
126 else if (copy == "OUTPUT")
128 doing = doing_output;
130 else if (copy == "WORKING_DIRECTORY")
132 doing = doing_working_directory;
134 else if (copy == "MAIN_DEPENDENCY")
136 doing = doing_main_dependency;
138 else if (copy == "IMPLICIT_DEPENDS")
140 doing = doing_implicit_depends_lang;
142 else if (copy == "COMMENT")
144 doing = doing_comment;
146 else
148 std::string filename;
149 switch (doing)
151 case doing_output:
152 case doing_outputs:
153 if (!cmSystemTools::FileIsFullPath(copy.c_str()))
155 filename = this->Makefile->GetStartDirectory();
156 filename += "/";
158 filename += copy;
159 break;
160 case doing_source:
161 // We do not want to convert the argument to SOURCE because
162 // that option is only available for backward compatibility.
163 // Old-style use of this command may use the SOURCE==TARGET
164 // trick which we must preserve. If we convert the source
165 // to a full path then it will no longer equal the target.
166 default:
167 break;
170 switch (doing)
172 case doing_working_directory:
173 working = copy;
174 break;
175 case doing_source:
176 source = copy;
177 break;
178 case doing_output:
179 output.push_back(filename);
180 break;
181 case doing_main_dependency:
182 main_dependency = copy;
183 break;
184 case doing_implicit_depends_lang:
185 implicit_depends_lang = copy;
186 doing = doing_implicit_depends_file;
187 break;
188 case doing_implicit_depends_file:
190 // An implicit dependency starting point is also an
191 // explicit dependency.
192 depends.push_back(copy);
194 // Add the implicit dependency language and file.
195 cmCustomCommand::ImplicitDependsPair
196 entry(implicit_depends_lang, copy);
197 implicit_depends.push_back(entry);
199 // Switch back to looking for a language.
200 doing = doing_implicit_depends_lang;
202 break;
203 case doing_command:
204 currentLine.push_back(copy);
205 break;
206 case doing_target:
207 target = copy;
208 break;
209 case doing_depends:
210 depends.push_back(copy);
211 break;
212 case doing_outputs:
213 outputs.push_back(filename);
214 break;
215 case doing_comment:
216 comment_buffer = copy;
217 comment = comment_buffer.c_str();
218 break;
219 default:
220 this->SetError("Wrong syntax. Unknown type of argument.");
221 return false;
226 // Store the last command line finished.
227 if(!currentLine.empty())
229 commandLines.push_back(currentLine);
230 currentLine.clear();
233 // At this point we could complain about the lack of arguments. For
234 // the moment, let's say that COMMAND, TARGET are always required.
235 if(output.empty() && target.empty())
237 this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
238 return false;
241 if(source.empty() && !target.empty() && !output.empty())
243 this->SetError(
244 "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
245 return false;
247 if(append && output.empty())
249 this->SetError("given APPEND option with no OUTPUT.");
250 return false;
253 // Make sure the output names and locations are safe.
254 if(!this->CheckOutputs(output) || !this->CheckOutputs(outputs))
256 return false;
259 // Check for an append request.
260 if(append)
262 // Lookup an existing command.
263 if(cmSourceFile* sf =
264 this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
266 if(cmCustomCommand* cc = sf->GetCustomCommand())
268 cc->AppendCommands(commandLines);
269 cc->AppendDepends(depends);
270 cc->AppendImplicitDepends(implicit_depends);
271 return true;
275 // No command for this output exists.
276 cmOStringStream e;
277 e << "given APPEND option with output \"" << output[0].c_str()
278 << "\" which is not already a custom command output.";
279 this->SetError(e.str().c_str());
280 return false;
283 // Choose which mode of the command to use.
284 bool escapeOldStyle = !verbatim;
285 if(source.empty() && output.empty())
287 // Source is empty, use the target.
288 std::vector<std::string> no_depends;
289 this->Makefile->AddCustomCommandToTarget(target.c_str(), no_depends,
290 commandLines, cctype,
291 comment, working.c_str(),
292 escapeOldStyle);
294 else if(target.empty())
296 // Target is empty, use the output.
297 this->Makefile->AddCustomCommandToOutput(output, depends,
298 main_dependency.c_str(),
299 commandLines, comment,
300 working.c_str(), false,
301 escapeOldStyle);
303 // Add implicit dependency scanning requests if any were given.
304 if(!implicit_depends.empty())
306 bool okay = false;
307 if(cmSourceFile* sf =
308 this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
310 if(cmCustomCommand* cc = sf->GetCustomCommand())
312 okay = true;
313 cc->SetImplicitDepends(implicit_depends);
316 if(!okay)
318 cmOStringStream e;
319 e << "could not locate source file with a custom command producing \""
320 << output[0] << "\" even though this command tried to create it!";
321 this->SetError(e.str().c_str());
322 return false;
326 else
328 // Use the old-style mode for backward compatibility.
329 this->Makefile->AddCustomCommandOldStyle(target.c_str(), outputs, depends,
330 source.c_str(), commandLines,
331 comment);
334 return true;
337 //----------------------------------------------------------------------------
338 bool
339 cmAddCustomCommandCommand
340 ::CheckOutputs(const std::vector<std::string>& outputs)
342 for(std::vector<std::string>::const_iterator o = outputs.begin();
343 o != outputs.end(); ++o)
345 // Make sure the file will not be generated into the source
346 // directory during an out of source build.
347 if(!this->Makefile->CanIWriteThisFile(o->c_str()))
349 std::string e = "attempted to have a file \"" + *o +
350 "\" in a source directory as an output of custom command.";
351 this->SetError(e.c_str());
352 cmSystemTools::SetFatalErrorOccured();
353 return false;
356 // Make sure the output file name has no invalid characters.
357 std::string::size_type pos = o->find_first_of("#<>");
358 if(pos != o->npos)
360 cmOStringStream msg;
361 msg << "called with OUTPUT containing a \"" << (*o)[pos]
362 << "\". This character is not allowed.";
363 this->SetError(msg.str().c_str());
364 return false;
367 return true;