blob: 063597fad12ddf5151fa110180b6320c62c323b7 [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 Duffin1a44a992022-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
Mathew Inwood7e9ddbe2021-07-07 12:47:51 +000038// By default every unversioned module in the generated snapshot has prefer: false. Building it
39// 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 Duffin43f7bf02021-05-05 22:00:51 +010072// SOONG_SDK_SNAPSHOT_VERSION
73// This provides control over the version of the generated snapshot.
74//
75// SOONG_SDK_SNAPSHOT_VERSION=current will generate unversioned and versioned prebuilts and a
76// versioned snapshot module. This is the default behavior. The zip file containing the
77// generated snapshot will be <sdk-name>-current.zip.
78//
79// SOONG_SDK_SNAPSHOT_VERSION=unversioned will generate unversioned prebuilts only and the zip
80// file containing the generated snapshot will be <sdk-name>.zip.
81//
82// SOONG_SDK_SNAPSHOT_VERSION=<number> will generate versioned prebuilts and a versioned
83// snapshot module only. The zip file containing the generated snapshot will be
84// <sdk-name>-<number>.zip.
85//
Paul Duffin39abf8f2021-09-24 14:58:27 +010086// SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE
87// This allows the target build release (i.e. the release version of the build within which
88// the snapshot will be used) of the snapshot to be specified. If unspecified then it defaults
89// to the current build release version. Otherwise, it must be the name of one of the build
90// releases defined in nameToBuildRelease, e.g. S, T, etc..
91//
92// The generated snapshot must only be used in the specified target release. If the target
93// build release is not the current build release then the generated Android.bp file not be
94// checked for compatibility.
95//
96// e.g. if setting SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE=S will cause the generated snapshot
97// to be compatible with S.
98//
Paul Duffin64fb5262021-05-05 21:36:04 +010099
Jiyong Park9b409bc2019-10-11 14:59:13 +0900100var pctx = android.NewPackageContext("android/soong/sdk")
101
Paul Duffin375058f2019-11-29 20:17:53 +0000102var (
103 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
104 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +0000105 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +0000106 CommandDeps: []string{
107 "${config.Zip2ZipCmd}",
108 },
109 },
110 "destdir")
111
112 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
113 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -0700114 Command: `${config.SoongZipCmd} -C $basedir -r $out.rsp -o $out`,
Paul Duffin375058f2019-11-29 20:17:53 +0000115 CommandDeps: []string{
116 "${config.SoongZipCmd}",
117 },
118 Rspfile: "$out.rsp",
119 RspfileContent: "$in",
120 },
121 "basedir")
122
123 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
124 blueprint.RuleParams{
125 Command: `${config.MergeZipsCmd} $out $in`,
126 CommandDeps: []string{
127 "${config.MergeZipsCmd}",
128 },
129 })
130)
131
Paul Duffin43f7bf02021-05-05 22:00:51 +0100132const (
133 soongSdkSnapshotVersionUnversioned = "unversioned"
134 soongSdkSnapshotVersionCurrent = "current"
135)
136
Paul Duffinb645ec82019-11-27 17:43:54 +0000137type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900138 content strings.Builder
139 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +0900140}
141
Paul Duffinb645ec82019-11-27 17:43:54 +0000142// generatedFile abstracts operations for writing contents into a file and emit a build rule
143// for the file.
144type generatedFile struct {
145 generatedContents
146 path android.OutputPath
147}
148
Jiyong Park232e7852019-11-04 12:23:40 +0900149func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900150 return &generatedFile{
Paul Duffinb645ec82019-11-27 17:43:54 +0000151 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park9b409bc2019-10-11 14:59:13 +0900152 }
153}
154
Paul Duffinb645ec82019-11-27 17:43:54 +0000155func (gc *generatedContents) Indent() {
156 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +0900157}
158
Paul Duffinb645ec82019-11-27 17:43:54 +0000159func (gc *generatedContents) Dedent() {
160 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +0900161}
162
Paul Duffina08e4dc2021-06-22 18:19:19 +0100163// IndentedPrintf will add spaces to indent the line to the appropriate level before printing the
164// arguments.
165func (gc *generatedContents) IndentedPrintf(format string, args ...interface{}) {
166 fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format, args...)
167}
168
169// UnindentedPrintf does not add spaces to indent the line to the appropriate level before printing
170// the arguments.
171func (gc *generatedContents) UnindentedPrintf(format string, args ...interface{}) {
172 fmt.Fprintf(&(gc.content), format, args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900173}
174
175func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800176 rb := android.NewRuleBuilder(pctx, ctx)
Paul Duffin11108272020-05-11 22:59:25 +0100177
178 content := gf.content.String()
179
180 // ninja consumes newline characters in rspfile_content. Prevent it by
181 // escaping the backslash in the newline character. The extra backslash
182 // is removed when the rspfile is written to the actual script file
183 content = strings.ReplaceAll(content, "\n", "\\n")
184
Jiyong Park9b409bc2019-10-11 14:59:13 +0900185 rb.Command().
186 Implicits(implicits).
Martin Stjernholmee9b24e2021-04-20 15:54:21 +0100187 Text("echo -n").Text(proptools.ShellEscape(content)).
Paul Duffin11108272020-05-11 22:59:25 +0100188 // convert \\n to \n
Jiyong Park9b409bc2019-10-11 14:59:13 +0900189 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
190 rb.Command().
191 Text("chmod a+x").Output(gf.path)
Colin Crossf1a035e2020-11-16 17:32:30 -0800192 rb.Build(gf.path.Base(), "Build "+gf.path.Base())
Jiyong Park9b409bc2019-10-11 14:59:13 +0900193}
194
Paul Duffin13879572019-11-28 14:31:38 +0000195// Collect all the members.
196//
Paul Duffinb97b1572021-04-29 21:50:40 +0100197// Updates the sdk module with a list of sdkMemberVariantDep instances and details as to which
198// multilibs (32/64/both) are used by this sdk variant.
Paul Duffin6a7e9532020-03-20 17:50:07 +0000199func (s *sdk) collectMembers(ctx android.ModuleContext) {
200 s.multilibUsages = multilibNone
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000201 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
202 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100203 if memberTag, ok := tag.(android.SdkMemberDependencyTag); ok {
Paul Duffineee466e2021-04-27 23:17:56 +0100204 memberType := memberTag.SdkMemberType(child)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900205
Paul Duffin5cca7c42021-05-26 10:16:01 +0100206 // If a nil SdkMemberType was returned then this module should not be added to the sdk.
207 if memberType == nil {
208 return false
209 }
210
Paul Duffin13879572019-11-28 14:31:38 +0000211 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000212 if !memberType.IsInstance(child) {
213 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900214 }
Paul Duffin13879572019-11-28 14:31:38 +0000215
Paul Duffin6a7e9532020-03-20 17:50:07 +0000216 // Keep track of which multilib variants are used by the sdk.
217 s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType)
218
Paul Duffinb97b1572021-04-29 21:50:40 +0100219 var exportedComponentsInfo android.ExportedComponentsInfo
220 if ctx.OtherModuleHasProvider(child, android.ExportedComponentsInfoProvider) {
221 exportedComponentsInfo = ctx.OtherModuleProvider(child, android.ExportedComponentsInfoProvider).(android.ExportedComponentsInfo)
222 }
223
Paul Duffin1a44a992022-05-06 09:38:02 +0000224 var container android.SdkAware
225 if parent != ctx.Module() {
226 container = parent.(android.SdkAware)
227 }
228
Paul Duffina7208112021-04-23 21:20:20 +0100229 export := memberTag.ExportMember()
Paul Duffinb97b1572021-04-29 21:50:40 +0100230 s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{
Paul Duffin1a44a992022-05-06 09:38:02 +0000231 sdkVariant: s,
232 memberType: memberType,
233 variant: child.(android.SdkAware),
234 container: container,
235 export: export,
236 exportedComponentsInfo: exportedComponentsInfo,
Paul Duffinb97b1572021-04-29 21:50:40 +0100237 })
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000238
Paul Duffin2d3da312021-05-06 12:02:27 +0100239 // Recurse down into the member's dependencies as it may have dependencies that need to be
240 // automatically added to the sdk.
241 return true
Jiyong Park73c54ee2019-10-22 20:31:18 +0900242 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000243
244 return false
Paul Duffin13879572019-11-28 14:31:38 +0000245 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000246}
247
Paul Duffincc3132e2021-04-24 01:10:30 +0100248// groupMemberVariantsByMemberThenType groups the member variant dependencies so that all the
249// variants of each member are grouped together within an sdkMember instance.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000250//
Paul Duffincc3132e2021-04-24 01:10:30 +0100251// The sdkMember instances are then grouped into slices by member type. Within each such slice the
252// sdkMember instances appear in the order they were added as dependencies.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000253//
Paul Duffincc3132e2021-04-24 01:10:30 +0100254// Finally, the member type slices are concatenated together to form a single slice. The order in
255// which they are concatenated is the order in which the member types were registered in the
256// android.SdkMemberTypesRegistry.
Paul Duffinb0c28d62022-07-01 15:56:06 +0000257func (s *sdk) groupMemberVariantsByMemberThenType(ctx android.ModuleContext, targetBuildRelease *buildRelease, memberVariantDeps []sdkMemberVariantDep) []*sdkMember {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000258 byType := make(map[android.SdkMemberType][]*sdkMember)
259 byName := make(map[string]*sdkMember)
260
Paul Duffin21827262021-04-24 12:16:36 +0100261 for _, memberVariantDep := range memberVariantDeps {
262 memberType := memberVariantDep.memberType
263 variant := memberVariantDep.variant
Paul Duffin1356d8c2020-02-25 19:26:33 +0000264
265 name := ctx.OtherModuleName(variant)
266 member := byName[name]
267 if member == nil {
268 member = &sdkMember{memberType: memberType, name: name}
269 byName[name] = member
270 byType[memberType] = append(byType[memberType], member)
Liz Kammer7b710832022-05-12 20:40:00 -0400271 } else if member.memberType != memberType {
272 // validate whether this is the same member type or and overriding member type
273 if memberType.Overrides(member.memberType) {
274 member.memberType = memberType
275 } else if !member.memberType.Overrides(memberType) {
276 ctx.ModuleErrorf("Incompatible member types %q %q", member.memberType, memberType)
277 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000278 }
279
Paul Duffin1356d8c2020-02-25 19:26:33 +0000280 // Only append new variants to the list. This is needed because a member can be both
281 // exported by the sdk and also be a transitive sdk member.
282 member.variants = appendUniqueVariants(member.variants, variant)
283 }
Paul Duffin13879572019-11-28 14:31:38 +0000284 var members []*sdkMember
Paul Duffin62782de2021-07-14 12:05:16 +0100285 for _, memberListProperty := range s.memberTypeListProperties() {
Paul Duffinb0c28d62022-07-01 15:56:06 +0000286 memberType := memberListProperty.memberType
287
288 if !isMemberTypeSupportedByTargetBuildRelease(memberType, targetBuildRelease) {
289 continue
290 }
291
292 membersOfType := byType[memberType]
Paul Duffin13879572019-11-28 14:31:38 +0000293 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900294 }
295
Paul Duffin6a7e9532020-03-20 17:50:07 +0000296 return members
Jiyong Park73c54ee2019-10-22 20:31:18 +0900297}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900298
Paul Duffinb0c28d62022-07-01 15:56:06 +0000299// isMemberTypeSupportedByTargetBuildRelease returns true if the member type is supported by the
300// target build release.
301func isMemberTypeSupportedByTargetBuildRelease(memberType android.SdkMemberType, targetBuildRelease *buildRelease) bool {
302 supportedByTargetBuildRelease := true
303 supportedBuildReleases := memberType.SupportedBuildReleases()
304 if supportedBuildReleases == "" {
305 supportedBuildReleases = "S+"
306 }
307
308 set, err := parseBuildReleaseSet(supportedBuildReleases)
309 if err != nil {
310 panic(fmt.Errorf("member type %s has invalid supported build releases %q: %s",
311 memberType.SdkPropertyName(), supportedBuildReleases, err))
312 }
313 if !set.contains(targetBuildRelease) {
314 supportedByTargetBuildRelease = false
315 }
316 return supportedByTargetBuildRelease
317}
318
Paul Duffin72910952020-01-20 18:16:30 +0000319func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
320 for _, v := range variants {
321 if v == newVariant {
322 return variants
323 }
324 }
325 return append(variants, newVariant)
326}
327
Paul Duffin51509a12022-04-06 12:48:09 +0000328// BUILD_NUMBER_FILE is the name of the file in the snapshot zip that will contain the number of
329// the build from which the snapshot was produced.
330const BUILD_NUMBER_FILE = "snapshot-creation-build-number.txt"
331
Jiyong Park73c54ee2019-10-22 20:31:18 +0900332// SDK directory structure
333// <sdk_root>/
334// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
335// <api_ver>/ : below this directory are all auto-generated
336// Android.bp : definition of 'sdk_snapshot' module is here
337// aidl/
338// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
339// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900340// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900341// include/
342// bionic/libc/include/stdlib.h : an exported header file
343// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900344// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900345// <arch>/include/ : arch-specific exported headers
346// <arch>/include_gen/ : arch-specific generated headers
347// <arch>/lib/
348// libFoo.so : a stub library
349
Jiyong Park232e7852019-11-04 12:23:40 +0900350// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900351// This isn't visible to users, so could be changed in future.
352func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
353 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
354}
355
Jiyong Park232e7852019-11-04 12:23:40 +0900356// buildSnapshot is the main function in this source file. It creates rules to copy
357// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffin1a44a992022-05-06 09:38:02 +0000358func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000359
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 Duffinb97b1572021-04-29 21:50:40 +0100370 // Record the names of all the members, both explicitly specified and implicitly
371 // included.
372 allMembersByName := make(map[string]struct{})
373 exportedMembersByName := make(map[string]struct{})
Paul Duffin62131702021-05-07 01:10:01 +0100374
Paul Duffinb97b1572021-04-29 21:50:40 +0100375 addMember := func(name string, export bool) {
376 allMembersByName[name] = struct{}{}
377 if export {
378 exportedMembersByName[name] = struct{}{}
379 }
380 }
381
382 for _, memberVariantDep := range memberVariantDeps {
383 name := memberVariantDep.variant.Name()
384 export := memberVariantDep.export
385
386 addMember(name, export)
387
388 // Add any components provided by the module.
389 for _, component := range memberVariantDep.exportedComponentsInfo.Components {
390 addMember(component, export)
391 }
392
393 if memberVariantDep.memberType == android.LicenseModuleSdkMemberType {
394 hasLicenses = true
Paul Duffin865171e2020-03-02 18:38:15 +0000395 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000396 }
397
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000398 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900399
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000400 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000401
402 bpFile := &bpFile{
403 modules: make(map[string]*bpModule),
404 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000405
Paul Duffin43f7bf02021-05-05 22:00:51 +0100406 config := ctx.Config()
407 version := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_VERSION", "current")
408
409 // Generate versioned modules in the snapshot unless an unversioned snapshot has been requested.
410 generateVersioned := version != soongSdkSnapshotVersionUnversioned
411
412 // Generate unversioned modules in the snapshot unless a numbered snapshot has been requested.
413 //
414 // Unversioned modules are not required in that case because the numbered version will be a
415 // finalized version of the snapshot that is intended to be kept separate from the
416 generateUnversioned := version == soongSdkSnapshotVersionUnversioned || version == soongSdkSnapshotVersionCurrent
Paul Duffin1a44a992022-05-06 09:38:02 +0000417 snapshotFileSuffix := ""
Paul Duffin43f7bf02021-05-05 22:00:51 +0100418 if generateVersioned {
Paul Duffin1a44a992022-05-06 09:38:02 +0000419 snapshotFileSuffix = "-" + version
Paul Duffin43f7bf02021-05-05 22:00:51 +0100420 }
421
Paul Duffin39abf8f2021-09-24 14:58:27 +0100422 currentBuildRelease := latestBuildRelease()
423 targetBuildReleaseEnv := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", currentBuildRelease.name)
424 targetBuildRelease, err := nameToRelease(targetBuildReleaseEnv)
425 if err != nil {
426 ctx.ModuleErrorf("invalid SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE: %s", err)
427 targetBuildRelease = currentBuildRelease
428 }
429
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000430 builder := &snapshotBuilder{
Paul Duffin13f02712020-03-06 12:30:43 +0000431 ctx: ctx,
432 sdk: s,
Paul Duffin43f7bf02021-05-05 22:00:51 +0100433 version: version,
Paul Duffin13f02712020-03-06 12:30:43 +0000434 snapshotDir: snapshotDir.OutputPath,
435 copies: make(map[string]string),
436 filesToZip: []android.Path{bp.path},
437 bpFile: bpFile,
438 prebuiltModules: make(map[string]*bpModule),
439 allMembersByName: allMembersByName,
440 exportedMembersByName: exportedMembersByName,
Paul Duffin39abf8f2021-09-24 14:58:27 +0100441 targetBuildRelease: targetBuildRelease,
Jiyong Park73c54ee2019-10-22 20:31:18 +0900442 }
Paul Duffinac37c502019-11-26 18:02:20 +0000443 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900444
Paul Duffin62131702021-05-07 01:10:01 +0100445 // If the sdk snapshot includes any license modules then add a package module which has a
446 // default_applicable_licenses property. That will prevent the LSC license process from updating
447 // the generated Android.bp file to add a package module that includes all licenses used by all
448 // the modules in that package. That would be unnecessary as every module in the sdk should have
449 // their own licenses property specified.
450 if hasLicenses {
451 pkg := bpFile.newModule("package")
452 property := "default_applicable_licenses"
453 pkg.AddCommentForProperty(property, `
454A default list here prevents the license LSC from adding its own list which would
455be unnecessary as every module in the sdk already has its own licenses property.
456`)
457 pkg.AddProperty(property, []string{"Android-Apache-2.0"})
458 bpFile.AddModule(pkg)
459 }
460
Paul Duffin0df49682021-05-07 01:10:01 +0100461 // Group the variants for each member module together and then group the members of each member
462 // type together.
Paul Duffinb0c28d62022-07-01 15:56:06 +0000463 members := s.groupMemberVariantsByMemberThenType(ctx, targetBuildRelease, memberVariantDeps)
Paul Duffin0df49682021-05-07 01:10:01 +0100464
465 // Create the prebuilt modules for each of the member modules.
Paul Duffind19f8942021-07-14 12:08:37 +0100466 traits := s.gatherTraits()
Paul Duffin13ad94f2020-02-19 16:19:27 +0000467 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000468 memberType := member.memberType
Paul Duffin3a4eb502020-03-19 16:11:18 +0000469
Paul Duffind19f8942021-07-14 12:08:37 +0100470 name := member.name
471 requiredTraits := traits[name]
472 if requiredTraits == nil {
473 requiredTraits = android.EmptySdkMemberTraitSet()
474 }
475
476 // Create the snapshot for the member.
477 memberCtx := &memberContext{ctx, builder, memberType, name, requiredTraits}
Paul Duffin3a4eb502020-03-19 16:11:18 +0000478
479 prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100480 s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
Jiyong Park73c54ee2019-10-22 20:31:18 +0900481 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900482
Paul Duffine6c0d842020-01-15 14:08:51 +0000483 // Create a transformer that will transform an unversioned module into a versioned module.
484 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
485
Paul Duffin72910952020-01-20 18:16:30 +0000486 // Create a transformer that will transform an unversioned module by replacing any references
487 // to internal members with a unique module name and setting prefer: false.
Paul Duffin64fb5262021-05-05 21:36:04 +0100488 unversionedTransformer := unversionedTransformation{
489 builder: builder,
Paul Duffin64fb5262021-05-05 21:36:04 +0100490 }
Paul Duffin72910952020-01-20 18:16:30 +0000491
Paul Duffinb645ec82019-11-27 17:43:54 +0000492 for _, unversioned := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000493 // Prune any empty property sets.
494 unversioned = unversioned.transform(pruneEmptySetTransformer{})
495
Paul Duffin43f7bf02021-05-05 22:00:51 +0100496 if generateVersioned {
497 // Copy the unversioned module so it can be modified to make it versioned.
498 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000499
Paul Duffin43f7bf02021-05-05 22:00:51 +0100500 // Transform the unversioned module into a versioned one.
501 versioned.transform(unversionedToVersionedTransformer)
502 bpFile.AddModule(versioned)
503 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000504
Paul Duffin43f7bf02021-05-05 22:00:51 +0100505 if generateUnversioned {
506 // Transform the unversioned module to make it suitable for use in the snapshot.
507 unversioned.transform(unversionedTransformer)
508 bpFile.AddModule(unversioned)
509 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000510 }
511
Paul Duffin43f7bf02021-05-05 22:00:51 +0100512 if generateVersioned {
513 // Add the sdk/module_exports_snapshot module to the bp file.
514 s.addSnapshotModule(ctx, builder, sdkVariants, memberVariantDeps)
515 }
Paul Duffin26197a62021-04-24 00:34:10 +0100516
517 // generate Android.bp
518 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
519 generateBpContents(&bp.generatedContents, bpFile)
520
521 contents := bp.content.String()
Paul Duffin39abf8f2021-09-24 14:58:27 +0100522 // If the snapshot is being generated for the current build release then check the syntax to make
523 // sure that it is compatible.
524 if targetBuildRelease == currentBuildRelease {
525 syntaxCheckSnapshotBpFile(ctx, contents)
526 }
Paul Duffin26197a62021-04-24 00:34:10 +0100527
528 bp.build(pctx, ctx, nil)
529
Paul Duffin51509a12022-04-06 12:48:09 +0000530 // Copy the build number file into the snapshot.
531 builder.CopyToSnapshot(ctx.Config().BuildNumberFile(ctx), BUILD_NUMBER_FILE)
532
Paul Duffin26197a62021-04-24 00:34:10 +0100533 filesToZip := builder.filesToZip
534
535 // zip them all
Paul Duffin1a44a992022-05-06 09:38:02 +0000536 zipPath := fmt.Sprintf("%s%s.zip", ctx.ModuleName(), snapshotFileSuffix)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100537 outputZipFile := android.PathForModuleOut(ctx, zipPath).OutputPath
Paul Duffin26197a62021-04-24 00:34:10 +0100538 outputDesc := "Building snapshot for " + ctx.ModuleName()
539
540 // If there are no zips to merge then generate the output zip directly.
541 // Otherwise, generate an intermediate zip file into which other zips can be
542 // merged.
543 var zipFile android.OutputPath
544 var desc string
545 if len(builder.zipsToMerge) == 0 {
546 zipFile = outputZipFile
547 desc = outputDesc
548 } else {
Paul Duffin1a44a992022-05-06 09:38:02 +0000549 intermediatePath := fmt.Sprintf("%s%s.unmerged.zip", ctx.ModuleName(), snapshotFileSuffix)
Paul Duffin43f7bf02021-05-05 22:00:51 +0100550 zipFile = android.PathForModuleOut(ctx, intermediatePath).OutputPath
Paul Duffin26197a62021-04-24 00:34:10 +0100551 desc = "Building intermediate snapshot for " + ctx.ModuleName()
552 }
553
554 ctx.Build(pctx, android.BuildParams{
555 Description: desc,
556 Rule: zipFiles,
557 Inputs: filesToZip,
558 Output: zipFile,
559 Args: map[string]string{
560 "basedir": builder.snapshotDir.String(),
561 },
562 })
563
564 if len(builder.zipsToMerge) != 0 {
565 ctx.Build(pctx, android.BuildParams{
566 Description: outputDesc,
567 Rule: mergeZips,
568 Input: zipFile,
569 Inputs: builder.zipsToMerge,
570 Output: outputZipFile,
571 })
572 }
573
Paul Duffin1a44a992022-05-06 09:38:02 +0000574 modules := s.generateInfoData(ctx, memberVariantDeps)
575
576 // Output the modules information as pretty printed JSON.
577 info := newGeneratedFile(ctx, fmt.Sprintf("%s%s.info", ctx.ModuleName(), snapshotFileSuffix))
578 output, err := json.MarshalIndent(modules, "", " ")
579 if err != nil {
580 ctx.ModuleErrorf("error generating %q: %s", info, err)
581 }
582 builder.infoContents = string(output)
583 info.generatedContents.UnindentedPrintf("%s", output)
584 info.build(pctx, ctx, nil)
585 infoPath := info.path
586 installedInfo := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), infoPath.Base(), infoPath)
587 s.infoFile = android.OptionalPathForPath(installedInfo)
588
589 // Install the zip, making sure that the info file has been installed as well.
590 installedZip := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), outputZipFile.Base(), outputZipFile, installedInfo)
591 s.snapshotFile = android.OptionalPathForPath(installedZip)
592}
593
594type moduleInfo struct {
595 // The type of the module, e.g. java_sdk_library
596 moduleType string
597 // The name of the module.
598 name string
599 // A list of additional dependencies of the module.
600 deps []string
Paul Duffindec81382022-05-16 13:10:47 +0000601 // Additional member specific properties.
602 // These will be added into the generated JSON alongside the above properties.
603 memberSpecific map[string]interface{}
Paul Duffin1a44a992022-05-06 09:38:02 +0000604}
605
606func (m *moduleInfo) MarshalJSON() ([]byte, error) {
607 buffer := bytes.Buffer{}
608
609 separator := ""
610 writeObjectPair := func(key string, value interface{}) {
611 buffer.WriteString(fmt.Sprintf("%s%q: ", separator, key))
612 b, err := json.Marshal(value)
613 if err != nil {
614 panic(err)
615 }
616 buffer.Write(b)
617 separator = ","
618 }
619
620 buffer.WriteString("{")
621 writeObjectPair("@type", m.moduleType)
622 writeObjectPair("@name", m.name)
623 if m.deps != nil {
624 writeObjectPair("@deps", m.deps)
625 }
Paul Duffindec81382022-05-16 13:10:47 +0000626 for _, k := range android.SortedStringKeys(m.memberSpecific) {
627 v := m.memberSpecific[k]
Paul Duffin1a44a992022-05-06 09:38:02 +0000628 writeObjectPair(k, v)
629 }
630 buffer.WriteString("}")
631 return buffer.Bytes(), nil
632}
633
634var _ json.Marshaler = (*moduleInfo)(nil)
635
636// generateInfoData creates a list of moduleInfo structures that will be marshalled into JSON.
637func (s *sdk) generateInfoData(ctx android.ModuleContext, memberVariantDeps []sdkMemberVariantDep) interface{} {
638 modules := []*moduleInfo{}
639 sdkInfo := moduleInfo{
Paul Duffindec81382022-05-16 13:10:47 +0000640 moduleType: "sdk",
641 name: ctx.ModuleName(),
642 memberSpecific: map[string]interface{}{},
Paul Duffin1a44a992022-05-06 09:38:02 +0000643 }
644 modules = append(modules, &sdkInfo)
645
646 name2Info := map[string]*moduleInfo{}
647 getModuleInfo := func(module android.Module) *moduleInfo {
648 name := module.Name()
649 info := name2Info[name]
650 if info == nil {
651 moduleType := ctx.OtherModuleType(module)
652 // Remove any suffix added when creating modules dynamically.
653 moduleType = strings.Split(moduleType, "__")[0]
654 info = &moduleInfo{
655 moduleType: moduleType,
656 name: name,
657 }
Paul Duffindec81382022-05-16 13:10:47 +0000658
659 additionalSdkInfo := ctx.OtherModuleProvider(module, android.AdditionalSdkInfoProvider).(android.AdditionalSdkInfo)
660 info.memberSpecific = additionalSdkInfo.Properties
661
Paul Duffin1a44a992022-05-06 09:38:02 +0000662 name2Info[name] = info
663 }
664 return info
665 }
666
667 for _, memberVariantDep := range memberVariantDeps {
668 propertyName := memberVariantDep.memberType.SdkPropertyName()
669 var list []string
Paul Duffindec81382022-05-16 13:10:47 +0000670 if v, ok := sdkInfo.memberSpecific[propertyName]; ok {
Paul Duffin1a44a992022-05-06 09:38:02 +0000671 list = v.([]string)
672 }
673
674 memberName := memberVariantDep.variant.Name()
675 list = append(list, memberName)
Paul Duffindec81382022-05-16 13:10:47 +0000676 sdkInfo.memberSpecific[propertyName] = android.SortedUniqueStrings(list)
Paul Duffin1a44a992022-05-06 09:38:02 +0000677
678 if memberVariantDep.container != nil {
679 containerInfo := getModuleInfo(memberVariantDep.container)
680 containerInfo.deps = android.SortedUniqueStrings(append(containerInfo.deps, memberName))
681 }
682
683 // Make sure that the module info is created for each module.
684 getModuleInfo(memberVariantDep.variant)
685 }
686
687 for _, memberName := range android.SortedStringKeys(name2Info) {
688 info := name2Info[memberName]
689 modules = append(modules, info)
690 }
691
692 return modules
Paul Duffin26197a62021-04-24 00:34:10 +0100693}
694
Paul Duffinb97b1572021-04-29 21:50:40 +0100695// filterOutComponents removes any item from the deps list that is a component of another item in
696// the deps list, e.g. if the deps list contains "foo" and "foo.stubs" which is component of "foo"
697// then it will remove "foo.stubs" from the deps.
698func filterOutComponents(ctx android.ModuleContext, deps []sdkMemberVariantDep) []sdkMemberVariantDep {
699 // Collate the set of components that all the modules added to the sdk provide.
700 components := map[string]*sdkMemberVariantDep{}
701 for i, _ := range deps {
702 dep := &deps[i]
703 for _, c := range dep.exportedComponentsInfo.Components {
704 components[c] = dep
705 }
706 }
707
708 // If no module provides components then return the input deps unfiltered.
709 if len(components) == 0 {
710 return deps
711 }
712
713 filtered := make([]sdkMemberVariantDep, 0, len(deps))
714 for _, dep := range deps {
715 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(dep.variant))
716 if owner, ok := components[name]; ok {
717 // This is a component of another module that is a member of the sdk.
718
719 // If the component is exported but the owning module is not then the configuration is not
720 // supported.
721 if dep.export && !owner.export {
722 ctx.ModuleErrorf("Module %s is internal to the SDK but provides component %s which is used outside the SDK")
723 continue
724 }
725
726 // This module must not be added to the list of members of the sdk as that would result in a
727 // duplicate module in the sdk snapshot.
728 continue
729 }
730
731 filtered = append(filtered, dep)
732 }
733 return filtered
734}
735
Paul Duffin26197a62021-04-24 00:34:10 +0100736// addSnapshotModule adds the sdk_snapshot/module_exports_snapshot module to the builder.
Paul Duffin21827262021-04-24 12:16:36 +0100737func (s *sdk) addSnapshotModule(ctx android.ModuleContext, builder *snapshotBuilder, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) {
Paul Duffin26197a62021-04-24 00:34:10 +0100738 bpFile := builder.bpFile
739
Paul Duffinb645ec82019-11-27 17:43:54 +0000740 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000741 var snapshotModuleType string
742 if s.properties.Module_exports {
743 snapshotModuleType = "module_exports_snapshot"
744 } else {
745 snapshotModuleType = "sdk_snapshot"
746 }
747 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000748 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000749
750 // Make sure that the snapshot has the same visibility as the sdk.
Paul Duffin157f40f2020-09-29 16:01:08 +0100751 visibility := android.EffectiveVisibilityRules(ctx, s).Strings()
Paul Duffin593b3c92019-12-05 14:31:48 +0000752 if len(visibility) != 0 {
753 snapshotModule.AddProperty("visibility", visibility)
754 }
755
Paul Duffin865171e2020-03-02 18:38:15 +0000756 addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000757
Paul Duffincd064672021-04-24 00:47:29 +0100758 combinedPropertiesList := s.collateSnapshotModuleInfo(ctx, sdkVariants, memberVariantDeps)
Paul Duffin2d1bb892021-04-24 11:32:59 +0100759 commonCombinedProperties := s.optimizeSnapshotModuleProperties(ctx, combinedPropertiesList)
Paul Duffin865171e2020-03-02 18:38:15 +0000760
Paul Duffin2d1bb892021-04-24 11:32:59 +0100761 s.addSnapshotPropertiesToPropertySet(builder, snapshotModule, commonCombinedProperties)
Martin Stjernholm4cfa2c62020-07-10 19:55:36 +0100762
Paul Duffin6a7e9532020-03-20 17:50:07 +0000763 targetPropertySet := snapshotModule.AddPropertySet("target")
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100764
Paul Duffin2d1bb892021-04-24 11:32:59 +0100765 // Create a mapping from osType to combined properties.
766 osTypeToCombinedProperties := map[android.OsType]*combinedSnapshotModuleProperties{}
767 for _, combined := range combinedPropertiesList {
768 osTypeToCombinedProperties[combined.sdkVariant.Os()] = combined
769 }
770
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100771 // Iterate over the os types in a fixed order.
Paul Duffin865171e2020-03-02 18:38:15 +0000772 for _, osType := range s.getPossibleOsTypes() {
Paul Duffin2d1bb892021-04-24 11:32:59 +0100773 if combined, ok := osTypeToCombinedProperties[osType]; ok {
Paul Duffincc3132e2021-04-24 01:10:30 +0100774 osPropertySet := targetPropertySet.AddPropertySet(osType.Name)
Paul Duffin6a7e9532020-03-20 17:50:07 +0000775
Paul Duffin2d1bb892021-04-24 11:32:59 +0100776 s.addSnapshotPropertiesToPropertySet(builder, osPropertySet, combined)
Paul Duffin13879572019-11-28 14:31:38 +0000777 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000778 }
Paul Duffin865171e2020-03-02 18:38:15 +0000779
Jiyong Park8fe14e62020-10-19 22:47:34 +0900780 // If host is supported and any member is host OS dependent then disable host
781 // by default, so that we can enable each host OS variant explicitly. This
782 // avoids problems with implicitly enabled OS variants when the snapshot is
783 // used, which might be different from this run (e.g. different build OS).
784 if s.HostSupported() {
785 var supportedHostTargets []string
Paul Duffin21827262021-04-24 12:16:36 +0100786 for _, memberVariantDep := range memberVariantDeps {
787 if memberVariantDep.memberType.IsHostOsDependent() && memberVariantDep.variant.Target().Os.Class == android.Host {
788 targetString := memberVariantDep.variant.Target().Os.String() + "_" + memberVariantDep.variant.Target().Arch.ArchType.String()
Jiyong Park8fe14e62020-10-19 22:47:34 +0900789 if !android.InList(targetString, supportedHostTargets) {
790 supportedHostTargets = append(supportedHostTargets, targetString)
791 }
792 }
793 }
794 if len(supportedHostTargets) > 0 {
795 hostPropertySet := targetPropertySet.AddPropertySet("host")
796 hostPropertySet.AddProperty("enabled", false)
797 }
798 // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
799 for _, hostTarget := range supportedHostTargets {
800 propertySet := targetPropertySet.AddPropertySet(hostTarget)
801 propertySet.AddProperty("enabled", true)
802 }
803 }
804
Paul Duffin865171e2020-03-02 18:38:15 +0000805 // Prune any empty property sets.
806 snapshotModule.transform(pruneEmptySetTransformer{})
807
Paul Duffinb645ec82019-11-27 17:43:54 +0000808 bpFile.AddModule(snapshotModule)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900809}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000810
Paul Duffinf88d8e02020-05-07 20:21:34 +0100811// Check the syntax of the generated Android.bp file contents and if they are
812// invalid then log an error with the contents (tagged with line numbers) and the
813// errors that were found so that it is easy to see where the problem lies.
814func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) {
815 errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents)
816 if len(errs) != 0 {
817 message := &strings.Builder{}
818 _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot:
819
820Generated Android.bp contents
821========================================================================
822`)
823 for i, line := range strings.Split(contents, "\n") {
824 _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line)
825 }
826
827 _, _ = fmt.Fprint(message, `
828========================================================================
829
830Errors found:
831`)
832
833 for _, err := range errs {
834 _, _ = fmt.Fprintf(message, "%s\n", err.Error())
835 }
836
837 ctx.ModuleErrorf("%s", message.String())
838 }
839}
840
Paul Duffin4b8b7932020-05-06 12:35:38 +0100841func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
842 err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
843 if err != nil {
844 ctx.ModuleErrorf("error extracting common properties: %s", err)
845 }
846}
847
Paul Duffinfbe470e2021-04-24 12:37:13 +0100848// snapshotModuleStaticProperties contains snapshot static (i.e. not dynamically generated) properties.
849type snapshotModuleStaticProperties struct {
850 Compile_multilib string `android:"arch_variant"`
851}
852
Paul Duffin2d1bb892021-04-24 11:32:59 +0100853// combinedSnapshotModuleProperties are the properties that are associated with the snapshot module.
854type combinedSnapshotModuleProperties struct {
855 // The sdk variant from which this information was collected.
856 sdkVariant *sdk
857
858 // Static snapshot module properties.
859 staticProperties *snapshotModuleStaticProperties
860
861 // The dynamically generated member list properties.
862 dynamicProperties interface{}
863}
864
865// collateSnapshotModuleInfo collates all the snapshot module info from supplied sdk variants.
Paul Duffincd064672021-04-24 00:47:29 +0100866func (s *sdk) collateSnapshotModuleInfo(ctx android.BaseModuleContext, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) []*combinedSnapshotModuleProperties {
867 sdkVariantToCombinedProperties := map[*sdk]*combinedSnapshotModuleProperties{}
Paul Duffin2d1bb892021-04-24 11:32:59 +0100868 var list []*combinedSnapshotModuleProperties
869 for _, sdkVariant := range sdkVariants {
870 staticProperties := &snapshotModuleStaticProperties{
871 Compile_multilib: sdkVariant.multilibUsages.String(),
872 }
Paul Duffin62782de2021-07-14 12:05:16 +0100873 dynamicProperties := s.dynamicSdkMemberTypes.createMemberTypeListProperties()
Paul Duffin2d1bb892021-04-24 11:32:59 +0100874
Paul Duffincd064672021-04-24 00:47:29 +0100875 combinedProperties := &combinedSnapshotModuleProperties{
Paul Duffin2d1bb892021-04-24 11:32:59 +0100876 sdkVariant: sdkVariant,
877 staticProperties: staticProperties,
878 dynamicProperties: dynamicProperties,
Paul Duffincd064672021-04-24 00:47:29 +0100879 }
880 sdkVariantToCombinedProperties[sdkVariant] = combinedProperties
881
882 list = append(list, combinedProperties)
Paul Duffin2d1bb892021-04-24 11:32:59 +0100883 }
Paul Duffincd064672021-04-24 00:47:29 +0100884
885 for _, memberVariantDep := range memberVariantDeps {
886 // If the member dependency is internal then do not add the dependency to the snapshot member
887 // list properties.
888 if !memberVariantDep.export {
889 continue
890 }
891
892 combined := sdkVariantToCombinedProperties[memberVariantDep.sdkVariant]
Paul Duffin62782de2021-07-14 12:05:16 +0100893 memberListProperty := s.memberTypeListProperty(memberVariantDep.memberType)
Paul Duffincd064672021-04-24 00:47:29 +0100894 memberName := ctx.OtherModuleName(memberVariantDep.variant)
895
Paul Duffin13082052021-05-11 00:31:38 +0100896 if memberListProperty.getter == nil {
897 continue
898 }
899
Paul Duffincd064672021-04-24 00:47:29 +0100900 // Append the member to the appropriate list, if it is not already present in the list.
Paul Duffin13082052021-05-11 00:31:38 +0100901 memberList := memberListProperty.getter(combined.dynamicProperties)
Paul Duffincd064672021-04-24 00:47:29 +0100902 if !android.InList(memberName, memberList) {
903 memberList = append(memberList, memberName)
904 }
Paul Duffin13082052021-05-11 00:31:38 +0100905 memberListProperty.setter(combined.dynamicProperties, memberList)
Paul Duffincd064672021-04-24 00:47:29 +0100906 }
907
Paul Duffin2d1bb892021-04-24 11:32:59 +0100908 return list
909}
910
911func (s *sdk) optimizeSnapshotModuleProperties(ctx android.ModuleContext, list []*combinedSnapshotModuleProperties) *combinedSnapshotModuleProperties {
912
913 // Extract the dynamic properties and add them to a list of propertiesContainer.
914 propertyContainers := []propertiesContainer{}
915 for _, i := range list {
916 propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{
917 sdkVariant: i.sdkVariant,
918 properties: i.dynamicProperties,
919 })
920 }
921
922 // Extract the common members, removing them from the original properties.
Paul Duffin62782de2021-07-14 12:05:16 +0100923 commonDynamicProperties := s.dynamicSdkMemberTypes.createMemberTypeListProperties()
Paul Duffin2d1bb892021-04-24 11:32:59 +0100924 extractor := newCommonValueExtractor(commonDynamicProperties)
925 extractCommonProperties(ctx, extractor, commonDynamicProperties, propertyContainers)
926
927 // Extract the static properties and add them to a list of propertiesContainer.
928 propertyContainers = []propertiesContainer{}
929 for _, i := range list {
930 propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{
931 sdkVariant: i.sdkVariant,
932 properties: i.staticProperties,
933 })
934 }
935
936 commonStaticProperties := &snapshotModuleStaticProperties{}
937 extractor = newCommonValueExtractor(commonStaticProperties)
938 extractCommonProperties(ctx, extractor, &commonStaticProperties, propertyContainers)
939
940 return &combinedSnapshotModuleProperties{
941 sdkVariant: nil,
942 staticProperties: commonStaticProperties,
943 dynamicProperties: commonDynamicProperties,
944 }
945}
946
947func (s *sdk) addSnapshotPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, combined *combinedSnapshotModuleProperties) {
948 staticProperties := combined.staticProperties
Paul Duffinfbe470e2021-04-24 12:37:13 +0100949 multilib := staticProperties.Compile_multilib
950 if multilib != "" && multilib != "both" {
951 // Compile_multilib defaults to both so only needs to be set when it's specified and not both.
952 propertySet.AddProperty("compile_multilib", multilib)
953 }
954
Paul Duffin2d1bb892021-04-24 11:32:59 +0100955 dynamicMemberTypeListProperties := combined.dynamicProperties
Paul Duffin62782de2021-07-14 12:05:16 +0100956 for _, memberListProperty := range s.memberTypeListProperties() {
Paul Duffin13082052021-05-11 00:31:38 +0100957 if memberListProperty.getter == nil {
958 continue
959 }
Paul Duffinb0c28d62022-07-01 15:56:06 +0000960 if !isMemberTypeSupportedByTargetBuildRelease(memberListProperty.memberType, builder.targetBuildRelease) {
961 continue
962 }
Paul Duffin865171e2020-03-02 18:38:15 +0000963 names := memberListProperty.getter(dynamicMemberTypeListProperties)
964 if len(names) > 0 {
Paul Duffin13f02712020-03-06 12:30:43 +0000965 propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names, false))
Paul Duffin865171e2020-03-02 18:38:15 +0000966 }
967 }
968}
969
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000970type propertyTag struct {
971 name string
972}
973
Paul Duffin94289702021-09-09 15:38:32 +0100974var _ android.BpPropertyTag = propertyTag{}
975
Paul Duffin0cb37b92020-03-04 14:52:46 +0000976// A BpPropertyTag to add to a property that contains references to other sdk members.
977//
978// This will cause the references to be rewritten to a versioned reference in the version
979// specific instance of a snapshot module.
Paul Duffin13f02712020-03-06 12:30:43 +0000980var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
Paul Duffin13f02712020-03-06 12:30:43 +0000981var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000982
Paul Duffin0cb37b92020-03-04 14:52:46 +0000983// A BpPropertyTag that indicates the property should only be present in the versioned
984// module.
985//
986// This will cause the property to be removed from the unversioned instance of a
987// snapshot module.
988var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"}
989
Paul Duffine6c0d842020-01-15 14:08:51 +0000990type unversionedToVersionedTransformation struct {
991 identityTransformation
992 builder *snapshotBuilder
993}
994
Paul Duffine6c0d842020-01-15 14:08:51 +0000995func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
996 // Use a versioned name for the module but remember the original name for the
997 // snapshot.
Paul Duffin0df49682021-05-07 01:10:01 +0100998 name := module.Name()
Paul Duffin13f02712020-03-06 12:30:43 +0000999 module.setProperty("name", t.builder.versionedSdkMemberName(name, true))
Paul Duffine6c0d842020-01-15 14:08:51 +00001000 module.insertAfter("name", "sdk_member_name", name)
Paul Duffin83ad9562021-05-10 23:49:04 +01001001 // Remove the prefer property if present as versioned modules never need marking with prefer.
1002 module.removeProperty("prefer")
Paul Duffinfb9a7f92021-07-06 17:18:42 +01001003 // Ditto for use_source_config_var
1004 module.removeProperty("use_source_config_var")
Paul Duffine6c0d842020-01-15 14:08:51 +00001005 return module
1006}
1007
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001008func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +00001009 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
1010 required := tag == requiredSdkMemberReferencePropertyTag
1011 return t.builder.versionedSdkMemberNames(value.([]string), required), tag
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001012 } else {
1013 return value, tag
1014 }
1015}
1016
Paul Duffin72910952020-01-20 18:16:30 +00001017type unversionedTransformation struct {
1018 identityTransformation
1019 builder *snapshotBuilder
1020}
1021
1022func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
1023 // If the module is an internal member then use a unique name for it.
Paul Duffin0df49682021-05-07 01:10:01 +01001024 name := module.Name()
Paul Duffin13f02712020-03-06 12:30:43 +00001025 module.setProperty("name", t.builder.unversionedSdkMemberName(name, true))
Paul Duffin72910952020-01-20 18:16:30 +00001026 return module
1027}
1028
1029func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
Paul Duffin13f02712020-03-06 12:30:43 +00001030 if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
1031 required := tag == requiredSdkMemberReferencePropertyTag
1032 return t.builder.unversionedSdkMemberNames(value.([]string), required), tag
Paul Duffin0cb37b92020-03-04 14:52:46 +00001033 } else if tag == sdkVersionedOnlyPropertyTag {
1034 // The property is not allowed in the unversioned module so remove it.
1035 return nil, nil
Paul Duffin72910952020-01-20 18:16:30 +00001036 } else {
1037 return value, tag
1038 }
1039}
1040
Paul Duffina78f3a72020-02-21 16:29:35 +00001041type pruneEmptySetTransformer struct {
1042 identityTransformation
1043}
1044
1045var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
1046
1047func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
1048 if len(propertySet.properties) == 0 {
1049 return nil, nil
1050 } else {
1051 return propertySet, tag
1052 }
1053}
1054
Paul Duffinb645ec82019-11-27 17:43:54 +00001055func generateBpContents(contents *generatedContents, bpFile *bpFile) {
Paul Duffind0759072021-02-17 11:23:00 +00001056 generateFilteredBpContents(contents, bpFile, func(*bpModule) bool {
1057 return true
1058 })
1059}
1060
1061func generateFilteredBpContents(contents *generatedContents, bpFile *bpFile, moduleFilter func(module *bpModule) bool) {
Paul Duffina08e4dc2021-06-22 18:19:19 +01001062 contents.IndentedPrintf("// This is auto-generated. DO NOT EDIT.\n")
Paul Duffinb645ec82019-11-27 17:43:54 +00001063 for _, bpModule := range bpFile.order {
Paul Duffind0759072021-02-17 11:23:00 +00001064 if moduleFilter(bpModule) {
Paul Duffina08e4dc2021-06-22 18:19:19 +01001065 contents.IndentedPrintf("\n")
1066 contents.IndentedPrintf("%s {\n", bpModule.moduleType)
Paul Duffind0759072021-02-17 11:23:00 +00001067 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffina08e4dc2021-06-22 18:19:19 +01001068 contents.IndentedPrintf("}\n")
Paul Duffind0759072021-02-17 11:23:00 +00001069 }
Paul Duffinb645ec82019-11-27 17:43:54 +00001070 }
Paul Duffinb645ec82019-11-27 17:43:54 +00001071}
1072
1073func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
1074 contents.Indent()
Paul Duffin07ef3cb2020-03-11 18:17:42 +00001075
Paul Duffin0df49682021-05-07 01:10:01 +01001076 addComment := func(name string) {
1077 if text, ok := set.comments[name]; ok {
1078 for _, line := range strings.Split(text, "\n") {
Paul Duffina08e4dc2021-06-22 18:19:19 +01001079 contents.IndentedPrintf("// %s\n", line)
Paul Duffin0df49682021-05-07 01:10:01 +01001080 }
1081 }
1082 }
1083
Paul Duffin07ef3cb2020-03-11 18:17:42 +00001084 // Output the properties first, followed by the nested sets. This ensures a
1085 // consistent output irrespective of whether property sets are created before
1086 // or after the properties. This simplifies the creation of the module.
Paul Duffinb645ec82019-11-27 17:43:54 +00001087 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +00001088 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +00001089
Paul Duffin0df49682021-05-07 01:10:01 +01001090 // Do not write property sets in the properties phase.
1091 if _, ok := value.(*bpPropertySet); ok {
1092 continue
1093 }
1094
1095 addComment(name)
Paul Duffina08e4dc2021-06-22 18:19:19 +01001096 reflectValue := reflect.ValueOf(value)
1097 outputNamedValue(contents, name, reflectValue)
Paul Duffinb645ec82019-11-27 17:43:54 +00001098 }
Paul Duffin07ef3cb2020-03-11 18:17:42 +00001099
1100 for _, name := range set.order {
1101 value := set.getValue(name)
1102
1103 // Only write property sets in the sets phase.
1104 switch v := value.(type) {
1105 case *bpPropertySet:
Paul Duffin0df49682021-05-07 01:10:01 +01001106 addComment(name)
Paul Duffina08e4dc2021-06-22 18:19:19 +01001107 contents.IndentedPrintf("%s: {\n", name)
Paul Duffin07ef3cb2020-03-11 18:17:42 +00001108 outputPropertySet(contents, v)
Paul Duffina08e4dc2021-06-22 18:19:19 +01001109 contents.IndentedPrintf("},\n")
Paul Duffin07ef3cb2020-03-11 18:17:42 +00001110 }
1111 }
1112
Paul Duffinb645ec82019-11-27 17:43:54 +00001113 contents.Dedent()
1114}
1115
Paul Duffina08e4dc2021-06-22 18:19:19 +01001116// outputNamedValue outputs a value that has an associated name. The name will be indented, followed
1117// by the value and then followed by a , and a newline.
1118func outputNamedValue(contents *generatedContents, name string, value reflect.Value) {
1119 contents.IndentedPrintf("%s: ", name)
1120 outputUnnamedValue(contents, value)
1121 contents.UnindentedPrintf(",\n")
1122}
1123
1124// outputUnnamedValue outputs a single value. The value is not indented and is not followed by
1125// either a , or a newline. With multi-line values, e.g. slices, all but the first line will be
1126// indented and all but the last line will end with a newline.
1127func outputUnnamedValue(contents *generatedContents, value reflect.Value) {
1128 valueType := value.Type()
1129 switch valueType.Kind() {
1130 case reflect.Bool:
1131 contents.UnindentedPrintf("%t", value.Bool())
1132
1133 case reflect.String:
1134 contents.UnindentedPrintf("%q", value)
1135
Paul Duffin51227d82021-05-18 12:54:27 +01001136 case reflect.Ptr:
1137 outputUnnamedValue(contents, value.Elem())
1138
Paul Duffina08e4dc2021-06-22 18:19:19 +01001139 case reflect.Slice:
1140 length := value.Len()
1141 if length == 0 {
1142 contents.UnindentedPrintf("[]")
Paul Duffina08e4dc2021-06-22 18:19:19 +01001143 } else {
Paul Duffin51227d82021-05-18 12:54:27 +01001144 firstValue := value.Index(0)
1145 if length == 1 && !multiLineValue(firstValue) {
1146 contents.UnindentedPrintf("[")
1147 outputUnnamedValue(contents, firstValue)
1148 contents.UnindentedPrintf("]")
1149 } else {
1150 contents.UnindentedPrintf("[\n")
1151 contents.Indent()
1152 for i := 0; i < length; i++ {
1153 itemValue := value.Index(i)
1154 contents.IndentedPrintf("")
1155 outputUnnamedValue(contents, itemValue)
1156 contents.UnindentedPrintf(",\n")
1157 }
1158 contents.Dedent()
1159 contents.IndentedPrintf("]")
Paul Duffina08e4dc2021-06-22 18:19:19 +01001160 }
Paul Duffina08e4dc2021-06-22 18:19:19 +01001161 }
1162
Paul Duffin51227d82021-05-18 12:54:27 +01001163 case reflect.Struct:
1164 // Avoid unlimited recursion by requiring every structure to implement android.BpPrintable.
1165 v := value.Interface()
1166 if _, ok := v.(android.BpPrintable); !ok {
1167 panic(fmt.Errorf("property value %#v of type %T does not implement android.BpPrintable", v, v))
1168 }
1169 contents.UnindentedPrintf("{\n")
1170 contents.Indent()
1171 for f := 0; f < valueType.NumField(); f++ {
1172 fieldType := valueType.Field(f)
1173 if fieldType.Anonymous {
1174 continue
1175 }
1176 fieldValue := value.Field(f)
1177 fieldName := fieldType.Name
1178 propertyName := proptools.PropertyNameForField(fieldName)
1179 outputNamedValue(contents, propertyName, fieldValue)
1180 }
1181 contents.Dedent()
1182 contents.IndentedPrintf("}")
1183
Paul Duffina08e4dc2021-06-22 18:19:19 +01001184 default:
1185 panic(fmt.Errorf("Unknown type: %T of value %#v", value, value))
1186 }
1187}
1188
Paul Duffin51227d82021-05-18 12:54:27 +01001189// multiLineValue returns true if the supplied value may require multiple lines in the output.
1190func multiLineValue(value reflect.Value) bool {
1191 kind := value.Kind()
1192 return kind == reflect.Slice || kind == reflect.Struct
1193}
1194
Paul Duffinac37c502019-11-26 18:02:20 +00001195func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +00001196 contents := &generatedContents{}
1197 generateBpContents(contents, s.builderForTests.bpFile)
1198 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +00001199}
1200
Paul Duffin1a44a992022-05-06 09:38:02 +00001201func (s *sdk) GetInfoContentsForTests() string {
1202 return s.builderForTests.infoContents
1203}
1204
Paul Duffind0759072021-02-17 11:23:00 +00001205func (s *sdk) GetUnversionedAndroidBpContentsForTests() string {
1206 contents := &generatedContents{}
1207 generateFilteredBpContents(contents, s.builderForTests.bpFile, func(module *bpModule) bool {
Paul Duffin0df49682021-05-07 01:10:01 +01001208 name := module.Name()
1209 // Include modules that are either unversioned or have no name.
1210 return !strings.Contains(name, "@")
Paul Duffind0759072021-02-17 11:23:00 +00001211 })
1212 return contents.content.String()
1213}
1214
1215func (s *sdk) GetVersionedAndroidBpContentsForTests() string {
1216 contents := &generatedContents{}
1217 generateFilteredBpContents(contents, s.builderForTests.bpFile, func(module *bpModule) bool {
Paul Duffin0df49682021-05-07 01:10:01 +01001218 name := module.Name()
1219 // Include modules that are either versioned or have no name.
1220 return name == "" || strings.Contains(name, "@")
Paul Duffind0759072021-02-17 11:23:00 +00001221 })
1222 return contents.content.String()
1223}
1224
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001225type snapshotBuilder struct {
Paul Duffin43f7bf02021-05-05 22:00:51 +01001226 ctx android.ModuleContext
1227 sdk *sdk
1228
1229 // The version of the generated snapshot.
1230 //
1231 // See the documentation of SOONG_SDK_SNAPSHOT_VERSION above for details of the valid values of
1232 // this field.
1233 version string
1234
Paul Duffinb645ec82019-11-27 17:43:54 +00001235 snapshotDir android.OutputPath
1236 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +00001237
1238 // Map from destination to source of each copy - used to eliminate duplicates and
1239 // detect conflicts.
1240 copies map[string]string
1241
Paul Duffinb645ec82019-11-27 17:43:54 +00001242 filesToZip android.Paths
1243 zipsToMerge android.Paths
1244
Paul Duffin5c211452021-07-15 12:42:44 +01001245 // The path to an empty file.
1246 emptyFile android.WritablePath
1247
Paul Duffinb645ec82019-11-27 17:43:54 +00001248 prebuiltModules map[string]*bpModule
1249 prebuiltOrder []*bpModule
Paul Duffin13f02712020-03-06 12:30:43 +00001250
1251 // The set of all members by name.
1252 allMembersByName map[string]struct{}
1253
1254 // The set of exported members by name.
1255 exportedMembersByName map[string]struct{}
Paul Duffin39abf8f2021-09-24 14:58:27 +01001256
1257 // The target build release for which the snapshot is to be generated.
1258 targetBuildRelease *buildRelease
Paul Duffin1a44a992022-05-06 09:38:02 +00001259
Paul Duffindec81382022-05-16 13:10:47 +00001260 // The contents of the .info file that describes the sdk contents.
Paul Duffin1a44a992022-05-06 09:38:02 +00001261 infoContents string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001262}
1263
1264func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +00001265 if existing, ok := s.copies[dest]; ok {
1266 if existing != src.String() {
1267 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
1268 return
1269 }
1270 } else {
1271 path := s.snapshotDir.Join(s.ctx, dest)
1272 s.ctx.Build(pctx, android.BuildParams{
1273 Rule: android.Cp,
1274 Input: src,
1275 Output: path,
1276 })
1277 s.filesToZip = append(s.filesToZip, path)
1278
1279 s.copies[dest] = src.String()
1280 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001281}
1282
Paul Duffin91547182019-11-12 19:39:36 +00001283func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
1284 ctx := s.ctx
1285
1286 // Repackage the zip file so that the entries are in the destDir directory.
1287 // This will allow the zip file to be merged into the snapshot.
1288 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +00001289
1290 ctx.Build(pctx, android.BuildParams{
1291 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
1292 Rule: repackageZip,
1293 Input: zipPath,
1294 Output: tmpZipPath,
1295 Args: map[string]string{
1296 "destdir": destDir,
1297 },
1298 })
Paul Duffin91547182019-11-12 19:39:36 +00001299
1300 // Add the repackaged zip file to the files to merge.
1301 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
1302}
1303
Paul Duffin5c211452021-07-15 12:42:44 +01001304func (s *snapshotBuilder) EmptyFile() android.Path {
1305 if s.emptyFile == nil {
1306 ctx := s.ctx
1307 s.emptyFile = android.PathForModuleOut(ctx, "empty")
1308 s.ctx.Build(pctx, android.BuildParams{
1309 Rule: android.Touch,
1310 Output: s.emptyFile,
1311 })
1312 }
1313
1314 return s.emptyFile
1315}
1316
Paul Duffin9d8d6092019-12-05 18:19:29 +00001317func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
1318 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +00001319 if s.prebuiltModules[name] != nil {
1320 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
1321 }
1322
1323 m := s.bpFile.newModule(moduleType)
1324 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +00001325
Paul Duffinbefa4b92020-03-04 14:22:45 +00001326 variant := member.Variants()[0]
1327
Paul Duffin13f02712020-03-06 12:30:43 +00001328 if s.isInternalMember(name) {
Paul Duffin72910952020-01-20 18:16:30 +00001329 // An internal member is only referenced from the sdk snapshot which is in the
1330 // same package so can be marked as private.
1331 m.AddProperty("visibility", []string{"//visibility:private"})
1332 } else {
1333 // Extract visibility information from a member variant. All variants have the same
1334 // visibility so it doesn't matter which one is used.
Paul Duffin157f40f2020-09-29 16:01:08 +01001335 visibilityRules := android.EffectiveVisibilityRules(s.ctx, variant)
1336
1337 // Add any additional visibility rules needed for the prebuilts to reference each other.
1338 err := visibilityRules.Widen(s.sdk.properties.Prebuilt_visibility)
1339 if err != nil {
1340 s.ctx.PropertyErrorf("prebuilt_visibility", "%s", err)
1341 }
1342
1343 visibility := visibilityRules.Strings()
Paul Duffin72910952020-01-20 18:16:30 +00001344 if len(visibility) != 0 {
1345 m.AddProperty("visibility", visibility)
1346 }
Paul Duffin593b3c92019-12-05 14:31:48 +00001347 }
1348
Martin Stjernholm1e041092020-11-03 00:11:09 +00001349 // Where available copy apex_available properties from the member.
1350 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
1351 apexAvailable := apexAware.ApexAvailable()
1352 if len(apexAvailable) == 0 {
1353 // //apex_available:platform is the default.
1354 apexAvailable = []string{android.AvailableToPlatform}
1355 }
1356
1357 // Add in any baseline apex available settings.
1358 apexAvailable = append(apexAvailable, apex.BaselineApexAvailable(member.Name())...)
1359
1360 // Remove duplicates and sort.
1361 apexAvailable = android.FirstUniqueStrings(apexAvailable)
1362 sort.Strings(apexAvailable)
1363
1364 m.AddProperty("apex_available", apexAvailable)
1365 }
1366
Paul Duffinb0bb3762021-05-06 16:48:05 +01001367 // The licenses are the same for all variants.
1368 mctx := s.ctx
1369 licenseInfo := mctx.OtherModuleProvider(variant, android.LicenseInfoProvider).(android.LicenseInfo)
1370 if len(licenseInfo.Licenses) > 0 {
1371 m.AddPropertyWithTag("licenses", licenseInfo.Licenses, s.OptionalSdkMemberReferencePropertyTag())
1372 }
1373
Paul Duffin865171e2020-03-02 18:38:15 +00001374 deviceSupported := false
1375 hostSupported := false
1376
1377 for _, variant := range member.Variants() {
1378 osClass := variant.Target().Os.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001379 if osClass == android.Host {
Paul Duffin865171e2020-03-02 18:38:15 +00001380 hostSupported = true
1381 } else if osClass == android.Device {
1382 deviceSupported = true
1383 }
1384 }
1385
1386 addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
Paul Duffinb645ec82019-11-27 17:43:54 +00001387
Paul Duffin0cb37b92020-03-04 14:52:46 +00001388 // Disable installation in the versioned module of those modules that are ever installable.
1389 if installable, ok := variant.(interface{ EverInstallable() bool }); ok {
1390 if installable.EverInstallable() {
1391 m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag)
1392 }
1393 }
1394
Paul Duffinb645ec82019-11-27 17:43:54 +00001395 s.prebuiltModules[name] = m
1396 s.prebuiltOrder = append(s.prebuiltOrder, m)
1397 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001398}
1399
Paul Duffin865171e2020-03-02 18:38:15 +00001400func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
Paul Duffinb0bb3762021-05-06 16:48:05 +01001401 // If neither device or host is supported then this module does not support either so will not
1402 // recognize the properties.
1403 if !deviceSupported && !hostSupported {
1404 return
1405 }
1406
Paul Duffin865171e2020-03-02 18:38:15 +00001407 if !deviceSupported {
Paul Duffine44358f2019-11-26 18:04:12 +00001408 bpModule.AddProperty("device_supported", false)
1409 }
Paul Duffin865171e2020-03-02 18:38:15 +00001410 if hostSupported {
Paul Duffine44358f2019-11-26 18:04:12 +00001411 bpModule.AddProperty("host_supported", true)
1412 }
1413}
1414
Paul Duffin13f02712020-03-06 12:30:43 +00001415func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag {
1416 if required {
1417 return requiredSdkMemberReferencePropertyTag
1418 } else {
1419 return optionalSdkMemberReferencePropertyTag
1420 }
1421}
1422
1423func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag {
1424 return optionalSdkMemberReferencePropertyTag
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001425}
1426
Paul Duffinb645ec82019-11-27 17:43:54 +00001427// Get a versioned name appropriate for the SDK snapshot version being taken.
Paul Duffin13f02712020-03-06 12:30:43 +00001428func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string, required bool) string {
1429 if _, ok := s.allMembersByName[unversionedName]; !ok {
1430 if required {
1431 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
1432 }
1433 return unversionedName
1434 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001435 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
1436}
Paul Duffinb645ec82019-11-27 17:43:54 +00001437
Paul Duffin13f02712020-03-06 12:30:43 +00001438func (s *snapshotBuilder) versionedSdkMemberNames(members []string, required bool) []string {
Paul Duffinb645ec82019-11-27 17:43:54 +00001439 var references []string = nil
1440 for _, m := range members {
Paul Duffin13f02712020-03-06 12:30:43 +00001441 references = append(references, s.versionedSdkMemberName(m, required))
Paul Duffinb645ec82019-11-27 17:43:54 +00001442 }
1443 return references
1444}
Paul Duffin13879572019-11-28 14:31:38 +00001445
Paul Duffin72910952020-01-20 18:16:30 +00001446// Get an internal name unique to the sdk.
Paul Duffin13f02712020-03-06 12:30:43 +00001447func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string, required bool) string {
1448 if _, ok := s.allMembersByName[unversionedName]; !ok {
1449 if required {
1450 s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
1451 }
1452 return unversionedName
1453 }
1454
1455 if s.isInternalMember(unversionedName) {
Paul Duffin72910952020-01-20 18:16:30 +00001456 return s.ctx.ModuleName() + "_" + unversionedName
1457 } else {
1458 return unversionedName
1459 }
1460}
1461
Paul Duffin13f02712020-03-06 12:30:43 +00001462func (s *snapshotBuilder) unversionedSdkMemberNames(members []string, required bool) []string {
Paul Duffin72910952020-01-20 18:16:30 +00001463 var references []string = nil
1464 for _, m := range members {
Paul Duffin13f02712020-03-06 12:30:43 +00001465 references = append(references, s.unversionedSdkMemberName(m, required))
Paul Duffin72910952020-01-20 18:16:30 +00001466 }
1467 return references
1468}
1469
Paul Duffin13f02712020-03-06 12:30:43 +00001470func (s *snapshotBuilder) isInternalMember(memberName string) bool {
1471 _, ok := s.exportedMembersByName[memberName]
1472 return !ok
1473}
1474
Martin Stjernholm89238f42020-07-10 00:14:03 +01001475// Add the properties from the given SdkMemberProperties to the blueprint
1476// property set. This handles common properties in SdkMemberPropertiesBase and
1477// calls the member-specific AddToPropertySet for the rest.
1478func addSdkMemberPropertiesToSet(ctx *memberContext, memberProperties android.SdkMemberProperties, targetPropertySet android.BpPropertySet) {
1479 if memberProperties.Base().Compile_multilib != "" {
1480 targetPropertySet.AddProperty("compile_multilib", memberProperties.Base().Compile_multilib)
1481 }
1482
1483 memberProperties.AddToPropertySet(ctx, targetPropertySet)
1484}
1485
Paul Duffin21827262021-04-24 12:16:36 +01001486// sdkMemberVariantDep represents a dependency from an sdk variant onto a member variant.
1487type sdkMemberVariantDep struct {
Paul Duffincd064672021-04-24 00:47:29 +01001488 // The sdk variant that depends (possibly indirectly) on the member variant.
1489 sdkVariant *sdk
Paul Duffinb97b1572021-04-29 21:50:40 +01001490
1491 // The type of sdk member the variant is to be treated as.
Paul Duffin1356d8c2020-02-25 19:26:33 +00001492 memberType android.SdkMemberType
Paul Duffinb97b1572021-04-29 21:50:40 +01001493
1494 // The variant that is added to the sdk.
1495 variant android.SdkAware
1496
Paul Duffin1a44a992022-05-06 09:38:02 +00001497 // The optional container of this member, i.e. the module that is depended upon by the sdk
1498 // (possibly transitively) and whose dependency on this module is why it was added to the sdk.
1499 // Is nil if this a direct dependency of the sdk.
1500 container android.SdkAware
1501
Paul Duffinb97b1572021-04-29 21:50:40 +01001502 // True if the member should be exported, i.e. accessible, from outside the sdk.
1503 export bool
1504
1505 // The names of additional component modules provided by the variant.
1506 exportedComponentsInfo android.ExportedComponentsInfo
Paul Duffin1356d8c2020-02-25 19:26:33 +00001507}
1508
Paul Duffin13879572019-11-28 14:31:38 +00001509var _ android.SdkMember = (*sdkMember)(nil)
1510
Paul Duffin21827262021-04-24 12:16:36 +01001511// sdkMember groups all the variants of a specific member module together along with the name of the
1512// module and the member type. This is used to generate the prebuilt modules for a specific member.
Paul Duffin13879572019-11-28 14:31:38 +00001513type sdkMember struct {
1514 memberType android.SdkMemberType
1515 name string
1516 variants []android.SdkAware
1517}
1518
1519func (m *sdkMember) Name() string {
1520 return m.name
1521}
1522
1523func (m *sdkMember) Variants() []android.SdkAware {
1524 return m.variants
1525}
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001526
Paul Duffin9c3760e2020-03-16 19:52:08 +00001527// Track usages of multilib variants.
1528type multilibUsage int
1529
1530const (
1531 multilibNone multilibUsage = 0
1532 multilib32 multilibUsage = 1
1533 multilib64 multilibUsage = 2
1534 multilibBoth = multilib32 | multilib64
1535)
1536
1537// Add the multilib that is used in the arch type.
1538func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage {
1539 multilib := archType.Multilib
1540 switch multilib {
1541 case "":
1542 return m
1543 case "lib32":
1544 return m | multilib32
1545 case "lib64":
1546 return m | multilib64
1547 default:
1548 panic(fmt.Errorf("Unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib))
1549 }
1550}
1551
1552func (m multilibUsage) String() string {
1553 switch m {
1554 case multilibNone:
1555 return ""
1556 case multilib32:
1557 return "32"
1558 case multilib64:
1559 return "64"
1560 case multilibBoth:
1561 return "both"
1562 default:
1563 panic(fmt.Errorf("Unknown multilib value, found %b, expected one of %b, %b, %b or %b",
1564 m, multilibNone, multilib32, multilib64, multilibBoth))
1565 }
1566}
1567
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001568type baseInfo struct {
1569 Properties android.SdkMemberProperties
1570}
1571
Paul Duffinf34f6d82020-04-30 15:48:31 +01001572func (b *baseInfo) optimizableProperties() interface{} {
1573 return b.Properties
1574}
1575
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001576type osTypeSpecificInfo struct {
1577 baseInfo
1578
Paul Duffin00e46802020-03-12 20:40:35 +00001579 osType android.OsType
1580
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001581 // The list of arch type specific info for this os type.
Paul Duffinb44b33a2020-03-17 10:58:23 +00001582 //
1583 // Nil if there is one variant whose arch type is common
1584 archInfos []*archTypeSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001585}
1586
Paul Duffin4b8b7932020-05-06 12:35:38 +01001587var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
1588
Paul Duffinfc8dd232020-03-17 12:51:37 +00001589type variantPropertiesFactoryFunc func() android.SdkMemberProperties
1590
Paul Duffin00e46802020-03-12 20:40:35 +00001591// Create a new osTypeSpecificInfo for the specified os type and its properties
1592// structures populated with information from the variants.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001593func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
Paul Duffin00e46802020-03-12 20:40:35 +00001594 osInfo := &osTypeSpecificInfo{
1595 osType: osType,
1596 }
1597
1598 osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
1599 properties := variantPropertiesFactory()
1600 properties.Base().Os = osType
1601 return properties
1602 }
1603
1604 // Create a structure into which properties common across the architectures in
1605 // this os type will be stored.
1606 osInfo.Properties = osSpecificVariantPropertiesFactory()
1607
1608 // Group the variants by arch type.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001609 var variantsByArchId = make(map[archId][]android.Module)
1610 var archIds []archId
Paul Duffin00e46802020-03-12 20:40:35 +00001611 for _, variant := range osTypeVariants {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001612 target := variant.Target()
1613 id := archIdFromTarget(target)
1614 if _, ok := variantsByArchId[id]; !ok {
1615 archIds = append(archIds, id)
Paul Duffin00e46802020-03-12 20:40:35 +00001616 }
1617
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001618 variantsByArchId[id] = append(variantsByArchId[id], variant)
Paul Duffin00e46802020-03-12 20:40:35 +00001619 }
1620
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001621 if commonVariants, ok := variantsByArchId[commonArchId]; ok {
Paul Duffin00e46802020-03-12 20:40:35 +00001622 if len(osTypeVariants) != 1 {
Colin Crossafa6a772020-07-06 17:41:08 -07001623 panic(fmt.Errorf("Expected to only have 1 variant when arch type is common but found %d", len(osTypeVariants)))
Paul Duffin00e46802020-03-12 20:40:35 +00001624 }
1625
1626 // A common arch type only has one variant and its properties should be treated
1627 // as common to the os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001628 osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
Paul Duffin00e46802020-03-12 20:40:35 +00001629 } else {
1630 // Create an arch specific info for each supported architecture type.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001631 for _, id := range archIds {
1632 archVariants := variantsByArchId[id]
1633 archInfo := newArchSpecificInfo(ctx, id, osType, osSpecificVariantPropertiesFactory, archVariants)
Paul Duffin00e46802020-03-12 20:40:35 +00001634
1635 osInfo.archInfos = append(osInfo.archInfos, archInfo)
1636 }
1637 }
1638
1639 return osInfo
1640}
1641
Paul Duffin39abf8f2021-09-24 14:58:27 +01001642func (osInfo *osTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1643 if len(osInfo.archInfos) == 0 {
1644 pruner.pruneProperties(osInfo.Properties)
1645 } else {
1646 for _, archInfo := range osInfo.archInfos {
1647 archInfo.pruneUnsupportedProperties(pruner)
1648 }
1649 }
1650}
1651
Paul Duffin00e46802020-03-12 20:40:35 +00001652// Optimize the properties by extracting common properties from arch type specific
1653// properties into os type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001654func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffin00e46802020-03-12 20:40:35 +00001655 // Nothing to do if there is only a single common architecture.
1656 if len(osInfo.archInfos) == 0 {
1657 return
1658 }
1659
Paul Duffin9c3760e2020-03-16 19:52:08 +00001660 multilib := multilibNone
Paul Duffin00e46802020-03-12 20:40:35 +00001661 for _, archInfo := range osInfo.archInfos {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001662 multilib = multilib.addArchType(archInfo.archId.archType)
Paul Duffin9c3760e2020-03-16 19:52:08 +00001663
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001664 // Optimize the arch properties first.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001665 archInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin00e46802020-03-12 20:40:35 +00001666 }
1667
Paul Duffin4b8b7932020-05-06 12:35:38 +01001668 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
Paul Duffin00e46802020-03-12 20:40:35 +00001669
1670 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
Paul Duffin9c3760e2020-03-16 19:52:08 +00001671 osInfo.Properties.Base().Compile_multilib = multilib.String()
Paul Duffin00e46802020-03-12 20:40:35 +00001672}
1673
1674// Add the properties for an os to a property set.
1675//
1676// Maps the properties related to the os variants through to an appropriate
1677// module structure that will produce equivalent set of variants when it is
1678// processed in a build.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001679func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
Paul Duffin00e46802020-03-12 20:40:35 +00001680
1681 var osPropertySet android.BpPropertySet
1682 var archPropertySet android.BpPropertySet
1683 var archOsPrefix string
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01001684 if osInfo.Properties.Base().Os_count == 1 &&
1685 (osInfo.osType.Class == android.Device || !ctx.memberType.IsHostOsDependent()) {
1686 // There is only one OS type present in the variants and it shouldn't have a
1687 // variant-specific target. The latter is the case if it's either for device
1688 // where there is only one OS (android), or for host and the member type
1689 // isn't host OS dependent.
Paul Duffin00e46802020-03-12 20:40:35 +00001690
1691 // Create a structure that looks like:
1692 // module_type {
1693 // name: "...",
1694 // ...
1695 // <common properties>
1696 // ...
1697 // <single os type specific properties>
1698 //
1699 // arch: {
1700 // <arch specific sections>
1701 // }
1702 //
1703 osPropertySet = bpModule
1704 archPropertySet = osPropertySet.AddPropertySet("arch")
1705
1706 // Arch specific properties need to be added to an arch specific section
1707 // within arch.
1708 archOsPrefix = ""
1709 } else {
1710 // Create a structure that looks like:
1711 // module_type {
1712 // name: "...",
1713 // ...
1714 // <common properties>
1715 // ...
1716 // target: {
1717 // <arch independent os specific sections, e.g. android>
1718 // ...
1719 // <arch and os specific sections, e.g. android_x86>
1720 // }
1721 //
1722 osType := osInfo.osType
1723 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
1724 archPropertySet = targetPropertySet
1725
1726 // Arch specific properties need to be added to an os and arch specific
1727 // section prefixed with <os>_.
1728 archOsPrefix = osType.Name + "_"
1729 }
1730
1731 // Add the os specific but arch independent properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01001732 addSdkMemberPropertiesToSet(ctx, osInfo.Properties, osPropertySet)
Paul Duffin00e46802020-03-12 20:40:35 +00001733
1734 // Add arch (and possibly os) specific sections for each set of arch (and possibly
1735 // os) specific properties.
1736 //
1737 // The archInfos list will be empty if the os contains variants for the common
1738 // architecture.
1739 for _, archInfo := range osInfo.archInfos {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001740 archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
Paul Duffin00e46802020-03-12 20:40:35 +00001741 }
1742}
1743
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001744func (osInfo *osTypeSpecificInfo) isHostVariant() bool {
1745 osClass := osInfo.osType.Class
Jiyong Park1613e552020-09-14 19:43:17 +09001746 return osClass == android.Host
Paul Duffin7a1f7f32020-05-04 15:32:08 +01001747}
1748
1749var _ isHostVariant = (*osTypeSpecificInfo)(nil)
1750
Paul Duffin4b8b7932020-05-06 12:35:38 +01001751func (osInfo *osTypeSpecificInfo) String() string {
1752 return fmt.Sprintf("OsType{%s}", osInfo.osType)
1753}
1754
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001755// archId encapsulates the information needed to identify a combination of arch type and native
1756// bridge support.
1757//
1758// Conceptually, native bridge support is a facet of an android.Target, not an android.Arch as it is
1759// essentially using one android.Arch to implement another. However, in terms of the handling of
1760// the variants native bridge is treated as part of the arch variation. See the ArchVariation method
1761// on android.Target.
1762//
1763// So, it makes sense when optimizing the variants to combine native bridge with the arch type.
1764type archId struct {
1765 // The arch type of the variant's target.
1766 archType android.ArchType
1767
1768 // True if the variants is for the native bridge, false otherwise.
1769 nativeBridge bool
1770}
1771
1772// propertyName returns the name of the property corresponding to use for this arch id.
1773func (i *archId) propertyName() string {
1774 name := i.archType.Name
1775 if i.nativeBridge {
1776 // Note: This does not result in a valid property because there is no architecture specific
1777 // native bridge property, only a generic "native_bridge" property. However, this will be used
1778 // in error messages if there is an attempt to use this in a generated bp file.
1779 name += "_native_bridge"
1780 }
1781 return name
1782}
1783
1784func (i *archId) String() string {
1785 return fmt.Sprintf("ArchType{%s}, NativeBridge{%t}", i.archType, i.nativeBridge)
1786}
1787
1788// archIdFromTarget returns an archId initialized from information in the supplied target.
1789func archIdFromTarget(target android.Target) archId {
1790 return archId{
1791 archType: target.Arch.ArchType,
1792 nativeBridge: target.NativeBridge == android.NativeBridgeEnabled,
1793 }
1794}
1795
1796// commonArchId is the archId for the common architecture.
1797var commonArchId = archId{archType: android.Common}
1798
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001799type archTypeSpecificInfo struct {
1800 baseInfo
1801
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001802 archId archId
1803 osType android.OsType
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001804
Paul Duffinb42fa672021-09-09 16:37:49 +01001805 imageVariantInfos []*imageVariantSpecificInfo
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001806}
1807
Paul Duffin4b8b7932020-05-06 12:35:38 +01001808var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
1809
Paul Duffinfc8dd232020-03-17 12:51:37 +00001810// Create a new archTypeSpecificInfo for the specified arch type and its properties
1811// structures populated with information from the variants.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001812func newArchSpecificInfo(ctx android.SdkMemberContext, archId archId, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
Paul Duffinfc8dd232020-03-17 12:51:37 +00001813
Paul Duffinfc8dd232020-03-17 12:51:37 +00001814 // Create an arch specific info into which the variant properties can be copied.
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001815 archInfo := &archTypeSpecificInfo{archId: archId, osType: osType}
Paul Duffinfc8dd232020-03-17 12:51:37 +00001816
1817 // Create the properties into which the arch type specific properties will be
1818 // added.
1819 archInfo.Properties = variantPropertiesFactory()
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001820
Liz Kammer7b710832022-05-12 20:40:00 -04001821 // if there are multiple supported link variants, we want to nest based on linkage even if there
1822 // is only one variant, otherwise, if there is only one variant we can populate based on the arch
1823 if len(archVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
Paul Duffin3a4eb502020-03-19 16:11:18 +00001824 archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001825 } else {
Paul Duffinb42fa672021-09-09 16:37:49 +01001826 // Group the variants by image type.
1827 variantsByImage := make(map[string][]android.Module)
1828 for _, variant := range archVariants {
1829 image := variant.ImageVariation().Variation
1830 variantsByImage[image] = append(variantsByImage[image], variant)
1831 }
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001832
Paul Duffinb42fa672021-09-09 16:37:49 +01001833 // Create the image variant info in a fixed order.
1834 for _, imageVariantName := range android.SortedStringKeys(variantsByImage) {
1835 variants := variantsByImage[imageVariantName]
1836 archInfo.imageVariantInfos = append(archInfo.imageVariantInfos, newImageVariantSpecificInfo(ctx, imageVariantName, variantPropertiesFactory, variants))
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001837 }
1838 }
Paul Duffinfc8dd232020-03-17 12:51:37 +00001839
1840 return archInfo
1841}
1842
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001843// Get the link type of the variant
1844//
1845// If the variant is not differentiated by link type then it returns "",
1846// otherwise it returns one of "static" or "shared".
1847func getLinkType(variant android.Module) string {
1848 linkType := ""
1849 if linkable, ok := variant.(cc.LinkableInterface); ok {
1850 if linkable.Shared() && linkable.Static() {
1851 panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String()))
1852 } else if linkable.Shared() {
1853 linkType = "shared"
1854 } else if linkable.Static() {
1855 linkType = "static"
1856 } else {
1857 panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String()))
1858 }
1859 }
1860 return linkType
1861}
1862
Paul Duffin39abf8f2021-09-24 14:58:27 +01001863func (archInfo *archTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1864 if len(archInfo.imageVariantInfos) == 0 {
1865 pruner.pruneProperties(archInfo.Properties)
1866 } else {
1867 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1868 imageVariantInfo.pruneUnsupportedProperties(pruner)
1869 }
1870 }
1871}
1872
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001873// Optimize the properties by extracting common properties from link type specific
1874// properties into arch type specific properties.
Paul Duffin4b8b7932020-05-06 12:35:38 +01001875func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
Paul Duffinb42fa672021-09-09 16:37:49 +01001876 if len(archInfo.imageVariantInfos) == 0 {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001877 return
1878 }
1879
Paul Duffinb42fa672021-09-09 16:37:49 +01001880 // Optimize the image variant properties first.
1881 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1882 imageVariantInfo.optimizeProperties(ctx, commonValueExtractor)
1883 }
1884
1885 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.imageVariantInfos)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001886}
1887
Paul Duffinfc8dd232020-03-17 12:51:37 +00001888// Add the properties for an arch type to a property set.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001889func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001890 archPropertySuffix := archInfo.archId.propertyName()
1891 propertySetName := archOsPrefix + archPropertySuffix
1892 archTypePropertySet := archPropertySet.AddPropertySet(propertySetName)
Jiyong Park8fe14e62020-10-19 22:47:34 +09001893 // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
1894 if ctx.memberType.IsHostOsDependent() && archInfo.osType.Class == android.Host {
1895 archTypePropertySet.AddProperty("enabled", true)
1896 }
Martin Stjernholm89238f42020-07-10 00:14:03 +01001897 addSdkMemberPropertiesToSet(ctx, archInfo.Properties, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001898
Paul Duffinb42fa672021-09-09 16:37:49 +01001899 for _, imageVariantInfo := range archInfo.imageVariantInfos {
1900 imageVariantInfo.addToPropertySet(ctx, archTypePropertySet)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001901 }
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001902
1903 // If this is for a native bridge architecture then make sure that the property set does not
1904 // contain any properties as providing native bridge specific properties is not currently
1905 // supported.
1906 if archInfo.archId.nativeBridge {
1907 propertySetContents := getPropertySetContents(archTypePropertySet)
1908 if propertySetContents != "" {
1909 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",
1910 propertySetName, ctx.name, propertySetContents)
1911 }
1912 }
1913}
1914
1915// getPropertySetContents returns the string representation of the contents of a property set, after
1916// recursively pruning any empty nested property sets.
1917func getPropertySetContents(propertySet android.BpPropertySet) string {
1918 set := propertySet.(*bpPropertySet)
1919 set.transformContents(pruneEmptySetTransformer{})
1920 if len(set.properties) != 0 {
1921 contents := &generatedContents{}
1922 contents.Indent()
1923 outputPropertySet(contents, set)
1924 setAsString := contents.content.String()
1925 return setAsString
1926 }
1927 return ""
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001928}
1929
Paul Duffin4b8b7932020-05-06 12:35:38 +01001930func (archInfo *archTypeSpecificInfo) String() string {
Paul Duffinfefdb0b2021-09-09 18:50:49 +01001931 return archInfo.archId.String()
Paul Duffin4b8b7932020-05-06 12:35:38 +01001932}
1933
Paul Duffinb42fa672021-09-09 16:37:49 +01001934type imageVariantSpecificInfo struct {
1935 baseInfo
1936
1937 imageVariant string
1938
1939 linkInfos []*linkTypeSpecificInfo
1940}
1941
1942func newImageVariantSpecificInfo(ctx android.SdkMemberContext, imageVariant string, variantPropertiesFactory variantPropertiesFactoryFunc, imageVariants []android.Module) *imageVariantSpecificInfo {
1943
1944 // Create an image variant specific info into which the variant properties can be copied.
1945 imageInfo := &imageVariantSpecificInfo{imageVariant: imageVariant}
1946
1947 // Create the properties into which the image variant specific properties will be added.
1948 imageInfo.Properties = variantPropertiesFactory()
1949
Liz Kammer7b710832022-05-12 20:40:00 -04001950 // if there are multiple supported link variants, we want to nest even if there is only one
1951 // variant, otherwise, if there is only one variant we can populate based on the image
1952 if len(imageVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
Paul Duffinb42fa672021-09-09 16:37:49 +01001953 imageInfo.Properties.PopulateFromVariant(ctx, imageVariants[0])
1954 } else {
1955 // There is more than one variant for this image variant which must be differentiated by link
Liz Kammer7b710832022-05-12 20:40:00 -04001956 // type. Or there are multiple supported linkages and we need to nest based on link type.
Paul Duffinb42fa672021-09-09 16:37:49 +01001957 for _, linkVariant := range imageVariants {
1958 linkType := getLinkType(linkVariant)
1959 if linkType == "" {
1960 panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(imageVariants)))
1961 } else {
1962 linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
1963
1964 imageInfo.linkInfos = append(imageInfo.linkInfos, linkInfo)
1965 }
1966 }
1967 }
1968
1969 return imageInfo
1970}
1971
Paul Duffin39abf8f2021-09-24 14:58:27 +01001972func (imageInfo *imageVariantSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
1973 if len(imageInfo.linkInfos) == 0 {
1974 pruner.pruneProperties(imageInfo.Properties)
1975 } else {
1976 for _, linkInfo := range imageInfo.linkInfos {
1977 linkInfo.pruneUnsupportedProperties(pruner)
1978 }
1979 }
1980}
1981
Paul Duffinb42fa672021-09-09 16:37:49 +01001982// Optimize the properties by extracting common properties from link type specific
1983// properties into arch type specific properties.
1984func (imageInfo *imageVariantSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
1985 if len(imageInfo.linkInfos) == 0 {
1986 return
1987 }
1988
1989 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, imageInfo.Properties, imageInfo.linkInfos)
1990}
1991
1992// Add the properties for an arch type to a property set.
1993func (imageInfo *imageVariantSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
1994 if imageInfo.imageVariant != android.CoreVariation {
1995 propertySet = propertySet.AddPropertySet(imageInfo.imageVariant)
1996 }
1997
1998 addSdkMemberPropertiesToSet(ctx, imageInfo.Properties, propertySet)
1999
Liz Kammer7b710832022-05-12 20:40:00 -04002000 usedLinkages := make(map[string]bool, len(imageInfo.linkInfos))
Paul Duffinb42fa672021-09-09 16:37:49 +01002001 for _, linkInfo := range imageInfo.linkInfos {
Liz Kammer7b710832022-05-12 20:40:00 -04002002 usedLinkages[linkInfo.linkType] = true
Paul Duffinb42fa672021-09-09 16:37:49 +01002003 linkInfo.addToPropertySet(ctx, propertySet)
2004 }
2005
Liz Kammer7b710832022-05-12 20:40:00 -04002006 // If not all supported linkages had existing variants, we need to disable the unsupported variant
2007 if len(imageInfo.linkInfos) < len(ctx.MemberType().SupportedLinkages()) {
2008 for _, l := range ctx.MemberType().SupportedLinkages() {
2009 if _, ok := usedLinkages[l]; !ok {
2010 otherLinkagePropertySet := propertySet.AddPropertySet(l)
2011 otherLinkagePropertySet.AddProperty("enabled", false)
2012 }
2013 }
2014 }
2015
Paul Duffinb42fa672021-09-09 16:37:49 +01002016 // If this is for a non-core image variant then make sure that the property set does not contain
2017 // any properties as providing non-core image variant specific properties for prebuilts is not
2018 // currently supported.
2019 if imageInfo.imageVariant != android.CoreVariation {
2020 propertySetContents := getPropertySetContents(propertySet)
2021 if propertySetContents != "" {
2022 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",
2023 imageInfo.imageVariant, ctx.name, propertySetContents)
2024 }
2025 }
2026}
2027
2028func (imageInfo *imageVariantSpecificInfo) String() string {
2029 return imageInfo.imageVariant
2030}
2031
Paul Duffin9b76c0b2020-03-12 10:24:35 +00002032type linkTypeSpecificInfo struct {
2033 baseInfo
2034
2035 linkType string
2036}
2037
Paul Duffin4b8b7932020-05-06 12:35:38 +01002038var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
2039
Paul Duffin9b76c0b2020-03-12 10:24:35 +00002040// Create a new linkTypeSpecificInfo for the specified link type and its properties
2041// structures populated with information from the variant.
Paul Duffin3a4eb502020-03-19 16:11:18 +00002042func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
Paul Duffin9b76c0b2020-03-12 10:24:35 +00002043 linkInfo := &linkTypeSpecificInfo{
2044 baseInfo: baseInfo{
2045 // Create the properties into which the link type specific properties will be
2046 // added.
2047 Properties: variantPropertiesFactory(),
2048 },
2049 linkType: linkType,
2050 }
Paul Duffin3a4eb502020-03-19 16:11:18 +00002051 linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
Paul Duffin9b76c0b2020-03-12 10:24:35 +00002052 return linkInfo
Paul Duffinfc8dd232020-03-17 12:51:37 +00002053}
2054
Paul Duffinf68f85a2021-09-09 16:11:42 +01002055func (l *linkTypeSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
2056 linkPropertySet := propertySet.AddPropertySet(l.linkType)
2057 addSdkMemberPropertiesToSet(ctx, l.Properties, linkPropertySet)
2058}
2059
Paul Duffin39abf8f2021-09-24 14:58:27 +01002060func (l *linkTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
2061 pruner.pruneProperties(l.Properties)
2062}
2063
Paul Duffin4b8b7932020-05-06 12:35:38 +01002064func (l *linkTypeSpecificInfo) String() string {
2065 return fmt.Sprintf("LinkType{%s}", l.linkType)
2066}
2067
Paul Duffin3a4eb502020-03-19 16:11:18 +00002068type memberContext struct {
2069 sdkMemberContext android.ModuleContext
2070 builder *snapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +00002071 memberType android.SdkMemberType
2072 name string
Paul Duffind19f8942021-07-14 12:08:37 +01002073
2074 // The set of traits required of this member.
2075 requiredTraits android.SdkMemberTraitSet
Paul Duffin3a4eb502020-03-19 16:11:18 +00002076}
2077
2078func (m *memberContext) SdkModuleContext() android.ModuleContext {
2079 return m.sdkMemberContext
2080}
2081
2082func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
2083 return m.builder
2084}
2085
Paul Duffina551a1c2020-03-17 21:04:24 +00002086func (m *memberContext) MemberType() android.SdkMemberType {
2087 return m.memberType
2088}
2089
2090func (m *memberContext) Name() string {
2091 return m.name
2092}
2093
Paul Duffind19f8942021-07-14 12:08:37 +01002094func (m *memberContext) RequiresTrait(trait android.SdkMemberTrait) bool {
2095 return m.requiredTraits.Contains(trait)
2096}
2097
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01002098func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002099
2100 memberType := member.memberType
2101
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002102 // Do not add the prefer property if the member snapshot module is a source module type.
Paul Duffin39abf8f2021-09-24 14:58:27 +01002103 config := ctx.sdkMemberContext.Config()
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002104 if !memberType.UsesSourceModuleTypeInSnapshot() {
Mathew Inwood7e9ddbe2021-07-07 12:47:51 +00002105 // Set the prefer based on the environment variable. This is a temporary work around to allow a
2106 // snapshot to be created that sets prefer: true.
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002107 // TODO(b/174997203): Remove once the ability to select the modules to prefer can be done
2108 // dynamically at build time not at snapshot generation time.
Paul Duffinfb9a7f92021-07-06 17:18:42 +01002109 prefer := config.IsEnvTrue("SOONG_SDK_SNAPSHOT_PREFER")
Paul Duffin83ad9562021-05-10 23:49:04 +01002110
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002111 // Set prefer. Setting this to false is not strictly required as that is the default but it does
2112 // provide a convenient hook to post-process the generated Android.bp file, e.g. in tests to
2113 // check the behavior when a prebuilt is preferred. It also makes it explicit what the default
2114 // behavior is for the module.
2115 bpModule.insertAfter("name", "prefer", prefer)
Paul Duffinfb9a7f92021-07-06 17:18:42 +01002116
2117 configVar := config.Getenv("SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR")
2118 if configVar != "" {
2119 parts := strings.Split(configVar, ":")
2120 cfp := android.ConfigVarProperties{
2121 Config_namespace: proptools.StringPtr(parts[0]),
2122 Var_name: proptools.StringPtr(parts[1]),
2123 }
2124 bpModule.insertAfter("prefer", "use_source_config_var", cfp)
2125 }
Paul Duffin0d4ed0a2021-05-10 23:58:40 +01002126 }
Paul Duffin83ad9562021-05-10 23:49:04 +01002127
Paul Duffina04c1072020-03-02 10:16:35 +00002128 // Group the variants by os type.
Paul Duffin3a4eb502020-03-19 16:11:18 +00002129 variantsByOsType := make(map[android.OsType][]android.Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002130 variants := member.Variants()
2131 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +00002132 osType := variant.Target().Os
2133 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002134 }
2135
Paul Duffina04c1072020-03-02 10:16:35 +00002136 osCount := len(variantsByOsType)
Paul Duffinb44b33a2020-03-17 10:58:23 +00002137 variantPropertiesFactory := func() android.SdkMemberProperties {
Paul Duffina04c1072020-03-02 10:16:35 +00002138 properties := memberType.CreateVariantPropertiesStruct()
2139 base := properties.Base()
2140 base.Os_count = osCount
Paul Duffina04c1072020-03-02 10:16:35 +00002141 return properties
2142 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002143
Paul Duffina04c1072020-03-02 10:16:35 +00002144 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +00002145
Paul Duffina04c1072020-03-02 10:16:35 +00002146 // The set of properties that are common across all architectures and os types.
Paul Duffinb44b33a2020-03-17 10:58:23 +00002147 commonProperties := variantPropertiesFactory()
2148 commonProperties.Base().Os = android.CommonOS
Paul Duffina04c1072020-03-02 10:16:35 +00002149
Paul Duffin39abf8f2021-09-24 14:58:27 +01002150 // Create a property pruner that will prune any properties unsupported by the target build
2151 // release.
2152 targetBuildRelease := ctx.builder.targetBuildRelease
2153 unsupportedPropertyPruner := newPropertyPrunerByBuildRelease(commonProperties, targetBuildRelease)
2154
Paul Duffinc097e362020-03-10 22:50:03 +00002155 // Create common value extractor that can be used to optimize the properties.
2156 commonValueExtractor := newCommonValueExtractor(commonProperties)
2157
Paul Duffina04c1072020-03-02 10:16:35 +00002158 // The list of property structures which are os type specific but common across
2159 // architectures within that os type.
Paul Duffinf34f6d82020-04-30 15:48:31 +01002160 var osSpecificPropertiesContainers []*osTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +00002161
2162 for osType, osTypeVariants := range variantsByOsType {
Paul Duffin3a4eb502020-03-19 16:11:18 +00002163 osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
Paul Duffina04c1072020-03-02 10:16:35 +00002164 osTypeToInfo[osType] = osInfo
Paul Duffinb44b33a2020-03-17 10:58:23 +00002165 // Add the os specific properties to a list of os type specific yet architecture
2166 // independent properties structs.
Paul Duffinf34f6d82020-04-30 15:48:31 +01002167 osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
Paul Duffina04c1072020-03-02 10:16:35 +00002168
Paul Duffin39abf8f2021-09-24 14:58:27 +01002169 osInfo.pruneUnsupportedProperties(unsupportedPropertyPruner)
2170
Paul Duffin00e46802020-03-12 20:40:35 +00002171 // Optimize the properties across all the variants for a specific os type.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002172 osInfo.optimizeProperties(ctx, commonValueExtractor)
Paul Duffin14eb4672020-03-02 11:33:02 +00002173 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002174
Paul Duffina04c1072020-03-02 10:16:35 +00002175 // Extract properties which are common across all architectures and os types.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002176 extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002177
Paul Duffina04c1072020-03-02 10:16:35 +00002178 // Add the common properties to the module.
Martin Stjernholm89238f42020-07-10 00:14:03 +01002179 addSdkMemberPropertiesToSet(ctx, commonProperties, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002180
Paul Duffina04c1072020-03-02 10:16:35 +00002181 // Create a target property set into which target specific properties can be
2182 // added.
2183 targetPropertySet := bpModule.AddPropertySet("target")
2184
Martin Stjernholmcaa47d72020-07-11 04:52:24 +01002185 // If the member is host OS dependent and has host_supported then disable by
2186 // default and enable each host OS variant explicitly. This avoids problems
2187 // with implicitly enabled OS variants when the snapshot is used, which might
2188 // be different from this run (e.g. different build OS).
2189 if ctx.memberType.IsHostOsDependent() {
2190 hostSupported := bpModule.getValue("host_supported") == true // Missing means false.
2191 if hostSupported {
2192 hostPropertySet := targetPropertySet.AddPropertySet("host")
2193 hostPropertySet.AddProperty("enabled", false)
2194 }
2195 }
2196
Paul Duffina04c1072020-03-02 10:16:35 +00002197 // Iterate over the os types in a fixed order.
2198 for _, osType := range s.getPossibleOsTypes() {
2199 osInfo := osTypeToInfo[osType]
2200 if osInfo == nil {
2201 continue
2202 }
2203
Paul Duffin3a4eb502020-03-19 16:11:18 +00002204 osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002205 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002206}
2207
Paul Duffina04c1072020-03-02 10:16:35 +00002208// Compute the list of possible os types that this sdk could support.
2209func (s *sdk) getPossibleOsTypes() []android.OsType {
2210 var osTypes []android.OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +00002211 for _, osType := range android.OsTypeList() {
Paul Duffina04c1072020-03-02 10:16:35 +00002212 if s.DeviceSupported() {
Colin Crosscb0ac952021-07-20 13:17:15 -07002213 if osType.Class == android.Device {
Paul Duffina04c1072020-03-02 10:16:35 +00002214 osTypes = append(osTypes, osType)
2215 }
2216 }
2217 if s.HostSupported() {
Jiyong Park1613e552020-09-14 19:43:17 +09002218 if osType.Class == android.Host {
Paul Duffina04c1072020-03-02 10:16:35 +00002219 osTypes = append(osTypes, osType)
2220 }
2221 }
2222 }
2223 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
2224 return osTypes
2225}
2226
Paul Duffinb28369a2020-05-04 15:39:59 +01002227// Given a set of properties (struct value), return the value of the field within that
2228// struct (or one of its embedded structs).
Paul Duffinc097e362020-03-10 22:50:03 +00002229type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
2230
Paul Duffinc459f892020-04-30 18:08:29 +01002231// Checks the metadata to determine whether the property should be ignored for the
2232// purposes of common value extraction or not.
2233type extractorMetadataPredicate func(metadata propertiesContainer) bool
2234
2235// Indicates whether optimizable properties are provided by a host variant or
2236// not.
2237type isHostVariant interface {
2238 isHostVariant() bool
2239}
2240
Paul Duffinb28369a2020-05-04 15:39:59 +01002241// A property that can be optimized by the commonValueExtractor.
2242type extractorProperty struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +01002243 // The name of the field for this property. It is a "."-separated path for
2244 // fields in non-anonymous substructs.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002245 name string
2246
Paul Duffinc459f892020-04-30 18:08:29 +01002247 // Filter that can use metadata associated with the properties being optimized
2248 // to determine whether the field should be ignored during common value
2249 // optimization.
2250 filter extractorMetadataPredicate
2251
Paul Duffinb28369a2020-05-04 15:39:59 +01002252 // Retrieves the value on which common value optimization will be performed.
2253 getter fieldAccessorFunc
2254
2255 // The empty value for the field.
2256 emptyValue reflect.Value
Paul Duffin864e1b42020-05-06 10:23:19 +01002257
2258 // True if the property can support arch variants false otherwise.
2259 archVariant bool
Paul Duffinb28369a2020-05-04 15:39:59 +01002260}
2261
Paul Duffin4b8b7932020-05-06 12:35:38 +01002262func (p extractorProperty) String() string {
2263 return p.name
2264}
2265
Paul Duffinc097e362020-03-10 22:50:03 +00002266// Supports extracting common values from a number of instances of a properties
2267// structure into a separate common set of properties.
2268type commonValueExtractor struct {
Paul Duffinb28369a2020-05-04 15:39:59 +01002269 // The properties that the extractor can optimize.
2270 properties []extractorProperty
Paul Duffinc097e362020-03-10 22:50:03 +00002271}
2272
2273// Create a new common value extractor for the structure type for the supplied
2274// properties struct.
2275//
2276// The returned extractor can be used on any properties structure of the same type
2277// as the supplied set of properties.
2278func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor {
2279 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
2280 extractor := &commonValueExtractor{}
Martin Stjernholmb0249572020-09-15 02:32:35 +01002281 extractor.gatherFields(structType, nil, "")
Paul Duffinc097e362020-03-10 22:50:03 +00002282 return extractor
2283}
2284
2285// Gather the fields from the supplied structure type from which common values will
2286// be extracted.
Paul Duffinb07fa512020-03-10 22:17:04 +00002287//
Martin Stjernholmb0249572020-09-15 02:32:35 +01002288// This is recursive function. If it encounters a struct then it will recurse
2289// into it, passing in the accessor for the field and the struct name as prefix
2290// for the nested fields. That will then be used in the accessors for the fields
2291// in the embedded struct.
2292func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string) {
Paul Duffinc097e362020-03-10 22:50:03 +00002293 for f := 0; f < structType.NumField(); f++ {
2294 field := structType.Field(f)
2295 if field.PkgPath != "" {
2296 // Ignore unexported fields.
2297 continue
2298 }
2299
Paul Duffinb07fa512020-03-10 22:17:04 +00002300 // Ignore fields whose value should be kept.
2301 if proptools.HasTag(field, "sdk", "keep") {
Paul Duffinc097e362020-03-10 22:50:03 +00002302 continue
2303 }
2304
Paul Duffinc459f892020-04-30 18:08:29 +01002305 var filter extractorMetadataPredicate
2306
2307 // Add a filter
2308 if proptools.HasTag(field, "sdk", "ignored-on-host") {
2309 filter = func(metadata propertiesContainer) bool {
2310 if m, ok := metadata.(isHostVariant); ok {
2311 if m.isHostVariant() {
2312 return false
2313 }
2314 }
2315 return true
2316 }
2317 }
2318
Paul Duffinc097e362020-03-10 22:50:03 +00002319 // Save a copy of the field index for use in the function.
2320 fieldIndex := f
Paul Duffin4b8b7932020-05-06 12:35:38 +01002321
Martin Stjernholmb0249572020-09-15 02:32:35 +01002322 name := namePrefix + field.Name
Paul Duffin4b8b7932020-05-06 12:35:38 +01002323
Paul Duffinc097e362020-03-10 22:50:03 +00002324 fieldGetter := func(value reflect.Value) reflect.Value {
Paul Duffinb07fa512020-03-10 22:17:04 +00002325 if containingStructAccessor != nil {
2326 // This is an embedded structure so first access the field for the embedded
2327 // structure.
2328 value = containingStructAccessor(value)
2329 }
2330
Paul Duffinc097e362020-03-10 22:50:03 +00002331 // Skip through interface and pointer values to find the structure.
2332 value = getStructValue(value)
2333
Paul Duffin4b8b7932020-05-06 12:35:38 +01002334 defer func() {
2335 if r := recover(); r != nil {
2336 panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
2337 }
2338 }()
2339
Paul Duffinc097e362020-03-10 22:50:03 +00002340 // Return the field.
2341 return value.Field(fieldIndex)
2342 }
2343
Martin Stjernholmb0249572020-09-15 02:32:35 +01002344 if field.Type.Kind() == reflect.Struct {
2345 // Gather fields from the nested or embedded structure.
2346 var subNamePrefix string
2347 if field.Anonymous {
2348 subNamePrefix = namePrefix
2349 } else {
2350 subNamePrefix = name + "."
2351 }
2352 e.gatherFields(field.Type, fieldGetter, subNamePrefix)
Paul Duffinb07fa512020-03-10 22:17:04 +00002353 } else {
Paul Duffinb28369a2020-05-04 15:39:59 +01002354 property := extractorProperty{
Paul Duffin4b8b7932020-05-06 12:35:38 +01002355 name,
Paul Duffinc459f892020-04-30 18:08:29 +01002356 filter,
Paul Duffinb28369a2020-05-04 15:39:59 +01002357 fieldGetter,
2358 reflect.Zero(field.Type),
Paul Duffin864e1b42020-05-06 10:23:19 +01002359 proptools.HasTag(field, "android", "arch_variant"),
Paul Duffinb28369a2020-05-04 15:39:59 +01002360 }
2361 e.properties = append(e.properties, property)
Paul Duffinb07fa512020-03-10 22:17:04 +00002362 }
Paul Duffinc097e362020-03-10 22:50:03 +00002363 }
2364}
2365
2366func getStructValue(value reflect.Value) reflect.Value {
2367foundStruct:
2368 for {
2369 kind := value.Kind()
2370 switch kind {
2371 case reflect.Interface, reflect.Ptr:
2372 value = value.Elem()
2373 case reflect.Struct:
2374 break foundStruct
2375 default:
2376 panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind))
2377 }
2378 }
2379 return value
2380}
2381
Paul Duffinf34f6d82020-04-30 15:48:31 +01002382// A container of properties to be optimized.
2383//
2384// Allows additional information to be associated with the properties, e.g. for
2385// filtering.
2386type propertiesContainer interface {
Paul Duffin4b8b7932020-05-06 12:35:38 +01002387 fmt.Stringer
2388
Paul Duffinf34f6d82020-04-30 15:48:31 +01002389 // Get the properties that need optimizing.
2390 optimizableProperties() interface{}
2391}
2392
Paul Duffin2d1bb892021-04-24 11:32:59 +01002393// A wrapper for sdk variant related properties to allow them to be optimized.
2394type sdkVariantPropertiesContainer struct {
2395 sdkVariant *sdk
2396 properties interface{}
Paul Duffinf34f6d82020-04-30 15:48:31 +01002397}
2398
Paul Duffin2d1bb892021-04-24 11:32:59 +01002399func (c sdkVariantPropertiesContainer) optimizableProperties() interface{} {
2400 return c.properties
Paul Duffinf34f6d82020-04-30 15:48:31 +01002401}
2402
Paul Duffin2d1bb892021-04-24 11:32:59 +01002403func (c sdkVariantPropertiesContainer) String() string {
Paul Duffin4b8b7932020-05-06 12:35:38 +01002404 return c.sdkVariant.String()
2405}
2406
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002407// Extract common properties from a slice of property structures of the same type.
2408//
2409// All the property structures must be of the same type.
2410// commonProperties - must be a pointer to the structure into which common properties will be added.
Paul Duffinf34f6d82020-04-30 15:48:31 +01002411// inputPropertiesSlice - must be a slice of propertiesContainer interfaces.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002412//
2413// Iterates over each exported field (capitalized name) and checks to see whether they
2414// have the same value (using DeepEquals) across all the input properties. If it does not then no
2415// change is made. Otherwise, the common value is stored in the field in the commonProperties
Martin Stjernholmb0249572020-09-15 02:32:35 +01002416// and the field in each of the input properties structure is set to its default value. Nested
2417// structs are visited recursively and their non-struct fields are compared.
Paul Duffin4b8b7932020-05-06 12:35:38 +01002418func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002419 commonPropertiesValue := reflect.ValueOf(commonProperties)
2420 commonStructValue := commonPropertiesValue.Elem()
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002421
Paul Duffinf34f6d82020-04-30 15:48:31 +01002422 sliceValue := reflect.ValueOf(inputPropertiesSlice)
2423
Paul Duffinb28369a2020-05-04 15:39:59 +01002424 for _, property := range e.properties {
2425 fieldGetter := property.getter
Paul Duffinc459f892020-04-30 18:08:29 +01002426 filter := property.filter
2427 if filter == nil {
2428 filter = func(metadata propertiesContainer) bool {
2429 return true
2430 }
2431 }
Paul Duffinb28369a2020-05-04 15:39:59 +01002432
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002433 // Check to see if all the structures have the same value for the field. The commonValue
Paul Duffin864e1b42020-05-06 10:23:19 +01002434 // is nil on entry to the loop and if it is nil on exit then there is no common value or
2435 // all the values have been filtered out, otherwise it points to the common value.
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002436 var commonValue *reflect.Value
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002437
Paul Duffin864e1b42020-05-06 10:23:19 +01002438 // Assume that all the values will be the same.
2439 //
2440 // While similar to this is not quite the same as commonValue == nil. If all the values
2441 // have been filtered out then this will be false but commonValue == nil will be true.
2442 valuesDiffer := false
2443
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002444 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01002445 container := sliceValue.Index(i).Interface().(propertiesContainer)
2446 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00002447 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002448
Paul Duffinc459f892020-04-30 18:08:29 +01002449 if !filter(container) {
2450 expectedValue := property.emptyValue.Interface()
2451 actualValue := fieldValue.Interface()
2452 if !reflect.DeepEqual(expectedValue, actualValue) {
2453 return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue)
2454 }
2455 continue
2456 }
2457
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002458 if commonValue == nil {
2459 // Use the first value as the commonProperties value.
2460 commonValue = &fieldValue
2461 } else {
2462 // If the value does not match the current common value then there is
2463 // no value in common so break out.
2464 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
2465 commonValue = nil
Paul Duffin864e1b42020-05-06 10:23:19 +01002466 valuesDiffer = true
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002467 break
2468 }
2469 }
2470 }
2471
Paul Duffin864e1b42020-05-06 10:23:19 +01002472 // If the fields all have common value then store it in the common struct field
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002473 // and set the input struct's field to the empty value.
2474 if commonValue != nil {
Paul Duffinb28369a2020-05-04 15:39:59 +01002475 emptyValue := property.emptyValue
Paul Duffinc097e362020-03-10 22:50:03 +00002476 fieldGetter(commonStructValue).Set(*commonValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002477 for i := 0; i < sliceValue.Len(); i++ {
Paul Duffinf34f6d82020-04-30 15:48:31 +01002478 container := sliceValue.Index(i).Interface().(propertiesContainer)
2479 itemValue := reflect.ValueOf(container.optimizableProperties())
Paul Duffinc097e362020-03-10 22:50:03 +00002480 fieldValue := fieldGetter(itemValue)
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002481 fieldValue.Set(emptyValue)
2482 }
2483 }
Paul Duffin864e1b42020-05-06 10:23:19 +01002484
2485 if valuesDiffer && !property.archVariant {
2486 // The values differ but the property does not support arch variants so it
2487 // is an error.
2488 var details strings.Builder
2489 for i := 0; i < sliceValue.Len(); i++ {
2490 container := sliceValue.Index(i).Interface().(propertiesContainer)
2491 itemValue := reflect.ValueOf(container.optimizableProperties())
2492 fieldValue := fieldGetter(itemValue)
2493
2494 _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface())
2495 }
2496
2497 return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String())
2498 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002499 }
Paul Duffin4b8b7932020-05-06 12:35:38 +01002500
2501 return nil
Paul Duffin88f2fbe2020-02-27 16:00:53 +00002502}