1# Copyright (C) 2023 The Android Open Source Project
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
15load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
16
17visibility(["//build/bazel/..."])
18
19BuildFingerprintInfo = provider(
20    fields = {
21        "fingerprint_blank_build_number": "The fingerprint, but without the build number",
22        "fingerprint_placeholder_build_number": "The fingerprint, but with the build number replaced with {BUILD_NUMBER}",
23    },
24)
25
26def _build_fingerprint_impl(ctx):
27    build_version_tags = ctx.attr._build_version_tags[BuildSettingInfo].value
28    default_app_certificate = ctx.attr._default_app_certificate[BuildSettingInfo].value
29    if not default_app_certificate or default_app_certificate == "build/make/target/product/security/testkey":
30        build_version_tags = build_version_tags + ["test-keys"]
31    else:
32        build_version_tags = build_version_tags + ["dev-keys"]
33    build_version_tags = sorted(build_version_tags)
34
35    build_fingerprint_blank = "%s/%s/%s:%s/%s/%s:%s/%s" % (
36        ctx.attr._product_brand[BuildSettingInfo].value,
37        ctx.attr._device_product[BuildSettingInfo].value,
38        ctx.attr._device_name[BuildSettingInfo].value,
39        ctx.attr._platform_version_name[BuildSettingInfo].value,
40        ctx.attr._build_id[BuildSettingInfo].value,
41        "",
42        ctx.attr._target_build_variant[BuildSettingInfo].value,
43        ",".join(build_version_tags),
44    )
45    build_fingerprint_placeholder = "%s/%s/%s:%s/%s/%s:%s/%s" % (
46        ctx.attr._product_brand[BuildSettingInfo].value,
47        ctx.attr._device_product[BuildSettingInfo].value,
48        ctx.attr._device_name[BuildSettingInfo].value,
49        ctx.attr._platform_version_name[BuildSettingInfo].value,
50        ctx.attr._build_id[BuildSettingInfo].value,
51        "{BUILD_NUMBER}",
52        ctx.attr._target_build_variant[BuildSettingInfo].value,
53        ",".join(build_version_tags),
54    )
55    return [
56        BuildFingerprintInfo(
57            fingerprint_blank_build_number = build_fingerprint_blank,
58            fingerprint_placeholder_build_number = build_fingerprint_placeholder,
59        ),
60    ]
61
62build_fingerprint = rule(
63    implementation = _build_fingerprint_impl,
64    attrs = {
65        "_build_id": attr.label(
66            default = "//build/bazel/product_config:build_id",
67        ),
68        "_build_version_tags": attr.label(
69            default = "//build/bazel/product_config:build_version_tags",
70        ),
71        "_default_app_certificate": attr.label(
72            default = "//build/bazel/product_config:default_app_certificate",
73        ),
74        "_device_name": attr.label(
75            default = "//build/bazel/product_config:device_name",
76        ),
77        "_device_product": attr.label(
78            default = "//build/bazel/product_config:device_product",
79        ),
80        "_product_brand": attr.label(
81            default = "//build/bazel/product_config:product_brand",
82        ),
83        "_platform_version_name": attr.label(
84            default = "//build/bazel/product_config:platform_version_name",
85        ),
86        "_target_build_variant": attr.label(
87            default = "//build/bazel/product_config:target_build_variant",
88        ),
89    },
90)
91