Skip to content

Commit

Permalink
Merge branch 'google:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
shitamo authored May 1, 2024
2 parents 92ddb80 + 83641d9 commit 96f51bf
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 121 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ jobs:
shell: cmd
working-directory: .\src
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsamd64_x86.bat"
python build_mozc.py gyp
- name: build package
shell: cmd
working-directory: .\src
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsamd64_x86.bat"
python build_mozc.py build -c Release package
- name: upload Mozc64.msi
Expand Down Expand Up @@ -112,14 +110,12 @@ jobs:
shell: cmd
working-directory: .\src
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsamd64_x86.bat"
python build_mozc.py gyp --noqt --msvs_version=2022
- name: runtests
shell: cmd
working-directory: .\src
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsamd64_x86.bat"
python build_mozc.py runtests -c Debug
# actions/cache works without this job, but having this would increase the likelihood of cache hit
Expand Down
12 changes: 0 additions & 12 deletions docs/build_mozc_in_windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ git clone https://github.com/google/mozc.git
cd mozc\src
python build_tools/update_deps.py
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsamd64_x86.bat"
python build_tools/build_qt.py --release --confirm_license
python build_mozc.py gyp
python build_mozc.py build -c Release package
Expand Down Expand Up @@ -76,15 +73,6 @@ You can skip this step if you would like to manually download these libraries.

## Build

### Setup Build system

If you have not set up the build system in your command prompt, you might need
to execute the setup command like this.

```
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsamd64_x86.bat"
```

### Build Qt

