From 8e3fea8b76634dd28caa0b76b0d7246f64eaa1a2 Mon Sep 17 00:00:00 2001 From: Kai Zhang Date: Tue, 18 Apr 2023 01:27:52 +0800 Subject: [PATCH] Make cpp file extensions case sensitive again This fixes an issue introduced by PR #14005 where .c and .C extensions were handled case-insensitive on Windows so the cxxopt will be passed to C source files. Closes #15073 . --- .../build/lib/rules/cpp/CppFileTypes.java | 38 ++++++++++++++++++- .../build/lib/rules/cpp/CppFileTypesTest.java | 8 ++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java index d525d0be65d8e5..a8e7c535240e14 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java @@ -30,9 +30,43 @@ public final class CppFileTypes { // .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. + // FileType is extended to use case-sensitive comparison also on Windows public static final FileType CPP_SOURCE = - FileType.of(".cc", ".cpp", ".cxx", ".c++", ".C", ".cu", ".cl"); - public static final FileType C_SOURCE = FileType.of(".c"); + new FileType() { + final ImmutableList extensions = ImmutableList.of(".cc", ".cpp", ".cxx", ".c++", ".C", ".cu", ".cl"); + + @Override + public boolean apply(String path) { + for (String ext : extensions) { + if (path.endsWith(ext)) { + return true; + } + } + return false; + } + + @Override + public ImmutableList getExtensions() { + return extensions; + } + }; + + // FileType is extended to use case-sensitive comparison also on Windows + public static final FileType C_SOURCE = + new FileType() { + final String ext = ".c"; + + @Override + public boolean apply(String path) { + return path.endsWith(ext); + } + + @Override + public ImmutableList getExtensions() { + return ImmutableList.of(ext); + } + }; + public static final FileType OBJC_SOURCE = FileType.of(".m"); public static final FileType OBJCPP_SOURCE = FileType.of(".mm"); public static final FileType CLIF_INPUT_PROTO = FileType.of(".ipb"); diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppFileTypesTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppFileTypesTest.java index 88aa7ba707292f..4487d6545b8096 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppFileTypesTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppFileTypesTest.java @@ -77,4 +77,12 @@ public void testNoExtensionLibraries() { assertThat(Link.ARCHIVE_LIBRARY_FILETYPES.matches("someframework")).isTrue(); assertThat(Link.ARCHIVE_FILETYPES.matches("someframework")).isTrue(); } + + @Test + public void testCaseSensitiveCFiles() { + assertThat(CppFileTypes.C_SOURCE.matches("foo.c")).isTrue(); + assertThat(CppFileTypes.CPP_SOURCE.matches("foo.c")).isFalse(); + assertThat(CppFileTypes.C_SOURCE.matches("foo.C")).isFalse(); + assertThat(CppFileTypes.CPP_SOURCE.matches("foo.C")).isTrue(); + } }