• 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"""Values and helpers for flags.
16
17NOTE: The transitive loads of this should be kept minimal. This avoids loading
18unnecessary files when all that are needed are flag definitions.
19"""
20
21load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
22load(":enum.bzl", "enum")
23
24def _FlagEnum_flag_values(self):
25    return sorted(self.__members__.values())
26
27def FlagEnum(**kwargs):
28    """Define an enum specialized for flags.
29
30    Args:
31        **kwargs: members of the enum.
32
33    Returns:
34        {type}`FlagEnum` struct. This is an enum with the following extras:
35        * `flag_values`: A function that returns a sorted list of the
36          flag values (enum `__members__`). Useful for passing to the
37          `values` attribute for string flags.
38    """
39    return enum(
40        methods = dict(flag_values = _FlagEnum_flag_values),
41        **kwargs
42    )
43
44def _AddSrcsToRunfilesFlag_is_enabled(ctx):
45    value = ctx.attr._add_srcs_to_runfiles_flag[BuildSettingInfo].value
46    if value == AddSrcsToRunfilesFlag.AUTO:
47        value = AddSrcsToRunfilesFlag.ENABLED
48    return value == AddSrcsToRunfilesFlag.ENABLED
49
50# buildifier: disable=name-conventions
51AddSrcsToRunfilesFlag = FlagEnum(
52    AUTO = "auto",
53    ENABLED = "enabled",
54    DISABLED = "disabled",
55    is_enabled = _AddSrcsToRunfilesFlag_is_enabled,
56)
57
58def _bootstrap_impl_flag_get_value(ctx):
59    return ctx.attr._bootstrap_impl_flag[BuildSettingInfo].value
60
61# buildifier: disable=name-conventions
62BootstrapImplFlag = enum(
63    SYSTEM_PYTHON = "system_python",
64    SCRIPT = "script",
65    get_value = _bootstrap_impl_flag_get_value,
66)
67
68def _precompile_flag_get_effective_value(ctx):
69    value = ctx.attr._precompile_flag[BuildSettingInfo].value
70    if value == PrecompileFlag.AUTO:
71        value = PrecompileFlag.DISABLED
72    return value
73
74# Determines if the Python exec tools toolchain should be registered.
75# buildifier: disable=name-conventions
76ExecToolsToolchainFlag = enum(
77    # Enable registering the exec tools toolchain using the hermetic toolchain.
78    ENABLED = "enabled",
79    # Disable registering the exec tools toolchain using the hermetic toolchain.
80    DISABLED = "disabled",
81)
82
83# Determines if Python source files should be compiled at build time.
84#
85# NOTE: The flag value is overridden by the target-level attribute, except
86# for the case of `force_enabled` and `forced_disabled`.
87# buildifier: disable=name-conventions
88PrecompileFlag = enum(
89    # Automatically decide the effective value based on environment,
90    # target platform, etc.
91    AUTO = "auto",
92    # Compile Python source files at build time.
93    ENABLED = "enabled",
94    # Don't compile Python source files at build time.
95    DISABLED = "disabled",
96    # Like `enabled`, except overrides target-level setting. This is mostly
97    # useful for development, testing enabling precompilation more broadly, or
98    # as an escape hatch to force all transitive deps to precompile.
99    FORCE_ENABLED = "force_enabled",
100    # Like `disabled`, except overrides target-level setting. This is useful
101    # useful for development, testing enabling precompilation more broadly, or
102    # as an escape hatch if build-time compiling is not available.
103    FORCE_DISABLED = "force_disabled",
104    get_effective_value = _precompile_flag_get_effective_value,
105)
106
107def _precompile_source_retention_flag_get_effective_value(ctx):
108    value = ctx.attr._precompile_source_retention_flag[BuildSettingInfo].value
109    if value == PrecompileSourceRetentionFlag.AUTO:
110        value = PrecompileSourceRetentionFlag.KEEP_SOURCE
111    return value
112
113# Determines if, when a source file is compiled, if the source file is kept
114# in the resulting output or not.
115# buildifier: disable=name-conventions
116PrecompileSourceRetentionFlag = enum(
117    # Automatically decide the effective value based on environment, etc.
118    AUTO = "auto",
119    # Include the original py source in the output.
120    KEEP_SOURCE = "keep_source",
121    # Don't include the original py source.
122    OMIT_SOURCE = "omit_source",
123    get_effective_value = _precompile_source_retention_flag_get_effective_value,
124)
125
126# Used for matching freethreaded toolchains and would have to be used in wheels
127# as well.
128# buildifier: disable=name-conventions
129FreeThreadedFlag = enum(
130    # Use freethreaded python toolchain and wheels.
131    YES = "yes",
132    # Do not use freethreaded python toolchain and wheels.
133    NO = "no",
134)
135
136# Determines which libc flavor is preferred when selecting the toolchain and
137# linux whl distributions.
138#
139# buildifier: disable=name-conventions
140LibcFlag = FlagEnum(
141    # Prefer glibc wheels (e.g. manylinux_2_17_x86_64 or linux_x86_64)
142    GLIBC = "glibc",
143    # Prefer musl wheels (e.g. musllinux_2_17_x86_64)
144    MUSL = "musl",
145)
146