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