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