blob: d211e80d74adc749d6dd29b141a528af4780ecf3 [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 {
Paul Duffina78f3a72020-02-21 16:29:35 +0000244 // Prune any empty property sets.
245 unversioned = unversioned.transform(pruneEmptySetTransformer{})
246
Paul Duffinb645ec82019-11-27 17:43:54 +0000247 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000248 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000249
250 // Transform the unversioned module into a versioned one.
251 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000252 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000253
Paul Duffin72910952020-01-20 18:16:30 +0000254 // Transform the unversioned module to make it suitable for use in the snapshot.
255 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000256 bpFile.AddModule(unversioned)
257 }
258
259 // Create the snapshot module.
260 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000261 var snapshotModuleType string
262 if s.properties.Module_exports {
263 snapshotModuleType = "module_exports_snapshot"
264 } else {
265 snapshotModuleType = "sdk_snapshot"
266 }
267 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000268 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000269
270 // Make sure that the snapshot has the same visibility as the sdk.
271 visibility := android.EffectiveVisibilityRules(ctx, s)
272 if len(visibility) != 0 {
273 snapshotModule.AddProperty("visibility", visibility)
274 }
275
Paul Duffine44358f2019-11-26 18:04:12 +0000276 addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000277
278 // Compile_multilib defaults to both and must always be set to both on the
279 // device and so only needs to be set when targeted at the host and is neither
280 // unspecified or both.
281 if s.HostSupported() && multilib != "" && multilib != "both" {
282 targetSet := snapshotModule.AddPropertySet("target")
283 hostSet := targetSet.AddPropertySet("host")
284 hostSet.AddProperty("compile_multilib", multilib)
285 }
286
Paul Duffin72910952020-01-20 18:16:30 +0000287 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin255f18e2019-12-13 11:22:16 +0000288 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000289 if len(names) > 0 {
Paul Duffin255f18e2019-12-13 11:22:16 +0000290 snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names))
Paul Duffin13879572019-11-28 14:31:38 +0000291 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000292 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000293 bpFile.AddModule(snapshotModule)
294
295 // generate Android.bp
296 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
297 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000298
299 bp.build(pctx, ctx, nil)
300
301 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900302
Jiyong Park232e7852019-11-04 12:23:40 +0900303 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000304 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000305 outputDesc := "Building snapshot for " + ctx.ModuleName()
306
307 // If there are no zips to merge then generate the output zip directly.
308 // Otherwise, generate an intermediate zip file into which other zips can be
309 // merged.
310 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000311 var desc string
312 if len(builder.zipsToMerge) == 0 {
313 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000314 desc = outputDesc
315 } else {
316 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000317 desc = "Building intermediate snapshot for " + ctx.ModuleName()
318 }
319
Paul Duffin375058f2019-11-29 20:17:53 +0000320 ctx.Build(pctx, android.BuildParams{
321 Description: desc,
322 Rule: zipFiles,
323 Inputs: filesToZip,
324 Output: zipFile,
325 Args: map[string]string{
326 "basedir": builder.snapshotDir.String(),
327 },
328 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900329
Paul Duffin91547182019-11-12 19:39:36 +0000330 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000331 ctx.Build(pctx, android.BuildParams{
332 Description: outputDesc,
333 Rule: mergeZips,
334 Input: zipFile,
335 Inputs: builder.zipsToMerge,
336 Output: outputZipFile,
337 })
Paul Duffin91547182019-11-12 19:39:36 +0000338 }
339
340 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900341}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000342
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000343type propertyTag struct {
344 name string
345}
346
347var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"}
348
Paul Duffine6c0d842020-01-15 14:08:51 +0000349type unversionedToVersionedTransformation struct {
350 identityTransformation
351 builder *snapshotBuilder
352}
353
Paul Duffine6c0d842020-01-15 14:08:51 +0000354func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
355 // Use a versioned name for the module but remember the original name for the
356 // snapshot.
357 name := module.getValue("name").(string)
358 module.setProperty("name", t.builder.versionedSdkMemberName(name))
359 module.insertAfter("name", "sdk_member_name", name)
360 return module
361}
362
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000363func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
364 if tag == sdkMemberReferencePropertyTag {
365 return t.builder.versionedSdkMemberNames(value.([]string)), tag
366 } else {
367 return value, tag
368 }
369}
370
Paul Duffin72910952020-01-20 18:16:30 +0000371type unversionedTransformation struct {
372 identityTransformation
373 builder *snapshotBuilder
374}
375
376func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
377 // If the module is an internal member then use a unique name for it.
378 name := module.getValue("name").(string)
379 module.setProperty("name", t.builder.unversionedSdkMemberName(name))
380
381 // Set prefer: false - this is not strictly required as that is the default.
382 module.insertAfter("name", "prefer", false)
383
384 return module
385}
386
387func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
388 if tag == sdkMemberReferencePropertyTag {
389 return t.builder.unversionedSdkMemberNames(value.([]string)), tag
390 } else {
391 return value, tag
392 }
393}
394
Paul Duffina78f3a72020-02-21 16:29:35 +0000395type pruneEmptySetTransformer struct {
396 identityTransformation
397}
398
399var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
400
401func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
402 if len(propertySet.properties) == 0 {
403 return nil, nil
404 } else {
405 return propertySet, tag
406 }
407}
408
Paul Duffinb645ec82019-11-27 17:43:54 +0000409func generateBpContents(contents *generatedContents, bpFile *bpFile) {
410 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
411 for _, bpModule := range bpFile.order {
412 contents.Printfln("")
413 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000414 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000415 contents.Printfln("}")
416 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000417}
418
419func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
420 contents.Indent()
421 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000422 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000423
424 reflectedValue := reflect.ValueOf(value)
425 t := reflectedValue.Type()
426
427 kind := t.Kind()
428 switch kind {
429 case reflect.Slice:
430 length := reflectedValue.Len()
431 if length > 1 {
432 contents.Printfln("%s: [", name)
433 contents.Indent()
434 for i := 0; i < length; i = i + 1 {
435 contents.Printfln("%q,", reflectedValue.Index(i).Interface())
436 }
437 contents.Dedent()
438 contents.Printfln("],")
439 } else if length == 0 {
440 contents.Printfln("%s: [],", name)
441 } else {
442 contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface())
443 }
444 case reflect.Bool:
445 contents.Printfln("%s: %t,", name, reflectedValue.Bool())
446
447 case reflect.Ptr:
448 contents.Printfln("%s: {", name)
449 outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet))
450 contents.Printfln("},")
451
452 default:
453 contents.Printfln("%s: %q,", name, value)
454 }
455 }
456 contents.Dedent()
457}
458
Paul Duffinac37c502019-11-26 18:02:20 +0000459func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000460 contents := &generatedContents{}
461 generateBpContents(contents, s.builderForTests.bpFile)
462 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000463}
464
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000465type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000466 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000467 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000468 version string
469 snapshotDir android.OutputPath
470 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000471
472 // Map from destination to source of each copy - used to eliminate duplicates and
473 // detect conflicts.
474 copies map[string]string
475
Paul Duffinb645ec82019-11-27 17:43:54 +0000476 filesToZip android.Paths
477 zipsToMerge android.Paths
478
479 prebuiltModules map[string]*bpModule
480 prebuiltOrder []*bpModule
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000481}
482
483func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000484 if existing, ok := s.copies[dest]; ok {
485 if existing != src.String() {
486 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
487 return
488 }
489 } else {
490 path := s.snapshotDir.Join(s.ctx, dest)
491 s.ctx.Build(pctx, android.BuildParams{
492 Rule: android.Cp,
493 Input: src,
494 Output: path,
495 })
496 s.filesToZip = append(s.filesToZip, path)
497
498 s.copies[dest] = src.String()
499 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000500}
501
Paul Duffin91547182019-11-12 19:39:36 +0000502func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
503 ctx := s.ctx
504
505 // Repackage the zip file so that the entries are in the destDir directory.
506 // This will allow the zip file to be merged into the snapshot.
507 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000508
509 ctx.Build(pctx, android.BuildParams{
510 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
511 Rule: repackageZip,
512 Input: zipPath,
513 Output: tmpZipPath,
514 Args: map[string]string{
515 "destdir": destDir,
516 },
517 })
Paul Duffin91547182019-11-12 19:39:36 +0000518
519 // Add the repackaged zip file to the files to merge.
520 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
521}
522
Paul Duffin9d8d6092019-12-05 18:19:29 +0000523func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
524 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000525 if s.prebuiltModules[name] != nil {
526 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
527 }
528
529 m := s.bpFile.newModule(moduleType)
530 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000531
Paul Duffin72910952020-01-20 18:16:30 +0000532 if s.sdk.isInternalMember(name) {
533 // An internal member is only referenced from the sdk snapshot which is in the
534 // same package so can be marked as private.
535 m.AddProperty("visibility", []string{"//visibility:private"})
536 } else {
537 // Extract visibility information from a member variant. All variants have the same
538 // visibility so it doesn't matter which one is used.
539 visibility := android.EffectiveVisibilityRules(s.ctx, member.Variants()[0])
540 if len(visibility) != 0 {
541 m.AddProperty("visibility", visibility)
542 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000543 }
544
Paul Duffine44358f2019-11-26 18:04:12 +0000545 addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000546
547 s.prebuiltModules[name] = m
548 s.prebuiltOrder = append(s.prebuiltOrder, m)
549 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000550}
551
Paul Duffine44358f2019-11-26 18:04:12 +0000552func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) {
553 if !module.DeviceSupported() {
554 bpModule.AddProperty("device_supported", false)
555 }
556 if module.HostSupported() {
557 bpModule.AddProperty("host_supported", true)
558 }
559}
560
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000561func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag {
562 return sdkMemberReferencePropertyTag
563}
564
Paul Duffinb645ec82019-11-27 17:43:54 +0000565// Get a versioned name appropriate for the SDK snapshot version being taken.
566func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000567 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
568}
Paul Duffinb645ec82019-11-27 17:43:54 +0000569
570func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string {
571 var references []string = nil
572 for _, m := range members {
573 references = append(references, s.versionedSdkMemberName(m))
574 }
575 return references
576}
Paul Duffin13879572019-11-28 14:31:38 +0000577
Paul Duffin72910952020-01-20 18:16:30 +0000578// Get an internal name unique to the sdk.
579func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string {
580 if s.sdk.isInternalMember(unversionedName) {
581 return s.ctx.ModuleName() + "_" + unversionedName
582 } else {
583 return unversionedName
584 }
585}
586
587func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string {
588 var references []string = nil
589 for _, m := range members {
590 references = append(references, s.unversionedSdkMemberName(m))
591 }
592 return references
593}
594
Paul Duffin13879572019-11-28 14:31:38 +0000595var _ android.SdkMember = (*sdkMember)(nil)
596
597type sdkMember struct {
598 memberType android.SdkMemberType
599 name string
600 variants []android.SdkAware
601}
602
603func (m *sdkMember) Name() string {
604 return m.name
605}
606
607func (m *sdkMember) Variants() []android.SdkAware {
608 return m.variants
609}