Skip to content

Commit 5ed87be

Browse files
committed
windows fixes
1 parent f0667bd commit 5ed87be

File tree

20 files changed

+906
-647
lines changed

20 files changed

+906
-647
lines changed

.bazelrc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ common --enable_bzlmod
44

55
common --test_output=errors
66

7+
# Windows settings
8+
startup --windows_enable_symlinks
9+
10+
# Point tools such as coursier (used in rules_jvm_external) to Bazel's downloaded JDK
11+
# suggested in https://github.com/bazelbuild/rules_jvm_external/issues/445
12+
common --repo_env=JAVA_HOME=../bazel_tools/jdk
13+
common --action_env=JAVA_HOME=../bazel_tools/jdk
14+
715
# Define value used by tests
816
common --define=SOME_VAR=SOME_VALUE
917

@@ -17,7 +25,9 @@ common --per_file_copt=external/.*protobuf.*@--PROTOBUF_WAS_NOT_SUPPOSED_TO_BE_B
1725
common --host_per_file_copt=external/.*protobuf.*@--PROTOBUF_WAS_NOT_SUPPOSED_TO_BE_BUILT
1826

1927
# Don't try and auto detect the cc toolchain, as we use our own gcc toolchains.
20-
common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
28+
# Except on windows, where we use msvc
29+
common:linux --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
30+
common:macos --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
2131

2232
# Don't link against libunwind on macos as it causes linking failures (https://github.com/bazel-contrib/toolchains_llvm/pull/346)
2333
common:macos --@toolchains_llvm//toolchain/config:libunwind=False

MODULE.bazel

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ bazel_dep(name = "platforms", version = "1.0.0")
1313
bazel_dep(name = "rules_python", version = "1.0.0")
1414
bazel_dep(name = "with_cfg.bzl", version = "0.11.0")
1515

16+
python_stdlib_list = use_extension("@rules_python//python:extensions.bzl", "python_stdlib_list")
17+
use_repo(
18+
python_stdlib_list,
19+
"python_stdlib_list",
20+
)
21+
1622
tools = use_extension("//py:extensions.bzl", "py_tools")
1723
tools.rules_py_tools()
1824
use_repo(tools, "rules_py_tools")
@@ -30,8 +36,10 @@ use_repo(
3036
toml,
3137
"toml2json_aarch64_linux_gnu",
3238
"toml2json_aarch64_osx_libsystem",
39+
"toml2json_aarch64_windows_msvc",
3340
"toml2json_x86_64_linux_gnu",
3441
"toml2json_x86_64_osx_libsystem",
42+
"toml2json_x86_64_windows_msvc",
3543
)
3644

3745
host = use_extension("//uv/private/host:extension.bzl", "host_platform")
@@ -409,7 +417,7 @@ register_toolchains("@rust_toolchains//:all")
409417
bazel_dep(name = "xz", version = "5.4.5.bcr.5")
410418
bazel_dep(name = "zstd", version = "1.5.7")
411419
bazel_dep(name = "bzip2", version = "1.0.8.bcr.2")
412-
bazel_dep(name = "rules_rs", version = "0.0.7")
420+
bazel_dep(name = "rules_rs", version = "0.0.13")
413421

