|
| 1 | +# Copyright 2024 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Run a py_binary/py_test with altered config settings. |
| 15 | +
|
| 16 | +This facilitates verify running binaries with different configuration settings |
| 17 | +without the overhead of a bazel-in-bazel integration test. |
| 18 | +""" |
| 19 | + |
| 20 | +load("//python/private:attr_builders.bzl", "attrb") # buildifier: disable=bzl-visibility |
| 21 | +load("//python/private:py_binary_macro.bzl", "py_binary_macro") # buildifier: disable=bzl-visibility |
| 22 | +load("//python/private:py_binary_rule.bzl", "create_py_binary_rule_builder") # buildifier: disable=bzl-visibility |
| 23 | +load("//python/private:py_test_macro.bzl", "py_test_macro") # buildifier: disable=bzl-visibility |
| 24 | +load("//python/private:py_test_rule.bzl", "create_py_test_rule_builder") # buildifier: disable=bzl-visibility |
| 25 | +load("//tests/support:support.bzl", "VISIBLE_FOR_TESTING") |
| 26 | + |
| 27 | +def _perform_transition_impl(input_settings, attr, base_impl): |
| 28 | + settings = {k: input_settings[k] for k in _RECONFIG_INHERITED_OUTPUTS if k in input_settings} |
| 29 | + settings.update(base_impl(input_settings, attr)) |
| 30 | + |
| 31 | + settings[VISIBLE_FOR_TESTING] = True |
| 32 | + settings["//command_line_option:build_python_zip"] = attr.build_python_zip |
| 33 | + if attr.bootstrap_impl: |
| 34 | + settings["//python/config_settings:bootstrap_impl"] = attr.bootstrap_impl |
| 35 | + if attr.extra_toolchains: |
| 36 | + settings["//command_line_option:extra_toolchains"] = attr.extra_toolchains |
| 37 | + if attr.python_src: |
| 38 | + settings["//python/bin:python_src"] = attr.python_src |
| 39 | + if attr.repl_dep: |
| 40 | + settings["//python/bin:repl_dep"] = attr.repl_dep |
| 41 | + if attr.venvs_use_declare_symlink: |
| 42 | + settings["//python/config_settings:venvs_use_declare_symlink"] = attr.venvs_use_declare_symlink |
| 43 | + if attr.venvs_site_packages: |
| 44 | + settings["//python/config_settings:venvs_site_packages"] = attr.venvs_site_packages |
| 45 | + return settings |
| 46 | + |
| 47 | +_RECONFIG_INPUTS = [ |
| 48 | + "//python/config_settings:bootstrap_impl", |
| 49 | + "//python/bin:python_src", |
| 50 | + "//python/bin:repl_dep", |
| 51 | + "//command_line_option:extra_toolchains", |
| 52 | + "//python/config_settings:venvs_use_declare_symlink", |
| 53 | + "//python/config_settings:venvs_site_packages", |
| 54 | +] |
| 55 | +_RECONFIG_OUTPUTS = _RECONFIG_INPUTS + [ |
| 56 | + "//command_line_option:build_python_zip", |
| 57 | + VISIBLE_FOR_TESTING, |
| 58 | +] |
| 59 | +_RECONFIG_INHERITED_OUTPUTS = [v for v in _RECONFIG_OUTPUTS if v in _RECONFIG_INPUTS] |
| 60 | + |
| 61 | +_RECONFIG_ATTRS = { |
| 62 | + "bootstrap_impl": attrb.String(), |
| 63 | + "build_python_zip": attrb.String(default = "auto"), |
| 64 | + "extra_toolchains": attrb.StringList( |
| 65 | + doc = """ |
| 66 | +Value for the --extra_toolchains flag. |
| 67 | +
|
| 68 | +NOTE: You'll likely have to also specify //tests/support/cc_toolchains:all (or some CC toolchain) |
| 69 | +to make the RBE presubmits happy, which disable auto-detection of a CC |
| 70 | +toolchain. |
| 71 | +""", |
| 72 | + ), |
| 73 | + "python_src": attrb.Label(), |
| 74 | + "repl_dep": attrb.Label(), |
| 75 | + "venvs_site_packages": attrb.String(), |
| 76 | + "venvs_use_declare_symlink": attrb.String(), |
| 77 | +} |
| 78 | + |
| 79 | +def _create_reconfig_rule(builder): |
| 80 | + builder.attrs.update(_RECONFIG_ATTRS) |
| 81 | + |
| 82 | + base_cfg_impl = builder.cfg.implementation() |
| 83 | + builder.cfg.set_implementation(lambda *args: _perform_transition_impl(base_impl = base_cfg_impl, *args)) |
| 84 | + builder.cfg.update_inputs(_RECONFIG_INPUTS) |
| 85 | + builder.cfg.update_outputs(_RECONFIG_OUTPUTS) |
| 86 | + return builder.build() |
| 87 | + |
| 88 | +_py_reconfig_binary = _create_reconfig_rule(create_py_binary_rule_builder()) |
| 89 | + |
| 90 | +_py_reconfig_test = _create_reconfig_rule(create_py_test_rule_builder()) |
| 91 | + |
| 92 | +def py_reconfig_test(**kwargs): |
| 93 | + """Create a py_test with customized build settings for testing. |
| 94 | +
|
| 95 | + Args: |
| 96 | + **kwargs: kwargs to pass along to _py_reconfig_test. |
| 97 | + """ |
| 98 | + py_test_macro(_py_reconfig_test, **kwargs) |
| 99 | + |
| 100 | +def py_reconfig_binary(**kwargs): |
| 101 | + py_binary_macro(_py_reconfig_binary, **kwargs) |
0 commit comments