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 | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 23 | "android/soong/apex" |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 24 | "android/soong/cc" |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 25 | "github.com/google/blueprint" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
| 27 | |
| 28 | "android/soong/android" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | var pctx = android.NewPackageContext("android/soong/sdk") |
| 32 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 33 | var ( |
| 34 | repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip", |
| 35 | blueprint.RuleParams{ |
Paul Duffin | ce482dc | 2019-12-09 19:58:17 +0000 | [diff] [blame] | 36 | Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`, |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 37 | CommandDeps: []string{ |
| 38 | "${config.Zip2ZipCmd}", |
| 39 | }, |
| 40 | }, |
| 41 | "destdir") |
| 42 | |
| 43 | zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles", |
| 44 | blueprint.RuleParams{ |
| 45 | Command: `${config.SoongZipCmd} -C $basedir -l $out.rsp -o $out`, |
| 46 | CommandDeps: []string{ |
| 47 | "${config.SoongZipCmd}", |
| 48 | }, |
| 49 | Rspfile: "$out.rsp", |
| 50 | RspfileContent: "$in", |
| 51 | }, |
| 52 | "basedir") |
| 53 | |
| 54 | mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips", |
| 55 | blueprint.RuleParams{ |
| 56 | Command: `${config.MergeZipsCmd} $out $in`, |
| 57 | CommandDeps: []string{ |
| 58 | "${config.MergeZipsCmd}", |
| 59 | }, |
| 60 | }) |
| 61 | ) |
| 62 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 63 | type generatedContents struct { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 64 | content strings.Builder |
| 65 | indentLevel int |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 66 | } |
| 67 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 68 | // generatedFile abstracts operations for writing contents into a file and emit a build rule |
| 69 | // for the file. |
| 70 | type generatedFile struct { |
| 71 | generatedContents |
| 72 | path android.OutputPath |
| 73 | } |
| 74 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 75 | func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 76 | return &generatedFile{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 77 | path: android.PathForModuleOut(ctx, path...).OutputPath, |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 81 | func (gc *generatedContents) Indent() { |
| 82 | gc.indentLevel++ |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 85 | func (gc *generatedContents) Dedent() { |
| 86 | gc.indentLevel-- |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 87 | } |
| 88 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 89 | func (gc *generatedContents) Printfln(format string, args ...interface{}) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 90 | // ninja consumes newline characters in rspfile_content. Prevent it by |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 91 | // escaping the backslash in the newline character. The extra backslash |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 92 | // is removed when the rspfile is written to the actual script file |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 93 | fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\\n", args...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) { |
| 97 | rb := android.NewRuleBuilder() |
| 98 | // convert \\n to \n |
| 99 | rb.Command(). |
| 100 | Implicits(implicits). |
| 101 | Text("echo").Text(proptools.ShellEscape(gf.content.String())). |
| 102 | Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path) |
| 103 | rb.Command(). |
| 104 | Text("chmod a+x").Output(gf.path) |
| 105 | rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base()) |
| 106 | } |
| 107 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 108 | // Collect all the members. |
| 109 | // |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 110 | // Returns a list containing type (extracted from the dependency tag) and the variant. |
| 111 | func (s *sdk) collectMembers(ctx android.ModuleContext) []sdkMemberRef { |
| 112 | var memberRefs []sdkMemberRef |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 113 | ctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 114 | tag := ctx.OtherModuleDependencyTag(child) |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 115 | if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok { |
| 116 | memberType := memberTag.SdkMemberType() |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 117 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 118 | // 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] | 119 | if !memberType.IsInstance(child) { |
| 120 | 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] | 121 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 122 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 123 | memberRefs = append(memberRefs, sdkMemberRef{memberType, child.(android.SdkAware)}) |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 124 | |
| 125 | // If the member type supports transitive sdk members then recurse down into |
| 126 | // its dependencies, otherwise exit traversal. |
| 127 | return memberType.HasTransitiveSdkMembers() |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 128 | } |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 129 | |
| 130 | return false |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 131 | }) |
| 132 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 133 | return memberRefs |
| 134 | } |
| 135 | |
| 136 | // Organize the members. |
| 137 | // |
| 138 | // The members are first grouped by type and then grouped by name. The order of |
| 139 | // the types is the order they are referenced in android.SdkMemberTypesRegistry. |
| 140 | // The names are in the order in which the dependencies were added. |
| 141 | // |
| 142 | // Returns the members as well as the multilib setting to use. |
| 143 | func (s *sdk) organizeMembers(ctx android.ModuleContext, memberRefs []sdkMemberRef) ([]*sdkMember, string) { |
| 144 | byType := make(map[android.SdkMemberType][]*sdkMember) |
| 145 | byName := make(map[string]*sdkMember) |
| 146 | |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 147 | multilib := multilibNone |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 148 | |
| 149 | for _, memberRef := range memberRefs { |
| 150 | memberType := memberRef.memberType |
| 151 | variant := memberRef.variant |
| 152 | |
| 153 | name := ctx.OtherModuleName(variant) |
| 154 | member := byName[name] |
| 155 | if member == nil { |
| 156 | member = &sdkMember{memberType: memberType, name: name} |
| 157 | byName[name] = member |
| 158 | byType[memberType] = append(byType[memberType], member) |
| 159 | } |
| 160 | |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 161 | multilib = multilib.addArchType(variant.Target().Arch.ArchType) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 162 | |
| 163 | // Only append new variants to the list. This is needed because a member can be both |
| 164 | // exported by the sdk and also be a transitive sdk member. |
| 165 | member.variants = appendUniqueVariants(member.variants, variant) |
| 166 | } |
| 167 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 168 | var members []*sdkMember |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 169 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 170 | membersOfType := byType[memberListProperty.memberType] |
| 171 | members = append(members, membersOfType...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 174 | return members, multilib.String() |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 175 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 176 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 177 | func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware { |
| 178 | for _, v := range variants { |
| 179 | if v == newVariant { |
| 180 | return variants |
| 181 | } |
| 182 | } |
| 183 | return append(variants, newVariant) |
| 184 | } |
| 185 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 186 | // SDK directory structure |
| 187 | // <sdk_root>/ |
| 188 | // Android.bp : definition of a 'sdk' module is here. This is a hand-made one. |
| 189 | // <api_ver>/ : below this directory are all auto-generated |
| 190 | // Android.bp : definition of 'sdk_snapshot' module is here |
| 191 | // aidl/ |
| 192 | // frameworks/base/core/..../IFoo.aidl : an exported AIDL file |
| 193 | // java/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 194 | // <module_name>.jar : the stub jar for a java library 'module_name' |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 195 | // include/ |
| 196 | // bionic/libc/include/stdlib.h : an exported header file |
| 197 | // include_gen/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 198 | // <module_name>/com/android/.../IFoo.h : a generated header file |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 199 | // <arch>/include/ : arch-specific exported headers |
| 200 | // <arch>/include_gen/ : arch-specific generated headers |
| 201 | // <arch>/lib/ |
| 202 | // libFoo.so : a stub library |
| 203 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 204 | // 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] | 205 | // This isn't visible to users, so could be changed in future. |
| 206 | func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string { |
| 207 | return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version |
| 208 | } |
| 209 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 210 | // buildSnapshot is the main function in this source file. It creates rules to copy |
| 211 | // the contents (header files, stub libraries, etc) into the zip file. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 212 | func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) android.OutputPath { |
| 213 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 214 | allMembersByName := make(map[string]struct{}) |
| 215 | exportedMembersByName := make(map[string]struct{}) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 216 | var memberRefs []sdkMemberRef |
| 217 | for _, sdkVariant := range sdkVariants { |
| 218 | memberRefs = append(memberRefs, sdkVariant.memberRefs...) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 219 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 220 | // Record the names of all the members, both explicitly specified and implicitly |
| 221 | // included. |
| 222 | for _, memberRef := range sdkVariant.memberRefs { |
| 223 | allMembersByName[memberRef.variant.Name()] = struct{}{} |
| 224 | } |
| 225 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 226 | // Merge the exported member sets from all sdk variants. |
| 227 | for key, _ := range sdkVariant.getExportedMembers() { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 228 | exportedMembersByName[key] = struct{}{} |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 229 | } |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 232 | snapshotDir := android.PathForModuleOut(ctx, "snapshot") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 233 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 234 | bp := newGeneratedFile(ctx, "snapshot", "Android.bp") |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 235 | |
| 236 | bpFile := &bpFile{ |
| 237 | modules: make(map[string]*bpModule), |
| 238 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 239 | |
| 240 | builder := &snapshotBuilder{ |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 241 | ctx: ctx, |
| 242 | sdk: s, |
| 243 | version: "current", |
| 244 | snapshotDir: snapshotDir.OutputPath, |
| 245 | copies: make(map[string]string), |
| 246 | filesToZip: []android.Path{bp.path}, |
| 247 | bpFile: bpFile, |
| 248 | prebuiltModules: make(map[string]*bpModule), |
| 249 | allMembersByName: allMembersByName, |
| 250 | exportedMembersByName: exportedMembersByName, |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 251 | } |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 252 | s.builderForTests = builder |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 253 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 254 | members, multilib := s.organizeMembers(ctx, memberRefs) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 255 | for _, member := range members { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 256 | memberType := member.memberType |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 257 | |
| 258 | memberCtx := &memberContext{ctx, builder} |
| 259 | |
| 260 | prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 261 | if prebuiltModule == nil { |
| 262 | // Fall back to legacy method of building a snapshot |
| 263 | memberType.BuildSnapshot(ctx, builder, member) |
| 264 | } else { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 265 | s.createMemberSnapshot(memberCtx, member, prebuiltModule) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 266 | } |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 267 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 268 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 269 | // Create a transformer that will transform an unversioned module into a versioned module. |
| 270 | unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder} |
| 271 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 272 | // Create a transformer that will transform an unversioned module by replacing any references |
| 273 | // to internal members with a unique module name and setting prefer: false. |
| 274 | unversionedTransformer := unversionedTransformation{builder: builder} |
| 275 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 276 | for _, unversioned := range builder.prebuiltOrder { |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 277 | // Prune any empty property sets. |
| 278 | unversioned = unversioned.transform(pruneEmptySetTransformer{}) |
| 279 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 280 | // 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] | 281 | versioned := unversioned.deepCopy() |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 282 | |
| 283 | // Transform the unversioned module into a versioned one. |
| 284 | versioned.transform(unversionedToVersionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 285 | bpFile.AddModule(versioned) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 286 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 287 | // Transform the unversioned module to make it suitable for use in the snapshot. |
| 288 | unversioned.transform(unversionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 289 | bpFile.AddModule(unversioned) |
| 290 | } |
| 291 | |
| 292 | // Create the snapshot module. |
| 293 | snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 294 | var snapshotModuleType string |
| 295 | if s.properties.Module_exports { |
| 296 | snapshotModuleType = "module_exports_snapshot" |
| 297 | } else { |
| 298 | snapshotModuleType = "sdk_snapshot" |
| 299 | } |
| 300 | snapshotModule := bpFile.newModule(snapshotModuleType) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 301 | snapshotModule.AddProperty("name", snapshotName) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 302 | |
| 303 | // Make sure that the snapshot has the same visibility as the sdk. |
| 304 | visibility := android.EffectiveVisibilityRules(ctx, s) |
| 305 | if len(visibility) != 0 { |
| 306 | snapshotModule.AddProperty("visibility", visibility) |
| 307 | } |
| 308 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 309 | addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 310 | |
| 311 | // Compile_multilib defaults to both and must always be set to both on the |
| 312 | // device and so only needs to be set when targeted at the host and is neither |
| 313 | // unspecified or both. |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 314 | targetPropertySet := snapshotModule.AddPropertySet("target") |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 315 | if s.HostSupported() && multilib != "" && multilib != "both" { |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 316 | hostSet := targetPropertySet.AddPropertySet("host") |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 317 | hostSet.AddProperty("compile_multilib", multilib) |
| 318 | } |
| 319 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 320 | var dynamicMemberPropertiesList []interface{} |
| 321 | osTypeToMemberProperties := make(map[android.OsType]*sdk) |
| 322 | for _, sdkVariant := range sdkVariants { |
| 323 | properties := sdkVariant.dynamicMemberTypeListProperties |
| 324 | osTypeToMemberProperties[sdkVariant.Target().Os] = sdkVariant |
| 325 | dynamicMemberPropertiesList = append(dynamicMemberPropertiesList, properties) |
| 326 | } |
| 327 | |
| 328 | // Extract the common lists of members into a separate struct. |
| 329 | commonDynamicMemberProperties := s.dynamicSdkMemberTypes.createMemberListProperties() |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 330 | extractor := newCommonValueExtractor(commonDynamicMemberProperties) |
| 331 | extractor.extractCommonProperties(commonDynamicMemberProperties, dynamicMemberPropertiesList) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 332 | |
| 333 | // Add properties common to all os types. |
| 334 | s.addMemberPropertiesToPropertySet(builder, snapshotModule, commonDynamicMemberProperties) |
| 335 | |
| 336 | // Iterate over the os types in a fixed order. |
| 337 | for _, osType := range s.getPossibleOsTypes() { |
| 338 | if sdkVariant, ok := osTypeToMemberProperties[osType]; ok { |
| 339 | osPropertySet := targetPropertySet.AddPropertySet(sdkVariant.Target().Os.Name) |
| 340 | s.addMemberPropertiesToPropertySet(builder, osPropertySet, sdkVariant.dynamicMemberTypeListProperties) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 341 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 342 | } |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 343 | |
| 344 | // Prune any empty property sets. |
| 345 | snapshotModule.transform(pruneEmptySetTransformer{}) |
| 346 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 347 | bpFile.AddModule(snapshotModule) |
| 348 | |
| 349 | // generate Android.bp |
| 350 | bp = newGeneratedFile(ctx, "snapshot", "Android.bp") |
| 351 | generateBpContents(&bp.generatedContents, bpFile) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 352 | |
| 353 | bp.build(pctx, ctx, nil) |
| 354 | |
| 355 | filesToZip := builder.filesToZip |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 356 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 357 | // zip them all |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 358 | outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 359 | outputDesc := "Building snapshot for " + ctx.ModuleName() |
| 360 | |
| 361 | // If there are no zips to merge then generate the output zip directly. |
| 362 | // Otherwise, generate an intermediate zip file into which other zips can be |
| 363 | // merged. |
| 364 | var zipFile android.OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 365 | var desc string |
| 366 | if len(builder.zipsToMerge) == 0 { |
| 367 | zipFile = outputZipFile |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 368 | desc = outputDesc |
| 369 | } else { |
| 370 | zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 371 | desc = "Building intermediate snapshot for " + ctx.ModuleName() |
| 372 | } |
| 373 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 374 | ctx.Build(pctx, android.BuildParams{ |
| 375 | Description: desc, |
| 376 | Rule: zipFiles, |
| 377 | Inputs: filesToZip, |
| 378 | Output: zipFile, |
| 379 | Args: map[string]string{ |
| 380 | "basedir": builder.snapshotDir.String(), |
| 381 | }, |
| 382 | }) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 383 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 384 | if len(builder.zipsToMerge) != 0 { |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 385 | ctx.Build(pctx, android.BuildParams{ |
| 386 | Description: outputDesc, |
| 387 | Rule: mergeZips, |
| 388 | Input: zipFile, |
| 389 | Inputs: builder.zipsToMerge, |
| 390 | Output: outputZipFile, |
| 391 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | return outputZipFile |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 395 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 396 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 397 | func (s *sdk) addMemberPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, dynamicMemberTypeListProperties interface{}) { |
| 398 | for _, memberListProperty := range s.memberListProperties() { |
| 399 | names := memberListProperty.getter(dynamicMemberTypeListProperties) |
| 400 | if len(names) > 0 { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 401 | propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names, false)) |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 406 | type propertyTag struct { |
| 407 | name string |
| 408 | } |
| 409 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 410 | // A BpPropertyTag to add to a property that contains references to other sdk members. |
| 411 | // |
| 412 | // This will cause the references to be rewritten to a versioned reference in the version |
| 413 | // specific instance of a snapshot module. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 414 | var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"} |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 415 | var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"} |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 416 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 417 | // A BpPropertyTag that indicates the property should only be present in the versioned |
| 418 | // module. |
| 419 | // |
| 420 | // This will cause the property to be removed from the unversioned instance of a |
| 421 | // snapshot module. |
| 422 | var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"} |
| 423 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 424 | type unversionedToVersionedTransformation struct { |
| 425 | identityTransformation |
| 426 | builder *snapshotBuilder |
| 427 | } |
| 428 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 429 | func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule { |
| 430 | // Use a versioned name for the module but remember the original name for the |
| 431 | // snapshot. |
| 432 | name := module.getValue("name").(string) |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 433 | module.setProperty("name", t.builder.versionedSdkMemberName(name, true)) |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 434 | module.insertAfter("name", "sdk_member_name", name) |
| 435 | return module |
| 436 | } |
| 437 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 438 | 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] | 439 | if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag { |
| 440 | required := tag == requiredSdkMemberReferencePropertyTag |
| 441 | return t.builder.versionedSdkMemberNames(value.([]string), required), tag |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 442 | } else { |
| 443 | return value, tag |
| 444 | } |
| 445 | } |
| 446 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 447 | type unversionedTransformation struct { |
| 448 | identityTransformation |
| 449 | builder *snapshotBuilder |
| 450 | } |
| 451 | |
| 452 | func (t unversionedTransformation) transformModule(module *bpModule) *bpModule { |
| 453 | // If the module is an internal member then use a unique name for it. |
| 454 | name := module.getValue("name").(string) |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 455 | module.setProperty("name", t.builder.unversionedSdkMemberName(name, true)) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 456 | |
| 457 | // Set prefer: false - this is not strictly required as that is the default. |
| 458 | module.insertAfter("name", "prefer", false) |
| 459 | |
| 460 | return module |
| 461 | } |
| 462 | |
| 463 | 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] | 464 | if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag { |
| 465 | required := tag == requiredSdkMemberReferencePropertyTag |
| 466 | return t.builder.unversionedSdkMemberNames(value.([]string), required), tag |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 467 | } else if tag == sdkVersionedOnlyPropertyTag { |
| 468 | // The property is not allowed in the unversioned module so remove it. |
| 469 | return nil, nil |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 470 | } else { |
| 471 | return value, tag |
| 472 | } |
| 473 | } |
| 474 | |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame] | 475 | type pruneEmptySetTransformer struct { |
| 476 | identityTransformation |
| 477 | } |
| 478 | |
| 479 | var _ bpTransformer = (*pruneEmptySetTransformer)(nil) |
| 480 | |
| 481 | func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { |
| 482 | if len(propertySet.properties) == 0 { |
| 483 | return nil, nil |
| 484 | } else { |
| 485 | return propertySet, tag |
| 486 | } |
| 487 | } |
| 488 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 489 | func generateBpContents(contents *generatedContents, bpFile *bpFile) { |
| 490 | contents.Printfln("// This is auto-generated. DO NOT EDIT.") |
| 491 | for _, bpModule := range bpFile.order { |
| 492 | contents.Printfln("") |
| 493 | contents.Printfln("%s {", bpModule.moduleType) |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 494 | outputPropertySet(contents, bpModule.bpPropertySet) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 495 | contents.Printfln("}") |
| 496 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | func outputPropertySet(contents *generatedContents, set *bpPropertySet) { |
| 500 | contents.Indent() |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 501 | |
| 502 | // Output the properties first, followed by the nested sets. This ensures a |
| 503 | // consistent output irrespective of whether property sets are created before |
| 504 | // or after the properties. This simplifies the creation of the module. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 505 | for _, name := range set.order { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 506 | value := set.getValue(name) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 507 | |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 508 | switch v := value.(type) { |
| 509 | case []string: |
| 510 | length := len(v) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 511 | if length > 1 { |
| 512 | contents.Printfln("%s: [", name) |
| 513 | contents.Indent() |
| 514 | for i := 0; i < length; i = i + 1 { |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 515 | contents.Printfln("%q,", v[i]) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 516 | } |
| 517 | contents.Dedent() |
| 518 | contents.Printfln("],") |
| 519 | } else if length == 0 { |
| 520 | contents.Printfln("%s: [],", name) |
| 521 | } else { |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 522 | contents.Printfln("%s: [%q],", name, v[0]) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 523 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 524 | |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 525 | case bool: |
| 526 | contents.Printfln("%s: %t,", name, v) |
| 527 | |
| 528 | case *bpPropertySet: |
| 529 | // Do not write property sets in the properties phase. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 530 | |
| 531 | default: |
| 532 | contents.Printfln("%s: %q,", name, value) |
| 533 | } |
| 534 | } |
Paul Duffin | 07ef3cb | 2020-03-11 18:17:42 +0000 | [diff] [blame] | 535 | |
| 536 | for _, name := range set.order { |
| 537 | value := set.getValue(name) |
| 538 | |
| 539 | // Only write property sets in the sets phase. |
| 540 | switch v := value.(type) { |
| 541 | case *bpPropertySet: |
| 542 | contents.Printfln("%s: {", name) |
| 543 | outputPropertySet(contents, v) |
| 544 | contents.Printfln("},") |
| 545 | } |
| 546 | } |
| 547 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 548 | contents.Dedent() |
| 549 | } |
| 550 | |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 551 | func (s *sdk) GetAndroidBpContentsForTests() string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 552 | contents := &generatedContents{} |
| 553 | generateBpContents(contents, s.builderForTests.bpFile) |
| 554 | return contents.content.String() |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 557 | type snapshotBuilder struct { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 558 | ctx android.ModuleContext |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 559 | sdk *sdk |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 560 | version string |
| 561 | snapshotDir android.OutputPath |
| 562 | bpFile *bpFile |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 563 | |
| 564 | // Map from destination to source of each copy - used to eliminate duplicates and |
| 565 | // detect conflicts. |
| 566 | copies map[string]string |
| 567 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 568 | filesToZip android.Paths |
| 569 | zipsToMerge android.Paths |
| 570 | |
| 571 | prebuiltModules map[string]*bpModule |
| 572 | prebuiltOrder []*bpModule |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 573 | |
| 574 | // The set of all members by name. |
| 575 | allMembersByName map[string]struct{} |
| 576 | |
| 577 | // The set of exported members by name. |
| 578 | exportedMembersByName map[string]struct{} |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) { |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 582 | if existing, ok := s.copies[dest]; ok { |
| 583 | if existing != src.String() { |
| 584 | s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src) |
| 585 | return |
| 586 | } |
| 587 | } else { |
| 588 | path := s.snapshotDir.Join(s.ctx, dest) |
| 589 | s.ctx.Build(pctx, android.BuildParams{ |
| 590 | Rule: android.Cp, |
| 591 | Input: src, |
| 592 | Output: path, |
| 593 | }) |
| 594 | s.filesToZip = append(s.filesToZip, path) |
| 595 | |
| 596 | s.copies[dest] = src.String() |
| 597 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 600 | func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) { |
| 601 | ctx := s.ctx |
| 602 | |
| 603 | // Repackage the zip file so that the entries are in the destDir directory. |
| 604 | // This will allow the zip file to be merged into the snapshot. |
| 605 | tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 606 | |
| 607 | ctx.Build(pctx, android.BuildParams{ |
| 608 | Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(), |
| 609 | Rule: repackageZip, |
| 610 | Input: zipPath, |
| 611 | Output: tmpZipPath, |
| 612 | Args: map[string]string{ |
| 613 | "destdir": destDir, |
| 614 | }, |
| 615 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 616 | |
| 617 | // Add the repackaged zip file to the files to merge. |
| 618 | s.zipsToMerge = append(s.zipsToMerge, tmpZipPath) |
| 619 | } |
| 620 | |
Paul Duffin | 9d8d609 | 2019-12-05 18:19:29 +0000 | [diff] [blame] | 621 | func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule { |
| 622 | name := member.Name() |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 623 | if s.prebuiltModules[name] != nil { |
| 624 | panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name)) |
| 625 | } |
| 626 | |
| 627 | m := s.bpFile.newModule(moduleType) |
| 628 | m.AddProperty("name", name) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 629 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 630 | variant := member.Variants()[0] |
| 631 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 632 | if s.isInternalMember(name) { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 633 | // An internal member is only referenced from the sdk snapshot which is in the |
| 634 | // same package so can be marked as private. |
| 635 | m.AddProperty("visibility", []string{"//visibility:private"}) |
| 636 | } else { |
| 637 | // Extract visibility information from a member variant. All variants have the same |
| 638 | // visibility so it doesn't matter which one is used. |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 639 | visibility := android.EffectiveVisibilityRules(s.ctx, variant) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 640 | if len(visibility) != 0 { |
| 641 | m.AddProperty("visibility", visibility) |
| 642 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 645 | deviceSupported := false |
| 646 | hostSupported := false |
| 647 | |
| 648 | for _, variant := range member.Variants() { |
| 649 | osClass := variant.Target().Os.Class |
| 650 | if osClass == android.Host || osClass == android.HostCross { |
| 651 | hostSupported = true |
| 652 | } else if osClass == android.Device { |
| 653 | deviceSupported = true |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | addHostDeviceSupportedProperties(deviceSupported, hostSupported, m) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 658 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 659 | // Where available copy apex_available properties from the member. |
| 660 | if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok { |
| 661 | apexAvailable := apexAware.ApexAvailable() |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 662 | |
| 663 | // Add in any white listed apex available settings. |
| 664 | apexAvailable = append(apexAvailable, apex.WhitelistedApexAvailable(member.Name())...) |
| 665 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 666 | if len(apexAvailable) > 0 { |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 667 | // Remove duplicates and sort. |
| 668 | apexAvailable = android.FirstUniqueStrings(apexAvailable) |
| 669 | sort.Strings(apexAvailable) |
| 670 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 671 | m.AddProperty("apex_available", apexAvailable) |
| 672 | } |
| 673 | } |
| 674 | |
Paul Duffin | 0cb37b9 | 2020-03-04 14:52:46 +0000 | [diff] [blame] | 675 | // Disable installation in the versioned module of those modules that are ever installable. |
| 676 | if installable, ok := variant.(interface{ EverInstallable() bool }); ok { |
| 677 | if installable.EverInstallable() { |
| 678 | m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag) |
| 679 | } |
| 680 | } |
| 681 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 682 | s.prebuiltModules[name] = m |
| 683 | s.prebuiltOrder = append(s.prebuiltOrder, m) |
| 684 | return m |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 687 | func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) { |
| 688 | if !deviceSupported { |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 689 | bpModule.AddProperty("device_supported", false) |
| 690 | } |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 691 | if hostSupported { |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 692 | bpModule.AddProperty("host_supported", true) |
| 693 | } |
| 694 | } |
| 695 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 696 | func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag { |
| 697 | if required { |
| 698 | return requiredSdkMemberReferencePropertyTag |
| 699 | } else { |
| 700 | return optionalSdkMemberReferencePropertyTag |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag { |
| 705 | return optionalSdkMemberReferencePropertyTag |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 708 | // Get a versioned name appropriate for the SDK snapshot version being taken. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 709 | func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string, required bool) string { |
| 710 | if _, ok := s.allMembersByName[unversionedName]; !ok { |
| 711 | if required { |
| 712 | s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName) |
| 713 | } |
| 714 | return unversionedName |
| 715 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 716 | return versionedSdkMemberName(s.ctx, unversionedName, s.version) |
| 717 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 718 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 719 | func (s *snapshotBuilder) versionedSdkMemberNames(members []string, required bool) []string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 720 | var references []string = nil |
| 721 | for _, m := range members { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 722 | references = append(references, s.versionedSdkMemberName(m, required)) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 723 | } |
| 724 | return references |
| 725 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 726 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 727 | // Get an internal name unique to the sdk. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 728 | func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string, required bool) string { |
| 729 | if _, ok := s.allMembersByName[unversionedName]; !ok { |
| 730 | if required { |
| 731 | s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName) |
| 732 | } |
| 733 | return unversionedName |
| 734 | } |
| 735 | |
| 736 | if s.isInternalMember(unversionedName) { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 737 | return s.ctx.ModuleName() + "_" + unversionedName |
| 738 | } else { |
| 739 | return unversionedName |
| 740 | } |
| 741 | } |
| 742 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 743 | func (s *snapshotBuilder) unversionedSdkMemberNames(members []string, required bool) []string { |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 744 | var references []string = nil |
| 745 | for _, m := range members { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 746 | references = append(references, s.unversionedSdkMemberName(m, required)) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 747 | } |
| 748 | return references |
| 749 | } |
| 750 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 751 | func (s *snapshotBuilder) isInternalMember(memberName string) bool { |
| 752 | _, ok := s.exportedMembersByName[memberName] |
| 753 | return !ok |
| 754 | } |
| 755 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 756 | type sdkMemberRef struct { |
| 757 | memberType android.SdkMemberType |
| 758 | variant android.SdkAware |
| 759 | } |
| 760 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 761 | var _ android.SdkMember = (*sdkMember)(nil) |
| 762 | |
| 763 | type sdkMember struct { |
| 764 | memberType android.SdkMemberType |
| 765 | name string |
| 766 | variants []android.SdkAware |
| 767 | } |
| 768 | |
| 769 | func (m *sdkMember) Name() string { |
| 770 | return m.name |
| 771 | } |
| 772 | |
| 773 | func (m *sdkMember) Variants() []android.SdkAware { |
| 774 | return m.variants |
| 775 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 776 | |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 777 | // Track usages of multilib variants. |
| 778 | type multilibUsage int |
| 779 | |
| 780 | const ( |
| 781 | multilibNone multilibUsage = 0 |
| 782 | multilib32 multilibUsage = 1 |
| 783 | multilib64 multilibUsage = 2 |
| 784 | multilibBoth = multilib32 | multilib64 |
| 785 | ) |
| 786 | |
| 787 | // Add the multilib that is used in the arch type. |
| 788 | func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage { |
| 789 | multilib := archType.Multilib |
| 790 | switch multilib { |
| 791 | case "": |
| 792 | return m |
| 793 | case "lib32": |
| 794 | return m | multilib32 |
| 795 | case "lib64": |
| 796 | return m | multilib64 |
| 797 | default: |
| 798 | panic(fmt.Errorf("Unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib)) |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | func (m multilibUsage) String() string { |
| 803 | switch m { |
| 804 | case multilibNone: |
| 805 | return "" |
| 806 | case multilib32: |
| 807 | return "32" |
| 808 | case multilib64: |
| 809 | return "64" |
| 810 | case multilibBoth: |
| 811 | return "both" |
| 812 | default: |
| 813 | panic(fmt.Errorf("Unknown multilib value, found %b, expected one of %b, %b, %b or %b", |
| 814 | m, multilibNone, multilib32, multilib64, multilibBoth)) |
| 815 | } |
| 816 | } |
| 817 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 818 | type baseInfo struct { |
| 819 | Properties android.SdkMemberProperties |
| 820 | } |
| 821 | |
| 822 | type osTypeSpecificInfo struct { |
| 823 | baseInfo |
| 824 | |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 825 | osType android.OsType |
| 826 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 827 | // The list of arch type specific info for this os type. |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 828 | // |
| 829 | // Nil if there is one variant whose arch type is common |
| 830 | archInfos []*archTypeSpecificInfo |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 833 | type variantPropertiesFactoryFunc func() android.SdkMemberProperties |
| 834 | |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 835 | // Create a new osTypeSpecificInfo for the specified os type and its properties |
| 836 | // structures populated with information from the variants. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 837 | 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] | 838 | osInfo := &osTypeSpecificInfo{ |
| 839 | osType: osType, |
| 840 | } |
| 841 | |
| 842 | osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties { |
| 843 | properties := variantPropertiesFactory() |
| 844 | properties.Base().Os = osType |
| 845 | return properties |
| 846 | } |
| 847 | |
| 848 | // Create a structure into which properties common across the architectures in |
| 849 | // this os type will be stored. |
| 850 | osInfo.Properties = osSpecificVariantPropertiesFactory() |
| 851 | |
| 852 | // Group the variants by arch type. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 853 | var variantsByArchName = make(map[string][]android.Module) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 854 | var archTypes []android.ArchType |
| 855 | for _, variant := range osTypeVariants { |
| 856 | archType := variant.Target().Arch.ArchType |
| 857 | archTypeName := archType.Name |
| 858 | if _, ok := variantsByArchName[archTypeName]; !ok { |
| 859 | archTypes = append(archTypes, archType) |
| 860 | } |
| 861 | |
| 862 | variantsByArchName[archTypeName] = append(variantsByArchName[archTypeName], variant) |
| 863 | } |
| 864 | |
| 865 | if commonVariants, ok := variantsByArchName["common"]; ok { |
| 866 | if len(osTypeVariants) != 1 { |
| 867 | panic("Expected to only have 1 variant when arch type is common but found " + string(len(osTypeVariants))) |
| 868 | } |
| 869 | |
| 870 | // A common arch type only has one variant and its properties should be treated |
| 871 | // as common to the os type. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 872 | osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0]) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 873 | } else { |
| 874 | // Create an arch specific info for each supported architecture type. |
| 875 | for _, archType := range archTypes { |
| 876 | archTypeName := archType.Name |
| 877 | |
| 878 | archVariants := variantsByArchName[archTypeName] |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 879 | archInfo := newArchSpecificInfo(ctx, archType, osSpecificVariantPropertiesFactory, archVariants) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 880 | |
| 881 | osInfo.archInfos = append(osInfo.archInfos, archInfo) |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return osInfo |
| 886 | } |
| 887 | |
| 888 | // Optimize the properties by extracting common properties from arch type specific |
| 889 | // properties into os type specific properties. |
| 890 | func (osInfo *osTypeSpecificInfo) optimizeProperties(commonValueExtractor *commonValueExtractor) { |
| 891 | // Nothing to do if there is only a single common architecture. |
| 892 | if len(osInfo.archInfos) == 0 { |
| 893 | return |
| 894 | } |
| 895 | |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 896 | multilib := multilibNone |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 897 | var archPropertiesList []android.SdkMemberProperties |
| 898 | for _, archInfo := range osInfo.archInfos { |
Paul Duffin | 9c3760e | 2020-03-16 19:52:08 +0000 | [diff] [blame] | 899 | multilib = multilib.addArchType(archInfo.archType) |
| 900 | |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 901 | // Optimize the arch properties first. |
| 902 | archInfo.optimizeProperties(commonValueExtractor) |
| 903 | |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 904 | archPropertiesList = append(archPropertiesList, archInfo.Properties) |
| 905 | } |
| 906 | |
| 907 | commonValueExtractor.extractCommonProperties(osInfo.Properties, archPropertiesList) |
| 908 | |
| 909 | // 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] | 910 | osInfo.Properties.Base().Compile_multilib = multilib.String() |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | // Add the properties for an os to a property set. |
| 914 | // |
| 915 | // Maps the properties related to the os variants through to an appropriate |
| 916 | // module structure that will produce equivalent set of variants when it is |
| 917 | // processed in a build. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 918 | func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) { |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 919 | |
| 920 | var osPropertySet android.BpPropertySet |
| 921 | var archPropertySet android.BpPropertySet |
| 922 | var archOsPrefix string |
| 923 | if osInfo.Properties.Base().Os_count == 1 { |
| 924 | // There is only one os type present in the variants so don't bother |
| 925 | // with adding target specific properties. |
| 926 | |
| 927 | // Create a structure that looks like: |
| 928 | // module_type { |
| 929 | // name: "...", |
| 930 | // ... |
| 931 | // <common properties> |
| 932 | // ... |
| 933 | // <single os type specific properties> |
| 934 | // |
| 935 | // arch: { |
| 936 | // <arch specific sections> |
| 937 | // } |
| 938 | // |
| 939 | osPropertySet = bpModule |
| 940 | archPropertySet = osPropertySet.AddPropertySet("arch") |
| 941 | |
| 942 | // Arch specific properties need to be added to an arch specific section |
| 943 | // within arch. |
| 944 | archOsPrefix = "" |
| 945 | } else { |
| 946 | // Create a structure that looks like: |
| 947 | // module_type { |
| 948 | // name: "...", |
| 949 | // ... |
| 950 | // <common properties> |
| 951 | // ... |
| 952 | // target: { |
| 953 | // <arch independent os specific sections, e.g. android> |
| 954 | // ... |
| 955 | // <arch and os specific sections, e.g. android_x86> |
| 956 | // } |
| 957 | // |
| 958 | osType := osInfo.osType |
| 959 | osPropertySet = targetPropertySet.AddPropertySet(osType.Name) |
| 960 | archPropertySet = targetPropertySet |
| 961 | |
| 962 | // Arch specific properties need to be added to an os and arch specific |
| 963 | // section prefixed with <os>_. |
| 964 | archOsPrefix = osType.Name + "_" |
| 965 | } |
| 966 | |
| 967 | // Add the os specific but arch independent properties to the module. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 968 | osInfo.Properties.AddToPropertySet(ctx, osPropertySet) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 969 | |
| 970 | // Add arch (and possibly os) specific sections for each set of arch (and possibly |
| 971 | // os) specific properties. |
| 972 | // |
| 973 | // The archInfos list will be empty if the os contains variants for the common |
| 974 | // architecture. |
| 975 | for _, archInfo := range osInfo.archInfos { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 976 | archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix) |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 980 | type archTypeSpecificInfo struct { |
| 981 | baseInfo |
| 982 | |
| 983 | archType android.ArchType |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 984 | |
| 985 | linkInfos []*linkTypeSpecificInfo |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 988 | // Create a new archTypeSpecificInfo for the specified arch type and its properties |
| 989 | // structures populated with information from the variants. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 990 | func newArchSpecificInfo(ctx android.SdkMemberContext, archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo { |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 991 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 992 | // Create an arch specific info into which the variant properties can be copied. |
| 993 | archInfo := &archTypeSpecificInfo{archType: archType} |
| 994 | |
| 995 | // Create the properties into which the arch type specific properties will be |
| 996 | // added. |
| 997 | archInfo.Properties = variantPropertiesFactory() |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 998 | |
| 999 | if len(archVariants) == 1 { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1000 | archInfo.Properties.PopulateFromVariant(ctx, archVariants[0]) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1001 | } else { |
| 1002 | // There is more than one variant for this arch type which must be differentiated |
| 1003 | // by link type. |
| 1004 | for _, linkVariant := range archVariants { |
| 1005 | linkType := getLinkType(linkVariant) |
| 1006 | if linkType == "" { |
| 1007 | panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(archVariants))) |
| 1008 | } else { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1009 | linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1010 | |
| 1011 | archInfo.linkInfos = append(archInfo.linkInfos, linkInfo) |
| 1012 | } |
| 1013 | } |
| 1014 | } |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1015 | |
| 1016 | return archInfo |
| 1017 | } |
| 1018 | |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1019 | // Get the link type of the variant |
| 1020 | // |
| 1021 | // If the variant is not differentiated by link type then it returns "", |
| 1022 | // otherwise it returns one of "static" or "shared". |
| 1023 | func getLinkType(variant android.Module) string { |
| 1024 | linkType := "" |
| 1025 | if linkable, ok := variant.(cc.LinkableInterface); ok { |
| 1026 | if linkable.Shared() && linkable.Static() { |
| 1027 | panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String())) |
| 1028 | } else if linkable.Shared() { |
| 1029 | linkType = "shared" |
| 1030 | } else if linkable.Static() { |
| 1031 | linkType = "static" |
| 1032 | } else { |
| 1033 | panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String())) |
| 1034 | } |
| 1035 | } |
| 1036 | return linkType |
| 1037 | } |
| 1038 | |
| 1039 | // Optimize the properties by extracting common properties from link type specific |
| 1040 | // properties into arch type specific properties. |
| 1041 | func (archInfo *archTypeSpecificInfo) optimizeProperties(commonValueExtractor *commonValueExtractor) { |
| 1042 | if len(archInfo.linkInfos) == 0 { |
| 1043 | return |
| 1044 | } |
| 1045 | |
| 1046 | var propertiesList []android.SdkMemberProperties |
| 1047 | for _, linkInfo := range archInfo.linkInfos { |
| 1048 | propertiesList = append(propertiesList, linkInfo.Properties) |
| 1049 | } |
| 1050 | |
| 1051 | commonValueExtractor.extractCommonProperties(archInfo.Properties, propertiesList) |
| 1052 | } |
| 1053 | |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1054 | // Add the properties for an arch type to a property set. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1055 | func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) { |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1056 | archTypeName := archInfo.archType.Name |
| 1057 | archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName) |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1058 | archInfo.Properties.AddToPropertySet(ctx, archTypePropertySet) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1059 | |
| 1060 | for _, linkInfo := range archInfo.linkInfos { |
| 1061 | linkPropertySet := archTypePropertySet.AddPropertySet(linkInfo.linkType) |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1062 | linkInfo.Properties.AddToPropertySet(ctx, linkPropertySet) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | type linkTypeSpecificInfo struct { |
| 1067 | baseInfo |
| 1068 | |
| 1069 | linkType string |
| 1070 | } |
| 1071 | |
| 1072 | // Create a new linkTypeSpecificInfo for the specified link type and its properties |
| 1073 | // structures populated with information from the variant. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1074 | 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] | 1075 | linkInfo := &linkTypeSpecificInfo{ |
| 1076 | baseInfo: baseInfo{ |
| 1077 | // Create the properties into which the link type specific properties will be |
| 1078 | // added. |
| 1079 | Properties: variantPropertiesFactory(), |
| 1080 | }, |
| 1081 | linkType: linkType, |
| 1082 | } |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1083 | linkInfo.Properties.PopulateFromVariant(ctx, linkVariant) |
Paul Duffin | 9b76c0b | 2020-03-12 10:24:35 +0000 | [diff] [blame] | 1084 | return linkInfo |
Paul Duffin | fc8dd23 | 2020-03-17 12:51:37 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1087 | type memberContext struct { |
| 1088 | sdkMemberContext android.ModuleContext |
| 1089 | builder *snapshotBuilder |
| 1090 | } |
| 1091 | |
| 1092 | func (m *memberContext) SdkModuleContext() android.ModuleContext { |
| 1093 | return m.sdkMemberContext |
| 1094 | } |
| 1095 | |
| 1096 | func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder { |
| 1097 | return m.builder |
| 1098 | } |
| 1099 | |
| 1100 | func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule android.BpModule) { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1101 | |
| 1102 | memberType := member.memberType |
| 1103 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1104 | // Group the variants by os type. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1105 | variantsByOsType := make(map[android.OsType][]android.Module) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1106 | variants := member.Variants() |
| 1107 | for _, variant := range variants { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1108 | osType := variant.Target().Os |
| 1109 | variantsByOsType[osType] = append(variantsByOsType[osType], variant) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1112 | osCount := len(variantsByOsType) |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 1113 | variantPropertiesFactory := func() android.SdkMemberProperties { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1114 | properties := memberType.CreateVariantPropertiesStruct() |
| 1115 | base := properties.Base() |
| 1116 | base.Os_count = osCount |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1117 | return properties |
| 1118 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1119 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1120 | osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1121 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1122 | // 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] | 1123 | commonProperties := variantPropertiesFactory() |
| 1124 | commonProperties.Base().Os = android.CommonOS |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1125 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1126 | // Create common value extractor that can be used to optimize the properties. |
| 1127 | commonValueExtractor := newCommonValueExtractor(commonProperties) |
| 1128 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1129 | // The list of property structures which are os type specific but common across |
| 1130 | // architectures within that os type. |
| 1131 | var osSpecificPropertiesList []android.SdkMemberProperties |
| 1132 | |
| 1133 | for osType, osTypeVariants := range variantsByOsType { |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1134 | osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants) |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1135 | osTypeToInfo[osType] = osInfo |
Paul Duffin | b44b33a | 2020-03-17 10:58:23 +0000 | [diff] [blame] | 1136 | // Add the os specific properties to a list of os type specific yet architecture |
| 1137 | // independent properties structs. |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1138 | osSpecificPropertiesList = append(osSpecificPropertiesList, osInfo.Properties) |
| 1139 | |
Paul Duffin | 00e4680 | 2020-03-12 20:40:35 +0000 | [diff] [blame] | 1140 | // Optimize the properties across all the variants for a specific os type. |
| 1141 | osInfo.optimizeProperties(commonValueExtractor) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1142 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1143 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1144 | // Extract properties which are common across all architectures and os types. |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1145 | commonValueExtractor.extractCommonProperties(commonProperties, osSpecificPropertiesList) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1146 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1147 | // Add the common properties to the module. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1148 | commonProperties.AddToPropertySet(ctx, bpModule) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1149 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1150 | // Create a target property set into which target specific properties can be |
| 1151 | // added. |
| 1152 | targetPropertySet := bpModule.AddPropertySet("target") |
| 1153 | |
| 1154 | // Iterate over the os types in a fixed order. |
| 1155 | for _, osType := range s.getPossibleOsTypes() { |
| 1156 | osInfo := osTypeToInfo[osType] |
| 1157 | if osInfo == nil { |
| 1158 | continue |
| 1159 | } |
| 1160 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame^] | 1161 | osInfo.addToPropertySet(ctx, bpModule, targetPropertySet) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1162 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1165 | // Compute the list of possible os types that this sdk could support. |
| 1166 | func (s *sdk) getPossibleOsTypes() []android.OsType { |
| 1167 | var osTypes []android.OsType |
| 1168 | for _, osType := range android.OsTypeList { |
| 1169 | if s.DeviceSupported() { |
| 1170 | if osType.Class == android.Device && osType != android.Fuchsia { |
| 1171 | osTypes = append(osTypes, osType) |
| 1172 | } |
| 1173 | } |
| 1174 | if s.HostSupported() { |
| 1175 | if osType.Class == android.Host || osType.Class == android.HostCross { |
| 1176 | osTypes = append(osTypes, osType) |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name }) |
| 1181 | return osTypes |
| 1182 | } |
| 1183 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1184 | // Given a struct value, access a field within that struct (or one of its embedded |
| 1185 | // structs). |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1186 | type fieldAccessorFunc func(structValue reflect.Value) reflect.Value |
| 1187 | |
| 1188 | // Supports extracting common values from a number of instances of a properties |
| 1189 | // structure into a separate common set of properties. |
| 1190 | type commonValueExtractor struct { |
| 1191 | // The getters for every field from which common values can be extracted. |
| 1192 | fieldGetters []fieldAccessorFunc |
| 1193 | } |
| 1194 | |
| 1195 | // Create a new common value extractor for the structure type for the supplied |
| 1196 | // properties struct. |
| 1197 | // |
| 1198 | // The returned extractor can be used on any properties structure of the same type |
| 1199 | // as the supplied set of properties. |
| 1200 | func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor { |
| 1201 | structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type() |
| 1202 | extractor := &commonValueExtractor{} |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1203 | extractor.gatherFields(structType, nil) |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1204 | return extractor |
| 1205 | } |
| 1206 | |
| 1207 | // Gather the fields from the supplied structure type from which common values will |
| 1208 | // be extracted. |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1209 | // |
| 1210 | // This is recursive function. If it encounters an embedded field (no field name) |
| 1211 | // that is a struct then it will recurse into that struct passing in the accessor |
| 1212 | // for the field. That will then be used in the accessors for the fields in the |
| 1213 | // embedded struct. |
| 1214 | func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc) { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1215 | for f := 0; f < structType.NumField(); f++ { |
| 1216 | field := structType.Field(f) |
| 1217 | if field.PkgPath != "" { |
| 1218 | // Ignore unexported fields. |
| 1219 | continue |
| 1220 | } |
| 1221 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1222 | // Ignore fields whose value should be kept. |
| 1223 | if proptools.HasTag(field, "sdk", "keep") { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1224 | continue |
| 1225 | } |
| 1226 | |
| 1227 | // Save a copy of the field index for use in the function. |
| 1228 | fieldIndex := f |
| 1229 | fieldGetter := func(value reflect.Value) reflect.Value { |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1230 | if containingStructAccessor != nil { |
| 1231 | // This is an embedded structure so first access the field for the embedded |
| 1232 | // structure. |
| 1233 | value = containingStructAccessor(value) |
| 1234 | } |
| 1235 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1236 | // Skip through interface and pointer values to find the structure. |
| 1237 | value = getStructValue(value) |
| 1238 | |
| 1239 | // Return the field. |
| 1240 | return value.Field(fieldIndex) |
| 1241 | } |
| 1242 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 1243 | if field.Type.Kind() == reflect.Struct && field.Anonymous { |
| 1244 | // Gather fields from the embedded structure. |
| 1245 | e.gatherFields(field.Type, fieldGetter) |
| 1246 | } else { |
| 1247 | e.fieldGetters = append(e.fieldGetters, fieldGetter) |
| 1248 | } |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | func getStructValue(value reflect.Value) reflect.Value { |
| 1253 | foundStruct: |
| 1254 | for { |
| 1255 | kind := value.Kind() |
| 1256 | switch kind { |
| 1257 | case reflect.Interface, reflect.Ptr: |
| 1258 | value = value.Elem() |
| 1259 | case reflect.Struct: |
| 1260 | break foundStruct |
| 1261 | default: |
| 1262 | panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind)) |
| 1263 | } |
| 1264 | } |
| 1265 | return value |
| 1266 | } |
| 1267 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1268 | // Extract common properties from a slice of property structures of the same type. |
| 1269 | // |
| 1270 | // All the property structures must be of the same type. |
| 1271 | // commonProperties - must be a pointer to the structure into which common properties will be added. |
| 1272 | // inputPropertiesSlice - must be a slice of input properties structures. |
| 1273 | // |
| 1274 | // Iterates over each exported field (capitalized name) and checks to see whether they |
| 1275 | // have the same value (using DeepEquals) across all the input properties. If it does not then no |
| 1276 | // change is made. Otherwise, the common value is stored in the field in the commonProperties |
| 1277 | // and the field in each of the input properties structure is set to its default value. |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1278 | func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1279 | commonPropertiesValue := reflect.ValueOf(commonProperties) |
| 1280 | commonStructValue := commonPropertiesValue.Elem() |
| 1281 | propertiesStructType := commonStructValue.Type() |
| 1282 | |
| 1283 | // Create an empty structure from which default values for the field can be copied. |
| 1284 | emptyStructValue := reflect.New(propertiesStructType).Elem() |
| 1285 | |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1286 | for _, fieldGetter := range e.fieldGetters { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1287 | // Check to see if all the structures have the same value for the field. The commonValue |
| 1288 | // is nil on entry to the loop and if it is nil on exit then there is no common value, |
| 1289 | // otherwise it points to the common value. |
| 1290 | var commonValue *reflect.Value |
| 1291 | sliceValue := reflect.ValueOf(inputPropertiesSlice) |
| 1292 | |
| 1293 | for i := 0; i < sliceValue.Len(); i++ { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1294 | itemValue := sliceValue.Index(i) |
| 1295 | fieldValue := fieldGetter(itemValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1296 | |
| 1297 | if commonValue == nil { |
| 1298 | // Use the first value as the commonProperties value. |
| 1299 | commonValue = &fieldValue |
| 1300 | } else { |
| 1301 | // If the value does not match the current common value then there is |
| 1302 | // no value in common so break out. |
| 1303 | if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) { |
| 1304 | commonValue = nil |
| 1305 | break |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | // If the fields all have a common value then store it in the common struct field |
| 1311 | // and set the input struct's field to the empty value. |
| 1312 | if commonValue != nil { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1313 | emptyValue := fieldGetter(emptyStructValue) |
| 1314 | fieldGetter(commonStructValue).Set(*commonValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1315 | for i := 0; i < sliceValue.Len(); i++ { |
Paul Duffin | c097e36 | 2020-03-10 22:50:03 +0000 | [diff] [blame] | 1316 | itemValue := sliceValue.Index(i) |
| 1317 | fieldValue := fieldGetter(itemValue) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 1318 | fieldValue.Set(emptyValue) |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | } |