blob: 6ebbf09d3d86b7b82b7717e4d3620c4b854498d4 [file] [log] [blame]
Jiyong Park9b409bc2019-10-11 14:59:13 +09001// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package sdk
16
17import (
Paul Duffinc6ba1822022-05-06 09:38:02 +000018 "bytes"
19 "encoding/json"
Jiyong Park9b409bc2019-10-11 14:59:13 +090020 "fmt"
Paul Duffinb645ec82019-11-27 17:43:54 +000021 "reflect"
Paul Duffina04c1072020-03-02 10:16:35 +000022 "sort"
Jiyong Park9b409bc2019-10-11 14:59:13 +090023 "strings"
24
Paul Duffin7d74e7b2020-03-06 12:30:13 +000025 "android/soong/apex"
Paul Duffin9b76c0b2020-03-12 10:24:35 +000026 "android/soong/cc"
Colin Crosscb0ac952021-07-20 13:17:15 -070027
Paul Duffin375058f2019-11-29 20:17:53 +000028 "github.com/google/blueprint"
Jiyong Park9b409bc2019-10-11 14:59:13 +090029 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
Jiyong Park9b409bc2019-10-11 14:59:13 +090032)
33
Paul Duffin64fb5262021-05-05 21:36:04 +010034// Environment variables that affect the generated snapshot
35// ========================================================
36//
37// SOONG_SDK_SNAPSHOT_PREFER
Paul Duffinb01ac4b2022-05-24 20:10:05 +000038// By default every module in the generated snapshot has prefer: false. Building it
Mathew Inwood7e9ddbe2021-07-07 12:47:51 +000039// with SOONG_SDK_SNAPSHOT_PREFER=true will force them to use prefer: true.
Paul Duffin64fb5262021-05-05 21:36:04 +010040//
Paul Duffinfb9a7f92021-07-06 17:18:42 +010041// SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR
42// If set this specifies the Soong config var that can be used to control whether the prebuilt
43// modules from the generated snapshot or the original source modules. Values must be a colon
44// separated pair of strings, the first of which is the Soong config namespace, and the second
45// is the name of the variable within that namespace.
46//
47// The config namespace and var name are used to set the `use_source_config_var` property. That
48// in turn will cause the generated prebuilts to use the soong config variable to select whether
49// source or the prebuilt is used.
50// e.g. If an sdk snapshot is built using:
51// m SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR=acme:build_from_source sdkextensions-sdk
52// Then the resulting snapshot will include:
53// use_source_config_var: {
54// config_namespace: "acme",
55// var_name: "build_from_source",
56// }
57//
58// Assuming that the config variable is defined in .mk using something like:
59// $(call add_soong_config_namespace,acme)
60// $(call add_soong_config_var_value,acme,build_from_source,true)
61//
62// Then when the snapshot is unpacked in the repository it will have the following behavior:
63// m droid - will use the sdkextensions-sdk prebuilts if present. Otherwise, it will use the
64// sources.
65// m SOONG_CONFIG_acme_build_from_source=true droid - will use the sdkextensions-sdk
66// sources, if present. Otherwise, it will use the prebuilts.
67//
68// This is a temporary mechanism to control the prefer flags and will be removed once a more
69// maintainable solution has been implemented.
70// TODO(b/174997203): Remove when no longer necessary.
71//
Paul Duffin39abf8f2021-09-24 14:58:27 +010072// SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE
73// This allows the target build release (i.e. the release version of the build within which
74// the snapshot will be used) of the snapshot to be specified. If unspecified then it defaults
75// to the current build release version. Otherwise, it must be the name of one of the build
76// releases defined in nameToBuildRelease, e.g. S, T, etc..
77//
78// The generated snapshot must only be used in the specified target release. If the target
79// build release is not the current build release then the generated Android.bp file not be
80// checked for compatibility.
81//
82// e.g. if setting SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE=S will cause the generated snapshot
83// to be compatible with S.
84//
Paul Duffin64fb5262021-05-05 21:36:04 +010085
Jiyong Park9b409bc2019-10-11 14:59:13 +090086var pctx = android.NewPackageContext("android/soong/sdk")
87
Paul Duffin375058f2019-11-29 20:17:53 +000088var (
89 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
90 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000091 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000092 CommandDeps: []string{
93 "${config.Zip2ZipCmd}",
94 },
95 },
96 "destdir")
97
98 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
99 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -0700100 Command: `${config.SoongZipCmd} -C $basedir -r $out.rsp -o $out`,
Paul Duffin375058f2019-11-29 20:17:53 +0000101 CommandDeps: []string{
102 "${config.SoongZipCmd}",
103 },
104 Rspfile: "$out.rsp",
105 RspfileContent: "$in",
106 },
107 "basedir")
108
109 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
110 blueprint.RuleParams{
Paul Duffin74f1dcd2022-07-18 13:18:23 +0000111 Command: `${config.MergeZipsCmd} -s $out $in`,
Paul Duffin375058f2019-11-29 20:17:53 +0000112 CommandDeps: []string{
113 "${config.MergeZipsCmd}",
114 },
115 })
116)
117
Paul Duffin43f7bf02021-05-05 22:00:51 +0100118const (
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000119 soongSdkSnapshotVersionCurrent = "current"
Paul Duffin43f7bf02021-05-05 22:00:51 +0100120)
121
Paul Duffinb645ec82019-11-27 17:43:54 +0000122type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900123 content strings.Builder
124 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +0900125}
126
Paul Duffinb645ec82019-11-27 17:43:54 +0000127// generatedFile abstracts operations for writing contents into a file and emit a build rule
128// for the file.
129type generatedFile struct {
130 generatedContents
131 path android.OutputPath
132}
133
Jiyong Park232e7852019-11-04 12:23:40 +0900134func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900135 return &generatedFile{
Paul Duffinb645ec82019-11-27 17:43:54 +0000136 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park9b409bc2019-10-11 14:59:13 +0900137 }
138}
139
Paul Duffinb645ec82019-11-27 17:43:54 +0000140func (gc *generatedContents) Indent() {
141 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +0900142}
143
Paul Duffinb645ec82019-11-27 17:43:54 +0000144func (gc *generatedContents) Dedent() {
145 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +0900146}
147
Paul Duffina08e4dc2021-06-22 18:19:19 +0100148// IndentedPrintf will add spaces to indent the line to the appropriate level before printing the
149// arguments.
150func (gc *generatedContents) IndentedPrintf(format string, args ...interface{}) {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000151 _, _ = fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format, args...)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100152}
153
154// UnindentedPrintf does not add spaces to indent the line to the appropriate level before printing
155// the arguments.
156func (gc *generatedContents) UnindentedPrintf(format string, args ...interface{}) {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000157 _, _ = fmt.Fprintf(&(gc.content), format, args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900158}
159
160func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800161 rb := android.NewRuleBuilder(pctx, ctx)
Paul Duffin11108272020-05-11 22:59:25 +0100162
163 content := gf.content.String()
164
165 // ninja consumes newline characters in rspfile_content. Prevent it by
166 // escaping the backslash in the newline character. The extra backslash
167 // is removed when the rspfile is written to the actual script file
168 content = strings.ReplaceAll(content, "\n", "\\n")
169
Jiyong Park9b409bc2019-10-11 14:59:13 +0900170 rb.Command().
171 Implicits(implicits).
Martin Stjernholmee9b24e2021-04-20 15:54:21 +0100172 Text("echo -n").Text(proptools.ShellEscape(content)).
Paul Duffin11108272020-05-11 22:59:25 +0100173 // convert \\n to \n
Jiyong Park9b409bc2019-10-11 14:59:13 +0900174 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
175 rb.Command().
176 Text("chmod a+x").Output(gf.path)
Colin Crossf1a035e2020-11-16 17:32:30 -0800177 rb.Build(gf.path.Base(), "Build "+gf.path.Base())
Jiyong Park9b409bc2019-10-11 14:59:13 +0900178}
179
Paul Duffin13879572019-11-28 14:31:38 +0000180// Collect all the members.
181//
Paul Duffinb97b1572021-04-29 21:50:40 +0100182// Updates the sdk module with a list of sdkMemberVariantDep instances and details as to which
183// multilibs (32/64/both) are used by this sdk variant.
Paul Duffin6a7e9532020-03-20 17:50:07 +0000184func (s *sdk) collectMembers(ctx android.ModuleContext) {
185 s.multilibUsages = multilibNone
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000186 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
187 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100188 if memberTag, ok := tag.(android.SdkMemberDependencyTag); ok {
Paul Duffineee466e2021-04-27 23:17:56 +0100189 memberType := memberTag.SdkMemberType(child)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900190
Paul Duffin5cca7c42021-05-26 10:16:01 +0100191 // If a nil SdkMemberType was returned then this module should not be added to the sdk.
192 if memberType == nil {
193 return false
194 }
195
Paul Duffin13879572019-11-28 14:31:38 +0000196 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000197 if !memberType.IsInstance(child) {
198 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900199 }
Paul Duffin13879572019-11-28 14:31:38 +0000200
Paul Duffin6a7e9532020-03-20 17:50:07 +0000201 // Keep track of which multilib variants are used by the sdk.
202 s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType)
203
Paul Duffinb97b1572021-04-29 21:50:40 +0100204 var exportedComponentsInfo android.ExportedComponentsInfo
205 if ctx.OtherModuleHasProvider(child, android.ExportedComponentsInfoProvider) {
206 exportedComponentsInfo = ctx.OtherModuleProvider(child, android.ExportedComponentsInfoProvider).(android.ExportedComponentsInfo)
207 }
208
Paul Duffinc6ba1822022-05-06 09:38:02 +0000209 var container android.SdkAware
210 if parent != ctx.Module() {
211 container = parent.(android.SdkAware)
212 }
213
Paul Duffin1938dba2022-07-26 23:53:00 +0000214 minApiLevel := android.MinApiLevelForSdkSnapshot(ctx, child)
215
Paul Duffina7208112021-04-23 21:20:20 +0100216 export := memberTag.ExportMember()
Paul Duffinb97b1572021-04-29 21:50:40 +0100217 s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{
Paul Duffinc6ba1822022-05-06 09:38:02 +0000218 sdkVariant: s,
219 memberType: memberType,
220 variant: child.(android.SdkAware),
Paul Duffin1938dba2022-07-26 23:53:00 +0000221 minApiLevel: minApiLevel,
Paul Duffinc6ba1822022-05-06 09:38:02 +0000222 container: container,
223 export: export,
224 exportedComponentsInfo: exportedComponentsInfo,
Paul Duffinb97b1572021-04-29 21:50:40 +0100225 })
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000226
Paul Duffin2d3da312021-05-06 12:02:27 +0100227 // Recurse down into the member's dependencies as it may have dependencies that need to be
228 // automatically added to the sdk.
229 return true
Jiyong Park73c54ee2019-10-22 20:31:18 +0900230 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000231
232 return false
Paul Duffin13879572019-11-28 14:31:38 +0000233 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000234}
235
Paul Duffincc3132e2021-04-24 01:10:30 +0100236// groupMemberVariantsByMemberThenType groups the member variant dependencies so that all the
237// variants of each member are grouped together within an sdkMember instance.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000238//
Paul Duffincc3132e2021-04-24 01:10:30 +0100239// The sdkMember instances are then grouped into slices by member type. Within each such slice the
240// sdkMember instances appear in the order they were added as dependencies.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000241//
Paul Duffincc3132e2021-04-24 01:10:30 +0100242// Finally, the member type slices are concatenated together to form a single slice. The order in
243// which they are concatenated is the order in which the member types were registered in the
244// android.SdkMemberTypesRegistry.
Paul Duffinf861df72022-07-01 15:56:06 +0000245func (s *sdk) groupMemberVariantsByMemberThenType(ctx android.ModuleContext, targetBuildRelease *buildRelease, memberVariantDeps []sdkMemberVariantDep) []*sdkMember {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000246 byType := make(map[android.SdkMemberType][]*sdkMember)
247 byName := make(map[string]*sdkMember)
248
Paul Duffin21827262021-04-24 12:16:36 +0100249 for _, memberVariantDep := range memberVariantDeps {
250 memberType := memberVariantDep.memberType
251 variant := memberVariantDep.variant
Paul Duffin1356d8c2020-02-25 19:26:33 +0000252
253 name := ctx.OtherModuleName(variant)
254 member := byName[name]
255 if member == nil {
256 member = &sdkMember{memberType: memberType, name: name}
257 byName[name] = member
258 byType[memberType] = append(byType[memberType], member)
Liz Kammer96320df2022-05-12 20:40:00 -0400259 } else if member.memberType != memberType {
260 // validate whether this is the same member type or and overriding member type
261 if memberType.Overrides(member.memberType) {
262 member.memberType = memberType
263 } else if !member.memberType.Overrides(memberType) {
264 ctx.ModuleErrorf("Incompatible member types %q %q", member.memberType, memberType)
265 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000266 }
267
Paul Duffin1356d8c2020-02-25 19:26:33 +0000268 // Only append new variants to the list. This is needed because a member can be both
269 // exported by the sdk and also be a transitive sdk member.
270 member.variants = appendUniqueVariants(member.variants, variant)
271 }
Paul Duffin13879572019-11-28 14:31:38 +0000272 var members []*sdkMember
Paul Duffin62782de2021-07-14 12:05:16 +0100273 for _, memberListProperty := range s.memberTypeListProperties() {
Paul Duffinf861df72022-07-01 15:56:06 +0000274 memberType := memberListProperty.memberType
275
276 if !isMemberTypeSupportedByTargetBuildRelease(memberType, targetBuildRelease) {
277 continue
278 }
279
280 membersOfType := byType[memberType]
Paul Duffin13879572019-11-28 14:31:38 +0000281 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900282 }
283
Paul Duffin6a7e9532020-03-20 17:50:07 +0000284 return members
Jiyong Park73c54ee2019-10-22 20:31:18 +0900285}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900286
Paul Duffinf861df72022-07-01 15:56:06 +0000287// isMemberTypeSupportedByTargetBuildRelease returns true if the member type is supported by the
288// target build release.
289func isMemberTypeSupportedByTargetBuildRelease(memberType android.SdkMemberType, targetBuildRelease *buildRelease) bool {
290 supportedByTargetBuildRelease := true
291 supportedBuildReleases := memberType.SupportedBuildReleases()
292 if supportedBuildReleases == "" {
293 supportedBuildReleases = "S+"
294 }
295
296 set, err := parseBuildReleaseSet(supportedBuildReleases)
297 if err != nil {
298 panic(fmt.Errorf("member type %s has invalid supported build releases %q: %s",
299 memberType.SdkPropertyName(), supportedBuildReleases, err))
300 }
301 if !set.contains(targetBuildRelease) {
302 supportedByTargetBuildRelease = false
303 }
304 return supportedByTargetBuildRelease
305}
306
Paul Duffin72910952020-01-20 18:16:30 +0000307func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
308 for _, v := range variants {
309 if v == newVariant {
310 return variants
311 }
312 }
313 return append(variants, newVariant)
314}
315
Paul Duffin51509a12022-04-06 12:48:09 +0000316// BUILD_NUMBER_FILE is the name of the file in the snapshot zip that will contain the number of
317// the build from which the snapshot was produced.
318const BUILD_NUMBER_FILE = "snapshot-creation-build-number.txt"
319
Jiyong Park73c54ee2019-10-22 20:31:18 +0900320// SDK directory structure
321// <sdk_root>/
322// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
323// <api_ver>/ : below this directory are all auto-generated
324// Android.bp : definition of 'sdk_snapshot' module is here
325// aidl/
326// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
327// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900328// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900329// include/
330// bionic/libc/include/stdlib.h : an exported header file
331// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900332// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900333// <arch>/include/ : arch-specific exported headers
334// <arch>/include_gen/ : arch-specific generated headers
335// <arch>/lib/
336// libFoo.so : a stub library
337
Paul Duffin1938dba2022-07-26 23:53:00 +0000338func (s sdk) targetBuildRelease(ctx android.ModuleContext) *buildRelease {
339 config := ctx.Config()
340 targetBuildReleaseEnv := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", buildReleaseCurrent.name)
341 targetBuildRelease, err := nameToRelease(targetBuildReleaseEnv)
342 if err != nil {
343 ctx.ModuleErrorf("invalid SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE: %s", err)
344 targetBuildRelease = buildReleaseCurrent
345 }
346
347 return targetBuildRelease
348}
349
Jiyong Park232e7852019-11-04 12:23:40 +0900350// buildSnapshot is the main function in this source file. It creates rules to copy
351// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffinc6ba1822022-05-06 09:38:02 +0000352func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000353
Paul Duffin1938dba2022-07-26 23:53:00 +0000354 targetBuildRelease := s.targetBuildRelease(ctx)
355 targetApiLevel, err := android.ApiLevelFromUser(ctx, targetBuildRelease.name)
356 if err != nil {
357 targetApiLevel = android.FutureApiLevel
358 }
359
Paul Duffinb97b1572021-04-29 21:50:40 +0100360 // Aggregate all the sdkMemberVariantDep instances from all the sdk variants.
Paul Duffin62131702021-05-07 01:10:01 +0100361 hasLicenses := false
Paul Duffin21827262021-04-24 12:16:36 +0100362 var memberVariantDeps []sdkMemberVariantDep
Paul Duffin1356d8c2020-02-25 19:26:33 +0000363 for _, sdkVariant := range sdkVariants {
Paul Duffin21827262021-04-24 12:16:36 +0100364 memberVariantDeps = append(memberVariantDeps, sdkVariant.memberVariantDeps...)
Paul Duffinb97b1572021-04-29 21:50:40 +0100365 }
Paul Duffin865171e2020-03-02 18:38:15 +0000366
Paul Duffinb97b1572021-04-29 21:50:40 +0100367 // Filter out any sdkMemberVariantDep that is a component of another.
368 memberVariantDeps = filterOutComponents(ctx, memberVariantDeps)
Paul Duffin13f02712020-03-06 12:30:43 +0000369
Paul Duffin1938dba2022-07-26 23:53:00 +0000370 // Record the names of all the members, both explicitly specified and implicitly included. Also,
371 // record the names of any members that should be excluded from this snapshot.
Paul Duffinb97b1572021-04-29 21:50:40 +0100372 allMembersByName := make(map[string]struct{})
373 exportedMembersByName := make(map[string]struct{})
Paul Duffin1938dba2022-07-26 23:53:00 +0000374 excludedMembersByName := make(map[string]struct{})
Paul Duffin62131702021-05-07 01:10:01 +0100375
Paul Duffin1938dba2022-07-26 23:53:00 +0000376 addMember := func(name string, export bool, exclude bool) {
377 if exclude {
378 excludedMembersByName[name] = struct{}{}
379 return
380 }
381
Paul Duffinb97b1572021-04-29 21:50:40 +0100382 allMembersByName[name] = struct{}{}
383 if export {
384 exportedMembersByName[name] = struct{}{}
385 }
386 }
387
388 for _, memberVariantDep := range memberVariantDeps {
389 name := memberVariantDep.variant.Name()
390 export := memberVariantDep.export
391
Paul Duffin1938dba2022-07-26 23:53:00 +0000392 // If the minApiLevel of the member is greater than the target API level then exclude it from
393 // this snapshot.
394 exclude := memberVariantDep.minApiLevel.GreaterThan(targetApiLevel)
395
396 addMember(name, export, exclude)
Paul Duffinb97b1572021-04-29 21:50:40 +0100397
398 // Add any components provided by the module.
399 for _, component := range memberVariantDep.exportedComponentsInfo.Components {
Paul Duffin1938dba2022-07-26 23:53:00 +0000400 addMember(component, export, exclude)
Paul Duffinb97b1572021-04-29 21:50:40 +0100401 }
402
403 if memberVariantDep.memberType == android.LicenseModuleSdkMemberType {
404 hasLicenses = true
Paul Duffin865171e2020-03-02 18:38:15 +0000405 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000406 }
407
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000408 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900409
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000410 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000411
412 bpFile := &bpFile{
413 modules: make(map[string]*bpModule),
414 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000415
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000416 // Always add -current to the end
417 snapshotFileSuffix := "-current"
Paul Duffin43f7bf02021-05-05 22:00:51 +0100418
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000419 builder := &snapshotBuilder{
Paul Duffin13f02712020-03-06 12:30:43 +0000420 ctx: ctx,
421 sdk: s,
Paul Duffin13f02712020-03-06 12:30:43 +0000422 snapshotDir: snapshotDir.OutputPath,
423 copies: make(map[string]string),
424 filesToZip: []android.Path{bp.path},
425 bpFile: bpFile,
426 prebuiltModules: make(map[string]*bpModule),
427 allMembersByName: allMembersByName,
428 exportedMembersByName: exportedMembersByName,
Paul Duffin1938dba2022-07-26 23:53:00 +0000429 excludedMembersByName: excludedMembersByName,
Paul Duffin39abf8f2021-09-24 14:58:27 +0100430 targetBuildRelease: targetBuildRelease,
Jiyong Park73c54ee2019-10-22 20:31:18 +0900431 }
Paul Duffinac37c502019-11-26 18:02:20 +0000432 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900433
Paul Duffin62131702021-05-07 01:10:01 +0100434 // If the sdk snapshot includes any license modules then add a package module which has a
435 // default_applicable_licenses property. That will prevent the LSC license process from updating
436 // the generated Android.bp file to add a package module that includes all licenses used by all
437 // the modules in that package. That would be unnecessary as every module in the sdk should have
438 // their own licenses property specified.
439 if hasLicenses {
440 pkg := bpFile.newModule("package")
441 property := "default_applicable_licenses"
442 pkg.AddCommentForProperty(property, `
443A default list here prevents the license LSC from adding its own list which would
444be unnecessary as every module in the sdk already has its own licenses property.
445`)
446 pkg.AddProperty(property, []string{"Android-Apache-2.0"})
447 bpFile.AddModule(pkg)
448 }
449
Paul Duffin0df49682021-05-07 01:10:01 +0100450 // Group the variants for each member module together and then group the members of each member
451 // type together.
Paul Duffinf861df72022-07-01 15:56:06 +0000452 members := s.groupMemberVariantsByMemberThenType(ctx, targetBuildRelease, memberVariantDeps)
Paul Duffin0df49682021-05-07 01:10:01 +0100453
454 // Create the prebuilt modules for each of the member modules.
Paul Duffind19f8942021-07-14 12:08:37 +0100455 traits := s.gatherTraits()
Paul Duffin13ad94f2020-02-19 16:19:27 +0000456 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000457 memberType := member.memberType
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000458 if !memberType.ArePrebuiltsRequired() {
459 continue
460 }
Paul Duffin3a4eb502020-03-19 16:11:18 +0000461
Paul Duffind19f8942021-07-14 12:08:37 +0100462 name := member.name
Paul Duffin1938dba2022-07-26 23:53:00 +0000463 if _, ok := excludedMembersByName[name]; ok {
464 continue
465 }
466
Paul Duffind19f8942021-07-14 12:08:37 +0100467 requiredTraits := traits[name]
468 if requiredTraits == nil {
469 requiredTraits = android.EmptySdkMemberTraitSet()
470 }
471
472 // Create the snapshot for the member.
473 memberCtx := &memberContext{ctx, builder, memberType, name, requiredTraits}
Paul Duffin3a4eb502020-03-19 16:11:18 +0000474
475 prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100476 s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
Jiyong Park73c54ee2019-10-22 20:31:18 +0900477 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900478
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000479 // Create a transformer that will transform a module by replacing any references
Paul Duffin72910952020-01-20 18:16:30 +0000480 // to internal members with a unique module name and setting prefer: false.
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000481 snapshotTransformer := snapshotTransformation{
Paul Duffin64fb5262021-05-05 21:36:04 +0100482 builder: builder,
Paul Duffin64fb5262021-05-05 21:36:04 +0100483 }
Paul Duffin72910952020-01-20 18:16:30 +0000484
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000485 for _, module := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000486 // Prune any empty property sets.
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000487 module = module.transform(pruneEmptySetTransformer{})
Paul Duffina78f3a72020-02-21 16:29:35 +0000488
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000489 // Transform the module module to make it suitable for use in the snapshot.
490 module.transform(snapshotTransformer)
491 bpFile.AddModule(module)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100492 }
Paul Duffin26197a62021-04-24 00:34:10 +0100493
494 // generate Android.bp
495 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
496 generateBpContents(&bp.generatedContents, bpFile)
497
498 contents := bp.content.String()
Paul Duffin39abf8f2021-09-24 14:58:27 +0100499 // If the snapshot is being generated for the current build release then check the syntax to make
500 // sure that it is compatible.
Paul Duffin42a49f12022-08-17 22:09:55 +0000501 if targetBuildRelease == buildReleaseCurrent {
Paul Duffin39abf8f2021-09-24 14:58:27 +0100502 syntaxCheckSnapshotBpFile(ctx, contents)
503 }
Paul Duffin26197a62021-04-24 00:34:10 +0100504
505 bp.build(pctx, ctx, nil)
506
Paul Duffin51509a12022-04-06 12:48:09 +0000507 // Copy the build number file into the snapshot.
508 builder.CopyToSnapshot(ctx.Config().BuildNumberFile(ctx), BUILD_NUMBER_FILE)
509
Paul Duffin74f1dcd2022-07-18 13:18:23 +0000510 filesToZip := android.SortedUniquePaths(builder.filesToZip)
Paul Duffin26197a62021-04-24 00:34:10 +0100511
512 // zip them all
Paul Duffinc6ba1822022-05-06 09:38:02 +0000513 zipPath := fmt.Sprintf("%s%s.zip", ctx.ModuleName(), snapshotFileSuffix)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100514 outputZipFile := android.PathForModuleOut(ctx, zipPath).OutputPath
Paul Duffin26197a62021-04-24 00:34:10 +0100515 outputDesc := "Building snapshot for " + ctx.ModuleName()
516
517 // If there are no zips to merge then generate the output zip directly.
518 // Otherwise, generate an intermediate zip file into which other zips can be
519 // merged.
520 var zipFile android.OutputPath
521 var desc string
522 if len(builder.zipsToMerge) == 0 {
523 zipFile = outputZipFile
524 desc = outputDesc
525 } else {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000526 intermediatePath := fmt.Sprintf("%s%s.unmerged.zip", ctx.ModuleName(), snapshotFileSuffix)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100527 zipFile = android.PathForModuleOut(ctx, intermediatePath).OutputPath
Paul Duffin26197a62021-04-24 00:34:10 +0100528 desc = "Building intermediate snapshot for " + ctx.ModuleName()
529 }
530
531 ctx.Build(pctx, android.BuildParams{
532 Description: desc,
533 Rule: zipFiles,
534 Inputs: filesToZip,
535 Output: zipFile,
536 Args: map[string]string{
537 "basedir": builder.snapshotDir.String(),
538 },
539 })
540
541 if len(builder.zipsToMerge) != 0 {
542 ctx.Build(pctx, android.BuildParams{
543 Description: outputDesc,
544 Rule: mergeZips,
545 Input: zipFile,
Paul Duffin74f1dcd2022-07-18 13:18:23 +0000546 Inputs: android.SortedUniquePaths(builder.zipsToMerge),
Paul Duffin26197a62021-04-24 00:34:10 +0100547 Output: outputZipFile,
548 })
549 }
550
Paul Duffinc6ba1822022-05-06 09:38:02 +0000551 modules := s.generateInfoData(ctx, memberVariantDeps)
552
553 // Output the modules information as pretty printed JSON.
554 info := newGeneratedFile(ctx, fmt.Sprintf("%s%s.info", ctx.ModuleName(), snapshotFileSuffix))
555 output, err := json.MarshalIndent(modules, "", " ")
556 if err != nil {
557 ctx.ModuleErrorf("error generating %q: %s", info, err)
558 }
559 builder.infoContents = string(output)
560 info.generatedContents.UnindentedPrintf("%s", output)
561 info.build(pctx, ctx, nil)
562 infoPath := info.path
563 installedInfo := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), infoPath.Base(), infoPath)
564 s.infoFile = android.OptionalPathForPath(installedInfo)
565
566 // Install the zip, making sure that the info file has been installed as well.
567 installedZip := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), outputZipFile.Base(), outputZipFile, installedInfo)
568 s.snapshotFile = android.OptionalPathForPath(installedZip)
569}
570
571type moduleInfo struct {
572 // The type of the module, e.g. java_sdk_library
573 moduleType string
574 // The name of the module.
575 name string
576 // A list of additional dependencies of the module.
577 deps []string
Paul Duffin958806b2022-05-16 13:10:47 +0000578 // Additional member specific properties.
579 // These will be added into the generated JSON alongside the above properties.
580 memberSpecific map[string]interface{}
Paul Duffinc6ba1822022-05-06 09:38:02 +0000581}
582
583func (m *moduleInfo) MarshalJSON() ([]byte, error) {
584 buffer := bytes.Buffer{}
585
586 separator := ""
587 writeObjectPair := func(key string, value interface{}) {
588 buffer.WriteString(fmt.Sprintf("%s%q: ", separator, key))
589 b, err := json.Marshal(value)
590 if err != nil {
591 panic(err)
592 }
593 buffer.Write(b)
594 separator = ","
595 }
596
597 buffer.WriteString("{")
598 writeObjectPair("@type", m.moduleType)
599 writeObjectPair("@name", m.name)
600 if m.deps != nil {
601 writeObjectPair("@deps", m.deps)
602 }
Paul Duffin958806b2022-05-16 13:10:47 +0000603 for _, k := range android.SortedStringKeys(m.memberSpecific) {
604 v := m.memberSpecific[k]
Paul Duffinc6ba1822022-05-06 09:38:02 +0000605 writeObjectPair(k, v)
606 }
607 buffer.WriteString("}")
608 return buffer.Bytes(), nil
609}
610
611var _ json.Marshaler = (*moduleInfo)(nil)
612
613// generateInfoData creates a list of moduleInfo structures that will be marshalled into JSON.
614func (s *sdk) generateInfoData(ctx android.ModuleContext, memberVariantDeps []sdkMemberVariantDep) interface{} {
615 modules := []*moduleInfo{}
616 sdkInfo := moduleInfo{
Paul Duffin958806b2022-05-16 13:10:47 +0000617 moduleType: "sdk",
618 name: ctx.ModuleName(),
619 memberSpecific: map[string]interface{}{},
Paul Duffinc6ba1822022-05-06 09:38:02 +0000620 }
621 modules = append(modules, &sdkInfo)
622
623 name2Info := map[string]*moduleInfo{}
624 getModuleInfo := func(module android.Module) *moduleInfo {
625 name := module.Name()
626 info := name2Info[name]
627 if info == nil {
628 moduleType := ctx.OtherModuleType(module)
629 // Remove any suffix added when creating modules dynamically.
630 moduleType = strings.Split(moduleType, "__")[0]
631 info = &moduleInfo{
632 moduleType: moduleType,
633 name: name,
634 }
Paul Duffin958806b2022-05-16 13:10:47 +0000635
636 additionalSdkInfo := ctx.OtherModuleProvider(module, android.AdditionalSdkInfoProvider).(android.AdditionalSdkInfo)
637 info.memberSpecific = additionalSdkInfo.Properties
638
Paul Duffinc6ba1822022-05-06 09:38:02 +0000639 name2Info[name] = info
640 }
641 return info
642 }
643
644 for _, memberVariantDep := range memberVariantDeps {
645 propertyName := memberVariantDep.memberType.SdkPropertyName()
646 var list []string
Paul Duffin958806b2022-05-16 13:10:47 +0000647 if v, ok := sdkInfo.memberSpecific[propertyName]; ok {
Paul Duffinc6ba1822022-05-06 09:38:02 +0000648 list = v.([]string)
649 }
650
651 memberName := memberVariantDep.variant.Name()
652 list = append(list, memberName)
Paul Duffin958806b2022-05-16 13:10:47 +0000653 sdkInfo.memberSpecific[propertyName] = android.SortedUniqueStrings(list)
Paul Duffinc6ba1822022-05-06 09:38:02 +0000654
655 if memberVariantDep.container != nil {
656 containerInfo := getModuleInfo(memberVariantDep.container)
657 containerInfo.deps = android.SortedUniqueStrings(append(containerInfo.deps, memberName))
658 }
659
660 // Make sure that the module info is created for each module.
661 getModuleInfo(memberVariantDep.variant)
662 }
663
664 for _, memberName := range android.SortedStringKeys(name2Info) {
665 info := name2Info[memberName]
666 modules = append(modules, info)
667 }
668
669 return modules
Paul Duffin26197a62021-04-24 00:34:10 +0100670}
671
Paul Duffinb97b1572021-04-29 21:50:40 +0100672// filterOutComponents removes any item from the deps list that is a component of another item in
673// the deps list, e.g. if the deps list contains "foo" and "foo.stubs" which is component of "foo"
674// then it will remove "foo.stubs" from the deps.
675func filterOutComponents(ctx android.ModuleContext, deps []sdkMemberVariantDep) []sdkMemberVariantDep {
676 // Collate the set of components that all the modules added to the sdk provide.
677 components := map[string]*sdkMemberVariantDep{}
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000678 for i := range deps {
Paul Duffinb97b1572021-04-29 21:50:40 +0100679 dep := &deps[i]
680 for _, c := range dep.exportedComponentsInfo.Components {
681 components[c] = dep
682 }
683 }
684
685 // If no module provides components then return the input deps unfiltered.
686 if len(components) == 0 {
687 return deps
688 }
689
690 filtered := make([]sdkMemberVariantDep, 0, len(deps))
691 for _, dep := range deps {
692 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(dep.variant))
693 if owner, ok := components[name]; ok {
694 // This is a component of another module that is a member of the sdk.
695
696 // If the component is exported but the owning module is not then the configuration is not
697 // supported.
698 if dep.export && !owner.export {
699 ctx.ModuleErrorf("Module %s is internal to the SDK but provides component %s which is used outside the SDK")
700 continue
701 }
702
703 // This module must not be added to the list of members of the sdk as that would result in a
704 // duplicate module in the sdk snapshot.
705 continue
706 }
707
708 filtered = append(filtered, dep)
709 }
710 return filtered
711}
712
Paul Duffinf88d8e02020-05-07 20:21:34 +0100713// Check the syntax of the generated Android.bp file contents and if they are
714// invalid then log an error with the contents (tagged with line numbers) and the
715// errors that were found so that it is easy to see where the problem lies.
716func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) {
717 errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents)
718 if len(errs) != 0 {
719 message := &strings.Builder{}
720 _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot:
721
722Generated Android.bp contents
723========================================================================
724`)
725 for i, line := range strings.Split(contents, "\n") {
726 _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line)
727 }
728
729 _, _ = fmt.Fprint(message, `
730========================================================================
731
732Errors found:
733`)
734
735 for _, err := range errs {
736 _, _ = fmt.Fprintf(message, "%s\n", err.Error())
737 }
738
739 ctx.ModuleErrorf("%s", message.String())
740 }
741}
742
Paul Duffin4b8b7932020-05-06 12:35:38 +0100743func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
744 err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
745 if err != nil {
746 ctx.ModuleErrorf("error extracting common properties: %s", err)
747 }
748}
749
Paul Duffinfbe470e2021-04-24 12:37:13 +0100750// snapshotModuleStaticProperties contains snapshot static (i.e. not dynamically generated) properties.
751type snapshotModuleStaticProperties struct {
752 Compile_multilib string `android:"arch_variant"`
753}
754
Paul Duffin2d1bb892021-04-24 11:32:59 +0100755// combinedSnapshotModuleProperties are the properties that are associated with the snapshot module.
756type combinedSnapshotModuleProperties struct {
757 // The sdk variant from which this information was collected.
758 sdkVariant *sdk
759
760 // Static snapshot module properties.
761 staticProperties *snapshotModuleStaticProperties
762
763 // The dynamically generated member list properties.
764 dynamicProperties interface{}
765}
766
767// collateSnapshotModuleInfo collates all the snapshot module info from supplied sdk variants.
Paul Duffincd064672021-04-24 00:47:29 +0100768func (s *sdk) collateSnapshotModuleInfo(ctx android.BaseModuleContext, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) []*combinedSnapshotModuleProperties {
769 sdkVariantToCombinedProperties := map[*sdk]*combinedSnapshotModuleProperties{}
Paul Duffin2d1bb892021-04-24 11:32:59 +0100770 var list []*combinedSnapshotModuleProperties
771 for _, sdkVariant := range sdkVariants {
772 staticProperties := &snapshotModuleStaticProperties{
773 Compile_multilib: sdkVariant.multilibUsages.String(),
774 }
Paul Duffin62782de2021-07-14 12:05:16 +0100775 dynamicProperties := s.dynamicSdkMemberTypes.createMemberTypeListProperties()
Paul Duffin2d1bb892021-04-24 11:32:59 +0100776
Paul Duffincd064672021-04-24 00:47:29 +0100777 combinedProperties := &combinedSnapshotModuleProperties{
Paul Duffin2d1bb892021-04-24 11:32:59 +0100778 sdkVariant: sdkVariant,
779 staticProperties: staticProperties,
780 dynamicProperties: dynamicProperties,
Paul Duffincd064672021-04-24 00:47:29 +0100781 }
782 sdkVariantToCombinedProperties[sdkVariant] = combinedProperties
783
784 list = append(list, combinedProperties)
Paul Duffin2d1bb892021-04-24 11:32:59 +0100785 }
Paul Duffincd064672021-04-24 00:47:29 +0100786
787 for _, memberVariantDep := range memberVariantDeps {
788 // If the member dependency is internal then do not add the dependency to the snapshot member
789 // list properties.
790 if !memberVariantDep.export {
791 continue
792 }
793
794 combined := sdkVariantToCombinedProperties[memberVariantDep.sdkVariant]
Paul Duffin62782de2021-07-14 12:05:16 +0100795 memberListProperty := s.memberTypeListProperty(memberVariantDep.memberType)
Paul Duffincd064672021-04-24 00:47:29 +0100796 memberName := ctx.OtherModuleName(memberVariantDep.variant)
797
Paul Duffin13082052021-05-11 00:31:38 +0100798 if memberListProperty.getter == nil {
799 continue
800 }
801
Paul Duffincd064672021-04-24 00:47:29 +0100802 // Append the member to the appropriate list, if it is not already present in the list.
Paul Duffin13082052021-05-11 00:31:38 +0100803 memberList := memberListProperty.getter(combined.dynamicProperties)
Paul Duffincd064672021-04-24 00:47:29 +0100804 if !android.InList(memberName, memberList) {
805 memberList = append(memberList, memberName)
806 }
Paul Duffin13082052021-05-11 00:31:38 +0100807 memberListProperty.setter(combined.dynamicProperties, memberList)
Paul Duffincd064672021-04-24 00:47:29 +0100808 }
809
Paul Duffin2d1bb892021-04-24 11:32:59 +0100810 return list
811}
812
813func (s *sdk) optimizeSnapshotModuleProperties(ctx android.ModuleContext, list []*combinedSnapshotModuleProperties) *combinedSnapshotModuleProperties {
814
815 // Extract the dynamic properties and add them to a list of propertiesContainer.
816 propertyContainers := []propertiesContainer{}
817 for _, i := range list {
818 propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{
819 sdkVariant: i.sdkVariant,
820 properties: i.dynamicProperties,
821 })
822 }
823
824 // Extract the common members, removing them from the original properties.
Paul Duffin62782de2021-07-14 12:05:16 +0100825 commonDynamicProperties := s.dynamicSdkMemberTypes.createMemberTypeListProperties()
Paul Duffin2d1bb892021-04-24 11:32:59 +0100826 extractor := newCommonValueExtractor(commonDynamicProperties)
827 extractCommonProperties(ctx, extractor, commonDynamicProperties, propertyContainers)
828
829 // Extract the static properties and add them to a list of propertiesContainer.
830 propertyContainers = []propertiesContainer{}
831 for _, i := range list {
832 propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{
833 sdkVariant: i.sdkVariant,
834 properties: i.staticProperties,
835 })
836 }
837
838 commonStaticProperties := &snapshotModuleStaticProperties{}
839 extractor = newCommonValueExtractor(commonStaticProperties)
840 extractCommonProperties(ctx, extractor, &commonStaticProperties, propertyContainers)
841
842 return &combinedSnapshotModuleProperties{
843 sdkVariant: nil,
844 staticProperties: commonStaticProperties,
845 dynamicProperties: commonDynamicProperties,
846 }
847}
848
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000849type propertyTag struct {
850 name string
851}
852
Paul Duffin94289702021-09-09 15:38:32 +0100853var _ android.BpPropertyTag = propertyTag{}
854
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000855// BpPropertyTag instances to add to a property that contains references to other sdk members.
Paul Duffin0cb37b92020-03-04 14:52:46 +0000856//
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000857// These will ensure that the referenced modules are available, if required.
Paul Duffin13f02712020-03-06 12:30:43 +0000858var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
Paul Duffin13f02712020-03-06 12:30:43 +0000859var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000860
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000861type snapshotTransformation struct {
Paul Duffine6c0d842020-01-15 14:08:51 +0000862 identityTransformation
863 builder *snapshotBuilder
864}
865
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000866func (t snapshotTransformation) transformModule(module *bpModule) *bpModule {
Paul Duffin72910952020-01-20 18:16:30 +0000867 // If the module is an internal member then use a unique name for it.
Paul Duffin0df49682021-05-07 01:10:01 +0100868 name := module.Name()
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000869 module.setProperty("name", t.builder.snapshotSdkMemberName(name, true))
Paul Duffin72910952020-01-20 18:16:30 +0000870 return module
871}
872
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000873func (t snapshotTransformation) transformProperty(_ string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +0000874 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
875 required := tag == requiredSdkMemberReferencePropertyTag
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000876 return t.builder.snapshotSdkMemberNames(value.([]string), required), tag
Paul Duffin72910952020-01-20 18:16:30 +0000877 } else {
878 return value, tag
879 }
880}
881
Paul Duffina78f3a72020-02-21 16:29:35 +0000882type pruneEmptySetTransformer struct {
883 identityTransformation
884}
885
886var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
887
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000888func (t pruneEmptySetTransformer) transformPropertySetAfterContents(_ string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
Paul Duffina78f3a72020-02-21 16:29:35 +0000889 if len(propertySet.properties) == 0 {
890 return nil, nil
891 } else {
892 return propertySet, tag
893 }
894}
895
Paul Duffinb645ec82019-11-27 17:43:54 +0000896func generateBpContents(contents *generatedContents, bpFile *bpFile) {
Paul Duffina08e4dc2021-06-22 18:19:19 +0100897 contents.IndentedPrintf("// This is auto-generated. DO NOT EDIT.\n")
Paul Duffinb645ec82019-11-27 17:43:54 +0000898 for _, bpModule := range bpFile.order {
Paul Duffinb01ac4b2022-05-24 20:10:05 +0000899 contents.IndentedPrintf("\n")
900 contents.IndentedPrintf("%s {\n", bpModule.moduleType)
901 outputPropertySet(contents, bpModule.bpPropertySet)
902 contents.IndentedPrintf("}\n")
Paul Duffinb645ec82019-11-27 17:43:54 +0000903 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000904}
905
906func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
907 contents.Indent()
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000908
Paul Duffin0df49682021-05-07 01:10:01 +0100909 addComment := func(name string) {
910 if text, ok := set.comments[name]; ok {
911 for _, line := range strings.Split(text, "\n") {
Paul Duffina08e4dc2021-06-22 18:19:19 +0100912 contents.IndentedPrintf("// %s\n", line)
Paul Duffin0df49682021-05-07 01:10:01 +0100913 }
914 }
915 }
916
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000917 // Output the properties first, followed by the nested sets. This ensures a
918 // consistent output irrespective of whether property sets are created before
919 // or after the properties. This simplifies the creation of the module.
Paul Duffinb645ec82019-11-27 17:43:54 +0000920 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000921 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000922
Paul Duffin0df49682021-05-07 01:10:01 +0100923 // Do not write property sets in the properties phase.
924 if _, ok := value.(*bpPropertySet); ok {
925 continue
926 }
927
928 addComment(name)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100929 reflectValue := reflect.ValueOf(value)
930 outputNamedValue(contents, name, reflectValue)
Paul Duffinb645ec82019-11-27 17:43:54 +0000931 }
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000932
933 for _, name := range set.order {
934 value := set.getValue(name)
935
936 // Only write property sets in the sets phase.
937 switch v := value.(type) {
938 case *bpPropertySet:
Paul Duffin0df49682021-05-07 01:10:01 +0100939 addComment(name)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100940 contents.IndentedPrintf("%s: {\n", name)
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000941 outputPropertySet(contents, v)
Paul Duffina08e4dc2021-06-22 18:19:19 +0100942 contents.IndentedPrintf("},\n")
Paul Duffin07ef3cb2020-03-11 18:17:42 +0000943 }
944 }
945
Paul Duffinb645ec82019-11-27 17:43:54 +0000946 contents.Dedent()
947}
948
Paul Duffina08e4dc2021-06-22 18:19:19 +0100949// outputNamedValue outputs a value that has an associated name. The name will be indented, followed
950// by the value and then followed by a , and a newline.
951func outputNamedValue(contents *generatedContents, name string, value reflect.Value) {
952 contents.IndentedPrintf("%s: ", name)
953 outputUnnamedValue(contents, value)
954 contents.UnindentedPrintf(",\n")
955}
956
957// outputUnnamedValue outputs a single value. The value is not indented and is not followed by
958// either a , or a newline. With multi-line values, e.g. slices, all but the first line will be
959// indented and all but the last line will end with a newline.
960func outputUnnamedValue(contents *generatedContents, value reflect.Value) {
961 valueType := value.Type()
962 switch valueType.Kind() {
963 case reflect.Bool:
964 contents.UnindentedPrintf("%t", value.Bool())
965
966 case reflect.String:
967 contents.UnindentedPrintf("%q", value)
968
Paul Duffin51227d82021-05-18 12:54:27 +0100969 case reflect.Ptr:
970 outputUnnamedValue(contents, value.Elem())
971
Paul Duffina08e4dc2021-06-22 18:19:19 +0100972 case reflect.Slice:
973 length := value.Len()
974 if length == 0 {
975 contents.UnindentedPrintf("[]")
Paul Duffina08e4dc2021-06-22 18:19:19 +0100976 } else {
Paul Duffin51227d82021-05-18 12:54:27 +0100977 firstValue := value.Index(0)
978 if length == 1 && !multiLineValue(firstValue) {
979 contents.UnindentedPrintf("[")
980 outputUnnamedValue(contents, firstValue)
981 contents.UnindentedPrintf("]")
982 } else {
983 contents.UnindentedPrintf("[\n")
984 contents.Indent()
985 for i := 0; i < length; i++ {
986 itemValue := value.Index(i)
987 contents.IndentedPrintf("")
988 outputUnnamedValue(contents, itemValue)
989 contents.UnindentedPrintf(",\n")
990 }
991 contents.Dedent()
992 contents.IndentedPrintf("]")
Paul Duffina08e4dc2021-06-22 18:19:19 +0100993 }
Paul Duffina08e4dc2021-06-22 18:19:19 +0100994 }
995
Paul Duffin51227d82021-05-18 12:54:27 +0100996 case reflect.Struct:
997 // Avoid unlimited recursion by requiring every structure to implement android.BpPrintable.
998 v := value.Interface()
999 if _, ok := v.(android.BpPrintable); !ok {
1000 panic(fmt.Errorf("property value %#v of type %T does not implement android.BpPrintable", v, v))
1001 }
1002 contents.UnindentedPrintf("{\n")
1003 contents.Indent()
1004 for f := 0; f < valueType.NumField(); f++ {
1005 fieldType := valueType.Field(f)
1006 if fieldType.Anonymous {
1007 continue
1008 }
1009 fieldValue := value.Field(f)
1010 fieldName := fieldType.Name
1011 propertyName := proptools.PropertyNameForField(fieldName)
1012 outputNamedValue(contents, propertyName, fieldValue)
1013 }
1014 contents.Dedent()
1015 contents.IndentedPrintf("}")
1016
Paul Duffina08e4dc2021-06-22 18:19:19 +01001017 default:
1018 panic(fmt.Errorf("Unknown type: %T of value %#v", value, value))
1019 }
1020}
1021
Paul Duffin51227d82021-05-18 12:54:27 +01001022// multiLineValue returns true if the supplied value may require multiple lines in the output.
1023func multiLineValue(value reflect.Value) bool {
1024 kind := value.Kind()
1025 return kind == reflect.Slice || kind == reflect.Struct
1026}
1027
Paul Duffinac37c502019-11-26 18:02:20 +00001028func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +00001029 contents := &generatedContents{}
1030 generateBpContents(contents, s.builderForTests.bpFile)
1031 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +00001032}
1033
Paul Duffinc6ba1822022-05-06 09:38:02 +00001034func (s *sdk) GetInfoContentsForTests() string {
1035 return s.builderForTests.infoContents
1036}
1037
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001038type snapshotBuilder struct {
Paul Duffin43f7bf02021-05-05 22:00:51 +01001039 ctx android.ModuleContext
1040 sdk *sdk
1041
Paul Duffinb645ec82019-11-27 17:43:54 +00001042 snapshotDir android.OutputPath
1043 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +00001044
1045 // Map from destination to source of each copy - used to eliminate duplicates and
1046 // detect conflicts.
1047 copies map[string]string
1048
Paul Duffinb645ec82019-11-27 17:43:54 +00001049 filesToZip android.Paths
1050 zipsToMerge android.Paths
1051
1052 prebuiltModules map[string]*bpModule
1053 prebuiltOrder []*bpModule
Paul Duffin13f02712020-03-06 12:30:43 +00001054
1055 // The set of all members by name.
1056 allMembersByName map[string]struct{}
1057
1058 // The set of exported members by name.
1059 exportedMembersByName map[string]struct{}
Paul Duffin39abf8f2021-09-24 14:58:27 +01001060
Paul Duffin1938dba2022-07-26 23:53:00 +00001061 // The set of members which have been excluded from this snapshot; by name.
1062 excludedMembersByName map[string]struct{}
1063
Paul Duffin39abf8f2021-09-24 14:58:27 +01001064 // The target build release for which the snapshot is to be generated.
1065 targetBuildRelease *buildRelease
Paul Duffinc6ba1822022-05-06 09:38:02 +00001066
Paul Duffin958806b2022-05-16 13:10:47 +00001067 // The contents of the .info file that describes the sdk contents.
Paul Duffinc6ba1822022-05-06 09:38:02 +00001068 infoContents string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001069}
1070
1071func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +00001072 if existing, ok := s.copies[dest]; ok {
1073 if existing != src.String() {
1074 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
1075 return
1076 }
1077 } else {
1078 path := s.snapshotDir.Join(s.ctx, dest)
1079 s.ctx.Build(pctx, android.BuildParams{
1080 Rule: android.Cp,
1081 Input: src,
1082 Output: path,
1083 })
1084 s.filesToZip = append(s.filesToZip, path)
1085
1086 s.copies[dest] = src.String()
1087 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001088}
1089
Paul Duffin91547182019-11-12 19:39:36 +00001090func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
1091 ctx := s.ctx
1092
1093 // Repackage the zip file so that the entries are in the destDir directory.
1094 // This will allow the zip file to be merged into the snapshot.
1095 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +00001096
1097 ctx.Build(pctx, android.BuildParams{
1098 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
1099 Rule: repackageZip,
1100 Input: zipPath,
1101 Output: tmpZipPath,
1102 Args: map[string]string{
1103 "destdir": destDir,
1104 },
1105 })
Paul Duffin91547182019-11-12 19:39:36 +00001106
1107 // Add the repackaged zip file to the files to merge.
1108 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
1109}
1110
Paul Duffin9d8d6092019-12-05 18:19:29 +00001111func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
1112 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +00001113 if s.prebuiltModules[name] != nil {
1114 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
1115 }
1116
1117 m := s.bpFile.newModule(moduleType)
1118 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +00001119
Paul Duffinbefa4b92020-03-04 14:22:45 +00001120 variant := member.Variants()[0]
1121
Paul Duffin13f02712020-03-06 12:30:43 +00001122 if s.isInternalMember(name) {
Paul Duffin72910952020-01-20 18:16:30 +00001123 // An internal member is only referenced from the sdk snapshot which is in the
1124 // same package so can be marked as private.
1125 m.AddProperty("visibility", []string{"//visibility:private"})
1126 } else {
1127 // Extract visibility information from a member variant. All variants have the same
1128 // visibility so it doesn't matter which one is used.
Paul Duffin157f40f2020-09-29 16:01:08 +01001129 visibilityRules := android.EffectiveVisibilityRules(s.ctx, variant)
1130
1131 // Add any additional visibility rules needed for the prebuilts to reference each other.
1132 err := visibilityRules.Widen(s.sdk.properties.Prebuilt_visibility)
1133 if err != nil {
1134 s.ctx.PropertyErrorf("prebuilt_visibility", "%s", err)
1135 }
1136
1137 visibility := visibilityRules.Strings()
Paul Duffin72910952020-01-20 18:16:30 +00001138 if len(visibility) != 0 {
1139 m.AddProperty("visibility", visibility)
1140 }
Paul Duffin593b3c92019-12-05 14:31:48 +00001141 }
1142
Martin Stjernholm1e041092020-11-03 00:11:09 +00001143 // Where available copy apex_available properties from the member.
1144 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
1145 apexAvailable := apexAware.ApexAvailable()
1146 if len(apexAvailable) == 0 {
1147 // //apex_available:platform is the default.
1148 apexAvailable = []string{android.AvailableToPlatform}
1149 }
1150
1151 // Add in any baseline apex available settings.
1152 apexAvailable = append(apexAvailable, apex.BaselineApexAvailable(member.Name())...)
1153
1154 // Remove duplicates and sort.
1155 apexAvailable = android.FirstUniqueStrings(apexAvailable)
1156 sort.Strings(apexAvailable)
1157
1158 m.AddProperty("apex_available", apexAvailable)
1159 }
1160
Paul Duffinb0bb3762021-05-06 16:48:05 +01001161 // The licenses are the same for all variants.
1162 mctx := s.ctx
1163 licenseInfo := mctx.OtherModuleProvider(variant, android.LicenseInfoProvider).(android.LicenseInfo)
1164 if len(licenseInfo.Licenses) > 0 {
1165 m.AddPropertyWithTag("licenses", licenseInfo.Licenses, s.OptionalSdkMemberReferencePropertyTag())
1166 }
1167
Paul Duffin865171e2020-03-02 18:38:15 +00001168 deviceSupported := false
1169 hostSupported := false
1170
1171 for _, variant := range member.Variants() {
1172 osClass := variant.Target().Os.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001173 if osClass == android.Host {
Paul Duffin865171e2020-03-02 18:38:15 +00001174 hostSupported = true
1175 } else if osClass == android.Device {
1176 deviceSupported = true
1177 }
1178 }
1179
1180 addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
Paul Duffinb645ec82019-11-27 17:43:54 +00001181
1182 s.prebuiltModules[name] = m
1183 s.prebuiltOrder = append(s.prebuiltOrder, m)
1184 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001185}
1186
Paul Duffinc61783b2022-10-20 17:21:40 +01001187func (s *snapshotBuilder) AddInternalModule(properties android.SdkMemberProperties, moduleType string, nameSuffix string) android.BpModule {
1188 name := properties.Name() + "-" + nameSuffix
1189
1190 if s.prebuiltModules[name] != nil {
1191 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
1192 }
1193
1194 m := s.bpFile.newModule(moduleType)
1195 m.AddProperty("name", name)
1196 m.AddProperty("visibility", []string{"//visibility:private"})
1197
1198 s.prebuiltModules[name] = m
1199 s.prebuiltOrder = append(s.prebuiltOrder, m)
1200
1201 s.allMembersByName[name] = struct{}{}
1202 return m
1203}
1204
Paul Duffin865171e2020-03-02 18:38:15 +00001205func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
Paul Duffinb0bb3762021-05-06 16:48:05 +01001206 // If neither device or host is supported then this module does not support either so will not
1207 // recognize the properties.
1208 if !deviceSupported && !hostSupported {
1209 return
1210 }
1211
Paul Duffin865171e2020-03-02 18:38:15 +00001212 if !deviceSupported {
Paul Duffine44358f2019-11-26 18:04:12 +00001213 bpModule.AddProperty("device_supported", false)
1214 }
Paul Duffin865171e2020-03-02 18:38:15 +00001215 if hostSupported {
Paul Duffine44358f2019-11-26 18:04:12 +00001216 bpModule.AddProperty("host_supported", true)
1217 }
1218}
1219
Paul Duffin13f02712020-03-06 12:30:43 +00001220func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag {
1221 if required {
1222 return requiredSdkMemberReferencePropertyTag
1223 } else {
1224 return optionalSdkMemberReferencePropertyTag
1225 }
1226}
1227
1228func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag {
1229 return optionalSdkMemberReferencePropertyTag
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001230}
1231
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001232// Get a name for sdk snapshot member. If the member is private then generate a snapshot specific
1233// name. As part of the processing this checks to make sure that any required members are part of
1234// the snapshot.
Paul Duffinc61783b2022-10-20 17:21:40 +01001235func (s *snapshotBuilder) snapshotSdkMemberName(reference string, required bool) string {
1236 prefix := ""
1237 name := strings.TrimPrefix(reference, ":")
1238 if name != reference {
1239 prefix = ":"
1240 }
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001241 if _, ok := s.allMembersByName[name]; !ok {
Paul Duffin13f02712020-03-06 12:30:43 +00001242 if required {
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001243 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", name)
Paul Duffin13f02712020-03-06 12:30:43 +00001244 }
Paul Duffinc61783b2022-10-20 17:21:40 +01001245 return reference
Paul Duffin13f02712020-03-06 12:30:43 +00001246 }
1247
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001248 if s.isInternalMember(name) {
Paul Duffinc61783b2022-10-20 17:21:40 +01001249 return prefix + s.ctx.ModuleName() + "_" + name
Paul Duffin72910952020-01-20 18:16:30 +00001250 } else {
Paul Duffinc61783b2022-10-20 17:21:40 +01001251 return reference
Paul Duffin72910952020-01-20 18:16:30 +00001252 }
1253}
1254
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001255func (s *snapshotBuilder) snapshotSdkMemberNames(members []string, required bool) []string {
Paul Duffin72910952020-01-20 18:16:30 +00001256 var references []string = nil
1257 for _, m := range members {
Paul Duffin1938dba2022-07-26 23:53:00 +00001258 if _, ok := s.excludedMembersByName[m]; ok {
1259 continue
1260 }
Paul Duffinb01ac4b2022-05-24 20:10:05 +00001261 references = append(references, s.snapshotSdkMemberName(m, required))
Paul Duffin72910952020-01-20 18:16:30 +00001262 }
1263 return references
1264}
1265
Paul Duffin13f02712020-03-06 12:30:43 +00001266func (s *snapshotBuilder) isInternalMember(memberName string) bool {
1267 _, ok := s.exportedMembersByName[memberName]
1268 return !ok
1269}
1270
Martin Stjernholm89238f42020-07-10 00:14:03 +01001271// Add the properties from the given SdkMemberProperties to the blueprint
1272// property set. This handles common properties in SdkMemberPropertiesBase and
1273// calls the member-specific AddToPropertySet for the rest.
1274func addSdkMemberPropertiesToSet(ctx *memberContext, memberProperties android.SdkMemberProperties, targetPropertySet android.BpPropertySet) {
1275 if memberProperties.Base().Compile_multilib != "" {
1276 targetPropertySet.AddProperty("compile_multilib", memberProperties.Base().Compile_multilib)
1277 }
1278
1279 memberProperties.AddToPropertySet(ctx, targetPropertySet)
1280}
1281
Paul Duffin21827262021-04-24 12:16:36 +01001282// sdkMemberVariantDep represents a dependency from an sdk variant onto a member variant.
1283type sdkMemberVariantDep struct {
Paul Duffincd064672021-04-24 00:47:29 +01001284 // The sdk variant that depends (possibly indirectly) on the member variant.
1285 sdkVariant *sdk
Paul Duffinb97b1572021-04-29 21:50:40 +01001286
1287 // The type of sdk member the variant is to be treated as.
Paul Duffin1356d8c2020-02-25 19:26:33 +00001288 memberType android.SdkMemberType
Paul Duffinb97b1572021-04-29 21:50:40 +01001289
1290 // The variant that is added to the sdk.
1291 variant android.SdkAware
1292
Paul Duffinc6ba1822022-05-06 09:38:02 +00001293 // The optional container of this member, i.e. the module that is depended upon by the sdk
1294 // (possibly transitively) and whose dependency on this module is why it was added to the sdk.
1295 // Is nil if this a direct dependency of the sdk.
1296 container android.SdkAware
1297
Paul Duffinb97b1572021-04-29 21:50:40 +01001298 // True if the member should be exported, i.e. accessible, from outside the sdk.
1299 export bool
1300
1301 // The names of additional component modules provided by the variant.
1302 exportedComponentsInfo android.ExportedComponentsInfo
Paul Duffin1938dba2022-07-26 23:53:00 +00001303
1304 // The minimum API level on which this module is supported.
1305 minApiLevel android.ApiLevel
Paul Duffin1356d8c2020-02-25 19:26:33 +00001306}
1307
Paul Duffin13879572019-11-28 14:31:38 +00001308var _ android.SdkMember = (*sdkMember)(nil)
1309
Paul Duffin21827262021-04-24 12:16:36 +01001310// sdkMember groups all the variants of a specific member module together along with the name of the
1311// module and the member type. This is used to generate the prebuilt modules for a specific member.
Paul Duffin13879572019-11-28 14:31:38 +00001312type sdkMember struct {
1313 memberType android.SdkMemberType
1314 name string
1315 variants []android.SdkAware
1316}
1317
1318func (m *sdkMember) Name() string {
1319 return m.name
1320}
1321
1322func (m *sdkMember) Variants() []android.SdkAware {
1323 return m.variants
1324}
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001325
Paul Duffin9c3760e2020-03-16 19:52:08 +00001326// Track usages of multilib variants.
1327type multilibUsage int
1328
1329const (
1330 multilibNone multilibUsage = 0
1331 multilib32 multilibUsage = 1
1332 multilib64 multilibUsage = 2
1333 multilibBoth = multilib32 | multilib64
1334)
1335
1336// Add the multilib that is used in the arch type.
1337func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage {
1338 multilib := archType.Multilib
1339 switch multilib {
1340 case "":
1341 return m
1342 case "lib32":
1343 return m | multilib32
1344 case "lib64":
1345 return m | multilib64
1346 default:
1347 panic(fmt.Errorf("Unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib))
1348 }
1349}
1350
1351func (m multilibUsage) String() string {
1352 switch m {
1353 case multilibNone:
1354 return ""
1355 case multilib32:
1356 return "32"
1357 case multilib64:
1358 return "64"
1359 case multilibBoth:
1360 return "both"
1361 default:
1362 panic(fmt.Errorf("Unknown multilib value, found %b, expected one of %b, %b, %b or %b",
1363 m, multilibNone, multilib32, multilib64, multilibBoth))
1364 }
1365}
1366
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001367// TODO(187910671): BEGIN - Remove once modules do not have an APEX and default variant.
1368// variantCoordinate contains the coordinates used to identify a variant of an SDK member.
1369type variantCoordinate struct {
1370 // osType identifies the OS target of a variant.
1371 osType android.OsType
1372 // archId identifies the architecture and whether it is for the native bridge.
1373 archId archId
1374 // image is the image variant name.
1375 image string
1376 // linkType is the link type name.
1377 linkType string
1378}
1379
1380func getVariantCoordinate(ctx *memberContext, variant android.Module) variantCoordinate {
1381 linkType := ""
1382 if len(ctx.MemberType().SupportedLinkages()) > 0 {
1383 linkType = getLinkType(variant)
1384 }
1385 return variantCoordinate{
1386 osType: variant.Target().Os,
1387 archId: archIdFromTarget(variant.Target()),
1388 image: variant.ImageVariation().Variation,
1389 linkType: linkType,
1390 }
1391}
1392
1393// selectApexVariantsWhereAvailable filters the input list of variants by selecting the APEX
1394// specific variant for a specific variantCoordinate when there is both an APEX and default variant.
1395//
1396// There is a long-standing issue where a module that is added to an APEX has both an APEX and
1397// default/platform variant created even when the module does not require a platform variant. As a
1398// result an indirect dependency onto a module via the APEX will use the APEX variant, whereas a
1399// direct dependency onto the module will use the default/platform variant. That would result in a
1400// failure while attempting to optimize the properties for a member as it would have two variants
1401// when only one was expected.
1402//
1403// This function mitigates that problem by detecting when there are two variants that differ only
1404// by apex variant, where one is the default/platform variant and one is the APEX variant. In that
1405// case it picks the APEX variant. It picks the APEX variant because that is the behavior that would
1406// be expected
1407func selectApexVariantsWhereAvailable(ctx *memberContext, variants []android.SdkAware) []android.SdkAware {
1408 moduleCtx := ctx.sdkMemberContext
1409
1410 // Group the variants by coordinates.
1411 variantsByCoord := make(map[variantCoordinate][]android.SdkAware)
1412 for _, variant := range variants {
1413 coord := getVariantCoordinate(ctx, variant)
1414 variantsByCoord[coord] = append(variantsByCoord[coord], variant)
1415 }
1416
1417 toDiscard := make(map[android.SdkAware]struct{})
1418 for coord, list := range variantsByCoord {
1419 count := len(list)
1420 if count == 1 {
1421 continue
1422 }
1423
1424 variantsByApex := make(map[string]android.SdkAware)
1425 conflictDetected := false
1426 for _, variant := range list {
1427 apexInfo := moduleCtx.OtherModuleProvider(variant, android.ApexInfoProvider).(android.ApexInfo)
1428 apexVariationName := apexInfo.ApexVariationName
1429 // If there are two variants for a specific APEX variation then there is conflict.
1430 if _, ok := variantsByApex[apexVariationName]; ok {
1431 conflictDetected = true
1432 break
1433 }
1434 variantsByApex[apexVariationName] = variant
1435 }
1436
1437 // If there are more than 2 apex variations or one of the apex variations is not the
1438 // default/platform variation then there is a conflict.
1439 if len(variantsByApex) != 2 {
1440 conflictDetected = true
1441 } else if _, ok := variantsByApex[""]; !ok {
1442 conflictDetected = true
1443 }
1444
1445 // If there are no conflicts then add the default/platform variation to the list to remove.
1446 if !conflictDetected {
1447 toDiscard[variantsByApex[""]] = struct{}{}
1448 continue
1449 }
1450
1451 // There are duplicate variants at this coordinate and they are not the default and APEX variant
1452 // so fail.
1453 variantDescriptions := []string{}
1454 for _, m := range list {
1455 variantDescriptions = append(variantDescriptions, fmt.Sprintf(" %s", m.String()))
1456 }
1457
1458 moduleCtx.ModuleErrorf("multiple conflicting variants detected for OsType{%s}, %s, Image{%s}, Link{%s}\n%s",
1459 coord.osType, coord.archId.String(), coord.image, coord.linkType,
1460 strings.Join(variantDescriptions, "\n"))
1461 }
1462
1463 // If there are any variants to discard then remove them from the list of variants, while
1464 // preserving the order.
1465 if len(toDiscard) > 0 {
1466 filtered := []android.SdkAware{}
1467 for _, variant := range variants {
1468 if _, ok := toDiscard[variant]; !ok {
1469 filtered = append(filtered, variant)
1470 }
1471 }
1472 variants = filtered
1473 }
1474
1475 return variants
1476}
1477
1478// TODO(187910671): END - Remove once modules do not have an APEX and default variant.
1479
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001480type baseInfo struct {
1481 Properties android.SdkMemberProperties
1482}
1483
Paul Duffinf34f6d82020-04-30 15:48:31 +01001484func (b *baseInfo) optimizableProperties() interface{} {
1485 return b.Properties
1486}
1487
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001488type osTypeSpecificInfo struct {
1489 baseInfo
1490
Paul Duffin00e46802020-03-12 20:40:35 +00001491 osType android.OsType
1492
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001493 // The list of arch type specific info for this os type.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001494 //
1495 // Nil if there is one variant whose arch type is common
1496 archInfos []*archTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001497}
1498
Paul Duffin4b8b7932020-05-06 12:35:38 +01001499var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
1500
Paul Duffinfc8dd232020-03-17 12:51:37 +00001501type variantPropertiesFactoryFunc func() android.SdkMemberProperties
1502
Paul Duffin00e46802020-03-12 20:40:35 +00001503// Create a new osTypeSpecificInfo for the specified os type and its properties
1504// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001505func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
Paul Duffin00e46802020-03-12 20:40:35 +00001506 osInfo := &osTypeSpecificInfo{
1507 osType: osType,
1508 }
1509
1510 osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
1511 properties := variantPropertiesFactory()
1512 properties.Base().Os = osType
1513 return properties
1514 }
1515
1516 // Create a structure into which properties common across the architectures in
1517 // this os type will be stored.
1518 osInfo.Properties = osSpecificVariantPropertiesFactory()
1519
1520 // Group the variants by arch type.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001521 var variantsByArchId = make(map[archId][]android.Module)
1522 var archIds []archId
Paul Duffin00e46802020-03-12 20:40:35 +00001523 for _, variant := range osTypeVariants {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001524 target := variant.Target()
1525 id := archIdFromTarget(target)
1526 if _, ok := variantsByArchId[id]; !ok {
1527 archIds = append(archIds, id)
Paul Duffin00e46802020-03-12 20:40:35 +00001528 }
1529
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001530 variantsByArchId[id] = append(variantsByArchId[id], variant)
Paul Duffin00e46802020-03-12 20:40:35 +00001531 }
1532
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001533 if commonVariants, ok := variantsByArchId[commonArchId]; ok {
Paul Duffin00e46802020-03-12 20:40:35 +00001534 if len(osTypeVariants) != 1 {
Paul Duffin4e7d1c42022-05-13 13:12:19 +00001535 variants := []string{}
1536 for _, m := range osTypeVariants {
1537 variants = append(variants, fmt.Sprintf(" %s", m.String()))
1538 }
1539 panic(fmt.Errorf("expected to only have 1 variant of %q when arch type is common but found %d\n%s",
1540 ctx.Name(),
1541 len(osTypeVariants),
1542 strings.Join(variants, "\n")))
Paul Duffin00e46802020-03-12 20:40:35 +00001543 }
1544
1545 // A common arch type only has one variant and its properties should be treated
1546 // as common to the os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001547 osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
Paul Duffin00e46802020-03-12 20:40:35 +00001548 } else {
1549 // Create an arch specific info for each supported architecture type.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001550 for _, id := range archIds {
1551 archVariants := variantsByArchId[id]
1552 archInfo := newArchSpecificInfo(ctx, id, osType, osSpecificVariantPropertiesFactory, archVariants)
Paul Duffin00e46802020-03-12 20:40:35 +00001553
1554 osInfo.archInfos = append(osInfo.archInfos, archInfo)
1555 }
1556 }
1557
1558 return osInfo
1559}
1560
Paul Duffin39abf8f2021-09-24 14:58:27 +01001561func (osInfo *osTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1562 if len(osInfo.archInfos) == 0 {
1563 pruner.pruneProperties(osInfo.Properties)
1564 } else {
1565 for _, archInfo := range osInfo.archInfos {
1566 archInfo.pruneUnsupportedProperties(pruner)
1567 }
1568 }
1569}
1570
Paul Duffin00e46802020-03-12 20:40:35 +00001571// Optimize the properties by extracting common properties from arch type specific
1572// properties into os type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001573func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin00e46802020-03-12 20:40:35 +00001574 // Nothing to do if there is only a single common architecture.
1575 if len(osInfo.archInfos) == 0 {
1576 return
1577 }
1578
Paul Duffin9c3760e2020-03-16 19:52:08 +00001579 multilib := multilibNone
Paul Duffin00e46802020-03-12 20:40:35 +00001580 for _, archInfo := range osInfo.archInfos {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001581 multilib = multilib.addArchType(archInfo.archId.archType)
Paul Duffin9c3760e2020-03-16 19:52:08 +00001582
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001583 // Optimize the arch properties first.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001584 archInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin00e46802020-03-12 20:40:35 +00001585 }
1586
Paul Duffin4b8b7932020-05-06 12:35:38 +01001587 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
Paul Duffin00e46802020-03-12 20:40:35 +00001588
1589 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
Paul Duffin9c3760e2020-03-16 19:52:08 +00001590 osInfo.Properties.Base().Compile_multilib = multilib.String()
Paul Duffin00e46802020-03-12 20:40:35 +00001591}
1592
1593// Add the properties for an os to a property set.
1594//
1595// Maps the properties related to the os variants through to an appropriate
1596// module structure that will produce equivalent set of variants when it is
1597// processed in a build.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001598func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
Paul Duffin00e46802020-03-12 20:40:35 +00001599
1600 var osPropertySet android.BpPropertySet
1601 var archPropertySet android.BpPropertySet
1602 var archOsPrefix string
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001603 if osInfo.Properties.Base().Os_count == 1 &&
1604 (osInfo.osType.Class == android.Device || !ctx.memberType.IsHostOsDependent()) {
1605 // There is only one OS type present in the variants and it shouldn't have a
1606 // variant-specific target. The latter is the case if it's either for device
1607 // where there is only one OS (android), or for host and the member type
1608 // isn't host OS dependent.
Paul Duffin00e46802020-03-12 20:40:35 +00001609
1610 // Create a structure that looks like:
1611 // module_type {
1612 // name: "...",
1613 // ...
1614 // <common properties>
1615 // ...
1616 // <single os type specific properties>
1617 //
1618 // arch: {
1619 // <arch specific sections>
1620 // }
1621 //
1622 osPropertySet = bpModule
1623 archPropertySet = osPropertySet.AddPropertySet("arch")
1624
1625 // Arch specific properties need to be added to an arch specific section
1626 // within arch.
1627 archOsPrefix = ""
1628 } else {
1629 // Create a structure that looks like:
1630 // module_type {
1631 // name: "...",
1632 // ...
1633 // <common properties>
1634 // ...
1635 // target: {
1636 // <arch independent os specific sections, e.g. android>
1637 // ...
1638 // <arch and os specific sections, e.g. android_x86>
1639 // }
1640 //
1641 osType := osInfo.osType
1642 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
1643 archPropertySet = targetPropertySet
1644
1645 // Arch specific properties need to be added to an os and arch specific
1646 // section prefixed with <os>_.
1647 archOsPrefix = osType.Name + "_"
1648 }
1649
1650 // Add the os specific but arch independent properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001651 addSdkMemberPropertiesToSet(ctx, osInfo.Properties, osPropertySet)
Paul Duffin00e46802020-03-12 20:40:35 +00001652
1653 // Add arch (and possibly os) specific sections for each set of arch (and possibly
1654 // os) specific properties.
1655 //
1656 // The archInfos list will be empty if the os contains variants for the common
1657 // architecture.
1658 for _, archInfo := range osInfo.archInfos {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001659 archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
Paul Duffin00e46802020-03-12 20:40:35 +00001660 }
1661}
1662
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001663func (osInfo *osTypeSpecificInfo) isHostVariant() bool {
1664 osClass := osInfo.osType.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001665 return osClass == android.Host
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001666}
1667
1668var _ isHostVariant = (*osTypeSpecificInfo)(nil)
1669
Paul Duffin4b8b7932020-05-06 12:35:38 +01001670func (osInfo *osTypeSpecificInfo) String() string {
1671 return fmt.Sprintf("OsType{%s}", osInfo.osType)
1672}
1673
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001674// archId encapsulates the information needed to identify a combination of arch type and native
1675// bridge support.
1676//
1677// Conceptually, native bridge support is a facet of an android.Target, not an android.Arch as it is
1678// essentially using one android.Arch to implement another. However, in terms of the handling of
1679// the variants native bridge is treated as part of the arch variation. See the ArchVariation method
1680// on android.Target.
1681//
1682// So, it makes sense when optimizing the variants to combine native bridge with the arch type.
1683type archId struct {
1684 // The arch type of the variant's target.
1685 archType android.ArchType
1686
1687 // True if the variants is for the native bridge, false otherwise.
1688 nativeBridge bool
1689}
1690
1691// propertyName returns the name of the property corresponding to use for this arch id.
1692func (i *archId) propertyName() string {
1693 name := i.archType.Name
1694 if i.nativeBridge {
1695 // Note: This does not result in a valid property because there is no architecture specific
1696 // native bridge property, only a generic "native_bridge" property. However, this will be used
1697 // in error messages if there is an attempt to use this in a generated bp file.
1698 name += "_native_bridge"
1699 }
1700 return name
1701}
1702
1703func (i *archId) String() string {
1704 return fmt.Sprintf("ArchType{%s}, NativeBridge{%t}", i.archType, i.nativeBridge)
1705}
1706
1707// archIdFromTarget returns an archId initialized from information in the supplied target.
1708func archIdFromTarget(target android.Target) archId {
1709 return archId{
1710 archType: target.Arch.ArchType,
1711 nativeBridge: target.NativeBridge == android.NativeBridgeEnabled,
1712 }
1713}
1714
1715// commonArchId is the archId for the common architecture.
1716var commonArchId = archId{archType: android.Common}
1717
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001718type archTypeSpecificInfo struct {
1719 baseInfo
1720
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001721 archId archId
1722 osType android.OsType
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001723
Paul Duffinb42fa672021-09-09 16:37:49 +01001724 imageVariantInfos []*imageVariantSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001725}
1726
Paul Duffin4b8b7932020-05-06 12:35:38 +01001727var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
1728
Paul Duffinfc8dd232020-03-17 12:51:37 +00001729// Create a new archTypeSpecificInfo for the specified arch type and its properties
1730// structures populated with information from the variants.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001731func newArchSpecificInfo(ctx android.SdkMemberContext, archId archId, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001732
Paul Duffinfc8dd232020-03-17 12:51:37 +00001733 // Create an arch specific info into which the variant properties can be copied.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001734 archInfo := &archTypeSpecificInfo{archId: archId, osType: osType}
Paul Duffinfc8dd232020-03-17 12:51:37 +00001735
1736 // Create the properties into which the arch type specific properties will be
1737 // added.
1738 archInfo.Properties = variantPropertiesFactory()
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001739
Liz Kammer96320df2022-05-12 20:40:00 -04001740 // if there are multiple supported link variants, we want to nest based on linkage even if there
1741 // is only one variant, otherwise, if there is only one variant we can populate based on the arch
1742 if len(archVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001743 archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001744 } else {
Paul Duffinb42fa672021-09-09 16:37:49 +01001745 // Group the variants by image type.
1746 variantsByImage := make(map[string][]android.Module)
1747 for _, variant := range archVariants {
1748 image := variant.ImageVariation().Variation
1749 variantsByImage[image] = append(variantsByImage[image], variant)
1750 }
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001751
Paul Duffinb42fa672021-09-09 16:37:49 +01001752 // Create the image variant info in a fixed order.
1753 for _, imageVariantName := range android.SortedStringKeys(variantsByImage) {
1754 variants := variantsByImage[imageVariantName]
1755 archInfo.imageVariantInfos = append(archInfo.imageVariantInfos, newImageVariantSpecificInfo(ctx, imageVariantName, variantPropertiesFactory, variants))
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001756 }
1757 }
Paul Duffinfc8dd232020-03-17 12:51:37 +00001758
1759 return archInfo
1760}
1761
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001762// Get the link type of the variant
1763//
1764// If the variant is not differentiated by link type then it returns "",
1765// otherwise it returns one of "static" or "shared".
1766func getLinkType(variant android.Module) string {
1767 linkType := ""
1768 if linkable, ok := variant.(cc.LinkableInterface); ok {
1769 if linkable.Shared() && linkable.Static() {
1770 panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String()))
1771 } else if linkable.Shared() {
1772 linkType = "shared"
1773 } else if linkable.Static() {
1774 linkType = "static"
1775 } else {
1776 panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String()))
1777 }
1778 }
1779 return linkType
1780}
1781
Paul Duffin39abf8f2021-09-24 14:58:27 +01001782func (archInfo *archTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1783 if len(archInfo.imageVariantInfos) == 0 {
1784 pruner.pruneProperties(archInfo.Properties)
1785 } else {
1786 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1787 imageVariantInfo.pruneUnsupportedProperties(pruner)
1788 }
1789 }
1790}
1791
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001792// Optimize the properties by extracting common properties from link type specific
1793// properties into arch type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001794func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffinb42fa672021-09-09 16:37:49 +01001795 if len(archInfo.imageVariantInfos) == 0 {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001796 return
1797 }
1798
Paul Duffinb42fa672021-09-09 16:37:49 +01001799 // Optimize the image variant properties first.
1800 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1801 imageVariantInfo.optimizeProperties(ctx, commonValueExtractor)
1802 }
1803
1804 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.imageVariantInfos)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001805}
1806
Paul Duffinfc8dd232020-03-17 12:51:37 +00001807// Add the properties for an arch type to a property set.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001808func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001809 archPropertySuffix := archInfo.archId.propertyName()
1810 propertySetName := archOsPrefix + archPropertySuffix
1811 archTypePropertySet := archPropertySet.AddPropertySet(propertySetName)
Jiyong Park8fe14e62020-10-19 22:47:34 +09001812 // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
1813 if ctx.memberType.IsHostOsDependent() && archInfo.osType.Class == android.Host {
1814 archTypePropertySet.AddProperty("enabled", true)
1815 }
Martin Stjernholm89238f42020-07-10 00:14:03 +01001816 addSdkMemberPropertiesToSet(ctx, archInfo.Properties, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001817
Paul Duffinb42fa672021-09-09 16:37:49 +01001818 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1819 imageVariantInfo.addToPropertySet(ctx, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001820 }
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001821
1822 // If this is for a native bridge architecture then make sure that the property set does not
1823 // contain any properties as providing native bridge specific properties is not currently
1824 // supported.
1825 if archInfo.archId.nativeBridge {
1826 propertySetContents := getPropertySetContents(archTypePropertySet)
1827 if propertySetContents != "" {
1828 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",
1829 propertySetName, ctx.name, propertySetContents)
1830 }
1831 }
1832}
1833
1834// getPropertySetContents returns the string representation of the contents of a property set, after
1835// recursively pruning any empty nested property sets.
1836func getPropertySetContents(propertySet android.BpPropertySet) string {
1837 set := propertySet.(*bpPropertySet)
1838 set.transformContents(pruneEmptySetTransformer{})
1839 if len(set.properties) != 0 {
1840 contents := &generatedContents{}
1841 contents.Indent()
1842 outputPropertySet(contents, set)
1843 setAsString := contents.content.String()
1844 return setAsString
1845 }
1846 return ""
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001847}
1848
Paul Duffin4b8b7932020-05-06 12:35:38 +01001849func (archInfo *archTypeSpecificInfo) String() string {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001850 return archInfo.archId.String()
Paul Duffin4b8b7932020-05-06 12:35:38 +01001851}
1852
Paul Duffinb42fa672021-09-09 16:37:49 +01001853type imageVariantSpecificInfo struct {
1854 baseInfo
1855
1856 imageVariant string
1857
1858 linkInfos []*linkTypeSpecificInfo
1859}
1860
1861func newImageVariantSpecificInfo(ctx android.SdkMemberContext, imageVariant string, variantPropertiesFactory variantPropertiesFactoryFunc, imageVariants []android.Module) *imageVariantSpecificInfo {
1862
1863 // Create an image variant specific info into which the variant properties can be copied.
1864 imageInfo := &imageVariantSpecificInfo{imageVariant: imageVariant}
1865
1866 // Create the properties into which the image variant specific properties will be added.
1867 imageInfo.Properties = variantPropertiesFactory()
1868
Liz Kammer96320df2022-05-12 20:40:00 -04001869 // if there are multiple supported link variants, we want to nest even if there is only one
1870 // variant, otherwise, if there is only one variant we can populate based on the image
1871 if len(imageVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
Paul Duffinb42fa672021-09-09 16:37:49 +01001872 imageInfo.Properties.PopulateFromVariant(ctx, imageVariants[0])
1873 } else {
1874 // There is more than one variant for this image variant which must be differentiated by link
Liz Kammer96320df2022-05-12 20:40:00 -04001875 // type. Or there are multiple supported linkages and we need to nest based on link type.
Paul Duffinb42fa672021-09-09 16:37:49 +01001876 for _, linkVariant := range imageVariants {
1877 linkType := getLinkType(linkVariant)
1878 if linkType == "" {
1879 panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(imageVariants)))
1880 } else {
1881 linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
1882
1883 imageInfo.linkInfos = append(imageInfo.linkInfos, linkInfo)
1884 }
1885 }
1886 }
1887
1888 return imageInfo
1889}
1890
Paul Duffin39abf8f2021-09-24 14:58:27 +01001891func (imageInfo *imageVariantSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1892 if len(imageInfo.linkInfos) == 0 {
1893 pruner.pruneProperties(imageInfo.Properties)
1894 } else {
1895 for _, linkInfo := range imageInfo.linkInfos {
1896 linkInfo.pruneUnsupportedProperties(pruner)
1897 }
1898 }
1899}
1900
Paul Duffinb42fa672021-09-09 16:37:49 +01001901// Optimize the properties by extracting common properties from link type specific
1902// properties into arch type specific properties.
1903func (imageInfo *imageVariantSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
1904 if len(imageInfo.linkInfos) == 0 {
1905 return
1906 }
1907
1908 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, imageInfo.Properties, imageInfo.linkInfos)
1909}
1910
1911// Add the properties for an arch type to a property set.
1912func (imageInfo *imageVariantSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
1913 if imageInfo.imageVariant != android.CoreVariation {
1914 propertySet = propertySet.AddPropertySet(imageInfo.imageVariant)
1915 }
1916
1917 addSdkMemberPropertiesToSet(ctx, imageInfo.Properties, propertySet)
1918
Liz Kammer96320df2022-05-12 20:40:00 -04001919 usedLinkages := make(map[string]bool, len(imageInfo.linkInfos))
Paul Duffinb42fa672021-09-09 16:37:49 +01001920 for _, linkInfo := range imageInfo.linkInfos {
Liz Kammer96320df2022-05-12 20:40:00 -04001921 usedLinkages[linkInfo.linkType] = true
Paul Duffinb42fa672021-09-09 16:37:49 +01001922 linkInfo.addToPropertySet(ctx, propertySet)
1923 }
1924
Liz Kammer96320df2022-05-12 20:40:00 -04001925 // If not all supported linkages had existing variants, we need to disable the unsupported variant
1926 if len(imageInfo.linkInfos) < len(ctx.MemberType().SupportedLinkages()) {
1927 for _, l := range ctx.MemberType().SupportedLinkages() {
1928 if _, ok := usedLinkages[l]; !ok {
1929 otherLinkagePropertySet := propertySet.AddPropertySet(l)
1930 otherLinkagePropertySet.AddProperty("enabled", false)
1931 }
1932 }
1933 }
1934
Paul Duffinb42fa672021-09-09 16:37:49 +01001935 // If this is for a non-core image variant then make sure that the property set does not contain
1936 // any properties as providing non-core image variant specific properties for prebuilts is not
1937 // currently supported.
1938 if imageInfo.imageVariant != android.CoreVariation {
1939 propertySetContents := getPropertySetContents(propertySet)
1940 if propertySetContents != "" {
1941 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",
1942 imageInfo.imageVariant, ctx.name, propertySetContents)
1943 }
1944 }
1945}
1946
1947func (imageInfo *imageVariantSpecificInfo) String() string {
1948 return imageInfo.imageVariant
1949}
1950
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001951type linkTypeSpecificInfo struct {
1952 baseInfo
1953
1954 linkType string
1955}
1956
Paul Duffin4b8b7932020-05-06 12:35:38 +01001957var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
1958
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001959// Create a new linkTypeSpecificInfo for the specified link type and its properties
1960// structures populated with information from the variant.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001961func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001962 linkInfo := &linkTypeSpecificInfo{
1963 baseInfo: baseInfo{
1964 // Create the properties into which the link type specific properties will be
1965 // added.
1966 Properties: variantPropertiesFactory(),
1967 },
1968 linkType: linkType,
1969 }
Paul Duffin3a4eb502020-03-19 16:11:18 +00001970 linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001971 return linkInfo
Paul Duffinfc8dd232020-03-17 12:51:37 +00001972}
1973
Paul Duffinf68f85a2021-09-09 16:11:42 +01001974func (l *linkTypeSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
1975 linkPropertySet := propertySet.AddPropertySet(l.linkType)
1976 addSdkMemberPropertiesToSet(ctx, l.Properties, linkPropertySet)
1977}
1978
Paul Duffin39abf8f2021-09-24 14:58:27 +01001979func (l *linkTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1980 pruner.pruneProperties(l.Properties)
1981}
1982
Paul Duffin4b8b7932020-05-06 12:35:38 +01001983func (l *linkTypeSpecificInfo) String() string {
1984 return fmt.Sprintf("LinkType{%s}", l.linkType)
1985}
1986
Paul Duffin3a4eb502020-03-19 16:11:18 +00001987type memberContext struct {
1988 sdkMemberContext android.ModuleContext
1989 builder *snapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +00001990 memberType android.SdkMemberType
1991 name string
Paul Duffind19f8942021-07-14 12:08:37 +01001992
1993 // The set of traits required of this member.
1994 requiredTraits android.SdkMemberTraitSet
Paul Duffin3a4eb502020-03-19 16:11:18 +00001995}
1996
1997func (m *memberContext) SdkModuleContext() android.ModuleContext {
1998 return m.sdkMemberContext
1999}
2000
2001func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
2002 return m.builder
2003}
2004
Paul Duffina551a1c2020-03-17 21:04:24 +00002005func (m *memberContext) MemberType() android.SdkMemberType {
2006 return m.memberType
2007}
2008
2009func (m *memberContext) Name() string {
2010 return m.name
2011}
2012
Paul Duffind19f8942021-07-14 12:08:37 +01002013func (m *memberContext) RequiresTrait(trait android.SdkMemberTrait) bool {
2014 return m.requiredTraits.Contains(trait)
2015}
2016
Paul Duffin13648912022-07-15 13:12:35 +00002017func (m *memberContext) IsTargetBuildBeforeTiramisu() bool {
2018 return m.builder.targetBuildRelease.EarlierThan(buildReleaseT)
2019}
2020
2021var _ android.SdkMemberContext = (*memberContext)(nil)
2022
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01002023func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002024
2025 memberType := member.memberType
2026
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002027 // Do not add the prefer property if the member snapshot module is a source module type.
Paul Duffin4e7d1c42022-05-13 13:12:19 +00002028 moduleCtx := ctx.sdkMemberContext
2029 config := moduleCtx.Config()
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002030 if !memberType.UsesSourceModuleTypeInSnapshot() {
Mathew Inwood7e9ddbe2021-07-07 12:47:51 +00002031 // Set the prefer based on the environment variable. This is a temporary work around to allow a
2032 // snapshot to be created that sets prefer: true.
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002033 // TODO(b/174997203): Remove once the ability to select the modules to prefer can be done
2034 // dynamically at build time not at snapshot generation time.
Paul Duffinfb9a7f92021-07-06 17:18:42 +01002035 prefer := config.IsEnvTrue("SOONG_SDK_SNAPSHOT_PREFER")
Paul Duffin83ad9562021-05-10 23:49:04 +01002036
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002037 // Set prefer. Setting this to false is not strictly required as that is the default but it does
2038 // provide a convenient hook to post-process the generated Android.bp file, e.g. in tests to
2039 // check the behavior when a prebuilt is preferred. It also makes it explicit what the default
2040 // behavior is for the module.
2041 bpModule.insertAfter("name", "prefer", prefer)
Paul Duffinfb9a7f92021-07-06 17:18:42 +01002042
2043 configVar := config.Getenv("SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR")
2044 if configVar != "" {
2045 parts := strings.Split(configVar, ":")
2046 cfp := android.ConfigVarProperties{
2047 Config_namespace: proptools.StringPtr(parts[0]),
2048 Var_name: proptools.StringPtr(parts[1]),
2049 }
2050 bpModule.insertAfter("prefer", "use_source_config_var", cfp)
2051 }
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002052 }
Paul Duffin83ad9562021-05-10 23:49:04 +01002053
Paul Duffin4e7d1c42022-05-13 13:12:19 +00002054 variants := selectApexVariantsWhereAvailable(ctx, member.variants)
2055
Paul Duffina04c1072020-03-02 10:16:35 +00002056 // Group the variants by os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00002057 variantsByOsType := make(map[android.OsType][]android.Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002058 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +00002059 osType := variant.Target().Os
2060 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002061 }
2062
Paul Duffina04c1072020-03-02 10:16:35 +00002063 osCount := len(variantsByOsType)
Paul Duffinb44b33a2020-03-17 10:58:23 +00002064 variantPropertiesFactory := func() android.SdkMemberProperties {
Paul Duffina04c1072020-03-02 10:16:35 +00002065 properties := memberType.CreateVariantPropertiesStruct()
2066 base := properties.Base()
Paul Duffinc61783b2022-10-20 17:21:40 +01002067 base.MemberName = member.Name()
Paul Duffina04c1072020-03-02 10:16:35 +00002068 base.Os_count = osCount
Paul Duffina04c1072020-03-02 10:16:35 +00002069 return properties
2070 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002071
Paul Duffina04c1072020-03-02 10:16:35 +00002072 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +00002073
Paul Duffina04c1072020-03-02 10:16:35 +00002074 // The set of properties that are common across all architectures and os types.
Paul Duffinb44b33a2020-03-17 10:58:23 +00002075 commonProperties := variantPropertiesFactory()
2076 commonProperties.Base().Os = android.CommonOS
Paul Duffina04c1072020-03-02 10:16:35 +00002077
Paul Duffin39abf8f2021-09-24 14:58:27 +01002078 // Create a property pruner that will prune any properties unsupported by the target build
2079 // release.
2080 targetBuildRelease := ctx.builder.targetBuildRelease
2081 unsupportedPropertyPruner := newPropertyPrunerByBuildRelease(commonProperties, targetBuildRelease)
2082
Paul Duffinc097e362020-03-10 22:50:03 +00002083 // Create common value extractor that can be used to optimize the properties.
2084 commonValueExtractor := newCommonValueExtractor(commonProperties)
2085
Paul Duffina04c1072020-03-02 10:16:35 +00002086 // The list of property structures which are os type specific but common across
2087 // architectures within that os type.
Paul Duffinf34f6d82020-04-30 15:48:31 +01002088 var osSpecificPropertiesContainers []*osTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +00002089
2090 for osType, osTypeVariants := range variantsByOsType {
Paul Duffin3a4eb502020-03-19 16:11:18 +00002091 osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
Paul Duffina04c1072020-03-02 10:16:35 +00002092 osTypeToInfo[osType] = osInfo
Paul Duffinb44b33a2020-03-17 10:58:23 +00002093 // Add the os specific properties to a list of os type specific yet architecture
2094 // independent properties structs.
Paul Duffinf34f6d82020-04-30 15:48:31 +01002095 osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
Paul Duffina04c1072020-03-02 10:16:35 +00002096
Paul Duffin39abf8f2021-09-24 14:58:27 +01002097 osInfo.pruneUnsupportedProperties(unsupportedPropertyPruner)
2098
Paul Duffin00e46802020-03-12 20:40:35 +00002099 // Optimize the properties across all the variants for a specific os type.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002100 osInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin14eb4672020-03-02 11:33:02 +00002101 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002102
Paul Duffina04c1072020-03-02 10:16:35 +00002103 // Extract properties which are common across all architectures and os types.
Paul Duffin4e7d1c42022-05-13 13:12:19 +00002104 extractCommonProperties(moduleCtx, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002105
Paul Duffina04c1072020-03-02 10:16:35 +00002106 // Add the common properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01002107 addSdkMemberPropertiesToSet(ctx, commonProperties, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002108
Paul Duffina04c1072020-03-02 10:16:35 +00002109 // Create a target property set into which target specific properties can be
2110 // added.
2111 targetPropertySet := bpModule.AddPropertySet("target")
2112
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01002113 // If the member is host OS dependent and has host_supported then disable by
2114 // default and enable each host OS variant explicitly. This avoids problems
2115 // with implicitly enabled OS variants when the snapshot is used, which might
2116 // be different from this run (e.g. different build OS).
2117 if ctx.memberType.IsHostOsDependent() {
2118 hostSupported := bpModule.getValue("host_supported") == true // Missing means false.
2119 if hostSupported {
2120 hostPropertySet := targetPropertySet.AddPropertySet("host")
2121 hostPropertySet.AddProperty("enabled", false)
2122 }
2123 }
2124
Paul Duffina04c1072020-03-02 10:16:35 +00002125 // Iterate over the os types in a fixed order.
2126 for _, osType := range s.getPossibleOsTypes() {
2127 osInfo := osTypeToInfo[osType]
2128 if osInfo == nil {
2129 continue
2130 }
2131
Paul Duffin3a4eb502020-03-19 16:11:18 +00002132 osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002133 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002134}
2135
Paul Duffina04c1072020-03-02 10:16:35 +00002136// Compute the list of possible os types that this sdk could support.
2137func (s *sdk) getPossibleOsTypes() []android.OsType {
2138 var osTypes []android.OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +00002139 for _, osType := range android.OsTypeList() {
Paul Duffina04c1072020-03-02 10:16:35 +00002140 if s.DeviceSupported() {
Colin Crosscb0ac952021-07-20 13:17:15 -07002141 if osType.Class == android.Device {
Paul Duffina04c1072020-03-02 10:16:35 +00002142 osTypes = append(osTypes, osType)
2143 }
2144 }
2145 if s.HostSupported() {
Jiyong Park1613e552020-09-14 19:43:17 +09002146 if osType.Class == android.Host {
Paul Duffina04c1072020-03-02 10:16:35 +00002147 osTypes = append(osTypes, osType)
2148 }
2149 }
2150 }
2151 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
2152 return osTypes
2153}
2154
Paul Duffinb28369a2020-05-04 15:39:59 +01002155// Given a set of properties (struct value), return the value of the field within that
2156// struct (or one of its embedded structs).
Paul Duffinc097e362020-03-10 22:50:03 +00002157type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
2158
Paul Duffinc459f892020-04-30 18:08:29 +01002159// Checks the metadata to determine whether the property should be ignored for the
2160// purposes of common value extraction or not.
2161type extractorMetadataPredicate func(metadata propertiesContainer) bool
2162
2163// Indicates whether optimizable properties are provided by a host variant or
2164// not.
2165type isHostVariant interface {
2166 isHostVariant() bool
2167}
2168
Paul Duffinb28369a2020-05-04 15:39:59 +01002169// A property that can be optimized by the commonValueExtractor.
2170type extractorProperty struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +01002171 // The name of the field for this property. It is a "."-separated path for
2172 // fields in non-anonymous substructs.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002173 name string
2174
Paul Duffinc459f892020-04-30 18:08:29 +01002175 // Filter that can use metadata associated with the properties being optimized
2176 // to determine whether the field should be ignored during common value
2177 // optimization.
2178 filter extractorMetadataPredicate
2179
Paul Duffinb28369a2020-05-04 15:39:59 +01002180 // Retrieves the value on which common value optimization will be performed.
2181 getter fieldAccessorFunc
2182
Paul Duffinbfdca962022-09-22 16:21:54 +01002183 // True if the field should never be cleared.
2184 //
2185 // This is set to true if and only if the field is annotated with `sdk:"keep"`.
2186 keep bool
2187
Paul Duffinb28369a2020-05-04 15:39:59 +01002188 // The empty value for the field.
2189 emptyValue reflect.Value
Paul Duffin864e1b42020-05-06 10:23:19 +01002190
2191 // True if the property can support arch variants false otherwise.
2192 archVariant bool
Paul Duffinb28369a2020-05-04 15:39:59 +01002193}
2194
Paul Duffin4b8b7932020-05-06 12:35:38 +01002195func (p extractorProperty) String() string {
2196 return p.name
2197}
2198
Paul Duffinc097e362020-03-10 22:50:03 +00002199// Supports extracting common values from a number of instances of a properties
2200// structure into a separate common set of properties.
2201type commonValueExtractor struct {
Paul Duffinb28369a2020-05-04 15:39:59 +01002202 // The properties that the extractor can optimize.
2203 properties []extractorProperty
Paul Duffinc097e362020-03-10 22:50:03 +00002204}
2205
2206// Create a new common value extractor for the structure type for the supplied
2207// properties struct.
2208//
2209// The returned extractor can be used on any properties structure of the same type
2210// as the supplied set of properties.
2211func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor {
2212 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
2213 extractor := &commonValueExtractor{}
Martin Stjernholmb0249572020-09-15 02:32:35 +01002214 extractor.gatherFields(structType, nil, "")
Paul Duffinc097e362020-03-10 22:50:03 +00002215 return extractor
2216}
2217
2218// Gather the fields from the supplied structure type from which common values will
2219// be extracted.
Paul Duffinb07fa512020-03-10 22:17:04 +00002220//
Martin Stjernholmb0249572020-09-15 02:32:35 +01002221// This is recursive function. If it encounters a struct then it will recurse
2222// into it, passing in the accessor for the field and the struct name as prefix
2223// for the nested fields. That will then be used in the accessors for the fields
2224// in the embedded struct.
2225func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string) {
Paul Duffinc097e362020-03-10 22:50:03 +00002226 for f := 0; f < structType.NumField(); f++ {
2227 field := structType.Field(f)
2228 if field.PkgPath != "" {
2229 // Ignore unexported fields.
2230 continue
2231 }
2232
Paul Duffin02e25c82022-09-22 15:30:58 +01002233 // Ignore fields tagged with sdk:"ignore".
2234 if proptools.HasTag(field, "sdk", "ignore") {
Paul Duffinc097e362020-03-10 22:50:03 +00002235 continue
2236 }
2237
Paul Duffinc459f892020-04-30 18:08:29 +01002238 var filter extractorMetadataPredicate
2239
2240 // Add a filter
2241 if proptools.HasTag(field, "sdk", "ignored-on-host") {
2242 filter = func(metadata propertiesContainer) bool {
2243 if m, ok := metadata.(isHostVariant); ok {
2244 if m.isHostVariant() {
2245 return false
2246 }
2247 }
2248 return true
2249 }
2250 }
2251
Paul Duffinbfdca962022-09-22 16:21:54 +01002252 keep := proptools.HasTag(field, "sdk", "keep")
2253
Paul Duffinc097e362020-03-10 22:50:03 +00002254 // Save a copy of the field index for use in the function.
2255 fieldIndex := f
Paul Duffin4b8b7932020-05-06 12:35:38 +01002256
Martin Stjernholmb0249572020-09-15 02:32:35 +01002257 name := namePrefix + field.Name
Paul Duffin4b8b7932020-05-06 12:35:38 +01002258
Paul Duffinc097e362020-03-10 22:50:03 +00002259 fieldGetter := func(value reflect.Value) reflect.Value {
Paul Duffinb07fa512020-03-10 22:17:04 +00002260 if containingStructAccessor != nil {
2261 // This is an embedded structure so first access the field for the embedded
2262 // structure.
2263 value = containingStructAccessor(value)
2264 }
2265
Paul Duffinc097e362020-03-10 22:50:03 +00002266 // Skip through interface and pointer values to find the structure.
2267 value = getStructValue(value)
2268
Paul Duffin4b8b7932020-05-06 12:35:38 +01002269 defer func() {
2270 if r := recover(); r != nil {
2271 panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
2272 }
2273 }()
2274
Paul Duffinc097e362020-03-10 22:50:03 +00002275 // Return the field.
2276 return value.Field(fieldIndex)
2277 }
2278
Martin Stjernholmb0249572020-09-15 02:32:35 +01002279 if field.Type.Kind() == reflect.Struct {
2280 // Gather fields from the nested or embedded structure.
2281 var subNamePrefix string
2282 if field.Anonymous {
2283 subNamePrefix = namePrefix
2284 } else {
2285 subNamePrefix = name + "."
2286 }
2287 e.gatherFields(field.Type, fieldGetter, subNamePrefix)
Paul Duffinb07fa512020-03-10 22:17:04 +00002288 } else {
Paul Duffinb28369a2020-05-04 15:39:59 +01002289 property := extractorProperty{
Paul Duffin4b8b7932020-05-06 12:35:38 +01002290 name,
Paul Duffinc459f892020-04-30 18:08:29 +01002291 filter,
Paul Duffinb28369a2020-05-04 15:39:59 +01002292 fieldGetter,
Paul Duffinbfdca962022-09-22 16:21:54 +01002293 keep,
Paul Duffinb28369a2020-05-04 15:39:59 +01002294 reflect.Zero(field.Type),
Paul Duffin864e1b42020-05-06 10:23:19 +01002295 proptools.HasTag(field, "android", "arch_variant"),
Paul Duffinb28369a2020-05-04 15:39:59 +01002296 }
2297 e.properties = append(e.properties, property)
Paul Duffinb07fa512020-03-10 22:17:04 +00002298 }
Paul Duffinc097e362020-03-10 22:50:03 +00002299 }
2300}
2301
2302func getStructValue(value reflect.Value) reflect.Value {
2303foundStruct:
2304 for {
2305 kind := value.Kind()
2306 switch kind {
2307 case reflect.Interface, reflect.Ptr:
2308 value = value.Elem()
2309 case reflect.Struct:
2310 break foundStruct
2311 default:
2312 panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind))
2313 }
2314 }
2315 return value
2316}
2317
Paul Duffinf34f6d82020-04-30 15:48:31 +01002318// A container of properties to be optimized.
2319//
2320// Allows additional information to be associated with the properties, e.g. for
2321// filtering.
2322type propertiesContainer interface {
Paul Duffin4b8b7932020-05-06 12:35:38 +01002323 fmt.Stringer
2324
Paul Duffinf34f6d82020-04-30 15:48:31 +01002325 // Get the properties that need optimizing.
2326 optimizableProperties() interface{}
2327}
2328
Paul Duffin2d1bb892021-04-24 11:32:59 +01002329// A wrapper for sdk variant related properties to allow them to be optimized.
2330type sdkVariantPropertiesContainer struct {
2331 sdkVariant *sdk
2332 properties interface{}
Paul Duffinf34f6d82020-04-30 15:48:31 +01002333}
2334
Paul Duffin2d1bb892021-04-24 11:32:59 +01002335func (c sdkVariantPropertiesContainer) optimizableProperties() interface{} {
2336 return c.properties
Paul Duffinf34f6d82020-04-30 15:48:31 +01002337}
2338
Paul Duffin2d1bb892021-04-24 11:32:59 +01002339func (c sdkVariantPropertiesContainer) String() string {
Paul Duffin4b8b7932020-05-06 12:35:38 +01002340 return c.sdkVariant.String()
2341}
2342
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002343// Extract common properties from a slice of property structures of the same type.
2344//
2345// All the property structures must be of the same type.
2346// commonProperties - must be a pointer to the structure into which common properties will be added.
Paul Duffinf34f6d82020-04-30 15:48:31 +01002347// inputPropertiesSlice - must be a slice of propertiesContainer interfaces.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002348//
2349// Iterates over each exported field (capitalized name) and checks to see whether they
2350// have the same value (using DeepEquals) across all the input properties. If it does not then no
2351// change is made. Otherwise, the common value is stored in the field in the commonProperties
Martin Stjernholmb0249572020-09-15 02:32:35 +01002352// and the field in each of the input properties structure is set to its default value. Nested
2353// structs are visited recursively and their non-struct fields are compared.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002354func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002355 commonPropertiesValue := reflect.ValueOf(commonProperties)
2356 commonStructValue := commonPropertiesValue.Elem()
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002357
Paul Duffinf34f6d82020-04-30 15:48:31 +01002358 sliceValue := reflect.ValueOf(inputPropertiesSlice)
2359
Paul Duffinb28369a2020-05-04 15:39:59 +01002360 for _, property := range e.properties {
2361 fieldGetter := property.getter
Paul Duffinc459f892020-04-30 18:08:29 +01002362 filter := property.filter
2363 if filter == nil {
2364 filter = func(metadata propertiesContainer) bool {
2365 return true
2366 }
2367 }
Paul Duffinb28369a2020-05-04 15:39:59 +01002368
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002369 // Check to see if all the structures have the same value for the field. The commonValue
Paul Duffin864e1b42020-05-06 10:23:19 +01002370 // is nil on entry to the loop and if it is nil on exit then there is no common value or
2371 // all the values have been filtered out, otherwise it points to the common value.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002372 var commonValue *reflect.Value
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002373
Paul Duffin864e1b42020-05-06 10:23:19 +01002374 // Assume that all the values will be the same.
2375 //
2376 // While similar to this is not quite the same as commonValue == nil. If all the values
2377 // have been filtered out then this will be false but commonValue == nil will be true.
2378 valuesDiffer := false
2379
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002380 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01002381 container := sliceValue.Index(i).Interface().(propertiesContainer)
2382 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00002383 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002384
Paul Duffinc459f892020-04-30 18:08:29 +01002385 if !filter(container) {
2386 expectedValue := property.emptyValue.Interface()
2387 actualValue := fieldValue.Interface()
2388 if !reflect.DeepEqual(expectedValue, actualValue) {
2389 return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue)
2390 }
2391 continue
2392 }
2393
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002394 if commonValue == nil {
2395 // Use the first value as the commonProperties value.
2396 commonValue = &fieldValue
2397 } else {
2398 // If the value does not match the current common value then there is
2399 // no value in common so break out.
2400 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
2401 commonValue = nil
Paul Duffin864e1b42020-05-06 10:23:19 +01002402 valuesDiffer = true
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002403 break
2404 }
2405 }
2406 }
2407
Paul Duffin864e1b42020-05-06 10:23:19 +01002408 // If the fields all have common value then store it in the common struct field
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002409 // and set the input struct's field to the empty value.
2410 if commonValue != nil {
Paul Duffinb28369a2020-05-04 15:39:59 +01002411 emptyValue := property.emptyValue
Paul Duffinc097e362020-03-10 22:50:03 +00002412 fieldGetter(commonStructValue).Set(*commonValue)
Paul Duffinbfdca962022-09-22 16:21:54 +01002413 if !property.keep {
2414 for i := 0; i < sliceValue.Len(); i++ {
2415 container := sliceValue.Index(i).Interface().(propertiesContainer)
2416 itemValue := reflect.ValueOf(container.optimizableProperties())
2417 fieldValue := fieldGetter(itemValue)
2418 fieldValue.Set(emptyValue)
2419 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002420 }
2421 }
Paul Duffin864e1b42020-05-06 10:23:19 +01002422
2423 if valuesDiffer && !property.archVariant {
2424 // The values differ but the property does not support arch variants so it
2425 // is an error.
2426 var details strings.Builder
2427 for i := 0; i < sliceValue.Len(); i++ {
2428 container := sliceValue.Index(i).Interface().(propertiesContainer)
2429 itemValue := reflect.ValueOf(container.optimizableProperties())
2430 fieldValue := fieldGetter(itemValue)
2431
2432 _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface())
2433 }
2434
2435 return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String())
2436 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002437 }
Paul Duffin4b8b7932020-05-06 12:35:38 +01002438
2439 return nil
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002440}