Skip to content

Commit

Permalink
Make filetypes case insensitive on Windows
Browse files Browse the repository at this point in the history
Filetypes are based on checking file-extensions which are case insensitive on Windows.
For example, A lib file could be named .lib, Lib, etc which is currently not allowed
in for example cc_import.

This patch allows file extensions to vary in case on Windows.

Closes #14005.

PiperOrigin-RevId: 401589764
  • Loading branch information
torgil authored and copybara-github committed Oct 7, 2021
1 parent 5344e9b commit 861584c
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/actions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/util:var_int",
"//src/main/java/com/google/devtools/build/lib/util/io",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:ospathpolicy",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs",
"//src/main/java/com/google/devtools/build/skyframe",
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/cmdline/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:string",
"//src/main/java/com/google/devtools/build/lib/vfs:ospathpolicy",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//third_party:auto_value",
"//third_party:caffeine",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/util:string",
"//src/main/java/com/google/devtools/build/lib/util/io",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:ospathpolicy",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.util.FileType;
import com.google.devtools.build.lib.util.FileTypeSet;
import com.google.devtools.build.lib.vfs.OsPathPolicy;
import java.util.regex.Pattern;

/**
* C++-related file type definitions.
*/
public final class CppFileTypes {
private static final OsPathPolicy OS = OsPathPolicy.getFilePathOs();

// .cu and .cl are CUDA and OpenCL source extensions, respectively. They are expected to only be
// supported with clang. Bazel is not officially supporting these targets, and the extensions are
// listed only as long as they work with the existing C++ actions.
Expand Down Expand Up @@ -64,7 +67,7 @@ public final class CppFileTypes {

@Override
public boolean apply(String path) {
return path.endsWith(ext) && !PIC_PREPROCESSED_C.matches(path);
return OS.endsWith(path, ext) && !PIC_PREPROCESSED_C.matches(path);
}

@Override
Expand All @@ -79,7 +82,7 @@ public ImmutableList<String> getExtensions() {

@Override
public boolean apply(String path) {
return path.endsWith(ext) && !PIC_PREPROCESSED_CPP.matches(path);
return OS.endsWith(path, ext) && !PIC_PREPROCESSED_CPP.matches(path);
}

@Override
Expand All @@ -96,7 +99,8 @@ public ImmutableList<String> getExtensions() {

@Override
public boolean apply(String path) {
return (path.endsWith(ext) && !PIC_ASSEMBLER.matches(path)) || path.endsWith(".asm");
return (OS.endsWith(path, ext) && !PIC_ASSEMBLER.matches(path))
|| OS.endsWith(path, ".asm");
}

@Override
Expand All @@ -114,11 +118,11 @@ public ImmutableList<String> getExtensions() {
public boolean apply(String path) {
if (PIC_ARCHIVE.matches(path)
|| ALWAYS_LINK_LIBRARY.matches(path)
|| path.endsWith(".if.lib")) {
|| OS.endsWith(path, ".if.lib")) {
return false;
}
for (String ext : extensions) {
if (path.endsWith(ext)) {
if (OS.endsWith(path, ext)) {
return true;
}
}
Expand All @@ -138,8 +142,8 @@ public ImmutableList<String> getExtensions() {

@Override
public boolean apply(String path) {
return (path.endsWith(ext) && !ALWAYS_LINK_PIC_LIBRARY.matches(path))
|| path.endsWith(".lo.lib");
return (OS.endsWith(path, ext) && !ALWAYS_LINK_PIC_LIBRARY.matches(path))
|| OS.endsWith(path, ".lo.lib");
}

@Override
Expand All @@ -155,7 +159,8 @@ public ImmutableList<String> getExtensions() {

@Override
public boolean apply(String path) {
return (path.endsWith(ext) && !PIC_OBJECT_FILE.matches(path)) || path.endsWith(".obj");
return (OS.endsWith(path, ext) && !PIC_OBJECT_FILE.matches(path))
|| OS.endsWith(path, ".obj");
}

@Override
Expand Down Expand Up @@ -220,7 +225,7 @@ public boolean apply(String path) {
// The current clang (clang-600.0.57) on Darwin doesn't support 'textual', so we can't
// have '.inc' files in the module map (since they're implictly textual).
// TODO(bazel-team): Use HEADERS file type once clang-700 is the base clang we support.
return artifact.getFilename().endsWith(".h");
return OS.endsWith(artifact.getFilename(), ".h");
}
};

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ java_library(
deps = [
":string",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec",
"//src/main/java/com/google/devtools/build/lib/vfs:ospathpolicy",
"//third_party:guava",
"//third_party:jsr305",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import com.google.devtools.build.lib.vfs.OsPathPolicy;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.concurrent.Immutable;

/** A base class for FileType matchers. */
@Immutable
public abstract class FileType implements Predicate<String> {
private static final OsPathPolicy OS = OsPathPolicy.getFilePathOs();

// A special file type
@AutoCodec @VisibleForSerialization
public static final FileType NO_EXTENSION =
Expand Down Expand Up @@ -63,7 +66,7 @@ static final class SingletonFileType extends FileType {

@Override
public boolean apply(String path) {
return path.endsWith(ext);
return OS.endsWith(path, ext);
}

@Override
Expand All @@ -86,7 +89,7 @@ static final class ListFileType extends FileType {
public boolean apply(String path) {
// Do not use an iterator based for loop here as that creates excessive garbage.
for (int i = 0; i < extensions.size(); i++) {
if (path.endsWith(extensions.get(i))) {
if (OS.endsWith(path, extensions.get(i))) {
return true;
}
}
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/com/google/devtools/build/lib/vfs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@ filegroup(
visibility = ["//src:__subpackages__"],
)

OS_PATH_POLICY_SOURCES = [
"OsPathPolicy.java",
"UnixOsPathPolicy.java",
"WindowsOsPathPolicy.java",
]

PATH_FRAGMENT_SOURCES = [
"PathFragment.java",
"PathFragmentSerializationProxy.java",
"PathSegmentIterator.java",
"OsPathPolicy.java",
"UnixOsPathPolicy.java",
"WindowsOsPathPolicy.java",
]

OUTPUT_SERVICE_SOURCES = [
"OutputService.java",
]

java_library(
name = "ospathpolicy",
srcs = OS_PATH_POLICY_SOURCES,
deps = [
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/main/java/com/google/devtools/build/lib/windows:file",
"//src/main/java/com/google/devtools/build/lib/windows:windows_short_path",
"//third_party:guava",
],
)

java_library(
name = "pathfragment",
srcs = PATH_FRAGMENT_SOURCES,
deps = [
":ospathpolicy",
"//src/main/java/com/google/devtools/build/lib/actions:commandline_item",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/util:filetype",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/main/java/com/google/devtools/build/lib/windows:file",
"//src/main/java/com/google/devtools/build/lib/windows:windows_short_path",
"//third_party:error_prone_annotations",
"//third_party:guava",
"//third_party:jsr305",
Expand All @@ -45,7 +57,7 @@ java_library(
[
"*.java",
],
exclude = PATH_FRAGMENT_SOURCES + OUTPUT_SERVICE_SOURCES,
exclude = PATH_FRAGMENT_SOURCES + OUTPUT_SERVICE_SOURCES + OS_PATH_POLICY_SOURCES,
),
deps = [
":pathfragment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/concurrent",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:ospathpolicy",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//third_party:auto_value",
"//third_party:guava",
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/google/devtools/build/lib/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ java_test(
],
)

# Tests windows specific filetype handling on Unix.
java_library(
name = "FileTypeTests_lib",
srcs = ["FileTypeTest.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib/util:filetype",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs",
"//third_party:guava",
"//third_party:junit4",
"//third_party:truth",
],
)

java_test(
name = "FileTypeWindowsTests",
size = "small",
jvm_flags = [
"-Dblaze.os=Windows",
"-Dbazel.windows_unix_root=C:/fake/msys",
],
test_class = "com.google.devtools.build.lib.AllTests",
runtime_deps = [
":FileTypeTests_lib",
"//src/test/java/com/google/devtools/build/lib:test_runner",
],
)

# Tests windows specific path handling on Unix.
java_library(
name = "UtilWindowsTests_lib",
Expand Down
Loading

0 comments on commit 861584c

Please sign in to comment.