Skip to content

Commit

Permalink
Set charset for source and execution on Windows
Browse files Browse the repository at this point in the history
- For the discussion after doxygen#10084
is merged.
- Keep minimal imports in QueryCodePage.py as reviewed
  • Loading branch information
zchrissirhcz committed May 29, 2023
1 parent dfb8542 commit ae6edb5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[*]
indent_style = space
indent_size = 2
charset = utf-8

[*.py]
indent_size = 4
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ endif()

include(cmake/CompilerWarnings.cmake)
include(cmake/Coverage.cmake)
include(cmake/WindowsEncoding.cmake)

add_subdirectory(deps)
add_subdirectory(libversion)
Expand Down
21 changes: 21 additions & 0 deletions cmake/QueryCodePage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import platform

def is_windows():
return platform.system().lower() == "windows"

if is_windows():
from winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE, KEY_READ

if __name__ == '__main__':
if is_windows():
root = HKEY_LOCAL_MACHINE
subkey = R'SYSTEM\CurrentControlSet\Control\Nls\CodePage'

key = OpenKey(root, subkey, 0, KEY_READ)
name = 'ACP'

try:
codepage, _ = QueryValueEx(key, name)
print(codepage)
except WindowsError:
print('Failed to get code page')
18 changes: 18 additions & 0 deletions cmake/WindowsEncoding.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
include(FindPythonInterp)
execute_process(
COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_SOURCE_DIR}/cmake/QueryCodePage.py"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE ReturnCode
OUTPUT_VARIABLE CodePage
)
message(STATUS "CodePage is ${CodePage}")
if("${CodePage}" STREQUAL "936")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /source-charset:utf-8 /execution-charset:gbk")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /source-charset:utf-8 /execution-charset:gbk")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj") # /bigobj needed for language.cpp on 64bit
endif()

0 comments on commit ae6edb5

Please sign in to comment.