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" |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 25 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 26 | "github.com/google/blueprint" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 27 | "github.com/google/blueprint/proptools" |
| 28 | |
| 29 | "android/soong/android" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 30 | ) |
| 31 | |
Paul Duffin | 64fb526 | 2021-05-05 21:36:04 +0100 | [diff] [blame] | 32 | // Environment variables that affect the generated snapshot |
| 33 | // ======================================================== |
| 34 | // |
| 35 | // SOONG_SDK_SNAPSHOT_PREFER |
| 36 | // By default every unversioned module in the generated snapshot has prefer: false. Building it |
| 37 | // with SOONG_SDK_SNAPSHOT_PREFER=true will force them to use prefer: true. |
| 38 | // |
| 39 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 40 | var pctx = android.NewPackageContext("android/soong/sdk") |
| 41 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 42 | var ( |
| 43 | repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip", |
| 44 | blueprint.RuleParams{ |
Paul Duffin | ce482dc | 2019-12-09 19:58:17 +0000 | [diff] [blame] | 45 | Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`, |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 46 | CommandDeps: []string{ |
| 47 | "${config.Zip2ZipCmd}", |
| 48 | }, |
| 49 | }, |
| 50 | "destdir") |
| 51 | |
| 52 | zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles", |
| 53 | blueprint.RuleParams{ |
Colin Cross | 053fca1 | 2020-08-19 13:51:47 -0700 | [diff] [blame] | 54 | Command: `${config.SoongZipCmd} -C $basedir -r $out.rsp -o $out`, |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 55 | CommandDeps: []string{ |
| 56 | "${config.SoongZipCmd}", |
| 57 | }, |
| 58 | Rspfile: "$out.rsp", |
| 59 | RspfileContent: "$in", |
| 60 | }, |
| 61 | "basedir") |
| 62 | |
| 63 | mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips", |
| 64 | blueprint.RuleParams{ |
| 65 | Command: `${config.MergeZipsCmd} $out $in`, |
| 66 | CommandDeps: []string{ |
| 67 | "${config.MergeZipsCmd}", |
| 68 | }, |
| 69 | }) |
| 70 | ) |
| 71 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 72 | type generatedContents struct { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 73 | content strings.Builder |
| 74 | indentLevel int |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 77 | // generatedFile abstracts operations for writing contents into a file and emit a build rule |
| 78 | // for the file. |
| 79 | type generatedFile struct { |
| 80 | generatedContents |
| 81 | path android.OutputPath |
| 82 | } |
| 83 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 84 | func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 85 | return &generatedFile{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 86 | path: android.PathForModuleOut(ctx, path...).OutputPath, |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 90 | func (gc *generatedContents) Indent() { |
| 91 | gc.indentLevel++ |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 92 | } |
| 93 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 94 | func (gc *generatedContents) Dedent() { |
| 95 | gc.indentLevel-- |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 98 | func (gc *generatedContents) Printfln(format string, args ...interface{}) { |
Paul Duffin | 1110827 | 2020-05-11 22:59:25 +0100 | [diff] [blame] | 99 | fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\n", args...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 103 | rb := android.NewRuleBuilder(pctx, ctx) |
Paul Duffin | 1110827 | 2020-05-11 22:59:25 +0100 | [diff] [blame] | 104 | |
| 105 | content := gf.content.String() |
| 106 | |
| 107 | // ninja consumes newline characters in rspfile_content. Prevent it by |
| 108 | // escaping the backslash in the newline character. The extra backslash |
| 109 | // is removed when the rspfile is written to the actual script file |
| 110 | content = strings.ReplaceAll(content, "\n", "\\n") |
| 111 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 112 | rb.Command(). |
| 113 | Implicits(implicits). |
Martin Stjernholm | ee9b24e | 2021-04-20 15:54:21 +0100 | [diff] [blame] | 114 | Text("echo -n").Text(proptools.ShellEscape(content)). |
Paul Duffin | 1110827 | 2020-05-11 22:59:25 +0100 | [diff] [blame] | 115 | // convert \\n to \n |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 116 | Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path) |
| 117 | rb.Command(). |
| 118 | Text("chmod a+x").Output(gf.path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 119 | rb.Build(gf.path.Base(), "Build "+gf.path.Base()) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 120 | } |
| 121 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 122 | // Collect all the members. |
| 123 | // |
Paul Duffin | cc3132e | 2021-04-24 01:10:30 +0100 | [diff] [blame] | 124 | // Updates the sdk module with a list of sdkMemberVariantDeps and details as to which multilibs |
| 125 | // (32/64/both) are used by this sdk variant. |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 126 | func (s *sdk) collectMembers(ctx android.ModuleContext) { |
| 127 | s.multilibUsages = multilibNone |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 128 | ctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 129 | tag := ctx.OtherModuleDependencyTag(child) |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 130 | if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok { |
Paul Duffin | eee466e | 2021-04-27 23:17:56 +0100 | [diff] [blame] | 131 | memberType := memberTag.SdkMemberType(child) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 132 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 133 | // 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] | 134 | if !memberType.IsInstance(child) { |
| 135 | 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] | 136 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 137 | |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 138 | // Keep track of which multilib variants are used by the sdk. |
| 139 | s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType) |
| 140 | |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 141 | export := memberTag.ExportMember() |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 142 | s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{s, memberType, child.(android.SdkAware), export}) |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 143 | |
Paul Duffin | 2d3da31 | 2021-05-06 12:02:27 +0100 | [diff] [blame] | 144 | // Recurse down into the member's dependencies as it may have dependencies that need to be |
| 145 | // automatically added to the sdk. |
| 146 | return true |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 147 | } |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 148 | |
| 149 | return false |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 150 | }) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Paul Duffin | cc3132e | 2021-04-24 01:10:30 +0100 | [diff] [blame] | 153 | // groupMemberVariantsByMemberThenType groups the member variant dependencies so that all the |
| 154 | // variants of each member are grouped together within an sdkMember instance. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 155 | // |
Paul Duffin | cc3132e | 2021-04-24 01:10:30 +0100 | [diff] [blame] | 156 | // The sdkMember instances are then grouped into slices by member type. Within each such slice the |
| 157 | // sdkMember instances appear in the order they were added as dependencies. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 158 | // |
Paul Duffin | cc3132e | 2021-04-24 01:10:30 +0100 | [diff] [blame] | 159 | // Finally, the member type slices are concatenated together to form a single slice. The order in |
| 160 | // which they are concatenated is the order in which the member types were registered in the |
| 161 | // android.SdkMemberTypesRegistry. |
| 162 | func (s *sdk) groupMemberVariantsByMemberThenType(ctx android.ModuleContext, memberVariantDeps []sdkMemberVariantDep) []*sdkMember { |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 163 | byType := make(map[android.SdkMemberType][]*sdkMember) |
| 164 | byName := make(map[string]*sdkMember) |
| 165 | |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 166 | for _, memberVariantDep := range memberVariantDeps { |
| 167 | memberType := memberVariantDep.memberType |
| 168 | variant := memberVariantDep.variant |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 169 | |
| 170 | name := ctx.OtherModuleName(variant) |
| 171 | member := byName[name] |
| 172 | if member == nil { |
| 173 | member = &sdkMember{memberType: memberType, name: name} |
| 174 | byName[name] = member |
| 175 | byType[memberType] = append(byType[memberType], member) |
| 176 | } |
| 177 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 178 | // Only append new variants to the list. This is needed because a member can be both |
| 179 | // exported by the sdk and also be a transitive sdk member. |
| 180 | member.variants = appendUniqueVariants(member.variants, variant) |
| 181 | } |
| 182 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 183 | var members []*sdkMember |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 184 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 185 | membersOfType := byType[memberListProperty.memberType] |
| 186 | members = append(members, membersOfType...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 187 | } |
| 188 | |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 189 | return members |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 190 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 191 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 192 | func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware { |
| 193 | for _, v := range variants { |
| 194 | if v == newVariant { |
| 195 | return variants |
| 196 | } |
| 197 | } |
| 198 | return append(variants, newVariant) |
| 199 | } |
| 200 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 201 | // SDK directory structure |
| 202 | // <sdk_root>/ |
| 203 | // Android.bp : definition of a 'sdk' module is here. This is a hand-made one. |
| 204 | // <api_ver>/ : below this directory are all auto-generated |
| 205 | // Android.bp : definition of 'sdk_snapshot' module is here |
| 206 | // aidl/ |
| 207 | // frameworks/base/core/..../IFoo.aidl : an exported AIDL file |
| 208 | // java/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 209 | // <module_name>.jar : the stub jar for a java library 'module_name' |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 210 | // include/ |
| 211 | // bionic/libc/include/stdlib.h : an exported header file |
| 212 | // include_gen/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 213 | // <module_name>/com/android/.../IFoo.h : a generated header file |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 214 | // <arch>/include/ : arch-specific exported headers |
| 215 | // <arch>/include_gen/ : arch-specific generated headers |
| 216 | // <arch>/lib/ |
| 217 | // libFoo.so : a stub library |
| 218 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 219 | // A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 220 | // This isn't visible to users, so could be changed in future. |
| 221 | func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string { |
| 222 | return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version |
| 223 | } |
| 224 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 225 | // buildSnapshot is the main function in this source file. It creates rules to copy |
| 226 | // the contents (header files, stub libraries, etc) into the zip file. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 227 | func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath { |
| 228 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 229 | allMembersByName := make(map[string]struct{}) |
| 230 | exportedMembersByName := make(map[string]struct{}) |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 231 | var memberVariantDeps []sdkMemberVariantDep |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 232 | for _, sdkVariant := range sdkVariants { |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 233 | memberVariantDeps = append(memberVariantDeps, sdkVariant.memberVariantDeps...) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 234 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 235 | // Record the names of all the members, both explicitly specified and implicitly |
| 236 | // included. |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 237 | for _, memberVariantDep := range sdkVariant.memberVariantDeps { |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 238 | name := memberVariantDep.variant.Name() |
| 239 | allMembersByName[name] = struct{}{} |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 240 | |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 241 | if memberVariantDep.export { |
| 242 | exportedMembersByName[name] = struct{}{} |
| 243 | } |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 244 | } |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 247 | snapshotDir := android.PathForModuleOut(ctx, "snapshot") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 248 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 249 | bp := newGeneratedFile(ctx, "snapshot", "Android.bp") |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 250 | |
| 251 | bpFile := &bpFile{ |
| 252 | modules: make(map[string]*bpModule), |
| 253 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 254 | |
| 255 | builder := &snapshotBuilder{ |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 256 | ctx: ctx, |
| 257 | sdk: s, |
| 258 | version: "current", |
| 259 | snapshotDir: snapshotDir.OutputPath, |
| 260 | copies: make(map[string]string), |
| 261 | filesToZip: []android.Path{bp.path}, |
| 262 | bpFile: bpFile, |
| 263 | prebuiltModules: make(map[string]*bpModule), |
| 264 | allMembersByName: allMembersByName, |
| 265 | exportedMembersByName: exportedMembersByName, |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 266 | } |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 267 | s.builderForTests = builder |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 268 | |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 269 | // Group the variants for each member module together and then group the members of each member |
| 270 | // type together. |
Paul Duffin | cc3132e | 2021-04-24 01:10:30 +0100 | [diff] [blame] | 271 | members := s.groupMemberVariantsByMemberThenType(ctx, memberVariantDeps) |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 272 | |
| 273 | // Create the prebuilt modules for each of the member modules. |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 274 | for _, member := range members { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 275 | memberType := member.memberType |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 276 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 277 | memberCtx := &memberContext{ctx, builder, memberType, member.name} |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 278 | |
| 279 | prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member) |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 280 | s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule)) |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 281 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 282 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 283 | // Create a transformer that will transform an unversioned module into a versioned module. |
| 284 | unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder} |
| 285 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 286 | // Create a transformer that will transform an unversioned module by replacing any references |
| 287 | // to internal members with a unique module name and setting prefer: false. |
Paul Duffin | 64fb526 | 2021-05-05 21:36:04 +0100 | [diff] [blame] | 288 | unversionedTransformer := unversionedTransformation{ |
| 289 | builder: builder, |
Paul Duffin | 64fb526 | 2021-05-05 21:36:04 +0100 | [diff] [blame] | 290 | } |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 291 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 292 | for _, unversioned := range builder.prebuiltOrder { |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 293 | // Prune any empty property sets. |
| 294 | unversioned = unversioned.transform(pruneEmptySetTransformer{}) |
| 295 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 296 | // 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] | 297 | versioned := unversioned.deepCopy() |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 298 | |
| 299 | // Transform the unversioned module into a versioned one. |
| 300 | versioned.transform(unversionedToVersionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 301 | bpFile.AddModule(versioned) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 302 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 303 | // Transform the unversioned module to make it suitable for use in the snapshot. |
| 304 | unversioned.transform(unversionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 305 | bpFile.AddModule(unversioned) |
| 306 | } |
| 307 | |
Paul Duffin | 26197a6 | 2021-04-24 00:34:10 +0100 | [diff] [blame] | 308 | // Add the sdk/module_exports_snapshot module to the bp file. |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 309 | s.addSnapshotModule(ctx, builder, sdkVariants, memberVariantDeps) |
Paul Duffin | 26197a6 | 2021-04-24 00:34:10 +0100 | [diff] [blame] | 310 | |
| 311 | // generate Android.bp |
| 312 | bp = newGeneratedFile(ctx, "snapshot", "Android.bp") |
| 313 | generateBpContents(&bp.generatedContents, bpFile) |
| 314 | |
| 315 | contents := bp.content.String() |
| 316 | syntaxCheckSnapshotBpFile(ctx, contents) |
| 317 | |
| 318 | bp.build(pctx, ctx, nil) |
| 319 | |
| 320 | filesToZip := builder.filesToZip |
| 321 | |
| 322 | // zip them all |
| 323 | outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath |
| 324 | outputDesc := "Building snapshot for " + ctx.ModuleName() |
| 325 | |
| 326 | // If there are no zips to merge then generate the output zip directly. |
| 327 | // Otherwise, generate an intermediate zip file into which other zips can be |
| 328 | // merged. |
| 329 | var zipFile android.OutputPath |
| 330 | var desc string |
| 331 | if len(builder.zipsToMerge) == 0 { |
| 332 | zipFile = outputZipFile |
| 333 | desc = outputDesc |
| 334 | } else { |
| 335 | zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath |
| 336 | desc = "Building intermediate snapshot for " + ctx.ModuleName() |
| 337 | } |
| 338 | |
| 339 | ctx.Build(pctx, android.BuildParams{ |
| 340 | Description: desc, |
| 341 | Rule: zipFiles, |
| 342 | Inputs: filesToZip, |
| 343 | Output: zipFile, |
| 344 | Args: map[string]string{ |
| 345 | "basedir": builder.snapshotDir.String(), |
| 346 | }, |
| 347 | }) |
| 348 | |
| 349 | if len(builder.zipsToMerge) != 0 { |
| 350 | ctx.Build(pctx, android.BuildParams{ |
| 351 | Description: outputDesc, |
| 352 | Rule: mergeZips, |
| 353 | Input: zipFile, |
| 354 | Inputs: builder.zipsToMerge, |
| 355 | Output: outputZipFile, |
| 356 | }) |
| 357 | } |
| 358 | |
| 359 | return outputZipFile |
| 360 | } |
| 361 | |
| 362 | // addSnapshotModule adds the sdk_snapshot/module_exports_snapshot module to the builder. |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 363 | func (s *sdk) addSnapshotModule(ctx android.ModuleContext, builder *snapshotBuilder, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) { |
Paul Duffin | 26197a6 | 2021-04-24 00:34:10 +0100 | [diff] [blame] | 364 | bpFile := builder.bpFile |
| 365 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 366 | snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 367 | var snapshotModuleType string |
| 368 | if s.properties.Module_exports { |
| 369 | snapshotModuleType = "module_exports_snapshot" |
| 370 | } else { |
| 371 | snapshotModuleType = "sdk_snapshot" |
| 372 | } |
| 373 | snapshotModule := bpFile.newModule(snapshotModuleType) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 374 | snapshotModule.AddProperty("name", snapshotName) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 375 | |
| 376 | // Make sure that the snapshot has the same visibility as the sdk. |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 377 | visibility := android.EffectiveVisibilityRules(ctx, s).Strings() |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 378 | if len(visibility) != 0 { |
| 379 | snapshotModule.AddProperty("visibility", visibility) |
| 380 | } |
| 381 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 382 | addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 383 | |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 384 | combinedPropertiesList := s.collateSnapshotModuleInfo(ctx, sdkVariants, memberVariantDeps) |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 385 | commonCombinedProperties := s.optimizeSnapshotModuleProperties(ctx, combinedPropertiesList) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 386 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 387 | s.addSnapshotPropertiesToPropertySet(builder, snapshotModule, commonCombinedProperties) |
Martin Stjernholm | 4cfa2c6 | 2020-07-10 19:55:36 +0100 | [diff] [blame] | 388 | |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 389 | targetPropertySet := snapshotModule.AddPropertySet("target") |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 390 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 391 | // Create a mapping from osType to combined properties. |
| 392 | osTypeToCombinedProperties := map[android.OsType]*combinedSnapshotModuleProperties{} |
| 393 | for _, combined := range combinedPropertiesList { |
| 394 | osTypeToCombinedProperties[combined.sdkVariant.Os()] = combined |
| 395 | } |
| 396 | |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 397 | // Iterate over the os types in a fixed order. |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 398 | for _, osType := range s.getPossibleOsTypes() { |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 399 | if combined, ok := osTypeToCombinedProperties[osType]; ok { |
Paul Duffin | cc3132e | 2021-04-24 01:10:30 +0100 | [diff] [blame] | 400 | osPropertySet := targetPropertySet.AddPropertySet(osType.Name) |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 401 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 402 | s.addSnapshotPropertiesToPropertySet(builder, osPropertySet, combined) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 403 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 404 | } |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 405 | |
Jiyong Park | 8fe14e6 | 2020-10-19 22:47:34 +0900 | [diff] [blame] | 406 | // If host is supported and any member is host OS dependent then disable host |
| 407 | // by default, so that we can enable each host OS variant explicitly. This |
| 408 | // avoids problems with implicitly enabled OS variants when the snapshot is |
| 409 | // used, which might be different from this run (e.g. different build OS). |
| 410 | if s.HostSupported() { |
| 411 | var supportedHostTargets []string |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 412 | for _, memberVariantDep := range memberVariantDeps { |
| 413 | if memberVariantDep.memberType.IsHostOsDependent() && memberVariantDep.variant.Target().Os.Class == android.Host { |
| 414 | targetString := memberVariantDep.variant.Target().Os.String() + "_" + memberVariantDep.variant.Target().Arch.ArchType.String() |
Jiyong Park | 8fe14e6 | 2020-10-19 22:47:34 +0900 | [diff] [blame] | 415 | if !android.InList(targetString, supportedHostTargets) { |
| 416 | supportedHostTargets = append(supportedHostTargets, targetString) |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | if len(supportedHostTargets) > 0 { |
| 421 | hostPropertySet := targetPropertySet.AddPropertySet("host") |
| 422 | hostPropertySet.AddProperty("enabled", false) |
| 423 | } |
| 424 | // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host. |
| 425 | for _, hostTarget := range supportedHostTargets { |
| 426 | propertySet := targetPropertySet.AddPropertySet(hostTarget) |
| 427 | propertySet.AddProperty("enabled", true) |
| 428 | } |
| 429 | } |
| 430 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 431 | // Prune any empty property sets. |
| 432 | snapshotModule.transform(pruneEmptySetTransformer{}) |
| 433 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 434 | bpFile.AddModule(snapshotModule) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 435 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 436 | |
Paul Duffin | f88d8e0 | 2020-05-07 20:21:34 +0100 | [diff] [blame] | 437 | // Check the syntax of the generated Android.bp file contents and if they are |
| 438 | // invalid then log an error with the contents (tagged with line numbers) and the |
| 439 | // errors that were found so that it is easy to see where the problem lies. |
| 440 | func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) { |
| 441 | errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents) |
| 442 | if len(errs) != 0 { |
| 443 | message := &strings.Builder{} |
| 444 | _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot: |
| 445 | |
| 446 | Generated Android.bp contents |
| 447 | ======================================================================== |
| 448 | `) |
| 449 | for i, line := range strings.Split(contents, "\n") { |
| 450 | _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line) |
| 451 | } |
| 452 | |
| 453 | _, _ = fmt.Fprint(message, ` |
| 454 | ======================================================================== |
| 455 | |
| 456 | Errors found: |
| 457 | `) |
| 458 | |
| 459 | for _, err := range errs { |
| 460 | _, _ = fmt.Fprintf(message, "%s\n", err.Error()) |
| 461 | } |
| 462 | |
| 463 | ctx.ModuleErrorf("%s", message.String()) |
| 464 | } |
| 465 | } |
| 466 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 467 | func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) { |
| 468 | err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice) |
| 469 | if err != nil { |
| 470 | ctx.ModuleErrorf("error extracting common properties: %s", err) |
| 471 | } |
| 472 | } |
| 473 | |
Paul Duffin | fbe470e | 2021-04-24 12:37:13 +0100 | [diff] [blame] | 474 | // snapshotModuleStaticProperties contains snapshot static (i.e. not dynamically generated) properties. |
| 475 | type snapshotModuleStaticProperties struct { |
| 476 | Compile_multilib string `android:"arch_variant"` |
| 477 | } |
| 478 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 479 | // combinedSnapshotModuleProperties are the properties that are associated with the snapshot module. |
| 480 | type combinedSnapshotModuleProperties struct { |
| 481 | // The sdk variant from which this information was collected. |
| 482 | sdkVariant *sdk |
| 483 | |
| 484 | // Static snapshot module properties. |
| 485 | staticProperties *snapshotModuleStaticProperties |
| 486 | |
| 487 | // The dynamically generated member list properties. |
| 488 | dynamicProperties interface{} |
| 489 | } |
| 490 | |
| 491 | // collateSnapshotModuleInfo collates all the snapshot module info from supplied sdk variants. |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 492 | func (s *sdk) collateSnapshotModuleInfo(ctx android.BaseModuleContext, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) []*combinedSnapshotModuleProperties { |
| 493 | sdkVariantToCombinedProperties := map[*sdk]*combinedSnapshotModuleProperties{} |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 494 | var list []*combinedSnapshotModuleProperties |
| 495 | for _, sdkVariant := range sdkVariants { |
| 496 | staticProperties := &snapshotModuleStaticProperties{ |
| 497 | Compile_multilib: sdkVariant.multilibUsages.String(), |
| 498 | } |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 499 | dynamicProperties := s.dynamicSdkMemberTypes.createMemberListProperties() |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 500 | |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 501 | combinedProperties := &combinedSnapshotModuleProperties{ |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 502 | sdkVariant: sdkVariant, |
| 503 | staticProperties: staticProperties, |
| 504 | dynamicProperties: dynamicProperties, |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 505 | } |
| 506 | sdkVariantToCombinedProperties[sdkVariant] = combinedProperties |
| 507 | |
| 508 | list = append(list, combinedProperties) |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 509 | } |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 510 | |
| 511 | for _, memberVariantDep := range memberVariantDeps { |
| 512 | // If the member dependency is internal then do not add the dependency to the snapshot member |
| 513 | // list properties. |
| 514 | if !memberVariantDep.export { |
| 515 | continue |
| 516 | } |
| 517 | |
| 518 | combined := sdkVariantToCombinedProperties[memberVariantDep.sdkVariant] |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 519 | memberListProperty := s.memberListProperty(memberVariantDep.memberType) |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 520 | memberName := ctx.OtherModuleName(memberVariantDep.variant) |
| 521 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 522 | if memberListProperty.getter == nil { |
| 523 | continue |
| 524 | } |
| 525 | |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 526 | // Append the member to the appropriate list, if it is not already present in the list. |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 527 | memberList := memberListProperty.getter(combined.dynamicProperties) |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 528 | if !android.InList(memberName, memberList) { |
| 529 | memberList = append(memberList, memberName) |
| 530 | } |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 531 | memberListProperty.setter(combined.dynamicProperties, memberList) |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 534 | return list |
| 535 | } |
| 536 | |
| 537 | func (s *sdk) optimizeSnapshotModuleProperties(ctx android.ModuleContext, list []*combinedSnapshotModuleProperties) *combinedSnapshotModuleProperties { |
| 538 | |
| 539 | // Extract the dynamic properties and add them to a list of propertiesContainer. |
| 540 | propertyContainers := []propertiesContainer{} |
| 541 | for _, i := range list { |
| 542 | propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{ |
| 543 | sdkVariant: i.sdkVariant, |
| 544 | properties: i.dynamicProperties, |
| 545 | }) |
| 546 | } |
| 547 | |
| 548 | // Extract the common members, removing them from the original properties. |
| 549 | commonDynamicProperties := s.dynamicSdkMemberTypes.createMemberListProperties() |
| 550 | extractor := newCommonValueExtractor(commonDynamicProperties) |
| 551 | extractCommonProperties(ctx, extractor, commonDynamicProperties, propertyContainers) |
| 552 | |
| 553 | // Extract the static properties and add them to a list of propertiesContainer. |
| 554 | propertyContainers = []propertiesContainer{} |
| 555 | for _, i := range list { |
| 556 | propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{ |
| 557 | sdkVariant: i.sdkVariant, |
| 558 | properties: i.staticProperties, |
| 559 | }) |
| 560 | } |
| 561 | |
| 562 | commonStaticProperties := &snapshotModuleStaticProperties{} |
| 563 | extractor = newCommonValueExtractor(commonStaticProperties) |
| 564 | extractCommonProperties(ctx, extractor, &commonStaticProperties, propertyContainers) |
| 565 | |
| 566 | return &combinedSnapshotModuleProperties{ |
| 567 | sdkVariant: nil, |
| 568 | staticProperties: commonStaticProperties, |
| 569 | dynamicProperties: commonDynamicProperties, |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | func (s *sdk) addSnapshotPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, combined *combinedSnapshotModuleProperties) { |
| 574 | staticProperties := combined.staticProperties |
Paul Duffin | fbe470e | 2021-04-24 12:37:13 +0100 | [diff] [blame] | 575 | multilib := staticProperties.Compile_multilib |
| 576 | if multilib != "" && multilib != "both" { |
| 577 | // Compile_multilib defaults to both so only needs to be set when it's specified and not both. |
| 578 | propertySet.AddProperty("compile_multilib", multilib) |
| 579 | } |
| 580 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 581 | dynamicMemberTypeListProperties := combined.dynamicProperties |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 582 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 583 | if memberListProperty.getter == nil { |
| 584 | continue |
| 585 | } |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 586 | names := memberListProperty.getter(dynamicMemberTypeListProperties) |
| 587 | if len(names) > 0 { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 588 | propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names, false)) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 593 | type propertyTag struct { |
| 594 | name string |
| 595 | } |
| 596 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 597 | // A BpPropertyTag to add to a property that contains references to other sdk members. |
| 598 | // |
| 599 | // This will cause the references to be rewritten to a versioned reference in the version |
| 600 | // specific instance of a snapshot module. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 601 | var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"} |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 602 | var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"} |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 603 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 604 | // A BpPropertyTag that indicates the property should only be present in the versioned |
| 605 | // module. |
| 606 | // |
| 607 | // This will cause the property to be removed from the unversioned instance of a |
| 608 | // snapshot module. |
| 609 | var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"} |
| 610 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 611 | type unversionedToVersionedTransformation struct { |
| 612 | identityTransformation |
| 613 | builder *snapshotBuilder |
| 614 | } |
| 615 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 616 | func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule { |
| 617 | // Use a versioned name for the module but remember the original name for the |
| 618 | // snapshot. |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 619 | name := module.Name() |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 620 | module.setProperty("name", t.builder.versionedSdkMemberName(name, true)) |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 621 | module.insertAfter("name", "sdk_member_name", name) |
Paul Duffin | 83ad956 | 2021-05-10 23:49:04 +0100 | [diff] [blame] | 622 | // Remove the prefer property if present as versioned modules never need marking with prefer. |
| 623 | module.removeProperty("prefer") |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 624 | return module |
| 625 | } |
| 626 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 627 | 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] | 628 | if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag { |
| 629 | required := tag == requiredSdkMemberReferencePropertyTag |
| 630 | return t.builder.versionedSdkMemberNames(value.([]string), required), tag |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 631 | } else { |
| 632 | return value, tag |
| 633 | } |
| 634 | } |
| 635 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 636 | type unversionedTransformation struct { |
| 637 | identityTransformation |
| 638 | builder *snapshotBuilder |
| 639 | } |
| 640 | |
| 641 | func (t unversionedTransformation) transformModule(module *bpModule) *bpModule { |
| 642 | // If the module is an internal member then use a unique name for it. |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 643 | name := module.Name() |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 644 | module.setProperty("name", t.builder.unversionedSdkMemberName(name, true)) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 645 | return module |
| 646 | } |
| 647 | |
| 648 | 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] | 649 | if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag { |
| 650 | required := tag == requiredSdkMemberReferencePropertyTag |
| 651 | return t.builder.unversionedSdkMemberNames(value.([]string), required), tag |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 652 | } else if tag == sdkVersionedOnlyPropertyTag { |
| 653 | // The property is not allowed in the unversioned module so remove it. |
| 654 | return nil, nil |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 655 | } else { |
| 656 | return value, tag |
| 657 | } |
| 658 | } |
| 659 | |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 660 | type pruneEmptySetTransformer struct { |
| 661 | identityTransformation |
| 662 | } |
| 663 | |
| 664 | var _ bpTransformer = (*pruneEmptySetTransformer)(nil) |
| 665 | |
| 666 | func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { |
| 667 | if len(propertySet.properties) == 0 { |
| 668 | return nil, nil |
| 669 | } else { |
| 670 | return propertySet, tag |
| 671 | } |
| 672 | } |
| 673 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 674 | func generateBpContents(contents *generatedContents, bpFile *bpFile) { |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 675 | generateFilteredBpContents(contents, bpFile, func(*bpModule) bool { |
| 676 | return true |
| 677 | }) |
| 678 | } |
| 679 | |
| 680 | func generateFilteredBpContents(contents *generatedContents, bpFile *bpFile, moduleFilter func(module *bpModule) bool) { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 681 | contents.Printfln("// This is auto-generated. DO NOT EDIT.") |
| 682 | for _, bpModule := range bpFile.order { |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 683 | if moduleFilter(bpModule) { |
| 684 | contents.Printfln("") |
| 685 | contents.Printfln("%s {", bpModule.moduleType) |
| 686 | outputPropertySet(contents, bpModule.bpPropertySet) |
| 687 | contents.Printfln("}") |
| 688 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 689 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | func outputPropertySet(contents *generatedContents, set *bpPropertySet) { |
| 693 | contents.Indent() |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 694 | |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 695 | addComment := func(name string) { |
| 696 | if text, ok := set.comments[name]; ok { |
| 697 | for _, line := range strings.Split(text, "\n") { |
| 698 | contents.Printfln("// %s", line) |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 703 | // Output the properties first, followed by the nested sets. This ensures a |
| 704 | // consistent output irrespective of whether property sets are created before |
| 705 | // or after the properties. This simplifies the creation of the module. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 706 | for _, name := range set.order { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 707 | value := set.getValue(name) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 708 | |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 709 | // Do not write property sets in the properties phase. |
| 710 | if _, ok := value.(*bpPropertySet); ok { |
| 711 | continue |
| 712 | } |
| 713 | |
| 714 | addComment(name) |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 715 | switch v := value.(type) { |
| 716 | case []string: |
| 717 | length := len(v) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 718 | if length > 1 { |
| 719 | contents.Printfln("%s: [", name) |
| 720 | contents.Indent() |
| 721 | for i := 0; i < length; i = i + 1 { |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 722 | contents.Printfln("%q,", v[i]) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 723 | } |
| 724 | contents.Dedent() |
| 725 | contents.Printfln("],") |
| 726 | } else if length == 0 { |
| 727 | contents.Printfln("%s: [],", name) |
| 728 | } else { |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 729 | contents.Printfln("%s: [%q],", name, v[0]) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 730 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 731 | |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 732 | case bool: |
| 733 | contents.Printfln("%s: %t,", name, v) |
| 734 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 735 | default: |
| 736 | contents.Printfln("%s: %q,", name, value) |
| 737 | } |
| 738 | } |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 739 | |
| 740 | for _, name := range set.order { |
| 741 | value := set.getValue(name) |
| 742 | |
| 743 | // Only write property sets in the sets phase. |
| 744 | switch v := value.(type) { |
| 745 | case *bpPropertySet: |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 746 | addComment(name) |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 747 | contents.Printfln("%s: {", name) |
| 748 | outputPropertySet(contents, v) |
| 749 | contents.Printfln("},") |
| 750 | } |
| 751 | } |
| 752 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 753 | contents.Dedent() |
| 754 | } |
| 755 | |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 756 | func (s *sdk) GetAndroidBpContentsForTests() string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 757 | contents := &generatedContents{} |
| 758 | generateBpContents(contents, s.builderForTests.bpFile) |
| 759 | return contents.content.String() |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 762 | func (s *sdk) GetUnversionedAndroidBpContentsForTests() string { |
| 763 | contents := &generatedContents{} |
| 764 | generateFilteredBpContents(contents, s.builderForTests.bpFile, func(module *bpModule) bool { |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 765 | name := module.Name() |
| 766 | // Include modules that are either unversioned or have no name. |
| 767 | return !strings.Contains(name, "@") |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 768 | }) |
| 769 | return contents.content.String() |
| 770 | } |
| 771 | |
| 772 | func (s *sdk) GetVersionedAndroidBpContentsForTests() string { |
| 773 | contents := &generatedContents{} |
| 774 | generateFilteredBpContents(contents, s.builderForTests.bpFile, func(module *bpModule) bool { |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame^] | 775 | name := module.Name() |
| 776 | // Include modules that are either versioned or have no name. |
| 777 | return name == "" || strings.Contains(name, "@") |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 778 | }) |
| 779 | return contents.content.String() |
| 780 | } |
| 781 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 782 | type snapshotBuilder struct { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 783 | ctx android.ModuleContext |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 784 | sdk *sdk |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 785 | version string |
| 786 | snapshotDir android.OutputPath |
| 787 | bpFile *bpFile |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 788 | |
| 789 | // Map from destination to source of each copy - used to eliminate duplicates and |
| 790 | // detect conflicts. |
| 791 | copies map[string]string |
| 792 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 793 | filesToZip android.Paths |
| 794 | zipsToMerge android.Paths |
| 795 | |
| 796 | prebuiltModules map[string]*bpModule |
| 797 | prebuiltOrder []*bpModule |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 798 | |
| 799 | // The set of all members by name. |
| 800 | allMembersByName map[string]struct{} |
| 801 | |
| 802 | // The set of exported members by name. |
| 803 | exportedMembersByName map[string]struct{} |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) { |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 807 | if existing, ok := s.copies[dest]; ok { |
| 808 | if existing != src.String() { |
| 809 | s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src) |
| 810 | return |
| 811 | } |
| 812 | } else { |
| 813 | path := s.snapshotDir.Join(s.ctx, dest) |
| 814 | s.ctx.Build(pctx, android.BuildParams{ |
| 815 | Rule: android.Cp, |
| 816 | Input: src, |
| 817 | Output: path, |
| 818 | }) |
| 819 | s.filesToZip = append(s.filesToZip, path) |
| 820 | |
| 821 | s.copies[dest] = src.String() |
| 822 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 825 | func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) { |
| 826 | ctx := s.ctx |
| 827 | |
| 828 | // Repackage the zip file so that the entries are in the destDir directory. |
| 829 | // This will allow the zip file to be merged into the snapshot. |
| 830 | tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 831 | |
| 832 | ctx.Build(pctx, android.BuildParams{ |
| 833 | Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(), |
| 834 | Rule: repackageZip, |
| 835 | Input: zipPath, |
| 836 | Output: tmpZipPath, |
| 837 | Args: map[string]string{ |
| 838 | "destdir": destDir, |
| 839 | }, |
| 840 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 841 | |
| 842 | // Add the repackaged zip file to the files to merge. |
| 843 | s.zipsToMerge = append(s.zipsToMerge, tmpZipPath) |
| 844 | } |
| 845 | |
Paul Duffin | 9d8d609 | 2019-12-05 18:19:29 +0000 | [diff] [blame] | 846 | func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule { |
| 847 | name := member.Name() |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 848 | if s.prebuiltModules[name] != nil { |
| 849 | panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name)) |
| 850 | } |
| 851 | |
| 852 | m := s.bpFile.newModule(moduleType) |
| 853 | m.AddProperty("name", name) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 854 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 855 | variant := member.Variants()[0] |
| 856 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 857 | if s.isInternalMember(name) { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 858 | // An internal member is only referenced from the sdk snapshot which is in the |
| 859 | // same package so can be marked as private. |
| 860 | m.AddProperty("visibility", []string{"//visibility:private"}) |
| 861 | } else { |
| 862 | // Extract visibility information from a member variant. All variants have the same |
| 863 | // visibility so it doesn't matter which one is used. |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 864 | visibilityRules := android.EffectiveVisibilityRules(s.ctx, variant) |
| 865 | |
| 866 | // Add any additional visibility rules needed for the prebuilts to reference each other. |
| 867 | err := visibilityRules.Widen(s.sdk.properties.Prebuilt_visibility) |
| 868 | if err != nil { |
| 869 | s.ctx.PropertyErrorf("prebuilt_visibility", "%s", err) |
| 870 | } |
| 871 | |
| 872 | visibility := visibilityRules.Strings() |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 873 | if len(visibility) != 0 { |
| 874 | m.AddProperty("visibility", visibility) |
| 875 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 878 | // Where available copy apex_available properties from the member. |
| 879 | if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok { |
| 880 | apexAvailable := apexAware.ApexAvailable() |
| 881 | if len(apexAvailable) == 0 { |
| 882 | // //apex_available:platform is the default. |
| 883 | apexAvailable = []string{android.AvailableToPlatform} |
| 884 | } |
| 885 | |
| 886 | // Add in any baseline apex available settings. |
| 887 | apexAvailable = append(apexAvailable, apex.BaselineApexAvailable(member.Name())...) |
| 888 | |
| 889 | // Remove duplicates and sort. |
| 890 | apexAvailable = android.FirstUniqueStrings(apexAvailable) |
| 891 | sort.Strings(apexAvailable) |
| 892 | |
| 893 | m.AddProperty("apex_available", apexAvailable) |
| 894 | } |
| 895 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 896 | deviceSupported := false |
| 897 | hostSupported := false |
| 898 | |
| 899 | for _, variant := range member.Variants() { |
| 900 | osClass := variant.Target().Os.Class |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 901 | if osClass == android.Host { |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 902 | hostSupported = true |
| 903 | } else if osClass == android.Device { |
| 904 | deviceSupported = true |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | addHostDeviceSupportedProperties(deviceSupported, hostSupported, m) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 909 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 910 | // Disable installation in the versioned module of those modules that are ever installable. |
| 911 | if installable, ok := variant.(interface{ EverInstallable() bool }); ok { |
| 912 | if installable.EverInstallable() { |
| 913 | m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag) |
| 914 | } |
| 915 | } |
| 916 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 917 | s.prebuiltModules[name] = m |
| 918 | s.prebuiltOrder = append(s.prebuiltOrder, m) |
| 919 | return m |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 922 | func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) { |
| 923 | if !deviceSupported { |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 924 | bpModule.AddProperty("device_supported", false) |
| 925 | } |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 926 | if hostSupported { |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 927 | bpModule.AddProperty("host_supported", true) |
| 928 | } |
| 929 | } |
| 930 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 931 | func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag { |
| 932 | if required { |
| 933 | return requiredSdkMemberReferencePropertyTag |
| 934 | } else { |
| 935 | return optionalSdkMemberReferencePropertyTag |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag { |
| 940 | return optionalSdkMemberReferencePropertyTag |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 943 | // Get a versioned name appropriate for the SDK snapshot version being taken. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 944 | func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string, required bool) string { |
| 945 | if _, ok := s.allMembersByName[unversionedName]; !ok { |
| 946 | if required { |
| 947 | s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName) |
| 948 | } |
| 949 | return unversionedName |
| 950 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 951 | return versionedSdkMemberName(s.ctx, unversionedName, s.version) |
| 952 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 953 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 954 | func (s *snapshotBuilder) versionedSdkMemberNames(members []string, required bool) []string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 955 | var references []string = nil |
| 956 | for _, m := range members { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 957 | references = append(references, s.versionedSdkMemberName(m, required)) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 958 | } |
| 959 | return references |
| 960 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 961 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 962 | // Get an internal name unique to the sdk. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 963 | func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string, required bool) string { |
| 964 | if _, ok := s.allMembersByName[unversionedName]; !ok { |
| 965 | if required { |
| 966 | s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName) |
| 967 | } |
| 968 | return unversionedName |
| 969 | } |
| 970 | |
| 971 | if s.isInternalMember(unversionedName) { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 972 | return s.ctx.ModuleName() + "_" + unversionedName |
| 973 | } else { |
| 974 | return unversionedName |
| 975 | } |
| 976 | } |
| 977 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 978 | func (s *snapshotBuilder) unversionedSdkMemberNames(members []string, required bool) []string { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 979 | var references []string = nil |
| 980 | for _, m := range members { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 981 | references = append(references, s.unversionedSdkMemberName(m, required)) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 982 | } |
| 983 | return references |
| 984 | } |
| 985 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 986 | func (s *snapshotBuilder) isInternalMember(memberName string) bool { |
| 987 | _, ok := s.exportedMembersByName[memberName] |
| 988 | return !ok |
| 989 | } |
| 990 | |
Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 991 | // Add the properties from the given SdkMemberProperties to the blueprint |
| 992 | // property set. This handles common properties in SdkMemberPropertiesBase and |
| 993 | // calls the member-specific AddToPropertySet for the rest. |
| 994 | func addSdkMemberPropertiesToSet(ctx *memberContext, memberProperties android.SdkMemberProperties, targetPropertySet android.BpPropertySet) { |
| 995 | if memberProperties.Base().Compile_multilib != "" { |
| 996 | targetPropertySet.AddProperty("compile_multilib", memberProperties.Base().Compile_multilib) |
| 997 | } |
| 998 | |
| 999 | memberProperties.AddToPropertySet(ctx, targetPropertySet) |
| 1000 | } |
| 1001 | |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 1002 | // sdkMemberVariantDep represents a dependency from an sdk variant onto a member variant. |
| 1003 | type sdkMemberVariantDep struct { |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 1004 | // The sdk variant that depends (possibly indirectly) on the member variant. |
| 1005 | sdkVariant *sdk |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 1006 | memberType android.SdkMemberType |
| 1007 | variant android.SdkAware |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 1008 | export bool |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 1011 | var _ android.SdkMember = (*sdkMember)(nil) |
| 1012 | |
Paul Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 1013 | // sdkMember groups all the variants of a specific member module together along with the name of the |
| 1014 | // module and the member type. This is used to generate the prebuilt modules for a specific member. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 1015 | type sdkMember struct { |
| 1016 | memberType android.SdkMemberType |
| 1017 | name string |
| 1018 | variants []android.SdkAware |
| 1019 | } |
| 1020 | |
| 1021 | func (m *sdkMember) Name() string { |
| 1022 | return m.name |
| 1023 | } |
| 1024 | |
| 1025 | func (m *sdkMember) Variants() []android.SdkAware { |
| 1026 | return m.variants |
| 1027 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1028 | |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 1029 | // Track usages of multilib variants. |
| 1030 | type multilibUsage int |
| 1031 | |
| 1032 | const ( |
| 1033 | multilibNone multilibUsage = 0 |
| 1034 | multilib32 multilibUsage = 1 |
| 1035 | multilib64 multilibUsage = 2 |
| 1036 | multilibBoth = multilib32 | multilib64 |
| 1037 | ) |
| 1038 | |
| 1039 | // Add the multilib that is used in the arch type. |
| 1040 | func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage { |
| 1041 | multilib := archType.Multilib |
| 1042 | switch multilib { |
| 1043 | case "": |
| 1044 | return m |
| 1045 | case "lib32": |
| 1046 | return m | multilib32 |
| 1047 | case "lib64": |
| 1048 | return m | multilib64 |
| 1049 | default: |
| 1050 | panic(fmt.Errorf("Unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib)) |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | func (m multilibUsage) String() string { |
| 1055 | switch m { |
| 1056 | case multilibNone: |
| 1057 | return "" |
| 1058 | case multilib32: |
| 1059 | return "32" |
| 1060 | case multilib64: |
| 1061 | return "64" |
| 1062 | case multilibBoth: |
| 1063 | return "both" |
| 1064 | default: |
| 1065 | panic(fmt.Errorf("Unknown multilib value, found %b, expected one of %b, %b, %b or %b", |
| 1066 | m, multilibNone, multilib32, multilib64, multilibBoth)) |
| 1067 | } |
| 1068 | } |
| 1069 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1070 | type baseInfo struct { |
| 1071 | Properties android.SdkMemberProperties |
| 1072 | } |
| 1073 | |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1074 | func (b *baseInfo) optimizableProperties() interface{} { |
| 1075 | return b.Properties |
| 1076 | } |
| 1077 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1078 | type osTypeSpecificInfo struct { |
| 1079 | baseInfo |
| 1080 | |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1081 | osType android.OsType |
| 1082 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1083 | // The list of arch type specific info for this os type. |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 1084 | // |
| 1085 | // Nil if there is one variant whose arch type is common |
| 1086 | archInfos []*archTypeSpecificInfo |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1089 | var _ propertiesContainer = (*osTypeSpecificInfo)(nil) |
| 1090 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1091 | type variantPropertiesFactoryFunc func() android.SdkMemberProperties |
| 1092 | |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1093 | // Create a new osTypeSpecificInfo for the specified os type and its properties |
| 1094 | // structures populated with information from the variants. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1095 | 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] | 1096 | osInfo := &osTypeSpecificInfo{ |
| 1097 | osType: osType, |
| 1098 | } |
| 1099 | |
| 1100 | osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties { |
| 1101 | properties := variantPropertiesFactory() |
| 1102 | properties.Base().Os = osType |
| 1103 | return properties |
| 1104 | } |
| 1105 | |
| 1106 | // Create a structure into which properties common across the architectures in |
| 1107 | // this os type will be stored. |
| 1108 | osInfo.Properties = osSpecificVariantPropertiesFactory() |
| 1109 | |
| 1110 | // Group the variants by arch type. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1111 | var variantsByArchName = make(map[string][]android.Module) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1112 | var archTypes []android.ArchType |
| 1113 | for _, variant := range osTypeVariants { |
| 1114 | archType := variant.Target().Arch.ArchType |
| 1115 | archTypeName := archType.Name |
| 1116 | if _, ok := variantsByArchName[archTypeName]; !ok { |
| 1117 | archTypes = append(archTypes, archType) |
| 1118 | } |
| 1119 | |
| 1120 | variantsByArchName[archTypeName] = append(variantsByArchName[archTypeName], variant) |
| 1121 | } |
| 1122 | |
| 1123 | if commonVariants, ok := variantsByArchName["common"]; ok { |
| 1124 | if len(osTypeVariants) != 1 { |
Colin Cross | afa6a77 | 2020-07-06 17:41:08 -0700 | [diff] [blame] | 1125 | panic(fmt.Errorf("Expected to only have 1 variant when arch type is common but found %d", len(osTypeVariants))) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | // A common arch type only has one variant and its properties should be treated |
| 1129 | // as common to the os type. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1130 | osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0]) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1131 | } else { |
| 1132 | // Create an arch specific info for each supported architecture type. |
| 1133 | for _, archType := range archTypes { |
| 1134 | archTypeName := archType.Name |
| 1135 | |
| 1136 | archVariants := variantsByArchName[archTypeName] |
Jiyong Park | 8fe14e6 | 2020-10-19 22:47:34 +0900 | [diff] [blame] | 1137 | archInfo := newArchSpecificInfo(ctx, archType, osType, osSpecificVariantPropertiesFactory, archVariants) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1138 | |
| 1139 | osInfo.archInfos = append(osInfo.archInfos, archInfo) |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | return osInfo |
| 1144 | } |
| 1145 | |
| 1146 | // Optimize the properties by extracting common properties from arch type specific |
| 1147 | // properties into os type specific properties. |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1148 | func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) { |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1149 | // Nothing to do if there is only a single common architecture. |
| 1150 | if len(osInfo.archInfos) == 0 { |
| 1151 | return |
| 1152 | } |
| 1153 | |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 1154 | multilib := multilibNone |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1155 | for _, archInfo := range osInfo.archInfos { |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 1156 | multilib = multilib.addArchType(archInfo.archType) |
| 1157 | |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1158 | // Optimize the arch properties first. |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1159 | archInfo.optimizeProperties(ctx, commonValueExtractor) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1162 | extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1163 | |
| 1164 | // 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] | 1165 | osInfo.Properties.Base().Compile_multilib = multilib.String() |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | // Add the properties for an os to a property set. |
| 1169 | // |
| 1170 | // Maps the properties related to the os variants through to an appropriate |
| 1171 | // module structure that will produce equivalent set of variants when it is |
| 1172 | // processed in a build. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1173 | func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) { |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1174 | |
| 1175 | var osPropertySet android.BpPropertySet |
| 1176 | var archPropertySet android.BpPropertySet |
| 1177 | var archOsPrefix string |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 1178 | if osInfo.Properties.Base().Os_count == 1 && |
| 1179 | (osInfo.osType.Class == android.Device || !ctx.memberType.IsHostOsDependent()) { |
| 1180 | // There is only one OS type present in the variants and it shouldn't have a |
| 1181 | // variant-specific target. The latter is the case if it's either for device |
| 1182 | // where there is only one OS (android), or for host and the member type |
| 1183 | // isn't host OS dependent. |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1184 | |
| 1185 | // Create a structure that looks like: |
| 1186 | // module_type { |
| 1187 | // name: "...", |
| 1188 | // ... |
| 1189 | // <common properties> |
| 1190 | // ... |
| 1191 | // <single os type specific properties> |
| 1192 | // |
| 1193 | // arch: { |
| 1194 | // <arch specific sections> |
| 1195 | // } |
| 1196 | // |
| 1197 | osPropertySet = bpModule |
| 1198 | archPropertySet = osPropertySet.AddPropertySet("arch") |
| 1199 | |
| 1200 | // Arch specific properties need to be added to an arch specific section |
| 1201 | // within arch. |
| 1202 | archOsPrefix = "" |
| 1203 | } else { |
| 1204 | // Create a structure that looks like: |
| 1205 | // module_type { |
| 1206 | // name: "...", |
| 1207 | // ... |
| 1208 | // <common properties> |
| 1209 | // ... |
| 1210 | // target: { |
| 1211 | // <arch independent os specific sections, e.g. android> |
| 1212 | // ... |
| 1213 | // <arch and os specific sections, e.g. android_x86> |
| 1214 | // } |
| 1215 | // |
| 1216 | osType := osInfo.osType |
| 1217 | osPropertySet = targetPropertySet.AddPropertySet(osType.Name) |
| 1218 | archPropertySet = targetPropertySet |
| 1219 | |
| 1220 | // Arch specific properties need to be added to an os and arch specific |
| 1221 | // section prefixed with <os>_. |
| 1222 | archOsPrefix = osType.Name + "_" |
| 1223 | } |
| 1224 | |
| 1225 | // Add the os specific but arch independent properties to the module. |
Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 1226 | addSdkMemberPropertiesToSet(ctx, osInfo.Properties, osPropertySet) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1227 | |
| 1228 | // Add arch (and possibly os) specific sections for each set of arch (and possibly |
| 1229 | // os) specific properties. |
| 1230 | // |
| 1231 | // The archInfos list will be empty if the os contains variants for the common |
| 1232 | // architecture. |
| 1233 | for _, archInfo := range osInfo.archInfos { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1234 | archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Paul Duffin | 7a1f7f3 | 2020-05-04 15:32:08 +0100 | [diff] [blame] | 1238 | func (osInfo *osTypeSpecificInfo) isHostVariant() bool { |
| 1239 | osClass := osInfo.osType.Class |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1240 | return osClass == android.Host |
Paul Duffin | 7a1f7f3 | 2020-05-04 15:32:08 +0100 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | var _ isHostVariant = (*osTypeSpecificInfo)(nil) |
| 1244 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1245 | func (osInfo *osTypeSpecificInfo) String() string { |
| 1246 | return fmt.Sprintf("OsType{%s}", osInfo.osType) |
| 1247 | } |
| 1248 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1249 | type archTypeSpecificInfo struct { |
| 1250 | baseInfo |
| 1251 | |
| 1252 | archType android.ArchType |
Jiyong Park | 8fe14e6 | 2020-10-19 22:47:34 +0900 | [diff] [blame] | 1253 | osType android.OsType |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1254 | |
| 1255 | linkInfos []*linkTypeSpecificInfo |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1258 | var _ propertiesContainer = (*archTypeSpecificInfo)(nil) |
| 1259 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1260 | // Create a new archTypeSpecificInfo for the specified arch type and its properties |
| 1261 | // structures populated with information from the variants. |
Jiyong Park | 8fe14e6 | 2020-10-19 22:47:34 +0900 | [diff] [blame] | 1262 | func newArchSpecificInfo(ctx android.SdkMemberContext, archType android.ArchType, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo { |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1263 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1264 | // Create an arch specific info into which the variant properties can be copied. |
Jiyong Park | 8fe14e6 | 2020-10-19 22:47:34 +0900 | [diff] [blame] | 1265 | archInfo := &archTypeSpecificInfo{archType: archType, osType: osType} |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1266 | |
| 1267 | // Create the properties into which the arch type specific properties will be |
| 1268 | // added. |
| 1269 | archInfo.Properties = variantPropertiesFactory() |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1270 | |
| 1271 | if len(archVariants) == 1 { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1272 | archInfo.Properties.PopulateFromVariant(ctx, archVariants[0]) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1273 | } else { |
| 1274 | // There is more than one variant for this arch type which must be differentiated |
| 1275 | // by link type. |
| 1276 | for _, linkVariant := range archVariants { |
| 1277 | linkType := getLinkType(linkVariant) |
| 1278 | if linkType == "" { |
| 1279 | panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(archVariants))) |
| 1280 | } else { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1281 | linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1282 | |
| 1283 | archInfo.linkInfos = append(archInfo.linkInfos, linkInfo) |
| 1284 | } |
| 1285 | } |
| 1286 | } |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1287 | |
| 1288 | return archInfo |
| 1289 | } |
| 1290 | |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1291 | func (archInfo *archTypeSpecificInfo) optimizableProperties() interface{} { |
| 1292 | return archInfo.Properties |
| 1293 | } |
| 1294 | |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1295 | // Get the link type of the variant |
| 1296 | // |
| 1297 | // If the variant is not differentiated by link type then it returns "", |
| 1298 | // otherwise it returns one of "static" or "shared". |
| 1299 | func getLinkType(variant android.Module) string { |
| 1300 | linkType := "" |
| 1301 | if linkable, ok := variant.(cc.LinkableInterface); ok { |
| 1302 | if linkable.Shared() && linkable.Static() { |
| 1303 | panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String())) |
| 1304 | } else if linkable.Shared() { |
| 1305 | linkType = "shared" |
| 1306 | } else if linkable.Static() { |
| 1307 | linkType = "static" |
| 1308 | } else { |
| 1309 | panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String())) |
| 1310 | } |
| 1311 | } |
| 1312 | return linkType |
| 1313 | } |
| 1314 | |
| 1315 | // Optimize the properties by extracting common properties from link type specific |
| 1316 | // properties into arch type specific properties. |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1317 | func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) { |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1318 | if len(archInfo.linkInfos) == 0 { |
| 1319 | return |
| 1320 | } |
| 1321 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1322 | extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.linkInfos) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1325 | // Add the properties for an arch type to a property set. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1326 | func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) { |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1327 | archTypeName := archInfo.archType.Name |
| 1328 | archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName) |
Jiyong Park | 8fe14e6 | 2020-10-19 22:47:34 +0900 | [diff] [blame] | 1329 | // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host. |
| 1330 | if ctx.memberType.IsHostOsDependent() && archInfo.osType.Class == android.Host { |
| 1331 | archTypePropertySet.AddProperty("enabled", true) |
| 1332 | } |
Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 1333 | addSdkMemberPropertiesToSet(ctx, archInfo.Properties, archTypePropertySet) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1334 | |
| 1335 | for _, linkInfo := range archInfo.linkInfos { |
| 1336 | linkPropertySet := archTypePropertySet.AddPropertySet(linkInfo.linkType) |
Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 1337 | addSdkMemberPropertiesToSet(ctx, linkInfo.Properties, linkPropertySet) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1341 | func (archInfo *archTypeSpecificInfo) String() string { |
| 1342 | return fmt.Sprintf("ArchType{%s}", archInfo.archType) |
| 1343 | } |
| 1344 | |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1345 | type linkTypeSpecificInfo struct { |
| 1346 | baseInfo |
| 1347 | |
| 1348 | linkType string |
| 1349 | } |
| 1350 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1351 | var _ propertiesContainer = (*linkTypeSpecificInfo)(nil) |
| 1352 | |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1353 | // Create a new linkTypeSpecificInfo for the specified link type and its properties |
| 1354 | // structures populated with information from the variant. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1355 | 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] | 1356 | linkInfo := &linkTypeSpecificInfo{ |
| 1357 | baseInfo: baseInfo{ |
| 1358 | // Create the properties into which the link type specific properties will be |
| 1359 | // added. |
| 1360 | Properties: variantPropertiesFactory(), |
| 1361 | }, |
| 1362 | linkType: linkType, |
| 1363 | } |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1364 | linkInfo.Properties.PopulateFromVariant(ctx, linkVariant) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1365 | return linkInfo |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1368 | func (l *linkTypeSpecificInfo) String() string { |
| 1369 | return fmt.Sprintf("LinkType{%s}", l.linkType) |
| 1370 | } |
| 1371 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1372 | type memberContext struct { |
| 1373 | sdkMemberContext android.ModuleContext |
| 1374 | builder *snapshotBuilder |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1375 | memberType android.SdkMemberType |
| 1376 | name string |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | func (m *memberContext) SdkModuleContext() android.ModuleContext { |
| 1380 | return m.sdkMemberContext |
| 1381 | } |
| 1382 | |
| 1383 | func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder { |
| 1384 | return m.builder |
| 1385 | } |
| 1386 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1387 | func (m *memberContext) MemberType() android.SdkMemberType { |
| 1388 | return m.memberType |
| 1389 | } |
| 1390 | |
| 1391 | func (m *memberContext) Name() string { |
| 1392 | return m.name |
| 1393 | } |
| 1394 | |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 1395 | func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1396 | |
| 1397 | memberType := member.memberType |
| 1398 | |
Paul Duffin | 0d4ed0a | 2021-05-10 23:58:40 +0100 | [diff] [blame] | 1399 | // Do not add the prefer property if the member snapshot module is a source module type. |
| 1400 | if !memberType.UsesSourceModuleTypeInSnapshot() { |
| 1401 | // Set the prefer based on the environment variable. This is a temporary work around to allow a |
| 1402 | // snapshot to be created that sets prefer: true. |
| 1403 | // TODO(b/174997203): Remove once the ability to select the modules to prefer can be done |
| 1404 | // dynamically at build time not at snapshot generation time. |
| 1405 | prefer := ctx.sdkMemberContext.Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_PREFER") |
Paul Duffin | 83ad956 | 2021-05-10 23:49:04 +0100 | [diff] [blame] | 1406 | |
Paul Duffin | 0d4ed0a | 2021-05-10 23:58:40 +0100 | [diff] [blame] | 1407 | // Set prefer. Setting this to false is not strictly required as that is the default but it does |
| 1408 | // provide a convenient hook to post-process the generated Android.bp file, e.g. in tests to |
| 1409 | // check the behavior when a prebuilt is preferred. It also makes it explicit what the default |
| 1410 | // behavior is for the module. |
| 1411 | bpModule.insertAfter("name", "prefer", prefer) |
| 1412 | } |
Paul Duffin | 83ad956 | 2021-05-10 23:49:04 +0100 | [diff] [blame] | 1413 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1414 | // Group the variants by os type. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1415 | variantsByOsType := make(map[android.OsType][]android.Module) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1416 | variants := member.Variants() |
| 1417 | for _, variant := range variants { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1418 | osType := variant.Target().Os |
| 1419 | variantsByOsType[osType] = append(variantsByOsType[osType], variant) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1422 | osCount := len(variantsByOsType) |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 1423 | variantPropertiesFactory := func() android.SdkMemberProperties { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1424 | properties := memberType.CreateVariantPropertiesStruct() |
| 1425 | base := properties.Base() |
| 1426 | base.Os_count = osCount |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1427 | return properties |
| 1428 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1429 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1430 | osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1431 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1432 | // 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] | 1433 | commonProperties := variantPropertiesFactory() |
| 1434 | commonProperties.Base().Os = android.CommonOS |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1435 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1436 | // Create common value extractor that can be used to optimize the properties. |
| 1437 | commonValueExtractor := newCommonValueExtractor(commonProperties) |
| 1438 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1439 | // The list of property structures which are os type specific but common across |
| 1440 | // architectures within that os type. |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1441 | var osSpecificPropertiesContainers []*osTypeSpecificInfo |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1442 | |
| 1443 | for osType, osTypeVariants := range variantsByOsType { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1444 | osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants) |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1445 | osTypeToInfo[osType] = osInfo |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 1446 | // Add the os specific properties to a list of os type specific yet architecture |
| 1447 | // independent properties structs. |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1448 | osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo) |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1449 | |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1450 | // Optimize the properties across all the variants for a specific os type. |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1451 | osInfo.optimizeProperties(ctx, commonValueExtractor) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1452 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1453 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1454 | // Extract properties which are common across all architectures and os types. |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1455 | extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, commonProperties, osSpecificPropertiesContainers) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1456 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1457 | // Add the common properties to the module. |
Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 1458 | addSdkMemberPropertiesToSet(ctx, commonProperties, bpModule) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1459 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1460 | // Create a target property set into which target specific properties can be |
| 1461 | // added. |
| 1462 | targetPropertySet := bpModule.AddPropertySet("target") |
| 1463 | |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 1464 | // If the member is host OS dependent and has host_supported then disable by |
| 1465 | // default and enable each host OS variant explicitly. This avoids problems |
| 1466 | // with implicitly enabled OS variants when the snapshot is used, which might |
| 1467 | // be different from this run (e.g. different build OS). |
| 1468 | if ctx.memberType.IsHostOsDependent() { |
| 1469 | hostSupported := bpModule.getValue("host_supported") == true // Missing means false. |
| 1470 | if hostSupported { |
| 1471 | hostPropertySet := targetPropertySet.AddPropertySet("host") |
| 1472 | hostPropertySet.AddProperty("enabled", false) |
| 1473 | } |
| 1474 | } |
| 1475 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1476 | // Iterate over the os types in a fixed order. |
| 1477 | for _, osType := range s.getPossibleOsTypes() { |
| 1478 | osInfo := osTypeToInfo[osType] |
| 1479 | if osInfo == nil { |
| 1480 | continue |
| 1481 | } |
| 1482 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1483 | osInfo.addToPropertySet(ctx, bpModule, targetPropertySet) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1484 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1487 | // Compute the list of possible os types that this sdk could support. |
| 1488 | func (s *sdk) getPossibleOsTypes() []android.OsType { |
| 1489 | var osTypes []android.OsType |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 1490 | for _, osType := range android.OsTypeList() { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1491 | if s.DeviceSupported() { |
| 1492 | if osType.Class == android.Device && osType != android.Fuchsia { |
| 1493 | osTypes = append(osTypes, osType) |
| 1494 | } |
| 1495 | } |
| 1496 | if s.HostSupported() { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1497 | if osType.Class == android.Host { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1498 | osTypes = append(osTypes, osType) |
| 1499 | } |
| 1500 | } |
| 1501 | } |
| 1502 | sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name }) |
| 1503 | return osTypes |
| 1504 | } |
| 1505 | |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1506 | // Given a set of properties (struct value), return the value of the field within that |
| 1507 | // struct (or one of its embedded structs). |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1508 | type fieldAccessorFunc func(structValue reflect.Value) reflect.Value |
| 1509 | |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 1510 | // Checks the metadata to determine whether the property should be ignored for the |
| 1511 | // purposes of common value extraction or not. |
| 1512 | type extractorMetadataPredicate func(metadata propertiesContainer) bool |
| 1513 | |
| 1514 | // Indicates whether optimizable properties are provided by a host variant or |
| 1515 | // not. |
| 1516 | type isHostVariant interface { |
| 1517 | isHostVariant() bool |
| 1518 | } |
| 1519 | |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1520 | // A property that can be optimized by the commonValueExtractor. |
| 1521 | type extractorProperty struct { |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 1522 | // The name of the field for this property. It is a "."-separated path for |
| 1523 | // fields in non-anonymous substructs. |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1524 | name string |
| 1525 | |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 1526 | // Filter that can use metadata associated with the properties being optimized |
| 1527 | // to determine whether the field should be ignored during common value |
| 1528 | // optimization. |
| 1529 | filter extractorMetadataPredicate |
| 1530 | |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1531 | // Retrieves the value on which common value optimization will be performed. |
| 1532 | getter fieldAccessorFunc |
| 1533 | |
| 1534 | // The empty value for the field. |
| 1535 | emptyValue reflect.Value |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 1536 | |
| 1537 | // True if the property can support arch variants false otherwise. |
| 1538 | archVariant bool |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1541 | func (p extractorProperty) String() string { |
| 1542 | return p.name |
| 1543 | } |
| 1544 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1545 | // Supports extracting common values from a number of instances of a properties |
| 1546 | // structure into a separate common set of properties. |
| 1547 | type commonValueExtractor struct { |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1548 | // The properties that the extractor can optimize. |
| 1549 | properties []extractorProperty |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | // Create a new common value extractor for the structure type for the supplied |
| 1553 | // properties struct. |
| 1554 | // |
| 1555 | // The returned extractor can be used on any properties structure of the same type |
| 1556 | // as the supplied set of properties. |
| 1557 | func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor { |
| 1558 | structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type() |
| 1559 | extractor := &commonValueExtractor{} |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 1560 | extractor.gatherFields(structType, nil, "") |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1561 | return extractor |
| 1562 | } |
| 1563 | |
| 1564 | // Gather the fields from the supplied structure type from which common values will |
| 1565 | // be extracted. |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1566 | // |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 1567 | // This is recursive function. If it encounters a struct then it will recurse |
| 1568 | // into it, passing in the accessor for the field and the struct name as prefix |
| 1569 | // for the nested fields. That will then be used in the accessors for the fields |
| 1570 | // in the embedded struct. |
| 1571 | func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string) { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1572 | for f := 0; f < structType.NumField(); f++ { |
| 1573 | field := structType.Field(f) |
| 1574 | if field.PkgPath != "" { |
| 1575 | // Ignore unexported fields. |
| 1576 | continue |
| 1577 | } |
| 1578 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1579 | // Ignore fields whose value should be kept. |
| 1580 | if proptools.HasTag(field, "sdk", "keep") { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1581 | continue |
| 1582 | } |
| 1583 | |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 1584 | var filter extractorMetadataPredicate |
| 1585 | |
| 1586 | // Add a filter |
| 1587 | if proptools.HasTag(field, "sdk", "ignored-on-host") { |
| 1588 | filter = func(metadata propertiesContainer) bool { |
| 1589 | if m, ok := metadata.(isHostVariant); ok { |
| 1590 | if m.isHostVariant() { |
| 1591 | return false |
| 1592 | } |
| 1593 | } |
| 1594 | return true |
| 1595 | } |
| 1596 | } |
| 1597 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1598 | // Save a copy of the field index for use in the function. |
| 1599 | fieldIndex := f |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1600 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 1601 | name := namePrefix + field.Name |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1602 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1603 | fieldGetter := func(value reflect.Value) reflect.Value { |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1604 | if containingStructAccessor != nil { |
| 1605 | // This is an embedded structure so first access the field for the embedded |
| 1606 | // structure. |
| 1607 | value = containingStructAccessor(value) |
| 1608 | } |
| 1609 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1610 | // Skip through interface and pointer values to find the structure. |
| 1611 | value = getStructValue(value) |
| 1612 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1613 | defer func() { |
| 1614 | if r := recover(); r != nil { |
| 1615 | panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface())) |
| 1616 | } |
| 1617 | }() |
| 1618 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1619 | // Return the field. |
| 1620 | return value.Field(fieldIndex) |
| 1621 | } |
| 1622 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 1623 | if field.Type.Kind() == reflect.Struct { |
| 1624 | // Gather fields from the nested or embedded structure. |
| 1625 | var subNamePrefix string |
| 1626 | if field.Anonymous { |
| 1627 | subNamePrefix = namePrefix |
| 1628 | } else { |
| 1629 | subNamePrefix = name + "." |
| 1630 | } |
| 1631 | e.gatherFields(field.Type, fieldGetter, subNamePrefix) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1632 | } else { |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1633 | property := extractorProperty{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1634 | name, |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 1635 | filter, |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1636 | fieldGetter, |
| 1637 | reflect.Zero(field.Type), |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 1638 | proptools.HasTag(field, "android", "arch_variant"), |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1639 | } |
| 1640 | e.properties = append(e.properties, property) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1641 | } |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | func getStructValue(value reflect.Value) reflect.Value { |
| 1646 | foundStruct: |
| 1647 | for { |
| 1648 | kind := value.Kind() |
| 1649 | switch kind { |
| 1650 | case reflect.Interface, reflect.Ptr: |
| 1651 | value = value.Elem() |
| 1652 | case reflect.Struct: |
| 1653 | break foundStruct |
| 1654 | default: |
| 1655 | panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind)) |
| 1656 | } |
| 1657 | } |
| 1658 | return value |
| 1659 | } |
| 1660 | |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1661 | // A container of properties to be optimized. |
| 1662 | // |
| 1663 | // Allows additional information to be associated with the properties, e.g. for |
| 1664 | // filtering. |
| 1665 | type propertiesContainer interface { |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1666 | fmt.Stringer |
| 1667 | |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1668 | // Get the properties that need optimizing. |
| 1669 | optimizableProperties() interface{} |
| 1670 | } |
| 1671 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 1672 | // A wrapper for sdk variant related properties to allow them to be optimized. |
| 1673 | type sdkVariantPropertiesContainer struct { |
| 1674 | sdkVariant *sdk |
| 1675 | properties interface{} |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1676 | } |
| 1677 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 1678 | func (c sdkVariantPropertiesContainer) optimizableProperties() interface{} { |
| 1679 | return c.properties |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1680 | } |
| 1681 | |
Paul Duffin | 2d1bb89 | 2021-04-24 11:32:59 +0100 | [diff] [blame] | 1682 | func (c sdkVariantPropertiesContainer) String() string { |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1683 | return c.sdkVariant.String() |
| 1684 | } |
| 1685 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1686 | // Extract common properties from a slice of property structures of the same type. |
| 1687 | // |
| 1688 | // All the property structures must be of the same type. |
| 1689 | // commonProperties - must be a pointer to the structure into which common properties will be added. |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1690 | // inputPropertiesSlice - must be a slice of propertiesContainer interfaces. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1691 | // |
| 1692 | // Iterates over each exported field (capitalized name) and checks to see whether they |
| 1693 | // have the same value (using DeepEquals) across all the input properties. If it does not then no |
| 1694 | // change is made. Otherwise, the common value is stored in the field in the commonProperties |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 1695 | // and the field in each of the input properties structure is set to its default value. Nested |
| 1696 | // structs are visited recursively and their non-struct fields are compared. |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1697 | func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1698 | commonPropertiesValue := reflect.ValueOf(commonProperties) |
| 1699 | commonStructValue := commonPropertiesValue.Elem() |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1700 | |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1701 | sliceValue := reflect.ValueOf(inputPropertiesSlice) |
| 1702 | |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1703 | for _, property := range e.properties { |
| 1704 | fieldGetter := property.getter |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 1705 | filter := property.filter |
| 1706 | if filter == nil { |
| 1707 | filter = func(metadata propertiesContainer) bool { |
| 1708 | return true |
| 1709 | } |
| 1710 | } |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1711 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1712 | // Check to see if all the structures have the same value for the field. The commonValue |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 1713 | // is nil on entry to the loop and if it is nil on exit then there is no common value or |
| 1714 | // all the values have been filtered out, otherwise it points to the common value. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1715 | var commonValue *reflect.Value |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1716 | |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 1717 | // Assume that all the values will be the same. |
| 1718 | // |
| 1719 | // While similar to this is not quite the same as commonValue == nil. If all the values |
| 1720 | // have been filtered out then this will be false but commonValue == nil will be true. |
| 1721 | valuesDiffer := false |
| 1722 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1723 | for i := 0; i < sliceValue.Len(); i++ { |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1724 | container := sliceValue.Index(i).Interface().(propertiesContainer) |
| 1725 | itemValue := reflect.ValueOf(container.optimizableProperties()) |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1726 | fieldValue := fieldGetter(itemValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1727 | |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 1728 | if !filter(container) { |
| 1729 | expectedValue := property.emptyValue.Interface() |
| 1730 | actualValue := fieldValue.Interface() |
| 1731 | if !reflect.DeepEqual(expectedValue, actualValue) { |
| 1732 | return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue) |
| 1733 | } |
| 1734 | continue |
| 1735 | } |
| 1736 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1737 | if commonValue == nil { |
| 1738 | // Use the first value as the commonProperties value. |
| 1739 | commonValue = &fieldValue |
| 1740 | } else { |
| 1741 | // If the value does not match the current common value then there is |
| 1742 | // no value in common so break out. |
| 1743 | if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) { |
| 1744 | commonValue = nil |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 1745 | valuesDiffer = true |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1746 | break |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 1751 | // If the fields all have common value then store it in the common struct field |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1752 | // and set the input struct's field to the empty value. |
| 1753 | if commonValue != nil { |
Paul Duffin | b28369a | 2020-05-04 15:39:59 +0100 | [diff] [blame] | 1754 | emptyValue := property.emptyValue |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1755 | fieldGetter(commonStructValue).Set(*commonValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1756 | for i := 0; i < sliceValue.Len(); i++ { |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 1757 | container := sliceValue.Index(i).Interface().(propertiesContainer) |
| 1758 | itemValue := reflect.ValueOf(container.optimizableProperties()) |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1759 | fieldValue := fieldGetter(itemValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1760 | fieldValue.Set(emptyValue) |
| 1761 | } |
| 1762 | } |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 1763 | |
| 1764 | if valuesDiffer && !property.archVariant { |
| 1765 | // The values differ but the property does not support arch variants so it |
| 1766 | // is an error. |
| 1767 | var details strings.Builder |
| 1768 | for i := 0; i < sliceValue.Len(); i++ { |
| 1769 | container := sliceValue.Index(i).Interface().(propertiesContainer) |
| 1770 | itemValue := reflect.ValueOf(container.optimizableProperties()) |
| 1771 | fieldValue := fieldGetter(itemValue) |
| 1772 | |
| 1773 | _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface()) |
| 1774 | } |
| 1775 | |
| 1776 | return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String()) |
| 1777 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1778 | } |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 1779 | |
| 1780 | return nil |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1781 | } |