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} && ` + |
Dario Freni | 4abb1dc | 2018-11-20 18:04:58 +0000 | [diff] [blame] | 41 | `echo '/apex_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} ` + |
Jiyong Park | 2556015 | 2018-11-20 09:57:52 +0900 | [diff] [blame] | 55 | `${apexer} --force --manifest ${manifest} ` + |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 56 | `--file_contexts ${file_contexts} ` + |
| 57 | `--canned_fs_config ${canned_fs_config} ` + |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 58 | `--payload_type image ` + |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 59 | `--key ${key} ${image_dir} ${out} `, |
| 60 | CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}", |
| 61 | "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", |
| 62 | "${soong_zip}", "${zipalign}", "${aapt2}"}, |
| 63 | Description: "APEX ${image_dir} => ${out}", |
| 64 | }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key") |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 65 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 66 | zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{ |
| 67 | Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` + |
| 68 | `(${copy_commands}) && ` + |
| 69 | `APEXER_TOOL_PATH=${tool_path} ` + |
| 70 | `${apexer} --force --manifest ${manifest} ` + |
| 71 | `--payload_type zip ` + |
| 72 | `${image_dir} ${out} `, |
| 73 | CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"}, |
| 74 | Description: "ZipAPEX ${image_dir} => ${out}", |
| 75 | }, "tool_path", "image_dir", "copy_commands", "manifest") |
| 76 | |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 77 | apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule", |
| 78 | blueprint.RuleParams{ |
| 79 | Command: `${aapt2} convert --output-format proto $in -o $out`, |
| 80 | CommandDeps: []string{"${aapt2}"}, |
| 81 | }) |
| 82 | |
| 83 | apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{ |
Jiyong Park | 1ed0fc5 | 2018-11-23 13:22:21 +0900 | [diff] [blame] | 84 | Command: `${zip2zip} -i $in -o $out ` + |
Dario Freni | 4abb1dc | 2018-11-20 18:04:58 +0000 | [diff] [blame] | 85 | `apex_payload.img:apex/${abi}.img ` + |
| 86 | `apex_manifest.json:root/apex_manifest.json ` + |
Shahar Amitai | 328b077 | 2018-11-26 14:12:02 +0000 | [diff] [blame] | 87 | `AndroidManifest.xml:manifest/AndroidManifest.xml`, |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 88 | CommandDeps: []string{"${zip2zip}"}, |
| 89 | Description: "app bundle", |
| 90 | }, "abi") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 91 | ) |
| 92 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 93 | var imageApexSuffix = ".apex" |
| 94 | var zipApexSuffix = ".zipapex" |
| 95 | |
| 96 | var imageApexType = "image" |
| 97 | var zipApexType = "zip" |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 98 | |
| 99 | type dependencyTag struct { |
| 100 | blueprint.BaseDependencyTag |
| 101 | name string |
| 102 | } |
| 103 | |
| 104 | var ( |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 105 | sharedLibTag = dependencyTag{name: "sharedLib"} |
| 106 | executableTag = dependencyTag{name: "executable"} |
| 107 | javaLibTag = dependencyTag{name: "javaLib"} |
| 108 | prebuiltTag = dependencyTag{name: "prebuilt"} |
| 109 | keyTag = dependencyTag{name: "key"} |
| 110 | certificateTag = dependencyTag{name: "certificate"} |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 111 | ) |
| 112 | |
| 113 | func init() { |
| 114 | pctx.Import("android/soong/common") |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 115 | pctx.Import("android/soong/java") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 116 | pctx.HostBinToolVariable("apexer", "apexer") |
Roland Levillain | 54bdfda | 2018-10-05 19:34:32 +0100 | [diff] [blame] | 117 | // ART minimal builds (using the master-art manifest) do not have the "frameworks/base" |
| 118 | // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead. |
| 119 | hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) { |
| 120 | pctx.VariableFunc(name, func(ctx android.PackageVarContext) string { |
| 121 | if !android.ExistentPathForSource(ctx, "frameworks/base").Valid() { |
| 122 | return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool) |
| 123 | } else { |
| 124 | return pctx.HostBinToolPath(ctx, tool).String() |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 129 | pctx.HostBinToolVariable("avbtool", "avbtool") |
| 130 | pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid") |
| 131 | pctx.HostBinToolVariable("merge_zips", "merge_zips") |
| 132 | pctx.HostBinToolVariable("mke2fs", "mke2fs") |
| 133 | pctx.HostBinToolVariable("resize2fs", "resize2fs") |
| 134 | pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile") |
| 135 | pctx.HostBinToolVariable("soong_zip", "soong_zip") |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 136 | pctx.HostBinToolVariable("zip2zip", "zip2zip") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 137 | pctx.HostBinToolVariable("zipalign", "zipalign") |
| 138 | |
Alex Light | ee25072 | 2018-12-06 14:00:02 -0800 | [diff] [blame] | 139 | android.RegisterModuleType("apex", ApexBundleFactory) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 140 | |
| 141 | android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 142 | ctx.TopDown("apex_deps", apexDepsMutator) |
| 143 | ctx.BottomUp("apex", apexMutator) |
| 144 | }) |
| 145 | } |
| 146 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 147 | // Mark the direct and transitive dependencies of apex bundles so that they |
| 148 | // can be built for the apex bundles. |
| 149 | func apexDepsMutator(mctx android.TopDownMutatorContext) { |
| 150 | if _, ok := mctx.Module().(*apexBundle); ok { |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 151 | apexBundleName := mctx.ModuleName() |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 152 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 153 | if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() { |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 154 | moduleName := mctx.OtherModuleName(am) + "-" + am.Target().String() |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 155 | // If the parent is apexBundle, this child is directly depended. |
| 156 | _, directDep := parent.(*apexBundle) |
| 157 | android.BuildModuleForApexBundle(mctx, moduleName, apexBundleName, directDep) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 158 | return true |
| 159 | } else { |
| 160 | return false |
| 161 | } |
| 162 | }) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Create apex variations if a module is included in APEX(s). |
| 167 | func apexMutator(mctx android.BottomUpMutatorContext) { |
| 168 | if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() { |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 169 | moduleName := mctx.ModuleName() + "-" + am.Target().String() |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 170 | bundleNames := android.GetApexBundlesForModule(mctx, moduleName) |
| 171 | if len(bundleNames) > 0 { |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 172 | variations := []string{"platform"} |
| 173 | for bn := range bundleNames { |
| 174 | variations = append(variations, bn) |
| 175 | } |
| 176 | modules := mctx.CreateVariations(variations...) |
| 177 | for i, m := range modules { |
| 178 | if i == 0 { |
| 179 | continue // platform |
| 180 | } |
| 181 | m.(android.ApexModule).BuildForApex(variations[i]) |
| 182 | } |
| 183 | } |
| 184 | } else if _, ok := mctx.Module().(*apexBundle); ok { |
| 185 | // apex bundle itself is mutated so that it and its modules have same |
| 186 | // apex variant. |
| 187 | apexBundleName := mctx.ModuleName() |
| 188 | mctx.CreateVariations(apexBundleName) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | type apexBundleProperties struct { |
| 193 | // Json manifest file describing meta info of this APEX bundle. Default: |
Dario Freni | 4abb1dc | 2018-11-20 18:04:58 +0000 | [diff] [blame] | 194 | // "apex_manifest.json" |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 195 | Manifest *string |
| 196 | |
Jiyong Park | d0a65ba | 2018-11-10 06:37:15 +0900 | [diff] [blame] | 197 | // Determines the file contexts file for setting security context to each file in this APEX bundle. |
| 198 | // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is |
| 199 | // used. |
| 200 | // Default: <name_of_this_module> |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 201 | File_contexts *string |
| 202 | |
| 203 | // List of native shared libs that are embedded inside this APEX bundle |
| 204 | Native_shared_libs []string |
| 205 | |
| 206 | // List of native executables that are embedded inside this APEX bundle |
| 207 | Binaries []string |
| 208 | |
| 209 | // List of java libraries that are embedded inside this APEX bundle |
| 210 | Java_libs []string |
| 211 | |
| 212 | // List of prebuilt files that are embedded inside this APEX bundle |
| 213 | Prebuilts []string |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 214 | |
| 215 | // Name of the apex_key module that provides the private key to sign APEX |
| 216 | Key *string |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 217 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 218 | // The type of APEX to build. Controls what the APEX payload is. Either |
| 219 | // 'image', 'zip' or 'both'. Default: 'image'. |
| 220 | Payload_type *string |
| 221 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 222 | // The name of a certificate in the default certificate directory, blank to use the default product certificate, |
| 223 | // or an android_app_certificate module name in the form ":module". |
| 224 | Certificate *string |
| 225 | |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 226 | Multilib struct { |
| 227 | First struct { |
| 228 | // List of native libraries whose compile_multilib is "first" |
| 229 | Native_shared_libs []string |
| 230 | // List of native executables whose compile_multilib is "first" |
| 231 | Binaries []string |
| 232 | } |
| 233 | Both struct { |
| 234 | // List of native libraries whose compile_multilib is "both" |
| 235 | Native_shared_libs []string |
| 236 | // List of native executables whose compile_multilib is "both" |
| 237 | Binaries []string |
| 238 | } |
| 239 | Prefer32 struct { |
| 240 | // List of native libraries whose compile_multilib is "prefer32" |
| 241 | Native_shared_libs []string |
| 242 | // List of native executables whose compile_multilib is "prefer32" |
| 243 | Binaries []string |
| 244 | } |
| 245 | Lib32 struct { |
| 246 | // List of native libraries whose compile_multilib is "32" |
| 247 | Native_shared_libs []string |
| 248 | // List of native executables whose compile_multilib is "32" |
| 249 | Binaries []string |
| 250 | } |
| 251 | Lib64 struct { |
| 252 | // List of native libraries whose compile_multilib is "64" |
| 253 | Native_shared_libs []string |
| 254 | // List of native executables whose compile_multilib is "64" |
| 255 | Binaries []string |
| 256 | } |
| 257 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 258 | } |
| 259 | |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 260 | type apexFileClass int |
| 261 | |
| 262 | const ( |
| 263 | etc apexFileClass = iota |
| 264 | nativeSharedLib |
| 265 | nativeExecutable |
| 266 | javaSharedLib |
| 267 | ) |
| 268 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 269 | type apexPackaging int |
| 270 | |
| 271 | const ( |
| 272 | imageApex apexPackaging = iota |
| 273 | zipApex |
| 274 | both |
| 275 | ) |
| 276 | |
| 277 | func (a apexPackaging) image() bool { |
| 278 | switch a { |
| 279 | case imageApex, both: |
| 280 | return true |
| 281 | } |
| 282 | return false |
| 283 | } |
| 284 | |
| 285 | func (a apexPackaging) zip() bool { |
| 286 | switch a { |
| 287 | case zipApex, both: |
| 288 | return true |
| 289 | } |
| 290 | return false |
| 291 | } |
| 292 | |
| 293 | func (a apexPackaging) suffix() string { |
| 294 | switch a { |
| 295 | case imageApex: |
| 296 | return imageApexSuffix |
| 297 | case zipApex: |
| 298 | return zipApexSuffix |
| 299 | case both: |
| 300 | panic(fmt.Errorf("must be either zip or image")) |
| 301 | default: |
| 302 | panic(fmt.Errorf("unkonwn APEX type %d", a)) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | func (a apexPackaging) name() string { |
| 307 | switch a { |
| 308 | case imageApex: |
| 309 | return imageApexType |
| 310 | case zipApex: |
| 311 | return zipApexType |
| 312 | case both: |
| 313 | panic(fmt.Errorf("must be either zip or image")) |
| 314 | default: |
| 315 | panic(fmt.Errorf("unkonwn APEX type %d", a)) |
| 316 | } |
| 317 | } |
| 318 | |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 319 | func (class apexFileClass) NameInMake() string { |
| 320 | switch class { |
| 321 | case etc: |
| 322 | return "ETC" |
| 323 | case nativeSharedLib: |
| 324 | return "SHARED_LIBRARIES" |
| 325 | case nativeExecutable: |
| 326 | return "EXECUTABLES" |
| 327 | case javaSharedLib: |
| 328 | return "JAVA_LIBRARIES" |
| 329 | default: |
| 330 | panic(fmt.Errorf("unkonwn class %d", class)) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | type apexFile struct { |
| 335 | builtFile android.Path |
| 336 | moduleName string |
| 337 | archType android.ArchType |
| 338 | installDir string |
| 339 | class apexFileClass |
| 340 | } |
| 341 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 342 | type apexBundle struct { |
| 343 | android.ModuleBase |
| 344 | android.DefaultableModuleBase |
| 345 | |
| 346 | properties apexBundleProperties |
| 347 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 348 | apexTypes apexPackaging |
| 349 | |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 350 | bundleModuleFile android.WritablePath |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 351 | outputFiles map[apexPackaging]android.WritablePath |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 352 | installDir android.OutputPath |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 353 | |
| 354 | // list of files to be included in this apex |
| 355 | filesInfo []apexFile |
| 356 | |
| 357 | flattened bool |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 358 | } |
| 359 | |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 360 | func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, |
| 361 | native_shared_libs []string, binaries []string, arch string) { |
| 362 | // Use *FarVariation* to be able to depend on modules having |
| 363 | // conflicting variations with this module. This is required since |
| 364 | // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64' |
| 365 | // for native shared libs. |
| 366 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 367 | {Mutator: "arch", Variation: arch}, |
| 368 | {Mutator: "image", Variation: "core"}, |
| 369 | {Mutator: "link", Variation: "shared"}, |
Jiyong Park | 28d395a | 2018-12-07 22:42:47 +0900 | [diff] [blame^] | 370 | {Mutator: "version", Variation: ""}, // "" is the non-stub variant |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 371 | }, sharedLibTag, native_shared_libs...) |
| 372 | |
| 373 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 374 | {Mutator: "arch", Variation: arch}, |
| 375 | {Mutator: "image", Variation: "core"}, |
| 376 | }, executableTag, binaries...) |
| 377 | } |
| 378 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 379 | func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 380 | targets := ctx.MultiTargets() |
| 381 | has32BitTarget := false |
| 382 | for _, target := range targets { |
| 383 | if target.Arch.ArchType.Multilib == "lib32" { |
| 384 | has32BitTarget = true |
| 385 | } |
| 386 | } |
| 387 | for i, target := range targets { |
| 388 | // When multilib.* is omitted for native_shared_libs, it implies |
| 389 | // multilib.both. |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 390 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 391 | {Mutator: "arch", Variation: target.String()}, |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 392 | {Mutator: "image", Variation: "core"}, |
| 393 | {Mutator: "link", Variation: "shared"}, |
| 394 | }, sharedLibTag, a.properties.Native_shared_libs...) |
| 395 | |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 396 | // Add native modules targetting both ABIs |
| 397 | addDependenciesForNativeModules(ctx, |
| 398 | a.properties.Multilib.Both.Native_shared_libs, |
| 399 | a.properties.Multilib.Both.Binaries, target.String()) |
| 400 | |
| 401 | if i == 0 { |
| 402 | // When multilib.* is omitted for binaries, it implies |
| 403 | // multilib.first. |
| 404 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 405 | {Mutator: "arch", Variation: target.String()}, |
| 406 | {Mutator: "image", Variation: "core"}, |
| 407 | }, executableTag, a.properties.Binaries...) |
| 408 | |
| 409 | // Add native modules targetting the first ABI |
| 410 | addDependenciesForNativeModules(ctx, |
| 411 | a.properties.Multilib.First.Native_shared_libs, |
| 412 | a.properties.Multilib.First.Binaries, target.String()) |
| 413 | } |
| 414 | |
| 415 | switch target.Arch.ArchType.Multilib { |
| 416 | case "lib32": |
| 417 | // Add native modules targetting 32-bit ABI |
| 418 | addDependenciesForNativeModules(ctx, |
| 419 | a.properties.Multilib.Lib32.Native_shared_libs, |
| 420 | a.properties.Multilib.Lib32.Binaries, target.String()) |
| 421 | |
| 422 | addDependenciesForNativeModules(ctx, |
| 423 | a.properties.Multilib.Prefer32.Native_shared_libs, |
| 424 | a.properties.Multilib.Prefer32.Binaries, target.String()) |
| 425 | case "lib64": |
| 426 | // Add native modules targetting 64-bit ABI |
| 427 | addDependenciesForNativeModules(ctx, |
| 428 | a.properties.Multilib.Lib64.Native_shared_libs, |
| 429 | a.properties.Multilib.Lib64.Binaries, target.String()) |
| 430 | |
| 431 | if !has32BitTarget { |
| 432 | addDependenciesForNativeModules(ctx, |
| 433 | a.properties.Multilib.Prefer32.Native_shared_libs, |
| 434 | a.properties.Multilib.Prefer32.Binaries, target.String()) |
| 435 | } |
| 436 | } |
| 437 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 438 | } |
| 439 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 440 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 441 | {Mutator: "arch", Variation: "android_common"}, |
| 442 | }, javaLibTag, a.properties.Java_libs...) |
| 443 | |
| 444 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 445 | {Mutator: "arch", Variation: "android_common"}, |
| 446 | }, prebuiltTag, a.properties.Prebuilts...) |
| 447 | |
| 448 | if String(a.properties.Key) == "" { |
| 449 | ctx.ModuleErrorf("key is missing") |
| 450 | return |
| 451 | } |
| 452 | ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key)) |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 453 | |
| 454 | cert := android.SrcIsModule(String(a.properties.Certificate)) |
| 455 | if cert != "" { |
| 456 | ctx.AddDependency(ctx.Module(), certificateTag, cert) |
| 457 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | func getCopyManifestForNativeLibrary(cc *cc.Module) (fileToCopy android.Path, dirInApex string) { |
| 461 | // Decide the APEX-local directory by the multilib of the library |
| 462 | // In the future, we may query this to the module. |
| 463 | switch cc.Arch().ArchType.Multilib { |
| 464 | case "lib32": |
| 465 | dirInApex = "lib" |
| 466 | case "lib64": |
| 467 | dirInApex = "lib64" |
| 468 | } |
| 469 | if !cc.Arch().Native { |
| 470 | dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String()) |
| 471 | } |
| 472 | |
| 473 | fileToCopy = cc.OutputFile().Path() |
| 474 | return |
| 475 | } |
| 476 | |
| 477 | func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) { |
| 478 | dirInApex = "bin" |
| 479 | fileToCopy = cc.OutputFile().Path() |
| 480 | return |
| 481 | } |
| 482 | |
| 483 | func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) { |
| 484 | dirInApex = "javalib" |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 485 | fileToCopy = java.DexJarFile() |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 486 | return |
| 487 | } |
| 488 | |
| 489 | func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) { |
| 490 | dirInApex = filepath.Join("etc", prebuilt.SubDir()) |
| 491 | fileToCopy = prebuilt.OutputFile() |
| 492 | return |
| 493 | } |
| 494 | |
| 495 | func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 496 | filesInfo := []apexFile{} |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 497 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 498 | var keyFile android.Path |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 499 | var certificate java.Certificate |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 500 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 501 | if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" { |
| 502 | a.apexTypes = imageApex |
| 503 | } else if *a.properties.Payload_type == "zip" { |
| 504 | a.apexTypes = zipApex |
| 505 | } else if *a.properties.Payload_type == "both" { |
| 506 | a.apexTypes = both |
| 507 | } else { |
| 508 | ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type) |
| 509 | return |
| 510 | } |
| 511 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 512 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 513 | if _, ok := parent.(*apexBundle); ok { |
| 514 | // direct dependencies |
| 515 | depTag := ctx.OtherModuleDependencyTag(child) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 516 | depName := ctx.OtherModuleName(child) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 517 | switch depTag { |
| 518 | case sharedLibTag: |
| 519 | if cc, ok := child.(*cc.Module); ok { |
| 520 | fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 521 | filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib}) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 522 | return true |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 523 | } else { |
| 524 | 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] | 525 | } |
| 526 | case executableTag: |
| 527 | if cc, ok := child.(*cc.Module); ok { |
| 528 | fileToCopy, dirInApex := getCopyManifestForExecutable(cc) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 529 | filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeExecutable}) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 530 | return true |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 531 | } else { |
| 532 | ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 533 | } |
| 534 | case javaLibTag: |
| 535 | if java, ok := child.(*java.Library); ok { |
| 536 | fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 537 | if fileToCopy == nil { |
| 538 | ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName) |
| 539 | } else { |
| 540 | filesInfo = append(filesInfo, apexFile{fileToCopy, depName, java.Arch().ArchType, dirInApex, javaSharedLib}) |
| 541 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 542 | return true |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 543 | } else { |
| 544 | ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 545 | } |
| 546 | case prebuiltTag: |
| 547 | if prebuilt, ok := child.(*android.PrebuiltEtc); ok { |
| 548 | fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 549 | filesInfo = append(filesInfo, apexFile{fileToCopy, depName, prebuilt.Arch().ArchType, dirInApex, etc}) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 550 | return true |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 551 | } else { |
| 552 | ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName) |
| 553 | } |
| 554 | case keyTag: |
| 555 | if key, ok := child.(*apexKey); ok { |
| 556 | keyFile = key.private_key_file |
| 557 | return false |
| 558 | } else { |
| 559 | ctx.PropertyErrorf("key", "%q is not an apex_key module", depName) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 560 | } |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 561 | case certificateTag: |
| 562 | if dep, ok := child.(*java.AndroidAppCertificate); ok { |
| 563 | certificate = dep.Certificate |
| 564 | return false |
| 565 | } else { |
| 566 | ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName) |
| 567 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 568 | } |
| 569 | } else { |
| 570 | // indirect dependencies |
| 571 | if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() { |
| 572 | if cc, ok := child.(*cc.Module); ok { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 573 | if cc.IsStubs() || cc.HasStubsVariants() { |
| 574 | return false |
| 575 | } |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 576 | depName := ctx.OtherModuleName(child) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 577 | fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 578 | filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib}) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 579 | return true |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | return false |
| 584 | }) |
| 585 | |
Jiyong Park | fa0a373 | 2018-11-09 05:52:26 +0900 | [diff] [blame] | 586 | if keyFile == nil { |
| 587 | ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key)) |
| 588 | return |
| 589 | } |
| 590 | |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 591 | // remove duplicates in filesInfo |
| 592 | removeDup := func(filesInfo []apexFile) []apexFile { |
| 593 | encountered := make(map[android.Path]bool) |
| 594 | result := []apexFile{} |
| 595 | for _, f := range filesInfo { |
| 596 | if !encountered[f.builtFile] { |
| 597 | encountered[f.builtFile] = true |
| 598 | result = append(result, f) |
| 599 | } |
| 600 | } |
| 601 | return result |
| 602 | } |
| 603 | filesInfo = removeDup(filesInfo) |
| 604 | |
| 605 | // to have consistent build rules |
| 606 | sort.Slice(filesInfo, func(i, j int) bool { |
| 607 | return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String() |
| 608 | }) |
| 609 | |
| 610 | // prepend the name of this APEX to the module names. These names will be the names of |
| 611 | // modules that will be defined if the APEX is flattened. |
| 612 | for i := range filesInfo { |
| 613 | filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName |
| 614 | } |
| 615 | |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 616 | a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild() |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 617 | a.installDir = android.PathForModuleInstall(ctx, "apex") |
| 618 | a.filesInfo = filesInfo |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 619 | |
| 620 | if a.apexTypes.zip() { |
| 621 | a.buildUnflattenedApex(ctx, keyFile, certificate, zipApex) |
| 622 | } |
| 623 | if a.apexTypes.image() { |
| 624 | if ctx.Config().FlattenApex() { |
| 625 | a.buildFlattenedApex(ctx) |
| 626 | } else { |
| 627 | a.buildUnflattenedApex(ctx, keyFile, certificate, imageApex) |
| 628 | } |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 632 | func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path, certificate java.Certificate, apexType apexPackaging) { |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 633 | cert := String(a.properties.Certificate) |
| 634 | if cert != "" && android.SrcIsModule(cert) == "" { |
| 635 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) |
| 636 | certificate = java.Certificate{ |
| 637 | defaultDir.Join(ctx, cert+".x509.pem"), |
| 638 | defaultDir.Join(ctx, cert+".pk8"), |
| 639 | } |
| 640 | } else if cert == "" { |
| 641 | pem, key := ctx.Config().DefaultAppCertificate(ctx) |
| 642 | certificate = java.Certificate{pem, key} |
| 643 | } |
| 644 | |
Dario Freni | 4abb1dc | 2018-11-20 18:04:58 +0000 | [diff] [blame] | 645 | manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")) |
Jiyong Park | d0a65ba | 2018-11-10 06:37:15 +0900 | [diff] [blame] | 646 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 647 | var abis []string |
| 648 | for _, target := range ctx.MultiTargets() { |
| 649 | if len(target.Arch.Abi) > 0 { |
| 650 | abis = append(abis, target.Arch.Abi[0]) |
| 651 | } |
Jiyong Park | d0a65ba | 2018-11-10 06:37:15 +0900 | [diff] [blame] | 652 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 653 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 654 | abis = android.FirstUniqueStrings(abis) |
| 655 | |
| 656 | suffix := apexType.suffix() |
| 657 | unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 658 | |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 659 | filesToCopy := []android.Path{} |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 660 | for _, f := range a.filesInfo { |
| 661 | filesToCopy = append(filesToCopy, f.builtFile) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 662 | } |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 663 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 664 | copyCommands := []string{} |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 665 | for i, src := range filesToCopy { |
| 666 | dest := filepath.Join(a.filesInfo[i].installDir, src.Base()) |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 667 | dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 668 | copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path)) |
| 669 | copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path) |
| 670 | } |
Jiyong Park | ab3ceb3 | 2018-10-10 14:05:29 +0900 | [diff] [blame] | 671 | implicitInputs := append(android.Paths(nil), filesToCopy...) |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 672 | implicitInputs = append(implicitInputs, manifest) |
| 673 | |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 674 | outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String() |
| 675 | prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin") |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 676 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 677 | if apexType.image() { |
| 678 | // files and dirs that will be created in APEX |
| 679 | var readOnlyPaths []string |
| 680 | var executablePaths []string // this also includes dirs |
| 681 | for _, f := range a.filesInfo { |
| 682 | pathInApex := filepath.Join(f.installDir, f.builtFile.Base()) |
| 683 | if f.installDir == "bin" { |
| 684 | executablePaths = append(executablePaths, pathInApex) |
| 685 | } else { |
| 686 | readOnlyPaths = append(readOnlyPaths, pathInApex) |
| 687 | } |
Jiyong Park | 7c2ee71 | 2018-12-07 00:42:25 +0900 | [diff] [blame] | 688 | dir := f.installDir |
| 689 | for !android.InList(dir, executablePaths) && dir != "" { |
| 690 | executablePaths = append(executablePaths, dir) |
| 691 | dir, _ = filepath.Split(dir) // move up to the parent |
| 692 | if len(dir) > 0 { |
| 693 | // remove trailing slash |
| 694 | dir = dir[:len(dir)-1] |
| 695 | } |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | sort.Strings(readOnlyPaths) |
| 699 | sort.Strings(executablePaths) |
| 700 | cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config") |
| 701 | ctx.Build(pctx, android.BuildParams{ |
| 702 | Rule: generateFsConfig, |
| 703 | Output: cannedFsConfig, |
| 704 | Description: "generate fs config", |
| 705 | Args: map[string]string{ |
| 706 | "ro_paths": strings.Join(readOnlyPaths, " "), |
| 707 | "exec_paths": strings.Join(executablePaths, " "), |
| 708 | }, |
| 709 | }) |
| 710 | |
| 711 | fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName()) |
| 712 | fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts" |
| 713 | fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath) |
| 714 | if !fileContextsOptionalPath.Valid() { |
| 715 | ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath) |
| 716 | return |
| 717 | } |
| 718 | fileContexts := fileContextsOptionalPath.Path() |
| 719 | |
| 720 | // Additional implicit inputs. |
| 721 | implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile) |
| 722 | |
| 723 | ctx.Build(pctx, android.BuildParams{ |
| 724 | Rule: apexRule, |
| 725 | Implicits: implicitInputs, |
| 726 | Output: unsignedOutputFile, |
| 727 | Description: "apex (" + apexType.name() + ")", |
| 728 | Args: map[string]string{ |
| 729 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
| 730 | "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(), |
| 731 | "copy_commands": strings.Join(copyCommands, " && "), |
| 732 | "manifest": manifest.String(), |
| 733 | "file_contexts": fileContexts.String(), |
| 734 | "canned_fs_config": cannedFsConfig.String(), |
| 735 | "key": keyFile.String(), |
| 736 | }, |
| 737 | }) |
| 738 | |
| 739 | apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix) |
| 740 | bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip") |
| 741 | a.bundleModuleFile = bundleModuleFile |
| 742 | |
| 743 | ctx.Build(pctx, android.BuildParams{ |
| 744 | Rule: apexProtoConvertRule, |
| 745 | Input: unsignedOutputFile, |
| 746 | Output: apexProtoFile, |
| 747 | Description: "apex proto convert", |
| 748 | }) |
| 749 | |
| 750 | ctx.Build(pctx, android.BuildParams{ |
| 751 | Rule: apexBundleRule, |
| 752 | Input: apexProtoFile, |
| 753 | Output: a.bundleModuleFile, |
| 754 | Description: "apex bundle module", |
| 755 | Args: map[string]string{ |
| 756 | "abi": strings.Join(abis, "."), |
| 757 | }, |
| 758 | }) |
| 759 | } else { |
| 760 | ctx.Build(pctx, android.BuildParams{ |
| 761 | Rule: zipApexRule, |
| 762 | Implicits: implicitInputs, |
| 763 | Output: unsignedOutputFile, |
| 764 | Description: "apex (" + apexType.name() + ")", |
| 765 | Args: map[string]string{ |
| 766 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
| 767 | "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(), |
| 768 | "copy_commands": strings.Join(copyCommands, " && "), |
| 769 | "manifest": manifest.String(), |
| 770 | }, |
| 771 | }) |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 772 | } |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 773 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 774 | a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix) |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 775 | ctx.Build(pctx, android.BuildParams{ |
| 776 | Rule: java.Signapk, |
| 777 | Description: "signapk", |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 778 | Output: a.outputFiles[apexType], |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 779 | Input: unsignedOutputFile, |
| 780 | Args: map[string]string{ |
| 781 | "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "), |
Jiyong Park | bfe64a1 | 2018-11-22 02:51:54 +0900 | [diff] [blame] | 782 | "flags": "-a 4096", //alignment |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 783 | }, |
| 784 | }) |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 785 | |
| 786 | // Install to $OUT/soong/{target,host}/.../apex |
| 787 | ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType]) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 788 | } |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 789 | |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 790 | func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) { |
Dario Freni | 4abb1dc | 2018-11-20 18:04:58 +0000 | [diff] [blame] | 791 | // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 792 | // with other ordinary files. |
Dario Freni | 4abb1dc | 2018-11-20 18:04:58 +0000 | [diff] [blame] | 793 | manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")) |
| 794 | a.filesInfo = append(a.filesInfo, apexFile{manifest, ctx.ModuleName() + ".apex_manifest.json", android.Common, ".", etc}) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 795 | |
| 796 | for _, fi := range a.filesInfo { |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 797 | dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 798 | ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile) |
| 799 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | func (a *apexBundle) AndroidMk() android.AndroidMkData { |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 803 | writers := []android.AndroidMkData{} |
| 804 | if a.apexTypes.image() { |
| 805 | writers = append(writers, a.androidMkForType(imageApex)) |
| 806 | } |
| 807 | if a.apexTypes.zip() { |
| 808 | writers = append(writers, a.androidMkForType(zipApex)) |
| 809 | } |
| 810 | return android.AndroidMkData{ |
| 811 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 812 | for _, data := range writers { |
| 813 | data.Custom(w, name, prefix, moduleDir, data) |
| 814 | } |
| 815 | }} |
| 816 | } |
| 817 | |
| 818 | func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData { |
| 819 | // Only image APEXes can be flattened. |
| 820 | if a.flattened && apexType.image() { |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 821 | return android.AndroidMkData{ |
| 822 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 823 | moduleNames := []string{} |
| 824 | for _, fi := range a.filesInfo { |
| 825 | if !android.InList(fi.moduleName, moduleNames) { |
| 826 | moduleNames = append(moduleNames, fi.moduleName) |
| 827 | } |
| 828 | } |
| 829 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 830 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 831 | fmt.Fprintln(w, "LOCAL_MODULE :=", name) |
| 832 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " ")) |
| 833 | fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") |
| 834 | |
| 835 | for _, fi := range a.filesInfo { |
| 836 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 837 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 838 | fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName) |
| 839 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), name, fi.installDir)) |
| 840 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", fi.builtFile.Base()) |
| 841 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String()) |
| 842 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake()) |
| 843 | archStr := fi.archType.String() |
| 844 | if archStr != "common" { |
| 845 | fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr) |
| 846 | } |
| 847 | if fi.class == javaSharedLib { |
| 848 | fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String()) |
| 849 | fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false") |
| 850 | fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk") |
| 851 | } else { |
| 852 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 853 | } |
| 854 | } |
| 855 | }} |
| 856 | } else { |
| 857 | return android.AndroidMkData{ |
| 858 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 859 | // zip-apex is the less common type so have the name refer to the image-apex |
| 860 | // only and use {name}.zip if you want the zip-apex |
| 861 | if apexType == zipApex && a.apexTypes == both { |
| 862 | name = name + ".zip" |
| 863 | } |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 864 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 865 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 866 | fmt.Fprintln(w, "LOCAL_MODULE :=", name) |
| 867 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class? |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 868 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String()) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 869 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString())) |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 870 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", name+apexType.suffix()) |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 871 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key)) |
| 872 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
Colin Cross | a492590 | 2018-11-16 11:36:28 -0800 | [diff] [blame] | 873 | |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 874 | if apexType == imageApex { |
| 875 | fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String()) |
| 876 | } |
Jiyong Park | 8fd6192 | 2018-11-08 02:50:25 +0900 | [diff] [blame] | 877 | }} |
| 878 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 879 | } |
| 880 | |
Alex Light | ee25072 | 2018-12-06 14:00:02 -0800 | [diff] [blame] | 881 | func ApexBundleFactory() android.Module { |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 882 | module := &apexBundle{ |
| 883 | outputFiles: map[apexPackaging]android.WritablePath{}, |
| 884 | } |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 885 | module.AddProperties(&module.properties) |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 886 | module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool { |
Jiyong Park | 397e55e | 2018-10-24 21:09:55 +0900 | [diff] [blame] | 887 | return class == android.Device && ctx.Config().DevicePrefer32BitExecutables() |
| 888 | }) |
Alex Light | 5098a61 | 2018-11-29 17:12:15 -0800 | [diff] [blame] | 889 | android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
Jiyong Park | 48ca7dc | 2018-10-10 14:01:00 +0900 | [diff] [blame] | 890 | android.InitDefaultableModule(module) |
| 891 | return module |
| 892 | } |