blob: 537ab13cb6eca24359a4dc2957551b6cc79545be [file] [log] [blame]
Jiyong Park9b409bc2019-10-11 14:59:13 +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 (
18 "fmt"
Paul Duffinb645ec82019-11-27 17:43:54 +000019 "reflect"
Paul Duffina04c1072020-03-02 10:16:35 +000020 "sort"
Jiyong Park9b409bc2019-10-11 14:59:13 +090021 "strings"
22
Paul Duffin7d74e7b2020-03-06 12:30:13 +000023 "android/soong/apex"
Paul Duffin9b76c0b2020-03-12 10:24:35 +000024 "android/soong/cc"
Colin Cross440e0d02020-06-11 11:32:11 -070025
Paul Duffin375058f2019-11-29 20:17:53 +000026 "github.com/google/blueprint"
Jiyong Park9b409bc2019-10-11 14:59:13 +090027 "github.com/google/blueprint/proptools"
28
29 "android/soong/android"
Jiyong Park9b409bc2019-10-11 14:59:13 +090030)
31
32var pctx = android.NewPackageContext("android/soong/sdk")
33
Paul Duffin375058f2019-11-29 20:17:53 +000034var (
35 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
36 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000037 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000038 CommandDeps: []string{
39 "${config.Zip2ZipCmd}",
40 },
41 },
42 "destdir")
43
44 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
45 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -070046 Command: `${config.SoongZipCmd} -C $basedir -r $out.rsp -o $out`,
Paul Duffin375058f2019-11-29 20:17:53 +000047 CommandDeps: []string{
48 "${config.SoongZipCmd}",
49 },
50 Rspfile: "$out.rsp",
51 RspfileContent: "$in",
52 },
53 "basedir")
54
55 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
56 blueprint.RuleParams{
57 Command: `${config.MergeZipsCmd} $out $in`,
58 CommandDeps: []string{
59 "${config.MergeZipsCmd}",
60 },
61 })
62)
63
Paul Duffinb645ec82019-11-27 17:43:54 +000064type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090065 content strings.Builder
66 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090067}
68
Paul Duffinb645ec82019-11-27 17:43:54 +000069// generatedFile abstracts operations for writing contents into a file and emit a build rule
70// for the file.
71type generatedFile struct {
72 generatedContents
73 path android.OutputPath
74}
75
Jiyong Park232e7852019-11-04 12:23:40 +090076func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +090077 return &generatedFile{
Paul Duffinb645ec82019-11-27 17:43:54 +000078 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park9b409bc2019-10-11 14:59:13 +090079 }
80}
81
Paul Duffinb645ec82019-11-27 17:43:54 +000082func (gc *generatedContents) Indent() {
83 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +090084}
85
Paul Duffinb645ec82019-11-27 17:43:54 +000086func (gc *generatedContents) Dedent() {
87 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +090088}
89
Paul Duffinb645ec82019-11-27 17:43:54 +000090func (gc *generatedContents) Printfln(format string, args ...interface{}) {
Paul Duffin11108272020-05-11 22:59:25 +010091 fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\n", args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +090092}
93
94func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
95 rb := android.NewRuleBuilder()
Paul Duffin11108272020-05-11 22:59:25 +010096
97 content := gf.content.String()
98
99 // ninja consumes newline characters in rspfile_content. Prevent it by
100 // escaping the backslash in the newline character. The extra backslash
101 // is removed when the rspfile is written to the actual script file
102 content = strings.ReplaceAll(content, "\n", "\\n")
103
Jiyong Park9b409bc2019-10-11 14:59:13 +0900104 rb.Command().
105 Implicits(implicits).
Paul Duffin11108272020-05-11 22:59:25 +0100106 Text("echo").Text(proptools.ShellEscape(content)).
107 // convert \\n to \n
Jiyong Park9b409bc2019-10-11 14:59:13 +0900108 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
109 rb.Command().
110 Text("chmod a+x").Output(gf.path)
111 rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base())
112}
113
Paul Duffin13879572019-11-28 14:31:38 +0000114// Collect all the members.
115//
Paul Duffin6a7e9532020-03-20 17:50:07 +0000116// Returns a list containing type (extracted from the dependency tag) and the variant
117// plus the multilib usages.
118func (s *sdk) collectMembers(ctx android.ModuleContext) {
119 s.multilibUsages = multilibNone
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000120 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
121 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf8539922019-11-19 19:44:10 +0000122 if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok {
123 memberType := memberTag.SdkMemberType()
Jiyong Park9b409bc2019-10-11 14:59:13 +0900124
Paul Duffin13879572019-11-28 14:31:38 +0000125 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000126 if !memberType.IsInstance(child) {
127 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900128 }
Paul Duffin13879572019-11-28 14:31:38 +0000129
Paul Duffin6a7e9532020-03-20 17:50:07 +0000130 // Keep track of which multilib variants are used by the sdk.
131 s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType)
132
133 s.memberRefs = append(s.memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)})
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000134
135 // If the member type supports transitive sdk members then recurse down into
136 // its dependencies, otherwise exit traversal.
137 return memberType.HasTransitiveSdkMembers()
Jiyong Park73c54ee2019-10-22 20:31:18 +0900138 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000139
140 return false
Paul Duffin13879572019-11-28 14:31:38 +0000141 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000142}
143
144// Organize the members.
145//
146// The members are first grouped by type and then grouped by name. The order of
147// the types is the order they are referenced in android.SdkMemberTypesRegistry.
148// The names are in the order in which the dependencies were added.
149//
150// Returns the members as well as the multilib setting to use.
Paul Duffin6a7e9532020-03-20 17:50:07 +0000151func (s *sdk) organizeMembers(ctx android.ModuleContext, memberRefs []sdkMemberRef) []*sdkMember {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000152 byType := make(map[android.SdkMemberType][]*sdkMember)
153 byName := make(map[string]*sdkMember)
154
Paul Duffin1356d8c2020-02-25 19:26:33 +0000155 for _, memberRef := range memberRefs {
156 memberType := memberRef.memberType
157 variant := memberRef.variant
158
159 name := ctx.OtherModuleName(variant)
160 member := byName[name]
161 if member == nil {
162 member = &sdkMember{memberType: memberType, name: name}
163 byName[name] = member
164 byType[memberType] = append(byType[memberType], member)
165 }
166
Paul Duffin1356d8c2020-02-25 19:26:33 +0000167 // Only append new variants to the list. This is needed because a member can be both
168 // exported by the sdk and also be a transitive sdk member.
169 member.variants = appendUniqueVariants(member.variants, variant)
170 }
171
Paul Duffin13879572019-11-28 14:31:38 +0000172 var members []*sdkMember
Paul Duffin72910952020-01-20 18:16:30 +0000173 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13879572019-11-28 14:31:38 +0000174 membersOfType := byType[memberListProperty.memberType]
175 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900176 }
177
Paul Duffin6a7e9532020-03-20 17:50:07 +0000178 return members
Jiyong Park73c54ee2019-10-22 20:31:18 +0900179}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900180
Paul Duffin72910952020-01-20 18:16:30 +0000181func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
182 for _, v := range variants {
183 if v == newVariant {
184 return variants
185 }
186 }
187 return append(variants, newVariant)
188}
189
Jiyong Park73c54ee2019-10-22 20:31:18 +0900190// SDK directory structure
191// <sdk_root>/
192// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
193// <api_ver>/ : below this directory are all auto-generated
194// Android.bp : definition of 'sdk_snapshot' module is here
195// aidl/
196// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
197// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900198// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900199// include/
200// bionic/libc/include/stdlib.h : an exported header file
201// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900202// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900203// <arch>/include/ : arch-specific exported headers
204// <arch>/include_gen/ : arch-specific generated headers
205// <arch>/lib/
206// libFoo.so : a stub library
207
Jiyong Park232e7852019-11-04 12:23:40 +0900208// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900209// This isn't visible to users, so could be changed in future.
210func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
211 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
212}
213
Jiyong Park232e7852019-11-04 12:23:40 +0900214// buildSnapshot is the main function in this source file. It creates rules to copy
215// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000216func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath {
217
Paul Duffin13f02712020-03-06 12:30:43 +0000218 allMembersByName := make(map[string]struct{})
219 exportedMembersByName := make(map[string]struct{})
Paul Duffin1356d8c2020-02-25 19:26:33 +0000220 var memberRefs []sdkMemberRef
221 for _, sdkVariant := range sdkVariants {
222 memberRefs = append(memberRefs, sdkVariant.memberRefs...)
Paul Duffin865171e2020-03-02 18:38:15 +0000223
Paul Duffin13f02712020-03-06 12:30:43 +0000224 // Record the names of all the members, both explicitly specified and implicitly
225 // included.
226 for _, memberRef := range sdkVariant.memberRefs {
227 allMembersByName[memberRef.variant.Name()] = struct{}{}
228 }
229
Paul Duffin865171e2020-03-02 18:38:15 +0000230 // Merge the exported member sets from all sdk variants.
231 for key, _ := range sdkVariant.getExportedMembers() {
Paul Duffin13f02712020-03-06 12:30:43 +0000232 exportedMembersByName[key] = struct{}{}
Paul Duffin865171e2020-03-02 18:38:15 +0000233 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000234 }
235
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000236 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900237
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000238 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000239
240 bpFile := &bpFile{
241 modules: make(map[string]*bpModule),
242 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000243
244 builder := &snapshotBuilder{
Paul Duffin13f02712020-03-06 12:30:43 +0000245 ctx: ctx,
246 sdk: s,
247 version: "current",
248 snapshotDir: snapshotDir.OutputPath,
249 copies: make(map[string]string),
250 filesToZip: []android.Path{bp.path},
251 bpFile: bpFile,
252 prebuiltModules: make(map[string]*bpModule),
253 allMembersByName: allMembersByName,
254 exportedMembersByName: exportedMembersByName,
Jiyong Park73c54ee2019-10-22 20:31:18 +0900255 }
Paul Duffinac37c502019-11-26 18:02:20 +0000256 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900257
Paul Duffin6a7e9532020-03-20 17:50:07 +0000258 members := s.organizeMembers(ctx, memberRefs)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000259 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000260 memberType := member.memberType
Paul Duffin3a4eb502020-03-19 16:11:18 +0000261
Paul Duffina551a1c2020-03-17 21:04:24 +0000262 memberCtx := &memberContext{ctx, builder, memberType, member.name}
Paul Duffin3a4eb502020-03-19 16:11:18 +0000263
264 prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100265 s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
Jiyong Park73c54ee2019-10-22 20:31:18 +0900266 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900267
Paul Duffine6c0d842020-01-15 14:08:51 +0000268 // Create a transformer that will transform an unversioned module into a versioned module.
269 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
270
Paul Duffin72910952020-01-20 18:16:30 +0000271 // Create a transformer that will transform an unversioned module by replacing any references
272 // to internal members with a unique module name and setting prefer: false.
273 unversionedTransformer := unversionedTransformation{builder: builder}
274
Paul Duffinb645ec82019-11-27 17:43:54 +0000275 for _, unversioned := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000276 // Prune any empty property sets.
277 unversioned = unversioned.transform(pruneEmptySetTransformer{})
278
Paul Duffinb645ec82019-11-27 17:43:54 +0000279 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000280 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000281
282 // Transform the unversioned module into a versioned one.
283 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000284 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000285
Paul Duffin72910952020-01-20 18:16:30 +0000286 // Transform the unversioned module to make it suitable for use in the snapshot.
287 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000288 bpFile.AddModule(unversioned)
289 }
290
291 // Create the snapshot module.
292 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000293 var snapshotModuleType string
294 if s.properties.Module_exports {
295 snapshotModuleType = "module_exports_snapshot"
296 } else {
297 snapshotModuleType = "sdk_snapshot"
298 }
299 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000300 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000301
302 // Make sure that the snapshot has the same visibility as the sdk.
303 visibility := android.EffectiveVisibilityRules(ctx, s)
304 if len(visibility) != 0 {
305 snapshotModule.AddProperty("visibility", visibility)
306 }
307
Paul Duffin865171e2020-03-02 18:38:15 +0000308 addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000309
Paul Duffinf34f6d82020-04-30 15:48:31 +0100310 var dynamicMemberPropertiesContainers []propertiesContainer
Paul Duffin865171e2020-03-02 18:38:15 +0000311 osTypeToMemberProperties := make(map[android.OsType]*sdk)
312 for _, sdkVariant := range sdkVariants {
313 properties := sdkVariant.dynamicMemberTypeListProperties
314 osTypeToMemberProperties[sdkVariant.Target().Os] = sdkVariant
Paul Duffin4b8b7932020-05-06 12:35:38 +0100315 dynamicMemberPropertiesContainers = append(dynamicMemberPropertiesContainers, &dynamicMemberPropertiesContainer{sdkVariant, properties})
Paul Duffin865171e2020-03-02 18:38:15 +0000316 }
317
318 // Extract the common lists of members into a separate struct.
319 commonDynamicMemberProperties := s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffinc097e362020-03-10 22:50:03 +0000320 extractor := newCommonValueExtractor(commonDynamicMemberProperties)
Paul Duffin4b8b7932020-05-06 12:35:38 +0100321 extractCommonProperties(ctx, extractor, commonDynamicMemberProperties, dynamicMemberPropertiesContainers)
Paul Duffin865171e2020-03-02 18:38:15 +0000322
323 // Add properties common to all os types.
324 s.addMemberPropertiesToPropertySet(builder, snapshotModule, commonDynamicMemberProperties)
325
Martin Stjernholm4cfa2c62020-07-10 19:55:36 +0100326 // Optimize other per-variant properties, besides the dynamic member lists.
327 type variantProperties struct {
328 Compile_multilib string `android:"arch_variant"`
329 }
330 var variantPropertiesContainers []propertiesContainer
331 variantToProperties := make(map[*sdk]*variantProperties)
332 for _, sdkVariant := range sdkVariants {
333 props := &variantProperties{
334 Compile_multilib: sdkVariant.multilibUsages.String(),
335 }
336 variantPropertiesContainers = append(variantPropertiesContainers, &dynamicMemberPropertiesContainer{sdkVariant, props})
337 variantToProperties[sdkVariant] = props
338 }
339 commonVariantProperties := variantProperties{}
340 extractor = newCommonValueExtractor(commonVariantProperties)
341 extractCommonProperties(ctx, extractor, &commonVariantProperties, variantPropertiesContainers)
342 if commonVariantProperties.Compile_multilib != "" && commonVariantProperties.Compile_multilib != "both" {
343 // Compile_multilib defaults to both so only needs to be set when it's
344 // specified and not both.
345 snapshotModule.AddProperty("compile_multilib", commonVariantProperties.Compile_multilib)
346 }
347
Paul Duffin6a7e9532020-03-20 17:50:07 +0000348 targetPropertySet := snapshotModule.AddPropertySet("target")
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100349
350 // If host is supported and any member is host OS dependent then disable host
351 // by default, so that we can enable each host OS variant explicitly. This
352 // avoids problems with implicitly enabled OS variants when the snapshot is
353 // used, which might be different from this run (e.g. different build OS).
354 hasHostOsDependentMember := false
355 if s.HostSupported() {
356 for _, memberRef := range memberRefs {
357 if memberRef.memberType.IsHostOsDependent() {
358 hasHostOsDependentMember = true
359 break
360 }
361 }
362 if hasHostOsDependentMember {
363 hostPropertySet := targetPropertySet.AddPropertySet("host")
364 hostPropertySet.AddProperty("enabled", false)
365 }
366 }
367
368 // Iterate over the os types in a fixed order.
Paul Duffin865171e2020-03-02 18:38:15 +0000369 for _, osType := range s.getPossibleOsTypes() {
370 if sdkVariant, ok := osTypeToMemberProperties[osType]; ok {
371 osPropertySet := targetPropertySet.AddPropertySet(sdkVariant.Target().Os.Name)
Paul Duffin6a7e9532020-03-20 17:50:07 +0000372
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100373 // Enable the variant explicitly when we've disabled it by default on host.
374 if hasHostOsDependentMember &&
375 (osType.Class == android.Host || osType.Class == android.HostCross) {
376 osPropertySet.AddProperty("enabled", true)
377 }
378
Martin Stjernholm4cfa2c62020-07-10 19:55:36 +0100379 variantProps := variantToProperties[sdkVariant]
380 if variantProps.Compile_multilib != "" && variantProps.Compile_multilib != "both" {
381 osPropertySet.AddProperty("compile_multilib", variantProps.Compile_multilib)
Paul Duffin6a7e9532020-03-20 17:50:07 +0000382 }
383
Paul Duffin865171e2020-03-02 18:38:15 +0000384 s.addMemberPropertiesToPropertySet(builder, osPropertySet, sdkVariant.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000385 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000386 }
Paul Duffin865171e2020-03-02 18:38:15 +0000387
388 // Prune any empty property sets.
389 snapshotModule.transform(pruneEmptySetTransformer{})
390
Paul Duffinb645ec82019-11-27 17:43:54 +0000391 bpFile.AddModule(snapshotModule)
392
393 // generate Android.bp
394 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
395 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000396
Paul Duffinf88d8e02020-05-07 20:21:34 +0100397 contents := bp.content.String()
398 syntaxCheckSnapshotBpFile(ctx, contents)
399
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000400 bp.build(pctx, ctx, nil)
401
402 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900403
Jiyong Park232e7852019-11-04 12:23:40 +0900404 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000405 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000406 outputDesc := "Building snapshot for " + ctx.ModuleName()
407
408 // If there are no zips to merge then generate the output zip directly.
409 // Otherwise, generate an intermediate zip file into which other zips can be
410 // merged.
411 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000412 var desc string
413 if len(builder.zipsToMerge) == 0 {
414 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000415 desc = outputDesc
416 } else {
417 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000418 desc = "Building intermediate snapshot for " + ctx.ModuleName()
419 }
420
Paul Duffin375058f2019-11-29 20:17:53 +0000421 ctx.Build(pctx, android.BuildParams{
422 Description: desc,
423 Rule: zipFiles,
424 Inputs: filesToZip,
425 Output: zipFile,
426 Args: map[string]string{
427 "basedir": builder.snapshotDir.String(),
428 },
429 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900430
Paul Duffin91547182019-11-12 19:39:36 +0000431 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000432 ctx.Build(pctx, android.BuildParams{
433 Description: outputDesc,
434 Rule: mergeZips,
435 Input: zipFile,
436 Inputs: builder.zipsToMerge,
437 Output: outputZipFile,
438 })
Paul Duffin91547182019-11-12 19:39:36 +0000439 }
440
441 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900442}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000443
Paul Duffinf88d8e02020-05-07 20:21:34 +0100444// Check the syntax of the generated Android.bp file contents and if they are
445// invalid then log an error with the contents (tagged with line numbers) and the
446// errors that were found so that it is easy to see where the problem lies.
447func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) {
448 errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents)
449 if len(errs) != 0 {
450 message := &strings.Builder{}
451 _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot:
452
453Generated Android.bp contents
454========================================================================
455`)
456 for i, line := range strings.Split(contents, "\n") {
457 _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line)
458 }
459
460 _, _ = fmt.Fprint(message, `
461========================================================================
462
463Errors found:
464`)
465
466 for _, err := range errs {
467 _, _ = fmt.Fprintf(message, "%s\n", err.Error())
468 }
469
470 ctx.ModuleErrorf("%s", message.String())
471 }
472}
473
Paul Duffin4b8b7932020-05-06 12:35:38 +0100474func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
475 err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
476 if err != nil {
477 ctx.ModuleErrorf("error extracting common properties: %s", err)
478 }
479}
480
Paul Duffin865171e2020-03-02 18:38:15 +0000481func (s *sdk) addMemberPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, dynamicMemberTypeListProperties interface{}) {
482 for _, memberListProperty := range s.memberListProperties() {
483 names := memberListProperty.getter(dynamicMemberTypeListProperties)
484 if len(names) > 0 {
Paul Duffin13f02712020-03-06 12:30:43 +0000485 propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names, false))
Paul Duffin865171e2020-03-02 18:38:15 +0000486 }
487 }
488}
489
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000490type propertyTag struct {
491 name string
492}
493
Paul Duffin0cb37b92020-03-04 14:52:46 +0000494// A BpPropertyTag to add to a property that contains references to other sdk members.
495//
496// This will cause the references to be rewritten to a versioned reference in the version
497// specific instance of a snapshot module.
Paul Duffin13f02712020-03-06 12:30:43 +0000498var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
Paul Duffin13f02712020-03-06 12:30:43 +0000499var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000500
Paul Duffin0cb37b92020-03-04 14:52:46 +0000501// A BpPropertyTag that indicates the property should only be present in the versioned
502// module.
503//
504// This will cause the property to be removed from the unversioned instance of a
505// snapshot module.
506var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"}
507
Paul Duffine6c0d842020-01-15 14:08:51 +0000508type unversionedToVersionedTransformation struct {
509 identityTransformation
510 builder *snapshotBuilder
511}
512
Paul Duffine6c0d842020-01-15 14:08:51 +0000513func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
514 // Use a versioned name for the module but remember the original name for the
515 // snapshot.
516 name := module.getValue("name").(string)
Paul Duffin13f02712020-03-06 12:30:43 +0000517 module.setProperty("name", t.builder.versionedSdkMemberName(name, true))
Paul Duffine6c0d842020-01-15 14:08:51 +0000518 module.insertAfter("name", "sdk_member_name", name)
519 return module
520}
521
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000522func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000523 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
524 required := tag == requiredSdkMemberReferencePropertyTag
525 return t.builder.versionedSdkMemberNames(value.([]string), required), tag
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000526 } else {
527 return value, tag
528 }
529}
530
Paul Duffin72910952020-01-20 18:16:30 +0000531type unversionedTransformation struct {
532 identityTransformation
533 builder *snapshotBuilder
534}
535
536func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
537 // If the module is an internal member then use a unique name for it.
538 name := module.getValue("name").(string)
Paul Duffin13f02712020-03-06 12:30:43 +0000539 module.setProperty("name", t.builder.unversionedSdkMemberName(name, true))
Paul Duffin72910952020-01-20 18:16:30 +0000540
541 // Set prefer: false - this is not strictly required as that is the default.
542 module.insertAfter("name", "prefer", false)
543
544 return module
545}
546
547func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000548 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
549 required := tag == requiredSdkMemberReferencePropertyTag
550 return t.builder.unversionedSdkMemberNames(value.([]string), required), tag
Paul Duffin0cb37b92020-03-04 14:52:46 +0000551 } else if tag == sdkVersionedOnlyPropertyTag {
552 // The property is not allowed in the unversioned module so remove it.
553 return nil, nil
Paul Duffin72910952020-01-20 18:16:30 +0000554 } else {
555 return value, tag
556 }
557}
558
Paul Duffina78f3a72020-02-21 16:29:35 +0000559type pruneEmptySetTransformer struct {
560 identityTransformation
561}
562
563var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
564
565func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
566 if len(propertySet.properties) == 0 {
567 return nil, nil
568 } else {
569 return propertySet, tag
570 }
571}
572
Paul Duffinb645ec82019-11-27 17:43:54 +0000573func generateBpContents(contents *generatedContents, bpFile *bpFile) {
574 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
575 for _, bpModule := range bpFile.order {
576 contents.Printfln("")
577 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000578 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000579 contents.Printfln("}")
580 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000581}
582
583func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
584 contents.Indent()
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000585
586 // Output the properties first, followed by the nested sets. This ensures a
587 // consistent output irrespective of whether property sets are created before
588 // or after the properties. This simplifies the creation of the module.
Paul Duffinb645ec82019-11-27 17:43:54 +0000589 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000590 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000591
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000592 switch v := value.(type) {
593 case []string:
594 length := len(v)
Paul Duffinb645ec82019-11-27 17:43:54 +0000595 if length > 1 {
596 contents.Printfln("%s: [", name)
597 contents.Indent()
598 for i := 0; i < length; i = i + 1 {
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000599 contents.Printfln("%q,", v[i])
Paul Duffinb645ec82019-11-27 17:43:54 +0000600 }
601 contents.Dedent()
602 contents.Printfln("],")
603 } else if length == 0 {
604 contents.Printfln("%s: [],", name)
605 } else {
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000606 contents.Printfln("%s: [%q],", name, v[0])
Paul Duffinb645ec82019-11-27 17:43:54 +0000607 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000608
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000609 case bool:
610 contents.Printfln("%s: %t,", name, v)
611
612 case *bpPropertySet:
613 // Do not write property sets in the properties phase.
Paul Duffinb645ec82019-11-27 17:43:54 +0000614
615 default:
616 contents.Printfln("%s: %q,", name, value)
617 }
618 }
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000619
620 for _, name := range set.order {
621 value := set.getValue(name)
622
623 // Only write property sets in the sets phase.
624 switch v := value.(type) {
625 case *bpPropertySet:
626 contents.Printfln("%s: {", name)
627 outputPropertySet(contents, v)
628 contents.Printfln("},")
629 }
630 }
631
Paul Duffinb645ec82019-11-27 17:43:54 +0000632 contents.Dedent()
633}
634
Paul Duffinac37c502019-11-26 18:02:20 +0000635func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000636 contents := &generatedContents{}
637 generateBpContents(contents, s.builderForTests.bpFile)
638 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000639}
640
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000641type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000642 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000643 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000644 version string
645 snapshotDir android.OutputPath
646 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000647
648 // Map from destination to source of each copy - used to eliminate duplicates and
649 // detect conflicts.
650 copies map[string]string
651
Paul Duffinb645ec82019-11-27 17:43:54 +0000652 filesToZip android.Paths
653 zipsToMerge android.Paths
654
655 prebuiltModules map[string]*bpModule
656 prebuiltOrder []*bpModule
Paul Duffin13f02712020-03-06 12:30:43 +0000657
658 // The set of all members by name.
659 allMembersByName map[string]struct{}
660
661 // The set of exported members by name.
662 exportedMembersByName map[string]struct{}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000663}
664
665func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000666 if existing, ok := s.copies[dest]; ok {
667 if existing != src.String() {
668 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
669 return
670 }
671 } else {
672 path := s.snapshotDir.Join(s.ctx, dest)
673 s.ctx.Build(pctx, android.BuildParams{
674 Rule: android.Cp,
675 Input: src,
676 Output: path,
677 })
678 s.filesToZip = append(s.filesToZip, path)
679
680 s.copies[dest] = src.String()
681 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000682}
683
Paul Duffin91547182019-11-12 19:39:36 +0000684func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
685 ctx := s.ctx
686
687 // Repackage the zip file so that the entries are in the destDir directory.
688 // This will allow the zip file to be merged into the snapshot.
689 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000690
691 ctx.Build(pctx, android.BuildParams{
692 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
693 Rule: repackageZip,
694 Input: zipPath,
695 Output: tmpZipPath,
696 Args: map[string]string{
697 "destdir": destDir,
698 },
699 })
Paul Duffin91547182019-11-12 19:39:36 +0000700
701 // Add the repackaged zip file to the files to merge.
702 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
703}
704
Paul Duffin9d8d6092019-12-05 18:19:29 +0000705func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
706 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000707 if s.prebuiltModules[name] != nil {
708 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
709 }
710
711 m := s.bpFile.newModule(moduleType)
712 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000713
Paul Duffinbefa4b92020-03-04 14:22:45 +0000714 variant := member.Variants()[0]
715
Paul Duffin13f02712020-03-06 12:30:43 +0000716 if s.isInternalMember(name) {
Paul Duffin72910952020-01-20 18:16:30 +0000717 // An internal member is only referenced from the sdk snapshot which is in the
718 // same package so can be marked as private.
719 m.AddProperty("visibility", []string{"//visibility:private"})
720 } else {
721 // Extract visibility information from a member variant. All variants have the same
722 // visibility so it doesn't matter which one is used.
Paul Duffinbefa4b92020-03-04 14:22:45 +0000723 visibility := android.EffectiveVisibilityRules(s.ctx, variant)
Paul Duffin72910952020-01-20 18:16:30 +0000724 if len(visibility) != 0 {
725 m.AddProperty("visibility", visibility)
726 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000727 }
728
Paul Duffin865171e2020-03-02 18:38:15 +0000729 deviceSupported := false
730 hostSupported := false
731
732 for _, variant := range member.Variants() {
733 osClass := variant.Target().Os.Class
734 if osClass == android.Host || osClass == android.HostCross {
735 hostSupported = true
736 } else if osClass == android.Device {
737 deviceSupported = true
738 }
739 }
740
741 addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000742
Paul Duffinbefa4b92020-03-04 14:22:45 +0000743 // Where available copy apex_available properties from the member.
744 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
745 apexAvailable := apexAware.ApexAvailable()
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000746
Colin Cross440e0d02020-06-11 11:32:11 -0700747 // Add in any baseline apex available settings.
748 apexAvailable = append(apexAvailable, apex.BaselineApexAvailable(member.Name())...)
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000749
Paul Duffinbefa4b92020-03-04 14:22:45 +0000750 if len(apexAvailable) > 0 {
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000751 // Remove duplicates and sort.
752 apexAvailable = android.FirstUniqueStrings(apexAvailable)
753 sort.Strings(apexAvailable)
754
Paul Duffinbefa4b92020-03-04 14:22:45 +0000755 m.AddProperty("apex_available", apexAvailable)
756 }
757 }
758
Paul Duffin0cb37b92020-03-04 14:52:46 +0000759 // Disable installation in the versioned module of those modules that are ever installable.
760 if installable, ok := variant.(interface{ EverInstallable() bool }); ok {
761 if installable.EverInstallable() {
762 m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag)
763 }
764 }
765
Paul Duffinb645ec82019-11-27 17:43:54 +0000766 s.prebuiltModules[name] = m
767 s.prebuiltOrder = append(s.prebuiltOrder, m)
768 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000769}
770
Paul Duffin865171e2020-03-02 18:38:15 +0000771func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
772 if !deviceSupported {
Paul Duffine44358f2019-11-26 18:04:12 +0000773 bpModule.AddProperty("device_supported", false)
774 }
Paul Duffin865171e2020-03-02 18:38:15 +0000775 if hostSupported {
Paul Duffine44358f2019-11-26 18:04:12 +0000776 bpModule.AddProperty("host_supported", true)
777 }
778}
779
Paul Duffin13f02712020-03-06 12:30:43 +0000780func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag {
781 if required {
782 return requiredSdkMemberReferencePropertyTag
783 } else {
784 return optionalSdkMemberReferencePropertyTag
785 }
786}
787
788func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag {
789 return optionalSdkMemberReferencePropertyTag
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000790}
791
Paul Duffinb645ec82019-11-27 17:43:54 +0000792// Get a versioned name appropriate for the SDK snapshot version being taken.
Paul Duffin13f02712020-03-06 12:30:43 +0000793func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string, required bool) string {
794 if _, ok := s.allMembersByName[unversionedName]; !ok {
795 if required {
796 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
797 }
798 return unversionedName
799 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000800 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
801}
Paul Duffinb645ec82019-11-27 17:43:54 +0000802
Paul Duffin13f02712020-03-06 12:30:43 +0000803func (s *snapshotBuilder) versionedSdkMemberNames(members []string, required bool) []string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000804 var references []string = nil
805 for _, m := range members {
Paul Duffin13f02712020-03-06 12:30:43 +0000806 references = append(references, s.versionedSdkMemberName(m, required))
Paul Duffinb645ec82019-11-27 17:43:54 +0000807 }
808 return references
809}
Paul Duffin13879572019-11-28 14:31:38 +0000810
Paul Duffin72910952020-01-20 18:16:30 +0000811// Get an internal name unique to the sdk.
Paul Duffin13f02712020-03-06 12:30:43 +0000812func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string, required bool) string {
813 if _, ok := s.allMembersByName[unversionedName]; !ok {
814 if required {
815 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
816 }
817 return unversionedName
818 }
819
820 if s.isInternalMember(unversionedName) {
Paul Duffin72910952020-01-20 18:16:30 +0000821 return s.ctx.ModuleName() + "_" + unversionedName
822 } else {
823 return unversionedName
824 }
825}
826
Paul Duffin13f02712020-03-06 12:30:43 +0000827func (s *snapshotBuilder) unversionedSdkMemberNames(members []string, required bool) []string {
Paul Duffin72910952020-01-20 18:16:30 +0000828 var references []string = nil
829 for _, m := range members {
Paul Duffin13f02712020-03-06 12:30:43 +0000830 references = append(references, s.unversionedSdkMemberName(m, required))
Paul Duffin72910952020-01-20 18:16:30 +0000831 }
832 return references
833}
834
Paul Duffin13f02712020-03-06 12:30:43 +0000835func (s *snapshotBuilder) isInternalMember(memberName string) bool {
836 _, ok := s.exportedMembersByName[memberName]
837 return !ok
838}
839
Martin Stjernholm89238f42020-07-10 00:14:03 +0100840// Add the properties from the given SdkMemberProperties to the blueprint
841// property set. This handles common properties in SdkMemberPropertiesBase and
842// calls the member-specific AddToPropertySet for the rest.
843func addSdkMemberPropertiesToSet(ctx *memberContext, memberProperties android.SdkMemberProperties, targetPropertySet android.BpPropertySet) {
844 if memberProperties.Base().Compile_multilib != "" {
845 targetPropertySet.AddProperty("compile_multilib", memberProperties.Base().Compile_multilib)
846 }
847
848 memberProperties.AddToPropertySet(ctx, targetPropertySet)
849}
850
Paul Duffin1356d8c2020-02-25 19:26:33 +0000851type sdkMemberRef struct {
852 memberType android.SdkMemberType
853 variant android.SdkAware
854}
855
Paul Duffin13879572019-11-28 14:31:38 +0000856var _ android.SdkMember = (*sdkMember)(nil)
857
858type sdkMember struct {
859 memberType android.SdkMemberType
860 name string
861 variants []android.SdkAware
862}
863
864func (m *sdkMember) Name() string {
865 return m.name
866}
867
868func (m *sdkMember) Variants() []android.SdkAware {
869 return m.variants
870}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000871
Paul Duffin9c3760e2020-03-16 19:52:08 +0000872// Track usages of multilib variants.
873type multilibUsage int
874
875const (
876 multilibNone multilibUsage = 0
877 multilib32 multilibUsage = 1
878 multilib64 multilibUsage = 2
879 multilibBoth = multilib32 | multilib64
880)
881
882// Add the multilib that is used in the arch type.
883func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage {
884 multilib := archType.Multilib
885 switch multilib {
886 case "":
887 return m
888 case "lib32":
889 return m | multilib32
890 case "lib64":
891 return m | multilib64
892 default:
893 panic(fmt.Errorf("Unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib))
894 }
895}
896
897func (m multilibUsage) String() string {
898 switch m {
899 case multilibNone:
900 return ""
901 case multilib32:
902 return "32"
903 case multilib64:
904 return "64"
905 case multilibBoth:
906 return "both"
907 default:
908 panic(fmt.Errorf("Unknown multilib value, found %b, expected one of %b, %b, %b or %b",
909 m, multilibNone, multilib32, multilib64, multilibBoth))
910 }
911}
912
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000913type baseInfo struct {
914 Properties android.SdkMemberProperties
915}
916
Paul Duffinf34f6d82020-04-30 15:48:31 +0100917func (b *baseInfo) optimizableProperties() interface{} {
918 return b.Properties
919}
920
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000921type osTypeSpecificInfo struct {
922 baseInfo
923
Paul Duffin00e46802020-03-12 20:40:35 +0000924 osType android.OsType
925
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000926 // The list of arch type specific info for this os type.
Paul Duffinb44b33a2020-03-17 10:58:23 +0000927 //
928 // Nil if there is one variant whose arch type is common
929 archInfos []*archTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000930}
931
Paul Duffin4b8b7932020-05-06 12:35:38 +0100932var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
933
Paul Duffinfc8dd232020-03-17 12:51:37 +0000934type variantPropertiesFactoryFunc func() android.SdkMemberProperties
935
Paul Duffin00e46802020-03-12 20:40:35 +0000936// Create a new osTypeSpecificInfo for the specified os type and its properties
937// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000938func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
Paul Duffin00e46802020-03-12 20:40:35 +0000939 osInfo := &osTypeSpecificInfo{
940 osType: osType,
941 }
942
943 osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
944 properties := variantPropertiesFactory()
945 properties.Base().Os = osType
946 return properties
947 }
948
949 // Create a structure into which properties common across the architectures in
950 // this os type will be stored.
951 osInfo.Properties = osSpecificVariantPropertiesFactory()
952
953 // Group the variants by arch type.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000954 var variantsByArchName = make(map[string][]android.Module)
Paul Duffin00e46802020-03-12 20:40:35 +0000955 var archTypes []android.ArchType
956 for _, variant := range osTypeVariants {
957 archType := variant.Target().Arch.ArchType
958 archTypeName := archType.Name
959 if _, ok := variantsByArchName[archTypeName]; !ok {
960 archTypes = append(archTypes, archType)
961 }
962
963 variantsByArchName[archTypeName] = append(variantsByArchName[archTypeName], variant)
964 }
965
966 if commonVariants, ok := variantsByArchName["common"]; ok {
967 if len(osTypeVariants) != 1 {
Colin Crossafa6a772020-07-06 17:41:08 -0700968 panic(fmt.Errorf("Expected to only have 1 variant when arch type is common but found %d", len(osTypeVariants)))
Paul Duffin00e46802020-03-12 20:40:35 +0000969 }
970
971 // A common arch type only has one variant and its properties should be treated
972 // as common to the os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000973 osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
Paul Duffin00e46802020-03-12 20:40:35 +0000974 } else {
975 // Create an arch specific info for each supported architecture type.
976 for _, archType := range archTypes {
977 archTypeName := archType.Name
978
979 archVariants := variantsByArchName[archTypeName]
Paul Duffin3a4eb502020-03-19 16:11:18 +0000980 archInfo := newArchSpecificInfo(ctx, archType, osSpecificVariantPropertiesFactory, archVariants)
Paul Duffin00e46802020-03-12 20:40:35 +0000981
982 osInfo.archInfos = append(osInfo.archInfos, archInfo)
983 }
984 }
985
986 return osInfo
987}
988
989// Optimize the properties by extracting common properties from arch type specific
990// properties into os type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +0100991func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin00e46802020-03-12 20:40:35 +0000992 // Nothing to do if there is only a single common architecture.
993 if len(osInfo.archInfos) == 0 {
994 return
995 }
996
Paul Duffin9c3760e2020-03-16 19:52:08 +0000997 multilib := multilibNone
Paul Duffin00e46802020-03-12 20:40:35 +0000998 for _, archInfo := range osInfo.archInfos {
Paul Duffin9c3760e2020-03-16 19:52:08 +0000999 multilib = multilib.addArchType(archInfo.archType)
1000
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001001 // Optimize the arch properties first.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001002 archInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin00e46802020-03-12 20:40:35 +00001003 }
1004
Paul Duffin4b8b7932020-05-06 12:35:38 +01001005 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
Paul Duffin00e46802020-03-12 20:40:35 +00001006
1007 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
Paul Duffin9c3760e2020-03-16 19:52:08 +00001008 osInfo.Properties.Base().Compile_multilib = multilib.String()
Paul Duffin00e46802020-03-12 20:40:35 +00001009}
1010
1011// Add the properties for an os to a property set.
1012//
1013// Maps the properties related to the os variants through to an appropriate
1014// module structure that will produce equivalent set of variants when it is
1015// processed in a build.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001016func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
Paul Duffin00e46802020-03-12 20:40:35 +00001017
1018 var osPropertySet android.BpPropertySet
1019 var archPropertySet android.BpPropertySet
1020 var archOsPrefix string
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001021 if osInfo.Properties.Base().Os_count == 1 &&
1022 (osInfo.osType.Class == android.Device || !ctx.memberType.IsHostOsDependent()) {
1023 // There is only one OS type present in the variants and it shouldn't have a
1024 // variant-specific target. The latter is the case if it's either for device
1025 // where there is only one OS (android), or for host and the member type
1026 // isn't host OS dependent.
Paul Duffin00e46802020-03-12 20:40:35 +00001027
1028 // Create a structure that looks like:
1029 // module_type {
1030 // name: "...",
1031 // ...
1032 // <common properties>
1033 // ...
1034 // <single os type specific properties>
1035 //
1036 // arch: {
1037 // <arch specific sections>
1038 // }
1039 //
1040 osPropertySet = bpModule
1041 archPropertySet = osPropertySet.AddPropertySet("arch")
1042
1043 // Arch specific properties need to be added to an arch specific section
1044 // within arch.
1045 archOsPrefix = ""
1046 } else {
1047 // Create a structure that looks like:
1048 // module_type {
1049 // name: "...",
1050 // ...
1051 // <common properties>
1052 // ...
1053 // target: {
1054 // <arch independent os specific sections, e.g. android>
1055 // ...
1056 // <arch and os specific sections, e.g. android_x86>
1057 // }
1058 //
1059 osType := osInfo.osType
1060 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
1061 archPropertySet = targetPropertySet
1062
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001063 // Enable the variant explicitly when we've disabled it by default on host.
1064 if ctx.memberType.IsHostOsDependent() &&
1065 (osType.Class == android.Host || osType.Class == android.HostCross) {
1066 osPropertySet.AddProperty("enabled", true)
1067 }
1068
Paul Duffin00e46802020-03-12 20:40:35 +00001069 // Arch specific properties need to be added to an os and arch specific
1070 // section prefixed with <os>_.
1071 archOsPrefix = osType.Name + "_"
1072 }
1073
1074 // Add the os specific but arch independent properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001075 addSdkMemberPropertiesToSet(ctx, osInfo.Properties, osPropertySet)
Paul Duffin00e46802020-03-12 20:40:35 +00001076
1077 // Add arch (and possibly os) specific sections for each set of arch (and possibly
1078 // os) specific properties.
1079 //
1080 // The archInfos list will be empty if the os contains variants for the common
1081 // architecture.
1082 for _, archInfo := range osInfo.archInfos {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001083 archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
Paul Duffin00e46802020-03-12 20:40:35 +00001084 }
1085}
1086
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001087func (osInfo *osTypeSpecificInfo) isHostVariant() bool {
1088 osClass := osInfo.osType.Class
1089 return osClass == android.Host || osClass == android.HostCross
1090}
1091
1092var _ isHostVariant = (*osTypeSpecificInfo)(nil)
1093
Paul Duffin4b8b7932020-05-06 12:35:38 +01001094func (osInfo *osTypeSpecificInfo) String() string {
1095 return fmt.Sprintf("OsType{%s}", osInfo.osType)
1096}
1097
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001098type archTypeSpecificInfo struct {
1099 baseInfo
1100
1101 archType android.ArchType
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001102
1103 linkInfos []*linkTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001104}
1105
Paul Duffin4b8b7932020-05-06 12:35:38 +01001106var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
1107
Paul Duffinfc8dd232020-03-17 12:51:37 +00001108// Create a new archTypeSpecificInfo for the specified arch type and its properties
1109// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001110func newArchSpecificInfo(ctx android.SdkMemberContext, archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001111
Paul Duffinfc8dd232020-03-17 12:51:37 +00001112 // Create an arch specific info into which the variant properties can be copied.
1113 archInfo := &archTypeSpecificInfo{archType: archType}
1114
1115 // Create the properties into which the arch type specific properties will be
1116 // added.
1117 archInfo.Properties = variantPropertiesFactory()
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001118
1119 if len(archVariants) == 1 {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001120 archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001121 } else {
1122 // There is more than one variant for this arch type which must be differentiated
1123 // by link type.
1124 for _, linkVariant := range archVariants {
1125 linkType := getLinkType(linkVariant)
1126 if linkType == "" {
1127 panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(archVariants)))
1128 } else {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001129 linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001130
1131 archInfo.linkInfos = append(archInfo.linkInfos, linkInfo)
1132 }
1133 }
1134 }
Paul Duffinfc8dd232020-03-17 12:51:37 +00001135
1136 return archInfo
1137}
1138
Paul Duffinf34f6d82020-04-30 15:48:31 +01001139func (archInfo *archTypeSpecificInfo) optimizableProperties() interface{} {
1140 return archInfo.Properties
1141}
1142
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001143// Get the link type of the variant
1144//
1145// If the variant is not differentiated by link type then it returns "",
1146// otherwise it returns one of "static" or "shared".
1147func getLinkType(variant android.Module) string {
1148 linkType := ""
1149 if linkable, ok := variant.(cc.LinkableInterface); ok {
1150 if linkable.Shared() && linkable.Static() {
1151 panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String()))
1152 } else if linkable.Shared() {
1153 linkType = "shared"
1154 } else if linkable.Static() {
1155 linkType = "static"
1156 } else {
1157 panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String()))
1158 }
1159 }
1160 return linkType
1161}
1162
1163// Optimize the properties by extracting common properties from link type specific
1164// properties into arch type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001165func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001166 if len(archInfo.linkInfos) == 0 {
1167 return
1168 }
1169
Paul Duffin4b8b7932020-05-06 12:35:38 +01001170 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.linkInfos)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001171}
1172
Paul Duffinfc8dd232020-03-17 12:51:37 +00001173// Add the properties for an arch type to a property set.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001174func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001175 archTypeName := archInfo.archType.Name
1176 archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName)
Martin Stjernholm89238f42020-07-10 00:14:03 +01001177 addSdkMemberPropertiesToSet(ctx, archInfo.Properties, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001178
1179 for _, linkInfo := range archInfo.linkInfos {
1180 linkPropertySet := archTypePropertySet.AddPropertySet(linkInfo.linkType)
Martin Stjernholm89238f42020-07-10 00:14:03 +01001181 addSdkMemberPropertiesToSet(ctx, linkInfo.Properties, linkPropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001182 }
1183}
1184
Paul Duffin4b8b7932020-05-06 12:35:38 +01001185func (archInfo *archTypeSpecificInfo) String() string {
1186 return fmt.Sprintf("ArchType{%s}", archInfo.archType)
1187}
1188
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001189type linkTypeSpecificInfo struct {
1190 baseInfo
1191
1192 linkType string
1193}
1194
Paul Duffin4b8b7932020-05-06 12:35:38 +01001195var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
1196
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001197// Create a new linkTypeSpecificInfo for the specified link type and its properties
1198// structures populated with information from the variant.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001199func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001200 linkInfo := &linkTypeSpecificInfo{
1201 baseInfo: baseInfo{
1202 // Create the properties into which the link type specific properties will be
1203 // added.
1204 Properties: variantPropertiesFactory(),
1205 },
1206 linkType: linkType,
1207 }
Paul Duffin3a4eb502020-03-19 16:11:18 +00001208 linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001209 return linkInfo
Paul Duffinfc8dd232020-03-17 12:51:37 +00001210}
1211
Paul Duffin4b8b7932020-05-06 12:35:38 +01001212func (l *linkTypeSpecificInfo) String() string {
1213 return fmt.Sprintf("LinkType{%s}", l.linkType)
1214}
1215
Paul Duffin3a4eb502020-03-19 16:11:18 +00001216type memberContext struct {
1217 sdkMemberContext android.ModuleContext
1218 builder *snapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +00001219 memberType android.SdkMemberType
1220 name string
Paul Duffin3a4eb502020-03-19 16:11:18 +00001221}
1222
1223func (m *memberContext) SdkModuleContext() android.ModuleContext {
1224 return m.sdkMemberContext
1225}
1226
1227func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
1228 return m.builder
1229}
1230
Paul Duffina551a1c2020-03-17 21:04:24 +00001231func (m *memberContext) MemberType() android.SdkMemberType {
1232 return m.memberType
1233}
1234
1235func (m *memberContext) Name() string {
1236 return m.name
1237}
1238
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001239func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001240
1241 memberType := member.memberType
1242
Paul Duffina04c1072020-03-02 10:16:35 +00001243 // Group the variants by os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001244 variantsByOsType := make(map[android.OsType][]android.Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001245 variants := member.Variants()
1246 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +00001247 osType := variant.Target().Os
1248 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001249 }
1250
Paul Duffina04c1072020-03-02 10:16:35 +00001251 osCount := len(variantsByOsType)
Paul Duffinb44b33a2020-03-17 10:58:23 +00001252 variantPropertiesFactory := func() android.SdkMemberProperties {
Paul Duffina04c1072020-03-02 10:16:35 +00001253 properties := memberType.CreateVariantPropertiesStruct()
1254 base := properties.Base()
1255 base.Os_count = osCount
Paul Duffina04c1072020-03-02 10:16:35 +00001256 return properties
1257 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001258
Paul Duffina04c1072020-03-02 10:16:35 +00001259 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +00001260
Paul Duffina04c1072020-03-02 10:16:35 +00001261 // The set of properties that are common across all architectures and os types.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001262 commonProperties := variantPropertiesFactory()
1263 commonProperties.Base().Os = android.CommonOS
Paul Duffina04c1072020-03-02 10:16:35 +00001264
Paul Duffinc097e362020-03-10 22:50:03 +00001265 // Create common value extractor that can be used to optimize the properties.
1266 commonValueExtractor := newCommonValueExtractor(commonProperties)
1267
Paul Duffina04c1072020-03-02 10:16:35 +00001268 // The list of property structures which are os type specific but common across
1269 // architectures within that os type.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001270 var osSpecificPropertiesContainers []*osTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +00001271
1272 for osType, osTypeVariants := range variantsByOsType {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001273 osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
Paul Duffina04c1072020-03-02 10:16:35 +00001274 osTypeToInfo[osType] = osInfo
Paul Duffinb44b33a2020-03-17 10:58:23 +00001275 // Add the os specific properties to a list of os type specific yet architecture
1276 // independent properties structs.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001277 osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
Paul Duffina04c1072020-03-02 10:16:35 +00001278
Paul Duffin00e46802020-03-12 20:40:35 +00001279 // Optimize the properties across all the variants for a specific os type.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001280 osInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin14eb4672020-03-02 11:33:02 +00001281 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001282
Paul Duffina04c1072020-03-02 10:16:35 +00001283 // Extract properties which are common across all architectures and os types.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001284 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001285
Paul Duffina04c1072020-03-02 10:16:35 +00001286 // Add the common properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001287 addSdkMemberPropertiesToSet(ctx, commonProperties, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001288
Paul Duffina04c1072020-03-02 10:16:35 +00001289 // Create a target property set into which target specific properties can be
1290 // added.
1291 targetPropertySet := bpModule.AddPropertySet("target")
1292
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001293 // If the member is host OS dependent and has host_supported then disable by
1294 // default and enable each host OS variant explicitly. This avoids problems
1295 // with implicitly enabled OS variants when the snapshot is used, which might
1296 // be different from this run (e.g. different build OS).
1297 if ctx.memberType.IsHostOsDependent() {
1298 hostSupported := bpModule.getValue("host_supported") == true // Missing means false.
1299 if hostSupported {
1300 hostPropertySet := targetPropertySet.AddPropertySet("host")
1301 hostPropertySet.AddProperty("enabled", false)
1302 }
1303 }
1304
Paul Duffina04c1072020-03-02 10:16:35 +00001305 // Iterate over the os types in a fixed order.
1306 for _, osType := range s.getPossibleOsTypes() {
1307 osInfo := osTypeToInfo[osType]
1308 if osInfo == nil {
1309 continue
1310 }
1311
Paul Duffin3a4eb502020-03-19 16:11:18 +00001312 osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001313 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001314}
1315
Paul Duffina04c1072020-03-02 10:16:35 +00001316// Compute the list of possible os types that this sdk could support.
1317func (s *sdk) getPossibleOsTypes() []android.OsType {
1318 var osTypes []android.OsType
1319 for _, osType := range android.OsTypeList {
1320 if s.DeviceSupported() {
1321 if osType.Class == android.Device && osType != android.Fuchsia {
1322 osTypes = append(osTypes, osType)
1323 }
1324 }
1325 if s.HostSupported() {
1326 if osType.Class == android.Host || osType.Class == android.HostCross {
1327 osTypes = append(osTypes, osType)
1328 }
1329 }
1330 }
1331 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
1332 return osTypes
1333}
1334
Paul Duffinb28369a2020-05-04 15:39:59 +01001335// Given a set of properties (struct value), return the value of the field within that
1336// struct (or one of its embedded structs).
Paul Duffinc097e362020-03-10 22:50:03 +00001337type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
1338
Paul Duffinc459f892020-04-30 18:08:29 +01001339// Checks the metadata to determine whether the property should be ignored for the
1340// purposes of common value extraction or not.
1341type extractorMetadataPredicate func(metadata propertiesContainer) bool
1342
1343// Indicates whether optimizable properties are provided by a host variant or
1344// not.
1345type isHostVariant interface {
1346 isHostVariant() bool
1347}
1348
Paul Duffinb28369a2020-05-04 15:39:59 +01001349// A property that can be optimized by the commonValueExtractor.
1350type extractorProperty struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +01001351 // The name of the field for this property.
1352 name string
1353
Paul Duffinc459f892020-04-30 18:08:29 +01001354 // Filter that can use metadata associated with the properties being optimized
1355 // to determine whether the field should be ignored during common value
1356 // optimization.
1357 filter extractorMetadataPredicate
1358
Paul Duffinb28369a2020-05-04 15:39:59 +01001359 // Retrieves the value on which common value optimization will be performed.
1360 getter fieldAccessorFunc
1361
1362 // The empty value for the field.
1363 emptyValue reflect.Value
Paul Duffin864e1b42020-05-06 10:23:19 +01001364
1365 // True if the property can support arch variants false otherwise.
1366 archVariant bool
Paul Duffinb28369a2020-05-04 15:39:59 +01001367}
1368
Paul Duffin4b8b7932020-05-06 12:35:38 +01001369func (p extractorProperty) String() string {
1370 return p.name
1371}
1372
Paul Duffinc097e362020-03-10 22:50:03 +00001373// Supports extracting common values from a number of instances of a properties
1374// structure into a separate common set of properties.
1375type commonValueExtractor struct {
Paul Duffinb28369a2020-05-04 15:39:59 +01001376 // The properties that the extractor can optimize.
1377 properties []extractorProperty
Paul Duffinc097e362020-03-10 22:50:03 +00001378}
1379
1380// Create a new common value extractor for the structure type for the supplied
1381// properties struct.
1382//
1383// The returned extractor can be used on any properties structure of the same type
1384// as the supplied set of properties.
1385func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor {
1386 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
1387 extractor := &commonValueExtractor{}
Paul Duffinb07fa512020-03-10 22:17:04 +00001388 extractor.gatherFields(structType, nil)
Paul Duffinc097e362020-03-10 22:50:03 +00001389 return extractor
1390}
1391
1392// Gather the fields from the supplied structure type from which common values will
1393// be extracted.
Paul Duffinb07fa512020-03-10 22:17:04 +00001394//
1395// This is recursive function. If it encounters an embedded field (no field name)
1396// that is a struct then it will recurse into that struct passing in the accessor
1397// for the field. That will then be used in the accessors for the fields in the
1398// embedded struct.
1399func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc) {
Paul Duffinc097e362020-03-10 22:50:03 +00001400 for f := 0; f < structType.NumField(); f++ {
1401 field := structType.Field(f)
1402 if field.PkgPath != "" {
1403 // Ignore unexported fields.
1404 continue
1405 }
1406
Paul Duffinb07fa512020-03-10 22:17:04 +00001407 // Ignore fields whose value should be kept.
1408 if proptools.HasTag(field, "sdk", "keep") {
Paul Duffinc097e362020-03-10 22:50:03 +00001409 continue
1410 }
1411
Paul Duffinc459f892020-04-30 18:08:29 +01001412 var filter extractorMetadataPredicate
1413
1414 // Add a filter
1415 if proptools.HasTag(field, "sdk", "ignored-on-host") {
1416 filter = func(metadata propertiesContainer) bool {
1417 if m, ok := metadata.(isHostVariant); ok {
1418 if m.isHostVariant() {
1419 return false
1420 }
1421 }
1422 return true
1423 }
1424 }
1425
Paul Duffinc097e362020-03-10 22:50:03 +00001426 // Save a copy of the field index for use in the function.
1427 fieldIndex := f
Paul Duffin4b8b7932020-05-06 12:35:38 +01001428
1429 name := field.Name
1430
Paul Duffinc097e362020-03-10 22:50:03 +00001431 fieldGetter := func(value reflect.Value) reflect.Value {
Paul Duffinb07fa512020-03-10 22:17:04 +00001432 if containingStructAccessor != nil {
1433 // This is an embedded structure so first access the field for the embedded
1434 // structure.
1435 value = containingStructAccessor(value)
1436 }
1437
Paul Duffinc097e362020-03-10 22:50:03 +00001438 // Skip through interface and pointer values to find the structure.
1439 value = getStructValue(value)
1440
Paul Duffin4b8b7932020-05-06 12:35:38 +01001441 defer func() {
1442 if r := recover(); r != nil {
1443 panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
1444 }
1445 }()
1446
Paul Duffinc097e362020-03-10 22:50:03 +00001447 // Return the field.
1448 return value.Field(fieldIndex)
1449 }
1450
Paul Duffinb07fa512020-03-10 22:17:04 +00001451 if field.Type.Kind() == reflect.Struct && field.Anonymous {
1452 // Gather fields from the embedded structure.
1453 e.gatherFields(field.Type, fieldGetter)
1454 } else {
Paul Duffinb28369a2020-05-04 15:39:59 +01001455 property := extractorProperty{
Paul Duffin4b8b7932020-05-06 12:35:38 +01001456 name,
Paul Duffinc459f892020-04-30 18:08:29 +01001457 filter,
Paul Duffinb28369a2020-05-04 15:39:59 +01001458 fieldGetter,
1459 reflect.Zero(field.Type),
Paul Duffin864e1b42020-05-06 10:23:19 +01001460 proptools.HasTag(field, "android", "arch_variant"),
Paul Duffinb28369a2020-05-04 15:39:59 +01001461 }
1462 e.properties = append(e.properties, property)
Paul Duffinb07fa512020-03-10 22:17:04 +00001463 }
Paul Duffinc097e362020-03-10 22:50:03 +00001464 }
1465}
1466
1467func getStructValue(value reflect.Value) reflect.Value {
1468foundStruct:
1469 for {
1470 kind := value.Kind()
1471 switch kind {
1472 case reflect.Interface, reflect.Ptr:
1473 value = value.Elem()
1474 case reflect.Struct:
1475 break foundStruct
1476 default:
1477 panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind))
1478 }
1479 }
1480 return value
1481}
1482
Paul Duffinf34f6d82020-04-30 15:48:31 +01001483// A container of properties to be optimized.
1484//
1485// Allows additional information to be associated with the properties, e.g. for
1486// filtering.
1487type propertiesContainer interface {
Paul Duffin4b8b7932020-05-06 12:35:38 +01001488 fmt.Stringer
1489
Paul Duffinf34f6d82020-04-30 15:48:31 +01001490 // Get the properties that need optimizing.
1491 optimizableProperties() interface{}
1492}
1493
1494// A wrapper for dynamic member properties to allow them to be optimized.
1495type dynamicMemberPropertiesContainer struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +01001496 sdkVariant *sdk
Paul Duffinf34f6d82020-04-30 15:48:31 +01001497 dynamicMemberProperties interface{}
1498}
1499
1500func (c dynamicMemberPropertiesContainer) optimizableProperties() interface{} {
1501 return c.dynamicMemberProperties
1502}
1503
Paul Duffin4b8b7932020-05-06 12:35:38 +01001504func (c dynamicMemberPropertiesContainer) String() string {
1505 return c.sdkVariant.String()
1506}
1507
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001508// Extract common properties from a slice of property structures of the same type.
1509//
1510// All the property structures must be of the same type.
1511// commonProperties - must be a pointer to the structure into which common properties will be added.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001512// inputPropertiesSlice - must be a slice of propertiesContainer interfaces.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001513//
1514// Iterates over each exported field (capitalized name) and checks to see whether they
1515// have the same value (using DeepEquals) across all the input properties. If it does not then no
1516// change is made. Otherwise, the common value is stored in the field in the commonProperties
1517// and the field in each of the input properties structure is set to its default value.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001518func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001519 commonPropertiesValue := reflect.ValueOf(commonProperties)
1520 commonStructValue := commonPropertiesValue.Elem()
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001521
Paul Duffinf34f6d82020-04-30 15:48:31 +01001522 sliceValue := reflect.ValueOf(inputPropertiesSlice)
1523
Paul Duffinb28369a2020-05-04 15:39:59 +01001524 for _, property := range e.properties {
1525 fieldGetter := property.getter
Paul Duffinc459f892020-04-30 18:08:29 +01001526 filter := property.filter
1527 if filter == nil {
1528 filter = func(metadata propertiesContainer) bool {
1529 return true
1530 }
1531 }
Paul Duffinb28369a2020-05-04 15:39:59 +01001532
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001533 // Check to see if all the structures have the same value for the field. The commonValue
Paul Duffin864e1b42020-05-06 10:23:19 +01001534 // is nil on entry to the loop and if it is nil on exit then there is no common value or
1535 // all the values have been filtered out, otherwise it points to the common value.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001536 var commonValue *reflect.Value
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001537
Paul Duffin864e1b42020-05-06 10:23:19 +01001538 // Assume that all the values will be the same.
1539 //
1540 // While similar to this is not quite the same as commonValue == nil. If all the values
1541 // have been filtered out then this will be false but commonValue == nil will be true.
1542 valuesDiffer := false
1543
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001544 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01001545 container := sliceValue.Index(i).Interface().(propertiesContainer)
1546 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00001547 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001548
Paul Duffinc459f892020-04-30 18:08:29 +01001549 if !filter(container) {
1550 expectedValue := property.emptyValue.Interface()
1551 actualValue := fieldValue.Interface()
1552 if !reflect.DeepEqual(expectedValue, actualValue) {
1553 return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue)
1554 }
1555 continue
1556 }
1557
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001558 if commonValue == nil {
1559 // Use the first value as the commonProperties value.
1560 commonValue = &fieldValue
1561 } else {
1562 // If the value does not match the current common value then there is
1563 // no value in common so break out.
1564 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
1565 commonValue = nil
Paul Duffin864e1b42020-05-06 10:23:19 +01001566 valuesDiffer = true
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001567 break
1568 }
1569 }
1570 }
1571
Paul Duffin864e1b42020-05-06 10:23:19 +01001572 // If the fields all have common value then store it in the common struct field
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001573 // and set the input struct's field to the empty value.
1574 if commonValue != nil {
Paul Duffinb28369a2020-05-04 15:39:59 +01001575 emptyValue := property.emptyValue
Paul Duffinc097e362020-03-10 22:50:03 +00001576 fieldGetter(commonStructValue).Set(*commonValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001577 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01001578 container := sliceValue.Index(i).Interface().(propertiesContainer)
1579 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00001580 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001581 fieldValue.Set(emptyValue)
1582 }
1583 }
Paul Duffin864e1b42020-05-06 10:23:19 +01001584
1585 if valuesDiffer && !property.archVariant {
1586 // The values differ but the property does not support arch variants so it
1587 // is an error.
1588 var details strings.Builder
1589 for i := 0; i < sliceValue.Len(); i++ {
1590 container := sliceValue.Index(i).Interface().(propertiesContainer)
1591 itemValue := reflect.ValueOf(container.optimizableProperties())
1592 fieldValue := fieldGetter(itemValue)
1593
1594 _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface())
1595 }
1596
1597 return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String())
1598 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001599 }
Paul Duffin4b8b7932020-05-06 12:35:38 +01001600
1601 return nil
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001602}