Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
Colin Cross | bb137a3 | 2023-01-26 09:54:42 -0800 | [diff] [blame] | 23 | // MinApiLevelForArch returns the ApiLevel for the Android version that |
| 24 | // first supported the architecture. |
| 25 | func MinApiForArch(ctx android.EarlyModuleContext, |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 26 | 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 Cross | a2aaa2f | 2022-10-03 12:41:50 -0700 | [diff] [blame] | 33 | case android.Riscv64: |
Prashanth Swaminathan | 81532c2 | 2023-07-26 09:43:31 -0700 | [diff] [blame] | 34 | apiLevel, err := android.ApiLevelFromUser(ctx, "VanillaIceCream") |
| 35 | if err != nil { |
| 36 | panic(err) |
| 37 | } |
| 38 | return apiLevel |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 39 | default: |
| 40 | panic(fmt.Errorf("Unknown arch %q", arch)) |
| 41 | } |
| 42 | } |
| 43 | |
Justin Yun | 22abf21 | 2024-10-10 16:22:09 +0900 | [diff] [blame] | 44 | // Native API levels cannot be less than the MinApiLevelForArch. This function |
| 45 | // sets the lower bound of the API level with the MinApiLevelForArch. |
| 46 | func nativeClampedApiLevel(ctx android.BaseModuleContext, |
| 47 | apiLevel android.ApiLevel) android.ApiLevel { |
| 48 | |
| 49 | min := MinApiForArch(ctx, ctx.Arch().ArchType) |
| 50 | |
| 51 | if apiLevel.LessThan(min) { |
| 52 | return min |
| 53 | } |
| 54 | |
| 55 | return apiLevel |
| 56 | } |
| 57 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 58 | func nativeApiLevelFromUser(ctx android.BaseModuleContext, |
| 59 | raw string) (android.ApiLevel, error) { |
| 60 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 61 | if raw == "minimum" { |
Justin Yun | 22abf21 | 2024-10-10 16:22:09 +0900 | [diff] [blame] | 62 | return MinApiForArch(ctx, ctx.Arch().ArchType), nil |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | value, err := android.ApiLevelFromUser(ctx, raw) |
| 66 | if err != nil { |
| 67 | return android.NoneApiLevel, err |
| 68 | } |
| 69 | |
Justin Yun | 22abf21 | 2024-10-10 16:22:09 +0900 | [diff] [blame] | 70 | return nativeClampedApiLevel(ctx, value), nil |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 73 | func nativeApiLevelOrPanic(ctx android.BaseModuleContext, |
| 74 | raw string) android.ApiLevel { |
Justin Yun | 22abf21 | 2024-10-10 16:22:09 +0900 | [diff] [blame] | 75 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 76 | value, err := nativeApiLevelFromUser(ctx, raw) |
| 77 | if err != nil { |
| 78 | panic(err.Error()) |
| 79 | } |
| 80 | return value |
| 81 | } |