blob: 468ed99abc36942ae4ed62effae908da19ab5a44 [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)
Jiyong Parkd1063c12019-07-17 20:08:41 +090042}
43
44type sdk struct {
45 android.ModuleBase
46 android.DefaultableModuleBase
47
Paul Duffin255f18e2019-12-13 11:22:16 +000048 // The dynamically generated information about the registered SdkMemberType
49 dynamicSdkMemberTypes *dynamicSdkMemberTypes
50
Paul Duffin62782de2021-07-14 12:05:16 +010051 // The dynamically created instance of the properties struct containing the sdk member type
Paul Duffin255f18e2019-12-13 11:22:16 +000052 // list properties, e.g. java_libs.
53 dynamicMemberTypeListProperties interface{}
54
Paul Duffind19f8942021-07-14 12:08:37 +010055 // The dynamically generated information about the registered SdkMemberTrait
56 dynamicSdkMemberTraits *dynamicSdkMemberTraits
57
58 // The dynamically created instance of the properties struct containing the sdk member trait
59 // list properties.
60 dynamicMemberTraitListProperties interface{}
61
Paul Duffin21827262021-04-24 12:16:36 +010062 // Information about the OsType specific member variants depended upon by this variant.
Paul Duffin1356d8c2020-02-25 19:26:33 +000063 //
Paul Duffin6a7e9532020-03-20 17:50:07 +000064 // Set by OsType specific variants in the collectMembers() method and used by the
65 // CommonOS variant when building the snapshot. That work is all done on separate
66 // calls to the sdk.GenerateAndroidBuildActions method which is guaranteed to be
67 // called for the OsType specific variants before the CommonOS variant (because
68 // the latter depends on the former).
Paul Duffin21827262021-04-24 12:16:36 +010069 memberVariantDeps []sdkMemberVariantDep
Paul Duffin1356d8c2020-02-25 19:26:33 +000070
Paul Duffin6a7e9532020-03-20 17:50:07 +000071 // The multilib variants that are used by this sdk variant.
72 multilibUsages multilibUsage
73
Jiyong Parkd1063c12019-07-17 20:08:41 +090074 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090075
Jiyong Park232e7852019-11-04 12:23:40 +090076 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000077
Paul Duffinc6ba1822022-05-06 09:38:02 +000078 infoFile android.OptionalPath
79
Paul Duffinac37c502019-11-26 18:02:20 +000080 // The builder, preserved for testing.
81 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090082}
83
84type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090085 Snapshot bool `blueprint:"mutated"`
Paul Duffin8150da62019-12-16 17:21:27 +000086
87 // True if this is a module_exports (or module_exports_snapshot) module type.
88 Module_exports bool `blueprint:"mutated"`
Paul Duffin157f40f2020-09-29 16:01:08 +010089
90 // The additional visibility to add to the prebuilt modules to allow them to
91 // reference each other.
92 //
93 // This can only be used to widen the visibility of the members:
94 //
95 // * Specifying //visibility:public here will make all members visible and
96 // essentially ignore their own visibility.
97 // * Specifying //visibility:private here is an error.
98 // * Specifying any other rule here will add it to the members visibility and
99 // be output to the member prebuilt in the snapshot. Duplicates will be
100 // dropped. Adding a rule to members that have //visibility:private will
101 // cause the //visibility:private to be discarded.
102 Prebuilt_visibility []string
Jiyong Parkd1063c12019-07-17 20:08:41 +0900103}
104
105// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
106// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000107func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000108 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000109}
Paul Duffin255f18e2019-12-13 11:22:16 +0000110
Paul Duffine6029182019-12-16 17:43:48 +0000111func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000112 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000113 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000114 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffin30c830b2021-09-22 11:49:47 +0100115 sdkMemberTypeKey, sdkMemberTypes := android.RegisteredSdkMemberTypes(moduleExports)
116 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(sdkMemberTypeKey, sdkMemberTypes)
Paul Duffin255f18e2019-12-13 11:22:16 +0000117 // Create an instance of the dynamically created struct that contains all the
118 // properties for the member type specific list properties.
Paul Duffin62782de2021-07-14 12:05:16 +0100119 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberTypeListProperties()
Paul Duffind19f8942021-07-14 12:08:37 +0100120
Paul Duffin30c830b2021-09-22 11:49:47 +0100121 sdkMemberTraitsKey, sdkMemberTraits := android.RegisteredSdkMemberTraits()
122 s.dynamicSdkMemberTraits = getDynamicSdkMemberTraits(sdkMemberTraitsKey, sdkMemberTraits)
Paul Duffind19f8942021-07-14 12:08:37 +0100123 // Create an instance of the dynamically created struct that contains all the properties for the
124 // member trait specific list properties.
125 s.dynamicMemberTraitListProperties = s.dynamicSdkMemberTraits.createMemberTraitListProperties()
126
127 // Create a wrapper around the dynamic trait specific properties so that they have to be
128 // specified within a traits:{} section in the .bp file.
129 traitsWrapper := struct {
130 Traits interface{}
131 }{s.dynamicMemberTraitListProperties}
132
133 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties, &traitsWrapper)
Paul Duffin157f40f2020-09-29 16:01:08 +0100134
135 // Make sure that the prebuilt visibility property is verified for errors.
136 android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000137 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900138 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900139 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
140 type props struct {
141 Compile_multilib *string
142 }
143 p := &props{Compile_multilib: proptools.StringPtr("both")}
Martin Stjernholm26ab8e82020-06-30 20:34:00 +0100144 ctx.PrependProperties(p)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900145 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900146 return s
147}
148
Jiyong Park9b409bc2019-10-11 14:59:13 +0900149// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
150func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000151 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000152 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900153 return s
154}
155
Paul Duffin62782de2021-07-14 12:05:16 +0100156func (s *sdk) memberTypeListProperties() []*sdkMemberTypeListProperty {
157 return s.dynamicSdkMemberTypes.memberTypeListProperties
Paul Duffin72910952020-01-20 18:16:30 +0000158}
159
Paul Duffin62782de2021-07-14 12:05:16 +0100160func (s *sdk) memberTypeListProperty(memberType android.SdkMemberType) *sdkMemberTypeListProperty {
Paul Duffincd064672021-04-24 00:47:29 +0100161 return s.dynamicSdkMemberTypes.memberTypeToProperty[memberType]
162}
163
Paul Duffind19f8942021-07-14 12:08:37 +0100164// memberTraitListProperties returns the list of *sdkMemberTraitListProperty instances for this sdk.
165func (s *sdk) memberTraitListProperties() []*sdkMemberTraitListProperty {
166 return s.dynamicSdkMemberTraits.memberTraitListProperties
167}
168
Jiyong Park9b409bc2019-10-11 14:59:13 +0900169func (s *sdk) snapshot() bool {
170 return s.properties.Snapshot
171}
172
Jiyong Parkd1063c12019-07-17 20:08:41 +0900173func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000174 if s.snapshot() {
Jiyong Park232e7852019-11-04 12:23:40 +0900175 // We don't need to create a snapshot out of sdk_snapshot.
176 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000177 return
178 }
179
180 // This method is guaranteed to be called on OsType specific variants before it is called
181 // on their corresponding CommonOS variant.
182 if !s.IsCommonOSVariant() {
Paul Duffin6a7e9532020-03-20 17:50:07 +0000183 // Update the OsType specific sdk variant with information about its members.
184 s.collectMembers(ctx)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000185 } else {
186 // Get the OsType specific variants on which the CommonOS depends.
187 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
188 var sdkVariants []*sdk
189 for _, m := range osSpecificVariants {
190 if sdkVariant, ok := m.(*sdk); ok {
191 sdkVariants = append(sdkVariants, sdkVariant)
192 }
193 }
194
195 // Generate the snapshot from the member info.
Paul Duffinc6ba1822022-05-06 09:38:02 +0000196 s.buildSnapshot(ctx, sdkVariants)
Jiyong Park232e7852019-11-04 12:23:40 +0900197 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900198}
199
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900200func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000201 if !s.snapshotFile.Valid() != !s.infoFile.Valid() {
202 panic("Snapshot (%q) and info file (%q) should both be set or neither should be set.")
203 } else if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900204 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900205 }
206
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900207 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900208 Class: "FAKE",
209 OutputFile: s.snapshotFile,
Paul Duffinc6ba1822022-05-06 09:38:02 +0000210 DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path(), s.infoFile.Path()),
Jiyong Park232e7852019-11-04 12:23:40 +0900211 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000212 ExtraFooters: []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800213 func(w io.Writer, name, prefix, moduleDir string) {
Paul Duffin504b4612019-11-22 14:52:29 +0000214 // Allow the sdk to be built by simply passing its name on the command line.
215 fmt.Fprintln(w, ".PHONY:", s.Name())
216 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
Paul Duffinc6ba1822022-05-06 09:38:02 +0000217
218 // Allow the sdk info to be built by simply passing its name on the command line.
219 infoTarget := s.Name() + ".info"
220 fmt.Fprintln(w, ".PHONY:", infoTarget)
221 fmt.Fprintln(w, infoTarget+":", s.infoFile.String())
Paul Duffin504b4612019-11-22 14:52:29 +0000222 },
223 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900224 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900225}
226
Paul Duffind19f8942021-07-14 12:08:37 +0100227// gatherTraits gathers the traits from the dynamically generated trait specific properties.
228//
229// Returns a map from member name to the set of required traits.
230func (s *sdk) gatherTraits() map[string]android.SdkMemberTraitSet {
231 traitListByMember := map[string][]android.SdkMemberTrait{}
232 for _, memberListProperty := range s.memberTraitListProperties() {
233 names := memberListProperty.getter(s.dynamicMemberTraitListProperties)
234 for _, name := range names {
235 traitListByMember[name] = append(traitListByMember[name], memberListProperty.memberTrait)
236 }
237 }
238
239 traitSetByMember := map[string]android.SdkMemberTraitSet{}
240 for name, list := range traitListByMember {
241 traitSetByMember[name] = android.NewSdkMemberTraitSet(list)
242 }
243
244 return traitSetByMember
245}
246
Paul Duffin296701e2021-07-14 10:29:36 +0100247// newDependencyContext creates a new SdkDependencyContext for this sdk.
248func (s *sdk) newDependencyContext(mctx android.BottomUpMutatorContext) android.SdkDependencyContext {
Paul Duffind19f8942021-07-14 12:08:37 +0100249 traits := s.gatherTraits()
250
Paul Duffin296701e2021-07-14 10:29:36 +0100251 return &dependencyContext{
252 BottomUpMutatorContext: mctx,
Paul Duffind19f8942021-07-14 12:08:37 +0100253 requiredTraits: traits,
Paul Duffin296701e2021-07-14 10:29:36 +0100254 }
255}
256
257type dependencyContext struct {
258 android.BottomUpMutatorContext
Paul Duffind19f8942021-07-14 12:08:37 +0100259
260 // Map from member name to the set of traits that the sdk requires the member provides.
261 requiredTraits map[string]android.SdkMemberTraitSet
262}
263
264func (d *dependencyContext) RequiredTraits(name string) android.SdkMemberTraitSet {
265 if s, ok := d.requiredTraits[name]; ok {
266 return s
267 } else {
268 return android.EmptySdkMemberTraitSet()
269 }
270}
271
272func (d *dependencyContext) RequiresTrait(name string, trait android.SdkMemberTrait) bool {
273 return d.RequiredTraits(name).Contains(trait)
Paul Duffin296701e2021-07-14 10:29:36 +0100274}
275
276var _ android.SdkDependencyContext = (*dependencyContext)(nil)
277
Jiyong Parkd1063c12019-07-17 20:08:41 +0900278// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
279// interface and the sdk module type. This function has been made public to be called by tests
280// outside of the sdk package
281func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
282 ctx.BottomUp("SdkMember", memberMutator).Parallel()
283 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900284}
285
Jiyong Parkd1063c12019-07-17 20:08:41 +0900286type dependencyTag struct {
287 blueprint.BaseDependencyTag
288}
289
Martin Stjernholmcc776012020-07-07 03:22:21 +0100290// Mark this tag so dependencies that use it are excluded from APEX contents.
291func (t dependencyTag) ExcludeFromApexContents() {}
292
293var _ android.ExcludeFromApexContentsTag = dependencyTag{}
294
Jiyong Parkd1063c12019-07-17 20:08:41 +0900295// For dependencies from an in-development version of an SDK member to frozen versions of the same member
296// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffin573989d2021-03-17 13:25:29 +0000297//
298// The dependency represented by this tag requires that for every APEX variant created for the
299// `from` module that an equivalent APEX variant is created for the 'to' module. This is because an
300// APEX that requires a specific version of an sdk (via the `uses_sdks` property will replace
301// dependencies on the unversioned sdk member with a dependency on the appropriate versioned sdk
302// member. In order for that to work the versioned sdk member needs to have a variant for that APEX.
303// As it is not known at the time that the APEX variants are created which specific APEX variants of
304// a versioned sdk members will be required it is necessary for the versioned sdk members to have
305// variants for any APEX that it could be used within.
306//
307// If the APEX selects a versioned sdk member then it will not have a dependency on the `from`
308// module at all so any dependencies of that module will not affect the APEX. However, if the APEX
309// selects the unversioned sdk member then it must exclude all the versioned sdk members. In no
310// situation would this dependency cause the `to` module to be added to the APEX hence why this tag
311// also excludes the `to` module from being added to the APEX contents.
Paul Duffinfe149222020-01-14 14:06:09 +0000312type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900313 dependencyTag
314 member string
315 version string
316}
317
Paul Duffin573989d2021-03-17 13:25:29 +0000318func (t sdkMemberVersionedDepTag) AlwaysRequireApexVariant() bool {
319 return true
320}
321
Paul Duffinfe149222020-01-14 14:06:09 +0000322// Mark this tag so dependencies that use it are excluded from visibility enforcement.
323func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
324
Paul Duffin573989d2021-03-17 13:25:29 +0000325var _ android.AlwaysRequireApexVariantTag = sdkMemberVersionedDepTag{}
326
Jiyong Parkd1063c12019-07-17 20:08:41 +0900327// Step 1: create dependencies from an SDK module to its members.
328func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000329 if s, ok := mctx.Module().(*sdk); ok {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000330 // Add dependencies from enabled and non CommonOS variants to the sdk member variants.
331 if s.Enabled() && !s.IsCommonOSVariant() {
Paul Duffin296701e2021-07-14 10:29:36 +0100332 ctx := s.newDependencyContext(mctx)
Paul Duffin62782de2021-07-14 12:05:16 +0100333 for _, memberListProperty := range s.memberTypeListProperties() {
Paul Duffin13082052021-05-11 00:31:38 +0100334 if memberListProperty.getter == nil {
335 continue
336 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000337 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000338 if len(names) > 0 {
Paul Duffind19f8942021-07-14 12:08:37 +0100339 memberType := memberListProperty.memberType
340
341 // Verify that the member type supports the specified traits.
342 supportedTraits := memberType.SupportedTraits()
343 for _, name := range names {
344 requiredTraits := ctx.RequiredTraits(name)
345 unsupportedTraits := requiredTraits.Subtract(supportedTraits)
346 if !unsupportedTraits.Empty() {
347 ctx.ModuleErrorf("sdk member %q has traits %s that are unsupported by its member type %q", name, unsupportedTraits, memberType.SdkPropertyName())
348 }
349 }
350
351 // Add dependencies using the appropriate tag.
Paul Duffincc1b3da2020-02-27 13:29:56 +0000352 tag := memberListProperty.dependencyTag
Paul Duffind19f8942021-07-14 12:08:37 +0100353 memberType.AddDependencies(ctx, tag, names)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000354 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000355 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900356 }
357 }
358}
359
360// Step 2: record that dependencies of SDK modules are members of the SDK modules
361func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900362 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900363 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900364 if s.snapshot() && mySdkRef.Unversioned() {
365 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
366 "Did you manually modify Android.bp?")
367 }
368 if !s.snapshot() && !mySdkRef.Unversioned() {
369 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
370 }
371 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
372 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
373 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
374 }
375 }
376
Jiyong Parkd1063c12019-07-17 20:08:41 +0900377 mctx.VisitDirectDeps(func(child android.Module) {
378 if member, ok := child.(android.SdkAware); ok {
379 member.MakeMemberOf(mySdkRef)
380 }
381 })
382 }
383}
384
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000385// An interface that encapsulates all the functionality needed to manage the sdk dependencies.
386//
387// It is a mixture of apex and sdk module functionality.
388type sdkAndApexModule interface {
389 android.Module
390 android.DepIsInSameApex
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900391}