blob: 1fadda0cad85ebe514dba264fda3fa8c13e48b8e [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
Spandan Dasa26eda72023-03-02 00:56:06 +000031 // ReplaceMaxSdkVersionPlaceholder returns Apilevel to replace the maxSdkVersion property of permission and
William Loh5a082f92022-05-17 20:21:50 +000032 // uses-permission tags if it is set.
Spandan Dasa26eda72023-03-02 00:56:06 +000033 ReplaceMaxSdkVersionPlaceholder(ctx EarlyModuleContext) ApiLevel
Spandan Dasca70fc42023-03-01 23:38:49 +000034 // TargetSdkVersion returns the ApiLevel that corresponds to the target_sdk_version property of the current module,
Jiyong Parkf1691d22021-03-29 20:11:58 +090035 // or from sdk_version if it is not set.
Spandan Dasca70fc42023-03-01 23:38:49 +000036 TargetSdkVersion(ctx EarlyModuleContext) ApiLevel
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
Jihoon Kang5b065bb2023-04-05 21:08:08 +000087func (k SdkKind) DefaultJavaLibraryName() string {
Spandan Das626a8ad2023-03-20 18:52:50 +000088 switch k {
89 case SdkPublic:
90 return "android_stubs_current"
91 case SdkSystem:
92 return "android_system_stubs_current"
93 case SdkTest:
94 return "android_test_stubs_current"
95 case SdkCore:
96 return "core.current.stubs"
97 case SdkModule:
98 return "android_module_lib_stubs_current"
99 case SdkSystemServer:
100 return "android_system_server_stubs_current"
101 default:
102 panic(fmt.Errorf("APIs of API surface %v cannot be provided by a single Soong module\n", k))
103 }
104}
105
Jiyong Parkf1691d22021-03-29 20:11:58 +0900106// SdkSpec represents the kind and the version of an SDK for a module to build against
107type SdkSpec struct {
Jiyong Park54105c42021-03-31 18:17:53 +0900108 Kind SdkKind
109 ApiLevel ApiLevel
110 Raw string
Jiyong Parkf1691d22021-03-29 20:11:58 +0900111}
112
113func (s SdkSpec) String() string {
Jiyong Park54105c42021-03-31 18:17:53 +0900114 return fmt.Sprintf("%s_%s", s.Kind, s.ApiLevel)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900115}
116
117// Valid checks if this SdkSpec is well-formed. Note however that true doesn't mean that the
118// specified SDK actually exists.
119func (s SdkSpec) Valid() bool {
120 return s.Kind != SdkInvalid
121}
122
123// Specified checks if this SdkSpec is well-formed and is not "".
124func (s SdkSpec) Specified() bool {
125 return s.Valid() && s.Kind != SdkPrivate
126}
127
128// whether the API surface is managed and versioned, i.e. has .txt file that
129// get frozen on SDK freeze and changes get reviewed by API council.
130func (s SdkSpec) Stable() bool {
131 if !s.Specified() {
132 return false
133 }
134 switch s.Kind {
135 case SdkNone:
136 // there is nothing to manage and version in this case; de facto stable API.
137 return true
138 case SdkCore, SdkPublic, SdkSystem, SdkModule, SdkSystemServer:
139 return true
140 case SdkCorePlatform, SdkTest, SdkPrivate:
141 return false
142 default:
143 panic(fmt.Errorf("unknown SdkKind=%v", s.Kind))
144 }
145 return false
146}
147
satayev0ee2f912021-12-01 17:39:48 +0000148// PrebuiltSdkAvailableForUnbundledBuild tells whether this SdkSpec can have a prebuilt SDK
Jiyong Parkf1691d22021-03-29 20:11:58 +0900149// that can be used for unbundled builds.
150func (s SdkSpec) PrebuiltSdkAvailableForUnbundledBuild() bool {
151 // "", "none", and "core_platform" are not available for unbundled build
152 // as we don't/can't have prebuilt stub for the versions
153 return s.Kind != SdkPrivate && s.Kind != SdkNone && s.Kind != SdkCorePlatform
154}
155
156func (s SdkSpec) ForVendorPartition(ctx EarlyModuleContext) SdkSpec {
157 // If BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES has a numeric value,
158 // use it instead of "current" for the vendor partition.
159 currentSdkVersion := ctx.DeviceConfig().CurrentApiLevelForVendorModules()
160 if currentSdkVersion == "current" {
161 return s
162 }
163
164 if s.Kind == SdkPublic || s.Kind == SdkSystem {
Jiyong Park54105c42021-03-31 18:17:53 +0900165 if s.ApiLevel.IsCurrent() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900166 if i, err := strconv.Atoi(currentSdkVersion); err == nil {
Jiyong Park54105c42021-03-31 18:17:53 +0900167 apiLevel := uncheckedFinalApiLevel(i)
168 return SdkSpec{s.Kind, apiLevel, s.Raw}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900169 }
170 panic(fmt.Errorf("BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES must be either \"current\" or a number, but was %q", currentSdkVersion))
171 }
172 }
173 return s
174}
175
176// UsePrebuilt determines whether prebuilt SDK should be used for this SdkSpec with the given context.
177func (s SdkSpec) UsePrebuilt(ctx EarlyModuleContext) bool {
Jiyong Parkc7022042021-04-15 16:53:05 +0900178 switch s {
179 case SdkSpecNone, SdkSpecCorePlatform, SdkSpecPrivate:
180 return false
181 }
182
Jiyong Park54105c42021-03-31 18:17:53 +0900183 if s.ApiLevel.IsCurrent() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900184 // "current" can be built from source and be from prebuilt SDK
185 return ctx.Config().AlwaysUsePrebuiltSdks()
Jiyong Park54105c42021-03-31 18:17:53 +0900186 } else if !s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900187 // validation check
Paul Duffin12e311d2021-10-28 17:42:16 +0100188 if s.Kind != SdkPublic && s.Kind != SdkSystem && s.Kind != SdkTest && s.Kind != SdkModule && s.Kind != SdkSystemServer {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900189 panic(fmt.Errorf("prebuilt SDK is not not available for SdkKind=%q", s.Kind))
190 return false
191 }
192 // numbered SDKs are always from prebuilt
193 return true
194 }
Jiyong Parkf1691d22021-03-29 20:11:58 +0900195 return false
196}
197
Jiyong Park54105c42021-03-31 18:17:53 +0900198// EffectiveVersion converts an SdkSpec into the concrete ApiLevel that the module should use. For
199// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns
200// FutureApiLevel(10000).
201func (s SdkSpec) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900202 if !s.Valid() {
Jiyong Park54105c42021-03-31 18:17:53 +0900203 return s.ApiLevel, fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900204 }
205
206 if ctx.DeviceSpecific() || ctx.SocSpecific() {
207 s = s.ForVendorPartition(ctx)
208 }
Spandan Dasdd7057c2023-01-05 01:03:47 +0000209 return s.ApiLevel.EffectiveVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900210}
211
212// EffectiveVersionString converts an SdkSpec into the concrete version string that the module
213// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
214// it returns the codename (P, Q, R, etc.)
215func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) {
Jiyong Park54105c42021-03-31 18:17:53 +0900216 if !s.Valid() {
217 return s.ApiLevel.String(), fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900218 }
Jiyong Park54105c42021-03-31 18:17:53 +0900219
220 if ctx.DeviceSpecific() || ctx.SocSpecific() {
221 s = s.ForVendorPartition(ctx)
222 }
Spandan Dasdd7057c2023-01-05 01:03:47 +0000223 return s.ApiLevel.EffectiveVersionString(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900224}
225
Jiyong Park92315372021-04-02 08:45:46 +0900226var (
Jiyong Parkc7022042021-04-15 16:53:05 +0900227 SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"}
Spandan Das15da5882023-03-02 23:36:39 +0000228 SdkSpecPrivate = SdkSpec{SdkPrivate, PrivateApiLevel, ""}
Jiyong Parkc7022042021-04-15 16:53:05 +0900229 SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"}
Jiyong Park92315372021-04-02 08:45:46 +0900230)
231
232func SdkSpecFrom(ctx EarlyModuleContext, str string) SdkSpec {
satayev0ee2f912021-12-01 17:39:48 +0000233 return SdkSpecFromWithConfig(ctx.Config(), str)
234}
235
236func SdkSpecFromWithConfig(config Config, str string) SdkSpec {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900237 switch str {
238 // special cases first
239 case "":
Jiyong Park92315372021-04-02 08:45:46 +0900240 return SdkSpecPrivate
Jiyong Parkf1691d22021-03-29 20:11:58 +0900241 case "none":
Jiyong Park92315372021-04-02 08:45:46 +0900242 return SdkSpecNone
Jiyong Parkf1691d22021-03-29 20:11:58 +0900243 case "core_platform":
Jiyong Park92315372021-04-02 08:45:46 +0900244 return SdkSpecCorePlatform
Jiyong Parkf1691d22021-03-29 20:11:58 +0900245 default:
246 // the syntax is [kind_]version
247 sep := strings.LastIndex(str, "_")
248
249 var kindString string
250 if sep == 0 {
Spandan Das15da5882023-03-02 23:36:39 +0000251 return SdkSpec{SdkInvalid, NewInvalidApiLevel(str), str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900252 } else if sep == -1 {
253 kindString = ""
254 } else {
255 kindString = str[0:sep]
256 }
257 versionString := str[sep+1 : len(str)]
258
259 var kind SdkKind
260 switch kindString {
261 case "":
262 kind = SdkPublic
263 case "core":
264 kind = SdkCore
265 case "system":
266 kind = SdkSystem
267 case "test":
268 kind = SdkTest
269 case "module":
270 kind = SdkModule
271 case "system_server":
272 kind = SdkSystemServer
273 default:
Jiyong Park54105c42021-03-31 18:17:53 +0900274 return SdkSpec{SdkInvalid, NoneApiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900275 }
276
satayev0ee2f912021-12-01 17:39:48 +0000277 apiLevel, err := ApiLevelFromUserWithConfig(config, versionString)
Jiyong Park92315372021-04-02 08:45:46 +0900278 if err != nil {
Spandan Das15da5882023-03-02 23:36:39 +0000279 return SdkSpec{SdkInvalid, NewInvalidApiLevel(versionString), str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900280 }
Jiyong Park54105c42021-03-31 18:17:53 +0900281 return SdkSpec{kind, apiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900282 }
283}
284
285func (s SdkSpec) ValidateSystemSdk(ctx EarlyModuleContext) bool {
286 // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module)
287 // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29,
288 // 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 +0900289 if s.Kind != SdkSystem || s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900290 return true
291 }
292 allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions()
293 if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
294 systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions()
295 if len(systemSdkVersions) > 0 {
296 allowedVersions = systemSdkVersions
297 }
298 }
Jiyong Park54105c42021-03-31 18:17:53 +0900299 if len(allowedVersions) > 0 && !InList(s.ApiLevel.String(), allowedVersions) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900300 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
301 s.Raw, allowedVersions)
302 return false
303 }
304 return true
305}
Spandan Das6b73fcb2023-03-20 20:20:58 +0000306
307func init() {
308 RegisterMakeVarsProvider(pctx, javaSdkMakeVars)
309}
310
311// Export the name of the soong modules representing the various Java API surfaces.
312func javaSdkMakeVars(ctx MakeVarsContext) {
Jihoon Kang91c83952023-05-30 19:12:28 +0000313 ctx.Strict("ANDROID_PUBLIC_STUBS", SdkPublic.DefaultJavaLibraryName())
314 ctx.Strict("ANDROID_SYSTEM_STUBS", SdkSystem.DefaultJavaLibraryName())
315 ctx.Strict("ANDROID_TEST_STUBS", SdkTest.DefaultJavaLibraryName())
316 ctx.Strict("ANDROID_MODULE_LIB_STUBS", SdkModule.DefaultJavaLibraryName())
317 ctx.Strict("ANDROID_SYSTEM_SERVER_STUBS", SdkSystemServer.DefaultJavaLibraryName())
318 ctx.Strict("ANDROID_CORE_STUBS", SdkCore.DefaultJavaLibraryName())
Spandan Das6b73fcb2023-03-20 20:20:58 +0000319}