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