• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"Repo rule used by bzlmod extension to create a repo that has a map of Python interpreters and their labels"
16
17load("//python:versions.bzl", "MINOR_MAPPING", "WINDOWS_NAME")
18load(
19    "//python/private:toolchains_repo.bzl",
20    "get_host_os_arch",
21    "get_host_platform",
22    "get_repository_name",
23    "python_toolchain_build_file_content",
24)
25
26def _have_same_length(*lists):
27    if not lists:
28        fail("expected at least one list")
29    return len({len(length): None for length in lists}) == 1
30
31def _get_version(python_version):
32    # we need to get the MINOR_MAPPING or use the full version
33    if python_version in MINOR_MAPPING:
34        python_version = MINOR_MAPPING[python_version]
35    return python_version
36
37def _python_toolchain_build_file_content(
38        prefixes,
39        python_versions,
40        set_python_version_constraints,
41        user_repository_names,
42        workspace_location):
43    """This macro iterates over each of the lists and returns the toolchain content.
44
45    python_toolchain_build_file_content is called to generate each of the toolchain
46    definitions.
47    """
48
49    if not _have_same_length(python_versions, set_python_version_constraints, user_repository_names):
50        fail("all lists must have the same length")
51
52    rules_python = get_repository_name(workspace_location)
53
54    # Iterate over the length of python_versions and call
55    # build the toolchain content by calling python_toolchain_build_file_content
56    return "\n".join([python_toolchain_build_file_content(
57        prefix = prefixes[i],
58        python_version = _get_version(python_versions[i]),
59        set_python_version_constraint = set_python_version_constraints[i],
60        user_repository_name = user_repository_names[i],
61        rules_python = rules_python,
62    ) for i in range(len(python_versions))])
63
64_build_file_for_hub_template = """
65INTERPRETER_LABELS = {{
66{interpreter_labels}
67}}
68DEFAULT_PYTHON_VERSION = "{default_python_version}"
69"""
70
71_line_for_hub_template = """\
72    "{name}": Label("@{name}_{platform}//:{path}"),
73"""
74
75def _hub_repo_impl(rctx):
76    # Create the various toolchain definitions and
77    # write them to the BUILD file.
78    rctx.file(
79        "BUILD.bazel",
80        _python_toolchain_build_file_content(
81            rctx.attr.toolchain_prefixes,
82            rctx.attr.toolchain_python_versions,
83            rctx.attr.toolchain_set_python_version_constraints,
84            rctx.attr.toolchain_user_repository_names,
85            rctx.attr._rules_python_workspace,
86        ),
87        executable = False,
88    )
89
90    (os, arch) = get_host_os_arch(rctx)
91    platform = get_host_platform(os, arch)
92    is_windows = (os == WINDOWS_NAME)
93    path = "python.exe" if is_windows else "bin/python3"
94
95    # Create a dict that is later used to create
96    # a symlink to a interpreter.
97    interpreter_labels = "".join([_line_for_hub_template.format(
98        name = name,
99        platform = platform,
100        path = path,
101    ) for name in rctx.attr.toolchain_user_repository_names])
102
103    rctx.file(
104        "interpreters.bzl",
105        _build_file_for_hub_template.format(
106            interpreter_labels = interpreter_labels,
107            default_python_version = rctx.attr.default_python_version,
108        ),
109        executable = False,
110    )
111
112hub_repo = repository_rule(
113    doc = """\
114This private rule create a repo with a BUILD file that contains a map of interpreter names
115and the labels to said interpreters. This map is used to by the interpreter hub extension.
116This rule also writes out the various toolchains for the different Python versions.
117""",
118    implementation = _hub_repo_impl,
119    attrs = {
120        "default_python_version": attr.string(
121            doc = "Default Python version for the build.",
122            mandatory = True,
123        ),
124        "toolchain_prefixes": attr.string_list(
125            doc = "List prefixed for the toolchains",
126            mandatory = True,
127        ),
128        "toolchain_python_versions": attr.string_list(
129            doc = "List of Python versions for the toolchains",
130            mandatory = True,
131        ),
132        "toolchain_set_python_version_constraints": attr.string_list(
133            doc = "List of version contraints for the toolchains",
134            mandatory = True,
135        ),
136        "toolchain_user_repository_names": attr.string_list(
137            doc = "List of the user repo names for the toolchains",
138            mandatory = True,
139        ),
140        "_rules_python_workspace": attr.label(default = Label("//:does_not_matter_what_this_name_is")),
141    },
142)
143