blob: 3b0752ff73fddf8852687f7cd7dfbdf70a8af6c0 [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"
Paul Duffin255f18e2019-12-13 11:22:16 +000020 "reflect"
Jiyong Park9b409bc2019-10-11 14:59:13 +090021 "strconv"
22
Jiyong Parkd1063c12019-07-17 20:08:41 +090023 "github.com/google/blueprint"
Jiyong Park100f3fd2019-11-06 16:03:32 +090024 "github.com/google/blueprint/proptools"
Jiyong Parkd1063c12019-07-17 20:08:41 +090025
26 "android/soong/android"
27 // This package doesn't depend on the apex package, but import it to make its mutators to be
28 // registered before mutators in this package. See RegisterPostDepsMutators for more details.
29 _ "android/soong/apex"
30)
31
32func init() {
Jiyong Park232e7852019-11-04 12:23:40 +090033 pctx.Import("android/soong/android")
Paul Duffin375058f2019-11-29 20:17:53 +000034 pctx.Import("android/soong/java/config")
35
Paul Duffin8150da62019-12-16 17:21:27 +000036 android.RegisterModuleType("sdk", SdkModuleFactory)
Jiyong Park9b409bc2019-10-11 14:59:13 +090037 android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090038 android.PreDepsMutators(RegisterPreDepsMutators)
39 android.PostDepsMutators(RegisterPostDepsMutators)
40}
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
49 // The dynamically created instance of the properties struct containing the sdk member
50 // list properties, e.g. java_libs.
51 dynamicMemberTypeListProperties interface{}
52
Jiyong Parkd1063c12019-07-17 20:08:41 +090053 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090054
Jiyong Park232e7852019-11-04 12:23:40 +090055 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000056
57 // The builder, preserved for testing.
58 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090059}
60
61type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090062 Snapshot bool `blueprint:"mutated"`
Paul Duffin8150da62019-12-16 17:21:27 +000063
64 // True if this is a module_exports (or module_exports_snapshot) module type.
65 Module_exports bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090066}
67
Paul Duffin13879572019-11-28 14:31:38 +000068type sdkMemberDependencyTag struct {
69 blueprint.BaseDependencyTag
Paul Duffin255f18e2019-12-13 11:22:16 +000070 memberType android.SdkMemberType
Paul Duffin13879572019-11-28 14:31:38 +000071}
72
73// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000074// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000075type sdkMemberListProperty struct {
Paul Duffin13879572019-11-28 14:31:38 +000076 // getter for the list of member names
Paul Duffin255f18e2019-12-13 11:22:16 +000077 getter func(properties interface{}) []string
Paul Duffin13879572019-11-28 14:31:38 +000078
79 // the type of member referenced in the list
80 memberType android.SdkMemberType
81
Paul Duffin255f18e2019-12-13 11:22:16 +000082 // the dependency tag used for items in this list that can be used to determine the memberType
83 // for a resolved dependency.
Paul Duffin13879572019-11-28 14:31:38 +000084 dependencyTag *sdkMemberDependencyTag
85}
86
Paul Duffin255f18e2019-12-13 11:22:16 +000087func (p *sdkMemberListProperty) propertyName() string {
88 return p.memberType.SdkPropertyName()
89}
90
91// Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
92// to a slice of SdkMemberType instances held in android.SdkMemberTypes.
93var dynamicSdkMemberTypesMap android.OncePer
94
95// A dynamically generated set of member list properties and associated structure type.
96type dynamicSdkMemberTypes struct {
97 // The dynamically generated structure type.
98 //
99 // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
100 // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
101 propertiesStructType reflect.Type
102
103 // Information about each of the member type specific list properties.
104 memberListProperties []*sdkMemberListProperty
105}
106
107func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} {
108 return reflect.New(d.propertiesStructType).Interface()
109}
110
111func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes {
112
113 // Get a key that uniquely identifies the registry contents.
114 key := registry.UniqueOnceKey()
115
116 // Get the registered types.
117 registeredTypes := registry.RegisteredTypes()
118
119 // Get the cached value, creating new instance if necessary.
120 return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
121 return createDynamicSdkMemberTypes(registeredTypes)
122 }).(*dynamicSdkMemberTypes)
123}
124
125// Create the dynamicSdkMemberTypes from the list of registered member types.
Paul Duffina6e737b2019-11-29 11:55:51 +0000126//
Paul Duffin255f18e2019-12-13 11:22:16 +0000127// A struct is created which contains one exported field per member type corresponding to
128// the SdkMemberType.SdkPropertyName() value.
129//
130// A list of sdkMemberListProperty instances is created, one per member type that provides:
131// * a reference to the member type.
132// * a getter for the corresponding field in the properties struct.
133// * a dependency tag that identifies the member type of a resolved dependency.
134//
135func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
Paul Duffine6029182019-12-16 17:43:48 +0000136
Paul Duffin255f18e2019-12-13 11:22:16 +0000137 var listProperties []*sdkMemberListProperty
138 var fields []reflect.StructField
139
140 // Iterate over the member types creating StructField and sdkMemberListProperty objects.
141 for f, memberType := range sdkMemberTypes {
142 p := memberType.SdkPropertyName()
143
144 // Create a dynamic exported field for the member type's property.
145 fields = append(fields, reflect.StructField{
146 Name: proptools.FieldNameForProperty(p),
147 Type: reflect.TypeOf([]string{}),
148 })
149
150 // Copy the field index for use in the getter func as using the loop variable directly will
151 // cause all funcs to use the last value.
152 fieldIndex := f
153
154 // Create an sdkMemberListProperty for the member type.
155 memberListProperty := &sdkMemberListProperty{
156 getter: func(properties interface{}) []string {
157 // The properties is expected to be of the following form (where
158 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
159 // properties *struct {<Module_types> []string, ....}
160 //
161 // Although it accesses the field by index the following reflection code is equivalent to:
162 // *properties.<Module_types>
163 //
164 list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
165 return list
166 },
167
168 memberType: memberType,
169
170 dependencyTag: &sdkMemberDependencyTag{
171 memberType: memberType,
172 },
173 }
174
175 listProperties = append(listProperties, memberListProperty)
176 }
177
178 // Create a dynamic struct from the collated fields.
179 propertiesStructType := reflect.StructOf(fields)
180
181 return &dynamicSdkMemberTypes{
182 memberListProperties: listProperties,
183 propertiesStructType: propertiesStructType,
184 }
Paul Duffin13879572019-11-28 14:31:38 +0000185}
186
Jiyong Parkd1063c12019-07-17 20:08:41 +0900187// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
188// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000189func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000190 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000191}
Paul Duffin255f18e2019-12-13 11:22:16 +0000192
Paul Duffine6029182019-12-16 17:43:48 +0000193func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000194 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000195 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000196 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffine6029182019-12-16 17:43:48 +0000197 var registry *android.SdkMemberTypesRegistry
198 if moduleExports {
199 registry = android.ModuleExportsMemberTypes
200 } else {
201 registry = android.SdkMemberTypes
202 }
203 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry)
Paul Duffin255f18e2019-12-13 11:22:16 +0000204 // Create an instance of the dynamically created struct that contains all the
205 // properties for the member type specific list properties.
206 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffin255f18e2019-12-13 11:22:16 +0000207 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900208 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
209 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900210 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
211 type props struct {
212 Compile_multilib *string
213 }
214 p := &props{Compile_multilib: proptools.StringPtr("both")}
215 ctx.AppendProperties(p)
216 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900217 return s
218}
219
Jiyong Park9b409bc2019-10-11 14:59:13 +0900220// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
221func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000222 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000223 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900224 return s
225}
226
227func (s *sdk) snapshot() bool {
228 return s.properties.Snapshot
229}
230
Jiyong Parkd1063c12019-07-17 20:08:41 +0900231func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900232 if !s.snapshot() {
233 // We don't need to create a snapshot out of sdk_snapshot.
234 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
235 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
236 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900237}
238
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900239func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900240 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900241 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900242 }
243
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900244 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900245 Class: "FAKE",
246 OutputFile: s.snapshotFile,
247 DistFile: s.snapshotFile,
248 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000249 ExtraFooters: []android.AndroidMkExtraFootersFunc{
250 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
251 // Allow the sdk to be built by simply passing its name on the command line.
252 fmt.Fprintln(w, ".PHONY:", s.Name())
253 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
254 },
255 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900256 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900257}
258
259// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
260// interface and the sdk module type. This function has been made public to be called by tests
261// outside of the sdk package
262func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
263 ctx.BottomUp("SdkMember", memberMutator).Parallel()
264 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
265 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
266}
267
268// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
269// interface and the sdk module type. This function has been made public to be called by tests
270// outside of the sdk package
271func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
272 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
273 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
274 // APEX to its dependents. Since different versions of the same SDK can be used by different
275 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
276 // should have been mutated for the apex before the SDK requirements are set.
277 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
278 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900279 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900280}
281
282type dependencyTag struct {
283 blueprint.BaseDependencyTag
284}
285
Jiyong Parkd1063c12019-07-17 20:08:41 +0900286// For dependencies from an in-development version of an SDK member to frozen versions of the same member
287// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffinfe149222020-01-14 14:06:09 +0000288type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900289 dependencyTag
290 member string
291 version string
292}
293
Paul Duffinfe149222020-01-14 14:06:09 +0000294// Mark this tag so dependencies that use it are excluded from visibility enforcement.
295func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
296
Jiyong Parkd1063c12019-07-17 20:08:41 +0900297// Step 1: create dependencies from an SDK module to its members.
298func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000299 if s, ok := mctx.Module().(*sdk); ok {
300 for _, memberListProperty := range s.dynamicSdkMemberTypes.memberListProperties {
301 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000302 tag := memberListProperty.dependencyTag
303 memberListProperty.memberType.AddDependencies(mctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900304 }
305 }
306}
307
308// Step 2: record that dependencies of SDK modules are members of the SDK modules
309func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900310 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900311 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900312 if s.snapshot() && mySdkRef.Unversioned() {
313 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
314 "Did you manually modify Android.bp?")
315 }
316 if !s.snapshot() && !mySdkRef.Unversioned() {
317 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
318 }
319 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
320 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
321 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
322 }
323 }
324
Jiyong Parkd1063c12019-07-17 20:08:41 +0900325 mctx.VisitDirectDeps(func(child android.Module) {
326 if member, ok := child.(android.SdkAware); ok {
327 member.MakeMemberOf(mySdkRef)
328 }
329 })
330 }
331}
332
Jiyong Park9b409bc2019-10-11 14:59:13 +0900333// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900334// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
335// (apex and apk), each of which might want different sdks to be built with. For example, if both
336// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
337// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
338// using.
339func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
340 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900341 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900342 memberName := m.MemberName()
Paul Duffinfe149222020-01-14 14:06:09 +0000343 tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900344 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
345 }
346 }
347}
348
349// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
350// descendants
351func sdkDepsMutator(mctx android.TopDownMutatorContext) {
352 if m, ok := mctx.Module().(android.SdkAware); ok {
353 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
354 // by reading its own properties like `uses_sdks`.
355 requiredSdks := m.RequiredSdks()
356 if len(requiredSdks) > 0 {
357 mctx.VisitDirectDeps(func(m android.Module) {
358 if dep, ok := m.(android.SdkAware); ok {
359 dep.BuildWithSdks(requiredSdks)
360 }
361 })
362 }
363 }
364}
365
366// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
367// versioned module is used instead of the un-versioned (in-development) module libfoo
368func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
369 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900370 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900371 if m.RequiredSdks().Contains(sdk) {
372 // Note that this replacement is done only for the modules that have the same
373 // variations as the current module. Since current module is already mutated for
374 // apex references in other APEXes are not affected by this replacement.
375 memberName := m.MemberName()
376 mctx.ReplaceDependencies(memberName)
377 }
378 }
379 }
380}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900381
382// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
383func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
384 if m, ok := mctx.Module().(interface {
385 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
386 RequiredSdks() android.SdkRefs
387 }); ok {
388 requiredSdks := m.RequiredSdks()
389 if len(requiredSdks) == 0 {
390 return
391 }
392 mctx.VisitDirectDeps(func(dep android.Module) {
393 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
394 // dependency to defaults is always okay
395 return
396 }
397
398 // If the dep is from outside of the APEX, but is not in any of the
399 // required SDKs, we know that the dep is a violation.
400 if sa, ok := dep.(android.SdkAware); ok {
401 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
402 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
403 sa.Name(), sa.ContainingSdk(), requiredSdks)
404 }
405 }
406 })
407 }
408}