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