1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmAddCustomCommandCommand.cxx,v $
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"
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 filename
= this->Makefile
->GetStartDirectory();
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.
172 case doing_working_directory
:
179 output
.push_back(filename
);
181 case doing_main_dependency
:
182 main_dependency
= copy
;
184 case doing_implicit_depends_lang
:
185 implicit_depends_lang
= copy
;
186 doing
= doing_implicit_depends_file
;
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
;
204 currentLine
.push_back(copy
);
210 depends
.push_back(copy
);
213 outputs
.push_back(filename
);
216 comment_buffer
= copy
;
217 comment
= comment_buffer
.c_str();
220 this->SetError("Wrong syntax. Unknown type of argument.");
226 // Store the last command line finished.
227 if(!currentLine
.empty())
229 commandLines
.push_back(currentLine
);
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.");
241 if(source
.empty() && !target
.empty() && !output
.empty())
244 "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
247 if(append
&& output
.empty())
249 this->SetError("given APPEND option with no OUTPUT.");
253 // Make sure the output names and locations are safe.
254 if(!this->CheckOutputs(output
) || !this->CheckOutputs(outputs
))
259 // Check for an append request.
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
);
275 // No command for this output exists.
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());
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(),
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,
303 // Add implicit dependency scanning requests if any were given.
304 if(!implicit_depends
.empty())
307 if(cmSourceFile
* sf
=
308 this->Makefile
->GetSourceFileWithOutput(output
[0].c_str()))
310 if(cmCustomCommand
* cc
= sf
->GetCustomCommand())
313 cc
->SetImplicitDepends(implicit_depends
);
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());
328 // Use the old-style mode for backward compatibility.
329 this->Makefile
->AddCustomCommandOldStyle(target
.c_str(), outputs
, depends
,
330 source
.c_str(), commandLines
,
337 //----------------------------------------------------------------------------
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();
356 // Make sure the output file name has no invalid characters.
357 std::string::size_type pos
= o
->find_first_of("#<>");
361 msg
<< "called with OUTPUT containing a \"" << (*o
)[pos
]
362 << "\". This character is not allowed.";
363 this->SetError(msg
.str().c_str());