blob: c64995f46345b6211c095485bea8a8443513af75 [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 Duffin13ad94f2020-02-19 16:19:27 +0000110//
111// Returns the members as well as the multilib setting to use.
112func (s *sdk) collectMembers(ctx android.ModuleContext) ([]*sdkMember, string) {
Paul Duffin13879572019-11-28 14:31:38 +0000113 byType := make(map[android.SdkMemberType][]*sdkMember)
114 byName := make(map[string]*sdkMember)
115
Paul Duffin13ad94f2020-02-19 16:19:27 +0000116 lib32 := false // True if any of the members have 32 bit version.
117 lib64 := false // True if any of the members have 64 bit version.
118
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000119 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
120 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf8539922019-11-19 19:44:10 +0000121 if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok {
122 memberType := memberTag.SdkMemberType()
Jiyong Park9b409bc2019-10-11 14:59:13 +0900123
Paul Duffin13879572019-11-28 14:31:38 +0000124 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000125 if !memberType.IsInstance(child) {
126 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900127 }
Paul Duffin13879572019-11-28 14:31:38 +0000128
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000129 name := ctx.OtherModuleName(child)
Paul Duffin13879572019-11-28 14:31:38 +0000130 member := byName[name]
131 if member == nil {
132 member = &sdkMember{memberType: memberType, name: name}
133 byName[name] = member
134 byType[memberType] = append(byType[memberType], member)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900135 }
Paul Duffin13879572019-11-28 14:31:38 +0000136
Paul Duffin13ad94f2020-02-19 16:19:27 +0000137 multilib := child.Target().Arch.ArchType.Multilib
138 if multilib == "lib32" {
139 lib32 = true
140 } else if multilib == "lib64" {
141 lib64 = true
142 }
143
Paul Duffin72910952020-01-20 18:16:30 +0000144 // Only append new variants to the list. This is needed because a member can be both
145 // exported by the sdk and also be a transitive sdk member.
146 member.variants = appendUniqueVariants(member.variants, child.(android.SdkAware))
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000147
148 // If the member type supports transitive sdk members then recurse down into
149 // its dependencies, otherwise exit traversal.
150 return memberType.HasTransitiveSdkMembers()
Jiyong Park73c54ee2019-10-22 20:31:18 +0900151 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000152
153 return false
Paul Duffin13879572019-11-28 14:31:38 +0000154 })
155
156 var members []*sdkMember
Paul Duffin72910952020-01-20 18:16:30 +0000157 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13879572019-11-28 14:31:38 +0000158 membersOfType := byType[memberListProperty.memberType]
159 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900160 }
161
Paul Duffin13ad94f2020-02-19 16:19:27 +0000162 // Compute the setting of multilib.
163 var multilib string
164 if lib32 && lib64 {
165 multilib = "both"
166 } else if lib32 {
167 multilib = "32"
168 } else if lib64 {
169 multilib = "64"
170 }
171
172 return members, multilib
Jiyong Park73c54ee2019-10-22 20:31:18 +0900173}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900174
Paul Duffin72910952020-01-20 18:16:30 +0000175func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
176 for _, v := range variants {
177 if v == newVariant {
178 return variants
179 }
180 }
181 return append(variants, newVariant)
182}
183
Jiyong Park73c54ee2019-10-22 20:31:18 +0900184// SDK directory structure
185// <sdk_root>/
186// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
187// <api_ver>/ : below this directory are all auto-generated
188// Android.bp : definition of 'sdk_snapshot' module is here
189// aidl/
190// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
191// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900192// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900193// include/
194// bionic/libc/include/stdlib.h : an exported header file
195// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900196// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900197// <arch>/include/ : arch-specific exported headers
198// <arch>/include_gen/ : arch-specific generated headers
199// <arch>/lib/
200// libFoo.so : a stub library
201
Jiyong Park232e7852019-11-04 12:23:40 +0900202// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900203// This isn't visible to users, so could be changed in future.
204func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
205 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
206}
207
Jiyong Park232e7852019-11-04 12:23:40 +0900208// buildSnapshot is the main function in this source file. It creates rules to copy
209// the contents (header files, stub libraries, etc) into the zip file.
210func (s *sdk) buildSnapshot(ctx android.ModuleContext) android.OutputPath {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000211 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900212
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000213 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000214
215 bpFile := &bpFile{
216 modules: make(map[string]*bpModule),
217 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000218
219 builder := &snapshotBuilder{
Paul Duffinb645ec82019-11-27 17:43:54 +0000220 ctx: ctx,
Paul Duffine44358f2019-11-26 18:04:12 +0000221 sdk: s,
Paul Duffinb645ec82019-11-27 17:43:54 +0000222 version: "current",
223 snapshotDir: snapshotDir.OutputPath,
Paul Duffinc62a5102019-12-11 18:34:15 +0000224 copies: make(map[string]string),
Paul Duffinb645ec82019-11-27 17:43:54 +0000225 filesToZip: []android.Path{bp.path},
226 bpFile: bpFile,
227 prebuiltModules: make(map[string]*bpModule),
Jiyong Park73c54ee2019-10-22 20:31:18 +0900228 }
Paul Duffinac37c502019-11-26 18:02:20 +0000229 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900230
Paul Duffin13ad94f2020-02-19 16:19:27 +0000231 members, multilib := s.collectMembers(ctx)
232 for _, member := range members {
Paul Duffin13879572019-11-28 14:31:38 +0000233 member.memberType.BuildSnapshot(ctx, builder, member)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900234 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900235
Paul Duffine6c0d842020-01-15 14:08:51 +0000236 // Create a transformer that will transform an unversioned module into a versioned module.
237 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
238
Paul Duffin72910952020-01-20 18:16:30 +0000239 // Create a transformer that will transform an unversioned module by replacing any references
240 // to internal members with a unique module name and setting prefer: false.
241 unversionedTransformer := unversionedTransformation{builder: builder}
242
Paul Duffinb645ec82019-11-27 17:43:54 +0000243 for _, unversioned := range builder.prebuiltOrder {
244 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000245 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000246
247 // Transform the unversioned module into a versioned one.
248 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000249 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000250
Paul Duffin72910952020-01-20 18:16:30 +0000251 // Transform the unversioned module to make it suitable for use in the snapshot.
252 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000253 bpFile.AddModule(unversioned)
254 }
255
256 // Create the snapshot module.
257 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000258 var snapshotModuleType string
259 if s.properties.Module_exports {
260 snapshotModuleType = "module_exports_snapshot"
261 } else {
262 snapshotModuleType = "sdk_snapshot"
263 }
264 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000265 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000266
267 // Make sure that the snapshot has the same visibility as the sdk.
268 visibility := android.EffectiveVisibilityRules(ctx, s)
269 if len(visibility) != 0 {
270 snapshotModule.AddProperty("visibility", visibility)
271 }
272
Paul Duffine44358f2019-11-26 18:04:12 +0000273 addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000274
275 // Compile_multilib defaults to both and must always be set to both on the
276 // device and so only needs to be set when targeted at the host and is neither
277 // unspecified or both.
278 if s.HostSupported() && multilib != "" && multilib != "both" {
279 targetSet := snapshotModule.AddPropertySet("target")
280 hostSet := targetSet.AddPropertySet("host")
281 hostSet.AddProperty("compile_multilib", multilib)
282 }
283
Paul Duffin72910952020-01-20 18:16:30 +0000284 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin255f18e2019-12-13 11:22:16 +0000285 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000286 if len(names) > 0 {
Paul Duffin255f18e2019-12-13 11:22:16 +0000287 snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names))
Paul Duffin13879572019-11-28 14:31:38 +0000288 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000289 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000290 bpFile.AddModule(snapshotModule)
291
292 // generate Android.bp
293 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
294 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000295
296 bp.build(pctx, ctx, nil)
297
298 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900299
Jiyong Park232e7852019-11-04 12:23:40 +0900300 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000301 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000302 outputDesc := "Building snapshot for " + ctx.ModuleName()
303
304 // If there are no zips to merge then generate the output zip directly.
305 // Otherwise, generate an intermediate zip file into which other zips can be
306 // merged.
307 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000308 var desc string
309 if len(builder.zipsToMerge) == 0 {
310 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000311 desc = outputDesc
312 } else {
313 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000314 desc = "Building intermediate snapshot for " + ctx.ModuleName()
315 }
316
Paul Duffin375058f2019-11-29 20:17:53 +0000317 ctx.Build(pctx, android.BuildParams{
318 Description: desc,
319 Rule: zipFiles,
320 Inputs: filesToZip,
321 Output: zipFile,
322 Args: map[string]string{
323 "basedir": builder.snapshotDir.String(),
324 },
325 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900326
Paul Duffin91547182019-11-12 19:39:36 +0000327 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000328 ctx.Build(pctx, android.BuildParams{
329 Description: outputDesc,
330 Rule: mergeZips,
331 Input: zipFile,
332 Inputs: builder.zipsToMerge,
333 Output: outputZipFile,
334 })
Paul Duffin91547182019-11-12 19:39:36 +0000335 }
336
337 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900338}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000339
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000340type propertyTag struct {
341 name string
342}
343
344var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"}
345
Paul Duffine6c0d842020-01-15 14:08:51 +0000346type unversionedToVersionedTransformation struct {
347 identityTransformation
348 builder *snapshotBuilder
349}
350
Paul Duffine6c0d842020-01-15 14:08:51 +0000351func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
352 // Use a versioned name for the module but remember the original name for the
353 // snapshot.
354 name := module.getValue("name").(string)
355 module.setProperty("name", t.builder.versionedSdkMemberName(name))
356 module.insertAfter("name", "sdk_member_name", name)
357 return module
358}
359
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000360func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
361 if tag == sdkMemberReferencePropertyTag {
362 return t.builder.versionedSdkMemberNames(value.([]string)), tag
363 } else {
364 return value, tag
365 }
366}
367
Paul Duffin72910952020-01-20 18:16:30 +0000368type unversionedTransformation struct {
369 identityTransformation
370 builder *snapshotBuilder
371}
372
373func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
374 // If the module is an internal member then use a unique name for it.
375 name := module.getValue("name").(string)
376 module.setProperty("name", t.builder.unversionedSdkMemberName(name))
377
378 // Set prefer: false - this is not strictly required as that is the default.
379 module.insertAfter("name", "prefer", false)
380
381 return module
382}
383
384func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
385 if tag == sdkMemberReferencePropertyTag {
386 return t.builder.unversionedSdkMemberNames(value.([]string)), tag
387 } else {
388 return value, tag
389 }
390}
391
Paul Duffinb645ec82019-11-27 17:43:54 +0000392func generateBpContents(contents *generatedContents, bpFile *bpFile) {
393 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
394 for _, bpModule := range bpFile.order {
395 contents.Printfln("")
396 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000397 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000398 contents.Printfln("}")
399 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000400}
401
402func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
403 contents.Indent()
404 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000405 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000406
407 reflectedValue := reflect.ValueOf(value)
408 t := reflectedValue.Type()
409
410 kind := t.Kind()
411 switch kind {
412 case reflect.Slice:
413 length := reflectedValue.Len()
414 if length > 1 {
415 contents.Printfln("%s: [", name)
416 contents.Indent()
417 for i := 0; i < length; i = i + 1 {
418 contents.Printfln("%q,", reflectedValue.Index(i).Interface())
419 }
420 contents.Dedent()
421 contents.Printfln("],")
422 } else if length == 0 {
423 contents.Printfln("%s: [],", name)
424 } else {
425 contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface())
426 }
427 case reflect.Bool:
428 contents.Printfln("%s: %t,", name, reflectedValue.Bool())
429
430 case reflect.Ptr:
431 contents.Printfln("%s: {", name)
432 outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet))
433 contents.Printfln("},")
434
435 default:
436 contents.Printfln("%s: %q,", name, value)
437 }
438 }
439 contents.Dedent()
440}
441
Paul Duffinac37c502019-11-26 18:02:20 +0000442func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000443 contents := &generatedContents{}
444 generateBpContents(contents, s.builderForTests.bpFile)
445 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000446}
447
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000448type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000449 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000450 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000451 version string
452 snapshotDir android.OutputPath
453 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000454
455 // Map from destination to source of each copy - used to eliminate duplicates and
456 // detect conflicts.
457 copies map[string]string
458
Paul Duffinb645ec82019-11-27 17:43:54 +0000459 filesToZip android.Paths
460 zipsToMerge android.Paths
461
462 prebuiltModules map[string]*bpModule
463 prebuiltOrder []*bpModule
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000464}
465
466func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000467 if existing, ok := s.copies[dest]; ok {
468 if existing != src.String() {
469 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
470 return
471 }
472 } else {
473 path := s.snapshotDir.Join(s.ctx, dest)
474 s.ctx.Build(pctx, android.BuildParams{
475 Rule: android.Cp,
476 Input: src,
477 Output: path,
478 })
479 s.filesToZip = append(s.filesToZip, path)
480
481 s.copies[dest] = src.String()
482 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000483}
484
Paul Duffin91547182019-11-12 19:39:36 +0000485func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
486 ctx := s.ctx
487
488 // Repackage the zip file so that the entries are in the destDir directory.
489 // This will allow the zip file to be merged into the snapshot.
490 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000491
492 ctx.Build(pctx, android.BuildParams{
493 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
494 Rule: repackageZip,
495 Input: zipPath,
496 Output: tmpZipPath,
497 Args: map[string]string{
498 "destdir": destDir,
499 },
500 })
Paul Duffin91547182019-11-12 19:39:36 +0000501
502 // Add the repackaged zip file to the files to merge.
503 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
504}
505
Paul Duffin9d8d6092019-12-05 18:19:29 +0000506func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
507 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000508 if s.prebuiltModules[name] != nil {
509 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
510 }
511
512 m := s.bpFile.newModule(moduleType)
513 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000514
Paul Duffin72910952020-01-20 18:16:30 +0000515 if s.sdk.isInternalMember(name) {
516 // An internal member is only referenced from the sdk snapshot which is in the
517 // same package so can be marked as private.
518 m.AddProperty("visibility", []string{"//visibility:private"})
519 } else {
520 // Extract visibility information from a member variant. All variants have the same
521 // visibility so it doesn't matter which one is used.
522 visibility := android.EffectiveVisibilityRules(s.ctx, member.Variants()[0])
523 if len(visibility) != 0 {
524 m.AddProperty("visibility", visibility)
525 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000526 }
527
Paul Duffine44358f2019-11-26 18:04:12 +0000528 addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000529
530 s.prebuiltModules[name] = m
531 s.prebuiltOrder = append(s.prebuiltOrder, m)
532 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000533}
534
Paul Duffine44358f2019-11-26 18:04:12 +0000535func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) {
536 if !module.DeviceSupported() {
537 bpModule.AddProperty("device_supported", false)
538 }
539 if module.HostSupported() {
540 bpModule.AddProperty("host_supported", true)
541 }
542}
543
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000544func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag {
545 return sdkMemberReferencePropertyTag
546}
547
Paul Duffinb645ec82019-11-27 17:43:54 +0000548// Get a versioned name appropriate for the SDK snapshot version being taken.
549func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000550 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
551}
Paul Duffinb645ec82019-11-27 17:43:54 +0000552
553func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string {
554 var references []string = nil
555 for _, m := range members {
556 references = append(references, s.versionedSdkMemberName(m))
557 }
558 return references
559}
Paul Duffin13879572019-11-28 14:31:38 +0000560
Paul Duffin72910952020-01-20 18:16:30 +0000561// Get an internal name unique to the sdk.
562func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string {
563 if s.sdk.isInternalMember(unversionedName) {
564 return s.ctx.ModuleName() + "_" + unversionedName
565 } else {
566 return unversionedName
567 }
568}
569
570func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string {
571 var references []string = nil
572 for _, m := range members {
573 references = append(references, s.unversionedSdkMemberName(m))
574 }
575 return references
576}
577
Paul Duffin13879572019-11-28 14:31:38 +0000578var _ android.SdkMember = (*sdkMember)(nil)
579
580type sdkMember struct {
581 memberType android.SdkMemberType
582 name string
583 variants []android.SdkAware
584}
585
586func (m *sdkMember) Name() string {
587 return m.name
588}
589
590func (m *sdkMember) Variants() []android.SdkAware {
591 return m.variants
592}