blob: a731414eeb8ccdde2639517db3d72c0f8c3f4e7d [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 (
Paul Duffinc6ba1822022-05-06 09:38:02 +000018 "bytes"
19 "encoding/json"
Jiyong Park9b409bc2019-10-11 14:59:13 +090020 "fmt"
Paul Duffinb645ec82019-11-27 17:43:54 +000021 "reflect"
Paul Duffina04c1072020-03-02 10:16:35 +000022 "sort"
Jiyong Park9b409bc2019-10-11 14:59:13 +090023 "strings"
24
Paul Duffin7d74e7b2020-03-06 12:30:13 +000025 "android/soong/apex"
Paul Duffin9b76c0b2020-03-12 10:24:35 +000026 "android/soong/cc"
Colin Crosscb0ac952021-07-20 13:17:15 -070027
Paul Duffin375058f2019-11-29 20:17:53 +000028 "github.com/google/blueprint"
Jiyong Park9b409bc2019-10-11 14:59:13 +090029 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
Jiyong Park9b409bc2019-10-11 14:59:13 +090032)
33
Paul Duffin64fb5262021-05-05 21:36:04 +010034// Environment variables that affect the generated snapshot
35// ========================================================
36//
Paul Duffin39abf8f2021-09-24 14:58:27 +010037// SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE
38// This allows the target build release (i.e. the release version of the build within which
39// the snapshot will be used) of the snapshot to be specified. If unspecified then it defaults
40// to the current build release version. Otherwise, it must be the name of one of the build
41// releases defined in nameToBuildRelease, e.g. S, T, etc..
42//
43// The generated snapshot must only be used in the specified target release. If the target
44// build release is not the current build release then the generated Android.bp file not be
45// checked for compatibility.
46//
47// e.g. if setting SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE=S will cause the generated snapshot
48// to be compatible with S.
49//
Paul Duffin64fb5262021-05-05 21:36:04 +010050
Jiyong Park9b409bc2019-10-11 14:59:13 +090051var pctx = android.NewPackageContext("android/soong/sdk")
52
Paul Duffin375058f2019-11-29 20:17:53 +000053var (
54 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
55 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000056 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000057 CommandDeps: []string{
58 "${config.Zip2ZipCmd}",
59 },
60 },
61 "destdir")
62
63 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
64 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -070065 Command: `${config.SoongZipCmd} -C $basedir -r $out.rsp -o $out`,
Paul Duffin375058f2019-11-29 20:17:53 +000066 CommandDeps: []string{
67 "${config.SoongZipCmd}",
68 },
69 Rspfile: "$out.rsp",
70 RspfileContent: "$in",
71 },
72 "basedir")
73
74 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
75 blueprint.RuleParams{
Paul Duffin74f1dcd2022-07-18 13:18:23 +000076 Command: `${config.MergeZipsCmd} -s $out $in`,
Paul Duffin375058f2019-11-29 20:17:53 +000077 CommandDeps: []string{
78 "${config.MergeZipsCmd}",
79 },
80 })
81)
82
Paul Duffin43f7bf02021-05-05 22:00:51 +010083const (
Paul Duffinb01ac4b2022-05-24 20:10:05 +000084 soongSdkSnapshotVersionCurrent = "current"
Paul Duffin43f7bf02021-05-05 22:00:51 +010085)
86
Paul Duffinb645ec82019-11-27 17:43:54 +000087type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090088 content strings.Builder
89 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090090}
91
Paul Duffinb645ec82019-11-27 17:43:54 +000092func (gc *generatedContents) Indent() {
93 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +090094}
95
Paul Duffinb645ec82019-11-27 17:43:54 +000096func (gc *generatedContents) Dedent() {
97 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +090098}
99
Paul Duffina08e4dc2021-06-22 18:19:19 +0100100// IndentedPrintf will add spaces to indent the line to the appropriate level before printing the
101// arguments.
102func (gc *generatedContents) IndentedPrintf(format string, args ...interface{}) {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000103 _, _ = fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format, args...)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100104}
105
106// UnindentedPrintf does not add spaces to indent the line to the appropriate level before printing
107// the arguments.
108func (gc *generatedContents) UnindentedPrintf(format string, args ...interface{}) {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000109 _, _ = fmt.Fprintf(&(gc.content), format, args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900110}
111
Paul Duffin13879572019-11-28 14:31:38 +0000112// Collect all the members.
113//
Paul Duffinb97b1572021-04-29 21:50:40 +0100114// Updates the sdk module with a list of sdkMemberVariantDep instances and details as to which
115// multilibs (32/64/both) are used by this sdk variant.
Paul Duffin6a7e9532020-03-20 17:50:07 +0000116func (s *sdk) collectMembers(ctx android.ModuleContext) {
117 s.multilibUsages = multilibNone
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000118 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
119 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100120 if memberTag, ok := tag.(android.SdkMemberDependencyTag); ok {
Paul Duffineee466e2021-04-27 23:17:56 +0100121 memberType := memberTag.SdkMemberType(child)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900122
Paul Duffin5cca7c42021-05-26 10:16:01 +0100123 // If a nil SdkMemberType was returned then this module should not be added to the sdk.
124 if memberType == nil {
125 return false
126 }
127
Paul Duffin13879572019-11-28 14:31:38 +0000128 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000129 if !memberType.IsInstance(child) {
130 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900131 }
Paul Duffin13879572019-11-28 14:31:38 +0000132
Paul Duffin6a7e9532020-03-20 17:50:07 +0000133 // Keep track of which multilib variants are used by the sdk.
134 s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType)
135
Colin Cross313aa542023-12-13 13:47:44 -0800136 exportedComponentsInfo, _ := android.OtherModuleProvider(ctx, child, android.ExportedComponentsInfoProvider)
Paul Duffinb97b1572021-04-29 21:50:40 +0100137
Paul Duffin5e71e682022-11-23 18:09:54 +0000138 var container android.Module
Paul Duffinc6ba1822022-05-06 09:38:02 +0000139 if parent != ctx.Module() {
Cole Fausted158f42024-03-07 16:41:27 -0800140 container = parent
Paul Duffinc6ba1822022-05-06 09:38:02 +0000141 }
142
Paul Duffin1938dba2022-07-26 23:53:00 +0000143 minApiLevel := android.MinApiLevelForSdkSnapshot(ctx, child)
144
Paul Duffina7208112021-04-23 21:20:20 +0100145 export := memberTag.ExportMember()
Paul Duffinb97b1572021-04-29 21:50:40 +0100146 s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{
Paul Duffinc6ba1822022-05-06 09:38:02 +0000147 sdkVariant: s,
148 memberType: memberType,
Cole Fausted158f42024-03-07 16:41:27 -0800149 variant: child,
Paul Duffin1938dba2022-07-26 23:53:00 +0000150 minApiLevel: minApiLevel,
Paul Duffinc6ba1822022-05-06 09:38:02 +0000151 container: container,
152 export: export,
153 exportedComponentsInfo: exportedComponentsInfo,
Paul Duffinb97b1572021-04-29 21:50:40 +0100154 })
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000155
Paul Duffin2d3da312021-05-06 12:02:27 +0100156 // Recurse down into the member's dependencies as it may have dependencies that need to be
157 // automatically added to the sdk.
158 return true
Jiyong Park73c54ee2019-10-22 20:31:18 +0900159 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000160
161 return false
Paul Duffin13879572019-11-28 14:31:38 +0000162 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000163}
164
Paul Duffincc3132e2021-04-24 01:10:30 +0100165// groupMemberVariantsByMemberThenType groups the member variant dependencies so that all the
166// variants of each member are grouped together within an sdkMember instance.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000167//
Paul Duffincc3132e2021-04-24 01:10:30 +0100168// The sdkMember instances are then grouped into slices by member type. Within each such slice the
169// sdkMember instances appear in the order they were added as dependencies.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000170//
Paul Duffincc3132e2021-04-24 01:10:30 +0100171// Finally, the member type slices are concatenated together to form a single slice. The order in
172// which they are concatenated is the order in which the member types were registered in the
173// android.SdkMemberTypesRegistry.
Paul Duffinf861df72022-07-01 15:56:06 +0000174func (s *sdk) groupMemberVariantsByMemberThenType(ctx android.ModuleContext, targetBuildRelease *buildRelease, memberVariantDeps []sdkMemberVariantDep) []*sdkMember {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000175 byType := make(map[android.SdkMemberType][]*sdkMember)
176 byName := make(map[string]*sdkMember)
177
Paul Duffin21827262021-04-24 12:16:36 +0100178 for _, memberVariantDep := range memberVariantDeps {
179 memberType := memberVariantDep.memberType
180 variant := memberVariantDep.variant
Paul Duffin1356d8c2020-02-25 19:26:33 +0000181
182 name := ctx.OtherModuleName(variant)
183 member := byName[name]
184 if member == nil {
185 member = &sdkMember{memberType: memberType, name: name}
186 byName[name] = member
187 byType[memberType] = append(byType[memberType], member)
Liz Kammer96320df2022-05-12 20:40:00 -0400188 } else if member.memberType != memberType {
189 // validate whether this is the same member type or and overriding member type
190 if memberType.Overrides(member.memberType) {
191 member.memberType = memberType
192 } else if !member.memberType.Overrides(memberType) {
193 ctx.ModuleErrorf("Incompatible member types %q %q", member.memberType, memberType)
194 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000195 }
196
Paul Duffin1356d8c2020-02-25 19:26:33 +0000197 // Only append new variants to the list. This is needed because a member can be both
198 // exported by the sdk and also be a transitive sdk member.
199 member.variants = appendUniqueVariants(member.variants, variant)
200 }
Paul Duffin13879572019-11-28 14:31:38 +0000201 var members []*sdkMember
Paul Duffin62782de2021-07-14 12:05:16 +0100202 for _, memberListProperty := range s.memberTypeListProperties() {
Paul Duffinf861df72022-07-01 15:56:06 +0000203 memberType := memberListProperty.memberType
204
205 if !isMemberTypeSupportedByTargetBuildRelease(memberType, targetBuildRelease) {
206 continue
207 }
208
209 membersOfType := byType[memberType]
Paul Duffin13879572019-11-28 14:31:38 +0000210 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900211 }
212
Paul Duffin6a7e9532020-03-20 17:50:07 +0000213 return members
Jiyong Park73c54ee2019-10-22 20:31:18 +0900214}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900215
Paul Duffinf861df72022-07-01 15:56:06 +0000216// isMemberTypeSupportedByTargetBuildRelease returns true if the member type is supported by the
217// target build release.
218func isMemberTypeSupportedByTargetBuildRelease(memberType android.SdkMemberType, targetBuildRelease *buildRelease) bool {
219 supportedByTargetBuildRelease := true
220 supportedBuildReleases := memberType.SupportedBuildReleases()
221 if supportedBuildReleases == "" {
222 supportedBuildReleases = "S+"
223 }
224
225 set, err := parseBuildReleaseSet(supportedBuildReleases)
226 if err != nil {
227 panic(fmt.Errorf("member type %s has invalid supported build releases %q: %s",
228 memberType.SdkPropertyName(), supportedBuildReleases, err))
229 }
230 if !set.contains(targetBuildRelease) {
231 supportedByTargetBuildRelease = false
232 }
233 return supportedByTargetBuildRelease
234}
235
Paul Duffin5e71e682022-11-23 18:09:54 +0000236func appendUniqueVariants(variants []android.Module, newVariant android.Module) []android.Module {
Paul Duffin72910952020-01-20 18:16:30 +0000237 for _, v := range variants {
238 if v == newVariant {
239 return variants
240 }
241 }
242 return append(variants, newVariant)
243}
244
Paul Duffin51509a12022-04-06 12:48:09 +0000245// BUILD_NUMBER_FILE is the name of the file in the snapshot zip that will contain the number of
246// the build from which the snapshot was produced.
247const BUILD_NUMBER_FILE = "snapshot-creation-build-number.txt"
248
Jiyong Park73c54ee2019-10-22 20:31:18 +0900249// SDK directory structure
250// <sdk_root>/
251// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
252// <api_ver>/ : below this directory are all auto-generated
253// Android.bp : definition of 'sdk_snapshot' module is here
254// aidl/
255// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
256// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900257// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900258// include/
259// bionic/libc/include/stdlib.h : an exported header file
260// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900261// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900262// <arch>/include/ : arch-specific exported headers
263// <arch>/include_gen/ : arch-specific generated headers
264// <arch>/lib/
265// libFoo.so : a stub library
266
Paul Duffin1938dba2022-07-26 23:53:00 +0000267func (s sdk) targetBuildRelease(ctx android.ModuleContext) *buildRelease {
268 config := ctx.Config()
269 targetBuildReleaseEnv := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", buildReleaseCurrent.name)
270 targetBuildRelease, err := nameToRelease(targetBuildReleaseEnv)
271 if err != nil {
272 ctx.ModuleErrorf("invalid SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE: %s", err)
273 targetBuildRelease = buildReleaseCurrent
274 }
275
276 return targetBuildRelease
277}
278
Jiyong Park232e7852019-11-04 12:23:40 +0900279// buildSnapshot is the main function in this source file. It creates rules to copy
280// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffinc6ba1822022-05-06 09:38:02 +0000281func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000282
Paul Duffin1938dba2022-07-26 23:53:00 +0000283 targetBuildRelease := s.targetBuildRelease(ctx)
284 targetApiLevel, err := android.ApiLevelFromUser(ctx, targetBuildRelease.name)
285 if err != nil {
286 targetApiLevel = android.FutureApiLevel
287 }
288
Paul Duffinb97b1572021-04-29 21:50:40 +0100289 // Aggregate all the sdkMemberVariantDep instances from all the sdk variants.
Paul Duffin62131702021-05-07 01:10:01 +0100290 hasLicenses := false
Paul Duffin21827262021-04-24 12:16:36 +0100291 var memberVariantDeps []sdkMemberVariantDep
Paul Duffin1356d8c2020-02-25 19:26:33 +0000292 for _, sdkVariant := range sdkVariants {
Paul Duffin21827262021-04-24 12:16:36 +0100293 memberVariantDeps = append(memberVariantDeps, sdkVariant.memberVariantDeps...)
Paul Duffinb97b1572021-04-29 21:50:40 +0100294 }
Paul Duffin865171e2020-03-02 18:38:15 +0000295
Paul Duffinb97b1572021-04-29 21:50:40 +0100296 // Filter out any sdkMemberVariantDep that is a component of another.
297 memberVariantDeps = filterOutComponents(ctx, memberVariantDeps)
Paul Duffin13f02712020-03-06 12:30:43 +0000298
Paul Duffin1938dba2022-07-26 23:53:00 +0000299 // Record the names of all the members, both explicitly specified and implicitly included. Also,
300 // record the names of any members that should be excluded from this snapshot.
Paul Duffinb97b1572021-04-29 21:50:40 +0100301 allMembersByName := make(map[string]struct{})
302 exportedMembersByName := make(map[string]struct{})
Paul Duffin1938dba2022-07-26 23:53:00 +0000303 excludedMembersByName := make(map[string]struct{})
Paul Duffin62131702021-05-07 01:10:01 +0100304
Paul Duffin1938dba2022-07-26 23:53:00 +0000305 addMember := func(name string, export bool, exclude bool) {
306 if exclude {
307 excludedMembersByName[name] = struct{}{}
308 return
309 }
310
Paul Duffinb97b1572021-04-29 21:50:40 +0100311 allMembersByName[name] = struct{}{}
312 if export {
313 exportedMembersByName[name] = struct{}{}
314 }
315 }
316
317 for _, memberVariantDep := range memberVariantDeps {
318 name := memberVariantDep.variant.Name()
319 export := memberVariantDep.export
320
Paul Duffin1938dba2022-07-26 23:53:00 +0000321 // If the minApiLevel of the member is greater than the target API level then exclude it from
322 // this snapshot.
323 exclude := memberVariantDep.minApiLevel.GreaterThan(targetApiLevel)
Spandan Dasb84dbb22023-03-08 22:06:35 +0000324 // Always include host variants (e.g. host tools) in the snapshot.
325 // Host variants should not be guarded by a min_sdk_version check. In fact, host variants
326 // do not have a `min_sdk_version`.
327 if memberVariantDep.Host() {
328 exclude = false
329 }
Paul Duffin1938dba2022-07-26 23:53:00 +0000330
331 addMember(name, export, exclude)
Paul Duffinb97b1572021-04-29 21:50:40 +0100332
333 // Add any components provided by the module.
334 for _, component := range memberVariantDep.exportedComponentsInfo.Components {
Paul Duffin1938dba2022-07-26 23:53:00 +0000335 addMember(component, export, exclude)
Paul Duffinb97b1572021-04-29 21:50:40 +0100336 }
337
338 if memberVariantDep.memberType == android.LicenseModuleSdkMemberType {
339 hasLicenses = true
Paul Duffin865171e2020-03-02 18:38:15 +0000340 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000341 }
342
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000343 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900344
Cole Fausted158f42024-03-07 16:41:27 -0800345 bp := android.PathForModuleOut(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000346
347 bpFile := &bpFile{
348 modules: make(map[string]*bpModule),
349 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000350
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000351 // Always add -current to the end
352 snapshotFileSuffix := "-current"
Paul Duffin43f7bf02021-05-05 22:00:51 +0100353
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000354 builder := &snapshotBuilder{
Paul Duffin13f02712020-03-06 12:30:43 +0000355 ctx: ctx,
356 sdk: s,
Paul Duffin13f02712020-03-06 12:30:43 +0000357 snapshotDir: snapshotDir.OutputPath,
358 copies: make(map[string]string),
Cole Fausted158f42024-03-07 16:41:27 -0800359 filesToZip: []android.Path{bp},
Paul Duffin13f02712020-03-06 12:30:43 +0000360 bpFile: bpFile,
361 prebuiltModules: make(map[string]*bpModule),
362 allMembersByName: allMembersByName,
363 exportedMembersByName: exportedMembersByName,
Paul Duffin1938dba2022-07-26 23:53:00 +0000364 excludedMembersByName: excludedMembersByName,
Paul Duffin39abf8f2021-09-24 14:58:27 +0100365 targetBuildRelease: targetBuildRelease,
Jiyong Park73c54ee2019-10-22 20:31:18 +0900366 }
Paul Duffinac37c502019-11-26 18:02:20 +0000367 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900368
Paul Duffin62131702021-05-07 01:10:01 +0100369 // If the sdk snapshot includes any license modules then add a package module which has a
370 // default_applicable_licenses property. That will prevent the LSC license process from updating
371 // the generated Android.bp file to add a package module that includes all licenses used by all
372 // the modules in that package. That would be unnecessary as every module in the sdk should have
373 // their own licenses property specified.
374 if hasLicenses {
375 pkg := bpFile.newModule("package")
376 property := "default_applicable_licenses"
377 pkg.AddCommentForProperty(property, `
378A default list here prevents the license LSC from adding its own list which would
379be unnecessary as every module in the sdk already has its own licenses property.
380`)
381 pkg.AddProperty(property, []string{"Android-Apache-2.0"})
382 bpFile.AddModule(pkg)
383 }
384
Paul Duffin0df49682021-05-07 01:10:01 +0100385 // Group the variants for each member module together and then group the members of each member
386 // type together.
Paul Duffinf861df72022-07-01 15:56:06 +0000387 members := s.groupMemberVariantsByMemberThenType(ctx, targetBuildRelease, memberVariantDeps)
Paul Duffin0df49682021-05-07 01:10:01 +0100388
389 // Create the prebuilt modules for each of the member modules.
Paul Duffind19f8942021-07-14 12:08:37 +0100390 traits := s.gatherTraits()
Paul Duffin13ad94f2020-02-19 16:19:27 +0000391 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000392 memberType := member.memberType
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000393 if !memberType.ArePrebuiltsRequired() {
394 continue
395 }
Paul Duffin3a4eb502020-03-19 16:11:18 +0000396
Paul Duffind19f8942021-07-14 12:08:37 +0100397 name := member.name
Paul Duffin1938dba2022-07-26 23:53:00 +0000398 if _, ok := excludedMembersByName[name]; ok {
399 continue
400 }
401
Paul Duffind19f8942021-07-14 12:08:37 +0100402 requiredTraits := traits[name]
403 if requiredTraits == nil {
404 requiredTraits = android.EmptySdkMemberTraitSet()
405 }
406
407 // Create the snapshot for the member.
408 memberCtx := &memberContext{ctx, builder, memberType, name, requiredTraits}
Paul Duffin3a4eb502020-03-19 16:11:18 +0000409
410 prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100411 s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
Jiyong Park73c54ee2019-10-22 20:31:18 +0900412 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900413
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000414 // Create a transformer that will transform a module by replacing any references
Paul Duffin72910952020-01-20 18:16:30 +0000415 // to internal members with a unique module name and setting prefer: false.
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000416 snapshotTransformer := snapshotTransformation{
Paul Duffin64fb5262021-05-05 21:36:04 +0100417 builder: builder,
Paul Duffin64fb5262021-05-05 21:36:04 +0100418 }
Paul Duffin72910952020-01-20 18:16:30 +0000419
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000420 for _, module := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000421 // Prune any empty property sets.
Sam Delmerico35881362023-06-30 14:40:10 -0400422 module = transformModule(module, pruneEmptySetTransformer{})
Paul Duffina78f3a72020-02-21 16:29:35 +0000423
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000424 // Transform the module module to make it suitable for use in the snapshot.
Sam Delmerico35881362023-06-30 14:40:10 -0400425 module = transformModule(module, snapshotTransformer)
426 module = transformModule(module, emptyClasspathContentsTransformation{})
427 if module != nil {
428 bpFile.AddModule(module)
429 }
Paul Duffin43f7bf02021-05-05 22:00:51 +0100430 }
Paul Duffin26197a62021-04-24 00:34:10 +0100431
432 // generate Android.bp
Cole Fausted158f42024-03-07 16:41:27 -0800433 contents := generateBpContents(bpFile)
Paul Duffin39abf8f2021-09-24 14:58:27 +0100434 // If the snapshot is being generated for the current build release then check the syntax to make
435 // sure that it is compatible.
Paul Duffin42a49f12022-08-17 22:09:55 +0000436 if targetBuildRelease == buildReleaseCurrent {
Paul Duffin39abf8f2021-09-24 14:58:27 +0100437 syntaxCheckSnapshotBpFile(ctx, contents)
438 }
Paul Duffin26197a62021-04-24 00:34:10 +0100439
Cole Fausted158f42024-03-07 16:41:27 -0800440 android.WriteFileRuleVerbatim(ctx, bp, contents)
Paul Duffin26197a62021-04-24 00:34:10 +0100441
Paul Duffin51509a12022-04-06 12:48:09 +0000442 // Copy the build number file into the snapshot.
443 builder.CopyToSnapshot(ctx.Config().BuildNumberFile(ctx), BUILD_NUMBER_FILE)
444
Paul Duffin74f1dcd2022-07-18 13:18:23 +0000445 filesToZip := android.SortedUniquePaths(builder.filesToZip)
Paul Duffin26197a62021-04-24 00:34:10 +0100446
447 // zip them all
Paul Duffinc6ba1822022-05-06 09:38:02 +0000448 zipPath := fmt.Sprintf("%s%s.zip", ctx.ModuleName(), snapshotFileSuffix)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100449 outputZipFile := android.PathForModuleOut(ctx, zipPath).OutputPath
Paul Duffin26197a62021-04-24 00:34:10 +0100450 outputDesc := "Building snapshot for " + ctx.ModuleName()
451
452 // If there are no zips to merge then generate the output zip directly.
453 // Otherwise, generate an intermediate zip file into which other zips can be
454 // merged.
455 var zipFile android.OutputPath
456 var desc string
457 if len(builder.zipsToMerge) == 0 {
458 zipFile = outputZipFile
459 desc = outputDesc
460 } else {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000461 intermediatePath := fmt.Sprintf("%s%s.unmerged.zip", ctx.ModuleName(), snapshotFileSuffix)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100462 zipFile = android.PathForModuleOut(ctx, intermediatePath).OutputPath
Paul Duffin26197a62021-04-24 00:34:10 +0100463 desc = "Building intermediate snapshot for " + ctx.ModuleName()
464 }
465
466 ctx.Build(pctx, android.BuildParams{
467 Description: desc,
468 Rule: zipFiles,
469 Inputs: filesToZip,
470 Output: zipFile,
471 Args: map[string]string{
472 "basedir": builder.snapshotDir.String(),
473 },
474 })
475
476 if len(builder.zipsToMerge) != 0 {
477 ctx.Build(pctx, android.BuildParams{
478 Description: outputDesc,
479 Rule: mergeZips,
480 Input: zipFile,
Paul Duffin74f1dcd2022-07-18 13:18:23 +0000481 Inputs: android.SortedUniquePaths(builder.zipsToMerge),
Paul Duffin26197a62021-04-24 00:34:10 +0100482 Output: outputZipFile,
483 })
484 }
485
Paul Duffinc6ba1822022-05-06 09:38:02 +0000486 modules := s.generateInfoData(ctx, memberVariantDeps)
487
488 // Output the modules information as pretty printed JSON.
Cole Fausted158f42024-03-07 16:41:27 -0800489 info := android.PathForModuleOut(ctx, fmt.Sprintf("%s%s.info", ctx.ModuleName(), snapshotFileSuffix))
Paul Duffinc6ba1822022-05-06 09:38:02 +0000490 output, err := json.MarshalIndent(modules, "", " ")
491 if err != nil {
492 ctx.ModuleErrorf("error generating %q: %s", info, err)
493 }
494 builder.infoContents = string(output)
Cole Fausted158f42024-03-07 16:41:27 -0800495 android.WriteFileRuleVerbatim(ctx, info, builder.infoContents)
496 installedInfo := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), info.Base(), info)
Paul Duffinc6ba1822022-05-06 09:38:02 +0000497 s.infoFile = android.OptionalPathForPath(installedInfo)
498
499 // Install the zip, making sure that the info file has been installed as well.
500 installedZip := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), outputZipFile.Base(), outputZipFile, installedInfo)
501 s.snapshotFile = android.OptionalPathForPath(installedZip)
502}
503
504type moduleInfo struct {
505 // The type of the module, e.g. java_sdk_library
506 moduleType string
507 // The name of the module.
508 name string
509 // A list of additional dependencies of the module.
510 deps []string
Paul Duffin958806b2022-05-16 13:10:47 +0000511 // Additional member specific properties.
512 // These will be added into the generated JSON alongside the above properties.
513 memberSpecific map[string]interface{}
Paul Duffinc6ba1822022-05-06 09:38:02 +0000514}
515
516func (m *moduleInfo) MarshalJSON() ([]byte, error) {
517 buffer := bytes.Buffer{}
518
519 separator := ""
520 writeObjectPair := func(key string, value interface{}) {
521 buffer.WriteString(fmt.Sprintf("%s%q: ", separator, key))
522 b, err := json.Marshal(value)
523 if err != nil {
524 panic(err)
525 }
526 buffer.Write(b)
527 separator = ","
528 }
529
530 buffer.WriteString("{")
531 writeObjectPair("@type", m.moduleType)
532 writeObjectPair("@name", m.name)
533 if m.deps != nil {
534 writeObjectPair("@deps", m.deps)
535 }
Cole Faust18994c72023-02-28 16:02:16 -0800536 for _, k := range android.SortedKeys(m.memberSpecific) {
Paul Duffin958806b2022-05-16 13:10:47 +0000537 v := m.memberSpecific[k]
Paul Duffinc6ba1822022-05-06 09:38:02 +0000538 writeObjectPair(k, v)
539 }
540 buffer.WriteString("}")
541 return buffer.Bytes(), nil
542}
543
544var _ json.Marshaler = (*moduleInfo)(nil)
545
546// generateInfoData creates a list of moduleInfo structures that will be marshalled into JSON.
547func (s *sdk) generateInfoData(ctx android.ModuleContext, memberVariantDeps []sdkMemberVariantDep) interface{} {
548 modules := []*moduleInfo{}
549 sdkInfo := moduleInfo{
Paul Duffin958806b2022-05-16 13:10:47 +0000550 moduleType: "sdk",
551 name: ctx.ModuleName(),
552 memberSpecific: map[string]interface{}{},
Paul Duffinc6ba1822022-05-06 09:38:02 +0000553 }
554 modules = append(modules, &sdkInfo)
555
556 name2Info := map[string]*moduleInfo{}
557 getModuleInfo := func(module android.Module) *moduleInfo {
558 name := module.Name()
559 info := name2Info[name]
560 if info == nil {
561 moduleType := ctx.OtherModuleType(module)
562 // Remove any suffix added when creating modules dynamically.
563 moduleType = strings.Split(moduleType, "__")[0]
564 info = &moduleInfo{
565 moduleType: moduleType,
566 name: name,
567 }
Paul Duffin958806b2022-05-16 13:10:47 +0000568
Colin Cross313aa542023-12-13 13:47:44 -0800569 additionalSdkInfo, _ := android.OtherModuleProvider(ctx, module, android.AdditionalSdkInfoProvider)
Paul Duffin958806b2022-05-16 13:10:47 +0000570 info.memberSpecific = additionalSdkInfo.Properties
571
Paul Duffinc6ba1822022-05-06 09:38:02 +0000572 name2Info[name] = info
573 }
574 return info
575 }
576
577 for _, memberVariantDep := range memberVariantDeps {
578 propertyName := memberVariantDep.memberType.SdkPropertyName()
579 var list []string
Paul Duffin958806b2022-05-16 13:10:47 +0000580 if v, ok := sdkInfo.memberSpecific[propertyName]; ok {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000581 list = v.([]string)
582 }
583
584 memberName := memberVariantDep.variant.Name()
585 list = append(list, memberName)
Paul Duffin958806b2022-05-16 13:10:47 +0000586 sdkInfo.memberSpecific[propertyName] = android.SortedUniqueStrings(list)
Paul Duffinc6ba1822022-05-06 09:38:02 +0000587
588 if memberVariantDep.container != nil {
589 containerInfo := getModuleInfo(memberVariantDep.container)
590 containerInfo.deps = android.SortedUniqueStrings(append(containerInfo.deps, memberName))
591 }
592
593 // Make sure that the module info is created for each module.
594 getModuleInfo(memberVariantDep.variant)
595 }
596
Cole Faust18994c72023-02-28 16:02:16 -0800597 for _, memberName := range android.SortedKeys(name2Info) {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000598 info := name2Info[memberName]
599 modules = append(modules, info)
600 }
601
602 return modules
Paul Duffin26197a62021-04-24 00:34:10 +0100603}
604
Paul Duffinb97b1572021-04-29 21:50:40 +0100605// filterOutComponents removes any item from the deps list that is a component of another item in
606// the deps list, e.g. if the deps list contains "foo" and "foo.stubs" which is component of "foo"
607// then it will remove "foo.stubs" from the deps.
608func filterOutComponents(ctx android.ModuleContext, deps []sdkMemberVariantDep) []sdkMemberVariantDep {
609 // Collate the set of components that all the modules added to the sdk provide.
610 components := map[string]*sdkMemberVariantDep{}
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000611 for i := range deps {
Paul Duffinb97b1572021-04-29 21:50:40 +0100612 dep := &deps[i]
613 for _, c := range dep.exportedComponentsInfo.Components {
614 components[c] = dep
615 }
616 }
617
618 // If no module provides components then return the input deps unfiltered.
619 if len(components) == 0 {
620 return deps
621 }
622
623 filtered := make([]sdkMemberVariantDep, 0, len(deps))
624 for _, dep := range deps {
625 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(dep.variant))
626 if owner, ok := components[name]; ok {
627 // This is a component of another module that is a member of the sdk.
628
629 // If the component is exported but the owning module is not then the configuration is not
630 // supported.
631 if dep.export && !owner.export {
632 ctx.ModuleErrorf("Module %s is internal to the SDK but provides component %s which is used outside the SDK")
633 continue
634 }
635
636 // This module must not be added to the list of members of the sdk as that would result in a
637 // duplicate module in the sdk snapshot.
638 continue
639 }
640
641 filtered = append(filtered, dep)
642 }
643 return filtered
644}
645
Paul Duffinf88d8e02020-05-07 20:21:34 +0100646// Check the syntax of the generated Android.bp file contents and if they are
647// invalid then log an error with the contents (tagged with line numbers) and the
648// errors that were found so that it is easy to see where the problem lies.
649func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) {
650 errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents)
651 if len(errs) != 0 {
652 message := &strings.Builder{}
653 _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot:
654
655Generated Android.bp contents
656========================================================================
657`)
658 for i, line := range strings.Split(contents, "\n") {
659 _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line)
660 }
661
662 _, _ = fmt.Fprint(message, `
663========================================================================
664
665Errors found:
666`)
667
668 for _, err := range errs {
669 _, _ = fmt.Fprintf(message, "%s\n", err.Error())
670 }
671
672 ctx.ModuleErrorf("%s", message.String())
673 }
674}
675
Paul Duffin4b8b7932020-05-06 12:35:38 +0100676func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
677 err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
678 if err != nil {
679 ctx.ModuleErrorf("error extracting common properties: %s", err)
680 }
681}
682
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000683type propertyTag struct {
684 name string
685}
686
Paul Duffin94289702021-09-09 15:38:32 +0100687var _ android.BpPropertyTag = propertyTag{}
688
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000689// BpPropertyTag instances to add to a property that contains references to other sdk members.
Paul Duffin0cb37b92020-03-04 14:52:46 +0000690//
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000691// These will ensure that the referenced modules are available, if required.
Paul Duffin13f02712020-03-06 12:30:43 +0000692var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
Paul Duffin13f02712020-03-06 12:30:43 +0000693var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000694
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000695type snapshotTransformation struct {
Paul Duffine6c0d842020-01-15 14:08:51 +0000696 identityTransformation
697 builder *snapshotBuilder
698}
699
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000700func (t snapshotTransformation) transformModule(module *bpModule) *bpModule {
Sam Delmerico35881362023-06-30 14:40:10 -0400701 if module != nil {
702 // If the module is an internal member then use a unique name for it.
703 name := module.Name()
704 module.setProperty("name", t.builder.snapshotSdkMemberName(name, true))
705 }
Paul Duffin72910952020-01-20 18:16:30 +0000706 return module
707}
708
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000709func (t snapshotTransformation) transformProperty(_ string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000710 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
711 required := tag == requiredSdkMemberReferencePropertyTag
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000712 return t.builder.snapshotSdkMemberNames(value.([]string), required), tag
Paul Duffin72910952020-01-20 18:16:30 +0000713 } else {
714 return value, tag
715 }
716}
717
Sam Delmerico35881362023-06-30 14:40:10 -0400718type emptyClasspathContentsTransformation struct {
719 identityTransformation
720}
721
722func (t emptyClasspathContentsTransformation) transformModule(module *bpModule) *bpModule {
723 classpathModuleTypes := []string{
724 "prebuilt_bootclasspath_fragment",
725 "prebuilt_systemserverclasspath_fragment",
726 }
727 if module != nil && android.InList(module.moduleType, classpathModuleTypes) {
728 if contents, ok := module.bpPropertySet.properties["contents"].([]string); ok {
729 if len(contents) == 0 {
730 return nil
731 }
732 }
733 }
734 return module
735}
736
Paul Duffina78f3a72020-02-21 16:29:35 +0000737type pruneEmptySetTransformer struct {
738 identityTransformation
739}
740
741var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
742
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000743func (t pruneEmptySetTransformer) transformPropertySetAfterContents(_ string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
Paul Duffina78f3a72020-02-21 16:29:35 +0000744 if len(propertySet.properties) == 0 {
745 return nil, nil
746 } else {
747 return propertySet, tag
748 }
749}
750
Cole Fausted158f42024-03-07 16:41:27 -0800751func generateBpContents(bpFile *bpFile) string {
752 contents := &generatedContents{}
Paul Duffina08e4dc2021-06-22 18:19:19 +0100753 contents.IndentedPrintf("// This is auto-generated. DO NOT EDIT.\n")
Paul Duffinb645ec82019-11-27 17:43:54 +0000754 for _, bpModule := range bpFile.order {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000755 contents.IndentedPrintf("\n")
756 contents.IndentedPrintf("%s {\n", bpModule.moduleType)
757 outputPropertySet(contents, bpModule.bpPropertySet)
758 contents.IndentedPrintf("}\n")
Paul Duffinb645ec82019-11-27 17:43:54 +0000759 }
Cole Fausted158f42024-03-07 16:41:27 -0800760 return contents.content.String()
Paul Duffinb645ec82019-11-27 17:43:54 +0000761}
762
763func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
764 contents.Indent()
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000765
Paul Duffin0df49682021-05-07 01:10:01 +0100766 addComment := func(name string) {
767 if text, ok := set.comments[name]; ok {
768 for _, line := range strings.Split(text, "\n") {
Paul Duffina08e4dc2021-06-22 18:19:19 +0100769 contents.IndentedPrintf("// %s\n", line)
Paul Duffin0df49682021-05-07 01:10:01 +0100770 }
771 }
772 }
773
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000774 // Output the properties first, followed by the nested sets. This ensures a
775 // consistent output irrespective of whether property sets are created before
776 // or after the properties. This simplifies the creation of the module.
Paul Duffinb645ec82019-11-27 17:43:54 +0000777 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000778 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000779
Paul Duffin0df49682021-05-07 01:10:01 +0100780 // Do not write property sets in the properties phase.
781 if _, ok := value.(*bpPropertySet); ok {
782 continue
783 }
784
785 addComment(name)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100786 reflectValue := reflect.ValueOf(value)
787 outputNamedValue(contents, name, reflectValue)
Paul Duffinb645ec82019-11-27 17:43:54 +0000788 }
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000789
790 for _, name := range set.order {
791 value := set.getValue(name)
792
793 // Only write property sets in the sets phase.
794 switch v := value.(type) {
795 case *bpPropertySet:
Paul Duffin0df49682021-05-07 01:10:01 +0100796 addComment(name)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100797 contents.IndentedPrintf("%s: {\n", name)
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000798 outputPropertySet(contents, v)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100799 contents.IndentedPrintf("},\n")
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000800 }
801 }
802
Paul Duffinb645ec82019-11-27 17:43:54 +0000803 contents.Dedent()
804}
805
Paul Duffina08e4dc2021-06-22 18:19:19 +0100806// outputNamedValue outputs a value that has an associated name. The name will be indented, followed
807// by the value and then followed by a , and a newline.
808func outputNamedValue(contents *generatedContents, name string, value reflect.Value) {
809 contents.IndentedPrintf("%s: ", name)
810 outputUnnamedValue(contents, value)
811 contents.UnindentedPrintf(",\n")
812}
813
814// outputUnnamedValue outputs a single value. The value is not indented and is not followed by
815// either a , or a newline. With multi-line values, e.g. slices, all but the first line will be
816// indented and all but the last line will end with a newline.
817func outputUnnamedValue(contents *generatedContents, value reflect.Value) {
818 valueType := value.Type()
819 switch valueType.Kind() {
820 case reflect.Bool:
821 contents.UnindentedPrintf("%t", value.Bool())
822
823 case reflect.String:
824 contents.UnindentedPrintf("%q", value)
825
Paul Duffin51227d82021-05-18 12:54:27 +0100826 case reflect.Ptr:
827 outputUnnamedValue(contents, value.Elem())
828
Paul Duffina08e4dc2021-06-22 18:19:19 +0100829 case reflect.Slice:
830 length := value.Len()
831 if length == 0 {
832 contents.UnindentedPrintf("[]")
Paul Duffina08e4dc2021-06-22 18:19:19 +0100833 } else {
Paul Duffin51227d82021-05-18 12:54:27 +0100834 firstValue := value.Index(0)
835 if length == 1 && !multiLineValue(firstValue) {
836 contents.UnindentedPrintf("[")
837 outputUnnamedValue(contents, firstValue)
838 contents.UnindentedPrintf("]")
839 } else {
840 contents.UnindentedPrintf("[\n")
841 contents.Indent()
842 for i := 0; i < length; i++ {
843 itemValue := value.Index(i)
844 contents.IndentedPrintf("")
845 outputUnnamedValue(contents, itemValue)
846 contents.UnindentedPrintf(",\n")
847 }
848 contents.Dedent()
849 contents.IndentedPrintf("]")
Paul Duffina08e4dc2021-06-22 18:19:19 +0100850 }
Paul Duffina08e4dc2021-06-22 18:19:19 +0100851 }
852
Paul Duffin51227d82021-05-18 12:54:27 +0100853 case reflect.Struct:
854 // Avoid unlimited recursion by requiring every structure to implement android.BpPrintable.
855 v := value.Interface()
856 if _, ok := v.(android.BpPrintable); !ok {
857 panic(fmt.Errorf("property value %#v of type %T does not implement android.BpPrintable", v, v))
858 }
859 contents.UnindentedPrintf("{\n")
860 contents.Indent()
861 for f := 0; f < valueType.NumField(); f++ {
862 fieldType := valueType.Field(f)
863 if fieldType.Anonymous {
864 continue
865 }
866 fieldValue := value.Field(f)
867 fieldName := fieldType.Name
868 propertyName := proptools.PropertyNameForField(fieldName)
869 outputNamedValue(contents, propertyName, fieldValue)
870 }
871 contents.Dedent()
872 contents.IndentedPrintf("}")
873
Paul Duffina08e4dc2021-06-22 18:19:19 +0100874 default:
Cole Faust8c366312024-03-08 10:58:32 -0800875 panic(fmt.Errorf("unknown type: %T of value %#v", value, value))
Paul Duffina08e4dc2021-06-22 18:19:19 +0100876 }
877}
878
Paul Duffin51227d82021-05-18 12:54:27 +0100879// multiLineValue returns true if the supplied value may require multiple lines in the output.
880func multiLineValue(value reflect.Value) bool {
881 kind := value.Kind()
882 return kind == reflect.Slice || kind == reflect.Struct
883}
884
Paul Duffinac37c502019-11-26 18:02:20 +0000885func (s *sdk) GetAndroidBpContentsForTests() string {
Cole Fausted158f42024-03-07 16:41:27 -0800886 return generateBpContents(s.builderForTests.bpFile)
Paul Duffinac37c502019-11-26 18:02:20 +0000887}
888
Paul Duffinc6ba1822022-05-06 09:38:02 +0000889func (s *sdk) GetInfoContentsForTests() string {
890 return s.builderForTests.infoContents
891}
892
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000893type snapshotBuilder struct {
Paul Duffin43f7bf02021-05-05 22:00:51 +0100894 ctx android.ModuleContext
895 sdk *sdk
896
Paul Duffinb645ec82019-11-27 17:43:54 +0000897 snapshotDir android.OutputPath
898 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000899
900 // Map from destination to source of each copy - used to eliminate duplicates and
901 // detect conflicts.
902 copies map[string]string
903
Paul Duffinb645ec82019-11-27 17:43:54 +0000904 filesToZip android.Paths
905 zipsToMerge android.Paths
906
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000907 // The path to an empty file.
908 emptyFile android.WritablePath
909
Paul Duffinb645ec82019-11-27 17:43:54 +0000910 prebuiltModules map[string]*bpModule
911 prebuiltOrder []*bpModule
Paul Duffin13f02712020-03-06 12:30:43 +0000912
913 // The set of all members by name.
914 allMembersByName map[string]struct{}
915
916 // The set of exported members by name.
917 exportedMembersByName map[string]struct{}
Paul Duffin39abf8f2021-09-24 14:58:27 +0100918
Paul Duffin1938dba2022-07-26 23:53:00 +0000919 // The set of members which have been excluded from this snapshot; by name.
920 excludedMembersByName map[string]struct{}
921
Paul Duffin39abf8f2021-09-24 14:58:27 +0100922 // The target build release for which the snapshot is to be generated.
923 targetBuildRelease *buildRelease
Paul Duffinc6ba1822022-05-06 09:38:02 +0000924
Paul Duffin958806b2022-05-16 13:10:47 +0000925 // The contents of the .info file that describes the sdk contents.
Paul Duffinc6ba1822022-05-06 09:38:02 +0000926 infoContents string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000927}
928
929func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000930 if existing, ok := s.copies[dest]; ok {
931 if existing != src.String() {
932 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
933 return
934 }
935 } else {
936 path := s.snapshotDir.Join(s.ctx, dest)
937 s.ctx.Build(pctx, android.BuildParams{
938 Rule: android.Cp,
939 Input: src,
940 Output: path,
941 })
942 s.filesToZip = append(s.filesToZip, path)
943
944 s.copies[dest] = src.String()
945 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000946}
947
Paul Duffin91547182019-11-12 19:39:36 +0000948func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
949 ctx := s.ctx
950
951 // Repackage the zip file so that the entries are in the destDir directory.
952 // This will allow the zip file to be merged into the snapshot.
953 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000954
955 ctx.Build(pctx, android.BuildParams{
956 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
957 Rule: repackageZip,
958 Input: zipPath,
959 Output: tmpZipPath,
960 Args: map[string]string{
961 "destdir": destDir,
962 },
963 })
Paul Duffin91547182019-11-12 19:39:36 +0000964
965 // Add the repackaged zip file to the files to merge.
966 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
967}
968
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000969func (s *snapshotBuilder) EmptyFile() android.Path {
970 if s.emptyFile == nil {
971 ctx := s.ctx
972 s.emptyFile = android.PathForModuleOut(ctx, "empty")
973 s.ctx.Build(pctx, android.BuildParams{
974 Rule: android.Touch,
975 Output: s.emptyFile,
976 })
977 }
978
979 return s.emptyFile
980}
981
Paul Duffin9d8d6092019-12-05 18:19:29 +0000982func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
983 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000984 if s.prebuiltModules[name] != nil {
985 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
986 }
987
988 m := s.bpFile.newModule(moduleType)
989 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000990
Paul Duffinbefa4b92020-03-04 14:22:45 +0000991 variant := member.Variants()[0]
992
Paul Duffin13f02712020-03-06 12:30:43 +0000993 if s.isInternalMember(name) {
Paul Duffin72910952020-01-20 18:16:30 +0000994 // An internal member is only referenced from the sdk snapshot which is in the
995 // same package so can be marked as private.
996 m.AddProperty("visibility", []string{"//visibility:private"})
997 } else {
998 // Extract visibility information from a member variant. All variants have the same
999 // visibility so it doesn't matter which one is used.
Paul Duffin157f40f2020-09-29 16:01:08 +01001000 visibilityRules := android.EffectiveVisibilityRules(s.ctx, variant)
1001
1002 // Add any additional visibility rules needed for the prebuilts to reference each other.
1003 err := visibilityRules.Widen(s.sdk.properties.Prebuilt_visibility)
1004 if err != nil {
1005 s.ctx.PropertyErrorf("prebuilt_visibility", "%s", err)
1006 }
1007
1008 visibility := visibilityRules.Strings()
Paul Duffin72910952020-01-20 18:16:30 +00001009 if len(visibility) != 0 {
1010 m.AddProperty("visibility", visibility)
1011 }
Paul Duffin593b3c92019-12-05 14:31:48 +00001012 }
1013
Martin Stjernholm1e041092020-11-03 00:11:09 +00001014 // Where available copy apex_available properties from the member.
1015 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
1016 apexAvailable := apexAware.ApexAvailable()
1017 if len(apexAvailable) == 0 {
1018 // //apex_available:platform is the default.
1019 apexAvailable = []string{android.AvailableToPlatform}
1020 }
1021
1022 // Add in any baseline apex available settings.
1023 apexAvailable = append(apexAvailable, apex.BaselineApexAvailable(member.Name())...)
1024
1025 // Remove duplicates and sort.
1026 apexAvailable = android.FirstUniqueStrings(apexAvailable)
1027 sort.Strings(apexAvailable)
1028
1029 m.AddProperty("apex_available", apexAvailable)
1030 }
1031
Paul Duffinb0bb3762021-05-06 16:48:05 +01001032 // The licenses are the same for all variants.
1033 mctx := s.ctx
Colin Cross313aa542023-12-13 13:47:44 -08001034 licenseInfo, _ := android.OtherModuleProvider(mctx, variant, android.LicenseInfoProvider)
Paul Duffinb0bb3762021-05-06 16:48:05 +01001035 if len(licenseInfo.Licenses) > 0 {
1036 m.AddPropertyWithTag("licenses", licenseInfo.Licenses, s.OptionalSdkMemberReferencePropertyTag())
1037 }
1038
Paul Duffin865171e2020-03-02 18:38:15 +00001039 deviceSupported := false
1040 hostSupported := false
1041
1042 for _, variant := range member.Variants() {
1043 osClass := variant.Target().Os.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001044 if osClass == android.Host {
Paul Duffin865171e2020-03-02 18:38:15 +00001045 hostSupported = true
1046 } else if osClass == android.Device {
1047 deviceSupported = true
1048 }
1049 }
1050
1051 addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
Paul Duffinb645ec82019-11-27 17:43:54 +00001052
1053 s.prebuiltModules[name] = m
1054 s.prebuiltOrder = append(s.prebuiltOrder, m)
1055 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001056}
1057
Paul Duffin865171e2020-03-02 18:38:15 +00001058func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
Paul Duffinb0bb3762021-05-06 16:48:05 +01001059 // If neither device or host is supported then this module does not support either so will not
1060 // recognize the properties.
1061 if !deviceSupported && !hostSupported {
1062 return
1063 }
1064
Paul Duffin865171e2020-03-02 18:38:15 +00001065 if !deviceSupported {
Paul Duffine44358f2019-11-26 18:04:12 +00001066 bpModule.AddProperty("device_supported", false)
1067 }
Paul Duffin865171e2020-03-02 18:38:15 +00001068 if hostSupported {
Paul Duffine44358f2019-11-26 18:04:12 +00001069 bpModule.AddProperty("host_supported", true)
1070 }
1071}
1072
Paul Duffin13f02712020-03-06 12:30:43 +00001073func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag {
1074 if required {
1075 return requiredSdkMemberReferencePropertyTag
1076 } else {
1077 return optionalSdkMemberReferencePropertyTag
1078 }
1079}
1080
1081func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag {
1082 return optionalSdkMemberReferencePropertyTag
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001083}
1084
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001085// Get a name for sdk snapshot member. If the member is private then generate a snapshot specific
1086// name. As part of the processing this checks to make sure that any required members are part of
1087// the snapshot.
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001088func (s *snapshotBuilder) snapshotSdkMemberName(name string, required bool) string {
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001089 if _, ok := s.allMembersByName[name]; !ok {
Paul Duffin13f02712020-03-06 12:30:43 +00001090 if required {
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001091 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", name)
Paul Duffin13f02712020-03-06 12:30:43 +00001092 }
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001093 return name
Paul Duffin13f02712020-03-06 12:30:43 +00001094 }
1095
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001096 if s.isInternalMember(name) {
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001097 return s.ctx.ModuleName() + "_" + name
Paul Duffin72910952020-01-20 18:16:30 +00001098 } else {
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001099 return name
Paul Duffin72910952020-01-20 18:16:30 +00001100 }
1101}
1102
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001103func (s *snapshotBuilder) snapshotSdkMemberNames(members []string, required bool) []string {
Paul Duffin72910952020-01-20 18:16:30 +00001104 var references []string = nil
1105 for _, m := range members {
Paul Duffin1938dba2022-07-26 23:53:00 +00001106 if _, ok := s.excludedMembersByName[m]; ok {
1107 continue
1108 }
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001109 references = append(references, s.snapshotSdkMemberName(m, required))
Paul Duffin72910952020-01-20 18:16:30 +00001110 }
1111 return references
1112}
1113
Paul Duffin13f02712020-03-06 12:30:43 +00001114func (s *snapshotBuilder) isInternalMember(memberName string) bool {
1115 _, ok := s.exportedMembersByName[memberName]
1116 return !ok
1117}
1118
Martin Stjernholm89238f42020-07-10 00:14:03 +01001119// Add the properties from the given SdkMemberProperties to the blueprint
1120// property set. This handles common properties in SdkMemberPropertiesBase and
1121// calls the member-specific AddToPropertySet for the rest.
1122func addSdkMemberPropertiesToSet(ctx *memberContext, memberProperties android.SdkMemberProperties, targetPropertySet android.BpPropertySet) {
1123 if memberProperties.Base().Compile_multilib != "" {
1124 targetPropertySet.AddProperty("compile_multilib", memberProperties.Base().Compile_multilib)
1125 }
1126
1127 memberProperties.AddToPropertySet(ctx, targetPropertySet)
1128}
1129
Paul Duffin21827262021-04-24 12:16:36 +01001130// sdkMemberVariantDep represents a dependency from an sdk variant onto a member variant.
1131type sdkMemberVariantDep struct {
Paul Duffincd064672021-04-24 00:47:29 +01001132 // The sdk variant that depends (possibly indirectly) on the member variant.
1133 sdkVariant *sdk
Paul Duffinb97b1572021-04-29 21:50:40 +01001134
1135 // The type of sdk member the variant is to be treated as.
Paul Duffin1356d8c2020-02-25 19:26:33 +00001136 memberType android.SdkMemberType
Paul Duffinb97b1572021-04-29 21:50:40 +01001137
1138 // The variant that is added to the sdk.
Paul Duffin5e71e682022-11-23 18:09:54 +00001139 variant android.Module
Paul Duffinb97b1572021-04-29 21:50:40 +01001140
Paul Duffinc6ba1822022-05-06 09:38:02 +00001141 // The optional container of this member, i.e. the module that is depended upon by the sdk
1142 // (possibly transitively) and whose dependency on this module is why it was added to the sdk.
1143 // Is nil if this a direct dependency of the sdk.
Paul Duffin5e71e682022-11-23 18:09:54 +00001144 container android.Module
Paul Duffinc6ba1822022-05-06 09:38:02 +00001145
Paul Duffinb97b1572021-04-29 21:50:40 +01001146 // True if the member should be exported, i.e. accessible, from outside the sdk.
1147 export bool
1148
1149 // The names of additional component modules provided by the variant.
1150 exportedComponentsInfo android.ExportedComponentsInfo
Paul Duffin1938dba2022-07-26 23:53:00 +00001151
1152 // The minimum API level on which this module is supported.
1153 minApiLevel android.ApiLevel
Paul Duffin1356d8c2020-02-25 19:26:33 +00001154}
1155
Spandan Dasb84dbb22023-03-08 22:06:35 +00001156// Host returns true if the sdk member is a host variant (e.g. host tool)
1157func (s *sdkMemberVariantDep) Host() bool {
1158 return s.variant.Target().Os.Class == android.Host
1159}
1160
Paul Duffin13879572019-11-28 14:31:38 +00001161var _ android.SdkMember = (*sdkMember)(nil)
1162
Paul Duffin21827262021-04-24 12:16:36 +01001163// sdkMember groups all the variants of a specific member module together along with the name of the
1164// module and the member type. This is used to generate the prebuilt modules for a specific member.
Paul Duffin13879572019-11-28 14:31:38 +00001165type sdkMember struct {
1166 memberType android.SdkMemberType
1167 name string
Paul Duffin5e71e682022-11-23 18:09:54 +00001168 variants []android.Module
Paul Duffin13879572019-11-28 14:31:38 +00001169}
1170
1171func (m *sdkMember) Name() string {
1172 return m.name
1173}
1174
Paul Duffin5e71e682022-11-23 18:09:54 +00001175func (m *sdkMember) Variants() []android.Module {
Paul Duffin13879572019-11-28 14:31:38 +00001176 return m.variants
1177}
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001178
Paul Duffin9c3760e2020-03-16 19:52:08 +00001179// Track usages of multilib variants.
1180type multilibUsage int
1181
1182const (
1183 multilibNone multilibUsage = 0
1184 multilib32 multilibUsage = 1
1185 multilib64 multilibUsage = 2
1186 multilibBoth = multilib32 | multilib64
1187)
1188
1189// Add the multilib that is used in the arch type.
1190func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage {
1191 multilib := archType.Multilib
1192 switch multilib {
1193 case "":
1194 return m
1195 case "lib32":
1196 return m | multilib32
1197 case "lib64":
1198 return m | multilib64
1199 default:
Cole Faust8c366312024-03-08 10:58:32 -08001200 panic(fmt.Errorf("unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib))
Paul Duffin9c3760e2020-03-16 19:52:08 +00001201 }
1202}
1203
1204func (m multilibUsage) String() string {
1205 switch m {
1206 case multilibNone:
1207 return ""
1208 case multilib32:
1209 return "32"
1210 case multilib64:
1211 return "64"
1212 case multilibBoth:
1213 return "both"
1214 default:
Cole Faust8c366312024-03-08 10:58:32 -08001215 panic(fmt.Errorf("unknown multilib value, found %b, expected one of %b, %b, %b or %b",
Paul Duffin9c3760e2020-03-16 19:52:08 +00001216 m, multilibNone, multilib32, multilib64, multilibBoth))
1217 }
1218}
1219
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001220// TODO(187910671): BEGIN - Remove once modules do not have an APEX and default variant.
1221// variantCoordinate contains the coordinates used to identify a variant of an SDK member.
1222type variantCoordinate struct {
1223 // osType identifies the OS target of a variant.
1224 osType android.OsType
1225 // archId identifies the architecture and whether it is for the native bridge.
1226 archId archId
1227 // image is the image variant name.
1228 image string
1229 // linkType is the link type name.
1230 linkType string
1231}
1232
1233func getVariantCoordinate(ctx *memberContext, variant android.Module) variantCoordinate {
1234 linkType := ""
1235 if len(ctx.MemberType().SupportedLinkages()) > 0 {
1236 linkType = getLinkType(variant)
1237 }
1238 return variantCoordinate{
1239 osType: variant.Target().Os,
1240 archId: archIdFromTarget(variant.Target()),
1241 image: variant.ImageVariation().Variation,
1242 linkType: linkType,
1243 }
1244}
1245
1246// selectApexVariantsWhereAvailable filters the input list of variants by selecting the APEX
1247// specific variant for a specific variantCoordinate when there is both an APEX and default variant.
1248//
1249// There is a long-standing issue where a module that is added to an APEX has both an APEX and
1250// default/platform variant created even when the module does not require a platform variant. As a
1251// result an indirect dependency onto a module via the APEX will use the APEX variant, whereas a
1252// direct dependency onto the module will use the default/platform variant. That would result in a
1253// failure while attempting to optimize the properties for a member as it would have two variants
1254// when only one was expected.
1255//
1256// This function mitigates that problem by detecting when there are two variants that differ only
1257// by apex variant, where one is the default/platform variant and one is the APEX variant. In that
1258// case it picks the APEX variant. It picks the APEX variant because that is the behavior that would
1259// be expected
Paul Duffin5e71e682022-11-23 18:09:54 +00001260func selectApexVariantsWhereAvailable(ctx *memberContext, variants []android.Module) []android.Module {
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001261 moduleCtx := ctx.sdkMemberContext
1262
1263 // Group the variants by coordinates.
Paul Duffin5e71e682022-11-23 18:09:54 +00001264 variantsByCoord := make(map[variantCoordinate][]android.Module)
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001265 for _, variant := range variants {
1266 coord := getVariantCoordinate(ctx, variant)
1267 variantsByCoord[coord] = append(variantsByCoord[coord], variant)
1268 }
1269
Paul Duffin5e71e682022-11-23 18:09:54 +00001270 toDiscard := make(map[android.Module]struct{})
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001271 for coord, list := range variantsByCoord {
1272 count := len(list)
1273 if count == 1 {
1274 continue
1275 }
1276
Paul Duffin5e71e682022-11-23 18:09:54 +00001277 variantsByApex := make(map[string]android.Module)
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001278 conflictDetected := false
1279 for _, variant := range list {
Colin Cross313aa542023-12-13 13:47:44 -08001280 apexInfo, _ := android.OtherModuleProvider(moduleCtx, variant, android.ApexInfoProvider)
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001281 apexVariationName := apexInfo.ApexVariationName
1282 // If there are two variants for a specific APEX variation then there is conflict.
1283 if _, ok := variantsByApex[apexVariationName]; ok {
1284 conflictDetected = true
1285 break
1286 }
1287 variantsByApex[apexVariationName] = variant
1288 }
1289
1290 // If there are more than 2 apex variations or one of the apex variations is not the
1291 // default/platform variation then there is a conflict.
1292 if len(variantsByApex) != 2 {
1293 conflictDetected = true
1294 } else if _, ok := variantsByApex[""]; !ok {
1295 conflictDetected = true
1296 }
1297
1298 // If there are no conflicts then add the default/platform variation to the list to remove.
1299 if !conflictDetected {
1300 toDiscard[variantsByApex[""]] = struct{}{}
1301 continue
1302 }
1303
1304 // There are duplicate variants at this coordinate and they are not the default and APEX variant
1305 // so fail.
1306 variantDescriptions := []string{}
1307 for _, m := range list {
1308 variantDescriptions = append(variantDescriptions, fmt.Sprintf(" %s", m.String()))
1309 }
1310
1311 moduleCtx.ModuleErrorf("multiple conflicting variants detected for OsType{%s}, %s, Image{%s}, Link{%s}\n%s",
1312 coord.osType, coord.archId.String(), coord.image, coord.linkType,
1313 strings.Join(variantDescriptions, "\n"))
1314 }
1315
1316 // If there are any variants to discard then remove them from the list of variants, while
1317 // preserving the order.
1318 if len(toDiscard) > 0 {
Paul Duffin5e71e682022-11-23 18:09:54 +00001319 filtered := []android.Module{}
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001320 for _, variant := range variants {
1321 if _, ok := toDiscard[variant]; !ok {
1322 filtered = append(filtered, variant)
1323 }
1324 }
1325 variants = filtered
1326 }
1327
1328 return variants
1329}
1330
1331// TODO(187910671): END - Remove once modules do not have an APEX and default variant.
1332
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001333type baseInfo struct {
1334 Properties android.SdkMemberProperties
1335}
1336
Paul Duffinf34f6d82020-04-30 15:48:31 +01001337func (b *baseInfo) optimizableProperties() interface{} {
1338 return b.Properties
1339}
1340
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001341type osTypeSpecificInfo struct {
1342 baseInfo
1343
Paul Duffin00e46802020-03-12 20:40:35 +00001344 osType android.OsType
1345
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001346 // The list of arch type specific info for this os type.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001347 //
1348 // Nil if there is one variant whose arch type is common
1349 archInfos []*archTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001350}
1351
Paul Duffin4b8b7932020-05-06 12:35:38 +01001352var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
1353
Paul Duffinfc8dd232020-03-17 12:51:37 +00001354type variantPropertiesFactoryFunc func() android.SdkMemberProperties
1355
Paul Duffin00e46802020-03-12 20:40:35 +00001356// Create a new osTypeSpecificInfo for the specified os type and its properties
1357// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001358func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
Paul Duffin00e46802020-03-12 20:40:35 +00001359 osInfo := &osTypeSpecificInfo{
1360 osType: osType,
1361 }
1362
1363 osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
1364 properties := variantPropertiesFactory()
1365 properties.Base().Os = osType
1366 return properties
1367 }
1368
1369 // Create a structure into which properties common across the architectures in
1370 // this os type will be stored.
1371 osInfo.Properties = osSpecificVariantPropertiesFactory()
1372
1373 // Group the variants by arch type.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001374 var variantsByArchId = make(map[archId][]android.Module)
1375 var archIds []archId
Paul Duffin00e46802020-03-12 20:40:35 +00001376 for _, variant := range osTypeVariants {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001377 target := variant.Target()
1378 id := archIdFromTarget(target)
1379 if _, ok := variantsByArchId[id]; !ok {
1380 archIds = append(archIds, id)
Paul Duffin00e46802020-03-12 20:40:35 +00001381 }
1382
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001383 variantsByArchId[id] = append(variantsByArchId[id], variant)
Paul Duffin00e46802020-03-12 20:40:35 +00001384 }
1385
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001386 if commonVariants, ok := variantsByArchId[commonArchId]; ok {
Paul Duffin00e46802020-03-12 20:40:35 +00001387 if len(osTypeVariants) != 1 {
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001388 variants := []string{}
1389 for _, m := range osTypeVariants {
1390 variants = append(variants, fmt.Sprintf(" %s", m.String()))
1391 }
1392 panic(fmt.Errorf("expected to only have 1 variant of %q when arch type is common but found %d\n%s",
1393 ctx.Name(),
1394 len(osTypeVariants),
1395 strings.Join(variants, "\n")))
Paul Duffin00e46802020-03-12 20:40:35 +00001396 }
1397
1398 // A common arch type only has one variant and its properties should be treated
1399 // as common to the os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001400 osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
Paul Duffin00e46802020-03-12 20:40:35 +00001401 } else {
1402 // Create an arch specific info for each supported architecture type.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001403 for _, id := range archIds {
1404 archVariants := variantsByArchId[id]
1405 archInfo := newArchSpecificInfo(ctx, id, osType, osSpecificVariantPropertiesFactory, archVariants)
Paul Duffin00e46802020-03-12 20:40:35 +00001406
1407 osInfo.archInfos = append(osInfo.archInfos, archInfo)
1408 }
1409 }
1410
1411 return osInfo
1412}
1413
Paul Duffin39abf8f2021-09-24 14:58:27 +01001414func (osInfo *osTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1415 if len(osInfo.archInfos) == 0 {
1416 pruner.pruneProperties(osInfo.Properties)
1417 } else {
1418 for _, archInfo := range osInfo.archInfos {
1419 archInfo.pruneUnsupportedProperties(pruner)
1420 }
1421 }
1422}
1423
Paul Duffin00e46802020-03-12 20:40:35 +00001424// Optimize the properties by extracting common properties from arch type specific
1425// properties into os type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001426func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin00e46802020-03-12 20:40:35 +00001427 // Nothing to do if there is only a single common architecture.
1428 if len(osInfo.archInfos) == 0 {
1429 return
1430 }
1431
Paul Duffin9c3760e2020-03-16 19:52:08 +00001432 multilib := multilibNone
Paul Duffin00e46802020-03-12 20:40:35 +00001433 for _, archInfo := range osInfo.archInfos {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001434 multilib = multilib.addArchType(archInfo.archId.archType)
Paul Duffin9c3760e2020-03-16 19:52:08 +00001435
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001436 // Optimize the arch properties first.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001437 archInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin00e46802020-03-12 20:40:35 +00001438 }
1439
Paul Duffin4b8b7932020-05-06 12:35:38 +01001440 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
Paul Duffin00e46802020-03-12 20:40:35 +00001441
1442 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
Paul Duffin9c3760e2020-03-16 19:52:08 +00001443 osInfo.Properties.Base().Compile_multilib = multilib.String()
Paul Duffin00e46802020-03-12 20:40:35 +00001444}
1445
1446// Add the properties for an os to a property set.
1447//
1448// Maps the properties related to the os variants through to an appropriate
1449// module structure that will produce equivalent set of variants when it is
1450// processed in a build.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001451func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
Paul Duffin00e46802020-03-12 20:40:35 +00001452
1453 var osPropertySet android.BpPropertySet
1454 var archPropertySet android.BpPropertySet
1455 var archOsPrefix string
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001456 if osInfo.Properties.Base().Os_count == 1 &&
1457 (osInfo.osType.Class == android.Device || !ctx.memberType.IsHostOsDependent()) {
1458 // There is only one OS type present in the variants and it shouldn't have a
1459 // variant-specific target. The latter is the case if it's either for device
1460 // where there is only one OS (android), or for host and the member type
1461 // isn't host OS dependent.
Paul Duffin00e46802020-03-12 20:40:35 +00001462
1463 // Create a structure that looks like:
1464 // module_type {
1465 // name: "...",
1466 // ...
1467 // <common properties>
1468 // ...
1469 // <single os type specific properties>
1470 //
1471 // arch: {
1472 // <arch specific sections>
1473 // }
1474 //
1475 osPropertySet = bpModule
1476 archPropertySet = osPropertySet.AddPropertySet("arch")
1477
1478 // Arch specific properties need to be added to an arch specific section
1479 // within arch.
1480 archOsPrefix = ""
1481 } else {
1482 // Create a structure that looks like:
1483 // module_type {
1484 // name: "...",
1485 // ...
1486 // <common properties>
1487 // ...
1488 // target: {
1489 // <arch independent os specific sections, e.g. android>
1490 // ...
1491 // <arch and os specific sections, e.g. android_x86>
1492 // }
1493 //
1494 osType := osInfo.osType
1495 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
1496 archPropertySet = targetPropertySet
1497
1498 // Arch specific properties need to be added to an os and arch specific
1499 // section prefixed with <os>_.
1500 archOsPrefix = osType.Name + "_"
1501 }
1502
1503 // Add the os specific but arch independent properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001504 addSdkMemberPropertiesToSet(ctx, osInfo.Properties, osPropertySet)
Paul Duffin00e46802020-03-12 20:40:35 +00001505
1506 // Add arch (and possibly os) specific sections for each set of arch (and possibly
1507 // os) specific properties.
1508 //
1509 // The archInfos list will be empty if the os contains variants for the common
1510 // architecture.
1511 for _, archInfo := range osInfo.archInfos {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001512 archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
Paul Duffin00e46802020-03-12 20:40:35 +00001513 }
1514}
1515
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001516func (osInfo *osTypeSpecificInfo) isHostVariant() bool {
1517 osClass := osInfo.osType.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001518 return osClass == android.Host
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001519}
1520
1521var _ isHostVariant = (*osTypeSpecificInfo)(nil)
1522
Paul Duffin4b8b7932020-05-06 12:35:38 +01001523func (osInfo *osTypeSpecificInfo) String() string {
1524 return fmt.Sprintf("OsType{%s}", osInfo.osType)
1525}
1526
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001527// archId encapsulates the information needed to identify a combination of arch type and native
1528// bridge support.
1529//
1530// Conceptually, native bridge support is a facet of an android.Target, not an android.Arch as it is
1531// essentially using one android.Arch to implement another. However, in terms of the handling of
1532// the variants native bridge is treated as part of the arch variation. See the ArchVariation method
1533// on android.Target.
1534//
1535// So, it makes sense when optimizing the variants to combine native bridge with the arch type.
1536type archId struct {
1537 // The arch type of the variant's target.
1538 archType android.ArchType
1539
1540 // True if the variants is for the native bridge, false otherwise.
1541 nativeBridge bool
1542}
1543
1544// propertyName returns the name of the property corresponding to use for this arch id.
1545func (i *archId) propertyName() string {
1546 name := i.archType.Name
1547 if i.nativeBridge {
1548 // Note: This does not result in a valid property because there is no architecture specific
1549 // native bridge property, only a generic "native_bridge" property. However, this will be used
1550 // in error messages if there is an attempt to use this in a generated bp file.
1551 name += "_native_bridge"
1552 }
1553 return name
1554}
1555
1556func (i *archId) String() string {
1557 return fmt.Sprintf("ArchType{%s}, NativeBridge{%t}", i.archType, i.nativeBridge)
1558}
1559
1560// archIdFromTarget returns an archId initialized from information in the supplied target.
1561func archIdFromTarget(target android.Target) archId {
1562 return archId{
1563 archType: target.Arch.ArchType,
1564 nativeBridge: target.NativeBridge == android.NativeBridgeEnabled,
1565 }
1566}
1567
1568// commonArchId is the archId for the common architecture.
1569var commonArchId = archId{archType: android.Common}
1570
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001571type archTypeSpecificInfo struct {
1572 baseInfo
1573
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001574 archId archId
1575 osType android.OsType
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001576
Paul Duffinb42fa672021-09-09 16:37:49 +01001577 imageVariantInfos []*imageVariantSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001578}
1579
Paul Duffin4b8b7932020-05-06 12:35:38 +01001580var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
1581
Paul Duffinfc8dd232020-03-17 12:51:37 +00001582// Create a new archTypeSpecificInfo for the specified arch type and its properties
1583// structures populated with information from the variants.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001584func newArchSpecificInfo(ctx android.SdkMemberContext, archId archId, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001585
Paul Duffinfc8dd232020-03-17 12:51:37 +00001586 // Create an arch specific info into which the variant properties can be copied.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001587 archInfo := &archTypeSpecificInfo{archId: archId, osType: osType}
Paul Duffinfc8dd232020-03-17 12:51:37 +00001588
1589 // Create the properties into which the arch type specific properties will be
1590 // added.
1591 archInfo.Properties = variantPropertiesFactory()
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001592
Liz Kammer96320df2022-05-12 20:40:00 -04001593 // if there are multiple supported link variants, we want to nest based on linkage even if there
1594 // is only one variant, otherwise, if there is only one variant we can populate based on the arch
1595 if len(archVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001596 archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001597 } else {
Paul Duffinb42fa672021-09-09 16:37:49 +01001598 // Group the variants by image type.
1599 variantsByImage := make(map[string][]android.Module)
1600 for _, variant := range archVariants {
1601 image := variant.ImageVariation().Variation
1602 variantsByImage[image] = append(variantsByImage[image], variant)
1603 }
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001604
Paul Duffinb42fa672021-09-09 16:37:49 +01001605 // Create the image variant info in a fixed order.
Cole Faust18994c72023-02-28 16:02:16 -08001606 for _, imageVariantName := range android.SortedKeys(variantsByImage) {
Paul Duffinb42fa672021-09-09 16:37:49 +01001607 variants := variantsByImage[imageVariantName]
1608 archInfo.imageVariantInfos = append(archInfo.imageVariantInfos, newImageVariantSpecificInfo(ctx, imageVariantName, variantPropertiesFactory, variants))
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001609 }
1610 }
Paul Duffinfc8dd232020-03-17 12:51:37 +00001611
1612 return archInfo
1613}
1614
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001615// Get the link type of the variant
1616//
1617// If the variant is not differentiated by link type then it returns "",
1618// otherwise it returns one of "static" or "shared".
1619func getLinkType(variant android.Module) string {
1620 linkType := ""
1621 if linkable, ok := variant.(cc.LinkableInterface); ok {
1622 if linkable.Shared() && linkable.Static() {
1623 panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String()))
1624 } else if linkable.Shared() {
1625 linkType = "shared"
1626 } else if linkable.Static() {
1627 linkType = "static"
1628 } else {
1629 panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String()))
1630 }
1631 }
1632 return linkType
1633}
1634
Paul Duffin39abf8f2021-09-24 14:58:27 +01001635func (archInfo *archTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1636 if len(archInfo.imageVariantInfos) == 0 {
1637 pruner.pruneProperties(archInfo.Properties)
1638 } else {
1639 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1640 imageVariantInfo.pruneUnsupportedProperties(pruner)
1641 }
1642 }
1643}
1644
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001645// Optimize the properties by extracting common properties from link type specific
1646// properties into arch type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001647func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffinb42fa672021-09-09 16:37:49 +01001648 if len(archInfo.imageVariantInfos) == 0 {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001649 return
1650 }
1651
Paul Duffinb42fa672021-09-09 16:37:49 +01001652 // Optimize the image variant properties first.
1653 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1654 imageVariantInfo.optimizeProperties(ctx, commonValueExtractor)
1655 }
1656
1657 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.imageVariantInfos)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001658}
1659
Paul Duffinfc8dd232020-03-17 12:51:37 +00001660// Add the properties for an arch type to a property set.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001661func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001662 archPropertySuffix := archInfo.archId.propertyName()
1663 propertySetName := archOsPrefix + archPropertySuffix
1664 archTypePropertySet := archPropertySet.AddPropertySet(propertySetName)
Jiyong Park8fe14e62020-10-19 22:47:34 +09001665 // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
1666 if ctx.memberType.IsHostOsDependent() && archInfo.osType.Class == android.Host {
1667 archTypePropertySet.AddProperty("enabled", true)
1668 }
Martin Stjernholm89238f42020-07-10 00:14:03 +01001669 addSdkMemberPropertiesToSet(ctx, archInfo.Properties, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001670
Paul Duffinb42fa672021-09-09 16:37:49 +01001671 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1672 imageVariantInfo.addToPropertySet(ctx, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001673 }
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001674
1675 // If this is for a native bridge architecture then make sure that the property set does not
1676 // contain any properties as providing native bridge specific properties is not currently
1677 // supported.
1678 if archInfo.archId.nativeBridge {
1679 propertySetContents := getPropertySetContents(archTypePropertySet)
1680 if propertySetContents != "" {
1681 ctx.SdkModuleContext().ModuleErrorf("Architecture variant %q of sdk member %q has properties distinct from other variants; this is not yet supported. The properties are:\n%s",
1682 propertySetName, ctx.name, propertySetContents)
1683 }
1684 }
1685}
1686
1687// getPropertySetContents returns the string representation of the contents of a property set, after
1688// recursively pruning any empty nested property sets.
1689func getPropertySetContents(propertySet android.BpPropertySet) string {
1690 set := propertySet.(*bpPropertySet)
1691 set.transformContents(pruneEmptySetTransformer{})
1692 if len(set.properties) != 0 {
1693 contents := &generatedContents{}
1694 contents.Indent()
1695 outputPropertySet(contents, set)
1696 setAsString := contents.content.String()
1697 return setAsString
1698 }
1699 return ""
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001700}
1701
Paul Duffin4b8b7932020-05-06 12:35:38 +01001702func (archInfo *archTypeSpecificInfo) String() string {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001703 return archInfo.archId.String()
Paul Duffin4b8b7932020-05-06 12:35:38 +01001704}
1705
Paul Duffinb42fa672021-09-09 16:37:49 +01001706type imageVariantSpecificInfo struct {
1707 baseInfo
1708
1709 imageVariant string
1710
1711 linkInfos []*linkTypeSpecificInfo
1712}
1713
1714func newImageVariantSpecificInfo(ctx android.SdkMemberContext, imageVariant string, variantPropertiesFactory variantPropertiesFactoryFunc, imageVariants []android.Module) *imageVariantSpecificInfo {
1715
1716 // Create an image variant specific info into which the variant properties can be copied.
1717 imageInfo := &imageVariantSpecificInfo{imageVariant: imageVariant}
1718
1719 // Create the properties into which the image variant specific properties will be added.
1720 imageInfo.Properties = variantPropertiesFactory()
1721
Liz Kammer96320df2022-05-12 20:40:00 -04001722 // if there are multiple supported link variants, we want to nest even if there is only one
1723 // variant, otherwise, if there is only one variant we can populate based on the image
1724 if len(imageVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
Paul Duffinb42fa672021-09-09 16:37:49 +01001725 imageInfo.Properties.PopulateFromVariant(ctx, imageVariants[0])
1726 } else {
1727 // There is more than one variant for this image variant which must be differentiated by link
Liz Kammer96320df2022-05-12 20:40:00 -04001728 // type. Or there are multiple supported linkages and we need to nest based on link type.
Paul Duffinb42fa672021-09-09 16:37:49 +01001729 for _, linkVariant := range imageVariants {
1730 linkType := getLinkType(linkVariant)
1731 if linkType == "" {
1732 panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(imageVariants)))
1733 } else {
1734 linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
1735
1736 imageInfo.linkInfos = append(imageInfo.linkInfos, linkInfo)
1737 }
1738 }
1739 }
1740
1741 return imageInfo
1742}
1743
Paul Duffin39abf8f2021-09-24 14:58:27 +01001744func (imageInfo *imageVariantSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1745 if len(imageInfo.linkInfos) == 0 {
1746 pruner.pruneProperties(imageInfo.Properties)
1747 } else {
1748 for _, linkInfo := range imageInfo.linkInfos {
1749 linkInfo.pruneUnsupportedProperties(pruner)
1750 }
1751 }
1752}
1753
Paul Duffinb42fa672021-09-09 16:37:49 +01001754// Optimize the properties by extracting common properties from link type specific
1755// properties into arch type specific properties.
1756func (imageInfo *imageVariantSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
1757 if len(imageInfo.linkInfos) == 0 {
1758 return
1759 }
1760
1761 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, imageInfo.Properties, imageInfo.linkInfos)
1762}
1763
1764// Add the properties for an arch type to a property set.
1765func (imageInfo *imageVariantSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
1766 if imageInfo.imageVariant != android.CoreVariation {
1767 propertySet = propertySet.AddPropertySet(imageInfo.imageVariant)
1768 }
1769
1770 addSdkMemberPropertiesToSet(ctx, imageInfo.Properties, propertySet)
1771
Liz Kammer96320df2022-05-12 20:40:00 -04001772 usedLinkages := make(map[string]bool, len(imageInfo.linkInfos))
Paul Duffinb42fa672021-09-09 16:37:49 +01001773 for _, linkInfo := range imageInfo.linkInfos {
Liz Kammer96320df2022-05-12 20:40:00 -04001774 usedLinkages[linkInfo.linkType] = true
Paul Duffinb42fa672021-09-09 16:37:49 +01001775 linkInfo.addToPropertySet(ctx, propertySet)
1776 }
1777
Liz Kammer96320df2022-05-12 20:40:00 -04001778 // If not all supported linkages had existing variants, we need to disable the unsupported variant
1779 if len(imageInfo.linkInfos) < len(ctx.MemberType().SupportedLinkages()) {
1780 for _, l := range ctx.MemberType().SupportedLinkages() {
1781 if _, ok := usedLinkages[l]; !ok {
1782 otherLinkagePropertySet := propertySet.AddPropertySet(l)
1783 otherLinkagePropertySet.AddProperty("enabled", false)
1784 }
1785 }
1786 }
1787
Paul Duffinb42fa672021-09-09 16:37:49 +01001788 // If this is for a non-core image variant then make sure that the property set does not contain
1789 // any properties as providing non-core image variant specific properties for prebuilts is not
1790 // currently supported.
1791 if imageInfo.imageVariant != android.CoreVariation {
1792 propertySetContents := getPropertySetContents(propertySet)
1793 if propertySetContents != "" {
1794 ctx.SdkModuleContext().ModuleErrorf("Image variant %q of sdk member %q has properties distinct from other variants; this is not yet supported. The properties are:\n%s",
1795 imageInfo.imageVariant, ctx.name, propertySetContents)
1796 }
1797 }
1798}
1799
1800func (imageInfo *imageVariantSpecificInfo) String() string {
1801 return imageInfo.imageVariant
1802}
1803
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001804type linkTypeSpecificInfo struct {
1805 baseInfo
1806
1807 linkType string
1808}
1809
Paul Duffin4b8b7932020-05-06 12:35:38 +01001810var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
1811
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001812// Create a new linkTypeSpecificInfo for the specified link type and its properties
1813// structures populated with information from the variant.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001814func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001815 linkInfo := &linkTypeSpecificInfo{
1816 baseInfo: baseInfo{
1817 // Create the properties into which the link type specific properties will be
1818 // added.
1819 Properties: variantPropertiesFactory(),
1820 },
1821 linkType: linkType,
1822 }
Paul Duffin3a4eb502020-03-19 16:11:18 +00001823 linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001824 return linkInfo
Paul Duffinfc8dd232020-03-17 12:51:37 +00001825}
1826
Paul Duffinf68f85a2021-09-09 16:11:42 +01001827func (l *linkTypeSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
1828 linkPropertySet := propertySet.AddPropertySet(l.linkType)
1829 addSdkMemberPropertiesToSet(ctx, l.Properties, linkPropertySet)
1830}
1831
Paul Duffin39abf8f2021-09-24 14:58:27 +01001832func (l *linkTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1833 pruner.pruneProperties(l.Properties)
1834}
1835
Paul Duffin4b8b7932020-05-06 12:35:38 +01001836func (l *linkTypeSpecificInfo) String() string {
1837 return fmt.Sprintf("LinkType{%s}", l.linkType)
1838}
1839
Paul Duffin3a4eb502020-03-19 16:11:18 +00001840type memberContext struct {
1841 sdkMemberContext android.ModuleContext
1842 builder *snapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +00001843 memberType android.SdkMemberType
1844 name string
Paul Duffind19f8942021-07-14 12:08:37 +01001845
1846 // The set of traits required of this member.
1847 requiredTraits android.SdkMemberTraitSet
Paul Duffin3a4eb502020-03-19 16:11:18 +00001848}
1849
Cole Faust34867402023-04-28 12:32:27 -07001850func (m *memberContext) ModuleErrorf(fmt string, args ...interface{}) {
1851 m.sdkMemberContext.ModuleErrorf(fmt, args...)
1852}
1853
Paul Duffin3a4eb502020-03-19 16:11:18 +00001854func (m *memberContext) SdkModuleContext() android.ModuleContext {
1855 return m.sdkMemberContext
1856}
1857
1858func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
1859 return m.builder
1860}
1861
Paul Duffina551a1c2020-03-17 21:04:24 +00001862func (m *memberContext) MemberType() android.SdkMemberType {
1863 return m.memberType
1864}
1865
1866func (m *memberContext) Name() string {
1867 return m.name
1868}
1869
Paul Duffind19f8942021-07-14 12:08:37 +01001870func (m *memberContext) RequiresTrait(trait android.SdkMemberTrait) bool {
1871 return m.requiredTraits.Contains(trait)
1872}
1873
Paul Duffin13648912022-07-15 13:12:35 +00001874func (m *memberContext) IsTargetBuildBeforeTiramisu() bool {
1875 return m.builder.targetBuildRelease.EarlierThan(buildReleaseT)
1876}
1877
1878var _ android.SdkMemberContext = (*memberContext)(nil)
1879
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001880func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001881
1882 memberType := member.memberType
1883
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001884 // Do not add the prefer property if the member snapshot module is a source module type.
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001885 moduleCtx := ctx.sdkMemberContext
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001886 if !memberType.UsesSourceModuleTypeInSnapshot() {
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001887 // Set prefer. Setting this to false is not strictly required as that is the default but it does
1888 // provide a convenient hook to post-process the generated Android.bp file, e.g. in tests to
1889 // check the behavior when a prebuilt is preferred. It also makes it explicit what the default
1890 // behavior is for the module.
Paul Duffin82d75ad2022-11-14 17:35:50 +00001891 bpModule.insertAfter("name", "prefer", false)
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001892 }
Paul Duffin83ad9562021-05-10 23:49:04 +01001893
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001894 variants := selectApexVariantsWhereAvailable(ctx, member.variants)
1895
Paul Duffina04c1072020-03-02 10:16:35 +00001896 // Group the variants by os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001897 variantsByOsType := make(map[android.OsType][]android.Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001898 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +00001899 osType := variant.Target().Os
1900 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001901 }
1902
Paul Duffina04c1072020-03-02 10:16:35 +00001903 osCount := len(variantsByOsType)
Paul Duffinb44b33a2020-03-17 10:58:23 +00001904 variantPropertiesFactory := func() android.SdkMemberProperties {
Paul Duffina04c1072020-03-02 10:16:35 +00001905 properties := memberType.CreateVariantPropertiesStruct()
1906 base := properties.Base()
1907 base.Os_count = osCount
Paul Duffina04c1072020-03-02 10:16:35 +00001908 return properties
1909 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001910
Paul Duffina04c1072020-03-02 10:16:35 +00001911 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +00001912
Paul Duffina04c1072020-03-02 10:16:35 +00001913 // The set of properties that are common across all architectures and os types.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001914 commonProperties := variantPropertiesFactory()
1915 commonProperties.Base().Os = android.CommonOS
Paul Duffina04c1072020-03-02 10:16:35 +00001916
Paul Duffin39abf8f2021-09-24 14:58:27 +01001917 // Create a property pruner that will prune any properties unsupported by the target build
1918 // release.
1919 targetBuildRelease := ctx.builder.targetBuildRelease
1920 unsupportedPropertyPruner := newPropertyPrunerByBuildRelease(commonProperties, targetBuildRelease)
1921
Paul Duffinc097e362020-03-10 22:50:03 +00001922 // Create common value extractor that can be used to optimize the properties.
1923 commonValueExtractor := newCommonValueExtractor(commonProperties)
1924
Paul Duffina04c1072020-03-02 10:16:35 +00001925 // The list of property structures which are os type specific but common across
1926 // architectures within that os type.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001927 var osSpecificPropertiesContainers []*osTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +00001928
1929 for osType, osTypeVariants := range variantsByOsType {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001930 osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
Paul Duffina04c1072020-03-02 10:16:35 +00001931 osTypeToInfo[osType] = osInfo
Paul Duffinb44b33a2020-03-17 10:58:23 +00001932 // Add the os specific properties to a list of os type specific yet architecture
1933 // independent properties structs.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001934 osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
Paul Duffina04c1072020-03-02 10:16:35 +00001935
Paul Duffin39abf8f2021-09-24 14:58:27 +01001936 osInfo.pruneUnsupportedProperties(unsupportedPropertyPruner)
1937
Paul Duffin00e46802020-03-12 20:40:35 +00001938 // Optimize the properties across all the variants for a specific os type.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001939 osInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin14eb4672020-03-02 11:33:02 +00001940 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001941
Paul Duffina04c1072020-03-02 10:16:35 +00001942 // Extract properties which are common across all architectures and os types.
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001943 extractCommonProperties(moduleCtx, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001944
Paul Duffina04c1072020-03-02 10:16:35 +00001945 // Add the common properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001946 addSdkMemberPropertiesToSet(ctx, commonProperties, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001947
Paul Duffina04c1072020-03-02 10:16:35 +00001948 // Create a target property set into which target specific properties can be
1949 // added.
1950 targetPropertySet := bpModule.AddPropertySet("target")
1951
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001952 // If the member is host OS dependent and has host_supported then disable by
1953 // default and enable each host OS variant explicitly. This avoids problems
1954 // with implicitly enabled OS variants when the snapshot is used, which might
1955 // be different from this run (e.g. different build OS).
1956 if ctx.memberType.IsHostOsDependent() {
1957 hostSupported := bpModule.getValue("host_supported") == true // Missing means false.
1958 if hostSupported {
1959 hostPropertySet := targetPropertySet.AddPropertySet("host")
1960 hostPropertySet.AddProperty("enabled", false)
1961 }
1962 }
1963
Paul Duffina04c1072020-03-02 10:16:35 +00001964 // Iterate over the os types in a fixed order.
1965 for _, osType := range s.getPossibleOsTypes() {
1966 osInfo := osTypeToInfo[osType]
1967 if osInfo == nil {
1968 continue
1969 }
1970
Paul Duffin3a4eb502020-03-19 16:11:18 +00001971 osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001972 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001973}
1974
Paul Duffina04c1072020-03-02 10:16:35 +00001975// Compute the list of possible os types that this sdk could support.
1976func (s *sdk) getPossibleOsTypes() []android.OsType {
1977 var osTypes []android.OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +00001978 for _, osType := range android.OsTypeList() {
Paul Duffina04c1072020-03-02 10:16:35 +00001979 if s.DeviceSupported() {
Colin Crosscb0ac952021-07-20 13:17:15 -07001980 if osType.Class == android.Device {
Paul Duffina04c1072020-03-02 10:16:35 +00001981 osTypes = append(osTypes, osType)
1982 }
1983 }
1984 if s.HostSupported() {
Jiyong Park1613e552020-09-14 19:43:17 +09001985 if osType.Class == android.Host {
Paul Duffina04c1072020-03-02 10:16:35 +00001986 osTypes = append(osTypes, osType)
1987 }
1988 }
1989 }
1990 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
1991 return osTypes
1992}
1993
Paul Duffinb28369a2020-05-04 15:39:59 +01001994// Given a set of properties (struct value), return the value of the field within that
1995// struct (or one of its embedded structs).
Paul Duffinc097e362020-03-10 22:50:03 +00001996type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
1997
Paul Duffinc459f892020-04-30 18:08:29 +01001998// Checks the metadata to determine whether the property should be ignored for the
1999// purposes of common value extraction or not.
2000type extractorMetadataPredicate func(metadata propertiesContainer) bool
2001
2002// Indicates whether optimizable properties are provided by a host variant or
2003// not.
2004type isHostVariant interface {
2005 isHostVariant() bool
2006}
2007
Paul Duffinb28369a2020-05-04 15:39:59 +01002008// A property that can be optimized by the commonValueExtractor.
2009type extractorProperty struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +01002010 // The name of the field for this property. It is a "."-separated path for
2011 // fields in non-anonymous substructs.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002012 name string
2013
Paul Duffinc459f892020-04-30 18:08:29 +01002014 // Filter that can use metadata associated with the properties being optimized
2015 // to determine whether the field should be ignored during common value
2016 // optimization.
2017 filter extractorMetadataPredicate
2018
Paul Duffinb28369a2020-05-04 15:39:59 +01002019 // Retrieves the value on which common value optimization will be performed.
2020 getter fieldAccessorFunc
2021
Paul Duffinbfdca962022-09-22 16:21:54 +01002022 // True if the field should never be cleared.
2023 //
2024 // This is set to true if and only if the field is annotated with `sdk:"keep"`.
2025 keep bool
2026
Paul Duffinb28369a2020-05-04 15:39:59 +01002027 // The empty value for the field.
2028 emptyValue reflect.Value
Paul Duffin864e1b42020-05-06 10:23:19 +01002029
2030 // True if the property can support arch variants false otherwise.
2031 archVariant bool
Paul Duffinb28369a2020-05-04 15:39:59 +01002032}
2033
Paul Duffin4b8b7932020-05-06 12:35:38 +01002034func (p extractorProperty) String() string {
2035 return p.name
2036}
2037
Paul Duffinc097e362020-03-10 22:50:03 +00002038// Supports extracting common values from a number of instances of a properties
2039// structure into a separate common set of properties.
2040type commonValueExtractor struct {
Paul Duffinb28369a2020-05-04 15:39:59 +01002041 // The properties that the extractor can optimize.
2042 properties []extractorProperty
Paul Duffinc097e362020-03-10 22:50:03 +00002043}
2044
2045// Create a new common value extractor for the structure type for the supplied
2046// properties struct.
2047//
2048// The returned extractor can be used on any properties structure of the same type
2049// as the supplied set of properties.
2050func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor {
2051 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
2052 extractor := &commonValueExtractor{}
Martin Stjernholmb0249572020-09-15 02:32:35 +01002053 extractor.gatherFields(structType, nil, "")
Paul Duffinc097e362020-03-10 22:50:03 +00002054 return extractor
2055}
2056
2057// Gather the fields from the supplied structure type from which common values will
2058// be extracted.
Paul Duffinb07fa512020-03-10 22:17:04 +00002059//
Martin Stjernholmb0249572020-09-15 02:32:35 +01002060// This is recursive function. If it encounters a struct then it will recurse
2061// into it, passing in the accessor for the field and the struct name as prefix
2062// for the nested fields. That will then be used in the accessors for the fields
2063// in the embedded struct.
2064func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string) {
Paul Duffinc097e362020-03-10 22:50:03 +00002065 for f := 0; f < structType.NumField(); f++ {
2066 field := structType.Field(f)
2067 if field.PkgPath != "" {
2068 // Ignore unexported fields.
2069 continue
2070 }
2071
Paul Duffin02e25c82022-09-22 15:30:58 +01002072 // Ignore fields tagged with sdk:"ignore".
2073 if proptools.HasTag(field, "sdk", "ignore") {
Paul Duffinc097e362020-03-10 22:50:03 +00002074 continue
2075 }
2076
Paul Duffinc459f892020-04-30 18:08:29 +01002077 var filter extractorMetadataPredicate
2078
2079 // Add a filter
2080 if proptools.HasTag(field, "sdk", "ignored-on-host") {
2081 filter = func(metadata propertiesContainer) bool {
2082 if m, ok := metadata.(isHostVariant); ok {
2083 if m.isHostVariant() {
2084 return false
2085 }
2086 }
2087 return true
2088 }
2089 }
2090
Paul Duffinbfdca962022-09-22 16:21:54 +01002091 keep := proptools.HasTag(field, "sdk", "keep")
2092
Paul Duffinc097e362020-03-10 22:50:03 +00002093 // Save a copy of the field index for use in the function.
2094 fieldIndex := f
Paul Duffin4b8b7932020-05-06 12:35:38 +01002095
Martin Stjernholmb0249572020-09-15 02:32:35 +01002096 name := namePrefix + field.Name
Paul Duffin4b8b7932020-05-06 12:35:38 +01002097
Paul Duffinc097e362020-03-10 22:50:03 +00002098 fieldGetter := func(value reflect.Value) reflect.Value {
Paul Duffinb07fa512020-03-10 22:17:04 +00002099 if containingStructAccessor != nil {
2100 // This is an embedded structure so first access the field for the embedded
2101 // structure.
2102 value = containingStructAccessor(value)
2103 }
2104
Paul Duffinc097e362020-03-10 22:50:03 +00002105 // Skip through interface and pointer values to find the structure.
2106 value = getStructValue(value)
2107
Paul Duffin4b8b7932020-05-06 12:35:38 +01002108 defer func() {
2109 if r := recover(); r != nil {
2110 panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
2111 }
2112 }()
2113
Paul Duffinc097e362020-03-10 22:50:03 +00002114 // Return the field.
2115 return value.Field(fieldIndex)
2116 }
2117
Martin Stjernholmb0249572020-09-15 02:32:35 +01002118 if field.Type.Kind() == reflect.Struct {
2119 // Gather fields from the nested or embedded structure.
2120 var subNamePrefix string
2121 if field.Anonymous {
2122 subNamePrefix = namePrefix
2123 } else {
2124 subNamePrefix = name + "."
2125 }
2126 e.gatherFields(field.Type, fieldGetter, subNamePrefix)
Paul Duffinb07fa512020-03-10 22:17:04 +00002127 } else {
Paul Duffinb28369a2020-05-04 15:39:59 +01002128 property := extractorProperty{
Paul Duffin4b8b7932020-05-06 12:35:38 +01002129 name,
Paul Duffinc459f892020-04-30 18:08:29 +01002130 filter,
Paul Duffinb28369a2020-05-04 15:39:59 +01002131 fieldGetter,
Paul Duffinbfdca962022-09-22 16:21:54 +01002132 keep,
Paul Duffinb28369a2020-05-04 15:39:59 +01002133 reflect.Zero(field.Type),
Paul Duffin864e1b42020-05-06 10:23:19 +01002134 proptools.HasTag(field, "android", "arch_variant"),
Paul Duffinb28369a2020-05-04 15:39:59 +01002135 }
2136 e.properties = append(e.properties, property)
Paul Duffinb07fa512020-03-10 22:17:04 +00002137 }
Paul Duffinc097e362020-03-10 22:50:03 +00002138 }
2139}
2140
2141func getStructValue(value reflect.Value) reflect.Value {
2142foundStruct:
2143 for {
2144 kind := value.Kind()
2145 switch kind {
2146 case reflect.Interface, reflect.Ptr:
2147 value = value.Elem()
2148 case reflect.Struct:
2149 break foundStruct
2150 default:
2151 panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind))
2152 }
2153 }
2154 return value
2155}
2156
Paul Duffinf34f6d82020-04-30 15:48:31 +01002157// A container of properties to be optimized.
2158//
2159// Allows additional information to be associated with the properties, e.g. for
2160// filtering.
2161type propertiesContainer interface {
Paul Duffin4b8b7932020-05-06 12:35:38 +01002162 fmt.Stringer
2163
Paul Duffinf34f6d82020-04-30 15:48:31 +01002164 // Get the properties that need optimizing.
2165 optimizableProperties() interface{}
2166}
2167
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002168// Extract common properties from a slice of property structures of the same type.
2169//
2170// All the property structures must be of the same type.
2171// commonProperties - must be a pointer to the structure into which common properties will be added.
Paul Duffinf34f6d82020-04-30 15:48:31 +01002172// inputPropertiesSlice - must be a slice of propertiesContainer interfaces.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002173//
2174// Iterates over each exported field (capitalized name) and checks to see whether they
2175// have the same value (using DeepEquals) across all the input properties. If it does not then no
2176// change is made. Otherwise, the common value is stored in the field in the commonProperties
Martin Stjernholmb0249572020-09-15 02:32:35 +01002177// and the field in each of the input properties structure is set to its default value. Nested
2178// structs are visited recursively and their non-struct fields are compared.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002179func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002180 commonPropertiesValue := reflect.ValueOf(commonProperties)
2181 commonStructValue := commonPropertiesValue.Elem()
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002182
Paul Duffinf34f6d82020-04-30 15:48:31 +01002183 sliceValue := reflect.ValueOf(inputPropertiesSlice)
2184
Paul Duffinb28369a2020-05-04 15:39:59 +01002185 for _, property := range e.properties {
2186 fieldGetter := property.getter
Paul Duffinc459f892020-04-30 18:08:29 +01002187 filter := property.filter
2188 if filter == nil {
2189 filter = func(metadata propertiesContainer) bool {
2190 return true
2191 }
2192 }
Paul Duffinb28369a2020-05-04 15:39:59 +01002193
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002194 // Check to see if all the structures have the same value for the field. The commonValue
Paul Duffin864e1b42020-05-06 10:23:19 +01002195 // is nil on entry to the loop and if it is nil on exit then there is no common value or
2196 // all the values have been filtered out, otherwise it points to the common value.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002197 var commonValue *reflect.Value
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002198
Paul Duffin864e1b42020-05-06 10:23:19 +01002199 // Assume that all the values will be the same.
2200 //
2201 // While similar to this is not quite the same as commonValue == nil. If all the values
2202 // have been filtered out then this will be false but commonValue == nil will be true.
2203 valuesDiffer := false
2204
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002205 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01002206 container := sliceValue.Index(i).Interface().(propertiesContainer)
2207 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00002208 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002209
Paul Duffinc459f892020-04-30 18:08:29 +01002210 if !filter(container) {
2211 expectedValue := property.emptyValue.Interface()
2212 actualValue := fieldValue.Interface()
2213 if !reflect.DeepEqual(expectedValue, actualValue) {
2214 return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue)
2215 }
2216 continue
2217 }
2218
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002219 if commonValue == nil {
2220 // Use the first value as the commonProperties value.
2221 commonValue = &fieldValue
2222 } else {
2223 // If the value does not match the current common value then there is
2224 // no value in common so break out.
2225 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
2226 commonValue = nil
Paul Duffin864e1b42020-05-06 10:23:19 +01002227 valuesDiffer = true
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002228 break
2229 }
2230 }
2231 }
2232
Paul Duffin864e1b42020-05-06 10:23:19 +01002233 // If the fields all have common value then store it in the common struct field
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002234 // and set the input struct's field to the empty value.
2235 if commonValue != nil {
Paul Duffinb28369a2020-05-04 15:39:59 +01002236 emptyValue := property.emptyValue
Paul Duffinc097e362020-03-10 22:50:03 +00002237 fieldGetter(commonStructValue).Set(*commonValue)
Paul Duffinbfdca962022-09-22 16:21:54 +01002238 if !property.keep {
2239 for i := 0; i < sliceValue.Len(); i++ {
2240 container := sliceValue.Index(i).Interface().(propertiesContainer)
2241 itemValue := reflect.ValueOf(container.optimizableProperties())
2242 fieldValue := fieldGetter(itemValue)
2243 fieldValue.Set(emptyValue)
2244 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002245 }
2246 }
Paul Duffin864e1b42020-05-06 10:23:19 +01002247
2248 if valuesDiffer && !property.archVariant {
2249 // The values differ but the property does not support arch variants so it
2250 // is an error.
2251 var details strings.Builder
2252 for i := 0; i < sliceValue.Len(); i++ {
2253 container := sliceValue.Index(i).Interface().(propertiesContainer)
2254 itemValue := reflect.ValueOf(container.optimizableProperties())
2255 fieldValue := fieldGetter(itemValue)
2256
2257 _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface())
2258 }
2259
2260 return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String())
2261 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002262 }
Paul Duffin4b8b7932020-05-06 12:35:38 +01002263
2264 return nil
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002265}