Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ common --enable_bzlmod

common --test_output=errors

# Windows settings
startup --windows_enable_symlinks

# Point tools such as coursier (used in rules_jvm_external) to Bazel's downloaded JDK
# suggested in https://github.com/bazelbuild/rules_jvm_external/issues/445
common --repo_env=JAVA_HOME=../bazel_tools/jdk
common --action_env=JAVA_HOME=../bazel_tools/jdk

# Define value used by tests
common --define=SOME_VAR=SOME_VALUE

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

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

# Don't link against libunwind on macos as it causes linking failures (https://github.com/bazel-contrib/toolchains_llvm/pull/346)
common:macos --@toolchains_llvm//toolchain/config:libunwind=False
Expand Down
6 changes: 6 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ load("//uv/private/manifest:defs.bzl", "gazelle_python_manifest")
# gazelle:exclude examples/django
# gazelle:map_kind bzl_library bzl_library @bazel_lib//:bzl_library.bzl

not_windows = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
})

gazelle_python_manifest(
name = "gazelle_python_manifest",
hub = "pypi",
Expand All @@ -19,6 +24,7 @@ gazelle_python_manifest(
gazelle(
name = "gazelle",
gazelle = "@multitool//tools/gazelle",
target_compatible_with = not_windows,
)

compile_pip_requirements(
Expand Down
10 changes: 9 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ bazel_dep(name = "platforms", version = "1.0.0")
bazel_dep(name = "rules_python", version = "1.0.0")
bazel_dep(name = "with_cfg.bzl", version = "0.11.0")

python_stdlib_list = use_extension("@rules_python//python:extensions.bzl", "python_stdlib_list")
use_repo(
python_stdlib_list,
"python_stdlib_list",
)

tools = use_extension("//py:extensions.bzl", "py_tools")
tools.rules_py_tools()
use_repo(tools, "rules_py_tools")
Expand All @@ -30,8 +36,10 @@ use_repo(
toml,
"toml2json_aarch64_linux_gnu",
"toml2json_aarch64_osx_libsystem",
"toml2json_aarch64_windows_msvc",
"toml2json_x86_64_linux_gnu",
"toml2json_x86_64_osx_libsystem",
"toml2json_x86_64_windows_msvc",
)

host = use_extension("//uv/private/host:extension.bzl", "host_platform")
Expand Down Expand Up @@ -409,7 +417,7 @@ register_toolchains("@rust_toolchains//:all")
bazel_dep(name = "xz", version = "5.4.5.bcr.5")
bazel_dep(name = "zstd", version = "1.5.7")
bazel_dep(name = "bzip2", version = "1.0.8.bcr.2")
bazel_dep(name = "rules_rs", version = "0.0.7")
bazel_dep(name = "rules_rs", version = "0.0.13")

crate = use_extension(
"@rules_rs//rs:extensions.bzl",
Expand Down
1 change: 1 addition & 0 deletions bazel/platforms/config/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cpus = ["aarch64", "x86_64"]
os_to_linker = {
"linux": ["unknown"],
"macos": ["unknown"],
"windows": ["unknown"],
}

platforms = [
Expand Down
15 changes: 14 additions & 1 deletion bazel/rust/multi_platform_rust_binaries.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ TARGET_TRIPLES = [
("aarch64_unknown_linux_gnu", "linux_aarch64"),
("x86_64_apple_darwin", "macos_x86_64"),
("aarch64_apple_darwin", "macos_aarch64"),
("x86_64_pc_windows_msvc", "windows_x86_64"),
]

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

for (target_triple, target_platform) in target_triples:
target_naming = name_scheme.get(target_triple, target_triple)

not_windows = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
})
target_compatible_with = not_windows
if target_platform.startswith("windows"):
target_compatible_with = ["@platforms//os:windows"]
transition_build = "{}_{}_build".format(bin, target_naming)
platform_transition_filegroup(
name = transition_build,
srcs = [target],
target_platform = "//bazel/platforms:{}".format(target_platform),
target_compatible_with = target_compatible_with,
tags = ["release"],
)

Expand All @@ -54,13 +62,15 @@ def multi_platform_rust_binaries(name, target, name_scheme = TARGET_NAMING_SCHEM
name = "{}_copy".format(copy_name),
src = transition_build,
out = copy_name,
target_compatible_with = target_compatible_with,
tags = ["release"],
)

bin_sha256 = "{}_bin_hash".format(copy_name)
hashes(
name = bin_sha256,
src = copy_name,
target_compatible_with = target_compatible_with,
tags = ["release"],
)

Expand All @@ -71,6 +81,7 @@ def multi_platform_rust_binaries(name, target, name_scheme = TARGET_NAMING_SCHEM
renames = {copy_name: bin},
attributes = pkg_attributes(mode = "0744"),
strip_prefix = "/",
target_compatible_with = target_compatible_with,
tags = ["release"],
)

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

pkged_sha256 = "{}_pkged_hash".format(copy_name)
hashes(
name = pkged_sha256,
src = pkged,
target_compatible_with = target_compatible_with,
tags = ["release"],
)

Expand Down
3 changes: 3 additions & 0 deletions e2e/cases/repository-rule-deps-299/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ common --enable_bzlmod

# The imports=[".."] only works properly when this matches the python toolchain major version.
common --@aspect_rules_py//py:interpreter_version=3.11.6

# Windows settings
startup --windows_enable_symlinks
7 changes: 7 additions & 0 deletions examples/py_venv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")
load("//py:defs.bzl", "py_image_layer")
load("//py/unstable:defs.bzl", "py_venv", "py_venv_binary")

not_windows = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
})

expand_template(
name = "stamped",
stamp_substitutions = {
Expand Down Expand Up @@ -82,10 +87,12 @@ platform_transition_filegroup(
"@platforms//cpu:arm64": ":aarch64_linux",
"@platforms//cpu:x86_64": ":x86_64_linux",
}),
target_compatible_with = not_windows,
)

oci_load(
name = "load",
image = ":platform_image",
repo_tags = ["py_venv_example:latest"],
target_compatible_with = not_windows,
)
1 change: 1 addition & 0 deletions py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bzl_library(
"//py/private:virtual",
"//py/private/py_venv",
"@aspect_bazel_lib//lib:utils",
"@aspect_bazel_lib//lib:windows_utils",
],
)

