1 // Copyright (c) 2013 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/build_settings.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/settings.h"
11 #include "tools/gn/source_dir.h"
12 #include "tools/gn/source_file.h"
13 #include "tools/gn/value.h"
19 // We want the output to match the input in terms of ending in a slash or not.
20 // Through all the transformations, these can get added or removed in various
22 void MakeSlashEndingMatchInput(const std::string
& input
, std::string
* output
) {
23 if (EndsWithSlash(input
)) {
24 if (!EndsWithSlash(*output
)) // Preserve same slash type as input.
25 output
->push_back(input
[input
.size() - 1]);
27 if (EndsWithSlash(*output
))
28 output
->resize(output
->size() - 1);
32 // Returns true if the given value looks like a directory, otherwise we'll
33 // assume it's a file.
34 bool ValueLooksLikeDir(const std::string
& value
) {
37 size_t value_size
= value
.size();
39 // Count the number of dots at the end of the string.
41 while (num_dots
< value_size
&& value
[value_size
- num_dots
- 1] == '.')
44 if (num_dots
== value
.size())
45 return true; // String is all dots.
47 if (IsSlash(value
[value_size
- num_dots
- 1]))
48 return true; // String is a [back]slash followed by 0 or more dots.
54 Value
ConvertOnePath(const Scope
* scope
,
55 const FunctionCallNode
* function
,
57 const SourceDir
& from_dir
,
58 const SourceDir
& to_dir
,
59 bool convert_to_system_absolute
,
61 Value result
; // Ensure return value optimization.
63 if (!value
.VerifyTypeIs(Value::STRING
, err
))
65 const std::string
& string_value
= value
.string_value();
67 bool looks_like_dir
= ValueLooksLikeDir(string_value
);
69 // System-absolute output special case.
70 if (convert_to_system_absolute
) {
71 base::FilePath system_path
;
73 system_path
= scope
->settings()->build_settings()->GetFullPath(
74 from_dir
.ResolveRelativeDir(string_value
,
75 scope
->settings()->build_settings()->root_path_utf8()));
77 system_path
= scope
->settings()->build_settings()->GetFullPath(
78 from_dir
.ResolveRelativeFile(string_value
,
79 scope
->settings()->build_settings()->root_path_utf8()));
81 result
= Value(function
, FilePathToUTF8(system_path
));
83 MakeSlashEndingMatchInput(string_value
, &result
.string_value());
87 result
= Value(function
, Value::STRING
);
89 result
.string_value() = RebasePath(
90 from_dir
.ResolveRelativeDir(string_value
,
91 scope
->settings()->build_settings()->root_path_utf8()).value(),
93 scope
->settings()->build_settings()->root_path_utf8());
94 MakeSlashEndingMatchInput(string_value
, &result
.string_value());
96 result
.string_value() = RebasePath(
97 from_dir
.ResolveRelativeFile(string_value
,
98 scope
->settings()->build_settings()->root_path_utf8()).value(),
100 scope
->settings()->build_settings()->root_path_utf8());
108 const char kRebasePath
[] = "rebase_path";
109 const char kRebasePath_HelpShort
[] =
110 "rebase_path: Rebase a file or directory to another location.";
111 const char kRebasePath_Help
[] =
112 "rebase_path: Rebase a file or directory to another location.\n"
114 " converted = rebase_path(input,\n"
115 " new_base = \"\",\n"
116 " current_base = \".\")\n"
118 " Takes a string argument representing a file name, or a list of such\n"
119 " strings and converts it/them to be relative to a different base\n"
122 " When invoking the compiler or scripts, GN will automatically convert\n"
123 " sources and include directories to be relative to the build directory.\n"
124 " However, if you're passing files directly in the \"args\" array or\n"
125 " doing other manual manipulations where GN doesn't know something is\n"
126 " a file name, you will need to convert paths to be relative to what\n"
127 " your tool is expecting.\n"
129 " The common case is to use this to convert paths relative to the\n"
130 " current directory to be relative to the build directory (which will\n"
131 " be the current directory when executing scripts).\n"
133 " If you want to convert a file path to be source-absolute (that is,\n"
134 " beginning with a double slash like \"//foo/bar\"), you should use\n"
135 " the get_path_info() function. This function won't work because it will\n"
136 " always make relative paths, and it needs to support making paths\n"
137 " relative to the source root, so can't also generate source-absolute\n"
138 " paths without more special-cases.\n"
143 " A string or list of strings representing file or directory names\n"
144 " These can be relative paths (\"foo/bar.txt\"), system absolute\n"
145 " paths (\"/foo/bar.txt\"), or source absolute paths\n"
146 " (\"//foo/bar.txt\").\n"
149 " The directory to convert the paths to be relative to. This can be\n"
150 " an absolute path or a relative path (which will be treated\n"
151 " as being relative to the current BUILD-file's directory).\n"
153 " As a special case, if new_base is the empty string (the default),\n"
154 " all paths will be converted to system-absolute native style paths\n"
155 " with system path separators. This is useful for invoking external\n"
159 " Directory representing the base for relative paths in the input.\n"
160 " If this is not an absolute path, it will be treated as being\n"
161 " relative to the current build file. Use \".\" (the default) to\n"
162 " convert paths from the current BUILD-file's directory.\n"
166 " The return value will be the same type as the input value (either a\n"
167 " string or a list of strings). All relative and source-absolute file\n"
168 " names will be converted to be relative to the requested output\n"
169 " System-absolute paths will be unchanged.\n"
173 " # Convert a file in the current directory to be relative to the build\n"
174 " # directory (the current dir when executing compilers and scripts).\n"
175 " foo = rebase_path(\"myfile.txt\", root_build_dir)\n"
176 " # might produce \"../../project/myfile.txt\".\n"
178 " # Convert a file to be system absolute:\n"
179 " foo = rebase_path(\"myfile.txt\")\n"
180 " # Might produce \"D:\\source\\project\\myfile.txt\" on Windows or\n"
181 " # \"/home/you/source/project/myfile.txt\" on Linux.\n"
183 " # Typical usage for converting to the build directory for a script.\n"
184 " action(\"myscript\") {\n"
185 " # Don't convert sources, GN will automatically convert these to be\n"
186 " # relative to the build directory when it constructs the command\n"
187 " # line for your script.\n"
188 " sources = [ \"foo.txt\", \"bar.txt\" ]\n"
190 " # Extra file args passed manually need to be explicitly converted\n"
191 " # to be relative to the build directory:\n"
194 " rebase_path(\"//mything/data/input.dat\", root_build_dir),\n"
196 " rebase_path(\"relative_path.txt\", root_build_dir)\n"
197 " ] + rebase_path(sources, root_build_dir)\n"
200 Value
RunRebasePath(Scope
* scope
,
201 const FunctionCallNode
* function
,
202 const std::vector
<Value
>& args
,
207 static const size_t kArgIndexInputs
= 0;
208 static const size_t kArgIndexDest
= 1;
209 static const size_t kArgIndexFrom
= 2;
212 if (args
.size() < 1 || args
.size() > 3) {
213 *err
= Err(function
->function(), "Wrong # of arguments for rebase_path.");
216 const Value
& inputs
= args
[kArgIndexInputs
];
219 bool convert_to_system_absolute
= true;
221 const SourceDir
& current_dir
= scope
->GetSourceDir();
222 if (args
.size() > kArgIndexDest
) {
223 if (!args
[kArgIndexDest
].VerifyTypeIs(Value::STRING
, err
))
225 if (!args
[kArgIndexDest
].string_value().empty()) {
226 to_dir
= current_dir
.ResolveRelativeDir(
227 args
[kArgIndexDest
].string_value(),
228 scope
->settings()->build_settings()->root_path_utf8());
229 convert_to_system_absolute
= false;
235 if (args
.size() > kArgIndexFrom
) {
236 if (!args
[kArgIndexFrom
].VerifyTypeIs(Value::STRING
, err
))
238 from_dir
= current_dir
.ResolveRelativeDir(
239 args
[kArgIndexFrom
].string_value(),
240 scope
->settings()->build_settings()->root_path_utf8());
242 // Default to current directory if unspecified.
243 from_dir
= current_dir
;
247 if (inputs
.type() == Value::STRING
) {
248 return ConvertOnePath(scope
, function
, inputs
,
249 from_dir
, to_dir
, convert_to_system_absolute
, err
);
251 } else if (inputs
.type() == Value::LIST
) {
252 result
= Value(function
, Value::LIST
);
253 result
.list_value().reserve(inputs
.list_value().size());
255 for (const auto& input
: inputs
.list_value()) {
256 result
.list_value().push_back(
257 ConvertOnePath(scope
, function
, input
,
258 from_dir
, to_dir
, convert_to_system_absolute
, err
));
259 if (err
->has_error()) {
267 *err
= Err(function
->function(),
268 "rebase_path requires a list or a string.");
272 } // namespace functions