diff options
author | tomlu <tomlu@google.com> | 2017-12-14 12:37:58 -0500 |
---|---|---|
committer | John Cater <jcater@google.com> | 2017-12-14 12:40:27 -0500 |
commit | 8b3ba50246fed6ff13d70299fb039cc66be465c4 (patch) | |
tree | ba0dc049c248529b464fa0af91267dc59a3f29fe /src/main/java/com/google/devtools/build/lib/util | |
parent | e6863e2336f3aacac8181394f5edd92cac9471a9 (diff) |
Move msys path support into DependencySet, removing it from the general Path system.
This is the only place that should actually need it.
PiperOrigin-RevId: 179054861
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/util')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/util/DependencySet.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/util/DependencySet.java b/src/main/java/com/google/devtools/build/lib/util/DependencySet.java index 358610d14d..e2ce5fb6ce 100644 --- a/src/main/java/com/google/devtools/build/lib/util/DependencySet.java +++ b/src/main/java/com/google/devtools/build/lib/util/DependencySet.java @@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.concurrent.atomic.AtomicReference; /** * Representation of a set of file dependencies for a given output file. There @@ -99,10 +100,18 @@ public final class DependencySet { * Adds a given dependency to this DependencySet instance. */ private void addDependency(String dep) { + dep = translatePath(dep); Path depPath = root.getRelative(dep); dependencies.add(depPath); } + private String translatePath(String path) { + if (OS.getCurrent() != OS.WINDOWS) { + return path; + } + return WindowsPath.translateWindowsPath(path); + } + /** * Reads a dotd file into this DependencySet instance. */ @@ -241,4 +250,64 @@ public final class DependencySet { public int hashCode() { return dependencies.hashCode(); } + + private static final class WindowsPath { + private static final AtomicReference<String> UNIX_ROOT = new AtomicReference<>(null); + + private static String translateWindowsPath(String path) { + int n = path.length(); + if (n == 0 || path.charAt(0) != '/') { + return path; + } + if (n >= 2 && isAsciiLetter(path.charAt(1)) && (n == 2 || path.charAt(2) == '/')) { + StringBuilder sb = new StringBuilder(path.length()); + sb.append(Character.toUpperCase(path.charAt(1))); + sb.append(":/"); + sb.append(path, 2, path.length()); + return sb.toString(); + } else { + String unixRoot = getUnixRoot(); + return unixRoot + path; + } + } + + private static boolean isAsciiLetter(char c) { + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); + } + + private static String getUnixRoot() { + String value = UNIX_ROOT.get(); + if (value == null) { + String jvmFlag = "bazel.windows_unix_root"; + value = determineUnixRoot(jvmFlag); + if (value == null) { + throw new IllegalStateException( + String.format( + "\"%1$s\" JVM flag is not set. Use the --host_jvm_args flag. " + + "For example: " + + "\"--host_jvm_args=-D%1$s=c:/tools/msys64\".", + jvmFlag)); + } + value = value.replace('\\', '/'); + if (value.length() > 3 && value.endsWith("/")) { + value = value.substring(0, value.length() - 1); + } + UNIX_ROOT.set(value); + } + return value; + } + + private static String determineUnixRoot(String jvmArgName) { + // Get the path from a JVM flag, if specified. + String path = System.getProperty(jvmArgName); + if (path == null) { + return null; + } + path = path.trim(); + if (path.isEmpty()) { + return null; + } + return path; + } + } } |