File: python_version.bzl

package info (click to toggle)
bazel-bootstrap 4.2.3%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 85,476 kB
  • sloc: java: 721,710; sh: 55,859; cpp: 35,359; python: 12,139; xml: 295; objc: 269; makefile: 113; ansic: 106; ruby: 3
file content (100 lines) | stat: -rwxr-xr-x 3,711 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilities for selecting the Python major version (Python 2 vs Python 3)."""

_UNSET = "UNSET"
_FALSE = "FALSE"
_TRUE = "TRUE"
_PY2 = "PY2"
_PY3 = "PY3"

def _python_version_flag_impl(ctx):
    # Version is determined using the same logic as in
    # PythonOptions#getPythonVersion:
    #
    #   1. Consult --python_version first, if present.
    #   2. Fallback on the default, which is governed by an incompatible change
    #      flag.
    if ctx.attr.python_version_flag != _UNSET:
        version = ctx.attr.python_version_flag
    else:
        version = _PY3 if ctx.attr.incompatible_py3_is_default_flag else _PY2

    if version not in ["PY2", "PY3"]:
        fail("Internal error: _python_version_flag should only be able to " +
             "match 'PY2' or 'PY3'")
    return [config_common.FeatureFlagInfo(value = version)]

_python_version_flag = rule(
    implementation = _python_version_flag_impl,
    attrs = {
        "python_version_flag": attr.string(mandatory = True, values = [_PY2, _PY3, _UNSET]),
        "incompatible_py3_is_default_flag": attr.bool(mandatory = True),
    },
)

def define_python_version_flag(name):
    """Defines the target to expose the Python version to select().

    For use only by @bazel_tools//tools/python:BUILD; see the documentation
    comment there.

    Args:
        name: The name of the target to introduce. Must have value
            "python_version". This param is present only to make the BUILD file
            more readable.
    """
    if native.package_name() != "tools/python":
        fail("define_python_version_flag() is private to " +
             "@bazel_tools//tools/python")
    if name != "python_version":
        fail("Python version flag must be named 'python_version'")

    # Config settings for the underlying native flags we depend on:
    # --python_version and --incompatible_py3_is_default.
    native.config_setting(
        name = "_python_version_setting_PY2",
        values = {"python_version": "PY2"},
        visibility = ["//visibility:private"],
    )
    native.config_setting(
        name = "_python_version_setting_PY3",
        values = {"python_version": "PY3"},
        visibility = ["//visibility:private"],
    )
    native.config_setting(
        name = "_incompatible_py3_is_default_setting_false",
        values = {"incompatible_py3_is_default": "false"},
        visibility = ["//visibility:private"],
    )
    native.config_setting(
        name = "_incompatible_py3_is_default_setting_true",
        values = {"incompatible_py3_is_default": "true"},
        visibility = ["//visibility:private"],
    )

    _python_version_flag(
        name = name,
        python_version_flag = select({
            ":_python_version_setting_PY2": _PY2,
            ":_python_version_setting_PY3": _PY3,
            "//conditions:default": _UNSET,
        }),
        incompatible_py3_is_default_flag = select({
            ":_incompatible_py3_is_default_setting_false": False,
            ":_incompatible_py3_is_default_setting_true": True,
        }),
        visibility = ["//visibility:public"],
    )