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