1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef TOOLS_GN_FUNCTIONS_H_
6 #define TOOLS_GN_FUNCTIONS_H_
12 #include "base/strings/string_piece.h"
16 class FunctionCallNode
;
24 // -----------------------------------------------------------------------------
28 // This type of function invocation has no block and evaluates its arguments
29 // itself rather than taking a pre-executed list. This allows us to implement
30 // certain built-in functions.
31 typedef Value (*SelfEvaluatingArgsFunction
)(Scope
* scope
,
32 const FunctionCallNode
* function
,
33 const ListNode
* args_list
,
36 // This type of function invocation takes a block node that it will execute.
37 typedef Value (*GenericBlockFunction
)(Scope
* scope
,
38 const FunctionCallNode
* function
,
39 const std::vector
<Value
>& args
,
43 // This type of function takes a block, but does not need to control execution
44 // of it. The dispatch function will pre-execute the block and pass the
45 // resulting block_scope to the function.
46 typedef Value(*ExecutedBlockFunction
)(const FunctionCallNode
* function
,
47 const std::vector
<Value
>& args
,
51 // This type of function does not take a block. It just has arguments.
52 typedef Value (*NoBlockFunction
)(Scope
* scope
,
53 const FunctionCallNode
* function
,
54 const std::vector
<Value
>& args
,
57 extern const char kAction
[];
58 extern const char kAction_HelpShort
[];
59 extern const char kAction_Help
[];
60 Value
RunAction(Scope
* scope
,
61 const FunctionCallNode
* function
,
62 const std::vector
<Value
>& args
,
66 extern const char kActionForEach
[];
67 extern const char kActionForEach_HelpShort
[];
68 extern const char kActionForEach_Help
[];
69 Value
RunActionForEach(Scope
* scope
,
70 const FunctionCallNode
* function
,
71 const std::vector
<Value
>& args
,
75 extern const char kAssert
[];
76 extern const char kAssert_HelpShort
[];
77 extern const char kAssert_Help
[];
78 Value
RunAssert(Scope
* scope
,
79 const FunctionCallNode
* function
,
80 const std::vector
<Value
>& args
,
83 extern const char kComponent
[];
84 extern const char kComponent_HelpShort
[];
85 extern const char kComponent_Help
[];
86 Value
RunComponent(Scope
* scope
,
87 const FunctionCallNode
* function
,
88 const std::vector
<Value
>& args
,
92 extern const char kConfig
[];
93 extern const char kConfig_HelpShort
[];
94 extern const char kConfig_Help
[];
95 Value
RunConfig(const FunctionCallNode
* function
,
96 const std::vector
<Value
>& args
,
100 extern const char kCopy
[];
101 extern const char kCopy_HelpShort
[];
102 extern const char kCopy_Help
[];
103 Value
RunCopy(const FunctionCallNode
* function
,
104 const std::vector
<Value
>& args
,
108 extern const char kDeclareArgs
[];
109 extern const char kDeclareArgs_HelpShort
[];
110 extern const char kDeclareArgs_Help
[];
111 Value
RunDeclareArgs(Scope
* scope
,
112 const FunctionCallNode
* function
,
113 const std::vector
<Value
>& args
,
117 extern const char kDefined
[];
118 extern const char kDefined_HelpShort
[];
119 extern const char kDefined_Help
[];
120 Value
RunDefined(Scope
* scope
,
121 const FunctionCallNode
* function
,
122 const ListNode
* args_list
,
125 extern const char kExecScript
[];
126 extern const char kExecScript_HelpShort
[];
127 extern const char kExecScript_Help
[];
128 Value
RunExecScript(Scope
* scope
,
129 const FunctionCallNode
* function
,
130 const std::vector
<Value
>& args
,
133 extern const char kExecutable
[];
134 extern const char kExecutable_HelpShort
[];
135 extern const char kExecutable_Help
[];
136 Value
RunExecutable(Scope
* scope
,
137 const FunctionCallNode
* function
,
138 const std::vector
<Value
>& args
,
142 extern const char kGetEnv
[];
143 extern const char kGetEnv_HelpShort
[];
144 extern const char kGetEnv_Help
[];
145 Value
RunGetEnv(Scope
* scope
,
146 const FunctionCallNode
* function
,
147 const std::vector
<Value
>& args
,
150 extern const char kGetTargetOutputs
[];
151 extern const char kGetTargetOutputs_HelpShort
[];
152 extern const char kGetTargetOutputs_Help
[];
153 Value
RunGetTargetOutputs(Scope
* scope
,
154 const FunctionCallNode
* function
,
155 const std::vector
<Value
>& args
,
158 extern const char kGroup
[];
159 extern const char kGroup_HelpShort
[];
160 extern const char kGroup_Help
[];
161 Value
RunGroup(Scope
* scope
,
162 const FunctionCallNode
* function
,
163 const std::vector
<Value
>& args
,
167 extern const char kImport
[];
168 extern const char kImport_HelpShort
[];
169 extern const char kImport_Help
[];
170 Value
RunImport(Scope
* scope
,
171 const FunctionCallNode
* function
,
172 const std::vector
<Value
>& args
,
175 extern const char kPrint
[];
176 extern const char kPrint_HelpShort
[];
177 extern const char kPrint_Help
[];
178 Value
RunPrint(Scope
* scope
,
179 const FunctionCallNode
* function
,
180 const std::vector
<Value
>& args
,
183 extern const char kProcessFileTemplate
[];
184 extern const char kProcessFileTemplate_HelpShort
[];
185 extern const char kProcessFileTemplate_Help
[];
186 Value
RunProcessFileTemplate(Scope
* scope
,
187 const FunctionCallNode
* function
,
188 const std::vector
<Value
>& args
,
191 extern const char kReadFile
[];
192 extern const char kReadFile_HelpShort
[];
193 extern const char kReadFile_Help
[];
194 Value
RunReadFile(Scope
* scope
,
195 const FunctionCallNode
* function
,
196 const std::vector
<Value
>& args
,
199 extern const char kRebasePath
[];
200 extern const char kRebasePath_HelpShort
[];
201 extern const char kRebasePath_Help
[];
202 Value
RunRebasePath(Scope
* scope
,
203 const FunctionCallNode
* function
,
204 const std::vector
<Value
>& args
,
207 extern const char kSetDefaults
[];
208 extern const char kSetDefaults_HelpShort
[];
209 extern const char kSetDefaults_Help
[];
210 Value
RunSetDefaults(Scope
* scope
,
211 const FunctionCallNode
* function
,
212 const std::vector
<Value
>& args
,
216 extern const char kSetDefaultToolchain
[];
217 extern const char kSetDefaultToolchain_HelpShort
[];
218 extern const char kSetDefaultToolchain_Help
[];
219 Value
RunSetDefaultToolchain(Scope
* scope
,
220 const FunctionCallNode
* function
,
221 const std::vector
<Value
>& args
,
224 extern const char kSetSourcesAssignmentFilter
[];
225 extern const char kSetSourcesAssignmentFilter_HelpShort
[];
226 extern const char kSetSourcesAssignmentFilter_Help
[];
227 Value
RunSetSourcesAssignmentFilter(Scope
* scope
,
228 const FunctionCallNode
* function
,
229 const std::vector
<Value
>& args
,
232 extern const char kSharedLibrary
[];
233 extern const char kSharedLibrary_HelpShort
[];
234 extern const char kSharedLibrary_Help
[];
235 Value
RunSharedLibrary(Scope
* scope
,
236 const FunctionCallNode
* function
,
237 const std::vector
<Value
>& args
,
241 extern const char kSourceSet
[];
242 extern const char kSourceSet_HelpShort
[];
243 extern const char kSourceSet_Help
[];
244 Value
RunSourceSet(Scope
* scope
,
245 const FunctionCallNode
* function
,
246 const std::vector
<Value
>& args
,
250 extern const char kStaticLibrary
[];
251 extern const char kStaticLibrary_HelpShort
[];
252 extern const char kStaticLibrary_Help
[];
253 Value
RunStaticLibrary(Scope
* scope
,
254 const FunctionCallNode
* function
,
255 const std::vector
<Value
>& args
,
259 extern const char kTemplate
[];
260 extern const char kTemplate_HelpShort
[];
261 extern const char kTemplate_Help
[];
262 Value
RunTemplate(Scope
* scope
,
263 const FunctionCallNode
* function
,
264 const std::vector
<Value
>& args
,
268 extern const char kTest
[];
269 extern const char kTest_HelpShort
[];
270 extern const char kTest_Help
[];
271 Value
RunTest(Scope
* scope
,
272 const FunctionCallNode
* function
,
273 const std::vector
<Value
>& args
,
277 extern const char kTool
[];
278 extern const char kTool_HelpShort
[];
279 extern const char kTool_Help
[];
280 Value
RunTool(Scope
* scope
,
281 const FunctionCallNode
* function
,
282 const std::vector
<Value
>& args
,
286 extern const char kToolchain
[];
287 extern const char kToolchain_HelpShort
[];
288 extern const char kToolchain_Help
[];
289 Value
RunToolchain(Scope
* scope
,
290 const FunctionCallNode
* function
,
291 const std::vector
<Value
>& args
,
295 extern const char kToolchainArgs
[];
296 extern const char kToolchainArgs_HelpShort
[];
297 extern const char kToolchainArgs_Help
[];
298 Value
RunToolchainArgs(Scope
* scope
,
299 const FunctionCallNode
* function
,
300 const std::vector
<Value
>& args
,
304 extern const char kWriteFile
[];
305 extern const char kWriteFile_HelpShort
[];
306 extern const char kWriteFile_Help
[];
307 Value
RunWriteFile(Scope
* scope
,
308 const FunctionCallNode
* function
,
309 const std::vector
<Value
>& args
,
312 // -----------------------------------------------------------------------------
314 // One function record. Only one of the given runner types will be non-null
315 // which indicates the type of function it is.
316 struct FunctionInfo
{
318 FunctionInfo(SelfEvaluatingArgsFunction seaf
,
319 const char* in_help_short
,
322 FunctionInfo(GenericBlockFunction gbf
,
323 const char* in_help_short
,
326 FunctionInfo(ExecutedBlockFunction ebf
,
327 const char* in_help_short
,
330 FunctionInfo(NoBlockFunction nbf
,
331 const char* in_help_short
,
335 SelfEvaluatingArgsFunction self_evaluating_args_runner
;
336 GenericBlockFunction generic_block_runner
;
337 ExecutedBlockFunction executed_block_runner
;
338 NoBlockFunction no_block_runner
;
340 const char* help_short
;
346 typedef std::map
<base::StringPiece
, FunctionInfo
> FunctionInfoMap
;
348 // Returns the mapping of all built-in functions.
349 const FunctionInfoMap
& GetFunctions();
351 // Runs the given function.
352 Value
RunFunction(Scope
* scope
,
353 const FunctionCallNode
* function
,
354 const ListNode
* args_list
,
355 BlockNode
* block
, // Optional.
358 } // namespace functions
360 // Helper functions -----------------------------------------------------------
362 // Verifies that the current scope is not processing an import. If it is, it
363 // will set the error, blame the given parse node for it, and return false.
364 bool EnsureNotProcessingImport(const ParseNode
* node
,
368 // Like EnsureNotProcessingImport but checks for running the build config.
369 bool EnsureNotProcessingBuildConfig(const ParseNode
* node
,
373 // Sets up the |block_scope| for executing a target (or something like it).
374 // The |scope| is the containing scope. It should have been already set as the
375 // parent for the |block_scope| when the |block_scope| was created.
377 // This will set up the target defaults and set the |target_name| variable in
378 // the block scope to the current target name, which is assumed to be the first
379 // argument to the function.
381 // On success, returns true. On failure, sets the error and returns false.
382 bool FillTargetBlockScope(const Scope
* scope
,
383 const FunctionCallNode
* function
,
384 const std::string
& target_type
,
385 const BlockNode
* block
,
386 const std::vector
<Value
>& args
,
390 // Sets the given error to a message explaining that the function call requires
392 void FillNeedsBlockError(const FunctionCallNode
* function
, Err
* err
);
394 // Validates that the given function call has one string argument. This is
395 // the most common function signature, so it saves space to have this helper.
396 // Returns false and sets the error on failure.
397 bool EnsureSingleStringArg(const FunctionCallNode
* function
,
398 const std::vector
<Value
>& args
,
401 // Returns the name of the toolchain for the given scope.
402 const Label
& ToolchainLabelForScope(const Scope
* scope
);
404 // Generates a label for the given scope, using the current directory and
405 // toolchain, and the given name.
406 Label
MakeLabelForScope(const Scope
* scope
,
407 const FunctionCallNode
* function
,
408 const std::string
& name
);
410 #endif // TOOLS_GN_FUNCTIONS_H_