Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yukawa committed Dec 4, 2024
1 parent 8e7a307 commit 9128cb8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 25 deletions.
68 changes: 52 additions & 16 deletions src/build_mozc.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import optparse
import os
import pathlib
import platform
import re
import subprocess
import sys
Expand Down Expand Up @@ -349,29 +350,64 @@ def ParseCleanOptions(args):
return parser.parse_args(args)


def UpdateEnvironmentFilesForWindows(out_dir):
def ReadEnvironmentFile(path):
nul = chr(0)
with open(path, 'rb') as f:
content = f.read()
entries = content.decode('utf-8').split(nul)
env = dict()
for e in entries:
if '=' in e:
key, value = e.split('=', 1)
env[key] = value
return env


def WriteEnvironmentFile(path, env):
nul = chr(0)
entries = [f'{key}={value}' for (key, value) in env.items()]
entries.extend(['', ''])
with open(path, 'wb') as f:
f.write(nul.join(entries).encode('utf-8'))


def UpdateEnvironmentFilesForWindows(out_dir, vcvarsall_path):
"""Add required environment variables for Ninja build."""
python_path_root = MOZC_ROOT
python_path = os.path.abspath(python_path_root)
original_python_paths = os.environ.get('PYTHONPATH', '')
if original_python_paths:
python_path = os.pathsep.join([original_python_paths, python_path])
nul = chr(0)
additional_content = nul.join([
'PYTHONPATH=' + python_path,
'VSLANG=1033', # 1033 == MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)
nul]).encode('utf-8')

for d in os.listdir(out_dir):
abs_dir = os.path.abspath(os.path.join(out_dir, d))
with open(os.path.join(abs_dir, 'environment.x86'), 'rb') as x86_file:
x86_content = x86_file.read()[:-1] + additional_content
with open(os.path.join(abs_dir, 'environment.x86'), 'wb') as x86_file:
x86_file.write(x86_content)
with open(os.path.join(abs_dir, 'environment.x64'), 'rb') as x64_file:
x64_content = x64_file.read()[:-1] + additional_content
with open(os.path.join(abs_dir, 'environment.x64'), 'wb') as x64_file:
x64_file.write(x64_content)

# Tweak generated build rules for ARM64
if d.endswith('arm64'):
build_ninja = os.path.join(abs_dir, 'build.ninja')
with open(build_ninja, 'r', encoding='utf-8') as f:
lines = f.readlines()
for i in range(0, 2):
lines[i] = lines[i].replace('x64\\cl.exe', 'arm64\\cl.exe')
lines[i] = lines[i].replace('x86\\cl.exe', 'arm64\\cl.exe')
with open(build_ninja, 'w', encoding='utf-8') as f:
f.writelines(lines)

for arch in ['x86', 'x64']:
env_file = os.path.join(abs_dir, f'environment.{arch}')
env = ReadEnvironmentFile(env_file)
# Tweak for ARM64
if d.endswith('arm64'):
vs_arch = platform.uname().machine
if vs_arch != 'arm64':
vs_arch = vs_arch + '_arm64'
vs_env = get_vs_env_vars(vs_arch)
env['INCLUDE'] = vs_env['INCLUDE']
env['LIB'] = vs_env['LIB']
env['LIBPATH'] = vs_env['LIBPATH']
env['PATH'] = vs_env['PATH']
env['PYTHONPATH'] = python_path
env['VSLANG'] = '1033' # == MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)
WriteEnvironmentFile(env_file, env)


def GypMain(options, unused_args):
Expand Down Expand Up @@ -548,7 +584,7 @@ def GypMain(options, unused_args):
# For internal Ninja build on Windows, set up environment files
if IsWindows():
out_dir = os.path.join(MOZC_ROOT, 'out_win')
UpdateEnvironmentFilesForWindows(out_dir)
UpdateEnvironmentFilesForWindows(out_dir, options.vcvarsall_path)

if IsWindows() and qt_dir and qt_ver:
# When Windows build is configured to use DLL version of Qt, copy Qt's DLLs
Expand Down
2 changes: 2 additions & 0 deletions src/build_tools/protoc_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def main():
protoc_path = opts.protoc_command
if opts.protoc_dir:
protoc_path = os.path.join(os.path.abspath(opts.protoc_dir), protoc_path)
if os.name == 'nt' and protoc_path.endswith('_arm64\\protoc.exe'):
protoc_path = protoc_path.replace('_arm64\\protoc', '_x64\\protoc')

# The path of proto file should be transformed as a relative path from
# the project root so that correct relative paths should be embedded into
Expand Down
27 changes: 27 additions & 0 deletions src/gyp/common_win.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,27 @@
},
},
},
'arm64_Base': {
'abstract': 1,
'msvs_configuration_attributes': {
'OutputDirectory': '<(build_base)/$(ConfigurationName)_arm64',
'IntermediateDirectory': '<(build_base)/$(ConfigurationName)_arm64/obj/$(ProjectName)',
},
'msvs_target_platform': 'arm64',
'msvs_settings': {
'VCCLCompilerTool': {
'AdditionalOptions': [
'/bigobj',
],
},
'VCLinkerTool': {
'ImageHasSafeExceptionHandlers': 'false',
'AdditionalOptions': [
'/MACHINE:ARM64',
],
},
},
},
'Win_Static_Debug_CRT_Base': {
'abstract': 1,
'msvs_settings': {
Expand Down Expand Up @@ -276,6 +297,12 @@
'Release_x64': {
'inherit_from': ['x64_Base', 'Release_Base', 'Win_Static_Release_CRT_Base'],
},
'Debug_arm64': {
'inherit_from': ['arm64_Base', 'Debug_Base', 'Win_Static_Debug_CRT_Base'],
},
'Release_arm64': {
'inherit_from': ['arm64_Base', 'Release_Base', 'Win_Static_Release_CRT_Base'],
},
},
'default_configuration': 'Debug',
'defines': [
Expand Down
16 changes: 7 additions & 9 deletions src/win32/tip/tip_text_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <wil/com.h>
#include <windows.h>

#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <memory>
Expand Down Expand Up @@ -249,15 +251,11 @@ wil::com_ptr_nothrow<ITfCategoryMgr> GetCategoryMgr() {
template <typename T>
struct ComPtrHash {
size_t operator()(const wil::com_ptr_nothrow<T> &value) const {
// Caveats: On x86 environment, both _M_X64 and _M_IX86 are defined. So we
// need to check _M_X64 first.
#if defined(_M_X64)
constexpr size_t kUnusedBits = 3; // assuming 8-byte aligned
#elif defined(_M_IX86) // defined(_M_X64)
constexpr size_t kUnusedBits = 2; // assuming 4-byte aligned
#else // defined(_M_IX86)
#error "unsupported platform"
#endif // defined(_M_IX86)
// The minimum size of COM objects is the pointer to vtable.
// For instance the last 3 bits are guaranteed to be zero on 64-bit
// processes.
constexpr size_t kUnusedBits =
std::max(std::bit_width(sizeof(void *)), 1) - 1;
// Compress the data by shifting unused bits.
return reinterpret_cast<size_t>(value.get()) >> kUnusedBits;
}
Expand Down

0 comments on commit 9128cb8

Please sign in to comment.