xref: /aosp_15_r20/build/soong/cc/api_level.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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
15package cc
16
17import (
18	"fmt"
19
20	"android/soong/android"
21)
22
23// MinApiLevelForArch returns the ApiLevel for the Android version that
24// first supported the architecture.
25func MinApiForArch(ctx android.EarlyModuleContext,
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
33	case android.Riscv64:
34		return android.FutureApiLevel
35	default:
36		panic(fmt.Errorf("Unknown arch %q", arch))
37	}
38}
39
40// Native API levels cannot be less than the MinApiLevelForArch. This function
41// sets the lower bound of the API level with the MinApiLevelForArch.
42func nativeClampedApiLevel(ctx android.BaseModuleContext,
43	apiLevel android.ApiLevel) android.ApiLevel {
44
45	min := MinApiForArch(ctx, ctx.Arch().ArchType)
46
47	if apiLevel.LessThan(min) {
48		return min
49	}
50
51	return apiLevel
52}
53
54func nativeApiLevelFromUser(ctx android.BaseModuleContext,
55	raw string) (android.ApiLevel, error) {
56
57	if raw == "minimum" {
58		return MinApiForArch(ctx, ctx.Arch().ArchType), nil
59	}
60
61	value, err := android.ApiLevelFromUser(ctx, raw)
62	if err != nil {
63		return android.NoneApiLevel, err
64	}
65
66	return nativeClampedApiLevel(ctx, value), nil
67}
68
69func nativeApiLevelOrPanic(ctx android.BaseModuleContext,
70	raw string) android.ApiLevel {
71
72	value, err := nativeApiLevelFromUser(ctx, raw)
73	if err != nil {
74		panic(err.Error())
75	}
76	return value
77}
78