blob: b8ae38a33a1e5a5544569ebed860217014d1f005 [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"
Spandan Dasa5e26d32024-03-06 14:04:36 +000027 "android/soong/java"
Colin Crosscb0ac952021-07-20 13:17:15 -070028
Paul Duffin375058f2019-11-29 20:17:53 +000029 "github.com/google/blueprint"
Jiyong Park9b409bc2019-10-11 14:59:13 +090030 "github.com/google/blueprint/proptools"
31
32 "android/soong/android"
Jiyong Park9b409bc2019-10-11 14:59:13 +090033)
34
Paul Duffin64fb5262021-05-05 21:36:04 +010035// Environment variables that affect the generated snapshot
36// ========================================================
37//
Paul Duffin39abf8f2021-09-24 14:58:27 +010038// SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE
39// This allows the target build release (i.e. the release version of the build within which
40// the snapshot will be used) of the snapshot to be specified. If unspecified then it defaults
41// to the current build release version. Otherwise, it must be the name of one of the build
42// releases defined in nameToBuildRelease, e.g. S, T, etc..
43//
44// The generated snapshot must only be used in the specified target release. If the target
45// build release is not the current build release then the generated Android.bp file not be
46// checked for compatibility.
47//
48// e.g. if setting SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE=S will cause the generated snapshot
49// to be compatible with S.
50//
Paul Duffin64fb5262021-05-05 21:36:04 +010051
Jiyong Park9b409bc2019-10-11 14:59:13 +090052var pctx = android.NewPackageContext("android/soong/sdk")
53
Paul Duffin375058f2019-11-29 20:17:53 +000054var (
55 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
56 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000057 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000058 CommandDeps: []string{
59 "${config.Zip2ZipCmd}",
60 },
61 },
62 "destdir")
63
64 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
65 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -070066 Command: `${config.SoongZipCmd} -C $basedir -r $out.rsp -o $out`,
Paul Duffin375058f2019-11-29 20:17:53 +000067 CommandDeps: []string{
68 "${config.SoongZipCmd}",
69 },
70 Rspfile: "$out.rsp",
71 RspfileContent: "$in",
72 },
73 "basedir")
74
75 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
76 blueprint.RuleParams{
Paul Duffin74f1dcd2022-07-18 13:18:23 +000077 Command: `${config.MergeZipsCmd} -s $out $in`,
Paul Duffin375058f2019-11-29 20:17:53 +000078 CommandDeps: []string{
79 "${config.MergeZipsCmd}",
80 },
81 })
82)
83
Paul Duffin43f7bf02021-05-05 22:00:51 +010084const (
Paul Duffinb01ac4b2022-05-24 20:10:05 +000085 soongSdkSnapshotVersionCurrent = "current"
Paul Duffin43f7bf02021-05-05 22:00:51 +010086)
87
Paul Duffinb645ec82019-11-27 17:43:54 +000088type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090089 content strings.Builder
90 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090091}
92
Paul Duffinb645ec82019-11-27 17:43:54 +000093func (gc *generatedContents) Indent() {
94 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +090095}
96
Paul Duffinb645ec82019-11-27 17:43:54 +000097func (gc *generatedContents) Dedent() {
98 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +090099}
100
Paul Duffina08e4dc2021-06-22 18:19:19 +0100101// IndentedPrintf will add spaces to indent the line to the appropriate level before printing the
102// arguments.
103func (gc *generatedContents) IndentedPrintf(format string, args ...interface{}) {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000104 _, _ = fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format, args...)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100105}
106
107// UnindentedPrintf does not add spaces to indent the line to the appropriate level before printing
108// the arguments.
109func (gc *generatedContents) UnindentedPrintf(format string, args ...interface{}) {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000110 _, _ = fmt.Fprintf(&(gc.content), format, args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900111}
112
Paul Duffin13879572019-11-28 14:31:38 +0000113// Collect all the members.
114//
Paul Duffinb97b1572021-04-29 21:50:40 +0100115// Updates the sdk module with a list of sdkMemberVariantDep instances and details as to which
116// multilibs (32/64/both) are used by this sdk variant.
Paul Duffin6a7e9532020-03-20 17:50:07 +0000117func (s *sdk) collectMembers(ctx android.ModuleContext) {
118 s.multilibUsages = multilibNone
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000119 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
120 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100121 if memberTag, ok := tag.(android.SdkMemberDependencyTag); ok {
Paul Duffineee466e2021-04-27 23:17:56 +0100122 memberType := memberTag.SdkMemberType(child)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900123
Paul Duffin5cca7c42021-05-26 10:16:01 +0100124 // If a nil SdkMemberType was returned then this module should not be added to the sdk.
125 if memberType == nil {
126 return false
127 }
128
Paul Duffin13879572019-11-28 14:31:38 +0000129 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000130 if !memberType.IsInstance(child) {
131 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900132 }
Paul Duffin13879572019-11-28 14:31:38 +0000133
Paul Duffin6a7e9532020-03-20 17:50:07 +0000134 // Keep track of which multilib variants are used by the sdk.
135 s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType)
136
Colin Cross313aa542023-12-13 13:47:44 -0800137 exportedComponentsInfo, _ := android.OtherModuleProvider(ctx, child, android.ExportedComponentsInfoProvider)
Paul Duffinb97b1572021-04-29 21:50:40 +0100138
Paul Duffin5e71e682022-11-23 18:09:54 +0000139 var container android.Module
Paul Duffinc6ba1822022-05-06 09:38:02 +0000140 if parent != ctx.Module() {
Cole Fausted158f42024-03-07 16:41:27 -0800141 container = parent
Paul Duffinc6ba1822022-05-06 09:38:02 +0000142 }
143
Paul Duffin1938dba2022-07-26 23:53:00 +0000144 minApiLevel := android.MinApiLevelForSdkSnapshot(ctx, child)
145
Paul Duffina7208112021-04-23 21:20:20 +0100146 export := memberTag.ExportMember()
Paul Duffinb97b1572021-04-29 21:50:40 +0100147 s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{
Paul Duffinc6ba1822022-05-06 09:38:02 +0000148 sdkVariant: s,
149 memberType: memberType,
Cole Fausted158f42024-03-07 16:41:27 -0800150 variant: child,
Paul Duffin1938dba2022-07-26 23:53:00 +0000151 minApiLevel: minApiLevel,
Paul Duffinc6ba1822022-05-06 09:38:02 +0000152 container: container,
153 export: export,
154 exportedComponentsInfo: exportedComponentsInfo,
Paul Duffinb97b1572021-04-29 21:50:40 +0100155 })
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000156
Paul Duffin2d3da312021-05-06 12:02:27 +0100157 // Recurse down into the member's dependencies as it may have dependencies that need to be
158 // automatically added to the sdk.
159 return true
Jiyong Park73c54ee2019-10-22 20:31:18 +0900160 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000161
162 return false
Paul Duffin13879572019-11-28 14:31:38 +0000163 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000164}
165
Paul Duffincc3132e2021-04-24 01:10:30 +0100166// groupMemberVariantsByMemberThenType groups the member variant dependencies so that all the
167// variants of each member are grouped together within an sdkMember instance.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000168//
Paul Duffincc3132e2021-04-24 01:10:30 +0100169// The sdkMember instances are then grouped into slices by member type. Within each such slice the
170// sdkMember instances appear in the order they were added as dependencies.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000171//
Paul Duffincc3132e2021-04-24 01:10:30 +0100172// Finally, the member type slices are concatenated together to form a single slice. The order in
173// which they are concatenated is the order in which the member types were registered in the
174// android.SdkMemberTypesRegistry.
Paul Duffinf861df72022-07-01 15:56:06 +0000175func (s *sdk) groupMemberVariantsByMemberThenType(ctx android.ModuleContext, targetBuildRelease *buildRelease, memberVariantDeps []sdkMemberVariantDep) []*sdkMember {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000176 byType := make(map[android.SdkMemberType][]*sdkMember)
177 byName := make(map[string]*sdkMember)
178
Paul Duffin21827262021-04-24 12:16:36 +0100179 for _, memberVariantDep := range memberVariantDeps {
180 memberType := memberVariantDep.memberType
181 variant := memberVariantDep.variant
Paul Duffin1356d8c2020-02-25 19:26:33 +0000182
183 name := ctx.OtherModuleName(variant)
184 member := byName[name]
185 if member == nil {
186 member = &sdkMember{memberType: memberType, name: name}
187 byName[name] = member
188 byType[memberType] = append(byType[memberType], member)
Liz Kammer96320df2022-05-12 20:40:00 -0400189 } else if member.memberType != memberType {
190 // validate whether this is the same member type or and overriding member type
191 if memberType.Overrides(member.memberType) {
192 member.memberType = memberType
193 } else if !member.memberType.Overrides(memberType) {
194 ctx.ModuleErrorf("Incompatible member types %q %q", member.memberType, memberType)
195 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000196 }
197
Paul Duffin1356d8c2020-02-25 19:26:33 +0000198 // Only append new variants to the list. This is needed because a member can be both
199 // exported by the sdk and also be a transitive sdk member.
200 member.variants = appendUniqueVariants(member.variants, variant)
201 }
Paul Duffin13879572019-11-28 14:31:38 +0000202 var members []*sdkMember
Paul Duffin62782de2021-07-14 12:05:16 +0100203 for _, memberListProperty := range s.memberTypeListProperties() {
Paul Duffinf861df72022-07-01 15:56:06 +0000204 memberType := memberListProperty.memberType
205
206 if !isMemberTypeSupportedByTargetBuildRelease(memberType, targetBuildRelease) {
207 continue
208 }
209
210 membersOfType := byType[memberType]
Paul Duffin13879572019-11-28 14:31:38 +0000211 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900212 }
213
Paul Duffin6a7e9532020-03-20 17:50:07 +0000214 return members
Jiyong Park73c54ee2019-10-22 20:31:18 +0900215}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900216
Paul Duffinf861df72022-07-01 15:56:06 +0000217// isMemberTypeSupportedByTargetBuildRelease returns true if the member type is supported by the
218// target build release.
219func isMemberTypeSupportedByTargetBuildRelease(memberType android.SdkMemberType, targetBuildRelease *buildRelease) bool {
220 supportedByTargetBuildRelease := true
221 supportedBuildReleases := memberType.SupportedBuildReleases()
222 if supportedBuildReleases == "" {
223 supportedBuildReleases = "S+"
224 }
225
226 set, err := parseBuildReleaseSet(supportedBuildReleases)
227 if err != nil {
228 panic(fmt.Errorf("member type %s has invalid supported build releases %q: %s",
229 memberType.SdkPropertyName(), supportedBuildReleases, err))
230 }
231 if !set.contains(targetBuildRelease) {
232 supportedByTargetBuildRelease = false
233 }
234 return supportedByTargetBuildRelease
235}
236
Paul Duffin5e71e682022-11-23 18:09:54 +0000237func appendUniqueVariants(variants []android.Module, newVariant android.Module) []android.Module {
Paul Duffin72910952020-01-20 18:16:30 +0000238 for _, v := range variants {
239 if v == newVariant {
240 return variants
241 }
242 }
243 return append(variants, newVariant)
244}
245
Paul Duffin51509a12022-04-06 12:48:09 +0000246// BUILD_NUMBER_FILE is the name of the file in the snapshot zip that will contain the number of
247// the build from which the snapshot was produced.
248const BUILD_NUMBER_FILE = "snapshot-creation-build-number.txt"
249
Jiyong Park73c54ee2019-10-22 20:31:18 +0900250// SDK directory structure
251// <sdk_root>/
252// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
253// <api_ver>/ : below this directory are all auto-generated
254// Android.bp : definition of 'sdk_snapshot' module is here
255// aidl/
256// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
257// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900258// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900259// include/
260// bionic/libc/include/stdlib.h : an exported header file
261// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900262// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900263// <arch>/include/ : arch-specific exported headers
264// <arch>/include_gen/ : arch-specific generated headers
265// <arch>/lib/
266// libFoo.so : a stub library
267
Paul Duffin1938dba2022-07-26 23:53:00 +0000268func (s sdk) targetBuildRelease(ctx android.ModuleContext) *buildRelease {
269 config := ctx.Config()
270 targetBuildReleaseEnv := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", buildReleaseCurrent.name)
271 targetBuildRelease, err := nameToRelease(targetBuildReleaseEnv)
272 if err != nil {
273 ctx.ModuleErrorf("invalid SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE: %s", err)
274 targetBuildRelease = buildReleaseCurrent
275 }
276
277 return targetBuildRelease
278}
279
Jiyong Park232e7852019-11-04 12:23:40 +0900280// buildSnapshot is the main function in this source file. It creates rules to copy
281// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffinc6ba1822022-05-06 09:38:02 +0000282func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000283
Paul Duffin1938dba2022-07-26 23:53:00 +0000284 targetBuildRelease := s.targetBuildRelease(ctx)
285 targetApiLevel, err := android.ApiLevelFromUser(ctx, targetBuildRelease.name)
286 if err != nil {
287 targetApiLevel = android.FutureApiLevel
288 }
289
Paul Duffinb97b1572021-04-29 21:50:40 +0100290 // Aggregate all the sdkMemberVariantDep instances from all the sdk variants.
Paul Duffin62131702021-05-07 01:10:01 +0100291 hasLicenses := false
Paul Duffin21827262021-04-24 12:16:36 +0100292 var memberVariantDeps []sdkMemberVariantDep
Paul Duffin1356d8c2020-02-25 19:26:33 +0000293 for _, sdkVariant := range sdkVariants {
Paul Duffin21827262021-04-24 12:16:36 +0100294 memberVariantDeps = append(memberVariantDeps, sdkVariant.memberVariantDeps...)
Paul Duffinb97b1572021-04-29 21:50:40 +0100295 }
Paul Duffin865171e2020-03-02 18:38:15 +0000296
Paul Duffinb97b1572021-04-29 21:50:40 +0100297 // Filter out any sdkMemberVariantDep that is a component of another.
298 memberVariantDeps = filterOutComponents(ctx, memberVariantDeps)
Paul Duffin13f02712020-03-06 12:30:43 +0000299
Paul Duffin1938dba2022-07-26 23:53:00 +0000300 // Record the names of all the members, both explicitly specified and implicitly included. Also,
301 // record the names of any members that should be excluded from this snapshot.
Paul Duffinb97b1572021-04-29 21:50:40 +0100302 allMembersByName := make(map[string]struct{})
303 exportedMembersByName := make(map[string]struct{})
Paul Duffin1938dba2022-07-26 23:53:00 +0000304 excludedMembersByName := make(map[string]struct{})
Paul Duffin62131702021-05-07 01:10:01 +0100305
Paul Duffin1938dba2022-07-26 23:53:00 +0000306 addMember := func(name string, export bool, exclude bool) {
307 if exclude {
308 excludedMembersByName[name] = struct{}{}
309 return
310 }
311
Paul Duffinb97b1572021-04-29 21:50:40 +0100312 allMembersByName[name] = struct{}{}
313 if export {
314 exportedMembersByName[name] = struct{}{}
315 }
316 }
317
318 for _, memberVariantDep := range memberVariantDeps {
319 name := memberVariantDep.variant.Name()
320 export := memberVariantDep.export
321
Paul Duffin1938dba2022-07-26 23:53:00 +0000322 // If the minApiLevel of the member is greater than the target API level then exclude it from
323 // this snapshot.
324 exclude := memberVariantDep.minApiLevel.GreaterThan(targetApiLevel)
Spandan Dasb84dbb22023-03-08 22:06:35 +0000325 // Always include host variants (e.g. host tools) in the snapshot.
326 // Host variants should not be guarded by a min_sdk_version check. In fact, host variants
327 // do not have a `min_sdk_version`.
328 if memberVariantDep.Host() {
329 exclude = false
330 }
Paul Duffin1938dba2022-07-26 23:53:00 +0000331
332 addMember(name, export, exclude)
Paul Duffinb97b1572021-04-29 21:50:40 +0100333
334 // Add any components provided by the module.
335 for _, component := range memberVariantDep.exportedComponentsInfo.Components {
Paul Duffin1938dba2022-07-26 23:53:00 +0000336 addMember(component, export, exclude)
Paul Duffinb97b1572021-04-29 21:50:40 +0100337 }
338
339 if memberVariantDep.memberType == android.LicenseModuleSdkMemberType {
340 hasLicenses = true
Paul Duffin865171e2020-03-02 18:38:15 +0000341 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000342 }
343
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000344 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900345
Cole Fausted158f42024-03-07 16:41:27 -0800346 bp := android.PathForModuleOut(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000347
348 bpFile := &bpFile{
349 modules: make(map[string]*bpModule),
350 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000351
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000352 // Always add -current to the end
353 snapshotFileSuffix := "-current"
Paul Duffin43f7bf02021-05-05 22:00:51 +0100354
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000355 builder := &snapshotBuilder{
Paul Duffin13f02712020-03-06 12:30:43 +0000356 ctx: ctx,
357 sdk: s,
Paul Duffin13f02712020-03-06 12:30:43 +0000358 snapshotDir: snapshotDir.OutputPath,
359 copies: make(map[string]string),
Cole Fausted158f42024-03-07 16:41:27 -0800360 filesToZip: []android.Path{bp},
Paul Duffin13f02712020-03-06 12:30:43 +0000361 bpFile: bpFile,
362 prebuiltModules: make(map[string]*bpModule),
363 allMembersByName: allMembersByName,
364 exportedMembersByName: exportedMembersByName,
Paul Duffin1938dba2022-07-26 23:53:00 +0000365 excludedMembersByName: excludedMembersByName,
Paul Duffin39abf8f2021-09-24 14:58:27 +0100366 targetBuildRelease: targetBuildRelease,
Jiyong Park73c54ee2019-10-22 20:31:18 +0900367 }
Paul Duffinac37c502019-11-26 18:02:20 +0000368 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900369
Paul Duffin62131702021-05-07 01:10:01 +0100370 // If the sdk snapshot includes any license modules then add a package module which has a
371 // default_applicable_licenses property. That will prevent the LSC license process from updating
372 // the generated Android.bp file to add a package module that includes all licenses used by all
373 // the modules in that package. That would be unnecessary as every module in the sdk should have
374 // their own licenses property specified.
375 if hasLicenses {
376 pkg := bpFile.newModule("package")
377 property := "default_applicable_licenses"
378 pkg.AddCommentForProperty(property, `
379A default list here prevents the license LSC from adding its own list which would
380be unnecessary as every module in the sdk already has its own licenses property.
381`)
382 pkg.AddProperty(property, []string{"Android-Apache-2.0"})
383 bpFile.AddModule(pkg)
384 }
385
Paul Duffin0df49682021-05-07 01:10:01 +0100386 // Group the variants for each member module together and then group the members of each member
387 // type together.
Paul Duffinf861df72022-07-01 15:56:06 +0000388 members := s.groupMemberVariantsByMemberThenType(ctx, targetBuildRelease, memberVariantDeps)
Paul Duffin0df49682021-05-07 01:10:01 +0100389
390 // Create the prebuilt modules for each of the member modules.
Paul Duffind19f8942021-07-14 12:08:37 +0100391 traits := s.gatherTraits()
Spandan Dasa5e26d32024-03-06 14:04:36 +0000392 memberNames := []string{} // soong module names of the members. contains the prebuilt_ prefix.
Paul Duffin13ad94f2020-02-19 16:19:27 +0000393 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000394 memberType := member.memberType
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000395 if !memberType.ArePrebuiltsRequired() {
396 continue
397 }
Paul Duffin3a4eb502020-03-19 16:11:18 +0000398
Paul Duffind19f8942021-07-14 12:08:37 +0100399 name := member.name
Paul Duffin1938dba2022-07-26 23:53:00 +0000400 if _, ok := excludedMembersByName[name]; ok {
401 continue
402 }
403
Paul Duffind19f8942021-07-14 12:08:37 +0100404 requiredTraits := traits[name]
405 if requiredTraits == nil {
406 requiredTraits = android.EmptySdkMemberTraitSet()
407 }
408
409 // Create the snapshot for the member.
410 memberCtx := &memberContext{ctx, builder, memberType, name, requiredTraits}
Paul Duffin3a4eb502020-03-19 16:11:18 +0000411
412 prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100413 s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
Spandan Dasa5e26d32024-03-06 14:04:36 +0000414
415 if member.memberType != android.LicenseModuleSdkMemberType && !builder.isInternalMember(member.name) {
416 // More exceptions
417 // 1. Skip BCP and SCCP fragments
418 // 2. Skip non-sdk contents of BCP and SCCP fragments
419 //
420 // The non-sdk contents of BCP/SSCP fragments should only be used for dexpreopt and hiddenapi,
421 // and are not available to the rest of the build.
422 if android.InList(member.memberType,
423 []android.SdkMemberType{
424 // bcp
425 java.BootclasspathFragmentSdkMemberType,
426 java.JavaBootLibsSdkMemberType,
427 // sscp
428 java.SystemServerClasspathFragmentSdkMemberType,
429 java.JavaSystemserverLibsSdkMemberType,
430 },
431 ) {
432 continue
433 }
434
435 memberNames = append(memberNames, android.PrebuiltNameFromSource(member.name))
436 }
437 }
438
439 // create an apex_contributions_defaults for this module's sdk.
440 // this module type is supported in V and above.
441 if targetApiLevel.GreaterThan(android.ApiLevelUpsideDownCake) {
442 ac := newModule("apex_contributions_defaults")
443 ac.AddProperty("name", s.Name()+".contributions")
444 ac.AddProperty("contents", memberNames)
445 bpFile.AddModule(ac)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900446 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900447
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000448 // Create a transformer that will transform a module by replacing any references
Paul Duffin72910952020-01-20 18:16:30 +0000449 // to internal members with a unique module name and setting prefer: false.
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000450 snapshotTransformer := snapshotTransformation{
Paul Duffin64fb5262021-05-05 21:36:04 +0100451 builder: builder,
Paul Duffin64fb5262021-05-05 21:36:04 +0100452 }
Paul Duffin72910952020-01-20 18:16:30 +0000453
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000454 for _, module := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000455 // Prune any empty property sets.
Sam Delmerico35881362023-06-30 14:40:10 -0400456 module = transformModule(module, pruneEmptySetTransformer{})
Paul Duffina78f3a72020-02-21 16:29:35 +0000457
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000458 // Transform the module module to make it suitable for use in the snapshot.
Sam Delmerico35881362023-06-30 14:40:10 -0400459 module = transformModule(module, snapshotTransformer)
460 module = transformModule(module, emptyClasspathContentsTransformation{})
461 if module != nil {
462 bpFile.AddModule(module)
463 }
Paul Duffin43f7bf02021-05-05 22:00:51 +0100464 }
Paul Duffin26197a62021-04-24 00:34:10 +0100465
466 // generate Android.bp
Cole Fausted158f42024-03-07 16:41:27 -0800467 contents := generateBpContents(bpFile)
Paul Duffin39abf8f2021-09-24 14:58:27 +0100468 // If the snapshot is being generated for the current build release then check the syntax to make
469 // sure that it is compatible.
Paul Duffin42a49f12022-08-17 22:09:55 +0000470 if targetBuildRelease == buildReleaseCurrent {
Paul Duffin39abf8f2021-09-24 14:58:27 +0100471 syntaxCheckSnapshotBpFile(ctx, contents)
472 }
Paul Duffin26197a62021-04-24 00:34:10 +0100473
Cole Fausted158f42024-03-07 16:41:27 -0800474 android.WriteFileRuleVerbatim(ctx, bp, contents)
Paul Duffin26197a62021-04-24 00:34:10 +0100475
Paul Duffin51509a12022-04-06 12:48:09 +0000476 // Copy the build number file into the snapshot.
477 builder.CopyToSnapshot(ctx.Config().BuildNumberFile(ctx), BUILD_NUMBER_FILE)
478
Paul Duffin74f1dcd2022-07-18 13:18:23 +0000479 filesToZip := android.SortedUniquePaths(builder.filesToZip)
Paul Duffin26197a62021-04-24 00:34:10 +0100480
481 // zip them all
Paul Duffinc6ba1822022-05-06 09:38:02 +0000482 zipPath := fmt.Sprintf("%s%s.zip", ctx.ModuleName(), snapshotFileSuffix)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100483 outputZipFile := android.PathForModuleOut(ctx, zipPath).OutputPath
Paul Duffin26197a62021-04-24 00:34:10 +0100484 outputDesc := "Building snapshot for " + ctx.ModuleName()
485
486 // If there are no zips to merge then generate the output zip directly.
487 // Otherwise, generate an intermediate zip file into which other zips can be
488 // merged.
489 var zipFile android.OutputPath
490 var desc string
491 if len(builder.zipsToMerge) == 0 {
492 zipFile = outputZipFile
493 desc = outputDesc
494 } else {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000495 intermediatePath := fmt.Sprintf("%s%s.unmerged.zip", ctx.ModuleName(), snapshotFileSuffix)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100496 zipFile = android.PathForModuleOut(ctx, intermediatePath).OutputPath
Paul Duffin26197a62021-04-24 00:34:10 +0100497 desc = "Building intermediate snapshot for " + ctx.ModuleName()
498 }
499
500 ctx.Build(pctx, android.BuildParams{
501 Description: desc,
502 Rule: zipFiles,
503 Inputs: filesToZip,
504 Output: zipFile,
505 Args: map[string]string{
506 "basedir": builder.snapshotDir.String(),
507 },
508 })
509
510 if len(builder.zipsToMerge) != 0 {
511 ctx.Build(pctx, android.BuildParams{
512 Description: outputDesc,
513 Rule: mergeZips,
514 Input: zipFile,
Paul Duffin74f1dcd2022-07-18 13:18:23 +0000515 Inputs: android.SortedUniquePaths(builder.zipsToMerge),
Paul Duffin26197a62021-04-24 00:34:10 +0100516 Output: outputZipFile,
517 })
518 }
519
Paul Duffinc6ba1822022-05-06 09:38:02 +0000520 modules := s.generateInfoData(ctx, memberVariantDeps)
521
522 // Output the modules information as pretty printed JSON.
Cole Fausted158f42024-03-07 16:41:27 -0800523 info := android.PathForModuleOut(ctx, fmt.Sprintf("%s%s.info", ctx.ModuleName(), snapshotFileSuffix))
Paul Duffinc6ba1822022-05-06 09:38:02 +0000524 output, err := json.MarshalIndent(modules, "", " ")
525 if err != nil {
526 ctx.ModuleErrorf("error generating %q: %s", info, err)
527 }
528 builder.infoContents = string(output)
Cole Fausted158f42024-03-07 16:41:27 -0800529 android.WriteFileRuleVerbatim(ctx, info, builder.infoContents)
530 installedInfo := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), info.Base(), info)
Paul Duffinc6ba1822022-05-06 09:38:02 +0000531 s.infoFile = android.OptionalPathForPath(installedInfo)
532
533 // Install the zip, making sure that the info file has been installed as well.
534 installedZip := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), outputZipFile.Base(), outputZipFile, installedInfo)
535 s.snapshotFile = android.OptionalPathForPath(installedZip)
536}
537
538type moduleInfo struct {
539 // The type of the module, e.g. java_sdk_library
540 moduleType string
541 // The name of the module.
542 name string
543 // A list of additional dependencies of the module.
544 deps []string
Paul Duffin958806b2022-05-16 13:10:47 +0000545 // Additional member specific properties.
546 // These will be added into the generated JSON alongside the above properties.
547 memberSpecific map[string]interface{}
Paul Duffinc6ba1822022-05-06 09:38:02 +0000548}
549
550func (m *moduleInfo) MarshalJSON() ([]byte, error) {
551 buffer := bytes.Buffer{}
552
553 separator := ""
554 writeObjectPair := func(key string, value interface{}) {
555 buffer.WriteString(fmt.Sprintf("%s%q: ", separator, key))
556 b, err := json.Marshal(value)
557 if err != nil {
558 panic(err)
559 }
560 buffer.Write(b)
561 separator = ","
562 }
563
564 buffer.WriteString("{")
565 writeObjectPair("@type", m.moduleType)
566 writeObjectPair("@name", m.name)
567 if m.deps != nil {
568 writeObjectPair("@deps", m.deps)
569 }
Cole Faust18994c72023-02-28 16:02:16 -0800570 for _, k := range android.SortedKeys(m.memberSpecific) {
Paul Duffin958806b2022-05-16 13:10:47 +0000571 v := m.memberSpecific[k]
Paul Duffinc6ba1822022-05-06 09:38:02 +0000572 writeObjectPair(k, v)
573 }
574 buffer.WriteString("}")
575 return buffer.Bytes(), nil
576}
577
578var _ json.Marshaler = (*moduleInfo)(nil)
579
580// generateInfoData creates a list of moduleInfo structures that will be marshalled into JSON.
581func (s *sdk) generateInfoData(ctx android.ModuleContext, memberVariantDeps []sdkMemberVariantDep) interface{} {
582 modules := []*moduleInfo{}
583 sdkInfo := moduleInfo{
Paul Duffin958806b2022-05-16 13:10:47 +0000584 moduleType: "sdk",
585 name: ctx.ModuleName(),
586 memberSpecific: map[string]interface{}{},
Paul Duffinc6ba1822022-05-06 09:38:02 +0000587 }
588 modules = append(modules, &sdkInfo)
589
590 name2Info := map[string]*moduleInfo{}
591 getModuleInfo := func(module android.Module) *moduleInfo {
592 name := module.Name()
593 info := name2Info[name]
594 if info == nil {
595 moduleType := ctx.OtherModuleType(module)
596 // Remove any suffix added when creating modules dynamically.
597 moduleType = strings.Split(moduleType, "__")[0]
598 info = &moduleInfo{
599 moduleType: moduleType,
600 name: name,
601 }
Paul Duffin958806b2022-05-16 13:10:47 +0000602
Colin Cross313aa542023-12-13 13:47:44 -0800603 additionalSdkInfo, _ := android.OtherModuleProvider(ctx, module, android.AdditionalSdkInfoProvider)
Paul Duffin958806b2022-05-16 13:10:47 +0000604 info.memberSpecific = additionalSdkInfo.Properties
605
Paul Duffinc6ba1822022-05-06 09:38:02 +0000606 name2Info[name] = info
607 }
608 return info
609 }
610
611 for _, memberVariantDep := range memberVariantDeps {
612 propertyName := memberVariantDep.memberType.SdkPropertyName()
613 var list []string
Paul Duffin958806b2022-05-16 13:10:47 +0000614 if v, ok := sdkInfo.memberSpecific[propertyName]; ok {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000615 list = v.([]string)
616 }
617
618 memberName := memberVariantDep.variant.Name()
619 list = append(list, memberName)
Paul Duffin958806b2022-05-16 13:10:47 +0000620 sdkInfo.memberSpecific[propertyName] = android.SortedUniqueStrings(list)
Paul Duffinc6ba1822022-05-06 09:38:02 +0000621
622 if memberVariantDep.container != nil {
623 containerInfo := getModuleInfo(memberVariantDep.container)
624 containerInfo.deps = android.SortedUniqueStrings(append(containerInfo.deps, memberName))
625 }
626
627 // Make sure that the module info is created for each module.
628 getModuleInfo(memberVariantDep.variant)
629 }
630
Cole Faust18994c72023-02-28 16:02:16 -0800631 for _, memberName := range android.SortedKeys(name2Info) {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000632 info := name2Info[memberName]
633 modules = append(modules, info)
634 }
635
636 return modules
Paul Duffin26197a62021-04-24 00:34:10 +0100637}
638
Paul Duffinb97b1572021-04-29 21:50:40 +0100639// filterOutComponents removes any item from the deps list that is a component of another item in
640// the deps list, e.g. if the deps list contains "foo" and "foo.stubs" which is component of "foo"
641// then it will remove "foo.stubs" from the deps.
642func filterOutComponents(ctx android.ModuleContext, deps []sdkMemberVariantDep) []sdkMemberVariantDep {
643 // Collate the set of components that all the modules added to the sdk provide.
644 components := map[string]*sdkMemberVariantDep{}
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000645 for i := range deps {
Paul Duffinb97b1572021-04-29 21:50:40 +0100646 dep := &deps[i]
647 for _, c := range dep.exportedComponentsInfo.Components {
648 components[c] = dep
649 }
650 }
651
652 // If no module provides components then return the input deps unfiltered.
653 if len(components) == 0 {
654 return deps
655 }
656
657 filtered := make([]sdkMemberVariantDep, 0, len(deps))
658 for _, dep := range deps {
659 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(dep.variant))
660 if owner, ok := components[name]; ok {
661 // This is a component of another module that is a member of the sdk.
662
663 // If the component is exported but the owning module is not then the configuration is not
664 // supported.
665 if dep.export && !owner.export {
666 ctx.ModuleErrorf("Module %s is internal to the SDK but provides component %s which is used outside the SDK")
667 continue
668 }
669
670 // This module must not be added to the list of members of the sdk as that would result in a
671 // duplicate module in the sdk snapshot.
672 continue
673 }
674
675 filtered = append(filtered, dep)
676 }
677 return filtered
678}
679
Paul Duffinf88d8e02020-05-07 20:21:34 +0100680// Check the syntax of the generated Android.bp file contents and if they are
681// invalid then log an error with the contents (tagged with line numbers) and the
682// errors that were found so that it is easy to see where the problem lies.
683func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) {
684 errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents)
685 if len(errs) != 0 {
686 message := &strings.Builder{}
687 _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot:
688
689Generated Android.bp contents
690========================================================================
691`)
692 for i, line := range strings.Split(contents, "\n") {
693 _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line)
694 }
695
696 _, _ = fmt.Fprint(message, `
697========================================================================
698
699Errors found:
700`)
701
702 for _, err := range errs {
703 _, _ = fmt.Fprintf(message, "%s\n", err.Error())
704 }
705
706 ctx.ModuleErrorf("%s", message.String())
707 }
708}
709
Paul Duffin4b8b7932020-05-06 12:35:38 +0100710func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
711 err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
712 if err != nil {
713 ctx.ModuleErrorf("error extracting common properties: %s", err)
714 }
715}
716
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000717type propertyTag struct {
718 name string
719}
720
Paul Duffin94289702021-09-09 15:38:32 +0100721var _ android.BpPropertyTag = propertyTag{}
722
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000723// BpPropertyTag instances to add to a property that contains references to other sdk members.
Paul Duffin0cb37b92020-03-04 14:52:46 +0000724//
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000725// These will ensure that the referenced modules are available, if required.
Paul Duffin13f02712020-03-06 12:30:43 +0000726var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
Paul Duffin13f02712020-03-06 12:30:43 +0000727var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000728
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000729type snapshotTransformation struct {
Paul Duffine6c0d842020-01-15 14:08:51 +0000730 identityTransformation
731 builder *snapshotBuilder
732}
733
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000734func (t snapshotTransformation) transformModule(module *bpModule) *bpModule {
Sam Delmerico35881362023-06-30 14:40:10 -0400735 if module != nil {
736 // If the module is an internal member then use a unique name for it.
737 name := module.Name()
738 module.setProperty("name", t.builder.snapshotSdkMemberName(name, true))
739 }
Paul Duffin72910952020-01-20 18:16:30 +0000740 return module
741}
742
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000743func (t snapshotTransformation) transformProperty(_ string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000744 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
745 required := tag == requiredSdkMemberReferencePropertyTag
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000746 return t.builder.snapshotSdkMemberNames(value.([]string), required), tag
Paul Duffin72910952020-01-20 18:16:30 +0000747 } else {
748 return value, tag
749 }
750}
751
Sam Delmerico35881362023-06-30 14:40:10 -0400752type emptyClasspathContentsTransformation struct {
753 identityTransformation
754}
755
756func (t emptyClasspathContentsTransformation) transformModule(module *bpModule) *bpModule {
757 classpathModuleTypes := []string{
758 "prebuilt_bootclasspath_fragment",
759 "prebuilt_systemserverclasspath_fragment",
760 }
761 if module != nil && android.InList(module.moduleType, classpathModuleTypes) {
762 if contents, ok := module.bpPropertySet.properties["contents"].([]string); ok {
763 if len(contents) == 0 {
764 return nil
765 }
766 }
767 }
768 return module
769}
770
Paul Duffina78f3a72020-02-21 16:29:35 +0000771type pruneEmptySetTransformer struct {
772 identityTransformation
773}
774
775var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
776
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000777func (t pruneEmptySetTransformer) transformPropertySetAfterContents(_ string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
Paul Duffina78f3a72020-02-21 16:29:35 +0000778 if len(propertySet.properties) == 0 {
779 return nil, nil
780 } else {
781 return propertySet, tag
782 }
783}
784
Cole Fausted158f42024-03-07 16:41:27 -0800785func generateBpContents(bpFile *bpFile) string {
786 contents := &generatedContents{}
Paul Duffina08e4dc2021-06-22 18:19:19 +0100787 contents.IndentedPrintf("// This is auto-generated. DO NOT EDIT.\n")
Paul Duffinb645ec82019-11-27 17:43:54 +0000788 for _, bpModule := range bpFile.order {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000789 contents.IndentedPrintf("\n")
790 contents.IndentedPrintf("%s {\n", bpModule.moduleType)
791 outputPropertySet(contents, bpModule.bpPropertySet)
792 contents.IndentedPrintf("}\n")
Paul Duffinb645ec82019-11-27 17:43:54 +0000793 }
Cole Fausted158f42024-03-07 16:41:27 -0800794 return contents.content.String()
Paul Duffinb645ec82019-11-27 17:43:54 +0000795}
796
797func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
798 contents.Indent()
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000799
Paul Duffin0df49682021-05-07 01:10:01 +0100800 addComment := func(name string) {
801 if text, ok := set.comments[name]; ok {
802 for _, line := range strings.Split(text, "\n") {
Paul Duffina08e4dc2021-06-22 18:19:19 +0100803 contents.IndentedPrintf("// %s\n", line)
Paul Duffin0df49682021-05-07 01:10:01 +0100804 }
805 }
806 }
807
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000808 // Output the properties first, followed by the nested sets. This ensures a
809 // consistent output irrespective of whether property sets are created before
810 // or after the properties. This simplifies the creation of the module.
Paul Duffinb645ec82019-11-27 17:43:54 +0000811 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000812 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000813
Paul Duffin0df49682021-05-07 01:10:01 +0100814 // Do not write property sets in the properties phase.
815 if _, ok := value.(*bpPropertySet); ok {
816 continue
817 }
818
819 addComment(name)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100820 reflectValue := reflect.ValueOf(value)
821 outputNamedValue(contents, name, reflectValue)
Paul Duffinb645ec82019-11-27 17:43:54 +0000822 }
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000823
824 for _, name := range set.order {
825 value := set.getValue(name)
826
827 // Only write property sets in the sets phase.
828 switch v := value.(type) {
829 case *bpPropertySet:
Paul Duffin0df49682021-05-07 01:10:01 +0100830 addComment(name)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100831 contents.IndentedPrintf("%s: {\n", name)
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000832 outputPropertySet(contents, v)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100833 contents.IndentedPrintf("},\n")
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000834 }
835 }
836
Paul Duffinb645ec82019-11-27 17:43:54 +0000837 contents.Dedent()
838}
839
Paul Duffina08e4dc2021-06-22 18:19:19 +0100840// outputNamedValue outputs a value that has an associated name. The name will be indented, followed
841// by the value and then followed by a , and a newline.
842func outputNamedValue(contents *generatedContents, name string, value reflect.Value) {
843 contents.IndentedPrintf("%s: ", name)
844 outputUnnamedValue(contents, value)
845 contents.UnindentedPrintf(",\n")
846}
847
848// outputUnnamedValue outputs a single value. The value is not indented and is not followed by
849// either a , or a newline. With multi-line values, e.g. slices, all but the first line will be
850// indented and all but the last line will end with a newline.
851func outputUnnamedValue(contents *generatedContents, value reflect.Value) {
852 valueType := value.Type()
853 switch valueType.Kind() {
854 case reflect.Bool:
855 contents.UnindentedPrintf("%t", value.Bool())
856
857 case reflect.String:
858 contents.UnindentedPrintf("%q", value)
859
Paul Duffin51227d82021-05-18 12:54:27 +0100860 case reflect.Ptr:
861 outputUnnamedValue(contents, value.Elem())
862
Paul Duffina08e4dc2021-06-22 18:19:19 +0100863 case reflect.Slice:
864 length := value.Len()
865 if length == 0 {
866 contents.UnindentedPrintf("[]")
Paul Duffina08e4dc2021-06-22 18:19:19 +0100867 } else {
Paul Duffin51227d82021-05-18 12:54:27 +0100868 firstValue := value.Index(0)
869 if length == 1 && !multiLineValue(firstValue) {
870 contents.UnindentedPrintf("[")
871 outputUnnamedValue(contents, firstValue)
872 contents.UnindentedPrintf("]")
873 } else {
874 contents.UnindentedPrintf("[\n")
875 contents.Indent()
876 for i := 0; i < length; i++ {
877 itemValue := value.Index(i)
878 contents.IndentedPrintf("")
879 outputUnnamedValue(contents, itemValue)
880 contents.UnindentedPrintf(",\n")
881 }
882 contents.Dedent()
883 contents.IndentedPrintf("]")
Paul Duffina08e4dc2021-06-22 18:19:19 +0100884 }
Paul Duffina08e4dc2021-06-22 18:19:19 +0100885 }
886
Paul Duffin51227d82021-05-18 12:54:27 +0100887 case reflect.Struct:
888 // Avoid unlimited recursion by requiring every structure to implement android.BpPrintable.
889 v := value.Interface()
890 if _, ok := v.(android.BpPrintable); !ok {
891 panic(fmt.Errorf("property value %#v of type %T does not implement android.BpPrintable", v, v))
892 }
893 contents.UnindentedPrintf("{\n")
894 contents.Indent()
895 for f := 0; f < valueType.NumField(); f++ {
896 fieldType := valueType.Field(f)
897 if fieldType.Anonymous {
898 continue
899 }
900 fieldValue := value.Field(f)
901 fieldName := fieldType.Name
902 propertyName := proptools.PropertyNameForField(fieldName)
903 outputNamedValue(contents, propertyName, fieldValue)
904 }
905 contents.Dedent()
906 contents.IndentedPrintf("}")
907
Paul Duffina08e4dc2021-06-22 18:19:19 +0100908 default:
Cole Faust8c366312024-03-08 10:58:32 -0800909 panic(fmt.Errorf("unknown type: %T of value %#v", value, value))
Paul Duffina08e4dc2021-06-22 18:19:19 +0100910 }
911}
912
Paul Duffin51227d82021-05-18 12:54:27 +0100913// multiLineValue returns true if the supplied value may require multiple lines in the output.
914func multiLineValue(value reflect.Value) bool {
915 kind := value.Kind()
916 return kind == reflect.Slice || kind == reflect.Struct
917}
918
Paul Duffinac37c502019-11-26 18:02:20 +0000919func (s *sdk) GetAndroidBpContentsForTests() string {
Cole Fausted158f42024-03-07 16:41:27 -0800920 return generateBpContents(s.builderForTests.bpFile)
Paul Duffinac37c502019-11-26 18:02:20 +0000921}
922
Paul Duffinc6ba1822022-05-06 09:38:02 +0000923func (s *sdk) GetInfoContentsForTests() string {
924 return s.builderForTests.infoContents
925}
926
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000927type snapshotBuilder struct {
Paul Duffin43f7bf02021-05-05 22:00:51 +0100928 ctx android.ModuleContext
929 sdk *sdk
930
Paul Duffinb645ec82019-11-27 17:43:54 +0000931 snapshotDir android.OutputPath
932 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000933
934 // Map from destination to source of each copy - used to eliminate duplicates and
935 // detect conflicts.
936 copies map[string]string
937
Paul Duffinb645ec82019-11-27 17:43:54 +0000938 filesToZip android.Paths
939 zipsToMerge android.Paths
940
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000941 // The path to an empty file.
942 emptyFile android.WritablePath
943
Paul Duffinb645ec82019-11-27 17:43:54 +0000944 prebuiltModules map[string]*bpModule
945 prebuiltOrder []*bpModule
Paul Duffin13f02712020-03-06 12:30:43 +0000946
947 // The set of all members by name.
948 allMembersByName map[string]struct{}
949
950 // The set of exported members by name.
951 exportedMembersByName map[string]struct{}
Paul Duffin39abf8f2021-09-24 14:58:27 +0100952
Paul Duffin1938dba2022-07-26 23:53:00 +0000953 // The set of members which have been excluded from this snapshot; by name.
954 excludedMembersByName map[string]struct{}
955
Paul Duffin39abf8f2021-09-24 14:58:27 +0100956 // The target build release for which the snapshot is to be generated.
957 targetBuildRelease *buildRelease
Paul Duffinc6ba1822022-05-06 09:38:02 +0000958
Paul Duffin958806b2022-05-16 13:10:47 +0000959 // The contents of the .info file that describes the sdk contents.
Paul Duffinc6ba1822022-05-06 09:38:02 +0000960 infoContents string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000961}
962
963func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000964 if existing, ok := s.copies[dest]; ok {
965 if existing != src.String() {
966 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
967 return
968 }
969 } else {
970 path := s.snapshotDir.Join(s.ctx, dest)
971 s.ctx.Build(pctx, android.BuildParams{
972 Rule: android.Cp,
973 Input: src,
974 Output: path,
975 })
976 s.filesToZip = append(s.filesToZip, path)
977
978 s.copies[dest] = src.String()
979 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000980}
981
Paul Duffin91547182019-11-12 19:39:36 +0000982func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
983 ctx := s.ctx
984
985 // Repackage the zip file so that the entries are in the destDir directory.
986 // This will allow the zip file to be merged into the snapshot.
987 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000988
989 ctx.Build(pctx, android.BuildParams{
990 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
991 Rule: repackageZip,
992 Input: zipPath,
993 Output: tmpZipPath,
994 Args: map[string]string{
995 "destdir": destDir,
996 },
997 })
Paul Duffin91547182019-11-12 19:39:36 +0000998
999 // Add the repackaged zip file to the files to merge.
1000 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
1001}
1002
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001003func (s *snapshotBuilder) EmptyFile() android.Path {
1004 if s.emptyFile == nil {
1005 ctx := s.ctx
1006 s.emptyFile = android.PathForModuleOut(ctx, "empty")
1007 s.ctx.Build(pctx, android.BuildParams{
1008 Rule: android.Touch,
1009 Output: s.emptyFile,
1010 })
1011 }
1012
1013 return s.emptyFile
1014}
1015
Paul Duffin9d8d6092019-12-05 18:19:29 +00001016func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
1017 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +00001018 if s.prebuiltModules[name] != nil {
1019 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
1020 }
1021
1022 m := s.bpFile.newModule(moduleType)
1023 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +00001024
Paul Duffinbefa4b92020-03-04 14:22:45 +00001025 variant := member.Variants()[0]
1026
Paul Duffin13f02712020-03-06 12:30:43 +00001027 if s.isInternalMember(name) {
Paul Duffin72910952020-01-20 18:16:30 +00001028 // An internal member is only referenced from the sdk snapshot which is in the
1029 // same package so can be marked as private.
1030 m.AddProperty("visibility", []string{"//visibility:private"})
1031 } else {
1032 // Extract visibility information from a member variant. All variants have the same
1033 // visibility so it doesn't matter which one is used.
Paul Duffin157f40f2020-09-29 16:01:08 +01001034 visibilityRules := android.EffectiveVisibilityRules(s.ctx, variant)
1035
1036 // Add any additional visibility rules needed for the prebuilts to reference each other.
1037 err := visibilityRules.Widen(s.sdk.properties.Prebuilt_visibility)
1038 if err != nil {
1039 s.ctx.PropertyErrorf("prebuilt_visibility", "%s", err)
1040 }
1041
1042 visibility := visibilityRules.Strings()
Paul Duffin72910952020-01-20 18:16:30 +00001043 if len(visibility) != 0 {
1044 m.AddProperty("visibility", visibility)
1045 }
Paul Duffin593b3c92019-12-05 14:31:48 +00001046 }
1047
Martin Stjernholm1e041092020-11-03 00:11:09 +00001048 // Where available copy apex_available properties from the member.
1049 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
1050 apexAvailable := apexAware.ApexAvailable()
1051 if len(apexAvailable) == 0 {
1052 // //apex_available:platform is the default.
1053 apexAvailable = []string{android.AvailableToPlatform}
1054 }
1055
1056 // Add in any baseline apex available settings.
1057 apexAvailable = append(apexAvailable, apex.BaselineApexAvailable(member.Name())...)
1058
1059 // Remove duplicates and sort.
1060 apexAvailable = android.FirstUniqueStrings(apexAvailable)
1061 sort.Strings(apexAvailable)
1062
1063 m.AddProperty("apex_available", apexAvailable)
1064 }
1065
Paul Duffinb0bb3762021-05-06 16:48:05 +01001066 // The licenses are the same for all variants.
1067 mctx := s.ctx
Colin Cross313aa542023-12-13 13:47:44 -08001068 licenseInfo, _ := android.OtherModuleProvider(mctx, variant, android.LicenseInfoProvider)
Paul Duffinb0bb3762021-05-06 16:48:05 +01001069 if len(licenseInfo.Licenses) > 0 {
1070 m.AddPropertyWithTag("licenses", licenseInfo.Licenses, s.OptionalSdkMemberReferencePropertyTag())
1071 }
1072
Paul Duffin865171e2020-03-02 18:38:15 +00001073 deviceSupported := false
1074 hostSupported := false
1075
1076 for _, variant := range member.Variants() {
1077 osClass := variant.Target().Os.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001078 if osClass == android.Host {
Paul Duffin865171e2020-03-02 18:38:15 +00001079 hostSupported = true
1080 } else if osClass == android.Device {
1081 deviceSupported = true
1082 }
1083 }
1084
1085 addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
Paul Duffinb645ec82019-11-27 17:43:54 +00001086
1087 s.prebuiltModules[name] = m
1088 s.prebuiltOrder = append(s.prebuiltOrder, m)
1089 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001090}
1091
Paul Duffin865171e2020-03-02 18:38:15 +00001092func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
Paul Duffinb0bb3762021-05-06 16:48:05 +01001093 // If neither device or host is supported then this module does not support either so will not
1094 // recognize the properties.
1095 if !deviceSupported && !hostSupported {
1096 return
1097 }
1098
Paul Duffin865171e2020-03-02 18:38:15 +00001099 if !deviceSupported {
Paul Duffine44358f2019-11-26 18:04:12 +00001100 bpModule.AddProperty("device_supported", false)
1101 }
Paul Duffin865171e2020-03-02 18:38:15 +00001102 if hostSupported {
Paul Duffine44358f2019-11-26 18:04:12 +00001103 bpModule.AddProperty("host_supported", true)
1104 }
1105}
1106
Paul Duffin13f02712020-03-06 12:30:43 +00001107func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag {
1108 if required {
1109 return requiredSdkMemberReferencePropertyTag
1110 } else {
1111 return optionalSdkMemberReferencePropertyTag
1112 }
1113}
1114
1115func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag {
1116 return optionalSdkMemberReferencePropertyTag
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001117}
1118
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001119// Get a name for sdk snapshot member. If the member is private then generate a snapshot specific
1120// name. As part of the processing this checks to make sure that any required members are part of
1121// the snapshot.
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001122func (s *snapshotBuilder) snapshotSdkMemberName(name string, required bool) string {
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001123 if _, ok := s.allMembersByName[name]; !ok {
Paul Duffin13f02712020-03-06 12:30:43 +00001124 if required {
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001125 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", name)
Paul Duffin13f02712020-03-06 12:30:43 +00001126 }
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001127 return name
Paul Duffin13f02712020-03-06 12:30:43 +00001128 }
1129
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001130 if s.isInternalMember(name) {
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001131 return s.ctx.ModuleName() + "_" + name
Paul Duffin72910952020-01-20 18:16:30 +00001132 } else {
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001133 return name
Paul Duffin72910952020-01-20 18:16:30 +00001134 }
1135}
1136
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001137func (s *snapshotBuilder) snapshotSdkMemberNames(members []string, required bool) []string {
Paul Duffin72910952020-01-20 18:16:30 +00001138 var references []string = nil
1139 for _, m := range members {
Paul Duffin1938dba2022-07-26 23:53:00 +00001140 if _, ok := s.excludedMembersByName[m]; ok {
1141 continue
1142 }
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001143 references = append(references, s.snapshotSdkMemberName(m, required))
Paul Duffin72910952020-01-20 18:16:30 +00001144 }
1145 return references
1146}
1147
Paul Duffin13f02712020-03-06 12:30:43 +00001148func (s *snapshotBuilder) isInternalMember(memberName string) bool {
1149 _, ok := s.exportedMembersByName[memberName]
1150 return !ok
1151}
1152
Martin Stjernholm89238f42020-07-10 00:14:03 +01001153// Add the properties from the given SdkMemberProperties to the blueprint
1154// property set. This handles common properties in SdkMemberPropertiesBase and
1155// calls the member-specific AddToPropertySet for the rest.
1156func addSdkMemberPropertiesToSet(ctx *memberContext, memberProperties android.SdkMemberProperties, targetPropertySet android.BpPropertySet) {
1157 if memberProperties.Base().Compile_multilib != "" {
1158 targetPropertySet.AddProperty("compile_multilib", memberProperties.Base().Compile_multilib)
1159 }
1160
1161 memberProperties.AddToPropertySet(ctx, targetPropertySet)
1162}
1163
Paul Duffin21827262021-04-24 12:16:36 +01001164// sdkMemberVariantDep represents a dependency from an sdk variant onto a member variant.
1165type sdkMemberVariantDep struct {
Paul Duffincd064672021-04-24 00:47:29 +01001166 // The sdk variant that depends (possibly indirectly) on the member variant.
1167 sdkVariant *sdk
Paul Duffinb97b1572021-04-29 21:50:40 +01001168
1169 // The type of sdk member the variant is to be treated as.
Paul Duffin1356d8c2020-02-25 19:26:33 +00001170 memberType android.SdkMemberType
Paul Duffinb97b1572021-04-29 21:50:40 +01001171
1172 // The variant that is added to the sdk.
Paul Duffin5e71e682022-11-23 18:09:54 +00001173 variant android.Module
Paul Duffinb97b1572021-04-29 21:50:40 +01001174
Paul Duffinc6ba1822022-05-06 09:38:02 +00001175 // The optional container of this member, i.e. the module that is depended upon by the sdk
1176 // (possibly transitively) and whose dependency on this module is why it was added to the sdk.
1177 // Is nil if this a direct dependency of the sdk.
Paul Duffin5e71e682022-11-23 18:09:54 +00001178 container android.Module
Paul Duffinc6ba1822022-05-06 09:38:02 +00001179
Paul Duffinb97b1572021-04-29 21:50:40 +01001180 // True if the member should be exported, i.e. accessible, from outside the sdk.
1181 export bool
1182
1183 // The names of additional component modules provided by the variant.
1184 exportedComponentsInfo android.ExportedComponentsInfo
Paul Duffin1938dba2022-07-26 23:53:00 +00001185
1186 // The minimum API level on which this module is supported.
1187 minApiLevel android.ApiLevel
Paul Duffin1356d8c2020-02-25 19:26:33 +00001188}
1189
Spandan Dasb84dbb22023-03-08 22:06:35 +00001190// Host returns true if the sdk member is a host variant (e.g. host tool)
1191func (s *sdkMemberVariantDep) Host() bool {
1192 return s.variant.Target().Os.Class == android.Host
1193}
1194
Paul Duffin13879572019-11-28 14:31:38 +00001195var _ android.SdkMember = (*sdkMember)(nil)
1196
Paul Duffin21827262021-04-24 12:16:36 +01001197// sdkMember groups all the variants of a specific member module together along with the name of the
1198// module and the member type. This is used to generate the prebuilt modules for a specific member.
Paul Duffin13879572019-11-28 14:31:38 +00001199type sdkMember struct {
1200 memberType android.SdkMemberType
1201 name string
Paul Duffin5e71e682022-11-23 18:09:54 +00001202 variants []android.Module
Paul Duffin13879572019-11-28 14:31:38 +00001203}
1204
1205func (m *sdkMember) Name() string {
1206 return m.name
1207}
1208
Paul Duffin5e71e682022-11-23 18:09:54 +00001209func (m *sdkMember) Variants() []android.Module {
Paul Duffin13879572019-11-28 14:31:38 +00001210 return m.variants
1211}
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001212
Paul Duffin9c3760e2020-03-16 19:52:08 +00001213// Track usages of multilib variants.
1214type multilibUsage int
1215
1216const (
1217 multilibNone multilibUsage = 0
1218 multilib32 multilibUsage = 1
1219 multilib64 multilibUsage = 2
1220 multilibBoth = multilib32 | multilib64
1221)
1222
1223// Add the multilib that is used in the arch type.
1224func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage {
1225 multilib := archType.Multilib
1226 switch multilib {
1227 case "":
1228 return m
1229 case "lib32":
1230 return m | multilib32
1231 case "lib64":
1232 return m | multilib64
1233 default:
Cole Faust8c366312024-03-08 10:58:32 -08001234 panic(fmt.Errorf("unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib))
Paul Duffin9c3760e2020-03-16 19:52:08 +00001235 }
1236}
1237
1238func (m multilibUsage) String() string {
1239 switch m {
1240 case multilibNone:
1241 return ""
1242 case multilib32:
1243 return "32"
1244 case multilib64:
1245 return "64"
1246 case multilibBoth:
1247 return "both"
1248 default:
Cole Faust8c366312024-03-08 10:58:32 -08001249 panic(fmt.Errorf("unknown multilib value, found %b, expected one of %b, %b, %b or %b",
Paul Duffin9c3760e2020-03-16 19:52:08 +00001250 m, multilibNone, multilib32, multilib64, multilibBoth))
1251 }
1252}
1253
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001254// TODO(187910671): BEGIN - Remove once modules do not have an APEX and default variant.
1255// variantCoordinate contains the coordinates used to identify a variant of an SDK member.
1256type variantCoordinate struct {
1257 // osType identifies the OS target of a variant.
1258 osType android.OsType
1259 // archId identifies the architecture and whether it is for the native bridge.
1260 archId archId
1261 // image is the image variant name.
1262 image string
1263 // linkType is the link type name.
1264 linkType string
1265}
1266
1267func getVariantCoordinate(ctx *memberContext, variant android.Module) variantCoordinate {
1268 linkType := ""
1269 if len(ctx.MemberType().SupportedLinkages()) > 0 {
1270 linkType = getLinkType(variant)
1271 }
1272 return variantCoordinate{
1273 osType: variant.Target().Os,
1274 archId: archIdFromTarget(variant.Target()),
1275 image: variant.ImageVariation().Variation,
1276 linkType: linkType,
1277 }
1278}
1279
1280// selectApexVariantsWhereAvailable filters the input list of variants by selecting the APEX
1281// specific variant for a specific variantCoordinate when there is both an APEX and default variant.
1282//
1283// There is a long-standing issue where a module that is added to an APEX has both an APEX and
1284// default/platform variant created even when the module does not require a platform variant. As a
1285// result an indirect dependency onto a module via the APEX will use the APEX variant, whereas a
1286// direct dependency onto the module will use the default/platform variant. That would result in a
1287// failure while attempting to optimize the properties for a member as it would have two variants
1288// when only one was expected.
1289//
1290// This function mitigates that problem by detecting when there are two variants that differ only
1291// by apex variant, where one is the default/platform variant and one is the APEX variant. In that
1292// case it picks the APEX variant. It picks the APEX variant because that is the behavior that would
1293// be expected
Paul Duffin5e71e682022-11-23 18:09:54 +00001294func selectApexVariantsWhereAvailable(ctx *memberContext, variants []android.Module) []android.Module {
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001295 moduleCtx := ctx.sdkMemberContext
1296
1297 // Group the variants by coordinates.
Paul Duffin5e71e682022-11-23 18:09:54 +00001298 variantsByCoord := make(map[variantCoordinate][]android.Module)
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001299 for _, variant := range variants {
1300 coord := getVariantCoordinate(ctx, variant)
1301 variantsByCoord[coord] = append(variantsByCoord[coord], variant)
1302 }
1303
Paul Duffin5e71e682022-11-23 18:09:54 +00001304 toDiscard := make(map[android.Module]struct{})
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001305 for coord, list := range variantsByCoord {
1306 count := len(list)
1307 if count == 1 {
1308 continue
1309 }
1310
Paul Duffin5e71e682022-11-23 18:09:54 +00001311 variantsByApex := make(map[string]android.Module)
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001312 conflictDetected := false
1313 for _, variant := range list {
Colin Cross313aa542023-12-13 13:47:44 -08001314 apexInfo, _ := android.OtherModuleProvider(moduleCtx, variant, android.ApexInfoProvider)
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001315 apexVariationName := apexInfo.ApexVariationName
1316 // If there are two variants for a specific APEX variation then there is conflict.
1317 if _, ok := variantsByApex[apexVariationName]; ok {
1318 conflictDetected = true
1319 break
1320 }
1321 variantsByApex[apexVariationName] = variant
1322 }
1323
1324 // If there are more than 2 apex variations or one of the apex variations is not the
1325 // default/platform variation then there is a conflict.
1326 if len(variantsByApex) != 2 {
1327 conflictDetected = true
1328 } else if _, ok := variantsByApex[""]; !ok {
1329 conflictDetected = true
1330 }
1331
1332 // If there are no conflicts then add the default/platform variation to the list to remove.
1333 if !conflictDetected {
1334 toDiscard[variantsByApex[""]] = struct{}{}
1335 continue
1336 }
1337
1338 // There are duplicate variants at this coordinate and they are not the default and APEX variant
1339 // so fail.
1340 variantDescriptions := []string{}
1341 for _, m := range list {
1342 variantDescriptions = append(variantDescriptions, fmt.Sprintf(" %s", m.String()))
1343 }
1344
1345 moduleCtx.ModuleErrorf("multiple conflicting variants detected for OsType{%s}, %s, Image{%s}, Link{%s}\n%s",
1346 coord.osType, coord.archId.String(), coord.image, coord.linkType,
1347 strings.Join(variantDescriptions, "\n"))
1348 }
1349
1350 // If there are any variants to discard then remove them from the list of variants, while
1351 // preserving the order.
1352 if len(toDiscard) > 0 {
Paul Duffin5e71e682022-11-23 18:09:54 +00001353 filtered := []android.Module{}
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001354 for _, variant := range variants {
1355 if _, ok := toDiscard[variant]; !ok {
1356 filtered = append(filtered, variant)
1357 }
1358 }
1359 variants = filtered
1360 }
1361
1362 return variants
1363}
1364
1365// TODO(187910671): END - Remove once modules do not have an APEX and default variant.
1366
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001367type baseInfo struct {
1368 Properties android.SdkMemberProperties
1369}
1370
Paul Duffinf34f6d82020-04-30 15:48:31 +01001371func (b *baseInfo) optimizableProperties() interface{} {
1372 return b.Properties
1373}
1374
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001375type osTypeSpecificInfo struct {
1376 baseInfo
1377
Paul Duffin00e46802020-03-12 20:40:35 +00001378 osType android.OsType
1379
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001380 // The list of arch type specific info for this os type.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001381 //
1382 // Nil if there is one variant whose arch type is common
1383 archInfos []*archTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001384}
1385
Paul Duffin4b8b7932020-05-06 12:35:38 +01001386var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
1387
Paul Duffinfc8dd232020-03-17 12:51:37 +00001388type variantPropertiesFactoryFunc func() android.SdkMemberProperties
1389
Paul Duffin00e46802020-03-12 20:40:35 +00001390// Create a new osTypeSpecificInfo for the specified os type and its properties
1391// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001392func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
Paul Duffin00e46802020-03-12 20:40:35 +00001393 osInfo := &osTypeSpecificInfo{
1394 osType: osType,
1395 }
1396
1397 osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
1398 properties := variantPropertiesFactory()
1399 properties.Base().Os = osType
1400 return properties
1401 }
1402
1403 // Create a structure into which properties common across the architectures in
1404 // this os type will be stored.
1405 osInfo.Properties = osSpecificVariantPropertiesFactory()
1406
1407 // Group the variants by arch type.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001408 var variantsByArchId = make(map[archId][]android.Module)
1409 var archIds []archId
Paul Duffin00e46802020-03-12 20:40:35 +00001410 for _, variant := range osTypeVariants {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001411 target := variant.Target()
1412 id := archIdFromTarget(target)
1413 if _, ok := variantsByArchId[id]; !ok {
1414 archIds = append(archIds, id)
Paul Duffin00e46802020-03-12 20:40:35 +00001415 }
1416
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001417 variantsByArchId[id] = append(variantsByArchId[id], variant)
Paul Duffin00e46802020-03-12 20:40:35 +00001418 }
1419
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001420 if commonVariants, ok := variantsByArchId[commonArchId]; ok {
Paul Duffin00e46802020-03-12 20:40:35 +00001421 if len(osTypeVariants) != 1 {
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001422 variants := []string{}
1423 for _, m := range osTypeVariants {
1424 variants = append(variants, fmt.Sprintf(" %s", m.String()))
1425 }
1426 panic(fmt.Errorf("expected to only have 1 variant of %q when arch type is common but found %d\n%s",
1427 ctx.Name(),
1428 len(osTypeVariants),
1429 strings.Join(variants, "\n")))
Paul Duffin00e46802020-03-12 20:40:35 +00001430 }
1431
1432 // A common arch type only has one variant and its properties should be treated
1433 // as common to the os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001434 osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
Paul Duffin00e46802020-03-12 20:40:35 +00001435 } else {
1436 // Create an arch specific info for each supported architecture type.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001437 for _, id := range archIds {
1438 archVariants := variantsByArchId[id]
1439 archInfo := newArchSpecificInfo(ctx, id, osType, osSpecificVariantPropertiesFactory, archVariants)
Paul Duffin00e46802020-03-12 20:40:35 +00001440
1441 osInfo.archInfos = append(osInfo.archInfos, archInfo)
1442 }
1443 }
1444
1445 return osInfo
1446}
1447
Paul Duffin39abf8f2021-09-24 14:58:27 +01001448func (osInfo *osTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1449 if len(osInfo.archInfos) == 0 {
1450 pruner.pruneProperties(osInfo.Properties)
1451 } else {
1452 for _, archInfo := range osInfo.archInfos {
1453 archInfo.pruneUnsupportedProperties(pruner)
1454 }
1455 }
1456}
1457
Paul Duffin00e46802020-03-12 20:40:35 +00001458// Optimize the properties by extracting common properties from arch type specific
1459// properties into os type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001460func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin00e46802020-03-12 20:40:35 +00001461 // Nothing to do if there is only a single common architecture.
1462 if len(osInfo.archInfos) == 0 {
1463 return
1464 }
1465
Paul Duffin9c3760e2020-03-16 19:52:08 +00001466 multilib := multilibNone
Paul Duffin00e46802020-03-12 20:40:35 +00001467 for _, archInfo := range osInfo.archInfos {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001468 multilib = multilib.addArchType(archInfo.archId.archType)
Paul Duffin9c3760e2020-03-16 19:52:08 +00001469
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001470 // Optimize the arch properties first.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001471 archInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin00e46802020-03-12 20:40:35 +00001472 }
1473
Paul Duffin4b8b7932020-05-06 12:35:38 +01001474 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
Paul Duffin00e46802020-03-12 20:40:35 +00001475
1476 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
Paul Duffin9c3760e2020-03-16 19:52:08 +00001477 osInfo.Properties.Base().Compile_multilib = multilib.String()
Paul Duffin00e46802020-03-12 20:40:35 +00001478}
1479
1480// Add the properties for an os to a property set.
1481//
1482// Maps the properties related to the os variants through to an appropriate
1483// module structure that will produce equivalent set of variants when it is
1484// processed in a build.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001485func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
Paul Duffin00e46802020-03-12 20:40:35 +00001486
1487 var osPropertySet android.BpPropertySet
1488 var archPropertySet android.BpPropertySet
1489 var archOsPrefix string
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001490 if osInfo.Properties.Base().Os_count == 1 &&
1491 (osInfo.osType.Class == android.Device || !ctx.memberType.IsHostOsDependent()) {
1492 // There is only one OS type present in the variants and it shouldn't have a
1493 // variant-specific target. The latter is the case if it's either for device
1494 // where there is only one OS (android), or for host and the member type
1495 // isn't host OS dependent.
Paul Duffin00e46802020-03-12 20:40:35 +00001496
1497 // Create a structure that looks like:
1498 // module_type {
1499 // name: "...",
1500 // ...
1501 // <common properties>
1502 // ...
1503 // <single os type specific properties>
1504 //
1505 // arch: {
1506 // <arch specific sections>
1507 // }
1508 //
1509 osPropertySet = bpModule
1510 archPropertySet = osPropertySet.AddPropertySet("arch")
1511
1512 // Arch specific properties need to be added to an arch specific section
1513 // within arch.
1514 archOsPrefix = ""
1515 } else {
1516 // Create a structure that looks like:
1517 // module_type {
1518 // name: "...",
1519 // ...
1520 // <common properties>
1521 // ...
1522 // target: {
1523 // <arch independent os specific sections, e.g. android>
1524 // ...
1525 // <arch and os specific sections, e.g. android_x86>
1526 // }
1527 //
1528 osType := osInfo.osType
1529 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
1530 archPropertySet = targetPropertySet
1531
1532 // Arch specific properties need to be added to an os and arch specific
1533 // section prefixed with <os>_.
1534 archOsPrefix = osType.Name + "_"
1535 }
1536
1537 // Add the os specific but arch independent properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001538 addSdkMemberPropertiesToSet(ctx, osInfo.Properties, osPropertySet)
Paul Duffin00e46802020-03-12 20:40:35 +00001539
1540 // Add arch (and possibly os) specific sections for each set of arch (and possibly
1541 // os) specific properties.
1542 //
1543 // The archInfos list will be empty if the os contains variants for the common
1544 // architecture.
1545 for _, archInfo := range osInfo.archInfos {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001546 archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
Paul Duffin00e46802020-03-12 20:40:35 +00001547 }
1548}
1549
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001550func (osInfo *osTypeSpecificInfo) isHostVariant() bool {
1551 osClass := osInfo.osType.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001552 return osClass == android.Host
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001553}
1554
1555var _ isHostVariant = (*osTypeSpecificInfo)(nil)
1556
Paul Duffin4b8b7932020-05-06 12:35:38 +01001557func (osInfo *osTypeSpecificInfo) String() string {
1558 return fmt.Sprintf("OsType{%s}", osInfo.osType)
1559}
1560
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001561// archId encapsulates the information needed to identify a combination of arch type and native
1562// bridge support.
1563//
1564// Conceptually, native bridge support is a facet of an android.Target, not an android.Arch as it is
1565// essentially using one android.Arch to implement another. However, in terms of the handling of
1566// the variants native bridge is treated as part of the arch variation. See the ArchVariation method
1567// on android.Target.
1568//
1569// So, it makes sense when optimizing the variants to combine native bridge with the arch type.
1570type archId struct {
1571 // The arch type of the variant's target.
1572 archType android.ArchType
1573
1574 // True if the variants is for the native bridge, false otherwise.
1575 nativeBridge bool
1576}
1577
1578// propertyName returns the name of the property corresponding to use for this arch id.
1579func (i *archId) propertyName() string {
1580 name := i.archType.Name
1581 if i.nativeBridge {
1582 // Note: This does not result in a valid property because there is no architecture specific
1583 // native bridge property, only a generic "native_bridge" property. However, this will be used
1584 // in error messages if there is an attempt to use this in a generated bp file.
1585 name += "_native_bridge"
1586 }
1587 return name
1588}
1589
1590func (i *archId) String() string {
1591 return fmt.Sprintf("ArchType{%s}, NativeBridge{%t}", i.archType, i.nativeBridge)
1592}
1593
1594// archIdFromTarget returns an archId initialized from information in the supplied target.
1595func archIdFromTarget(target android.Target) archId {
1596 return archId{
1597 archType: target.Arch.ArchType,
1598 nativeBridge: target.NativeBridge == android.NativeBridgeEnabled,
1599 }
1600}
1601
1602// commonArchId is the archId for the common architecture.
1603var commonArchId = archId{archType: android.Common}
1604
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001605type archTypeSpecificInfo struct {
1606 baseInfo
1607
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001608 archId archId
1609 osType android.OsType
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001610
Paul Duffinb42fa672021-09-09 16:37:49 +01001611 imageVariantInfos []*imageVariantSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001612}
1613
Paul Duffin4b8b7932020-05-06 12:35:38 +01001614var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
1615
Paul Duffinfc8dd232020-03-17 12:51:37 +00001616// Create a new archTypeSpecificInfo for the specified arch type and its properties
1617// structures populated with information from the variants.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001618func newArchSpecificInfo(ctx android.SdkMemberContext, archId archId, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001619
Paul Duffinfc8dd232020-03-17 12:51:37 +00001620 // Create an arch specific info into which the variant properties can be copied.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001621 archInfo := &archTypeSpecificInfo{archId: archId, osType: osType}
Paul Duffinfc8dd232020-03-17 12:51:37 +00001622
1623 // Create the properties into which the arch type specific properties will be
1624 // added.
1625 archInfo.Properties = variantPropertiesFactory()
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001626
Liz Kammer96320df2022-05-12 20:40:00 -04001627 // if there are multiple supported link variants, we want to nest based on linkage even if there
1628 // is only one variant, otherwise, if there is only one variant we can populate based on the arch
1629 if len(archVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001630 archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001631 } else {
Paul Duffinb42fa672021-09-09 16:37:49 +01001632 // Group the variants by image type.
1633 variantsByImage := make(map[string][]android.Module)
1634 for _, variant := range archVariants {
1635 image := variant.ImageVariation().Variation
1636 variantsByImage[image] = append(variantsByImage[image], variant)
1637 }
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001638
Paul Duffinb42fa672021-09-09 16:37:49 +01001639 // Create the image variant info in a fixed order.
Cole Faust18994c72023-02-28 16:02:16 -08001640 for _, imageVariantName := range android.SortedKeys(variantsByImage) {
Paul Duffinb42fa672021-09-09 16:37:49 +01001641 variants := variantsByImage[imageVariantName]
1642 archInfo.imageVariantInfos = append(archInfo.imageVariantInfos, newImageVariantSpecificInfo(ctx, imageVariantName, variantPropertiesFactory, variants))
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001643 }
1644 }
Paul Duffinfc8dd232020-03-17 12:51:37 +00001645
1646 return archInfo
1647}
1648
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001649// Get the link type of the variant
1650//
1651// If the variant is not differentiated by link type then it returns "",
1652// otherwise it returns one of "static" or "shared".
1653func getLinkType(variant android.Module) string {
1654 linkType := ""
1655 if linkable, ok := variant.(cc.LinkableInterface); ok {
1656 if linkable.Shared() && linkable.Static() {
1657 panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String()))
1658 } else if linkable.Shared() {
1659 linkType = "shared"
1660 } else if linkable.Static() {
1661 linkType = "static"
1662 } else {
1663 panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String()))
1664 }
1665 }
1666 return linkType
1667}
1668
Paul Duffin39abf8f2021-09-24 14:58:27 +01001669func (archInfo *archTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1670 if len(archInfo.imageVariantInfos) == 0 {
1671 pruner.pruneProperties(archInfo.Properties)
1672 } else {
1673 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1674 imageVariantInfo.pruneUnsupportedProperties(pruner)
1675 }
1676 }
1677}
1678
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001679// Optimize the properties by extracting common properties from link type specific
1680// properties into arch type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001681func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffinb42fa672021-09-09 16:37:49 +01001682 if len(archInfo.imageVariantInfos) == 0 {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001683 return
1684 }
1685
Paul Duffinb42fa672021-09-09 16:37:49 +01001686 // Optimize the image variant properties first.
1687 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1688 imageVariantInfo.optimizeProperties(ctx, commonValueExtractor)
1689 }
1690
1691 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.imageVariantInfos)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001692}
1693
Paul Duffinfc8dd232020-03-17 12:51:37 +00001694// Add the properties for an arch type to a property set.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001695func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001696 archPropertySuffix := archInfo.archId.propertyName()
1697 propertySetName := archOsPrefix + archPropertySuffix
1698 archTypePropertySet := archPropertySet.AddPropertySet(propertySetName)
Jiyong Park8fe14e62020-10-19 22:47:34 +09001699 // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
1700 if ctx.memberType.IsHostOsDependent() && archInfo.osType.Class == android.Host {
1701 archTypePropertySet.AddProperty("enabled", true)
1702 }
Martin Stjernholm89238f42020-07-10 00:14:03 +01001703 addSdkMemberPropertiesToSet(ctx, archInfo.Properties, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001704
Paul Duffinb42fa672021-09-09 16:37:49 +01001705 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1706 imageVariantInfo.addToPropertySet(ctx, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001707 }
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001708
1709 // If this is for a native bridge architecture then make sure that the property set does not
1710 // contain any properties as providing native bridge specific properties is not currently
1711 // supported.
1712 if archInfo.archId.nativeBridge {
1713 propertySetContents := getPropertySetContents(archTypePropertySet)
1714 if propertySetContents != "" {
1715 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",
1716 propertySetName, ctx.name, propertySetContents)
1717 }
1718 }
1719}
1720
1721// getPropertySetContents returns the string representation of the contents of a property set, after
1722// recursively pruning any empty nested property sets.
1723func getPropertySetContents(propertySet android.BpPropertySet) string {
1724 set := propertySet.(*bpPropertySet)
1725 set.transformContents(pruneEmptySetTransformer{})
1726 if len(set.properties) != 0 {
1727 contents := &generatedContents{}
1728 contents.Indent()
1729 outputPropertySet(contents, set)
1730 setAsString := contents.content.String()
1731 return setAsString
1732 }
1733 return ""
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001734}
1735
Paul Duffin4b8b7932020-05-06 12:35:38 +01001736func (archInfo *archTypeSpecificInfo) String() string {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001737 return archInfo.archId.String()
Paul Duffin4b8b7932020-05-06 12:35:38 +01001738}
1739
Paul Duffinb42fa672021-09-09 16:37:49 +01001740type imageVariantSpecificInfo struct {
1741 baseInfo
1742
1743 imageVariant string
1744
1745 linkInfos []*linkTypeSpecificInfo
1746}
1747
1748func newImageVariantSpecificInfo(ctx android.SdkMemberContext, imageVariant string, variantPropertiesFactory variantPropertiesFactoryFunc, imageVariants []android.Module) *imageVariantSpecificInfo {
1749
1750 // Create an image variant specific info into which the variant properties can be copied.
1751 imageInfo := &imageVariantSpecificInfo{imageVariant: imageVariant}
1752
1753 // Create the properties into which the image variant specific properties will be added.
1754 imageInfo.Properties = variantPropertiesFactory()
1755
Liz Kammer96320df2022-05-12 20:40:00 -04001756 // if there are multiple supported link variants, we want to nest even if there is only one
1757 // variant, otherwise, if there is only one variant we can populate based on the image
1758 if len(imageVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
Paul Duffinb42fa672021-09-09 16:37:49 +01001759 imageInfo.Properties.PopulateFromVariant(ctx, imageVariants[0])
1760 } else {
1761 // There is more than one variant for this image variant which must be differentiated by link
Liz Kammer96320df2022-05-12 20:40:00 -04001762 // type. Or there are multiple supported linkages and we need to nest based on link type.
Paul Duffinb42fa672021-09-09 16:37:49 +01001763 for _, linkVariant := range imageVariants {
1764 linkType := getLinkType(linkVariant)
1765 if linkType == "" {
1766 panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(imageVariants)))
1767 } else {
1768 linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
1769
1770 imageInfo.linkInfos = append(imageInfo.linkInfos, linkInfo)
1771 }
1772 }
1773 }
1774
1775 return imageInfo
1776}
1777
Paul Duffin39abf8f2021-09-24 14:58:27 +01001778func (imageInfo *imageVariantSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1779 if len(imageInfo.linkInfos) == 0 {
1780 pruner.pruneProperties(imageInfo.Properties)
1781 } else {
1782 for _, linkInfo := range imageInfo.linkInfos {
1783 linkInfo.pruneUnsupportedProperties(pruner)
1784 }
1785 }
1786}
1787
Paul Duffinb42fa672021-09-09 16:37:49 +01001788// Optimize the properties by extracting common properties from link type specific
1789// properties into arch type specific properties.
1790func (imageInfo *imageVariantSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
1791 if len(imageInfo.linkInfos) == 0 {
1792 return
1793 }
1794
1795 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, imageInfo.Properties, imageInfo.linkInfos)
1796}
1797
1798// Add the properties for an arch type to a property set.
1799func (imageInfo *imageVariantSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
1800 if imageInfo.imageVariant != android.CoreVariation {
1801 propertySet = propertySet.AddPropertySet(imageInfo.imageVariant)
1802 }
1803
1804 addSdkMemberPropertiesToSet(ctx, imageInfo.Properties, propertySet)
1805
Liz Kammer96320df2022-05-12 20:40:00 -04001806 usedLinkages := make(map[string]bool, len(imageInfo.linkInfos))
Paul Duffinb42fa672021-09-09 16:37:49 +01001807 for _, linkInfo := range imageInfo.linkInfos {
Liz Kammer96320df2022-05-12 20:40:00 -04001808 usedLinkages[linkInfo.linkType] = true
Paul Duffinb42fa672021-09-09 16:37:49 +01001809 linkInfo.addToPropertySet(ctx, propertySet)
1810 }
1811
Liz Kammer96320df2022-05-12 20:40:00 -04001812 // If not all supported linkages had existing variants, we need to disable the unsupported variant
1813 if len(imageInfo.linkInfos) < len(ctx.MemberType().SupportedLinkages()) {
1814 for _, l := range ctx.MemberType().SupportedLinkages() {
1815 if _, ok := usedLinkages[l]; !ok {
1816 otherLinkagePropertySet := propertySet.AddPropertySet(l)
1817 otherLinkagePropertySet.AddProperty("enabled", false)
1818 }
1819 }
1820 }
1821
Paul Duffinb42fa672021-09-09 16:37:49 +01001822 // If this is for a non-core image variant then make sure that the property set does not contain
1823 // any properties as providing non-core image variant specific properties for prebuilts is not
1824 // currently supported.
1825 if imageInfo.imageVariant != android.CoreVariation {
1826 propertySetContents := getPropertySetContents(propertySet)
1827 if propertySetContents != "" {
1828 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",
1829 imageInfo.imageVariant, ctx.name, propertySetContents)
1830 }
1831 }
1832}
1833
1834func (imageInfo *imageVariantSpecificInfo) String() string {
1835 return imageInfo.imageVariant
1836}
1837
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001838type linkTypeSpecificInfo struct {
1839 baseInfo
1840
1841 linkType string
1842}
1843
Paul Duffin4b8b7932020-05-06 12:35:38 +01001844var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
1845
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001846// Create a new linkTypeSpecificInfo for the specified link type and its properties
1847// structures populated with information from the variant.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001848func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001849 linkInfo := &linkTypeSpecificInfo{
1850 baseInfo: baseInfo{
1851 // Create the properties into which the link type specific properties will be
1852 // added.
1853 Properties: variantPropertiesFactory(),
1854 },
1855 linkType: linkType,
1856 }
Paul Duffin3a4eb502020-03-19 16:11:18 +00001857 linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001858 return linkInfo
Paul Duffinfc8dd232020-03-17 12:51:37 +00001859}
1860
Paul Duffinf68f85a2021-09-09 16:11:42 +01001861func (l *linkTypeSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
1862 linkPropertySet := propertySet.AddPropertySet(l.linkType)
1863 addSdkMemberPropertiesToSet(ctx, l.Properties, linkPropertySet)
1864}
1865
Paul Duffin39abf8f2021-09-24 14:58:27 +01001866func (l *linkTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1867 pruner.pruneProperties(l.Properties)
1868}
1869
Paul Duffin4b8b7932020-05-06 12:35:38 +01001870func (l *linkTypeSpecificInfo) String() string {
1871 return fmt.Sprintf("LinkType{%s}", l.linkType)
1872}
1873
Paul Duffin3a4eb502020-03-19 16:11:18 +00001874type memberContext struct {
1875 sdkMemberContext android.ModuleContext
1876 builder *snapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +00001877 memberType android.SdkMemberType
1878 name string
Paul Duffind19f8942021-07-14 12:08:37 +01001879
1880 // The set of traits required of this member.
1881 requiredTraits android.SdkMemberTraitSet
Paul Duffin3a4eb502020-03-19 16:11:18 +00001882}
1883
Cole Faust34867402023-04-28 12:32:27 -07001884func (m *memberContext) ModuleErrorf(fmt string, args ...interface{}) {
1885 m.sdkMemberContext.ModuleErrorf(fmt, args...)
1886}
1887
Paul Duffin3a4eb502020-03-19 16:11:18 +00001888func (m *memberContext) SdkModuleContext() android.ModuleContext {
1889 return m.sdkMemberContext
1890}
1891
1892func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
1893 return m.builder
1894}
1895
Paul Duffina551a1c2020-03-17 21:04:24 +00001896func (m *memberContext) MemberType() android.SdkMemberType {
1897 return m.memberType
1898}
1899
1900func (m *memberContext) Name() string {
1901 return m.name
1902}
1903
Paul Duffind19f8942021-07-14 12:08:37 +01001904func (m *memberContext) RequiresTrait(trait android.SdkMemberTrait) bool {
1905 return m.requiredTraits.Contains(trait)
1906}
1907
Paul Duffin13648912022-07-15 13:12:35 +00001908func (m *memberContext) IsTargetBuildBeforeTiramisu() bool {
1909 return m.builder.targetBuildRelease.EarlierThan(buildReleaseT)
1910}
1911
1912var _ android.SdkMemberContext = (*memberContext)(nil)
1913
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001914func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001915
1916 memberType := member.memberType
1917
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001918 // Do not add the prefer property if the member snapshot module is a source module type.
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001919 moduleCtx := ctx.sdkMemberContext
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001920 if !memberType.UsesSourceModuleTypeInSnapshot() {
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001921 // Set prefer. Setting this to false is not strictly required as that is the default but it does
1922 // provide a convenient hook to post-process the generated Android.bp file, e.g. in tests to
1923 // check the behavior when a prebuilt is preferred. It also makes it explicit what the default
1924 // behavior is for the module.
Paul Duffin82d75ad2022-11-14 17:35:50 +00001925 bpModule.insertAfter("name", "prefer", false)
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01001926 }
Paul Duffin83ad9562021-05-10 23:49:04 +01001927
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001928 variants := selectApexVariantsWhereAvailable(ctx, member.variants)
1929
Paul Duffina04c1072020-03-02 10:16:35 +00001930 // Group the variants by os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001931 variantsByOsType := make(map[android.OsType][]android.Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001932 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +00001933 osType := variant.Target().Os
1934 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001935 }
1936
Paul Duffina04c1072020-03-02 10:16:35 +00001937 osCount := len(variantsByOsType)
Paul Duffinb44b33a2020-03-17 10:58:23 +00001938 variantPropertiesFactory := func() android.SdkMemberProperties {
Paul Duffina04c1072020-03-02 10:16:35 +00001939 properties := memberType.CreateVariantPropertiesStruct()
1940 base := properties.Base()
1941 base.Os_count = osCount
Paul Duffina04c1072020-03-02 10:16:35 +00001942 return properties
1943 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001944
Paul Duffina04c1072020-03-02 10:16:35 +00001945 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +00001946
Paul Duffina04c1072020-03-02 10:16:35 +00001947 // The set of properties that are common across all architectures and os types.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001948 commonProperties := variantPropertiesFactory()
1949 commonProperties.Base().Os = android.CommonOS
Paul Duffina04c1072020-03-02 10:16:35 +00001950
Paul Duffin39abf8f2021-09-24 14:58:27 +01001951 // Create a property pruner that will prune any properties unsupported by the target build
1952 // release.
1953 targetBuildRelease := ctx.builder.targetBuildRelease
1954 unsupportedPropertyPruner := newPropertyPrunerByBuildRelease(commonProperties, targetBuildRelease)
1955
Paul Duffinc097e362020-03-10 22:50:03 +00001956 // Create common value extractor that can be used to optimize the properties.
1957 commonValueExtractor := newCommonValueExtractor(commonProperties)
1958
Paul Duffina04c1072020-03-02 10:16:35 +00001959 // The list of property structures which are os type specific but common across
1960 // architectures within that os type.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001961 var osSpecificPropertiesContainers []*osTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +00001962
1963 for osType, osTypeVariants := range variantsByOsType {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001964 osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
Paul Duffina04c1072020-03-02 10:16:35 +00001965 osTypeToInfo[osType] = osInfo
Paul Duffinb44b33a2020-03-17 10:58:23 +00001966 // Add the os specific properties to a list of os type specific yet architecture
1967 // independent properties structs.
Paul Duffinf34f6d82020-04-30 15:48:31 +01001968 osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
Paul Duffina04c1072020-03-02 10:16:35 +00001969
Paul Duffin39abf8f2021-09-24 14:58:27 +01001970 osInfo.pruneUnsupportedProperties(unsupportedPropertyPruner)
1971
Paul Duffin00e46802020-03-12 20:40:35 +00001972 // Optimize the properties across all the variants for a specific os type.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001973 osInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin14eb4672020-03-02 11:33:02 +00001974 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001975
Paul Duffina04c1072020-03-02 10:16:35 +00001976 // Extract properties which are common across all architectures and os types.
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001977 extractCommonProperties(moduleCtx, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001978
Paul Duffina04c1072020-03-02 10:16:35 +00001979 // Add the common properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001980 addSdkMemberPropertiesToSet(ctx, commonProperties, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001981
Paul Duffina04c1072020-03-02 10:16:35 +00001982 // Create a target property set into which target specific properties can be
1983 // added.
1984 targetPropertySet := bpModule.AddPropertySet("target")
1985
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001986 // If the member is host OS dependent and has host_supported then disable by
1987 // default and enable each host OS variant explicitly. This avoids problems
1988 // with implicitly enabled OS variants when the snapshot is used, which might
1989 // be different from this run (e.g. different build OS).
1990 if ctx.memberType.IsHostOsDependent() {
1991 hostSupported := bpModule.getValue("host_supported") == true // Missing means false.
1992 if hostSupported {
1993 hostPropertySet := targetPropertySet.AddPropertySet("host")
1994 hostPropertySet.AddProperty("enabled", false)
1995 }
1996 }
1997
Paul Duffina04c1072020-03-02 10:16:35 +00001998 // Iterate over the os types in a fixed order.
1999 for _, osType := range s.getPossibleOsTypes() {
2000 osInfo := osTypeToInfo[osType]
2001 if osInfo == nil {
2002 continue
2003 }
2004
Paul Duffin3a4eb502020-03-19 16:11:18 +00002005 osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002006 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002007}
2008
Paul Duffina04c1072020-03-02 10:16:35 +00002009// Compute the list of possible os types that this sdk could support.
2010func (s *sdk) getPossibleOsTypes() []android.OsType {
2011 var osTypes []android.OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +00002012 for _, osType := range android.OsTypeList() {
Paul Duffina04c1072020-03-02 10:16:35 +00002013 if s.DeviceSupported() {
Colin Crosscb0ac952021-07-20 13:17:15 -07002014 if osType.Class == android.Device {
Paul Duffina04c1072020-03-02 10:16:35 +00002015 osTypes = append(osTypes, osType)
2016 }
2017 }
2018 if s.HostSupported() {
Jiyong Park1613e552020-09-14 19:43:17 +09002019 if osType.Class == android.Host {
Paul Duffina04c1072020-03-02 10:16:35 +00002020 osTypes = append(osTypes, osType)
2021 }
2022 }
2023 }
2024 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
2025 return osTypes
2026}
2027
Paul Duffinb28369a2020-05-04 15:39:59 +01002028// Given a set of properties (struct value), return the value of the field within that
2029// struct (or one of its embedded structs).
Paul Duffinc097e362020-03-10 22:50:03 +00002030type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
2031
Paul Duffinc459f892020-04-30 18:08:29 +01002032// Checks the metadata to determine whether the property should be ignored for the
2033// purposes of common value extraction or not.
2034type extractorMetadataPredicate func(metadata propertiesContainer) bool
2035
2036// Indicates whether optimizable properties are provided by a host variant or
2037// not.
2038type isHostVariant interface {
2039 isHostVariant() bool
2040}
2041
Paul Duffinb28369a2020-05-04 15:39:59 +01002042// A property that can be optimized by the commonValueExtractor.
2043type extractorProperty struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +01002044 // The name of the field for this property. It is a "."-separated path for
2045 // fields in non-anonymous substructs.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002046 name string
2047
Paul Duffinc459f892020-04-30 18:08:29 +01002048 // Filter that can use metadata associated with the properties being optimized
2049 // to determine whether the field should be ignored during common value
2050 // optimization.
2051 filter extractorMetadataPredicate
2052
Paul Duffinb28369a2020-05-04 15:39:59 +01002053 // Retrieves the value on which common value optimization will be performed.
2054 getter fieldAccessorFunc
2055
Paul Duffinbfdca962022-09-22 16:21:54 +01002056 // True if the field should never be cleared.
2057 //
2058 // This is set to true if and only if the field is annotated with `sdk:"keep"`.
2059 keep bool
2060
Paul Duffinb28369a2020-05-04 15:39:59 +01002061 // The empty value for the field.
2062 emptyValue reflect.Value
Paul Duffin864e1b42020-05-06 10:23:19 +01002063
2064 // True if the property can support arch variants false otherwise.
2065 archVariant bool
Paul Duffinb28369a2020-05-04 15:39:59 +01002066}
2067
Paul Duffin4b8b7932020-05-06 12:35:38 +01002068func (p extractorProperty) String() string {
2069 return p.name
2070}
2071
Paul Duffinc097e362020-03-10 22:50:03 +00002072// Supports extracting common values from a number of instances of a properties
2073// structure into a separate common set of properties.
2074type commonValueExtractor struct {
Paul Duffinb28369a2020-05-04 15:39:59 +01002075 // The properties that the extractor can optimize.
2076 properties []extractorProperty
Paul Duffinc097e362020-03-10 22:50:03 +00002077}
2078
2079// Create a new common value extractor for the structure type for the supplied
2080// properties struct.
2081//
2082// The returned extractor can be used on any properties structure of the same type
2083// as the supplied set of properties.
2084func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor {
2085 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
2086 extractor := &commonValueExtractor{}
Martin Stjernholmb0249572020-09-15 02:32:35 +01002087 extractor.gatherFields(structType, nil, "")
Paul Duffinc097e362020-03-10 22:50:03 +00002088 return extractor
2089}
2090
2091// Gather the fields from the supplied structure type from which common values will
2092// be extracted.
Paul Duffinb07fa512020-03-10 22:17:04 +00002093//
Martin Stjernholmb0249572020-09-15 02:32:35 +01002094// This is recursive function. If it encounters a struct then it will recurse
2095// into it, passing in the accessor for the field and the struct name as prefix
2096// for the nested fields. That will then be used in the accessors for the fields
2097// in the embedded struct.
2098func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string) {
Paul Duffinc097e362020-03-10 22:50:03 +00002099 for f := 0; f < structType.NumField(); f++ {
2100 field := structType.Field(f)
2101 if field.PkgPath != "" {
2102 // Ignore unexported fields.
2103 continue
2104 }
2105
Paul Duffin02e25c82022-09-22 15:30:58 +01002106 // Ignore fields tagged with sdk:"ignore".
2107 if proptools.HasTag(field, "sdk", "ignore") {
Paul Duffinc097e362020-03-10 22:50:03 +00002108 continue
2109 }
2110
Paul Duffinc459f892020-04-30 18:08:29 +01002111 var filter extractorMetadataPredicate
2112
2113 // Add a filter
2114 if proptools.HasTag(field, "sdk", "ignored-on-host") {
2115 filter = func(metadata propertiesContainer) bool {
2116 if m, ok := metadata.(isHostVariant); ok {
2117 if m.isHostVariant() {
2118 return false
2119 }
2120 }
2121 return true
2122 }
2123 }
2124
Paul Duffinbfdca962022-09-22 16:21:54 +01002125 keep := proptools.HasTag(field, "sdk", "keep")
2126
Paul Duffinc097e362020-03-10 22:50:03 +00002127 // Save a copy of the field index for use in the function.
2128 fieldIndex := f
Paul Duffin4b8b7932020-05-06 12:35:38 +01002129
Martin Stjernholmb0249572020-09-15 02:32:35 +01002130 name := namePrefix + field.Name
Paul Duffin4b8b7932020-05-06 12:35:38 +01002131
Paul Duffinc097e362020-03-10 22:50:03 +00002132 fieldGetter := func(value reflect.Value) reflect.Value {
Paul Duffinb07fa512020-03-10 22:17:04 +00002133 if containingStructAccessor != nil {
2134 // This is an embedded structure so first access the field for the embedded
2135 // structure.
2136 value = containingStructAccessor(value)
2137 }
2138
Paul Duffinc097e362020-03-10 22:50:03 +00002139 // Skip through interface and pointer values to find the structure.
2140 value = getStructValue(value)
2141
Paul Duffin4b8b7932020-05-06 12:35:38 +01002142 defer func() {
2143 if r := recover(); r != nil {
2144 panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
2145 }
2146 }()
2147
Paul Duffinc097e362020-03-10 22:50:03 +00002148 // Return the field.
2149 return value.Field(fieldIndex)
2150 }
2151
Martin Stjernholmb0249572020-09-15 02:32:35 +01002152 if field.Type.Kind() == reflect.Struct {
2153 // Gather fields from the nested or embedded structure.
2154 var subNamePrefix string
2155 if field.Anonymous {
2156 subNamePrefix = namePrefix
2157 } else {
2158 subNamePrefix = name + "."
2159 }
2160 e.gatherFields(field.Type, fieldGetter, subNamePrefix)
Paul Duffinb07fa512020-03-10 22:17:04 +00002161 } else {
Paul Duffinb28369a2020-05-04 15:39:59 +01002162 property := extractorProperty{
Paul Duffin4b8b7932020-05-06 12:35:38 +01002163 name,
Paul Duffinc459f892020-04-30 18:08:29 +01002164 filter,
Paul Duffinb28369a2020-05-04 15:39:59 +01002165 fieldGetter,
Paul Duffinbfdca962022-09-22 16:21:54 +01002166 keep,
Paul Duffinb28369a2020-05-04 15:39:59 +01002167 reflect.Zero(field.Type),
Paul Duffin864e1b42020-05-06 10:23:19 +01002168 proptools.HasTag(field, "android", "arch_variant"),
Paul Duffinb28369a2020-05-04 15:39:59 +01002169 }
2170 e.properties = append(e.properties, property)
Paul Duffinb07fa512020-03-10 22:17:04 +00002171 }
Paul Duffinc097e362020-03-10 22:50:03 +00002172 }
2173}
2174
2175func getStructValue(value reflect.Value) reflect.Value {
2176foundStruct:
2177 for {
2178 kind := value.Kind()
2179 switch kind {
2180 case reflect.Interface, reflect.Ptr:
2181 value = value.Elem()
2182 case reflect.Struct:
2183 break foundStruct
2184 default:
2185 panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind))
2186 }
2187 }
2188 return value
2189}
2190
Paul Duffinf34f6d82020-04-30 15:48:31 +01002191// A container of properties to be optimized.
2192//
2193// Allows additional information to be associated with the properties, e.g. for
2194// filtering.
2195type propertiesContainer interface {
Paul Duffin4b8b7932020-05-06 12:35:38 +01002196 fmt.Stringer
2197
Paul Duffinf34f6d82020-04-30 15:48:31 +01002198 // Get the properties that need optimizing.
2199 optimizableProperties() interface{}
2200}
2201
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002202// Extract common properties from a slice of property structures of the same type.
2203//
2204// All the property structures must be of the same type.
2205// commonProperties - must be a pointer to the structure into which common properties will be added.
Paul Duffinf34f6d82020-04-30 15:48:31 +01002206// inputPropertiesSlice - must be a slice of propertiesContainer interfaces.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002207//
2208// Iterates over each exported field (capitalized name) and checks to see whether they
2209// have the same value (using DeepEquals) across all the input properties. If it does not then no
2210// change is made. Otherwise, the common value is stored in the field in the commonProperties
Martin Stjernholmb0249572020-09-15 02:32:35 +01002211// and the field in each of the input properties structure is set to its default value. Nested
2212// structs are visited recursively and their non-struct fields are compared.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002213func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002214 commonPropertiesValue := reflect.ValueOf(commonProperties)
2215 commonStructValue := commonPropertiesValue.Elem()
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002216
Paul Duffinf34f6d82020-04-30 15:48:31 +01002217 sliceValue := reflect.ValueOf(inputPropertiesSlice)
2218
Paul Duffinb28369a2020-05-04 15:39:59 +01002219 for _, property := range e.properties {
2220 fieldGetter := property.getter
Paul Duffinc459f892020-04-30 18:08:29 +01002221 filter := property.filter
2222 if filter == nil {
2223 filter = func(metadata propertiesContainer) bool {
2224 return true
2225 }
2226 }
Paul Duffinb28369a2020-05-04 15:39:59 +01002227
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002228 // Check to see if all the structures have the same value for the field. The commonValue
Paul Duffin864e1b42020-05-06 10:23:19 +01002229 // is nil on entry to the loop and if it is nil on exit then there is no common value or
2230 // all the values have been filtered out, otherwise it points to the common value.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002231 var commonValue *reflect.Value
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002232
Paul Duffin864e1b42020-05-06 10:23:19 +01002233 // Assume that all the values will be the same.
2234 //
2235 // While similar to this is not quite the same as commonValue == nil. If all the values
2236 // have been filtered out then this will be false but commonValue == nil will be true.
2237 valuesDiffer := false
2238
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002239 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01002240 container := sliceValue.Index(i).Interface().(propertiesContainer)
2241 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00002242 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002243
Paul Duffinc459f892020-04-30 18:08:29 +01002244 if !filter(container) {
2245 expectedValue := property.emptyValue.Interface()
2246 actualValue := fieldValue.Interface()
2247 if !reflect.DeepEqual(expectedValue, actualValue) {
2248 return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue)
2249 }
2250 continue
2251 }
2252
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002253 if commonValue == nil {
2254 // Use the first value as the commonProperties value.
2255 commonValue = &fieldValue
2256 } else {
2257 // If the value does not match the current common value then there is
2258 // no value in common so break out.
2259 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
2260 commonValue = nil
Paul Duffin864e1b42020-05-06 10:23:19 +01002261 valuesDiffer = true
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002262 break
2263 }
2264 }
2265 }
2266
Paul Duffin864e1b42020-05-06 10:23:19 +01002267 // If the fields all have common value then store it in the common struct field
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002268 // and set the input struct's field to the empty value.
2269 if commonValue != nil {
Paul Duffinb28369a2020-05-04 15:39:59 +01002270 emptyValue := property.emptyValue
Paul Duffinc097e362020-03-10 22:50:03 +00002271 fieldGetter(commonStructValue).Set(*commonValue)
Paul Duffinbfdca962022-09-22 16:21:54 +01002272 if !property.keep {
2273 for i := 0; i < sliceValue.Len(); i++ {
2274 container := sliceValue.Index(i).Interface().(propertiesContainer)
2275 itemValue := reflect.ValueOf(container.optimizableProperties())
2276 fieldValue := fieldGetter(itemValue)
2277 fieldValue.Set(emptyValue)
2278 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002279 }
2280 }
Paul Duffin864e1b42020-05-06 10:23:19 +01002281
2282 if valuesDiffer && !property.archVariant {
2283 // The values differ but the property does not support arch variants so it
2284 // is an error.
2285 var details strings.Builder
2286 for i := 0; i < sliceValue.Len(); i++ {
2287 container := sliceValue.Index(i).Interface().(propertiesContainer)
2288 itemValue := reflect.ValueOf(container.optimizableProperties())
2289 fieldValue := fieldGetter(itemValue)
2290
2291 _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface())
2292 }
2293
2294 return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String())
2295 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002296 }
Paul Duffin4b8b7932020-05-06 12:35:38 +01002297
2298 return nil
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002299}