blob: 0084eb76dde1a589c0e602afbd4ba8e3ddb90a12 [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 Duffin7d74e7b2020-03-06 12:30:13 +000023 "android/soong/apex"
Paul Duffin375058f2019-11-29 20:17:53 +000024 "github.com/google/blueprint"
Jiyong Park9b409bc2019-10-11 14:59:13 +090025 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
Jiyong Park9b409bc2019-10-11 14:59:13 +090028)
29
30var pctx = android.NewPackageContext("android/soong/sdk")
31
Paul Duffin375058f2019-11-29 20:17:53 +000032var (
33 repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
34 blueprint.RuleParams{
Paul Duffince482dc2019-12-09 19:58:17 +000035 Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
Paul Duffin375058f2019-11-29 20:17:53 +000036 CommandDeps: []string{
37 "${config.Zip2ZipCmd}",
38 },
39 },
40 "destdir")
41
42 zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
43 blueprint.RuleParams{
44 Command: `${config.SoongZipCmd} -C $basedir -l $out.rsp -o $out`,
45 CommandDeps: []string{
46 "${config.SoongZipCmd}",
47 },
48 Rspfile: "$out.rsp",
49 RspfileContent: "$in",
50 },
51 "basedir")
52
53 mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
54 blueprint.RuleParams{
55 Command: `${config.MergeZipsCmd} $out $in`,
56 CommandDeps: []string{
57 "${config.MergeZipsCmd}",
58 },
59 })
60)
61
Paul Duffinb645ec82019-11-27 17:43:54 +000062type generatedContents struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090063 content strings.Builder
64 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090065}
66
Paul Duffinb645ec82019-11-27 17:43:54 +000067// generatedFile abstracts operations for writing contents into a file and emit a build rule
68// for the file.
69type generatedFile struct {
70 generatedContents
71 path android.OutputPath
72}
73
Jiyong Park232e7852019-11-04 12:23:40 +090074func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +090075 return &generatedFile{
Paul Duffinb645ec82019-11-27 17:43:54 +000076 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park9b409bc2019-10-11 14:59:13 +090077 }
78}
79
Paul Duffinb645ec82019-11-27 17:43:54 +000080func (gc *generatedContents) Indent() {
81 gc.indentLevel++
Jiyong Park73c54ee2019-10-22 20:31:18 +090082}
83
Paul Duffinb645ec82019-11-27 17:43:54 +000084func (gc *generatedContents) Dedent() {
85 gc.indentLevel--
Jiyong Park73c54ee2019-10-22 20:31:18 +090086}
87
Paul Duffinb645ec82019-11-27 17:43:54 +000088func (gc *generatedContents) Printfln(format string, args ...interface{}) {
Jiyong Park9b409bc2019-10-11 14:59:13 +090089 // ninja consumes newline characters in rspfile_content. Prevent it by
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000090 // escaping the backslash in the newline character. The extra backslash
Jiyong Park9b409bc2019-10-11 14:59:13 +090091 // is removed when the rspfile is written to the actual script file
Paul Duffinb645ec82019-11-27 17:43:54 +000092 fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\\n", args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +090093}
94
95func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
96 rb := android.NewRuleBuilder()
97 // convert \\n to \n
98 rb.Command().
99 Implicits(implicits).
100 Text("echo").Text(proptools.ShellEscape(gf.content.String())).
101 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
102 rb.Command().
103 Text("chmod a+x").Output(gf.path)
104 rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base())
105}
106
Paul Duffin13879572019-11-28 14:31:38 +0000107// Collect all the members.
108//
Paul Duffin1356d8c2020-02-25 19:26:33 +0000109// Returns a list containing type (extracted from the dependency tag) and the variant.
110func (s *sdk) collectMembers(ctx android.ModuleContext) []sdkMemberRef {
111 var memberRefs []sdkMemberRef
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000112 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
113 tag := ctx.OtherModuleDependencyTag(child)
Paul Duffinf8539922019-11-19 19:44:10 +0000114 if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok {
115 memberType := memberTag.SdkMemberType()
Jiyong Park9b409bc2019-10-11 14:59:13 +0900116
Paul Duffin13879572019-11-28 14:31:38 +0000117 // Make sure that the resolved module is allowed in the member list property.
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000118 if !memberType.IsInstance(child) {
119 ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
Jiyong Park73c54ee2019-10-22 20:31:18 +0900120 }
Paul Duffin13879572019-11-28 14:31:38 +0000121
Paul Duffin1356d8c2020-02-25 19:26:33 +0000122 memberRefs = append(memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)})
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000123
124 // If the member type supports transitive sdk members then recurse down into
125 // its dependencies, otherwise exit traversal.
126 return memberType.HasTransitiveSdkMembers()
Jiyong Park73c54ee2019-10-22 20:31:18 +0900127 }
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000128
129 return false
Paul Duffin13879572019-11-28 14:31:38 +0000130 })
131
Paul Duffin1356d8c2020-02-25 19:26:33 +0000132 return memberRefs
133}
134
135// Organize the members.
136//
137// The members are first grouped by type and then grouped by name. The order of
138// the types is the order they are referenced in android.SdkMemberTypesRegistry.
139// The names are in the order in which the dependencies were added.
140//
141// Returns the members as well as the multilib setting to use.
142func (s *sdk) organizeMembers(ctx android.ModuleContext, memberRefs []sdkMemberRef) ([]*sdkMember, string) {
143 byType := make(map[android.SdkMemberType][]*sdkMember)
144 byName := make(map[string]*sdkMember)
145
146 lib32 := false // True if any of the members have 32 bit version.
147 lib64 := false // True if any of the members have 64 bit version.
148
149 for _, memberRef := range memberRefs {
150 memberType := memberRef.memberType
151 variant := memberRef.variant
152
153 name := ctx.OtherModuleName(variant)
154 member := byName[name]
155 if member == nil {
156 member = &sdkMember{memberType: memberType, name: name}
157 byName[name] = member
158 byType[memberType] = append(byType[memberType], member)
159 }
160
161 multilib := variant.Target().Arch.ArchType.Multilib
162 if multilib == "lib32" {
163 lib32 = true
164 } else if multilib == "lib64" {
165 lib64 = true
166 }
167
168 // Only append new variants to the list. This is needed because a member can be both
169 // exported by the sdk and also be a transitive sdk member.
170 member.variants = appendUniqueVariants(member.variants, variant)
171 }
172
Paul Duffin13879572019-11-28 14:31:38 +0000173 var members []*sdkMember
Paul Duffin72910952020-01-20 18:16:30 +0000174 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13879572019-11-28 14:31:38 +0000175 membersOfType := byType[memberListProperty.memberType]
176 members = append(members, membersOfType...)
Jiyong Park9b409bc2019-10-11 14:59:13 +0900177 }
178
Paul Duffin13ad94f2020-02-19 16:19:27 +0000179 // Compute the setting of multilib.
180 var multilib string
181 if lib32 && lib64 {
182 multilib = "both"
183 } else if lib32 {
184 multilib = "32"
185 } else if lib64 {
186 multilib = "64"
187 }
188
189 return members, multilib
Jiyong Park73c54ee2019-10-22 20:31:18 +0900190}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900191
Paul Duffin72910952020-01-20 18:16:30 +0000192func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
193 for _, v := range variants {
194 if v == newVariant {
195 return variants
196 }
197 }
198 return append(variants, newVariant)
199}
200
Jiyong Park73c54ee2019-10-22 20:31:18 +0900201// SDK directory structure
202// <sdk_root>/
203// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
204// <api_ver>/ : below this directory are all auto-generated
205// Android.bp : definition of 'sdk_snapshot' module is here
206// aidl/
207// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
208// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900209// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900210// include/
211// bionic/libc/include/stdlib.h : an exported header file
212// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900213// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900214// <arch>/include/ : arch-specific exported headers
215// <arch>/include_gen/ : arch-specific generated headers
216// <arch>/lib/
217// libFoo.so : a stub library
218
Jiyong Park232e7852019-11-04 12:23:40 +0900219// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900220// This isn't visible to users, so could be changed in future.
221func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
222 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
223}
224
Jiyong Park232e7852019-11-04 12:23:40 +0900225// buildSnapshot is the main function in this source file. It creates rules to copy
226// the contents (header files, stub libraries, etc) into the zip file.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000227func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath {
228
Paul Duffin865171e2020-03-02 18:38:15 +0000229 exportedMembers := make(map[string]struct{})
Paul Duffin1356d8c2020-02-25 19:26:33 +0000230 var memberRefs []sdkMemberRef
231 for _, sdkVariant := range sdkVariants {
232 memberRefs = append(memberRefs, sdkVariant.memberRefs...)
Paul Duffin865171e2020-03-02 18:38:15 +0000233
234 // Merge the exported member sets from all sdk variants.
235 for key, _ := range sdkVariant.getExportedMembers() {
236 exportedMembers[key] = struct{}{}
237 }
Paul Duffin1356d8c2020-02-25 19:26:33 +0000238 }
Paul Duffin865171e2020-03-02 18:38:15 +0000239 s.exportedMembers = exportedMembers
Paul Duffin1356d8c2020-02-25 19:26:33 +0000240
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000241 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900242
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000243 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
Paul Duffinb645ec82019-11-27 17:43:54 +0000244
245 bpFile := &bpFile{
246 modules: make(map[string]*bpModule),
247 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000248
249 builder := &snapshotBuilder{
Paul Duffinb645ec82019-11-27 17:43:54 +0000250 ctx: ctx,
Paul Duffine44358f2019-11-26 18:04:12 +0000251 sdk: s,
Paul Duffinb645ec82019-11-27 17:43:54 +0000252 version: "current",
253 snapshotDir: snapshotDir.OutputPath,
Paul Duffinc62a5102019-12-11 18:34:15 +0000254 copies: make(map[string]string),
Paul Duffinb645ec82019-11-27 17:43:54 +0000255 filesToZip: []android.Path{bp.path},
256 bpFile: bpFile,
257 prebuiltModules: make(map[string]*bpModule),
Jiyong Park73c54ee2019-10-22 20:31:18 +0900258 }
Paul Duffinac37c502019-11-26 18:02:20 +0000259 s.builderForTests = builder
Jiyong Park9b409bc2019-10-11 14:59:13 +0900260
Paul Duffin1356d8c2020-02-25 19:26:33 +0000261 members, multilib := s.organizeMembers(ctx, memberRefs)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000262 for _, member := range members {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000263 memberType := member.memberType
264 prebuiltModule := memberType.AddPrebuiltModule(ctx, builder, member)
265 if prebuiltModule == nil {
266 // Fall back to legacy method of building a snapshot
267 memberType.BuildSnapshot(ctx, builder, member)
268 } else {
269 s.createMemberSnapshot(ctx, builder, member, prebuiltModule)
270 }
Jiyong Park73c54ee2019-10-22 20:31:18 +0900271 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900272
Paul Duffine6c0d842020-01-15 14:08:51 +0000273 // Create a transformer that will transform an unversioned module into a versioned module.
274 unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
275
Paul Duffin72910952020-01-20 18:16:30 +0000276 // Create a transformer that will transform an unversioned module by replacing any references
277 // to internal members with a unique module name and setting prefer: false.
278 unversionedTransformer := unversionedTransformation{builder: builder}
279
Paul Duffinb645ec82019-11-27 17:43:54 +0000280 for _, unversioned := range builder.prebuiltOrder {
Paul Duffina78f3a72020-02-21 16:29:35 +0000281 // Prune any empty property sets.
282 unversioned = unversioned.transform(pruneEmptySetTransformer{})
283
Paul Duffinb645ec82019-11-27 17:43:54 +0000284 // Copy the unversioned module so it can be modified to make it versioned.
Paul Duffincc72e982020-01-14 15:53:11 +0000285 versioned := unversioned.deepCopy()
Paul Duffine6c0d842020-01-15 14:08:51 +0000286
287 // Transform the unversioned module into a versioned one.
288 versioned.transform(unversionedToVersionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000289 bpFile.AddModule(versioned)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000290
Paul Duffin72910952020-01-20 18:16:30 +0000291 // Transform the unversioned module to make it suitable for use in the snapshot.
292 unversioned.transform(unversionedTransformer)
Paul Duffinb645ec82019-11-27 17:43:54 +0000293 bpFile.AddModule(unversioned)
294 }
295
296 // Create the snapshot module.
297 snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
Paul Duffin8150da62019-12-16 17:21:27 +0000298 var snapshotModuleType string
299 if s.properties.Module_exports {
300 snapshotModuleType = "module_exports_snapshot"
301 } else {
302 snapshotModuleType = "sdk_snapshot"
303 }
304 snapshotModule := bpFile.newModule(snapshotModuleType)
Paul Duffinb645ec82019-11-27 17:43:54 +0000305 snapshotModule.AddProperty("name", snapshotName)
Paul Duffin593b3c92019-12-05 14:31:48 +0000306
307 // Make sure that the snapshot has the same visibility as the sdk.
308 visibility := android.EffectiveVisibilityRules(ctx, s)
309 if len(visibility) != 0 {
310 snapshotModule.AddProperty("visibility", visibility)
311 }
312
Paul Duffin865171e2020-03-02 18:38:15 +0000313 addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule)
Paul Duffin13ad94f2020-02-19 16:19:27 +0000314
315 // Compile_multilib defaults to both and must always be set to both on the
316 // device and so only needs to be set when targeted at the host and is neither
317 // unspecified or both.
Paul Duffin865171e2020-03-02 18:38:15 +0000318 targetPropertySet := snapshotModule.AddPropertySet("target")
Paul Duffin13ad94f2020-02-19 16:19:27 +0000319 if s.HostSupported() && multilib != "" && multilib != "both" {
Paul Duffin865171e2020-03-02 18:38:15 +0000320 hostSet := targetPropertySet.AddPropertySet("host")
Paul Duffin13ad94f2020-02-19 16:19:27 +0000321 hostSet.AddProperty("compile_multilib", multilib)
322 }
323
Paul Duffin865171e2020-03-02 18:38:15 +0000324 var dynamicMemberPropertiesList []interface{}
325 osTypeToMemberProperties := make(map[android.OsType]*sdk)
326 for _, sdkVariant := range sdkVariants {
327 properties := sdkVariant.dynamicMemberTypeListProperties
328 osTypeToMemberProperties[sdkVariant.Target().Os] = sdkVariant
329 dynamicMemberPropertiesList = append(dynamicMemberPropertiesList, properties)
330 }
331
332 // Extract the common lists of members into a separate struct.
333 commonDynamicMemberProperties := s.dynamicSdkMemberTypes.createMemberListProperties()
334 extractCommonProperties(commonDynamicMemberProperties, dynamicMemberPropertiesList)
335
336 // Add properties common to all os types.
337 s.addMemberPropertiesToPropertySet(builder, snapshotModule, commonDynamicMemberProperties)
338
339 // Iterate over the os types in a fixed order.
340 for _, osType := range s.getPossibleOsTypes() {
341 if sdkVariant, ok := osTypeToMemberProperties[osType]; ok {
342 osPropertySet := targetPropertySet.AddPropertySet(sdkVariant.Target().Os.Name)
343 s.addMemberPropertiesToPropertySet(builder, osPropertySet, sdkVariant.dynamicMemberTypeListProperties)
Paul Duffin13879572019-11-28 14:31:38 +0000344 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000345 }
Paul Duffin865171e2020-03-02 18:38:15 +0000346
347 // Prune any empty property sets.
348 snapshotModule.transform(pruneEmptySetTransformer{})
349
Paul Duffinb645ec82019-11-27 17:43:54 +0000350 bpFile.AddModule(snapshotModule)
351
352 // generate Android.bp
353 bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
354 generateBpContents(&bp.generatedContents, bpFile)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000355
356 bp.build(pctx, ctx, nil)
357
358 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900359
Jiyong Park232e7852019-11-04 12:23:40 +0900360 // zip them all
Paul Duffin91547182019-11-12 19:39:36 +0000361 outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000362 outputDesc := "Building snapshot for " + ctx.ModuleName()
363
364 // If there are no zips to merge then generate the output zip directly.
365 // Otherwise, generate an intermediate zip file into which other zips can be
366 // merged.
367 var zipFile android.OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000368 var desc string
369 if len(builder.zipsToMerge) == 0 {
370 zipFile = outputZipFile
Paul Duffin91547182019-11-12 19:39:36 +0000371 desc = outputDesc
372 } else {
373 zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
Paul Duffin91547182019-11-12 19:39:36 +0000374 desc = "Building intermediate snapshot for " + ctx.ModuleName()
375 }
376
Paul Duffin375058f2019-11-29 20:17:53 +0000377 ctx.Build(pctx, android.BuildParams{
378 Description: desc,
379 Rule: zipFiles,
380 Inputs: filesToZip,
381 Output: zipFile,
382 Args: map[string]string{
383 "basedir": builder.snapshotDir.String(),
384 },
385 })
Jiyong Park9b409bc2019-10-11 14:59:13 +0900386
Paul Duffin91547182019-11-12 19:39:36 +0000387 if len(builder.zipsToMerge) != 0 {
Paul Duffin375058f2019-11-29 20:17:53 +0000388 ctx.Build(pctx, android.BuildParams{
389 Description: outputDesc,
390 Rule: mergeZips,
391 Input: zipFile,
392 Inputs: builder.zipsToMerge,
393 Output: outputZipFile,
394 })
Paul Duffin91547182019-11-12 19:39:36 +0000395 }
396
397 return outputZipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900398}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000399
Paul Duffin865171e2020-03-02 18:38:15 +0000400func (s *sdk) addMemberPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, dynamicMemberTypeListProperties interface{}) {
401 for _, memberListProperty := range s.memberListProperties() {
402 names := memberListProperty.getter(dynamicMemberTypeListProperties)
403 if len(names) > 0 {
404 propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names))
405 }
406 }
407}
408
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000409type propertyTag struct {
410 name string
411}
412
Paul Duffin0cb37b92020-03-04 14:52:46 +0000413// A BpPropertyTag to add to a property that contains references to other sdk members.
414//
415// This will cause the references to be rewritten to a versioned reference in the version
416// specific instance of a snapshot module.
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000417var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"}
418
Paul Duffin0cb37b92020-03-04 14:52:46 +0000419// A BpPropertyTag that indicates the property should only be present in the versioned
420// module.
421//
422// This will cause the property to be removed from the unversioned instance of a
423// snapshot module.
424var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"}
425
Paul Duffine6c0d842020-01-15 14:08:51 +0000426type unversionedToVersionedTransformation struct {
427 identityTransformation
428 builder *snapshotBuilder
429}
430
Paul Duffine6c0d842020-01-15 14:08:51 +0000431func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
432 // Use a versioned name for the module but remember the original name for the
433 // snapshot.
434 name := module.getValue("name").(string)
435 module.setProperty("name", t.builder.versionedSdkMemberName(name))
436 module.insertAfter("name", "sdk_member_name", name)
437 return module
438}
439
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000440func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
441 if tag == sdkMemberReferencePropertyTag {
442 return t.builder.versionedSdkMemberNames(value.([]string)), tag
443 } else {
444 return value, tag
445 }
446}
447
Paul Duffin72910952020-01-20 18:16:30 +0000448type unversionedTransformation struct {
449 identityTransformation
450 builder *snapshotBuilder
451}
452
453func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
454 // If the module is an internal member then use a unique name for it.
455 name := module.getValue("name").(string)
456 module.setProperty("name", t.builder.unversionedSdkMemberName(name))
457
458 // Set prefer: false - this is not strictly required as that is the default.
459 module.insertAfter("name", "prefer", false)
460
461 return module
462}
463
464func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
465 if tag == sdkMemberReferencePropertyTag {
466 return t.builder.unversionedSdkMemberNames(value.([]string)), tag
Paul Duffin0cb37b92020-03-04 14:52:46 +0000467 } else if tag == sdkVersionedOnlyPropertyTag {
468 // The property is not allowed in the unversioned module so remove it.
469 return nil, nil
Paul Duffin72910952020-01-20 18:16:30 +0000470 } else {
471 return value, tag
472 }
473}
474
Paul Duffina78f3a72020-02-21 16:29:35 +0000475type pruneEmptySetTransformer struct {
476 identityTransformation
477}
478
479var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
480
481func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
482 if len(propertySet.properties) == 0 {
483 return nil, nil
484 } else {
485 return propertySet, tag
486 }
487}
488
Paul Duffinb645ec82019-11-27 17:43:54 +0000489func generateBpContents(contents *generatedContents, bpFile *bpFile) {
490 contents.Printfln("// This is auto-generated. DO NOT EDIT.")
491 for _, bpModule := range bpFile.order {
492 contents.Printfln("")
493 contents.Printfln("%s {", bpModule.moduleType)
Paul Duffincc72e982020-01-14 15:53:11 +0000494 outputPropertySet(contents, bpModule.bpPropertySet)
Paul Duffinb645ec82019-11-27 17:43:54 +0000495 contents.Printfln("}")
496 }
Paul Duffinb645ec82019-11-27 17:43:54 +0000497}
498
499func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
500 contents.Indent()
501 for _, name := range set.order {
Paul Duffin5b511a22020-01-15 14:23:52 +0000502 value := set.getValue(name)
Paul Duffinb645ec82019-11-27 17:43:54 +0000503
504 reflectedValue := reflect.ValueOf(value)
505 t := reflectedValue.Type()
506
507 kind := t.Kind()
508 switch kind {
509 case reflect.Slice:
510 length := reflectedValue.Len()
511 if length > 1 {
512 contents.Printfln("%s: [", name)
513 contents.Indent()
514 for i := 0; i < length; i = i + 1 {
515 contents.Printfln("%q,", reflectedValue.Index(i).Interface())
516 }
517 contents.Dedent()
518 contents.Printfln("],")
519 } else if length == 0 {
520 contents.Printfln("%s: [],", name)
521 } else {
522 contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface())
523 }
524 case reflect.Bool:
525 contents.Printfln("%s: %t,", name, reflectedValue.Bool())
526
527 case reflect.Ptr:
528 contents.Printfln("%s: {", name)
529 outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet))
530 contents.Printfln("},")
531
532 default:
533 contents.Printfln("%s: %q,", name, value)
534 }
535 }
536 contents.Dedent()
537}
538
Paul Duffinac37c502019-11-26 18:02:20 +0000539func (s *sdk) GetAndroidBpContentsForTests() string {
Paul Duffinb645ec82019-11-27 17:43:54 +0000540 contents := &generatedContents{}
541 generateBpContents(contents, s.builderForTests.bpFile)
542 return contents.content.String()
Paul Duffinac37c502019-11-26 18:02:20 +0000543}
544
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000545type snapshotBuilder struct {
Paul Duffinb645ec82019-11-27 17:43:54 +0000546 ctx android.ModuleContext
Paul Duffine44358f2019-11-26 18:04:12 +0000547 sdk *sdk
Paul Duffinb645ec82019-11-27 17:43:54 +0000548 version string
549 snapshotDir android.OutputPath
550 bpFile *bpFile
Paul Duffinc62a5102019-12-11 18:34:15 +0000551
552 // Map from destination to source of each copy - used to eliminate duplicates and
553 // detect conflicts.
554 copies map[string]string
555
Paul Duffinb645ec82019-11-27 17:43:54 +0000556 filesToZip android.Paths
557 zipsToMerge android.Paths
558
559 prebuiltModules map[string]*bpModule
560 prebuiltOrder []*bpModule
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000561}
562
563func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
Paul Duffinc62a5102019-12-11 18:34:15 +0000564 if existing, ok := s.copies[dest]; ok {
565 if existing != src.String() {
566 s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
567 return
568 }
569 } else {
570 path := s.snapshotDir.Join(s.ctx, dest)
571 s.ctx.Build(pctx, android.BuildParams{
572 Rule: android.Cp,
573 Input: src,
574 Output: path,
575 })
576 s.filesToZip = append(s.filesToZip, path)
577
578 s.copies[dest] = src.String()
579 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000580}
581
Paul Duffin91547182019-11-12 19:39:36 +0000582func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
583 ctx := s.ctx
584
585 // Repackage the zip file so that the entries are in the destDir directory.
586 // This will allow the zip file to be merged into the snapshot.
587 tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
Paul Duffin375058f2019-11-29 20:17:53 +0000588
589 ctx.Build(pctx, android.BuildParams{
590 Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
591 Rule: repackageZip,
592 Input: zipPath,
593 Output: tmpZipPath,
594 Args: map[string]string{
595 "destdir": destDir,
596 },
597 })
Paul Duffin91547182019-11-12 19:39:36 +0000598
599 // Add the repackaged zip file to the files to merge.
600 s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
601}
602
Paul Duffin9d8d6092019-12-05 18:19:29 +0000603func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
604 name := member.Name()
Paul Duffinb645ec82019-11-27 17:43:54 +0000605 if s.prebuiltModules[name] != nil {
606 panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
607 }
608
609 m := s.bpFile.newModule(moduleType)
610 m.AddProperty("name", name)
Paul Duffin593b3c92019-12-05 14:31:48 +0000611
Paul Duffinbefa4b92020-03-04 14:22:45 +0000612 variant := member.Variants()[0]
613
Paul Duffin72910952020-01-20 18:16:30 +0000614 if s.sdk.isInternalMember(name) {
615 // An internal member is only referenced from the sdk snapshot which is in the
616 // same package so can be marked as private.
617 m.AddProperty("visibility", []string{"//visibility:private"})
618 } else {
619 // Extract visibility information from a member variant. All variants have the same
620 // visibility so it doesn't matter which one is used.
Paul Duffinbefa4b92020-03-04 14:22:45 +0000621 visibility := android.EffectiveVisibilityRules(s.ctx, variant)
Paul Duffin72910952020-01-20 18:16:30 +0000622 if len(visibility) != 0 {
623 m.AddProperty("visibility", visibility)
624 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000625 }
626
Paul Duffin865171e2020-03-02 18:38:15 +0000627 deviceSupported := false
628 hostSupported := false
629
630 for _, variant := range member.Variants() {
631 osClass := variant.Target().Os.Class
632 if osClass == android.Host || osClass == android.HostCross {
633 hostSupported = true
634 } else if osClass == android.Device {
635 deviceSupported = true
636 }
637 }
638
639 addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
Paul Duffinb645ec82019-11-27 17:43:54 +0000640
Paul Duffinbefa4b92020-03-04 14:22:45 +0000641 // Where available copy apex_available properties from the member.
642 if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
643 apexAvailable := apexAware.ApexAvailable()
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000644
645 // Add in any white listed apex available settings.
646 apexAvailable = append(apexAvailable, apex.WhitelistedApexAvailable(member.Name())...)
647
Paul Duffinbefa4b92020-03-04 14:22:45 +0000648 if len(apexAvailable) > 0 {
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000649 // Remove duplicates and sort.
650 apexAvailable = android.FirstUniqueStrings(apexAvailable)
651 sort.Strings(apexAvailable)
652
Paul Duffinbefa4b92020-03-04 14:22:45 +0000653 m.AddProperty("apex_available", apexAvailable)
654 }
655 }
656
Paul Duffin0cb37b92020-03-04 14:52:46 +0000657 // Disable installation in the versioned module of those modules that are ever installable.
658 if installable, ok := variant.(interface{ EverInstallable() bool }); ok {
659 if installable.EverInstallable() {
660 m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag)
661 }
662 }
663
Paul Duffinb645ec82019-11-27 17:43:54 +0000664 s.prebuiltModules[name] = m
665 s.prebuiltOrder = append(s.prebuiltOrder, m)
666 return m
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000667}
668
Paul Duffin865171e2020-03-02 18:38:15 +0000669func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
670 if !deviceSupported {
Paul Duffine44358f2019-11-26 18:04:12 +0000671 bpModule.AddProperty("device_supported", false)
672 }
Paul Duffin865171e2020-03-02 18:38:15 +0000673 if hostSupported {
Paul Duffine44358f2019-11-26 18:04:12 +0000674 bpModule.AddProperty("host_supported", true)
675 }
676}
677
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000678func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag {
679 return sdkMemberReferencePropertyTag
680}
681
Paul Duffinb645ec82019-11-27 17:43:54 +0000682// Get a versioned name appropriate for the SDK snapshot version being taken.
683func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000684 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
685}
Paul Duffinb645ec82019-11-27 17:43:54 +0000686
687func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string {
688 var references []string = nil
689 for _, m := range members {
690 references = append(references, s.versionedSdkMemberName(m))
691 }
692 return references
693}
Paul Duffin13879572019-11-28 14:31:38 +0000694
Paul Duffin72910952020-01-20 18:16:30 +0000695// Get an internal name unique to the sdk.
696func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string {
697 if s.sdk.isInternalMember(unversionedName) {
698 return s.ctx.ModuleName() + "_" + unversionedName
699 } else {
700 return unversionedName
701 }
702}
703
704func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string {
705 var references []string = nil
706 for _, m := range members {
707 references = append(references, s.unversionedSdkMemberName(m))
708 }
709 return references
710}
711
Paul Duffin1356d8c2020-02-25 19:26:33 +0000712type sdkMemberRef struct {
713 memberType android.SdkMemberType
714 variant android.SdkAware
715}
716
Paul Duffin13879572019-11-28 14:31:38 +0000717var _ android.SdkMember = (*sdkMember)(nil)
718
719type sdkMember struct {
720 memberType android.SdkMemberType
721 name string
722 variants []android.SdkAware
723}
724
725func (m *sdkMember) Name() string {
726 return m.name
727}
728
729func (m *sdkMember) Variants() []android.SdkAware {
730 return m.variants
731}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000732
733type baseInfo struct {
734 Properties android.SdkMemberProperties
735}
736
737type osTypeSpecificInfo struct {
738 baseInfo
739
740 // The list of arch type specific info for this os type.
741 archTypes []*archTypeSpecificInfo
Paul Duffina04c1072020-03-02 10:16:35 +0000742
743 // True if the member has common arch variants for this os type.
744 commonArch bool
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000745}
746
747type archTypeSpecificInfo struct {
748 baseInfo
749
750 archType android.ArchType
751}
752
753func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) {
754
755 memberType := member.memberType
756
Paul Duffina04c1072020-03-02 10:16:35 +0000757 // Group the variants by os type.
758 variantsByOsType := make(map[android.OsType][]android.SdkAware)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000759 variants := member.Variants()
760 for _, variant := range variants {
Paul Duffina04c1072020-03-02 10:16:35 +0000761 osType := variant.Target().Os
762 variantsByOsType[osType] = append(variantsByOsType[osType], variant)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000763 }
764
Paul Duffina04c1072020-03-02 10:16:35 +0000765 osCount := len(variantsByOsType)
766 createVariantPropertiesStruct := func(os android.OsType) android.SdkMemberProperties {
767 properties := memberType.CreateVariantPropertiesStruct()
768 base := properties.Base()
769 base.Os_count = osCount
770 base.Os = os
771 return properties
772 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000773
Paul Duffina04c1072020-03-02 10:16:35 +0000774 osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +0000775
Paul Duffina04c1072020-03-02 10:16:35 +0000776 // The set of properties that are common across all architectures and os types.
777 commonProperties := createVariantPropertiesStruct(android.CommonOS)
778
779 // The list of property structures which are os type specific but common across
780 // architectures within that os type.
781 var osSpecificPropertiesList []android.SdkMemberProperties
782
783 for osType, osTypeVariants := range variantsByOsType {
784 // Group the properties for each variant by arch type within the os.
785 osInfo := &osTypeSpecificInfo{}
786 osTypeToInfo[osType] = osInfo
787
788 // Create a structure into which properties common across the architectures in
789 // this os type will be stored. Add it to the list of os type specific yet
790 // architecture independent properties structs.
791 osInfo.Properties = createVariantPropertiesStruct(osType)
792 osSpecificPropertiesList = append(osSpecificPropertiesList, osInfo.Properties)
793
794 commonArch := false
795 for _, variant := range osTypeVariants {
796 var properties android.SdkMemberProperties
797
798 // Get the info associated with the arch type inside the os info.
799 archType := variant.Target().Arch.ArchType
800
801 if archType.Name == "common" {
802 // The arch type is common so populate the common properties directly.
803 properties = osInfo.Properties
804
805 commonArch = true
Paul Duffin14eb4672020-03-02 11:33:02 +0000806 } else {
Paul Duffina04c1072020-03-02 10:16:35 +0000807 archInfo := &archTypeSpecificInfo{archType: archType}
808 properties = createVariantPropertiesStruct(osType)
809 archInfo.Properties = properties
810
811 osInfo.archTypes = append(osInfo.archTypes, archInfo)
Paul Duffin14eb4672020-03-02 11:33:02 +0000812 }
Paul Duffina04c1072020-03-02 10:16:35 +0000813
814 properties.PopulateFromVariant(variant)
Paul Duffin14eb4672020-03-02 11:33:02 +0000815 }
816
Paul Duffina04c1072020-03-02 10:16:35 +0000817 if commonArch {
818 if len(osTypeVariants) != 1 {
819 panic("Expected to only have 1 variant when arch type is common but found " + string(len(variants)))
820 }
821 } else {
822 var archPropertiesList []android.SdkMemberProperties
823 for _, archInfo := range osInfo.archTypes {
824 archPropertiesList = append(archPropertiesList, archInfo.Properties)
825 }
826
827 extractCommonProperties(osInfo.Properties, archPropertiesList)
828
829 // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
830 var multilib string
831 archVariantCount := len(osInfo.archTypes)
832 if archVariantCount == 2 {
833 multilib = "both"
834 } else if archVariantCount == 1 {
835 if strings.HasSuffix(osInfo.archTypes[0].archType.Name, "64") {
836 multilib = "64"
837 } else {
838 multilib = "32"
839 }
840 }
841
842 osInfo.commonArch = commonArch
843 osInfo.Properties.Base().Compile_multilib = multilib
844 }
Paul Duffin14eb4672020-03-02 11:33:02 +0000845 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000846
Paul Duffina04c1072020-03-02 10:16:35 +0000847 // Extract properties which are common across all architectures and os types.
848 extractCommonProperties(commonProperties, osSpecificPropertiesList)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000849
Paul Duffina04c1072020-03-02 10:16:35 +0000850 // Add the common properties to the module.
851 commonProperties.AddToPropertySet(sdkModuleContext, builder, bpModule)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000852
Paul Duffina04c1072020-03-02 10:16:35 +0000853 // Create a target property set into which target specific properties can be
854 // added.
855 targetPropertySet := bpModule.AddPropertySet("target")
856
857 // Iterate over the os types in a fixed order.
858 for _, osType := range s.getPossibleOsTypes() {
859 osInfo := osTypeToInfo[osType]
860 if osInfo == nil {
861 continue
862 }
863
864 var osPropertySet android.BpPropertySet
865 var archOsPrefix string
866 if len(osTypeToInfo) == 1 {
867 // There is only one os type present in the variants sp don't bother
868 // with adding target specific properties.
869
870 // Create a structure that looks like:
871 // module_type {
872 // name: "...",
873 // ...
874 // <common properties>
875 // ...
876 // <single os type specific properties>
877 //
878 // arch: {
879 // <arch specific sections>
880 // }
881 //
882 osPropertySet = bpModule
883
884 // Arch specific properties need to be added to an arch specific section
885 // within arch.
886 archOsPrefix = ""
887 } else {
888 // Create a structure that looks like:
889 // module_type {
890 // name: "...",
891 // ...
892 // <common properties>
893 // ...
894 // target: {
895 // <arch independent os specific sections, e.g. android>
896 // ...
897 // <arch and os specific sections, e.g. android_x86>
898 // }
899 //
900 osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
901
902 // Arch specific properties need to be added to an os and arch specific
903 // section prefixed with <os>_.
904 archOsPrefix = osType.Name + "_"
905 }
906
907 osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, osPropertySet)
908 if !osInfo.commonArch {
909 // Either add the arch specific sections into the target or arch sections
910 // depending on whether they will also be os specific.
911 var archPropertySet android.BpPropertySet
912 if archOsPrefix == "" {
913 archPropertySet = osPropertySet.AddPropertySet("arch")
914 } else {
915 archPropertySet = targetPropertySet
916 }
917
918 // Add arch (and possibly os) specific sections for each set of
919 // arch (and possibly os) specific properties.
920 for _, av := range osInfo.archTypes {
921 archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + av.archType.Name)
922
923 av.Properties.AddToPropertySet(sdkModuleContext, builder, archTypePropertySet)
924 }
Paul Duffin14eb4672020-03-02 11:33:02 +0000925 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000926 }
927
928 memberType.FinalizeModule(sdkModuleContext, builder, member, bpModule)
929}
930
Paul Duffina04c1072020-03-02 10:16:35 +0000931// Compute the list of possible os types that this sdk could support.
932func (s *sdk) getPossibleOsTypes() []android.OsType {
933 var osTypes []android.OsType
934 for _, osType := range android.OsTypeList {
935 if s.DeviceSupported() {
936 if osType.Class == android.Device && osType != android.Fuchsia {
937 osTypes = append(osTypes, osType)
938 }
939 }
940 if s.HostSupported() {
941 if osType.Class == android.Host || osType.Class == android.HostCross {
942 osTypes = append(osTypes, osType)
943 }
944 }
945 }
946 sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
947 return osTypes
948}
949
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000950// Extract common properties from a slice of property structures of the same type.
951//
952// All the property structures must be of the same type.
953// commonProperties - must be a pointer to the structure into which common properties will be added.
954// inputPropertiesSlice - must be a slice of input properties structures.
955//
956// Iterates over each exported field (capitalized name) and checks to see whether they
957// have the same value (using DeepEquals) across all the input properties. If it does not then no
958// change is made. Otherwise, the common value is stored in the field in the commonProperties
959// and the field in each of the input properties structure is set to its default value.
960func extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) {
961 commonPropertiesValue := reflect.ValueOf(commonProperties)
962 commonStructValue := commonPropertiesValue.Elem()
963 propertiesStructType := commonStructValue.Type()
964
965 // Create an empty structure from which default values for the field can be copied.
966 emptyStructValue := reflect.New(propertiesStructType).Elem()
967
968 for f := 0; f < propertiesStructType.NumField(); f++ {
969 // Check to see if all the structures have the same value for the field. The commonValue
970 // is nil on entry to the loop and if it is nil on exit then there is no common value,
971 // otherwise it points to the common value.
972 var commonValue *reflect.Value
973 sliceValue := reflect.ValueOf(inputPropertiesSlice)
974
Paul Duffina04c1072020-03-02 10:16:35 +0000975 field := propertiesStructType.Field(f)
976 if field.Name == "SdkMemberPropertiesBase" {
977 continue
978 }
979
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000980 for i := 0; i < sliceValue.Len(); i++ {
981 structValue := sliceValue.Index(i).Elem().Elem()
982 fieldValue := structValue.Field(f)
983 if !fieldValue.CanInterface() {
984 // The field is not exported so ignore it.
985 continue
986 }
987
988 if commonValue == nil {
989 // Use the first value as the commonProperties value.
990 commonValue = &fieldValue
991 } else {
992 // If the value does not match the current common value then there is
993 // no value in common so break out.
994 if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
995 commonValue = nil
996 break
997 }
998 }
999 }
1000
1001 // If the fields all have a common value then store it in the common struct field
1002 // and set the input struct's field to the empty value.
1003 if commonValue != nil {
1004 emptyValue := emptyStructValue.Field(f)
1005 commonStructValue.Field(f).Set(*commonValue)
1006 for i := 0; i < sliceValue.Len(); i++ {
1007 structValue := sliceValue.Index(i).Elem().Elem()
1008 fieldValue := structValue.Field(f)
1009 fieldValue.Set(emptyValue)
1010 }
1011 }
1012 }
1013}