blob: 26f517de3456a2c6d1c974d050613b84dd2a3185 [file] [log] [blame]
Jiyong Parkd1063c12019-07-17 20:08:41 +09001// Copyright (C) 2019 The Android Open Source Project
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 sdk
16
17import (
Jiyong Park9b409bc2019-10-11 14:59:13 +090018 "fmt"
Paul Duffin504b4612019-11-22 14:52:29 +000019 "io"
Jiyong Park9b409bc2019-10-11 14:59:13 +090020 "strconv"
21
Jiyong Parkd1063c12019-07-17 20:08:41 +090022 "github.com/google/blueprint"
Jiyong Park100f3fd2019-11-06 16:03:32 +090023 "github.com/google/blueprint/proptools"
Jiyong Parkd1063c12019-07-17 20:08:41 +090024
25 "android/soong/android"
26 // This package doesn't depend on the apex package, but import it to make its mutators to be
27 // registered before mutators in this package. See RegisterPostDepsMutators for more details.
28 _ "android/soong/apex"
29)
30
31func init() {
Jiyong Park232e7852019-11-04 12:23:40 +090032 pctx.Import("android/soong/android")
Paul Duffin375058f2019-11-29 20:17:53 +000033 pctx.Import("android/soong/java/config")
34
Paul Duffin6d9108f02021-03-09 22:59:28 +000035 registerSdkBuildComponents(android.InitRegistrationContext)
36}
37
38func registerSdkBuildComponents(ctx android.RegistrationContext) {
39 ctx.RegisterModuleType("sdk", SdkModuleFactory)
40 ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
41 ctx.PreDepsMutators(RegisterPreDepsMutators)
42 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jiyong Parkd1063c12019-07-17 20:08:41 +090043}
44
45type sdk struct {
46 android.ModuleBase
47 android.DefaultableModuleBase
48
Paul Duffin255f18e2019-12-13 11:22:16 +000049 // The dynamically generated information about the registered SdkMemberType
50 dynamicSdkMemberTypes *dynamicSdkMemberTypes
51
Paul Duffin62782de2021-07-14 12:05:16 +010052 // The dynamically created instance of the properties struct containing the sdk member type
Paul Duffin255f18e2019-12-13 11:22:16 +000053 // list properties, e.g. java_libs.
54 dynamicMemberTypeListProperties interface{}
55
Paul Duffind19f8942021-07-14 12:08:37 +010056 // The dynamically generated information about the registered SdkMemberTrait
57 dynamicSdkMemberTraits *dynamicSdkMemberTraits
58
59 // The dynamically created instance of the properties struct containing the sdk member trait
60 // list properties.
61 dynamicMemberTraitListProperties interface{}
62
Paul Duffin21827262021-04-24 12:16:36 +010063 // Information about the OsType specific member variants depended upon by this variant.
Paul Duffin1356d8c2020-02-25 19:26:33 +000064 //
Paul Duffin6a7e9532020-03-20 17:50:07 +000065 // Set by OsType specific variants in the collectMembers() method and used by the
66 // CommonOS variant when building the snapshot. That work is all done on separate
67 // calls to the sdk.GenerateAndroidBuildActions method which is guaranteed to be
68 // called for the OsType specific variants before the CommonOS variant (because
69 // the latter depends on the former).
Paul Duffin21827262021-04-24 12:16:36 +010070 memberVariantDeps []sdkMemberVariantDep
Paul Duffin1356d8c2020-02-25 19:26:33 +000071
Paul Duffin6a7e9532020-03-20 17:50:07 +000072 // The multilib variants that are used by this sdk variant.
73 multilibUsages multilibUsage
74
Jiyong Parkd1063c12019-07-17 20:08:41 +090075 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090076
Jiyong Park232e7852019-11-04 12:23:40 +090077 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000078
Paul Duffin1a44a992022-05-06 09:38:02 +000079 infoFile android.OptionalPath
80
Paul Duffinac37c502019-11-26 18:02:20 +000081 // The builder, preserved for testing.
82 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090083}
84
85type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090086 Snapshot bool `blueprint:"mutated"`
Paul Duffin8150da62019-12-16 17:21:27 +000087
88 // True if this is a module_exports (or module_exports_snapshot) module type.
89 Module_exports bool `blueprint:"mutated"`
Paul Duffin157f40f2020-09-29 16:01:08 +010090
91 // The additional visibility to add to the prebuilt modules to allow them to
92 // reference each other.
93 //
94 // This can only be used to widen the visibility of the members:
95 //
96 // * Specifying //visibility:public here will make all members visible and
97 // essentially ignore their own visibility.
98 // * Specifying //visibility:private here is an error.
99 // * Specifying any other rule here will add it to the members visibility and
100 // be output to the member prebuilt in the snapshot. Duplicates will be
101 // dropped. Adding a rule to members that have //visibility:private will
102 // cause the //visibility:private to be discarded.
103 Prebuilt_visibility []string
Jiyong Parkd1063c12019-07-17 20:08:41 +0900104}
105
106// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
107// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000108func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000109 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000110}
Paul Duffin255f18e2019-12-13 11:22:16 +0000111
Paul Duffine6029182019-12-16 17:43:48 +0000112func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000113 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000114 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000115 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffin30c830b2021-09-22 11:49:47 +0100116 sdkMemberTypeKey, sdkMemberTypes := android.RegisteredSdkMemberTypes(moduleExports)
117 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(sdkMemberTypeKey, sdkMemberTypes)
Paul Duffin255f18e2019-12-13 11:22:16 +0000118 // Create an instance of the dynamically created struct that contains all the
119 // properties for the member type specific list properties.
Paul Duffin62782de2021-07-14 12:05:16 +0100120 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberTypeListProperties()
Paul Duffind19f8942021-07-14 12:08:37 +0100121
Paul Duffin30c830b2021-09-22 11:49:47 +0100122 sdkMemberTraitsKey, sdkMemberTraits := android.RegisteredSdkMemberTraits()
123 s.dynamicSdkMemberTraits = getDynamicSdkMemberTraits(sdkMemberTraitsKey, sdkMemberTraits)
Paul Duffind19f8942021-07-14 12:08:37 +0100124 // Create an instance of the dynamically created struct that contains all the properties for the
125 // member trait specific list properties.
126 s.dynamicMemberTraitListProperties = s.dynamicSdkMemberTraits.createMemberTraitListProperties()
127
128 // Create a wrapper around the dynamic trait specific properties so that they have to be
129 // specified within a traits:{} section in the .bp file.
130 traitsWrapper := struct {
131 Traits interface{}
132 }{s.dynamicMemberTraitListProperties}
133
134 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties, &traitsWrapper)
Paul Duffin157f40f2020-09-29 16:01:08 +0100135
136 // Make sure that the prebuilt visibility property is verified for errors.
137 android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000138 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900139 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900140 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
141 type props struct {
142 Compile_multilib *string
143 }
144 p := &props{Compile_multilib: proptools.StringPtr("both")}
Martin Stjernholm26ab8e82020-06-30 20:34:00 +0100145 ctx.PrependProperties(p)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900146 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900147 return s
148}
149
Jiyong Park9b409bc2019-10-11 14:59:13 +0900150// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
151func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000152 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000153 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900154 return s
155}
156
Paul Duffin62782de2021-07-14 12:05:16 +0100157func (s *sdk) memberTypeListProperties() []*sdkMemberTypeListProperty {
158 return s.dynamicSdkMemberTypes.memberTypeListProperties
Paul Duffin72910952020-01-20 18:16:30 +0000159}
160
Paul Duffin62782de2021-07-14 12:05:16 +0100161func (s *sdk) memberTypeListProperty(memberType android.SdkMemberType) *sdkMemberTypeListProperty {
Paul Duffincd064672021-04-24 00:47:29 +0100162 return s.dynamicSdkMemberTypes.memberTypeToProperty[memberType]
163}
164
Paul Duffind19f8942021-07-14 12:08:37 +0100165// memberTraitListProperties returns the list of *sdkMemberTraitListProperty instances for this sdk.
166func (s *sdk) memberTraitListProperties() []*sdkMemberTraitListProperty {
167 return s.dynamicSdkMemberTraits.memberTraitListProperties
168}
169
Jiyong Park9b409bc2019-10-11 14:59:13 +0900170func (s *sdk) snapshot() bool {
171 return s.properties.Snapshot
172}
173
Jiyong Parkd1063c12019-07-17 20:08:41 +0900174func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000175 if s.snapshot() {
Jiyong Park232e7852019-11-04 12:23:40 +0900176 // We don't need to create a snapshot out of sdk_snapshot.
177 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000178 return
179 }
180
181 // This method is guaranteed to be called on OsType specific variants before it is called
182 // on their corresponding CommonOS variant.
183 if !s.IsCommonOSVariant() {
Paul Duffin6a7e9532020-03-20 17:50:07 +0000184 // Update the OsType specific sdk variant with information about its members.
185 s.collectMembers(ctx)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000186 } else {
187 // Get the OsType specific variants on which the CommonOS depends.
188 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
189 var sdkVariants []*sdk
190 for _, m := range osSpecificVariants {
191 if sdkVariant, ok := m.(*sdk); ok {
192 sdkVariants = append(sdkVariants, sdkVariant)
193 }
194 }
195
196 // Generate the snapshot from the member info.
Paul Duffin1a44a992022-05-06 09:38:02 +0000197 s.buildSnapshot(ctx, sdkVariants)
Jiyong Park232e7852019-11-04 12:23:40 +0900198 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900199}
200
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900201func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin1a44a992022-05-06 09:38:02 +0000202 if !s.snapshotFile.Valid() != !s.infoFile.Valid() {
203 panic("Snapshot (%q) and info file (%q) should both be set or neither should be set.")
204 } else if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900205 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900206 }
207
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900208 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900209 Class: "FAKE",
210 OutputFile: s.snapshotFile,
Paul Duffin1a44a992022-05-06 09:38:02 +0000211 DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path(), s.infoFile.Path()),
Jiyong Park232e7852019-11-04 12:23:40 +0900212 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000213 ExtraFooters: []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800214 func(w io.Writer, name, prefix, moduleDir string) {
Paul Duffin504b4612019-11-22 14:52:29 +0000215 // Allow the sdk to be built by simply passing its name on the command line.
216 fmt.Fprintln(w, ".PHONY:", s.Name())
217 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
Paul Duffin1a44a992022-05-06 09:38:02 +0000218
219 // Allow the sdk info to be built by simply passing its name on the command line.
220 infoTarget := s.Name() + ".info"
221 fmt.Fprintln(w, ".PHONY:", infoTarget)
222 fmt.Fprintln(w, infoTarget+":", s.infoFile.String())
Paul Duffin504b4612019-11-22 14:52:29 +0000223 },
224 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900225 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900226}
227
Paul Duffind19f8942021-07-14 12:08:37 +0100228// gatherTraits gathers the traits from the dynamically generated trait specific properties.
229//
230// Returns a map from member name to the set of required traits.
231func (s *sdk) gatherTraits() map[string]android.SdkMemberTraitSet {
232 traitListByMember := map[string][]android.SdkMemberTrait{}
233 for _, memberListProperty := range s.memberTraitListProperties() {
234 names := memberListProperty.getter(s.dynamicMemberTraitListProperties)
235 for _, name := range names {
236 traitListByMember[name] = append(traitListByMember[name], memberListProperty.memberTrait)
237 }
238 }
239
240 traitSetByMember := map[string]android.SdkMemberTraitSet{}
241 for name, list := range traitListByMember {
242 traitSetByMember[name] = android.NewSdkMemberTraitSet(list)
243 }
244
245 return traitSetByMember
246}
247
Paul Duffin296701e2021-07-14 10:29:36 +0100248// newDependencyContext creates a new SdkDependencyContext for this sdk.
249func (s *sdk) newDependencyContext(mctx android.BottomUpMutatorContext) android.SdkDependencyContext {
Paul Duffind19f8942021-07-14 12:08:37 +0100250 traits := s.gatherTraits()
251
Paul Duffin296701e2021-07-14 10:29:36 +0100252 return &dependencyContext{
253 BottomUpMutatorContext: mctx,
Paul Duffind19f8942021-07-14 12:08:37 +0100254 requiredTraits: traits,
Paul Duffin296701e2021-07-14 10:29:36 +0100255 }
256}
257
258type dependencyContext struct {
259 android.BottomUpMutatorContext
Paul Duffind19f8942021-07-14 12:08:37 +0100260
261 // Map from member name to the set of traits that the sdk requires the member provides.
262 requiredTraits map[string]android.SdkMemberTraitSet
263}
264
265func (d *dependencyContext) RequiredTraits(name string) android.SdkMemberTraitSet {
266 if s, ok := d.requiredTraits[name]; ok {
267 return s
268 } else {
269 return android.EmptySdkMemberTraitSet()
270 }
271}
272
273func (d *dependencyContext) RequiresTrait(name string, trait android.SdkMemberTrait) bool {
274 return d.RequiredTraits(name).Contains(trait)
Paul Duffin296701e2021-07-14 10:29:36 +0100275}
276
277var _ android.SdkDependencyContext = (*dependencyContext)(nil)
278
Jiyong Parkd1063c12019-07-17 20:08:41 +0900279// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
280// interface and the sdk module type. This function has been made public to be called by tests
281// outside of the sdk package
282func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
283 ctx.BottomUp("SdkMember", memberMutator).Parallel()
284 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
285 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
286}
287
Paul Duffin65347702020-03-31 15:23:40 +0100288// RegisterPostDepsMutators registers post-deps mutators to support modules implementing SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900289// interface and the sdk module type. This function has been made public to be called by tests
290// outside of the sdk package
291func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
292 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
293 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
294 // APEX to its dependents. Since different versions of the same SDK can be used by different
295 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
296 // should have been mutated for the apex before the SDK requirements are set.
297 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
298 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900299 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900300}
301
302type dependencyTag struct {
303 blueprint.BaseDependencyTag
304}
305
Martin Stjernholmcc776012020-07-07 03:22:21 +0100306// Mark this tag so dependencies that use it are excluded from APEX contents.
307func (t dependencyTag) ExcludeFromApexContents() {}
308
309var _ android.ExcludeFromApexContentsTag = dependencyTag{}
310
Jiyong Parkd1063c12019-07-17 20:08:41 +0900311// For dependencies from an in-development version of an SDK member to frozen versions of the same member
312// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffin573989d2021-03-17 13:25:29 +0000313//
314// The dependency represented by this tag requires that for every APEX variant created for the
315// `from` module that an equivalent APEX variant is created for the 'to' module. This is because an
316// APEX that requires a specific version of an sdk (via the `uses_sdks` property will replace
317// dependencies on the unversioned sdk member with a dependency on the appropriate versioned sdk
318// member. In order for that to work the versioned sdk member needs to have a variant for that APEX.
319// As it is not known at the time that the APEX variants are created which specific APEX variants of
320// a versioned sdk members will be required it is necessary for the versioned sdk members to have
321// variants for any APEX that it could be used within.
322//
323// If the APEX selects a versioned sdk member then it will not have a dependency on the `from`
324// module at all so any dependencies of that module will not affect the APEX. However, if the APEX
325// selects the unversioned sdk member then it must exclude all the versioned sdk members. In no
326// situation would this dependency cause the `to` module to be added to the APEX hence why this tag
327// also excludes the `to` module from being added to the APEX contents.
Paul Duffinfe149222020-01-14 14:06:09 +0000328type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900329 dependencyTag
330 member string
331 version string
332}
333
Paul Duffin573989d2021-03-17 13:25:29 +0000334func (t sdkMemberVersionedDepTag) AlwaysRequireApexVariant() bool {
335 return true
336}
337
Paul Duffinfe149222020-01-14 14:06:09 +0000338// Mark this tag so dependencies that use it are excluded from visibility enforcement.
339func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
340
Paul Duffin573989d2021-03-17 13:25:29 +0000341var _ android.AlwaysRequireApexVariantTag = sdkMemberVersionedDepTag{}
342
Jiyong Parkd1063c12019-07-17 20:08:41 +0900343// Step 1: create dependencies from an SDK module to its members.
344func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000345 if s, ok := mctx.Module().(*sdk); ok {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000346 // Add dependencies from enabled and non CommonOS variants to the sdk member variants.
347 if s.Enabled() && !s.IsCommonOSVariant() {
Paul Duffin296701e2021-07-14 10:29:36 +0100348 ctx := s.newDependencyContext(mctx)
Paul Duffin62782de2021-07-14 12:05:16 +0100349 for _, memberListProperty := range s.memberTypeListProperties() {
Paul Duffin13082052021-05-11 00:31:38 +0100350 if memberListProperty.getter == nil {
351 continue
352 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000353 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000354 if len(names) > 0 {
Paul Duffind19f8942021-07-14 12:08:37 +0100355 memberType := memberListProperty.memberType
356
357 // Verify that the member type supports the specified traits.
358 supportedTraits := memberType.SupportedTraits()
359 for _, name := range names {
360 requiredTraits := ctx.RequiredTraits(name)
361 unsupportedTraits := requiredTraits.Subtract(supportedTraits)
362 if !unsupportedTraits.Empty() {
363 ctx.ModuleErrorf("sdk member %q has traits %s that are unsupported by its member type %q", name, unsupportedTraits, memberType.SdkPropertyName())
364 }
365 }
366
367 // Add dependencies using the appropriate tag.
Paul Duffincc1b3da2020-02-27 13:29:56 +0000368 tag := memberListProperty.dependencyTag
Paul Duffind19f8942021-07-14 12:08:37 +0100369 memberType.AddDependencies(ctx, tag, names)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000370 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000371 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900372 }
373 }
374}
375
376// Step 2: record that dependencies of SDK modules are members of the SDK modules
377func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900378 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900379 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900380 if s.snapshot() && mySdkRef.Unversioned() {
381 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
382 "Did you manually modify Android.bp?")
383 }
384 if !s.snapshot() && !mySdkRef.Unversioned() {
385 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
386 }
387 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
388 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
389 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
390 }
391 }
392
Jiyong Parkd1063c12019-07-17 20:08:41 +0900393 mctx.VisitDirectDeps(func(child android.Module) {
394 if member, ok := child.(android.SdkAware); ok {
395 member.MakeMemberOf(mySdkRef)
396 }
397 })
398 }
399}
400
Jiyong Park9b409bc2019-10-11 14:59:13 +0900401// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900402// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
403// (apex and apk), each of which might want different sdks to be built with. For example, if both
404// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
405// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
406// using.
407func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100408 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() && m.IsVersioned() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900409 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900410 memberName := m.MemberName()
Paul Duffinfe149222020-01-14 14:06:09 +0000411 tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900412 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
413 }
414 }
415}
416
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000417// An interface that encapsulates all the functionality needed to manage the sdk dependencies.
418//
419// It is a mixture of apex and sdk module functionality.
420type sdkAndApexModule interface {
421 android.Module
422 android.DepIsInSameApex
423 android.RequiredSdks
424}
425
Jiyong Parkd1063c12019-07-17 20:08:41 +0900426// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
427// descendants
428func sdkDepsMutator(mctx android.TopDownMutatorContext) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000429 if parent, ok := mctx.Module().(sdkAndApexModule); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900430 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
431 // by reading its own properties like `uses_sdks`.
Paul Duffina37eca22020-07-22 13:00:54 +0100432 requiredSdks := parent.RequiredSdks()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900433 if len(requiredSdks) > 0 {
434 mctx.VisitDirectDeps(func(m android.Module) {
Paul Duffina37eca22020-07-22 13:00:54 +0100435 // Only propagate required sdks from the apex onto its contents.
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000436 if dep, ok := m.(android.SdkAware); ok && android.IsDepInSameApex(mctx, parent, dep) {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900437 dep.BuildWithSdks(requiredSdks)
438 }
439 })
440 }
441 }
442}
443
444// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
445// versioned module is used instead of the un-versioned (in-development) module libfoo
446func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100447 if versionedSdkMember, ok := mctx.Module().(android.SdkAware); ok && versionedSdkMember.IsInAnySdk() && versionedSdkMember.IsVersioned() {
Paul Duffina37eca22020-07-22 13:00:54 +0100448 if sdk := versionedSdkMember.ContainingSdk(); !sdk.Unversioned() {
449 // Only replace dependencies to <sdkmember> with <sdkmember@required-version>
450 // if the depending module requires it. e.g.
451 // foo -> sdkmember
452 // will be transformed to:
453 // foo -> sdkmember@1
454 // if and only if foo is a member of an APEX that requires version 1 of the
455 // sdk containing sdkmember.
456 memberName := versionedSdkMember.MemberName()
457
Paul Duffin1822a0a2021-03-21 12:56:33 +0000458 // Convert a panic into a normal error to allow it to be more easily tested for. This is a
459 // temporary workaround, once http://b/183204176 has been fixed this can be removed.
460 // TODO(b/183204176): Remove this after fixing.
461 defer func() {
462 if r := recover(); r != nil {
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100463 mctx.ModuleErrorf("sdkDepsReplaceMutator %s", r)
Paul Duffin1822a0a2021-03-21 12:56:33 +0000464 }
465 }()
466
Paul Duffina37eca22020-07-22 13:00:54 +0100467 // Replace dependencies on sdkmember with a dependency on the current module which
468 // is a versioned prebuilt of the sdkmember if required.
469 mctx.ReplaceDependenciesIf(memberName, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
470 // from - foo
471 // to - sdkmember
472 replace := false
473 if parent, ok := from.(android.RequiredSdks); ok {
474 replace = parent.RequiredSdks().Contains(sdk)
475 }
476 return replace
477 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900478 }
479 }
480}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900481
Paul Duffin65347702020-03-31 15:23:40 +0100482// Step 6: ensure that the dependencies outside of the APEX are all from the required SDKs
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900483func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000484 if m, ok := mctx.Module().(sdkAndApexModule); ok {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900485 requiredSdks := m.RequiredSdks()
486 if len(requiredSdks) == 0 {
487 return
488 }
489 mctx.VisitDirectDeps(func(dep android.Module) {
Paul Duffin65347702020-03-31 15:23:40 +0100490 tag := mctx.OtherModuleDependencyTag(dep)
491 if tag == android.DefaultsDepTag {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900492 // dependency to defaults is always okay
493 return
494 }
495
Paul Duffin65347702020-03-31 15:23:40 +0100496 // Ignore the dependency from the unversioned member to any versioned members as an
497 // apex that depends on the unversioned member will not also be depending on a versioned
498 // member.
499 if _, ok := tag.(sdkMemberVersionedDepTag); ok {
500 return
501 }
502
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000503 // If the dep is outside of the APEX, but is not in any of the required SDKs, we know that the
504 // dep is a violation.
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900505 if sa, ok := dep.(android.SdkAware); ok {
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000506 // It is not an error if a dependency that is excluded from the apex due to the tag is not
507 // in one of the required SDKs. That is because all of the existing tags that implement it
508 // do not depend on modules which can or should belong to an sdk_snapshot.
509 if _, ok := tag.(android.ExcludeFromApexContentsTag); ok {
510 // The tag defines a dependency that never requires the child module to be part of the
511 // same apex.
512 return
513 }
514
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900515 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
516 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
517 sa.Name(), sa.ContainingSdk(), requiredSdks)
518 }
519 }
520 })
521 }
522}