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("@soong_injection//api_levels:platform_versions.bzl", "platform_versions") 16load("//build/bazel/rules/common:api.bzl", "api", "api_from_product") 17load(":manifest_fixer_internal.bzl", _internal = "manifest_fixer_internal") 18 19# TODO(b/300428335): access these variables in a transition friendly way. 20_PLATFORM_SDK_VERSION = platform_versions.platform_sdk_version 21_PLATFORM_SDK_CODENAME = platform_versions.platform_sdk_codename 22_PLATFORM_VERSION_ACTIVE_CODENAMES = platform_versions.platform_version_active_codenames 23 24# Starlark implementation of TargetSdkVersionForManifestFixer from build/soong/java/android_manifest.go 25def _target_sdk_version_for_manifest_fixer( 26 target_sdk_version, 27 platform_sdk_final, 28 has_unbundled_build_apps): 29 platform_sdk_variables = struct( 30 platform_sdk_final = platform_sdk_final, 31 platform_sdk_version = _PLATFORM_SDK_VERSION, 32 platform_sdk_codename = _PLATFORM_SDK_CODENAME, 33 platform_version_active_codenames = _PLATFORM_VERSION_ACTIVE_CODENAMES, 34 ) 35 return _internal.target_sdk_version_for_manifest_fixer( 36 target_sdk_version = target_sdk_version, 37 has_unbundled_build_apps = has_unbundled_build_apps, 38 platform_sdk_variables = platform_sdk_variables, 39 ) 40 41# TODO: b/301430823 - Only pass ctx.actions to limit the scope of what this function can access. 42def _fix( 43 ctx, 44 manifest_fixer, 45 in_manifest, 46 out_manifest, 47 mnemonic = "FixAndroidManifest", 48 test_only = None, 49 min_sdk_version = None, 50 target_sdk_version = None): 51 args = ctx.actions.args() 52 if test_only: 53 args.add("--test-only") 54 if min_sdk_version: 55 args.add("--minSdkVersion", min_sdk_version) 56 if target_sdk_version: 57 args.add("--targetSdkVersion", target_sdk_version) 58 if min_sdk_version or target_sdk_version: 59 args.add("--raise-min-sdk-version") 60 args.add(in_manifest) 61 args.add(out_manifest) 62 ctx.actions.run( 63 inputs = [in_manifest], 64 outputs = [out_manifest], 65 executable = manifest_fixer, 66 arguments = [args], 67 mnemonic = mnemonic, 68 ) 69 70manifest_fixer = struct( 71 fix = _fix, 72 target_sdk_version_for_manifest_fixer = _target_sdk_version_for_manifest_fixer, 73) 74