Make rebase_path() aware of Windows drive letter capitalization

Make sure rebase_path() supports both capital and non-capital Windows path
drive letters. It's unable to find common path prefix otherwise.

BUG=596072

Review URL: https://codereview.chromium.org/1817533002

Cr-Original-Commit-Position: refs/heads/master@{#382532}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 01a402cd6fb6873e61d2faa269b6b991d307635e
diff --git a/tools/gn/filesystem_utils.cc b/tools/gn/filesystem_utils.cc
index 2ceca81..facf442 100644
--- a/tools/gn/filesystem_utils.cc
+++ b/tools/gn/filesystem_utils.cc
@@ -529,6 +529,19 @@
     corrected_dest.append(dest);
     return MakeRelativePath(input, corrected_dest);
   }
+
+  // Make sure that both absolute paths use the same drive letter case.
+  if (IsPathAbsolute(input) && IsPathAbsolute(dest) && input.size() > 1 &&
+      dest.size() > 1) {
+    int letter_pos = base::IsAsciiAlpha(input[0]) ? 0 : 1;
+    if (input[letter_pos] != dest[letter_pos] &&
+        base::ToUpperASCII(input[letter_pos]) ==
+            base::ToUpperASCII(dest[letter_pos])) {
+      std::string corrected_input = input;
+      corrected_input[letter_pos] = dest[letter_pos];
+      return MakeRelativePath(corrected_input, dest);
+    }
+  }
 #endif
 
   std::string ret;
diff --git a/tools/gn/filesystem_utils_unittest.cc b/tools/gn/filesystem_utils_unittest.cc
index d563890..894b3da 100644
--- a/tools/gn/filesystem_utils_unittest.cc
+++ b/tools/gn/filesystem_utils_unittest.cc
@@ -488,6 +488,24 @@
             RebasePath("/path/to/foo", SourceDir("/source/root/a/b"),
                        base::StringPiece("/x/y/z")));
 
+#if defined(OS_WIN)
+  // Test corrections while rebasing Windows-style absolute paths.
+  EXPECT_EQ("../../../../path/to/foo",
+            RebasePath("C:/path/to/foo", SourceDir("//a/b"),
+                       base::StringPiece("/C:/source/root")));
+  EXPECT_EQ("../../../../path/to/foo",
+            RebasePath("/C:/path/to/foo", SourceDir("//a/b"),
+                       base::StringPiece("C:/source/root")));
+  EXPECT_EQ("../../../../path/to/foo",
+            RebasePath("/C:/path/to/foo", SourceDir("//a/b"),
+                       base::StringPiece("/c:/source/root")));
+  EXPECT_EQ("../../../../path/to/foo",
+            RebasePath("/c:/path/to/foo", SourceDir("//a/b"),
+                       base::StringPiece("c:/source/root")));
+  EXPECT_EQ("../../../../path/to/foo",
+            RebasePath("/c:/path/to/foo", SourceDir("//a/b"),
+                       base::StringPiece("C:/source/root")));
+#endif
 }
 
 TEST(FilesystemUtils, DirectoryWithNoLastSlash) {