blob: 141762c6f71167f92121ba045e4671c115c0377e [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
Paul Duffin64fb5262021-05-05 21:36:04 +010032// Environment variables that affect the generated snapshot
33// ========================================================
34//
35// SOONG_SDK_SNAPSHOT_PREFER
36// By default every unversioned module in the generated snapshot has prefer: false. Building it
37// with SOONG_SDK_SNAPSHOT_PREFER=true will force them to use prefer: true.
38//
39
Jiyong Park9b409bc2019-10-11 14:59:13 +090040var pctx = android.NewPackageContext("android/soong/sdk")
41
Paul Duffin375058f2019-11-29 20:17:53 +000042var (
43 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
44 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000045 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000046 CommandDeps: []string{
47 "${config.Zip2ZipCmd}",
48 },
49 },
50 "destdir")
51
52 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
53 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -070054 Command: `${config.SoongZipCmd} -C $basedir -r $out.rsp -o $out`,
Paul Duffin375058f2019-11-29 20:17:53 +000055 CommandDeps: []string{
56 "${config.SoongZipCmd}",
57 },
58 Rspfile: "$out.rsp",
59 RspfileContent: "$in",
60 },
61 "basedir")
62
63 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
64 blueprint.RuleParams{
65 Command: `${config.MergeZipsCmd} $out $in`,
66 CommandDeps: []string{
67 "${config.MergeZipsCmd}",
68 },
69 })
70)
71
Paul Duffinb645ec82019-11-27 17:43:54 +000072type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090073 content strings.Builder
74 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090075}
76
Paul Duffinb645ec82019-11-27 17:43:54 +000077// generatedFile abstracts operations for writing contents into a file and emit a build rule
78// for the file.
79type generatedFile struct {
80 generatedContents
81 path android.OutputPath
82}
83
Jiyong Park232e7852019-11-04 12:23:40 +090084func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +090085 return &generatedFile{
Paul Duffinb645ec82019-11-27 17:43:54 +000086 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park9b409bc2019-10-11 14:59:13 +090087 }
88}
89
Paul Duffinb645ec82019-11-27 17:43:54 +000090func (gc *generatedContents) Indent() {
91 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +090092}
93
Paul Duffinb645ec82019-11-27 17:43:54 +000094func (gc *generatedContents) Dedent() {
95 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +090096}
97
Paul Duffinb645ec82019-11-27 17:43:54 +000098func (gc *generatedContents) Printfln(format string, args ...interface{}) {
Paul Duffin11108272020-05-11 22:59:25 +010099 fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\n", args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900100}
101
102func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800103 rb := android.NewRuleBuilder(pctx, ctx)
Paul Duffin11108272020-05-11 22:59:25 +0100104
105 content := gf.content.String()
106
107 // ninja consumes newline characters in rspfile_content. Prevent it by
108 // escaping the backslash in the newline character. The extra backslash
109 // is removed when the rspfile is written to the actual script file
110 content = strings.ReplaceAll(content, "\n", "\\n")
111
Jiyong Park9b409bc2019-10-11 14:59:13 +0900112 rb.Command().
113 Implicits(implicits).
Martin Stjernholmee9b24e2021-04-20 15:54:21 +0100114 Text("echo -n").Text(proptools.ShellEscape(content)).
Paul Duffin11108272020-05-11 22:59:25 +0100115 // convert \\n to \n
Jiyong Park9b409bc2019-10-11 14:59:13 +0900116 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
117 rb.Command().
118 Text("chmod a+x").Output(gf.path)
Colin Crossf1a035e2020-11-16 17:32:30 -0800119 rb.Build(gf.path.Base(), "Build "+gf.path.Base())
Jiyong Park9b409bc2019-10-11 14:59:13 +0900120}
121
Paul Duffin13879572019-11-28 14:31:38 +0000122// Collect all the members.
123//
Paul Duffincc3132e2021-04-24 01:10:30 +0100124// Updates the sdk module with a list of sdkMemberVariantDeps and details as to which multilibs
125// (32/64/both) are used by this sdk variant.
Paul Duffin6a7e9532020-03-20 17:50:07 +0000126func (s *sdk) collectMembers(ctx android.ModuleContext) {
127 s.multilibUsages = multilibNone
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000128 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
129 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf8539922019-11-19 19:44:10 +0000130 if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok {
Paul Duffineee466e2021-04-27 23:17:56 +0100131 memberType := memberTag.SdkMemberType(child)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900132
Paul Duffin13879572019-11-28 14:31:38 +0000133 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000134 if !memberType.IsInstance(child) {
135 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900136 }
Paul Duffin13879572019-11-28 14:31:38 +0000137
Paul Duffin6a7e9532020-03-20 17:50:07 +0000138 // Keep track of which multilib variants are used by the sdk.
139 s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType)
140
Paul Duffina7208112021-04-23 21:20:20 +0100141 export := memberTag.ExportMember()
Paul Duffincd064672021-04-24 00:47:29 +0100142 s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{s, memberType, child.(android.SdkAware), export})
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000143
Paul Duffin2d3da312021-05-06 12:02:27 +0100144 // Recurse down into the member's dependencies as it may have dependencies that need to be
145 // automatically added to the sdk.
146 return true
Jiyong Park73c54ee2019-10-22 20:31:18 +0900147 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000148
149 return false
Paul Duffin13879572019-11-28 14:31:38 +0000150 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000151}
152
Paul Duffincc3132e2021-04-24 01:10:30 +0100153// groupMemberVariantsByMemberThenType groups the member variant dependencies so that all the
154// variants of each member are grouped together within an sdkMember instance.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000155//
Paul Duffincc3132e2021-04-24 01:10:30 +0100156// The sdkMember instances are then grouped into slices by member type. Within each such slice the
157// sdkMember instances appear in the order they were added as dependencies.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000158//
Paul Duffincc3132e2021-04-24 01:10:30 +0100159// Finally, the member type slices are concatenated together to form a single slice. The order in
160// which they are concatenated is the order in which the member types were registered in the
161// android.SdkMemberTypesRegistry.
162func (s *sdk) groupMemberVariantsByMemberThenType(ctx android.ModuleContext, memberVariantDeps []sdkMemberVariantDep) []*sdkMember {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000163 byType := make(map[android.SdkMemberType][]*sdkMember)
164 byName := make(map[string]*sdkMember)
165
Paul Duffin21827262021-04-24 12:16:36 +0100166 for _, memberVariantDep := range memberVariantDeps {
167 memberType := memberVariantDep.memberType
168 variant := memberVariantDep.variant
Paul Duffin1356d8c2020-02-25 19:26:33 +0000169
170 name := ctx.OtherModuleName(variant)
171 member := byName[name]
172 if member == nil {
173 member = &sdkMember{memberType: memberType, name: name}
174 byName[name] = member
175 byType[memberType] = append(byType[memberType], member)
176 }
177
Paul Duffin1356d8c2020-02-25 19:26:33 +0000178 // Only append new variants to the list. This is needed because a member can be both
179 // exported by the sdk and also be a transitive sdk member.
180 member.variants = appendUniqueVariants(member.variants, variant)
181 }
182
Paul Duffin13879572019-11-28 14:31:38 +0000183 var members []*sdkMember
Paul Duffin72910952020-01-20 18:16:30 +0000184 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13879572019-11-28 14:31:38 +0000185 membersOfType := byType[memberListProperty.memberType]
186 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900187 }
188
Paul Duffin6a7e9532020-03-20 17:50:07 +0000189 return members
Jiyong Park73c54ee2019-10-22 20:31:18 +0900190}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900191
Paul Duffin72910952020-01-20 18:16:30 +0000192func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
193 for _, v := range variants {
194 if v == newVariant {
195 return variants
196 }
197 }
198 return append(variants, newVariant)
199}
200
Jiyong Park73c54ee2019-10-22 20:31:18 +0900201// SDK directory structure
202// <sdk_root>/
203// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
204// <api_ver>/ : below this directory are all auto-generated
205// Android.bp : definition of 'sdk_snapshot' module is here
206// aidl/
207// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
208// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900209// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900210// include/
211// bionic/libc/include/stdlib.h : an exported header file
212// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900213// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900214// <arch>/include/ : arch-specific exported headers
215// <arch>/include_gen/ : arch-specific generated headers
216// <arch>/lib/
217// libFoo.so : a stub library
218
Jiyong Park232e7852019-11-04 12:23:40 +0900219// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900220// This isn't visible to users, so could be changed in future.
221func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
222 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
223}
224
Jiyong Park232e7852019-11-04 12:23:40 +0900225// buildSnapshot is the main function in this source file. It creates rules to copy
226// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000227func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath {
228
Paul Duffin13f02712020-03-06 12:30:43 +0000229 allMembersByName := make(map[string]struct{})
230 exportedMembersByName := make(map[string]struct{})
Paul Duffin21827262021-04-24 12:16:36 +0100231 var memberVariantDeps []sdkMemberVariantDep
Paul Duffin1356d8c2020-02-25 19:26:33 +0000232 for _, sdkVariant := range sdkVariants {
Paul Duffin21827262021-04-24 12:16:36 +0100233 memberVariantDeps = append(memberVariantDeps, sdkVariant.memberVariantDeps...)
Paul Duffin865171e2020-03-02 18:38:15 +0000234
Paul Duffin13f02712020-03-06 12:30:43 +0000235 // Record the names of all the members, both explicitly specified and implicitly
236 // included.
Paul Duffin21827262021-04-24 12:16:36 +0100237 for _, memberVariantDep := range sdkVariant.memberVariantDeps {
Paul Duffina7208112021-04-23 21:20:20 +0100238 name := memberVariantDep.variant.Name()
239 allMembersByName[name] = struct{}{}
Paul Duffin13f02712020-03-06 12:30:43 +0000240
Paul Duffina7208112021-04-23 21:20:20 +0100241 if memberVariantDep.export {
242 exportedMembersByName[name] = struct{}{}
243 }
Paul Duffin865171e2020-03-02 18:38:15 +0000244 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000245 }
246
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000247 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900248
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000249 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000250
251 bpFile := &bpFile{
252 modules: make(map[string]*bpModule),
253 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000254
255 builder := &snapshotBuilder{
Paul Duffin13f02712020-03-06 12:30:43 +0000256 ctx: ctx,
257 sdk: s,
258 version: "current",
259 snapshotDir: snapshotDir.OutputPath,
260 copies: make(map[string]string),
261 filesToZip: []android.Path{bp.path},
262 bpFile: bpFile,
263 prebuiltModules: make(map[string]*bpModule),
264 allMembersByName: allMembersByName,
265 exportedMembersByName: exportedMembersByName,
Jiyong Park73c54ee2019-10-22 20:31:18 +0900266 }
Paul Duffinac37c502019-11-26 18:02:20 +0000267 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900268
Paul Duffincc3132e2021-04-24 01:10:30 +0100269 // Create the prebuilt modules for each of the member modules.
270 members := s.groupMemberVariantsByMemberThenType(ctx, memberVariantDeps)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000271 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000272 memberType := member.memberType
Paul Duffin3a4eb502020-03-19 16:11:18 +0000273
Paul Duffina551a1c2020-03-17 21:04:24 +0000274 memberCtx := &memberContext{ctx, builder, memberType, member.name}
Paul Duffin3a4eb502020-03-19 16:11:18 +0000275
276 prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100277 s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
Jiyong Park73c54ee2019-10-22 20:31:18 +0900278 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900279
Paul Duffine6c0d842020-01-15 14:08:51 +0000280 // Create a transformer that will transform an unversioned module into a versioned module.
281 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
282
Paul Duffin72910952020-01-20 18:16:30 +0000283 // Create a transformer that will transform an unversioned module by replacing any references
284 // to internal members with a unique module name and setting prefer: false.
Paul Duffin64fb5262021-05-05 21:36:04 +0100285 unversionedTransformer := unversionedTransformation{
286 builder: builder,
Paul Duffin64fb5262021-05-05 21:36:04 +0100287 }
Paul Duffin72910952020-01-20 18:16:30 +0000288
Paul Duffinb645ec82019-11-27 17:43:54 +0000289 for _, unversioned := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000290 // Prune any empty property sets.
291 unversioned = unversioned.transform(pruneEmptySetTransformer{})
292
Paul Duffinb645ec82019-11-27 17:43:54 +0000293 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000294 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000295
296 // Transform the unversioned module into a versioned one.
297 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000298 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000299
Paul Duffin72910952020-01-20 18:16:30 +0000300 // Transform the unversioned module to make it suitable for use in the snapshot.
301 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000302 bpFile.AddModule(unversioned)
303 }
304
Paul Duffin26197a62021-04-24 00:34:10 +0100305 // Add the sdk/module_exports_snapshot module to the bp file.
Paul Duffin21827262021-04-24 12:16:36 +0100306 s.addSnapshotModule(ctx, builder, sdkVariants, memberVariantDeps)
Paul Duffin26197a62021-04-24 00:34:10 +0100307
308 // generate Android.bp
309 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
310 generateBpContents(&bp.generatedContents, bpFile)
311
312 contents := bp.content.String()
313 syntaxCheckSnapshotBpFile(ctx, contents)
314
315 bp.build(pctx, ctx, nil)
316
317 filesToZip := builder.filesToZip
318
319 // zip them all
320 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
321 outputDesc := "Building snapshot for " + ctx.ModuleName()
322
323 // If there are no zips to merge then generate the output zip directly.
324 // Otherwise, generate an intermediate zip file into which other zips can be
325 // merged.
326 var zipFile android.OutputPath
327 var desc string
328 if len(builder.zipsToMerge) == 0 {
329 zipFile = outputZipFile
330 desc = outputDesc
331 } else {
332 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
333 desc = "Building intermediate snapshot for " + ctx.ModuleName()
334 }
335
336 ctx.Build(pctx, android.BuildParams{
337 Description: desc,
338 Rule: zipFiles,
339 Inputs: filesToZip,
340 Output: zipFile,
341 Args: map[string]string{
342 "basedir": builder.snapshotDir.String(),
343 },
344 })
345
346 if len(builder.zipsToMerge) != 0 {
347 ctx.Build(pctx, android.BuildParams{
348 Description: outputDesc,
349 Rule: mergeZips,
350 Input: zipFile,
351 Inputs: builder.zipsToMerge,
352 Output: outputZipFile,
353 })
354 }
355
356 return outputZipFile
357}
358
359// addSnapshotModule adds the sdk_snapshot/module_exports_snapshot module to the builder.
Paul Duffin21827262021-04-24 12:16:36 +0100360func (s *sdk) addSnapshotModule(ctx android.ModuleContext, builder *snapshotBuilder, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) {
Paul Duffin26197a62021-04-24 00:34:10 +0100361 bpFile := builder.bpFile
362
Paul Duffinb645ec82019-11-27 17:43:54 +0000363 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000364 var snapshotModuleType string
365 if s.properties.Module_exports {
366 snapshotModuleType = "module_exports_snapshot"
367 } else {
368 snapshotModuleType = "sdk_snapshot"
369 }
370 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000371 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000372
373 // Make sure that the snapshot has the same visibility as the sdk.
Paul Duffin157f40f2020-09-29 16:01:08 +0100374 visibility := android.EffectiveVisibilityRules(ctx, s).Strings()
Paul Duffin593b3c92019-12-05 14:31:48 +0000375 if len(visibility) != 0 {
376 snapshotModule.AddProperty("visibility", visibility)
377 }
378
Paul Duffin865171e2020-03-02 18:38:15 +0000379 addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000380
Paul Duffincd064672021-04-24 00:47:29 +0100381 combinedPropertiesList := s.collateSnapshotModuleInfo(ctx, sdkVariants, memberVariantDeps)
Paul Duffin2d1bb892021-04-24 11:32:59 +0100382 commonCombinedProperties := s.optimizeSnapshotModuleProperties(ctx, combinedPropertiesList)
Paul Duffin865171e2020-03-02 18:38:15 +0000383
Paul Duffin2d1bb892021-04-24 11:32:59 +0100384 s.addSnapshotPropertiesToPropertySet(builder, snapshotModule, commonCombinedProperties)
Martin Stjernholm4cfa2c62020-07-10 19:55:36 +0100385
Paul Duffin6a7e9532020-03-20 17:50:07 +0000386 targetPropertySet := snapshotModule.AddPropertySet("target")
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100387
Paul Duffin2d1bb892021-04-24 11:32:59 +0100388 // Create a mapping from osType to combined properties.
389 osTypeToCombinedProperties := map[android.OsType]*combinedSnapshotModuleProperties{}
390 for _, combined := range combinedPropertiesList {
391 osTypeToCombinedProperties[combined.sdkVariant.Os()] = combined
392 }
393
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100394 // Iterate over the os types in a fixed order.
Paul Duffin865171e2020-03-02 18:38:15 +0000395 for _, osType := range s.getPossibleOsTypes() {
Paul Duffin2d1bb892021-04-24 11:32:59 +0100396 if combined, ok := osTypeToCombinedProperties[osType]; ok {
Paul Duffincc3132e2021-04-24 01:10:30 +0100397 osPropertySet := targetPropertySet.AddPropertySet(osType.Name)
Paul Duffin6a7e9532020-03-20 17:50:07 +0000398
Paul Duffin2d1bb892021-04-24 11:32:59 +0100399 s.addSnapshotPropertiesToPropertySet(builder, osPropertySet, combined)
Paul Duffin13879572019-11-28 14:31:38 +0000400 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000401 }
Paul Duffin865171e2020-03-02 18:38:15 +0000402
Jiyong Park8fe14e62020-10-19 22:47:34 +0900403 // If host is supported and any member is host OS dependent then disable host
404 // by default, so that we can enable each host OS variant explicitly. This
405 // avoids problems with implicitly enabled OS variants when the snapshot is
406 // used, which might be different from this run (e.g. different build OS).
407 if s.HostSupported() {
408 var supportedHostTargets []string
Paul Duffin21827262021-04-24 12:16:36 +0100409 for _, memberVariantDep := range memberVariantDeps {
410 if memberVariantDep.memberType.IsHostOsDependent() && memberVariantDep.variant.Target().Os.Class == android.Host {
411 targetString := memberVariantDep.variant.Target().Os.String() + "_" + memberVariantDep.variant.Target().Arch.ArchType.String()
Jiyong Park8fe14e62020-10-19 22:47:34 +0900412 if !android.InList(targetString, supportedHostTargets) {
413 supportedHostTargets = append(supportedHostTargets, targetString)
414 }
415 }
416 }
417 if len(supportedHostTargets) > 0 {
418 hostPropertySet := targetPropertySet.AddPropertySet("host")
419 hostPropertySet.AddProperty("enabled", false)
420 }
421 // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
422 for _, hostTarget := range supportedHostTargets {
423 propertySet := targetPropertySet.AddPropertySet(hostTarget)
424 propertySet.AddProperty("enabled", true)
425 }
426 }
427
Paul Duffin865171e2020-03-02 18:38:15 +0000428 // Prune any empty property sets.
429 snapshotModule.transform(pruneEmptySetTransformer{})
430
Paul Duffinb645ec82019-11-27 17:43:54 +0000431 bpFile.AddModule(snapshotModule)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900432}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000433
Paul Duffinf88d8e02020-05-07 20:21:34 +0100434// Check the syntax of the generated Android.bp file contents and if they are
435// invalid then log an error with the contents (tagged with line numbers) and the
436// errors that were found so that it is easy to see where the problem lies.
437func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) {
438 errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents)
439 if len(errs) != 0 {
440 message := &strings.Builder{}
441 _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot:
442
443Generated Android.bp contents
444========================================================================
445`)
446 for i, line := range strings.Split(contents, "\n") {
447 _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line)
448 }
449
450 _, _ = fmt.Fprint(message, `
451========================================================================
452
453Errors found:
454`)
455
456 for _, err := range errs {
457 _, _ = fmt.Fprintf(message, "%s\n", err.Error())
458 }
459
460 ctx.ModuleErrorf("%s", message.String())
461 }
462}
463
Paul Duffin4b8b7932020-05-06 12:35:38 +0100464func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
465 err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
466 if err != nil {
467 ctx.ModuleErrorf("error extracting common properties: %s", err)
468 }
469}
470
Paul Duffinfbe470e2021-04-24 12:37:13 +0100471// snapshotModuleStaticProperties contains snapshot static (i.e. not dynamically generated) properties.
472type snapshotModuleStaticProperties struct {
473 Compile_multilib string `android:"arch_variant"`
474}
475
Paul Duffin2d1bb892021-04-24 11:32:59 +0100476// combinedSnapshotModuleProperties are the properties that are associated with the snapshot module.
477type combinedSnapshotModuleProperties struct {
478 // The sdk variant from which this information was collected.
479 sdkVariant *sdk
480
481 // Static snapshot module properties.
482 staticProperties *snapshotModuleStaticProperties
483
484 // The dynamically generated member list properties.
485 dynamicProperties interface{}
486}
487
488// collateSnapshotModuleInfo collates all the snapshot module info from supplied sdk variants.
Paul Duffincd064672021-04-24 00:47:29 +0100489func (s *sdk) collateSnapshotModuleInfo(ctx android.BaseModuleContext, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) []*combinedSnapshotModuleProperties {
490 sdkVariantToCombinedProperties := map[*sdk]*combinedSnapshotModuleProperties{}
Paul Duffin2d1bb892021-04-24 11:32:59 +0100491 var list []*combinedSnapshotModuleProperties
492 for _, sdkVariant := range sdkVariants {
493 staticProperties := &snapshotModuleStaticProperties{
494 Compile_multilib: sdkVariant.multilibUsages.String(),
495 }
Paul Duffincd064672021-04-24 00:47:29 +0100496 dynamicProperties := s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffin2d1bb892021-04-24 11:32:59 +0100497
Paul Duffincd064672021-04-24 00:47:29 +0100498 combinedProperties := &combinedSnapshotModuleProperties{
Paul Duffin2d1bb892021-04-24 11:32:59 +0100499 sdkVariant: sdkVariant,
500 staticProperties: staticProperties,
501 dynamicProperties: dynamicProperties,
Paul Duffincd064672021-04-24 00:47:29 +0100502 }
503 sdkVariantToCombinedProperties[sdkVariant] = combinedProperties
504
505 list = append(list, combinedProperties)
Paul Duffin2d1bb892021-04-24 11:32:59 +0100506 }
Paul Duffincd064672021-04-24 00:47:29 +0100507
508 for _, memberVariantDep := range memberVariantDeps {
509 // If the member dependency is internal then do not add the dependency to the snapshot member
510 // list properties.
511 if !memberVariantDep.export {
512 continue
513 }
514
515 combined := sdkVariantToCombinedProperties[memberVariantDep.sdkVariant]
Paul Duffin13082052021-05-11 00:31:38 +0100516 memberListProperty := s.memberListProperty(memberVariantDep.memberType)
Paul Duffincd064672021-04-24 00:47:29 +0100517 memberName := ctx.OtherModuleName(memberVariantDep.variant)
518
Paul Duffin13082052021-05-11 00:31:38 +0100519 if memberListProperty.getter == nil {
520 continue
521 }
522
Paul Duffincd064672021-04-24 00:47:29 +0100523 // Append the member to the appropriate list, if it is not already present in the list.
Paul Duffin13082052021-05-11 00:31:38 +0100524 memberList := memberListProperty.getter(combined.dynamicProperties)
Paul Duffincd064672021-04-24 00:47:29 +0100525 if !android.InList(memberName, memberList) {
526 memberList = append(memberList, memberName)
527 }
Paul Duffin13082052021-05-11 00:31:38 +0100528 memberListProperty.setter(combined.dynamicProperties, memberList)
Paul Duffincd064672021-04-24 00:47:29 +0100529 }
530
Paul Duffin2d1bb892021-04-24 11:32:59 +0100531 return list
532}
533
534func (s *sdk) optimizeSnapshotModuleProperties(ctx android.ModuleContext, list []*combinedSnapshotModuleProperties) *combinedSnapshotModuleProperties {
535
536 // Extract the dynamic properties and add them to a list of propertiesContainer.
537 propertyContainers := []propertiesContainer{}
538 for _, i := range list {
539 propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{
540 sdkVariant: i.sdkVariant,
541 properties: i.dynamicProperties,
542 })
543 }
544
545 // Extract the common members, removing them from the original properties.
546 commonDynamicProperties := s.dynamicSdkMemberTypes.createMemberListProperties()
547 extractor := newCommonValueExtractor(commonDynamicProperties)
548 extractCommonProperties(ctx, extractor, commonDynamicProperties, propertyContainers)
549
550 // Extract the static properties and add them to a list of propertiesContainer.
551 propertyContainers = []propertiesContainer{}
552 for _, i := range list {
553 propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{
554 sdkVariant: i.sdkVariant,
555 properties: i.staticProperties,
556 })
557 }
558
559 commonStaticProperties := &snapshotModuleStaticProperties{}
560 extractor = newCommonValueExtractor(commonStaticProperties)
561 extractCommonProperties(ctx, extractor, &commonStaticProperties, propertyContainers)
562
563 return &combinedSnapshotModuleProperties{
564 sdkVariant: nil,
565 staticProperties: commonStaticProperties,
566 dynamicProperties: commonDynamicProperties,
567 }
568}
569
570func (s *sdk) addSnapshotPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, combined *combinedSnapshotModuleProperties) {
571 staticProperties := combined.staticProperties
Paul Duffinfbe470e2021-04-24 12:37:13 +0100572 multilib := staticProperties.Compile_multilib
573 if multilib != "" && multilib != "both" {
574 // Compile_multilib defaults to both so only needs to be set when it's specified and not both.
575 propertySet.AddProperty("compile_multilib", multilib)
576 }
577
Paul Duffin2d1bb892021-04-24 11:32:59 +0100578 dynamicMemberTypeListProperties := combined.dynamicProperties
Paul Duffin865171e2020-03-02 18:38:15 +0000579 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13082052021-05-11 00:31:38 +0100580 if memberListProperty.getter == nil {
581 continue
582 }
Paul Duffin865171e2020-03-02 18:38:15 +0000583 names := memberListProperty.getter(dynamicMemberTypeListProperties)
584 if len(names) > 0 {
Paul Duffin13f02712020-03-06 12:30:43 +0000585 propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names, false))
Paul Duffin865171e2020-03-02 18:38:15 +0000586 }
587 }
588}
589
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000590type propertyTag struct {
591 name string
592}
593
Paul Duffin0cb37b92020-03-04 14:52:46 +0000594// A BpPropertyTag to add to a property that contains references to other sdk members.
595//
596// This will cause the references to be rewritten to a versioned reference in the version
597// specific instance of a snapshot module.
Paul Duffin13f02712020-03-06 12:30:43 +0000598var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
Paul Duffin13f02712020-03-06 12:30:43 +0000599var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000600
Paul Duffin0cb37b92020-03-04 14:52:46 +0000601// A BpPropertyTag that indicates the property should only be present in the versioned
602// module.
603//
604// This will cause the property to be removed from the unversioned instance of a
605// snapshot module.
606var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"}
607
Paul Duffine6c0d842020-01-15 14:08:51 +0000608type unversionedToVersionedTransformation struct {
609 identityTransformation
610 builder *snapshotBuilder
611}
612
Paul Duffine6c0d842020-01-15 14:08:51 +0000613func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
614 // Use a versioned name for the module but remember the original name for the
615 // snapshot.
616 name := module.getValue("name").(string)
Paul Duffin13f02712020-03-06 12:30:43 +0000617 module.setProperty("name", t.builder.versionedSdkMemberName(name, true))
Paul Duffine6c0d842020-01-15 14:08:51 +0000618 module.insertAfter("name", "sdk_member_name", name)
Paul Duffin83ad9562021-05-10 23:49:04 +0100619 // Remove the prefer property if present as versioned modules never need marking with prefer.
620 module.removeProperty("prefer")
Paul Duffine6c0d842020-01-15 14:08:51 +0000621 return module
622}
623
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000624func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000625 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
626 required := tag == requiredSdkMemberReferencePropertyTag
627 return t.builder.versionedSdkMemberNames(value.([]string), required), tag
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000628 } else {
629 return value, tag
630 }
631}
632
Paul Duffin72910952020-01-20 18:16:30 +0000633type unversionedTransformation struct {
634 identityTransformation
635 builder *snapshotBuilder
636}
637
638func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
639 // If the module is an internal member then use a unique name for it.
640 name := module.getValue("name").(string)
Paul Duffin13f02712020-03-06 12:30:43 +0000641 module.setProperty("name", t.builder.unversionedSdkMemberName(name, true))
Paul Duffin72910952020-01-20 18:16:30 +0000642 return module
643}
644
645func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000646 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
647 required := tag == requiredSdkMemberReferencePropertyTag
648 return t.builder.unversionedSdkMemberNames(value.([]string), required), tag
Paul Duffin0cb37b92020-03-04 14:52:46 +0000649 } else if tag == sdkVersionedOnlyPropertyTag {
650 // The property is not allowed in the unversioned module so remove it.
651 return nil, nil
Paul Duffin72910952020-01-20 18:16:30 +0000652 } else {
653 return value, tag
654 }
655}
656
Paul Duffina78f3a72020-02-21 16:29:35 +0000657type pruneEmptySetTransformer struct {
658 identityTransformation
659}
660
661var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
662
663func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
664 if len(propertySet.properties) == 0 {
665 return nil, nil
666 } else {
667 return propertySet, tag
668 }
669}
670
Paul Duffinb645ec82019-11-27 17:43:54 +0000671func generateBpContents(contents *generatedContents, bpFile *bpFile) {
Paul Duffind0759072021-02-17 11:23:00 +0000672 generateFilteredBpContents(contents, bpFile, func(*bpModule) bool {
673 return true
674 })
675}
676
677func generateFilteredBpContents(contents *generatedContents, bpFile *bpFile, moduleFilter func(module *bpModule) bool) {
Paul Duffinb645ec82019-11-27 17:43:54 +0000678 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
679 for _, bpModule := range bpFile.order {
Paul Duffind0759072021-02-17 11:23:00 +0000680 if moduleFilter(bpModule) {
681 contents.Printfln("")
682 contents.Printfln("%s {", bpModule.moduleType)
683 outputPropertySet(contents, bpModule.bpPropertySet)
684 contents.Printfln("}")
685 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000686 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000687}
688
689func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
690 contents.Indent()
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000691
692 // Output the properties first, followed by the nested sets. This ensures a
693 // consistent output irrespective of whether property sets are created before
694 // or after the properties. This simplifies the creation of the module.
Paul Duffinb645ec82019-11-27 17:43:54 +0000695 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000696 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000697
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000698 switch v := value.(type) {
699 case []string:
700 length := len(v)
Paul Duffinb645ec82019-11-27 17:43:54 +0000701 if length > 1 {
702 contents.Printfln("%s: [", name)
703 contents.Indent()
704 for i := 0; i < length; i = i + 1 {
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000705 contents.Printfln("%q,", v[i])
Paul Duffinb645ec82019-11-27 17:43:54 +0000706 }
707 contents.Dedent()
708 contents.Printfln("],")
709 } else if length == 0 {
710 contents.Printfln("%s: [],", name)
711 } else {
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000712 contents.Printfln("%s: [%q],", name, v[0])
Paul Duffinb645ec82019-11-27 17:43:54 +0000713 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000714
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000715 case bool:
716 contents.Printfln("%s: %t,", name, v)
717
718 case *bpPropertySet:
719 // Do not write property sets in the properties phase.
Paul Duffinb645ec82019-11-27 17:43:54 +0000720
721 default:
722 contents.Printfln("%s: %q,", name, value)
723 }
724 }
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000725
726 for _, name := range set.order {
727 value := set.getValue(name)
728
729 // Only write property sets in the sets phase.
730 switch v := value.(type) {
731 case *bpPropertySet:
732 contents.Printfln("%s: {", name)
733 outputPropertySet(contents, v)
734 contents.Printfln("},")
735 }
736 }
737
Paul Duffinb645ec82019-11-27 17:43:54 +0000738 contents.Dedent()
739}
740
Paul Duffinac37c502019-11-26 18:02:20 +0000741func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000742 contents := &generatedContents{}
743 generateBpContents(contents, s.builderForTests.bpFile)
744 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000745}
746
Paul Duffind0759072021-02-17 11:23:00 +0000747func (s *sdk) GetUnversionedAndroidBpContentsForTests() string {
748 contents := &generatedContents{}
749 generateFilteredBpContents(contents, s.builderForTests.bpFile, func(module *bpModule) bool {
750 return !strings.Contains(module.properties["name"].(string), "@")
751 })
752 return contents.content.String()
753}
754
755func (s *sdk) GetVersionedAndroidBpContentsForTests() string {
756 contents := &generatedContents{}
757 generateFilteredBpContents(contents, s.builderForTests.bpFile, func(module *bpModule) bool {
758 return strings.Contains(module.properties["name"].(string), "@")
759 })
760 return contents.content.String()
761}
762
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000763type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000764 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000765 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000766 version string
767 snapshotDir android.OutputPath
768 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000769
770 // Map from destination to source of each copy - used to eliminate duplicates and
771 // detect conflicts.
772 copies map[string]string
773
Paul Duffinb645ec82019-11-27 17:43:54 +0000774 filesToZip android.Paths
775 zipsToMerge android.Paths
776
777 prebuiltModules map[string]*bpModule
778 prebuiltOrder []*bpModule
Paul Duffin13f02712020-03-06 12:30:43 +0000779
780 // The set of all members by name.
781 allMembersByName map[string]struct{}
782
783 // The set of exported members by name.
784 exportedMembersByName map[string]struct{}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000785}
786
787func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000788 if existing, ok := s.copies[dest]; ok {
789 if existing != src.String() {
790 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
791 return
792 }
793 } else {
794 path := s.snapshotDir.Join(s.ctx, dest)
795 s.ctx.Build(pctx, android.BuildParams{
796 Rule: android.Cp,
797 Input: src,
798 Output: path,
799 })
800 s.filesToZip = append(s.filesToZip, path)
801
802 s.copies[dest] = src.String()
803 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000804}
805
Paul Duffin91547182019-11-12 19:39:36 +0000806func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
807 ctx := s.ctx
808
809 // Repackage the zip file so that the entries are in the destDir directory.
810 // This will allow the zip file to be merged into the snapshot.
811 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000812
813 ctx.Build(pctx, android.BuildParams{
814 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
815 Rule: repackageZip,
816 Input: zipPath,
817 Output: tmpZipPath,
818 Args: map[string]string{
819 "destdir": destDir,
820 },
821 })
Paul Duffin91547182019-11-12 19:39:36 +0000822
823 // Add the repackaged zip file to the files to merge.
824 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
825}
826
Paul Duffin9d8d6092019-12-05 18:19:29 +0000827func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
828 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000829 if s.prebuiltModules[name] != nil {
830 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
831 }
832
833 m := s.bpFile.newModule(moduleType)
834 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000835
Paul Duffinbefa4b92020-03-04 14:22:45 +0000836 variant := member.Variants()[0]
837
Paul Duffin13f02712020-03-06 12:30:43 +0000838 if s.isInternalMember(name) {
Paul Duffin72910952020-01-20 18:16:30 +0000839 // An internal member is only referenced from the sdk snapshot which is in the
840 // same package so can be marked as private.
841 m.AddProperty("visibility", []string{"//visibility:private"})
842 } else {
843 // Extract visibility information from a member variant. All variants have the same
844 // visibility so it doesn't matter which one is used.
Paul Duffin157f40f2020-09-29 16:01:08 +0100845 visibilityRules := android.EffectiveVisibilityRules(s.ctx, variant)
846
847 // Add any additional visibility rules needed for the prebuilts to reference each other.
848 err := visibilityRules.Widen(s.sdk.properties.Prebuilt_visibility)
849 if err != nil {
850 s.ctx.PropertyErrorf("prebuilt_visibility", "%s", err)
851 }
852
853 visibility := visibilityRules.Strings()
Paul Duffin72910952020-01-20 18:16:30 +0000854 if len(visibility) != 0 {
855 m.AddProperty("visibility", visibility)
856 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000857 }
858
Martin Stjernholm1e041092020-11-03 00:11:09 +0000859 // Where available copy apex_available properties from the member.
860 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
861 apexAvailable := apexAware.ApexAvailable()
862 if len(apexAvailable) == 0 {
863 // //apex_available:platform is the default.
864 apexAvailable = []string{android.AvailableToPlatform}
865 }
866
867 // Add in any baseline apex available settings.
868 apexAvailable = append(apexAvailable, apex.BaselineApexAvailable(member.Name())...)
869
870 // Remove duplicates and sort.
871 apexAvailable = android.FirstUniqueStrings(apexAvailable)
872 sort.Strings(apexAvailable)
873
874 m.AddProperty("apex_available", apexAvailable)
875 }
876
Paul Duffin865171e2020-03-02 18:38:15 +0000877 deviceSupported := false
878 hostSupported := false
879
880 for _, variant := range member.Variants() {
881 osClass := variant.Target().Os.Class
Jiyong Park1613e552020-09-14 19:43:17 +0900882 if osClass == android.Host {
Paul Duffin865171e2020-03-02 18:38:15 +0000883 hostSupported = true
884 } else if osClass == android.Device {
885 deviceSupported = true
886 }
887 }
888
889 addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000890
Paul Duffin0cb37b92020-03-04 14:52:46 +0000891 // Disable installation in the versioned module of those modules that are ever installable.
892 if installable, ok := variant.(interface{ EverInstallable() bool }); ok {
893 if installable.EverInstallable() {
894 m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag)
895 }
896 }
897
Paul Duffinb645ec82019-11-27 17:43:54 +0000898 s.prebuiltModules[name] = m
899 s.prebuiltOrder = append(s.prebuiltOrder, m)
900 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000901}
902
Paul Duffin865171e2020-03-02 18:38:15 +0000903func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
904 if !deviceSupported {
Paul Duffine44358f2019-11-26 18:04:12 +0000905 bpModule.AddProperty("device_supported", false)
906 }
Paul Duffin865171e2020-03-02 18:38:15 +0000907 if hostSupported {
Paul Duffine44358f2019-11-26 18:04:12 +0000908 bpModule.AddProperty("host_supported", true)
909 }
910}
911
Paul Duffin13f02712020-03-06 12:30:43 +0000912func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag {
913 if required {
914 return requiredSdkMemberReferencePropertyTag
915 } else {
916 return optionalSdkMemberReferencePropertyTag
917 }
918}
919
920func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag {
921 return optionalSdkMemberReferencePropertyTag
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000922}
923
Paul Duffinb645ec82019-11-27 17:43:54 +0000924// Get a versioned name appropriate for the SDK snapshot version being taken.
Paul Duffin13f02712020-03-06 12:30:43 +0000925func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string, required bool) string {
926 if _, ok := s.allMembersByName[unversionedName]; !ok {
927 if required {
928 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
929 }
930 return unversionedName
931 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000932 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
933}
Paul Duffinb645ec82019-11-27 17:43:54 +0000934
Paul Duffin13f02712020-03-06 12:30:43 +0000935func (s *snapshotBuilder) versionedSdkMemberNames(members []string, required bool) []string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000936 var references []string = nil
937 for _, m := range members {
Paul Duffin13f02712020-03-06 12:30:43 +0000938 references = append(references, s.versionedSdkMemberName(m, required))
Paul Duffinb645ec82019-11-27 17:43:54 +0000939 }
940 return references
941}
Paul Duffin13879572019-11-28 14:31:38 +0000942
Paul Duffin72910952020-01-20 18:16:30 +0000943// Get an internal name unique to the sdk.
Paul Duffin13f02712020-03-06 12:30:43 +0000944func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string, required bool) string {
945 if _, ok := s.allMembersByName[unversionedName]; !ok {
946 if required {
947 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
948 }
949 return unversionedName
950 }
951
952 if s.isInternalMember(unversionedName) {
Paul Duffin72910952020-01-20 18:16:30 +0000953 return s.ctx.ModuleName() + "_" + unversionedName
954 } else {
955 return unversionedName
956 }
957}
958
Paul Duffin13f02712020-03-06 12:30:43 +0000959func (s *snapshotBuilder) unversionedSdkMemberNames(members []string, required bool) []string {
Paul Duffin72910952020-01-20 18:16:30 +0000960 var references []string = nil
961 for _, m := range members {
Paul Duffin13f02712020-03-06 12:30:43 +0000962 references = append(references, s.unversionedSdkMemberName(m, required))
Paul Duffin72910952020-01-20 18:16:30 +0000963 }
964 return references
965}
966
Paul Duffin13f02712020-03-06 12:30:43 +0000967func (s *snapshotBuilder) isInternalMember(memberName string) bool {
968 _, ok := s.exportedMembersByName[memberName]
969 return !ok
970}
971
Martin Stjernholm89238f42020-07-10 00:14:03 +0100972// Add the properties from the given SdkMemberProperties to the blueprint
973// property set. This handles common properties in SdkMemberPropertiesBase and
974// calls the member-specific AddToPropertySet for the rest.
975func addSdkMemberPropertiesToSet(ctx *memberContext, memberProperties android.SdkMemberProperties, targetPropertySet android.BpPropertySet) {
976 if memberProperties.Base().Compile_multilib != "" {
977 targetPropertySet.AddProperty("compile_multilib", memberProperties.Base().Compile_multilib)
978 }
979
980 memberProperties.AddToPropertySet(ctx, targetPropertySet)
981}
982
Paul Duffin21827262021-04-24 12:16:36 +0100983// sdkMemberVariantDep represents a dependency from an sdk variant onto a member variant.
984type sdkMemberVariantDep struct {
Paul Duffincd064672021-04-24 00:47:29 +0100985 // The sdk variant that depends (possibly indirectly) on the member variant.
986 sdkVariant *sdk
Paul Duffin1356d8c2020-02-25 19:26:33 +0000987 memberType android.SdkMemberType
988 variant android.SdkAware
Paul Duffina7208112021-04-23 21:20:20 +0100989 export bool
Paul Duffin1356d8c2020-02-25 19:26:33 +0000990}
991
Paul Duffin13879572019-11-28 14:31:38 +0000992var _ android.SdkMember = (*sdkMember)(nil)
993
Paul Duffin21827262021-04-24 12:16:36 +0100994// sdkMember groups all the variants of a specific member module together along with the name of the
995// module and the member type. This is used to generate the prebuilt modules for a specific member.
Paul Duffin13879572019-11-28 14:31:38 +0000996type sdkMember struct {
997 memberType android.SdkMemberType
998 name string
999 variants []android.SdkAware
1000}
1001
1002func (m *sdkMember) Name() string {
1003 return m.name
1004}
1005
1006func (m *sdkMember) Variants() []android.SdkAware {
1007 return m.variants
1008}
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001009
Paul Duffin9c3760e2020-03-16 19:52:08 +00001010// Track usages of multilib variants.
1011type multilibUsage int
1012
1013const (
1014 multilibNone multilibUsage = 0
1015 multilib32 multilibUsage = 1
1016 multilib64 multilibUsage = 2
1017 multilibBoth = multilib32 | multilib64
1018)
1019
1020// Add the multilib that is used in the arch type.
1021func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage {
1022 multilib := archType.Multilib
1023 switch multilib {
1024 case "":
1025 return m
1026 case "lib32":
1027 return m | multilib32
1028 case "lib64":
1029 return m | multilib64
1030 default:
1031 panic(fmt.Errorf("Unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib))
1032 }
1033}
1034
1035func (m multilibUsage) String() string {
1036 switch m {
1037 case multilibNone:
1038 return ""
1039 case multilib32:
1040 return "32"
1041 case multilib64:
1042 return "64"
1043 case multilibBoth:
1044 return "both"
1045 default:
1046 panic(fmt.Errorf("Unknown multilib value, found %b, expected one of %b, %b, %b or %b",
1047 m, multilibNone, multilib32, multilib64, multilibBoth))
1048 }
1049}
1050
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001051type baseInfo struct {
1052 Properties android.SdkMemberProperties
1053}
1054
Paul Duffinf34f6d82020-04-30 15:48:31 +01001055func (b *baseInfo) optimizableProperties() interface{} {
1056 return b.Properties
1057}
1058
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001059type osTypeSpecificInfo struct {
1060 baseInfo
1061
Paul Duffin00e46802020-03-12 20:40:35 +00001062 osType android.OsType
1063
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001064 // The list of arch type specific info for this os type.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001065 //
1066 // Nil if there is one variant whose arch type is common
1067 archInfos []*archTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001068}
1069
Paul Duffin4b8b7932020-05-06 12:35:38 +01001070var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
1071
Paul Duffinfc8dd232020-03-17 12:51:37 +00001072type variantPropertiesFactoryFunc func() android.SdkMemberProperties
1073
Paul Duffin00e46802020-03-12 20:40:35 +00001074// Create a new osTypeSpecificInfo for the specified os type and its properties
1075// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001076func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
Paul Duffin00e46802020-03-12 20:40:35 +00001077 osInfo := &osTypeSpecificInfo{
1078 osType: osType,
1079 }
1080
1081 osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
1082 properties := variantPropertiesFactory()
1083 properties.Base().Os = osType
1084 return properties
1085 }
1086
1087 // Create a structure into which properties common across the architectures in
1088 // this os type will be stored.
1089 osInfo.Properties = osSpecificVariantPropertiesFactory()
1090
1091 // Group the variants by arch type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001092 var variantsByArchName = make(map[string][]android.Module)
Paul Duffin00e46802020-03-12 20:40:35 +00001093 var archTypes []android.ArchType
1094 for _, variant := range osTypeVariants {
1095 archType := variant.Target().Arch.ArchType
1096 archTypeName := archType.Name
1097 if _, ok := variantsByArchName[archTypeName]; !ok {
1098 archTypes = append(archTypes, archType)
1099 }
1100
1101 variantsByArchName[archTypeName] = append(variantsByArchName[archTypeName], variant)
1102 }
1103
1104 if commonVariants, ok := variantsByArchName["common"]; ok {
1105 if len(osTypeVariants) != 1 {
Colin Crossafa6a772020-07-06 17:41:08 -07001106 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 +00001107 }
1108
1109 // A common arch type only has one variant and its properties should be treated
1110 // as common to the os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001111 osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
Paul Duffin00e46802020-03-12 20:40:35 +00001112 } else {
1113 // Create an arch specific info for each supported architecture type.
1114 for _, archType := range archTypes {
1115 archTypeName := archType.Name
1116
1117 archVariants := variantsByArchName[archTypeName]
Jiyong Park8fe14e62020-10-19 22:47:34 +09001118 archInfo := newArchSpecificInfo(ctx, archType, osType, osSpecificVariantPropertiesFactory, archVariants)
Paul Duffin00e46802020-03-12 20:40:35 +00001119
1120 osInfo.archInfos = append(osInfo.archInfos, archInfo)
1121 }
1122 }
1123
1124 return osInfo
1125}
1126
1127// Optimize the properties by extracting common properties from arch type specific
1128// properties into os type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001129func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin00e46802020-03-12 20:40:35 +00001130 // Nothing to do if there is only a single common architecture.
1131 if len(osInfo.archInfos) == 0 {
1132 return
1133 }
1134
Paul Duffin9c3760e2020-03-16 19:52:08 +00001135 multilib := multilibNone
Paul Duffin00e46802020-03-12 20:40:35 +00001136 for _, archInfo := range osInfo.archInfos {
Paul Duffin9c3760e2020-03-16 19:52:08 +00001137 multilib = multilib.addArchType(archInfo.archType)
1138
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001139 // Optimize the arch properties first.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001140 archInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin00e46802020-03-12 20:40:35 +00001141 }
1142
Paul Duffin4b8b7932020-05-06 12:35:38 +01001143 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
Paul Duffin00e46802020-03-12 20:40:35 +00001144
1145 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
Paul Duffin9c3760e2020-03-16 19:52:08 +00001146 osInfo.Properties.Base().Compile_multilib = multilib.String()
Paul Duffin00e46802020-03-12 20:40:35 +00001147}
1148
1149// Add the properties for an os to a property set.
1150//
1151// Maps the properties related to the os variants through to an appropriate
1152// module structure that will produce equivalent set of variants when it is
1153// processed in a build.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001154func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
Paul Duffin00e46802020-03-12 20:40:35 +00001155
1156 var osPropertySet android.BpPropertySet
1157 var archPropertySet android.BpPropertySet
1158 var archOsPrefix string
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001159 if osInfo.Properties.Base().Os_count == 1 &&
1160 (osInfo.osType.Class == android.Device || !ctx.memberType.IsHostOsDependent()) {
1161 // There is only one OS type present in the variants and it shouldn't have a
1162 // variant-specific target. The latter is the case if it's either for device
1163 // where there is only one OS (android), or for host and the member type
1164 // isn't host OS dependent.
Paul Duffin00e46802020-03-12 20:40:35 +00001165
1166 // Create a structure that looks like:
1167 // module_type {
1168 // name: "...",
1169 // ...
1170 // <common properties>
1171 // ...
1172 // <single os type specific properties>
1173 //
1174 // arch: {
1175 // <arch specific sections>
1176 // }
1177 //
1178 osPropertySet = bpModule
1179 archPropertySet = osPropertySet.AddPropertySet("arch")
1180
1181 // Arch specific properties need to be added to an arch specific section
1182 // within arch.
1183 archOsPrefix = ""
1184 } else {
1185 // Create a structure that looks like:
1186 // module_type {
1187 // name: "...",
1188 // ...
1189 // <common properties>
1190 // ...
1191 // target: {
1192 // <arch independent os specific sections, e.g. android>
1193 // ...
1194 // <arch and os specific sections, e.g. android_x86>
1195 // }
1196 //
1197 osType := osInfo.osType
1198 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
1199 archPropertySet = targetPropertySet
1200
1201 // Arch specific properties need to be added to an os and arch specific
1202 // section prefixed with <os>_.
1203 archOsPrefix = osType.Name + "_"
1204 }
1205
1206 // Add the os specific but arch independent properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001207 addSdkMemberPropertiesToSet(ctx, osInfo.Properties, osPropertySet)
Paul Duffin00e46802020-03-12 20:40:35 +00001208
1209 // Add arch (and possibly os) specific sections for each set of arch (and possibly
1210 // os) specific properties.
1211 //
1212 // The archInfos list will be empty if the os contains variants for the common
1213 // architecture.
1214 for _, archInfo := range osInfo.archInfos {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001215 archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
Paul Duffin00e46802020-03-12 20:40:35 +00001216 }
1217}
1218
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001219func (osInfo *osTypeSpecificInfo) isHostVariant() bool {
1220 osClass := osInfo.osType.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001221 return osClass == android.Host
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001222}
1223
1224var _ isHostVariant = (*osTypeSpecificInfo)(nil)
1225
Paul Duffin4b8b7932020-05-06 12:35:38 +01001226func (osInfo *osTypeSpecificInfo) String() string {
1227 return fmt.Sprintf("OsType{%s}", osInfo.osType)
1228}
1229
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001230type archTypeSpecificInfo struct {
1231 baseInfo
1232
1233 archType android.ArchType
Jiyong Park8fe14e62020-10-19 22:47:34 +09001234 osType android.OsType
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001235
1236 linkInfos []*linkTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001237}
1238
Paul Duffin4b8b7932020-05-06 12:35:38 +01001239var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
1240
Paul Duffinfc8dd232020-03-17 12:51:37 +00001241// Create a new archTypeSpecificInfo for the specified arch type and its properties
1242// structures populated with information from the variants.
Jiyong Park8fe14e62020-10-19 22:47:34 +09001243func newArchSpecificInfo(ctx android.SdkMemberContext, archType android.ArchType, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001244
Paul Duffinfc8dd232020-03-17 12:51:37 +00001245 // Create an arch specific info into which the variant properties can be copied.
Jiyong Park8fe14e62020-10-19 22:47:34 +09001246 archInfo := &archTypeSpecificInfo{archType: archType, osType: osType}
Paul Duffinfc8dd232020-03-17 12:51:37 +00001247
1248 // Create the properties into which the arch type specific properties will be
1249 // added.
1250 archInfo.Properties = variantPropertiesFactory()
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001251
1252 if len(archVariants) == 1 {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001253 archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001254 } else {
1255 // There is more than one variant for this arch type which must be differentiated
1256 // by link type.
1257 for _, linkVariant := range archVariants {
1258 linkType := getLinkType(linkVariant)
1259 if linkType == "" {
1260 panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(archVariants)))
1261 } else {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001262 linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001263
1264 archInfo.linkInfos = append(archInfo.linkInfos, linkInfo)
1265 }
1266 }
1267 }
Paul Duffinfc8dd232020-03-17 12:51:37 +00001268
1269 return archInfo
1270}
1271
Paul Duffinf34f6d82020-04-30 15:48:31 +01001272func (archInfo *archTypeSpecificInfo) optimizableProperties() interface{} {
1273 return archInfo.Properties
1274}
1275
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001276// Get the link type of the variant
1277//
1278// If the variant is not differentiated by link type then it returns "",
1279// otherwise it returns one of "static" or "shared".
1280func getLinkType(variant android.Module) string {
1281 linkType := ""
1282 if linkable, ok := variant.(cc.LinkableInterface); ok {
1283 if linkable.Shared() && linkable.Static() {
1284 panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String()))
1285 } else if linkable.Shared() {
1286 linkType = "shared"
1287 } else if linkable.Static() {
1288 linkType = "static"
1289 } else {
1290 panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String()))
1291 }
1292 }
1293 return linkType
1294}
1295
1296// Optimize the properties by extracting common properties from link type specific
1297// properties into arch type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001298func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001299 if len(archInfo.linkInfos) == 0 {
1300 return
1301 }
1302
Paul Duffin4b8b7932020-05-06 12:35:38 +01001303 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.linkInfos)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001304}
1305
Paul Duffinfc8dd232020-03-17 12:51:37 +00001306// Add the properties for an arch type to a property set.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001307func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001308 archTypeName := archInfo.archType.Name
1309 archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName)
Jiyong Park8fe14e62020-10-19 22:47:34 +09001310 // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
1311 if ctx.memberType.IsHostOsDependent() && archInfo.osType.Class == android.Host {
1312 archTypePropertySet.AddProperty("enabled", true)
1313 }
Martin Stjernholm89238f42020-07-10 00:14:03 +01001314 addSdkMemberPropertiesToSet(ctx, archInfo.Properties, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001315
1316 for _, linkInfo := range archInfo.linkInfos {
1317 linkPropertySet := archTypePropertySet.AddPropertySet(linkInfo.linkType)
Martin Stjernholm89238f42020-07-10 00:14:03 +01001318 addSdkMemberPropertiesToSet(ctx, linkInfo.Properties, linkPropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001319 }
1320}
1321
Paul Duffin4b8b7932020-05-06 12:35:38 +01001322func (archInfo *archTypeSpecificInfo) String() string {
1323 return fmt.Sprintf("ArchType{%s}", archInfo.archType)
1324}
1325
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001326type linkTypeSpecificInfo struct {
1327 baseInfo
1328
1329 linkType string
1330}
1331
Paul Duffin4b8b7932020-05-06 12:35:38 +01001332var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
1333
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001334// Create a new linkTypeSpecificInfo for the specified link type and its properties
1335// structures populated with information from the variant.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001336func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001337 linkInfo := &linkTypeSpecificInfo{
1338 baseInfo: baseInfo{
1339 // Create the properties into which the link type specific properties will be
1340 // added.
1341 Properties: variantPropertiesFactory(),
1342 },
1343 linkType: linkType,
1344 }
Paul Duffin3a4eb502020-03-19 16:11:18 +00001345 linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001346 return linkInfo
Paul Duffinfc8dd232020-03-17 12:51:37 +00001347}
1348
Paul Duffin4b8b7932020-05-06 12:35:38 +01001349func (l *linkTypeSpecificInfo) String() string {
1350 return fmt.Sprintf("LinkType{%s}", l.linkType)
1351}
1352
Paul Duffin3a4eb502020-03-19 16:11:18 +00001353type memberContext struct {
1354 sdkMemberContext android.ModuleContext
1355 builder *snapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +00001356 memberType android.SdkMemberType
1357 name string
Paul Duffin3a4eb502020-03-19 16:11:18 +00001358}
1359
1360func (m *memberContext) SdkModuleContext() android.ModuleContext {
1361 return m.sdkMemberContext
1362}
1363
1364func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
1365 return m.builder
1366}
1367
Paul Duffina551a1c2020-03-17 21:04:24 +00001368func (m *memberContext) MemberType() android.SdkMemberType {
1369 return m.memberType
1370}
1371
1372func (m *memberContext) Name() string {
1373 return m.name
1374}
1375
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001376func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001377
1378 memberType := member.memberType
1379
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001380 // Do not add the prefer property if the member snapshot module is a source module type.
1381 if !memberType.UsesSourceModuleTypeInSnapshot() {
1382 // Set the prefer based on the environment variable. This is a temporary work around to allow a
1383 // snapshot to be created that sets prefer: true.
1384 // TODO(b/174997203): Remove once the ability to select the modules to prefer can be done
1385 // dynamically at build time not at snapshot generation time.
1386 prefer := ctx.sdkMemberContext.Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_PREFER")
Paul Duffin83ad9562021-05-10 23:49:04 +01001387
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001388 // Set prefer. Setting this to false is not strictly required as that is the default but it does
1389 // provide a convenient hook to post-process the generated Android.bp file, e.g. in tests to
1390 // check the behavior when a prebuilt is preferred. It also makes it explicit what the default
1391 // behavior is for the module.
1392 bpModule.insertAfter("name", "prefer", prefer)
1393 }
Paul Duffin83ad9562021-05-10 23:49:04 +01001394
Paul Duffina04c1072020-03-02 10:16:35 +00001395 // Group the variants by os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001396 variantsByOsType := make(map[android.OsType][]android.Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001397 variants := member.Variants()
1398 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +00001399 osType := variant.Target().Os
1400 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001401 }
1402
Paul Duffina04c1072020-03-02 10:16:35 +00001403 osCount := len(variantsByOsType)
Paul Duffinb44b33a2020-03-17 10:58:23 +00001404 variantPropertiesFactory := func() android.SdkMemberProperties {
Paul Duffina04c1072020-03-02 10:16:35 +00001405 properties := memberType.CreateVariantPropertiesStruct()
1406 base := properties.Base()
1407 base.Os_count = osCount
Paul Duffina04c1072020-03-02 10:16:35 +00001408 return properties
1409 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001410
Paul Duffina04c1072020-03-02 10:16:35 +00001411 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +00001412
Paul Duffina04c1072020-03-02 10:16:35 +00001413 // The set of properties that are common across all architectures and os types.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001414 commonProperties := variantPropertiesFactory()
1415 commonProperties.Base().Os = android.CommonOS
Paul Duffina04c1072020-03-02 10:16:35 +00001416
Paul Duffinc097e362020-03-10 22:50:03 +00001417 // Create common value extractor that can be used to optimize the properties.
1418 commonValueExtractor := newCommonValueExtractor(commonProperties)
1419
Paul Duffina04c1072020-03-02 10:16:35 +00001420 // The list of property structures which are os type specific but common across
1421 // architectures within that os type.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001422 var osSpecificPropertiesContainers []*osTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +00001423
1424 for osType, osTypeVariants := range variantsByOsType {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001425 osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
Paul Duffina04c1072020-03-02 10:16:35 +00001426 osTypeToInfo[osType] = osInfo
Paul Duffinb44b33a2020-03-17 10:58:23 +00001427 // Add the os specific properties to a list of os type specific yet architecture
1428 // independent properties structs.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001429 osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
Paul Duffina04c1072020-03-02 10:16:35 +00001430
Paul Duffin00e46802020-03-12 20:40:35 +00001431 // Optimize the properties across all the variants for a specific os type.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001432 osInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin14eb4672020-03-02 11:33:02 +00001433 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001434
Paul Duffina04c1072020-03-02 10:16:35 +00001435 // Extract properties which are common across all architectures and os types.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001436 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001437
Paul Duffina04c1072020-03-02 10:16:35 +00001438 // Add the common properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001439 addSdkMemberPropertiesToSet(ctx, commonProperties, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001440
Paul Duffina04c1072020-03-02 10:16:35 +00001441 // Create a target property set into which target specific properties can be
1442 // added.
1443 targetPropertySet := bpModule.AddPropertySet("target")
1444
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001445 // If the member is host OS dependent and has host_supported then disable by
1446 // default and enable each host OS variant explicitly. This avoids problems
1447 // with implicitly enabled OS variants when the snapshot is used, which might
1448 // be different from this run (e.g. different build OS).
1449 if ctx.memberType.IsHostOsDependent() {
1450 hostSupported := bpModule.getValue("host_supported") == true // Missing means false.
1451 if hostSupported {
1452 hostPropertySet := targetPropertySet.AddPropertySet("host")
1453 hostPropertySet.AddProperty("enabled", false)
1454 }
1455 }
1456
Paul Duffina04c1072020-03-02 10:16:35 +00001457 // Iterate over the os types in a fixed order.
1458 for _, osType := range s.getPossibleOsTypes() {
1459 osInfo := osTypeToInfo[osType]
1460 if osInfo == nil {
1461 continue
1462 }
1463
Paul Duffin3a4eb502020-03-19 16:11:18 +00001464 osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001465 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001466}
1467
Paul Duffina04c1072020-03-02 10:16:35 +00001468// Compute the list of possible os types that this sdk could support.
1469func (s *sdk) getPossibleOsTypes() []android.OsType {
1470 var osTypes []android.OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +00001471 for _, osType := range android.OsTypeList() {
Paul Duffina04c1072020-03-02 10:16:35 +00001472 if s.DeviceSupported() {
1473 if osType.Class == android.Device && osType != android.Fuchsia {
1474 osTypes = append(osTypes, osType)
1475 }
1476 }
1477 if s.HostSupported() {
Jiyong Park1613e552020-09-14 19:43:17 +09001478 if osType.Class == android.Host {
Paul Duffina04c1072020-03-02 10:16:35 +00001479 osTypes = append(osTypes, osType)
1480 }
1481 }
1482 }
1483 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
1484 return osTypes
1485}
1486
Paul Duffinb28369a2020-05-04 15:39:59 +01001487// Given a set of properties (struct value), return the value of the field within that
1488// struct (or one of its embedded structs).
Paul Duffinc097e362020-03-10 22:50:03 +00001489type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
1490
Paul Duffinc459f892020-04-30 18:08:29 +01001491// Checks the metadata to determine whether the property should be ignored for the
1492// purposes of common value extraction or not.
1493type extractorMetadataPredicate func(metadata propertiesContainer) bool
1494
1495// Indicates whether optimizable properties are provided by a host variant or
1496// not.
1497type isHostVariant interface {
1498 isHostVariant() bool
1499}
1500
Paul Duffinb28369a2020-05-04 15:39:59 +01001501// A property that can be optimized by the commonValueExtractor.
1502type extractorProperty struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +01001503 // The name of the field for this property. It is a "."-separated path for
1504 // fields in non-anonymous substructs.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001505 name string
1506
Paul Duffinc459f892020-04-30 18:08:29 +01001507 // Filter that can use metadata associated with the properties being optimized
1508 // to determine whether the field should be ignored during common value
1509 // optimization.
1510 filter extractorMetadataPredicate
1511
Paul Duffinb28369a2020-05-04 15:39:59 +01001512 // Retrieves the value on which common value optimization will be performed.
1513 getter fieldAccessorFunc
1514
1515 // The empty value for the field.
1516 emptyValue reflect.Value
Paul Duffin864e1b42020-05-06 10:23:19 +01001517
1518 // True if the property can support arch variants false otherwise.
1519 archVariant bool
Paul Duffinb28369a2020-05-04 15:39:59 +01001520}
1521
Paul Duffin4b8b7932020-05-06 12:35:38 +01001522func (p extractorProperty) String() string {
1523 return p.name
1524}
1525
Paul Duffinc097e362020-03-10 22:50:03 +00001526// Supports extracting common values from a number of instances of a properties
1527// structure into a separate common set of properties.
1528type commonValueExtractor struct {
Paul Duffinb28369a2020-05-04 15:39:59 +01001529 // The properties that the extractor can optimize.
1530 properties []extractorProperty
Paul Duffinc097e362020-03-10 22:50:03 +00001531}
1532
1533// Create a new common value extractor for the structure type for the supplied
1534// properties struct.
1535//
1536// The returned extractor can be used on any properties structure of the same type
1537// as the supplied set of properties.
1538func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor {
1539 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
1540 extractor := &commonValueExtractor{}
Martin Stjernholmb0249572020-09-15 02:32:35 +01001541 extractor.gatherFields(structType, nil, "")
Paul Duffinc097e362020-03-10 22:50:03 +00001542 return extractor
1543}
1544
1545// Gather the fields from the supplied structure type from which common values will
1546// be extracted.
Paul Duffinb07fa512020-03-10 22:17:04 +00001547//
Martin Stjernholmb0249572020-09-15 02:32:35 +01001548// This is recursive function. If it encounters a struct then it will recurse
1549// into it, passing in the accessor for the field and the struct name as prefix
1550// for the nested fields. That will then be used in the accessors for the fields
1551// in the embedded struct.
1552func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string) {
Paul Duffinc097e362020-03-10 22:50:03 +00001553 for f := 0; f < structType.NumField(); f++ {
1554 field := structType.Field(f)
1555 if field.PkgPath != "" {
1556 // Ignore unexported fields.
1557 continue
1558 }
1559
Paul Duffinb07fa512020-03-10 22:17:04 +00001560 // Ignore fields whose value should be kept.
1561 if proptools.HasTag(field, "sdk", "keep") {
Paul Duffinc097e362020-03-10 22:50:03 +00001562 continue
1563 }
1564
Paul Duffinc459f892020-04-30 18:08:29 +01001565 var filter extractorMetadataPredicate
1566
1567 // Add a filter
1568 if proptools.HasTag(field, "sdk", "ignored-on-host") {
1569 filter = func(metadata propertiesContainer) bool {
1570 if m, ok := metadata.(isHostVariant); ok {
1571 if m.isHostVariant() {
1572 return false
1573 }
1574 }
1575 return true
1576 }
1577 }
1578
Paul Duffinc097e362020-03-10 22:50:03 +00001579 // Save a copy of the field index for use in the function.
1580 fieldIndex := f
Paul Duffin4b8b7932020-05-06 12:35:38 +01001581
Martin Stjernholmb0249572020-09-15 02:32:35 +01001582 name := namePrefix + field.Name
Paul Duffin4b8b7932020-05-06 12:35:38 +01001583
Paul Duffinc097e362020-03-10 22:50:03 +00001584 fieldGetter := func(value reflect.Value) reflect.Value {
Paul Duffinb07fa512020-03-10 22:17:04 +00001585 if containingStructAccessor != nil {
1586 // This is an embedded structure so first access the field for the embedded
1587 // structure.
1588 value = containingStructAccessor(value)
1589 }
1590
Paul Duffinc097e362020-03-10 22:50:03 +00001591 // Skip through interface and pointer values to find the structure.
1592 value = getStructValue(value)
1593
Paul Duffin4b8b7932020-05-06 12:35:38 +01001594 defer func() {
1595 if r := recover(); r != nil {
1596 panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
1597 }
1598 }()
1599
Paul Duffinc097e362020-03-10 22:50:03 +00001600 // Return the field.
1601 return value.Field(fieldIndex)
1602 }
1603
Martin Stjernholmb0249572020-09-15 02:32:35 +01001604 if field.Type.Kind() == reflect.Struct {
1605 // Gather fields from the nested or embedded structure.
1606 var subNamePrefix string
1607 if field.Anonymous {
1608 subNamePrefix = namePrefix
1609 } else {
1610 subNamePrefix = name + "."
1611 }
1612 e.gatherFields(field.Type, fieldGetter, subNamePrefix)
Paul Duffinb07fa512020-03-10 22:17:04 +00001613 } else {
Paul Duffinb28369a2020-05-04 15:39:59 +01001614 property := extractorProperty{
Paul Duffin4b8b7932020-05-06 12:35:38 +01001615 name,
Paul Duffinc459f892020-04-30 18:08:29 +01001616 filter,
Paul Duffinb28369a2020-05-04 15:39:59 +01001617 fieldGetter,
1618 reflect.Zero(field.Type),
Paul Duffin864e1b42020-05-06 10:23:19 +01001619 proptools.HasTag(field, "android", "arch_variant"),
Paul Duffinb28369a2020-05-04 15:39:59 +01001620 }
1621 e.properties = append(e.properties, property)
Paul Duffinb07fa512020-03-10 22:17:04 +00001622 }
Paul Duffinc097e362020-03-10 22:50:03 +00001623 }
1624}
1625
1626func getStructValue(value reflect.Value) reflect.Value {
1627foundStruct:
1628 for {
1629 kind := value.Kind()
1630 switch kind {
1631 case reflect.Interface, reflect.Ptr:
1632 value = value.Elem()
1633 case reflect.Struct:
1634 break foundStruct
1635 default:
1636 panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind))
1637 }
1638 }
1639 return value
1640}
1641
Paul Duffinf34f6d82020-04-30 15:48:31 +01001642// A container of properties to be optimized.
1643//
1644// Allows additional information to be associated with the properties, e.g. for
1645// filtering.
1646type propertiesContainer interface {
Paul Duffin4b8b7932020-05-06 12:35:38 +01001647 fmt.Stringer
1648
Paul Duffinf34f6d82020-04-30 15:48:31 +01001649 // Get the properties that need optimizing.
1650 optimizableProperties() interface{}
1651}
1652
Paul Duffin2d1bb892021-04-24 11:32:59 +01001653// A wrapper for sdk variant related properties to allow them to be optimized.
1654type sdkVariantPropertiesContainer struct {
1655 sdkVariant *sdk
1656 properties interface{}
Paul Duffinf34f6d82020-04-30 15:48:31 +01001657}
1658
Paul Duffin2d1bb892021-04-24 11:32:59 +01001659func (c sdkVariantPropertiesContainer) optimizableProperties() interface{} {
1660 return c.properties
Paul Duffinf34f6d82020-04-30 15:48:31 +01001661}
1662
Paul Duffin2d1bb892021-04-24 11:32:59 +01001663func (c sdkVariantPropertiesContainer) String() string {
Paul Duffin4b8b7932020-05-06 12:35:38 +01001664 return c.sdkVariant.String()
1665}
1666
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001667// Extract common properties from a slice of property structures of the same type.
1668//
1669// All the property structures must be of the same type.
1670// commonProperties - must be a pointer to the structure into which common properties will be added.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001671// inputPropertiesSlice - must be a slice of propertiesContainer interfaces.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001672//
1673// Iterates over each exported field (capitalized name) and checks to see whether they
1674// have the same value (using DeepEquals) across all the input properties. If it does not then no
1675// change is made. Otherwise, the common value is stored in the field in the commonProperties
Martin Stjernholmb0249572020-09-15 02:32:35 +01001676// and the field in each of the input properties structure is set to its default value. Nested
1677// structs are visited recursively and their non-struct fields are compared.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001678func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001679 commonPropertiesValue := reflect.ValueOf(commonProperties)
1680 commonStructValue := commonPropertiesValue.Elem()
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001681
Paul Duffinf34f6d82020-04-30 15:48:31 +01001682 sliceValue := reflect.ValueOf(inputPropertiesSlice)
1683
Paul Duffinb28369a2020-05-04 15:39:59 +01001684 for _, property := range e.properties {
1685 fieldGetter := property.getter
Paul Duffinc459f892020-04-30 18:08:29 +01001686 filter := property.filter
1687 if filter == nil {
1688 filter = func(metadata propertiesContainer) bool {
1689 return true
1690 }
1691 }
Paul Duffinb28369a2020-05-04 15:39:59 +01001692
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001693 // Check to see if all the structures have the same value for the field. The commonValue
Paul Duffin864e1b42020-05-06 10:23:19 +01001694 // is nil on entry to the loop and if it is nil on exit then there is no common value or
1695 // all the values have been filtered out, otherwise it points to the common value.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001696 var commonValue *reflect.Value
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001697
Paul Duffin864e1b42020-05-06 10:23:19 +01001698 // Assume that all the values will be the same.
1699 //
1700 // While similar to this is not quite the same as commonValue == nil. If all the values
1701 // have been filtered out then this will be false but commonValue == nil will be true.
1702 valuesDiffer := false
1703
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001704 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01001705 container := sliceValue.Index(i).Interface().(propertiesContainer)
1706 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00001707 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001708
Paul Duffinc459f892020-04-30 18:08:29 +01001709 if !filter(container) {
1710 expectedValue := property.emptyValue.Interface()
1711 actualValue := fieldValue.Interface()
1712 if !reflect.DeepEqual(expectedValue, actualValue) {
1713 return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue)
1714 }
1715 continue
1716 }
1717
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001718 if commonValue == nil {
1719 // Use the first value as the commonProperties value.
1720 commonValue = &fieldValue
1721 } else {
1722 // If the value does not match the current common value then there is
1723 // no value in common so break out.
1724 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
1725 commonValue = nil
Paul Duffin864e1b42020-05-06 10:23:19 +01001726 valuesDiffer = true
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001727 break
1728 }
1729 }
1730 }
1731
Paul Duffin864e1b42020-05-06 10:23:19 +01001732 // If the fields all have common value then store it in the common struct field
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001733 // and set the input struct's field to the empty value.
1734 if commonValue != nil {
Paul Duffinb28369a2020-05-04 15:39:59 +01001735 emptyValue := property.emptyValue
Paul Duffinc097e362020-03-10 22:50:03 +00001736 fieldGetter(commonStructValue).Set(*commonValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001737 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01001738 container := sliceValue.Index(i).Interface().(propertiesContainer)
1739 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00001740 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001741 fieldValue.Set(emptyValue)
1742 }
1743 }
Paul Duffin864e1b42020-05-06 10:23:19 +01001744
1745 if valuesDiffer && !property.archVariant {
1746 // The values differ but the property does not support arch variants so it
1747 // is an error.
1748 var details strings.Builder
1749 for i := 0; i < sliceValue.Len(); i++ {
1750 container := sliceValue.Index(i).Interface().(propertiesContainer)
1751 itemValue := reflect.ValueOf(container.optimizableProperties())
1752 fieldValue := fieldGetter(itemValue)
1753
1754 _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface())
1755 }
1756
1757 return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String())
1758 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001759 }
Paul Duffin4b8b7932020-05-06 12:35:38 +01001760
1761 return nil
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001762}