414422
crate = use_extension(
415423
"@rules_rs//rs:extensions.bzl",

bazel/platforms/config/defs.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cpus = ["aarch64", "x86_64"]
99
os_to_linker = {
1010
"linux": ["unknown"],
1111
"macos": ["unknown"],
12+
"windows": ["unknown"],
1213
}
1314

1415
platforms = [

bazel/rust/multi_platform_rust_binaries.bzl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ TARGET_TRIPLES = [
1111
("aarch64_unknown_linux_gnu", "linux_aarch64"),
1212
("x86_64_apple_darwin", "macos_x86_64"),
1313
("aarch64_apple_darwin", "macos_aarch64"),
14+
("x86_64_pc_windows_msvc", "windows_x86_64"),
1415
]
1516

1617
# Map a Rust naming scheme to a custom name.
@@ -40,12 +41,19 @@ def multi_platform_rust_binaries(name, target, name_scheme = TARGET_NAMING_SCHEM
4041

4142
for (target_triple, target_platform) in target_triples:
4243
target_naming = name_scheme.get(target_triple, target_triple)
43-
44+
not_windows = select({
45+
"@platforms//os:windows": ["@platforms//:incompatible"],
46+
"//conditions:default": [],
47+
})
48+
target_compatible_with = not_windows
49+
if target_platform.startswith("windows"):
50+
target_compatible_with = ["@platforms//os:windows"]
4451
transition_build = "{}_{}_build".format(bin, target_naming)
4552
platform_transition_filegroup(
4653
name = transition_build,
4754
srcs = [target],
4855
target_platform = "//bazel/platforms:{}".format(target_platform),
56+
target_compatible_with = target_compatible_with,
4957
tags = ["release"],
5058
)
5159

@@ -54,13 +62,15 @@ def multi_platform_rust_binaries(name, target, name_scheme = TARGET_NAMING_SCHEM
5462
name = "{}_copy".format(copy_name),
5563
src = transition_build,
5664
out = copy_name,
65+
target_compatible_with = target_compatible_with,
5766
tags = ["release"],
5867
)
5968

6069
bin_sha256 = "{}_bin_hash".format(copy_name)
6170
hashes(
6271
name = bin_sha256,
6372
src = copy_name,
73+
target_compatible_with = target_compatible_with,
6474
tags = ["release"],
6575
)
6676

@@ -71,6 +81,7 @@ def multi_platform_rust_binaries(name, target, name_scheme = TARGET_NAMING_SCHEM
7181
renames = {copy_name: bin},
7282
attributes = pkg_attributes(mode = "0744"),
7383
strip_prefix = "/",
84+
target_compatible_with = target_compatible_with,
7485
tags = ["release"],
7586
)
7687

@@ -84,13 +95,15 @@ def multi_platform_rust_binaries(name, target, name_scheme = TARGET_NAMING_SCHEM
8495
# Why is -1 not the default :/
8596
# This also sets the modified time in UTC.
8697
stamp = -1,
98+
target_compatible_with = target_compatible_with,
8799
tags = ["release"],
88100
)
89101

90102
pkged_sha256 = "{}_pkged_hash".format(copy_name)
91103
hashes(
92104
name = pkged_sha256,
93105
src = pkged,
106+
target_compatible_with = target_compatible_with,
94107
tags = ["release"],
95108
)
96109

e2e/cases/repository-rule-deps-299/.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ common --enable_bzlmod
55

66
# The imports=[".."] only works properly when this matches the python toolchain major version.
77
common --@aspect_rules_py//py:interpreter_version=3.11.6
8+
9+
# Windows settings
10+
startup --windows_enable_symlinks

py/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ bzl_library(
3636
"//py/private:virtual",
3737
"//py/private/py_venv",
3838
"@aspect_bazel_lib//lib:utils",
39+
"@aspect_bazel_lib//lib:windows_utils",
3940
],
4041
)
4142

py/private/py_binary.bzl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables")
44
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")
5+
load("@aspect_bazel_lib//lib:windows_utils.bzl", "create_windows_native_launcher_script")
56
load("@rules_python//python:defs.bzl", "PyInfo")
67
load("//py/private:py_library.bzl", _py_library = "py_library_utils")
78
load("//py/private:py_semantics.bzl", _py_semantics = "semantics")
@@ -15,6 +16,7 @@ def _dict_to_exports(env):
1516
]
1617

1718
def _py_binary_rule_impl(ctx):
19+
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
1820
venv_toolchain = ctx.toolchains[VENV_TOOLCHAIN]
1921
py_toolchain = _py_semantics.resolve_toolchain(ctx)
2022

@@ -69,10 +71,15 @@ def _py_binary_rule_impl(ctx):
6971
attribute_name = "env",
7072
)
7173

