blob: a5571f31f2e628a58f50bbc65ea7cd594dcc0e28 [file] [log] [blame]
Dan Albert1a246272020-07-06 14:49:35 -07001// Copyright 2020 Google Inc. All rights reserved.
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
15package cc
16
17import (
18 "fmt"
19
20 "android/soong/android"
21)
22
Colin Crossbb137a32023-01-26 09:54:42 -080023// MinApiLevelForArch returns the ApiLevel for the Android version that
24// first supported the architecture.
25func MinApiForArch(ctx android.EarlyModuleContext,
Dan Albert1a246272020-07-06 14:49:35 -070026 arch android.ArchType) android.ApiLevel {
27
28 switch arch {
29 case android.Arm, android.X86:
30 return ctx.Config().MinSupportedSdkVersion()
31 case android.Arm64, android.X86_64:
32 return android.FirstLp64Version
Colin Crossa2aaa2f2022-10-03 12:41:50 -070033 case android.Riscv64:
34 return android.FutureApiLevel
Dan Albert1a246272020-07-06 14:49:35 -070035 default:
36 panic(fmt.Errorf("Unknown arch %q", arch))
37 }
38}
39
40func nativeApiLevelFromUser(ctx android.BaseModuleContext,
41 raw string) (android.ApiLevel, error) {
42
Colin Crossbb137a32023-01-26 09:54:42 -080043 min := MinApiForArch(ctx, ctx.Arch().ArchType)
Dan Albert1a246272020-07-06 14:49:35 -070044 if raw == "minimum" {
45 return min, nil
46 }
47
48 value, err := android.ApiLevelFromUser(ctx, raw)
49 if err != nil {
50 return android.NoneApiLevel, err
51 }
52
53 if value.LessThan(min) {
54 return min, nil
55 }
56
57 return value, nil
58}
59
Dan Albert1a246272020-07-06 14:49:35 -070060func nativeApiLevelOrPanic(ctx android.BaseModuleContext,
61 raw string) android.ApiLevel {
62 value, err := nativeApiLevelFromUser(ctx, raw)
63 if err != nil {
64 panic(err.Error())
65 }
66 return value
67}