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