aboutsummaryrefslogtreecommitdiff
path: root/rules/apex/sdk_versions.bzl
blob: c81b3d0d8b8d340d03a2ccecd9d3af47db3cca58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Copyright (C) 2023 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//build/bazel/rules/common:api.bzl", "api")

def maybe_override_min_sdk_version(min_sdk_version, override_min_sdk_version):
    """
    Override a min_sdk_version with another if higher.

    Normalizes string codenames to API ints for direct comparisons.

    Args:
      min_sdk_version: The min_sdk_version to potentially be overridden, as a string.
        Can be "current", or a number.
      override_min_sdk_version: The version to potentially override min_sdk_version with, as a string.
        Can be a number, of a known api level codename.
    Returns:
      Either min_sdk_version or override_min_sdk_version, converted to a string representation of a number.
    """
    if min_sdk_version == "current":
        min_sdk_version = "10000"
    if not str(min_sdk_version).isdigit():
        fail("%s must only contain digits." % min_sdk_version)

    min_api_level = int(min_sdk_version)

    if str(override_min_sdk_version).isdigit():
        override_api_level = int(override_min_sdk_version)
    else:
        override_api_level = api.api_levels.get(override_min_sdk_version, -1)

    # Only override version numbers upwards.
    if override_api_level > min_api_level:
        min_api_level = override_api_level

    return str(min_api_level)