Skip to content

Commit 4366227

Browse files
committed
Use the launcher_maker toolchain if available
1 parent 846dfd0 commit 4366227

6 files changed

Lines changed: 57 additions & 11 deletions

File tree

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module(
44
compatibility_level = 1,
55
)
66

7-
bazel_dep(name = "bazel_features", version = "1.21.0")
7+
bazel_dep(name = "bazel_features", version = "1.30.0")
88
bazel_dep(name = "bazel_skylib", version = "1.8.2")
99
bazel_dep(name = "rules_cc", version = "0.1.5")
1010
bazel_dep(name = "platforms", version = "0.0.11")

internal_dev_deps.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ def rules_python_internal_deps():
231231

232232
http_archive(
233233
name = "bazel_features",
234-
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
235-
strip_prefix = "bazel_features-1.9.1",
236-
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
234+
sha256 = "a660027f5a87f13224ab54b8dc6e191693c554f2692fcca46e8e29ee7dabc43b",
235+
strip_prefix = "bazel_features-1.30.0",
236+
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.30.0/bazel_features-v1.30.0.tar.gz",
237237
)
238238

239239
http_archive(

python/private/py_executable.bzl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Common functionality between test/binary executables."""
1515

16+
load("@bazel_features//:features.bzl", "bazel_features")
1617
load("@bazel_skylib//lib:dicts.bzl", "dicts")
1718
load("@bazel_skylib//lib:paths.bzl", "paths")
1819
load("@bazel_skylib//lib:structs.bzl", "structs")
@@ -69,6 +70,7 @@ load(":venv_runfiles.bzl", "create_venv_app_files")
6970
_py_builtins = py_internal
7071
_EXTERNAL_PATH_PREFIX = "external"
7172
_ZIP_RUNFILES_DIRECTORY_NAME = "runfiles"
73+
_LAUNCHER_MAKER_TOOLCHAIN_TYPE = "@bazel_tools//tools/launcher:launcher_maker_toolchain_type"
7274

7375
# Non-Google-specific attributes for executables
7476
# These attributes are for rules that accept Python sources.
@@ -228,17 +230,19 @@ accepting arbitrary Python versions.
228230
"@platforms//os:windows",
229231
],
230232
),
231-
"_windows_launcher_maker": lambda: attrb.Label(
232-
default = "@bazel_tools//tools/launcher:launcher_maker",
233-
cfg = "exec",
234-
executable = True,
235-
),
236233
"_zipper": lambda: attrb.Label(
237234
cfg = "exec",
238235
executable = True,
239236
default = "@bazel_tools//tools/zip:zipper",
240237
),
241238
},
239+
{
240+
"_windows_launcher_maker": lambda: attrb.Label(
241+
default = "@bazel_tools//tools/launcher:launcher_maker",
242+
cfg = "exec",
243+
executable = True,
244+
),
245+
} if not bazel_features.rules._has_launcher_maker_toolchain else {},
242246
)
243247

244248
def convert_legacy_create_init_to_int(kwargs):
@@ -777,6 +781,11 @@ def _create_stage1_bootstrap(
777781
is_executable = True,
778782
)
779783

784+
def _find_launcher_maker(ctx):
785+
if bazel_features.rules._has_launcher_maker_toolchain:
786+
return ctx.toolchains[_LAUNCHER_MAKER_TOOLCHAIN_TYPE].binary
787+
return ctx.executable._windows_launcher_maker
788+
780789
def _create_windows_exe_launcher(
781790
ctx,
782791
*,
@@ -797,7 +806,7 @@ def _create_windows_exe_launcher(
797806

798807
launcher = ctx.attr._launcher[DefaultInfo].files_to_run.executable
799808
ctx.actions.run(
800-
executable = ctx.executable._windows_launcher_maker,
809+
executable = _find_launcher_maker(ctx),
801810
arguments = [launcher.path, launch_info, output.path],
802811
inputs = [launcher],
803812
outputs = [output],
@@ -1838,7 +1847,7 @@ def create_executable_rule_builder(implementation, **kwargs):
18381847
ruleb.ToolchainType(TOOLCHAIN_TYPE),
18391848
ruleb.ToolchainType(EXEC_TOOLS_TOOLCHAIN_TYPE, mandatory = False),
18401849
ruleb.ToolchainType("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
1841-
],
1850+
] + ([ruleb.ToolchainType(_LAUNCHER_MAKER_TOOLCHAIN_TYPE)] if bazel_features.rules._has_launcher_maker_toolchain else []),
18421851
cfg = dict(
18431852
implementation = _transition_executable_impl,
18441853
inputs = TRANSITION_LABELS + [labels.PYTHON_VERSION],

tests/base_rules/py_executable_base_tests.bzl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,31 @@ def _test_basic_zip_impl(env, target):
114114

115115
_tests.append(_test_basic_zip)
116116

117+
def _test_cross_compile_to_unix(name, config):
118+
if "py_test" in str(config.rule):
119+
# The default test toolchain prevents cross-compiling py_test to
120+
# a non-exec platform.
121+
return
122+
rt_util.helper_target(
123+
config.rule,
124+
name = name + "_subject",
125+
main_module = "dummy",
126+
)
127+
analysis_test(
128+
name = name,
129+
impl = _test_cross_compile_to_unix_impl,
130+
target = name + "_subject",
131+
config_settings = {
132+
"//command_line_option:platforms": [platform_targets.EXOTIC_UNIX],
133+
},
134+
expect_failure = True,
135+
)
136+
137+
def _test_cross_compile_to_unix_impl(_env, _target):
138+
pass
139+
140+
_tests.append(_test_cross_compile_to_unix)
141+
117142
def _test_executable_in_runfiles(name, config):
118143
rt_util.helper_target(
119144
config.rule,

tests/support/platforms/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,11 @@ platform(
7575
"@platforms//cpu:aarch64",
7676
],
7777
)
78+
79+
platform(
80+
name = "exotic_unix",
81+
constraint_values = [
82+
"@platforms//os:linux",
83+
"@platforms//cpu:s390x",
84+
],
85+
)

tests/support/platforms/platforms.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ platform_targets = struct(
1010
WINDOWS = Label("//tests/support/platforms:windows"),
1111
WINDOWS_AARCH64 = Label("//tests/support/platforms:windows_aarch64"),
1212
WINDOWS_X86_64 = Label("//tests/support/platforms:windows_x86_64"),
13+
14+
# Unspecified Unix platform that is unlikely to be the host platform in CI,
15+
# but still provides a Python toolchain.
16+
EXOTIC_UNIX = Label("//tests/support/platforms:exotic_unix"),
1317
)

0 commit comments

Comments
 (0)