1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmAddCustomCommandCommand.h"
16 #include "cmSourceFile.h"
18 // cmAddCustomCommandCommand
19 bool cmAddCustomCommandCommand
20 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
22 /* Let's complain at the end of this function about the lack of a particular
23 arg. For the moment, let's say that COMMAND, and either TARGET or SOURCE
28 this->SetError("called with wrong number of arguments.");
32 std::string source, target, main_dependency, working;
33 std::string comment_buffer;
34 const char* comment = 0;
35 std::vector<std::string> depends, outputs, output;
36 bool verbatim = false;
38 std::string implicit_depends_lang;
39 cmCustomCommand::ImplicitDependsList implicit_depends;
41 // Accumulate one command line at a time.
42 cmCustomCommandLine currentLine;
44 // Save all command lines.
45 cmCustomCommandLines commandLines;
47 cmTarget::CustomCommandType cctype = cmTarget::POST_BUILD;
54 doing_implicit_depends_lang,
55 doing_implicit_depends_file,
56 doing_main_dependency,
60 doing_working_directory,
64 tdoing doing = doing_nothing;
66 for (unsigned int j = 0; j < args.size(); ++j)
68 std::string const& copy = args[j];
74 else if(copy == "COMMAND")
76 doing = doing_command;
78 // Save the current command before starting the next command.
79 if(!currentLine.empty())
81 commandLines.push_back(currentLine);
85 else if(copy == "PRE_BUILD")
87 cctype = cmTarget::PRE_BUILD;
89 else if(copy == "PRE_LINK")
91 cctype = cmTarget::PRE_LINK;
93 else if(copy == "POST_BUILD")
95 cctype = cmTarget::POST_BUILD;
97 else if(copy == "VERBATIM")
101 else if(copy == "APPEND")
105 else if(copy == "TARGET")
107 doing = doing_target;
109 else if(copy == "ARGS")
111 // Ignore this old keyword.
113 else if (copy == "DEPENDS")
115 doing = doing_depends;
117 else if (copy == "OUTPUTS")
119 doing = doing_outputs;
121 else if (copy == "OUTPUT")
123 doing = doing_output;
125 else if (copy == "WORKING_DIRECTORY")
127 doing = doing_working_directory;
129 else if (copy == "MAIN_DEPENDENCY")
131 doing = doing_main_dependency;
133 else if (copy == "IMPLICIT_DEPENDS")
135 doing = doing_implicit_depends_lang;
137 else if (copy == "COMMENT")
139 doing = doing_comment;
143 std::string filename;
148 if (!cmSystemTools::FileIsFullPath(copy.c_str()))
150 // This is an output to be generated, so it should be
151 // under the build tree. CMake 2.4 placed this under the
152 // source tree. However the only case that this change
153 // will break is when someone writes
155 // add_custom_command(OUTPUT out.txt ...)
157 // and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
158 // This is fairly obscure so we can wait for someone to
160 filename = this->Makefile->GetCurrentOutputDirectory();
164 cmSystemTools::ConvertToUnixSlashes(filename);
167 // We do not want to convert the argument to SOURCE because
168 // that option is only available for backward compatibility.
169 // Old-style use of this command may use the SOURCE==TARGET
170 // trick which we must preserve. If we convert the source
171 // to a full path then it will no longer equal the target.
178 case doing_working_directory:
185 output.push_back(filename);
187 case doing_main_dependency:
188 main_dependency = copy;
190 case doing_implicit_depends_lang:
191 implicit_depends_lang = copy;
192 doing = doing_implicit_depends_file;
194 case doing_implicit_depends_file:
196 // An implicit dependency starting point is also an
197 // explicit dependency.
198 std::string dep = copy;
199 cmSystemTools::ConvertToUnixSlashes(dep);
200 depends.push_back(dep);
202 // Add the implicit dependency language and file.
203 cmCustomCommand::ImplicitDependsPair
204 entry(implicit_depends_lang, dep);
205 implicit_depends.push_back(entry);
207 // Switch back to looking for a language.
208 doing = doing_implicit_depends_lang;
212 currentLine.push_back(copy);
219 std::string dep = copy;
220 cmSystemTools::ConvertToUnixSlashes(dep);
221 depends.push_back(dep);
225 outputs.push_back(filename);
228 comment_buffer = copy;
229 comment = comment_buffer.c_str();
232 this->SetError("Wrong syntax. Unknown type of argument.");
238 // Store the last command line finished.
239 if(!currentLine.empty())
241 commandLines.push_back(currentLine);
245 // At this point we could complain about the lack of arguments. For
246 // the moment, let's say that COMMAND, TARGET are always required.
247 if(output.empty() && target.empty())
249 this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
253 if(source.empty() && !target.empty() && !output.empty())
256 "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
259 if(append && output.empty())
261 this->SetError("given APPEND option with no OUTPUT.");
265 // Make sure the output names and locations are safe.
266 if(!this->CheckOutputs(output) || !this->CheckOutputs(outputs))
271 // Check for an append request.
274 // Lookup an existing command.
275 if(cmSourceFile* sf =
276 this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
278 if(cmCustomCommand* cc = sf->GetCustomCommand())
280 cc->AppendCommands(commandLines);
281 cc->AppendDepends(depends);
282 cc->AppendImplicitDepends(implicit_depends);
287 // No command for this output exists.
289 e << "given APPEND option with output \"" << output[0].c_str()
290 << "\" which is not already a custom command output.";
291 this->SetError(e.str().c_str());
295 // Convert working directory to a full path.
298 const char* build_dir = this->Makefile->GetCurrentOutputDirectory();
299 working = cmSystemTools::CollapseFullPath(working.c_str(), build_dir);
302 // Choose which mode of the command to use.
303 bool escapeOldStyle = !verbatim;
304 if(source.empty() && output.empty())
306 // Source is empty, use the target.
307 std::vector<std::string> no_depends;
308 this->Makefile->AddCustomCommandToTarget(target.c_str(), no_depends,
309 commandLines, cctype,
310 comment, working.c_str(),
313 else if(target.empty())
315 // Target is empty, use the output.
316 this->Makefile->AddCustomCommandToOutput(output, depends,
317 main_dependency.c_str(),
318 commandLines, comment,
319 working.c_str(), false,
322 // Add implicit dependency scanning requests if any were given.
323 if(!implicit_depends.empty())
326 if(cmSourceFile* sf =
327 this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
329 if(cmCustomCommand* cc = sf->GetCustomCommand())
332 cc->SetImplicitDepends(implicit_depends);
338 e << "could not locate source file with a custom command producing \""
339 << output[0] << "\" even though this command tried to create it!";
340 this->SetError(e.str().c_str());
347 // Use the old-style mode for backward compatibility.
348 this->Makefile->AddCustomCommandOldStyle(target.c_str(), outputs, depends,
349 source.c_str(), commandLines,
356 //----------------------------------------------------------------------------
358 cmAddCustomCommandCommand
359 ::CheckOutputs(const std::vector<std::string>& outputs)
361 for(std::vector<std::string>::const_iterator o = outputs.begin();
362 o != outputs.end(); ++o)
364 // Make sure the file will not be generated into the source
365 // directory during an out of source build.
366 if(!this->Makefile->CanIWriteThisFile(o->c_str()))
368 std::string e = "attempted to have a file \"" + *o +
369 "\" in a source directory as an output of custom command.";
370 this->SetError(e.c_str());
371 cmSystemTools::SetFatalErrorOccured();
375 // Make sure the output file name has no invalid characters.
376 std::string::size_type pos = o->find_first_of("#<>");
380 msg << "called with OUTPUT containing a \"" << (*o)[pos]
381 << "\". This character is not allowed.";
382 this->SetError(msg.str().c_str());