Skip to content

Commit b940437

Browse files
authored
Allow building Mozc with Visual Studio 2026 (#1495)
This makes Mozc build on machines where only Visual Studio 2026 is installed, while keeping Visual Studio 2022 as the default toolchain on machines where VS 2022 is available. GitHub Actions runners stay on VS 2022 for the time being; explicit opt-in to VS 2026 when both versions are present can be added in a subsequent commit. Three coordinated changes make this work: 1. Have vs_util.py call vswhere twice when the first call returns nothing: '[17,18)' first to find VS 2022, then '[18,19)' to find VS 2026. Combined with -latest, the resulting matrix is: * Only VS 2022 installed -> picks VS 2022 (no behavior change). * Only VS 2026 installed -> picks VS 2026. * Both installed -> picks VS 2022 (no behavior change). 2. Pass BAZEL_VC into the installer genrule's command line as --vs_install_dir, derive vcvarsall.bat from it inside build_installer.py, and feed it to vs_util as the path hint. Add --action_env=BAZEL_VC under config:windows_env so Bazel propagates the value into action environments. Without this, build_installer.py would run its own vswhere lookup and could pick a different VS than Bazel did, especially on machines with multiple installs. 3. Pick the CRT redist subfolder dynamically. The subfolder is named after the platform toolset, not the MSVC compiler version: VS 2022 stayed on toolset v143 even after MSVC ticked into the 14.4x range (to avoid breaking existing project files / CI pipelines), so its redist lives under 'Microsoft.VC143.CRT'. VS 2026 jumped the toolset to v145 (matching MSVC 14.50), skipping v144 entirely; its redist lives under 'Microsoft.VC145.CRT'. Read VCTOOLSVERSION (which vcvarsall.bat exports next to VCTOOLSREDISTDIR) and pick the v145 subfolder when the minor component is >=50, otherwise v143. Closes #1487. PiperOrigin-RevId: 913010498
1 parent be0eb82 commit b940437

5 files changed

Lines changed: 68 additions & 36 deletions

File tree

docs/build_mozc_in_windows.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ Building Mozc on Windows requires the following software.
4545
* `.NET 6` or later (for `dotnet` command).
4646
* [Bazelisk](https://github.com/bazelbuild/bazelisk)
4747

48-
> [!NOTE] Bazelisk is a wrapper of [Bazel](https://bazel.build) that allows you
48+
> [!TIP]
49+
> Visual Studio 2026 Community Edition is also supported to build Mozc. When
50+
> both VS 2022 and 2026 are installed, VS 2022 will be used.
51+
52+
> [!NOTE]
53+
> Bazelisk is a wrapper of [Bazel](https://bazel.build) that allows you
4954
> to use a specific version of Bazel.
5055
5156
### Download the repository from GitHub

src/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ build:windows_env --extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows
3939
build:windows_env --extra_toolchains=@local_config_cc//:cc-toolchain-x64_x86_windows-clang-cl
4040
build:windows_env --host_platform=//:host-windows-clang-cl
4141
build:windows_env --repo_env=BAZEL_WIN32_WINNT=/D_WIN32_WINNT=0x0A00
42+
build:windows_env --action_env=BAZEL_VC
4243

4344
## Compiler options
4445
common:linux_env --config=compiler_gcc_like

src/build_tools/vs_util.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def get_vcvarsall(
7777
r' --vcvarsall_path=C:\VS\VC\Auxiliary\Build\vcvarsall.bat'
7878
)
7979

80-
cmd = [
80+
base_cmd = [
8181
str(vswhere_path),
8282
'-products',
8383
'Microsoft.VisualStudio.Product.Enterprise',
@@ -86,41 +86,43 @@ def get_vcvarsall(
8686
'Microsoft.VisualStudio.Product.BuildTools',
8787
'-find',
8888
'VC/Auxiliary/Build/vcvarsall.bat',
89-
'-version',
90-
'[17,18)', # See https://github.com/microsoft/vswhere/wiki/Versions
9189
'-latest',
9290
'-utf8',
93-
]
94-
cmd += [
9591
'-requires',
9692
'Microsoft.VisualStudio.Component.VC.Redist.14.Latest',
9793
]
9894
if arch.endswith('arm64'):
99-
cmd += ['Microsoft.VisualStudio.Component.VC.Tools.ARM64']
100-
101-
process = subprocess.Popen(
102-
cmd,
103-
stdout=subprocess.PIPE,
104-
stderr=subprocess.PIPE,
105-
shell=False,
106-
text=True,
107-
encoding='utf-8',
108-
)
109-
stdout, stderr = process.communicate()
110-
exitcode = process.wait()
111-
if exitcode != 0:
112-
msgs = ['Failed to execute vswhere.exe']
113-
if stdout:
114-
msgs += ['-----stdout-----', stdout]
115-
if stderr:
116-
msgs += ['-----stderr-----', stderr]
117-
raise ChildProcessError('\n'.join(msgs))
118-
119-
lines = stdout.splitlines()
120-
if len(lines) > 0:
121-
vcvarsall = pathlib.Path(lines[0])
122-
if vcvarsall.exists():
123-
return vcvarsall
95+
base_cmd += ['Microsoft.VisualStudio.Component.VC.Tools.ARM64']
96+
97+
# Try Visual Studio 2022 first, then fall back to Visual Studio 2026 when
98+
# VS 2022 is not installed. This keeps VS 2022 as the default on machines
99+
# that have both versions installed.
100+
# See https://github.com/microsoft/vswhere/wiki/Versions
101+
for version_filter in ('[17,18)', '[18,19)'):
102+
cmd = base_cmd + ['-version', version_filter]
103+
process = subprocess.Popen(
104+
cmd,
105+
stdout=subprocess.PIPE,
106+
stderr=subprocess.PIPE,
107+
shell=False,
108+
text=True,
109+
encoding='utf-8',
110+
)
111+
stdout, stderr = process.communicate()
112+
exitcode = process.wait()
113+
if exitcode != 0:
114+
msgs = ['Failed to execute vswhere.exe']
115+
if stdout:
116+
msgs += ['-----stdout-----', stdout]
117+
if stderr:
118+
msgs += ['-----stderr-----', stderr]
119+
raise ChildProcessError('\n'.join(msgs))
120+
121+
lines = stdout.splitlines()
122+
if len(lines) > 0:
123+
vcvarsall = pathlib.Path(lines[0])
124+
if vcvarsall.exists():
125+
return vcvarsall
124126

125127
msg = 'Could not find vcvarsall.bat.'
126128
if arch.endswith('arm64'):

src/win32/installer/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ genrule(
117117
"--wxs_path=$(location " + _WXS_FILE + ")",
118118
"--wix_path=$(location @wix//:wix.exe)",
119119
"--branding=" + BRANDING,
120+
"--vs_install_dir=\"$$BAZEL_VC\"",
120121
]) + select({
121122
":build_arm64_binaries": " " + " ".join([
122123
"--mozc_tip64arm=$(location //win32/tip:mozc_tip64arm)",

src/win32/installer/build_installer.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,30 @@ def run_wix4(args) -> None:
7878
"""
7979
arch = args.arch
8080

81+
vcvarsall_hint = None
82+
if args.vs_install_dir:
83+
vcvarsall_hint = str(
84+
pathlib.Path(args.vs_install_dir).joinpath(
85+
'Auxiliary', 'Build', 'vcvarsall.bat'
86+
)
87+
)
88+
8189
# 'VCTOOLSREDISTDIR' environment variable is the same among x86, x64 and arm64
8290
# architectures, so just using 'x64' should be fine here.
83-
redist_root = pathlib.Path(
84-
vs_util.get_vs_env_vars('x64')['VCTOOLSREDISTDIR']
85-
).resolve()
86-
87-
redist_64bit = redist_root.joinpath(arch).joinpath('Microsoft.VC143.CRT')
91+
vs_env = vs_util.get_vs_env_vars('x64', vcvarsall_hint)
92+
redist_root = pathlib.Path(vs_env['VCTOOLSREDISTDIR']).resolve()
93+
94+
# The CRT redist subfolder is named after the platform toolset, not the
95+
# MSVC compiler version: VS 2022 ships 'Microsoft.VC143.CRT' (toolset v143,
96+
# MSVC 14.3x/14.4x) while VS 2026 ships 'Microsoft.VC145.CRT' (toolset
97+
# v145, MSVC 14.50+). v144 was skipped. Pick the subfolder by reading the
98+
# MSVC compiler minor version that vcvarsall.bat exports as
99+
# 'VCTOOLSVERSION'.
100+
vc_tools_minor = int(vs_env['VCTOOLSVERSION'].split('.')[1])
101+
crt_subdir = (
102+
'Microsoft.VC145.CRT' if vc_tools_minor >= 50 else 'Microsoft.VC143.CRT'
103+
)
104+
redist_64bit = redist_root.joinpath(arch).joinpath(crt_subdir)
88105
version_file = pathlib.Path(args.version_file).resolve()
89106
version = mozc_version.MozcVersion(version_file)
90107
credit_file = pathlib.Path(args.credit_file).resolve()
@@ -194,6 +211,12 @@ def main():
194211
default=False,
195212
action='store_true',
196213
)
214+
parser.add_argument(
215+
'--vs_install_dir',
216+
type=str,
217+
default='',
218+
help='Path to the Visual Studio VC directory (same as BAZEL_VC).',
219+
)
197220

198221
args = parser.parse_args()
199222

0 commit comments

Comments
 (0)