xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/flags.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Copyright 2024 The Bazel Authors. All rights reserved.
2*60517a1eSAndroid Build Coastguard Worker#
3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*60517a1eSAndroid Build Coastguard Worker#
7*60517a1eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*60517a1eSAndroid Build Coastguard Worker#
9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*60517a1eSAndroid Build Coastguard Worker# limitations under the License.
14*60517a1eSAndroid Build Coastguard Worker
15*60517a1eSAndroid Build Coastguard Worker"""Values and helpers for flags.
16*60517a1eSAndroid Build Coastguard Worker
17*60517a1eSAndroid Build Coastguard WorkerNOTE: The transitive loads of this should be kept minimal. This avoids loading
18*60517a1eSAndroid Build Coastguard Workerunnecessary files when all that are needed are flag definitions.
19*60517a1eSAndroid Build Coastguard Worker"""
20*60517a1eSAndroid Build Coastguard Worker
21*60517a1eSAndroid Build Coastguard Workerload("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
22*60517a1eSAndroid Build Coastguard Workerload("//python/private:enum.bzl", "enum")
23*60517a1eSAndroid Build Coastguard Worker
24*60517a1eSAndroid Build Coastguard Workerdef _bootstrap_impl_flag_get_value(ctx):
25*60517a1eSAndroid Build Coastguard Worker    return ctx.attr._bootstrap_impl_flag[BuildSettingInfo].value
26*60517a1eSAndroid Build Coastguard Worker
27*60517a1eSAndroid Build Coastguard Worker# buildifier: disable=name-conventions
28*60517a1eSAndroid Build Coastguard WorkerBootstrapImplFlag = enum(
29*60517a1eSAndroid Build Coastguard Worker    SYSTEM_PYTHON = "system_python",
30*60517a1eSAndroid Build Coastguard Worker    SCRIPT = "script",
31*60517a1eSAndroid Build Coastguard Worker    get_value = _bootstrap_impl_flag_get_value,
32*60517a1eSAndroid Build Coastguard Worker)
33*60517a1eSAndroid Build Coastguard Worker
34*60517a1eSAndroid Build Coastguard Workerdef _precompile_flag_get_effective_value(ctx):
35*60517a1eSAndroid Build Coastguard Worker    value = ctx.attr._precompile_flag[BuildSettingInfo].value
36*60517a1eSAndroid Build Coastguard Worker    if value == PrecompileFlag.AUTO:
37*60517a1eSAndroid Build Coastguard Worker        value = PrecompileFlag.DISABLED
38*60517a1eSAndroid Build Coastguard Worker    return value
39*60517a1eSAndroid Build Coastguard Worker
40*60517a1eSAndroid Build Coastguard Worker# Determines if the Python exec tools toolchain should be registered.
41*60517a1eSAndroid Build Coastguard Worker# buildifier: disable=name-conventions
42*60517a1eSAndroid Build Coastguard WorkerExecToolsToolchainFlag = enum(
43*60517a1eSAndroid Build Coastguard Worker    # Enable registering the exec tools toolchain using the hermetic toolchain.
44*60517a1eSAndroid Build Coastguard Worker    ENABLED = "enabled",
45*60517a1eSAndroid Build Coastguard Worker    # Disable registering the exec tools toolchain using the hermetic toolchain.
46*60517a1eSAndroid Build Coastguard Worker    DISABLED = "disabled",
47*60517a1eSAndroid Build Coastguard Worker)
48*60517a1eSAndroid Build Coastguard Worker
49*60517a1eSAndroid Build Coastguard Worker# Determines if Python source files should be compiled at build time.
50*60517a1eSAndroid Build Coastguard Worker#
51*60517a1eSAndroid Build Coastguard Worker# NOTE: The flag value is overridden by the target-level attribute, except
52*60517a1eSAndroid Build Coastguard Worker# for the case of `force_enabled` and `forced_disabled`.
53*60517a1eSAndroid Build Coastguard Worker# buildifier: disable=name-conventions
54*60517a1eSAndroid Build Coastguard WorkerPrecompileFlag = enum(
55*60517a1eSAndroid Build Coastguard Worker    # Automatically decide the effective value based on environment,
56*60517a1eSAndroid Build Coastguard Worker    # target platform, etc.
57*60517a1eSAndroid Build Coastguard Worker    AUTO = "auto",
58*60517a1eSAndroid Build Coastguard Worker    # Compile Python source files at build time. Note that
59*60517a1eSAndroid Build Coastguard Worker    # --precompile_add_to_runfiles affects how the compiled files are included
60*60517a1eSAndroid Build Coastguard Worker    # into a downstream binary.
61*60517a1eSAndroid Build Coastguard Worker    ENABLED = "enabled",
62*60517a1eSAndroid Build Coastguard Worker    # Don't compile Python source files at build time.
63*60517a1eSAndroid Build Coastguard Worker    DISABLED = "disabled",
64*60517a1eSAndroid Build Coastguard Worker    # Compile Python source files, but only if they're a generated file.
65*60517a1eSAndroid Build Coastguard Worker    IF_GENERATED_SOURCE = "if_generated_source",
66*60517a1eSAndroid Build Coastguard Worker    # Like `enabled`, except overrides target-level setting. This is mostly
67*60517a1eSAndroid Build Coastguard Worker    # useful for development, testing enabling precompilation more broadly, or
68*60517a1eSAndroid Build Coastguard Worker    # as an escape hatch if build-time compiling is not available.
69*60517a1eSAndroid Build Coastguard Worker    FORCE_ENABLED = "force_enabled",
70*60517a1eSAndroid Build Coastguard Worker    # Like `disabled`, except overrides target-level setting. This is useful
71*60517a1eSAndroid Build Coastguard Worker    # useful for development, testing enabling precompilation more broadly, or
72*60517a1eSAndroid Build Coastguard Worker    # as an escape hatch if build-time compiling is not available.
73*60517a1eSAndroid Build Coastguard Worker    FORCE_DISABLED = "force_disabled",
74*60517a1eSAndroid Build Coastguard Worker    get_effective_value = _precompile_flag_get_effective_value,
75*60517a1eSAndroid Build Coastguard Worker)
76*60517a1eSAndroid Build Coastguard Worker
77*60517a1eSAndroid Build Coastguard Workerdef _precompile_source_retention_flag_get_effective_value(ctx):
78*60517a1eSAndroid Build Coastguard Worker    value = ctx.attr._precompile_source_retention_flag[BuildSettingInfo].value
79*60517a1eSAndroid Build Coastguard Worker    if value == PrecompileSourceRetentionFlag.AUTO:
80*60517a1eSAndroid Build Coastguard Worker        value = PrecompileSourceRetentionFlag.KEEP_SOURCE
81*60517a1eSAndroid Build Coastguard Worker    return value
82*60517a1eSAndroid Build Coastguard Worker
83*60517a1eSAndroid Build Coastguard Worker# Determines if, when a source file is compiled, if the source file is kept
84*60517a1eSAndroid Build Coastguard Worker# in the resulting output or not.
85*60517a1eSAndroid Build Coastguard Worker# buildifier: disable=name-conventions
86*60517a1eSAndroid Build Coastguard WorkerPrecompileSourceRetentionFlag = enum(
87*60517a1eSAndroid Build Coastguard Worker    # Automatically decide the effective value based on environment, etc.
88*60517a1eSAndroid Build Coastguard Worker    AUTO = "auto",
89*60517a1eSAndroid Build Coastguard Worker    # Include the original py source in the output.
90*60517a1eSAndroid Build Coastguard Worker    KEEP_SOURCE = "keep_source",
91*60517a1eSAndroid Build Coastguard Worker    # Don't include the original py source.
92*60517a1eSAndroid Build Coastguard Worker    OMIT_SOURCE = "omit_source",
93*60517a1eSAndroid Build Coastguard Worker    # Keep the original py source if it's a regular source file, but omit it
94*60517a1eSAndroid Build Coastguard Worker    # if it's a generated file.
95*60517a1eSAndroid Build Coastguard Worker    OMIT_IF_GENERATED_SOURCE = "omit_if_generated_source",
96*60517a1eSAndroid Build Coastguard Worker    get_effective_value = _precompile_source_retention_flag_get_effective_value,
97*60517a1eSAndroid Build Coastguard Worker)
98*60517a1eSAndroid Build Coastguard Worker
99*60517a1eSAndroid Build Coastguard Worker# Determines if a target adds its compiled files to its runfiles. When a target
100*60517a1eSAndroid Build Coastguard Worker# compiles its files, but doesn't add them to its own runfiles, it relies on
101*60517a1eSAndroid Build Coastguard Worker# a downstream target to retrieve them from `PyInfo.transitive_pyc_files`
102*60517a1eSAndroid Build Coastguard Worker# buildifier: disable=name-conventions
103*60517a1eSAndroid Build Coastguard WorkerPrecompileAddToRunfilesFlag = enum(
104*60517a1eSAndroid Build Coastguard Worker    # Always include the compiled files in the target's runfiles.
105*60517a1eSAndroid Build Coastguard Worker    ALWAYS = "always",
106*60517a1eSAndroid Build Coastguard Worker    # Don't include the compiled files in the target's runfiles; they are
107*60517a1eSAndroid Build Coastguard Worker    # still added to `PyInfo.transitive_pyc_files`. See also:
108*60517a1eSAndroid Build Coastguard Worker    # `py_binary.pyc_collection` attribute. This is useful for allowing
109*60517a1eSAndroid Build Coastguard Worker    # incrementally enabling precompilation on a per-binary basis.
110*60517a1eSAndroid Build Coastguard Worker    DECIDED_ELSEWHERE = "decided_elsewhere",
111*60517a1eSAndroid Build Coastguard Worker)
112*60517a1eSAndroid Build Coastguard Worker
113*60517a1eSAndroid Build Coastguard Worker# Determine if `py_binary` collects transitive pyc files.
114*60517a1eSAndroid Build Coastguard Worker# NOTE: This flag is only respect if `py_binary.pyc_collection` is `inherit`.
115*60517a1eSAndroid Build Coastguard Worker# buildifier: disable=name-conventions
116*60517a1eSAndroid Build Coastguard WorkerPycCollectionFlag = enum(
117*60517a1eSAndroid Build Coastguard Worker    # Include `PyInfo.transitive_pyc_files` as part of the binary.
118*60517a1eSAndroid Build Coastguard Worker    INCLUDE_PYC = "include_pyc",
119*60517a1eSAndroid Build Coastguard Worker    # Don't include `PyInfo.transitive_pyc_files` as part of the binary.
120*60517a1eSAndroid Build Coastguard Worker    DISABLED = "disabled",
121*60517a1eSAndroid Build Coastguard Worker)
122