|
| 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 | + |
| 15 | +""" |
| 16 | +Simple toolchain which overrides env and exec requirements. |
| 17 | +""" |
| 18 | + |
| 19 | +load(":text_util.bzl", "render") |
| 20 | +load( |
| 21 | + ":toolchain_types.bzl", |
| 22 | + "PY_TEST_TOOLCHAIN_TYPE", |
| 23 | +) |
| 24 | + |
| 25 | +PytestProvider = provider( |
| 26 | + fields = [ |
| 27 | + "coverage_rc", |
| 28 | + ], |
| 29 | +) |
| 30 | + |
| 31 | +def _py_test_toolchain_impl(ctx): |
| 32 | + return [ |
| 33 | + platform_common.ToolchainInfo( |
| 34 | + py_test_info = PytestProvider( |
| 35 | + coverage_rc = ctx.attr.coverage_rc, |
| 36 | + ), |
| 37 | + ), |
| 38 | + ] |
| 39 | + |
| 40 | +py_test_toolchain = rule( |
| 41 | + implementation = _py_test_toolchain_impl, |
| 42 | + attrs = { |
| 43 | + "coverage_rc": attr.label( |
| 44 | + allow_single_file = True, |
| 45 | + ), |
| 46 | + }, |
| 47 | +) |
| 48 | +_TOOLCHAIN_TEMPLATE = """ |
| 49 | +load("@rules_python//python/private:py_test_toolchain.bzl", "py_test_toolchain") |
| 50 | +py_test_toolchain( |
| 51 | + name = "{name}_toolchain", |
| 52 | + coverage_rc = "{coverage_rc}", |
| 53 | +) |
| 54 | +
|
| 55 | +toolchain( |
| 56 | + name = "{name}", |
| 57 | + target_compatible_with = [], |
| 58 | + exec_compatible_with = [], |
| 59 | + toolchain = "{name}_toolchain", |
| 60 | + toolchain_type = "{toolchain_type}", |
| 61 | +) |
| 62 | +""" |
| 63 | + |
| 64 | +def _toolchains_repo_impl(repository_ctx): |
| 65 | + build_content = _TOOLCHAIN_TEMPLATE.format( |
| 66 | + name = "py_test_toolchain", |
| 67 | + toolchain_type = repository_ctx.attr.toolchain_type, |
| 68 | + coverage_rc = repository_ctx.attr.coverage_rc, |
| 69 | + ) |
| 70 | + repository_ctx.file("BUILD.bazel", build_content) |
| 71 | + |
| 72 | +py_test_toolchain_repo = repository_rule( |
| 73 | + _toolchains_repo_impl, |
| 74 | + doc = "Generates a toolchain hub repository", |
| 75 | + attrs = { |
| 76 | + "toolchain_type": attr.string(doc = "Toolchain type", mandatory = True), |
| 77 | + "coverage_rc": attr.label( |
| 78 | + allow_single_file = True, |
| 79 | + doc = "The coverage rc file", |
| 80 | + mandatory = True, |
| 81 | + ), |
| 82 | + }, |
| 83 | +) |
| 84 | + |
| 85 | +def register_py_test_toolchain(coverage_rc, register_toolchains = True): |
| 86 | + # Need to create a repository rule for this to work. |
| 87 | + py_test_toolchain_repo( |
| 88 | + name = "py_test_toolchain", |
| 89 | + coverage_rc = coverage_rc, |
| 90 | + toolchain_type = str(PY_TEST_TOOLCHAIN_TYPE), |
| 91 | + ) |
| 92 | + if register_toolchains: |
| 93 | + native.toolchain(name = "py_test_toolchain") |
0 commit comments