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