blob: 0bc266dc5873b0dab9d6979dd7483327d955efff [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
[email protected]a623db1872014-02-19 19:11:179#include "base/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
102 // Check drive letter
103 if (!((path[0] >= 'A' && path[0] <= 'Z') ||
104 path[0] >= 'a' && path[0] <= 'z'))
105 return false;
106
[email protected]858ceda2014-02-27 23:26:40107 if (!IsSlash(path[2]))
[email protected]f6b314d12013-08-23 21:07:47108 return false;
109 return true;
110}
111#endif
112
[email protected]a623db1872014-02-19 19:11:17113// A wrapper around FilePath.GetComponents that works the way we need. This is
114// not super efficient since it does some O(n) transformations on the path. If
115// this is called a lot, we might want to optimize.
116std::vector<base::FilePath::StringType> GetPathComponents(
117 const base::FilePath& path) {
118 std::vector<base::FilePath::StringType> result;
119 path.GetComponents(&result);
120
121 if (result.empty())
122 return result;
123
124 // GetComponents will preserve the "/" at the beginning, which confuses us.
125 // We don't expect to have relative paths in this function.
126 // Don't use IsSeparator since we always want to allow backslashes.
127 if (result[0] == FILE_PATH_LITERAL("/") ||
128 result[0] == FILE_PATH_LITERAL("\\"))
129 result.erase(result.begin());
130
131#if defined(OS_WIN)
132 // On Windows, GetComponents will give us [ "C:", "/", "foo" ], and we
133 // don't want the slash in there. This doesn't support input like "C:foo"
134 // which means foo relative to the current directory of the C drive but
135 // that's basically legacy DOS behavior we don't need to support.
[email protected]858ceda2014-02-27 23:26:40136 if (result.size() >= 2 && result[1].size() == 1 && IsSlash(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]126d8b52014-04-07 22:17:35168SourceFileType GetSourceFileType(const SourceFile& file) {
[email protected]96ea63d2013-07-30 10:17:07169 base::StringPiece extension = FindExtension(&file.value());
170 if (extension == "cc" || extension == "cpp" || extension == "cxx")
171 return SOURCE_CC;
172 if (extension == "h")
173 return SOURCE_H;
174 if (extension == "c")
175 return SOURCE_C;
[email protected]126d8b52014-04-07 22:17:35176 if (extension == "m")
177 return SOURCE_M;
178 if (extension == "mm")
179 return SOURCE_MM;
180 if (extension == "rc")
181 return SOURCE_RC;
[email protected]e8ab6912014-04-21 20:54:51182 if (extension == "S" || extension == "s")
[email protected]126d8b52014-04-07 22:17:35183 return SOURCE_S;
[email protected]c6f27f22013-08-21 21:44:59184
[email protected]96ea63d2013-07-30 10:17:07185 return SOURCE_UNKNOWN;
186}
187
188const char* GetExtensionForOutputType(Target::OutputType type,
189 Settings::TargetOS os) {
190 switch (os) {
[email protected]8338eea2013-08-05 23:08:12191 case Settings::MAC:
192 switch (type) {
[email protected]8338eea2013-08-05 23:08:12193 case Target::EXECUTABLE:
194 return "";
195 case Target::SHARED_LIBRARY:
196 return "dylib";
197 case Target::STATIC_LIBRARY:
198 return "a";
[email protected]8338eea2013-08-05 23:08:12199 default:
200 NOTREACHED();
201 }
202 break;
203
[email protected]96ea63d2013-07-30 10:17:07204 case Settings::WIN:
205 switch (type) {
[email protected]96ea63d2013-07-30 10:17:07206 case Target::EXECUTABLE:
207 return "exe";
208 case Target::SHARED_LIBRARY:
209 return "dll.lib"; // Extension of import library.
210 case Target::STATIC_LIBRARY:
211 return "lib";
[email protected]c0822d7f2013-08-13 17:10:56212 default:
213 NOTREACHED();
214 }
215 break;
216
217 case Settings::LINUX:
218 switch (type) {
219 case Target::EXECUTABLE:
220 return "";
221 case Target::SHARED_LIBRARY:
222 return "so";
223 case Target::STATIC_LIBRARY:
224 return "a";
[email protected]96ea63d2013-07-30 10:17:07225 default:
226 NOTREACHED();
227 }
228 break;
229
230 default:
231 NOTREACHED();
232 }
233 return "";
234}
235
[email protected]1bcf0012013-09-19 21:13:32236std::string FilePathToUTF8(const base::FilePath::StringType& str) {
[email protected]96ea63d2013-07-30 10:17:07237#if defined(OS_WIN)
[email protected]6c3bf032013-12-25 19:37:03238 return base::WideToUTF8(str);
[email protected]96ea63d2013-07-30 10:17:07239#else
[email protected]1bcf0012013-09-19 21:13:32240 return str;
[email protected]96ea63d2013-07-30 10:17:07241#endif
242}
243
244base::FilePath UTF8ToFilePath(const base::StringPiece& sp) {
245#if defined(OS_WIN)
[email protected]6c3bf032013-12-25 19:37:03246 return base::FilePath(base::UTF8ToWide(sp));
[email protected]96ea63d2013-07-30 10:17:07247#else
248 return base::FilePath(sp.as_string());
249#endif
250}
251
252size_t FindExtensionOffset(const std::string& path) {
253 for (int i = static_cast<int>(path.size()); i >= 0; i--) {
[email protected]858ceda2014-02-27 23:26:40254 if (IsSlash(path[i]))
[email protected]96ea63d2013-07-30 10:17:07255 break;
256 if (path[i] == '.')
257 return i + 1;
258 }
259 return std::string::npos;
260}
261
262base::StringPiece FindExtension(const std::string* path) {
263 size_t extension_offset = FindExtensionOffset(*path);
264 if (extension_offset == std::string::npos)
265 return base::StringPiece();
266 return base::StringPiece(&path->data()[extension_offset],
267 path->size() - extension_offset);
268}
269
270size_t FindFilenameOffset(const std::string& path) {
271 for (int i = static_cast<int>(path.size()) - 1; i >= 0; i--) {
[email protected]858ceda2014-02-27 23:26:40272 if (IsSlash(path[i]))
[email protected]96ea63d2013-07-30 10:17:07273 return i + 1;
274 }
275 return 0; // No filename found means everything was the filename.
276}
277
278base::StringPiece FindFilename(const std::string* path) {
279 size_t filename_offset = FindFilenameOffset(*path);
280 if (filename_offset == 0)
281 return base::StringPiece(*path); // Everything is the file name.
282 return base::StringPiece(&(*path).data()[filename_offset],
283 path->size() - filename_offset);
284}
285
286base::StringPiece FindFilenameNoExtension(const std::string* path) {
287 if (path->empty())
288 return base::StringPiece();
289 size_t filename_offset = FindFilenameOffset(*path);
290 size_t extension_offset = FindExtensionOffset(*path);
291
292 size_t name_len;
293 if (extension_offset == std::string::npos)
294 name_len = path->size() - filename_offset;
295 else
296 name_len = extension_offset - filename_offset - 1;
297
298 return base::StringPiece(&(*path).data()[filename_offset], name_len);
299}
300
301void RemoveFilename(std::string* path) {
302 path->resize(FindFilenameOffset(*path));
303}
304
305bool EndsWithSlash(const std::string& s) {
[email protected]858ceda2014-02-27 23:26:40306 return !s.empty() && IsSlash(s[s.size() - 1]);
[email protected]96ea63d2013-07-30 10:17:07307}
308
309base::StringPiece FindDir(const std::string* path) {
310 size_t filename_offset = FindFilenameOffset(*path);
311 if (filename_offset == 0u)
312 return base::StringPiece();
313 return base::StringPiece(path->data(), filename_offset);
314}
315
[email protected]a3372c72014-04-28 22:39:26316base::StringPiece FindLastDirComponent(const SourceDir& dir) {
317 const std::string& dir_string = dir.value();
318
319 if (dir_string.empty())
320 return base::StringPiece();
321 int cur = static_cast<int>(dir_string.size()) - 1;
322 DCHECK(dir_string[cur] == '/');
323 int end = cur;
324 cur--; // Skip before the last slash.
325
326 for (; cur >= 0; cur--) {
327 if (dir_string[cur] == '/')
328 return base::StringPiece(&dir_string[cur + 1], end - cur - 1);
329 }
330 return base::StringPiece(&dir_string[0], end);
331}
332
[email protected]96ea63d2013-07-30 10:17:07333bool EnsureStringIsInOutputDir(const SourceDir& dir,
334 const std::string& str,
335 const Value& originating,
336 Err* err) {
337 // The last char of the dir will be a slash. We don't care if the input ends
338 // in a slash or not, so just compare up until there.
339 //
340 // This check will be wrong for all proper prefixes "e.g. "/output" will
341 // match "/out" but we don't really care since this is just a sanity check.
342 const std::string& dir_str = dir.value();
343 if (str.compare(0, dir_str.length() - 1, dir_str, 0, dir_str.length() - 1)
344 != 0) {
[email protected]b8aa81242014-01-05 11:59:51345 *err = Err(originating, "File is not inside output directory.",
[email protected]96ea63d2013-07-30 10:17:07346 "The given file should be in the output directory. Normally you would "
[email protected]b8aa81242014-01-05 11:59:51347 "specify\n\"$target_out_dir/foo\" or "
[email protected]96ea63d2013-07-30 10:17:07348 "\"$target_gen_dir/foo\". I interpreted this as\n\""
349 + str + "\".");
350 return false;
351 }
352 return true;
353}
354
[email protected]ac1128e2013-08-23 00:26:56355bool IsPathAbsolute(const base::StringPiece& path) {
356 if (path.empty())
357 return false;
358
[email protected]858ceda2014-02-27 23:26:40359 if (!IsSlash(path[0])) {
[email protected]ac1128e2013-08-23 00:26:56360#if defined(OS_WIN)
361 // Check for Windows system paths like "C:\foo".
[email protected]858ceda2014-02-27 23:26:40362 if (path.size() > 2 && path[1] == ':' && IsSlash(path[2]))
[email protected]ac1128e2013-08-23 00:26:56363 return true;
364#endif
365 return false; // Doesn't begin with a slash, is relative.
366 }
367
[email protected]858ceda2014-02-27 23:26:40368 // Double forward slash at the beginning means source-relative (we don't
369 // allow backslashes for denoting this).
[email protected]ac1128e2013-08-23 00:26:56370 if (path.size() > 1 && path[1] == '/')
[email protected]858ceda2014-02-27 23:26:40371 return false;
[email protected]ac1128e2013-08-23 00:26:56372
373 return true;
374}
375
376bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root,
377 const base::StringPiece& path,
378 std::string* dest) {
379 DCHECK(IsPathAbsolute(source_root));
380 DCHECK(IsPathAbsolute(path));
381
382 dest->clear();
383
384 if (source_root.size() > path.size())
385 return false; // The source root is longer: the path can never be inside.
386
387#if defined(OS_WIN)
[email protected]858ceda2014-02-27 23:26:40388 // Source root should be canonical on Windows. Note that the initial slash
389 // must be forward slash, but that the other ones can be either forward or
390 // backward.
[email protected]ac1128e2013-08-23 00:26:56391 DCHECK(source_root.size() > 2 && source_root[0] != '/' &&
[email protected]858ceda2014-02-27 23:26:40392 source_root[1] == ':' && IsSlash(source_root[2]));
[email protected]f6b314d12013-08-23 21:07:47393
394 size_t after_common_index = std::string::npos;
395 if (DoesBeginWindowsDriveLetter(path)) {
396 // Handle "C:\foo"
397 if (AreAbsoluteWindowsPathsEqual(source_root,
398 path.substr(0, source_root.size())))
399 after_common_index = source_root.size();
400 else
401 return false;
402 } else if (path[0] == '/' && source_root.size() <= path.size() - 1 &&
403 DoesBeginWindowsDriveLetter(path.substr(1))) {
404 // Handle "/C:/foo"
405 if (AreAbsoluteWindowsPathsEqual(source_root,
406 path.substr(1, source_root.size())))
407 after_common_index = source_root.size() + 1;
408 else
409 return false;
410 } else {
411 return false;
412 }
413
414 // If we get here, there's a match and after_common_index identifies the
415 // part after it.
416
417 // The base may or may not have a trailing slash, so skip all slashes from
418 // the path after our prefix match.
419 size_t first_after_slash = after_common_index;
[email protected]858ceda2014-02-27 23:26:40420 while (first_after_slash < path.size() && IsSlash(path[first_after_slash]))
[email protected]f6b314d12013-08-23 21:07:47421 first_after_slash++;
422
423 dest->assign("//"); // Result is source root relative.
424 dest->append(&path.data()[first_after_slash],
425 path.size() - first_after_slash);
426 return true;
427
[email protected]ac1128e2013-08-23 00:26:56428#else
[email protected]f6b314d12013-08-23 21:07:47429
[email protected]ac1128e2013-08-23 00:26:56430 // On non-Windows this is easy. Since we know both are absolute, just do a
431 // prefix check.
432 if (path.substr(0, source_root.size()) == source_root) {
[email protected]ac1128e2013-08-23 00:26:56433 // The base may or may not have a trailing slash, so skip all slashes from
434 // the path after our prefix match.
435 size_t first_after_slash = source_root.size();
[email protected]858ceda2014-02-27 23:26:40436 while (first_after_slash < path.size() && IsSlash(path[first_after_slash]))
[email protected]ac1128e2013-08-23 00:26:56437 first_after_slash++;
438
[email protected]f6b314d12013-08-23 21:07:47439 dest->assign("//"); // Result is source root relative.
[email protected]ac1128e2013-08-23 00:26:56440 dest->append(&path.data()[first_after_slash],
441 path.size() - first_after_slash);
442 return true;
443 }
[email protected]ac1128e2013-08-23 00:26:56444 return false;
[email protected]f6b314d12013-08-23 21:07:47445#endif
[email protected]ac1128e2013-08-23 00:26:56446}
447
[email protected]96ea63d2013-07-30 10:17:07448std::string InvertDir(const SourceDir& path) {
449 const std::string value = path.value();
450 if (value.empty())
451 return std::string();
452
453 DCHECK(value[0] == '/');
454 size_t begin_index = 1;
455
456 // If the input begins with two slashes, skip over both (this is a
[email protected]858ceda2014-02-27 23:26:40457 // source-relative dir). These must be forward slashes only.
[email protected]96ea63d2013-07-30 10:17:07458 if (value.size() > 1 && value[1] == '/')
459 begin_index = 2;
460
461 std::string ret;
462 for (size_t i = begin_index; i < value.size(); i++) {
[email protected]858ceda2014-02-27 23:26:40463 if (IsSlash(value[i]))
[email protected]96ea63d2013-07-30 10:17:07464 ret.append("../");
465 }
466 return ret;
467}
468
469void NormalizePath(std::string* path) {
470 char* pathbuf = path->empty() ? NULL : &(*path)[0];
471
472 // top_index is the first character we can modify in the path. Anything
473 // before this indicates where the path is relative to.
474 size_t top_index = 0;
475 bool is_relative = true;
476 if (!path->empty() && pathbuf[0] == '/') {
477 is_relative = false;
478
479 if (path->size() > 1 && pathbuf[1] == '/') {
480 // Two leading slashes, this is a path into the source dir.
481 top_index = 2;
482 } else {
483 // One leading slash, this is a system-absolute path.
484 top_index = 1;
485 }
486 }
487
488 size_t dest_i = top_index;
489 for (size_t src_i = top_index; src_i < path->size(); /* nothing */) {
490 if (pathbuf[src_i] == '.') {
[email protected]858ceda2014-02-27 23:26:40491 if (src_i == 0 || IsSlash(pathbuf[src_i - 1])) {
[email protected]96ea63d2013-07-30 10:17:07492 // Slash followed by a dot, see if it's something special.
493 size_t consumed_len;
494 switch (ClassifyAfterDot(*path, src_i + 1, &consumed_len)) {
495 case NOT_A_DIRECTORY:
496 // Copy the dot to the output, it means nothing special.
497 pathbuf[dest_i++] = pathbuf[src_i++];
498 break;
499 case DIRECTORY_CUR:
500 // Current directory, just skip the input.
501 src_i += consumed_len;
502 break;
503 case DIRECTORY_UP:
504 // Back up over previous directory component. If we're already
505 // at the top, preserve the "..".
506 if (dest_i > top_index) {
507 // The previous char was a slash, remove it.
508 dest_i--;
509 }
510
511 if (dest_i == top_index) {
512 if (is_relative) {
513 // We're already at the beginning of a relative input, copy the
514 // ".." and continue. We need the trailing slash if there was
515 // one before (otherwise we're at the end of the input).
516 pathbuf[dest_i++] = '.';
517 pathbuf[dest_i++] = '.';
518 if (consumed_len == 3)
519 pathbuf[dest_i++] = '/';
520
521 // This also makes a new "root" that we can't delete by going
522 // up more levels. Otherwise "../.." would collapse to
523 // nothing.
524 top_index = dest_i;
525 }
526 // Otherwise we're at the beginning of an absolute path. Don't
527 // allow ".." to go up another level and just eat it.
528 } else {
529 // Just find the previous slash or the beginning of input.
[email protected]858ceda2014-02-27 23:26:40530 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1]))
[email protected]96ea63d2013-07-30 10:17:07531 dest_i--;
532 }
533 src_i += consumed_len;
534 }
535 } else {
536 // Dot not preceeded by a slash, copy it literally.
537 pathbuf[dest_i++] = pathbuf[src_i++];
538 }
[email protected]858ceda2014-02-27 23:26:40539 } else if (IsSlash(pathbuf[src_i])) {
540 if (src_i > 0 && IsSlash(pathbuf[src_i - 1])) {
[email protected]96ea63d2013-07-30 10:17:07541 // Two slashes in a row, skip over it.
542 src_i++;
543 } else {
[email protected]858ceda2014-02-27 23:26:40544 // Just one slash, copy it, normalizing to foward slash.
545 pathbuf[dest_i] = '/';
546 dest_i++;
547 src_i++;
[email protected]96ea63d2013-07-30 10:17:07548 }
549 } else {
550 // Input nothing special, just copy it.
551 pathbuf[dest_i++] = pathbuf[src_i++];
552 }
553 }
554 path->resize(dest_i);
555}
556
557void ConvertPathToSystem(std::string* path) {
558#if defined(OS_WIN)
559 for (size_t i = 0; i < path->size(); i++) {
560 if ((*path)[i] == '/')
561 (*path)[i] = '\\';
562 }
563#endif
564}
565
[email protected]ea3690c2013-09-23 17:59:22566std::string RebaseSourceAbsolutePath(const std::string& input,
567 const SourceDir& dest_dir) {
568 CHECK(input.size() >= 2 && input[0] == '/' && input[1] == '/')
569 << "Input to rebase isn't source-absolute: " << input;
570 CHECK(dest_dir.is_source_absolute())
571 << "Dir to rebase to isn't source-absolute: " << dest_dir.value();
572
573 const std::string& dest = dest_dir.value();
574
575 // Skip the common prefixes of the source and dest as long as they end in
576 // a [back]slash.
577 size_t common_prefix_len = 2; // The beginning two "//" are always the same.
578 size_t max_common_length = std::min(input.size(), dest.size());
579 for (size_t i = common_prefix_len; i < max_common_length; i++) {
[email protected]858ceda2014-02-27 23:26:40580 if (IsSlash(input[i]) && IsSlash(dest[i]))
[email protected]ea3690c2013-09-23 17:59:22581 common_prefix_len = i + 1;
582 else if (input[i] != dest[i])
583 break;
584 }
585
586 // Invert the dest dir starting from the end of the common prefix.
587 std::string ret;
588 for (size_t i = common_prefix_len; i < dest.size(); i++) {
[email protected]858ceda2014-02-27 23:26:40589 if (IsSlash(dest[i]))
[email protected]ea3690c2013-09-23 17:59:22590 ret.append("../");
591 }
592
593 // Append any remaining unique input.
594 ret.append(&input[common_prefix_len], input.size() - common_prefix_len);
595
596 // If the result is still empty, the paths are the same.
597 if (ret.empty())
598 ret.push_back('.');
599
600 return ret;
601}
[email protected]05b94b432013-11-22 22:09:55602
603std::string DirectoryWithNoLastSlash(const SourceDir& dir) {
604 std::string ret;
605
606 if (dir.value().empty()) {
607 // Just keep input the same.
608 } else if (dir.value() == "/") {
609 ret.assign("/.");
610 } else if (dir.value() == "//") {
611 ret.assign("//.");
612 } else {
613 ret.assign(dir.value());
614 ret.resize(ret.size() - 1);
615 }
616 return ret;
617}
618
[email protected]a623db1872014-02-19 19:11:17619SourceDir SourceDirForPath(const base::FilePath& source_root,
620 const base::FilePath& path) {
621 std::vector<base::FilePath::StringType> source_comp =
622 GetPathComponents(source_root);
623 std::vector<base::FilePath::StringType> path_comp =
624 GetPathComponents(path);
625
626 // See if path is inside the source root by looking for each of source root's
627 // components at the beginning of path.
628 bool is_inside_source;
629 if (path_comp.size() < source_comp.size()) {
630 // Too small to fit.
631 is_inside_source = false;
632 } else {
633 is_inside_source = true;
634 for (size_t i = 0; i < source_comp.size(); i++) {
635 if (!FilesystemStringsEqual(source_comp[i], path_comp[i])) {
636 is_inside_source = false;
637 break;
638 }
639 }
640 }
641
642 std::string result_str;
643 size_t initial_path_comp_to_use;
644 if (is_inside_source) {
645 // Construct a source-relative path beginning in // and skip all of the
646 // shared directories.
647 result_str = "//";
648 initial_path_comp_to_use = source_comp.size();
649 } else {
650 // Not inside source code, construct a system-absolute path.
651 result_str = "/";
652 initial_path_comp_to_use = 0;
653 }
654
655 for (size_t i = initial_path_comp_to_use; i < path_comp.size(); i++) {
656 result_str.append(FilePathToUTF8(path_comp[i]));
657 result_str.push_back('/');
658 }
659 return SourceDir(result_str);
660}
661
662SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root) {
663 base::FilePath cd;
[email protected]37b3c1992014-03-11 20:59:02664 base::GetCurrentDirectory(&cd);
[email protected]a623db1872014-02-19 19:11:17665 return SourceDirForPath(source_root, cd);
666}
667
[email protected]05b94b432013-11-22 22:09:55668SourceDir GetToolchainOutputDir(const Settings* settings) {
669 const OutputFile& toolchain_subdir = settings->toolchain_output_subdir();
670
671 std::string result = settings->build_settings()->build_dir().value();
672 if (!toolchain_subdir.value().empty())
673 result.append(toolchain_subdir.value());
674
675 return SourceDir(SourceDir::SWAP_IN, &result);
676}
677
678SourceDir GetToolchainGenDir(const Settings* settings) {
679 const OutputFile& toolchain_subdir = settings->toolchain_output_subdir();
680
681 std::string result = settings->build_settings()->build_dir().value();
682 if (!toolchain_subdir.value().empty())
683 result.append(toolchain_subdir.value());
684
685 result.append("gen/");
686 return SourceDir(SourceDir::SWAP_IN, &result);
687}
688
689SourceDir GetOutputDirForSourceDir(const Settings* settings,
690 const SourceDir& source_dir) {
691 SourceDir toolchain = GetToolchainOutputDir(settings);
692
693 std::string ret;
694 toolchain.SwapValue(&ret);
695 ret.append("obj/");
696
697 // The source dir should be source-absolute, so we trim off the two leading
698 // slashes to append to the toolchain object directory.
699 DCHECK(source_dir.is_source_absolute());
700 ret.append(&source_dir.value()[2], source_dir.value().size() - 2);
701
702 return SourceDir(SourceDir::SWAP_IN, &ret);
703}
704
705SourceDir GetGenDirForSourceDir(const Settings* settings,
706 const SourceDir& source_dir) {
707 SourceDir toolchain = GetToolchainGenDir(settings);
708
709 std::string ret;
710 toolchain.SwapValue(&ret);
711
712 // The source dir should be source-absolute, so we trim off the two leading
713 // slashes to append to the toolchain object directory.
714 DCHECK(source_dir.is_source_absolute());
715 ret.append(&source_dir.value()[2], source_dir.value().size() - 2);
716
717 return SourceDir(SourceDir::SWAP_IN, &ret);
718}
719
720SourceDir GetTargetOutputDir(const Target* target) {
721 return GetOutputDirForSourceDir(target->settings(), target->label().dir());
722}
723
724SourceDir GetTargetGenDir(const Target* target) {
725 return GetGenDirForSourceDir(target->settings(), target->label().dir());
726}
727
728SourceDir GetCurrentOutputDir(const Scope* scope) {
729 return GetOutputDirForSourceDir(scope->settings(), scope->GetSourceDir());
730}
731
732SourceDir GetCurrentGenDir(const Scope* scope) {
733 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir());
734}