ENH: Return utility target after creation
[cmake.git] / Source / cmAddCustomTargetCommand.cxx
blobceaf9149162561d29eb399c6f955dd8a174d00aa
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmAddCustomTargetCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-03-07 22:05:06 $
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 "cmAddCustomTargetCommand.h"
19 // cmAddCustomTargetCommand
20 bool cmAddCustomTargetCommand
21 ::InitialPass(std::vector<std::string> const& args,
22 cmExecutionStatus&)
24 if(args.size() < 1 )
26 this->SetError("called with incorrect number of arguments");
27 return false;
30 // Check the target name.
31 if(args[0].find_first_of("/\\") != args[0].npos)
33 if(!this->Makefile->NeedBackwardsCompatibility(2,2))
35 cmOStringStream e;
36 e << "called with invalid target name \"" << args[0]
37 << "\". Target names may not contain a slash. "
38 << "Use ADD_CUSTOM_COMMAND to generate files. "
39 << "Set CMAKE_BACKWARDS_COMPATIBILITY to 2.2 "
40 << "or lower to skip this check.";
41 this->SetError(e.str().c_str());
42 return false;
46 // Accumulate one command line at a time.
47 cmCustomCommandLine currentLine;
49 // Save all command lines.
50 cmCustomCommandLines commandLines;
52 // Accumulate dependencies.
53 std::vector<std::string> depends;
54 std::string working_directory;
55 bool verbatim = false;
56 std::string comment_buffer;
57 const char* comment = 0;
59 // Keep track of parser state.
60 enum tdoing {
61 doing_command,
62 doing_depends,
63 doing_working_directory,
64 doing_comment,
65 doing_verbatim
67 tdoing doing = doing_command;
69 // Look for the ALL option.
70 bool excludeFromAll = true;
71 unsigned int start = 1;
72 if(args.size() > 1)
74 if(args[1] == "ALL")
76 excludeFromAll = false;
77 start = 2;
81 // Parse the rest of the arguments.
82 for(unsigned int j = start; j < args.size(); ++j)
84 std::string const& copy = args[j];
86 if(copy == "DEPENDS")
88 doing = doing_depends;
90 else if(copy == "WORKING_DIRECTORY")
92 doing = doing_working_directory;
94 else if(copy == "VERBATIM")
96 doing = doing_verbatim;
97 verbatim = true;
99 else if (copy == "COMMENT")
101 doing = doing_comment;
103 else if(copy == "COMMAND")
105 doing = doing_command;
107 // Save the current command before starting the next command.
108 if(!currentLine.empty())
110 commandLines.push_back(currentLine);
111 currentLine.clear();
114 else
116 switch (doing)
118 case doing_working_directory:
119 working_directory = copy;
120 break;
121 case doing_command:
122 currentLine.push_back(copy);
123 break;
124 case doing_depends:
125 depends.push_back(copy);
126 break;
127 case doing_comment:
128 comment_buffer = copy;
129 comment = comment_buffer.c_str();
130 break;
131 default:
132 this->SetError("Wrong syntax. Unknown type of argument.");
133 return false;
138 std::string::size_type pos = args[0].find_first_of("#<>");
139 if(pos != args[0].npos)
141 cmOStringStream msg;
142 msg << "called with target name containing a \"" << args[0][pos]
143 << "\". This character is not allowed.";
144 this->SetError(msg.str().c_str());
145 return false;
148 // Store the last command line finished.
149 if(!currentLine.empty())
151 commandLines.push_back(currentLine);
152 currentLine.clear();
155 // Enforce name uniqueness.
157 std::string msg;
158 if(!this->Makefile->EnforceUniqueName(args[0], msg, true))
160 this->SetError(msg.c_str());
161 return false;
165 // Add the utility target to the makefile.
166 bool escapeOldStyle = !verbatim;
167 this->Makefile->AddUtilityCommand(args[0].c_str(), excludeFromAll,
168 working_directory.c_str(), depends,
169 commandLines, escapeOldStyle, comment);
171 return true;