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.
14load("//build/bazel/rules/common:api.bzl", "api", "api_from_product")
15
16visibility("private")
17
18# Starlark implementation of shouldReturnFinalOrFutureInt from build/soong/java/android_manifest.go
19# TODO: b/300916781 - In Soong this also returns true when the module is an MTS test.
20def _should_return_future_int(
21        target_sdk_version,
22        platform_sdk_variables,
23        has_unbundled_build_apps):
24    if platform_sdk_variables.platform_sdk_final:
25        return False
26    return api_from_product(platform_sdk_variables).is_preview(target_sdk_version) and has_unbundled_build_apps
27
28# Starlark implementation of TargetSdkVersionForManifestFixer from build/soong/java/android_manifest.go
29def _target_sdk_version_for_manifest_fixer(
30        target_sdk_version,
31        platform_sdk_variables,
32        has_unbundled_build_apps):
33    if _should_return_future_int(
34        target_sdk_version = target_sdk_version,
35        platform_sdk_variables = platform_sdk_variables,
36        has_unbundled_build_apps = has_unbundled_build_apps,
37    ):
38        return str(api.FUTURE_API_LEVEL)
39    return api_from_product(platform_sdk_variables).effective_version_string(target_sdk_version)
40
41manifest_fixer_internal = struct(
42    target_sdk_version_for_manifest_fixer = _target_sdk_version_for_manifest_fixer,
43)
44