xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/py_cc_toolchain_rule.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 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"""Implementation of py_cc_toolchain rule.
16
17NOTE: This is a beta-quality feature. APIs subject to change until
18https://github.com/bazelbuild/rules_python/issues/824 is considered done.
19"""
20
21load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
22load("@rules_cc//cc:defs.bzl", "CcInfo")
23load(":py_cc_toolchain_info.bzl", "PyCcToolchainInfo")
24
25def _py_cc_toolchain_impl(ctx):
26    if ctx.attr.libs:
27        libs = struct(
28            providers_map = {
29                "CcInfo": ctx.attr.libs[CcInfo],
30                "DefaultInfo": ctx.attr.libs[DefaultInfo],
31            },
32        )
33    else:
34        libs = None
35
36    py_cc_toolchain = PyCcToolchainInfo(
37        headers = struct(
38            providers_map = {
39                "CcInfo": ctx.attr.headers[CcInfo],
40                "DefaultInfo": ctx.attr.headers[DefaultInfo],
41            },
42        ),
43        libs = libs,
44        python_version = ctx.attr.python_version,
45    )
46    extra_kwargs = {}
47    if ctx.attr._visible_for_testing[BuildSettingInfo].value:
48        extra_kwargs["toolchain_label"] = ctx.label
49    return [platform_common.ToolchainInfo(
50        py_cc_toolchain = py_cc_toolchain,
51        **extra_kwargs
52    )]
53
54py_cc_toolchain = rule(
55    implementation = _py_cc_toolchain_impl,
56    attrs = {
57        "headers": attr.label(
58            doc = ("Target that provides the Python headers. Typically this " +
59                   "is a cc_library target."),
60            providers = [CcInfo],
61            mandatory = True,
62        ),
63        "libs": attr.label(
64            doc = ("Target that provides the Python runtime libraries for linking. " +
65                   "Typically this is a cc_library target of `.so` files."),
66            providers = [CcInfo],
67        ),
68        "python_version": attr.string(
69            doc = "The Major.minor Python version, e.g. 3.11",
70            mandatory = True,
71        ),
72        "_visible_for_testing": attr.label(
73            default = "//python/private:visible_for_testing",
74        ),
75    },
76    doc = """\
77A toolchain for a Python runtime's C/C++ information (e.g. headers)
78
79This rule carries information about the C/C++ side of a Python runtime, e.g.
80headers, shared libraries, etc.
81
82This provides `ToolchainInfo` with the following attributes:
83* `py_cc_toolchain`: {type}`PyCcToolchainInfo`
84* `toolchain_label`: {type}`Label` _only present when `--visibile_for_testing=True`
85  for internal testing_. The rule's label; this allows identifying what toolchain
86  implmentation was selected for testing purposes.
87""",
88)
89