72-
executable_launcher = ctx.actions.declare_file(ctx.attr.name)
74+
print(py_toolchain.python)
75+
print(py_toolchain.runfiles_interpreter)
76+
print(py_toolchain.python.path)
77+
print(to_rlocation_path(ctx, py_toolchain.python))
78+
79+
bash_launcher = ctx.actions.declare_file(ctx.attr.name)
7380
ctx.actions.expand_template(
7481
template = ctx.file._run_tmpl,
75-
output = executable_launcher,
82+
output = bash_launcher,
7683
substitutions = {
7784
"{{BASH_RLOCATION_FN}}": BASH_RLOCATION_FUNCTION,
7885
"{{INTERPRETER_FLAGS}}": " ".join(py_toolchain.flags + ctx.attr.interpreter_options),
@@ -91,6 +98,8 @@ def _py_binary_rule_impl(ctx):
9198
is_executable = True,
9299
)
93100

101+
launcher = create_windows_native_launcher_script(ctx, bash_launcher) if is_windows else bash_launcher
102+
94103
srcs_depset = _py_library.make_srcs_depset(ctx)
95104

96105
runfiles = _py_library.make_merged_runfiles(
@@ -100,6 +109,8 @@ def _py_binary_rule_impl(ctx):
100109
srcs_depset,
101110
] + virtual_resolution.srcs + virtual_resolution.runfiles,
102111
extra_runfiles = [
112+
launcher,
113+
bash_launcher,
103114
site_packages_pth_file,
104115
],
105116
extra_runfiles_depsets = [
@@ -116,11 +127,11 @@ def _py_binary_rule_impl(ctx):
116127
return [
117128
DefaultInfo(
118129
files = depset([
119-
executable_launcher,
130+
bash_launcher,
120131
main,
121-
site_packages_pth_file,
132+
site_packages_pth_file
122133
]),
123-
executable = executable_launcher,
134+
executable = launcher,
124135
runfiles = runfiles,
125136
),
126137
PyInfo(
@@ -194,6 +205,7 @@ A collision can occur when multiple packages providing the same file are install
194205
"_allowlist_function_transition": attr.label(
195206
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
196207
),
208+
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
197209
})
198210

199211
_attrs.update(**_py_library.attrs)
@@ -222,6 +234,7 @@ py_base = struct(
222234
toolchains = [
223235
PY_TOOLCHAIN,
224236
VENV_TOOLCHAIN,
237+
"@bazel_tools//tools/sh:toolchain_type",
225238
],
226239
cfg = python_version_transition,
227240
)

py/tools/py/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ homepage.workspace = true
66
repository.workspace = true
77
license.workspace = true
88
edition.workspace = true
9-
readme.workspace = true
109
rust-version.workspace = true
1110

1211
[features]
@@ -30,3 +29,6 @@ uv-pypi-types = { workspace = true }
3029
uv-python = { workspace = true }
3130
uv-virtualenv = { workspace = true }
3231
walkdir = "2.5.0"
32+
# workaround issue with this package building with rules_rust on windows; the snap files used in their unit tests have
33+
# too long a path and import of this module is failing (even when tests are not run)
34+
rattler_installs_packages = { git = "https://github.com/peakschris/rip", rev = "3de9ee95eab577c9e48526464013e217287c6937", default-features = false, features = ["rustls-tls"] }

py/tools/py/src/pth.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,7 @@ fn create_symlink(
256256
};
257257
}
258258

259-
std::os::unix::fs::symlink(&tgt, &link)
260-
.into_diagnostic()
261-
.wrap_err(format!(
262-
"Unable to create symlink: {} -> {}",
263-
tgt.to_string_lossy(),
264-
link.to_string_lossy()
265-
))?;
259+
create_cross_platform_symlink(&tgt, &link)?;
266260

267261
Ok(())
268262
}
@@ -291,3 +285,37 @@ fn is_same_file(p1: &Path, p2: &Path) -> miette::Result<bool> {
291285

292286
return Ok(true);
293287
}
288+
289+
fn create_cross_platform_symlink(tgt: &Path, link: &Path) -> miette::Result<()> {
290+
#[cfg(unix)]
291+
{
292+
std::os::unix::fs::symlink(tgt, link)
293+
.into_diagnostic()
294+
.wrap_err(format!(
295+
"Unable to create symlink: {} -> {}",
296+
tgt.to_string_lossy(),
297+
link.to_string_lossy()
298+
))
299+
}
300+
301+
#[cfg(windows)]
302+
{
303+
if tgt.is_dir() {
304+
std::os::windows::fs::symlink_dir(tgt, link)
305+
.into_diagnostic()
306+
.wrap_err(format!(
307+
"Unable to create directory symlink: {} -> {}",
308+
tgt.to_string_lossy(),
309+
link.to_string_lossy()
310+
))
311+
} else {
312+
std::os::windows::fs::symlink_file(tgt, link)
313+
.into_diagnostic()
314+
.wrap_err(format!(
315+
"Unable to create file symlink: {} -> {}",
316+
tgt.to_string_lossy(),
317+
link.to_string_lossy()
318+
))
319+
}
320+
}
321+
}

py/tools/unpack_bin/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ release(
3131
name = "release",
3232
targets = [":bins"],
3333
)
34+
35+
# multi_arch_rust_binary_release(
36+
# name = "windows",
37+
# src = ":unpack",
38+
# os = "windows",
39+
# visibility = ["//tools/release:__pkg__"],
40+
# )

0 commit comments

Comments
 (0)