blob: 62bc06fd5b2ea1b87e5600c5098b2c50841dc554 [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
Jiyong Parkd1063c12019-07-17 20:08:41 +090036 android.RegisterModuleType("sdk", ModuleFactory)
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"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090063}
64
Paul Duffin13879572019-11-28 14:31:38 +000065type sdkMemberDependencyTag struct {
66 blueprint.BaseDependencyTag
Paul Duffin255f18e2019-12-13 11:22:16 +000067 memberType android.SdkMemberType
Paul Duffin13879572019-11-28 14:31:38 +000068}
69
70// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000071// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000072type sdkMemberListProperty struct {
Paul Duffin13879572019-11-28 14:31:38 +000073 // getter for the list of member names
Paul Duffin255f18e2019-12-13 11:22:16 +000074 getter func(properties interface{}) []string
Paul Duffin13879572019-11-28 14:31:38 +000075
76 // the type of member referenced in the list
77 memberType android.SdkMemberType
78
Paul Duffin255f18e2019-12-13 11:22:16 +000079 // the dependency tag used for items in this list that can be used to determine the memberType
80 // for a resolved dependency.
Paul Duffin13879572019-11-28 14:31:38 +000081 dependencyTag *sdkMemberDependencyTag
82}
83
Paul Duffin255f18e2019-12-13 11:22:16 +000084func (p *sdkMemberListProperty) propertyName() string {
85 return p.memberType.SdkPropertyName()
86}
87
88// Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
89// to a slice of SdkMemberType instances held in android.SdkMemberTypes.
90var dynamicSdkMemberTypesMap android.OncePer
91
92// A dynamically generated set of member list properties and associated structure type.
93type dynamicSdkMemberTypes struct {
94 // The dynamically generated structure type.
95 //
96 // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
97 // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
98 propertiesStructType reflect.Type
99
100 // Information about each of the member type specific list properties.
101 memberListProperties []*sdkMemberListProperty
102}
103
104func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} {
105 return reflect.New(d.propertiesStructType).Interface()
106}
107
108func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes {
109
110 // Get a key that uniquely identifies the registry contents.
111 key := registry.UniqueOnceKey()
112
113 // Get the registered types.
114 registeredTypes := registry.RegisteredTypes()
115
116 // Get the cached value, creating new instance if necessary.
117 return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
118 return createDynamicSdkMemberTypes(registeredTypes)
119 }).(*dynamicSdkMemberTypes)
120}
121
122// Create the dynamicSdkMemberTypes from the list of registered member types.
Paul Duffina6e737b2019-11-29 11:55:51 +0000123//
Paul Duffin255f18e2019-12-13 11:22:16 +0000124// A struct is created which contains one exported field per member type corresponding to
125// the SdkMemberType.SdkPropertyName() value.
126//
127// A list of sdkMemberListProperty instances is created, one per member type that provides:
128// * a reference to the member type.
129// * a getter for the corresponding field in the properties struct.
130// * a dependency tag that identifies the member type of a resolved dependency.
131//
132func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
133 var listProperties []*sdkMemberListProperty
134 var fields []reflect.StructField
135
136 // Iterate over the member types creating StructField and sdkMemberListProperty objects.
137 for f, memberType := range sdkMemberTypes {
138 p := memberType.SdkPropertyName()
139
140 // Create a dynamic exported field for the member type's property.
141 fields = append(fields, reflect.StructField{
142 Name: proptools.FieldNameForProperty(p),
143 Type: reflect.TypeOf([]string{}),
144 })
145
146 // Copy the field index for use in the getter func as using the loop variable directly will
147 // cause all funcs to use the last value.
148 fieldIndex := f
149
150 // Create an sdkMemberListProperty for the member type.
151 memberListProperty := &sdkMemberListProperty{
152 getter: func(properties interface{}) []string {
153 // The properties is expected to be of the following form (where
154 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
155 // properties *struct {<Module_types> []string, ....}
156 //
157 // Although it accesses the field by index the following reflection code is equivalent to:
158 // *properties.<Module_types>
159 //
160 list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
161 return list
162 },
163
164 memberType: memberType,
165
166 dependencyTag: &sdkMemberDependencyTag{
167 memberType: memberType,
168 },
169 }
170
171 listProperties = append(listProperties, memberListProperty)
172 }
173
174 // Create a dynamic struct from the collated fields.
175 propertiesStructType := reflect.StructOf(fields)
176
177 return &dynamicSdkMemberTypes{
178 memberListProperties: listProperties,
179 propertiesStructType: propertiesStructType,
180 }
Paul Duffin13879572019-11-28 14:31:38 +0000181}
182
Jiyong Parkd1063c12019-07-17 20:08:41 +0900183// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
184// which Mainline modules like APEX can choose to build with.
185func ModuleFactory() android.Module {
186 s := &sdk{}
Paul Duffin255f18e2019-12-13 11:22:16 +0000187
188 // Get the dynamic sdk member type data for the currently registered sdk member types.
189 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(android.SdkMemberTypes)
190
191 // Create an instance of the dynamically created struct that contains all the
192 // properties for the member type specific list properties.
193 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
194
195 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
196
Jiyong Parkd1063c12019-07-17 20:08:41 +0900197 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
198 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900199 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
200 type props struct {
201 Compile_multilib *string
202 }
203 p := &props{Compile_multilib: proptools.StringPtr("both")}
204 ctx.AppendProperties(p)
205 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900206 return s
207}
208
Jiyong Park9b409bc2019-10-11 14:59:13 +0900209// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
210func SnapshotModuleFactory() android.Module {
211 s := ModuleFactory()
212 s.(*sdk).properties.Snapshot = true
213 return s
214}
215
216func (s *sdk) snapshot() bool {
217 return s.properties.Snapshot
218}
219
Jiyong Parkd1063c12019-07-17 20:08:41 +0900220func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900221 if !s.snapshot() {
222 // We don't need to create a snapshot out of sdk_snapshot.
223 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
224 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
225 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900226}
227
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900228func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900229 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900230 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900231 }
232
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900233 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900234 Class: "FAKE",
235 OutputFile: s.snapshotFile,
236 DistFile: s.snapshotFile,
237 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000238 ExtraFooters: []android.AndroidMkExtraFootersFunc{
239 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
240 // Allow the sdk to be built by simply passing its name on the command line.
241 fmt.Fprintln(w, ".PHONY:", s.Name())
242 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
243 },
244 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900245 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900246}
247
248// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
249// interface and the sdk module type. This function has been made public to be called by tests
250// outside of the sdk package
251func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
252 ctx.BottomUp("SdkMember", memberMutator).Parallel()
253 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
254 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
255}
256
257// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
258// interface and the sdk module type. This function has been made public to be called by tests
259// outside of the sdk package
260func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
261 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
262 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
263 // APEX to its dependents. Since different versions of the same SDK can be used by different
264 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
265 // should have been mutated for the apex before the SDK requirements are set.
266 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
267 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900268 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900269}
270
271type dependencyTag struct {
272 blueprint.BaseDependencyTag
273}
274
Jiyong Parkd1063c12019-07-17 20:08:41 +0900275// For dependencies from an in-development version of an SDK member to frozen versions of the same member
276// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
277type sdkMemberVesionedDepTag struct {
278 dependencyTag
279 member string
280 version string
281}
282
283// Step 1: create dependencies from an SDK module to its members.
284func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000285 if s, ok := mctx.Module().(*sdk); ok {
286 for _, memberListProperty := range s.dynamicSdkMemberTypes.memberListProperties {
287 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000288 tag := memberListProperty.dependencyTag
289 memberListProperty.memberType.AddDependencies(mctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900290 }
291 }
292}
293
294// Step 2: record that dependencies of SDK modules are members of the SDK modules
295func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900296 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900297 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900298 if s.snapshot() && mySdkRef.Unversioned() {
299 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
300 "Did you manually modify Android.bp?")
301 }
302 if !s.snapshot() && !mySdkRef.Unversioned() {
303 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
304 }
305 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
306 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
307 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
308 }
309 }
310
Jiyong Parkd1063c12019-07-17 20:08:41 +0900311 mctx.VisitDirectDeps(func(child android.Module) {
312 if member, ok := child.(android.SdkAware); ok {
313 member.MakeMemberOf(mySdkRef)
314 }
315 })
316 }
317}
318
Jiyong Park9b409bc2019-10-11 14:59:13 +0900319// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900320// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
321// (apex and apk), each of which might want different sdks to be built with. For example, if both
322// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
323// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
324// using.
325func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
326 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900327 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900328 memberName := m.MemberName()
329 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
330 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
331 }
332 }
333}
334
335// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
336// descendants
337func sdkDepsMutator(mctx android.TopDownMutatorContext) {
338 if m, ok := mctx.Module().(android.SdkAware); ok {
339 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
340 // by reading its own properties like `uses_sdks`.
341 requiredSdks := m.RequiredSdks()
342 if len(requiredSdks) > 0 {
343 mctx.VisitDirectDeps(func(m android.Module) {
344 if dep, ok := m.(android.SdkAware); ok {
345 dep.BuildWithSdks(requiredSdks)
346 }
347 })
348 }
349 }
350}
351
352// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
353// versioned module is used instead of the un-versioned (in-development) module libfoo
354func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
355 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900356 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900357 if m.RequiredSdks().Contains(sdk) {
358 // Note that this replacement is done only for the modules that have the same
359 // variations as the current module. Since current module is already mutated for
360 // apex references in other APEXes are not affected by this replacement.
361 memberName := m.MemberName()
362 mctx.ReplaceDependencies(memberName)
363 }
364 }
365 }
366}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900367
368// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
369func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
370 if m, ok := mctx.Module().(interface {
371 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
372 RequiredSdks() android.SdkRefs
373 }); ok {
374 requiredSdks := m.RequiredSdks()
375 if len(requiredSdks) == 0 {
376 return
377 }
378 mctx.VisitDirectDeps(func(dep android.Module) {
379 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
380 // dependency to defaults is always okay
381 return
382 }
383
384 // If the dep is from outside of the APEX, but is not in any of the
385 // required SDKs, we know that the dep is a violation.
386 if sa, ok := dep.(android.SdkAware); ok {
387 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
388 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
389 sa.Name(), sa.ContainingSdk(), requiredSdks)
390 }
391 }
392 })
393 }
394}