blob: 2107cbedbe4392683b91053d3bdd89f77c75dafe [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
Spandan Das8c9ae7e2023-03-03 21:20:36 +000028 // MinSdkVersion returns ApiLevel that corresponds to the min_sdk_version property of the current module,
Jiyong Parkf1691d22021-03-29 20:11:58 +090029 // or from sdk_version if it is not set.
Spandan Das8c9ae7e2023-03-03 21:20:36 +000030 MinSdkVersion(ctx EarlyModuleContext) ApiLevel
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
Spandan Das626a8ad2023-03-20 18:52:50 +000087// JavaLibraryName returns the soong module containing the Java APIs of that API surface.
88func (k SdkKind) JavaLibraryName(c Config) string {
89 name := k.defaultJavaLibraryName()
90 return JavaLibraryNameFromText(c, name)
91}
92
93// JavaLibraryNameFromText returns the name of .txt equivalent of a java_library, but does
94// not check if either module exists.
95// TODO: Return .txt (single-tree or multi-tree equivalents) based on config
96func JavaLibraryNameFromText(c Config, name string) string {
97 // This returns the default for now.
98 // TODO: Implement this
99 return name
100}
101
102func (k SdkKind) defaultJavaLibraryName() string {
103 switch k {
104 case SdkPublic:
105 return "android_stubs_current"
106 case SdkSystem:
107 return "android_system_stubs_current"
108 case SdkTest:
109 return "android_test_stubs_current"
110 case SdkCore:
111 return "core.current.stubs"
112 case SdkModule:
113 return "android_module_lib_stubs_current"
114 case SdkSystemServer:
115 return "android_system_server_stubs_current"
116 default:
117 panic(fmt.Errorf("APIs of API surface %v cannot be provided by a single Soong module\n", k))
118 }
119}
120
Jiyong Parkf1691d22021-03-29 20:11:58 +0900121// SdkSpec represents the kind and the version of an SDK for a module to build against
122type SdkSpec struct {
Jiyong Park54105c42021-03-31 18:17:53 +0900123 Kind SdkKind
124 ApiLevel ApiLevel
125 Raw string
Jiyong Parkf1691d22021-03-29 20:11:58 +0900126}
127
128func (s SdkSpec) String() string {
Jiyong Park54105c42021-03-31 18:17:53 +0900129 return fmt.Sprintf("%s_%s", s.Kind, s.ApiLevel)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900130}
131
132// Valid checks if this SdkSpec is well-formed. Note however that true doesn't mean that the
133// specified SDK actually exists.
134func (s SdkSpec) Valid() bool {
135 return s.Kind != SdkInvalid
136}
137
138// Specified checks if this SdkSpec is well-formed and is not "".
139func (s SdkSpec) Specified() bool {
140 return s.Valid() && s.Kind != SdkPrivate
141}
142
143// whether the API surface is managed and versioned, i.e. has .txt file that
144// get frozen on SDK freeze and changes get reviewed by API council.
145func (s SdkSpec) Stable() bool {
146 if !s.Specified() {
147 return false
148 }
149 switch s.Kind {
150 case SdkNone:
151 // there is nothing to manage and version in this case; de facto stable API.
152 return true
153 case SdkCore, SdkPublic, SdkSystem, SdkModule, SdkSystemServer:
154 return true
155 case SdkCorePlatform, SdkTest, SdkPrivate:
156 return false
157 default:
158 panic(fmt.Errorf("unknown SdkKind=%v", s.Kind))
159 }
160 return false
161}
162
satayev0ee2f912021-12-01 17:39:48 +0000163// PrebuiltSdkAvailableForUnbundledBuild tells whether this SdkSpec can have a prebuilt SDK
Jiyong Parkf1691d22021-03-29 20:11:58 +0900164// that can be used for unbundled builds.
165func (s SdkSpec) PrebuiltSdkAvailableForUnbundledBuild() bool {
166 // "", "none", and "core_platform" are not available for unbundled build
167 // as we don't/can't have prebuilt stub for the versions
168 return s.Kind != SdkPrivate && s.Kind != SdkNone && s.Kind != SdkCorePlatform
169}
170
171func (s SdkSpec) ForVendorPartition(ctx EarlyModuleContext) SdkSpec {
172 // If BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES has a numeric value,
173 // use it instead of "current" for the vendor partition.
174 currentSdkVersion := ctx.DeviceConfig().CurrentApiLevelForVendorModules()
175 if currentSdkVersion == "current" {
176 return s
177 }
178
179 if s.Kind == SdkPublic || s.Kind == SdkSystem {
Jiyong Park54105c42021-03-31 18:17:53 +0900180 if s.ApiLevel.IsCurrent() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900181 if i, err := strconv.Atoi(currentSdkVersion); err == nil {
Jiyong Park54105c42021-03-31 18:17:53 +0900182 apiLevel := uncheckedFinalApiLevel(i)
183 return SdkSpec{s.Kind, apiLevel, s.Raw}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900184 }
185 panic(fmt.Errorf("BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES must be either \"current\" or a number, but was %q", currentSdkVersion))
186 }
187 }
188 return s
189}
190
191// UsePrebuilt determines whether prebuilt SDK should be used for this SdkSpec with the given context.
192func (s SdkSpec) UsePrebuilt(ctx EarlyModuleContext) bool {
Jiyong Parkc7022042021-04-15 16:53:05 +0900193 switch s {
194 case SdkSpecNone, SdkSpecCorePlatform, SdkSpecPrivate:
195 return false
196 }
197
Jiyong Park54105c42021-03-31 18:17:53 +0900198 if s.ApiLevel.IsCurrent() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900199 // "current" can be built from source and be from prebuilt SDK
200 return ctx.Config().AlwaysUsePrebuiltSdks()
Jiyong Park54105c42021-03-31 18:17:53 +0900201 } else if !s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900202 // validation check
Paul Duffin12e311d2021-10-28 17:42:16 +0100203 if s.Kind != SdkPublic && s.Kind != SdkSystem && s.Kind != SdkTest && s.Kind != SdkModule && s.Kind != SdkSystemServer {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900204 panic(fmt.Errorf("prebuilt SDK is not not available for SdkKind=%q", s.Kind))
205 return false
206 }
207 // numbered SDKs are always from prebuilt
208 return true
209 }
Jiyong Parkf1691d22021-03-29 20:11:58 +0900210 return false
211}
212
Jiyong Park54105c42021-03-31 18:17:53 +0900213// EffectiveVersion converts an SdkSpec into the concrete ApiLevel that the module should use. For
214// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns
215// FutureApiLevel(10000).
216func (s SdkSpec) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900217 if !s.Valid() {
Jiyong Park54105c42021-03-31 18:17:53 +0900218 return s.ApiLevel, fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900219 }
220
221 if ctx.DeviceSpecific() || ctx.SocSpecific() {
222 s = s.ForVendorPartition(ctx)
223 }
Spandan Dasdd7057c2023-01-05 01:03:47 +0000224 return s.ApiLevel.EffectiveVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900225}
226
227// EffectiveVersionString converts an SdkSpec into the concrete version string that the module
228// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
229// it returns the codename (P, Q, R, etc.)
230func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) {
Jiyong Park54105c42021-03-31 18:17:53 +0900231 if !s.Valid() {
232 return s.ApiLevel.String(), fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900233 }
Jiyong Park54105c42021-03-31 18:17:53 +0900234
235 if ctx.DeviceSpecific() || ctx.SocSpecific() {
236 s = s.ForVendorPartition(ctx)
237 }
Spandan Dasdd7057c2023-01-05 01:03:47 +0000238 return s.ApiLevel.EffectiveVersionString(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900239}
240
Jiyong Park92315372021-04-02 08:45:46 +0900241var (
Jiyong Parkc7022042021-04-15 16:53:05 +0900242 SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"}
Spandan Das15da5882023-03-02 23:36:39 +0000243 SdkSpecPrivate = SdkSpec{SdkPrivate, PrivateApiLevel, ""}
Jiyong Parkc7022042021-04-15 16:53:05 +0900244 SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"}
Jiyong Park92315372021-04-02 08:45:46 +0900245)
246
247func SdkSpecFrom(ctx EarlyModuleContext, str string) SdkSpec {
satayev0ee2f912021-12-01 17:39:48 +0000248 return SdkSpecFromWithConfig(ctx.Config(), str)
249}
250
251func SdkSpecFromWithConfig(config Config, str string) SdkSpec {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900252 switch str {
253 // special cases first
254 case "":
Jiyong Park92315372021-04-02 08:45:46 +0900255 return SdkSpecPrivate
Jiyong Parkf1691d22021-03-29 20:11:58 +0900256 case "none":
Jiyong Park92315372021-04-02 08:45:46 +0900257 return SdkSpecNone
Jiyong Parkf1691d22021-03-29 20:11:58 +0900258 case "core_platform":
Jiyong Park92315372021-04-02 08:45:46 +0900259 return SdkSpecCorePlatform
Jiyong Parkf1691d22021-03-29 20:11:58 +0900260 default:
261 // the syntax is [kind_]version
262 sep := strings.LastIndex(str, "_")
263
264 var kindString string
265 if sep == 0 {
Spandan Das15da5882023-03-02 23:36:39 +0000266 return SdkSpec{SdkInvalid, NewInvalidApiLevel(str), str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900267 } else if sep == -1 {
268 kindString = ""
269 } else {
270 kindString = str[0:sep]
271 }
272 versionString := str[sep+1 : len(str)]
273
274 var kind SdkKind
275 switch kindString {
276 case "":
277 kind = SdkPublic
278 case "core":
279 kind = SdkCore
280 case "system":
281 kind = SdkSystem
282 case "test":
283 kind = SdkTest
284 case "module":
285 kind = SdkModule
286 case "system_server":
287 kind = SdkSystemServer
288 default:
Jiyong Park54105c42021-03-31 18:17:53 +0900289 return SdkSpec{SdkInvalid, NoneApiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900290 }
291
satayev0ee2f912021-12-01 17:39:48 +0000292 apiLevel, err := ApiLevelFromUserWithConfig(config, versionString)
Jiyong Park92315372021-04-02 08:45:46 +0900293 if err != nil {
Spandan Das15da5882023-03-02 23:36:39 +0000294 return SdkSpec{SdkInvalid, NewInvalidApiLevel(versionString), str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900295 }
Jiyong Park54105c42021-03-31 18:17:53 +0900296 return SdkSpec{kind, apiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900297 }
298}
299
300func (s SdkSpec) ValidateSystemSdk(ctx EarlyModuleContext) bool {
301 // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module)
302 // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29,
303 // 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 +0900304 if s.Kind != SdkSystem || s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900305 return true
306 }
307 allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions()
308 if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
309 systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions()
310 if len(systemSdkVersions) > 0 {
311 allowedVersions = systemSdkVersions
312 }
313 }
Jiyong Park54105c42021-03-31 18:17:53 +0900314 if len(allowedVersions) > 0 && !InList(s.ApiLevel.String(), allowedVersions) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900315 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
316 s.Raw, allowedVersions)
317 return false
318 }
319 return true
320}