@@ -20,160 +20,78 @@ without the overhead of a bazel-in-bazel integration test.
2020load ("@rules_shell//shell:sh_test.bzl" , "sh_test" )
2121load ("//python:py_binary.bzl" , "py_binary" )
2222load ("//python:py_test.bzl" , "py_test" )
23+ load ("//python/private:py_binary_macro.bzl" , "py_binary_macro" )
24+ load ("//python/private:py_binary_rule.bzl" , "create_binary_rule" )
25+ load ("//python/private:py_executable.bzl" , create_executable_transition = "create_transition" )
26+ load ("//python/private:py_test_macro.bzl" , "py_test_macro" )
27+ load ("//python/private:py_test_rule.bzl" , "create_test_rule" )
2328load ("//python/private:toolchain_types.bzl" , "TARGET_TOOLCHAIN_TYPE" ) # buildifier: disable=bzl-visibility
2429load ("//tests/support:support.bzl" , "VISIBLE_FOR_TESTING" )
2530
26- def _perform_transition_impl (input_settings , attr ):
27- settings = dict (input_settings )
31+ def _perform_transition_impl (input_settings , attr , base_impl ):
32+ settings = base_impl ( input_settings , attr ) | dict (input_settings )
2833 settings [VISIBLE_FOR_TESTING ] = True
2934 settings ["//command_line_option:build_python_zip" ] = attr .build_python_zip
3035 if attr .bootstrap_impl :
3136 settings ["//python/config_settings:bootstrap_impl" ] = attr .bootstrap_impl
3237 if attr .extra_toolchains :
3338 settings ["//command_line_option:extra_toolchains" ] = attr .extra_toolchains
34- if attr .python_version :
35- settings ["//python/config_settings:python_version" ] = attr .python_version
3639 if attr .venvs_use_declare_symlink :
3740 settings ["//python/config_settings:venvs_use_declare_symlink" ] = attr .venvs_use_declare_symlink
3841 return settings
3942
40- _perform_transition = transition (
41- implementation = _perform_transition_impl ,
43+ _perform_transition = create_executable_transition (
44+ extend_implementation = _perform_transition_impl ,
4245 inputs = [
4346 "//python/config_settings:bootstrap_impl" ,
4447 "//command_line_option:extra_toolchains" ,
45- "//python/config_settings:python_version" ,
4648 "//python/config_settings:venvs_use_declare_symlink" ,
4749 ],
4850 outputs = [
4951 "//command_line_option:build_python_zip" ,
5052 "//command_line_option:extra_toolchains" ,
5153 "//python/config_settings:bootstrap_impl" ,
52- "//python/config_settings:python_version" ,
5354 "//python/config_settings:venvs_use_declare_symlink" ,
5455 VISIBLE_FOR_TESTING ,
5556 ],
5657)
5758
58- def _py_reconfig_impl (ctx ):
59- default_info = ctx .attr .target [DefaultInfo ]
60- exe_ext = default_info .files_to_run .executable .extension
61- if exe_ext :
62- exe_ext = "." + exe_ext
63- exe_name = ctx .label .name + exe_ext
64-
65- executable = ctx .actions .declare_file (exe_name )
66- ctx .actions .symlink (output = executable , target_file = default_info .files_to_run .executable )
67-
68- default_outputs = [executable ]
69-
70- # todo: could probably check target.owner vs src.owner to check if it should
71- # be symlinked or included as-is
72- # For simplicity of implementation, we're assuming the target being run is
73- # py_binary-like. In order for Windows to work, we need to make sure the
74- # file that the .exe launcher runs (the .zip or underlying non-exe
75- # executable) is a sibling of the .exe file with the same base name.
76- for src in default_info .files .to_list ():
77- if src .extension in ("" , "zip" ):
78- ext = ("." if src .extension else "" ) + src .extension
79- output = ctx .actions .declare_file (ctx .label .name + ext )
80- ctx .actions .symlink (output = output , target_file = src )
81- default_outputs .append (output )
82-
83- return [
84- DefaultInfo (
85- executable = executable ,
86- files = depset (default_outputs ),
87- # On windows, the other default outputs must also be included
88- # in runfiles so the exe launcher can find the backing file.
89- runfiles = ctx .runfiles (default_outputs ).merge (
90- default_info .default_runfiles ,
91- ),
92- ),
93- ctx .attr .target [OutputGroupInfo ],
94- # Inherit the expanded environment from the inner target.
95- ctx .attr .target [RunEnvironmentInfo ],
96- ]
97-
98- def _make_reconfig_rule (** kwargs ):
99- attrs = {
100- "bootstrap_impl" : attr .string (),
101- "build_python_zip" : attr .string (default = "auto" ),
102- "extra_toolchains" : attr .string_list (
103- doc = """
59+ _RECONFIG_ATTRS = {
60+ "bootstrap_impl" : attr .string (),
61+ "build_python_zip" : attr .string (default = "auto" ),
62+ "extra_toolchains" : attr .string_list (
63+ doc = """
10464Value for the --extra_toolchains flag.
10565
10666NOTE: You'll likely have to also specify //tests/support/cc_toolchains:all (or some CC toolchain)
10767to make the RBE presubmits happy, which disable auto-detection of a CC
10868toolchain.
10969""" ,
110- ),
111- "python_version" : attr .string (),
112- "target" : attr .label (executable = True , cfg = "target" ),
113- "venvs_use_declare_symlink" : attr .string (),
114- "_allowlist_function_transition" : attr .label (
115- default = "@bazel_tools//tools/allowlists/function_transition_allowlist" ,
116- ),
117- }
118- return rule (
119- implementation = _py_reconfig_impl ,
120- attrs = attrs ,
121- cfg = _perform_transition ,
122- ** kwargs
123- )
70+ ),
71+ "venvs_use_declare_symlink" : attr .string (),
72+ }
12473
125- _py_reconfig_binary = _make_reconfig_rule (executable = True )
126-
127- _py_reconfig_test = _make_reconfig_rule (test = True )
128-
129- def _py_reconfig_executable (* , name , py_reconfig_rule , py_inner_rule , ** kwargs ):
130- reconfig_only_kwarg_names = [
131- # keep sorted
132- "bootstrap_impl" ,
133- "build_python_zip" ,
134- "extra_toolchains" ,
135- "python_version" ,
136- "venvs_use_declare_symlink" ,
137- ]
138- reconfig_kwargs = {
139- key : kwargs .pop (key , None )
140- for key in reconfig_only_kwarg_names
141- }
142- reconfig_kwargs ["target_compatible_with" ] = kwargs .get ("target_compatible_with" )
143-
144- inner_name = "_{}_inner" .format (name )
145- py_reconfig_rule (
146- name = name ,
147- target = inner_name ,
148- ** reconfig_kwargs
149- )
150- py_inner_rule (
151- name = inner_name ,
152- tags = ["manual" ],
153- ** kwargs
154- )
74+ _py_reconfig_binary = create_binary_rule (
75+ attrs = _RECONFIG_ATTRS ,
76+ cfg = _perform_transition ,
77+ )
78+
79+ _py_reconfig_test = create_test_rule (
80+ attrs = _RECONFIG_ATTRS ,
81+ cfg = _perform_transition ,
82+ )
15583
156- def py_reconfig_test (* , name , * *kwargs ):
84+ def py_reconfig_test (** kwargs ):
15785 """Create a py_test with customized build settings for testing.
15886
15987 Args:
160- name: str, name of teset target.
161- **kwargs: kwargs to pass along to _py_reconfig_test and py_test .
88+ name: str, name of test target.
89+ **kwargs: kwargs to pass along to _py_reconfig_test.
16290 """
163- _py_reconfig_executable (
164- name = name ,
165- py_reconfig_rule = _py_reconfig_test ,
166- py_inner_rule = py_test ,
167- ** kwargs
168- )
91+ py_test_macro (_py_reconfig_test , ** kwargs )
16992
170- def py_reconfig_binary (* , name , ** kwargs ):
171- _py_reconfig_executable (
172- name = name ,
173- py_reconfig_rule = _py_reconfig_binary ,
174- py_inner_rule = py_binary ,
175- ** kwargs
176- )
93+ def py_reconfig_binary (** kwargs ):
94+ py_binary_macro (_py_reconfig_binary , ** kwargs )
17795
17896def sh_py_run_test (* , name , sh_src , py_src , ** kwargs ):
17997 """Run a py_binary within a sh_test.
@@ -196,26 +114,12 @@ def sh_py_run_test(*, name, sh_src, py_src, **kwargs):
196114 "BIN_RLOCATION" : "$(rlocationpaths {})" .format (bin_name ),
197115 },
198116 )
199-
200- py_binary_kwargs = {
201- key : kwargs .pop (key )
202- for key in ("imports" , "deps" , "env" )
203- if key in kwargs
204- }
205-
206- _py_reconfig_binary (
117+ py_reconfig_binary (
207118 name = bin_name ,
208- tags = ["manual" ],
209- target = "_{}_plain_bin" .format (name ),
210- ** kwargs
211- )
212-
213- py_binary (
214- name = "_{}_plain_bin" .format (name ),
215119 srcs = [py_src ],
216120 main = py_src ,
217121 tags = ["manual" ],
218- ** py_binary_kwargs
122+ ** kwargs
219123 )
220124
221125def _current_build_settings_impl (ctx ):
0 commit comments