Skip to content

Use the launcher_maker toolchain if available #2919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_features", version = "1.21.0")
bazel_dep(name = "bazel_features", version = "1.30.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "rules_cc", version = "0.0.16")
bazel_dep(name = "platforms", version = "0.0.11")
Expand Down
6 changes: 3 additions & 3 deletions internal_dev_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ def rules_python_internal_deps():

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
sha256 = "a660027f5a87f13224ab54b8dc6e191693c554f2692fcca46e8e29ee7dabc43b",
strip_prefix = "bazel_features-1.30.0",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.30.0/bazel_features-v1.30.0.tar.gz",
)

http_archive(
Expand Down
23 changes: 16 additions & 7 deletions python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Common functionality between test/binary executables."""

load("@bazel_features//:features.bzl", "bazel_features")
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:structs.bzl", "structs")
Expand Down Expand Up @@ -70,6 +71,7 @@ _py_builtins = py_internal
_EXTERNAL_PATH_PREFIX = "external"
_ZIP_RUNFILES_DIRECTORY_NAME = "runfiles"
_PYTHON_VERSION_FLAG = str(Label("//python/config_settings:python_version"))
_LAUNCHER_MAKER_TOOLCHAIN_TYPE = "@bazel_tools//tools/launcher:launcher_maker_toolchain_type"

# Non-Google-specific attributes for executables
# These attributes are for rules that accept Python sources.
Expand Down Expand Up @@ -228,17 +230,19 @@ accepting arbitrary Python versions.
"@platforms//os:windows",
],
),
"_windows_launcher_maker": lambda: attrb.Label(
default = "@bazel_tools//tools/launcher:launcher_maker",
cfg = "exec",
executable = True,
),
"_zipper": lambda: attrb.Label(
cfg = "exec",
executable = True,
default = "@bazel_tools//tools/zip:zipper",
),
},
{
"_windows_launcher_maker": lambda: attrb.Label(
default = "@bazel_tools//tools/launcher:launcher_maker",
cfg = "exec",
executable = True,
),
} if not bazel_features.rules._has_launcher_maker_toolchain else {},
)

def convert_legacy_create_init_to_int(kwargs):
Expand Down Expand Up @@ -842,6 +846,11 @@ def _create_stage1_bootstrap(
substitutions = subs,
)

def _find_launcher_maker(ctx):
if bazel_features.rules._has_launcher_maker_toolchain:
return ctx.toolchains[_LAUNCHER_MAKER_TOOLCHAIN_TYPE].binary
return ctx.executable._windows_launcher_maker

def _create_windows_exe_launcher(
ctx,
*,
Expand All @@ -862,7 +871,7 @@ def _create_windows_exe_launcher(

launcher = ctx.attr._launcher[DefaultInfo].files_to_run.executable
ctx.actions.run(
executable = ctx.executable._windows_launcher_maker,
executable = _find_launcher_maker(ctx),
arguments = [launcher.path, launch_info, output.path],
inputs = [launcher],
outputs = [output],
Expand Down Expand Up @@ -1910,7 +1919,7 @@ def create_executable_rule_builder(implementation, **kwargs):
ruleb.ToolchainType(TOOLCHAIN_TYPE),
ruleb.ToolchainType(EXEC_TOOLS_TOOLCHAIN_TYPE, mandatory = False),
ruleb.ToolchainType("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
],
] + ([ruleb.ToolchainType(_LAUNCHER_MAKER_TOOLCHAIN_TYPE)] if bazel_features.rules._has_launcher_maker_toolchain else []),
cfg = dict(
implementation = _transition_executable_impl,
inputs = [_PYTHON_VERSION_FLAG],
Expand Down
24 changes: 23 additions & 1 deletion tests/base_rules/py_executable_base_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util")
load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject")
load("//tests/support:support.bzl", "BOOTSTRAP_IMPL", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "WINDOWS_X86_64")
load("//tests/support:support.bzl", "BOOTSTRAP_IMPL", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "EXOTIC_UNIX", "LINUX_X86_64", "WINDOWS_X86_64")

_tests = []

Expand Down Expand Up @@ -115,6 +115,28 @@ def _test_basic_zip_impl(env, target):

_tests.append(_test_basic_zip)

def _test_cross_compile_to_unix(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
main_module = "dummy",
)
analysis_test(
name = name,
impl = _test_cross_compile_to_unix_impl,
target = name + "_subject",
config_settings = {
"//command_line_option:host_platform": EXOTIC_UNIX,
"//command_line_option:platforms": [EXOTIC_UNIX],
},
expect_failure = True,
)

def _test_cross_compile_to_unix_impl(_env, _target):
pass

_tests.append(_test_cross_compile_to_unix)

def _test_executable_in_runfiles(name, config):
rt_util.helper_target(
config.rule,
Expand Down
8 changes: 8 additions & 0 deletions tests/support/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ platform(
],
)

platform(
name = "exotic_unix",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:s390x",
],
)

current_build_settings(
name = "current_build_settings",
)
4 changes: 4 additions & 0 deletions tests/support/support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ LINUX_X86_64 = Label("//tests/support:linux_x86_64")
WINDOWS = Label("//tests/support:windows")
WINDOWS_X86_64 = Label("//tests/support:windows_x86_64")

# Unspecified Unix platform that are unlikely to be the host platform in CI,
# but still provide a Python toolchain.
EXOTIC_UNIX = Label("//tests/support:exotic_unix")

PY_TOOLCHAINS = str(Label("//tests/support/py_toolchains:all"))
CC_TOOLCHAIN = str(Label("//tests/support/cc_toolchains:all"))
CROSSTOOL_TOP = Label("//tests/support/cc_toolchains:cc_toolchain_suite")
Expand Down