xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/stamp.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
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"""A small utility module dedicated to detecting whether or not the `--stamp` flag is enabled
16
17This module can be removed likely after the following PRs ar addressed:
18- https://github.com/bazelbuild/bazel/issues/11164
19"""
20
21StampSettingInfo = provider(
22    doc = "Information about the `--stamp` command line flag",
23    fields = {
24        "value": "bool: Whether or not the `--stamp` flag was enabled",
25    },
26)
27
28def _stamp_build_setting_impl(ctx):
29    return StampSettingInfo(value = ctx.attr.value)
30
31_stamp_build_setting = rule(
32    doc = """\
33Whether to encode build information into the binary. Possible values:
34
35- stamp = 1: Always stamp the build information into the binary, even in [--nostamp][stamp] builds. \
36This setting should be avoided, since it potentially kills remote caching for the binary and \
37any downstream actions that depend on it.
38- stamp = 0: Always replace build information by constant values. This gives good build result caching.
39- stamp = -1: Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
40
41Stamped binaries are not rebuilt unless their dependencies change.
42[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp
43    """,
44    implementation = _stamp_build_setting_impl,
45    attrs = {
46        "value": attr.bool(
47            doc = "The default value of the stamp build flag",
48            mandatory = True,
49        ),
50    },
51)
52
53def stamp_build_setting(name, visibility = ["//visibility:public"]):
54    native.config_setting(
55        name = "stamp_detect",
56        values = {"stamp": "1"},
57        visibility = visibility,
58    )
59
60    _stamp_build_setting(
61        name = name,
62        value = select({
63            ":stamp_detect": True,
64            "//conditions:default": False,
65        }),
66        visibility = visibility,
67    )
68
69def is_stamping_enabled(attr):
70    """Determine whether or not build stamping is enabled
71
72    Args:
73        attr (struct): A rule's struct of attributes (`ctx.attr`)
74
75    Returns:
76        bool: The stamp value
77    """
78    stamp_num = getattr(attr, "stamp", -1)
79    if stamp_num == 1:
80        return True
81    elif stamp_num == 0:
82        return False
83    elif stamp_num == -1:
84        stamp_flag = getattr(attr, "_stamp_flag", None)
85        return stamp_flag[StampSettingInfo].value if stamp_flag else False
86    else:
87        fail("Unexpected `stamp` value: {}".format(stamp_num))
88