api_level.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. package cc
  15. import (
  16. "fmt"
  17. "android/soong/android"
  18. )
  19. // MinApiLevelForArch returns the ApiLevel for the Android version that
  20. // first supported the architecture.
  21. func MinApiForArch(ctx android.EarlyModuleContext,
  22. arch android.ArchType) android.ApiLevel {
  23. switch arch {
  24. case android.Arm, android.X86:
  25. return ctx.Config().MinSupportedSdkVersion()
  26. case android.Arm64, android.X86_64:
  27. return android.FirstLp64Version
  28. case android.Riscv64:
  29. return android.FutureApiLevel
  30. default:
  31. panic(fmt.Errorf("Unknown arch %q", arch))
  32. }
  33. }
  34. func nativeApiLevelFromUser(ctx android.BaseModuleContext,
  35. raw string) (android.ApiLevel, error) {
  36. min := MinApiForArch(ctx, ctx.Arch().ArchType)
  37. if raw == "minimum" {
  38. return min, nil
  39. }
  40. value, err := android.ApiLevelFromUser(ctx, raw)
  41. if err != nil {
  42. return android.NoneApiLevel, err
  43. }
  44. if value.LessThan(min) {
  45. return min, nil
  46. }
  47. return value, nil
  48. }
  49. func nativeApiLevelOrPanic(ctx android.BaseModuleContext,
  50. raw string) android.ApiLevel {
  51. value, err := nativeApiLevelFromUser(ctx, raw)
  52. if err != nil {
  53. panic(err.Error())
  54. }
  55. return value
  56. }