blob: b3357778eec403dfd5639906a98b796a6c5a75ef [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 Duffin13879572019-11-28 14:31:38 +0000254 member.memberType.BuildSnapshot(ctx, builder, member)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900255 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900256
Paul Duffine6c0d842020-01-15 14:08:51 +0000257 // Create a transformer that will transform an unversioned module into a versioned module.
258 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
259
Paul Duffin72910952020-01-20 18:16:30 +0000260 // Create a transformer that will transform an unversioned module by replacing any references
261 // to internal members with a unique module name and setting prefer: false.
262 unversionedTransformer := unversionedTransformation{builder: builder}
263
Paul Duffinb645ec82019-11-27 17:43:54 +0000264 for _, unversioned := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000265 // Prune any empty property sets.
266 unversioned = unversioned.transform(pruneEmptySetTransformer{})
267
Paul Duffinb645ec82019-11-27 17:43:54 +0000268 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000269 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000270
271 // Transform the unversioned module into a versioned one.
272 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000273 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000274
Paul Duffin72910952020-01-20 18:16:30 +0000275 // Transform the unversioned module to make it suitable for use in the snapshot.
276 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000277 bpFile.AddModule(unversioned)
278 }
279
280 // Create the snapshot module.
281 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000282 var snapshotModuleType string
283 if s.properties.Module_exports {
284 snapshotModuleType = "module_exports_snapshot"
285 } else {
286 snapshotModuleType = "sdk_snapshot"
287 }
288 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000289 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000290
291 // Make sure that the snapshot has the same visibility as the sdk.
292 visibility := android.EffectiveVisibilityRules(ctx, s)
293 if len(visibility) != 0 {
294 snapshotModule.AddProperty("visibility", visibility)
295 }
296
Paul Duffine44358f2019-11-26 18:04:12 +0000297 addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000298
299 // Compile_multilib defaults to both and must always be set to both on the
300 // device and so only needs to be set when targeted at the host and is neither
301 // unspecified or both.
302 if s.HostSupported() && multilib != "" && multilib != "both" {
303 targetSet := snapshotModule.AddPropertySet("target")
304 hostSet := targetSet.AddPropertySet("host")
305 hostSet.AddProperty("compile_multilib", multilib)
306 }
307
Paul Duffin72910952020-01-20 18:16:30 +0000308 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin255f18e2019-12-13 11:22:16 +0000309 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000310 if len(names) > 0 {
Paul Duffin255f18e2019-12-13 11:22:16 +0000311 snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names))
Paul Duffin13879572019-11-28 14:31:38 +0000312 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000313 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000314 bpFile.AddModule(snapshotModule)
315
316 // generate Android.bp
317 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
318 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000319
320 bp.build(pctx, ctx, nil)
321
322 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900323
Jiyong Park232e7852019-11-04 12:23:40 +0900324 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000325 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000326 outputDesc := "Building snapshot for " + ctx.ModuleName()
327
328 // If there are no zips to merge then generate the output zip directly.
329 // Otherwise, generate an intermediate zip file into which other zips can be
330 // merged.
331 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000332 var desc string
333 if len(builder.zipsToMerge) == 0 {
334 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000335 desc = outputDesc
336 } else {
337 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000338 desc = "Building intermediate snapshot for " + ctx.ModuleName()
339 }
340
Paul Duffin375058f2019-11-29 20:17:53 +0000341 ctx.Build(pctx, android.BuildParams{
342 Description: desc,
343 Rule: zipFiles,
344 Inputs: filesToZip,
345 Output: zipFile,
346 Args: map[string]string{
347 "basedir": builder.snapshotDir.String(),
348 },
349 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900350
Paul Duffin91547182019-11-12 19:39:36 +0000351 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000352 ctx.Build(pctx, android.BuildParams{
353 Description: outputDesc,
354 Rule: mergeZips,
355 Input: zipFile,
356 Inputs: builder.zipsToMerge,
357 Output: outputZipFile,
358 })
Paul Duffin91547182019-11-12 19:39:36 +0000359 }
360
361 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900362}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000363
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000364type propertyTag struct {
365 name string
366}
367
368var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"}
369
Paul Duffine6c0d842020-01-15 14:08:51 +0000370type unversionedToVersionedTransformation struct {
371 identityTransformation
372 builder *snapshotBuilder
373}
374
Paul Duffine6c0d842020-01-15 14:08:51 +0000375func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
376 // Use a versioned name for the module but remember the original name for the
377 // snapshot.
378 name := module.getValue("name").(string)
379 module.setProperty("name", t.builder.versionedSdkMemberName(name))
380 module.insertAfter("name", "sdk_member_name", name)
381 return module
382}
383
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000384func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
385 if tag == sdkMemberReferencePropertyTag {
386 return t.builder.versionedSdkMemberNames(value.([]string)), tag
387 } else {
388 return value, tag
389 }
390}
391
Paul Duffin72910952020-01-20 18:16:30 +0000392type unversionedTransformation struct {
393 identityTransformation
394 builder *snapshotBuilder
395}
396
397func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
398 // If the module is an internal member then use a unique name for it.
399 name := module.getValue("name").(string)
400 module.setProperty("name", t.builder.unversionedSdkMemberName(name))
401
402 // Set prefer: false - this is not strictly required as that is the default.
403 module.insertAfter("name", "prefer", false)
404
405 return module
406}
407
408func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
409 if tag == sdkMemberReferencePropertyTag {
410 return t.builder.unversionedSdkMemberNames(value.([]string)), tag
411 } else {
412 return value, tag
413 }
414}
415
Paul Duffina78f3a72020-02-21 16:29:35 +0000416type pruneEmptySetTransformer struct {
417 identityTransformation
418}
419
420var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
421
422func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
423 if len(propertySet.properties) == 0 {
424 return nil, nil
425 } else {
426 return propertySet, tag
427 }
428}
429
Paul Duffinb645ec82019-11-27 17:43:54 +0000430func generateBpContents(contents *generatedContents, bpFile *bpFile) {
431 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
432 for _, bpModule := range bpFile.order {
433 contents.Printfln("")
434 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000435 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000436 contents.Printfln("}")
437 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000438}
439
440func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
441 contents.Indent()
442 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000443 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000444
445 reflectedValue := reflect.ValueOf(value)
446 t := reflectedValue.Type()
447
448 kind := t.Kind()
449 switch kind {
450 case reflect.Slice:
451 length := reflectedValue.Len()
452 if length > 1 {
453 contents.Printfln("%s: [", name)
454 contents.Indent()
455 for i := 0; i < length; i = i + 1 {
456 contents.Printfln("%q,", reflectedValue.Index(i).Interface())
457 }
458 contents.Dedent()
459 contents.Printfln("],")
460 } else if length == 0 {
461 contents.Printfln("%s: [],", name)
462 } else {
463 contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface())
464 }
465 case reflect.Bool:
466 contents.Printfln("%s: %t,", name, reflectedValue.Bool())
467
468 case reflect.Ptr:
469 contents.Printfln("%s: {", name)
470 outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet))
471 contents.Printfln("},")
472
473 default:
474 contents.Printfln("%s: %q,", name, value)
475 }
476 }
477 contents.Dedent()
478}
479
Paul Duffinac37c502019-11-26 18:02:20 +0000480func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000481 contents := &generatedContents{}
482 generateBpContents(contents, s.builderForTests.bpFile)
483 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000484}
485
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000486type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000487 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000488 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000489 version string
490 snapshotDir android.OutputPath
491 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000492
493 // Map from destination to source of each copy - used to eliminate duplicates and
494 // detect conflicts.
495 copies map[string]string
496
Paul Duffinb645ec82019-11-27 17:43:54 +0000497 filesToZip android.Paths
498 zipsToMerge android.Paths
499
500 prebuiltModules map[string]*bpModule
501 prebuiltOrder []*bpModule
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000502}
503
504func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000505 if existing, ok := s.copies[dest]; ok {
506 if existing != src.String() {
507 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
508 return
509 }
510 } else {
511 path := s.snapshotDir.Join(s.ctx, dest)
512 s.ctx.Build(pctx, android.BuildParams{
513 Rule: android.Cp,
514 Input: src,
515 Output: path,
516 })
517 s.filesToZip = append(s.filesToZip, path)
518
519 s.copies[dest] = src.String()
520 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000521}
522
Paul Duffin91547182019-11-12 19:39:36 +0000523func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
524 ctx := s.ctx
525
526 // Repackage the zip file so that the entries are in the destDir directory.
527 // This will allow the zip file to be merged into the snapshot.
528 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000529
530 ctx.Build(pctx, android.BuildParams{
531 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
532 Rule: repackageZip,
533 Input: zipPath,
534 Output: tmpZipPath,
535 Args: map[string]string{
536 "destdir": destDir,
537 },
538 })
Paul Duffin91547182019-11-12 19:39:36 +0000539
540 // Add the repackaged zip file to the files to merge.
541 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
542}
543
Paul Duffin9d8d6092019-12-05 18:19:29 +0000544func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
545 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000546 if s.prebuiltModules[name] != nil {
547 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
548 }
549
550 m := s.bpFile.newModule(moduleType)
551 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000552
Paul Duffinbefa4b92020-03-04 14:22:45 +0000553 variant := member.Variants()[0]
554
Paul Duffin72910952020-01-20 18:16:30 +0000555 if s.sdk.isInternalMember(name) {
556 // An internal member is only referenced from the sdk snapshot which is in the
557 // same package so can be marked as private.
558 m.AddProperty("visibility", []string{"//visibility:private"})
559 } else {
560 // Extract visibility information from a member variant. All variants have the same
561 // visibility so it doesn't matter which one is used.
Paul Duffinbefa4b92020-03-04 14:22:45 +0000562 visibility := android.EffectiveVisibilityRules(s.ctx, variant)
Paul Duffin72910952020-01-20 18:16:30 +0000563 if len(visibility) != 0 {
564 m.AddProperty("visibility", visibility)
565 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000566 }
567
Paul Duffine44358f2019-11-26 18:04:12 +0000568 addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000569
Paul Duffinbefa4b92020-03-04 14:22:45 +0000570 // Where available copy apex_available properties from the member.
571 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
572 apexAvailable := apexAware.ApexAvailable()
573 if len(apexAvailable) > 0 {
574 m.AddProperty("apex_available", apexAvailable)
575 }
576 }
577
Paul Duffinb645ec82019-11-27 17:43:54 +0000578 s.prebuiltModules[name] = m
579 s.prebuiltOrder = append(s.prebuiltOrder, m)
580 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000581}
582
Paul Duffine44358f2019-11-26 18:04:12 +0000583func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) {
584 if !module.DeviceSupported() {
585 bpModule.AddProperty("device_supported", false)
586 }
587 if module.HostSupported() {
588 bpModule.AddProperty("host_supported", true)
589 }
590}
591
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000592func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag {
593 return sdkMemberReferencePropertyTag
594}
595
Paul Duffinb645ec82019-11-27 17:43:54 +0000596// Get a versioned name appropriate for the SDK snapshot version being taken.
597func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000598 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
599}
Paul Duffinb645ec82019-11-27 17:43:54 +0000600
601func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string {
602 var references []string = nil
603 for _, m := range members {
604 references = append(references, s.versionedSdkMemberName(m))
605 }
606 return references
607}
Paul Duffin13879572019-11-28 14:31:38 +0000608
Paul Duffin72910952020-01-20 18:16:30 +0000609// Get an internal name unique to the sdk.
610func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string {
611 if s.sdk.isInternalMember(unversionedName) {
612 return s.ctx.ModuleName() + "_" + unversionedName
613 } else {
614 return unversionedName
615 }
616}
617
618func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string {
619 var references []string = nil
620 for _, m := range members {
621 references = append(references, s.unversionedSdkMemberName(m))
622 }
623 return references
624}
625
Paul Duffin1356d8c2020-02-25 19:26:33 +0000626type sdkMemberRef struct {
627 memberType android.SdkMemberType
628 variant android.SdkAware
629}
630
Paul Duffin13879572019-11-28 14:31:38 +0000631var _ android.SdkMember = (*sdkMember)(nil)
632
633type sdkMember struct {
634 memberType android.SdkMemberType
635 name string
636 variants []android.SdkAware
637}
638
639func (m *sdkMember) Name() string {
640 return m.name
641}
642
643func (m *sdkMember) Variants() []android.SdkAware {
644 return m.variants
645}