blob: 8953eaecd4b5dfc40d3847a41480c94304b03e1e [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 }
Jiyong Park54105c42021-03-31 18:17:53 +0900190 if !s.ApiLevel.IsPreview() {
191 return s.ApiLevel, nil
Jiyong Parkf1691d22021-03-29 20:11:58 +0900192 }
Jiyong Park54105c42021-03-31 18:17:53 +0900193 ret := ctx.Config().DefaultAppTargetSdk(ctx)
194 if ret.IsPreview() {
195 return FutureApiLevel, nil
196 }
197 return ret, nil
Jiyong Parkf1691d22021-03-29 20:11:58 +0900198}
199
200// EffectiveVersionString converts an SdkSpec into the concrete version string that the module
201// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
202// it returns the codename (P, Q, R, etc.)
203func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) {
Jiyong Park54105c42021-03-31 18:17:53 +0900204 if !s.Valid() {
205 return s.ApiLevel.String(), fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900206 }
Jiyong Park54105c42021-03-31 18:17:53 +0900207
208 if ctx.DeviceSpecific() || ctx.SocSpecific() {
209 s = s.ForVendorPartition(ctx)
210 }
211 if !s.ApiLevel.IsPreview() {
212 return s.ApiLevel.String(), nil
213 }
214 return ctx.Config().DefaultAppTargetSdk(ctx).String(), nil
Jiyong Parkf1691d22021-03-29 20:11:58 +0900215}
216
Jiyong Park92315372021-04-02 08:45:46 +0900217var (
Jiyong Parkc7022042021-04-15 16:53:05 +0900218 SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"}
219 SdkSpecPrivate = SdkSpec{SdkPrivate, FutureApiLevel, ""}
220 SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"}
Jiyong Park92315372021-04-02 08:45:46 +0900221)
222
223func SdkSpecFrom(ctx EarlyModuleContext, str string) SdkSpec {
satayev0ee2f912021-12-01 17:39:48 +0000224 return SdkSpecFromWithConfig(ctx.Config(), str)
225}
226
227func SdkSpecFromWithConfig(config Config, str string) SdkSpec {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900228 switch str {
229 // special cases first
230 case "":
Jiyong Park92315372021-04-02 08:45:46 +0900231 return SdkSpecPrivate
Jiyong Parkf1691d22021-03-29 20:11:58 +0900232 case "none":
Jiyong Park92315372021-04-02 08:45:46 +0900233 return SdkSpecNone
Jiyong Parkf1691d22021-03-29 20:11:58 +0900234 case "core_platform":
Jiyong Park92315372021-04-02 08:45:46 +0900235 return SdkSpecCorePlatform
Jiyong Parkf1691d22021-03-29 20:11:58 +0900236 default:
237 // the syntax is [kind_]version
238 sep := strings.LastIndex(str, "_")
239
240 var kindString string
241 if sep == 0 {
Jiyong Park54105c42021-03-31 18:17:53 +0900242 return SdkSpec{SdkInvalid, NoneApiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900243 } else if sep == -1 {
244 kindString = ""
245 } else {
246 kindString = str[0:sep]
247 }
248 versionString := str[sep+1 : len(str)]
249
250 var kind SdkKind
251 switch kindString {
252 case "":
253 kind = SdkPublic
254 case "core":
255 kind = SdkCore
256 case "system":
257 kind = SdkSystem
258 case "test":
259 kind = SdkTest
260 case "module":
261 kind = SdkModule
262 case "system_server":
263 kind = SdkSystemServer
264 default:
Jiyong Park54105c42021-03-31 18:17:53 +0900265 return SdkSpec{SdkInvalid, NoneApiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900266 }
267
satayev0ee2f912021-12-01 17:39:48 +0000268 apiLevel, err := ApiLevelFromUserWithConfig(config, versionString)
Jiyong Park92315372021-04-02 08:45:46 +0900269 if err != nil {
Jiyong Park54105c42021-03-31 18:17:53 +0900270 return SdkSpec{SdkInvalid, apiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900271 }
Jiyong Park54105c42021-03-31 18:17:53 +0900272 return SdkSpec{kind, apiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900273 }
274}
275
276func (s SdkSpec) ValidateSystemSdk(ctx EarlyModuleContext) bool {
277 // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module)
278 // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29,
279 // 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 +0900280 if s.Kind != SdkSystem || s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900281 return true
282 }
283 allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions()
284 if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
285 systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions()
286 if len(systemSdkVersions) > 0 {
287 allowedVersions = systemSdkVersions
288 }
289 }
Jiyong Park54105c42021-03-31 18:17:53 +0900290 if len(allowedVersions) > 0 && !InList(s.ApiLevel.String(), allowedVersions) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900291 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
292 s.Raw, allowedVersions)
293 return false
294 }
295 return true
296}