BUG: fix some bad changes in progress calc
[cmake.git] / Source / cmAddCustomCommandCommand.cxx
blob04366bd41b1eff3c56d0b780cd88b4eb2157eb40
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmAddCustomCommandCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-01-30 16:22:10 $
7 Version: $Revision: 1.37 $
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 // This is an output to be generated, so it should be
156 // under the build tree. CMake 2.4 placed this under the
157 // source tree. However the only case that this change
158 // will break is when someone writes
160 // add_custom_command(OUTPUT out.txt ...)
162 // and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
163 // This is fairly obscure so we can wait for someone to
164 // complain.
165 filename = this->Makefile->GetCurrentOutputDirectory();
166 filename += "/";
168 filename += copy;
169 break;
170 case doing_source:
171 // We do not want to convert the argument to SOURCE because
172 // that option is only available for backward compatibility.
173 // Old-style use of this command may use the SOURCE==TARGET
174 // trick which we must preserve. If we convert the source
175 // to a full path then it will no longer equal the target.
176 default:
177 break;
180 switch (doing)
182 case doing_working_directory:
183 working = copy;
184 break;
185 case doing_source:
186 source = copy;
187 break;
188 case doing_output:
189 output.push_back(filename);
190 break;
191 case doing_main_dependency:
192 main_dependency = copy;
193 break;
194 case doing_implicit_depends_lang:
195 implicit_depends_lang = copy;
196 doing = doing_implicit_depends_file;
197 break;
198 case doing_implicit_depends_file:
200 // An implicit dependency starting point is also an
201 // explicit dependency.
202 depends.push_back(copy);
204 // Add the implicit dependency language and file.
205 cmCustomCommand::ImplicitDependsPair
206 entry(implicit_depends_lang, copy);
207 implicit_depends.push_back(entry);
209 // Switch back to looking for a language.
210 doing = doing_implicit_depends_lang;
212 break;
213 case doing_command:
214 currentLine.push_back(copy);
215 break;
216 case doing_target:
217 target = copy;
218 break;
219 case doing_depends:
220 depends.push_back(copy);
221 break;
222 case doing_outputs:
223 outputs.push_back(filename);
224 break;
225 case doing_comment:
226 comment_buffer = copy;
227 comment = comment_buffer.c_str();
228 break;
229 default:
230 this->SetError("Wrong syntax. Unknown type of argument.");
231 return false;
236 // Store the last command line finished.
237 if(!currentLine.empty())
239 commandLines.push_back(currentLine);
240 currentLine.clear();
243 // At this point we could complain about the lack of arguments. For
244 // the moment, let's say that COMMAND, TARGET are always required.
245 if(output.empty() && target.empty())
247 this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
248 return false;
251 if(source.empty() && !target.empty() && !output.empty())
253 this->SetError(
254 "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
255 return false;
257 if(append && output.empty())
259 this->SetError("given APPEND option with no OUTPUT.");
260 return false;
263 // Make sure the output names and locations are safe.
264 if(!this->CheckOutputs(output) || !this->CheckOutputs(outputs))
266 return false;
269 // Check for an append request.
270 if(append)
272 // Lookup an existing command.
273 if(cmSourceFile* sf =
274 this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
276 if(cmCustomCommand* cc = sf->GetCustomCommand())
278 cc->AppendCommands(commandLines);
279 cc->AppendDepends(depends);
280 cc->AppendImplicitDepends(implicit_depends);
281 return true;
285 // No command for this output exists.
286 cmOStringStream e;
287 e << "given APPEND option with output \"" << output[0].c_str()
288 << "\" which is not already a custom command output.";
289 this->SetError(e.str().c_str());
290 return false;
293 // Choose which mode of the command to use.
294 bool escapeOldStyle = !verbatim;
295 if(source.empty() && output.empty())
297 // Source is empty, use the target.
298 std::vector<std::string> no_depends;
299 this->Makefile->AddCustomCommandToTarget(target.c_str(), no_depends,
300 commandLines, cctype,
301 comment, working.c_str(),
302 escapeOldStyle);
304 else if(target.empty())
306 // Target is empty, use the output.
307 this->Makefile->AddCustomCommandToOutput(output, depends,
308 main_dependency.c_str(),
309 commandLines, comment,
310 working.c_str(), false,
311 escapeOldStyle);
313 // Add implicit dependency scanning requests if any were given.
314 if(!implicit_depends.empty())
316 bool okay = false;
317 if(cmSourceFile* sf =
318 this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
320 if(cmCustomCommand* cc = sf->GetCustomCommand())
322 okay = true;
323 cc->SetImplicitDepends(implicit_depends);
326 if(!okay)
328 cmOStringStream e;
329 e << "could not locate source file with a custom command producing \""
330 << output[0] << "\" even though this command tried to create it!";
331 this->SetError(e.str().c_str());
332 return false;
336 else
338 // Use the old-style mode for backward compatibility.
339 this->Makefile->AddCustomCommandOldStyle(target.c_str(), outputs, depends,
340 source.c_str(), commandLines,
341 comment);
344 return true;
347 //----------------------------------------------------------------------------
348 bool
349 cmAddCustomCommandCommand
350 ::CheckOutputs(const std::vector<std::string>& outputs)
352 for(std::vector<std::string>::const_iterator o = outputs.begin();
353 o != outputs.end(); ++o)
355 // Make sure the file will not be generated into the source
356 // directory during an out of source build.
357 if(!this->Makefile->CanIWriteThisFile(o->c_str()))
359 std::string e = "attempted to have a file \"" + *o +
360 "\" in a source directory as an output of custom command.";
361 this->SetError(e.c_str());
362 cmSystemTools::SetFatalErrorOccured();
363 return false;
366 // Make sure the output file name has no invalid characters.
367 std::string::size_type pos = o->find_first_of("#<>");
368 if(pos != o->npos)
370 cmOStringStream msg;
371 msg << "called with OUTPUT containing a \"" << (*o)[pos]
372 << "\". This character is not allowed.";
373 this->SetError(msg.str().c_str());
374 return false;
377 return true;