]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Support wide characters in filenames for MSVC
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 13 Aug 2025 17:36:53 +0000 (19:36 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 16 Aug 2025 11:18:39 +0000 (13:18 +0200)
Fixes #1619.

cmake/ccache.exe.manifest.in
src/ccache/ccache.cpp
src/ccache/core/statistic.hpp
src/ccache/core/statistics.cpp

index 0321dc37e393df63e307599e302ba230e27f05ef..6b8960b420f77e667756c3bae07dce12e3156ad0 100644 (file)
@@ -2,10 +2,12 @@
 <assembly xmlns="urn:schemas-microsoft-com:asm.v1"
           xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"
           xmlns:ws2016="http://schemas.microsoft.com/SMI/2016/WindowsSettings"
+          xmlns:ws2019="http://schemas.microsoft.com/SMI/2019/WindowsSettings"
           manifestVersion="1.0">
   <asmv3:application>
     <asmv3:windowsSettings>
       <ws2016:longPathAware>true</ws2016:longPathAware>
+      <ws2019:activeCodePage>UTF-8</ws2019:activeCodePage>
     </asmv3:windowsSettings>
   </asmv3:application>
 </assembly>
index 053c5d6a65e8ebcc7f449e144364e6ab409dbc3f..6177823e52735baaf76e89ef837b085bee726cf6 100644 (file)
@@ -1415,6 +1415,7 @@ get_result_key_from_cpp(Context& ctx, util::Args& args, Hash& hash)
     // compilers that don't exit with a proper status on write error to stdout.
     // See also <https://github.com/llvm/llvm-project/issues/56499>.
     if (ctx.config.is_compiler_group_msvc()) {
+      args.push_back("-utf-8"); // Avoid garbling filenames in output
       args.push_back("-P");
       args.push_back(FMT("-Fi{}", preprocessed_path));
     } else {
@@ -1442,6 +1443,17 @@ get_result_key_from_cpp(Context& ctx, util::Args& args, Hash& hash)
 
     cpp_stderr_data = result->stderr_data;
     cpp_stdout_data = result->stdout_data;
+
+    if (ctx.config.is_compiler_group_msvc()) {
+      // Check that usage of -utf-8 didn't garble the preprocessor output.
+      static constexpr char warning_c4828[] =
+        "warning C4828: The file contains a character starting at offset";
+      if (util::to_string_view(cpp_stderr_data).find(warning_c4828)
+          != std::string_view::npos) {
+        LOG_RAW("Non-UTF-8 source code unsupported in preprocessor mode");
+        return tl::unexpected(Statistic::unsupported_source_encoding);
+      }
+    }
   }
 
   if (is_clang_cu) {
index 55c30acc5677099717b20ea3cd1fe3a454ea7337..6ec2dd6910b1eba5e95f3a7637b96111d204d6c9 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -81,7 +81,9 @@ enum class Statistic {
   disabled = 81,
   bad_input_file = 82,
   modified_input_file = 83,
-  END = 84
+  unsupported_source_encoding = 84,
+
+  END = 85,
 };
 
 enum class StatisticsFormat {
index 9d9aa3e4bfb9855ab71c7747ec22aace0f8140ae..80082b751d92216ab972908799a8de01824c853d 100644 (file)
@@ -252,6 +252,12 @@ const StatisticsField k_statistics_fields[] = {
         "Unsupported environment variable",
         FLAG_UNCACHEABLE),
 
+  // Source file (or an included header) has unsupported encoding. ccache
+  // currently requires UTF-8-encoded source code for MSVC.
+  FIELD(unsupported_source_encoding,
+        "Unsupported source encoding",
+        FLAG_UNCACHEABLE),
+
   // A source language e.g. specified with -x was unsupported by ccache.
   FIELD(unsupported_source_language,
         "Unsupported source language",