```
Expand Down
17 changes: 12 additions & 5 deletions src/build_mozc.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
from build_tools.util import RemoveFile
from build_tools.util import RunOrDie
from build_tools.util import RunOrDieError
from build_tools.vs_util import get_vs_env_vars

SRC_DIR = '.'
OSS_SRC_DIR = '.'
Expand Down Expand Up @@ -252,6 +253,8 @@ def ParseGypOptions(args):
parser.add_option('--msvs_version', dest='msvs_version',
default='2022',
help='Version of the Visual Studio.')
parser.add_option('--vcvarsall_path', help='Path of vcvarsall.bat',
default=None)

if IsWindows() or IsMac():
parser.add_option('--qtdir', dest='qtdir',
Expand Down Expand Up @@ -373,6 +376,15 @@ def UpdateEnvironmentFilesForWindows(out_dir):

def GypMain(options, unused_args):
"""The main function for the 'gyp' command."""
if IsWindows():
# GYP captures environment variables while running so setting them up as if
# the developer manually executed 'vcvarsall.bat' command. Otherwise we end
# up having to explain how to do that for both cmd.exe and PowerShell.
# See https://github.com/google/mozc/pull/923
env_vars = get_vs_env_vars('x64', options.vcvarsall_path)
for (key, value) in env_vars.items():
os.environ[key] = value

# Generate a version definition file.
logging.info('Generating version definition file...')
template_path = '%s/%s' % (OSS_SRC_DIR, options.version_file)
Expand Down Expand Up @@ -863,11 +875,6 @@ def main():
command = sys.argv[1]
args = sys.argv[2:]

if IsWindows() and (not os.environ.get('VCToolsRedistDir', '')):
print('VCToolsRedistDir is not defined.')
print('Please use Developer Command Prompt or run vcvarsamd64_x86.bat')
return 1

if command == 'gyp':
(cmd_opts, cmd_args) = ParseGypOptions(args)
GypMain(cmd_opts, cmd_args)
Expand Down
88 changes: 1 addition & 87 deletions src/build_tools/build_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
from collections.abc import Iterator
import dataclasses
import functools
import json
import os
import pathlib
import shutil
Expand All @@ -60,6 +59,7 @@
import tarfile
import time
from typing import Any, Union
from vs_util import get_vs_env_vars


ABS_SCRIPT_PATH = pathlib.Path(__file__).absolute()
Expand Down Expand Up @@ -476,92 +476,6 @@ def build_on_mac(args: argparse.Namespace) -> None:
exec_command(install_cmds, cwd=qt_src_dir, env=env, dryrun=args.dryrun)


def get_vcvarsall(path_hint: Union[str, None] = None) -> pathlib.Path:
"""Returns the path of 'vcvarsall.bat'.
Args:
path_hint: optional path to vcvarsall.bat
Returns:
The path of 'vcvarsall.bat'.
Raises:
FileNotFoundError: When 'vcvarsall.bat' cannot be found.
"""
if path_hint is not None:
path = pathlib.Path(path_hint).resolve()
if path.exists():
return path

for program_files in ['Program Files', 'Program Files (x86)']:
for edition in ['Community', 'Professional', 'Enterprise', 'BuildTools']:
vcvarsall = pathlib.Path('C:\\', program_files, 'Microsoft Visual Studio',
'2022', edition, 'VC', 'Auxiliary', 'Build',
'vcvarsall.bat')
if vcvarsall.exists():
return vcvarsall

raise FileNotFoundError(
'Could not find vcvarsall.bat. '
'Consider using --vcvarsall_path option e.g.\n'
'python build_qt.py --release --confirm_license '
r' --vcvarsall_path=C:\VS\VC\Auxiliary\Build\vcvarsall.bat'
)


def get_vs_env_vars(
arch: str,
vcvarsall_path_hint: Union[str, None] = None,
) -> dict[str, str]:
"""Returns environment variables for the specified Visual Studio C++ tool.
Oftentimes commandline build process for Windows requires to us to set up
environment variables (especially 'PATH') first by executing an appropriate
Visual C++ batch file ('vcvarsall.bat'). As a result, commands to be passed
to methods like subprocess.run() can easily become complicated and difficult
to maintain.
With get_vs_env_vars() you can easily decouple environment variable handling
from the actual command execution as follows.
cwd = ...
env = get_vs_env_vars('amd64_x86')
subprocess.run(command_fullpath, shell=False, check=True, cwd=cwd, env=env)
or
cwd = ...
env = get_vs_env_vars('amd64_x86')
subprocess.run(command, shell=True, check=True, cwd=cwd, env=env)
For the 'arch' argument, see the following link to find supported values.
https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line#vcvarsall-syntax
Args:
arch: The architecture to be used to build Qt, e.g. 'amd64_x86'
vcvarsall_path_hint: optional path to vcvarsall.bat
Returns:
A dict of environment variable.
Raises:
ChildProcessError: When 'vcvarsall.bat' cannot be executed.
FileNotFoundError: When 'vcvarsall.bat' cannot be found.
"""
vcvarsall = get_vcvarsall(vcvarsall_path_hint)

pycmd = (r'import json;'
r'import os;'
r'print(json.dumps(dict(os.environ), ensure_ascii=True))')
cmd = f'("{vcvarsall}" {arch}>nul)&&("{sys.executable}" -c "{pycmd}")'
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
stdout, _ = process.communicate()
exitcode = process.wait()
if exitcode != 0:
raise ChildProcessError(f'Failed to execute {vcvarsall}')
return json.loads(stdout.decode('ascii'))


def exec_command(command: list[str], cwd: Union[str, pathlib.Path],
env: dict[str, str], dryrun: bool = False) -> None:
"""Run the specified command.
Expand Down
122 changes: 122 additions & 0 deletions src/build_tools/vs_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# -*- coding: utf-8 -*-
# Copyright 2010-2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""A helper script to use Visual Studio."""
import json
import pathlib
import subprocess
import sys
from typing import Union


def get_vcvarsall(path_hint: Union[str, None] = None) -> pathlib.Path:
"""Returns the path of 'vcvarsall.bat'.
Args:
path_hint: optional path to vcvarsall.bat
Returns:
The path of 'vcvarsall.bat'.
Raises:
FileNotFoundError: When 'vcvarsall.bat' cannot be found.
"""
if path_hint is not None:
path = pathlib.Path(path_hint).resolve()
if path.exists():
return path

for program_files in ['Program Files', 'Program Files (x86)']:
for edition in ['Community', 'Professional', 'Enterprise', 'BuildTools']:
vcvarsall = pathlib.Path('C:\\', program_files, 'Microsoft Visual Studio',
'2022', edition, 'VC', 'Auxiliary', 'Build',
'vcvarsall.bat')
if vcvarsall.exists():
return vcvarsall

raise FileNotFoundError(
'Could not find vcvarsall.bat. '
'Consider using --vcvarsall_path option e.g.\n'
r' --vcvarsall_path=C:\VS\VC\Auxiliary\Build\vcvarsall.bat'
)


def get_vs_env_vars(
arch: str,
vcvarsall_path_hint: Union[str, None] = None,
) -> dict[str, str]:
"""Returns environment variables for the specified Visual Studio C++ tool.
Oftentimes commandline build process for Windows requires to us to set up
environment variables (especially 'PATH') first by executing an appropriate
Visual C++ batch file ('vcvarsall.bat'). As a result, commands to be passed
to methods like subprocess.run() can easily become complicated and difficult
to maintain.
With get_vs_env_vars() you can easily decouple environment variable handling
from the actual command execution as follows.
cwd = ...
env = get_vs_env_vars('amd64_x86')
subprocess.run(command_fullpath, shell=False, check=True, cwd=cwd, env=env)
or
cwd = ...
env = get_vs_env_vars('amd64_x86')
subprocess.run(command, shell=True, check=True, cwd=cwd, env=env)
For the 'arch' argument, see the following link to find supported values.
https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line#vcvarsall-syntax
Args:
arch: The architecture to be used, e.g. 'amd64_x86'
vcvarsall_path_hint: optional path to vcvarsall.bat
Returns:
A dict of environment variable.
Raises:
ChildProcessError: When 'vcvarsall.bat' cannot be executed.
FileNotFoundError: When 'vcvarsall.bat' cannot be found.
"""
vcvarsall = get_vcvarsall(vcvarsall_path_hint)

pycmd = (r'import json;'
r'import os;'
r'print(json.dumps(dict(os.environ), ensure_ascii=True))')
cmd = f'("{vcvarsall}" {arch}>nul)&&("{sys.executable}" -c "{pycmd}")'
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
stdout, _ = process.communicate()
exitcode = process.wait()
if exitcode != 0:
raise ChildProcessError(f'Failed to execute {vcvarsall}')
return json.loads(stdout.decode('ascii'))

16 changes: 8 additions & 8 deletions src/converter/candidate_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1180,21 +1180,21 @@ TEST_P(CandidateFilterTestWithParam, FilterNoisyNumberCandidate) {
n2->value = "";
n2->lid = pos_matcher().GetCounterSuffixWordId();
n2->rid = pos_matcher().GetCounterSuffixWordId();
nodes3.push_back(n2);
nodes4.push_back(n2);

Node *n3 = NewNode();
n3->key = "";
n3->value = "";
nodes3.push_back(n3);
nodes4.push_back(n3);
}

Segment::Candidate *c4 = NewCandidate();
c3->key = "にねんご";
c3->value = "2年後";
c3->content_key = "";
c3->content_value = "2";
c3->cost = 1000;
c3->structure_cost = 50;
c4->key = "にねんご";
c4->value = "2年後";
c4->content_key = "";
c4->content_value = "2";
c4->cost = 1000;
c4->structure_cost = 50;

EXPECT_EQ(filter->FilterCandidate(*request_, c4->key, c4, nodes4, nodes4),
CandidateFilter::GOOD_CANDIDATE);
Expand Down
Loading

0 comments on commit 96f51bf

Please sign in to comment.