• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"A small function to create an alias for a whl distribution"
16
17def whl_config_setting(*, version = None, config_setting = None, filename = None, target_platforms = None):
18    """The bzl_packages value used by by the render_pkg_aliases function.
19
20    This contains the minimum amount of information required to generate correct
21    aliases in a hub repository.
22
23    Args:
24        version: optional(str), the version of the python toolchain that this
25            whl alias is for. If not set, then non-version aware aliases will be
26            constructed. This is mainly used for better error messages when there
27            is no match found during a select.
28        config_setting: optional(Label or str), the config setting that we should use. Defaults
29            to "//_config:is_python_{version}".
30        filename: optional(str), the distribution filename to derive the config_setting.
31        target_platforms: optional(list[str]), the list of target_platforms for this
32            distribution.
33
34    Returns:
35        a struct with the validated and parsed values.
36    """
37    if target_platforms:
38        for p in target_platforms:
39            if not p.startswith("cp"):
40                fail("target_platform should start with 'cp' denoting the python version, got: " + p)
41
42    return struct(
43        config_setting = config_setting,
44        filename = filename,
45        # Make the struct hashable
46        target_platforms = tuple(target_platforms) if target_platforms else None,
47        version = version,
48    )
49