xref: /aosp_15_r20/build/bazel/rules/apex/sdk_versions.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
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("//build/bazel/rules/common:api.bzl", "api")
16
17def maybe_override_min_sdk_version(min_sdk_version, override_min_sdk_version):
18    """
19    Override a min_sdk_version with another if higher.
20
21    Normalizes string codenames to API ints for direct comparisons.
22
23    Args:
24      min_sdk_version: The min_sdk_version to potentially be overridden, as a string.
25        Can be "current", or a number.
26      override_min_sdk_version: The version to potentially override min_sdk_version with, as a string.
27        Can be a number, of a known api level codename.
28    Returns:
29      Either min_sdk_version or override_min_sdk_version, converted to a string representation of a number.
30    """
31    if min_sdk_version == "current":
32        min_sdk_version = "10000"
33    if not str(min_sdk_version).isdigit():
34        fail("%s must only contain digits." % min_sdk_version)
35
36    min_api_level = int(min_sdk_version)
37
38    if str(override_min_sdk_version).isdigit():
39        override_api_level = int(override_min_sdk_version)
40    else:
41        override_api_level = api.api_levels.get(override_min_sdk_version, -1)
42
43    # Only override version numbers upwards.
44    if override_api_level > min_api_level:
45        min_api_level = override_api_level
46
47    return str(min_api_level)
48