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