blob: f22763c10f2646fffe8da944af0ac38c43fd6c15 [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 +000068// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000069// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000070type sdkMemberListProperty struct {
Paul Duffin13879572019-11-28 14:31:38 +000071 // getter for the list of member names
Paul Duffin255f18e2019-12-13 11:22:16 +000072 getter func(properties interface{}) []string
Paul Duffin13879572019-11-28 14:31:38 +000073
74 // the type of member referenced in the list
75 memberType android.SdkMemberType
76
Paul Duffin255f18e2019-12-13 11:22:16 +000077 // the dependency tag used for items in this list that can be used to determine the memberType
78 // for a resolved dependency.
Paul Duffinf8539922019-11-19 19:44:10 +000079 dependencyTag android.SdkMemberTypeDependencyTag
Paul Duffin13879572019-11-28 14:31:38 +000080}
81
Paul Duffin255f18e2019-12-13 11:22:16 +000082func (p *sdkMemberListProperty) propertyName() string {
83 return p.memberType.SdkPropertyName()
84}
85
86// Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
87// to a slice of SdkMemberType instances held in android.SdkMemberTypes.
88var dynamicSdkMemberTypesMap android.OncePer
89
90// A dynamically generated set of member list properties and associated structure type.
91type dynamicSdkMemberTypes struct {
92 // The dynamically generated structure type.
93 //
94 // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
95 // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
96 propertiesStructType reflect.Type
97
98 // Information about each of the member type specific list properties.
99 memberListProperties []*sdkMemberListProperty
100}
101
102func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} {
103 return reflect.New(d.propertiesStructType).Interface()
104}
105
106func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes {
107
108 // Get a key that uniquely identifies the registry contents.
109 key := registry.UniqueOnceKey()
110
111 // Get the registered types.
112 registeredTypes := registry.RegisteredTypes()
113
114 // Get the cached value, creating new instance if necessary.
115 return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
116 return createDynamicSdkMemberTypes(registeredTypes)
117 }).(*dynamicSdkMemberTypes)
118}
119
120// Create the dynamicSdkMemberTypes from the list of registered member types.
Paul Duffina6e737b2019-11-29 11:55:51 +0000121//
Paul Duffin255f18e2019-12-13 11:22:16 +0000122// A struct is created which contains one exported field per member type corresponding to
123// the SdkMemberType.SdkPropertyName() value.
124//
125// A list of sdkMemberListProperty instances is created, one per member type that provides:
126// * a reference to the member type.
127// * a getter for the corresponding field in the properties struct.
128// * a dependency tag that identifies the member type of a resolved dependency.
129//
130func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
Paul Duffine6029182019-12-16 17:43:48 +0000131
Paul Duffin255f18e2019-12-13 11:22:16 +0000132 var listProperties []*sdkMemberListProperty
133 var fields []reflect.StructField
134
135 // Iterate over the member types creating StructField and sdkMemberListProperty objects.
136 for f, memberType := range sdkMemberTypes {
137 p := memberType.SdkPropertyName()
138
139 // Create a dynamic exported field for the member type's property.
140 fields = append(fields, reflect.StructField{
141 Name: proptools.FieldNameForProperty(p),
142 Type: reflect.TypeOf([]string{}),
143 })
144
145 // Copy the field index for use in the getter func as using the loop variable directly will
146 // cause all funcs to use the last value.
147 fieldIndex := f
148
149 // Create an sdkMemberListProperty for the member type.
150 memberListProperty := &sdkMemberListProperty{
151 getter: func(properties interface{}) []string {
152 // The properties is expected to be of the following form (where
153 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
154 // properties *struct {<Module_types> []string, ....}
155 //
156 // Although it accesses the field by index the following reflection code is equivalent to:
157 // *properties.<Module_types>
158 //
159 list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
160 return list
161 },
162
163 memberType: memberType,
164
Paul Duffinf8539922019-11-19 19:44:10 +0000165 dependencyTag: android.DependencyTagForSdkMemberType(memberType),
Paul Duffin255f18e2019-12-13 11:22:16 +0000166 }
167
168 listProperties = append(listProperties, memberListProperty)
169 }
170
171 // Create a dynamic struct from the collated fields.
172 propertiesStructType := reflect.StructOf(fields)
173
174 return &dynamicSdkMemberTypes{
175 memberListProperties: listProperties,
176 propertiesStructType: propertiesStructType,
177 }
Paul Duffin13879572019-11-28 14:31:38 +0000178}
179
Jiyong Parkd1063c12019-07-17 20:08:41 +0900180// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
181// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000182func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000183 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000184}
Paul Duffin255f18e2019-12-13 11:22:16 +0000185
Paul Duffine6029182019-12-16 17:43:48 +0000186func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000187 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000188 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000189 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffine6029182019-12-16 17:43:48 +0000190 var registry *android.SdkMemberTypesRegistry
191 if moduleExports {
192 registry = android.ModuleExportsMemberTypes
193 } else {
194 registry = android.SdkMemberTypes
195 }
196 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry)
Paul Duffin255f18e2019-12-13 11:22:16 +0000197 // Create an instance of the dynamically created struct that contains all the
198 // properties for the member type specific list properties.
199 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffin255f18e2019-12-13 11:22:16 +0000200 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900201 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
202 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900203 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
204 type props struct {
205 Compile_multilib *string
206 }
207 p := &props{Compile_multilib: proptools.StringPtr("both")}
208 ctx.AppendProperties(p)
209 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900210 return s
211}
212
Jiyong Park9b409bc2019-10-11 14:59:13 +0900213// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
214func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000215 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000216 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900217 return s
218}
219
220func (s *sdk) snapshot() bool {
221 return s.properties.Snapshot
222}
223
Jiyong Parkd1063c12019-07-17 20:08:41 +0900224func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900225 if !s.snapshot() {
226 // We don't need to create a snapshot out of sdk_snapshot.
227 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
228 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
229 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900230}
231
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900232func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900233 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900234 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900235 }
236
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900237 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900238 Class: "FAKE",
239 OutputFile: s.snapshotFile,
240 DistFile: s.snapshotFile,
241 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000242 ExtraFooters: []android.AndroidMkExtraFootersFunc{
243 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
244 // Allow the sdk to be built by simply passing its name on the command line.
245 fmt.Fprintln(w, ".PHONY:", s.Name())
246 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
247 },
248 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900249 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900250}
251
252// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
253// interface and the sdk module type. This function has been made public to be called by tests
254// outside of the sdk package
255func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
256 ctx.BottomUp("SdkMember", memberMutator).Parallel()
257 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
258 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
259}
260
261// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
262// interface and the sdk module type. This function has been made public to be called by tests
263// outside of the sdk package
264func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
265 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
266 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
267 // APEX to its dependents. Since different versions of the same SDK can be used by different
268 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
269 // should have been mutated for the apex before the SDK requirements are set.
270 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
271 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900272 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900273}
274
275type dependencyTag struct {
276 blueprint.BaseDependencyTag
277}
278
Jiyong Parkd1063c12019-07-17 20:08:41 +0900279// For dependencies from an in-development version of an SDK member to frozen versions of the same member
280// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffinfe149222020-01-14 14:06:09 +0000281type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900282 dependencyTag
283 member string
284 version string
285}
286
Paul Duffinfe149222020-01-14 14:06:09 +0000287// Mark this tag so dependencies that use it are excluded from visibility enforcement.
288func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
289
Jiyong Parkd1063c12019-07-17 20:08:41 +0900290// Step 1: create dependencies from an SDK module to its members.
291func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000292 if s, ok := mctx.Module().(*sdk); ok {
293 for _, memberListProperty := range s.dynamicSdkMemberTypes.memberListProperties {
294 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000295 tag := memberListProperty.dependencyTag
296 memberListProperty.memberType.AddDependencies(mctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900297 }
298 }
299}
300
301// Step 2: record that dependencies of SDK modules are members of the SDK modules
302func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900303 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900304 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900305 if s.snapshot() && mySdkRef.Unversioned() {
306 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
307 "Did you manually modify Android.bp?")
308 }
309 if !s.snapshot() && !mySdkRef.Unversioned() {
310 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
311 }
312 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
313 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
314 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
315 }
316 }
317
Jiyong Parkd1063c12019-07-17 20:08:41 +0900318 mctx.VisitDirectDeps(func(child android.Module) {
319 if member, ok := child.(android.SdkAware); ok {
320 member.MakeMemberOf(mySdkRef)
321 }
322 })
323 }
324}
325
Jiyong Park9b409bc2019-10-11 14:59:13 +0900326// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900327// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
328// (apex and apk), each of which might want different sdks to be built with. For example, if both
329// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
330// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
331// using.
332func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
333 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900334 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900335 memberName := m.MemberName()
Paul Duffinfe149222020-01-14 14:06:09 +0000336 tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900337 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
338 }
339 }
340}
341
342// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
343// descendants
344func sdkDepsMutator(mctx android.TopDownMutatorContext) {
345 if m, ok := mctx.Module().(android.SdkAware); ok {
346 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
347 // by reading its own properties like `uses_sdks`.
348 requiredSdks := m.RequiredSdks()
349 if len(requiredSdks) > 0 {
350 mctx.VisitDirectDeps(func(m android.Module) {
351 if dep, ok := m.(android.SdkAware); ok {
352 dep.BuildWithSdks(requiredSdks)
353 }
354 })
355 }
356 }
357}
358
359// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
360// versioned module is used instead of the un-versioned (in-development) module libfoo
361func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
362 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900363 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900364 if m.RequiredSdks().Contains(sdk) {
365 // Note that this replacement is done only for the modules that have the same
366 // variations as the current module. Since current module is already mutated for
367 // apex references in other APEXes are not affected by this replacement.
368 memberName := m.MemberName()
369 mctx.ReplaceDependencies(memberName)
370 }
371 }
372 }
373}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900374
375// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
376func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
377 if m, ok := mctx.Module().(interface {
378 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
379 RequiredSdks() android.SdkRefs
380 }); ok {
381 requiredSdks := m.RequiredSdks()
382 if len(requiredSdks) == 0 {
383 return
384 }
385 mctx.VisitDirectDeps(func(dep android.Module) {
386 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
387 // dependency to defaults is always okay
388 return
389 }
390
391 // If the dep is from outside of the APEX, but is not in any of the
392 // required SDKs, we know that the dep is a violation.
393 if sa, ok := dep.(android.SdkAware); ok {
394 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
395 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
396 sa.Name(), sa.ContainingSdk(), requiredSdks)
397 }
398 }
399 })
400 }
401}