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 #include "tools/gn/action_target_generator.h"
7 #include "tools/gn/build_settings.h"
8 #include "tools/gn/err.h"
9 #include "tools/gn/filesystem_utils.h"
10 #include "tools/gn/parse_tree.h"
11 #include "tools/gn/scope.h"
12 #include "tools/gn/value.h"
13 #include "tools/gn/value_extractors.h"
14 #include "tools/gn/variables.h"
16 ActionTargetGenerator::ActionTargetGenerator(
19 const FunctionCallNode
* function_call
,
20 Target::OutputType type
,
22 : TargetGenerator(target
, scope
, function_call
, err
),
26 ActionTargetGenerator::~ActionTargetGenerator() {
29 void ActionTargetGenerator::DoRun() {
30 target_
->set_output_type(output_type_
);
34 if (output_type_
== Target::ACTION_FOREACH
&& target_
->sources().empty()) {
35 // Foreach rules must always have some sources to have an effect.
36 *err_
= Err(function_call_
, "action_foreach target has no sources.",
37 "If you don't specify any sources, there is nothing to run your\n"
48 if (!FillScriptArgs())
51 if (!FillOutputs(output_type_
== Target::ACTION_FOREACH
))
57 if (!FillCheckIncludes())
63 // Action outputs don't depend on the current toolchain so we can skip adding
67 bool ActionTargetGenerator::FillScript() {
68 // If this gets called, the target type requires a script, so error out
69 // if it doesn't have one.
70 const Value
* value
= scope_
->GetValue(variables::kScript
, true);
72 *err_
= Err(function_call_
, "This target type requires a \"script\".");
75 if (!value
->VerifyTypeIs(Value::STRING
, err_
))
78 SourceFile script_file
=
79 scope_
->GetSourceDir().ResolveRelativeFile(value
->string_value(),
80 scope_
->settings()->build_settings()->root_path_utf8());
81 if (script_file
.value().empty()) {
82 *err_
= Err(*value
, "script name is empty");
85 target_
->action_values().set_script(script_file
);
89 bool ActionTargetGenerator::FillScriptArgs() {
90 const Value
* value
= scope_
->GetValue(variables::kArgs
, true);
93 return target_
->action_values().args().Parse(*value
, err_
);
96 bool ActionTargetGenerator::FillDepfile() {
97 const Value
* value
= scope_
->GetValue(variables::kDepfile
, true);
101 SubstitutionPattern depfile
;
102 if (!depfile
.Parse(*value
, err_
))
104 if (!EnsureSubstitutionIsInOutputDir(depfile
, *value
))
107 target_
->action_values().set_depfile(depfile
);
111 bool ActionTargetGenerator::CheckOutputs() {
112 const SubstitutionList
& outputs
= target_
->action_values().outputs();
113 if (outputs
.list().empty()) {
114 *err_
= Err(function_call_
, "Action has no outputs.",
115 "If you have no outputs, the build system can not tell when your\n"
116 "script needs to be run.");
120 if (output_type_
== Target::ACTION
) {
121 if (!outputs
.required_types().empty()) {
122 *err_
= Err(function_call_
, "Action has patterns in the output.",
123 "An action target should have the outputs completely specified. If\n"
124 "you want to provide a mapping from source to output, use an\n"
125 "\"action_foreach\" target.");
128 } else if (output_type_
== Target::ACTION_FOREACH
) {
129 // A foreach target should always have a pattern in the outputs.
130 if (outputs
.required_types().empty()) {
131 *err_
= Err(function_call_
,
132 "action_foreach should have a pattern in the output.",
133 "An action_foreach target should have a source expansion pattern in\n"
134 "it to map source file to unique output file name. Otherwise, the\n"
135 "build system can't determine when your script needs to be run.");