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