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