From 629fdd59d18667f9d416a7cf0d558c27ee891a9a Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Wed, 4 Dec 2024 22:47:01 +0900 Subject: [PATCH] Simplify ninja env file logic in `build_mozc.py` As a preparation to build Mozc for ARM64 in Windows (#1130), this commit aims to simplify UpdateEnvironmentFilesForWindows in build_mozc.py so that we can tweak environment file for ARM64 build configurations in a subsequent commit easily. This is still a mechanical code clean up. There must be no difference in the final artifacts. --- src/build_mozc.py | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/build_mozc.py b/src/build_mozc.py index f3f55e9dc..6e17812cc 100755 --- a/src/build_mozc.py +++ b/src/build_mozc.py @@ -349,6 +349,27 @@ def ParseCleanOptions(args): return parser.parse_args(args) +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): """Add required environment variables for Ninja build.""" python_path_root = MOZC_ROOT @@ -356,22 +377,14 @@ def UpdateEnvironmentFilesForWindows(out_dir): 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) - + for arch in ['x86', 'x64']: + env_file = os.path.join(abs_dir, f'environment.{arch}') + env = ReadEnvironmentFile(env_file) + env['PYTHONPATH'] = python_path + env['VSLANG'] = '1033' # == MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US) + WriteEnvironmentFile(env_file, env) def GypMain(options, unused_args):