build: set /utf-8 flag when using MSVC (#4975)

MSVC by default decodes source files in the current Windows code page if
they don't have a Unicode BOM, and also encodes strings and chars into
the current code page before storing them into the compiled binary. Our
files are always encoded in UTF-8, and our code always assumes runtime
strings are encoded in UTF-8, so we should pass the `/utf-8` flag to
MSVC.

Microsoft Docs:
https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8
This commit is contained in:
fghzxm 2023-02-22 20:00:07 +08:00 committed by GitHub
parent 9d1a141482
commit e2b6dc48b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 0 deletions

View File

@ -181,6 +181,8 @@ if(WIN32)
# Ignore various deprecation and security warnings (at least for now)
set(CMAKE_${L}_FLAGS "${CMAKE_${L}_FLAGS} -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS")
if(MSVC)
# Set source file encoding and execution charset to UTF-8
set(CMAKE_${L}_FLAGS "${CMAKE_${L}_FLAGS} /utf-8")
# Reduce noise (at least for now)
set(CMAKE_${L}_FLAGS "${CMAKE_${L}_FLAGS} /wd4244 /wd4267")
# Make caching-friendly (store debug info inside object files)