Fixed incorrect call of updateContextMenuActionItems.
[chromium-blink-merge.git] / tools / gn / action_target_generator.cc
blob09c89e4ca949b5c36e11b883ef665a6b5297351c
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(
17 Target* target,
18 Scope* scope,
19 const FunctionCallNode* function_call,
20 Target::OutputType type,
21 Err* err)
22 : TargetGenerator(target, scope, function_call, err),
23 output_type_(type) {
26 ActionTargetGenerator::~ActionTargetGenerator() {
29 void ActionTargetGenerator::DoRun() {
30 target_->set_output_type(output_type_);
32 if (!FillSources())
33 return;
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"
38 "script over.");
39 return;
42 if (!FillInputs())
43 return;
45 if (!FillScript())
46 return;
48 if (!FillScriptArgs())
49 return;
51 if (!FillOutputs(output_type_ == Target::ACTION_FOREACH))
52 return;
54 if (!FillDepfile())
55 return;
57 if (!CheckOutputs())
58 return;
60 // Action outputs don't depend on the current toolchain so we can skip adding
61 // that dependency.
64 bool ActionTargetGenerator::FillScript() {
65 // If this gets called, the target type requires a script, so error out
66 // if it doesn't have one.
67 const Value* value = scope_->GetValue(variables::kScript, true);
68 if (!value) {
69 *err_ = Err(function_call_, "This target type requires a \"script\".");
70 return false;
72 if (!value->VerifyTypeIs(Value::STRING, err_))
73 return false;
75 SourceFile script_file =
76 scope_->GetSourceDir().ResolveRelativeFile(value->string_value(),
77 scope_->settings()->build_settings()->root_path_utf8());
78 if (script_file.value().empty()) {
79 *err_ = Err(*value, "script name is empty");
80 return false;
82 target_->action_values().set_script(script_file);
83 return true;
86 bool ActionTargetGenerator::FillScriptArgs() {
87 const Value* value = scope_->GetValue(variables::kArgs, true);
88 if (!value)
89 return true;
90 return target_->action_values().args().Parse(*value, err_);
93 bool ActionTargetGenerator::FillDepfile() {
94 const Value* value = scope_->GetValue(variables::kDepfile, true);
95 if (!value)
96 return true;
98 SubstitutionPattern depfile;
99 if (!depfile.Parse(*value, err_))
100 return false;
101 if (!EnsureSubstitutionIsInOutputDir(depfile, *value))
102 return false;
104 target_->action_values().set_depfile(depfile);
105 return true;
108 bool ActionTargetGenerator::CheckOutputs() {
109 const SubstitutionList& outputs = target_->action_values().outputs();
110 if (outputs.list().empty()) {
111 *err_ = Err(function_call_, "Action has no outputs.",
112 "If you have no outputs, the build system can not tell when your\n"
113 "script needs to be run.");
114 return false;
117 if (output_type_ == Target::ACTION) {
118 if (!outputs.required_types().empty()) {
119 *err_ = Err(function_call_, "Action has patterns in the output.",
120 "An action target should have the outputs completely specified. If\n"
121 "you want to provide a mapping from source to output, use an\n"
122 "\"action_foreach\" target.");
123 return false;
125 } else if (output_type_ == Target::ACTION_FOREACH) {
126 // A foreach target should always have a pattern in the outputs.
127 if (outputs.required_types().empty()) {
128 *err_ = Err(function_call_,
129 "action_foreach should have a pattern in the output.",
130 "An action_foreach target should have a source expansion pattern in\n"
131 "it to map source file to unique output file name. Otherwise, the\n"
132 "build system can't determine when your script needs to be run.");
133 return false;
136 return true;