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/parse_tree.h"
9 #include "tools/gn/scope.h"
10 #include "tools/gn/value.h"
16 // Corresponds to the various values of "what" in the function call.
27 // Returns the directory containing the input (resolving it against the
28 // |current_dir|), regardless of whether the input is a directory or a file.
29 SourceDir
DirForInput(const Settings
* settings
,
30 const SourceDir
& current_dir
,
31 const std::string
& input_string
) {
32 if (!input_string
.empty() && input_string
[input_string
.size() - 1] == '/') {
33 // Input is a directory.
34 return current_dir
.ResolveRelativeDir(input_string
,
35 settings
->build_settings()->root_path_utf8());
38 // Input is a directory.
39 return current_dir
.ResolveRelativeFile(input_string
,
40 settings
->build_settings()->root_path_utf8()).GetDir();
43 std::string
GetOnePathInfo(const Settings
* settings
,
44 const SourceDir
& current_dir
,
48 if (!input
.VerifyTypeIs(Value::STRING
, err
))
50 const std::string
& input_string
= input
.string_value();
51 if (input_string
.empty()) {
52 *err
= Err(input
, "Calling get_path_info on an empty string.");
58 return FindFilename(&input_string
).as_string();
61 std::string file
= FindFilename(&input_string
).as_string();
62 size_t extension_offset
= FindExtensionOffset(file
);
63 if (extension_offset
== std::string::npos
)
65 // Trim extension and dot.
66 return file
.substr(0, extension_offset
- 1);
68 case WHAT_EXTENSION
: {
69 return FindExtension(&input_string
).as_string();
72 base::StringPiece dir_incl_slash
= FindDir(&input_string
);
73 if (dir_incl_slash
.empty())
74 return std::string(".");
75 // Trim slash since this function doesn't return trailing slashes. The
76 // times we don't do this are if the result is "/" and "//" since those
77 // slashes can't be trimmed.
78 if (dir_incl_slash
== "/")
79 return std::string("/.");
80 if (dir_incl_slash
== "//")
81 return std::string("//.");
82 return dir_incl_slash
.substr(0, dir_incl_slash
.size() - 1).as_string();
85 return DirectoryWithNoLastSlash(
86 GetGenDirForSourceDir(settings
,
87 DirForInput(settings
, current_dir
,
91 return DirectoryWithNoLastSlash(
92 GetOutputDirForSourceDir(settings
,
93 DirForInput(settings
, current_dir
,
97 if (!input_string
.empty() &&
98 input_string
[input_string
.size() - 1] == '/') {
99 return current_dir
.ResolveRelativeDir(input_string
,
100 settings
->build_settings()->root_path_utf8()).value();
102 return current_dir
.ResolveRelativeFile(input_string
,
103 settings
->build_settings()->root_path_utf8()).value();
108 return std::string();
114 const char kGetPathInfo
[] = "get_path_info";
115 const char kGetPathInfo_HelpShort
[] =
116 "get_path_info: Extract parts of a file or directory name.";
117 const char kGetPathInfo_Help
[] =
118 "get_path_info: Extract parts of a file or directory name.\n"
120 " get_path_info(input, what)\n"
122 " The first argument is either a string representing a file or\n"
123 " directory name, or a list of such strings. If the input is a list\n"
124 " the return value will be a list containing the result of applying the\n"
125 " rule to each item in the input.\n"
127 "Possible values for the \"what\" parameter\n"
130 " The substring after the last slash in the path, including the name\n"
131 " and extension. If the input ends in a slash, the empty string will\n"
133 " \"foo/bar.txt\" => \"bar.txt\"\n"
134 " \"bar.txt\" => \"bar.txt\"\n"
135 " \"foo/\" => \"\"\n"
139 " The substring of the file name not including the extension.\n"
140 " \"foo/bar.txt\" => \"bar\"\n"
141 " \"foo/bar\" => \"bar\"\n"
142 " \"foo/\" => \"\"\n"
145 " The substring following the last period following the last slash,\n"
146 " or the empty string if not found. The period is not included.\n"
147 " \"foo/bar.txt\" => \"txt\"\n"
148 " \"foo/bar\" => \"\"\n"
151 " The directory portion of the name, not including the slash.\n"
152 " \"foo/bar.txt\" => \"foo\"\n"
153 " \"//foo/bar\" => \"//foo\"\n"
154 " \"foo\" => \".\"\n"
156 " The result will never end in a slash, so if the resulting\n"
157 " is empty, the system (\"/\") or source (\"//\") roots, a \".\"\n"
158 " will be appended such that it is always legal to append a slash\n"
159 " and a filename and get a valid path.\n"
162 " The output file directory corresponding to the path of the\n"
163 " given file, not including a trailing slash.\n"
164 " \"//foo/bar/baz.txt\" => \"//out/Default/obj/foo/bar\"\n"
167 " The generated file directory corresponding to the path of the\n"
168 " given file, not including a trailing slash.\n"
169 " \"//foo/bar/baz.txt\" => \"//out/Default/gen/foo/bar\"\n"
172 " The full absolute path name to the file or directory. It will be\n"
173 " resolved relative to the currebt directory, and then the source-\n"
174 " absolute version will be returned. If the input is system-\n"
175 " absolute, the same input will be returned.\n"
176 " \"foo/bar.txt\" => \"//mydir/foo/bar.txt\"\n"
177 " \"foo/\" => \"//mydir/foo/\"\n"
178 " \"//foo/bar\" => \"//foo/bar\" (already absolute)\n"
179 " \"/usr/include\" => \"/usr/include\" (already absolute)\n"
181 " If you want to make the path relative to another directory, or to\n"
182 " be system-absolute, see rebase_path().\n"
185 " sources = [ \"foo.cc\", \"foo.h\" ]\n"
186 " result = get_path_info(source, \"abspath\")\n"
187 " # result will be [ \"//mydir/foo.cc\", \"//mydir/foo.h\" ]\n"
189 " result = get_path_info(\"//foo/bar/baz.cc\", \"dir\")\n"
190 " # result will be \"//foo/bar\"\n"
192 " # Extract the source-absolute directory name,\n"
193 " result = get_path_info(get_path_info(path, \"dir\"), \"abspath\")\n";
195 Value
RunGetPathInfo(Scope
* scope
,
196 const FunctionCallNode
* function
,
197 const std::vector
<Value
>& args
,
199 if (args
.size() != 2) {
200 *err
= Err(function
, "Expecting two arguments to get_path_info.");
204 // Extract the "what".
205 if (!args
[1].VerifyTypeIs(Value::STRING
, err
))
208 if (args
[1].string_value() == "file") {
210 } else if (args
[1].string_value() == "name") {
212 } else if (args
[1].string_value() == "extension") {
213 what
= WHAT_EXTENSION
;
214 } else if (args
[1].string_value() == "dir") {
216 } else if (args
[1].string_value() == "out_dir") {
218 } else if (args
[1].string_value() == "gen_dir") {
220 } else if (args
[1].string_value() == "abspath") {
223 *err
= Err(args
[1], "Unknown value for 'what'.");
227 const SourceDir
& current_dir
= scope
->GetSourceDir();
228 if (args
[0].type() == Value::STRING
) {
229 return Value(function
, GetOnePathInfo(scope
->settings(), current_dir
, what
,
231 } else if (args
[0].type() == Value::LIST
) {
232 const std::vector
<Value
>& input_list
= args
[0].list_value();
233 Value
result(function
, Value::LIST
);
234 for (const auto& cur
: input_list
) {
235 result
.list_value().push_back(Value(function
,
236 GetOnePathInfo(scope
->settings(), current_dir
, what
, cur
, err
)));
237 if (err
->has_error())
243 *err
= Err(args
[0], "Path must be a string or a list of strings.");
247 } // namespace functions