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/target_generator.h"
7 #include "tools/gn/action_target_generator.h"
8 #include "tools/gn/binary_target_generator.h"
9 #include "tools/gn/build_settings.h"
10 #include "tools/gn/config.h"
11 #include "tools/gn/copy_target_generator.h"
12 #include "tools/gn/err.h"
13 #include "tools/gn/filesystem_utils.h"
14 #include "tools/gn/functions.h"
15 #include "tools/gn/group_target_generator.h"
16 #include "tools/gn/parse_tree.h"
17 #include "tools/gn/scheduler.h"
18 #include "tools/gn/scope.h"
19 #include "tools/gn/token.h"
20 #include "tools/gn/value.h"
21 #include "tools/gn/value_extractors.h"
22 #include "tools/gn/variables.h"
24 TargetGenerator::TargetGenerator(Target
* target
,
26 const FunctionCallNode
* function_call
,
30 function_call_(function_call
),
34 TargetGenerator::~TargetGenerator() {
37 void TargetGenerator::Run() {
38 // All target types use these.
39 FillDependentConfigs();
40 if (err_
->has_error())
44 if (err_
->has_error())
48 if (err_
->has_error())
51 if (!Visibility::FillItemVisibility(target_
, scope_
, err_
))
54 // Do type-specific generation.
59 void TargetGenerator::GenerateTarget(Scope
* scope
,
60 const FunctionCallNode
* function_call
,
61 const std::vector
<Value
>& args
,
62 const std::string
& output_type
,
64 // Name is the argument to the function.
65 if (args
.size() != 1u || args
[0].type() != Value::STRING
) {
66 *err
= Err(function_call
,
67 "Target generator requires one string argument.",
68 "Otherwise I'm not sure what to call this target.");
72 // The location of the target is the directory name with no slash at the end.
73 // FIXME(brettw) validate name.
74 const Label
& toolchain_label
= ToolchainLabelForScope(scope
);
75 Label
label(scope
->GetSourceDir(), args
[0].string_value(),
76 toolchain_label
.dir(), toolchain_label
.name());
78 if (g_scheduler
->verbose_logging())
79 g_scheduler
->Log("Defining target", label
.GetUserVisibleName(true));
81 scoped_ptr
<Target
> target(new Target(scope
->settings(), label
));
82 target
->set_defined_from(function_call
);
84 // Create and call out to the proper generator.
85 if (output_type
== functions::kCopy
) {
86 CopyTargetGenerator
generator(target
.get(), scope
, function_call
, err
);
88 } else if (output_type
== functions::kAction
) {
89 ActionTargetGenerator
generator(target
.get(), scope
, function_call
,
92 } else if (output_type
== functions::kActionForEach
) {
93 ActionTargetGenerator
generator(target
.get(), scope
, function_call
,
94 Target::ACTION_FOREACH
, err
);
96 } else if (output_type
== functions::kExecutable
) {
97 BinaryTargetGenerator
generator(target
.get(), scope
, function_call
,
98 Target::EXECUTABLE
, err
);
100 } else if (output_type
== functions::kGroup
) {
101 GroupTargetGenerator
generator(target
.get(), scope
, function_call
, err
);
103 } else if (output_type
== functions::kSharedLibrary
) {
104 BinaryTargetGenerator
generator(target
.get(), scope
, function_call
,
105 Target::SHARED_LIBRARY
, err
);
107 } else if (output_type
== functions::kSourceSet
) {
108 BinaryTargetGenerator
generator(target
.get(), scope
, function_call
,
109 Target::SOURCE_SET
, err
);
111 } else if (output_type
== functions::kStaticLibrary
) {
112 BinaryTargetGenerator
generator(target
.get(), scope
, function_call
,
113 Target::STATIC_LIBRARY
, err
);
116 *err
= Err(function_call
, "Not a known output type",
117 "I am very confused.");
120 if (err
->has_error())
123 // Save this target for the file.
124 Scope::ItemVector
* collector
= scope
->GetItemCollector();
126 *err
= Err(function_call
, "Can't define a target in this context.");
129 collector
->push_back(new scoped_ptr
<Item
>(target
.PassAs
<Item
>()));
132 const BuildSettings
* TargetGenerator::GetBuildSettings() const {
133 return scope_
->settings()->build_settings();
136 void TargetGenerator::FillSources() {
137 const Value
* value
= scope_
->GetValue(variables::kSources
, true);
141 Target::FileList dest_sources
;
142 if (!ExtractListOfRelativeFiles(scope_
->settings()->build_settings(), *value
,
143 scope_
->GetSourceDir(), &dest_sources
, err_
))
145 target_
->sources().swap(dest_sources
);
148 void TargetGenerator::FillPublic() {
149 const Value
* value
= scope_
->GetValue(variables::kPublic
, true);
153 // If the public headers are defined, don't default to public.
154 target_
->set_all_headers_public(false);
156 Target::FileList dest_public
;
157 if (!ExtractListOfRelativeFiles(scope_
->settings()->build_settings(), *value
,
158 scope_
->GetSourceDir(), &dest_public
, err_
))
160 target_
->public_headers().swap(dest_public
);
163 void TargetGenerator::FillSourcePrereqs() {
164 const Value
* value
= scope_
->GetValue(variables::kSourcePrereqs
, true);
168 Target::FileList dest_reqs
;
169 if (!ExtractListOfRelativeFiles(scope_
->settings()->build_settings(), *value
,
170 scope_
->GetSourceDir(), &dest_reqs
, err_
))
172 target_
->source_prereqs().swap(dest_reqs
);
175 void TargetGenerator::FillConfigs() {
176 FillGenericConfigs(variables::kConfigs
, &target_
->configs());
179 void TargetGenerator::FillDependentConfigs() {
180 FillGenericConfigs(variables::kAllDependentConfigs
,
181 &target_
->all_dependent_configs());
182 FillGenericConfigs(variables::kDirectDependentConfigs
,
183 &target_
->direct_dependent_configs());
186 void TargetGenerator::FillData() {
187 const Value
* value
= scope_
->GetValue(variables::kData
, true);
191 Target::FileList dest_data
;
192 if (!ExtractListOfRelativeFiles(scope_
->settings()->build_settings(), *value
,
193 scope_
->GetSourceDir(), &dest_data
, err_
))
195 target_
->data().swap(dest_data
);
198 void TargetGenerator::FillDependencies() {
199 FillGenericDeps(variables::kDeps
, &target_
->deps());
200 if (err_
->has_error())
202 FillGenericDeps(variables::kDatadeps
, &target_
->datadeps());
203 if (err_
->has_error())
206 // This is a list of dependent targets to have their configs fowarded, so
207 // it goes here rather than in FillConfigs.
208 FillForwardDependentConfigs();
209 if (err_
->has_error())
213 void TargetGenerator::FillOutputs() {
214 const Value
* value
= scope_
->GetValue(variables::kOutputs
, true);
218 Target::FileList outputs
;
219 if (!ExtractListOfRelativeFiles(scope_
->settings()->build_settings(), *value
,
220 scope_
->GetSourceDir(), &outputs
, err_
))
223 // Validate that outputs are in the output dir.
224 CHECK(outputs
.size() == value
->list_value().size());
225 for (size_t i
= 0; i
< outputs
.size(); i
++) {
226 if (!EnsureStringIsInOutputDir(
227 GetBuildSettings()->build_dir(),
228 outputs
[i
].value(), value
->list_value()[i
], err_
))
231 target_
->action_values().outputs().swap(outputs
);
234 void TargetGenerator::FillGenericConfigs(const char* var_name
,
235 LabelConfigVector
* dest
) {
236 const Value
* value
= scope_
->GetValue(var_name
, true);
238 ExtractListOfLabels(*value
, scope_
->GetSourceDir(),
239 ToolchainLabelForScope(scope_
), dest
, err_
);
243 void TargetGenerator::FillGenericDeps(const char* var_name
,
244 LabelTargetVector
* dest
) {
245 const Value
* value
= scope_
->GetValue(var_name
, true);
247 ExtractListOfLabels(*value
, scope_
->GetSourceDir(),
248 ToolchainLabelForScope(scope_
), dest
, err_
);
252 void TargetGenerator::FillForwardDependentConfigs() {
253 const Value
* value
= scope_
->GetValue(
254 variables::kForwardDependentConfigsFrom
, true);
256 ExtractListOfLabels(*value
, scope_
->GetSourceDir(),
257 ToolchainLabelForScope(scope_
),
258 &target_
->forward_dependent_configs(), err_
);