blob: 282a7a4cd6d0563f3931368eb0af0b58604f875c [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"
Paul Duffina04c1072020-03-02 10:16:35 +000020 "sort"
Jiyong Park9b409bc2019-10-11 14:59:13 +090021 "strings"
22
Paul Duffin375058f2019-11-29 20:17:53 +000023 "github.com/google/blueprint"
Jiyong Park9b409bc2019-10-11 14:59:13 +090024 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
Jiyong Park9b409bc2019-10-11 14:59:13 +090027)
28
29var pctx = android.NewPackageContext("android/soong/sdk")
30
Paul Duffin375058f2019-11-29 20:17:53 +000031var (
32 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
33 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000034 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000035 CommandDeps: []string{
36 "${config.Zip2ZipCmd}",
37 },
38 },
39 "destdir")
40
41 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
42 blueprint.RuleParams{
43 Command: `${config.SoongZipCmd} -C $basedir -l $out.rsp -o $out`,
44 CommandDeps: []string{
45 "${config.SoongZipCmd}",
46 },
47 Rspfile: "$out.rsp",
48 RspfileContent: "$in",
49 },
50 "basedir")
51
52 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
53 blueprint.RuleParams{
54 Command: `${config.MergeZipsCmd} $out $in`,
55 CommandDeps: []string{
56 "${config.MergeZipsCmd}",
57 },
58 })
59)
60
Paul Duffinb645ec82019-11-27 17:43:54 +000061type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090062 content strings.Builder
63 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090064}
65
Paul Duffinb645ec82019-11-27 17:43:54 +000066// generatedFile abstracts operations for writing contents into a file and emit a build rule
67// for the file.
68type generatedFile struct {
69 generatedContents
70 path android.OutputPath
71}
72
Jiyong Park232e7852019-11-04 12:23:40 +090073func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +090074 return &generatedFile{
Paul Duffinb645ec82019-11-27 17:43:54 +000075 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park9b409bc2019-10-11 14:59:13 +090076 }
77}
78
Paul Duffinb645ec82019-11-27 17:43:54 +000079func (gc *generatedContents) Indent() {
80 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +090081}
82
Paul Duffinb645ec82019-11-27 17:43:54 +000083func (gc *generatedContents) Dedent() {
84 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +090085}
86
Paul Duffinb645ec82019-11-27 17:43:54 +000087func (gc *generatedContents) Printfln(format string, args ...interface{}) {
Jiyong Park9b409bc2019-10-11 14:59:13 +090088 // ninja consumes newline characters in rspfile_content. Prevent it by
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000089 // escaping the backslash in the newline character. The extra backslash
Jiyong Park9b409bc2019-10-11 14:59:13 +090090 // is removed when the rspfile is written to the actual script file
Paul Duffinb645ec82019-11-27 17:43:54 +000091 fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\\n", args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +090092}
93
94func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
95 rb := android.NewRuleBuilder()
96 // convert \\n to \n
97 rb.Command().
98 Implicits(implicits).
99 Text("echo").Text(proptools.ShellEscape(gf.content.String())).
100 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
101 rb.Command().
102 Text("chmod a+x").Output(gf.path)
103 rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base())
104}
105
Paul Duffin13879572019-11-28 14:31:38 +0000106// Collect all the members.
107//
Paul Duffin1356d8c2020-02-25 19:26:33 +0000108// Returns a list containing type (extracted from the dependency tag) and the variant.
109func (s *sdk) collectMembers(ctx android.ModuleContext) []sdkMemberRef {
110 var memberRefs []sdkMemberRef
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000111 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
112 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf8539922019-11-19 19:44:10 +0000113 if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok {
114 memberType := memberTag.SdkMemberType()
Jiyong Park9b409bc2019-10-11 14:59:13 +0900115
Paul Duffin13879572019-11-28 14:31:38 +0000116 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000117 if !memberType.IsInstance(child) {
118 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900119 }
Paul Duffin13879572019-11-28 14:31:38 +0000120
Paul Duffin1356d8c2020-02-25 19:26:33 +0000121 memberRefs = append(memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)})
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000122
123 // If the member type supports transitive sdk members then recurse down into
124 // its dependencies, otherwise exit traversal.
125 return memberType.HasTransitiveSdkMembers()
Jiyong Park73c54ee2019-10-22 20:31:18 +0900126 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000127
128 return false
Paul Duffin13879572019-11-28 14:31:38 +0000129 })
130
Paul Duffin1356d8c2020-02-25 19:26:33 +0000131 return memberRefs
132}
133
134// Organize the members.
135//
136// The members are first grouped by type and then grouped by name. The order of
137// the types is the order they are referenced in android.SdkMemberTypesRegistry.
138// The names are in the order in which the dependencies were added.
139//
140// Returns the members as well as the multilib setting to use.
141func (s *sdk) organizeMembers(ctx android.ModuleContext, memberRefs []sdkMemberRef) ([]*sdkMember, string) {
142 byType := make(map[android.SdkMemberType][]*sdkMember)
143 byName := make(map[string]*sdkMember)
144
145 lib32 := false // True if any of the members have 32 bit version.
146 lib64 := false // True if any of the members have 64 bit version.
147
148 for _, memberRef := range memberRefs {
149 memberType := memberRef.memberType
150 variant := memberRef.variant
151
152 name := ctx.OtherModuleName(variant)
153 member := byName[name]
154 if member == nil {
155 member = &sdkMember{memberType: memberType, name: name}
156 byName[name] = member
157 byType[memberType] = append(byType[memberType], member)
158 }
159
160 multilib := variant.Target().Arch.ArchType.Multilib
161 if multilib == "lib32" {
162 lib32 = true
163 } else if multilib == "lib64" {
164 lib64 = true
165 }
166
167 // Only append new variants to the list. This is needed because a member can be both
168 // exported by the sdk and also be a transitive sdk member.
169 member.variants = appendUniqueVariants(member.variants, variant)
170 }
171
Paul Duffin13879572019-11-28 14:31:38 +0000172 var members []*sdkMember
Paul Duffin72910952020-01-20 18:16:30 +0000173 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13879572019-11-28 14:31:38 +0000174 membersOfType := byType[memberListProperty.memberType]
175 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900176 }
177
Paul Duffin13ad94f2020-02-19 16:19:27 +0000178 // Compute the setting of multilib.
179 var multilib string
180 if lib32 && lib64 {
181 multilib = "both"
182 } else if lib32 {
183 multilib = "32"
184 } else if lib64 {
185 multilib = "64"
186 }
187
188 return members, multilib
Jiyong Park73c54ee2019-10-22 20:31:18 +0900189}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900190
Paul Duffin72910952020-01-20 18:16:30 +0000191func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
192 for _, v := range variants {
193 if v == newVariant {
194 return variants
195 }
196 }
197 return append(variants, newVariant)
198}
199
Jiyong Park73c54ee2019-10-22 20:31:18 +0900200// SDK directory structure
201// <sdk_root>/
202// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
203// <api_ver>/ : below this directory are all auto-generated
204// Android.bp : definition of 'sdk_snapshot' module is here
205// aidl/
206// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
207// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900208// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900209// include/
210// bionic/libc/include/stdlib.h : an exported header file
211// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900212// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900213// <arch>/include/ : arch-specific exported headers
214// <arch>/include_gen/ : arch-specific generated headers
215// <arch>/lib/
216// libFoo.so : a stub library
217
Jiyong Park232e7852019-11-04 12:23:40 +0900218// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900219// This isn't visible to users, so could be changed in future.
220func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
221 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
222}
223
Jiyong Park232e7852019-11-04 12:23:40 +0900224// buildSnapshot is the main function in this source file. It creates rules to copy
225// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000226func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath {
227
228 var memberRefs []sdkMemberRef
229 for _, sdkVariant := range sdkVariants {
230 memberRefs = append(memberRefs, sdkVariant.memberRefs...)
231 }
232
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000233 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900234
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000235 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000236
237 bpFile := &bpFile{
238 modules: make(map[string]*bpModule),
239 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000240
241 builder := &snapshotBuilder{
Paul Duffinb645ec82019-11-27 17:43:54 +0000242 ctx: ctx,
Paul Duffine44358f2019-11-26 18:04:12 +0000243 sdk: s,
Paul Duffinb645ec82019-11-27 17:43:54 +0000244 version: "current",
245 snapshotDir: snapshotDir.OutputPath,
Paul Duffinc62a5102019-12-11 18:34:15 +0000246 copies: make(map[string]string),
Paul Duffinb645ec82019-11-27 17:43:54 +0000247 filesToZip: []android.Path{bp.path},
248 bpFile: bpFile,
249 prebuiltModules: make(map[string]*bpModule),
Jiyong Park73c54ee2019-10-22 20:31:18 +0900250 }
Paul Duffinac37c502019-11-26 18:02:20 +0000251 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900252
Paul Duffin1356d8c2020-02-25 19:26:33 +0000253 members, multilib := s.organizeMembers(ctx, memberRefs)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000254 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000255 memberType := member.memberType
256 prebuiltModule := memberType.AddPrebuiltModule(ctx, builder, member)
257 if prebuiltModule == nil {
258 // Fall back to legacy method of building a snapshot
259 memberType.BuildSnapshot(ctx, builder, member)
260 } else {
261 s.createMemberSnapshot(ctx, builder, member, prebuiltModule)
262 }
Jiyong Park73c54ee2019-10-22 20:31:18 +0900263 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900264
Paul Duffine6c0d842020-01-15 14:08:51 +0000265 // Create a transformer that will transform an unversioned module into a versioned module.
266 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
267
Paul Duffin72910952020-01-20 18:16:30 +0000268 // Create a transformer that will transform an unversioned module by replacing any references
269 // to internal members with a unique module name and setting prefer: false.
270 unversionedTransformer := unversionedTransformation{builder: builder}
271
Paul Duffinb645ec82019-11-27 17:43:54 +0000272 for _, unversioned := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000273 // Prune any empty property sets.
274 unversioned = unversioned.transform(pruneEmptySetTransformer{})
275
Paul Duffinb645ec82019-11-27 17:43:54 +0000276 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000277 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000278
279 // Transform the unversioned module into a versioned one.
280 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000281 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000282
Paul Duffin72910952020-01-20 18:16:30 +0000283 // Transform the unversioned module to make it suitable for use in the snapshot.
284 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000285 bpFile.AddModule(unversioned)
286 }
287
288 // Create the snapshot module.
289 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000290 var snapshotModuleType string
291 if s.properties.Module_exports {
292 snapshotModuleType = "module_exports_snapshot"
293 } else {
294 snapshotModuleType = "sdk_snapshot"
295 }
296 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000297 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000298
299 // Make sure that the snapshot has the same visibility as the sdk.
300 visibility := android.EffectiveVisibilityRules(ctx, s)
301 if len(visibility) != 0 {
302 snapshotModule.AddProperty("visibility", visibility)
303 }
304
Paul Duffine44358f2019-11-26 18:04:12 +0000305 addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000306
307 // Compile_multilib defaults to both and must always be set to both on the
308 // device and so only needs to be set when targeted at the host and is neither
309 // unspecified or both.
310 if s.HostSupported() && multilib != "" && multilib != "both" {
311 targetSet := snapshotModule.AddPropertySet("target")
312 hostSet := targetSet.AddPropertySet("host")
313 hostSet.AddProperty("compile_multilib", multilib)
314 }
315
Paul Duffin72910952020-01-20 18:16:30 +0000316 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin255f18e2019-12-13 11:22:16 +0000317 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000318 if len(names) > 0 {
Paul Duffin255f18e2019-12-13 11:22:16 +0000319 snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names))
Paul Duffin13879572019-11-28 14:31:38 +0000320 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000321 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000322 bpFile.AddModule(snapshotModule)
323
324 // generate Android.bp
325 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
326 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000327
328 bp.build(pctx, ctx, nil)
329
330 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900331
Jiyong Park232e7852019-11-04 12:23:40 +0900332 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000333 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000334 outputDesc := "Building snapshot for " + ctx.ModuleName()
335
336 // If there are no zips to merge then generate the output zip directly.
337 // Otherwise, generate an intermediate zip file into which other zips can be
338 // merged.
339 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000340 var desc string
341 if len(builder.zipsToMerge) == 0 {
342 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000343 desc = outputDesc
344 } else {
345 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000346 desc = "Building intermediate snapshot for " + ctx.ModuleName()
347 }
348
Paul Duffin375058f2019-11-29 20:17:53 +0000349 ctx.Build(pctx, android.BuildParams{
350 Description: desc,
351 Rule: zipFiles,
352 Inputs: filesToZip,
353 Output: zipFile,
354 Args: map[string]string{
355 "basedir": builder.snapshotDir.String(),
356 },
357 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900358
Paul Duffin91547182019-11-12 19:39:36 +0000359 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000360 ctx.Build(pctx, android.BuildParams{
361 Description: outputDesc,
362 Rule: mergeZips,
363 Input: zipFile,
364 Inputs: builder.zipsToMerge,
365 Output: outputZipFile,
366 })
Paul Duffin91547182019-11-12 19:39:36 +0000367 }
368
369 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900370}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000371
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000372type propertyTag struct {
373 name string
374}
375
376var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"}
377
Paul Duffine6c0d842020-01-15 14:08:51 +0000378type unversionedToVersionedTransformation struct {
379 identityTransformation
380 builder *snapshotBuilder
381}
382
Paul Duffine6c0d842020-01-15 14:08:51 +0000383func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
384 // Use a versioned name for the module but remember the original name for the
385 // snapshot.
386 name := module.getValue("name").(string)
387 module.setProperty("name", t.builder.versionedSdkMemberName(name))
388 module.insertAfter("name", "sdk_member_name", name)
389 return module
390}
391
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000392func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
393 if tag == sdkMemberReferencePropertyTag {
394 return t.builder.versionedSdkMemberNames(value.([]string)), tag
395 } else {
396 return value, tag
397 }
398}
399
Paul Duffin72910952020-01-20 18:16:30 +0000400type unversionedTransformation struct {
401 identityTransformation
402 builder *snapshotBuilder
403}
404
405func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
406 // If the module is an internal member then use a unique name for it.
407 name := module.getValue("name").(string)
408 module.setProperty("name", t.builder.unversionedSdkMemberName(name))
409
410 // Set prefer: false - this is not strictly required as that is the default.
411 module.insertAfter("name", "prefer", false)
412
413 return module
414}
415
416func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
417 if tag == sdkMemberReferencePropertyTag {
418 return t.builder.unversionedSdkMemberNames(value.([]string)), tag
419 } else {
420 return value, tag
421 }
422}
423
Paul Duffina78f3a72020-02-21 16:29:35 +0000424type pruneEmptySetTransformer struct {
425 identityTransformation
426}
427
428var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
429
430func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
431 if len(propertySet.properties) == 0 {
432 return nil, nil
433 } else {
434 return propertySet, tag
435 }
436}
437
Paul Duffinb645ec82019-11-27 17:43:54 +0000438func generateBpContents(contents *generatedContents, bpFile *bpFile) {
439 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
440 for _, bpModule := range bpFile.order {
441 contents.Printfln("")
442 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000443 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000444 contents.Printfln("}")
445 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000446}
447
448func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
449 contents.Indent()
450 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000451 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000452
453 reflectedValue := reflect.ValueOf(value)
454 t := reflectedValue.Type()
455
456 kind := t.Kind()
457 switch kind {
458 case reflect.Slice:
459 length := reflectedValue.Len()
460 if length > 1 {
461 contents.Printfln("%s: [", name)
462 contents.Indent()
463 for i := 0; i < length; i = i + 1 {
464 contents.Printfln("%q,", reflectedValue.Index(i).Interface())
465 }
466 contents.Dedent()
467 contents.Printfln("],")
468 } else if length == 0 {
469 contents.Printfln("%s: [],", name)
470 } else {
471 contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface())
472 }
473 case reflect.Bool:
474 contents.Printfln("%s: %t,", name, reflectedValue.Bool())
475
476 case reflect.Ptr:
477 contents.Printfln("%s: {", name)
478 outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet))
479 contents.Printfln("},")
480
481 default:
482 contents.Printfln("%s: %q,", name, value)
483 }
484 }
485 contents.Dedent()
486}
487
Paul Duffinac37c502019-11-26 18:02:20 +0000488func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000489 contents := &generatedContents{}
490 generateBpContents(contents, s.builderForTests.bpFile)
491 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000492}
493
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000494type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000495 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000496 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000497 version string
498 snapshotDir android.OutputPath
499 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000500
501 // Map from destination to source of each copy - used to eliminate duplicates and
502 // detect conflicts.
503 copies map[string]string
504
Paul Duffinb645ec82019-11-27 17:43:54 +0000505 filesToZip android.Paths
506 zipsToMerge android.Paths
507
508 prebuiltModules map[string]*bpModule
509 prebuiltOrder []*bpModule
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000510}
511
512func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000513 if existing, ok := s.copies[dest]; ok {
514 if existing != src.String() {
515 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
516 return
517 }
518 } else {
519 path := s.snapshotDir.Join(s.ctx, dest)
520 s.ctx.Build(pctx, android.BuildParams{
521 Rule: android.Cp,
522 Input: src,
523 Output: path,
524 })
525 s.filesToZip = append(s.filesToZip, path)
526
527 s.copies[dest] = src.String()
528 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000529}
530
Paul Duffin91547182019-11-12 19:39:36 +0000531func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
532 ctx := s.ctx
533
534 // Repackage the zip file so that the entries are in the destDir directory.
535 // This will allow the zip file to be merged into the snapshot.
536 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000537
538 ctx.Build(pctx, android.BuildParams{
539 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
540 Rule: repackageZip,
541 Input: zipPath,
542 Output: tmpZipPath,
543 Args: map[string]string{
544 "destdir": destDir,
545 },
546 })
Paul Duffin91547182019-11-12 19:39:36 +0000547
548 // Add the repackaged zip file to the files to merge.
549 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
550}
551
Paul Duffin9d8d6092019-12-05 18:19:29 +0000552func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
553 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000554 if s.prebuiltModules[name] != nil {
555 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
556 }
557
558 m := s.bpFile.newModule(moduleType)
559 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000560
Paul Duffinbefa4b92020-03-04 14:22:45 +0000561 variant := member.Variants()[0]
562
Paul Duffin72910952020-01-20 18:16:30 +0000563 if s.sdk.isInternalMember(name) {
564 // An internal member is only referenced from the sdk snapshot which is in the
565 // same package so can be marked as private.
566 m.AddProperty("visibility", []string{"//visibility:private"})
567 } else {
568 // Extract visibility information from a member variant. All variants have the same
569 // visibility so it doesn't matter which one is used.
Paul Duffinbefa4b92020-03-04 14:22:45 +0000570 visibility := android.EffectiveVisibilityRules(s.ctx, variant)
Paul Duffin72910952020-01-20 18:16:30 +0000571 if len(visibility) != 0 {
572 m.AddProperty("visibility", visibility)
573 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000574 }
575
Paul Duffine44358f2019-11-26 18:04:12 +0000576 addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000577
Paul Duffinbefa4b92020-03-04 14:22:45 +0000578 // Where available copy apex_available properties from the member.
579 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
580 apexAvailable := apexAware.ApexAvailable()
581 if len(apexAvailable) > 0 {
582 m.AddProperty("apex_available", apexAvailable)
583 }
584 }
585
Paul Duffinb645ec82019-11-27 17:43:54 +0000586 s.prebuiltModules[name] = m
587 s.prebuiltOrder = append(s.prebuiltOrder, m)
588 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000589}
590
Paul Duffine44358f2019-11-26 18:04:12 +0000591func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) {
592 if !module.DeviceSupported() {
593 bpModule.AddProperty("device_supported", false)
594 }
595 if module.HostSupported() {
596 bpModule.AddProperty("host_supported", true)
597 }
598}
599
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000600func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag {
601 return sdkMemberReferencePropertyTag
602}
603
Paul Duffinb645ec82019-11-27 17:43:54 +0000604// Get a versioned name appropriate for the SDK snapshot version being taken.
605func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000606 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
607}
Paul Duffinb645ec82019-11-27 17:43:54 +0000608
609func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string {
610 var references []string = nil
611 for _, m := range members {
612 references = append(references, s.versionedSdkMemberName(m))
613 }
614 return references
615}
Paul Duffin13879572019-11-28 14:31:38 +0000616
Paul Duffin72910952020-01-20 18:16:30 +0000617// Get an internal name unique to the sdk.
618func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string {
619 if s.sdk.isInternalMember(unversionedName) {
620 return s.ctx.ModuleName() + "_" + unversionedName
621 } else {
622 return unversionedName
623 }
624}
625
626func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string {
627 var references []string = nil
628 for _, m := range members {
629 references = append(references, s.unversionedSdkMemberName(m))
630 }
631 return references
632}
633
Paul Duffin1356d8c2020-02-25 19:26:33 +0000634type sdkMemberRef struct {
635 memberType android.SdkMemberType
636 variant android.SdkAware
637}
638
Paul Duffin13879572019-11-28 14:31:38 +0000639var _ android.SdkMember = (*sdkMember)(nil)
640
641type sdkMember struct {
642 memberType android.SdkMemberType
643 name string
644 variants []android.SdkAware
645}
646
647func (m *sdkMember) Name() string {
648 return m.name
649}
650
651func (m *sdkMember) Variants() []android.SdkAware {
652 return m.variants
653}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000654
655type baseInfo struct {
656 Properties android.SdkMemberProperties
657}
658
659type osTypeSpecificInfo struct {
660 baseInfo
661
662 // The list of arch type specific info for this os type.
663 archTypes []*archTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +0000664
665 // True if the member has common arch variants for this os type.
666 commonArch bool
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000667}
668
669type archTypeSpecificInfo struct {
670 baseInfo
671
672 archType android.ArchType
673}
674
675func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) {
676
677 memberType := member.memberType
678
Paul Duffina04c1072020-03-02 10:16:35 +0000679 // Group the variants by os type.
680 variantsByOsType := make(map[android.OsType][]android.SdkAware)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000681 variants := member.Variants()
682 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +0000683 osType := variant.Target().Os
684 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000685 }
686
Paul Duffina04c1072020-03-02 10:16:35 +0000687 osCount := len(variantsByOsType)
688 createVariantPropertiesStruct := func(os android.OsType) android.SdkMemberProperties {
689 properties := memberType.CreateVariantPropertiesStruct()
690 base := properties.Base()
691 base.Os_count = osCount
692 base.Os = os
693 return properties
694 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000695
Paul Duffina04c1072020-03-02 10:16:35 +0000696 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +0000697
Paul Duffina04c1072020-03-02 10:16:35 +0000698 // The set of properties that are common across all architectures and os types.
699 commonProperties := createVariantPropertiesStruct(android.CommonOS)
700
701 // The list of property structures which are os type specific but common across
702 // architectures within that os type.
703 var osSpecificPropertiesList []android.SdkMemberProperties
704
705 for osType, osTypeVariants := range variantsByOsType {
706 // Group the properties for each variant by arch type within the os.
707 osInfo := &osTypeSpecificInfo{}
708 osTypeToInfo[osType] = osInfo
709
710 // Create a structure into which properties common across the architectures in
711 // this os type will be stored. Add it to the list of os type specific yet
712 // architecture independent properties structs.
713 osInfo.Properties = createVariantPropertiesStruct(osType)
714 osSpecificPropertiesList = append(osSpecificPropertiesList, osInfo.Properties)
715
716 commonArch := false
717 for _, variant := range osTypeVariants {
718 var properties android.SdkMemberProperties
719
720 // Get the info associated with the arch type inside the os info.
721 archType := variant.Target().Arch.ArchType
722
723 if archType.Name == "common" {
724 // The arch type is common so populate the common properties directly.
725 properties = osInfo.Properties
726
727 commonArch = true
Paul Duffin14eb4672020-03-02 11:33:02 +0000728 } else {
Paul Duffina04c1072020-03-02 10:16:35 +0000729 archInfo := &archTypeSpecificInfo{archType: archType}
730 properties = createVariantPropertiesStruct(osType)
731 archInfo.Properties = properties
732
733 osInfo.archTypes = append(osInfo.archTypes, archInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +0000734 }
Paul Duffina04c1072020-03-02 10:16:35 +0000735
736 properties.PopulateFromVariant(variant)
Paul Duffin14eb4672020-03-02 11:33:02 +0000737 }
738
Paul Duffina04c1072020-03-02 10:16:35 +0000739 if commonArch {
740 if len(osTypeVariants) != 1 {
741 panic("Expected to only have 1 variant when arch type is common but found " + string(len(variants)))
742 }
743 } else {
744 var archPropertiesList []android.SdkMemberProperties
745 for _, archInfo := range osInfo.archTypes {
746 archPropertiesList = append(archPropertiesList, archInfo.Properties)
747 }
748
749 extractCommonProperties(osInfo.Properties, archPropertiesList)
750
751 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
752 var multilib string
753 archVariantCount := len(osInfo.archTypes)
754 if archVariantCount == 2 {
755 multilib = "both"
756 } else if archVariantCount == 1 {
757 if strings.HasSuffix(osInfo.archTypes[0].archType.Name, "64") {
758 multilib = "64"
759 } else {
760 multilib = "32"
761 }
762 }
763
764 osInfo.commonArch = commonArch
765 osInfo.Properties.Base().Compile_multilib = multilib
766 }
Paul Duffin14eb4672020-03-02 11:33:02 +0000767 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000768
Paul Duffina04c1072020-03-02 10:16:35 +0000769 // Extract properties which are common across all architectures and os types.
770 extractCommonProperties(commonProperties, osSpecificPropertiesList)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000771
Paul Duffina04c1072020-03-02 10:16:35 +0000772 // Add the common properties to the module.
773 commonProperties.AddToPropertySet(sdkModuleContext, builder, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000774
Paul Duffina04c1072020-03-02 10:16:35 +0000775 // Create a target property set into which target specific properties can be
776 // added.
777 targetPropertySet := bpModule.AddPropertySet("target")
778
779 // Iterate over the os types in a fixed order.
780 for _, osType := range s.getPossibleOsTypes() {
781 osInfo := osTypeToInfo[osType]
782 if osInfo == nil {
783 continue
784 }
785
786 var osPropertySet android.BpPropertySet
787 var archOsPrefix string
788 if len(osTypeToInfo) == 1 {
789 // There is only one os type present in the variants sp don't bother
790 // with adding target specific properties.
791
792 // Create a structure that looks like:
793 // module_type {
794 // name: "...",
795 // ...
796 // <common properties>
797 // ...
798 // <single os type specific properties>
799 //
800 // arch: {
801 // <arch specific sections>
802 // }
803 //
804 osPropertySet = bpModule
805
806 // Arch specific properties need to be added to an arch specific section
807 // within arch.
808 archOsPrefix = ""
809 } else {
810 // Create a structure that looks like:
811 // module_type {
812 // name: "...",
813 // ...
814 // <common properties>
815 // ...
816 // target: {
817 // <arch independent os specific sections, e.g. android>
818 // ...
819 // <arch and os specific sections, e.g. android_x86>
820 // }
821 //
822 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
823
824 // Arch specific properties need to be added to an os and arch specific
825 // section prefixed with <os>_.
826 archOsPrefix = osType.Name + "_"
827 }
828
829 osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, osPropertySet)
830 if !osInfo.commonArch {
831 // Either add the arch specific sections into the target or arch sections
832 // depending on whether they will also be os specific.
833 var archPropertySet android.BpPropertySet
834 if archOsPrefix == "" {
835 archPropertySet = osPropertySet.AddPropertySet("arch")
836 } else {
837 archPropertySet = targetPropertySet
838 }
839
840 // Add arch (and possibly os) specific sections for each set of
841 // arch (and possibly os) specific properties.
842 for _, av := range osInfo.archTypes {
843 archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + av.archType.Name)
844
845 av.Properties.AddToPropertySet(sdkModuleContext, builder, archTypePropertySet)
846 }
Paul Duffin14eb4672020-03-02 11:33:02 +0000847 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000848 }
849
850 memberType.FinalizeModule(sdkModuleContext, builder, member, bpModule)
851}
852
Paul Duffina04c1072020-03-02 10:16:35 +0000853// Compute the list of possible os types that this sdk could support.
854func (s *sdk) getPossibleOsTypes() []android.OsType {
855 var osTypes []android.OsType
856 for _, osType := range android.OsTypeList {
857 if s.DeviceSupported() {
858 if osType.Class == android.Device && osType != android.Fuchsia {
859 osTypes = append(osTypes, osType)
860 }
861 }
862 if s.HostSupported() {
863 if osType.Class == android.Host || osType.Class == android.HostCross {
864 osTypes = append(osTypes, osType)
865 }
866 }
867 }
868 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
869 return osTypes
870}
871
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000872// Extract common properties from a slice of property structures of the same type.
873//
874// All the property structures must be of the same type.
875// commonProperties - must be a pointer to the structure into which common properties will be added.
876// inputPropertiesSlice - must be a slice of input properties structures.
877//
878// Iterates over each exported field (capitalized name) and checks to see whether they
879// have the same value (using DeepEquals) across all the input properties. If it does not then no
880// change is made. Otherwise, the common value is stored in the field in the commonProperties
881// and the field in each of the input properties structure is set to its default value.
882func extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) {
883 commonPropertiesValue := reflect.ValueOf(commonProperties)
884 commonStructValue := commonPropertiesValue.Elem()
885 propertiesStructType := commonStructValue.Type()
886
887 // Create an empty structure from which default values for the field can be copied.
888 emptyStructValue := reflect.New(propertiesStructType).Elem()
889
890 for f := 0; f < propertiesStructType.NumField(); f++ {
891 // Check to see if all the structures have the same value for the field. The commonValue
892 // is nil on entry to the loop and if it is nil on exit then there is no common value,
893 // otherwise it points to the common value.
894 var commonValue *reflect.Value
895 sliceValue := reflect.ValueOf(inputPropertiesSlice)
896
Paul Duffina04c1072020-03-02 10:16:35 +0000897 field := propertiesStructType.Field(f)
898 if field.Name == "SdkMemberPropertiesBase" {
899 continue
900 }
901
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000902 for i := 0; i < sliceValue.Len(); i++ {
903 structValue := sliceValue.Index(i).Elem().Elem()
904 fieldValue := structValue.Field(f)
905 if !fieldValue.CanInterface() {
906 // The field is not exported so ignore it.
907 continue
908 }
909
910 if commonValue == nil {
911 // Use the first value as the commonProperties value.
912 commonValue = &fieldValue
913 } else {
914 // If the value does not match the current common value then there is
915 // no value in common so break out.
916 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
917 commonValue = nil
918 break
919 }
920 }
921 }
922
923 // If the fields all have a common value then store it in the common struct field
924 // and set the input struct's field to the empty value.
925 if commonValue != nil {
926 emptyValue := emptyStructValue.Field(f)
927 commonStructValue.Field(f).Set(*commonValue)
928 for i := 0; i < sliceValue.Len(); i++ {
929 structValue := sliceValue.Index(i).Elem().Elem()
930 fieldValue := structValue.Field(f)
931 fieldValue.Set(emptyValue)
932 }
933 }
934 }
935}