blob: 1e4194cab2c29a3594cdf14c0ac35310a0b429ea [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:07424std::string InvertDir(const SourceDir& path) {
425 const std::string value = path.value();
426 if (value.empty())
427 return std::string();
428
429 DCHECK(value[0] == '/');
430 size_t begin_index = 1;
431
432 // If the input begins with two slashes, skip over both (this is a
[email protected]858ceda2014-02-27 23:26:40433 // source-relative dir). These must be forward slashes only.
[email protected]96ea63d2013-07-30 10:17:07434 if (value.size() > 1 && value[1] == '/')
435 begin_index = 2;
436
437 std::string ret;
438 for (size_t i = begin_index; i < value.size(); i++) {
[email protected]858ceda2014-02-27 23:26:40439 if (IsSlash(value[i]))
[email protected]96ea63d2013-07-30 10:17:07440 ret.append("../");
441 }
442 return ret;
443}
444
445void NormalizePath(std::string* path) {
446 char* pathbuf = path->empty() ? NULL : &(*path)[0];
447
448 // top_index is the first character we can modify in the path. Anything
449 // before this indicates where the path is relative to.
450 size_t top_index = 0;
451 bool is_relative = true;
452 if (!path->empty() && pathbuf[0] == '/') {
453 is_relative = false;
454
455 if (path->size() > 1 && pathbuf[1] == '/') {
456 // Two leading slashes, this is a path into the source dir.
457 top_index = 2;
458 } else {
459 // One leading slash, this is a system-absolute path.
460 top_index = 1;
461 }
462 }
463
464 size_t dest_i = top_index;
465 for (size_t src_i = top_index; src_i < path->size(); /* nothing */) {
466 if (pathbuf[src_i] == '.') {
[email protected]858ceda2014-02-27 23:26:40467 if (src_i == 0 || IsSlash(pathbuf[src_i - 1])) {
[email protected]96ea63d2013-07-30 10:17:07468 // Slash followed by a dot, see if it's something special.
469 size_t consumed_len;
470 switch (ClassifyAfterDot(*path, src_i + 1, &consumed_len)) {
471 case NOT_A_DIRECTORY:
472 // Copy the dot to the output, it means nothing special.
473 pathbuf[dest_i++] = pathbuf[src_i++];
474 break;
475 case DIRECTORY_CUR:
476 // Current directory, just skip the input.
477 src_i += consumed_len;
478 break;
479 case DIRECTORY_UP:
480 // Back up over previous directory component. If we're already
481 // at the top, preserve the "..".
482 if (dest_i > top_index) {
483 // The previous char was a slash, remove it.
484 dest_i--;
485 }
486
487 if (dest_i == top_index) {
488 if (is_relative) {
489 // We're already at the beginning of a relative input, copy the
490 // ".." and continue. We need the trailing slash if there was
491 // one before (otherwise we're at the end of the input).
492 pathbuf[dest_i++] = '.';
493 pathbuf[dest_i++] = '.';
494 if (consumed_len == 3)
495 pathbuf[dest_i++] = '/';
496
497 // This also makes a new "root" that we can't delete by going
498 // up more levels. Otherwise "../.." would collapse to
499 // nothing.
500 top_index = dest_i;
501 }
502 // Otherwise we're at the beginning of an absolute path. Don't
503 // allow ".." to go up another level and just eat it.
504 } else {
505 // Just find the previous slash or the beginning of input.
[email protected]858ceda2014-02-27 23:26:40506 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1]))
[email protected]96ea63d2013-07-30 10:17:07507 dest_i--;
508 }
509 src_i += consumed_len;
510 }
511 } else {
512 // Dot not preceeded by a slash, copy it literally.
513 pathbuf[dest_i++] = pathbuf[src_i++];
514 }
[email protected]858ceda2014-02-27 23:26:40515 } else if (IsSlash(pathbuf[src_i])) {
516 if (src_i > 0 && IsSlash(pathbuf[src_i - 1])) {
[email protected]96ea63d2013-07-30 10:17:07517 // Two slashes in a row, skip over it.
518 src_i++;
519 } else {
[email protected]858ceda2014-02-27 23:26:40520 // Just one slash, copy it, normalizing to foward slash.
521 pathbuf[dest_i] = '/';
522 dest_i++;
523 src_i++;
[email protected]96ea63d2013-07-30 10:17:07524 }
525 } else {
526 // Input nothing special, just copy it.
527 pathbuf[dest_i++] = pathbuf[src_i++];
528 }
529 }
530 path->resize(dest_i);
531}
532
533void ConvertPathToSystem(std::string* path) {
534#if defined(OS_WIN)
535 for (size_t i = 0; i < path->size(); i++) {
536 if ((*path)[i] == '/')
537 (*path)[i] = '\\';
538 }
539#endif
540}
541
[email protected]ea3690c2013-09-23 17:59:22542std::string RebaseSourceAbsolutePath(const std::string& input,
543 const SourceDir& dest_dir) {
544 CHECK(input.size() >= 2 && input[0] == '/' && input[1] == '/')
545 << "Input to rebase isn't source-absolute: " << input;
546 CHECK(dest_dir.is_source_absolute())
547 << "Dir to rebase to isn't source-absolute: " << dest_dir.value();
548
549 const std::string& dest = dest_dir.value();
550
551 // Skip the common prefixes of the source and dest as long as they end in
552 // a [back]slash.
553 size_t common_prefix_len = 2; // The beginning two "//" are always the same.
554 size_t max_common_length = std::min(input.size(), dest.size());
555 for (size_t i = common_prefix_len; i < max_common_length; i++) {
[email protected]858ceda2014-02-27 23:26:40556 if (IsSlash(input[i]) && IsSlash(dest[i]))
[email protected]ea3690c2013-09-23 17:59:22557 common_prefix_len = i + 1;
558 else if (input[i] != dest[i])
559 break;
560 }
561
562 // Invert the dest dir starting from the end of the common prefix.
563 std::string ret;
564 for (size_t i = common_prefix_len; i < dest.size(); i++) {
[email protected]858ceda2014-02-27 23:26:40565 if (IsSlash(dest[i]))
[email protected]ea3690c2013-09-23 17:59:22566 ret.append("../");
567 }
568
569 // Append any remaining unique input.
570 ret.append(&input[common_prefix_len], input.size() - common_prefix_len);
571
572 // If the result is still empty, the paths are the same.
573 if (ret.empty())
574 ret.push_back('.');
575
576 return ret;
577}
[email protected]05b94b432013-11-22 22:09:55578
579std::string DirectoryWithNoLastSlash(const SourceDir& dir) {
580 std::string ret;
581
582 if (dir.value().empty()) {
583 // Just keep input the same.
584 } else if (dir.value() == "/") {
585 ret.assign("/.");
586 } else if (dir.value() == "//") {
587 ret.assign("//.");
588 } else {
589 ret.assign(dir.value());
590 ret.resize(ret.size() - 1);
591 }
592 return ret;
593}
594
[email protected]a623db1872014-02-19 19:11:17595SourceDir SourceDirForPath(const base::FilePath& source_root,
596 const base::FilePath& path) {
597 std::vector<base::FilePath::StringType> source_comp =
598 GetPathComponents(source_root);
599 std::vector<base::FilePath::StringType> path_comp =
600 GetPathComponents(path);
601
602 // See if path is inside the source root by looking for each of source root's
603 // components at the beginning of path.
604 bool is_inside_source;
605 if (path_comp.size() < source_comp.size()) {
606 // Too small to fit.
607 is_inside_source = false;
608 } else {
609 is_inside_source = true;
610 for (size_t i = 0; i < source_comp.size(); i++) {
611 if (!FilesystemStringsEqual(source_comp[i], path_comp[i])) {
612 is_inside_source = false;
613 break;
614 }
615 }
616 }
617
618 std::string result_str;
619 size_t initial_path_comp_to_use;
620 if (is_inside_source) {
621 // Construct a source-relative path beginning in // and skip all of the
622 // shared directories.
623 result_str = "//";
624 initial_path_comp_to_use = source_comp.size();
625 } else {
626 // Not inside source code, construct a system-absolute path.
627 result_str = "/";
628 initial_path_comp_to_use = 0;
629 }
630
631 for (size_t i = initial_path_comp_to_use; i < path_comp.size(); i++) {
632 result_str.append(FilePathToUTF8(path_comp[i]));
633 result_str.push_back('/');
634 }
635 return SourceDir(result_str);
636}
637
638SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root) {
639 base::FilePath cd;
[email protected]37b3c1992014-03-11 20:59:02640 base::GetCurrentDirectory(&cd);
[email protected]a623db1872014-02-19 19:11:17641 return SourceDirForPath(source_root, cd);
642}
643
[email protected]7380ca72014-05-13 16:56:20644std::string GetOutputSubdirName(const Label& toolchain_label, bool is_default) {
645 // The default toolchain has no subdir.
646 if (is_default)
647 return std::string();
648
649 // For now just assume the toolchain name is always a valid dir name. We may
650 // want to clean up the in the future.
651 return toolchain_label.name() + "/";
652}
653
[email protected]05b94b432013-11-22 22:09:55654SourceDir GetToolchainOutputDir(const Settings* settings) {
[email protected]0dfcae72014-08-19 22:52:16655 return settings->toolchain_output_subdir().AsSourceDir(
656 settings->build_settings());
[email protected]05b94b432013-11-22 22:09:55657}
658
[email protected]7380ca72014-05-13 16:56:20659SourceDir GetToolchainOutputDir(const BuildSettings* build_settings,
660 const Label& toolchain_label, bool is_default) {
661 std::string result = build_settings->build_dir().value();
662 result.append(GetOutputSubdirName(toolchain_label, is_default));
663 return SourceDir(SourceDir::SWAP_IN, &result);
664}
665
[email protected]05b94b432013-11-22 22:09:55666SourceDir GetToolchainGenDir(const Settings* settings) {
[email protected]0dfcae72014-08-19 22:52:16667 return GetToolchainGenDirAsOutputFile(settings).AsSourceDir(
668 settings->build_settings());
669}
[email protected]05b94b432013-11-22 22:09:55670
[email protected]0dfcae72014-08-19 22:52:16671OutputFile GetToolchainGenDirAsOutputFile(const Settings* settings) {
672 OutputFile result(settings->toolchain_output_subdir());
673 result.value().append("gen/");
674 return result;
[email protected]05b94b432013-11-22 22:09:55675}
676
[email protected]7380ca72014-05-13 16:56:20677SourceDir GetToolchainGenDir(const BuildSettings* build_settings,
678 const Label& toolchain_label, bool is_default) {
679 std::string result = GetToolchainOutputDir(
680 build_settings, toolchain_label, is_default).value();
681 result.append("gen/");
682 return SourceDir(SourceDir::SWAP_IN, &result);
683}
684
[email protected]05b94b432013-11-22 22:09:55685SourceDir GetOutputDirForSourceDir(const Settings* settings,
686 const SourceDir& source_dir) {
[email protected]0dfcae72014-08-19 22:52:16687 return GetOutputDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir(
688 settings->build_settings());
689}
[email protected]05b94b432013-11-22 22:09:55690
[email protected]0dfcae72014-08-19 22:52:16691OutputFile GetOutputDirForSourceDirAsOutputFile(const Settings* settings,
692 const SourceDir& source_dir) {
693 OutputFile result = settings->toolchain_output_subdir();
694 result.value().append("obj/");
[email protected]05b94b432013-11-22 22:09:55695
[email protected]e9bf4fc2014-06-19 16:50:52696 if (source_dir.is_source_absolute()) {
697 // The source dir is source-absolute, so we trim off the two leading
698 // slashes to append to the toolchain object directory.
[email protected]0dfcae72014-08-19 22:52:16699 result.value().append(&source_dir.value()[2],
700 source_dir.value().size() - 2);
[email protected]e9bf4fc2014-06-19 16:50:52701 }
[email protected]0dfcae72014-08-19 22:52:16702 return result;
[email protected]05b94b432013-11-22 22:09:55703}
704
705SourceDir GetGenDirForSourceDir(const Settings* settings,
706 const SourceDir& source_dir) {
[email protected]0dfcae72014-08-19 22:52:16707 return GetGenDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir(
708 settings->build_settings());
709}
[email protected]05b94b432013-11-22 22:09:55710
[email protected]0dfcae72014-08-19 22:52:16711OutputFile GetGenDirForSourceDirAsOutputFile(const Settings* settings,
712 const SourceDir& source_dir) {
713 OutputFile result = GetToolchainGenDirAsOutputFile(settings);
[email protected]05b94b432013-11-22 22:09:55714
[email protected]e9bf4fc2014-06-19 16:50:52715 if (source_dir.is_source_absolute()) {
716 // The source dir should be source-absolute, so we trim off the two leading
717 // slashes to append to the toolchain object directory.
718 DCHECK(source_dir.is_source_absolute());
[email protected]0dfcae72014-08-19 22:52:16719 result.value().append(&source_dir.value()[2],
720 source_dir.value().size() - 2);
[email protected]e9bf4fc2014-06-19 16:50:52721 }
[email protected]0dfcae72014-08-19 22:52:16722 return result;
[email protected]05b94b432013-11-22 22:09:55723}
724
725SourceDir GetTargetOutputDir(const Target* target) {
[email protected]0dfcae72014-08-19 22:52:16726 return GetOutputDirForSourceDirAsOutputFile(
727 target->settings(), target->label().dir()).AsSourceDir(
728 target->settings()->build_settings());
729}
730
731OutputFile GetTargetOutputDirAsOutputFile(const Target* target) {
732 return GetOutputDirForSourceDirAsOutputFile(
733 target->settings(), target->label().dir());
[email protected]05b94b432013-11-22 22:09:55734}
735
736SourceDir GetTargetGenDir(const Target* target) {
[email protected]0dfcae72014-08-19 22:52:16737 return GetTargetGenDirAsOutputFile(target).AsSourceDir(
738 target->settings()->build_settings());
739}
740
741OutputFile GetTargetGenDirAsOutputFile(const Target* target) {
742 return GetGenDirForSourceDirAsOutputFile(
743 target->settings(), target->label().dir());
[email protected]05b94b432013-11-22 22:09:55744}
745
746SourceDir GetCurrentOutputDir(const Scope* scope) {
[email protected]0dfcae72014-08-19 22:52:16747 return GetOutputDirForSourceDirAsOutputFile(
748 scope->settings(), scope->GetSourceDir()).AsSourceDir(
749 scope->settings()->build_settings());
[email protected]05b94b432013-11-22 22:09:55750}
751
752SourceDir GetCurrentGenDir(const Scope* scope) {
753 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir());
754}