Partially fix compilation of media_unittests with Xcode 7 (OS X 10.11 SDK).
[chromium-blink-merge.git] / tools / gn / target_generator.cc
blob833b382e274147275463fa4b3cbc2577359c6ec8
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,
25 Scope* scope,
26 const FunctionCallNode* function_call,
27 Err* err)
28 : target_(target),
29 scope_(scope),
30 function_call_(function_call),
31 err_(err) {
34 TargetGenerator::~TargetGenerator() {
37 void TargetGenerator::Run() {
38 // All target types use these.
39 if (!FillDependentConfigs())
40 return;
42 if (!FillData())
43 return;
45 if (!FillDependencies())
46 return;
48 if (!FillTestonly())
49 return;
51 if (!Visibility::FillItemVisibility(target_, scope_, err_))
52 return;
54 // Do type-specific generation.
55 DoRun();
58 // static
59 void TargetGenerator::GenerateTarget(Scope* scope,
60 const FunctionCallNode* function_call,
61 const std::vector<Value>& args,
62 const std::string& output_type,
63 Err* err) {
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.");
69 return;
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);
87 generator.Run();
88 } else if (output_type == functions::kAction) {
89 ActionTargetGenerator generator(target.get(), scope, function_call,
90 Target::ACTION, err);
91 generator.Run();
92 } else if (output_type == functions::kActionForEach) {
93 ActionTargetGenerator generator(target.get(), scope, function_call,
94 Target::ACTION_FOREACH, err);
95 generator.Run();
96 } else if (output_type == functions::kExecutable) {
97 BinaryTargetGenerator generator(target.get(), scope, function_call,
98 Target::EXECUTABLE, err);
99 generator.Run();
100 } else if (output_type == functions::kGroup) {
101 GroupTargetGenerator generator(target.get(), scope, function_call, err);
102 generator.Run();
103 } else if (output_type == functions::kSharedLibrary) {
104 BinaryTargetGenerator generator(target.get(), scope, function_call,
105 Target::SHARED_LIBRARY, err);
106 generator.Run();
107 } else if (output_type == functions::kSourceSet) {
108 BinaryTargetGenerator generator(target.get(), scope, function_call,
109 Target::SOURCE_SET, err);
110 generator.Run();
111 } else if (output_type == functions::kStaticLibrary) {
112 BinaryTargetGenerator generator(target.get(), scope, function_call,
113 Target::STATIC_LIBRARY, err);
114 generator.Run();
115 } else {
116 *err = Err(function_call, "Not a known target type",
117 "I am very confused by the target type \"" + output_type + "\"");
120 if (err->has_error())
121 return;
123 // Save this target for the file.
124 Scope::ItemVector* collector = scope->GetItemCollector();
125 if (!collector) {
126 *err = Err(function_call, "Can't define a target in this context.");
127 return;
129 collector->push_back(target.release());
132 const BuildSettings* TargetGenerator::GetBuildSettings() const {
133 return scope_->settings()->build_settings();
136 bool TargetGenerator::FillSources() {
137 const Value* value = scope_->GetValue(variables::kSources, true);
138 if (!value)
139 return true;
141 Target::FileList dest_sources;
142 if (!ExtractListOfRelativeFiles(scope_->settings()->build_settings(), *value,
143 scope_->GetSourceDir(), &dest_sources, err_))
144 return false;
145 target_->sources().swap(dest_sources);
146 return true;
149 bool TargetGenerator::FillPublic() {
150 const Value* value = scope_->GetValue(variables::kPublic, true);
151 if (!value)
152 return true;
154 // If the public headers are defined, don't default to public.
155 target_->set_all_headers_public(false);
157 Target::FileList dest_public;
158 if (!ExtractListOfRelativeFiles(scope_->settings()->build_settings(), *value,
159 scope_->GetSourceDir(), &dest_public, err_))
160 return false;
161 target_->public_headers().swap(dest_public);
162 return true;
165 bool TargetGenerator::FillInputs() {
166 const Value* value = scope_->GetValue(variables::kInputs, true);
167 if (!value)
168 return true;
170 Target::FileList dest_inputs;
171 if (!ExtractListOfRelativeFiles(scope_->settings()->build_settings(), *value,
172 scope_->GetSourceDir(), &dest_inputs, err_))
173 return false;
174 target_->inputs().swap(dest_inputs);
175 return true;
178 bool TargetGenerator::FillConfigs() {
179 return FillGenericConfigs(variables::kConfigs, &target_->configs());
182 bool TargetGenerator::FillDependentConfigs() {
183 if (!FillGenericConfigs(variables::kAllDependentConfigs,
184 &target_->all_dependent_configs()))
185 return false;
187 if (!FillGenericConfigs(variables::kPublicConfigs,
188 &target_->public_configs()))
189 return false;
191 return true;
194 bool TargetGenerator::FillData() {
195 const Value* value = scope_->GetValue(variables::kData, true);
196 if (!value)
197 return true;
198 if (!value->VerifyTypeIs(Value::LIST, err_))
199 return false;
201 const std::vector<Value>& input_list = value->list_value();
202 std::vector<std::string>& output_list = target_->data();
203 output_list.reserve(input_list.size());
205 const SourceDir& dir = scope_->GetSourceDir();
206 const std::string& root_path =
207 scope_->settings()->build_settings()->root_path_utf8();
209 for (size_t i = 0; i < input_list.size(); i++) {
210 const Value& input = input_list[i];
211 if (!input.VerifyTypeIs(Value::STRING, err_))
212 return false;
213 const std::string& input_str = input.string_value();
215 // Treat each input as either a file or a directory, depending on the
216 // last character.
217 if (!input_str.empty() && input_str[input_str.size() - 1] == '/') {
218 // Resolve as directory.
219 SourceDir resolved =
220 dir.ResolveRelativeDir(input, input_str, err_, root_path);
221 if (err_->has_error())
222 return false;
223 output_list.push_back(resolved.value());
224 } else {
225 // Resolve as file.
226 SourceFile resolved = dir.ResolveRelativeFile(input, err_, root_path);
227 if (err_->has_error())
228 return false;
229 output_list.push_back(resolved.value());
232 return true;
235 bool TargetGenerator::FillDependencies() {
236 if (!FillGenericDeps(variables::kDeps, &target_->private_deps()))
237 return false;
238 if (!FillGenericDeps(variables::kPublicDeps, &target_->public_deps()))
239 return false;
240 if (!FillGenericDeps(variables::kDataDeps, &target_->data_deps()))
241 return false;
243 // "data_deps" was previously named "datadeps". For backwards-compat, read
244 // the old one if no "data_deps" were specified.
245 if (!scope_->GetValue(variables::kDataDeps, false)) {
246 if (!FillGenericDeps("datadeps", &target_->data_deps()))
247 return false;
250 // This is a list of dependent targets to have their configs fowarded, so
251 // it goes here rather than in FillConfigs.
252 if (!FillForwardDependentConfigs())
253 return false;
254 return true;
257 bool TargetGenerator::FillTestonly() {
258 const Value* value = scope_->GetValue(variables::kTestonly, true);
259 if (value) {
260 if (!value->VerifyTypeIs(Value::BOOLEAN, err_))
261 return false;
262 target_->set_testonly(value->boolean_value());
264 return true;
267 bool TargetGenerator::FillOutputs(bool allow_substitutions) {
268 const Value* value = scope_->GetValue(variables::kOutputs, true);
269 if (!value)
270 return true;
272 SubstitutionList& outputs = target_->action_values().outputs();
273 if (!outputs.Parse(*value, err_))
274 return false;
276 if (!allow_substitutions) {
277 // Verify no substitutions were actually used.
278 if (!outputs.required_types().empty()) {
279 *err_ = Err(*value, "Source expansions not allowed here.",
280 "The outputs of this target used source {{expansions}} but this "
281 "targe type\ndoesn't support them. Just express the outputs "
282 "literally.");
283 return false;
287 // Check the substitutions used are valid for this purpose.
288 if (!EnsureValidSourcesSubstitutions(outputs.required_types(),
289 value->origin(), err_))
290 return false;
292 // Validate that outputs are in the output dir.
293 CHECK(outputs.list().size() == value->list_value().size());
294 for (size_t i = 0; i < outputs.list().size(); i++) {
295 if (!EnsureSubstitutionIsInOutputDir(outputs.list()[i],
296 value->list_value()[i]))
297 return false;
299 return true;
302 bool TargetGenerator::FillCheckIncludes() {
303 const Value* value = scope_->GetValue(variables::kCheckIncludes, true);
304 if (!value)
305 return true;
306 if (!value->VerifyTypeIs(Value::BOOLEAN, err_))
307 return false;
308 target_->set_check_includes(value->boolean_value());
309 return true;
312 bool TargetGenerator::EnsureSubstitutionIsInOutputDir(
313 const SubstitutionPattern& pattern,
314 const Value& original_value) {
315 if (pattern.ranges().empty()) {
316 // Pattern is empty, error out (this prevents weirdness below).
317 *err_ = Err(original_value, "This has an empty value in it.");
318 return false;
321 if (pattern.ranges()[0].type == SUBSTITUTION_LITERAL) {
322 // If the first thing is a literal, it must start with the output dir.
323 if (!EnsureStringIsInOutputDir(
324 GetBuildSettings()->build_dir(),
325 pattern.ranges()[0].literal, original_value.origin(), err_))
326 return false;
327 } else {
328 // Otherwise, the first subrange must be a pattern that expands to
329 // something in the output directory.
330 if (!SubstitutionIsInOutputDir(pattern.ranges()[0].type)) {
331 *err_ = Err(original_value,
332 "File is not inside output directory.",
333 "The given file should be in the output directory. Normally you\n"
334 "would specify\n\"$target_out_dir/foo\" or "
335 "\"{{source_gen_dir}}/foo\".");
336 return false;
340 return true;
343 bool TargetGenerator::FillGenericConfigs(const char* var_name,
344 UniqueVector<LabelConfigPair>* dest) {
345 const Value* value = scope_->GetValue(var_name, true);
346 if (value) {
347 ExtractListOfUniqueLabels(*value, scope_->GetSourceDir(),
348 ToolchainLabelForScope(scope_), dest, err_);
350 return !err_->has_error();
353 bool TargetGenerator::FillGenericDeps(const char* var_name,
354 LabelTargetVector* dest) {
355 const Value* value = scope_->GetValue(var_name, true);
356 if (value) {
357 ExtractListOfLabels(*value, scope_->GetSourceDir(),
358 ToolchainLabelForScope(scope_), dest, err_);
360 return !err_->has_error();
363 bool TargetGenerator::FillForwardDependentConfigs() {
364 const Value* value = scope_->GetValue(
365 variables::kForwardDependentConfigsFrom, true);
366 if (value) {
367 ExtractListOfUniqueLabels(*value, scope_->GetSourceDir(),
368 ToolchainLabelForScope(scope_),
369 &target_->forward_dependent_configs(), err_);
371 return !err_->has_error();