1 // Copyright 2014 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/err.h"
6 #include "tools/gn/filesystem_utils.h"
7 #include "tools/gn/functions.h"
8 #include "tools/gn/label.h"
9 #include "tools/gn/parse_tree.h"
10 #include "tools/gn/value.h"
14 const char kGetLabelInfo
[] = "get_label_info";
15 const char kGetLabelInfo_HelpShort
[] =
16 "get_label_info: Get an attribute from a target's label.";
17 const char kGetLabelInfo_Help
[] =
18 "get_label_info: Get an attribute from a target's label.\n"
20 " get_label_info(target_label, what)\n"
22 " Given the label of a target, returns some attribute of that target.\n"
23 " The target need not have been previously defined in the same file,\n"
24 " since none of the attributes depend on the actual target definition,\n"
25 " only the label itself.\n"
27 " See also \"gn help get_target_outputs\".\n"
29 "Possible values for the \"what\" parameter\n"
32 " The short name of the target. This will match the value of the\n"
33 " \"target_name\" variable inside that target's declaration. For the\n"
34 " label \"//foo/bar:baz\" this will return \"baz\".\n"
37 " The directory containing the target's definition, with no slash at\n"
38 " the end. For the label \"//foo/bar:baz\" this will return\n"
41 " \"target_gen_dir\"\n"
42 " The generated file directory for the target. This will match the\n"
43 " value of the \"target_gen_dir\" variable when inside that target's\n"
47 " The root of the generated file tree for the target. This will\n"
48 " match the value of the \"root_gen_dir\" variable when inside that\n"
49 " target's declaration.\n"
52 " The output directory for the target. This will match the\n"
53 " value of the \"target_out_dir\" variable when inside that target's\n"
57 " The root of the output file tree for the target. This will\n"
58 " match the value of the \"root_gen_dir\" variable when inside that\n"
59 " target's declaration.\n"
61 " \"label_no_toolchain\"\n"
62 " The fully qualified version of this label, not including the\n"
63 " toolchain. For the input \":bar\" it might return\n"
66 " \"label_with_toolchain\"\n"
67 " The fully qualified version of this label, including the\n"
68 " toolchain. For the input \":bar\" it might return\n"
69 " \"//foo:bar(//toolchain:x64)\".\n"
72 " The label of the toolchain. This will match the value of the\n"
73 " \"current_toolchain\" variable when inside that target's\n"
78 " get_label_info(\":foo\", \"name\")\n"
79 " # Returns string \"foo\".\n"
81 " get_label_info(\"//foo/bar:baz\", \"gen_dir\")\n"
82 " # Returns string \"//out/Debug/gen/foo/bar\".\n";
84 Value
RunGetLabelInfo(Scope
* scope
,
85 const FunctionCallNode
* function
,
86 const std::vector
<Value
>& args
,
88 if (args
.size() != 2) {
89 *err
= Err(function
, "Expected two arguments.");
93 // Resolve the requested label.
94 Label label
= Label::Resolve(scope
->GetSourceDir(),
95 ToolchainLabelForScope(scope
), args
[0], err
);
99 // Extract the "what" parameter.
100 if (!args
[1].VerifyTypeIs(Value::STRING
, err
))
102 const std::string
& what
= args
[1].string_value();
104 Value
result(function
, Value::STRING
);
105 if (what
== "name") {
106 result
.string_value() = label
.name();
108 } else if (what
== "dir") {
109 result
.string_value() = DirectoryWithNoLastSlash(label
.dir());
111 } else if (what
== "target_gen_dir") {
112 result
.string_value() = DirectoryWithNoLastSlash(
113 GetGenDirForSourceDir(scope
->settings(), label
.dir()));
115 } else if (what
== "root_gen_dir") {
116 Label toolchain_label
= label
.GetToolchainLabel();
118 scope
->settings()->default_toolchain_label() == toolchain_label
;
119 result
.string_value() = DirectoryWithNoLastSlash(
120 GetToolchainGenDir(scope
->settings()->build_settings(),
121 toolchain_label
, is_default
));
123 } else if (what
== "target_out_dir") {
124 result
.string_value() = DirectoryWithNoLastSlash(
125 GetOutputDirForSourceDir(scope
->settings(), label
.dir()));
127 } else if (what
== "root_out_dir") {
128 Label toolchain_label
= label
.GetToolchainLabel();
130 scope
->settings()->default_toolchain_label() == toolchain_label
;
131 result
.string_value() = DirectoryWithNoLastSlash(
132 GetToolchainOutputDir(scope
->settings()->build_settings(),
133 toolchain_label
, is_default
));
135 } else if (what
== "toolchain") {
136 result
.string_value() = label
.GetToolchainLabel().GetUserVisibleName(false);
138 } else if (what
== "label_no_toolchain") {
139 result
.string_value() =
140 label
.GetWithNoToolchain().GetUserVisibleName(false);
142 } else if (what
== "label_with_toolchain") {
143 result
.string_value() = label
.GetUserVisibleName(true);
146 *err
= Err(args
[1], "Unknown value for \"what\" parameter.");
153 } // namespace functions