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" |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 20 | "sort" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 21 | "strings" |
| 22 | |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 23 | "android/soong/apex" |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
| 26 | |
| 27 | "android/soong/android" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var pctx = android.NewPackageContext("android/soong/sdk") |
| 31 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 32 | var ( |
| 33 | repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip", |
| 34 | blueprint.RuleParams{ |
Paul Duffin | ce482dc | 2019-12-09 19:58:17 +0000 | [diff] [blame] | 35 | Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`, |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 36 | 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 Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 62 | type generatedContents struct { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 63 | content strings.Builder |
| 64 | indentLevel int |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 67 | // generatedFile abstracts operations for writing contents into a file and emit a build rule |
| 68 | // for the file. |
| 69 | type generatedFile struct { |
| 70 | generatedContents |
| 71 | path android.OutputPath |
| 72 | } |
| 73 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 74 | func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 75 | return &generatedFile{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 76 | path: android.PathForModuleOut(ctx, path...).OutputPath, |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 80 | func (gc *generatedContents) Indent() { |
| 81 | gc.indentLevel++ |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 82 | } |
| 83 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 84 | func (gc *generatedContents) Dedent() { |
| 85 | gc.indentLevel-- |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 88 | func (gc *generatedContents) Printfln(format string, args ...interface{}) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 89 | // ninja consumes newline characters in rspfile_content. Prevent it by |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 90 | // escaping the backslash in the newline character. The extra backslash |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 91 | // is removed when the rspfile is written to the actual script file |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 92 | fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\\n", args...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func (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 Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 107 | // Collect all the members. |
| 108 | // |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 109 | // Returns a list containing type (extracted from the dependency tag) and the variant. |
| 110 | func (s *sdk) collectMembers(ctx android.ModuleContext) []sdkMemberRef { |
| 111 | var memberRefs []sdkMemberRef |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 112 | ctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 113 | tag := ctx.OtherModuleDependencyTag(child) |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 114 | if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok { |
| 115 | memberType := memberTag.SdkMemberType() |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 116 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 117 | // 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] | 118 | if !memberType.IsInstance(child) { |
| 119 | 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] | 120 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 121 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 122 | memberRefs = append(memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)}) |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 123 | |
| 124 | // If the member type supports transitive sdk members then recurse down into |
| 125 | // its dependencies, otherwise exit traversal. |
| 126 | return memberType.HasTransitiveSdkMembers() |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 127 | } |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 128 | |
| 129 | return false |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 130 | }) |
| 131 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 132 | 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. |
| 142 | func (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 Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 173 | var members []*sdkMember |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 174 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 175 | membersOfType := byType[memberListProperty.memberType] |
| 176 | members = append(members, membersOfType...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 177 | } |
| 178 | |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 179 | // 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 Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 190 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 191 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 192 | func 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 Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 201 | // 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 Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 209 | // <module_name>.jar : the stub jar for a java library 'module_name' |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 210 | // include/ |
| 211 | // bionic/libc/include/stdlib.h : an exported header file |
| 212 | // include_gen/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 213 | // <module_name>/com/android/.../IFoo.h : a generated header file |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 214 | // <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 Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 219 | // 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] | 220 | // This isn't visible to users, so could be changed in future. |
| 221 | func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string { |
| 222 | return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version |
| 223 | } |
| 224 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 225 | // 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 Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 227 | func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath { |
| 228 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 229 | allMembersByName := make(map[string]struct{}) |
| 230 | exportedMembersByName := make(map[string]struct{}) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 231 | var memberRefs []sdkMemberRef |
| 232 | for _, sdkVariant := range sdkVariants { |
| 233 | memberRefs = append(memberRefs, sdkVariant.memberRefs...) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 234 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 235 | // Record the names of all the members, both explicitly specified and implicitly |
| 236 | // included. |
| 237 | for _, memberRef := range sdkVariant.memberRefs { |
| 238 | allMembersByName[memberRef.variant.Name()] = struct{}{} |
| 239 | } |
| 240 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 241 | // Merge the exported member sets from all sdk variants. |
| 242 | for key, _ := range sdkVariant.getExportedMembers() { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 243 | exportedMembersByName[key] = struct{}{} |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 244 | } |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 247 | snapshotDir := android.PathForModuleOut(ctx, "snapshot") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 248 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 249 | bp := newGeneratedFile(ctx, "snapshot", "Android.bp") |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 250 | |
| 251 | bpFile := &bpFile{ |
| 252 | modules: make(map[string]*bpModule), |
| 253 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 254 | |
| 255 | builder := &snapshotBuilder{ |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 256 | ctx: ctx, |
| 257 | sdk: s, |
| 258 | version: "current", |
| 259 | snapshotDir: snapshotDir.OutputPath, |
| 260 | copies: make(map[string]string), |
| 261 | filesToZip: []android.Path{bp.path}, |
| 262 | bpFile: bpFile, |
| 263 | prebuiltModules: make(map[string]*bpModule), |
| 264 | allMembersByName: allMembersByName, |
| 265 | exportedMembersByName: exportedMembersByName, |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 266 | } |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 267 | s.builderForTests = builder |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 268 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 269 | members, multilib := s.organizeMembers(ctx, memberRefs) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 270 | for _, member := range members { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 271 | memberType := member.memberType |
| 272 | prebuiltModule := memberType.AddPrebuiltModule(ctx, builder, member) |
| 273 | if prebuiltModule == nil { |
| 274 | // Fall back to legacy method of building a snapshot |
| 275 | memberType.BuildSnapshot(ctx, builder, member) |
| 276 | } else { |
| 277 | s.createMemberSnapshot(ctx, builder, member, prebuiltModule) |
| 278 | } |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 279 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 280 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 281 | // Create a transformer that will transform an unversioned module into a versioned module. |
| 282 | unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder} |
| 283 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 284 | // Create a transformer that will transform an unversioned module by replacing any references |
| 285 | // to internal members with a unique module name and setting prefer: false. |
| 286 | unversionedTransformer := unversionedTransformation{builder: builder} |
| 287 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 288 | for _, unversioned := range builder.prebuiltOrder { |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 289 | // Prune any empty property sets. |
| 290 | unversioned = unversioned.transform(pruneEmptySetTransformer{}) |
| 291 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 292 | // 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] | 293 | versioned := unversioned.deepCopy() |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 294 | |
| 295 | // Transform the unversioned module into a versioned one. |
| 296 | versioned.transform(unversionedToVersionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 297 | bpFile.AddModule(versioned) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 298 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 299 | // Transform the unversioned module to make it suitable for use in the snapshot. |
| 300 | unversioned.transform(unversionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 301 | bpFile.AddModule(unversioned) |
| 302 | } |
| 303 | |
| 304 | // Create the snapshot module. |
| 305 | snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 306 | var snapshotModuleType string |
| 307 | if s.properties.Module_exports { |
| 308 | snapshotModuleType = "module_exports_snapshot" |
| 309 | } else { |
| 310 | snapshotModuleType = "sdk_snapshot" |
| 311 | } |
| 312 | snapshotModule := bpFile.newModule(snapshotModuleType) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 313 | snapshotModule.AddProperty("name", snapshotName) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 314 | |
| 315 | // Make sure that the snapshot has the same visibility as the sdk. |
| 316 | visibility := android.EffectiveVisibilityRules(ctx, s) |
| 317 | if len(visibility) != 0 { |
| 318 | snapshotModule.AddProperty("visibility", visibility) |
| 319 | } |
| 320 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 321 | addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 322 | |
| 323 | // Compile_multilib defaults to both and must always be set to both on the |
| 324 | // device and so only needs to be set when targeted at the host and is neither |
| 325 | // unspecified or both. |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 326 | targetPropertySet := snapshotModule.AddPropertySet("target") |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 327 | if s.HostSupported() && multilib != "" && multilib != "both" { |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 328 | hostSet := targetPropertySet.AddPropertySet("host") |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 329 | hostSet.AddProperty("compile_multilib", multilib) |
| 330 | } |
| 331 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 332 | var dynamicMemberPropertiesList []interface{} |
| 333 | osTypeToMemberProperties := make(map[android.OsType]*sdk) |
| 334 | for _, sdkVariant := range sdkVariants { |
| 335 | properties := sdkVariant.dynamicMemberTypeListProperties |
| 336 | osTypeToMemberProperties[sdkVariant.Target().Os] = sdkVariant |
| 337 | dynamicMemberPropertiesList = append(dynamicMemberPropertiesList, properties) |
| 338 | } |
| 339 | |
| 340 | // Extract the common lists of members into a separate struct. |
| 341 | commonDynamicMemberProperties := s.dynamicSdkMemberTypes.createMemberListProperties() |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 342 | extractor := newCommonValueExtractor(commonDynamicMemberProperties) |
| 343 | extractor.extractCommonProperties(commonDynamicMemberProperties, dynamicMemberPropertiesList) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 344 | |
| 345 | // Add properties common to all os types. |
| 346 | s.addMemberPropertiesToPropertySet(builder, snapshotModule, commonDynamicMemberProperties) |
| 347 | |
| 348 | // Iterate over the os types in a fixed order. |
| 349 | for _, osType := range s.getPossibleOsTypes() { |
| 350 | if sdkVariant, ok := osTypeToMemberProperties[osType]; ok { |
| 351 | osPropertySet := targetPropertySet.AddPropertySet(sdkVariant.Target().Os.Name) |
| 352 | s.addMemberPropertiesToPropertySet(builder, osPropertySet, sdkVariant.dynamicMemberTypeListProperties) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 353 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 354 | } |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 355 | |
| 356 | // Prune any empty property sets. |
| 357 | snapshotModule.transform(pruneEmptySetTransformer{}) |
| 358 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 359 | bpFile.AddModule(snapshotModule) |
| 360 | |
| 361 | // generate Android.bp |
| 362 | bp = newGeneratedFile(ctx, "snapshot", "Android.bp") |
| 363 | generateBpContents(&bp.generatedContents, bpFile) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 364 | |
| 365 | bp.build(pctx, ctx, nil) |
| 366 | |
| 367 | filesToZip := builder.filesToZip |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 368 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 369 | // zip them all |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 370 | outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 371 | outputDesc := "Building snapshot for " + ctx.ModuleName() |
| 372 | |
| 373 | // If there are no zips to merge then generate the output zip directly. |
| 374 | // Otherwise, generate an intermediate zip file into which other zips can be |
| 375 | // merged. |
| 376 | var zipFile android.OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 377 | var desc string |
| 378 | if len(builder.zipsToMerge) == 0 { |
| 379 | zipFile = outputZipFile |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 380 | desc = outputDesc |
| 381 | } else { |
| 382 | zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 383 | desc = "Building intermediate snapshot for " + ctx.ModuleName() |
| 384 | } |
| 385 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 386 | ctx.Build(pctx, android.BuildParams{ |
| 387 | Description: desc, |
| 388 | Rule: zipFiles, |
| 389 | Inputs: filesToZip, |
| 390 | Output: zipFile, |
| 391 | Args: map[string]string{ |
| 392 | "basedir": builder.snapshotDir.String(), |
| 393 | }, |
| 394 | }) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 395 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 396 | if len(builder.zipsToMerge) != 0 { |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 397 | ctx.Build(pctx, android.BuildParams{ |
| 398 | Description: outputDesc, |
| 399 | Rule: mergeZips, |
| 400 | Input: zipFile, |
| 401 | Inputs: builder.zipsToMerge, |
| 402 | Output: outputZipFile, |
| 403 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | return outputZipFile |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 407 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 408 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 409 | func (s *sdk) addMemberPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, dynamicMemberTypeListProperties interface{}) { |
| 410 | for _, memberListProperty := range s.memberListProperties() { |
| 411 | names := memberListProperty.getter(dynamicMemberTypeListProperties) |
| 412 | if len(names) > 0 { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 413 | propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names, false)) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 418 | type propertyTag struct { |
| 419 | name string |
| 420 | } |
| 421 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 422 | // A BpPropertyTag to add to a property that contains references to other sdk members. |
| 423 | // |
| 424 | // This will cause the references to be rewritten to a versioned reference in the version |
| 425 | // specific instance of a snapshot module. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 426 | var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"} |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 427 | var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"} |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 428 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 429 | // A BpPropertyTag that indicates the property should only be present in the versioned |
| 430 | // module. |
| 431 | // |
| 432 | // This will cause the property to be removed from the unversioned instance of a |
| 433 | // snapshot module. |
| 434 | var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"} |
| 435 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 436 | type unversionedToVersionedTransformation struct { |
| 437 | identityTransformation |
| 438 | builder *snapshotBuilder |
| 439 | } |
| 440 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 441 | func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule { |
| 442 | // Use a versioned name for the module but remember the original name for the |
| 443 | // snapshot. |
| 444 | name := module.getValue("name").(string) |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 445 | module.setProperty("name", t.builder.versionedSdkMemberName(name, true)) |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 446 | module.insertAfter("name", "sdk_member_name", name) |
| 447 | return module |
| 448 | } |
| 449 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 450 | func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 451 | if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag { |
| 452 | required := tag == requiredSdkMemberReferencePropertyTag |
| 453 | return t.builder.versionedSdkMemberNames(value.([]string), required), tag |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 454 | } else { |
| 455 | return value, tag |
| 456 | } |
| 457 | } |
| 458 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 459 | type unversionedTransformation struct { |
| 460 | identityTransformation |
| 461 | builder *snapshotBuilder |
| 462 | } |
| 463 | |
| 464 | func (t unversionedTransformation) transformModule(module *bpModule) *bpModule { |
| 465 | // If the module is an internal member then use a unique name for it. |
| 466 | name := module.getValue("name").(string) |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 467 | module.setProperty("name", t.builder.unversionedSdkMemberName(name, true)) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 468 | |
| 469 | // Set prefer: false - this is not strictly required as that is the default. |
| 470 | module.insertAfter("name", "prefer", false) |
| 471 | |
| 472 | return module |
| 473 | } |
| 474 | |
| 475 | func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 476 | if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag { |
| 477 | required := tag == requiredSdkMemberReferencePropertyTag |
| 478 | return t.builder.unversionedSdkMemberNames(value.([]string), required), tag |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 479 | } else if tag == sdkVersionedOnlyPropertyTag { |
| 480 | // The property is not allowed in the unversioned module so remove it. |
| 481 | return nil, nil |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 482 | } else { |
| 483 | return value, tag |
| 484 | } |
| 485 | } |
| 486 | |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 487 | type pruneEmptySetTransformer struct { |
| 488 | identityTransformation |
| 489 | } |
| 490 | |
| 491 | var _ bpTransformer = (*pruneEmptySetTransformer)(nil) |
| 492 | |
| 493 | func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { |
| 494 | if len(propertySet.properties) == 0 { |
| 495 | return nil, nil |
| 496 | } else { |
| 497 | return propertySet, tag |
| 498 | } |
| 499 | } |
| 500 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 501 | func generateBpContents(contents *generatedContents, bpFile *bpFile) { |
| 502 | contents.Printfln("// This is auto-generated. DO NOT EDIT.") |
| 503 | for _, bpModule := range bpFile.order { |
| 504 | contents.Printfln("") |
| 505 | contents.Printfln("%s {", bpModule.moduleType) |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 506 | outputPropertySet(contents, bpModule.bpPropertySet) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 507 | contents.Printfln("}") |
| 508 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | func outputPropertySet(contents *generatedContents, set *bpPropertySet) { |
| 512 | contents.Indent() |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 513 | |
| 514 | // Output the properties first, followed by the nested sets. This ensures a |
| 515 | // consistent output irrespective of whether property sets are created before |
| 516 | // or after the properties. This simplifies the creation of the module. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 517 | for _, name := range set.order { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 518 | value := set.getValue(name) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 519 | |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 520 | switch v := value.(type) { |
| 521 | case []string: |
| 522 | length := len(v) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 523 | if length > 1 { |
| 524 | contents.Printfln("%s: [", name) |
| 525 | contents.Indent() |
| 526 | for i := 0; i < length; i = i + 1 { |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 527 | contents.Printfln("%q,", v[i]) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 528 | } |
| 529 | contents.Dedent() |
| 530 | contents.Printfln("],") |
| 531 | } else if length == 0 { |
| 532 | contents.Printfln("%s: [],", name) |
| 533 | } else { |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 534 | contents.Printfln("%s: [%q],", name, v[0]) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 535 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 536 | |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 537 | case bool: |
| 538 | contents.Printfln("%s: %t,", name, v) |
| 539 | |
| 540 | case *bpPropertySet: |
| 541 | // Do not write property sets in the properties phase. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 542 | |
| 543 | default: |
| 544 | contents.Printfln("%s: %q,", name, value) |
| 545 | } |
| 546 | } |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 547 | |
| 548 | for _, name := range set.order { |
| 549 | value := set.getValue(name) |
| 550 | |
| 551 | // Only write property sets in the sets phase. |
| 552 | switch v := value.(type) { |
| 553 | case *bpPropertySet: |
| 554 | contents.Printfln("%s: {", name) |
| 555 | outputPropertySet(contents, v) |
| 556 | contents.Printfln("},") |
| 557 | } |
| 558 | } |
| 559 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 560 | contents.Dedent() |
| 561 | } |
| 562 | |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 563 | func (s *sdk) GetAndroidBpContentsForTests() string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 564 | contents := &generatedContents{} |
| 565 | generateBpContents(contents, s.builderForTests.bpFile) |
| 566 | return contents.content.String() |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 569 | type snapshotBuilder struct { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 570 | ctx android.ModuleContext |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 571 | sdk *sdk |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 572 | version string |
| 573 | snapshotDir android.OutputPath |
| 574 | bpFile *bpFile |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 575 | |
| 576 | // Map from destination to source of each copy - used to eliminate duplicates and |
| 577 | // detect conflicts. |
| 578 | copies map[string]string |
| 579 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 580 | filesToZip android.Paths |
| 581 | zipsToMerge android.Paths |
| 582 | |
| 583 | prebuiltModules map[string]*bpModule |
| 584 | prebuiltOrder []*bpModule |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 585 | |
| 586 | // The set of all members by name. |
| 587 | allMembersByName map[string]struct{} |
| 588 | |
| 589 | // The set of exported members by name. |
| 590 | exportedMembersByName map[string]struct{} |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) { |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 594 | if existing, ok := s.copies[dest]; ok { |
| 595 | if existing != src.String() { |
| 596 | s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src) |
| 597 | return |
| 598 | } |
| 599 | } else { |
| 600 | path := s.snapshotDir.Join(s.ctx, dest) |
| 601 | s.ctx.Build(pctx, android.BuildParams{ |
| 602 | Rule: android.Cp, |
| 603 | Input: src, |
| 604 | Output: path, |
| 605 | }) |
| 606 | s.filesToZip = append(s.filesToZip, path) |
| 607 | |
| 608 | s.copies[dest] = src.String() |
| 609 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 612 | func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) { |
| 613 | ctx := s.ctx |
| 614 | |
| 615 | // Repackage the zip file so that the entries are in the destDir directory. |
| 616 | // This will allow the zip file to be merged into the snapshot. |
| 617 | tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 618 | |
| 619 | ctx.Build(pctx, android.BuildParams{ |
| 620 | Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(), |
| 621 | Rule: repackageZip, |
| 622 | Input: zipPath, |
| 623 | Output: tmpZipPath, |
| 624 | Args: map[string]string{ |
| 625 | "destdir": destDir, |
| 626 | }, |
| 627 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 628 | |
| 629 | // Add the repackaged zip file to the files to merge. |
| 630 | s.zipsToMerge = append(s.zipsToMerge, tmpZipPath) |
| 631 | } |
| 632 | |
Paul Duffin | 9d8d609 | 2019-12-05 18:19:29 +0000 | [diff] [blame] | 633 | func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule { |
| 634 | name := member.Name() |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 635 | if s.prebuiltModules[name] != nil { |
| 636 | panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name)) |
| 637 | } |
| 638 | |
| 639 | m := s.bpFile.newModule(moduleType) |
| 640 | m.AddProperty("name", name) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 641 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 642 | variant := member.Variants()[0] |
| 643 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 644 | if s.isInternalMember(name) { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 645 | // An internal member is only referenced from the sdk snapshot which is in the |
| 646 | // same package so can be marked as private. |
| 647 | m.AddProperty("visibility", []string{"//visibility:private"}) |
| 648 | } else { |
| 649 | // Extract visibility information from a member variant. All variants have the same |
| 650 | // visibility so it doesn't matter which one is used. |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 651 | visibility := android.EffectiveVisibilityRules(s.ctx, variant) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 652 | if len(visibility) != 0 { |
| 653 | m.AddProperty("visibility", visibility) |
| 654 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 657 | deviceSupported := false |
| 658 | hostSupported := false |
| 659 | |
| 660 | for _, variant := range member.Variants() { |
| 661 | osClass := variant.Target().Os.Class |
| 662 | if osClass == android.Host || osClass == android.HostCross { |
| 663 | hostSupported = true |
| 664 | } else if osClass == android.Device { |
| 665 | deviceSupported = true |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | addHostDeviceSupportedProperties(deviceSupported, hostSupported, m) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 670 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 671 | // Where available copy apex_available properties from the member. |
| 672 | if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok { |
| 673 | apexAvailable := apexAware.ApexAvailable() |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 674 | |
| 675 | // Add in any white listed apex available settings. |
| 676 | apexAvailable = append(apexAvailable, apex.WhitelistedApexAvailable(member.Name())...) |
| 677 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 678 | if len(apexAvailable) > 0 { |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 679 | // Remove duplicates and sort. |
| 680 | apexAvailable = android.FirstUniqueStrings(apexAvailable) |
| 681 | sort.Strings(apexAvailable) |
| 682 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 683 | m.AddProperty("apex_available", apexAvailable) |
| 684 | } |
| 685 | } |
| 686 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 687 | // Disable installation in the versioned module of those modules that are ever installable. |
| 688 | if installable, ok := variant.(interface{ EverInstallable() bool }); ok { |
| 689 | if installable.EverInstallable() { |
| 690 | m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag) |
| 691 | } |
| 692 | } |
| 693 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 694 | s.prebuiltModules[name] = m |
| 695 | s.prebuiltOrder = append(s.prebuiltOrder, m) |
| 696 | return m |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 699 | func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) { |
| 700 | if !deviceSupported { |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 701 | bpModule.AddProperty("device_supported", false) |
| 702 | } |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 703 | if hostSupported { |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 704 | bpModule.AddProperty("host_supported", true) |
| 705 | } |
| 706 | } |
| 707 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 708 | func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag { |
| 709 | if required { |
| 710 | return requiredSdkMemberReferencePropertyTag |
| 711 | } else { |
| 712 | return optionalSdkMemberReferencePropertyTag |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag { |
| 717 | return optionalSdkMemberReferencePropertyTag |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 720 | // Get a versioned name appropriate for the SDK snapshot version being taken. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 721 | func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string, required bool) string { |
| 722 | if _, ok := s.allMembersByName[unversionedName]; !ok { |
| 723 | if required { |
| 724 | s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName) |
| 725 | } |
| 726 | return unversionedName |
| 727 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 728 | return versionedSdkMemberName(s.ctx, unversionedName, s.version) |
| 729 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 730 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 731 | func (s *snapshotBuilder) versionedSdkMemberNames(members []string, required bool) []string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 732 | var references []string = nil |
| 733 | for _, m := range members { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 734 | references = append(references, s.versionedSdkMemberName(m, required)) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 735 | } |
| 736 | return references |
| 737 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 738 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 739 | // Get an internal name unique to the sdk. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 740 | func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string, required bool) string { |
| 741 | if _, ok := s.allMembersByName[unversionedName]; !ok { |
| 742 | if required { |
| 743 | s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName) |
| 744 | } |
| 745 | return unversionedName |
| 746 | } |
| 747 | |
| 748 | if s.isInternalMember(unversionedName) { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 749 | return s.ctx.ModuleName() + "_" + unversionedName |
| 750 | } else { |
| 751 | return unversionedName |
| 752 | } |
| 753 | } |
| 754 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 755 | func (s *snapshotBuilder) unversionedSdkMemberNames(members []string, required bool) []string { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 756 | var references []string = nil |
| 757 | for _, m := range members { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 758 | references = append(references, s.unversionedSdkMemberName(m, required)) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 759 | } |
| 760 | return references |
| 761 | } |
| 762 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 763 | func (s *snapshotBuilder) isInternalMember(memberName string) bool { |
| 764 | _, ok := s.exportedMembersByName[memberName] |
| 765 | return !ok |
| 766 | } |
| 767 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 768 | type sdkMemberRef struct { |
| 769 | memberType android.SdkMemberType |
| 770 | variant android.SdkAware |
| 771 | } |
| 772 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 773 | var _ android.SdkMember = (*sdkMember)(nil) |
| 774 | |
| 775 | type sdkMember struct { |
| 776 | memberType android.SdkMemberType |
| 777 | name string |
| 778 | variants []android.SdkAware |
| 779 | } |
| 780 | |
| 781 | func (m *sdkMember) Name() string { |
| 782 | return m.name |
| 783 | } |
| 784 | |
| 785 | func (m *sdkMember) Variants() []android.SdkAware { |
| 786 | return m.variants |
| 787 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 788 | |
| 789 | type baseInfo struct { |
| 790 | Properties android.SdkMemberProperties |
| 791 | } |
| 792 | |
| 793 | type osTypeSpecificInfo struct { |
| 794 | baseInfo |
| 795 | |
| 796 | // The list of arch type specific info for this os type. |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 797 | // |
| 798 | // Nil if there is one variant whose arch type is common |
| 799 | archInfos []*archTypeSpecificInfo |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame^] | 802 | type variantPropertiesFactoryFunc func() android.SdkMemberProperties |
| 803 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 804 | type archTypeSpecificInfo struct { |
| 805 | baseInfo |
| 806 | |
| 807 | archType android.ArchType |
| 808 | } |
| 809 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame^] | 810 | // Create a new archTypeSpecificInfo for the specified arch type and its properties |
| 811 | // structures populated with information from the variants. |
| 812 | func newArchSpecificInfo(archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.SdkAware) *archTypeSpecificInfo { |
| 813 | |
| 814 | if len(archVariants) != 1 { |
| 815 | panic(fmt.Errorf("expected one arch specific variant but found %d", len(archVariants))) |
| 816 | } |
| 817 | |
| 818 | // Create an arch specific info into which the variant properties can be copied. |
| 819 | archInfo := &archTypeSpecificInfo{archType: archType} |
| 820 | |
| 821 | // Create the properties into which the arch type specific properties will be |
| 822 | // added. |
| 823 | archInfo.Properties = variantPropertiesFactory() |
| 824 | archInfo.Properties.PopulateFromVariant(archVariants[0]) |
| 825 | |
| 826 | return archInfo |
| 827 | } |
| 828 | |
| 829 | // Add the properties for an arch type to a property set. |
| 830 | func (archInfo *archTypeSpecificInfo) addToPropertySet(builder *snapshotBuilder, archPropertySet android.BpPropertySet, archOsPrefix string) { |
| 831 | archTypeName := archInfo.archType.Name |
| 832 | archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName) |
| 833 | archInfo.Properties.AddToPropertySet(builder.ctx, builder, archTypePropertySet) |
| 834 | } |
| 835 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 836 | func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) { |
| 837 | |
| 838 | memberType := member.memberType |
| 839 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 840 | // Group the variants by os type. |
| 841 | variantsByOsType := make(map[android.OsType][]android.SdkAware) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 842 | variants := member.Variants() |
| 843 | for _, variant := range variants { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 844 | osType := variant.Target().Os |
| 845 | variantsByOsType[osType] = append(variantsByOsType[osType], variant) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 848 | osCount := len(variantsByOsType) |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 849 | variantPropertiesFactory := func() android.SdkMemberProperties { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 850 | properties := memberType.CreateVariantPropertiesStruct() |
| 851 | base := properties.Base() |
| 852 | base.Os_count = osCount |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 853 | return properties |
| 854 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 855 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 856 | osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 857 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 858 | // The set of properties that are common across all architectures and os types. |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 859 | commonProperties := variantPropertiesFactory() |
| 860 | commonProperties.Base().Os = android.CommonOS |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 861 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 862 | // Create common value extractor that can be used to optimize the properties. |
| 863 | commonValueExtractor := newCommonValueExtractor(commonProperties) |
| 864 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 865 | // The list of property structures which are os type specific but common across |
| 866 | // architectures within that os type. |
| 867 | var osSpecificPropertiesList []android.SdkMemberProperties |
| 868 | |
| 869 | for osType, osTypeVariants := range variantsByOsType { |
| 870 | // Group the properties for each variant by arch type within the os. |
| 871 | osInfo := &osTypeSpecificInfo{} |
| 872 | osTypeToInfo[osType] = osInfo |
| 873 | |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 874 | osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties { |
| 875 | properties := variantPropertiesFactory() |
| 876 | properties.Base().Os = osType |
| 877 | return properties |
| 878 | } |
| 879 | |
| 880 | // Add the os specific properties to a list of os type specific yet architecture |
| 881 | // independent properties structs. |
| 882 | osInfo.Properties = osSpecificVariantPropertiesFactory() |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 883 | osSpecificPropertiesList = append(osSpecificPropertiesList, osInfo.Properties) |
| 884 | |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 885 | // Group the variants by arch type. |
| 886 | var variantsByArchName = make(map[string][]android.SdkAware) |
| 887 | var archTypes []android.ArchType |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 888 | for _, variant := range osTypeVariants { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 889 | archType := variant.Target().Arch.ArchType |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 890 | archTypeName := archType.Name |
| 891 | if _, ok := variantsByArchName[archTypeName]; !ok { |
| 892 | archTypes = append(archTypes, archType) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 893 | } |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 894 | |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 895 | variantsByArchName[archTypeName] = append(variantsByArchName[archTypeName], variant) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 898 | if commonVariants, ok := variantsByArchName["common"]; ok { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 899 | if len(osTypeVariants) != 1 { |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 900 | panic("Expected to only have 1 variant when arch type is common but found " + string(len(osTypeVariants))) |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 901 | } |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 902 | |
| 903 | // A common arch type only has one variant and its properties should be treated |
| 904 | // as common to the os type. |
| 905 | osInfo.Properties.PopulateFromVariant(commonVariants[0]) |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 906 | } else { |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 907 | // Create an arch specific info for each supported architecture type. |
| 908 | for _, archType := range archTypes { |
| 909 | archTypeName := archType.Name |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 910 | |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 911 | archVariants := variantsByArchName[archTypeName] |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame^] | 912 | archInfo := newArchSpecificInfo(archType, osSpecificVariantPropertiesFactory, archVariants) |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 913 | |
| 914 | osInfo.archInfos = append(osInfo.archInfos, archInfo) |
| 915 | } |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 916 | } |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 917 | |
| 918 | var archPropertiesList []android.SdkMemberProperties |
| 919 | for _, archInfo := range osInfo.archInfos { |
| 920 | archPropertiesList = append(archPropertiesList, archInfo.Properties) |
| 921 | } |
| 922 | |
| 923 | commonValueExtractor.extractCommonProperties(osInfo.Properties, archPropertiesList) |
| 924 | |
| 925 | // Choose setting for compile_multilib that is appropriate for the arch variants supplied. |
| 926 | var multilib string |
| 927 | archVariantCount := len(osInfo.archInfos) |
| 928 | if archVariantCount == 2 { |
| 929 | multilib = "both" |
| 930 | } else if archVariantCount == 1 { |
| 931 | if strings.HasSuffix(osInfo.archInfos[0].archType.Name, "64") { |
| 932 | multilib = "64" |
| 933 | } else { |
| 934 | multilib = "32" |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | osInfo.Properties.Base().Compile_multilib = multilib |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 939 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 940 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 941 | // Extract properties which are common across all architectures and os types. |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 942 | commonValueExtractor.extractCommonProperties(commonProperties, osSpecificPropertiesList) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 943 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 944 | // Add the common properties to the module. |
| 945 | commonProperties.AddToPropertySet(sdkModuleContext, builder, bpModule) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 946 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 947 | // Create a target property set into which target specific properties can be |
| 948 | // added. |
| 949 | targetPropertySet := bpModule.AddPropertySet("target") |
| 950 | |
| 951 | // Iterate over the os types in a fixed order. |
| 952 | for _, osType := range s.getPossibleOsTypes() { |
| 953 | osInfo := osTypeToInfo[osType] |
| 954 | if osInfo == nil { |
| 955 | continue |
| 956 | } |
| 957 | |
| 958 | var osPropertySet android.BpPropertySet |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 959 | var archPropertySet android.BpPropertySet |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 960 | var archOsPrefix string |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 961 | if osInfo.Properties.Base().Os_count == 1 { |
| 962 | // There is only one os type present in the variants so don't bother |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 963 | // with adding target specific properties. |
| 964 | |
| 965 | // Create a structure that looks like: |
| 966 | // module_type { |
| 967 | // name: "...", |
| 968 | // ... |
| 969 | // <common properties> |
| 970 | // ... |
| 971 | // <single os type specific properties> |
| 972 | // |
| 973 | // arch: { |
| 974 | // <arch specific sections> |
| 975 | // } |
| 976 | // |
| 977 | osPropertySet = bpModule |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 978 | archPropertySet = osPropertySet.AddPropertySet("arch") |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 979 | |
| 980 | // Arch specific properties need to be added to an arch specific section |
| 981 | // within arch. |
| 982 | archOsPrefix = "" |
| 983 | } else { |
| 984 | // Create a structure that looks like: |
| 985 | // module_type { |
| 986 | // name: "...", |
| 987 | // ... |
| 988 | // <common properties> |
| 989 | // ... |
| 990 | // target: { |
| 991 | // <arch independent os specific sections, e.g. android> |
| 992 | // ... |
| 993 | // <arch and os specific sections, e.g. android_x86> |
| 994 | // } |
| 995 | // |
| 996 | osPropertySet = targetPropertySet.AddPropertySet(osType.Name) |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 997 | archPropertySet = targetPropertySet |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 998 | |
| 999 | // Arch specific properties need to be added to an os and arch specific |
| 1000 | // section prefixed with <os>_. |
| 1001 | archOsPrefix = osType.Name + "_" |
| 1002 | } |
| 1003 | |
| 1004 | osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, osPropertySet) |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1005 | |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 1006 | // Add arch (and possibly os) specific sections for each set of arch (and possibly |
| 1007 | // os) specific properties. |
| 1008 | // |
| 1009 | // The archInfos list will be empty if the os contains variants for the common |
| 1010 | for _, archInfo := range osInfo.archInfos { |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame^] | 1011 | archInfo.addToPropertySet(builder, archPropertySet, archOsPrefix) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1012 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1013 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1016 | // Compute the list of possible os types that this sdk could support. |
| 1017 | func (s *sdk) getPossibleOsTypes() []android.OsType { |
| 1018 | var osTypes []android.OsType |
| 1019 | for _, osType := range android.OsTypeList { |
| 1020 | if s.DeviceSupported() { |
| 1021 | if osType.Class == android.Device && osType != android.Fuchsia { |
| 1022 | osTypes = append(osTypes, osType) |
| 1023 | } |
| 1024 | } |
| 1025 | if s.HostSupported() { |
| 1026 | if osType.Class == android.Host || osType.Class == android.HostCross { |
| 1027 | osTypes = append(osTypes, osType) |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name }) |
| 1032 | return osTypes |
| 1033 | } |
| 1034 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1035 | // Given a struct value, access a field within that struct (or one of its embedded |
| 1036 | // structs). |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1037 | type fieldAccessorFunc func(structValue reflect.Value) reflect.Value |
| 1038 | |
| 1039 | // Supports extracting common values from a number of instances of a properties |
| 1040 | // structure into a separate common set of properties. |
| 1041 | type commonValueExtractor struct { |
| 1042 | // The getters for every field from which common values can be extracted. |
| 1043 | fieldGetters []fieldAccessorFunc |
| 1044 | } |
| 1045 | |
| 1046 | // Create a new common value extractor for the structure type for the supplied |
| 1047 | // properties struct. |
| 1048 | // |
| 1049 | // The returned extractor can be used on any properties structure of the same type |
| 1050 | // as the supplied set of properties. |
| 1051 | func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor { |
| 1052 | structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type() |
| 1053 | extractor := &commonValueExtractor{} |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1054 | extractor.gatherFields(structType, nil) |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1055 | return extractor |
| 1056 | } |
| 1057 | |
| 1058 | // Gather the fields from the supplied structure type from which common values will |
| 1059 | // be extracted. |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1060 | // |
| 1061 | // This is recursive function. If it encounters an embedded field (no field name) |
| 1062 | // that is a struct then it will recurse into that struct passing in the accessor |
| 1063 | // for the field. That will then be used in the accessors for the fields in the |
| 1064 | // embedded struct. |
| 1065 | func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc) { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1066 | for f := 0; f < structType.NumField(); f++ { |
| 1067 | field := structType.Field(f) |
| 1068 | if field.PkgPath != "" { |
| 1069 | // Ignore unexported fields. |
| 1070 | continue |
| 1071 | } |
| 1072 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1073 | // Ignore fields whose value should be kept. |
| 1074 | if proptools.HasTag(field, "sdk", "keep") { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1075 | continue |
| 1076 | } |
| 1077 | |
| 1078 | // Save a copy of the field index for use in the function. |
| 1079 | fieldIndex := f |
| 1080 | fieldGetter := func(value reflect.Value) reflect.Value { |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1081 | if containingStructAccessor != nil { |
| 1082 | // This is an embedded structure so first access the field for the embedded |
| 1083 | // structure. |
| 1084 | value = containingStructAccessor(value) |
| 1085 | } |
| 1086 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1087 | // Skip through interface and pointer values to find the structure. |
| 1088 | value = getStructValue(value) |
| 1089 | |
| 1090 | // Return the field. |
| 1091 | return value.Field(fieldIndex) |
| 1092 | } |
| 1093 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1094 | if field.Type.Kind() == reflect.Struct && field.Anonymous { |
| 1095 | // Gather fields from the embedded structure. |
| 1096 | e.gatherFields(field.Type, fieldGetter) |
| 1097 | } else { |
| 1098 | e.fieldGetters = append(e.fieldGetters, fieldGetter) |
| 1099 | } |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | func getStructValue(value reflect.Value) reflect.Value { |
| 1104 | foundStruct: |
| 1105 | for { |
| 1106 | kind := value.Kind() |
| 1107 | switch kind { |
| 1108 | case reflect.Interface, reflect.Ptr: |
| 1109 | value = value.Elem() |
| 1110 | case reflect.Struct: |
| 1111 | break foundStruct |
| 1112 | default: |
| 1113 | panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind)) |
| 1114 | } |
| 1115 | } |
| 1116 | return value |
| 1117 | } |
| 1118 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1119 | // Extract common properties from a slice of property structures of the same type. |
| 1120 | // |
| 1121 | // All the property structures must be of the same type. |
| 1122 | // commonProperties - must be a pointer to the structure into which common properties will be added. |
| 1123 | // inputPropertiesSlice - must be a slice of input properties structures. |
| 1124 | // |
| 1125 | // Iterates over each exported field (capitalized name) and checks to see whether they |
| 1126 | // have the same value (using DeepEquals) across all the input properties. If it does not then no |
| 1127 | // change is made. Otherwise, the common value is stored in the field in the commonProperties |
| 1128 | // and the field in each of the input properties structure is set to its default value. |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1129 | func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1130 | commonPropertiesValue := reflect.ValueOf(commonProperties) |
| 1131 | commonStructValue := commonPropertiesValue.Elem() |
| 1132 | propertiesStructType := commonStructValue.Type() |
| 1133 | |
| 1134 | // Create an empty structure from which default values for the field can be copied. |
| 1135 | emptyStructValue := reflect.New(propertiesStructType).Elem() |
| 1136 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1137 | for _, fieldGetter := range e.fieldGetters { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1138 | // Check to see if all the structures have the same value for the field. The commonValue |
| 1139 | // is nil on entry to the loop and if it is nil on exit then there is no common value, |
| 1140 | // otherwise it points to the common value. |
| 1141 | var commonValue *reflect.Value |
| 1142 | sliceValue := reflect.ValueOf(inputPropertiesSlice) |
| 1143 | |
| 1144 | for i := 0; i < sliceValue.Len(); i++ { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1145 | itemValue := sliceValue.Index(i) |
| 1146 | fieldValue := fieldGetter(itemValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1147 | |
| 1148 | if commonValue == nil { |
| 1149 | // Use the first value as the commonProperties value. |
| 1150 | commonValue = &fieldValue |
| 1151 | } else { |
| 1152 | // If the value does not match the current common value then there is |
| 1153 | // no value in common so break out. |
| 1154 | if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) { |
| 1155 | commonValue = nil |
| 1156 | break |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | // If the fields all have a common value then store it in the common struct field |
| 1162 | // and set the input struct's field to the empty value. |
| 1163 | if commonValue != nil { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1164 | emptyValue := fieldGetter(emptyStructValue) |
| 1165 | fieldGetter(commonStructValue).Set(*commonValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1166 | for i := 0; i < sliceValue.Len(); i++ { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1167 | itemValue := sliceValue.Index(i) |
| 1168 | fieldValue := fieldGetter(itemValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1169 | fieldValue.Set(emptyValue) |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | } |