xref: /aosp_15_r20/external/bazelbuild-rules_testing/dev_extension.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1*d6050574SRomain Jobredeaux# Copyright 2023 The Bazel Authors. All rights reserved.
2*d6050574SRomain Jobredeaux#
3*d6050574SRomain Jobredeaux# Licensed under the Apache License, Version 2.0 (the "License");
4*d6050574SRomain Jobredeaux# you may not use this file except in compliance with the License.
5*d6050574SRomain Jobredeaux# You may obtain a copy of the License at
6*d6050574SRomain Jobredeaux#
7*d6050574SRomain Jobredeaux#     http://www.apache.org/licenses/LICENSE-2.0
8*d6050574SRomain Jobredeaux#
9*d6050574SRomain Jobredeaux# Unless required by applicable law or agreed to in writing, software
10*d6050574SRomain Jobredeaux# distributed under the License is distributed on an "AS IS" BASIS,
11*d6050574SRomain Jobredeaux# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d6050574SRomain Jobredeaux# See the License for the specific language governing permissions and
13*d6050574SRomain Jobredeaux# limitations under the License.
14*d6050574SRomain Jobredeaux
15*d6050574SRomain Jobredeaux"""Extension only used for development purposes."""
16*d6050574SRomain Jobredeaux
17*d6050574SRomain Jobredeauxdef _dev_ext_impl(mctx):
18*d6050574SRomain Jobredeaux    module = mctx.modules[0]
19*d6050574SRomain Jobredeaux    _dev_toolchains_repo(
20*d6050574SRomain Jobredeaux        name = "rules_testing_dev_toolchains",
21*d6050574SRomain Jobredeaux        is_root = module.is_root,
22*d6050574SRomain Jobredeaux    )
23*d6050574SRomain Jobredeaux
24*d6050574SRomain Jobredeauxdev = module_extension(
25*d6050574SRomain Jobredeaux    implementation = _dev_ext_impl,
26*d6050574SRomain Jobredeaux)
27*d6050574SRomain Jobredeaux
28*d6050574SRomain Jobredeauxdef _dev_toolchains_repo_impl(rctx):
29*d6050574SRomain Jobredeaux    # If its the root module, then we're in rules_testing and
30*d6050574SRomain Jobredeaux    # it's a dev dependency situation.
31*d6050574SRomain Jobredeaux    if rctx.attr.is_root:
32*d6050574SRomain Jobredeaux        toolchain_build = Label("@python_3_11_toolchains//:BUILD.bazel")
33*d6050574SRomain Jobredeaux
34*d6050574SRomain Jobredeaux        # NOTE: This is brittle. It only works because, luckily,
35*d6050574SRomain Jobredeaux        # rules_python's toolchain BUILD file is essentially self-contained.
36*d6050574SRomain Jobredeaux        # It only uses absolute references and doesn't load anything,
37*d6050574SRomain Jobredeaux        # so we can copy it elsewhere and it still works.
38*d6050574SRomain Jobredeaux        rctx.symlink(toolchain_build, "BUILD.bazel")
39*d6050574SRomain Jobredeaux    else:
40*d6050574SRomain Jobredeaux        rctx.file("BUILD.bazel", "")
41*d6050574SRomain Jobredeaux
42*d6050574SRomain Jobredeaux_dev_toolchains_repo = repository_rule(
43*d6050574SRomain Jobredeaux    implementation = _dev_toolchains_repo_impl,
44*d6050574SRomain Jobredeaux    attrs = {
45*d6050574SRomain Jobredeaux        "is_root": attr.bool(),
46*d6050574SRomain Jobredeaux    },
47*d6050574SRomain Jobredeaux)
48