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		apiLevel, err := android.ApiLevelFromUser(ctx, "VanillaIceCream")
35		if err != nil {
36			panic(err)
37		}
38		return apiLevel
39	default:
40		panic(fmt.Errorf("Unknown arch %q", arch))
41	}
42}
43
44func nativeApiLevelFromUser(ctx android.BaseModuleContext,
45	raw string) (android.ApiLevel, error) {
46
47	min := MinApiForArch(ctx, ctx.Arch().ArchType)
48	if raw == "minimum" {
49		return min, nil
50	}
51
52	value, err := android.ApiLevelFromUser(ctx, raw)
53	if err != nil {
54		return android.NoneApiLevel, err
55	}
56
57	if value.LessThan(min) {
58		return min, nil
59	}
60
61	return value, nil
62}
63
64func nativeApiLevelOrPanic(ctx android.BaseModuleContext,
65	raw string) android.ApiLevel {
66	value, err := nativeApiLevelFromUser(ctx, raw)
67	if err != nil {
68		panic(err.Error())
69	}
70	return value
71}
72