22
33load ("@aspect_bazel_lib//lib:expand_make_vars.bzl" , "expand_locations" , "expand_variables" )
44load ("@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" )
56load ("@rules_python//python:defs.bzl" , "PyInfo" )
67load ("//py/private:py_library.bzl" , _py_library = "py_library_utils" )
78load ("//py/private:py_semantics.bzl" , _py_semantics = "semantics" )
@@ -15,6 +16,7 @@ def _dict_to_exports(env):
1516 ]
1617
1718def _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)
0 commit comments