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