Partially fix compilation of media_unittests with Xcode 7 (OS X 10.11 SDK).
[chromium-blink-merge.git] / tools / gn / filesystem_utils.cc
blob79e77310d2588cba16d714aa95320eb91316f735
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.
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 (!base::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 IsStringInOutputDir(const SourceDir& output_dir, const std::string& str) {
314 // This check will be wrong for all proper prefixes "e.g. "/output" will
315 // match "/out" but we don't really care since this is just a sanity check.
316 const std::string& dir_str = output_dir.value();
317 return str.compare(0, dir_str.length(), dir_str) == 0;
320 bool EnsureStringIsInOutputDir(const SourceDir& output_dir,
321 const std::string& str,
322 const ParseNode* origin,
323 Err* err) {
324 if (IsStringInOutputDir(output_dir, str))
325 return true; // Output directory is hardcoded.
327 *err = Err(origin, "File is not inside output directory.",
328 "The given file should be in the output directory. Normally you would "
329 "specify\n\"$target_out_dir/foo\" or "
330 "\"$target_gen_dir/foo\". I interpreted this as\n\""
331 + str + "\".");
332 return false;
335 bool IsPathAbsolute(const base::StringPiece& path) {
336 if (path.empty())
337 return false;
339 if (!IsSlash(path[0])) {
340 #if defined(OS_WIN)
341 // Check for Windows system paths like "C:\foo".
342 if (path.size() > 2 && path[1] == ':' && IsSlash(path[2]))
343 return true;
344 #endif
345 return false; // Doesn't begin with a slash, is relative.
348 // Double forward slash at the beginning means source-relative (we don't
349 // allow backslashes for denoting this).
350 if (path.size() > 1 && path[1] == '/')
351 return false;
353 return true;
356 bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root,
357 const base::StringPiece& path,
358 std::string* dest) {
359 DCHECK(IsPathAbsolute(source_root));
360 DCHECK(IsPathAbsolute(path));
362 dest->clear();
364 if (source_root.size() > path.size())
365 return false; // The source root is longer: the path can never be inside.
367 #if defined(OS_WIN)
368 // Source root should be canonical on Windows. Note that the initial slash
369 // must be forward slash, but that the other ones can be either forward or
370 // backward.
371 DCHECK(source_root.size() > 2 && source_root[0] != '/' &&
372 source_root[1] == ':' && IsSlash(source_root[2]));
374 size_t after_common_index = std::string::npos;
375 if (DoesBeginWindowsDriveLetter(path)) {
376 // Handle "C:\foo"
377 if (AreAbsoluteWindowsPathsEqual(source_root,
378 path.substr(0, source_root.size())))
379 after_common_index = source_root.size();
380 else
381 return false;
382 } else if (path[0] == '/' && source_root.size() <= path.size() - 1 &&
383 DoesBeginWindowsDriveLetter(path.substr(1))) {
384 // Handle "/C:/foo"
385 if (AreAbsoluteWindowsPathsEqual(source_root,
386 path.substr(1, source_root.size())))
387 after_common_index = source_root.size() + 1;
388 else
389 return false;
390 } else {
391 return false;
394 // If we get here, there's a match and after_common_index identifies the
395 // part after it.
397 // The base may or may not have a trailing slash, so skip all slashes from
398 // the path after our prefix match.
399 size_t first_after_slash = after_common_index;
400 while (first_after_slash < path.size() && IsSlash(path[first_after_slash]))
401 first_after_slash++;
403 dest->assign("//"); // Result is source root relative.
404 dest->append(&path.data()[first_after_slash],
405 path.size() - first_after_slash);
406 return true;
408 #else
410 // On non-Windows this is easy. Since we know both are absolute, just do a
411 // prefix check.
412 if (path.substr(0, source_root.size()) == source_root) {
413 // The base may or may not have a trailing slash, so skip all slashes from
414 // the path after our prefix match.
415 size_t first_after_slash = source_root.size();
416 while (first_after_slash < path.size() && IsSlash(path[first_after_slash]))
417 first_after_slash++;
419 dest->assign("//"); // Result is source root relative.
420 dest->append(&path.data()[first_after_slash],
421 path.size() - first_after_slash);
422 return true;
424 return false;
425 #endif
428 void NormalizePath(std::string* path) {
429 char* pathbuf = path->empty() ? nullptr : &(*path)[0];
431 // top_index is the first character we can modify in the path. Anything
432 // before this indicates where the path is relative to.
433 size_t top_index = 0;
434 bool is_relative = true;
435 if (!path->empty() && pathbuf[0] == '/') {
436 is_relative = false;
438 if (path->size() > 1 && pathbuf[1] == '/') {
439 // Two leading slashes, this is a path into the source dir.
440 top_index = 2;
441 } else {
442 // One leading slash, this is a system-absolute path.
443 top_index = 1;
447 size_t dest_i = top_index;
448 for (size_t src_i = top_index; src_i < path->size(); /* nothing */) {
449 if (pathbuf[src_i] == '.') {
450 if (src_i == 0 || IsSlash(pathbuf[src_i - 1])) {
451 // Slash followed by a dot, see if it's something special.
452 size_t consumed_len;
453 switch (ClassifyAfterDot(*path, src_i + 1, &consumed_len)) {
454 case NOT_A_DIRECTORY:
455 // Copy the dot to the output, it means nothing special.
456 pathbuf[dest_i++] = pathbuf[src_i++];
457 break;
458 case DIRECTORY_CUR:
459 // Current directory, just skip the input.
460 src_i += consumed_len;
461 break;
462 case DIRECTORY_UP:
463 // Back up over previous directory component. If we're already
464 // at the top, preserve the "..".
465 if (dest_i > top_index) {
466 // The previous char was a slash, remove it.
467 dest_i--;
470 if (dest_i == top_index) {
471 if (is_relative) {
472 // We're already at the beginning of a relative input, copy the
473 // ".." and continue. We need the trailing slash if there was
474 // one before (otherwise we're at the end of the input).
475 pathbuf[dest_i++] = '.';
476 pathbuf[dest_i++] = '.';
477 if (consumed_len == 3)
478 pathbuf[dest_i++] = '/';
480 // This also makes a new "root" that we can't delete by going
481 // up more levels. Otherwise "../.." would collapse to
482 // nothing.
483 top_index = dest_i;
485 // Otherwise we're at the beginning of an absolute path. Don't
486 // allow ".." to go up another level and just eat it.
487 } else {
488 // Just find the previous slash or the beginning of input.
489 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1]))
490 dest_i--;
492 src_i += consumed_len;
494 } else {
495 // Dot not preceeded by a slash, copy it literally.
496 pathbuf[dest_i++] = pathbuf[src_i++];
498 } else if (IsSlash(pathbuf[src_i])) {
499 if (src_i > 0 && IsSlash(pathbuf[src_i - 1])) {
500 // Two slashes in a row, skip over it.
501 src_i++;
502 } else {
503 // Just one slash, copy it, normalizing to foward slash.
504 pathbuf[dest_i] = '/';
505 dest_i++;
506 src_i++;
508 } else {
509 // Input nothing special, just copy it.
510 pathbuf[dest_i++] = pathbuf[src_i++];
513 path->resize(dest_i);
516 void ConvertPathToSystem(std::string* path) {
517 #if defined(OS_WIN)
518 for (size_t i = 0; i < path->size(); i++) {
519 if ((*path)[i] == '/')
520 (*path)[i] = '\\';
522 #endif
525 std::string MakeRelativePath(const std::string& input,
526 const std::string& dest) {
527 #if defined(OS_WIN)
528 // Make sure that absolute |input| path starts with a slash if |dest| path
529 // does. Otherwise skipping common prefixes won't work properly. Ensure the
530 // same for |dest| path too.
531 if (IsPathAbsolute(input) && !IsSlash(input[0]) && IsSlash(dest[0])) {
532 std::string corrected_input(1, dest[0]);
533 corrected_input.append(input);
534 return MakeRelativePath(corrected_input, dest);
536 if (IsPathAbsolute(dest) && !IsSlash(dest[0]) && IsSlash(input[0])) {
537 std::string corrected_dest(1, input[0]);
538 corrected_dest.append(dest);
539 return MakeRelativePath(input, corrected_dest);
541 #endif
543 std::string ret;
545 // Skip the common prefixes of the source and dest as long as they end in
546 // a [back]slash.
547 size_t common_prefix_len = 0;
548 size_t max_common_length = std::min(input.size(), dest.size());
549 for (size_t i = common_prefix_len; i < max_common_length; i++) {
550 if (IsSlash(input[i]) && IsSlash(dest[i]))
551 common_prefix_len = i + 1;
552 else if (input[i] != dest[i])
553 break;
556 // Invert the dest dir starting from the end of the common prefix.
557 for (size_t i = common_prefix_len; i < dest.size(); i++) {
558 if (IsSlash(dest[i]))
559 ret.append("../");
562 // Append any remaining unique input.
563 ret.append(&input[common_prefix_len], input.size() - common_prefix_len);
565 // If the result is still empty, the paths are the same.
566 if (ret.empty())
567 ret.push_back('.');
569 return ret;
572 std::string RebasePath(const std::string& input,
573 const SourceDir& dest_dir,
574 const base::StringPiece& source_root) {
575 std::string ret;
576 DCHECK(source_root.empty() || !source_root.ends_with("/"));
578 bool input_is_source_path = (input.size() >= 2 &&
579 input[0] == '/' && input[1] == '/');
581 if (!source_root.empty() &&
582 (!input_is_source_path || !dest_dir.is_source_absolute())) {
583 std::string input_full;
584 std::string dest_full;
585 if (input_is_source_path) {
586 source_root.AppendToString(&input_full);
587 input_full.push_back('/');
588 input_full.append(input, 2, std::string::npos);
589 } else {
590 input_full.append(input);
592 if (dest_dir.is_source_absolute()) {
593 source_root.AppendToString(&dest_full);
594 dest_full.push_back('/');
595 dest_full.append(dest_dir.value(), 2, std::string::npos);
596 } else {
597 #if defined(OS_WIN)
598 // On Windows, SourceDir system-absolute paths start
599 // with /, e.g. "/C:/foo/bar".
600 const std::string& value = dest_dir.value();
601 if (value.size() > 2 && value[2] == ':')
602 dest_full.append(dest_dir.value().substr(1));
603 else
604 dest_full.append(dest_dir.value());
605 #else
606 dest_full.append(dest_dir.value());
607 #endif
609 bool remove_slash = false;
610 if (!EndsWithSlash(input_full)) {
611 input_full.push_back('/');
612 remove_slash = true;
614 ret = MakeRelativePath(input_full, dest_full);
615 if (remove_slash && ret.size() > 1)
616 ret.resize(ret.size() - 1);
617 return ret;
620 ret = MakeRelativePath(input, dest_dir.value());
621 return ret;
624 std::string DirectoryWithNoLastSlash(const SourceDir& dir) {
625 std::string ret;
627 if (dir.value().empty()) {
628 // Just keep input the same.
629 } else if (dir.value() == "/") {
630 ret.assign("/.");
631 } else if (dir.value() == "//") {
632 ret.assign("//.");
633 } else {
634 ret.assign(dir.value());
635 ret.resize(ret.size() - 1);
637 return ret;
640 SourceDir SourceDirForPath(const base::FilePath& source_root,
641 const base::FilePath& path) {
642 std::vector<base::FilePath::StringType> source_comp =
643 GetPathComponents(source_root);
644 std::vector<base::FilePath::StringType> path_comp =
645 GetPathComponents(path);
647 // See if path is inside the source root by looking for each of source root's
648 // components at the beginning of path.
649 bool is_inside_source;
650 if (path_comp.size() < source_comp.size() || source_root.empty()) {
651 // Too small to fit.
652 is_inside_source = false;
653 } else {
654 is_inside_source = true;
655 for (size_t i = 0; i < source_comp.size(); i++) {
656 if (!FilesystemStringsEqual(source_comp[i], path_comp[i])) {
657 is_inside_source = false;
658 break;
663 std::string result_str;
664 size_t initial_path_comp_to_use;
665 if (is_inside_source) {
666 // Construct a source-relative path beginning in // and skip all of the
667 // shared directories.
668 result_str = "//";
669 initial_path_comp_to_use = source_comp.size();
670 } else {
671 // Not inside source code, construct a system-absolute path.
672 result_str = "/";
673 initial_path_comp_to_use = 0;
676 for (size_t i = initial_path_comp_to_use; i < path_comp.size(); i++) {
677 result_str.append(FilePathToUTF8(path_comp[i]));
678 result_str.push_back('/');
680 return SourceDir(result_str);
683 SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root) {
684 base::FilePath cd;
685 base::GetCurrentDirectory(&cd);
686 return SourceDirForPath(source_root, cd);
689 std::string GetOutputSubdirName(const Label& toolchain_label, bool is_default) {
690 // The default toolchain has no subdir.
691 if (is_default)
692 return std::string();
694 // For now just assume the toolchain name is always a valid dir name. We may
695 // want to clean up the in the future.
696 return toolchain_label.name() + "/";
699 SourceDir GetToolchainOutputDir(const Settings* settings) {
700 return settings->toolchain_output_subdir().AsSourceDir(
701 settings->build_settings());
704 SourceDir GetToolchainOutputDir(const BuildSettings* build_settings,
705 const Label& toolchain_label, bool is_default) {
706 std::string result = build_settings->build_dir().value();
707 result.append(GetOutputSubdirName(toolchain_label, is_default));
708 return SourceDir(SourceDir::SWAP_IN, &result);
711 SourceDir GetToolchainGenDir(const Settings* settings) {
712 return GetToolchainGenDirAsOutputFile(settings).AsSourceDir(
713 settings->build_settings());
716 OutputFile GetToolchainGenDirAsOutputFile(const Settings* settings) {
717 OutputFile result(settings->toolchain_output_subdir());
718 result.value().append("gen/");
719 return result;
722 SourceDir GetToolchainGenDir(const BuildSettings* build_settings,
723 const Label& toolchain_label, bool is_default) {
724 std::string result = GetToolchainOutputDir(
725 build_settings, toolchain_label, is_default).value();
726 result.append("gen/");
727 return SourceDir(SourceDir::SWAP_IN, &result);
730 SourceDir GetOutputDirForSourceDir(const Settings* settings,
731 const SourceDir& source_dir) {
732 return GetOutputDirForSourceDir(
733 settings->build_settings(), source_dir,
734 settings->toolchain_label(), settings->is_default());
737 SourceDir GetOutputDirForSourceDir(
738 const BuildSettings* build_settings,
739 const SourceDir& source_dir,
740 const Label& toolchain_label,
741 bool is_default_toolchain) {
742 return GetOutputDirForSourceDirAsOutputFile(
743 build_settings, source_dir, toolchain_label, is_default_toolchain)
744 .AsSourceDir(build_settings);
747 OutputFile GetOutputDirForSourceDirAsOutputFile(
748 const BuildSettings* build_settings,
749 const SourceDir& source_dir,
750 const Label& toolchain_label,
751 bool is_default_toolchain) {
752 OutputFile result(GetOutputSubdirName(toolchain_label, is_default_toolchain));
753 result.value().append("obj/");
755 if (source_dir.is_source_absolute()) {
756 // The source dir is source-absolute, so we trim off the two leading
757 // slashes to append to the toolchain object directory.
758 result.value().append(&source_dir.value()[2],
759 source_dir.value().size() - 2);
760 } else {
761 // System-absolute.
762 const std::string& build_dir = build_settings->build_dir().value();
764 if (base::StartsWith(source_dir.value(), build_dir,
765 base::CompareCase::SENSITIVE)) {
766 size_t build_dir_size = build_dir.size();
767 result.value().append(&source_dir.value()[build_dir_size],
768 source_dir.value().size() - build_dir_size);
769 } else {
770 result.value().append("ABS_PATH");
771 #if defined(OS_WIN)
772 // Windows absolute path contains ':' after drive letter. Remove it to
773 // avoid inserting ':' in the middle of path (eg. "ABS_PATH/C:/").
774 std::string src_dir_value = source_dir.value();
775 const auto colon_pos = src_dir_value.find(':');
776 if (colon_pos != std::string::npos)
777 src_dir_value.erase(src_dir_value.begin() + colon_pos);
778 #else
779 const std::string& src_dir_value = source_dir.value();
780 #endif
781 result.value().append(src_dir_value);
784 return result;
787 OutputFile GetOutputDirForSourceDirAsOutputFile(const Settings* settings,
788 const SourceDir& source_dir) {
789 return GetOutputDirForSourceDirAsOutputFile(
790 settings->build_settings(), source_dir,
791 settings->toolchain_label(), settings->is_default());
794 SourceDir GetGenDirForSourceDir(const Settings* settings,
795 const SourceDir& source_dir) {
796 return GetGenDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir(
797 settings->build_settings());
800 OutputFile GetGenDirForSourceDirAsOutputFile(const Settings* settings,
801 const SourceDir& source_dir) {
802 OutputFile result = GetToolchainGenDirAsOutputFile(settings);
804 if (source_dir.is_source_absolute()) {
805 // The source dir should be source-absolute, so we trim off the two leading
806 // slashes to append to the toolchain object directory.
807 DCHECK(source_dir.is_source_absolute());
808 result.value().append(&source_dir.value()[2],
809 source_dir.value().size() - 2);
811 return result;
814 SourceDir GetTargetOutputDir(const Target* target) {
815 return GetOutputDirForSourceDirAsOutputFile(
816 target->settings(), target->label().dir()).AsSourceDir(
817 target->settings()->build_settings());
820 OutputFile GetTargetOutputDirAsOutputFile(const Target* target) {
821 return GetOutputDirForSourceDirAsOutputFile(
822 target->settings(), target->label().dir());
825 SourceDir GetTargetGenDir(const Target* target) {
826 return GetTargetGenDirAsOutputFile(target).AsSourceDir(
827 target->settings()->build_settings());
830 OutputFile GetTargetGenDirAsOutputFile(const Target* target) {
831 return GetGenDirForSourceDirAsOutputFile(
832 target->settings(), target->label().dir());
835 SourceDir GetCurrentOutputDir(const Scope* scope) {
836 return GetOutputDirForSourceDirAsOutputFile(
837 scope->settings(), scope->GetSourceDir()).AsSourceDir(
838 scope->settings()->build_settings());
841 SourceDir GetCurrentGenDir(const Scope* scope) {
842 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir());