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