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