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