blob: f8ac44a4727766c82d7057b11d116f819b0d9d93 [file] [log] [blame]
Jiyong Parkf1691d22021-03-29 20:11:58 +09001// Copyright 2021 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 android
16
17import (
18 "fmt"
19 "strconv"
20 "strings"
21)
22
23type SdkContext interface {
24 // SdkVersion returns SdkSpec that corresponds to the sdk_version property of the current module
Jiyong Park92315372021-04-02 08:45:46 +090025 SdkVersion(ctx EarlyModuleContext) SdkSpec
Jiyong Parkf1691d22021-03-29 20:11:58 +090026 // SystemModules returns the system_modules property of the current module, or an empty string if it is not set.
27 SystemModules() string
28 // MinSdkVersion returns SdkSpec that corresponds to the min_sdk_version property of the current module,
29 // or from sdk_version if it is not set.
Jiyong Park92315372021-04-02 08:45:46 +090030 MinSdkVersion(ctx EarlyModuleContext) SdkSpec
William Loh5a082f92022-05-17 20:21:50 +000031 // ReplaceMaxSdkVersionPlaceholder returns SdkSpec to replace the maxSdkVersion property of permission and
32 // uses-permission tags if it is set.
33 ReplaceMaxSdkVersionPlaceholder(ctx EarlyModuleContext) SdkSpec
Jiyong Parkf1691d22021-03-29 20:11:58 +090034 // TargetSdkVersion returns the SdkSpec that corresponds to the target_sdk_version property of the current module,
35 // or from sdk_version if it is not set.
Jiyong Park92315372021-04-02 08:45:46 +090036 TargetSdkVersion(ctx EarlyModuleContext) SdkSpec
Jiyong Parkf1691d22021-03-29 20:11:58 +090037}
38
39// SdkKind represents a particular category of an SDK spec like public, system, test, etc.
40type SdkKind int
41
42const (
43 SdkInvalid SdkKind = iota
44 SdkNone
45 SdkCore
46 SdkCorePlatform
Spandan Das0b555e32022-11-28 18:48:51 +000047 SdkIntraCore // API surface provided by one core module to another
Jiyong Parkf1691d22021-03-29 20:11:58 +090048 SdkPublic
49 SdkSystem
50 SdkTest
51 SdkModule
52 SdkSystemServer
53 SdkPrivate
Spandan Das4ac2aed2022-12-28 01:54:29 +000054 SdkToolchain // API surface provided by ART to compile other API domains
Jiyong Parkf1691d22021-03-29 20:11:58 +090055)
56
57// String returns the string representation of this SdkKind
58func (k SdkKind) String() string {
59 switch k {
60 case SdkPrivate:
61 return "private"
62 case SdkNone:
63 return "none"
64 case SdkPublic:
65 return "public"
66 case SdkSystem:
67 return "system"
68 case SdkTest:
69 return "test"
70 case SdkCore:
71 return "core"
72 case SdkCorePlatform:
73 return "core_platform"
Spandan Das0b555e32022-11-28 18:48:51 +000074 case SdkIntraCore:
75 return "intracore"
Jiyong Parkf1691d22021-03-29 20:11:58 +090076 case SdkModule:
77 return "module-lib"
78 case SdkSystemServer:
79 return "system-server"
Spandan Das4ac2aed2022-12-28 01:54:29 +000080 case SdkToolchain:
81 return "toolchain"
Jiyong Parkf1691d22021-03-29 20:11:58 +090082 default:
83 return "invalid"
84 }
85}
86
Jiyong Parkf1691d22021-03-29 20:11:58 +090087// SdkSpec represents the kind and the version of an SDK for a module to build against
88type SdkSpec struct {
Jiyong Park54105c42021-03-31 18:17:53 +090089 Kind SdkKind
90 ApiLevel ApiLevel
91 Raw string
Jiyong Parkf1691d22021-03-29 20:11:58 +090092}
93
94func (s SdkSpec) String() string {
Jiyong Park54105c42021-03-31 18:17:53 +090095 return fmt.Sprintf("%s_%s", s.Kind, s.ApiLevel)
Jiyong Parkf1691d22021-03-29 20:11:58 +090096}
97
98// Valid checks if this SdkSpec is well-formed. Note however that true doesn't mean that the
99// specified SDK actually exists.
100func (s SdkSpec) Valid() bool {
101 return s.Kind != SdkInvalid
102}
103
104// Specified checks if this SdkSpec is well-formed and is not "".
105func (s SdkSpec) Specified() bool {
106 return s.Valid() && s.Kind != SdkPrivate
107}
108
109// whether the API surface is managed and versioned, i.e. has .txt file that
110// get frozen on SDK freeze and changes get reviewed by API council.
111func (s SdkSpec) Stable() bool {
112 if !s.Specified() {
113 return false
114 }
115 switch s.Kind {
116 case SdkNone:
117 // there is nothing to manage and version in this case; de facto stable API.
118 return true
119 case SdkCore, SdkPublic, SdkSystem, SdkModule, SdkSystemServer:
120 return true
121 case SdkCorePlatform, SdkTest, SdkPrivate:
122 return false
123 default:
124 panic(fmt.Errorf("unknown SdkKind=%v", s.Kind))
125 }
126 return false
127}
128
satayev0ee2f912021-12-01 17:39:48 +0000129// PrebuiltSdkAvailableForUnbundledBuild tells whether this SdkSpec can have a prebuilt SDK
Jiyong Parkf1691d22021-03-29 20:11:58 +0900130// that can be used for unbundled builds.
131func (s SdkSpec) PrebuiltSdkAvailableForUnbundledBuild() bool {
132 // "", "none", and "core_platform" are not available for unbundled build
133 // as we don't/can't have prebuilt stub for the versions
134 return s.Kind != SdkPrivate && s.Kind != SdkNone && s.Kind != SdkCorePlatform
135}
136
137func (s SdkSpec) ForVendorPartition(ctx EarlyModuleContext) SdkSpec {
138 // If BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES has a numeric value,
139 // use it instead of "current" for the vendor partition.
140 currentSdkVersion := ctx.DeviceConfig().CurrentApiLevelForVendorModules()
141 if currentSdkVersion == "current" {
142 return s
143 }
144
145 if s.Kind == SdkPublic || s.Kind == SdkSystem {
Jiyong Park54105c42021-03-31 18:17:53 +0900146 if s.ApiLevel.IsCurrent() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900147 if i, err := strconv.Atoi(currentSdkVersion); err == nil {
Jiyong Park54105c42021-03-31 18:17:53 +0900148 apiLevel := uncheckedFinalApiLevel(i)
149 return SdkSpec{s.Kind, apiLevel, s.Raw}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900150 }
151 panic(fmt.Errorf("BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES must be either \"current\" or a number, but was %q", currentSdkVersion))
152 }
153 }
154 return s
155}
156
157// UsePrebuilt determines whether prebuilt SDK should be used for this SdkSpec with the given context.
158func (s SdkSpec) UsePrebuilt(ctx EarlyModuleContext) bool {
Jiyong Parkc7022042021-04-15 16:53:05 +0900159 switch s {
160 case SdkSpecNone, SdkSpecCorePlatform, SdkSpecPrivate:
161 return false
162 }
163
Jiyong Park54105c42021-03-31 18:17:53 +0900164 if s.ApiLevel.IsCurrent() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900165 // "current" can be built from source and be from prebuilt SDK
166 return ctx.Config().AlwaysUsePrebuiltSdks()
Jiyong Park54105c42021-03-31 18:17:53 +0900167 } else if !s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900168 // validation check
Paul Duffin12e311d2021-10-28 17:42:16 +0100169 if s.Kind != SdkPublic && s.Kind != SdkSystem && s.Kind != SdkTest && s.Kind != SdkModule && s.Kind != SdkSystemServer {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900170 panic(fmt.Errorf("prebuilt SDK is not not available for SdkKind=%q", s.Kind))
171 return false
172 }
173 // numbered SDKs are always from prebuilt
174 return true
175 }
Jiyong Parkf1691d22021-03-29 20:11:58 +0900176 return false
177}
178
Jiyong Park54105c42021-03-31 18:17:53 +0900179// EffectiveVersion converts an SdkSpec into the concrete ApiLevel that the module should use. For
180// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns
181// FutureApiLevel(10000).
182func (s SdkSpec) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900183 if !s.Valid() {
Jiyong Park54105c42021-03-31 18:17:53 +0900184 return s.ApiLevel, fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900185 }
186
187 if ctx.DeviceSpecific() || ctx.SocSpecific() {
188 s = s.ForVendorPartition(ctx)
189 }
Spandan Das57800392023-01-05 01:03:47 +0000190 return s.ApiLevel.EffectiveVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900191}
192
193// EffectiveVersionString converts an SdkSpec into the concrete version string that the module
194// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
195// it returns the codename (P, Q, R, etc.)
196func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) {
Jiyong Park54105c42021-03-31 18:17:53 +0900197 if !s.Valid() {
198 return s.ApiLevel.String(), fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900199 }
Jiyong Park54105c42021-03-31 18:17:53 +0900200
201 if ctx.DeviceSpecific() || ctx.SocSpecific() {
202 s = s.ForVendorPartition(ctx)
203 }
Spandan Das57800392023-01-05 01:03:47 +0000204 return s.ApiLevel.EffectiveVersionString(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900205}
206
Jiyong Park92315372021-04-02 08:45:46 +0900207var (
Jiyong Parkc7022042021-04-15 16:53:05 +0900208 SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"}
Spandan Das7ee04612023-03-02 23:36:39 +0000209 SdkSpecPrivate = SdkSpec{SdkPrivate, PrivateApiLevel, ""}
Jiyong Parkc7022042021-04-15 16:53:05 +0900210 SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"}
Jiyong Park92315372021-04-02 08:45:46 +0900211)
212
213func SdkSpecFrom(ctx EarlyModuleContext, str string) SdkSpec {
satayev0ee2f912021-12-01 17:39:48 +0000214 return SdkSpecFromWithConfig(ctx.Config(), str)
215}
216
217func SdkSpecFromWithConfig(config Config, str string) SdkSpec {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900218 switch str {
219 // special cases first
220 case "":
Jiyong Park92315372021-04-02 08:45:46 +0900221 return SdkSpecPrivate
Jiyong Parkf1691d22021-03-29 20:11:58 +0900222 case "none":
Jiyong Park92315372021-04-02 08:45:46 +0900223 return SdkSpecNone
Jiyong Parkf1691d22021-03-29 20:11:58 +0900224 case "core_platform":
Jiyong Park92315372021-04-02 08:45:46 +0900225 return SdkSpecCorePlatform
Jiyong Parkf1691d22021-03-29 20:11:58 +0900226 default:
227 // the syntax is [kind_]version
228 sep := strings.LastIndex(str, "_")
229
230 var kindString string
231 if sep == 0 {
Spandan Das7ee04612023-03-02 23:36:39 +0000232 return SdkSpec{SdkInvalid, NewInvalidApiLevel(str), str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900233 } else if sep == -1 {
234 kindString = ""
235 } else {
236 kindString = str[0:sep]
237 }
238 versionString := str[sep+1 : len(str)]
239
240 var kind SdkKind
241 switch kindString {
242 case "":
243 kind = SdkPublic
244 case "core":
245 kind = SdkCore
246 case "system":
247 kind = SdkSystem
248 case "test":
249 kind = SdkTest
250 case "module":
251 kind = SdkModule
252 case "system_server":
253 kind = SdkSystemServer
254 default:
Jiyong Park54105c42021-03-31 18:17:53 +0900255 return SdkSpec{SdkInvalid, NoneApiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900256 }
257
satayev0ee2f912021-12-01 17:39:48 +0000258 apiLevel, err := ApiLevelFromUserWithConfig(config, versionString)
Jiyong Park92315372021-04-02 08:45:46 +0900259 if err != nil {
Spandan Das7ee04612023-03-02 23:36:39 +0000260 return SdkSpec{SdkInvalid, NewInvalidApiLevel(versionString), str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900261 }
Jiyong Park54105c42021-03-31 18:17:53 +0900262 return SdkSpec{kind, apiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900263 }
264}
265
266func (s SdkSpec) ValidateSystemSdk(ctx EarlyModuleContext) bool {
267 // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module)
268 // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29,
269 // sdk_version of the modules in vendor/product that use system sdk must be either system_28, system_29 or system_current
Jiyong Park54105c42021-03-31 18:17:53 +0900270 if s.Kind != SdkSystem || s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900271 return true
272 }
273 allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions()
274 if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
275 systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions()
276 if len(systemSdkVersions) > 0 {
277 allowedVersions = systemSdkVersions
278 }
279 }
Jiyong Park54105c42021-03-31 18:17:53 +0900280 if len(allowedVersions) > 0 && !InList(s.ApiLevel.String(), allowedVersions) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900281 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
282 s.Raw, allowedVersions)
283 return false
284 }
285 return true
286}