blob: ff567be61a316ab48dc9fe55db6e9267cdbbcc5d [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//
107// The members are first grouped by type and then grouped by name. The order of
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000108// the types is the order they are referenced in android.SdkMemberTypesRegistry.
109// The names are in the order in which the dependencies were added.
Paul Duffin255f18e2019-12-13 11:22:16 +0000110func (s *sdk) collectMembers(ctx android.ModuleContext) []*sdkMember {
Paul Duffin13879572019-11-28 14:31:38 +0000111 byType := make(map[android.SdkMemberType][]*sdkMember)
112 byName := make(map[string]*sdkMember)
113
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000114 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
115 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf8539922019-11-19 19:44:10 +0000116 if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok {
117 memberType := memberTag.SdkMemberType()
Jiyong Park9b409bc2019-10-11 14:59:13 +0900118
Paul Duffin13879572019-11-28 14:31:38 +0000119 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000120 if !memberType.IsInstance(child) {
121 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900122 }
Paul Duffin13879572019-11-28 14:31:38 +0000123
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000124 name := ctx.OtherModuleName(child)
Paul Duffin13879572019-11-28 14:31:38 +0000125
126 member := byName[name]
127 if member == nil {
128 member = &sdkMember{memberType: memberType, name: name}
129 byName[name] = member
130 byType[memberType] = append(byType[memberType], member)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900131 }
Paul Duffin13879572019-11-28 14:31:38 +0000132
Paul Duffin72910952020-01-20 18:16:30 +0000133 // Only append new variants to the list. This is needed because a member can be both
134 // exported by the sdk and also be a transitive sdk member.
135 member.variants = appendUniqueVariants(member.variants, child.(android.SdkAware))
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000136
137 // If the member type supports transitive sdk members then recurse down into
138 // its dependencies, otherwise exit traversal.
139 return memberType.HasTransitiveSdkMembers()
Jiyong Park73c54ee2019-10-22 20:31:18 +0900140 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000141
142 return false
Paul Duffin13879572019-11-28 14:31:38 +0000143 })
144
145 var members []*sdkMember
Paul Duffin72910952020-01-20 18:16:30 +0000146 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13879572019-11-28 14:31:38 +0000147 membersOfType := byType[memberListProperty.memberType]
148 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900149 }
150
Paul Duffin13879572019-11-28 14:31:38 +0000151 return members
Jiyong Park73c54ee2019-10-22 20:31:18 +0900152}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900153
Paul Duffin72910952020-01-20 18:16:30 +0000154func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
155 for _, v := range variants {
156 if v == newVariant {
157 return variants
158 }
159 }
160 return append(variants, newVariant)
161}
162
Jiyong Park73c54ee2019-10-22 20:31:18 +0900163// SDK directory structure
164// <sdk_root>/
165// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
166// <api_ver>/ : below this directory are all auto-generated
167// Android.bp : definition of 'sdk_snapshot' module is here
168// aidl/
169// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
170// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900171// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900172// include/
173// bionic/libc/include/stdlib.h : an exported header file
174// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900175// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900176// <arch>/include/ : arch-specific exported headers
177// <arch>/include_gen/ : arch-specific generated headers
178// <arch>/lib/
179// libFoo.so : a stub library
180
Jiyong Park232e7852019-11-04 12:23:40 +0900181// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900182// This isn't visible to users, so could be changed in future.
183func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
184 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
185}
186
Jiyong Park232e7852019-11-04 12:23:40 +0900187// buildSnapshot is the main function in this source file. It creates rules to copy
188// the contents (header files, stub libraries, etc) into the zip file.
189func (s *sdk) buildSnapshot(ctx android.ModuleContext) android.OutputPath {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000190 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900191
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000192 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000193
194 bpFile := &bpFile{
195 modules: make(map[string]*bpModule),
196 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000197
198 builder := &snapshotBuilder{
Paul Duffinb645ec82019-11-27 17:43:54 +0000199 ctx: ctx,
Paul Duffine44358f2019-11-26 18:04:12 +0000200 sdk: s,
Paul Duffinb645ec82019-11-27 17:43:54 +0000201 version: "current",
202 snapshotDir: snapshotDir.OutputPath,
Paul Duffinc62a5102019-12-11 18:34:15 +0000203 copies: make(map[string]string),
Paul Duffinb645ec82019-11-27 17:43:54 +0000204 filesToZip: []android.Path{bp.path},
205 bpFile: bpFile,
206 prebuiltModules: make(map[string]*bpModule),
Jiyong Park73c54ee2019-10-22 20:31:18 +0900207 }
Paul Duffinac37c502019-11-26 18:02:20 +0000208 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900209
Paul Duffin255f18e2019-12-13 11:22:16 +0000210 for _, member := range s.collectMembers(ctx) {
Paul Duffin13879572019-11-28 14:31:38 +0000211 member.memberType.BuildSnapshot(ctx, builder, member)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900212 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900213
Paul Duffine6c0d842020-01-15 14:08:51 +0000214 // Create a transformer that will transform an unversioned module into a versioned module.
215 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
216
Paul Duffin72910952020-01-20 18:16:30 +0000217 // Create a transformer that will transform an unversioned module by replacing any references
218 // to internal members with a unique module name and setting prefer: false.
219 unversionedTransformer := unversionedTransformation{builder: builder}
220
Paul Duffinb645ec82019-11-27 17:43:54 +0000221 for _, unversioned := range builder.prebuiltOrder {
222 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000223 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000224
225 // Transform the unversioned module into a versioned one.
226 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000227 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000228
Paul Duffin72910952020-01-20 18:16:30 +0000229 // Transform the unversioned module to make it suitable for use in the snapshot.
230 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000231 bpFile.AddModule(unversioned)
232 }
233
234 // Create the snapshot module.
235 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000236 var snapshotModuleType string
237 if s.properties.Module_exports {
238 snapshotModuleType = "module_exports_snapshot"
239 } else {
240 snapshotModuleType = "sdk_snapshot"
241 }
242 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000243 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000244
245 // Make sure that the snapshot has the same visibility as the sdk.
246 visibility := android.EffectiveVisibilityRules(ctx, s)
247 if len(visibility) != 0 {
248 snapshotModule.AddProperty("visibility", visibility)
249 }
250
Paul Duffine44358f2019-11-26 18:04:12 +0000251 addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule)
Paul Duffin72910952020-01-20 18:16:30 +0000252 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin255f18e2019-12-13 11:22:16 +0000253 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000254 if len(names) > 0 {
Paul Duffin255f18e2019-12-13 11:22:16 +0000255 snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names))
Paul Duffin13879572019-11-28 14:31:38 +0000256 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000257 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000258 bpFile.AddModule(snapshotModule)
259
260 // generate Android.bp
261 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
262 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000263
264 bp.build(pctx, ctx, nil)
265
266 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900267
Jiyong Park232e7852019-11-04 12:23:40 +0900268 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000269 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000270 outputDesc := "Building snapshot for " + ctx.ModuleName()
271
272 // If there are no zips to merge then generate the output zip directly.
273 // Otherwise, generate an intermediate zip file into which other zips can be
274 // merged.
275 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000276 var desc string
277 if len(builder.zipsToMerge) == 0 {
278 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000279 desc = outputDesc
280 } else {
281 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000282 desc = "Building intermediate snapshot for " + ctx.ModuleName()
283 }
284
Paul Duffin375058f2019-11-29 20:17:53 +0000285 ctx.Build(pctx, android.BuildParams{
286 Description: desc,
287 Rule: zipFiles,
288 Inputs: filesToZip,
289 Output: zipFile,
290 Args: map[string]string{
291 "basedir": builder.snapshotDir.String(),
292 },
293 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900294
Paul Duffin91547182019-11-12 19:39:36 +0000295 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000296 ctx.Build(pctx, android.BuildParams{
297 Description: outputDesc,
298 Rule: mergeZips,
299 Input: zipFile,
300 Inputs: builder.zipsToMerge,
301 Output: outputZipFile,
302 })
Paul Duffin91547182019-11-12 19:39:36 +0000303 }
304
305 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900306}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000307
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000308type propertyTag struct {
309 name string
310}
311
312var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"}
313
Paul Duffine6c0d842020-01-15 14:08:51 +0000314type unversionedToVersionedTransformation struct {
315 identityTransformation
316 builder *snapshotBuilder
317}
318
Paul Duffine6c0d842020-01-15 14:08:51 +0000319func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
320 // Use a versioned name for the module but remember the original name for the
321 // snapshot.
322 name := module.getValue("name").(string)
323 module.setProperty("name", t.builder.versionedSdkMemberName(name))
324 module.insertAfter("name", "sdk_member_name", name)
325 return module
326}
327
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000328func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
329 if tag == sdkMemberReferencePropertyTag {
330 return t.builder.versionedSdkMemberNames(value.([]string)), tag
331 } else {
332 return value, tag
333 }
334}
335
Paul Duffin72910952020-01-20 18:16:30 +0000336type unversionedTransformation struct {
337 identityTransformation
338 builder *snapshotBuilder
339}
340
341func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
342 // If the module is an internal member then use a unique name for it.
343 name := module.getValue("name").(string)
344 module.setProperty("name", t.builder.unversionedSdkMemberName(name))
345
346 // Set prefer: false - this is not strictly required as that is the default.
347 module.insertAfter("name", "prefer", false)
348
349 return module
350}
351
352func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
353 if tag == sdkMemberReferencePropertyTag {
354 return t.builder.unversionedSdkMemberNames(value.([]string)), tag
355 } else {
356 return value, tag
357 }
358}
359
Paul Duffinb645ec82019-11-27 17:43:54 +0000360func generateBpContents(contents *generatedContents, bpFile *bpFile) {
361 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
362 for _, bpModule := range bpFile.order {
363 contents.Printfln("")
364 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000365 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000366 contents.Printfln("}")
367 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000368}
369
370func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
371 contents.Indent()
372 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000373 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000374
375 reflectedValue := reflect.ValueOf(value)
376 t := reflectedValue.Type()
377
378 kind := t.Kind()
379 switch kind {
380 case reflect.Slice:
381 length := reflectedValue.Len()
382 if length > 1 {
383 contents.Printfln("%s: [", name)
384 contents.Indent()
385 for i := 0; i < length; i = i + 1 {
386 contents.Printfln("%q,", reflectedValue.Index(i).Interface())
387 }
388 contents.Dedent()
389 contents.Printfln("],")
390 } else if length == 0 {
391 contents.Printfln("%s: [],", name)
392 } else {
393 contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface())
394 }
395 case reflect.Bool:
396 contents.Printfln("%s: %t,", name, reflectedValue.Bool())
397
398 case reflect.Ptr:
399 contents.Printfln("%s: {", name)
400 outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet))
401 contents.Printfln("},")
402
403 default:
404 contents.Printfln("%s: %q,", name, value)
405 }
406 }
407 contents.Dedent()
408}
409
Paul Duffinac37c502019-11-26 18:02:20 +0000410func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000411 contents := &generatedContents{}
412 generateBpContents(contents, s.builderForTests.bpFile)
413 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000414}
415
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000416type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000417 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000418 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000419 version string
420 snapshotDir android.OutputPath
421 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000422
423 // Map from destination to source of each copy - used to eliminate duplicates and
424 // detect conflicts.
425 copies map[string]string
426
Paul Duffinb645ec82019-11-27 17:43:54 +0000427 filesToZip android.Paths
428 zipsToMerge android.Paths
429
430 prebuiltModules map[string]*bpModule
431 prebuiltOrder []*bpModule
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000432}
433
434func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000435 if existing, ok := s.copies[dest]; ok {
436 if existing != src.String() {
437 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
438 return
439 }
440 } else {
441 path := s.snapshotDir.Join(s.ctx, dest)
442 s.ctx.Build(pctx, android.BuildParams{
443 Rule: android.Cp,
444 Input: src,
445 Output: path,
446 })
447 s.filesToZip = append(s.filesToZip, path)
448
449 s.copies[dest] = src.String()
450 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000451}
452
Paul Duffin91547182019-11-12 19:39:36 +0000453func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
454 ctx := s.ctx
455
456 // Repackage the zip file so that the entries are in the destDir directory.
457 // This will allow the zip file to be merged into the snapshot.
458 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000459
460 ctx.Build(pctx, android.BuildParams{
461 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
462 Rule: repackageZip,
463 Input: zipPath,
464 Output: tmpZipPath,
465 Args: map[string]string{
466 "destdir": destDir,
467 },
468 })
Paul Duffin91547182019-11-12 19:39:36 +0000469
470 // Add the repackaged zip file to the files to merge.
471 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
472}
473
Paul Duffin9d8d6092019-12-05 18:19:29 +0000474func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
475 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000476 if s.prebuiltModules[name] != nil {
477 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
478 }
479
480 m := s.bpFile.newModule(moduleType)
481 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000482
Paul Duffin72910952020-01-20 18:16:30 +0000483 if s.sdk.isInternalMember(name) {
484 // An internal member is only referenced from the sdk snapshot which is in the
485 // same package so can be marked as private.
486 m.AddProperty("visibility", []string{"//visibility:private"})
487 } else {
488 // Extract visibility information from a member variant. All variants have the same
489 // visibility so it doesn't matter which one is used.
490 visibility := android.EffectiveVisibilityRules(s.ctx, member.Variants()[0])
491 if len(visibility) != 0 {
492 m.AddProperty("visibility", visibility)
493 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000494 }
495
Paul Duffine44358f2019-11-26 18:04:12 +0000496 addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000497
498 s.prebuiltModules[name] = m
499 s.prebuiltOrder = append(s.prebuiltOrder, m)
500 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000501}
502
Paul Duffine44358f2019-11-26 18:04:12 +0000503func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) {
504 if !module.DeviceSupported() {
505 bpModule.AddProperty("device_supported", false)
506 }
507 if module.HostSupported() {
508 bpModule.AddProperty("host_supported", true)
509 }
510}
511
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000512func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag {
513 return sdkMemberReferencePropertyTag
514}
515
Paul Duffinb645ec82019-11-27 17:43:54 +0000516// Get a versioned name appropriate for the SDK snapshot version being taken.
517func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000518 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
519}
Paul Duffinb645ec82019-11-27 17:43:54 +0000520
521func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string {
522 var references []string = nil
523 for _, m := range members {
524 references = append(references, s.versionedSdkMemberName(m))
525 }
526 return references
527}
Paul Duffin13879572019-11-28 14:31:38 +0000528
Paul Duffin72910952020-01-20 18:16:30 +0000529// Get an internal name unique to the sdk.
530func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string {
531 if s.sdk.isInternalMember(unversionedName) {
532 return s.ctx.ModuleName() + "_" + unversionedName
533 } else {
534 return unversionedName
535 }
536}
537
538func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string {
539 var references []string = nil
540 for _, m := range members {
541 references = append(references, s.unversionedSdkMemberName(m))
542 }
543 return references
544}
545
Paul Duffin13879572019-11-28 14:31:38 +0000546var _ android.SdkMember = (*sdkMember)(nil)
547
548type sdkMember struct {
549 memberType android.SdkMemberType
550 name string
551 variants []android.SdkAware
552}
553
554func (m *sdkMember) Name() string {
555 return m.name
556}
557
558func (m *sdkMember) Variants() []android.SdkAware {
559 return m.variants
560}