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