Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / tools / gn / function_get_label_info.cc
blobdd5e5867a65bad82a1f1e5997fdddb9d0c2c18eb
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"
12 namespace functions {
14 namespace {
16 bool ToolchainIsDefault(const Scope* scope, const Label& toolchain_label) {
17 return scope->settings()->default_toolchain_label() == toolchain_label;
20 } // namespace
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"
27 "\n"
28 " get_label_info(target_label, what)\n"
29 "\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"
34 "\n"
35 " See also \"gn help get_target_outputs\".\n"
36 "\n"
37 "Possible values for the \"what\" parameter\n"
38 "\n"
39 " \"name\"\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"
43 "\n"
44 " \"dir\"\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"
47 " \"//foo/bar\".\n"
48 "\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"
52 " declaration.\n"
53 "\n"
54 " \"root_gen_dir\"\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"
58 "\n"
59 " \"target_out_dir\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"
62 " declaration.\n"
63 "\n"
64 " \"root_out_dir\"\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"
68 "\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"
72 " \"//foo:bar\".\n"
73 "\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"
78 "\n"
79 " \"toolchain\"\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"
82 " declaration.\n"
83 "\n"
84 "Examples\n"
85 "\n"
86 " get_label_info(\":foo\", \"name\")\n"
87 " # Returns string \"foo\".\n"
88 "\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,
95 Err* err) {
96 if (args.size() != 2) {
97 *err = Err(function, "Expected two arguments.");
98 return Value();
101 // Resolve the requested label.
102 Label label = Label::Resolve(scope->GetSourceDir(),
103 ToolchainLabelForScope(scope), args[0], err);
104 if (label.is_null())
105 return Value();
107 // Extract the "what" parameter.
108 if (!args[1].VerifyTypeIs(Value::STRING, err))
109 return Value();
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(),
127 toolchain_label,
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(),
141 toolchain_label,
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);
154 } else {
155 *err = Err(args[1], "Unknown value for \"what\" parameter.");
156 return Value();
159 return result;
162 } // namespace functions