Expand Down
23 changes: 18 additions & 5 deletions py/private/py_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

def _py_binary_rule_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
venv_toolchain = ctx.toolchains[VENV_TOOLCHAIN]
py_toolchain = _py_semantics.resolve_toolchain(ctx)

Expand Down Expand Up @@ -69,10 +71,15 @@ def _py_binary_rule_impl(ctx):
attribute_name = "env",
)

executable_launcher = ctx.actions.declare_file(ctx.attr.name)
print(py_toolchain.python)
print(py_toolchain.runfiles_interpreter)
print(py_toolchain.python.path)
print(to_rlocation_path(ctx, py_toolchain.python))

bash_launcher = ctx.actions.declare_file(ctx.attr.name)
ctx.actions.expand_template(
template = ctx.file._run_tmpl,
output = executable_launcher,
output = bash_launcher,
substitutions = {
"{{BASH_RLOCATION_FN}}": BASH_RLOCATION_FUNCTION,
"{{INTERPRETER_FLAGS}}": " ".join(py_toolchain.flags + ctx.attr.interpreter_options),
Expand All @@ -91,6 +98,8 @@ def _py_binary_rule_impl(ctx):
is_executable = True,
)

launcher = create_windows_native_launcher_script(ctx, bash_launcher) if is_windows else bash_launcher

srcs_depset = _py_library.make_srcs_depset(ctx)

runfiles = _py_library.make_merged_runfiles(
Expand All @@ -100,6 +109,8 @@ def _py_binary_rule_impl(ctx):
srcs_depset,
] + virtual_resolution.srcs + virtual_resolution.runfiles,
extra_runfiles = [
launcher,
bash_launcher,
site_packages_pth_file,
],
extra_runfiles_depsets = [
Expand All @@ -116,11 +127,11 @@ def _py_binary_rule_impl(ctx):
return [
DefaultInfo(
files = depset([
executable_launcher,
bash_launcher,
main,
site_packages_pth_file,
site_packages_pth_file
]),
executable = executable_launcher,
executable = launcher,
runfiles = runfiles,
),
PyInfo(
Expand Down Expand Up @@ -194,6 +205,7 @@ A collision can occur when multiple packages providing the same file are install
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
})

_attrs.update(**_py_library.attrs)
Expand Down Expand Up @@ -222,6 +234,7 @@ py_base = struct(
toolchains = [
PY_TOOLCHAIN,
VENV_TOOLCHAIN,
"@bazel_tools//tools/sh:toolchain_type",
],
cfg = python_version_transition,
)
Expand Down
8 changes: 8 additions & 0 deletions py/tests/py_image_layer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")
load("//py:defs.bzl", "py_binary", "py_image_layer")
load("asserts.bzl", "assert_tar_listing")

not_windows = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
})

