From: Joel Rosdahl Date: Wed, 13 Aug 2025 17:36:53 +0000 (+0200) Subject: fix: Support wide characters in filenames for MSVC X-Git-Tag: v4.12~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=441da4b4408e5e21a15e5f3c6cc6e50a5db92842;p=thirdparty%2Fccache.git fix: Support wide characters in filenames for MSVC Fixes #1619. --- diff --git a/cmake/ccache.exe.manifest.in b/cmake/ccache.exe.manifest.in index 0321dc37..6b8960b4 100644 --- a/cmake/ccache.exe.manifest.in +++ b/cmake/ccache.exe.manifest.in @@ -2,10 +2,12 @@ true + UTF-8 diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index 053c5d6a..6177823e 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -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 . 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) { diff --git a/src/ccache/core/statistic.hpp b/src/ccache/core/statistic.hpp index 55c30acc..6ec2dd69 100644 --- a/src/ccache/core/statistic.hpp +++ b/src/ccache/core/statistic.hpp @@ -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 { diff --git a/src/ccache/core/statistics.cpp b/src/ccache/core/statistics.cpp index 9d9aa3e4..80082b75 100644 --- a/src/ccache/core/statistics.cpp +++ b/src/ccache/core/statistics.cpp @@ -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",