blob: 1ba58064d376fe0cecc590e118699654544866ba [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"
Paul Duffin375058f2019-11-29 20:17:53 +000025 "github.com/google/blueprint"
Jiyong Park9b409bc2019-10-11 14:59:13 +090026 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
Jiyong Park9b409bc2019-10-11 14:59:13 +090029)
30
31var pctx = android.NewPackageContext("android/soong/sdk")
32
Paul Duffin375058f2019-11-29 20:17:53 +000033var (
34 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
35 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000036 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000037 CommandDeps: []string{
38 "${config.Zip2ZipCmd}",
39 },
40 },
41 "destdir")
42
43 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
44 blueprint.RuleParams{
45 Command: `${config.SoongZipCmd} -C $basedir -l $out.rsp -o $out`,
46 CommandDeps: []string{
47 "${config.SoongZipCmd}",
48 },
49 Rspfile: "$out.rsp",
50 RspfileContent: "$in",
51 },
52 "basedir")
53
54 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
55 blueprint.RuleParams{
56 Command: `${config.MergeZipsCmd} $out $in`,
57 CommandDeps: []string{
58 "${config.MergeZipsCmd}",
59 },
60 })
61)
62
Paul Duffinb645ec82019-11-27 17:43:54 +000063type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090064 content strings.Builder
65 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090066}
67
Paul Duffinb645ec82019-11-27 17:43:54 +000068// generatedFile abstracts operations for writing contents into a file and emit a build rule
69// for the file.
70type generatedFile struct {
71 generatedContents
72 path android.OutputPath
73}
74
Jiyong Park232e7852019-11-04 12:23:40 +090075func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +090076 return &generatedFile{
Paul Duffinb645ec82019-11-27 17:43:54 +000077 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park9b409bc2019-10-11 14:59:13 +090078 }
79}
80
Paul Duffinb645ec82019-11-27 17:43:54 +000081func (gc *generatedContents) Indent() {
82 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +090083}
84
Paul Duffinb645ec82019-11-27 17:43:54 +000085func (gc *generatedContents) Dedent() {
86 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +090087}
88
Paul Duffinb645ec82019-11-27 17:43:54 +000089func (gc *generatedContents) Printfln(format string, args ...interface{}) {
Paul Duffin11108272020-05-11 22:59:25 +010090 fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\n", args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +090091}
92
93func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
94 rb := android.NewRuleBuilder()
Paul Duffin11108272020-05-11 22:59:25 +010095
96 content := gf.content.String()
97
98 // ninja consumes newline characters in rspfile_content. Prevent it by
99 // escaping the backslash in the newline character. The extra backslash
100 // is removed when the rspfile is written to the actual script file
101 content = strings.ReplaceAll(content, "\n", "\\n")
102
Jiyong Park9b409bc2019-10-11 14:59:13 +0900103 rb.Command().
104 Implicits(implicits).
Paul Duffin11108272020-05-11 22:59:25 +0100105 Text("echo").Text(proptools.ShellEscape(content)).
106 // convert \\n to \n
Jiyong Park9b409bc2019-10-11 14:59:13 +0900107 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
108 rb.Command().
109 Text("chmod a+x").Output(gf.path)
110 rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base())
111}
112
Paul Duffin13879572019-11-28 14:31:38 +0000113// Collect all the members.
114//
Paul Duffin6a7e9532020-03-20 17:50:07 +0000115// Returns a list containing type (extracted from the dependency tag) and the variant
116// plus the multilib usages.
117func (s *sdk) collectMembers(ctx android.ModuleContext) {
118 s.multilibUsages = multilibNone
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000119 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
120 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf8539922019-11-19 19:44:10 +0000121 if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok {
122 memberType := memberTag.SdkMemberType()
Jiyong Park9b409bc2019-10-11 14:59:13 +0900123
Paul Duffin13879572019-11-28 14:31:38 +0000124 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000125 if !memberType.IsInstance(child) {
126 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900127 }
Paul Duffin13879572019-11-28 14:31:38 +0000128
Paul Duffin6a7e9532020-03-20 17:50:07 +0000129 // Keep track of which multilib variants are used by the sdk.
130 s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType)
131
132 s.memberRefs = append(s.memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)})
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000133
134 // If the member type supports transitive sdk members then recurse down into
135 // its dependencies, otherwise exit traversal.
136 return memberType.HasTransitiveSdkMembers()
Jiyong Park73c54ee2019-10-22 20:31:18 +0900137 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000138
139 return false
Paul Duffin13879572019-11-28 14:31:38 +0000140 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000141}
142
143// Organize the members.
144//
145// The members are first grouped by type and then grouped by name. The order of
146// the types is the order they are referenced in android.SdkMemberTypesRegistry.
147// The names are in the order in which the dependencies were added.
148//
149// Returns the members as well as the multilib setting to use.
Paul Duffin6a7e9532020-03-20 17:50:07 +0000150func (s *sdk) organizeMembers(ctx android.ModuleContext, memberRefs []sdkMemberRef) []*sdkMember {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000151 byType := make(map[android.SdkMemberType][]*sdkMember)
152 byName := make(map[string]*sdkMember)
153
Paul Duffin1356d8c2020-02-25 19:26:33 +0000154 for _, memberRef := range memberRefs {
155 memberType := memberRef.memberType
156 variant := memberRef.variant
157
158 name := ctx.OtherModuleName(variant)
159 member := byName[name]
160 if member == nil {
161 member = &sdkMember{memberType: memberType, name: name}
162 byName[name] = member
163 byType[memberType] = append(byType[memberType], member)
164 }
165
Paul Duffin1356d8c2020-02-25 19:26:33 +0000166 // Only append new variants to the list. This is needed because a member can be both
167 // exported by the sdk and also be a transitive sdk member.
168 member.variants = appendUniqueVariants(member.variants, variant)
169 }
170
Paul Duffin13879572019-11-28 14:31:38 +0000171 var members []*sdkMember
Paul Duffin72910952020-01-20 18:16:30 +0000172 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13879572019-11-28 14:31:38 +0000173 membersOfType := byType[memberListProperty.memberType]
174 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900175 }
176
Paul Duffin6a7e9532020-03-20 17:50:07 +0000177 return members
Jiyong Park73c54ee2019-10-22 20:31:18 +0900178}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900179
Paul Duffin72910952020-01-20 18:16:30 +0000180func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
181 for _, v := range variants {
182 if v == newVariant {
183 return variants
184 }
185 }
186 return append(variants, newVariant)
187}
188
Jiyong Park73c54ee2019-10-22 20:31:18 +0900189// SDK directory structure
190// <sdk_root>/
191// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
192// <api_ver>/ : below this directory are all auto-generated
193// Android.bp : definition of 'sdk_snapshot' module is here
194// aidl/
195// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
196// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900197// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900198// include/
199// bionic/libc/include/stdlib.h : an exported header file
200// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900201// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900202// <arch>/include/ : arch-specific exported headers
203// <arch>/include_gen/ : arch-specific generated headers
204// <arch>/lib/
205// libFoo.so : a stub library
206
Jiyong Park232e7852019-11-04 12:23:40 +0900207// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900208// This isn't visible to users, so could be changed in future.
209func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
210 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
211}
212
Jiyong Park232e7852019-11-04 12:23:40 +0900213// buildSnapshot is the main function in this source file. It creates rules to copy
214// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000215func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath {
216
Paul Duffin13f02712020-03-06 12:30:43 +0000217 allMembersByName := make(map[string]struct{})
218 exportedMembersByName := make(map[string]struct{})
Paul Duffin1356d8c2020-02-25 19:26:33 +0000219 var memberRefs []sdkMemberRef
220 for _, sdkVariant := range sdkVariants {
221 memberRefs = append(memberRefs, sdkVariant.memberRefs...)
Paul Duffin865171e2020-03-02 18:38:15 +0000222
Paul Duffin13f02712020-03-06 12:30:43 +0000223 // Record the names of all the members, both explicitly specified and implicitly
224 // included.
225 for _, memberRef := range sdkVariant.memberRefs {
226 allMembersByName[memberRef.variant.Name()] = struct{}{}
227 }
228
Paul Duffin865171e2020-03-02 18:38:15 +0000229 // Merge the exported member sets from all sdk variants.
230 for key, _ := range sdkVariant.getExportedMembers() {
Paul Duffin13f02712020-03-06 12:30:43 +0000231 exportedMembersByName[key] = struct{}{}
Paul Duffin865171e2020-03-02 18:38:15 +0000232 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000233 }
234
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000235 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900236
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000237 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000238
239 bpFile := &bpFile{
240 modules: make(map[string]*bpModule),
241 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000242
243 builder := &snapshotBuilder{
Paul Duffin13f02712020-03-06 12:30:43 +0000244 ctx: ctx,
245 sdk: s,
246 version: "current",
247 snapshotDir: snapshotDir.OutputPath,
248 copies: make(map[string]string),
249 filesToZip: []android.Path{bp.path},
250 bpFile: bpFile,
251 prebuiltModules: make(map[string]*bpModule),
252 allMembersByName: allMembersByName,
253 exportedMembersByName: exportedMembersByName,
Jiyong Park73c54ee2019-10-22 20:31:18 +0900254 }
Paul Duffinac37c502019-11-26 18:02:20 +0000255 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900256
Paul Duffin6a7e9532020-03-20 17:50:07 +0000257 members := s.organizeMembers(ctx, memberRefs)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000258 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000259 memberType := member.memberType
Paul Duffin3a4eb502020-03-19 16:11:18 +0000260
Paul Duffina551a1c2020-03-17 21:04:24 +0000261 memberCtx := &memberContext{ctx, builder, memberType, member.name}
Paul Duffin3a4eb502020-03-19 16:11:18 +0000262
263 prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
Paul Duffin495ffb92020-03-20 13:35:40 +0000264 s.createMemberSnapshot(memberCtx, member, prebuiltModule)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900265 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900266
Paul Duffine6c0d842020-01-15 14:08:51 +0000267 // Create a transformer that will transform an unversioned module into a versioned module.
268 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
269
Paul Duffin72910952020-01-20 18:16:30 +0000270 // Create a transformer that will transform an unversioned module by replacing any references
271 // to internal members with a unique module name and setting prefer: false.
272 unversionedTransformer := unversionedTransformation{builder: builder}
273
Paul Duffinb645ec82019-11-27 17:43:54 +0000274 for _, unversioned := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000275 // Prune any empty property sets.
276 unversioned = unversioned.transform(pruneEmptySetTransformer{})
277
Paul Duffinb645ec82019-11-27 17:43:54 +0000278 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000279 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000280
281 // Transform the unversioned module into a versioned one.
282 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000283 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000284
Paul Duffin72910952020-01-20 18:16:30 +0000285 // Transform the unversioned module to make it suitable for use in the snapshot.
286 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000287 bpFile.AddModule(unversioned)
288 }
289
290 // Create the snapshot module.
291 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000292 var snapshotModuleType string
293 if s.properties.Module_exports {
294 snapshotModuleType = "module_exports_snapshot"
295 } else {
296 snapshotModuleType = "sdk_snapshot"
297 }
298 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000299 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000300
301 // Make sure that the snapshot has the same visibility as the sdk.
302 visibility := android.EffectiveVisibilityRules(ctx, s)
303 if len(visibility) != 0 {
304 snapshotModule.AddProperty("visibility", visibility)
305 }
306
Paul Duffin865171e2020-03-02 18:38:15 +0000307 addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000308
Paul Duffinf34f6d82020-04-30 15:48:31 +0100309 var dynamicMemberPropertiesContainers []propertiesContainer
Paul Duffin865171e2020-03-02 18:38:15 +0000310 osTypeToMemberProperties := make(map[android.OsType]*sdk)
311 for _, sdkVariant := range sdkVariants {
312 properties := sdkVariant.dynamicMemberTypeListProperties
313 osTypeToMemberProperties[sdkVariant.Target().Os] = sdkVariant
Paul Duffin4b8b7932020-05-06 12:35:38 +0100314 dynamicMemberPropertiesContainers = append(dynamicMemberPropertiesContainers, &dynamicMemberPropertiesContainer{sdkVariant, properties})
Paul Duffin865171e2020-03-02 18:38:15 +0000315 }
316
317 // Extract the common lists of members into a separate struct.
318 commonDynamicMemberProperties := s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffinc097e362020-03-10 22:50:03 +0000319 extractor := newCommonValueExtractor(commonDynamicMemberProperties)
Paul Duffin4b8b7932020-05-06 12:35:38 +0100320 extractCommonProperties(ctx, extractor, commonDynamicMemberProperties, dynamicMemberPropertiesContainers)
Paul Duffin865171e2020-03-02 18:38:15 +0000321
322 // Add properties common to all os types.
323 s.addMemberPropertiesToPropertySet(builder, snapshotModule, commonDynamicMemberProperties)
324
325 // Iterate over the os types in a fixed order.
Paul Duffin6a7e9532020-03-20 17:50:07 +0000326 targetPropertySet := snapshotModule.AddPropertySet("target")
Paul Duffin865171e2020-03-02 18:38:15 +0000327 for _, osType := range s.getPossibleOsTypes() {
328 if sdkVariant, ok := osTypeToMemberProperties[osType]; ok {
329 osPropertySet := targetPropertySet.AddPropertySet(sdkVariant.Target().Os.Name)
Paul Duffin6a7e9532020-03-20 17:50:07 +0000330
331 // Compile_multilib defaults to both and must always be set to both on the
332 // device and so only needs to be set when targeted at the host and is neither
333 // unspecified or both.
334 multilib := sdkVariant.multilibUsages
335 if (osType.Class == android.Host || osType.Class == android.HostCross) &&
336 multilib != multilibNone && multilib != multilibBoth {
337 osPropertySet.AddProperty("compile_multilib", multilib.String())
338 }
339
Paul Duffin865171e2020-03-02 18:38:15 +0000340 s.addMemberPropertiesToPropertySet(builder, osPropertySet, sdkVariant.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000341 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000342 }
Paul Duffin865171e2020-03-02 18:38:15 +0000343
344 // Prune any empty property sets.
345 snapshotModule.transform(pruneEmptySetTransformer{})
346
Paul Duffinb645ec82019-11-27 17:43:54 +0000347 bpFile.AddModule(snapshotModule)
348
349 // generate Android.bp
350 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
351 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000352
Paul Duffinf88d8e02020-05-07 20:21:34 +0100353 contents := bp.content.String()
354 syntaxCheckSnapshotBpFile(ctx, contents)
355
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000356 bp.build(pctx, ctx, nil)
357
358 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900359
Jiyong Park232e7852019-11-04 12:23:40 +0900360 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000361 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000362 outputDesc := "Building snapshot for " + ctx.ModuleName()
363
364 // If there are no zips to merge then generate the output zip directly.
365 // Otherwise, generate an intermediate zip file into which other zips can be
366 // merged.
367 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000368 var desc string
369 if len(builder.zipsToMerge) == 0 {
370 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000371 desc = outputDesc
372 } else {
373 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000374 desc = "Building intermediate snapshot for " + ctx.ModuleName()
375 }
376
Paul Duffin375058f2019-11-29 20:17:53 +0000377 ctx.Build(pctx, android.BuildParams{
378 Description: desc,
379 Rule: zipFiles,
380 Inputs: filesToZip,
381 Output: zipFile,
382 Args: map[string]string{
383 "basedir": builder.snapshotDir.String(),
384 },
385 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900386
Paul Duffin91547182019-11-12 19:39:36 +0000387 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000388 ctx.Build(pctx, android.BuildParams{
389 Description: outputDesc,
390 Rule: mergeZips,
391 Input: zipFile,
392 Inputs: builder.zipsToMerge,
393 Output: outputZipFile,
394 })
Paul Duffin91547182019-11-12 19:39:36 +0000395 }
396
397 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900398}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000399
Paul Duffinf88d8e02020-05-07 20:21:34 +0100400// Check the syntax of the generated Android.bp file contents and if they are
401// invalid then log an error with the contents (tagged with line numbers) and the
402// errors that were found so that it is easy to see where the problem lies.
403func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) {
404 errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents)
405 if len(errs) != 0 {
406 message := &strings.Builder{}
407 _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot:
408
409Generated Android.bp contents
410========================================================================
411`)
412 for i, line := range strings.Split(contents, "\n") {
413 _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line)
414 }
415
416 _, _ = fmt.Fprint(message, `
417========================================================================
418
419Errors found:
420`)
421
422 for _, err := range errs {
423 _, _ = fmt.Fprintf(message, "%s\n", err.Error())
424 }
425
426 ctx.ModuleErrorf("%s", message.String())
427 }
428}
429
Paul Duffin4b8b7932020-05-06 12:35:38 +0100430func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
431 err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
432 if err != nil {
433 ctx.ModuleErrorf("error extracting common properties: %s", err)
434 }
435}
436
Paul Duffin865171e2020-03-02 18:38:15 +0000437func (s *sdk) addMemberPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, dynamicMemberTypeListProperties interface{}) {
438 for _, memberListProperty := range s.memberListProperties() {
439 names := memberListProperty.getter(dynamicMemberTypeListProperties)
440 if len(names) > 0 {
Paul Duffin13f02712020-03-06 12:30:43 +0000441 propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names, false))
Paul Duffin865171e2020-03-02 18:38:15 +0000442 }
443 }
444}
445
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000446type propertyTag struct {
447 name string
448}
449
Paul Duffin0cb37b92020-03-04 14:52:46 +0000450// A BpPropertyTag to add to a property that contains references to other sdk members.
451//
452// This will cause the references to be rewritten to a versioned reference in the version
453// specific instance of a snapshot module.
Paul Duffin13f02712020-03-06 12:30:43 +0000454var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
Paul Duffin13f02712020-03-06 12:30:43 +0000455var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000456
Paul Duffin0cb37b92020-03-04 14:52:46 +0000457// A BpPropertyTag that indicates the property should only be present in the versioned
458// module.
459//
460// This will cause the property to be removed from the unversioned instance of a
461// snapshot module.
462var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"}
463
Paul Duffine6c0d842020-01-15 14:08:51 +0000464type unversionedToVersionedTransformation struct {
465 identityTransformation
466 builder *snapshotBuilder
467}
468
Paul Duffine6c0d842020-01-15 14:08:51 +0000469func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
470 // Use a versioned name for the module but remember the original name for the
471 // snapshot.
472 name := module.getValue("name").(string)
Paul Duffin13f02712020-03-06 12:30:43 +0000473 module.setProperty("name", t.builder.versionedSdkMemberName(name, true))
Paul Duffine6c0d842020-01-15 14:08:51 +0000474 module.insertAfter("name", "sdk_member_name", name)
475 return module
476}
477
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000478func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000479 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
480 required := tag == requiredSdkMemberReferencePropertyTag
481 return t.builder.versionedSdkMemberNames(value.([]string), required), tag
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000482 } else {
483 return value, tag
484 }
485}
486
Paul Duffin72910952020-01-20 18:16:30 +0000487type unversionedTransformation struct {
488 identityTransformation
489 builder *snapshotBuilder
490}
491
492func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
493 // If the module is an internal member then use a unique name for it.
494 name := module.getValue("name").(string)
Paul Duffin13f02712020-03-06 12:30:43 +0000495 module.setProperty("name", t.builder.unversionedSdkMemberName(name, true))
Paul Duffin72910952020-01-20 18:16:30 +0000496
497 // Set prefer: false - this is not strictly required as that is the default.
498 module.insertAfter("name", "prefer", false)
499
500 return module
501}
502
503func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000504 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
505 required := tag == requiredSdkMemberReferencePropertyTag
506 return t.builder.unversionedSdkMemberNames(value.([]string), required), tag
Paul Duffin0cb37b92020-03-04 14:52:46 +0000507 } else if tag == sdkVersionedOnlyPropertyTag {
508 // The property is not allowed in the unversioned module so remove it.
509 return nil, nil
Paul Duffin72910952020-01-20 18:16:30 +0000510 } else {
511 return value, tag
512 }
513}
514
Paul Duffina78f3a72020-02-21 16:29:35 +0000515type pruneEmptySetTransformer struct {
516 identityTransformation
517}
518
519var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
520
521func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
522 if len(propertySet.properties) == 0 {
523 return nil, nil
524 } else {
525 return propertySet, tag
526 }
527}
528
Paul Duffinb645ec82019-11-27 17:43:54 +0000529func generateBpContents(contents *generatedContents, bpFile *bpFile) {
530 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
531 for _, bpModule := range bpFile.order {
532 contents.Printfln("")
533 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000534 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000535 contents.Printfln("}")
536 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000537}
538
539func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
540 contents.Indent()
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000541
542 // Output the properties first, followed by the nested sets. This ensures a
543 // consistent output irrespective of whether property sets are created before
544 // or after the properties. This simplifies the creation of the module.
Paul Duffinb645ec82019-11-27 17:43:54 +0000545 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000546 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000547
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000548 switch v := value.(type) {
549 case []string:
550 length := len(v)
Paul Duffinb645ec82019-11-27 17:43:54 +0000551 if length > 1 {
552 contents.Printfln("%s: [", name)
553 contents.Indent()
554 for i := 0; i < length; i = i + 1 {
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000555 contents.Printfln("%q,", v[i])
Paul Duffinb645ec82019-11-27 17:43:54 +0000556 }
557 contents.Dedent()
558 contents.Printfln("],")
559 } else if length == 0 {
560 contents.Printfln("%s: [],", name)
561 } else {
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000562 contents.Printfln("%s: [%q],", name, v[0])
Paul Duffinb645ec82019-11-27 17:43:54 +0000563 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000564
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000565 case bool:
566 contents.Printfln("%s: %t,", name, v)
567
568 case *bpPropertySet:
569 // Do not write property sets in the properties phase.
Paul Duffinb645ec82019-11-27 17:43:54 +0000570
571 default:
572 contents.Printfln("%s: %q,", name, value)
573 }
574 }
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000575
576 for _, name := range set.order {
577 value := set.getValue(name)
578
579 // Only write property sets in the sets phase.
580 switch v := value.(type) {
581 case *bpPropertySet:
582 contents.Printfln("%s: {", name)
583 outputPropertySet(contents, v)
584 contents.Printfln("},")
585 }
586 }
587
Paul Duffinb645ec82019-11-27 17:43:54 +0000588 contents.Dedent()
589}
590
Paul Duffinac37c502019-11-26 18:02:20 +0000591func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000592 contents := &generatedContents{}
593 generateBpContents(contents, s.builderForTests.bpFile)
594 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000595}
596
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000597type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000598 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000599 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000600 version string
601 snapshotDir android.OutputPath
602 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000603
604 // Map from destination to source of each copy - used to eliminate duplicates and
605 // detect conflicts.
606 copies map[string]string
607
Paul Duffinb645ec82019-11-27 17:43:54 +0000608 filesToZip android.Paths
609 zipsToMerge android.Paths
610
611 prebuiltModules map[string]*bpModule
612 prebuiltOrder []*bpModule
Paul Duffin13f02712020-03-06 12:30:43 +0000613
614 // The set of all members by name.
615 allMembersByName map[string]struct{}
616
617 // The set of exported members by name.
618 exportedMembersByName map[string]struct{}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000619}
620
621func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000622 if existing, ok := s.copies[dest]; ok {
623 if existing != src.String() {
624 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
625 return
626 }
627 } else {
628 path := s.snapshotDir.Join(s.ctx, dest)
629 s.ctx.Build(pctx, android.BuildParams{
630 Rule: android.Cp,
631 Input: src,
632 Output: path,
633 })
634 s.filesToZip = append(s.filesToZip, path)
635
636 s.copies[dest] = src.String()
637 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000638}
639
Paul Duffin91547182019-11-12 19:39:36 +0000640func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
641 ctx := s.ctx
642
643 // Repackage the zip file so that the entries are in the destDir directory.
644 // This will allow the zip file to be merged into the snapshot.
645 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000646
647 ctx.Build(pctx, android.BuildParams{
648 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
649 Rule: repackageZip,
650 Input: zipPath,
651 Output: tmpZipPath,
652 Args: map[string]string{
653 "destdir": destDir,
654 },
655 })
Paul Duffin91547182019-11-12 19:39:36 +0000656
657 // Add the repackaged zip file to the files to merge.
658 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
659}
660
Paul Duffin9d8d6092019-12-05 18:19:29 +0000661func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
662 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000663 if s.prebuiltModules[name] != nil {
664 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
665 }
666
667 m := s.bpFile.newModule(moduleType)
668 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000669
Paul Duffinbefa4b92020-03-04 14:22:45 +0000670 variant := member.Variants()[0]
671
Paul Duffin13f02712020-03-06 12:30:43 +0000672 if s.isInternalMember(name) {
Paul Duffin72910952020-01-20 18:16:30 +0000673 // An internal member is only referenced from the sdk snapshot which is in the
674 // same package so can be marked as private.
675 m.AddProperty("visibility", []string{"//visibility:private"})
676 } else {
677 // Extract visibility information from a member variant. All variants have the same
678 // visibility so it doesn't matter which one is used.
Paul Duffinbefa4b92020-03-04 14:22:45 +0000679 visibility := android.EffectiveVisibilityRules(s.ctx, variant)
Paul Duffin72910952020-01-20 18:16:30 +0000680 if len(visibility) != 0 {
681 m.AddProperty("visibility", visibility)
682 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000683 }
684
Paul Duffin865171e2020-03-02 18:38:15 +0000685 deviceSupported := false
686 hostSupported := false
687
688 for _, variant := range member.Variants() {
689 osClass := variant.Target().Os.Class
690 if osClass == android.Host || osClass == android.HostCross {
691 hostSupported = true
692 } else if osClass == android.Device {
693 deviceSupported = true
694 }
695 }
696
697 addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000698
Paul Duffinbefa4b92020-03-04 14:22:45 +0000699 // Where available copy apex_available properties from the member.
700 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
701 apexAvailable := apexAware.ApexAvailable()
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000702
703 // Add in any white listed apex available settings.
704 apexAvailable = append(apexAvailable, apex.WhitelistedApexAvailable(member.Name())...)
705
Paul Duffinbefa4b92020-03-04 14:22:45 +0000706 if len(apexAvailable) > 0 {
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000707 // Remove duplicates and sort.
708 apexAvailable = android.FirstUniqueStrings(apexAvailable)
709 sort.Strings(apexAvailable)
710
Paul Duffinbefa4b92020-03-04 14:22:45 +0000711 m.AddProperty("apex_available", apexAvailable)
712 }
713 }
714
Paul Duffin0cb37b92020-03-04 14:52:46 +0000715 // Disable installation in the versioned module of those modules that are ever installable.
716 if installable, ok := variant.(interface{ EverInstallable() bool }); ok {
717 if installable.EverInstallable() {
718 m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag)
719 }
720 }
721
Paul Duffinb645ec82019-11-27 17:43:54 +0000722 s.prebuiltModules[name] = m
723 s.prebuiltOrder = append(s.prebuiltOrder, m)
724 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000725}
726
Paul Duffin865171e2020-03-02 18:38:15 +0000727func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
728 if !deviceSupported {
Paul Duffine44358f2019-11-26 18:04:12 +0000729 bpModule.AddProperty("device_supported", false)
730 }
Paul Duffin865171e2020-03-02 18:38:15 +0000731 if hostSupported {
Paul Duffine44358f2019-11-26 18:04:12 +0000732 bpModule.AddProperty("host_supported", true)
733 }
734}
735
Paul Duffin13f02712020-03-06 12:30:43 +0000736func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag {
737 if required {
738 return requiredSdkMemberReferencePropertyTag
739 } else {
740 return optionalSdkMemberReferencePropertyTag
741 }
742}
743
744func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag {
745 return optionalSdkMemberReferencePropertyTag
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000746}
747
Paul Duffinb645ec82019-11-27 17:43:54 +0000748// Get a versioned name appropriate for the SDK snapshot version being taken.
Paul Duffin13f02712020-03-06 12:30:43 +0000749func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string, required bool) string {
750 if _, ok := s.allMembersByName[unversionedName]; !ok {
751 if required {
752 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
753 }
754 return unversionedName
755 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000756 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
757}
Paul Duffinb645ec82019-11-27 17:43:54 +0000758
Paul Duffin13f02712020-03-06 12:30:43 +0000759func (s *snapshotBuilder) versionedSdkMemberNames(members []string, required bool) []string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000760 var references []string = nil
761 for _, m := range members {
Paul Duffin13f02712020-03-06 12:30:43 +0000762 references = append(references, s.versionedSdkMemberName(m, required))
Paul Duffinb645ec82019-11-27 17:43:54 +0000763 }
764 return references
765}
Paul Duffin13879572019-11-28 14:31:38 +0000766
Paul Duffin72910952020-01-20 18:16:30 +0000767// Get an internal name unique to the sdk.
Paul Duffin13f02712020-03-06 12:30:43 +0000768func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string, required bool) string {
769 if _, ok := s.allMembersByName[unversionedName]; !ok {
770 if required {
771 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
772 }
773 return unversionedName
774 }
775
776 if s.isInternalMember(unversionedName) {
Paul Duffin72910952020-01-20 18:16:30 +0000777 return s.ctx.ModuleName() + "_" + unversionedName
778 } else {
779 return unversionedName
780 }
781}
782
Paul Duffin13f02712020-03-06 12:30:43 +0000783func (s *snapshotBuilder) unversionedSdkMemberNames(members []string, required bool) []string {
Paul Duffin72910952020-01-20 18:16:30 +0000784 var references []string = nil
785 for _, m := range members {
Paul Duffin13f02712020-03-06 12:30:43 +0000786 references = append(references, s.unversionedSdkMemberName(m, required))
Paul Duffin72910952020-01-20 18:16:30 +0000787 }
788 return references
789}
790
Paul Duffin13f02712020-03-06 12:30:43 +0000791func (s *snapshotBuilder) isInternalMember(memberName string) bool {
792 _, ok := s.exportedMembersByName[memberName]
793 return !ok
794}
795
Paul Duffin1356d8c2020-02-25 19:26:33 +0000796type sdkMemberRef struct {
797 memberType android.SdkMemberType
798 variant android.SdkAware
799}
800
Paul Duffin13879572019-11-28 14:31:38 +0000801var _ android.SdkMember = (*sdkMember)(nil)
802
803type sdkMember struct {
804 memberType android.SdkMemberType
805 name string
806 variants []android.SdkAware
807}
808
809func (m *sdkMember) Name() string {
810 return m.name
811}
812
813func (m *sdkMember) Variants() []android.SdkAware {
814 return m.variants
815}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000816
Paul Duffin9c3760e2020-03-16 19:52:08 +0000817// Track usages of multilib variants.
818type multilibUsage int
819
820const (
821 multilibNone multilibUsage = 0
822 multilib32 multilibUsage = 1
823 multilib64 multilibUsage = 2
824 multilibBoth = multilib32 | multilib64
825)
826
827// Add the multilib that is used in the arch type.
828func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage {
829 multilib := archType.Multilib
830 switch multilib {
831 case "":
832 return m
833 case "lib32":
834 return m | multilib32
835 case "lib64":
836 return m | multilib64
837 default:
838 panic(fmt.Errorf("Unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib))
839 }
840}
841
842func (m multilibUsage) String() string {
843 switch m {
844 case multilibNone:
845 return ""
846 case multilib32:
847 return "32"
848 case multilib64:
849 return "64"
850 case multilibBoth:
851 return "both"
852 default:
853 panic(fmt.Errorf("Unknown multilib value, found %b, expected one of %b, %b, %b or %b",
854 m, multilibNone, multilib32, multilib64, multilibBoth))
855 }
856}
857
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000858type baseInfo struct {
859 Properties android.SdkMemberProperties
860}
861
Paul Duffinf34f6d82020-04-30 15:48:31 +0100862func (b *baseInfo) optimizableProperties() interface{} {
863 return b.Properties
864}
865
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000866type osTypeSpecificInfo struct {
867 baseInfo
868
Paul Duffin00e46802020-03-12 20:40:35 +0000869 osType android.OsType
870
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000871 // The list of arch type specific info for this os type.
Paul Duffinb44b33a2020-03-17 10:58:23 +0000872 //
873 // Nil if there is one variant whose arch type is common
874 archInfos []*archTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000875}
876
Paul Duffin4b8b7932020-05-06 12:35:38 +0100877var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
878
Paul Duffinfc8dd232020-03-17 12:51:37 +0000879type variantPropertiesFactoryFunc func() android.SdkMemberProperties
880
Paul Duffin00e46802020-03-12 20:40:35 +0000881// Create a new osTypeSpecificInfo for the specified os type and its properties
882// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000883func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
Paul Duffin00e46802020-03-12 20:40:35 +0000884 osInfo := &osTypeSpecificInfo{
885 osType: osType,
886 }
887
888 osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
889 properties := variantPropertiesFactory()
890 properties.Base().Os = osType
891 return properties
892 }
893
894 // Create a structure into which properties common across the architectures in
895 // this os type will be stored.
896 osInfo.Properties = osSpecificVariantPropertiesFactory()
897
898 // Group the variants by arch type.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000899 var variantsByArchName = make(map[string][]android.Module)
Paul Duffin00e46802020-03-12 20:40:35 +0000900 var archTypes []android.ArchType
901 for _, variant := range osTypeVariants {
902 archType := variant.Target().Arch.ArchType
903 archTypeName := archType.Name
904 if _, ok := variantsByArchName[archTypeName]; !ok {
905 archTypes = append(archTypes, archType)
906 }
907
908 variantsByArchName[archTypeName] = append(variantsByArchName[archTypeName], variant)
909 }
910
911 if commonVariants, ok := variantsByArchName["common"]; ok {
912 if len(osTypeVariants) != 1 {
913 panic("Expected to only have 1 variant when arch type is common but found " + string(len(osTypeVariants)))
914 }
915
916 // A common arch type only has one variant and its properties should be treated
917 // as common to the os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000918 osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
Paul Duffin00e46802020-03-12 20:40:35 +0000919 } else {
920 // Create an arch specific info for each supported architecture type.
921 for _, archType := range archTypes {
922 archTypeName := archType.Name
923
924 archVariants := variantsByArchName[archTypeName]
Paul Duffin3a4eb502020-03-19 16:11:18 +0000925 archInfo := newArchSpecificInfo(ctx, archType, osSpecificVariantPropertiesFactory, archVariants)
Paul Duffin00e46802020-03-12 20:40:35 +0000926
927 osInfo.archInfos = append(osInfo.archInfos, archInfo)
928 }
929 }
930
931 return osInfo
932}
933
934// Optimize the properties by extracting common properties from arch type specific
935// properties into os type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +0100936func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin00e46802020-03-12 20:40:35 +0000937 // Nothing to do if there is only a single common architecture.
938 if len(osInfo.archInfos) == 0 {
939 return
940 }
941
Paul Duffin9c3760e2020-03-16 19:52:08 +0000942 multilib := multilibNone
Paul Duffin00e46802020-03-12 20:40:35 +0000943 for _, archInfo := range osInfo.archInfos {
Paul Duffin9c3760e2020-03-16 19:52:08 +0000944 multilib = multilib.addArchType(archInfo.archType)
945
Paul Duffin9b76c0b2020-03-12 10:24:35 +0000946 // Optimize the arch properties first.
Paul Duffin4b8b7932020-05-06 12:35:38 +0100947 archInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin00e46802020-03-12 20:40:35 +0000948 }
949
Paul Duffin4b8b7932020-05-06 12:35:38 +0100950 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
Paul Duffin00e46802020-03-12 20:40:35 +0000951
952 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
Paul Duffin9c3760e2020-03-16 19:52:08 +0000953 osInfo.Properties.Base().Compile_multilib = multilib.String()
Paul Duffin00e46802020-03-12 20:40:35 +0000954}
955
956// Add the properties for an os to a property set.
957//
958// Maps the properties related to the os variants through to an appropriate
959// module structure that will produce equivalent set of variants when it is
960// processed in a build.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000961func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
Paul Duffin00e46802020-03-12 20:40:35 +0000962
963 var osPropertySet android.BpPropertySet
964 var archPropertySet android.BpPropertySet
965 var archOsPrefix string
966 if osInfo.Properties.Base().Os_count == 1 {
967 // There is only one os type present in the variants so don't bother
968 // with adding target specific properties.
969
970 // Create a structure that looks like:
971 // module_type {
972 // name: "...",
973 // ...
974 // <common properties>
975 // ...
976 // <single os type specific properties>
977 //
978 // arch: {
979 // <arch specific sections>
980 // }
981 //
982 osPropertySet = bpModule
983 archPropertySet = osPropertySet.AddPropertySet("arch")
984
985 // Arch specific properties need to be added to an arch specific section
986 // within arch.
987 archOsPrefix = ""
988 } else {
989 // Create a structure that looks like:
990 // module_type {
991 // name: "...",
992 // ...
993 // <common properties>
994 // ...
995 // target: {
996 // <arch independent os specific sections, e.g. android>
997 // ...
998 // <arch and os specific sections, e.g. android_x86>
999 // }
1000 //
1001 osType := osInfo.osType
1002 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
1003 archPropertySet = targetPropertySet
1004
1005 // Arch specific properties need to be added to an os and arch specific
1006 // section prefixed with <os>_.
1007 archOsPrefix = osType.Name + "_"
1008 }
1009
1010 // Add the os specific but arch independent properties to the module.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001011 osInfo.Properties.AddToPropertySet(ctx, osPropertySet)
Paul Duffin00e46802020-03-12 20:40:35 +00001012
1013 // Add arch (and possibly os) specific sections for each set of arch (and possibly
1014 // os) specific properties.
1015 //
1016 // The archInfos list will be empty if the os contains variants for the common
1017 // architecture.
1018 for _, archInfo := range osInfo.archInfos {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001019 archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
Paul Duffin00e46802020-03-12 20:40:35 +00001020 }
1021}
1022
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001023func (osInfo *osTypeSpecificInfo) isHostVariant() bool {
1024 osClass := osInfo.osType.Class
1025 return osClass == android.Host || osClass == android.HostCross
1026}
1027
1028var _ isHostVariant = (*osTypeSpecificInfo)(nil)
1029
Paul Duffin4b8b7932020-05-06 12:35:38 +01001030func (osInfo *osTypeSpecificInfo) String() string {
1031 return fmt.Sprintf("OsType{%s}", osInfo.osType)
1032}
1033
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001034type archTypeSpecificInfo struct {
1035 baseInfo
1036
1037 archType android.ArchType
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001038
1039 linkInfos []*linkTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001040}
1041
Paul Duffin4b8b7932020-05-06 12:35:38 +01001042var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
1043
Paul Duffinfc8dd232020-03-17 12:51:37 +00001044// Create a new archTypeSpecificInfo for the specified arch type and its properties
1045// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001046func newArchSpecificInfo(ctx android.SdkMemberContext, archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001047
Paul Duffinfc8dd232020-03-17 12:51:37 +00001048 // Create an arch specific info into which the variant properties can be copied.
1049 archInfo := &archTypeSpecificInfo{archType: archType}
1050
1051 // Create the properties into which the arch type specific properties will be
1052 // added.
1053 archInfo.Properties = variantPropertiesFactory()
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001054
1055 if len(archVariants) == 1 {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001056 archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001057 } else {
1058 // There is more than one variant for this arch type which must be differentiated
1059 // by link type.
1060 for _, linkVariant := range archVariants {
1061 linkType := getLinkType(linkVariant)
1062 if linkType == "" {
1063 panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(archVariants)))
1064 } else {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001065 linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001066
1067 archInfo.linkInfos = append(archInfo.linkInfos, linkInfo)
1068 }
1069 }
1070 }
Paul Duffinfc8dd232020-03-17 12:51:37 +00001071
1072 return archInfo
1073}
1074
Paul Duffinf34f6d82020-04-30 15:48:31 +01001075func (archInfo *archTypeSpecificInfo) optimizableProperties() interface{} {
1076 return archInfo.Properties
1077}
1078
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001079// Get the link type of the variant
1080//
1081// If the variant is not differentiated by link type then it returns "",
1082// otherwise it returns one of "static" or "shared".
1083func getLinkType(variant android.Module) string {
1084 linkType := ""
1085 if linkable, ok := variant.(cc.LinkableInterface); ok {
1086 if linkable.Shared() && linkable.Static() {
1087 panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String()))
1088 } else if linkable.Shared() {
1089 linkType = "shared"
1090 } else if linkable.Static() {
1091 linkType = "static"
1092 } else {
1093 panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String()))
1094 }
1095 }
1096 return linkType
1097}
1098
1099// Optimize the properties by extracting common properties from link type specific
1100// properties into arch type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001101func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001102 if len(archInfo.linkInfos) == 0 {
1103 return
1104 }
1105
Paul Duffin4b8b7932020-05-06 12:35:38 +01001106 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.linkInfos)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001107}
1108
Paul Duffinfc8dd232020-03-17 12:51:37 +00001109// Add the properties for an arch type to a property set.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001110func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001111 archTypeName := archInfo.archType.Name
1112 archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName)
Paul Duffin3a4eb502020-03-19 16:11:18 +00001113 archInfo.Properties.AddToPropertySet(ctx, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001114
1115 for _, linkInfo := range archInfo.linkInfos {
1116 linkPropertySet := archTypePropertySet.AddPropertySet(linkInfo.linkType)
Paul Duffin3a4eb502020-03-19 16:11:18 +00001117 linkInfo.Properties.AddToPropertySet(ctx, linkPropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001118 }
1119}
1120
Paul Duffin4b8b7932020-05-06 12:35:38 +01001121func (archInfo *archTypeSpecificInfo) String() string {
1122 return fmt.Sprintf("ArchType{%s}", archInfo.archType)
1123}
1124
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001125type linkTypeSpecificInfo struct {
1126 baseInfo
1127
1128 linkType string
1129}
1130
Paul Duffin4b8b7932020-05-06 12:35:38 +01001131var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
1132
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001133// Create a new linkTypeSpecificInfo for the specified link type and its properties
1134// structures populated with information from the variant.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001135func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001136 linkInfo := &linkTypeSpecificInfo{
1137 baseInfo: baseInfo{
1138 // Create the properties into which the link type specific properties will be
1139 // added.
1140 Properties: variantPropertiesFactory(),
1141 },
1142 linkType: linkType,
1143 }
Paul Duffin3a4eb502020-03-19 16:11:18 +00001144 linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001145 return linkInfo
Paul Duffinfc8dd232020-03-17 12:51:37 +00001146}
1147
Paul Duffin4b8b7932020-05-06 12:35:38 +01001148func (l *linkTypeSpecificInfo) String() string {
1149 return fmt.Sprintf("LinkType{%s}", l.linkType)
1150}
1151
Paul Duffin3a4eb502020-03-19 16:11:18 +00001152type memberContext struct {
1153 sdkMemberContext android.ModuleContext
1154 builder *snapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +00001155 memberType android.SdkMemberType
1156 name string
Paul Duffin3a4eb502020-03-19 16:11:18 +00001157}
1158
1159func (m *memberContext) SdkModuleContext() android.ModuleContext {
1160 return m.sdkMemberContext
1161}
1162
1163func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
1164 return m.builder
1165}
1166
Paul Duffina551a1c2020-03-17 21:04:24 +00001167func (m *memberContext) MemberType() android.SdkMemberType {
1168 return m.memberType
1169}
1170
1171func (m *memberContext) Name() string {
1172 return m.name
1173}
1174
Paul Duffin3a4eb502020-03-19 16:11:18 +00001175func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule android.BpModule) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001176
1177 memberType := member.memberType
1178
Paul Duffina04c1072020-03-02 10:16:35 +00001179 // Group the variants by os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001180 variantsByOsType := make(map[android.OsType][]android.Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001181 variants := member.Variants()
1182 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +00001183 osType := variant.Target().Os
1184 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001185 }
1186
Paul Duffina04c1072020-03-02 10:16:35 +00001187 osCount := len(variantsByOsType)
Paul Duffinb44b33a2020-03-17 10:58:23 +00001188 variantPropertiesFactory := func() android.SdkMemberProperties {
Paul Duffina04c1072020-03-02 10:16:35 +00001189 properties := memberType.CreateVariantPropertiesStruct()
1190 base := properties.Base()
1191 base.Os_count = osCount
Paul Duffina04c1072020-03-02 10:16:35 +00001192 return properties
1193 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001194
Paul Duffina04c1072020-03-02 10:16:35 +00001195 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +00001196
Paul Duffina04c1072020-03-02 10:16:35 +00001197 // The set of properties that are common across all architectures and os types.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001198 commonProperties := variantPropertiesFactory()
1199 commonProperties.Base().Os = android.CommonOS
Paul Duffina04c1072020-03-02 10:16:35 +00001200
Paul Duffinc097e362020-03-10 22:50:03 +00001201 // Create common value extractor that can be used to optimize the properties.
1202 commonValueExtractor := newCommonValueExtractor(commonProperties)
1203
Paul Duffina04c1072020-03-02 10:16:35 +00001204 // The list of property structures which are os type specific but common across
1205 // architectures within that os type.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001206 var osSpecificPropertiesContainers []*osTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +00001207
1208 for osType, osTypeVariants := range variantsByOsType {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001209 osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
Paul Duffina04c1072020-03-02 10:16:35 +00001210 osTypeToInfo[osType] = osInfo
Paul Duffinb44b33a2020-03-17 10:58:23 +00001211 // Add the os specific properties to a list of os type specific yet architecture
1212 // independent properties structs.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001213 osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
Paul Duffina04c1072020-03-02 10:16:35 +00001214
Paul Duffin00e46802020-03-12 20:40:35 +00001215 // Optimize the properties across all the variants for a specific os type.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001216 osInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin14eb4672020-03-02 11:33:02 +00001217 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001218
Paul Duffina04c1072020-03-02 10:16:35 +00001219 // Extract properties which are common across all architectures and os types.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001220 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001221
Paul Duffina04c1072020-03-02 10:16:35 +00001222 // Add the common properties to the module.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001223 commonProperties.AddToPropertySet(ctx, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001224
Paul Duffina04c1072020-03-02 10:16:35 +00001225 // Create a target property set into which target specific properties can be
1226 // added.
1227 targetPropertySet := bpModule.AddPropertySet("target")
1228
1229 // Iterate over the os types in a fixed order.
1230 for _, osType := range s.getPossibleOsTypes() {
1231 osInfo := osTypeToInfo[osType]
1232 if osInfo == nil {
1233 continue
1234 }
1235
Paul Duffin3a4eb502020-03-19 16:11:18 +00001236 osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001237 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001238}
1239
Paul Duffina04c1072020-03-02 10:16:35 +00001240// Compute the list of possible os types that this sdk could support.
1241func (s *sdk) getPossibleOsTypes() []android.OsType {
1242 var osTypes []android.OsType
1243 for _, osType := range android.OsTypeList {
1244 if s.DeviceSupported() {
1245 if osType.Class == android.Device && osType != android.Fuchsia {
1246 osTypes = append(osTypes, osType)
1247 }
1248 }
1249 if s.HostSupported() {
1250 if osType.Class == android.Host || osType.Class == android.HostCross {
1251 osTypes = append(osTypes, osType)
1252 }
1253 }
1254 }
1255 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
1256 return osTypes
1257}
1258
Paul Duffinb28369a2020-05-04 15:39:59 +01001259// Given a set of properties (struct value), return the value of the field within that
1260// struct (or one of its embedded structs).
Paul Duffinc097e362020-03-10 22:50:03 +00001261type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
1262
Paul Duffinc459f892020-04-30 18:08:29 +01001263// Checks the metadata to determine whether the property should be ignored for the
1264// purposes of common value extraction or not.
1265type extractorMetadataPredicate func(metadata propertiesContainer) bool
1266
1267// Indicates whether optimizable properties are provided by a host variant or
1268// not.
1269type isHostVariant interface {
1270 isHostVariant() bool
1271}
1272
Paul Duffinb28369a2020-05-04 15:39:59 +01001273// A property that can be optimized by the commonValueExtractor.
1274type extractorProperty struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +01001275 // The name of the field for this property.
1276 name string
1277
Paul Duffinc459f892020-04-30 18:08:29 +01001278 // Filter that can use metadata associated with the properties being optimized
1279 // to determine whether the field should be ignored during common value
1280 // optimization.
1281 filter extractorMetadataPredicate
1282
Paul Duffinb28369a2020-05-04 15:39:59 +01001283 // Retrieves the value on which common value optimization will be performed.
1284 getter fieldAccessorFunc
1285
1286 // The empty value for the field.
1287 emptyValue reflect.Value
Paul Duffin864e1b42020-05-06 10:23:19 +01001288
1289 // True if the property can support arch variants false otherwise.
1290 archVariant bool
Paul Duffinb28369a2020-05-04 15:39:59 +01001291}
1292
Paul Duffin4b8b7932020-05-06 12:35:38 +01001293func (p extractorProperty) String() string {
1294 return p.name
1295}
1296
Paul Duffinc097e362020-03-10 22:50:03 +00001297// Supports extracting common values from a number of instances of a properties
1298// structure into a separate common set of properties.
1299type commonValueExtractor struct {
Paul Duffinb28369a2020-05-04 15:39:59 +01001300 // The properties that the extractor can optimize.
1301 properties []extractorProperty
Paul Duffinc097e362020-03-10 22:50:03 +00001302}
1303
1304// Create a new common value extractor for the structure type for the supplied
1305// properties struct.
1306//
1307// The returned extractor can be used on any properties structure of the same type
1308// as the supplied set of properties.
1309func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor {
1310 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
1311 extractor := &commonValueExtractor{}
Paul Duffinb07fa512020-03-10 22:17:04 +00001312 extractor.gatherFields(structType, nil)
Paul Duffinc097e362020-03-10 22:50:03 +00001313 return extractor
1314}
1315
1316// Gather the fields from the supplied structure type from which common values will
1317// be extracted.
Paul Duffinb07fa512020-03-10 22:17:04 +00001318//
1319// This is recursive function. If it encounters an embedded field (no field name)
1320// that is a struct then it will recurse into that struct passing in the accessor
1321// for the field. That will then be used in the accessors for the fields in the
1322// embedded struct.
1323func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc) {
Paul Duffinc097e362020-03-10 22:50:03 +00001324 for f := 0; f < structType.NumField(); f++ {
1325 field := structType.Field(f)
1326 if field.PkgPath != "" {
1327 // Ignore unexported fields.
1328 continue
1329 }
1330
Paul Duffinb07fa512020-03-10 22:17:04 +00001331 // Ignore fields whose value should be kept.
1332 if proptools.HasTag(field, "sdk", "keep") {
Paul Duffinc097e362020-03-10 22:50:03 +00001333 continue
1334 }
1335
Paul Duffinc459f892020-04-30 18:08:29 +01001336 var filter extractorMetadataPredicate
1337
1338 // Add a filter
1339 if proptools.HasTag(field, "sdk", "ignored-on-host") {
1340 filter = func(metadata propertiesContainer) bool {
1341 if m, ok := metadata.(isHostVariant); ok {
1342 if m.isHostVariant() {
1343 return false
1344 }
1345 }
1346 return true
1347 }
1348 }
1349
Paul Duffinc097e362020-03-10 22:50:03 +00001350 // Save a copy of the field index for use in the function.
1351 fieldIndex := f
Paul Duffin4b8b7932020-05-06 12:35:38 +01001352
1353 name := field.Name
1354
Paul Duffinc097e362020-03-10 22:50:03 +00001355 fieldGetter := func(value reflect.Value) reflect.Value {
Paul Duffinb07fa512020-03-10 22:17:04 +00001356 if containingStructAccessor != nil {
1357 // This is an embedded structure so first access the field for the embedded
1358 // structure.
1359 value = containingStructAccessor(value)
1360 }
1361
Paul Duffinc097e362020-03-10 22:50:03 +00001362 // Skip through interface and pointer values to find the structure.
1363 value = getStructValue(value)
1364
Paul Duffin4b8b7932020-05-06 12:35:38 +01001365 defer func() {
1366 if r := recover(); r != nil {
1367 panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
1368 }
1369 }()
1370
Paul Duffinc097e362020-03-10 22:50:03 +00001371 // Return the field.
1372 return value.Field(fieldIndex)
1373 }
1374
Paul Duffinb07fa512020-03-10 22:17:04 +00001375 if field.Type.Kind() == reflect.Struct && field.Anonymous {
1376 // Gather fields from the embedded structure.
1377 e.gatherFields(field.Type, fieldGetter)
1378 } else {
Paul Duffinb28369a2020-05-04 15:39:59 +01001379 property := extractorProperty{
Paul Duffin4b8b7932020-05-06 12:35:38 +01001380 name,
Paul Duffinc459f892020-04-30 18:08:29 +01001381 filter,
Paul Duffinb28369a2020-05-04 15:39:59 +01001382 fieldGetter,
1383 reflect.Zero(field.Type),
Paul Duffin864e1b42020-05-06 10:23:19 +01001384 proptools.HasTag(field, "android", "arch_variant"),
Paul Duffinb28369a2020-05-04 15:39:59 +01001385 }
1386 e.properties = append(e.properties, property)
Paul Duffinb07fa512020-03-10 22:17:04 +00001387 }
Paul Duffinc097e362020-03-10 22:50:03 +00001388 }
1389}
1390
1391func getStructValue(value reflect.Value) reflect.Value {
1392foundStruct:
1393 for {
1394 kind := value.Kind()
1395 switch kind {
1396 case reflect.Interface, reflect.Ptr:
1397 value = value.Elem()
1398 case reflect.Struct:
1399 break foundStruct
1400 default:
1401 panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind))
1402 }
1403 }
1404 return value
1405}
1406
Paul Duffinf34f6d82020-04-30 15:48:31 +01001407// A container of properties to be optimized.
1408//
1409// Allows additional information to be associated with the properties, e.g. for
1410// filtering.
1411type propertiesContainer interface {
Paul Duffin4b8b7932020-05-06 12:35:38 +01001412 fmt.Stringer
1413
Paul Duffinf34f6d82020-04-30 15:48:31 +01001414 // Get the properties that need optimizing.
1415 optimizableProperties() interface{}
1416}
1417
1418// A wrapper for dynamic member properties to allow them to be optimized.
1419type dynamicMemberPropertiesContainer struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +01001420 sdkVariant *sdk
Paul Duffinf34f6d82020-04-30 15:48:31 +01001421 dynamicMemberProperties interface{}
1422}
1423
1424func (c dynamicMemberPropertiesContainer) optimizableProperties() interface{} {
1425 return c.dynamicMemberProperties
1426}
1427
Paul Duffin4b8b7932020-05-06 12:35:38 +01001428func (c dynamicMemberPropertiesContainer) String() string {
1429 return c.sdkVariant.String()
1430}
1431
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001432// Extract common properties from a slice of property structures of the same type.
1433//
1434// All the property structures must be of the same type.
1435// commonProperties - must be a pointer to the structure into which common properties will be added.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001436// inputPropertiesSlice - must be a slice of propertiesContainer interfaces.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001437//
1438// Iterates over each exported field (capitalized name) and checks to see whether they
1439// have the same value (using DeepEquals) across all the input properties. If it does not then no
1440// change is made. Otherwise, the common value is stored in the field in the commonProperties
1441// and the field in each of the input properties structure is set to its default value.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001442func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001443 commonPropertiesValue := reflect.ValueOf(commonProperties)
1444 commonStructValue := commonPropertiesValue.Elem()
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001445
Paul Duffinf34f6d82020-04-30 15:48:31 +01001446 sliceValue := reflect.ValueOf(inputPropertiesSlice)
1447
Paul Duffinb28369a2020-05-04 15:39:59 +01001448 for _, property := range e.properties {
1449 fieldGetter := property.getter
Paul Duffinc459f892020-04-30 18:08:29 +01001450 filter := property.filter
1451 if filter == nil {
1452 filter = func(metadata propertiesContainer) bool {
1453 return true
1454 }
1455 }
Paul Duffinb28369a2020-05-04 15:39:59 +01001456
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001457 // Check to see if all the structures have the same value for the field. The commonValue
Paul Duffin864e1b42020-05-06 10:23:19 +01001458 // is nil on entry to the loop and if it is nil on exit then there is no common value or
1459 // all the values have been filtered out, otherwise it points to the common value.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001460 var commonValue *reflect.Value
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001461
Paul Duffin864e1b42020-05-06 10:23:19 +01001462 // Assume that all the values will be the same.
1463 //
1464 // While similar to this is not quite the same as commonValue == nil. If all the values
1465 // have been filtered out then this will be false but commonValue == nil will be true.
1466 valuesDiffer := false
1467
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001468 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01001469 container := sliceValue.Index(i).Interface().(propertiesContainer)
1470 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00001471 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001472
Paul Duffinc459f892020-04-30 18:08:29 +01001473 if !filter(container) {
1474 expectedValue := property.emptyValue.Interface()
1475 actualValue := fieldValue.Interface()
1476 if !reflect.DeepEqual(expectedValue, actualValue) {
1477 return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue)
1478 }
1479 continue
1480 }
1481
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001482 if commonValue == nil {
1483 // Use the first value as the commonProperties value.
1484 commonValue = &fieldValue
1485 } else {
1486 // If the value does not match the current common value then there is
1487 // no value in common so break out.
1488 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
1489 commonValue = nil
Paul Duffin864e1b42020-05-06 10:23:19 +01001490 valuesDiffer = true
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001491 break
1492 }
1493 }
1494 }
1495
Paul Duffin864e1b42020-05-06 10:23:19 +01001496 // If the fields all have common value then store it in the common struct field
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001497 // and set the input struct's field to the empty value.
1498 if commonValue != nil {
Paul Duffinb28369a2020-05-04 15:39:59 +01001499 emptyValue := property.emptyValue
Paul Duffinc097e362020-03-10 22:50:03 +00001500 fieldGetter(commonStructValue).Set(*commonValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001501 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01001502 container := sliceValue.Index(i).Interface().(propertiesContainer)
1503 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00001504 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001505 fieldValue.Set(emptyValue)
1506 }
1507 }
Paul Duffin864e1b42020-05-06 10:23:19 +01001508
1509 if valuesDiffer && !property.archVariant {
1510 // The values differ but the property does not support arch variants so it
1511 // is an error.
1512 var details strings.Builder
1513 for i := 0; i < sliceValue.Len(); i++ {
1514 container := sliceValue.Index(i).Interface().(propertiesContainer)
1515 itemValue := reflect.ValueOf(container.optimizableProperties())
1516 fieldValue := fieldGetter(itemValue)
1517
1518 _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface())
1519 }
1520
1521 return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String())
1522 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001523 }
Paul Duffin4b8b7932020-05-06 12:35:38 +01001524
1525 return nil
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001526}