blob: 84b6c18f4b805660a3d040c6680ce2940ed1869e [file] [log] [blame]
Jiyong Park9b409bc2019-10-11 14:59:13 +09001// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package sdk
16
17import (
18 "fmt"
Paul Duffinb645ec82019-11-27 17:43:54 +000019 "reflect"
Jiyong Park9b409bc2019-10-11 14:59:13 +090020 "strings"
21
Paul Duffin375058f2019-11-29 20:17:53 +000022 "github.com/google/blueprint"
Jiyong Park9b409bc2019-10-11 14:59:13 +090023 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
Jiyong Park9b409bc2019-10-11 14:59:13 +090026)
27
28var pctx = android.NewPackageContext("android/soong/sdk")
29
Paul Duffin375058f2019-11-29 20:17:53 +000030var (
31 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
32 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000033 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000034 CommandDeps: []string{
35 "${config.Zip2ZipCmd}",
36 },
37 },
38 "destdir")
39
40 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
41 blueprint.RuleParams{
42 Command: `${config.SoongZipCmd} -C $basedir -l $out.rsp -o $out`,
43 CommandDeps: []string{
44 "${config.SoongZipCmd}",
45 },
46 Rspfile: "$out.rsp",
47 RspfileContent: "$in",
48 },
49 "basedir")
50
51 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
52 blueprint.RuleParams{
53 Command: `${config.MergeZipsCmd} $out $in`,
54 CommandDeps: []string{
55 "${config.MergeZipsCmd}",
56 },
57 })
58)
59
Paul Duffinb645ec82019-11-27 17:43:54 +000060type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090061 content strings.Builder
62 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090063}
64
Paul Duffinb645ec82019-11-27 17:43:54 +000065// generatedFile abstracts operations for writing contents into a file and emit a build rule
66// for the file.
67type generatedFile struct {
68 generatedContents
69 path android.OutputPath
70}
71
Jiyong Park232e7852019-11-04 12:23:40 +090072func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +090073 return &generatedFile{
Paul Duffinb645ec82019-11-27 17:43:54 +000074 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park9b409bc2019-10-11 14:59:13 +090075 }
76}
77
Paul Duffinb645ec82019-11-27 17:43:54 +000078func (gc *generatedContents) Indent() {
79 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +090080}
81
Paul Duffinb645ec82019-11-27 17:43:54 +000082func (gc *generatedContents) Dedent() {
83 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +090084}
85
Paul Duffinb645ec82019-11-27 17:43:54 +000086func (gc *generatedContents) Printfln(format string, args ...interface{}) {
Jiyong Park9b409bc2019-10-11 14:59:13 +090087 // ninja consumes newline characters in rspfile_content. Prevent it by
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000088 // escaping the backslash in the newline character. The extra backslash
Jiyong Park9b409bc2019-10-11 14:59:13 +090089 // is removed when the rspfile is written to the actual script file
Paul Duffinb645ec82019-11-27 17:43:54 +000090 fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\\n", args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +090091}
92
93func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
94 rb := android.NewRuleBuilder()
95 // convert \\n to \n
96 rb.Command().
97 Implicits(implicits).
98 Text("echo").Text(proptools.ShellEscape(gf.content.String())).
99 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
100 rb.Command().
101 Text("chmod a+x").Output(gf.path)
102 rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base())
103}
104
Paul Duffin13879572019-11-28 14:31:38 +0000105// Collect all the members.
106//
Paul Duffin1356d8c2020-02-25 19:26:33 +0000107// Returns a list containing type (extracted from the dependency tag) and the variant.
108func (s *sdk) collectMembers(ctx android.ModuleContext) []sdkMemberRef {
109 var memberRefs []sdkMemberRef
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000110 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
111 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf8539922019-11-19 19:44:10 +0000112 if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok {
113 memberType := memberTag.SdkMemberType()
Jiyong Park9b409bc2019-10-11 14:59:13 +0900114
Paul Duffin13879572019-11-28 14:31:38 +0000115 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000116 if !memberType.IsInstance(child) {
117 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900118 }
Paul Duffin13879572019-11-28 14:31:38 +0000119
Paul Duffin1356d8c2020-02-25 19:26:33 +0000120 memberRefs = append(memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)})
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000121
122 // If the member type supports transitive sdk members then recurse down into
123 // its dependencies, otherwise exit traversal.
124 return memberType.HasTransitiveSdkMembers()
Jiyong Park73c54ee2019-10-22 20:31:18 +0900125 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000126
127 return false
Paul Duffin13879572019-11-28 14:31:38 +0000128 })
129
Paul Duffin1356d8c2020-02-25 19:26:33 +0000130 return memberRefs
131}
132
133// Organize the members.
134//
135// The members are first grouped by type and then grouped by name. The order of
136// the types is the order they are referenced in android.SdkMemberTypesRegistry.
137// The names are in the order in which the dependencies were added.
138//
139// Returns the members as well as the multilib setting to use.
140func (s *sdk) organizeMembers(ctx android.ModuleContext, memberRefs []sdkMemberRef) ([]*sdkMember, string) {
141 byType := make(map[android.SdkMemberType][]*sdkMember)
142 byName := make(map[string]*sdkMember)
143
144 lib32 := false // True if any of the members have 32 bit version.
145 lib64 := false // True if any of the members have 64 bit version.
146
147 for _, memberRef := range memberRefs {
148 memberType := memberRef.memberType
149 variant := memberRef.variant
150
151 name := ctx.OtherModuleName(variant)
152 member := byName[name]
153 if member == nil {
154 member = &sdkMember{memberType: memberType, name: name}
155 byName[name] = member
156 byType[memberType] = append(byType[memberType], member)
157 }
158
159 multilib := variant.Target().Arch.ArchType.Multilib
160 if multilib == "lib32" {
161 lib32 = true
162 } else if multilib == "lib64" {
163 lib64 = true
164 }
165
166 // Only append new variants to the list. This is needed because a member can be both
167 // exported by the sdk and also be a transitive sdk member.
168 member.variants = appendUniqueVariants(member.variants, variant)
169 }
170
Paul Duffin13879572019-11-28 14:31:38 +0000171 var members []*sdkMember
Paul Duffin72910952020-01-20 18:16:30 +0000172 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13879572019-11-28 14:31:38 +0000173 membersOfType := byType[memberListProperty.memberType]
174 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900175 }
176
Paul Duffin13ad94f2020-02-19 16:19:27 +0000177 // Compute the setting of multilib.
178 var multilib string
179 if lib32 && lib64 {
180 multilib = "both"
181 } else if lib32 {
182 multilib = "32"
183 } else if lib64 {
184 multilib = "64"
185 }
186
187 return members, multilib
Jiyong Park73c54ee2019-10-22 20:31:18 +0900188}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900189
Paul Duffin72910952020-01-20 18:16:30 +0000190func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
191 for _, v := range variants {
192 if v == newVariant {
193 return variants
194 }
195 }
196 return append(variants, newVariant)
197}
198
Jiyong Park73c54ee2019-10-22 20:31:18 +0900199// SDK directory structure
200// <sdk_root>/
201// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
202// <api_ver>/ : below this directory are all auto-generated
203// Android.bp : definition of 'sdk_snapshot' module is here
204// aidl/
205// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
206// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900207// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900208// include/
209// bionic/libc/include/stdlib.h : an exported header file
210// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900211// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900212// <arch>/include/ : arch-specific exported headers
213// <arch>/include_gen/ : arch-specific generated headers
214// <arch>/lib/
215// libFoo.so : a stub library
216
Jiyong Park232e7852019-11-04 12:23:40 +0900217// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900218// This isn't visible to users, so could be changed in future.
219func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
220 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
221}
222
Jiyong Park232e7852019-11-04 12:23:40 +0900223// buildSnapshot is the main function in this source file. It creates rules to copy
224// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000225func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath {
226
227 var memberRefs []sdkMemberRef
228 for _, sdkVariant := range sdkVariants {
229 memberRefs = append(memberRefs, sdkVariant.memberRefs...)
230 }
231
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000232 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900233
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000234 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000235
236 bpFile := &bpFile{
237 modules: make(map[string]*bpModule),
238 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000239
240 builder := &snapshotBuilder{
Paul Duffinb645ec82019-11-27 17:43:54 +0000241 ctx: ctx,
Paul Duffine44358f2019-11-26 18:04:12 +0000242 sdk: s,
Paul Duffinb645ec82019-11-27 17:43:54 +0000243 version: "current",
244 snapshotDir: snapshotDir.OutputPath,
Paul Duffinc62a5102019-12-11 18:34:15 +0000245 copies: make(map[string]string),
Paul Duffinb645ec82019-11-27 17:43:54 +0000246 filesToZip: []android.Path{bp.path},
247 bpFile: bpFile,
248 prebuiltModules: make(map[string]*bpModule),
Jiyong Park73c54ee2019-10-22 20:31:18 +0900249 }
Paul Duffinac37c502019-11-26 18:02:20 +0000250 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900251
Paul Duffin1356d8c2020-02-25 19:26:33 +0000252 members, multilib := s.organizeMembers(ctx, memberRefs)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000253 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000254 memberType := member.memberType
255 prebuiltModule := memberType.AddPrebuiltModule(ctx, builder, member)
256 if prebuiltModule == nil {
257 // Fall back to legacy method of building a snapshot
258 memberType.BuildSnapshot(ctx, builder, member)
259 } else {
260 s.createMemberSnapshot(ctx, builder, member, prebuiltModule)
261 }
Jiyong Park73c54ee2019-10-22 20:31:18 +0900262 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900263
Paul Duffine6c0d842020-01-15 14:08:51 +0000264 // Create a transformer that will transform an unversioned module into a versioned module.
265 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
266
Paul Duffin72910952020-01-20 18:16:30 +0000267 // Create a transformer that will transform an unversioned module by replacing any references
268 // to internal members with a unique module name and setting prefer: false.
269 unversionedTransformer := unversionedTransformation{builder: builder}
270
Paul Duffinb645ec82019-11-27 17:43:54 +0000271 for _, unversioned := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000272 // Prune any empty property sets.
273 unversioned = unversioned.transform(pruneEmptySetTransformer{})
274
Paul Duffinb645ec82019-11-27 17:43:54 +0000275 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000276 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000277
278 // Transform the unversioned module into a versioned one.
279 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000280 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000281
Paul Duffin72910952020-01-20 18:16:30 +0000282 // Transform the unversioned module to make it suitable for use in the snapshot.
283 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000284 bpFile.AddModule(unversioned)
285 }
286
287 // Create the snapshot module.
288 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000289 var snapshotModuleType string
290 if s.properties.Module_exports {
291 snapshotModuleType = "module_exports_snapshot"
292 } else {
293 snapshotModuleType = "sdk_snapshot"
294 }
295 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000296 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000297
298 // Make sure that the snapshot has the same visibility as the sdk.
299 visibility := android.EffectiveVisibilityRules(ctx, s)
300 if len(visibility) != 0 {
301 snapshotModule.AddProperty("visibility", visibility)
302 }
303
Paul Duffine44358f2019-11-26 18:04:12 +0000304 addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000305
306 // Compile_multilib defaults to both and must always be set to both on the
307 // device and so only needs to be set when targeted at the host and is neither
308 // unspecified or both.
309 if s.HostSupported() && multilib != "" && multilib != "both" {
310 targetSet := snapshotModule.AddPropertySet("target")
311 hostSet := targetSet.AddPropertySet("host")
312 hostSet.AddProperty("compile_multilib", multilib)
313 }
314
Paul Duffin72910952020-01-20 18:16:30 +0000315 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin255f18e2019-12-13 11:22:16 +0000316 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000317 if len(names) > 0 {
Paul Duffin255f18e2019-12-13 11:22:16 +0000318 snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names))
Paul Duffin13879572019-11-28 14:31:38 +0000319 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000320 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000321 bpFile.AddModule(snapshotModule)
322
323 // generate Android.bp
324 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
325 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000326
327 bp.build(pctx, ctx, nil)
328
329 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900330
Jiyong Park232e7852019-11-04 12:23:40 +0900331 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000332 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000333 outputDesc := "Building snapshot for " + ctx.ModuleName()
334
335 // If there are no zips to merge then generate the output zip directly.
336 // Otherwise, generate an intermediate zip file into which other zips can be
337 // merged.
338 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000339 var desc string
340 if len(builder.zipsToMerge) == 0 {
341 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000342 desc = outputDesc
343 } else {
344 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000345 desc = "Building intermediate snapshot for " + ctx.ModuleName()
346 }
347
Paul Duffin375058f2019-11-29 20:17:53 +0000348 ctx.Build(pctx, android.BuildParams{
349 Description: desc,
350 Rule: zipFiles,
351 Inputs: filesToZip,
352 Output: zipFile,
353 Args: map[string]string{
354 "basedir": builder.snapshotDir.String(),
355 },
356 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900357
Paul Duffin91547182019-11-12 19:39:36 +0000358 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000359 ctx.Build(pctx, android.BuildParams{
360 Description: outputDesc,
361 Rule: mergeZips,
362 Input: zipFile,
363 Inputs: builder.zipsToMerge,
364 Output: outputZipFile,
365 })
Paul Duffin91547182019-11-12 19:39:36 +0000366 }
367
368 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900369}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000370
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000371type propertyTag struct {
372 name string
373}
374
375var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"}
376
Paul Duffine6c0d842020-01-15 14:08:51 +0000377type unversionedToVersionedTransformation struct {
378 identityTransformation
379 builder *snapshotBuilder
380}
381
Paul Duffine6c0d842020-01-15 14:08:51 +0000382func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
383 // Use a versioned name for the module but remember the original name for the
384 // snapshot.
385 name := module.getValue("name").(string)
386 module.setProperty("name", t.builder.versionedSdkMemberName(name))
387 module.insertAfter("name", "sdk_member_name", name)
388 return module
389}
390
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000391func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
392 if tag == sdkMemberReferencePropertyTag {
393 return t.builder.versionedSdkMemberNames(value.([]string)), tag
394 } else {
395 return value, tag
396 }
397}
398
Paul Duffin72910952020-01-20 18:16:30 +0000399type unversionedTransformation struct {
400 identityTransformation
401 builder *snapshotBuilder
402}
403
404func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
405 // If the module is an internal member then use a unique name for it.
406 name := module.getValue("name").(string)
407 module.setProperty("name", t.builder.unversionedSdkMemberName(name))
408
409 // Set prefer: false - this is not strictly required as that is the default.
410 module.insertAfter("name", "prefer", false)
411
412 return module
413}
414
415func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
416 if tag == sdkMemberReferencePropertyTag {
417 return t.builder.unversionedSdkMemberNames(value.([]string)), tag
418 } else {
419 return value, tag
420 }
421}
422
Paul Duffina78f3a72020-02-21 16:29:35 +0000423type pruneEmptySetTransformer struct {
424 identityTransformation
425}
426
427var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
428
429func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
430 if len(propertySet.properties) == 0 {
431 return nil, nil
432 } else {
433 return propertySet, tag
434 }
435}
436
Paul Duffinb645ec82019-11-27 17:43:54 +0000437func generateBpContents(contents *generatedContents, bpFile *bpFile) {
438 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
439 for _, bpModule := range bpFile.order {
440 contents.Printfln("")
441 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000442 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000443 contents.Printfln("}")
444 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000445}
446
447func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
448 contents.Indent()
449 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000450 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000451
452 reflectedValue := reflect.ValueOf(value)
453 t := reflectedValue.Type()
454
455 kind := t.Kind()
456 switch kind {
457 case reflect.Slice:
458 length := reflectedValue.Len()
459 if length > 1 {
460 contents.Printfln("%s: [", name)
461 contents.Indent()
462 for i := 0; i < length; i = i + 1 {
463 contents.Printfln("%q,", reflectedValue.Index(i).Interface())
464 }
465 contents.Dedent()
466 contents.Printfln("],")
467 } else if length == 0 {
468 contents.Printfln("%s: [],", name)
469 } else {
470 contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface())
471 }
472 case reflect.Bool:
473 contents.Printfln("%s: %t,", name, reflectedValue.Bool())
474
475 case reflect.Ptr:
476 contents.Printfln("%s: {", name)
477 outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet))
478 contents.Printfln("},")
479
480 default:
481 contents.Printfln("%s: %q,", name, value)
482 }
483 }
484 contents.Dedent()
485}
486
Paul Duffinac37c502019-11-26 18:02:20 +0000487func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000488 contents := &generatedContents{}
489 generateBpContents(contents, s.builderForTests.bpFile)
490 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000491}
492
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000493type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000494 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000495 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000496 version string
497 snapshotDir android.OutputPath
498 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000499
500 // Map from destination to source of each copy - used to eliminate duplicates and
501 // detect conflicts.
502 copies map[string]string
503
Paul Duffinb645ec82019-11-27 17:43:54 +0000504 filesToZip android.Paths
505 zipsToMerge android.Paths
506
507 prebuiltModules map[string]*bpModule
508 prebuiltOrder []*bpModule
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000509}
510
511func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000512 if existing, ok := s.copies[dest]; ok {
513 if existing != src.String() {
514 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
515 return
516 }
517 } else {
518 path := s.snapshotDir.Join(s.ctx, dest)
519 s.ctx.Build(pctx, android.BuildParams{
520 Rule: android.Cp,
521 Input: src,
522 Output: path,
523 })
524 s.filesToZip = append(s.filesToZip, path)
525
526 s.copies[dest] = src.String()
527 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000528}
529
Paul Duffin91547182019-11-12 19:39:36 +0000530func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
531 ctx := s.ctx
532
533 // Repackage the zip file so that the entries are in the destDir directory.
534 // This will allow the zip file to be merged into the snapshot.
535 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000536
537 ctx.Build(pctx, android.BuildParams{
538 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
539 Rule: repackageZip,
540 Input: zipPath,
541 Output: tmpZipPath,
542 Args: map[string]string{
543 "destdir": destDir,
544 },
545 })
Paul Duffin91547182019-11-12 19:39:36 +0000546
547 // Add the repackaged zip file to the files to merge.
548 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
549}
550
Paul Duffin9d8d6092019-12-05 18:19:29 +0000551func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
552 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000553 if s.prebuiltModules[name] != nil {
554 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
555 }
556
557 m := s.bpFile.newModule(moduleType)
558 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000559
Paul Duffinbefa4b92020-03-04 14:22:45 +0000560 variant := member.Variants()[0]
561
Paul Duffin72910952020-01-20 18:16:30 +0000562 if s.sdk.isInternalMember(name) {
563 // An internal member is only referenced from the sdk snapshot which is in the
564 // same package so can be marked as private.
565 m.AddProperty("visibility", []string{"//visibility:private"})
566 } else {
567 // Extract visibility information from a member variant. All variants have the same
568 // visibility so it doesn't matter which one is used.
Paul Duffinbefa4b92020-03-04 14:22:45 +0000569 visibility := android.EffectiveVisibilityRules(s.ctx, variant)
Paul Duffin72910952020-01-20 18:16:30 +0000570 if len(visibility) != 0 {
571 m.AddProperty("visibility", visibility)
572 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000573 }
574
Paul Duffine44358f2019-11-26 18:04:12 +0000575 addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000576
Paul Duffinbefa4b92020-03-04 14:22:45 +0000577 // Where available copy apex_available properties from the member.
578 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
579 apexAvailable := apexAware.ApexAvailable()
580 if len(apexAvailable) > 0 {
581 m.AddProperty("apex_available", apexAvailable)
582 }
583 }
584
Paul Duffinb645ec82019-11-27 17:43:54 +0000585 s.prebuiltModules[name] = m
586 s.prebuiltOrder = append(s.prebuiltOrder, m)
587 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000588}
589
Paul Duffine44358f2019-11-26 18:04:12 +0000590func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) {
591 if !module.DeviceSupported() {
592 bpModule.AddProperty("device_supported", false)
593 }
594 if module.HostSupported() {
595 bpModule.AddProperty("host_supported", true)
596 }
597}
598
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000599func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag {
600 return sdkMemberReferencePropertyTag
601}
602
Paul Duffinb645ec82019-11-27 17:43:54 +0000603// Get a versioned name appropriate for the SDK snapshot version being taken.
604func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000605 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
606}
Paul Duffinb645ec82019-11-27 17:43:54 +0000607
608func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string {
609 var references []string = nil
610 for _, m := range members {
611 references = append(references, s.versionedSdkMemberName(m))
612 }
613 return references
614}
Paul Duffin13879572019-11-28 14:31:38 +0000615
Paul Duffin72910952020-01-20 18:16:30 +0000616// Get an internal name unique to the sdk.
617func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string {
618 if s.sdk.isInternalMember(unversionedName) {
619 return s.ctx.ModuleName() + "_" + unversionedName
620 } else {
621 return unversionedName
622 }
623}
624
625func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string {
626 var references []string = nil
627 for _, m := range members {
628 references = append(references, s.unversionedSdkMemberName(m))
629 }
630 return references
631}
632
Paul Duffin1356d8c2020-02-25 19:26:33 +0000633type sdkMemberRef struct {
634 memberType android.SdkMemberType
635 variant android.SdkAware
636}
637
Paul Duffin13879572019-11-28 14:31:38 +0000638var _ android.SdkMember = (*sdkMember)(nil)
639
640type sdkMember struct {
641 memberType android.SdkMemberType
642 name string
643 variants []android.SdkAware
644}
645
646func (m *sdkMember) Name() string {
647 return m.name
648}
649
650func (m *sdkMember) Variants() []android.SdkAware {
651 return m.variants
652}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000653
654type baseInfo struct {
655 Properties android.SdkMemberProperties
656}
657
658type osTypeSpecificInfo struct {
659 baseInfo
660
661 // The list of arch type specific info for this os type.
662 archTypes []*archTypeSpecificInfo
663}
664
665type archTypeSpecificInfo struct {
666 baseInfo
667
668 archType android.ArchType
669}
670
671func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) {
672
673 memberType := member.memberType
674
675 // Group the properties for each variant by arch type.
676 osInfo := &osTypeSpecificInfo{}
677 osInfo.Properties = memberType.CreateVariantPropertiesStruct()
678 variants := member.Variants()
Paul Duffin14eb4672020-03-02 11:33:02 +0000679 commonArch := false
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000680 for _, variant := range variants {
681 var properties android.SdkMemberProperties
682
683 // Get the info associated with the arch type inside the os info.
684 archType := variant.Target().Arch.ArchType
685
Paul Duffin14eb4672020-03-02 11:33:02 +0000686 if archType.Name == "common" {
687 // The arch type is common so populate the common properties directly.
688 properties = osInfo.Properties
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000689
Paul Duffin14eb4672020-03-02 11:33:02 +0000690 commonArch = true
691 } else {
692 archInfo := &archTypeSpecificInfo{archType: archType}
693 properties = memberType.CreateVariantPropertiesStruct()
694 archInfo.Properties = properties
695
696 osInfo.archTypes = append(osInfo.archTypes, archInfo)
697 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000698
699 properties.PopulateFromVariant(variant)
700 }
701
Paul Duffin14eb4672020-03-02 11:33:02 +0000702 if commonArch {
703 if len(variants) != 1 {
704 panic("Expected to only have 1 variant when arch type is common but found " + string(len(variants)))
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000705 }
Paul Duffin14eb4672020-03-02 11:33:02 +0000706 } else {
707 var archProperties []android.SdkMemberProperties
708 for _, archInfo := range osInfo.archTypes {
709 archProperties = append(archProperties, archInfo.Properties)
710 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000711
Paul Duffin14eb4672020-03-02 11:33:02 +0000712 extractCommonProperties(osInfo.Properties, archProperties)
713
714 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
715 var multilib string
716 archVariantCount := len(osInfo.archTypes)
717 if archVariantCount == 2 {
718 multilib = "both"
719 } else if archVariantCount == 1 {
720 if strings.HasSuffix(osInfo.archTypes[0].archType.Name, "64") {
721 multilib = "64"
722 } else {
723 multilib = "32"
724 }
725 }
726
727 osInfo.Properties.Base().Compile_multilib = multilib
728 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000729
730 osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, bpModule)
731
Paul Duffin14eb4672020-03-02 11:33:02 +0000732 if !commonArch {
733 archPropertySet := bpModule.AddPropertySet("arch")
734 for _, av := range osInfo.archTypes {
735 archTypePropertySet := archPropertySet.AddPropertySet(av.archType.Name)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000736
Paul Duffin14eb4672020-03-02 11:33:02 +0000737 av.Properties.AddToPropertySet(sdkModuleContext, builder, archTypePropertySet)
738 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000739 }
740
741 memberType.FinalizeModule(sdkModuleContext, builder, member, bpModule)
742}
743
744// Extract common properties from a slice of property structures of the same type.
745//
746// All the property structures must be of the same type.
747// commonProperties - must be a pointer to the structure into which common properties will be added.
748// inputPropertiesSlice - must be a slice of input properties structures.
749//
750// Iterates over each exported field (capitalized name) and checks to see whether they
751// have the same value (using DeepEquals) across all the input properties. If it does not then no
752// change is made. Otherwise, the common value is stored in the field in the commonProperties
753// and the field in each of the input properties structure is set to its default value.
754func extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) {
755 commonPropertiesValue := reflect.ValueOf(commonProperties)
756 commonStructValue := commonPropertiesValue.Elem()
757 propertiesStructType := commonStructValue.Type()
758
759 // Create an empty structure from which default values for the field can be copied.
760 emptyStructValue := reflect.New(propertiesStructType).Elem()
761
762 for f := 0; f < propertiesStructType.NumField(); f++ {
763 // Check to see if all the structures have the same value for the field. The commonValue
764 // is nil on entry to the loop and if it is nil on exit then there is no common value,
765 // otherwise it points to the common value.
766 var commonValue *reflect.Value
767 sliceValue := reflect.ValueOf(inputPropertiesSlice)
768
769 for i := 0; i < sliceValue.Len(); i++ {
770 structValue := sliceValue.Index(i).Elem().Elem()
771 fieldValue := structValue.Field(f)
772 if !fieldValue.CanInterface() {
773 // The field is not exported so ignore it.
774 continue
775 }
776
777 if commonValue == nil {
778 // Use the first value as the commonProperties value.
779 commonValue = &fieldValue
780 } else {
781 // If the value does not match the current common value then there is
782 // no value in common so break out.
783 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
784 commonValue = nil
785 break
786 }
787 }
788 }
789
790 // If the fields all have a common value then store it in the common struct field
791 // and set the input struct's field to the empty value.
792 if commonValue != nil {
793 emptyValue := emptyStructValue.Field(f)
794 commonStructValue.Field(f).Set(*commonValue)
795 for i := 0; i < sliceValue.Len(); i++ {
796 structValue := sliceValue.Index(i).Elem().Elem()
797 fieldValue := structValue.Field(f)
798 fieldValue.Set(emptyValue)
799 }
800 }
801 }
802}