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