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
17# Only scopes that are available in prebuilts (and "none") are listed
18# here for now, but the list should eventually match Soong's SdkKind
19# enum.
20_KIND_PUBLIC = "public"
21_KIND_SYSTEM = "system"
22_KIND_TEST = "test"
23_KIND_SYSTEM_SERVER = "system_server"
24_KIND_MODULE = "module"
25_KIND_CORE = "core"
26_KIND_NONE = "none"
27_KIND_CORE_PLATFORM = "core_platform"
28_ALL_KINDS = [
29    _KIND_PUBLIC,
30    _KIND_SYSTEM,
31    _KIND_TEST,
32    _KIND_SYSTEM_SERVER,
33    _KIND_MODULE,
34    _KIND_CORE,
35    _KIND_NONE,
36    # _KIND_CORE_PLATFORM, # TODO: Add when core_plaform is supported in b builds
37]
38
39# Starlark implementation of SdkSpecFrom at https://cs.android.com/android/platform/build/soong/+/master:android/sdk_version.go;l=248-299;drc=69f4218c4feaeca953237cd9e76a9a8cc423d3e3.
40def _sdk_spec_from(sdk_version):
41    """Parses an sdk_version string into kind and api_level.
42
43    Args:
44        sdk_version: a string to specify which SDK version to depend on.
45            - The empty string maps to the full set of private APIs and is currently unsupported.
46            - "core_platform" maps to the module scope of the core system modules.
47            - "none" maps to no SDK (used for bootstrapping the core).
48            - Otherwise, the format is "{kind}_{api_level}", where kind must be one of the strings
49              in ALL_KINDS, and api_level is either an integer, and android codename, or "current".
50              The default kind is "public", and can be omitted by simply providing "{api_level}".
51
52    Returns:
53        A struct with a kind attribute set to one of the string in ALL_KINDS, and an api_level
54        attribute as returned by api.bzl's parse_api_level_from_version.
55    """
56    if not sdk_version:
57        fail("Only prebuilt SDK versions are available, sdk_version must be specified and non-empty.")
58    if sdk_version == "core_platform":
59        fail("Only prebuilt SDK versions are available, sdk_version core_platform is not yet supported.")
60    if sdk_version == "none":
61        return struct(kind = _KIND_NONE, api_level = api.NONE_API_LEVEL, _api_level_string = "(no version)")
62    if type(sdk_version) != type(""):
63        fail("sdk_version must be a string")
64    sep_index = sdk_version.rfind("_")
65    api_level_string = sdk_version if sep_index < 0 else sdk_version[sep_index + 1:]
66    api_level = api.parse_api_level_from_version(api_level_string)
67    kind = _KIND_PUBLIC if sep_index == -1 else sdk_version[:sep_index]
68    if kind not in _ALL_KINDS:
69        fail("kind %s parsed from sdk_version %s must be one of %s" % (
70            kind,
71            sdk_version,
72            ",".join(_ALL_KINDS),
73        ))
74    return struct(kind = kind, api_level = api_level, _api_level_string = api_level_string)
75
76def _api_level_string_with_fallback(api_level_string, sdk_version):
77    return api_level_string if api_level_string else _sdk_spec_from(sdk_version)._api_level_string
78
79sdk_version = struct(
80    KIND_PUBLIC = _KIND_PUBLIC,
81    KIND_SYSTEM = _KIND_SYSTEM,
82    KIND_TEST = _KIND_TEST,
83    KIND_SYSTEM_SERVER = _KIND_SYSTEM_SERVER,
84    KIND_MODULE = _KIND_MODULE,
85    KIND_CORE = _KIND_CORE,
86    KIND_NONE = _KIND_NONE,
87    KIND_CORE_PLATFORM = _KIND_CORE_PLATFORM,
88    ALL_KINDS = _ALL_KINDS,
89    api_level_string_with_fallback = _api_level_string_with_fallback,
90    sdk_spec_from = _sdk_spec_from,
91)
92