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