platform(
name = "aarch64_linux",
constraint_values = [
Expand Down Expand Up @@ -39,6 +44,7 @@ platform_transition_filegroup(
name = "platform_layers",
srcs = [":my_app_layers"],
target_platform = ":x86_64_linux",
target_compatible_with = not_windows,
)

# FIXME(Bazel 8+): This listing needs to be updated/
Expand All @@ -64,6 +70,7 @@ platform_transition_filegroup(
"@platforms//cpu:arm64": ":aarch64_linux",
"@platforms//cpu:x86_64": ":x86_64_linux",
}),
target_compatible_with = not_windows,
)

# To build the image and load it into it into a local runtime:
Expand All @@ -79,6 +86,7 @@ platform_transition_filegroup(
name = "amd64_image",
srcs = [":image"],
target_platform = ":x86_64_linux",
target_compatible_with = not_windows,
)

container_structure_test(
Expand Down
10 changes: 10 additions & 0 deletions py/tests/py_venv_image_layer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ load("//py:defs.bzl", "py_image_layer")
load("//py/tests/py_image_layer:asserts.bzl", "assert_tar_listing")
load("//py/unstable:defs.bzl", "py_venv_binary")

not_windows = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
})

platform(
name = "arm64_linux",
constraint_values = [
Expand Down Expand Up @@ -56,6 +61,7 @@ platform_transition_filegroup(
name = "amd64_layers",
srcs = [":my_app_layers"],
target_platform = ":amd64_linux",
target_compatible_with = not_windows,
)

# FIXME(Bazel 8+): This listing needs to be updated/
Expand All @@ -70,6 +76,7 @@ platform_transition_filegroup(
name = "arm64_layers",
srcs = [":my_app_layers"],
target_platform = ":arm64_linux",
target_compatible_with = not_windows,
)

# FIXME(Bazel 8+): This listing needs to be updated/
Expand All @@ -95,6 +102,7 @@ platform_transition_filegroup(
"@platforms//cpu:arm64": ":arm64_linux",
"@platforms//cpu:x86_64": ":amd64_linux",
}),
target_compatible_with = not_windows,
)

# To build the image and load it into it into a local runtime:
Expand All @@ -110,6 +118,7 @@ platform_transition_filegroup(
name = "amd64_image",
srcs = [":image"],
target_platform = ":amd64_linux",
target_compatible_with = not_windows,
)

container_structure_test(
Expand All @@ -133,6 +142,7 @@ platform_transition_filegroup(
name = "arm64_image",
srcs = [":image"],
target_platform = ":arm64_linux",
target_compatible_with = not_windows,
)

container_structure_test(
Expand Down
1 change: 1 addition & 0 deletions py/tests/virtual/django/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ compile_pip_requirements(
name = "requirements",
requirements_in = "requirements.in",
requirements_txt = "requirements.txt",
requirements_windows = "requirements_windows.txt",
)

# Test fixture: a library with an external dependency
Expand Down
4 changes: 4 additions & 0 deletions py/tests/virtual/django/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ typing-extensions==4.8.0 \
--hash=sha256:8f92fc8806f9a6b641eaa5318da32b44d401efaac0f6678c9bc448ba3605faa0 \
--hash=sha256:df8e4339e9cb77357558cbdbceca33c303714cf861d1eef15e1070055ae8b7ef
# via asgiref
tzdata==2025.2 \
--hash=sha256:1a403fada01ff9221ca8044d701868fa132215d84beb92242d9acd2147f667a8 \
--hash=sha256:b60a638fcc0daffadf82fe0f57e53d06bdec2f36c4df66280ae79bce6bd6f2b9
# via django
Loading