blob: 5b644fd85417e2829f1514a46c46b78cfd52ff4a [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
Jiyong Parkd1063c12019-07-17 20:08:41 +090021 "github.com/google/blueprint"
Jiyong Park100f3fd2019-11-06 16:03:32 +090022 "github.com/google/blueprint/proptools"
Jiyong Parkd1063c12019-07-17 20:08:41 +090023
24 "android/soong/android"
25 // This package doesn't depend on the apex package, but import it to make its mutators to be
26 // registered before mutators in this package. See RegisterPostDepsMutators for more details.
27 _ "android/soong/apex"
28)
29
30func init() {
Jiyong Park232e7852019-11-04 12:23:40 +090031 pctx.Import("android/soong/android")
Paul Duffin375058f2019-11-29 20:17:53 +000032 pctx.Import("android/soong/java/config")
33
Paul Duffin6d9108f02021-03-09 22:59:28 +000034 registerSdkBuildComponents(android.InitRegistrationContext)
35}
36
37func registerSdkBuildComponents(ctx android.RegistrationContext) {
38 ctx.RegisterModuleType("sdk", SdkModuleFactory)
39 ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090040}
41
42type sdk struct {
43 android.ModuleBase
44 android.DefaultableModuleBase
45
Paul Duffin255f18e2019-12-13 11:22:16 +000046 // The dynamically generated information about the registered SdkMemberType
47 dynamicSdkMemberTypes *dynamicSdkMemberTypes
48
Paul Duffin62782de2021-07-14 12:05:16 +010049 // The dynamically created instance of the properties struct containing the sdk member type
Paul Duffin255f18e2019-12-13 11:22:16 +000050 // list properties, e.g. java_libs.
51 dynamicMemberTypeListProperties interface{}
52
Paul Duffind19f8942021-07-14 12:08:37 +010053 // The dynamically generated information about the registered SdkMemberTrait
54 dynamicSdkMemberTraits *dynamicSdkMemberTraits
55
56 // The dynamically created instance of the properties struct containing the sdk member trait
57 // list properties.
58 dynamicMemberTraitListProperties interface{}
59
Paul Duffin21827262021-04-24 12:16:36 +010060 // Information about the OsType specific member variants depended upon by this variant.
Paul Duffin1356d8c2020-02-25 19:26:33 +000061 //
Paul Duffin6a7e9532020-03-20 17:50:07 +000062 // Set by OsType specific variants in the collectMembers() method and used by the
63 // CommonOS variant when building the snapshot. That work is all done on separate
64 // calls to the sdk.GenerateAndroidBuildActions method which is guaranteed to be
65 // called for the OsType specific variants before the CommonOS variant (because
66 // the latter depends on the former).
Paul Duffin21827262021-04-24 12:16:36 +010067 memberVariantDeps []sdkMemberVariantDep
Paul Duffin1356d8c2020-02-25 19:26:33 +000068
Paul Duffin6a7e9532020-03-20 17:50:07 +000069 // The multilib variants that are used by this sdk variant.
70 multilibUsages multilibUsage
71
Jiyong Parkd1063c12019-07-17 20:08:41 +090072 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090073
Jiyong Park232e7852019-11-04 12:23:40 +090074 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000075
Paul Duffinc6ba1822022-05-06 09:38:02 +000076 infoFile android.OptionalPath
77
Paul Duffinac37c502019-11-26 18:02:20 +000078 // The builder, preserved for testing.
79 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090080}
81
82type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090083 Snapshot bool `blueprint:"mutated"`
Paul Duffin8150da62019-12-16 17:21:27 +000084
85 // True if this is a module_exports (or module_exports_snapshot) module type.
86 Module_exports bool `blueprint:"mutated"`
Paul Duffin157f40f2020-09-29 16:01:08 +010087
88 // The additional visibility to add to the prebuilt modules to allow them to
89 // reference each other.
90 //
91 // This can only be used to widen the visibility of the members:
92 //
93 // * Specifying //visibility:public here will make all members visible and
94 // essentially ignore their own visibility.
95 // * Specifying //visibility:private here is an error.
96 // * Specifying any other rule here will add it to the members visibility and
97 // be output to the member prebuilt in the snapshot. Duplicates will be
98 // dropped. Adding a rule to members that have //visibility:private will
99 // cause the //visibility:private to be discarded.
100 Prebuilt_visibility []string
Jiyong Parkd1063c12019-07-17 20:08:41 +0900101}
102
103// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
104// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000105func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000106 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000107}
Paul Duffin255f18e2019-12-13 11:22:16 +0000108
Paul Duffine6029182019-12-16 17:43:48 +0000109func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000110 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000111 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000112 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffin30c830b2021-09-22 11:49:47 +0100113 sdkMemberTypeKey, sdkMemberTypes := android.RegisteredSdkMemberTypes(moduleExports)
114 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(sdkMemberTypeKey, sdkMemberTypes)
Paul Duffin255f18e2019-12-13 11:22:16 +0000115 // Create an instance of the dynamically created struct that contains all the
116 // properties for the member type specific list properties.
Paul Duffin62782de2021-07-14 12:05:16 +0100117 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberTypeListProperties()
Paul Duffind19f8942021-07-14 12:08:37 +0100118
Paul Duffin30c830b2021-09-22 11:49:47 +0100119 sdkMemberTraitsKey, sdkMemberTraits := android.RegisteredSdkMemberTraits()
120 s.dynamicSdkMemberTraits = getDynamicSdkMemberTraits(sdkMemberTraitsKey, sdkMemberTraits)
Paul Duffind19f8942021-07-14 12:08:37 +0100121 // Create an instance of the dynamically created struct that contains all the properties for the
122 // member trait specific list properties.
123 s.dynamicMemberTraitListProperties = s.dynamicSdkMemberTraits.createMemberTraitListProperties()
124
125 // Create a wrapper around the dynamic trait specific properties so that they have to be
126 // specified within a traits:{} section in the .bp file.
127 traitsWrapper := struct {
128 Traits interface{}
129 }{s.dynamicMemberTraitListProperties}
130
131 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties, &traitsWrapper)
Paul Duffin157f40f2020-09-29 16:01:08 +0100132
133 // Make sure that the prebuilt visibility property is verified for errors.
134 android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000135 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900136 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900137 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
138 type props struct {
139 Compile_multilib *string
140 }
141 p := &props{Compile_multilib: proptools.StringPtr("both")}
Martin Stjernholm26ab8e82020-06-30 20:34:00 +0100142 ctx.PrependProperties(p)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900143 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900144 return s
145}
146
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000147// sdk_snapshot is a snapshot of an SDK. This is an auto-generated module.
Jiyong Park9b409bc2019-10-11 14:59:13 +0900148func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000149 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000150 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900151 return s
152}
153
Paul Duffin62782de2021-07-14 12:05:16 +0100154func (s *sdk) memberTypeListProperties() []*sdkMemberTypeListProperty {
155 return s.dynamicSdkMemberTypes.memberTypeListProperties
Paul Duffin72910952020-01-20 18:16:30 +0000156}
157
Paul Duffin62782de2021-07-14 12:05:16 +0100158func (s *sdk) memberTypeListProperty(memberType android.SdkMemberType) *sdkMemberTypeListProperty {
Paul Duffincd064672021-04-24 00:47:29 +0100159 return s.dynamicSdkMemberTypes.memberTypeToProperty[memberType]
160}
161
Paul Duffind19f8942021-07-14 12:08:37 +0100162// memberTraitListProperties returns the list of *sdkMemberTraitListProperty instances for this sdk.
163func (s *sdk) memberTraitListProperties() []*sdkMemberTraitListProperty {
164 return s.dynamicSdkMemberTraits.memberTraitListProperties
165}
166
Jiyong Park9b409bc2019-10-11 14:59:13 +0900167func (s *sdk) snapshot() bool {
168 return s.properties.Snapshot
169}
170
Jiyong Parkd1063c12019-07-17 20:08:41 +0900171func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000172 if s.snapshot() {
Jiyong Park232e7852019-11-04 12:23:40 +0900173 // We don't need to create a snapshot out of sdk_snapshot.
174 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000175 return
176 }
177
178 // This method is guaranteed to be called on OsType specific variants before it is called
179 // on their corresponding CommonOS variant.
180 if !s.IsCommonOSVariant() {
Paul Duffin6a7e9532020-03-20 17:50:07 +0000181 // Update the OsType specific sdk variant with information about its members.
182 s.collectMembers(ctx)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000183 } else {
184 // Get the OsType specific variants on which the CommonOS depends.
185 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
186 var sdkVariants []*sdk
187 for _, m := range osSpecificVariants {
188 if sdkVariant, ok := m.(*sdk); ok {
189 sdkVariants = append(sdkVariants, sdkVariant)
190 }
191 }
192
193 // Generate the snapshot from the member info.
Paul Duffinc6ba1822022-05-06 09:38:02 +0000194 s.buildSnapshot(ctx, sdkVariants)
Jiyong Park232e7852019-11-04 12:23:40 +0900195 }
mrziwangd40d3682024-06-18 15:15:38 -0700196
197 if s.snapshotFile.Valid() {
198 ctx.SetOutputFiles([]android.Path{s.snapshotFile.Path()}, "")
199 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900200}
201
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900202func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000203 if !s.snapshotFile.Valid() != !s.infoFile.Valid() {
204 panic("Snapshot (%q) and info file (%q) should both be set or neither should be set.")
205 } else if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900206 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900207 }
208
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900209 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900210 Class: "FAKE",
211 OutputFile: s.snapshotFile,
Paul Duffinc6ba1822022-05-06 09:38:02 +0000212 DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path(), s.infoFile.Path()),
Jiyong Park232e7852019-11-04 12:23:40 +0900213 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000214 ExtraFooters: []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800215 func(w io.Writer, name, prefix, moduleDir string) {
Paul Duffin504b4612019-11-22 14:52:29 +0000216 // Allow the sdk to be built by simply passing its name on the command line.
217 fmt.Fprintln(w, ".PHONY:", s.Name())
218 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
Paul Duffinc6ba1822022-05-06 09:38:02 +0000219
220 // Allow the sdk info to be built by simply passing its name on the command line.
221 infoTarget := s.Name() + ".info"
222 fmt.Fprintln(w, ".PHONY:", infoTarget)
223 fmt.Fprintln(w, infoTarget+":", s.infoFile.String())
Paul Duffin504b4612019-11-22 14:52:29 +0000224 },
225 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900226 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900227}
228
Paul Duffind19f8942021-07-14 12:08:37 +0100229// gatherTraits gathers the traits from the dynamically generated trait specific properties.
230//
231// Returns a map from member name to the set of required traits.
232func (s *sdk) gatherTraits() map[string]android.SdkMemberTraitSet {
233 traitListByMember := map[string][]android.SdkMemberTrait{}
234 for _, memberListProperty := range s.memberTraitListProperties() {
235 names := memberListProperty.getter(s.dynamicMemberTraitListProperties)
236 for _, name := range names {
237 traitListByMember[name] = append(traitListByMember[name], memberListProperty.memberTrait)
238 }
239 }
240
241 traitSetByMember := map[string]android.SdkMemberTraitSet{}
242 for name, list := range traitListByMember {
243 traitSetByMember[name] = android.NewSdkMemberTraitSet(list)
244 }
245
246 return traitSetByMember
247}
248
Paul Duffin296701e2021-07-14 10:29:36 +0100249// newDependencyContext creates a new SdkDependencyContext for this sdk.
250func (s *sdk) newDependencyContext(mctx android.BottomUpMutatorContext) android.SdkDependencyContext {
Paul Duffind19f8942021-07-14 12:08:37 +0100251 traits := s.gatherTraits()
252
Paul Duffin296701e2021-07-14 10:29:36 +0100253 return &dependencyContext{
254 BottomUpMutatorContext: mctx,
Paul Duffind19f8942021-07-14 12:08:37 +0100255 requiredTraits: traits,
Paul Duffin296701e2021-07-14 10:29:36 +0100256 }
257}
258
259type dependencyContext struct {
260 android.BottomUpMutatorContext
Paul Duffind19f8942021-07-14 12:08:37 +0100261
262 // Map from member name to the set of traits that the sdk requires the member provides.
263 requiredTraits map[string]android.SdkMemberTraitSet
264}
265
266func (d *dependencyContext) RequiredTraits(name string) android.SdkMemberTraitSet {
267 if s, ok := d.requiredTraits[name]; ok {
268 return s
269 } else {
270 return android.EmptySdkMemberTraitSet()
271 }
272}
273
274func (d *dependencyContext) RequiresTrait(name string, trait android.SdkMemberTrait) bool {
275 return d.RequiredTraits(name).Contains(trait)
Paul Duffin296701e2021-07-14 10:29:36 +0100276}
277
278var _ android.SdkDependencyContext = (*dependencyContext)(nil)
279
Jiyong Parkd1063c12019-07-17 20:08:41 +0900280type dependencyTag struct {
281 blueprint.BaseDependencyTag
282}
283
Martin Stjernholmcc776012020-07-07 03:22:21 +0100284// Mark this tag so dependencies that use it are excluded from APEX contents.
285func (t dependencyTag) ExcludeFromApexContents() {}
286
287var _ android.ExcludeFromApexContentsTag = dependencyTag{}
288
Paul Duffin2156abe2022-11-23 17:59:00 +0000289func (s *sdk) DepsMutator(mctx android.BottomUpMutatorContext) {
290 // Add dependencies from non CommonOS variants to the sdk member variants.
291 if s.IsCommonOSVariant() {
292 return
293 }
Paul Duffind19f8942021-07-14 12:08:37 +0100294
Paul Duffin2156abe2022-11-23 17:59:00 +0000295 ctx := s.newDependencyContext(mctx)
296 for _, memberListProperty := range s.memberTypeListProperties() {
297 if memberListProperty.getter == nil {
298 continue
299 }
300 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
301 if len(names) > 0 {
302 memberType := memberListProperty.memberType
Paul Duffind19f8942021-07-14 12:08:37 +0100303
Paul Duffin2156abe2022-11-23 17:59:00 +0000304 // Verify that the member type supports the specified traits.
305 supportedTraits := memberType.SupportedTraits()
306 for _, name := range names {
307 requiredTraits := ctx.RequiredTraits(name)
308 unsupportedTraits := requiredTraits.Subtract(supportedTraits)
309 if !unsupportedTraits.Empty() {
310 ctx.ModuleErrorf("sdk member %q has traits %s that are unsupported by its member type %q",
311 name, unsupportedTraits, memberType.SdkPropertyName())
Paul Duffincc1b3da2020-02-27 13:29:56 +0000312 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000313 }
Paul Duffin2156abe2022-11-23 17:59:00 +0000314
315 // Add dependencies using the appropriate tag.
316 tag := memberListProperty.dependencyTag
317 memberType.AddDependencies(ctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900318 }
319 }
320}