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
23func minApiForArch(ctx android.BaseModuleContext,
24	arch android.ArchType) android.ApiLevel {
25
26	switch arch {
27	case android.Arm, android.X86:
28		return ctx.Config().MinSupportedSdkVersion()
29	case android.Arm64, android.X86_64:
30		return android.FirstLp64Version
31	default:
32		panic(fmt.Errorf("Unknown arch %q", arch))
33	}
34}
35
36func nativeApiLevelFromUser(ctx android.BaseModuleContext,
37	raw string) (android.ApiLevel, error) {
38
39	min := minApiForArch(ctx, ctx.Arch().ArchType)
40	if raw == "minimum" {
41		return min, nil
42	}
43
44	value, err := android.ApiLevelFromUser(ctx, raw)
45	if err != nil {
46		return android.NoneApiLevel, err
47	}
48
49	if value.LessThan(min) {
50		return min, nil
51	}
52
53	return value, nil
54}
55
56func nativeApiLevelOrPanic(ctx android.BaseModuleContext,
57	raw string) android.ApiLevel {
58	value, err := nativeApiLevelFromUser(ctx, raw)
59	if err != nil {
60		panic(err.Error())
61	}
62	return value
63}
64