blob: 80aeb2e69302b52b97f6d200e1a098b8991333ea [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
Spandan Das69f42182023-03-29 16:25:16 +000087// JavaApiLibraryName returns the name of .txt equivalent of a java_library, but does
Spandan Das626a8ad2023-03-20 18:52:50 +000088// not check if either module exists.
89// TODO: Return .txt (single-tree or multi-tree equivalents) based on config
Spandan Das69f42182023-03-29 16:25:16 +000090func JavaApiLibraryName(c Config, name string) string {
Jihoon Kang1bff0342023-01-17 20:40:22 +000091 if c.BuildFromTextStub() {
92 return name + ".from-text"
93 }
Spandan Das626a8ad2023-03-20 18:52:50 +000094 return name
95}
96
Jihoon Kangb5078312023-03-29 23:25:49 +000097// JavaApiLibraryNames applies JavaApiLibraryName to the list of java_library names.
98func JavaApiLibraryNames(c Config, names []string) []string {
99 apiLibs := make([]string, len(names))
100 for i, name := range names {
101 apiLibs[i] = JavaApiLibraryName(c, name)
102 }
103 return apiLibs
104}
105
Jihoon Kang5b065bb2023-04-05 21:08:08 +0000106func (k SdkKind) DefaultJavaLibraryName() string {
Spandan Das626a8ad2023-03-20 18:52:50 +0000107 switch k {
108 case SdkPublic:
109 return "android_stubs_current"
110 case SdkSystem:
111 return "android_system_stubs_current"
112 case SdkTest:
113 return "android_test_stubs_current"
114 case SdkCore:
115 return "core.current.stubs"
116 case SdkModule:
117 return "android_module_lib_stubs_current"
118 case SdkSystemServer:
119 return "android_system_server_stubs_current"
120 default:
121 panic(fmt.Errorf("APIs of API surface %v cannot be provided by a single Soong module\n", k))
122 }
123}
124
Jiyong Parkf1691d22021-03-29 20:11:58 +0900125// SdkSpec represents the kind and the version of an SDK for a module to build against
126type SdkSpec struct {
Jiyong Park54105c42021-03-31 18:17:53 +0900127 Kind SdkKind
128 ApiLevel ApiLevel
129 Raw string
Jiyong Parkf1691d22021-03-29 20:11:58 +0900130}
131
132func (s SdkSpec) String() string {
Jiyong Park54105c42021-03-31 18:17:53 +0900133 return fmt.Sprintf("%s_%s", s.Kind, s.ApiLevel)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900134}
135
136// Valid checks if this SdkSpec is well-formed. Note however that true doesn't mean that the
137// specified SDK actually exists.
138func (s SdkSpec) Valid() bool {
139 return s.Kind != SdkInvalid
140}
141
142// Specified checks if this SdkSpec is well-formed and is not "".
143func (s SdkSpec) Specified() bool {
144 return s.Valid() && s.Kind != SdkPrivate
145}
146
147// whether the API surface is managed and versioned, i.e. has .txt file that
148// get frozen on SDK freeze and changes get reviewed by API council.
149func (s SdkSpec) Stable() bool {
150 if !s.Specified() {
151 return false
152 }
153 switch s.Kind {
154 case SdkNone:
155 // there is nothing to manage and version in this case; de facto stable API.
156 return true
157 case SdkCore, SdkPublic, SdkSystem, SdkModule, SdkSystemServer:
158 return true
159 case SdkCorePlatform, SdkTest, SdkPrivate:
160 return false
161 default:
162 panic(fmt.Errorf("unknown SdkKind=%v", s.Kind))
163 }
164 return false
165}
166
satayev0ee2f912021-12-01 17:39:48 +0000167// PrebuiltSdkAvailableForUnbundledBuild tells whether this SdkSpec can have a prebuilt SDK
Jiyong Parkf1691d22021-03-29 20:11:58 +0900168// that can be used for unbundled builds.
169func (s SdkSpec) PrebuiltSdkAvailableForUnbundledBuild() bool {
170 // "", "none", and "core_platform" are not available for unbundled build
171 // as we don't/can't have prebuilt stub for the versions
172 return s.Kind != SdkPrivate && s.Kind != SdkNone && s.Kind != SdkCorePlatform
173}
174
175func (s SdkSpec) ForVendorPartition(ctx EarlyModuleContext) SdkSpec {
176 // If BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES has a numeric value,
177 // use it instead of "current" for the vendor partition.
178 currentSdkVersion := ctx.DeviceConfig().CurrentApiLevelForVendorModules()
179 if currentSdkVersion == "current" {
180 return s
181 }
182
183 if s.Kind == SdkPublic || s.Kind == SdkSystem {
Jiyong Park54105c42021-03-31 18:17:53 +0900184 if s.ApiLevel.IsCurrent() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900185 if i, err := strconv.Atoi(currentSdkVersion); err == nil {
Jiyong Park54105c42021-03-31 18:17:53 +0900186 apiLevel := uncheckedFinalApiLevel(i)
187 return SdkSpec{s.Kind, apiLevel, s.Raw}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900188 }
189 panic(fmt.Errorf("BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES must be either \"current\" or a number, but was %q", currentSdkVersion))
190 }
191 }
192 return s
193}
194
195// UsePrebuilt determines whether prebuilt SDK should be used for this SdkSpec with the given context.
196func (s SdkSpec) UsePrebuilt(ctx EarlyModuleContext) bool {
Jiyong Parkc7022042021-04-15 16:53:05 +0900197 switch s {
198 case SdkSpecNone, SdkSpecCorePlatform, SdkSpecPrivate:
199 return false
200 }
201
Jiyong Park54105c42021-03-31 18:17:53 +0900202 if s.ApiLevel.IsCurrent() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900203 // "current" can be built from source and be from prebuilt SDK
204 return ctx.Config().AlwaysUsePrebuiltSdks()
Jiyong Park54105c42021-03-31 18:17:53 +0900205 } else if !s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900206 // validation check
Paul Duffin12e311d2021-10-28 17:42:16 +0100207 if s.Kind != SdkPublic && s.Kind != SdkSystem && s.Kind != SdkTest && s.Kind != SdkModule && s.Kind != SdkSystemServer {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900208 panic(fmt.Errorf("prebuilt SDK is not not available for SdkKind=%q", s.Kind))
209 return false
210 }
211 // numbered SDKs are always from prebuilt
212 return true
213 }
Jiyong Parkf1691d22021-03-29 20:11:58 +0900214 return false
215}
216
Jiyong Park54105c42021-03-31 18:17:53 +0900217// EffectiveVersion converts an SdkSpec into the concrete ApiLevel that the module should use. For
218// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns
219// FutureApiLevel(10000).
220func (s SdkSpec) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900221 if !s.Valid() {
Jiyong Park54105c42021-03-31 18:17:53 +0900222 return s.ApiLevel, fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900223 }
224
225 if ctx.DeviceSpecific() || ctx.SocSpecific() {
226 s = s.ForVendorPartition(ctx)
227 }
Spandan Dasdd7057c2023-01-05 01:03:47 +0000228 return s.ApiLevel.EffectiveVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900229}
230
231// EffectiveVersionString converts an SdkSpec into the concrete version string that the module
232// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
233// it returns the codename (P, Q, R, etc.)
234func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) {
Jiyong Park54105c42021-03-31 18:17:53 +0900235 if !s.Valid() {
236 return s.ApiLevel.String(), fmt.Errorf("invalid sdk version %q", s.Raw)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900237 }
Jiyong Park54105c42021-03-31 18:17:53 +0900238
239 if ctx.DeviceSpecific() || ctx.SocSpecific() {
240 s = s.ForVendorPartition(ctx)
241 }
Spandan Dasdd7057c2023-01-05 01:03:47 +0000242 return s.ApiLevel.EffectiveVersionString(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900243}
244
Jiyong Park92315372021-04-02 08:45:46 +0900245var (
Jiyong Parkc7022042021-04-15 16:53:05 +0900246 SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"}
Spandan Das15da5882023-03-02 23:36:39 +0000247 SdkSpecPrivate = SdkSpec{SdkPrivate, PrivateApiLevel, ""}
Jiyong Parkc7022042021-04-15 16:53:05 +0900248 SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"}
Jiyong Park92315372021-04-02 08:45:46 +0900249)
250
251func SdkSpecFrom(ctx EarlyModuleContext, str string) SdkSpec {
satayev0ee2f912021-12-01 17:39:48 +0000252 return SdkSpecFromWithConfig(ctx.Config(), str)
253}
254
255func SdkSpecFromWithConfig(config Config, str string) SdkSpec {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900256 switch str {
257 // special cases first
258 case "":
Jiyong Park92315372021-04-02 08:45:46 +0900259 return SdkSpecPrivate
Jiyong Parkf1691d22021-03-29 20:11:58 +0900260 case "none":
Jiyong Park92315372021-04-02 08:45:46 +0900261 return SdkSpecNone
Jiyong Parkf1691d22021-03-29 20:11:58 +0900262 case "core_platform":
Jiyong Park92315372021-04-02 08:45:46 +0900263 return SdkSpecCorePlatform
Jiyong Parkf1691d22021-03-29 20:11:58 +0900264 default:
265 // the syntax is [kind_]version
266 sep := strings.LastIndex(str, "_")
267
268 var kindString string
269 if sep == 0 {
Spandan Das15da5882023-03-02 23:36:39 +0000270 return SdkSpec{SdkInvalid, NewInvalidApiLevel(str), str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900271 } else if sep == -1 {
272 kindString = ""
273 } else {
274 kindString = str[0:sep]
275 }
276 versionString := str[sep+1 : len(str)]
277
278 var kind SdkKind
279 switch kindString {
280 case "":
281 kind = SdkPublic
282 case "core":
283 kind = SdkCore
284 case "system":
285 kind = SdkSystem
286 case "test":
287 kind = SdkTest
288 case "module":
289 kind = SdkModule
290 case "system_server":
291 kind = SdkSystemServer
292 default:
Jiyong Park54105c42021-03-31 18:17:53 +0900293 return SdkSpec{SdkInvalid, NoneApiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900294 }
295
satayev0ee2f912021-12-01 17:39:48 +0000296 apiLevel, err := ApiLevelFromUserWithConfig(config, versionString)
Jiyong Park92315372021-04-02 08:45:46 +0900297 if err != nil {
Spandan Das15da5882023-03-02 23:36:39 +0000298 return SdkSpec{SdkInvalid, NewInvalidApiLevel(versionString), str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900299 }
Jiyong Park54105c42021-03-31 18:17:53 +0900300 return SdkSpec{kind, apiLevel, str}
Jiyong Parkf1691d22021-03-29 20:11:58 +0900301 }
302}
303
304func (s SdkSpec) ValidateSystemSdk(ctx EarlyModuleContext) bool {
305 // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module)
306 // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29,
307 // 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 +0900308 if s.Kind != SdkSystem || s.ApiLevel.IsPreview() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900309 return true
310 }
311 allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions()
312 if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
313 systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions()
314 if len(systemSdkVersions) > 0 {
315 allowedVersions = systemSdkVersions
316 }
317 }
Jiyong Park54105c42021-03-31 18:17:53 +0900318 if len(allowedVersions) > 0 && !InList(s.ApiLevel.String(), allowedVersions) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900319 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
320 s.Raw, allowedVersions)
321 return false
322 }
323 return true
324}
Spandan Das6b73fcb2023-03-20 20:20:58 +0000325
326func init() {
327 RegisterMakeVarsProvider(pctx, javaSdkMakeVars)
328}
329
330// Export the name of the soong modules representing the various Java API surfaces.
331func javaSdkMakeVars(ctx MakeVarsContext) {
Jihoon Kang91c83952023-05-30 19:12:28 +0000332 ctx.Strict("ANDROID_PUBLIC_STUBS", SdkPublic.DefaultJavaLibraryName())
333 ctx.Strict("ANDROID_SYSTEM_STUBS", SdkSystem.DefaultJavaLibraryName())
334 ctx.Strict("ANDROID_TEST_STUBS", SdkTest.DefaultJavaLibraryName())
335 ctx.Strict("ANDROID_MODULE_LIB_STUBS", SdkModule.DefaultJavaLibraryName())
336 ctx.Strict("ANDROID_SYSTEM_SERVER_STUBS", SdkSystemServer.DefaultJavaLibraryName())
337 ctx.Strict("ANDROID_CORE_STUBS", SdkCore.DefaultJavaLibraryName())
Spandan Das6b73fcb2023-03-20 20:20:58 +0000338}