Roll DEPS for PDFium to 19ae17578f99621100a26dac3e2c7c3dbf7c7cd1
[chromium-blink-merge.git] / tools / gn / filesystem_utils.cc
blob7495b05c903312cfd0da7fd0e9cdac15d0a9bf82
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/filesystem_utils.h"
7 #include <algorithm>
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h"
14 #include "tools/gn/location.h"
15 #include "tools/gn/settings.h"
16 #include "tools/gn/source_dir.h"
18 namespace {
20 enum DotDisposition {
21 // The given dot is just part of a filename and is not special.
22 NOT_A_DIRECTORY,
24 // The given dot is the current directory.
25 DIRECTORY_CUR,
27 // The given dot is the first of a double dot that should take us up one.
28 DIRECTORY_UP
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.
36 // |*consumed_len| will contain the number of characters in the input that
37 // express what we found.
38 DotDisposition 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;
46 if (IsSlash(path[after_dot])) {
47 // Single dot followed by a slash.
48 *consumed_len = 2; // Consume the slash
49 return DIRECTORY_CUR;
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;
59 if (IsSlash(path[after_dot + 1])) {
60 // Double dot folowed by a slash.
61 *consumed_len = 3;
62 return DIRECTORY_UP;
66 // The dots are followed by something else, not a directory.
67 *consumed_len = 1;
68 return NOT_A_DIRECTORY;
71 #if defined(OS_WIN)
72 inline char NormalizeWindowsPathChar(char c) {
73 if (c == '/')
74 return '\\';
75 return base::ToLowerASCII(c);
78 // Attempts to do a case and slash-insensitive comparison of two 8-bit Windows
79 // paths.
80 bool AreAbsoluteWindowsPathsEqual(const base::StringPiece& a,
81 const base::StringPiece& b) {
82 if (a.size() != b.size())
83 return false;
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;
91 return true;
94 bool DoesBeginWindowsDriveLetter(const base::StringPiece& path) {
95 if (path.size() < 3)
96 return false;
98 // Check colon first, this will generally fail fastest.
99 if (path[1] != ':')
100 return false;
102 // Check drive letter.
103 if (!IsAsciiAlpha(path[0]))
104 return false;
106 if (!IsSlash(path[2]))
107 return false;
108 return true;
110 #endif
112 // 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.
115 std::vector<base::FilePath::StringType> GetPathComponents(
116 const base::FilePath& path) {
117 std::vector<base::FilePath::StringType> result;
118 path.GetComponents(&result);
120 if (result.empty())
121 return result;
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());
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.
135 if (result.size() >= 2 && result[1].size() == 1 &&
136 IsSlash(static_cast<char>(result[1][0])))
137 result.erase(result.begin() + 1);
138 #endif
140 return result;
143 // Provides the equivalent of == for filesystem strings, trying to do
144 // approximately the right thing with case.
145 bool 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.
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
166 } // namespace
168 const char* GetExtensionForOutputType(Target::OutputType type,
169 Settings::TargetOS os) {
170 switch (os) {
171 case Settings::MAC:
172 switch (type) {
173 case Target::EXECUTABLE:
174 return "";
175 case Target::SHARED_LIBRARY:
176 return "dylib";
177 case Target::STATIC_LIBRARY:
178 return "a";
179 default:
180 NOTREACHED();
182 break;
184 case Settings::WIN:
185 switch (type) {
186 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";
192 default:
193 NOTREACHED();
195 break;
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";
205 default:
206 NOTREACHED();
208 break;
210 default:
211 NOTREACHED();
213 return "";
216 std::string FilePathToUTF8(const base::FilePath::StringType& str) {
217 #if defined(OS_WIN)
218 return base::WideToUTF8(str);
219 #else
220 return str;
221 #endif
224 base::FilePath UTF8ToFilePath(const base::StringPiece& sp) {
225 #if defined(OS_WIN)
226 return base::FilePath(base::UTF8ToWide(sp));
227 #else
228 return base::FilePath(sp.as_string());
229 #endif
232 size_t FindExtensionOffset(const std::string& path) {
233 for (int i = static_cast<int>(path.size()); i >= 0; i--) {
234 if (IsSlash(path[i]))
235 break;
236 if (path[i] == '.')
237 return i + 1;
239 return std::string::npos;
242 base::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);
250 size_t FindFilenameOffset(const std::string& path) {
251 for (int i = static_cast<int>(path.size()) - 1; i >= 0; i--) {
252 if (IsSlash(path[i]))
253 return i + 1;
255 return 0; // No filename found means everything was the filename.
258 base::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);
266 base::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);
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;
278 return base::StringPiece(&(*path).data()[filename_offset], name_len);
281 void RemoveFilename(std::string* path) {
282 path->resize(FindFilenameOffset(*path));
285 bool EndsWithSlash(const std::string& s) {
286 return !s.empty() && IsSlash(s[s.size() - 1]);
289 base::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);
296 base::StringPiece FindLastDirComponent(const SourceDir& dir) {
297 const std::string& dir_string = dir.value();
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.
306 for (; cur >= 0; cur--) {
307 if (dir_string[cur] == '/')
308 return base::StringPiece(&dir_string[cur + 1], end - cur - 1);
310 return base::StringPiece(&dir_string[0], end);
313 bool EnsureStringIsInOutputDir(const SourceDir& dir,
314 const std::string& str,
315 const ParseNode* origin,
316 Err* err) {
317 // 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();
320 if (str.compare(0, dir_str.length(), dir_str) == 0)
321 return true; // Output directory is hardcoded.
323 *err = Err(origin, "File is not inside output directory.",
324 "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;
331 bool IsPathAbsolute(const base::StringPiece& path) {
332 if (path.empty())
333 return false;
335 if (!IsSlash(path[0])) {
336 #if defined(OS_WIN)
337 // Check for Windows system paths like "C:\foo".
338 if (path.size() > 2 && path[1] == ':' && IsSlash(path[2]))
339 return true;
340 #endif
341 return false; // Doesn't begin with a slash, is relative.
344 // Double forward slash at the beginning means source-relative (we don't
345 // allow backslashes for denoting this).
346 if (path.size() > 1 && path[1] == '/')
347 return false;
349 return true;
352 bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root,
353 const base::StringPiece& path,
354 std::string* dest) {
355 DCHECK(IsPathAbsolute(source_root));
356 DCHECK(IsPathAbsolute(path));
358 dest->clear();
360 if (source_root.size() > path.size())
361 return false; // The source root is longer: the path can never be inside.
363 #if defined(OS_WIN)
364 // 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.
367 DCHECK(source_root.size() > 2 && source_root[0] != '/' &&
368 source_root[1] == ':' && IsSlash(source_root[2]));
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;
390 // If we get here, there's a match and after_common_index identifies the
391 // part after it.
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;
396 while (first_after_slash < path.size() && IsSlash(path[first_after_slash]))
397 first_after_slash++;
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;
404 #else
406 // 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) {
409 // 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();
412 while (first_after_slash < path.size() && IsSlash(path[first_after_slash]))
413 first_after_slash++;
415 dest->assign("//"); // Result is source root relative.
416 dest->append(&path.data()[first_after_slash],
417 path.size() - first_after_slash);
418 return true;
420 return false;
421 #endif
424 void NormalizePath(std::string* path) {
425 char* pathbuf = path->empty() ? nullptr : &(*path)[0];
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;
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;
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] == '.') {
446 if (src_i == 0 || IsSlash(pathbuf[src_i - 1])) {
447 // 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--;
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++] = '/';
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;
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.
485 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1]))
486 dest_i--;
488 src_i += consumed_len;
490 } else {
491 // Dot not preceeded by a slash, copy it literally.
492 pathbuf[dest_i++] = pathbuf[src_i++];
494 } else if (IsSlash(pathbuf[src_i])) {
495 if (src_i > 0 && IsSlash(pathbuf[src_i - 1])) {
496 // Two slashes in a row, skip over it.
497 src_i++;
498 } else {
499 // Just one slash, copy it, normalizing to foward slash.
500 pathbuf[dest_i] = '/';
501 dest_i++;
502 src_i++;
504 } else {
505 // Input nothing special, just copy it.
506 pathbuf[dest_i++] = pathbuf[src_i++];
509 path->resize(dest_i);
512 void 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] = '\\';
518 #endif
521 std::string MakeRelativePath(const std::string& input,
522 const std::string& dest) {
523 #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);
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);
537 #endif
539 std::string ret;
541 // Skip the common prefixes of the source and dest as long as they end in
542 // a [back]slash.
543 size_t common_prefix_len = 0;
544 size_t max_common_length = std::min(input.size(), dest.size());
545 for (size_t i = common_prefix_len; i < max_common_length; i++) {
546 if (IsSlash(input[i]) && IsSlash(dest[i]))
547 common_prefix_len = i + 1;
548 else if (input[i] != dest[i])
549 break;
552 // Invert the dest dir starting from the end of the common prefix.
553 for (size_t i = common_prefix_len; i < dest.size(); i++) {
554 if (IsSlash(dest[i]))
555 ret.append("../");
558 // Append any remaining unique input.
559 ret.append(&input[common_prefix_len], input.size() - common_prefix_len);
561 // If the result is still empty, the paths are the same.
562 if (ret.empty())
563 ret.push_back('.');
565 return ret;
568 std::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("/"));
574 bool input_is_source_path = (input.size() >= 2 &&
575 input[0] == '/' && input[1] == '/');
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);
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
605 bool remove_slash = false;
606 if (!EndsWithSlash(input_full)) {
607 input_full.push_back('/');
608 remove_slash = true;
610 ret = MakeRelativePath(input_full, dest_full);
611 if (remove_slash && ret.size() > 1)
612 ret.resize(ret.size() - 1);
613 return ret;
616 ret = MakeRelativePath(input, dest_dir.value());
617 return ret;
620 std::string DirectoryWithNoLastSlash(const SourceDir& dir) {
621 std::string ret;
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);
633 return ret;
636 SourceDir 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);
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;
646 if (path_comp.size() < source_comp.size() || source_root.empty()) {
647 // 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;
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;
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('/');
676 return SourceDir(result_str);
679 SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root) {
680 base::FilePath cd;
681 base::GetCurrentDirectory(&cd);
682 return SourceDirForPath(source_root, cd);
685 std::string GetOutputSubdirName(const Label& toolchain_label, bool is_default) {
686 // The default toolchain has no subdir.
687 if (is_default)
688 return std::string();
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() + "/";
695 SourceDir GetToolchainOutputDir(const Settings* settings) {
696 return settings->toolchain_output_subdir().AsSourceDir(
697 settings->build_settings());
700 SourceDir 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);
707 SourceDir GetToolchainGenDir(const Settings* settings) {
708 return GetToolchainGenDirAsOutputFile(settings).AsSourceDir(
709 settings->build_settings());
712 OutputFile GetToolchainGenDirAsOutputFile(const Settings* settings) {
713 OutputFile result(settings->toolchain_output_subdir());
714 result.value().append("gen/");
715 return result;
718 SourceDir 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);
726 SourceDir GetOutputDirForSourceDir(const Settings* settings,
727 const SourceDir& source_dir) {
728 return GetOutputDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir(
729 settings->build_settings());
732 OutputFile GetOutputDirForSourceDirAsOutputFile(const Settings* settings,
733 const SourceDir& source_dir) {
734 OutputFile result = settings->toolchain_output_subdir();
735 result.value().append("obj/");
737 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.
740 result.value().append(&source_dir.value()[2],
741 source_dir.value().size() - 2);
742 } else {
743 // system-absolute
744 const std::string& build_dir =
745 settings->build_settings()->build_dir().value();
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);
751 } 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);
766 return result;
769 SourceDir GetGenDirForSourceDir(const Settings* settings,
770 const SourceDir& source_dir) {
771 return GetGenDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir(
772 settings->build_settings());
775 OutputFile GetGenDirForSourceDirAsOutputFile(const Settings* settings,
776 const SourceDir& source_dir) {
777 OutputFile result = GetToolchainGenDirAsOutputFile(settings);
779 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());
783 result.value().append(&source_dir.value()[2],
784 source_dir.value().size() - 2);
786 return result;
789 SourceDir GetTargetOutputDir(const Target* target) {
790 return GetOutputDirForSourceDirAsOutputFile(
791 target->settings(), target->label().dir()).AsSourceDir(
792 target->settings()->build_settings());
795 OutputFile GetTargetOutputDirAsOutputFile(const Target* target) {
796 return GetOutputDirForSourceDirAsOutputFile(
797 target->settings(), target->label().dir());
800 SourceDir GetTargetGenDir(const Target* target) {
801 return GetTargetGenDirAsOutputFile(target).AsSourceDir(
802 target->settings()->build_settings());
805 OutputFile GetTargetGenDirAsOutputFile(const Target* target) {
806 return GetGenDirForSourceDirAsOutputFile(
807 target->settings(), target->label().dir());
810 SourceDir GetCurrentOutputDir(const Scope* scope) {
811 return GetOutputDirForSourceDirAsOutputFile(
812 scope->settings(), scope->GetSourceDir()).AsSourceDir(
813 scope->settings()->build_settings());
816 SourceDir GetCurrentGenDir(const Scope* scope) {
817 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir());