blob: 7495b05c903312cfd0da7fd0e9cdac15d0a9bf82 [file] [log] [blame]
[email protected]96ea63d2013-07-30 10:17:071// 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.
4
5#include "tools/gn/filesystem_utils.h"
6
[email protected]ea3690c2013-09-23 17:59:227#include <algorithm>
8
thestig1ecdcf42014-09-12 05:09:149#include "base/files/file_util.h"
[email protected]96ea63d2013-07-30 10:17:0710#include "base/logging.h"
[email protected]f6b314d12013-08-23 21:07:4711#include "base/strings/string_util.h"
[email protected]96ea63d2013-07-30 10:17:0712#include "base/strings/utf_string_conversions.h"
13#include "build/build_config.h"
14#include "tools/gn/location.h"
[email protected]05b94b432013-11-22 22:09:5515#include "tools/gn/settings.h"
[email protected]96ea63d2013-07-30 10:17:0716#include "tools/gn/source_dir.h"
17
18namespace {
19
20enum DotDisposition {
21 // The given dot is just part of a filename and is not special.
22 NOT_A_DIRECTORY,
23
24 // The given dot is the current directory.
25 DIRECTORY_CUR,
26
27 // The given dot is the first of a double dot that should take us up one.
28 DIRECTORY_UP
29};
30
31// When we find a dot, this function is called with the character following
32// that dot to see what it is. The return value indicates what type this dot is
33// (see above). This code handles the case where the dot is at the end of the
34// input.
35//
36// |*consumed_len| will contain the number of characters in the input that
37// express what we found.
38DotDisposition ClassifyAfterDot(const std::string& path,
39 size_t after_dot,
40 size_t* consumed_len) {
41 if (after_dot == path.size()) {
42 // Single dot at the end.
43 *consumed_len = 1;
44 return DIRECTORY_CUR;
45 }
[email protected]858ceda2014-02-27 23:26:4046 if (IsSlash(path[after_dot])) {
[email protected]96ea63d2013-07-30 10:17:0747 // Single dot followed by a slash.
48 *consumed_len = 2; // Consume the slash
49 return DIRECTORY_CUR;
50 }
51
52 if (path[after_dot] == '.') {
53 // Two dots.
54 if (after_dot + 1 == path.size()) {
55 // Double dot at the end.
56 *consumed_len = 2;
57 return DIRECTORY_UP;
58 }
[email protected]858ceda2014-02-27 23:26:4059 if (IsSlash(path[after_dot + 1])) {
[email protected]96ea63d2013-07-30 10:17:0760 // Double dot folowed by a slash.
61 *consumed_len = 3;
62 return DIRECTORY_UP;
63 }
64 }
65
66 // The dots are followed by something else, not a directory.
67 *consumed_len = 1;
68 return NOT_A_DIRECTORY;
69}
70
[email protected]f6b314d12013-08-23 21:07:4771#if defined(OS_WIN)
72inline char NormalizeWindowsPathChar(char c) {
73 if (c == '/')
74 return '\\';
75 return base::ToLowerASCII(c);
76}
77
78// Attempts to do a case and slash-insensitive comparison of two 8-bit Windows
79// paths.
80bool AreAbsoluteWindowsPathsEqual(const base::StringPiece& a,
81 const base::StringPiece& b) {
82 if (a.size() != b.size())
83 return false;
84
85 // For now, just do a case-insensitive ASCII comparison. We could convert to
86 // UTF-16 and use ICU if necessary. Or maybe base::strcasecmp is good enough?
87 for (size_t i = 0; i < a.size(); i++) {
88 if (NormalizeWindowsPathChar(a[i]) != NormalizeWindowsPathChar(b[i]))
89 return false;
90 }
91 return true;
92}
93
94bool DoesBeginWindowsDriveLetter(const base::StringPiece& path) {
95 if (path.size() < 3)
96 return false;
97
98 // Check colon first, this will generally fail fastest.
99 if (path[1] != ':')
100 return false;
101
[email protected]92df9852014-05-15 21:52:35102 // Check drive letter.
103 if (!IsAsciiAlpha(path[0]))
[email protected]f6b314d12013-08-23 21:07:47104 return false;
105
[email protected]858ceda2014-02-27 23:26:40106 if (!IsSlash(path[2]))
[email protected]f6b314d12013-08-23 21:07:47107 return false;
108 return true;
109}
110#endif
111
[email protected]a623db1872014-02-19 19:11:17112// A wrapper around FilePath.GetComponents that works the way we need. This is
113// not super efficient since it does some O(n) transformations on the path. If
114// this is called a lot, we might want to optimize.
115std::vector<base::FilePath::StringType> GetPathComponents(
116 const base::FilePath& path) {
117 std::vector<base::FilePath::StringType> result;
118 path.GetComponents(&result);
119
120 if (result.empty())
121 return result;
122
123 // GetComponents will preserve the "/" at the beginning, which confuses us.
124 // We don't expect to have relative paths in this function.
125 // Don't use IsSeparator since we always want to allow backslashes.
126 if (result[0] == FILE_PATH_LITERAL("/") ||
127 result[0] == FILE_PATH_LITERAL("\\"))
128 result.erase(result.begin());
129
130#if defined(OS_WIN)
131 // On Windows, GetComponents will give us [ "C:", "/", "foo" ], and we
132 // don't want the slash in there. This doesn't support input like "C:foo"
133 // which means foo relative to the current directory of the C drive but
134 // that's basically legacy DOS behavior we don't need to support.
pkasting5c4699c22014-10-17 18:17:15135 if (result.size() >= 2 && result[1].size() == 1 &&
136 IsSlash(static_cast<char>(result[1][0])))
[email protected]a623db1872014-02-19 19:11:17137 result.erase(result.begin() + 1);
138#endif
139
140 return result;
141}
142
143// Provides the equivalent of == for filesystem strings, trying to do
144// approximately the right thing with case.
145bool FilesystemStringsEqual(const base::FilePath::StringType& a,
146 const base::FilePath::StringType& b) {
147#if defined(OS_WIN)
148 // Assume case-insensitive filesystems on Windows. We use the CompareString
149 // function to do a case-insensitive comparison based on the current locale
150 // (we don't want GN to depend on ICU which is large and requires data
151 // files). This isn't perfect, but getting this perfectly right is very
152 // difficult and requires I/O, and this comparison should cover 99.9999% of
153 // all cases.
154 //
155 // Note: The documentation for CompareString says it runs fastest on
156 // null-terminated strings with -1 passed for the length, so we do that here.
157 // There should not be embedded nulls in filesystem strings.
158 return ::CompareString(LOCALE_USER_DEFAULT, LINGUISTIC_IGNORECASE,
159 a.c_str(), -1, b.c_str(), -1) == CSTR_EQUAL;
160#else
161 // Assume case-sensitive filesystems on non-Windows.
162 return a == b;
163#endif
164}
165
[email protected]f6b314d12013-08-23 21:07:47166} // namespace
[email protected]96ea63d2013-07-30 10:17:07167
[email protected]96ea63d2013-07-30 10:17:07168const char* GetExtensionForOutputType(Target::OutputType type,
169 Settings::TargetOS os) {
170 switch (os) {
[email protected]8338eea2013-08-05 23:08:12171 case Settings::MAC:
172 switch (type) {
[email protected]8338eea2013-08-05 23:08:12173 case Target::EXECUTABLE:
174 return "";
175 case Target::SHARED_LIBRARY:
176 return "dylib";
177 case Target::STATIC_LIBRARY:
178 return "a";
[email protected]8338eea2013-08-05 23:08:12179 default:
180 NOTREACHED();
181 }
182 break;
183
[email protected]96ea63d2013-07-30 10:17:07184 case Settings::WIN:
185 switch (type) {
[email protected]96ea63d2013-07-30 10:17:07186 case Target::EXECUTABLE:
187 return "exe";
188 case Target::SHARED_LIBRARY:
189 return "dll.lib"; // Extension of import library.
190 case Target::STATIC_LIBRARY:
191 return "lib";
[email protected]c0822d7f2013-08-13 17:10:56192 default:
193 NOTREACHED();
194 }
195 break;
196
197 case Settings::LINUX:
198 switch (type) {
199 case Target::EXECUTABLE:
200 return "";
201 case Target::SHARED_LIBRARY:
202 return "so";
203 case Target::STATIC_LIBRARY:
204 return "a";
[email protected]96ea63d2013-07-30 10:17:07205 default:
206 NOTREACHED();
207 }
208 break;
209
210 default:
211 NOTREACHED();
212 }
213 return "";
214}
215
[email protected]1bcf0012013-09-19 21:13:32216std::string FilePathToUTF8(const base::FilePath::StringType& str) {
[email protected]96ea63d2013-07-30 10:17:07217#if defined(OS_WIN)
[email protected]6c3bf032013-12-25 19:37:03218 return base::WideToUTF8(str);
[email protected]96ea63d2013-07-30 10:17:07219#else
[email protected]1bcf0012013-09-19 21:13:32220 return str;
[email protected]96ea63d2013-07-30 10:17:07221#endif
222}
223
224base::FilePath UTF8ToFilePath(const base::StringPiece& sp) {
225#if defined(OS_WIN)
[email protected]6c3bf032013-12-25 19:37:03226 return base::FilePath(base::UTF8ToWide(sp));
[email protected]96ea63d2013-07-30 10:17:07227#else
228 return base::FilePath(sp.as_string());
229#endif
230}
231
232size_t FindExtensionOffset(const std::string& path) {
233 for (int i = static_cast<int>(path.size()); i >= 0; i--) {
[email protected]858ceda2014-02-27 23:26:40234 if (IsSlash(path[i]))
[email protected]96ea63d2013-07-30 10:17:07235 break;
236 if (path[i] == '.')
237 return i + 1;
238 }
239 return std::string::npos;
240}
241
242base::StringPiece FindExtension(const std::string* path) {
243 size_t extension_offset = FindExtensionOffset(*path);
244 if (extension_offset == std::string::npos)
245 return base::StringPiece();
246 return base::StringPiece(&path->data()[extension_offset],
247 path->size() - extension_offset);
248}
249
250size_t FindFilenameOffset(const std::string& path) {
251 for (int i = static_cast<int>(path.size()) - 1; i >= 0; i--) {
[email protected]858ceda2014-02-27 23:26:40252 if (IsSlash(path[i]))
[email protected]96ea63d2013-07-30 10:17:07253 return i + 1;
254 }
255 return 0; // No filename found means everything was the filename.
256}
257
258base::StringPiece FindFilename(const std::string* path) {
259 size_t filename_offset = FindFilenameOffset(*path);
260 if (filename_offset == 0)
261 return base::StringPiece(*path); // Everything is the file name.
262 return base::StringPiece(&(*path).data()[filename_offset],
263 path->size() - filename_offset);
264}
265
266base::StringPiece FindFilenameNoExtension(const std::string* path) {
267 if (path->empty())
268 return base::StringPiece();
269 size_t filename_offset = FindFilenameOffset(*path);
270 size_t extension_offset = FindExtensionOffset(*path);
271
272 size_t name_len;
273 if (extension_offset == std::string::npos)
274 name_len = path->size() - filename_offset;
275 else
276 name_len = extension_offset - filename_offset - 1;
277
278 return base::StringPiece(&(*path).data()[filename_offset], name_len);
279}
280
281void RemoveFilename(std::string* path) {
282 path->resize(FindFilenameOffset(*path));
283}
284
285bool EndsWithSlash(const std::string& s) {
[email protected]858ceda2014-02-27 23:26:40286 return !s.empty() && IsSlash(s[s.size() - 1]);
[email protected]96ea63d2013-07-30 10:17:07287}
288
289base::StringPiece FindDir(const std::string* path) {
290 size_t filename_offset = FindFilenameOffset(*path);
291 if (filename_offset == 0u)
292 return base::StringPiece();
293 return base::StringPiece(path->data(), filename_offset);
294}
295
[email protected]a3372c72014-04-28 22:39:26296base::StringPiece FindLastDirComponent(const SourceDir& dir) {
297 const std::string& dir_string = dir.value();
298
299 if (dir_string.empty())
300 return base::StringPiece();
301 int cur = static_cast<int>(dir_string.size()) - 1;
302 DCHECK(dir_string[cur] == '/');
303 int end = cur;
304 cur--; // Skip before the last slash.
305
306 for (; cur >= 0; cur--) {
307 if (dir_string[cur] == '/')
308 return base::StringPiece(&dir_string[cur + 1], end - cur - 1);
309 }
310 return base::StringPiece(&dir_string[0], end);
311}
312
[email protected]96ea63d2013-07-30 10:17:07313bool EnsureStringIsInOutputDir(const SourceDir& dir,
314 const std::string& str,
[email protected]0dfcae72014-08-19 22:52:16315 const ParseNode* origin,
[email protected]96ea63d2013-07-30 10:17:07316 Err* err) {
[email protected]96ea63d2013-07-30 10:17:07317 // This check will be wrong for all proper prefixes "e.g. "/output" will
318 // match "/out" but we don't really care since this is just a sanity check.
319 const std::string& dir_str = dir.value();
[email protected]3b6e6832014-07-14 19:25:57320 if (str.compare(0, dir_str.length(), dir_str) == 0)
[email protected]f0bbcdc22014-07-11 17:13:35321 return true; // Output directory is hardcoded.
322
[email protected]0dfcae72014-08-19 22:52:16323 *err = Err(origin, "File is not inside output directory.",
[email protected]f0bbcdc22014-07-11 17:13:35324 "The given file should be in the output directory. Normally you would "
325 "specify\n\"$target_out_dir/foo\" or "
326 "\"$target_gen_dir/foo\". I interpreted this as\n\""
327 + str + "\".");
328 return false;
[email protected]96ea63d2013-07-30 10:17:07329}
330
[email protected]ac1128e2013-08-23 00:26:56331bool IsPathAbsolute(const base::StringPiece& path) {
332 if (path.empty())
333 return false;
334
[email protected]858ceda2014-02-27 23:26:40335 if (!IsSlash(path[0])) {
[email protected]ac1128e2013-08-23 00:26:56336#if defined(OS_WIN)
337 // Check for Windows system paths like "C:\foo".
[email protected]858ceda2014-02-27 23:26:40338 if (path.size() > 2 && path[1] == ':' && IsSlash(path[2]))
[email protected]ac1128e2013-08-23 00:26:56339 return true;
340#endif
341 return false; // Doesn't begin with a slash, is relative.
342 }
343
[email protected]858ceda2014-02-27 23:26:40344 // Double forward slash at the beginning means source-relative (we don't
345 // allow backslashes for denoting this).
[email protected]ac1128e2013-08-23 00:26:56346 if (path.size() > 1 && path[1] == '/')
[email protected]858ceda2014-02-27 23:26:40347 return false;
[email protected]ac1128e2013-08-23 00:26:56348
349 return true;
350}
351
352bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root,
353 const base::StringPiece& path,
354 std::string* dest) {
355 DCHECK(IsPathAbsolute(source_root));
356 DCHECK(IsPathAbsolute(path));
357
358 dest->clear();
359
360 if (source_root.size() > path.size())
361 return false; // The source root is longer: the path can never be inside.
362
363#if defined(OS_WIN)
[email protected]858ceda2014-02-27 23:26:40364 // Source root should be canonical on Windows. Note that the initial slash
365 // must be forward slash, but that the other ones can be either forward or
366 // backward.
[email protected]ac1128e2013-08-23 00:26:56367 DCHECK(source_root.size() > 2 && source_root[0] != '/' &&
[email protected]858ceda2014-02-27 23:26:40368 source_root[1] == ':' && IsSlash(source_root[2]));
[email protected]f6b314d12013-08-23 21:07:47369
370 size_t after_common_index = std::string::npos;
371 if (DoesBeginWindowsDriveLetter(path)) {
372 // Handle "C:\foo"
373 if (AreAbsoluteWindowsPathsEqual(source_root,
374 path.substr(0, source_root.size())))
375 after_common_index = source_root.size();
376 else
377 return false;
378 } else if (path[0] == '/' && source_root.size() <= path.size() - 1 &&
379 DoesBeginWindowsDriveLetter(path.substr(1))) {
380 // Handle "/C:/foo"
381 if (AreAbsoluteWindowsPathsEqual(source_root,
382 path.substr(1, source_root.size())))
383 after_common_index = source_root.size() + 1;
384 else
385 return false;
386 } else {
387 return false;
388 }
389
390 // If we get here, there's a match and after_common_index identifies the
391 // part after it.
392
393 // The base may or may not have a trailing slash, so skip all slashes from
394 // the path after our prefix match.
395 size_t first_after_slash = after_common_index;
[email protected]858ceda2014-02-27 23:26:40396 while (first_after_slash < path.size() && IsSlash(path[first_after_slash]))
[email protected]f6b314d12013-08-23 21:07:47397 first_after_slash++;
398
399 dest->assign("//"); // Result is source root relative.
400 dest->append(&path.data()[first_after_slash],
401 path.size() - first_after_slash);
402 return true;
403
[email protected]ac1128e2013-08-23 00:26:56404#else
[email protected]f6b314d12013-08-23 21:07:47405
[email protected]ac1128e2013-08-23 00:26:56406 // On non-Windows this is easy. Since we know both are absolute, just do a
407 // prefix check.
408 if (path.substr(0, source_root.size()) == source_root) {
[email protected]ac1128e2013-08-23 00:26:56409 // The base may or may not have a trailing slash, so skip all slashes from
410 // the path after our prefix match.
411 size_t first_after_slash = source_root.size();
[email protected]858ceda2014-02-27 23:26:40412 while (first_after_slash < path.size() && IsSlash(path[first_after_slash]))
[email protected]ac1128e2013-08-23 00:26:56413 first_after_slash++;
414
[email protected]f6b314d12013-08-23 21:07:47415 dest->assign("//"); // Result is source root relative.
[email protected]ac1128e2013-08-23 00:26:56416 dest->append(&path.data()[first_after_slash],
417 path.size() - first_after_slash);
418 return true;
419 }
[email protected]ac1128e2013-08-23 00:26:56420 return false;
[email protected]f6b314d12013-08-23 21:07:47421#endif
[email protected]ac1128e2013-08-23 00:26:56422}
423
[email protected]96ea63d2013-07-30 10:17:07424void NormalizePath(std::string* path) {
tfarina9b636af2014-12-23 00:52:07425 char* pathbuf = path->empty() ? nullptr : &(*path)[0];
[email protected]96ea63d2013-07-30 10:17:07426
427 // top_index is the first character we can modify in the path. Anything
428 // before this indicates where the path is relative to.
429 size_t top_index = 0;
430 bool is_relative = true;
431 if (!path->empty() && pathbuf[0] == '/') {
432 is_relative = false;
433
434 if (path->size() > 1 && pathbuf[1] == '/') {
435 // Two leading slashes, this is a path into the source dir.
436 top_index = 2;
437 } else {
438 // One leading slash, this is a system-absolute path.
439 top_index = 1;
440 }
441 }
442
443 size_t dest_i = top_index;
444 for (size_t src_i = top_index; src_i < path->size(); /* nothing */) {
445 if (pathbuf[src_i] == '.') {
[email protected]858ceda2014-02-27 23:26:40446 if (src_i == 0 || IsSlash(pathbuf[src_i - 1])) {
[email protected]96ea63d2013-07-30 10:17:07447 // Slash followed by a dot, see if it's something special.
448 size_t consumed_len;
449 switch (ClassifyAfterDot(*path, src_i + 1, &consumed_len)) {
450 case NOT_A_DIRECTORY:
451 // Copy the dot to the output, it means nothing special.
452 pathbuf[dest_i++] = pathbuf[src_i++];
453 break;
454 case DIRECTORY_CUR:
455 // Current directory, just skip the input.
456 src_i += consumed_len;
457 break;
458 case DIRECTORY_UP:
459 // Back up over previous directory component. If we're already
460 // at the top, preserve the "..".
461 if (dest_i > top_index) {
462 // The previous char was a slash, remove it.
463 dest_i--;
464 }
465
466 if (dest_i == top_index) {
467 if (is_relative) {
468 // We're already at the beginning of a relative input, copy the
469 // ".." and continue. We need the trailing slash if there was
470 // one before (otherwise we're at the end of the input).
471 pathbuf[dest_i++] = '.';
472 pathbuf[dest_i++] = '.';
473 if (consumed_len == 3)
474 pathbuf[dest_i++] = '/';
475
476 // This also makes a new "root" that we can't delete by going
477 // up more levels. Otherwise "../.." would collapse to
478 // nothing.
479 top_index = dest_i;
480 }
481 // Otherwise we're at the beginning of an absolute path. Don't
482 // allow ".." to go up another level and just eat it.
483 } else {
484 // Just find the previous slash or the beginning of input.
[email protected]858ceda2014-02-27 23:26:40485 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1]))
[email protected]96ea63d2013-07-30 10:17:07486 dest_i--;
487 }
488 src_i += consumed_len;
489 }
490 } else {
491 // Dot not preceeded by a slash, copy it literally.
492 pathbuf[dest_i++] = pathbuf[src_i++];
493 }
[email protected]858ceda2014-02-27 23:26:40494 } else if (IsSlash(pathbuf[src_i])) {
495 if (src_i > 0 && IsSlash(pathbuf[src_i - 1])) {
[email protected]96ea63d2013-07-30 10:17:07496 // Two slashes in a row, skip over it.
497 src_i++;
498 } else {
[email protected]858ceda2014-02-27 23:26:40499 // Just one slash, copy it, normalizing to foward slash.
500 pathbuf[dest_i] = '/';
501 dest_i++;
502 src_i++;
[email protected]96ea63d2013-07-30 10:17:07503 }
504 } else {
505 // Input nothing special, just copy it.
506 pathbuf[dest_i++] = pathbuf[src_i++];
507 }
508 }
509 path->resize(dest_i);
510}
511
512void ConvertPathToSystem(std::string* path) {
513#if defined(OS_WIN)
514 for (size_t i = 0; i < path->size(); i++) {
515 if ((*path)[i] == '/')
516 (*path)[i] = '\\';
517 }
518#endif
519}
520
zeuthen6f9c64562014-11-11 21:01:13521std::string MakeRelativePath(const std::string& input,
522 const std::string& dest) {
ohrn8808c5542015-02-10 10:18:38523#if defined(OS_WIN)
524 // Make sure that absolute |input| path starts with a slash if |dest| path
525 // does. Otherwise skipping common prefixes won't work properly. Ensure the
526 // same for |dest| path too.
527 if (IsPathAbsolute(input) && !IsSlash(input[0]) && IsSlash(dest[0])) {
528 std::string corrected_input(1, dest[0]);
529 corrected_input.append(input);
530 return MakeRelativePath(corrected_input, dest);
531 }
532 if (IsPathAbsolute(dest) && !IsSlash(dest[0]) && IsSlash(input[0])) {
533 std::string corrected_dest(1, input[0]);
534 corrected_dest.append(dest);
535 return MakeRelativePath(input, corrected_dest);
536 }
537#endif
538
zeuthen6f9c64562014-11-11 21:01:13539 std::string ret;
[email protected]ea3690c2013-09-23 17:59:22540
541 // Skip the common prefixes of the source and dest as long as they end in
542 // a [back]slash.
zeuthen6f9c64562014-11-11 21:01:13543 size_t common_prefix_len = 0;
[email protected]ea3690c2013-09-23 17:59:22544 size_t max_common_length = std::min(input.size(), dest.size());
545 for (size_t i = common_prefix_len; i < max_common_length; i++) {
[email protected]858ceda2014-02-27 23:26:40546 if (IsSlash(input[i]) && IsSlash(dest[i]))
[email protected]ea3690c2013-09-23 17:59:22547 common_prefix_len = i + 1;
548 else if (input[i] != dest[i])
549 break;
550 }
551
552 // Invert the dest dir starting from the end of the common prefix.
[email protected]ea3690c2013-09-23 17:59:22553 for (size_t i = common_prefix_len; i < dest.size(); i++) {
[email protected]858ceda2014-02-27 23:26:40554 if (IsSlash(dest[i]))
[email protected]ea3690c2013-09-23 17:59:22555 ret.append("../");
556 }
557
558 // Append any remaining unique input.
559 ret.append(&input[common_prefix_len], input.size() - common_prefix_len);
560
561 // If the result is still empty, the paths are the same.
562 if (ret.empty())
563 ret.push_back('.');
564
565 return ret;
566}
[email protected]05b94b432013-11-22 22:09:55567
zeuthen6f9c64562014-11-11 21:01:13568std::string RebasePath(const std::string& input,
569 const SourceDir& dest_dir,
570 const base::StringPiece& source_root) {
571 std::string ret;
572 DCHECK(source_root.empty() || !source_root.ends_with("/"));
573
574 bool input_is_source_path = (input.size() >= 2 &&
575 input[0] == '/' && input[1] == '/');
576
577 if (!source_root.empty() &&
578 (!input_is_source_path || !dest_dir.is_source_absolute())) {
579 std::string input_full;
580 std::string dest_full;
581 if (input_is_source_path) {
582 source_root.AppendToString(&input_full);
583 input_full.push_back('/');
584 input_full.append(input, 2, std::string::npos);
585 } else {
586 input_full.append(input);
587 }
588 if (dest_dir.is_source_absolute()) {
589 source_root.AppendToString(&dest_full);
590 dest_full.push_back('/');
591 dest_full.append(dest_dir.value(), 2, std::string::npos);
592 } else {
593#if defined(OS_WIN)
594 // On Windows, SourceDir system-absolute paths start
595 // with /, e.g. "/C:/foo/bar".
596 const std::string& value = dest_dir.value();
597 if (value.size() > 2 && value[2] == ':')
598 dest_full.append(dest_dir.value().substr(1));
599 else
600 dest_full.append(dest_dir.value());
601#else
602 dest_full.append(dest_dir.value());
603#endif
604 }
605 bool remove_slash = false;
606 if (!EndsWithSlash(input_full)) {
607 input_full.push_back('/');
608 remove_slash = true;
609 }
610 ret = MakeRelativePath(input_full, dest_full);
611 if (remove_slash && ret.size() > 1)
612 ret.resize(ret.size() - 1);
613 return ret;
614 }
615
616 ret = MakeRelativePath(input, dest_dir.value());
617 return ret;
618}
619
[email protected]05b94b432013-11-22 22:09:55620std::string DirectoryWithNoLastSlash(const SourceDir& dir) {
621 std::string ret;
622
623 if (dir.value().empty()) {
624 // Just keep input the same.
625 } else if (dir.value() == "/") {
626 ret.assign("/.");
627 } else if (dir.value() == "//") {
628 ret.assign("//.");
629 } else {
630 ret.assign(dir.value());
631 ret.resize(ret.size() - 1);
632 }
633 return ret;
634}
635
[email protected]a623db1872014-02-19 19:11:17636SourceDir SourceDirForPath(const base::FilePath& source_root,
637 const base::FilePath& path) {
638 std::vector<base::FilePath::StringType> source_comp =
639 GetPathComponents(source_root);
640 std::vector<base::FilePath::StringType> path_comp =
641 GetPathComponents(path);
642
643 // See if path is inside the source root by looking for each of source root's
644 // components at the beginning of path.
645 bool is_inside_source;
scottmg865b9bb2014-12-02 10:48:29646 if (path_comp.size() < source_comp.size() || source_root.empty()) {
[email protected]a623db1872014-02-19 19:11:17647 // Too small to fit.
648 is_inside_source = false;
649 } else {
650 is_inside_source = true;
651 for (size_t i = 0; i < source_comp.size(); i++) {
652 if (!FilesystemStringsEqual(source_comp[i], path_comp[i])) {
653 is_inside_source = false;
654 break;
655 }
656 }
657 }
658
659 std::string result_str;
660 size_t initial_path_comp_to_use;
661 if (is_inside_source) {
662 // Construct a source-relative path beginning in // and skip all of the
663 // shared directories.
664 result_str = "//";
665 initial_path_comp_to_use = source_comp.size();
666 } else {
667 // Not inside source code, construct a system-absolute path.
668 result_str = "/";
669 initial_path_comp_to_use = 0;
670 }
671
672 for (size_t i = initial_path_comp_to_use; i < path_comp.size(); i++) {
673 result_str.append(FilePathToUTF8(path_comp[i]));
674 result_str.push_back('/');
675 }
676 return SourceDir(result_str);
677}
678
679SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root) {
680 base::FilePath cd;
[email protected]37b3c1992014-03-11 20:59:02681 base::GetCurrentDirectory(&cd);
[email protected]a623db1872014-02-19 19:11:17682 return SourceDirForPath(source_root, cd);
683}
684
[email protected]7380ca72014-05-13 16:56:20685std::string GetOutputSubdirName(const Label& toolchain_label, bool is_default) {
686 // The default toolchain has no subdir.
687 if (is_default)
688 return std::string();
689
690 // For now just assume the toolchain name is always a valid dir name. We may
691 // want to clean up the in the future.
692 return toolchain_label.name() + "/";
693}
694
[email protected]05b94b432013-11-22 22:09:55695SourceDir GetToolchainOutputDir(const Settings* settings) {
[email protected]0dfcae72014-08-19 22:52:16696 return settings->toolchain_output_subdir().AsSourceDir(
697 settings->build_settings());
[email protected]05b94b432013-11-22 22:09:55698}
699
[email protected]7380ca72014-05-13 16:56:20700SourceDir GetToolchainOutputDir(const BuildSettings* build_settings,
701 const Label& toolchain_label, bool is_default) {
702 std::string result = build_settings->build_dir().value();
703 result.append(GetOutputSubdirName(toolchain_label, is_default));
704 return SourceDir(SourceDir::SWAP_IN, &result);
705}
706
[email protected]05b94b432013-11-22 22:09:55707SourceDir GetToolchainGenDir(const Settings* settings) {
[email protected]0dfcae72014-08-19 22:52:16708 return GetToolchainGenDirAsOutputFile(settings).AsSourceDir(
709 settings->build_settings());
710}
[email protected]05b94b432013-11-22 22:09:55711
[email protected]0dfcae72014-08-19 22:52:16712OutputFile GetToolchainGenDirAsOutputFile(const Settings* settings) {
713 OutputFile result(settings->toolchain_output_subdir());
714 result.value().append("gen/");
715 return result;
[email protected]05b94b432013-11-22 22:09:55716}
717
[email protected]7380ca72014-05-13 16:56:20718SourceDir GetToolchainGenDir(const BuildSettings* build_settings,
719 const Label& toolchain_label, bool is_default) {
720 std::string result = GetToolchainOutputDir(
721 build_settings, toolchain_label, is_default).value();
722 result.append("gen/");
723 return SourceDir(SourceDir::SWAP_IN, &result);
724}
725
[email protected]05b94b432013-11-22 22:09:55726SourceDir GetOutputDirForSourceDir(const Settings* settings,
727 const SourceDir& source_dir) {
[email protected]0dfcae72014-08-19 22:52:16728 return GetOutputDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir(
729 settings->build_settings());
730}
[email protected]05b94b432013-11-22 22:09:55731
[email protected]0dfcae72014-08-19 22:52:16732OutputFile GetOutputDirForSourceDirAsOutputFile(const Settings* settings,
733 const SourceDir& source_dir) {
734 OutputFile result = settings->toolchain_output_subdir();
735 result.value().append("obj/");
[email protected]05b94b432013-11-22 22:09:55736
[email protected]e9bf4fc2014-06-19 16:50:52737 if (source_dir.is_source_absolute()) {
738 // The source dir is source-absolute, so we trim off the two leading
739 // slashes to append to the toolchain object directory.
[email protected]0dfcae72014-08-19 22:52:16740 result.value().append(&source_dir.value()[2],
741 source_dir.value().size() - 2);
zeuthen6f9c64562014-11-11 21:01:13742 } else {
743 // system-absolute
744 const std::string& build_dir =
745 settings->build_settings()->build_dir().value();
746
747 if (StartsWithASCII(source_dir.value(), build_dir, true)) {
748 size_t build_dir_size = build_dir.size();
749 result.value().append(&source_dir.value()[build_dir_size],
750 source_dir.value().size() - build_dir_size);
ohrn8808c5542015-02-10 10:18:38751 } else {
752 result.value().append("ABS_PATH");
753#if defined(OS_WIN)
754 // Windows absolute path contains ':' after drive letter. Remove it to
755 // avoid inserting ':' in the middle of path (eg. "ABS_PATH/C:/").
756 std::string src_dir_value = source_dir.value();
757 const auto colon_pos = src_dir_value.find(':');
758 if (colon_pos != std::string::npos)
759 src_dir_value.erase(src_dir_value.begin() + colon_pos);
760#else
761 const std::string& src_dir_value = source_dir.value();
762#endif
763 result.value().append(src_dir_value);
zeuthen6f9c64562014-11-11 21:01:13764 }
[email protected]e9bf4fc2014-06-19 16:50:52765 }
[email protected]0dfcae72014-08-19 22:52:16766 return result;
[email protected]05b94b432013-11-22 22:09:55767}
768
769SourceDir GetGenDirForSourceDir(const Settings* settings,
770 const SourceDir& source_dir) {
[email protected]0dfcae72014-08-19 22:52:16771 return GetGenDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir(
772 settings->build_settings());
773}
[email protected]05b94b432013-11-22 22:09:55774
[email protected]0dfcae72014-08-19 22:52:16775OutputFile GetGenDirForSourceDirAsOutputFile(const Settings* settings,
776 const SourceDir& source_dir) {
777 OutputFile result = GetToolchainGenDirAsOutputFile(settings);
[email protected]05b94b432013-11-22 22:09:55778
[email protected]e9bf4fc2014-06-19 16:50:52779 if (source_dir.is_source_absolute()) {
780 // The source dir should be source-absolute, so we trim off the two leading
781 // slashes to append to the toolchain object directory.
782 DCHECK(source_dir.is_source_absolute());
[email protected]0dfcae72014-08-19 22:52:16783 result.value().append(&source_dir.value()[2],
784 source_dir.value().size() - 2);
[email protected]e9bf4fc2014-06-19 16:50:52785 }
[email protected]0dfcae72014-08-19 22:52:16786 return result;
[email protected]05b94b432013-11-22 22:09:55787}
788
789SourceDir GetTargetOutputDir(const Target* target) {
[email protected]0dfcae72014-08-19 22:52:16790 return GetOutputDirForSourceDirAsOutputFile(
791 target->settings(), target->label().dir()).AsSourceDir(
792 target->settings()->build_settings());
793}
794
795OutputFile GetTargetOutputDirAsOutputFile(const Target* target) {
796 return GetOutputDirForSourceDirAsOutputFile(
797 target->settings(), target->label().dir());
[email protected]05b94b432013-11-22 22:09:55798}
799
800SourceDir GetTargetGenDir(const Target* target) {
[email protected]0dfcae72014-08-19 22:52:16801 return GetTargetGenDirAsOutputFile(target).AsSourceDir(
802 target->settings()->build_settings());
803}
804
805OutputFile GetTargetGenDirAsOutputFile(const Target* target) {
806 return GetGenDirForSourceDirAsOutputFile(
807 target->settings(), target->label().dir());
[email protected]05b94b432013-11-22 22:09:55808}
809
810SourceDir GetCurrentOutputDir(const Scope* scope) {
[email protected]0dfcae72014-08-19 22:52:16811 return GetOutputDirForSourceDirAsOutputFile(
812 scope->settings(), scope->GetSourceDir()).AsSourceDir(
813 scope->settings()->build_settings());
[email protected]05b94b432013-11-22 22:09:55814}
815
816SourceDir GetCurrentGenDir(const Scope* scope) {
817 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir());
818}