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 SourceDir
& current_dir
,
30 const std::string
& input_string
) {
31 if (!input_string
.empty() && input_string
[input_string
.size() - 1] == '/') {
32 // Input is a directory.
33 return current_dir
.ResolveRelativeDir(input_string
);
36 // Input is a directory.
37 return current_dir
.ResolveRelativeFile(input_string
).GetDir();
40 std::string
GetOnePathInfo(const Settings
* settings
,
41 const SourceDir
& current_dir
,
45 if (!input
.VerifyTypeIs(Value::STRING
, err
))
47 const std::string
& input_string
= input
.string_value();
48 if (input_string
.empty()) {
49 *err
= Err(input
, "Calling get_path_info on an empty string.");
55 return FindFilename(&input_string
).as_string();
58 std::string file
= FindFilename(&input_string
).as_string();
59 size_t extension_offset
= FindExtensionOffset(file
);
60 if (extension_offset
== std::string::npos
)
62 // Trim extension and dot.
63 return file
.substr(0, extension_offset
- 1);
65 case WHAT_EXTENSION
: {
66 return FindExtension(&input_string
).as_string();
69 base::StringPiece dir_incl_slash
= FindDir(&input_string
);
70 if (dir_incl_slash
.empty())
71 return std::string(".");
72 // Trim slash since this function doesn't return trailing slashes. The
73 // times we don't do this are if the result is "/" and "//" since those
74 // slashes can't be trimmed.
75 if (dir_incl_slash
== "/")
76 return std::string("/.");
77 if (dir_incl_slash
== "//")
78 return std::string("//.");
79 return dir_incl_slash
.substr(0, dir_incl_slash
.size() - 1).as_string();
82 return DirectoryWithNoLastSlash(
83 GetGenDirForSourceDir(settings
,
84 DirForInput(current_dir
, input_string
)));
87 return DirectoryWithNoLastSlash(
88 GetOutputDirForSourceDir(settings
,
89 DirForInput(current_dir
, input_string
)));
92 if (!input_string
.empty() && input_string
[input_string
.size() - 1] == '/')
93 return current_dir
.ResolveRelativeDir(input_string
).value();
95 return current_dir
.ResolveRelativeFile(input_string
).value();
105 const char kGetPathInfo
[] = "get_path_info";
106 const char kGetPathInfo_HelpShort
[] =
107 "get_path_info: Extract parts of a file or directory name.";
108 const char kGetPathInfo_Help
[] =
109 "get_path_info: Extract parts of a file or directory name.\n"
111 " get_path_info(input, what)\n"
113 " The first argument is either a string representing a file or\n"
114 " directory name, or a list of such strings. If the input is a list\n"
115 " the return value will be a list containing the result of applying the\n"
116 " rule to each item in the input.\n"
118 "Possible values for the \"what\" parameter\n"
121 " The substring after the last slash in the path, including the name\n"
122 " and extension. If the input ends in a slash, the empty string will\n"
124 " \"foo/bar.txt\" => \"bar.txt\"\n"
125 " \"bar.txt\" => \"bar.txt\"\n"
126 " \"foo/\" => \"\"\n"
130 " The substring of the file name not including the extension.\n"
131 " \"foo/bar.txt\" => \"bar\"\n"
132 " \"foo/bar\" => \"bar\"\n"
133 " \"foo/\" => \"\"\n"
136 " The substring following the last period following the last slash,\n"
137 " or the empty string if not found. The period is not included.\n"
138 " \"foo/bar.txt\" => \"txt\"\n"
139 " \"foo/bar\" => \"\"\n"
142 " The directory portion of the name, not including the slash.\n"
143 " \"foo/bar.txt\" => \"foo\"\n"
144 " \"//foo/bar\" => \"//foo\"\n"
145 " \"foo\" => \".\"\n"
147 " The result will never end in a slash, so if the resulting\n"
148 " is empty, the system (\"/\") or source (\"//\") roots, a \".\"\n"
149 " will be appended such that it is always legal to append a slash\n"
150 " and a filename and get a valid path.\n"
153 " The output file directory corresponding to the path of the\n"
154 " given file, not including a trailing slash.\n"
155 " \"//foo/bar/baz.txt\" => \"//out/Default/obj/foo/bar\"\n"
158 " The generated file directory corresponding to the path of the\n"
159 " given file, not including a trailing slash.\n"
160 " \"//foo/bar/baz.txt\" => \"//out/Default/gen/foo/bar\"\n"
163 " The full absolute path name to the file or directory. It will be\n"
164 " resolved relative to the currebt directory, and then the source-\n"
165 " absolute version will be returned. If the input is system-\n"
166 " absolute, the same input will be returned.\n"
167 " \"foo/bar.txt\" => \"//mydir/foo/bar.txt\"\n"
168 " \"foo/\" => \"//mydir/foo/\"\n"
169 " \"//foo/bar\" => \"//foo/bar\" (already absolute)\n"
170 " \"/usr/include\" => \"/usr/include\" (already absolute)\n"
172 " If you want to make the path relative to another directory, or to\n"
173 " be system-absolute, see rebase_path().\n"
176 " sources = [ \"foo.cc\", \"foo.h\" ]\n"
177 " result = get_path_info(source, \"abspath\")\n"
178 " # result will be [ \"//mydir/foo.cc\", \"//mydir/foo.h\" ]\n"
180 " result = get_path_info(\"//foo/bar/baz.cc\", \"dir\")\n"
181 " # result will be \"//foo/bar\"\n"
183 " # Extract the source-absolute directory name,\n"
184 " result = get_path_info(get_path_info(path, \"dir\"), \"abspath\")\n";
186 Value
RunGetPathInfo(Scope
* scope
,
187 const FunctionCallNode
* function
,
188 const std::vector
<Value
>& args
,
190 if (args
.size() != 2) {
191 *err
= Err(function
, "Expecting two arguments to get_path_info.");
195 // Extract the "what".
196 if (!args
[1].VerifyTypeIs(Value::STRING
, err
))
199 if (args
[1].string_value() == "file") {
201 } else if (args
[1].string_value() == "name") {
203 } else if (args
[1].string_value() == "extension") {
204 what
= WHAT_EXTENSION
;
205 } else if (args
[1].string_value() == "dir") {
207 } else if (args
[1].string_value() == "out_dir") {
209 } else if (args
[1].string_value() == "gen_dir") {
211 } else if (args
[1].string_value() == "abspath") {
214 *err
= Err(args
[1], "Unknown value for 'what'.");
218 const SourceDir
& current_dir
= scope
->GetSourceDir();
219 if (args
[0].type() == Value::STRING
) {
220 return Value(function
, GetOnePathInfo(scope
->settings(), current_dir
, what
,
222 } else if (args
[0].type() == Value::LIST
) {
223 const std::vector
<Value
>& input_list
= args
[0].list_value();
224 Value
result(function
, Value::LIST
);
225 for (size_t i
= 0; i
< input_list
.size(); i
++) {
226 result
.list_value().push_back(Value(function
,
227 GetOnePathInfo(scope
->settings(), current_dir
, what
,
228 input_list
[i
], err
)));
229 if (err
->has_error())
235 *err
= Err(args
[0], "Path must be a string or a list of strings.");
239 } // namespace functions