Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 1 | // Copyright (C) 2018 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 apex |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "path/filepath" |
| 21 | "runtime" |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 22 | "sort" |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 23 | "strings" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | "android/soong/cc" |
| 27 | "android/soong/java" |
| 28 | |
| 29 | "github.com/google/blueprint" |
| 30 | "github.com/google/blueprint/proptools" |
| 31 | ) |
| 32 | |
| 33 | var ( |
| 34 | pctx = android.NewPackageContext("android/apex") |
| 35 | |
| 36 | // Create a canned fs config file where all files and directories are |
| 37 | // by default set to (uid/gid/mode) = (1000/1000/0644) |
| 38 | // TODO(b/113082813) make this configurable using config.fs syntax |
| 39 | generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{ |
Roland Levillain | 2b11f74 | 2018-11-02 11:50:42 +0000 | [diff] [blame^] | 40 | Command: `echo '/ 1000 1000 0755' > ${out} && ` + |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 41 | `echo '/manifest.json 1000 1000 0644' >> ${out} && ` + |
Jiyong Park | 92905d6 | 2018-10-11 13:23:09 +0900 | [diff] [blame] | 42 | `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` + |
| 43 | `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0755"}' >> ${out}`, |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 44 | Description: "fs_config ${out}", |
Jiyong Park | 92905d6 | 2018-10-11 13:23:09 +0900 | [diff] [blame] | 45 | }, "ro_paths", "exec_paths") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 46 | |
| 47 | // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate |
| 48 | // against the binary policy using sefcontext_compiler -p <policy>. |
| 49 | |
| 50 | // TODO(b/114327326): automate the generation of file_contexts |
| 51 | apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{ |
| 52 | Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` + |
| 53 | `(${copy_commands}) && ` + |
| 54 | `APEXER_TOOL_PATH=${tool_path} ` + |
| 55 | `${apexer} --verbose --force --manifest ${manifest} ` + |
| 56 | `--file_contexts ${file_contexts} ` + |
| 57 | `--canned_fs_config ${canned_fs_config} ` + |
| 58 | `--key ${key} ${image_dir} ${out} `, |
| 59 | CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}", |
| 60 | "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", |
| 61 | "${soong_zip}", "${zipalign}", "${aapt2}"}, |
| 62 | Description: "APEX ${image_dir} => ${out}", |
| 63 | }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key") |
| 64 | ) |
| 65 | |
| 66 | var apexSuffix = ".apex" |
| 67 | |
| 68 | type dependencyTag struct { |
| 69 | blueprint.BaseDependencyTag |
| 70 | name string |
| 71 | } |
| 72 | |
| 73 | var ( |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 74 | sharedLibTag = dependencyTag{name: "sharedLib"} |
| 75 | executableTag = dependencyTag{name: "executable"} |
| 76 | javaLibTag = dependencyTag{name: "javaLib"} |
| 77 | prebuiltTag = dependencyTag{name: "prebuilt"} |
| 78 | keyTag = dependencyTag{name: "key"} |
| 79 | certificateTag = dependencyTag{name: "certificate"} |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 80 | ) |
| 81 | |
| 82 | func init() { |
| 83 | pctx.Import("android/soong/common") |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 84 | pctx.Import("android/soong/java") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 85 | pctx.HostBinToolVariable("apexer", "apexer") |
Roland Levillain | 54bdfda | 2018-10-05 19:34:32 +0100 | [diff] [blame] | 86 | // ART minimal builds (using the master-art manifest) do not have the "frameworks/base" |
| 87 | // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead. |
| 88 | hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) { |
| 89 | pctx.VariableFunc(name, func(ctx android.PackageVarContext) string { |
| 90 | if !android.ExistentPathForSource(ctx, "frameworks/base").Valid() { |
| 91 | return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool) |
| 92 | } else { |
| 93 | return pctx.HostBinToolPath(ctx, tool).String() |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 98 | pctx.HostBinToolVariable("avbtool", "avbtool") |
| 99 | pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid") |
| 100 | pctx.HostBinToolVariable("merge_zips", "merge_zips") |
| 101 | pctx.HostBinToolVariable("mke2fs", "mke2fs") |
| 102 | pctx.HostBinToolVariable("resize2fs", "resize2fs") |
| 103 | pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile") |
| 104 | pctx.HostBinToolVariable("soong_zip", "soong_zip") |
| 105 | pctx.HostBinToolVariable("zipalign", "zipalign") |
| 106 | |
| 107 | android.RegisterModuleType("apex", apexBundleFactory) |
| 108 | |
| 109 | android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 110 | ctx.TopDown("apex_deps", apexDepsMutator) |
| 111 | ctx.BottomUp("apex", apexMutator) |
| 112 | }) |
| 113 | } |
| 114 | |
| 115 | // maps a module name to set of apex bundle names that the module should be built for |
| 116 | func apexBundleNamesFor(config android.Config) map[string]map[string]bool { |
| 117 | return config.Once("apexBundleNames", func() interface{} { |
| 118 | return make(map[string]map[string]bool) |
| 119 | }).(map[string]map[string]bool) |
| 120 | } |
| 121 | |
| 122 | // Mark the direct and transitive dependencies of apex bundles so that they |
| 123 | // can be built for the apex bundles. |
| 124 | func apexDepsMutator(mctx android.TopDownMutatorContext) { |
| 125 | if _, ok := mctx.Module().(*apexBundle); ok { |
| 126 | apexBundleName := mctx.Module().Name() |
| 127 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 128 | if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() { |
Jiyong Park | 678529e | 2018-10-23 23:58:01 +0900 | [diff] [blame] | 129 | moduleName := am.Name() + "-" + am.Target().String() |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 130 | bundleNames, ok := apexBundleNamesFor(mctx.Config())[moduleName] |
| 131 | if !ok { |
| 132 | bundleNames = make(map[string]bool) |
| 133 | apexBundleNamesFor(mctx.Config())[moduleName] = bundleNames |
| 134 | } |
| 135 | bundleNames[apexBundleName] = true |
| 136 | return true |
| 137 | } else { |
| 138 | return false |
| 139 | } |
| 140 | }) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Create apex variations if a module is included in APEX(s). |
| 145 | func apexMutator(mctx android.BottomUpMutatorContext) { |
| 146 | if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() { |
Jiyong Park | 678529e | 2018-10-23 23:58:01 +0900 | [diff] [blame] | 147 | moduleName := am.Name() + "-" + am.Target().String() |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 148 | if bundleNames, ok := apexBundleNamesFor(mctx.Config())[moduleName]; ok { |
| 149 | variations := []string{"platform"} |
| 150 | for bn := range bundleNames { |
| 151 | variations = append(variations, bn) |
| 152 | } |
| 153 | modules := mctx.CreateVariations(variations...) |
| 154 | for i, m := range modules { |
| 155 | if i == 0 { |
| 156 | continue // platform |
| 157 | } |
| 158 | m.(android.ApexModule).BuildForApex(variations[i]) |
| 159 | } |
| 160 | } |
| 161 | } else if _, ok := mctx.Module().(*apexBundle); ok { |
| 162 | // apex bundle itself is mutated so that it and its modules have same |
| 163 | // apex variant. |
| 164 | apexBundleName := mctx.ModuleName() |
| 165 | mctx.CreateVariations(apexBundleName) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | type apexBundleProperties struct { |
| 170 | // Json manifest file describing meta info of this APEX bundle. Default: |
| 171 | // "manifest.json" |
| 172 | Manifest *string |
| 173 | |
| 174 | // File contexts file for setting security context to each file in this APEX bundle |
| 175 | // Default: "file_contexts". |
| 176 | File_contexts *string |
| 177 | |
| 178 | // List of native shared libs that are embedded inside this APEX bundle |
| 179 | Native_shared_libs []string |
| 180 | |
| 181 | // List of native executables that are embedded inside this APEX bundle |
| 182 | Binaries []string |
| 183 | |
| 184 | // List of java libraries that are embedded inside this APEX bundle |
| 185 | Java_libs []string |
| 186 | |
| 187 | // List of prebuilt files that are embedded inside this APEX bundle |
| 188 | Prebuilts []string |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 189 | |
| 190 | // Name of the apex_key module that provides the private key to sign APEX |
| 191 | Key *string |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 192 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 193 | // The name of a certificate in the default certificate directory, blank to use the default product certificate, |
| 194 | // or an android_app_certificate module name in the form ":module". |
| 195 | Certificate *string |
| 196 | |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 197 | Multilib struct { |
| 198 | First struct { |
| 199 | // List of native libraries whose compile_multilib is "first" |
| 200 | Native_shared_libs []string |
| 201 | // List of native executables whose compile_multilib is "first" |
| 202 | Binaries []string |
| 203 | } |
| 204 | Both struct { |
| 205 | // List of native libraries whose compile_multilib is "both" |
| 206 | Native_shared_libs []string |
| 207 | // List of native executables whose compile_multilib is "both" |
| 208 | Binaries []string |
| 209 | } |
| 210 | Prefer32 struct { |
| 211 | // List of native libraries whose compile_multilib is "prefer32" |
| 212 | Native_shared_libs []string |
| 213 | // List of native executables whose compile_multilib is "prefer32" |
| 214 | Binaries []string |
| 215 | } |
| 216 | Lib32 struct { |
| 217 | // List of native libraries whose compile_multilib is "32" |
| 218 | Native_shared_libs []string |
| 219 | // List of native executables whose compile_multilib is "32" |
| 220 | Binaries []string |
| 221 | } |
| 222 | Lib64 struct { |
| 223 | // List of native libraries whose compile_multilib is "64" |
| 224 | Native_shared_libs []string |
| 225 | // List of native executables whose compile_multilib is "64" |
| 226 | Binaries []string |
| 227 | } |
| 228 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | type apexBundle struct { |
| 232 | android.ModuleBase |
| 233 | android.DefaultableModuleBase |
| 234 | |
| 235 | properties apexBundleProperties |
| 236 | |
| 237 | outputFile android.WritablePath |
| 238 | installDir android.OutputPath |
| 239 | } |
| 240 | |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 241 | func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, |
| 242 | native_shared_libs []string, binaries []string, arch string) { |
| 243 | // Use *FarVariation* to be able to depend on modules having |
| 244 | // conflicting variations with this module. This is required since |
| 245 | // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64' |
| 246 | // for native shared libs. |
| 247 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 248 | {Mutator: "arch", Variation: arch}, |
| 249 | {Mutator: "image", Variation: "core"}, |
| 250 | {Mutator: "link", Variation: "shared"}, |
| 251 | }, sharedLibTag, native_shared_libs...) |
| 252 | |
| 253 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 254 | {Mutator: "arch", Variation: arch}, |
| 255 | {Mutator: "image", Variation: "core"}, |
| 256 | }, executableTag, binaries...) |
| 257 | } |
| 258 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 259 | func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 260 | targets := ctx.MultiTargets() |
| 261 | has32BitTarget := false |
| 262 | for _, target := range targets { |
| 263 | if target.Arch.ArchType.Multilib == "lib32" { |
| 264 | has32BitTarget = true |
| 265 | } |
| 266 | } |
| 267 | for i, target := range targets { |
| 268 | // When multilib.* is omitted for native_shared_libs, it implies |
| 269 | // multilib.both. |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 270 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 271 | {Mutator: "arch", Variation: target.String()}, |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 272 | {Mutator: "image", Variation: "core"}, |
| 273 | {Mutator: "link", Variation: "shared"}, |
| 274 | }, sharedLibTag, a.properties.Native_shared_libs...) |
| 275 | |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 276 | // Add native modules targetting both ABIs |
| 277 | addDependenciesForNativeModules(ctx, |
| 278 | a.properties.Multilib.Both.Native_shared_libs, |
| 279 | a.properties.Multilib.Both.Binaries, target.String()) |
| 280 | |
| 281 | if i == 0 { |
| 282 | // When multilib.* is omitted for binaries, it implies |
| 283 | // multilib.first. |
| 284 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 285 | {Mutator: "arch", Variation: target.String()}, |
| 286 | {Mutator: "image", Variation: "core"}, |
| 287 | }, executableTag, a.properties.Binaries...) |
| 288 | |
| 289 | // Add native modules targetting the first ABI |
| 290 | addDependenciesForNativeModules(ctx, |
| 291 | a.properties.Multilib.First.Native_shared_libs, |
| 292 | a.properties.Multilib.First.Binaries, target.String()) |
| 293 | } |
| 294 | |
| 295 | switch target.Arch.ArchType.Multilib { |
| 296 | case "lib32": |
| 297 | // Add native modules targetting 32-bit ABI |
| 298 | addDependenciesForNativeModules(ctx, |
| 299 | a.properties.Multilib.Lib32.Native_shared_libs, |
| 300 | a.properties.Multilib.Lib32.Binaries, target.String()) |
| 301 | |
| 302 | addDependenciesForNativeModules(ctx, |
| 303 | a.properties.Multilib.Prefer32.Native_shared_libs, |
| 304 | a.properties.Multilib.Prefer32.Binaries, target.String()) |
| 305 | case "lib64": |
| 306 | // Add native modules targetting 64-bit ABI |
| 307 | addDependenciesForNativeModules(ctx, |
| 308 | a.properties.Multilib.Lib64.Native_shared_libs, |
| 309 | a.properties.Multilib.Lib64.Binaries, target.String()) |
| 310 | |
| 311 | if !has32BitTarget { |
| 312 | addDependenciesForNativeModules(ctx, |
| 313 | a.properties.Multilib.Prefer32.Native_shared_libs, |
| 314 | a.properties.Multilib.Prefer32.Binaries, target.String()) |
| 315 | } |
| 316 | } |
| 317 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 318 | } |
| 319 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 320 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 321 | {Mutator: "arch", Variation: "android_common"}, |
| 322 | }, javaLibTag, a.properties.Java_libs...) |
| 323 | |
| 324 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 325 | {Mutator: "arch", Variation: "android_common"}, |
| 326 | }, prebuiltTag, a.properties.Prebuilts...) |
| 327 | |
| 328 | if String(a.properties.Key) == "" { |
| 329 | ctx.ModuleErrorf("key is missing") |
| 330 | return |
| 331 | } |
| 332 | ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key)) |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 333 | |
| 334 | cert := android.SrcIsModule(String(a.properties.Certificate)) |
| 335 | if cert != "" { |
| 336 | ctx.AddDependency(ctx.Module(), certificateTag, cert) |
| 337 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | func getCopyManifestForNativeLibrary(cc *cc.Module) (fileToCopy android.Path, dirInApex string) { |
| 341 | // Decide the APEX-local directory by the multilib of the library |
| 342 | // In the future, we may query this to the module. |
| 343 | switch cc.Arch().ArchType.Multilib { |
| 344 | case "lib32": |
| 345 | dirInApex = "lib" |
| 346 | case "lib64": |
| 347 | dirInApex = "lib64" |
| 348 | } |
| 349 | if !cc.Arch().Native { |
| 350 | dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String()) |
| 351 | } |
| 352 | |
| 353 | fileToCopy = cc.OutputFile().Path() |
| 354 | return |
| 355 | } |
| 356 | |
| 357 | func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) { |
| 358 | dirInApex = "bin" |
| 359 | fileToCopy = cc.OutputFile().Path() |
| 360 | return |
| 361 | } |
| 362 | |
| 363 | func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) { |
| 364 | dirInApex = "javalib" |
| 365 | fileToCopy = java.Srcs()[0] |
| 366 | return |
| 367 | } |
| 368 | |
| 369 | func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) { |
| 370 | dirInApex = filepath.Join("etc", prebuilt.SubDir()) |
| 371 | fileToCopy = prebuilt.OutputFile() |
| 372 | return |
| 373 | } |
| 374 | |
| 375 | func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 376 | // files to copy -> dir in apex |
| 377 | copyManifest := make(map[android.Path]string) |
| 378 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 379 | var keyFile android.Path |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 380 | var certificate java.Certificate |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 381 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 382 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 383 | if _, ok := parent.(*apexBundle); ok { |
| 384 | // direct dependencies |
| 385 | depTag := ctx.OtherModuleDependencyTag(child) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 386 | depName := ctx.OtherModuleName(child) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 387 | switch depTag { |
| 388 | case sharedLibTag: |
| 389 | if cc, ok := child.(*cc.Module); ok { |
| 390 | fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc) |
| 391 | copyManifest[fileToCopy] = dirInApex |
| 392 | return true |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 393 | } else { |
| 394 | ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 395 | } |
| 396 | case executableTag: |
| 397 | if cc, ok := child.(*cc.Module); ok { |
| 398 | fileToCopy, dirInApex := getCopyManifestForExecutable(cc) |
| 399 | copyManifest[fileToCopy] = dirInApex |
| 400 | return true |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 401 | } else { |
| 402 | ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 403 | } |
| 404 | case javaLibTag: |
| 405 | if java, ok := child.(*java.Library); ok { |
| 406 | fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java) |
| 407 | copyManifest[fileToCopy] = dirInApex |
| 408 | return true |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 409 | } else { |
| 410 | ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 411 | } |
| 412 | case prebuiltTag: |
| 413 | if prebuilt, ok := child.(*android.PrebuiltEtc); ok { |
| 414 | fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt) |
| 415 | copyManifest[fileToCopy] = dirInApex |
| 416 | return true |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 417 | } else { |
| 418 | ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName) |
| 419 | } |
| 420 | case keyTag: |
| 421 | if key, ok := child.(*apexKey); ok { |
| 422 | keyFile = key.private_key_file |
| 423 | return false |
| 424 | } else { |
| 425 | ctx.PropertyErrorf("key", "%q is not an apex_key module", depName) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 426 | } |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 427 | case certificateTag: |
| 428 | if dep, ok := child.(*java.AndroidAppCertificate); ok { |
| 429 | certificate = dep.Certificate |
| 430 | return false |
| 431 | } else { |
| 432 | ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName) |
| 433 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 434 | } |
| 435 | } else { |
| 436 | // indirect dependencies |
| 437 | if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() { |
| 438 | if cc, ok := child.(*cc.Module); ok { |
| 439 | fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc) |
| 440 | copyManifest[fileToCopy] = dirInApex |
| 441 | return true |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | return false |
| 446 | }) |
| 447 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 448 | cert := String(a.properties.Certificate) |
| 449 | if cert != "" && android.SrcIsModule(cert) == "" { |
| 450 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) |
| 451 | certificate = java.Certificate{ |
| 452 | defaultDir.Join(ctx, cert+".x509.pem"), |
| 453 | defaultDir.Join(ctx, cert+".pk8"), |
| 454 | } |
| 455 | } else if cert == "" { |
| 456 | pem, key := ctx.Config().DefaultAppCertificate(ctx) |
| 457 | certificate = java.Certificate{pem, key} |
| 458 | } |
| 459 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 460 | // files and dirs that will be created in apex |
Jiyong Park | 92905d6 | 2018-10-11 13:23:09 +0900 | [diff] [blame] | 461 | var readOnlyPaths []string |
| 462 | var executablePaths []string // this also includes dirs |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 463 | for fileToCopy, dirInApex := range copyManifest { |
| 464 | pathInApex := filepath.Join(dirInApex, fileToCopy.Base()) |
Jiyong Park | 92905d6 | 2018-10-11 13:23:09 +0900 | [diff] [blame] | 465 | if dirInApex == "bin" { |
| 466 | executablePaths = append(executablePaths, pathInApex) |
| 467 | } else { |
| 468 | readOnlyPaths = append(readOnlyPaths, pathInApex) |
| 469 | } |
| 470 | if !android.InList(dirInApex, executablePaths) { |
| 471 | executablePaths = append(executablePaths, dirInApex) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 472 | } |
| 473 | } |
Jiyong Park | 92905d6 | 2018-10-11 13:23:09 +0900 | [diff] [blame] | 474 | sort.Strings(readOnlyPaths) |
| 475 | sort.Strings(executablePaths) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 476 | cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config") |
| 477 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 478 | Rule: generateFsConfig, |
| 479 | Output: cannedFsConfig, |
| 480 | Args: map[string]string{ |
Jiyong Park | 92905d6 | 2018-10-11 13:23:09 +0900 | [diff] [blame] | 481 | "ro_paths": strings.Join(readOnlyPaths, " "), |
| 482 | "exec_paths": strings.Join(executablePaths, " "), |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 483 | }, |
| 484 | }) |
| 485 | |
| 486 | manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "manifest.json")) |
| 487 | fileContexts := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.File_contexts, "file_contexts")) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 488 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 489 | unsignedOutputFile := android.PathForModuleOut(ctx, a.ModuleBase.Name()+apexSuffix+".unsigned") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 490 | |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 491 | filesToCopy := []android.Path{} |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 492 | for file := range copyManifest { |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 493 | filesToCopy = append(filesToCopy, file) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 494 | } |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 495 | sort.Slice(filesToCopy, func(i, j int) bool { |
| 496 | return filesToCopy[i].String() < filesToCopy[j].String() |
| 497 | }) |
| 498 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 499 | copyCommands := []string{} |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 500 | for _, src := range filesToCopy { |
| 501 | dest := filepath.Join(copyManifest[src], src.Base()) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 502 | dest_path := filepath.Join(android.PathForModuleOut(ctx, "image").String(), dest) |
| 503 | copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path)) |
| 504 | copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path) |
| 505 | } |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 506 | implicitInputs := append(android.Paths(nil), filesToCopy...) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 507 | implicitInputs = append(implicitInputs, cannedFsConfig, manifest, fileContexts, keyFile) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 508 | outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String() |
| 509 | prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin") |
| 510 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 511 | Rule: apexRule, |
| 512 | Implicits: implicitInputs, |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 513 | Output: unsignedOutputFile, |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 514 | Args: map[string]string{ |
| 515 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
| 516 | "image_dir": android.PathForModuleOut(ctx, "image").String(), |
| 517 | "copy_commands": strings.Join(copyCommands, " && "), |
| 518 | "manifest": manifest.String(), |
| 519 | "file_contexts": fileContexts.String(), |
| 520 | "canned_fs_config": cannedFsConfig.String(), |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 521 | "key": keyFile.String(), |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 522 | }, |
| 523 | }) |
| 524 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 525 | a.outputFile = android.PathForModuleOut(ctx, a.ModuleBase.Name()+apexSuffix) |
| 526 | ctx.Build(pctx, android.BuildParams{ |
| 527 | Rule: java.Signapk, |
| 528 | Description: "signapk", |
| 529 | Output: a.outputFile, |
| 530 | Input: unsignedOutputFile, |
| 531 | Args: map[string]string{ |
| 532 | "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "), |
| 533 | }, |
| 534 | }) |
| 535 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 536 | a.installDir = android.PathForModuleInstall(ctx, "apex") |
| 537 | } |
| 538 | |
| 539 | func (a *apexBundle) AndroidMk() android.AndroidMkData { |
| 540 | return android.AndroidMkData{ |
| 541 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 542 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 543 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 544 | fmt.Fprintln(w, "LOCAL_MODULE :=", name) |
| 545 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class? |
| 546 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String()) |
| 547 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString())) |
| 548 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", name+apexSuffix) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 549 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key)) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 550 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 551 | }} |
| 552 | } |
| 553 | |
| 554 | func apexBundleFactory() android.Module { |
| 555 | module := &apexBundle{} |
| 556 | module.AddProperties(&module.properties) |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 557 | module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, |
| 558 | class android.OsClass) bool { |
| 559 | return class == android.Device && ctx.Config().DevicePrefer32BitExecutables() |
| 560 | }) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 561 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 562 | android.InitDefaultableModule(module) |
| 563 | return module |
| 564 | } |