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