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"
16 bool ToolchainIsDefault(const Scope
* scope
, const Label
& toolchain_label
) {
17 return scope
->settings()->default_toolchain_label() == toolchain_label
;
22 const char kGetLabelInfo
[] = "get_label_info";
23 const char kGetLabelInfo_HelpShort
[] =
24 "get_label_info: Get an attribute from a target's label.";
25 const char kGetLabelInfo_Help
[] =
26 "get_label_info: Get an attribute from a target's label.\n"
28 " get_label_info(target_label, what)\n"
30 " Given the label of a target, returns some attribute of that target.\n"
31 " The target need not have been previously defined in the same file,\n"
32 " since none of the attributes depend on the actual target definition,\n"
33 " only the label itself.\n"
35 " See also \"gn help get_target_outputs\".\n"
37 "Possible values for the \"what\" parameter\n"
40 " The short name of the target. This will match the value of the\n"
41 " \"target_name\" variable inside that target's declaration. For the\n"
42 " label \"//foo/bar:baz\" this will return \"baz\".\n"
45 " The directory containing the target's definition, with no slash at\n"
46 " the end. For the label \"//foo/bar:baz\" this will return\n"
49 " \"target_gen_dir\"\n"
50 " The generated file directory for the target. This will match the\n"
51 " value of the \"target_gen_dir\" variable when inside that target's\n"
55 " The root of the generated file tree for the target. This will\n"
56 " match the value of the \"root_gen_dir\" variable when inside that\n"
57 " target's declaration.\n"
60 " The output directory for the target. This will match the\n"
61 " value of the \"target_out_dir\" variable when inside that target's\n"
65 " The root of the output file tree for the target. This will\n"
66 " match the value of the \"root_gen_dir\" variable when inside that\n"
67 " target's declaration.\n"
69 " \"label_no_toolchain\"\n"
70 " The fully qualified version of this label, not including the\n"
71 " toolchain. For the input \":bar\" it might return\n"
74 " \"label_with_toolchain\"\n"
75 " The fully qualified version of this label, including the\n"
76 " toolchain. For the input \":bar\" it might return\n"
77 " \"//foo:bar(//toolchain:x64)\".\n"
80 " The label of the toolchain. This will match the value of the\n"
81 " \"current_toolchain\" variable when inside that target's\n"
86 " get_label_info(\":foo\", \"name\")\n"
87 " # Returns string \"foo\".\n"
89 " get_label_info(\"//foo/bar:baz\", \"gen_dir\")\n"
90 " # Returns string \"//out/Debug/gen/foo/bar\".\n";
92 Value
RunGetLabelInfo(Scope
* scope
,
93 const FunctionCallNode
* function
,
94 const std::vector
<Value
>& args
,
96 if (args
.size() != 2) {
97 *err
= Err(function
, "Expected two arguments.");
101 // Resolve the requested label.
102 Label label
= Label::Resolve(scope
->GetSourceDir(),
103 ToolchainLabelForScope(scope
), args
[0], err
);
107 // Extract the "what" parameter.
108 if (!args
[1].VerifyTypeIs(Value::STRING
, err
))
110 const std::string
& what
= args
[1].string_value();
112 Value
result(function
, Value::STRING
);
113 if (what
== "name") {
114 result
.string_value() = label
.name();
116 } else if (what
== "dir") {
117 result
.string_value() = DirectoryWithNoLastSlash(label
.dir());
119 } else if (what
== "target_gen_dir") {
120 result
.string_value() = DirectoryWithNoLastSlash(
121 GetGenDirForSourceDir(scope
->settings(), label
.dir()));
123 } else if (what
== "root_gen_dir") {
124 Label toolchain_label
= label
.GetToolchainLabel();
125 result
.string_value() = DirectoryWithNoLastSlash(
126 GetToolchainGenDir(scope
->settings()->build_settings(),
128 ToolchainIsDefault(scope
, toolchain_label
)));
130 } else if (what
== "target_out_dir") {
131 Label toolchain_label
= label
.GetToolchainLabel();
132 result
.string_value() = DirectoryWithNoLastSlash(
133 GetOutputDirForSourceDir(scope
->settings()->build_settings(),
134 label
.dir(), toolchain_label
,
135 ToolchainIsDefault(scope
, toolchain_label
)));
137 } else if (what
== "root_out_dir") {
138 Label toolchain_label
= label
.GetToolchainLabel();
139 result
.string_value() = DirectoryWithNoLastSlash(
140 GetToolchainOutputDir(scope
->settings()->build_settings(),
142 ToolchainIsDefault(scope
, toolchain_label
)));
144 } else if (what
== "toolchain") {
145 result
.string_value() = label
.GetToolchainLabel().GetUserVisibleName(false);
147 } else if (what
== "label_no_toolchain") {
148 result
.string_value() =
149 label
.GetWithNoToolchain().GetUserVisibleName(false);
151 } else if (what
== "label_with_toolchain") {
152 result
.string_value() = label
.GetUserVisibleName(true);
155 *err
= Err(args
[1], "Unknown value for \"what\" parameter.");
162 } // namespace functions