1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmAddCustomCommandCommand.cxx,v $
6 Date: $Date: 2008-06-02 20:45:06 $
7 Version: $Revision: 1.39 $
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"
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
33 this->SetError("called with wrong number of arguments.");
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;
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
;
59 doing_implicit_depends_lang
,
60 doing_implicit_depends_file
,
61 doing_main_dependency
,
65 doing_working_directory
,
69 tdoing doing
= doing_nothing
;
71 for (unsigned int j
= 0; j
< args
.size(); ++j
)
73 std::string
const& copy
= args
[j
];
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
);
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")
106 else if(copy
== "APPEND")
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
;
148 std::string filename
;
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
165 filename
= this->Makefile
->GetCurrentOutputDirectory();
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.
182 case doing_working_directory
:
189 output
.push_back(filename
);
191 case doing_main_dependency
:
192 main_dependency
= copy
;
194 case doing_implicit_depends_lang
:
195 implicit_depends_lang
= copy
;
196 doing
= doing_implicit_depends_file
;
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
;
214 currentLine
.push_back(copy
);
220 depends
.push_back(copy
);
223 outputs
.push_back(filename
);
226 comment_buffer
= copy
;
227 comment
= comment_buffer
.c_str();
230 this->SetError("Wrong syntax. Unknown type of argument.");
236 // Store the last command line finished.
237 if(!currentLine
.empty())
239 commandLines
.push_back(currentLine
);
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.");
251 if(source
.empty() && !target
.empty() && !output
.empty())
254 "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
257 if(append
&& output
.empty())
259 this->SetError("given APPEND option with no OUTPUT.");
263 // Make sure the output names and locations are safe.
264 if(!this->CheckOutputs(output
) || !this->CheckOutputs(outputs
))
269 // Check for an append request.
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
);
285 // No command for this output exists.
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());
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(),
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,
313 // Add implicit dependency scanning requests if any were given.
314 if(!implicit_depends
.empty())
317 if(cmSourceFile
* sf
=
318 this->Makefile
->GetSourceFileWithOutput(output
[0].c_str()))
320 if(cmCustomCommand
* cc
= sf
->GetCustomCommand())
323 cc
->SetImplicitDepends(implicit_depends
);
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());
338 // Use the old-style mode for backward compatibility.
339 this->Makefile
->AddCustomCommandOldStyle(target
.c_str(), outputs
, depends
,
340 source
.c_str(), commandLines
,
347 //----------------------------------------------------------------------------
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();
366 // Make sure the output file name has no invalid characters.
367 std::string::size_type pos
= o
->find_first_of("#<>");
371 msg
<< "called with OUTPUT containing a \"" << (*o
)[pos
]
372 << "\". This character is not allowed.";
373 this->SetError(msg
.str().c_str());