xref: /aosp_15_r20/external/bazelbuild-rules_android/rules/android_revision.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
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"""Parse and compare Android revision strings."""
16
17# TODO(katre): support preview versions.
18AndroidRevisionInfo = provider(
19    "Information about Android revision specifications.",
20    fields = {
21        "major": "The major version number",
22        "minor": "The minor version number, or 0 if unset.",
23        "micro": "The micro version number, or 0 if unset.",
24        "version": "The version string.",
25        "dir": "The directory where the revision would exist in an Android SDK.",
26    },
27)
28
29def parse_android_revision(input):
30    """Parse and Android revision string and return an AndroidRevisionInfo.
31
32    Args:
33      input: The raw revision string to parse.
34
35    Returns:
36      An AndroidRevisionInfo provider representing the input.
37    """
38    input = input.strip()
39    parts = input.split(".")
40    if len(parts) < 1:
41        fail("Invalid Android revision %s" % input)
42    major = int(parts[0]) if len(parts) >= 1 else 0
43    minor = int(parts[1]) if len(parts) >= 2 else 0
44    micro = int(parts[2]) if len(parts) >= 3 else 0
45
46    return AndroidRevisionInfo(
47        version = input,
48        dir = input,
49        major = major,
50        minor = minor,
51        micro = micro,
52    )
53
54def _compare_android_revision_field(first, second, name):
55    first_val = getattr(first, name)
56    second_val = getattr(second, name)
57    if first_val > second_val:
58        return first
59    elif first_val < second_val:
60        return second
61    return None
62
63def compare_android_revisions(first, second):
64    """Compares two AndroidRevisionInfo providers and returns the one with the highest version.
65
66    Args:
67      first: The first revision to compare.
68      second: The first revision to compare.
69
70    Returns:
71      The revision with the higher version number, or the first if they are equal.
72    """
73    if first == None and second == None:
74        return None
75    if first != None and second == None:
76        return first
77    if first == None and second != None:
78        return second
79    highest = _compare_android_revision_field(first, second, "major")
80    if highest != None:
81        return highest
82    highest = _compare_android_revision_field(first, second, "minor")
83    if highest != None:
84        return highest
85    highest = _compare_android_revision_field(first, second, "micro")
86    if highest != None:
87        return highest
88    return first
89