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" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | |
| 25 | "android/soong/android" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var pctx = android.NewPackageContext("android/soong/sdk") |
| 29 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 30 | var ( |
| 31 | repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip", |
| 32 | blueprint.RuleParams{ |
Paul Duffin | ce482dc | 2019-12-09 19:58:17 +0000 | [diff] [blame] | 33 | Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`, |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 34 | CommandDeps: []string{ |
| 35 | "${config.Zip2ZipCmd}", |
| 36 | }, |
| 37 | }, |
| 38 | "destdir") |
| 39 | |
| 40 | zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles", |
| 41 | blueprint.RuleParams{ |
| 42 | Command: `${config.SoongZipCmd} -C $basedir -l $out.rsp -o $out`, |
| 43 | CommandDeps: []string{ |
| 44 | "${config.SoongZipCmd}", |
| 45 | }, |
| 46 | Rspfile: "$out.rsp", |
| 47 | RspfileContent: "$in", |
| 48 | }, |
| 49 | "basedir") |
| 50 | |
| 51 | mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips", |
| 52 | blueprint.RuleParams{ |
| 53 | Command: `${config.MergeZipsCmd} $out $in`, |
| 54 | CommandDeps: []string{ |
| 55 | "${config.MergeZipsCmd}", |
| 56 | }, |
| 57 | }) |
| 58 | ) |
| 59 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 60 | type generatedContents struct { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 61 | content strings.Builder |
| 62 | indentLevel int |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 65 | // generatedFile abstracts operations for writing contents into a file and emit a build rule |
| 66 | // for the file. |
| 67 | type generatedFile struct { |
| 68 | generatedContents |
| 69 | path android.OutputPath |
| 70 | } |
| 71 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 72 | func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 73 | return &generatedFile{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 74 | path: android.PathForModuleOut(ctx, path...).OutputPath, |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 78 | func (gc *generatedContents) Indent() { |
| 79 | gc.indentLevel++ |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 80 | } |
| 81 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 82 | func (gc *generatedContents) Dedent() { |
| 83 | gc.indentLevel-- |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 84 | } |
| 85 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 86 | func (gc *generatedContents) Printfln(format string, args ...interface{}) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 87 | // ninja consumes newline characters in rspfile_content. Prevent it by |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 88 | // escaping the backslash in the newline character. The extra backslash |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 89 | // is removed when the rspfile is written to the actual script file |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 90 | fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format+"\\n", args...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) { |
| 94 | rb := android.NewRuleBuilder() |
| 95 | // convert \\n to \n |
| 96 | rb.Command(). |
| 97 | Implicits(implicits). |
| 98 | Text("echo").Text(proptools.ShellEscape(gf.content.String())). |
| 99 | Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path) |
| 100 | rb.Command(). |
| 101 | Text("chmod a+x").Output(gf.path) |
| 102 | rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base()) |
| 103 | } |
| 104 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 105 | // Collect all the members. |
| 106 | // |
| 107 | // The members are first grouped by type and then grouped by name. The order of |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 108 | // the types is the order they are referenced in android.SdkMemberTypesRegistry. |
| 109 | // The names are in the order in which the dependencies were added. |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 110 | // |
| 111 | // Returns the members as well as the multilib setting to use. |
| 112 | func (s *sdk) collectMembers(ctx android.ModuleContext) ([]*sdkMember, string) { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 113 | byType := make(map[android.SdkMemberType][]*sdkMember) |
| 114 | byName := make(map[string]*sdkMember) |
| 115 | |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 116 | lib32 := false // True if any of the members have 32 bit version. |
| 117 | lib64 := false // True if any of the members have 64 bit version. |
| 118 | |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 119 | ctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 120 | tag := ctx.OtherModuleDependencyTag(child) |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 121 | if memberTag, ok := tag.(android.SdkMemberTypeDependencyTag); ok { |
| 122 | memberType := memberTag.SdkMemberType() |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 123 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 124 | // 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] | 125 | if !memberType.IsInstance(child) { |
| 126 | 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] | 127 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 128 | |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 129 | name := ctx.OtherModuleName(child) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 130 | member := byName[name] |
| 131 | if member == nil { |
| 132 | member = &sdkMember{memberType: memberType, name: name} |
| 133 | byName[name] = member |
| 134 | byType[memberType] = append(byType[memberType], member) |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 135 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 136 | |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 137 | multilib := child.Target().Arch.ArchType.Multilib |
| 138 | if multilib == "lib32" { |
| 139 | lib32 = true |
| 140 | } else if multilib == "lib64" { |
| 141 | lib64 = true |
| 142 | } |
| 143 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 144 | // Only append new variants to the list. This is needed because a member can be both |
| 145 | // exported by the sdk and also be a transitive sdk member. |
| 146 | member.variants = appendUniqueVariants(member.variants, child.(android.SdkAware)) |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 147 | |
| 148 | // If the member type supports transitive sdk members then recurse down into |
| 149 | // its dependencies, otherwise exit traversal. |
| 150 | return memberType.HasTransitiveSdkMembers() |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 151 | } |
Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 152 | |
| 153 | return false |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 154 | }) |
| 155 | |
| 156 | var members []*sdkMember |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 157 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 158 | membersOfType := byType[memberListProperty.memberType] |
| 159 | members = append(members, membersOfType...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 160 | } |
| 161 | |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 162 | // Compute the setting of multilib. |
| 163 | var multilib string |
| 164 | if lib32 && lib64 { |
| 165 | multilib = "both" |
| 166 | } else if lib32 { |
| 167 | multilib = "32" |
| 168 | } else if lib64 { |
| 169 | multilib = "64" |
| 170 | } |
| 171 | |
| 172 | return members, multilib |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 173 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 174 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 175 | func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware { |
| 176 | for _, v := range variants { |
| 177 | if v == newVariant { |
| 178 | return variants |
| 179 | } |
| 180 | } |
| 181 | return append(variants, newVariant) |
| 182 | } |
| 183 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 184 | // SDK directory structure |
| 185 | // <sdk_root>/ |
| 186 | // Android.bp : definition of a 'sdk' module is here. This is a hand-made one. |
| 187 | // <api_ver>/ : below this directory are all auto-generated |
| 188 | // Android.bp : definition of 'sdk_snapshot' module is here |
| 189 | // aidl/ |
| 190 | // frameworks/base/core/..../IFoo.aidl : an exported AIDL file |
| 191 | // java/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 192 | // <module_name>.jar : the stub jar for a java library 'module_name' |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 193 | // include/ |
| 194 | // bionic/libc/include/stdlib.h : an exported header file |
| 195 | // include_gen/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 196 | // <module_name>/com/android/.../IFoo.h : a generated header file |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 197 | // <arch>/include/ : arch-specific exported headers |
| 198 | // <arch>/include_gen/ : arch-specific generated headers |
| 199 | // <arch>/lib/ |
| 200 | // libFoo.so : a stub library |
| 201 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 202 | // 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] | 203 | // This isn't visible to users, so could be changed in future. |
| 204 | func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string { |
| 205 | return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version |
| 206 | } |
| 207 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 208 | // buildSnapshot is the main function in this source file. It creates rules to copy |
| 209 | // the contents (header files, stub libraries, etc) into the zip file. |
| 210 | func (s *sdk) buildSnapshot(ctx android.ModuleContext) android.OutputPath { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 211 | snapshotDir := android.PathForModuleOut(ctx, "snapshot") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 212 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 213 | bp := newGeneratedFile(ctx, "snapshot", "Android.bp") |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 214 | |
| 215 | bpFile := &bpFile{ |
| 216 | modules: make(map[string]*bpModule), |
| 217 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 218 | |
| 219 | builder := &snapshotBuilder{ |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 220 | ctx: ctx, |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 221 | sdk: s, |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 222 | version: "current", |
| 223 | snapshotDir: snapshotDir.OutputPath, |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 224 | copies: make(map[string]string), |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 225 | filesToZip: []android.Path{bp.path}, |
| 226 | bpFile: bpFile, |
| 227 | prebuiltModules: make(map[string]*bpModule), |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 228 | } |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 229 | s.builderForTests = builder |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 230 | |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 231 | members, multilib := s.collectMembers(ctx) |
| 232 | for _, member := range members { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 233 | member.memberType.BuildSnapshot(ctx, builder, member) |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 234 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 235 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 236 | // Create a transformer that will transform an unversioned module into a versioned module. |
| 237 | unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder} |
| 238 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 239 | // Create a transformer that will transform an unversioned module by replacing any references |
| 240 | // to internal members with a unique module name and setting prefer: false. |
| 241 | unversionedTransformer := unversionedTransformation{builder: builder} |
| 242 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 243 | for _, unversioned := range builder.prebuiltOrder { |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame^] | 244 | // Prune any empty property sets. |
| 245 | unversioned = unversioned.transform(pruneEmptySetTransformer{}) |
| 246 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 247 | // 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] | 248 | versioned := unversioned.deepCopy() |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 249 | |
| 250 | // Transform the unversioned module into a versioned one. |
| 251 | versioned.transform(unversionedToVersionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 252 | bpFile.AddModule(versioned) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 253 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 254 | // Transform the unversioned module to make it suitable for use in the snapshot. |
| 255 | unversioned.transform(unversionedTransformer) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 256 | bpFile.AddModule(unversioned) |
| 257 | } |
| 258 | |
| 259 | // Create the snapshot module. |
| 260 | snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 261 | var snapshotModuleType string |
| 262 | if s.properties.Module_exports { |
| 263 | snapshotModuleType = "module_exports_snapshot" |
| 264 | } else { |
| 265 | snapshotModuleType = "sdk_snapshot" |
| 266 | } |
| 267 | snapshotModule := bpFile.newModule(snapshotModuleType) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 268 | snapshotModule.AddProperty("name", snapshotName) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 269 | |
| 270 | // Make sure that the snapshot has the same visibility as the sdk. |
| 271 | visibility := android.EffectiveVisibilityRules(ctx, s) |
| 272 | if len(visibility) != 0 { |
| 273 | snapshotModule.AddProperty("visibility", visibility) |
| 274 | } |
| 275 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 276 | addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule) |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 277 | |
| 278 | // Compile_multilib defaults to both and must always be set to both on the |
| 279 | // device and so only needs to be set when targeted at the host and is neither |
| 280 | // unspecified or both. |
| 281 | if s.HostSupported() && multilib != "" && multilib != "both" { |
| 282 | targetSet := snapshotModule.AddPropertySet("target") |
| 283 | hostSet := targetSet.AddPropertySet("host") |
| 284 | hostSet.AddProperty("compile_multilib", multilib) |
| 285 | } |
| 286 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 287 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 288 | names := memberListProperty.getter(s.dynamicMemberTypeListProperties) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 289 | if len(names) > 0 { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 290 | snapshotModule.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names)) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 291 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 292 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 293 | bpFile.AddModule(snapshotModule) |
| 294 | |
| 295 | // generate Android.bp |
| 296 | bp = newGeneratedFile(ctx, "snapshot", "Android.bp") |
| 297 | generateBpContents(&bp.generatedContents, bpFile) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 298 | |
| 299 | bp.build(pctx, ctx, nil) |
| 300 | |
| 301 | filesToZip := builder.filesToZip |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 302 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 303 | // zip them all |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 304 | outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 305 | outputDesc := "Building snapshot for " + ctx.ModuleName() |
| 306 | |
| 307 | // If there are no zips to merge then generate the output zip directly. |
| 308 | // Otherwise, generate an intermediate zip file into which other zips can be |
| 309 | // merged. |
| 310 | var zipFile android.OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 311 | var desc string |
| 312 | if len(builder.zipsToMerge) == 0 { |
| 313 | zipFile = outputZipFile |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 314 | desc = outputDesc |
| 315 | } else { |
| 316 | zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 317 | desc = "Building intermediate snapshot for " + ctx.ModuleName() |
| 318 | } |
| 319 | |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 320 | ctx.Build(pctx, android.BuildParams{ |
| 321 | Description: desc, |
| 322 | Rule: zipFiles, |
| 323 | Inputs: filesToZip, |
| 324 | Output: zipFile, |
| 325 | Args: map[string]string{ |
| 326 | "basedir": builder.snapshotDir.String(), |
| 327 | }, |
| 328 | }) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 329 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 330 | if len(builder.zipsToMerge) != 0 { |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 331 | ctx.Build(pctx, android.BuildParams{ |
| 332 | Description: outputDesc, |
| 333 | Rule: mergeZips, |
| 334 | Input: zipFile, |
| 335 | Inputs: builder.zipsToMerge, |
| 336 | Output: outputZipFile, |
| 337 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return outputZipFile |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 341 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 342 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 343 | type propertyTag struct { |
| 344 | name string |
| 345 | } |
| 346 | |
| 347 | var sdkMemberReferencePropertyTag = propertyTag{"sdkMemberReferencePropertyTag"} |
| 348 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 349 | type unversionedToVersionedTransformation struct { |
| 350 | identityTransformation |
| 351 | builder *snapshotBuilder |
| 352 | } |
| 353 | |
Paul Duffin | e6c0d84 | 2020-01-15 14:08:51 +0000 | [diff] [blame] | 354 | func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule { |
| 355 | // Use a versioned name for the module but remember the original name for the |
| 356 | // snapshot. |
| 357 | name := module.getValue("name").(string) |
| 358 | module.setProperty("name", t.builder.versionedSdkMemberName(name)) |
| 359 | module.insertAfter("name", "sdk_member_name", name) |
| 360 | return module |
| 361 | } |
| 362 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 363 | func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
| 364 | if tag == sdkMemberReferencePropertyTag { |
| 365 | return t.builder.versionedSdkMemberNames(value.([]string)), tag |
| 366 | } else { |
| 367 | return value, tag |
| 368 | } |
| 369 | } |
| 370 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 371 | type unversionedTransformation struct { |
| 372 | identityTransformation |
| 373 | builder *snapshotBuilder |
| 374 | } |
| 375 | |
| 376 | func (t unversionedTransformation) transformModule(module *bpModule) *bpModule { |
| 377 | // If the module is an internal member then use a unique name for it. |
| 378 | name := module.getValue("name").(string) |
| 379 | module.setProperty("name", t.builder.unversionedSdkMemberName(name)) |
| 380 | |
| 381 | // Set prefer: false - this is not strictly required as that is the default. |
| 382 | module.insertAfter("name", "prefer", false) |
| 383 | |
| 384 | return module |
| 385 | } |
| 386 | |
| 387 | func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
| 388 | if tag == sdkMemberReferencePropertyTag { |
| 389 | return t.builder.unversionedSdkMemberNames(value.([]string)), tag |
| 390 | } else { |
| 391 | return value, tag |
| 392 | } |
| 393 | } |
| 394 | |
Paul Duffin | a78f3a7 | 2020-02-21 16:29:35 +0000 | [diff] [blame^] | 395 | type pruneEmptySetTransformer struct { |
| 396 | identityTransformation |
| 397 | } |
| 398 | |
| 399 | var _ bpTransformer = (*pruneEmptySetTransformer)(nil) |
| 400 | |
| 401 | func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { |
| 402 | if len(propertySet.properties) == 0 { |
| 403 | return nil, nil |
| 404 | } else { |
| 405 | return propertySet, tag |
| 406 | } |
| 407 | } |
| 408 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 409 | func generateBpContents(contents *generatedContents, bpFile *bpFile) { |
| 410 | contents.Printfln("// This is auto-generated. DO NOT EDIT.") |
| 411 | for _, bpModule := range bpFile.order { |
| 412 | contents.Printfln("") |
| 413 | contents.Printfln("%s {", bpModule.moduleType) |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 414 | outputPropertySet(contents, bpModule.bpPropertySet) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 415 | contents.Printfln("}") |
| 416 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | func outputPropertySet(contents *generatedContents, set *bpPropertySet) { |
| 420 | contents.Indent() |
| 421 | for _, name := range set.order { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 422 | value := set.getValue(name) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 423 | |
| 424 | reflectedValue := reflect.ValueOf(value) |
| 425 | t := reflectedValue.Type() |
| 426 | |
| 427 | kind := t.Kind() |
| 428 | switch kind { |
| 429 | case reflect.Slice: |
| 430 | length := reflectedValue.Len() |
| 431 | if length > 1 { |
| 432 | contents.Printfln("%s: [", name) |
| 433 | contents.Indent() |
| 434 | for i := 0; i < length; i = i + 1 { |
| 435 | contents.Printfln("%q,", reflectedValue.Index(i).Interface()) |
| 436 | } |
| 437 | contents.Dedent() |
| 438 | contents.Printfln("],") |
| 439 | } else if length == 0 { |
| 440 | contents.Printfln("%s: [],", name) |
| 441 | } else { |
| 442 | contents.Printfln("%s: [%q],", name, reflectedValue.Index(0).Interface()) |
| 443 | } |
| 444 | case reflect.Bool: |
| 445 | contents.Printfln("%s: %t,", name, reflectedValue.Bool()) |
| 446 | |
| 447 | case reflect.Ptr: |
| 448 | contents.Printfln("%s: {", name) |
| 449 | outputPropertySet(contents, reflectedValue.Interface().(*bpPropertySet)) |
| 450 | contents.Printfln("},") |
| 451 | |
| 452 | default: |
| 453 | contents.Printfln("%s: %q,", name, value) |
| 454 | } |
| 455 | } |
| 456 | contents.Dedent() |
| 457 | } |
| 458 | |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 459 | func (s *sdk) GetAndroidBpContentsForTests() string { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 460 | contents := &generatedContents{} |
| 461 | generateBpContents(contents, s.builderForTests.bpFile) |
| 462 | return contents.content.String() |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 465 | type snapshotBuilder struct { |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 466 | ctx android.ModuleContext |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 467 | sdk *sdk |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 468 | version string |
| 469 | snapshotDir android.OutputPath |
| 470 | bpFile *bpFile |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 471 | |
| 472 | // Map from destination to source of each copy - used to eliminate duplicates and |
| 473 | // detect conflicts. |
| 474 | copies map[string]string |
| 475 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 476 | filesToZip android.Paths |
| 477 | zipsToMerge android.Paths |
| 478 | |
| 479 | prebuiltModules map[string]*bpModule |
| 480 | prebuiltOrder []*bpModule |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) { |
Paul Duffin | c62a510 | 2019-12-11 18:34:15 +0000 | [diff] [blame] | 484 | if existing, ok := s.copies[dest]; ok { |
| 485 | if existing != src.String() { |
| 486 | s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src) |
| 487 | return |
| 488 | } |
| 489 | } else { |
| 490 | path := s.snapshotDir.Join(s.ctx, dest) |
| 491 | s.ctx.Build(pctx, android.BuildParams{ |
| 492 | Rule: android.Cp, |
| 493 | Input: src, |
| 494 | Output: path, |
| 495 | }) |
| 496 | s.filesToZip = append(s.filesToZip, path) |
| 497 | |
| 498 | s.copies[dest] = src.String() |
| 499 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 502 | func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) { |
| 503 | ctx := s.ctx |
| 504 | |
| 505 | // Repackage the zip file so that the entries are in the destDir directory. |
| 506 | // This will allow the zip file to be merged into the snapshot. |
| 507 | tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 508 | |
| 509 | ctx.Build(pctx, android.BuildParams{ |
| 510 | Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(), |
| 511 | Rule: repackageZip, |
| 512 | Input: zipPath, |
| 513 | Output: tmpZipPath, |
| 514 | Args: map[string]string{ |
| 515 | "destdir": destDir, |
| 516 | }, |
| 517 | }) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 518 | |
| 519 | // Add the repackaged zip file to the files to merge. |
| 520 | s.zipsToMerge = append(s.zipsToMerge, tmpZipPath) |
| 521 | } |
| 522 | |
Paul Duffin | 9d8d609 | 2019-12-05 18:19:29 +0000 | [diff] [blame] | 523 | func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule { |
| 524 | name := member.Name() |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 525 | if s.prebuiltModules[name] != nil { |
| 526 | panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name)) |
| 527 | } |
| 528 | |
| 529 | m := s.bpFile.newModule(moduleType) |
| 530 | m.AddProperty("name", name) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 531 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 532 | if s.sdk.isInternalMember(name) { |
| 533 | // An internal member is only referenced from the sdk snapshot which is in the |
| 534 | // same package so can be marked as private. |
| 535 | m.AddProperty("visibility", []string{"//visibility:private"}) |
| 536 | } else { |
| 537 | // Extract visibility information from a member variant. All variants have the same |
| 538 | // visibility so it doesn't matter which one is used. |
| 539 | visibility := android.EffectiveVisibilityRules(s.ctx, member.Variants()[0]) |
| 540 | if len(visibility) != 0 { |
| 541 | m.AddProperty("visibility", visibility) |
| 542 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 545 | addHostDeviceSupportedProperties(&s.sdk.ModuleBase, m) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 546 | |
| 547 | s.prebuiltModules[name] = m |
| 548 | s.prebuiltOrder = append(s.prebuiltOrder, m) |
| 549 | return m |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Paul Duffin | e44358f | 2019-11-26 18:04:12 +0000 | [diff] [blame] | 552 | func addHostDeviceSupportedProperties(module *android.ModuleBase, bpModule *bpModule) { |
| 553 | if !module.DeviceSupported() { |
| 554 | bpModule.AddProperty("device_supported", false) |
| 555 | } |
| 556 | if module.HostSupported() { |
| 557 | bpModule.AddProperty("host_supported", true) |
| 558 | } |
| 559 | } |
| 560 | |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 561 | func (s *snapshotBuilder) SdkMemberReferencePropertyTag() android.BpPropertyTag { |
| 562 | return sdkMemberReferencePropertyTag |
| 563 | } |
| 564 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 565 | // Get a versioned name appropriate for the SDK snapshot version being taken. |
| 566 | func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string) string { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 567 | return versionedSdkMemberName(s.ctx, unversionedName, s.version) |
| 568 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 569 | |
| 570 | func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string { |
| 571 | var references []string = nil |
| 572 | for _, m := range members { |
| 573 | references = append(references, s.versionedSdkMemberName(m)) |
| 574 | } |
| 575 | return references |
| 576 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 577 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 578 | // Get an internal name unique to the sdk. |
| 579 | func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string) string { |
| 580 | if s.sdk.isInternalMember(unversionedName) { |
| 581 | return s.ctx.ModuleName() + "_" + unversionedName |
| 582 | } else { |
| 583 | return unversionedName |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | func (s *snapshotBuilder) unversionedSdkMemberNames(members []string) []string { |
| 588 | var references []string = nil |
| 589 | for _, m := range members { |
| 590 | references = append(references, s.unversionedSdkMemberName(m)) |
| 591 | } |
| 592 | return references |
| 593 | } |
| 594 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 595 | var _ android.SdkMember = (*sdkMember)(nil) |
| 596 | |
| 597 | type sdkMember struct { |
| 598 | memberType android.SdkMemberType |
| 599 | name string |
| 600 | variants []android.SdkAware |
| 601 | } |
| 602 | |
| 603 | func (m *sdkMember) Name() string { |
| 604 | return m.name |
| 605 | } |
| 606 | |
| 607 | func (m *sdkMember) Variants() []android.SdkAware { |
| 608 | return m.variants |
| 609 | } |