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.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "tools/gn/config_values_extractors.h"
11 #include "tools/gn/deps_iterator.h"
12 #include "tools/gn/filesystem_utils.h"
13 #include "tools/gn/scheduler.h"
14 #include "tools/gn/substitution_writer.h"
18 typedef std::set
<const Config
*> ConfigSet
;
20 // Merges the public configs from the given target to the given config list.
21 void MergePublicConfigsFrom(const Target
* from_target
,
22 UniqueVector
<LabelConfigPair
>* dest
) {
23 const UniqueVector
<LabelConfigPair
>& pub
= from_target
->public_configs();
24 dest
->Append(pub
.begin(), pub
.end());
27 // Like MergePublicConfigsFrom above except does the "all dependent" ones. This
28 // additionally adds all configs to the all_dependent_configs_ of the dest
29 // target given in *all_dest.
30 void MergeAllDependentConfigsFrom(const Target
* from_target
,
31 UniqueVector
<LabelConfigPair
>* dest
,
32 UniqueVector
<LabelConfigPair
>* all_dest
) {
33 for (const auto& pair
: from_target
->all_dependent_configs()) {
34 all_dest
->push_back(pair
);
35 dest
->push_back(pair
);
39 Err
MakeTestOnlyError(const Target
* from
, const Target
* to
) {
40 return Err(from
->defined_from(), "Test-only dependency not allowed.",
41 from
->label().GetUserVisibleName(false) + "\n"
42 "which is NOT marked testonly can't depend on\n" +
43 to
->label().GetUserVisibleName(false) + "\n"
44 "which is marked testonly. Only targets with \"testonly = true\"\n"
45 "can depend on other test-only targets.\n"
47 "Either mark it test-only or don't do this dependency.");
50 Err
MakeStaticLibDepsError(const Target
* from
, const Target
* to
) {
51 return Err(from
->defined_from(),
52 "Complete static libraries can't depend on static libraries.",
53 from
->label().GetUserVisibleName(false) +
55 "which is a complete static library can't depend on\n" +
56 to
->label().GetUserVisibleName(false) +
58 "which is a static library.\n"
60 "Use source sets for intermediate targets instead.");
63 // Set check_private_deps to true for the first invocation since a target
64 // can see all of its dependencies. For recursive invocations this will be set
65 // to false to follow only public dependency paths.
67 // Pass a pointer to an empty set for the first invocation. This will be used
68 // to avoid duplicate checking.
69 bool EnsureFileIsGeneratedByDependency(const Target
* target
,
70 const OutputFile
& file
,
71 bool check_private_deps
,
72 std::set
<const Target
*>* seen_targets
) {
73 if (seen_targets
->find(target
) != seen_targets
->end())
74 return false; // Already checked this one and it's not found.
75 seen_targets
->insert(target
);
77 // Assume that we have relatively few generated inputs so brute-force
78 // searching here is OK. If this becomes a bottleneck, consider storing
79 // computed_outputs as a hash set.
80 for (const OutputFile
& cur
: target
->computed_outputs()) {
85 // Check all public dependencies (don't do data ones since those are
87 for (const auto& pair
: target
->public_deps()) {
88 if (EnsureFileIsGeneratedByDependency(pair
.ptr
, file
, false,
90 return true; // Found a path.
93 // Only check private deps if requested.
94 if (check_private_deps
) {
95 for (const auto& pair
: target
->private_deps()) {
96 if (EnsureFileIsGeneratedByDependency(pair
.ptr
, file
, false,
98 return true; // Found a path.
106 Target::Target(const Settings
* settings
, const Label
& label
)
107 : Item(settings
, label
),
108 output_type_(UNKNOWN
),
109 all_headers_public_(true),
110 check_includes_(true),
111 complete_static_lib_(false),
113 toolchain_(nullptr) {
120 const char* Target::GetStringForOutputType(OutputType type
) {
129 return "Shared library";
131 return "Static library";
139 return "ActionForEach";
145 Target
* Target::AsTarget() {
149 const Target
* Target::AsTarget() const {
153 bool Target::OnResolved(Err
* err
) {
154 DCHECK(output_type_
!= UNKNOWN
);
155 DCHECK(toolchain_
) << "Toolchain should have been set before resolving.";
157 // Copy our own dependent configs to the list of configs applying to us.
158 configs_
.Append(all_dependent_configs_
.begin(), all_dependent_configs_
.end());
159 MergePublicConfigsFrom(this, &configs_
);
161 // Copy our own libs and lib_dirs to the final set. This will be from our
162 // target and all of our configs. We do this specially since these must be
163 // inherited through the dependency tree (other flags don't work this way).
164 for (ConfigValuesIterator
iter(this); !iter
.done(); iter
.Next()) {
165 const ConfigValues
& cur
= iter
.cur();
166 all_lib_dirs_
.append(cur
.lib_dirs().begin(), cur
.lib_dirs().end());
167 all_libs_
.append(cur
.libs().begin(), cur
.libs().end());
170 PullDependentTargets();
171 PullForwardedDependentConfigs();
172 PullRecursiveHardDeps();
176 if (settings()->build_settings()->check_for_bad_items()) {
177 if (!CheckVisibility(err
))
179 if (!CheckTestonly(err
))
181 if (!CheckNoNestedStaticLibs(err
))
183 CheckSourcesGenerated();
189 bool Target::IsLinkable() const {
190 return output_type_
== STATIC_LIBRARY
|| output_type_
== SHARED_LIBRARY
;
193 bool Target::IsFinal() const {
194 return output_type_
== EXECUTABLE
|| output_type_
== SHARED_LIBRARY
||
195 (output_type_
== STATIC_LIBRARY
&& complete_static_lib_
);
198 DepsIteratorRange
Target::GetDeps(DepsIterationType type
) const {
199 if (type
== DEPS_LINKED
) {
200 return DepsIteratorRange(DepsIterator(
201 &public_deps_
, &private_deps_
, nullptr));
204 return DepsIteratorRange(DepsIterator(
205 &public_deps_
, &private_deps_
, &data_deps_
));
208 std::string
Target::GetComputedOutputName(bool include_prefix
) const {
210 << "Toolchain must be specified before getting the computed output name.";
212 const std::string
& name
= output_name_
.empty() ? label().name()
216 if (include_prefix
) {
217 const Tool
* tool
= toolchain_
->GetToolForTargetFinalOutput(this);
218 const std::string
& prefix
= tool
->output_prefix();
219 // Only add the prefix if the name doesn't already have it.
220 if (!base::StartsWithASCII(name
, prefix
, true))
228 bool Target::SetToolchain(const Toolchain
* toolchain
, Err
* err
) {
230 DCHECK_NE(UNKNOWN
, output_type_
);
231 toolchain_
= toolchain
;
233 const Tool
* tool
= toolchain
->GetToolForTargetFinalOutput(this);
237 // Tool not specified for this target type.
239 *err
= Err(defined_from(), "This target uses an undefined tool.",
243 "uses toolchain %s\n"
244 "which doesn't have the tool \"%s\" defined.\n\n"
245 "Alas, I can not continue.",
246 label().GetUserVisibleName(false).c_str(),
247 GetStringForOutputType(output_type_
),
248 label().GetToolchainLabel().GetUserVisibleName(false).c_str(),
249 Toolchain::ToolTypeToName(
250 toolchain
->GetToolTypeForTargetFinalOutput(this)).c_str()));
255 void Target::PullDependentTarget(const Target
* dep
, bool is_public
) {
256 MergeAllDependentConfigsFrom(dep
, &configs_
, &all_dependent_configs_
);
257 MergePublicConfigsFrom(dep
, &configs_
);
259 // Direct dependent libraries.
260 if (dep
->output_type() == STATIC_LIBRARY
||
261 dep
->output_type() == SHARED_LIBRARY
||
262 dep
->output_type() == SOURCE_SET
)
263 inherited_libraries_
.Append(dep
, is_public
);
265 if (dep
->output_type() == SHARED_LIBRARY
) {
266 // Shared library dependendencies are inherited across public shared
267 // library boundaries.
270 // EXE -> INTERMEDIATE_SHLIB --[public]--> FINAL_SHLIB
271 // The EXE will also link to to FINAL_SHLIB. The public dependeny means
272 // that the EXE can use the headers in FINAL_SHLIB so the FINAL_SHLIB
273 // will need to appear on EXE's link line.
275 // However, if the dependency is private:
276 // EXE -> INTERMEDIATE_SHLIB --[private]--> FINAL_SHLIB
277 // the dependency will not be propogated because INTERMEDIATE_SHLIB is
278 // not granting permission to call functiosn from FINAL_SHLIB. If EXE
279 // wants to use functions (and link to) FINAL_SHLIB, it will need to do
282 // Static libraries and source sets aren't inherited across shared
283 // library boundaries because they will be linked into the shared
285 inherited_libraries_
.AppendPublicSharedLibraries(
286 dep
->inherited_libraries(), is_public
);
287 } else if (!dep
->IsFinal()) {
288 // The current target isn't linked, so propogate linked deps and
289 // libraries up the dependency tree.
290 inherited_libraries_
.AppendInherited(dep
->inherited_libraries(), is_public
);
292 // Inherited library settings.
293 all_lib_dirs_
.append(dep
->all_lib_dirs());
294 all_libs_
.append(dep
->all_libs());
298 void Target::PullDependentTargets() {
299 for (const auto& dep
: public_deps_
)
300 PullDependentTarget(dep
.ptr
, true);
301 for (const auto& dep
: private_deps_
)
302 PullDependentTarget(dep
.ptr
, false);
305 void Target::PullForwardedDependentConfigs() {
306 // Pull public configs from each of our dependency's public deps.
307 for (const auto& dep
: public_deps_
)
308 PullForwardedDependentConfigsFrom(dep
.ptr
);
310 // Forward public configs if explicitly requested.
311 for (const auto& dep
: forward_dependent_configs_
) {
312 const Target
* from_target
= dep
.ptr
;
314 // The forward_dependent_configs_ must be in the deps (public or private)
315 // already, so we don't need to bother copying to our configs, only
317 DCHECK(std::find_if(private_deps_
.begin(), private_deps_
.end(),
318 LabelPtrPtrEquals
<Target
>(from_target
)) !=
319 private_deps_
.end() ||
320 std::find_if(public_deps_
.begin(), public_deps_
.end(),
321 LabelPtrPtrEquals
<Target
>(from_target
)) !=
324 PullForwardedDependentConfigsFrom(from_target
);
328 void Target::PullForwardedDependentConfigsFrom(const Target
* from
) {
329 public_configs_
.Append(from
->public_configs().begin(),
330 from
->public_configs().end());
333 void Target::PullRecursiveHardDeps() {
334 for (const auto& pair
: GetDeps(DEPS_LINKED
)) {
335 if (pair
.ptr
->hard_dep())
336 recursive_hard_deps_
.insert(pair
.ptr
);
338 // Android STL doesn't like insert(begin, end) so do it manually.
339 // TODO(brettw) this can be changed to
340 // insert(iter.target()->begin(), iter.target()->end())
341 // when Android uses a better STL.
342 for (std::set
<const Target
*>::const_iterator cur
=
343 pair
.ptr
->recursive_hard_deps().begin();
344 cur
!= pair
.ptr
->recursive_hard_deps().end(); ++cur
)
345 recursive_hard_deps_
.insert(*cur
);
349 void Target::FillOutputFiles() {
350 const Tool
* tool
= toolchain_
->GetToolForTargetFinalOutput(this);
351 bool check_tool_outputs
= false;
352 switch (output_type_
) {
357 case ACTION_FOREACH
: {
358 // These don't get linked to and use stamps which should be the first
359 // entry in the outputs. These stamps are named
360 // "<target_out_dir>/<targetname>.stamp".
361 dependency_output_file_
= GetTargetOutputDirAsOutputFile(this);
362 dependency_output_file_
.value().append(GetComputedOutputName(true));
363 dependency_output_file_
.value().append(".stamp");
367 // Executables don't get linked to, but the first output is used for
368 // dependency management.
369 CHECK_GE(tool
->outputs().list().size(), 1u);
370 check_tool_outputs
= true;
371 dependency_output_file_
=
372 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
373 this, tool
, tool
->outputs().list()[0]);
376 // Static libraries both have dependencies and linking going off of the
378 CHECK(tool
->outputs().list().size() >= 1);
379 check_tool_outputs
= true;
380 link_output_file_
= dependency_output_file_
=
381 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
382 this, tool
, tool
->outputs().list()[0]);
385 CHECK(tool
->outputs().list().size() >= 1);
386 check_tool_outputs
= true;
387 if (tool
->link_output().empty() && tool
->depend_output().empty()) {
388 // Default behavior, use the first output file for both.
389 link_output_file_
= dependency_output_file_
=
390 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
391 this, tool
, tool
->outputs().list()[0]);
393 // Use the tool-specified ones.
394 if (!tool
->link_output().empty()) {
396 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
397 this, tool
, tool
->link_output());
399 if (!tool
->depend_output().empty()) {
400 dependency_output_file_
=
401 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
402 this, tool
, tool
->depend_output());
411 // Count all outputs from this tool as something generated by this target.
412 if (check_tool_outputs
) {
413 SubstitutionWriter::ApplyListToLinkerAsOutputFile(
414 this, tool
, tool
->outputs(), &computed_outputs_
);
416 // Output names aren't canonicalized in the same way that source files
417 // are. For example, the tool outputs often use
418 // {{some_var}}/{{output_name}} which expands to "./foo", but this won't
419 // match "foo" which is what we'll compute when converting a SourceFile to
421 for (auto& out
: computed_outputs_
)
422 NormalizePath(&out
.value());
425 // Also count anything the target has declared to be an output.
426 std::vector
<SourceFile
> outputs_as_sources
;
427 action_values_
.GetOutputsAsSourceFiles(this, &outputs_as_sources
);
428 for (const SourceFile
& out
: outputs_as_sources
)
429 computed_outputs_
.push_back(OutputFile(settings()->build_settings(), out
));
432 bool Target::CheckVisibility(Err
* err
) const {
433 for (const auto& pair
: GetDeps(DEPS_ALL
)) {
434 if (!Visibility::CheckItemVisibility(this, pair
.ptr
, err
))
440 bool Target::CheckTestonly(Err
* err
) const {
441 // If the current target is marked testonly, it can include both testonly
442 // and non-testonly targets, so there's nothing to check.
446 // Verify no deps have "testonly" set.
447 for (const auto& pair
: GetDeps(DEPS_ALL
)) {
448 if (pair
.ptr
->testonly()) {
449 *err
= MakeTestOnlyError(this, pair
.ptr
);
457 bool Target::CheckNoNestedStaticLibs(Err
* err
) const {
458 // If the current target is not a complete static library, it can depend on
459 // static library targets with no problem.
460 if (!(output_type() == Target::STATIC_LIBRARY
&& complete_static_lib()))
463 // Verify no deps are static libraries.
464 for (const auto& pair
: GetDeps(DEPS_ALL
)) {
465 if (pair
.ptr
->output_type() == Target::STATIC_LIBRARY
) {
466 *err
= MakeStaticLibDepsError(this, pair
.ptr
);
471 // Verify no inherited libraries are static libraries.
472 for (const auto& lib
: inherited_libraries().GetOrdered()) {
473 if (lib
->output_type() == Target::STATIC_LIBRARY
) {
474 *err
= MakeStaticLibDepsError(this, lib
);
481 void Target::CheckSourcesGenerated() const {
482 // Checks that any inputs or sources to this target that are in the build
483 // directory are generated by a target that this one transitively depends on
484 // in some way. We already guarantee that all generated files are written
487 // See Scheduler::AddUnknownGeneratedInput's declaration for more.
488 for (const SourceFile
& file
: sources_
)
489 CheckSourceGenerated(file
);
490 for (const SourceFile
& file
: inputs_
)
491 CheckSourceGenerated(file
);
494 void Target::CheckSourceGenerated(const SourceFile
& source
) const {
495 if (!IsStringInOutputDir(settings()->build_settings()->build_dir(),
497 return; // Not in output dir, this is OK.
499 // Tell the scheduler about unknown files. This will be noted for later so
500 // the list of files written by the GN build itself (often response files)
501 // can be filtered out of this list.
502 OutputFile
out_file(settings()->build_settings(), source
);
503 std::set
<const Target
*> seen_targets
;
504 if (!EnsureFileIsGeneratedByDependency(this, out_file
, true, &seen_targets
))
505 g_scheduler
->AddUnknownGeneratedInput(this, source
);