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