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 ( |
| 18 | "fmt" |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 19 | "reflect" |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 20 | "sort" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 21 | "strings" |
| 22 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | |
| 26 | "android/soong/android" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var pctx = android.NewPackageContext("android/soong/sdk") |
| 30 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 31 | var ( |
| 32 | repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip", |
| 33 | blueprint.RuleParams{ |
Paul Duffin | ce482dc | 2019-12-09 19:58:17 +0000 | [diff] [blame] | 34 | Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`, |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 35 | CommandDeps: []string{ |
| 36 | "${config.Zip2ZipCmd}", |
| 37 | }, |
| 38 | }, |
| 39 | "destdir") |
| 40 | |
| 41 | zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles", |
| 42 | blueprint.RuleParams{ |
| 43 | Command: `${config.SoongZipCmd} -C $basedir -l $out.rsp -o $out`, |
| 44 | CommandDeps: []string{ |
| 45 | "${config.SoongZipCmd}", |
| 46 | }, |
| 47 | Rspfile: "$out.rsp", |
| 48 | RspfileContent: "$in", |
| 49 | }, |
| 50 | "basedir") |
| 51 | |
| 52 | mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips", |
| 53 | blueprint.RuleParams{ |
| 54 | Command: `${config.MergeZipsCmd} $out $in`, |
| 55 | CommandDeps: []string{ |
| 56 | "${config.MergeZipsCmd}", |
| 57 | }, |
| 58 | }) |
| 59 | ) |
| 60 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 61 | type generatedContents struct { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 62 | content strings.Builder |
| 63 | indentLevel int |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 64 | } |
| 65 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 66 | // generatedFile abstracts operations for writing contents into a file and emit a build rule |
| 67 | // for the file. |
| 68 | type generatedFile struct { |
| 69 | generatedContents |
| 70 | path android.OutputPath |
| 71 | } |
| 72 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 73 | func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 74 | return &generatedFile{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 75 | path: android.PathForModuleOut(ctx, path...).OutputPath, |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 79 | func (gc *generatedContents) Indent() { |
| 80 | gc.indentLevel++ |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 83 | func (gc *generatedContents) Dedent() { |
| 84 | gc.indentLevel-- |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 87 | func (gc *generatedContents) Printfln(format string, args ...interface{}) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 88 | // ninja consumes newline characters in rspfile_content. Prevent it by |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 89 | // escaping the backslash in the newline character. The extra backslash |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 90 | // is removed when the rspfile is written to the actual script file |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 91 | fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\\n", args...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) { |
| 95 | rb := android.NewRuleBuilder() |
| 96 | // convert \\n to \n |
| 97 | rb.Command(). |
| 98 | Implicits(implicits). |
| 99 | Text("echo").Text(proptools.ShellEscape(gf.content.String())). |
| 100 | Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path) |
| 101 | rb.Command(). |
| 102 | Text("chmod a+x").Output(gf.path) |
| 103 | rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base()) |
| 104 | } |
| 105 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 106 | // Collect all the members. |
| 107 | // |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 108 | // Returns a list containing type (extracted from the dependency tag) and the variant. |
| 109 | func (s *sdk) collectMembers(ctx android.ModuleContext) []sdkMemberRef { |
| 110 | var memberRefs []sdkMemberRef |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 111 | ctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 112 | tag := ctx.OtherModuleDependencyTag(child) |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 113 | if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok { |
| 114 | memberType := memberTag.SdkMemberType() |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 115 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 116 | // 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] | 117 | if !memberType.IsInstance(child) { |
| 118 | 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] | 119 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 120 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 121 | memberRefs = append(memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)}) |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 122 | |
| 123 | // If the member type supports transitive sdk members then recurse down into |
| 124 | // its dependencies, otherwise exit traversal. |
| 125 | return memberType.HasTransitiveSdkMembers() |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 126 | } |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 127 | |
| 128 | return false |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 129 | }) |
| 130 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 131 | return memberRefs |
| 132 | } |
| 133 | |
| 134 | // Organize the members. |
| 135 | // |
| 136 | // The members are first grouped by type and then grouped by name. The order of |
| 137 | // the types is the order they are referenced in android.SdkMemberTypesRegistry. |
| 138 | // The names are in the order in which the dependencies were added. |
| 139 | // |
| 140 | // Returns the members as well as the multilib setting to use. |
| 141 | func (s *sdk) organizeMembers(ctx android.ModuleContext, memberRefs []sdkMemberRef) ([]*sdkMember, string) { |
| 142 | byType := make(map[android.SdkMemberType][]*sdkMember) |
| 143 | byName := make(map[string]*sdkMember) |
| 144 | |
| 145 | lib32 := false // True if any of the members have 32 bit version. |
| 146 | lib64 := false // True if any of the members have 64 bit version. |
| 147 | |
| 148 | for _, memberRef := range memberRefs { |
| 149 | memberType := memberRef.memberType |
| 150 | variant := memberRef.variant |
| 151 | |
| 152 | name := ctx.OtherModuleName(variant) |
| 153 | member := byName[name] |
| 154 | if member == nil { |
| 155 | member = &sdkMember{memberType: memberType, name: name} |
| 156 | byName[name] = member |
| 157 | byType[memberType] = append(byType[memberType], member) |
| 158 | } |
| 159 | |
| 160 | multilib := variant.Target().Arch.ArchType.Multilib |
| 161 | if multilib == "lib32" { |
| 162 | lib32 = true |
| 163 | } else if multilib == "lib64" { |
| 164 | lib64 = true |
| 165 | } |
| 166 | |
| 167 | // Only append new variants to the list. This is needed because a member can be both |
| 168 | // exported by the sdk and also be a transitive sdk member. |
| 169 | member.variants = appendUniqueVariants(member.variants, variant) |
| 170 | } |
| 171 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 172 | var members []*sdkMember |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 173 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 174 | membersOfType := byType[memberListProperty.memberType] |
| 175 | members = append(members, membersOfType...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 176 | } |
| 177 | |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 178 | // Compute the setting of multilib. |
| 179 | var multilib string |
| 180 | if lib32 && lib64 { |
| 181 | multilib = "both" |
| 182 | } else if lib32 { |
| 183 | multilib = "32" |
| 184 | } else if lib64 { |
| 185 | multilib = "64" |
| 186 | } |
| 187 | |
| 188 | return members, multilib |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 189 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 190 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 191 | func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware { |
| 192 | for _, v := range variants { |
| 193 | if v == newVariant { |
| 194 | return variants |
| 195 | } |
| 196 | } |
| 197 | return append(variants, newVariant) |
| 198 | } |
| 199 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 200 | // SDK directory structure |
| 201 | // <sdk_root>/ |
| 202 | // Android.bp : definition of a 'sdk' module is here. This is a hand-made one. |
| 203 | // <api_ver>/ : below this directory are all auto-generated |
| 204 | // Android.bp : definition of 'sdk_snapshot' module is here |
| 205 | // aidl/ |
| 206 | // frameworks/base/core/..../IFoo.aidl : an exported AIDL file |
| 207 | // java/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 208 | // <module_name>.jar : the stub jar for a java library 'module_name' |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 209 | // include/ |
| 210 | // bionic/libc/include/stdlib.h : an exported header file |
| 211 | // include_gen/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 212 | // <module_name>/com/android/.../IFoo.h : a generated header file |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 213 | // <arch>/include/ : arch-specific exported headers |
| 214 | // <arch>/include_gen/ : arch-specific generated headers |
| 215 | // <arch>/lib/ |
| 216 | // libFoo.so : a stub library |
| 217 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 218 | // 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] | 219 | // This isn't visible to users, so could be changed in future. |
| 220 | func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string { |
| 221 | return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version |
| 222 | } |
| 223 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 224 | // buildSnapshot is the main function in this source file. It creates rules to copy |
| 225 | // the contents (header files, stub libraries, etc) into the zip file. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 226 | func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath { |
| 227 | |
| 228 | var memberRefs []sdkMemberRef |
| 229 | for _, sdkVariant := range sdkVariants { |
| 230 | memberRefs = append(memberRefs, sdkVariant.memberRefs...) |
| 231 | } |
| 232 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 233 | snapshotDir := android.PathForModuleOut(ctx, "snapshot") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 234 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 235 | bp := newGeneratedFile(ctx, "snapshot", "Android.bp") |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 236 | |
| 237 | bpFile := &bpFile{ |
| 238 | modules: make(map[string]*bpModule), |
| 239 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 240 | |
| 241 | builder := &snapshotBuilder{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 242 | ctx: ctx, |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 243 | sdk: s, |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 244 | version: "current", |
| 245 | snapshotDir: snapshotDir.OutputPath, |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 246 | copies: make(map[string]string), |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 247 | filesToZip: []android.Path{bp.path}, |
| 248 | bpFile: bpFile, |
| 249 | prebuiltModules: make(map[string]*bpModule), |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 250 | } |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 251 | s.builderForTests = builder |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 252 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 253 | members, multilib := s.organizeMembers(ctx, memberRefs) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 254 | for _, member := range members { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 255 | memberType := member.memberType |
| 256 | prebuiltModule := memberType.AddPrebuiltModule(ctx, builder, member) |
| 257 | if prebuiltModule == nil { |
| 258 | // Fall back to legacy method of building a snapshot |
| 259 | memberType.BuildSnapshot(ctx, builder, member) |
| 260 | } else { |
| 261 | s.createMemberSnapshot(ctx, builder, member, prebuiltModule) |
| 262 | } |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 263 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 264 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 265 | // Create a transformer that will transform an unversioned module into a versioned module. |
| 266 | unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder} |
| 267 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 268 | // Create a transformer that will transform an unversioned module by replacing any references |
| 269 | // to internal members with a unique module name and setting prefer: false. |
| 270 | unversionedTransformer := unversionedTransformation{builder: builder} |
| 271 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 272 | for _, unversioned := range builder.prebuiltOrder { |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 273 | // Prune any empty property sets. |
| 274 | unversioned = unversioned.transform(pruneEmptySetTransformer{}) |
| 275 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 276 | // Copy the unversioned module so it can be modified to make it versioned. |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 277 | versioned := unversioned.deepCopy() |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 278 | |
| 279 | // Transform the unversioned module into a versioned one. |
| 280 | versioned.transform(unversionedToVersionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 281 | bpFile.AddModule(versioned) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 282 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 283 | // Transform the unversioned module to make it suitable for use in the snapshot. |
| 284 | unversioned.transform(unversionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 285 | bpFile.AddModule(unversioned) |
| 286 | } |
| 287 | |
| 288 | // Create the snapshot module. |
| 289 | snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 290 | var snapshotModuleType string |
| 291 | if s.properties.Module_exports { |
| 292 | snapshotModuleType = "module_exports_snapshot" |
| 293 | } else { |
| 294 | snapshotModuleType = "sdk_snapshot" |
| 295 | } |
| 296 | snapshotModule := bpFile.newModule(snapshotModuleType) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 297 | snapshotModule.AddProperty("name", snapshotName) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 298 | |
| 299 | // Make sure that the snapshot has the same visibility as the sdk. |
| 300 | visibility := android.EffectiveVisibilityRules(ctx, s) |
| 301 | if len(visibility) != 0 { |
| 302 | snapshotModule.AddProperty("visibility", visibility) |
| 303 | } |
| 304 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 305 | addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 306 | |
| 307 | // Compile_multilib defaults to both and must always be set to both on the |
| 308 | // device and so only needs to be set when targeted at the host and is neither |
| 309 | // unspecified or both. |
| 310 | if s.HostSupported() && multilib != "" && multilib != "both" { |
| 311 | targetSet := snapshotModule.AddPropertySet("target") |
| 312 | hostSet := targetSet.AddPropertySet("host") |
| 313 | hostSet.AddProperty("compile_multilib", multilib) |
| 314 | } |
| 315 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 316 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 317 | names := memberListProperty.getter(s.dynamicMemberTypeListProperties) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 318 | if len(names) > 0 { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 319 | snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names)) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 320 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 321 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 322 | bpFile.AddModule(snapshotModule) |
| 323 | |
| 324 | // generate Android.bp |
| 325 | bp = newGeneratedFile(ctx, "snapshot", "Android.bp") |
| 326 | generateBpContents(&bp.generatedContents, bpFile) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 327 | |
| 328 | bp.build(pctx, ctx, nil) |
| 329 | |
| 330 | filesToZip := builder.filesToZip |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 331 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 332 | // zip them all |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 333 | outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 334 | outputDesc := "Building snapshot for " + ctx.ModuleName() |
| 335 | |
| 336 | // If there are no zips to merge then generate the output zip directly. |
| 337 | // Otherwise, generate an intermediate zip file into which other zips can be |
| 338 | // merged. |
| 339 | var zipFile android.OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 340 | var desc string |
| 341 | if len(builder.zipsToMerge) == 0 { |
| 342 | zipFile = outputZipFile |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 343 | desc = outputDesc |
| 344 | } else { |
| 345 | zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 346 | desc = "Building intermediate snapshot for " + ctx.ModuleName() |
| 347 | } |
| 348 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 349 | ctx.Build(pctx, android.BuildParams{ |
| 350 | Description: desc, |
| 351 | Rule: zipFiles, |
| 352 | Inputs: filesToZip, |
| 353 | Output: zipFile, |
| 354 | Args: map[string]string{ |
| 355 | "basedir": builder.snapshotDir.String(), |
| 356 | }, |
| 357 | }) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 358 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 359 | if len(builder.zipsToMerge) != 0 { |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 360 | ctx.Build(pctx, android.BuildParams{ |
| 361 | Description: outputDesc, |
| 362 | Rule: mergeZips, |
| 363 | Input: zipFile, |
| 364 | Inputs: builder.zipsToMerge, |
| 365 | Output: outputZipFile, |
| 366 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | return outputZipFile |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 370 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 371 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 372 | type propertyTag struct { |
| 373 | name string |
| 374 | } |
| 375 | |
| 376 | var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"} |
| 377 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 378 | type unversionedToVersionedTransformation struct { |
| 379 | identityTransformation |
| 380 | builder *snapshotBuilder |
| 381 | } |
| 382 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 383 | func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule { |
| 384 | // Use a versioned name for the module but remember the original name for the |
| 385 | // snapshot. |
| 386 | name := module.getValue("name").(string) |
| 387 | module.setProperty("name", t.builder.versionedSdkMemberName(name)) |
| 388 | module.insertAfter("name", "sdk_member_name", name) |
| 389 | return module |
| 390 | } |
| 391 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 392 | func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
| 393 | if tag == sdkMemberReferencePropertyTag { |
| 394 | return t.builder.versionedSdkMemberNames(value.([]string)), tag |
| 395 | } else { |
| 396 | return value, tag |
| 397 | } |
| 398 | } |
| 399 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 400 | type unversionedTransformation struct { |
| 401 | identityTransformation |
| 402 | builder *snapshotBuilder |
| 403 | } |
| 404 | |
| 405 | func (t unversionedTransformation) transformModule(module *bpModule) *bpModule { |
| 406 | // If the module is an internal member then use a unique name for it. |
| 407 | name := module.getValue("name").(string) |
| 408 | module.setProperty("name", t.builder.unversionedSdkMemberName(name)) |
| 409 | |
| 410 | // Set prefer: false - this is not strictly required as that is the default. |
| 411 | module.insertAfter("name", "prefer", false) |
| 412 | |
| 413 | return module |
| 414 | } |
| 415 | |
| 416 | func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
| 417 | if tag == sdkMemberReferencePropertyTag { |
| 418 | return t.builder.unversionedSdkMemberNames(value.([]string)), tag |
| 419 | } else { |
| 420 | return value, tag |
| 421 | } |
| 422 | } |
| 423 | |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 424 | type pruneEmptySetTransformer struct { |
| 425 | identityTransformation |
| 426 | } |
| 427 | |
| 428 | var _ bpTransformer = (*pruneEmptySetTransformer)(nil) |
| 429 | |
| 430 | func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { |
| 431 | if len(propertySet.properties) == 0 { |
| 432 | return nil, nil |
| 433 | } else { |
| 434 | return propertySet, tag |
| 435 | } |
| 436 | } |
| 437 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 438 | func generateBpContents(contents *generatedContents, bpFile *bpFile) { |
| 439 | contents.Printfln("// This is auto-generated. DO NOT EDIT.") |
| 440 | for _, bpModule := range bpFile.order { |
| 441 | contents.Printfln("") |
| 442 | contents.Printfln("%s {", bpModule.moduleType) |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 443 | outputPropertySet(contents, bpModule.bpPropertySet) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 444 | contents.Printfln("}") |
| 445 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | func outputPropertySet(contents *generatedContents, set *bpPropertySet) { |
| 449 | contents.Indent() |
| 450 | for _, name := range set.order { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 451 | value := set.getValue(name) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 452 | |
| 453 | reflectedValue := reflect.ValueOf(value) |
| 454 | t := reflectedValue.Type() |
| 455 | |
| 456 | kind := t.Kind() |
| 457 | switch kind { |
| 458 | case reflect.Slice: |
| 459 | length := reflectedValue.Len() |
| 460 | if length > 1 { |
| 461 | contents.Printfln("%s: [", name) |
| 462 | contents.Indent() |
| 463 | for i := 0; i < length; i = i + 1 { |
| 464 | contents.Printfln("%q,", reflectedValue.Index(i).Interface()) |
| 465 | } |
| 466 | contents.Dedent() |
| 467 | contents.Printfln("],") |
| 468 | } else if length == 0 { |
| 469 | contents.Printfln("%s: [],", name) |
| 470 | } else { |
| 471 | contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface()) |
| 472 | } |
| 473 | case reflect.Bool: |
| 474 | contents.Printfln("%s: %t,", name, reflectedValue.Bool()) |
| 475 | |
| 476 | case reflect.Ptr: |
| 477 | contents.Printfln("%s: {", name) |
| 478 | outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet)) |
| 479 | contents.Printfln("},") |
| 480 | |
| 481 | default: |
| 482 | contents.Printfln("%s: %q,", name, value) |
| 483 | } |
| 484 | } |
| 485 | contents.Dedent() |
| 486 | } |
| 487 | |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 488 | func (s *sdk) GetAndroidBpContentsForTests() string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 489 | contents := &generatedContents{} |
| 490 | generateBpContents(contents, s.builderForTests.bpFile) |
| 491 | return contents.content.String() |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 494 | type snapshotBuilder struct { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 495 | ctx android.ModuleContext |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 496 | sdk *sdk |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 497 | version string |
| 498 | snapshotDir android.OutputPath |
| 499 | bpFile *bpFile |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 500 | |
| 501 | // Map from destination to source of each copy - used to eliminate duplicates and |
| 502 | // detect conflicts. |
| 503 | copies map[string]string |
| 504 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 505 | filesToZip android.Paths |
| 506 | zipsToMerge android.Paths |
| 507 | |
| 508 | prebuiltModules map[string]*bpModule |
| 509 | prebuiltOrder []*bpModule |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) { |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 513 | if existing, ok := s.copies[dest]; ok { |
| 514 | if existing != src.String() { |
| 515 | s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src) |
| 516 | return |
| 517 | } |
| 518 | } else { |
| 519 | path := s.snapshotDir.Join(s.ctx, dest) |
| 520 | s.ctx.Build(pctx, android.BuildParams{ |
| 521 | Rule: android.Cp, |
| 522 | Input: src, |
| 523 | Output: path, |
| 524 | }) |
| 525 | s.filesToZip = append(s.filesToZip, path) |
| 526 | |
| 527 | s.copies[dest] = src.String() |
| 528 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 531 | func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) { |
| 532 | ctx := s.ctx |
| 533 | |
| 534 | // Repackage the zip file so that the entries are in the destDir directory. |
| 535 | // This will allow the zip file to be merged into the snapshot. |
| 536 | tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 537 | |
| 538 | ctx.Build(pctx, android.BuildParams{ |
| 539 | Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(), |
| 540 | Rule: repackageZip, |
| 541 | Input: zipPath, |
| 542 | Output: tmpZipPath, |
| 543 | Args: map[string]string{ |
| 544 | "destdir": destDir, |
| 545 | }, |
| 546 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 547 | |
| 548 | // Add the repackaged zip file to the files to merge. |
| 549 | s.zipsToMerge = append(s.zipsToMerge, tmpZipPath) |
| 550 | } |
| 551 | |
Paul Duffin | 9d8d609 | 2019-12-05 18:19:29 +0000 | [diff] [blame] | 552 | func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule { |
| 553 | name := member.Name() |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 554 | if s.prebuiltModules[name] != nil { |
| 555 | panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name)) |
| 556 | } |
| 557 | |
| 558 | m := s.bpFile.newModule(moduleType) |
| 559 | m.AddProperty("name", name) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 560 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 561 | variant := member.Variants()[0] |
| 562 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 563 | if s.sdk.isInternalMember(name) { |
| 564 | // An internal member is only referenced from the sdk snapshot which is in the |
| 565 | // same package so can be marked as private. |
| 566 | m.AddProperty("visibility", []string{"//visibility:private"}) |
| 567 | } else { |
| 568 | // Extract visibility information from a member variant. All variants have the same |
| 569 | // visibility so it doesn't matter which one is used. |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 570 | visibility := android.EffectiveVisibilityRules(s.ctx, variant) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 571 | if len(visibility) != 0 { |
| 572 | m.AddProperty("visibility", visibility) |
| 573 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 576 | addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 577 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 578 | // Where available copy apex_available properties from the member. |
| 579 | if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok { |
| 580 | apexAvailable := apexAware.ApexAvailable() |
| 581 | if len(apexAvailable) > 0 { |
| 582 | m.AddProperty("apex_available", apexAvailable) |
| 583 | } |
| 584 | } |
| 585 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 586 | s.prebuiltModules[name] = m |
| 587 | s.prebuiltOrder = append(s.prebuiltOrder, m) |
| 588 | return m |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 591 | func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) { |
| 592 | if !module.DeviceSupported() { |
| 593 | bpModule.AddProperty("device_supported", false) |
| 594 | } |
| 595 | if module.HostSupported() { |
| 596 | bpModule.AddProperty("host_supported", true) |
| 597 | } |
| 598 | } |
| 599 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 600 | func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag { |
| 601 | return sdkMemberReferencePropertyTag |
| 602 | } |
| 603 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 604 | // Get a versioned name appropriate for the SDK snapshot version being taken. |
| 605 | func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 606 | return versionedSdkMemberName(s.ctx, unversionedName, s.version) |
| 607 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 608 | |
| 609 | func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string { |
| 610 | var references []string = nil |
| 611 | for _, m := range members { |
| 612 | references = append(references, s.versionedSdkMemberName(m)) |
| 613 | } |
| 614 | return references |
| 615 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 616 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 617 | // Get an internal name unique to the sdk. |
| 618 | func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string { |
| 619 | if s.sdk.isInternalMember(unversionedName) { |
| 620 | return s.ctx.ModuleName() + "_" + unversionedName |
| 621 | } else { |
| 622 | return unversionedName |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string { |
| 627 | var references []string = nil |
| 628 | for _, m := range members { |
| 629 | references = append(references, s.unversionedSdkMemberName(m)) |
| 630 | } |
| 631 | return references |
| 632 | } |
| 633 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 634 | type sdkMemberRef struct { |
| 635 | memberType android.SdkMemberType |
| 636 | variant android.SdkAware |
| 637 | } |
| 638 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 639 | var _ android.SdkMember = (*sdkMember)(nil) |
| 640 | |
| 641 | type sdkMember struct { |
| 642 | memberType android.SdkMemberType |
| 643 | name string |
| 644 | variants []android.SdkAware |
| 645 | } |
| 646 | |
| 647 | func (m *sdkMember) Name() string { |
| 648 | return m.name |
| 649 | } |
| 650 | |
| 651 | func (m *sdkMember) Variants() []android.SdkAware { |
| 652 | return m.variants |
| 653 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 654 | |
| 655 | type baseInfo struct { |
| 656 | Properties android.SdkMemberProperties |
| 657 | } |
| 658 | |
| 659 | type osTypeSpecificInfo struct { |
| 660 | baseInfo |
| 661 | |
| 662 | // The list of arch type specific info for this os type. |
| 663 | archTypes []*archTypeSpecificInfo |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 664 | |
| 665 | // True if the member has common arch variants for this os type. |
| 666 | commonArch bool |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | type archTypeSpecificInfo struct { |
| 670 | baseInfo |
| 671 | |
| 672 | archType android.ArchType |
| 673 | } |
| 674 | |
| 675 | func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) { |
| 676 | |
| 677 | memberType := member.memberType |
| 678 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 679 | // Group the variants by os type. |
| 680 | variantsByOsType := make(map[android.OsType][]android.SdkAware) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 681 | variants := member.Variants() |
| 682 | for _, variant := range variants { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 683 | osType := variant.Target().Os |
| 684 | variantsByOsType[osType] = append(variantsByOsType[osType], variant) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 687 | osCount := len(variantsByOsType) |
| 688 | createVariantPropertiesStruct := func(os android.OsType) android.SdkMemberProperties { |
| 689 | properties := memberType.CreateVariantPropertiesStruct() |
| 690 | base := properties.Base() |
| 691 | base.Os_count = osCount |
| 692 | base.Os = os |
| 693 | return properties |
| 694 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 695 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 696 | osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 697 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 698 | // The set of properties that are common across all architectures and os types. |
| 699 | commonProperties := createVariantPropertiesStruct(android.CommonOS) |
| 700 | |
| 701 | // The list of property structures which are os type specific but common across |
| 702 | // architectures within that os type. |
| 703 | var osSpecificPropertiesList []android.SdkMemberProperties |
| 704 | |
| 705 | for osType, osTypeVariants := range variantsByOsType { |
| 706 | // Group the properties for each variant by arch type within the os. |
| 707 | osInfo := &osTypeSpecificInfo{} |
| 708 | osTypeToInfo[osType] = osInfo |
| 709 | |
| 710 | // Create a structure into which properties common across the architectures in |
| 711 | // this os type will be stored. Add it to the list of os type specific yet |
| 712 | // architecture independent properties structs. |
| 713 | osInfo.Properties = createVariantPropertiesStruct(osType) |
| 714 | osSpecificPropertiesList = append(osSpecificPropertiesList, osInfo.Properties) |
| 715 | |
| 716 | commonArch := false |
| 717 | for _, variant := range osTypeVariants { |
| 718 | var properties android.SdkMemberProperties |
| 719 | |
| 720 | // Get the info associated with the arch type inside the os info. |
| 721 | archType := variant.Target().Arch.ArchType |
| 722 | |
| 723 | if archType.Name == "common" { |
| 724 | // The arch type is common so populate the common properties directly. |
| 725 | properties = osInfo.Properties |
| 726 | |
| 727 | commonArch = true |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 728 | } else { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 729 | archInfo := &archTypeSpecificInfo{archType: archType} |
| 730 | properties = createVariantPropertiesStruct(osType) |
| 731 | archInfo.Properties = properties |
| 732 | |
| 733 | osInfo.archTypes = append(osInfo.archTypes, archInfo) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 734 | } |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 735 | |
| 736 | properties.PopulateFromVariant(variant) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 739 | if commonArch { |
| 740 | if len(osTypeVariants) != 1 { |
| 741 | panic("Expected to only have 1 variant when arch type is common but found " + string(len(variants))) |
| 742 | } |
| 743 | } else { |
| 744 | var archPropertiesList []android.SdkMemberProperties |
| 745 | for _, archInfo := range osInfo.archTypes { |
| 746 | archPropertiesList = append(archPropertiesList, archInfo.Properties) |
| 747 | } |
| 748 | |
| 749 | extractCommonProperties(osInfo.Properties, archPropertiesList) |
| 750 | |
| 751 | // Choose setting for compile_multilib that is appropriate for the arch variants supplied. |
| 752 | var multilib string |
| 753 | archVariantCount := len(osInfo.archTypes) |
| 754 | if archVariantCount == 2 { |
| 755 | multilib = "both" |
| 756 | } else if archVariantCount == 1 { |
| 757 | if strings.HasSuffix(osInfo.archTypes[0].archType.Name, "64") { |
| 758 | multilib = "64" |
| 759 | } else { |
| 760 | multilib = "32" |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | osInfo.commonArch = commonArch |
| 765 | osInfo.Properties.Base().Compile_multilib = multilib |
| 766 | } |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 767 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 768 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 769 | // Extract properties which are common across all architectures and os types. |
| 770 | extractCommonProperties(commonProperties, osSpecificPropertiesList) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 771 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 772 | // Add the common properties to the module. |
| 773 | commonProperties.AddToPropertySet(sdkModuleContext, builder, bpModule) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 774 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 775 | // Create a target property set into which target specific properties can be |
| 776 | // added. |
| 777 | targetPropertySet := bpModule.AddPropertySet("target") |
| 778 | |
| 779 | // Iterate over the os types in a fixed order. |
| 780 | for _, osType := range s.getPossibleOsTypes() { |
| 781 | osInfo := osTypeToInfo[osType] |
| 782 | if osInfo == nil { |
| 783 | continue |
| 784 | } |
| 785 | |
| 786 | var osPropertySet android.BpPropertySet |
| 787 | var archOsPrefix string |
| 788 | if len(osTypeToInfo) == 1 { |
| 789 | // There is only one os type present in the variants sp don't bother |
| 790 | // with adding target specific properties. |
| 791 | |
| 792 | // Create a structure that looks like: |
| 793 | // module_type { |
| 794 | // name: "...", |
| 795 | // ... |
| 796 | // <common properties> |
| 797 | // ... |
| 798 | // <single os type specific properties> |
| 799 | // |
| 800 | // arch: { |
| 801 | // <arch specific sections> |
| 802 | // } |
| 803 | // |
| 804 | osPropertySet = bpModule |
| 805 | |
| 806 | // Arch specific properties need to be added to an arch specific section |
| 807 | // within arch. |
| 808 | archOsPrefix = "" |
| 809 | } else { |
| 810 | // Create a structure that looks like: |
| 811 | // module_type { |
| 812 | // name: "...", |
| 813 | // ... |
| 814 | // <common properties> |
| 815 | // ... |
| 816 | // target: { |
| 817 | // <arch independent os specific sections, e.g. android> |
| 818 | // ... |
| 819 | // <arch and os specific sections, e.g. android_x86> |
| 820 | // } |
| 821 | // |
| 822 | osPropertySet = targetPropertySet.AddPropertySet(osType.Name) |
| 823 | |
| 824 | // Arch specific properties need to be added to an os and arch specific |
| 825 | // section prefixed with <os>_. |
| 826 | archOsPrefix = osType.Name + "_" |
| 827 | } |
| 828 | |
| 829 | osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, osPropertySet) |
| 830 | if !osInfo.commonArch { |
| 831 | // Either add the arch specific sections into the target or arch sections |
| 832 | // depending on whether they will also be os specific. |
| 833 | var archPropertySet android.BpPropertySet |
| 834 | if archOsPrefix == "" { |
| 835 | archPropertySet = osPropertySet.AddPropertySet("arch") |
| 836 | } else { |
| 837 | archPropertySet = targetPropertySet |
| 838 | } |
| 839 | |
| 840 | // Add arch (and possibly os) specific sections for each set of |
| 841 | // arch (and possibly os) specific properties. |
| 842 | for _, av := range osInfo.archTypes { |
| 843 | archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + av.archType.Name) |
| 844 | |
| 845 | av.Properties.AddToPropertySet(sdkModuleContext, builder, archTypePropertySet) |
| 846 | } |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 847 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | memberType.FinalizeModule(sdkModuleContext, builder, member, bpModule) |
| 851 | } |
| 852 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 853 | // Compute the list of possible os types that this sdk could support. |
| 854 | func (s *sdk) getPossibleOsTypes() []android.OsType { |
| 855 | var osTypes []android.OsType |
| 856 | for _, osType := range android.OsTypeList { |
| 857 | if s.DeviceSupported() { |
| 858 | if osType.Class == android.Device && osType != android.Fuchsia { |
| 859 | osTypes = append(osTypes, osType) |
| 860 | } |
| 861 | } |
| 862 | if s.HostSupported() { |
| 863 | if osType.Class == android.Host || osType.Class == android.HostCross { |
| 864 | osTypes = append(osTypes, osType) |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name }) |
| 869 | return osTypes |
| 870 | } |
| 871 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 872 | // Extract common properties from a slice of property structures of the same type. |
| 873 | // |
| 874 | // All the property structures must be of the same type. |
| 875 | // commonProperties - must be a pointer to the structure into which common properties will be added. |
| 876 | // inputPropertiesSlice - must be a slice of input properties structures. |
| 877 | // |
| 878 | // Iterates over each exported field (capitalized name) and checks to see whether they |
| 879 | // have the same value (using DeepEquals) across all the input properties. If it does not then no |
| 880 | // change is made. Otherwise, the common value is stored in the field in the commonProperties |
| 881 | // and the field in each of the input properties structure is set to its default value. |
| 882 | func extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) { |
| 883 | commonPropertiesValue := reflect.ValueOf(commonProperties) |
| 884 | commonStructValue := commonPropertiesValue.Elem() |
| 885 | propertiesStructType := commonStructValue.Type() |
| 886 | |
| 887 | // Create an empty structure from which default values for the field can be copied. |
| 888 | emptyStructValue := reflect.New(propertiesStructType).Elem() |
| 889 | |
| 890 | for f := 0; f < propertiesStructType.NumField(); f++ { |
| 891 | // Check to see if all the structures have the same value for the field. The commonValue |
| 892 | // is nil on entry to the loop and if it is nil on exit then there is no common value, |
| 893 | // otherwise it points to the common value. |
| 894 | var commonValue *reflect.Value |
| 895 | sliceValue := reflect.ValueOf(inputPropertiesSlice) |
| 896 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 897 | field := propertiesStructType.Field(f) |
| 898 | if field.Name == "SdkMemberPropertiesBase" { |
| 899 | continue |
| 900 | } |
| 901 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 902 | for i := 0; i < sliceValue.Len(); i++ { |
| 903 | structValue := sliceValue.Index(i).Elem().Elem() |
| 904 | fieldValue := structValue.Field(f) |
| 905 | if !fieldValue.CanInterface() { |
| 906 | // The field is not exported so ignore it. |
| 907 | continue |
| 908 | } |
| 909 | |
| 910 | if commonValue == nil { |
| 911 | // Use the first value as the commonProperties value. |
| 912 | commonValue = &fieldValue |
| 913 | } else { |
| 914 | // If the value does not match the current common value then there is |
| 915 | // no value in common so break out. |
| 916 | if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) { |
| 917 | commonValue = nil |
| 918 | break |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | // If the fields all have a common value then store it in the common struct field |
| 924 | // and set the input struct's field to the empty value. |
| 925 | if commonValue != nil { |
| 926 | emptyValue := emptyStructValue.Field(f) |
| 927 | commonStructValue.Field(f).Set(*commonValue) |
| 928 | for i := 0; i < sliceValue.Len(); i++ { |
| 929 | structValue := sliceValue.Index(i).Elem().Elem() |
| 930 | fieldValue := structValue.Field(f) |
| 931 | fieldValue.Set(emptyValue) |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |