Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 19 | "reflect" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | |
| 25 | "android/soong/android" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var pctx = android.NewPackageContext("android/soong/sdk") |
| 29 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 30 | var ( |
| 31 | repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip", |
| 32 | blueprint.RuleParams{ |
Paul Duffin | ce482dc | 2019-12-09 19:58:17 +0000 | [diff] [blame] | 33 | Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`, |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 34 | 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 Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 60 | type generatedContents struct { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 61 | content strings.Builder |
| 62 | indentLevel int |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 65 | // generatedFile abstracts operations for writing contents into a file and emit a build rule |
| 66 | // for the file. |
| 67 | type generatedFile struct { |
| 68 | generatedContents |
| 69 | path android.OutputPath |
| 70 | } |
| 71 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 72 | func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 73 | return &generatedFile{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 74 | path: android.PathForModuleOut(ctx, path...).OutputPath, |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 78 | func (gc *generatedContents) Indent() { |
| 79 | gc.indentLevel++ |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 80 | } |
| 81 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 82 | func (gc *generatedContents) Dedent() { |
| 83 | gc.indentLevel-- |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 84 | } |
| 85 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 86 | func (gc *generatedContents) Printfln(format string, args ...interface{}) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 87 | // ninja consumes newline characters in rspfile_content. Prevent it by |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 88 | // escaping the backslash in the newline character. The extra backslash |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 89 | // is removed when the rspfile is written to the actual script file |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 90 | fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\\n", args...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func (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 Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 105 | // Collect all the members. |
| 106 | // |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 107 | // Returns a list containing type (extracted from the dependency tag) and the variant. |
| 108 | func (s *sdk) collectMembers(ctx android.ModuleContext) []sdkMemberRef { |
| 109 | var memberRefs []sdkMemberRef |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 110 | ctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 111 | tag := ctx.OtherModuleDependencyTag(child) |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 112 | if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok { |
| 113 | memberType := memberTag.SdkMemberType() |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 114 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 115 | // Make sure that the resolved module is allowed in the member list property. |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 116 | if !memberType.IsInstance(child) { |
| 117 | ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName()) |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 118 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 119 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 120 | memberRefs = append(memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)}) |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 121 | |
| 122 | // If the member type supports transitive sdk members then recurse down into |
| 123 | // its dependencies, otherwise exit traversal. |
| 124 | return memberType.HasTransitiveSdkMembers() |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 125 | } |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 126 | |
| 127 | return false |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 128 | }) |
| 129 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 130 | return memberRefs |
| 131 | } |
| 132 | |
| 133 | // Organize the members. |
| 134 | // |
| 135 | // The members are first grouped by type and then grouped by name. The order of |
| 136 | // the types is the order they are referenced in android.SdkMemberTypesRegistry. |
| 137 | // The names are in the order in which the dependencies were added. |
| 138 | // |
| 139 | // Returns the members as well as the multilib setting to use. |
| 140 | func (s *sdk) organizeMembers(ctx android.ModuleContext, memberRefs []sdkMemberRef) ([]*sdkMember, string) { |
| 141 | byType := make(map[android.SdkMemberType][]*sdkMember) |
| 142 | byName := make(map[string]*sdkMember) |
| 143 | |
| 144 | lib32 := false // True if any of the members have 32 bit version. |
| 145 | lib64 := false // True if any of the members have 64 bit version. |
| 146 | |
| 147 | for _, memberRef := range memberRefs { |
| 148 | memberType := memberRef.memberType |
| 149 | variant := memberRef.variant |
| 150 | |
| 151 | name := ctx.OtherModuleName(variant) |
| 152 | member := byName[name] |
| 153 | if member == nil { |
| 154 | member = &sdkMember{memberType: memberType, name: name} |
| 155 | byName[name] = member |
| 156 | byType[memberType] = append(byType[memberType], member) |
| 157 | } |
| 158 | |
| 159 | multilib := variant.Target().Arch.ArchType.Multilib |
| 160 | if multilib == "lib32" { |
| 161 | lib32 = true |
| 162 | } else if multilib == "lib64" { |
| 163 | lib64 = true |
| 164 | } |
| 165 | |
| 166 | // Only append new variants to the list. This is needed because a member can be both |
| 167 | // exported by the sdk and also be a transitive sdk member. |
| 168 | member.variants = appendUniqueVariants(member.variants, variant) |
| 169 | } |
| 170 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 171 | var members []*sdkMember |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 172 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 173 | membersOfType := byType[memberListProperty.memberType] |
| 174 | members = append(members, membersOfType...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 177 | // Compute the setting of multilib. |
| 178 | var multilib string |
| 179 | if lib32 && lib64 { |
| 180 | multilib = "both" |
| 181 | } else if lib32 { |
| 182 | multilib = "32" |
| 183 | } else if lib64 { |
| 184 | multilib = "64" |
| 185 | } |
| 186 | |
| 187 | return members, multilib |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 188 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 189 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 190 | func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware { |
| 191 | for _, v := range variants { |
| 192 | if v == newVariant { |
| 193 | return variants |
| 194 | } |
| 195 | } |
| 196 | return append(variants, newVariant) |
| 197 | } |
| 198 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 199 | // SDK directory structure |
| 200 | // <sdk_root>/ |
| 201 | // Android.bp : definition of a 'sdk' module is here. This is a hand-made one. |
| 202 | // <api_ver>/ : below this directory are all auto-generated |
| 203 | // Android.bp : definition of 'sdk_snapshot' module is here |
| 204 | // aidl/ |
| 205 | // frameworks/base/core/..../IFoo.aidl : an exported AIDL file |
| 206 | // java/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 207 | // <module_name>.jar : the stub jar for a java library 'module_name' |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 208 | // include/ |
| 209 | // bionic/libc/include/stdlib.h : an exported header file |
| 210 | // include_gen/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 211 | // <module_name>/com/android/.../IFoo.h : a generated header file |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 212 | // <arch>/include/ : arch-specific exported headers |
| 213 | // <arch>/include_gen/ : arch-specific generated headers |
| 214 | // <arch>/lib/ |
| 215 | // libFoo.so : a stub library |
| 216 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 217 | // A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 218 | // This isn't visible to users, so could be changed in future. |
| 219 | func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string { |
| 220 | return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version |
| 221 | } |
| 222 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 223 | // buildSnapshot is the main function in this source file. It creates rules to copy |
| 224 | // the contents (header files, stub libraries, etc) into the zip file. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 225 | func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath { |
| 226 | |
| 227 | var memberRefs []sdkMemberRef |
| 228 | for _, sdkVariant := range sdkVariants { |
| 229 | memberRefs = append(memberRefs, sdkVariant.memberRefs...) |
| 230 | } |
| 231 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 232 | snapshotDir := android.PathForModuleOut(ctx, "snapshot") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 233 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 234 | bp := newGeneratedFile(ctx, "snapshot", "Android.bp") |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 235 | |
| 236 | bpFile := &bpFile{ |
| 237 | modules: make(map[string]*bpModule), |
| 238 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 239 | |
| 240 | builder := &snapshotBuilder{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 241 | ctx: ctx, |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 242 | sdk: s, |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 243 | version: "current", |
| 244 | snapshotDir: snapshotDir.OutputPath, |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 245 | copies: make(map[string]string), |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 246 | filesToZip: []android.Path{bp.path}, |
| 247 | bpFile: bpFile, |
| 248 | prebuiltModules: make(map[string]*bpModule), |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 249 | } |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 250 | s.builderForTests = builder |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 251 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 252 | members, multilib := s.organizeMembers(ctx, memberRefs) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 253 | for _, member := range members { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame^] | 254 | memberType := member.memberType |
| 255 | prebuiltModule := memberType.AddPrebuiltModule(ctx, builder, member) |
| 256 | if prebuiltModule == nil { |
| 257 | // Fall back to legacy method of building a snapshot |
| 258 | memberType.BuildSnapshot(ctx, builder, member) |
| 259 | } else { |
| 260 | s.createMemberSnapshot(ctx, builder, member, prebuiltModule) |
| 261 | } |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 262 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 263 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 264 | // Create a transformer that will transform an unversioned module into a versioned module. |
| 265 | unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder} |
| 266 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 267 | // Create a transformer that will transform an unversioned module by replacing any references |
| 268 | // to internal members with a unique module name and setting prefer: false. |
| 269 | unversionedTransformer := unversionedTransformation{builder: builder} |
| 270 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 271 | for _, unversioned := range builder.prebuiltOrder { |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 272 | // Prune any empty property sets. |
| 273 | unversioned = unversioned.transform(pruneEmptySetTransformer{}) |
| 274 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 275 | // Copy the unversioned module so it can be modified to make it versioned. |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 276 | versioned := unversioned.deepCopy() |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 277 | |
| 278 | // Transform the unversioned module into a versioned one. |
| 279 | versioned.transform(unversionedToVersionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 280 | bpFile.AddModule(versioned) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 281 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 282 | // Transform the unversioned module to make it suitable for use in the snapshot. |
| 283 | unversioned.transform(unversionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 284 | bpFile.AddModule(unversioned) |
| 285 | } |
| 286 | |
| 287 | // Create the snapshot module. |
| 288 | snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 289 | var snapshotModuleType string |
| 290 | if s.properties.Module_exports { |
| 291 | snapshotModuleType = "module_exports_snapshot" |
| 292 | } else { |
| 293 | snapshotModuleType = "sdk_snapshot" |
| 294 | } |
| 295 | snapshotModule := bpFile.newModule(snapshotModuleType) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 296 | snapshotModule.AddProperty("name", snapshotName) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 297 | |
| 298 | // Make sure that the snapshot has the same visibility as the sdk. |
| 299 | visibility := android.EffectiveVisibilityRules(ctx, s) |
| 300 | if len(visibility) != 0 { |
| 301 | snapshotModule.AddProperty("visibility", visibility) |
| 302 | } |
| 303 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 304 | addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 305 | |
| 306 | // Compile_multilib defaults to both and must always be set to both on the |
| 307 | // device and so only needs to be set when targeted at the host and is neither |
| 308 | // unspecified or both. |
| 309 | if s.HostSupported() && multilib != "" && multilib != "both" { |
| 310 | targetSet := snapshotModule.AddPropertySet("target") |
| 311 | hostSet := targetSet.AddPropertySet("host") |
| 312 | hostSet.AddProperty("compile_multilib", multilib) |
| 313 | } |
| 314 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 315 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 316 | names := memberListProperty.getter(s.dynamicMemberTypeListProperties) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 317 | if len(names) > 0 { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 318 | snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names)) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 319 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 320 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 321 | bpFile.AddModule(snapshotModule) |
| 322 | |
| 323 | // generate Android.bp |
| 324 | bp = newGeneratedFile(ctx, "snapshot", "Android.bp") |
| 325 | generateBpContents(&bp.generatedContents, bpFile) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 326 | |
| 327 | bp.build(pctx, ctx, nil) |
| 328 | |
| 329 | filesToZip := builder.filesToZip |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 330 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 331 | // zip them all |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 332 | outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 333 | outputDesc := "Building snapshot for " + ctx.ModuleName() |
| 334 | |
| 335 | // If there are no zips to merge then generate the output zip directly. |
| 336 | // Otherwise, generate an intermediate zip file into which other zips can be |
| 337 | // merged. |
| 338 | var zipFile android.OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 339 | var desc string |
| 340 | if len(builder.zipsToMerge) == 0 { |
| 341 | zipFile = outputZipFile |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 342 | desc = outputDesc |
| 343 | } else { |
| 344 | zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 345 | desc = "Building intermediate snapshot for " + ctx.ModuleName() |
| 346 | } |
| 347 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 348 | ctx.Build(pctx, android.BuildParams{ |
| 349 | Description: desc, |
| 350 | Rule: zipFiles, |
| 351 | Inputs: filesToZip, |
| 352 | Output: zipFile, |
| 353 | Args: map[string]string{ |
| 354 | "basedir": builder.snapshotDir.String(), |
| 355 | }, |
| 356 | }) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 357 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 358 | if len(builder.zipsToMerge) != 0 { |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 359 | ctx.Build(pctx, android.BuildParams{ |
| 360 | Description: outputDesc, |
| 361 | Rule: mergeZips, |
| 362 | Input: zipFile, |
| 363 | Inputs: builder.zipsToMerge, |
| 364 | Output: outputZipFile, |
| 365 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | return outputZipFile |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 369 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 370 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 371 | type propertyTag struct { |
| 372 | name string |
| 373 | } |
| 374 | |
| 375 | var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"} |
| 376 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 377 | type unversionedToVersionedTransformation struct { |
| 378 | identityTransformation |
| 379 | builder *snapshotBuilder |
| 380 | } |
| 381 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 382 | func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule { |
| 383 | // Use a versioned name for the module but remember the original name for the |
| 384 | // snapshot. |
| 385 | name := module.getValue("name").(string) |
| 386 | module.setProperty("name", t.builder.versionedSdkMemberName(name)) |
| 387 | module.insertAfter("name", "sdk_member_name", name) |
| 388 | return module |
| 389 | } |
| 390 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 391 | func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
| 392 | if tag == sdkMemberReferencePropertyTag { |
| 393 | return t.builder.versionedSdkMemberNames(value.([]string)), tag |
| 394 | } else { |
| 395 | return value, tag |
| 396 | } |
| 397 | } |
| 398 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 399 | type unversionedTransformation struct { |
| 400 | identityTransformation |
| 401 | builder *snapshotBuilder |
| 402 | } |
| 403 | |
| 404 | func (t unversionedTransformation) transformModule(module *bpModule) *bpModule { |
| 405 | // If the module is an internal member then use a unique name for it. |
| 406 | name := module.getValue("name").(string) |
| 407 | module.setProperty("name", t.builder.unversionedSdkMemberName(name)) |
| 408 | |
| 409 | // Set prefer: false - this is not strictly required as that is the default. |
| 410 | module.insertAfter("name", "prefer", false) |
| 411 | |
| 412 | return module |
| 413 | } |
| 414 | |
| 415 | func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
| 416 | if tag == sdkMemberReferencePropertyTag { |
| 417 | return t.builder.unversionedSdkMemberNames(value.([]string)), tag |
| 418 | } else { |
| 419 | return value, tag |
| 420 | } |
| 421 | } |
| 422 | |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 423 | type pruneEmptySetTransformer struct { |
| 424 | identityTransformation |
| 425 | } |
| 426 | |
| 427 | var _ bpTransformer = (*pruneEmptySetTransformer)(nil) |
| 428 | |
| 429 | func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { |
| 430 | if len(propertySet.properties) == 0 { |
| 431 | return nil, nil |
| 432 | } else { |
| 433 | return propertySet, tag |
| 434 | } |
| 435 | } |
| 436 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 437 | func generateBpContents(contents *generatedContents, bpFile *bpFile) { |
| 438 | contents.Printfln("// This is auto-generated. DO NOT EDIT.") |
| 439 | for _, bpModule := range bpFile.order { |
| 440 | contents.Printfln("") |
| 441 | contents.Printfln("%s {", bpModule.moduleType) |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 442 | outputPropertySet(contents, bpModule.bpPropertySet) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 443 | contents.Printfln("}") |
| 444 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | func outputPropertySet(contents *generatedContents, set *bpPropertySet) { |
| 448 | contents.Indent() |
| 449 | for _, name := range set.order { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 450 | value := set.getValue(name) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 451 | |
| 452 | reflectedValue := reflect.ValueOf(value) |
| 453 | t := reflectedValue.Type() |
| 454 | |
| 455 | kind := t.Kind() |
| 456 | switch kind { |
| 457 | case reflect.Slice: |
| 458 | length := reflectedValue.Len() |
| 459 | if length > 1 { |
| 460 | contents.Printfln("%s: [", name) |
| 461 | contents.Indent() |
| 462 | for i := 0; i < length; i = i + 1 { |
| 463 | contents.Printfln("%q,", reflectedValue.Index(i).Interface()) |
| 464 | } |
| 465 | contents.Dedent() |
| 466 | contents.Printfln("],") |
| 467 | } else if length == 0 { |
| 468 | contents.Printfln("%s: [],", name) |
| 469 | } else { |
| 470 | contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface()) |
| 471 | } |
| 472 | case reflect.Bool: |
| 473 | contents.Printfln("%s: %t,", name, reflectedValue.Bool()) |
| 474 | |
| 475 | case reflect.Ptr: |
| 476 | contents.Printfln("%s: {", name) |
| 477 | outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet)) |
| 478 | contents.Printfln("},") |
| 479 | |
| 480 | default: |
| 481 | contents.Printfln("%s: %q,", name, value) |
| 482 | } |
| 483 | } |
| 484 | contents.Dedent() |
| 485 | } |
| 486 | |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 487 | func (s *sdk) GetAndroidBpContentsForTests() string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 488 | contents := &generatedContents{} |
| 489 | generateBpContents(contents, s.builderForTests.bpFile) |
| 490 | return contents.content.String() |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 493 | type snapshotBuilder struct { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 494 | ctx android.ModuleContext |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 495 | sdk *sdk |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 496 | version string |
| 497 | snapshotDir android.OutputPath |
| 498 | bpFile *bpFile |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 499 | |
| 500 | // Map from destination to source of each copy - used to eliminate duplicates and |
| 501 | // detect conflicts. |
| 502 | copies map[string]string |
| 503 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 504 | filesToZip android.Paths |
| 505 | zipsToMerge android.Paths |
| 506 | |
| 507 | prebuiltModules map[string]*bpModule |
| 508 | prebuiltOrder []*bpModule |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) { |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 512 | if existing, ok := s.copies[dest]; ok { |
| 513 | if existing != src.String() { |
| 514 | s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src) |
| 515 | return |
| 516 | } |
| 517 | } else { |
| 518 | path := s.snapshotDir.Join(s.ctx, dest) |
| 519 | s.ctx.Build(pctx, android.BuildParams{ |
| 520 | Rule: android.Cp, |
| 521 | Input: src, |
| 522 | Output: path, |
| 523 | }) |
| 524 | s.filesToZip = append(s.filesToZip, path) |
| 525 | |
| 526 | s.copies[dest] = src.String() |
| 527 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 530 | func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) { |
| 531 | ctx := s.ctx |
| 532 | |
| 533 | // Repackage the zip file so that the entries are in the destDir directory. |
| 534 | // This will allow the zip file to be merged into the snapshot. |
| 535 | tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 536 | |
| 537 | ctx.Build(pctx, android.BuildParams{ |
| 538 | Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(), |
| 539 | Rule: repackageZip, |
| 540 | Input: zipPath, |
| 541 | Output: tmpZipPath, |
| 542 | Args: map[string]string{ |
| 543 | "destdir": destDir, |
| 544 | }, |
| 545 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 546 | |
| 547 | // Add the repackaged zip file to the files to merge. |
| 548 | s.zipsToMerge = append(s.zipsToMerge, tmpZipPath) |
| 549 | } |
| 550 | |
Paul Duffin | 9d8d609 | 2019-12-05 18:19:29 +0000 | [diff] [blame] | 551 | func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule { |
| 552 | name := member.Name() |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 553 | if s.prebuiltModules[name] != nil { |
| 554 | panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name)) |
| 555 | } |
| 556 | |
| 557 | m := s.bpFile.newModule(moduleType) |
| 558 | m.AddProperty("name", name) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 559 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 560 | variant := member.Variants()[0] |
| 561 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 562 | if s.sdk.isInternalMember(name) { |
| 563 | // An internal member is only referenced from the sdk snapshot which is in the |
| 564 | // same package so can be marked as private. |
| 565 | m.AddProperty("visibility", []string{"//visibility:private"}) |
| 566 | } else { |
| 567 | // Extract visibility information from a member variant. All variants have the same |
| 568 | // visibility so it doesn't matter which one is used. |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 569 | visibility := android.EffectiveVisibilityRules(s.ctx, variant) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 570 | if len(visibility) != 0 { |
| 571 | m.AddProperty("visibility", visibility) |
| 572 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 575 | addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 576 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 577 | // Where available copy apex_available properties from the member. |
| 578 | if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok { |
| 579 | apexAvailable := apexAware.ApexAvailable() |
| 580 | if len(apexAvailable) > 0 { |
| 581 | m.AddProperty("apex_available", apexAvailable) |
| 582 | } |
| 583 | } |
| 584 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 585 | s.prebuiltModules[name] = m |
| 586 | s.prebuiltOrder = append(s.prebuiltOrder, m) |
| 587 | return m |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 590 | func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) { |
| 591 | if !module.DeviceSupported() { |
| 592 | bpModule.AddProperty("device_supported", false) |
| 593 | } |
| 594 | if module.HostSupported() { |
| 595 | bpModule.AddProperty("host_supported", true) |
| 596 | } |
| 597 | } |
| 598 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 599 | func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag { |
| 600 | return sdkMemberReferencePropertyTag |
| 601 | } |
| 602 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 603 | // Get a versioned name appropriate for the SDK snapshot version being taken. |
| 604 | func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 605 | return versionedSdkMemberName(s.ctx, unversionedName, s.version) |
| 606 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 607 | |
| 608 | func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string { |
| 609 | var references []string = nil |
| 610 | for _, m := range members { |
| 611 | references = append(references, s.versionedSdkMemberName(m)) |
| 612 | } |
| 613 | return references |
| 614 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 615 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 616 | // Get an internal name unique to the sdk. |
| 617 | func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string { |
| 618 | if s.sdk.isInternalMember(unversionedName) { |
| 619 | return s.ctx.ModuleName() + "_" + unversionedName |
| 620 | } else { |
| 621 | return unversionedName |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string { |
| 626 | var references []string = nil |
| 627 | for _, m := range members { |
| 628 | references = append(references, s.unversionedSdkMemberName(m)) |
| 629 | } |
| 630 | return references |
| 631 | } |
| 632 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 633 | type sdkMemberRef struct { |
| 634 | memberType android.SdkMemberType |
| 635 | variant android.SdkAware |
| 636 | } |
| 637 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 638 | var _ android.SdkMember = (*sdkMember)(nil) |
| 639 | |
| 640 | type sdkMember struct { |
| 641 | memberType android.SdkMemberType |
| 642 | name string |
| 643 | variants []android.SdkAware |
| 644 | } |
| 645 | |
| 646 | func (m *sdkMember) Name() string { |
| 647 | return m.name |
| 648 | } |
| 649 | |
| 650 | func (m *sdkMember) Variants() []android.SdkAware { |
| 651 | return m.variants |
| 652 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame^] | 653 | |
| 654 | type baseInfo struct { |
| 655 | Properties android.SdkMemberProperties |
| 656 | } |
| 657 | |
| 658 | type osTypeSpecificInfo struct { |
| 659 | baseInfo |
| 660 | |
| 661 | // The list of arch type specific info for this os type. |
| 662 | archTypes []*archTypeSpecificInfo |
| 663 | } |
| 664 | |
| 665 | type archTypeSpecificInfo struct { |
| 666 | baseInfo |
| 667 | |
| 668 | archType android.ArchType |
| 669 | } |
| 670 | |
| 671 | func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) { |
| 672 | |
| 673 | memberType := member.memberType |
| 674 | |
| 675 | // Group the properties for each variant by arch type. |
| 676 | osInfo := &osTypeSpecificInfo{} |
| 677 | osInfo.Properties = memberType.CreateVariantPropertiesStruct() |
| 678 | variants := member.Variants() |
| 679 | for _, variant := range variants { |
| 680 | var properties android.SdkMemberProperties |
| 681 | |
| 682 | // Get the info associated with the arch type inside the os info. |
| 683 | archType := variant.Target().Arch.ArchType |
| 684 | |
| 685 | archInfo := &archTypeSpecificInfo{archType: archType} |
| 686 | properties = memberType.CreateVariantPropertiesStruct() |
| 687 | archInfo.Properties = properties |
| 688 | |
| 689 | osInfo.archTypes = append(osInfo.archTypes, archInfo) |
| 690 | |
| 691 | properties.PopulateFromVariant(variant) |
| 692 | } |
| 693 | |
| 694 | var archProperties []android.SdkMemberProperties |
| 695 | for _, archInfo := range osInfo.archTypes { |
| 696 | archProperties = append(archProperties, archInfo.Properties) |
| 697 | } |
| 698 | |
| 699 | extractCommonProperties(osInfo.Properties, archProperties) |
| 700 | |
| 701 | // Choose setting for compile_multilib that is appropriate for the arch variants supplied. |
| 702 | var multilib string |
| 703 | archVariantCount := len(osInfo.archTypes) |
| 704 | if archVariantCount == 2 { |
| 705 | multilib = "both" |
| 706 | } else if archVariantCount == 1 { |
| 707 | if strings.HasSuffix(osInfo.archTypes[0].archType.Name, "64") { |
| 708 | multilib = "64" |
| 709 | } else { |
| 710 | multilib = "32" |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | osInfo.Properties.Base().Compile_multilib = multilib |
| 715 | |
| 716 | osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, bpModule) |
| 717 | |
| 718 | archPropertySet := bpModule.AddPropertySet("arch") |
| 719 | for _, av := range osInfo.archTypes { |
| 720 | archTypePropertySet := archPropertySet.AddPropertySet(av.archType.Name) |
| 721 | |
| 722 | av.Properties.AddToPropertySet(sdkModuleContext, builder, archTypePropertySet) |
| 723 | } |
| 724 | |
| 725 | memberType.FinalizeModule(sdkModuleContext, builder, member, bpModule) |
| 726 | } |
| 727 | |
| 728 | // Extract common properties from a slice of property structures of the same type. |
| 729 | // |
| 730 | // All the property structures must be of the same type. |
| 731 | // commonProperties - must be a pointer to the structure into which common properties will be added. |
| 732 | // inputPropertiesSlice - must be a slice of input properties structures. |
| 733 | // |
| 734 | // Iterates over each exported field (capitalized name) and checks to see whether they |
| 735 | // have the same value (using DeepEquals) across all the input properties. If it does not then no |
| 736 | // change is made. Otherwise, the common value is stored in the field in the commonProperties |
| 737 | // and the field in each of the input properties structure is set to its default value. |
| 738 | func extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) { |
| 739 | commonPropertiesValue := reflect.ValueOf(commonProperties) |
| 740 | commonStructValue := commonPropertiesValue.Elem() |
| 741 | propertiesStructType := commonStructValue.Type() |
| 742 | |
| 743 | // Create an empty structure from which default values for the field can be copied. |
| 744 | emptyStructValue := reflect.New(propertiesStructType).Elem() |
| 745 | |
| 746 | for f := 0; f < propertiesStructType.NumField(); f++ { |
| 747 | // Check to see if all the structures have the same value for the field. The commonValue |
| 748 | // is nil on entry to the loop and if it is nil on exit then there is no common value, |
| 749 | // otherwise it points to the common value. |
| 750 | var commonValue *reflect.Value |
| 751 | sliceValue := reflect.ValueOf(inputPropertiesSlice) |
| 752 | |
| 753 | for i := 0; i < sliceValue.Len(); i++ { |
| 754 | structValue := sliceValue.Index(i).Elem().Elem() |
| 755 | fieldValue := structValue.Field(f) |
| 756 | if !fieldValue.CanInterface() { |
| 757 | // The field is not exported so ignore it. |
| 758 | continue |
| 759 | } |
| 760 | |
| 761 | if commonValue == nil { |
| 762 | // Use the first value as the commonProperties value. |
| 763 | commonValue = &fieldValue |
| 764 | } else { |
| 765 | // If the value does not match the current common value then there is |
| 766 | // no value in common so break out. |
| 767 | if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) { |
| 768 | commonValue = nil |
| 769 | break |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // If the fields all have a common value then store it in the common struct field |
| 775 | // and set the input struct's field to the empty value. |
| 776 | if commonValue != nil { |
| 777 | emptyValue := emptyStructValue.Field(f) |
| 778 | commonStructValue.Field(f).Set(*commonValue) |
| 779 | for i := 0; i < sliceValue.Len(); i++ { |
| 780 | structValue := sliceValue.Index(i).Elem().Elem() |
| 781 | fieldValue := structValue.Field(f) |
| 782 | fieldValue.Set(emptyValue) |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | } |