blob: fdb1cfc66340408475f87218744573c81c16f91d [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// 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
15package apex
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
24
25 "android/soong/android"
26 "android/soong/cc"
27 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080028 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090029
30 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080031 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090032 "github.com/google/blueprint/proptools"
33)
34
35var (
36 pctx = android.NewPackageContext("android/apex")
37
38 // Create a canned fs config file where all files and directories are
39 // by default set to (uid/gid/mode) = (1000/1000/0644)
40 // TODO(b/113082813) make this configurable using config.fs syntax
41 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000042 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000043 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090044 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090045 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090047 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090048
Jooyung Hane1633032019-08-01 17:41:43 +090049 injectApexDependency = pctx.StaticRule("injectApexDependency", blueprint.RuleParams{
50 Command: `rm -f $out && ${jsonmodify} $in ` +
51 `-a provideNativeLibs ${provideNativeLibs} ` +
52 `-a requireNativeLibs ${requireNativeLibs} -o $out`,
53 CommandDeps: []string{"${jsonmodify}"},
54 Description: "Inject dependency into ${out}",
55 }, "provideNativeLibs", "requireNativeLibs")
56
Jiyong Park48ca7dc2018-10-10 14:01:00 +090057 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
58 // against the binary policy using sefcontext_compiler -p <policy>.
59
60 // TODO(b/114327326): automate the generation of file_contexts
61 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
62 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010063 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090064 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090065 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090066 `--file_contexts ${file_contexts} ` +
67 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080068 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090069 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090070 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
71 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000072 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010073 Rspfile: "${out}.copy_commands",
74 RspfileContent: "${copy_commands}",
75 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090076 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080077
Alex Light5098a612018-11-29 17:12:15 -080078 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
79 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010080 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080081 `APEXER_TOOL_PATH=${tool_path} ` +
82 `${apexer} --force --manifest ${manifest} ` +
83 `--payload_type zip ` +
84 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010085 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
86 Rspfile: "${out}.copy_commands",
87 RspfileContent: "${copy_commands}",
88 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080089 }, "tool_path", "image_dir", "copy_commands", "manifest")
90
Colin Crossa4925902018-11-16 11:36:28 -080091 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
92 blueprint.RuleParams{
93 Command: `${aapt2} convert --output-format proto $in -o $out`,
94 CommandDeps: []string{"${aapt2}"},
95 })
96
97 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090098 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000099 `apex_payload.img:apex/${abi}.img ` +
100 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +0000101 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -0800102 CommandDeps: []string{"${zip2zip}"},
103 Description: "app bundle",
104 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900105)
106
Alex Light5098a612018-11-29 17:12:15 -0800107var imageApexSuffix = ".apex"
108var zipApexSuffix = ".zipapex"
109
110var imageApexType = "image"
111var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900112
113type dependencyTag struct {
114 blueprint.BaseDependencyTag
115 name string
116}
117
118var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900119 sharedLibTag = dependencyTag{name: "sharedLib"}
120 executableTag = dependencyTag{name: "executable"}
121 javaLibTag = dependencyTag{name: "javaLib"}
122 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100123 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900124 keyTag = dependencyTag{name: "key"}
125 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900126 usesTag = dependencyTag{name: "uses"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900127)
128
129func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700130 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900131 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900132 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100133 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
134 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
135 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
136 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000137 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100138 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
139 } else {
140 return pctx.HostBinToolPath(ctx, tool).String()
141 }
142 })
143 }
144 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900145 pctx.HostBinToolVariable("avbtool", "avbtool")
146 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
147 pctx.HostBinToolVariable("merge_zips", "merge_zips")
148 pctx.HostBinToolVariable("mke2fs", "mke2fs")
149 pctx.HostBinToolVariable("resize2fs", "resize2fs")
150 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
151 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800152 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900153 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900154 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900155
Alex Light0851b882019-02-07 13:20:53 -0800156 android.RegisterModuleType("apex", apexBundleFactory)
157 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900158 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700159 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160
161 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
162 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700163 ctx.BottomUp("apex", apexMutator).Parallel()
Jooyung Han5c998b92019-06-27 11:30:33 +0900164 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900165 })
166}
167
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900168// Mark the direct and transitive dependencies of apex bundles so that they
169// can be built for the apex bundles.
170func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800171 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800172 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900173 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900174 depName := mctx.OtherModuleName(child)
175 // If the parent is apexBundle, this child is directly depended.
176 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800177 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800178 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
179 // non-installable apex's cannot be installed and so should not prevent libraries from being
180 // installed to the system.
181 android.UpdateApexDependency(apexBundleName, depName, directDep)
182 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900183
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900184 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900185 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900186 return true
187 } else {
188 return false
189 }
190 })
191 }
192}
193
194// Create apex variations if a module is included in APEX(s).
195func apexMutator(mctx android.BottomUpMutatorContext) {
196 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900197 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900198 } else if _, ok := mctx.Module().(*apexBundle); ok {
199 // apex bundle itself is mutated so that it and its modules have same
200 // apex variant.
201 apexBundleName := mctx.ModuleName()
202 mctx.CreateVariations(apexBundleName)
203 }
204}
Jooyung Han5c998b92019-06-27 11:30:33 +0900205func apexUsesMutator(mctx android.BottomUpMutatorContext) {
206 if ab, ok := mctx.Module().(*apexBundle); ok {
207 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
208 }
209}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900210
Alex Light9670d332019-01-29 18:07:33 -0800211type apexNativeDependencies struct {
212 // List of native libraries
213 Native_shared_libs []string
214 // List of native executables
215 Binaries []string
Roland Levillain630846d2019-06-26 12:48:34 +0100216 // List of native tests
217 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800218}
219type apexMultilibProperties struct {
220 // Native dependencies whose compile_multilib is "first"
221 First apexNativeDependencies
222
223 // Native dependencies whose compile_multilib is "both"
224 Both apexNativeDependencies
225
226 // Native dependencies whose compile_multilib is "prefer32"
227 Prefer32 apexNativeDependencies
228
229 // Native dependencies whose compile_multilib is "32"
230 Lib32 apexNativeDependencies
231
232 // Native dependencies whose compile_multilib is "64"
233 Lib64 apexNativeDependencies
234}
235
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900236type apexBundleProperties struct {
237 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000238 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800239 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900240
Jiyong Park40e26a22019-02-08 02:53:06 +0900241 // AndroidManifest.xml file used for the zip container of this APEX bundle.
242 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800243 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900244
Jiyong Park05e70dd2019-03-18 14:26:32 +0900245 // Canonical name of the APEX bundle in the manifest file.
246 // If unspecified, defaults to the value of name
247 Apex_name *string
248
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900249 // Determines the file contexts file for setting security context to each file in this APEX bundle.
250 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
251 // used.
252 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900253 File_contexts *string
254
255 // List of native shared libs that are embedded inside this APEX bundle
256 Native_shared_libs []string
257
Roland Levillain630846d2019-06-26 12:48:34 +0100258 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900259 Binaries []string
260
261 // List of java libraries that are embedded inside this APEX bundle
262 Java_libs []string
263
264 // List of prebuilt files that are embedded inside this APEX bundle
265 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900266
Roland Levillain630846d2019-06-26 12:48:34 +0100267 // List of tests that are embedded inside this APEX bundle
268 Tests []string
269
Jiyong Parkff1458f2018-10-12 21:49:38 +0900270 // Name of the apex_key module that provides the private key to sign APEX
271 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900272
Alex Light5098a612018-11-29 17:12:15 -0800273 // The type of APEX to build. Controls what the APEX payload is. Either
274 // 'image', 'zip' or 'both'. Default: 'image'.
275 Payload_type *string
276
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900277 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
278 // or an android_app_certificate module name in the form ":module".
279 Certificate *string
280
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900281 // Whether this APEX is installable to one of the partitions. Default: true.
282 Installable *bool
283
Jiyong Parkda6eb592018-12-19 17:12:36 +0900284 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
285 // Default is false.
286 Use_vendor *bool
287
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800288 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
289 Ignore_system_library_special_case *bool
290
Alex Light9670d332019-01-29 18:07:33 -0800291 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900292
Jiyong Parkf97782b2019-02-13 20:28:58 +0900293 // List of sanitizer names that this APEX is enabled for
294 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900295
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900296 PreventInstall bool `blueprint:"mutated"`
297
298 HideFromMake bool `blueprint:"mutated"`
299
Jooyung Han5c998b92019-06-27 11:30:33 +0900300 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
301 Provide_cpp_shared_libs *bool
302
303 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
304 Uses []string
Alex Light9670d332019-01-29 18:07:33 -0800305}
306
307type apexTargetBundleProperties struct {
308 Target struct {
309 // Multilib properties only for android.
310 Android struct {
311 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900312 }
Alex Light9670d332019-01-29 18:07:33 -0800313 // Multilib properties only for host.
314 Host struct {
315 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900316 }
Alex Light9670d332019-01-29 18:07:33 -0800317 // Multilib properties only for host linux_bionic.
318 Linux_bionic struct {
319 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900320 }
Alex Light9670d332019-01-29 18:07:33 -0800321 // Multilib properties only for host linux_glibc.
322 Linux_glibc struct {
323 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900324 }
325 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900326}
327
Jiyong Park8fd61922018-11-08 02:50:25 +0900328type apexFileClass int
329
330const (
331 etc apexFileClass = iota
332 nativeSharedLib
333 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900334 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800335 pyBinary
336 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900337 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100338 nativeTest
Jiyong Park8fd61922018-11-08 02:50:25 +0900339)
340
Alex Light5098a612018-11-29 17:12:15 -0800341type apexPackaging int
342
343const (
344 imageApex apexPackaging = iota
345 zipApex
346 both
347)
348
349func (a apexPackaging) image() bool {
350 switch a {
351 case imageApex, both:
352 return true
353 }
354 return false
355}
356
357func (a apexPackaging) zip() bool {
358 switch a {
359 case zipApex, both:
360 return true
361 }
362 return false
363}
364
365func (a apexPackaging) suffix() string {
366 switch a {
367 case imageApex:
368 return imageApexSuffix
369 case zipApex:
370 return zipApexSuffix
371 case both:
372 panic(fmt.Errorf("must be either zip or image"))
373 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100374 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800375 }
376}
377
378func (a apexPackaging) name() string {
379 switch a {
380 case imageApex:
381 return imageApexType
382 case zipApex:
383 return zipApexType
384 case both:
385 panic(fmt.Errorf("must be either zip or image"))
386 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100387 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800388 }
389}
390
Jiyong Park8fd61922018-11-08 02:50:25 +0900391func (class apexFileClass) NameInMake() string {
392 switch class {
393 case etc:
394 return "ETC"
395 case nativeSharedLib:
396 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800397 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900398 return "EXECUTABLES"
399 case javaSharedLib:
400 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100401 case nativeTest:
402 return "NATIVE_TESTS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900403 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100404 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900405 }
406}
407
408type apexFile struct {
409 builtFile android.Path
410 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900411 installDir string
412 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900413 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800414 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900415}
416
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900417type apexBundle struct {
418 android.ModuleBase
419 android.DefaultableModuleBase
420
Alex Light9670d332019-01-29 18:07:33 -0800421 properties apexBundleProperties
422 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900423
Alex Light5098a612018-11-29 17:12:15 -0800424 apexTypes apexPackaging
425
Colin Crossa4925902018-11-16 11:36:28 -0800426 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800427 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800428 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900429
Jiyong Park03b68dd2019-07-26 23:20:40 +0900430 prebuiltFileToDelete string
431
Jiyong Park42cca6c2019-04-01 11:15:50 +0900432 public_key_file android.Path
433 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900434
435 container_certificate_file android.Path
436 container_private_key_file android.Path
437
Jiyong Park8fd61922018-11-08 02:50:25 +0900438 // list of files to be included in this apex
439 filesInfo []apexFile
440
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900441 // list of module names that this APEX is depending on
442 externalDeps []string
443
Jiyong Park8fd61922018-11-08 02:50:25 +0900444 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800445
446 testApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900447
448 // intermediate path for apex_manifest.json
449 manifestOut android.WritablePath
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900450}
451
Jiyong Park397e55e2018-10-24 21:09:55 +0900452func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100453 native_shared_libs []string, binaries []string, tests []string,
454 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900455 // Use *FarVariation* to be able to depend on modules having
456 // conflicting variations with this module. This is required since
457 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
458 // for native shared libs.
459 ctx.AddFarVariationDependencies([]blueprint.Variation{
460 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900461 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900462 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900463 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900464 }, sharedLibTag, native_shared_libs...)
465
466 ctx.AddFarVariationDependencies([]blueprint.Variation{
467 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900468 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900469 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100470
471 ctx.AddFarVariationDependencies([]blueprint.Variation{
472 {Mutator: "arch", Variation: arch},
473 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100474 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100475 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900476}
477
Alex Light9670d332019-01-29 18:07:33 -0800478func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
479 if ctx.Os().Class == android.Device {
480 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
481 } else {
482 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
483 if ctx.Os().Bionic() {
484 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
485 } else {
486 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
487 }
488 }
489}
490
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900491func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800492
Jiyong Park397e55e2018-10-24 21:09:55 +0900493 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900494 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800495
496 a.combineProperties(ctx)
497
Jiyong Park397e55e2018-10-24 21:09:55 +0900498 has32BitTarget := false
499 for _, target := range targets {
500 if target.Arch.ArchType.Multilib == "lib32" {
501 has32BitTarget = true
502 }
503 }
504 for i, target := range targets {
505 // When multilib.* is omitted for native_shared_libs, it implies
506 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900507 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900508 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900509 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900510 {Mutator: "link", Variation: "shared"},
511 }, sharedLibTag, a.properties.Native_shared_libs...)
512
Roland Levillain630846d2019-06-26 12:48:34 +0100513 // When multilib.* is omitted for tests, it implies
514 // multilib.both.
515 ctx.AddFarVariationDependencies([]blueprint.Variation{
516 {Mutator: "arch", Variation: target.String()},
517 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100518 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100519 }, testTag, a.properties.Tests...)
520
Jiyong Park397e55e2018-10-24 21:09:55 +0900521 // Add native modules targetting both ABIs
522 addDependenciesForNativeModules(ctx,
523 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100524 a.properties.Multilib.Both.Binaries,
525 a.properties.Multilib.Both.Tests,
526 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900527 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900528
Alex Light3d673592019-01-18 14:37:31 -0800529 isPrimaryAbi := i == 0
530 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900531 // When multilib.* is omitted for binaries, it implies
532 // multilib.first.
533 ctx.AddFarVariationDependencies([]blueprint.Variation{
534 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900535 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900536 }, executableTag, a.properties.Binaries...)
537
538 // Add native modules targetting the first ABI
539 addDependenciesForNativeModules(ctx,
540 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100541 a.properties.Multilib.First.Binaries,
542 a.properties.Multilib.First.Tests,
543 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900544 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800545
546 // When multilib.* is omitted for prebuilts, it implies multilib.first.
547 ctx.AddFarVariationDependencies([]blueprint.Variation{
548 {Mutator: "arch", Variation: target.String()},
549 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900550 }
551
552 switch target.Arch.ArchType.Multilib {
553 case "lib32":
554 // Add native modules targetting 32-bit ABI
555 addDependenciesForNativeModules(ctx,
556 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100557 a.properties.Multilib.Lib32.Binaries,
558 a.properties.Multilib.Lib32.Tests,
559 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900560 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900561
562 addDependenciesForNativeModules(ctx,
563 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100564 a.properties.Multilib.Prefer32.Binaries,
565 a.properties.Multilib.Prefer32.Tests,
566 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900567 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900568 case "lib64":
569 // Add native modules targetting 64-bit ABI
570 addDependenciesForNativeModules(ctx,
571 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100572 a.properties.Multilib.Lib64.Binaries,
573 a.properties.Multilib.Lib64.Tests,
574 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900575 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900576
577 if !has32BitTarget {
578 addDependenciesForNativeModules(ctx,
579 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100580 a.properties.Multilib.Prefer32.Binaries,
581 a.properties.Multilib.Prefer32.Tests,
582 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900583 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900584 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700585
586 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
587 for _, sanitizer := range ctx.Config().SanitizeDevice() {
588 if sanitizer == "hwaddress" {
589 addDependenciesForNativeModules(ctx,
590 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100591 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700592 break
593 }
594 }
595 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900596 }
597
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900598 }
599
Jiyong Parkff1458f2018-10-12 21:49:38 +0900600 ctx.AddFarVariationDependencies([]blueprint.Variation{
601 {Mutator: "arch", Variation: "android_common"},
602 }, javaLibTag, a.properties.Java_libs...)
603
Jiyong Park23c52b02019-02-02 13:13:47 +0900604 if String(a.properties.Key) == "" {
605 ctx.ModuleErrorf("key is missing")
606 return
607 }
608 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900609
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900610 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900611 if cert != "" {
612 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900613 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900614}
615
Colin Cross0ea8ba82019-06-06 14:33:29 -0700616func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900617 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
618 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000619 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900620 }
621 return String(a.properties.Certificate)
622}
623
Colin Cross41955e82019-05-29 14:40:35 -0700624func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
625 switch tag {
626 case "":
627 if file, ok := a.outputFiles[imageApex]; ok {
628 return android.Paths{file}, nil
629 } else {
630 return nil, nil
631 }
632 default:
633 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900634 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900635}
636
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900637func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900638 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900639}
640
Jiyong Park7c1dc612019-01-05 11:15:24 +0900641func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
642 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900643 return "vendor"
644 } else {
645 return "core"
646 }
647}
648
Jiyong Parkf97782b2019-02-13 20:28:58 +0900649func (a *apexBundle) EnableSanitizer(sanitizerName string) {
650 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
651 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
652 }
653}
654
Jiyong Park388ef3f2019-01-28 19:47:32 +0900655func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900656 if android.InList(sanitizerName, a.properties.SanitizerNames) {
657 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900658 }
659
660 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900661 globalSanitizerNames := []string{}
662 if a.Host() {
663 globalSanitizerNames = ctx.Config().SanitizeHost()
664 } else {
665 arches := ctx.Config().SanitizeDeviceArch()
666 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
667 globalSanitizerNames = ctx.Config().SanitizeDevice()
668 }
669 }
670 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900671}
672
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900673func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
674 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
675}
676
677func (a *apexBundle) PreventInstall() {
678 a.properties.PreventInstall = true
679}
680
681func (a *apexBundle) HideFromMake() {
682 a.properties.HideFromMake = true
683}
684
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800685func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900686 // Decide the APEX-local directory by the multilib of the library
687 // In the future, we may query this to the module.
688 switch cc.Arch().ArchType.Multilib {
689 case "lib32":
690 dirInApex = "lib"
691 case "lib64":
692 dirInApex = "lib64"
693 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900694 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200695 if !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900696 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200697 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
698 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900699 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800700 if handleSpecialLibs {
701 switch cc.Name() {
702 case "libc", "libm", "libdl":
703 // Special case for bionic libs. This is to prevent the bionic libs
704 // from being included in the search path /apex/com.android.apex/lib.
705 // This exclusion is required because bionic libs in the runtime APEX
706 // are available via the legacy paths /system/lib/libc.so, etc. By the
707 // init process, the bionic libs in the APEX are bind-mounted to the
708 // legacy paths and thus will be loaded into the default linker namespace.
709 // If the bionic libs are directly in /apex/com.android.apex/lib then
710 // the same libs will be again loaded to the runtime linker namespace,
711 // which will result double loading of bionic libs that isn't supported.
712 dirInApex = filepath.Join(dirInApex, "bionic")
713 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900714 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900715
716 fileToCopy = cc.OutputFile().Path()
717 return
718}
719
720func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900721 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200722 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900723 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200724 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
725 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900726 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900727 fileToCopy = cc.OutputFile().Path()
728 return
729}
730
Alex Light778127a2019-02-27 14:19:50 -0800731func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
732 dirInApex = "bin"
733 fileToCopy = py.HostToolPath().Path()
734 return
735}
736func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
737 dirInApex = "bin"
738 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
739 if err != nil {
740 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
741 return
742 }
743 fileToCopy = android.PathForOutput(ctx, s)
744 return
745}
746
Jiyong Park04480cf2019-02-06 00:16:29 +0900747func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
748 dirInApex = filepath.Join("bin", sh.SubDir())
749 fileToCopy = sh.OutputFile()
750 return
751}
752
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900753func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
754 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900755 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900756 return
757}
758
Jiyong Park9e6c2422019-08-09 20:39:45 +0900759func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
760 dirInApex = "javalib"
761 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
762 implJars := java.ImplementationJars()
763 if len(implJars) != 1 {
764 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
765 strings.Join(implJars.Strings(), ", ")))
766 }
767 fileToCopy = implJars[0]
768 return
769}
770
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900771func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
772 dirInApex = filepath.Join("etc", prebuilt.SubDir())
773 fileToCopy = prebuilt.OutputFile()
774 return
775}
776
777func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900778 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900779
Alex Light5098a612018-11-29 17:12:15 -0800780 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
781 a.apexTypes = imageApex
782 } else if *a.properties.Payload_type == "zip" {
783 a.apexTypes = zipApex
784 } else if *a.properties.Payload_type == "both" {
785 a.apexTypes = both
786 } else {
787 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
788 return
789 }
790
Roland Levillain630846d2019-06-26 12:48:34 +0100791 if len(a.properties.Tests) > 0 && !a.testApex {
792 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
793 return
794 }
795
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800796 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
797
Jooyung Hane1633032019-08-01 17:41:43 +0900798 // native lib dependencies
799 var provideNativeLibs []string
800 var requireNativeLibs []string
801
Jooyung Han5c998b92019-06-27 11:30:33 +0900802 // Check if "uses" requirements are met with dependent apexBundles
803 var providedNativeSharedLibs []string
804 useVendor := proptools.Bool(a.properties.Use_vendor)
805 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
806 if ctx.OtherModuleDependencyTag(m) != usesTag {
807 return
808 }
809 otherName := ctx.OtherModuleName(m)
810 other, ok := m.(*apexBundle)
811 if !ok {
812 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
813 return
814 }
815 if proptools.Bool(other.properties.Use_vendor) != useVendor {
816 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
817 return
818 }
819 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
820 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
821 return
822 }
823 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
824 })
825
Alex Light778127a2019-02-27 14:19:50 -0800826 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100827 depTag := ctx.OtherModuleDependencyTag(child)
828 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900829 if _, ok := parent.(*apexBundle); ok {
830 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900831 switch depTag {
832 case sharedLibTag:
833 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900834 if cc.HasStubsVariants() {
835 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
836 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800837 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900838 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900839 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900840 } else {
841 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900842 }
843 case executableTag:
844 if cc, ok := child.(*cc.Module); ok {
845 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900846 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900847 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900848 } else if sh, ok := child.(*android.ShBinary); ok {
849 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
850 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800851 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
852 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
853 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
854 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
855 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
856 // NB: Since go binaries are static we don't need the module for anything here, which is
857 // good since the go tool is a blueprint.Module not an android.Module like we would
858 // normally use.
859 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900860 } else {
Alex Light778127a2019-02-27 14:19:50 -0800861 ctx.PropertyErrorf("binaries", "%q is neither cc_binary, (embedded) py_binary, (host) blueprint_go_binary, (host) bootstrap_go_binary, nor sh_binary", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900862 }
863 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +0900864 if javaLib, ok := child.(*java.Library); ok {
865 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +0900866 if fileToCopy == nil {
867 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
868 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900869 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
870 }
871 return true
872 } else if javaLib, ok := child.(*java.Import); ok {
873 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
874 if fileToCopy == nil {
875 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
876 } else {
877 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900878 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900879 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900880 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900881 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900882 }
883 case prebuiltTag:
884 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
885 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900886 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900887 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900888 } else {
889 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
890 }
Roland Levillain630846d2019-06-26 12:48:34 +0100891 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100892 if ccTest, ok := child.(*cc.Module); ok {
893 if ccTest.IsTestPerSrcAllTestsVariation() {
894 // Multiple-output test module (where `test_per_src: true`).
895 //
896 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
897 // We do not add this variation to `filesInfo`, as it has no output;
898 // however, we do add the other variations of this module as indirect
899 // dependencies (see below).
900 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100901 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100902 // Single-output test module (where `test_per_src: false`).
903 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
904 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100905 }
Roland Levillain630846d2019-06-26 12:48:34 +0100906 return true
907 } else {
908 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
909 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900910 case keyTag:
911 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900912 a.private_key_file = key.private_key_file
913 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900914 return false
915 } else {
916 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900917 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900918 case certificateTag:
919 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900920 a.container_certificate_file = dep.Certificate.Pem
921 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900922 return false
923 } else {
924 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
925 }
Jiyong Park03b68dd2019-07-26 23:20:40 +0900926 case android.PrebuiltDepTag:
927 // If the prebuilt is force disabled, remember to delete the prebuilt file
928 // that might have been installed in the previous builds
929 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
930 a.prebuiltFileToDelete = prebuilt.InstallFilename()
931 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900932 }
933 } else {
934 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +0900935 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +0100936 // We cannot use a switch statement on `depTag` here as the checked
937 // tags used below are private (e.g. `cc.sharedDepTag`).
938 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
939 if cc, ok := child.(*cc.Module); ok {
940 if android.InList(cc.Name(), providedNativeSharedLibs) {
941 // If we're using a shared library which is provided from other APEX,
942 // don't include it in this APEX
943 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900944 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100945 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
946 // If the dependency is a stubs lib, don't include it in this APEX,
947 // but make sure that the lib is installed on the device.
948 // In case no APEX is having the lib, the lib is installed to the system
949 // partition.
950 //
951 // Always include if we are a host-apex however since those won't have any
952 // system libraries.
953 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
954 a.externalDeps = append(a.externalDeps, cc.Name())
955 }
Jooyung Hane1633032019-08-01 17:41:43 +0900956 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +0100957 // Don't track further
958 return false
959 }
960 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
961 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
962 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +0900963 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100964 } else if cc.IsTestPerSrcDepTag(depTag) {
965 if cc, ok := child.(*cc.Module); ok {
966 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
967 // Handle modules created as `test_per_src` variations of a single test module:
968 // use the name of the generated test binary (`fileToCopy`) instead of the name
969 // of the original test module (`depName`, shared by all `test_per_src`
970 // variations of that module).
971 moduleName := filepath.Base(fileToCopy.String())
972 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
973 return true
974 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900975 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +0100976 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900977 }
978 }
979 }
980 return false
981 })
982
Jiyong Park9335a262018-12-24 11:31:58 +0900983 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900984 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900985 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
986 return
987 }
988
Jiyong Park8fd61922018-11-08 02:50:25 +0900989 // remove duplicates in filesInfo
990 removeDup := func(filesInfo []apexFile) []apexFile {
991 encountered := make(map[android.Path]bool)
992 result := []apexFile{}
993 for _, f := range filesInfo {
994 if !encountered[f.builtFile] {
995 encountered[f.builtFile] = true
996 result = append(result, f)
997 }
998 }
999 return result
1000 }
1001 filesInfo = removeDup(filesInfo)
1002
1003 // to have consistent build rules
1004 sort.Slice(filesInfo, func(i, j int) bool {
1005 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1006 })
1007
1008 // prepend the name of this APEX to the module names. These names will be the names of
1009 // modules that will be defined if the APEX is flattened.
1010 for i := range filesInfo {
1011 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1012 }
1013
Jiyong Park8fd61922018-11-08 02:50:25 +09001014 a.installDir = android.PathForModuleInstall(ctx, "apex")
1015 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001016
Jooyung Hane1633032019-08-01 17:41:43 +09001017 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1018 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1019 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1020 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1021 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1022 ctx.Build(pctx, android.BuildParams{
1023 Rule: injectApexDependency,
1024 Input: manifestSrc,
1025 Output: a.manifestOut,
1026 Args: map[string]string{
1027 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1028 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1029 },
1030 })
1031
Alex Light5098a612018-11-29 17:12:15 -08001032 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001033 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001034 }
1035 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001036 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001037 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001038 // in other modules. It is in AndroidMk where the selection of flattened
1039 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001040 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001041 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001042 }
1043}
1044
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001045func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001046 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001047 for _, f := range a.filesInfo {
1048 if f.module != nil {
1049 notice := f.module.NoticeFile()
1050 if notice.Valid() {
1051 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001052 }
1053 }
1054 }
1055 // append the notice file specified in the apex module itself
1056 if a.NoticeFile().Valid() {
1057 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001058 }
1059
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001060 if len(noticeFiles) == 0 {
1061 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001062 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001063
Jaewoong Jung98772792019-07-01 17:15:13 -07001064 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001065}
1066
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001067func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001068 cert := String(a.properties.Certificate)
1069 if cert != "" && android.SrcIsModule(cert) == "" {
1070 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001071 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1072 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001073 } else if cert == "" {
1074 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001075 a.container_certificate_file = pem
1076 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001077 }
1078
Alex Light5098a612018-11-29 17:12:15 -08001079 var abis []string
1080 for _, target := range ctx.MultiTargets() {
1081 if len(target.Arch.Abi) > 0 {
1082 abis = append(abis, target.Arch.Abi[0])
1083 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001084 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001085
Alex Light5098a612018-11-29 17:12:15 -08001086 abis = android.FirstUniqueStrings(abis)
1087
1088 suffix := apexType.suffix()
1089 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001090
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001091 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001092 for _, f := range a.filesInfo {
1093 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001094 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001095
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001096 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001097 for i, src := range filesToCopy {
1098 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -08001099 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001100 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1101 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001102 for _, sym := range a.filesInfo[i].symlinks {
1103 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1104 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1105 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001106 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001107 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001108 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001109
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001110 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1111 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001112
Alex Light5098a612018-11-29 17:12:15 -08001113 if apexType.image() {
1114 // files and dirs that will be created in APEX
1115 var readOnlyPaths []string
1116 var executablePaths []string // this also includes dirs
1117 for _, f := range a.filesInfo {
1118 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001119 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001120 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001121 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001122 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001123 }
Alex Light5098a612018-11-29 17:12:15 -08001124 } else {
1125 readOnlyPaths = append(readOnlyPaths, pathInApex)
1126 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001127 dir := f.installDir
1128 for !android.InList(dir, executablePaths) && dir != "" {
1129 executablePaths = append(executablePaths, dir)
1130 dir, _ = filepath.Split(dir) // move up to the parent
1131 if len(dir) > 0 {
1132 // remove trailing slash
1133 dir = dir[:len(dir)-1]
1134 }
Alex Light5098a612018-11-29 17:12:15 -08001135 }
1136 }
1137 sort.Strings(readOnlyPaths)
1138 sort.Strings(executablePaths)
1139 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1140 ctx.Build(pctx, android.BuildParams{
1141 Rule: generateFsConfig,
1142 Output: cannedFsConfig,
1143 Description: "generate fs config",
1144 Args: map[string]string{
1145 "ro_paths": strings.Join(readOnlyPaths, " "),
1146 "exec_paths": strings.Join(executablePaths, " "),
1147 },
1148 })
1149
1150 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1151 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1152 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1153 if !fileContextsOptionalPath.Valid() {
1154 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1155 return
1156 }
1157 fileContexts := fileContextsOptionalPath.Path()
1158
Jiyong Park835d82b2018-12-27 16:04:18 +09001159 optFlags := []string{}
1160
Alex Light5098a612018-11-29 17:12:15 -08001161 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001162 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1163 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001164
Jiyong Park7f67f482019-01-05 12:57:48 +09001165 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1166 if overridden {
1167 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1168 }
1169
Jiyong Park40e26a22019-02-08 02:53:06 +09001170 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001171 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001172 implicitInputs = append(implicitInputs, androidManifestFile)
1173 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1174 }
1175
Jiyong Park71b519d2019-04-18 17:25:49 +09001176 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1177 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1178 ctx.Config().UnbundledBuild() &&
1179 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1180 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1181 apiFingerprint := java.ApiFingerprintPath(ctx)
1182 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1183 implicitInputs = append(implicitInputs, apiFingerprint)
1184 }
1185 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1186
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001187 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1188 if noticeFile.Valid() {
1189 // If there's a NOTICE file, embed it as an asset file in the APEX.
1190 implicitInputs = append(implicitInputs, noticeFile.Path())
1191 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1192 }
1193
Alex Light5098a612018-11-29 17:12:15 -08001194 ctx.Build(pctx, android.BuildParams{
1195 Rule: apexRule,
1196 Implicits: implicitInputs,
1197 Output: unsignedOutputFile,
1198 Description: "apex (" + apexType.name() + ")",
1199 Args: map[string]string{
1200 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1201 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1202 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001203 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001204 "file_contexts": fileContexts.String(),
1205 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001206 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001207 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001208 },
1209 })
1210
1211 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1212 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1213 a.bundleModuleFile = bundleModuleFile
1214
1215 ctx.Build(pctx, android.BuildParams{
1216 Rule: apexProtoConvertRule,
1217 Input: unsignedOutputFile,
1218 Output: apexProtoFile,
1219 Description: "apex proto convert",
1220 })
1221
1222 ctx.Build(pctx, android.BuildParams{
1223 Rule: apexBundleRule,
1224 Input: apexProtoFile,
1225 Output: a.bundleModuleFile,
1226 Description: "apex bundle module",
1227 Args: map[string]string{
1228 "abi": strings.Join(abis, "."),
1229 },
1230 })
1231 } else {
1232 ctx.Build(pctx, android.BuildParams{
1233 Rule: zipApexRule,
1234 Implicits: implicitInputs,
1235 Output: unsignedOutputFile,
1236 Description: "apex (" + apexType.name() + ")",
1237 Args: map[string]string{
1238 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1239 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1240 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001241 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001242 },
1243 })
Colin Crossa4925902018-11-16 11:36:28 -08001244 }
Colin Crossa4925902018-11-16 11:36:28 -08001245
Alex Light5098a612018-11-29 17:12:15 -08001246 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001247 ctx.Build(pctx, android.BuildParams{
1248 Rule: java.Signapk,
1249 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001250 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001251 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001252 Implicits: []android.Path{
1253 a.container_certificate_file,
1254 a.container_private_key_file,
1255 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001256 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001257 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001258 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001259 },
1260 })
Alex Light5098a612018-11-29 17:12:15 -08001261
1262 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001263 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001264 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001265 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001266}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001267
Jiyong Park8fd61922018-11-08 02:50:25 +09001268func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001269 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001270 // For flattened APEX, do nothing but make sure that apex_manifest.json and apex_pubkey are also copied along
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001271 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001272 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001273
Jiyong Park42cca6c2019-04-01 11:15:50 +09001274 // rename to apex_pubkey
1275 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1276 ctx.Build(pctx, android.BuildParams{
1277 Rule: android.Cp,
1278 Input: a.public_key_file,
1279 Output: copiedPubkey,
1280 })
1281 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1282
Jiyong Park23c52b02019-02-02 13:13:47 +09001283 if ctx.Config().FlattenApex() {
1284 for _, fi := range a.filesInfo {
1285 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001286 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1287 for _, sym := range fi.symlinks {
1288 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1289 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001290 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001291 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001292 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001293}
1294
1295func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001296 if a.properties.HideFromMake {
1297 return android.AndroidMkData{
1298 Disabled: true,
1299 }
1300 }
Alex Light5098a612018-11-29 17:12:15 -08001301 writers := []android.AndroidMkData{}
1302 if a.apexTypes.image() {
1303 writers = append(writers, a.androidMkForType(imageApex))
1304 }
1305 if a.apexTypes.zip() {
1306 writers = append(writers, a.androidMkForType(zipApex))
1307 }
1308 return android.AndroidMkData{
1309 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1310 for _, data := range writers {
1311 data.Custom(w, name, prefix, moduleDir, data)
1312 }
1313 }}
1314}
1315
Alex Lightf1801bc2019-02-13 11:10:07 -08001316func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001317 moduleNames := []string{}
1318
1319 for _, fi := range a.filesInfo {
1320 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1321 continue
1322 }
1323 if !android.InList(fi.moduleName, moduleNames) {
1324 moduleNames = append(moduleNames, fi.moduleName)
1325 }
1326 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1327 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1328 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001329 // /apex/<name>/{lib|framework|...}
1330 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1331 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001332 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001333 // /system/apex/<name>/{lib|framework|...}
1334 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1335 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001336 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001337 if len(fi.symlinks) > 0 {
1338 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1339 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001340
1341 if fi.module != nil && fi.module.NoticeFile().Valid() {
1342 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1343 }
Jiyong Park94427262019-02-05 23:18:47 +09001344 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001345 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001346 }
1347 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1348 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1349 if fi.module != nil {
1350 archStr := fi.module.Target().Arch.ArchType.String()
1351 host := false
1352 switch fi.module.Target().Os.Class {
1353 case android.Host:
1354 if archStr != "common" {
1355 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1356 }
1357 host = true
1358 case android.HostCross:
1359 if archStr != "common" {
1360 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1361 }
1362 host = true
1363 case android.Device:
1364 if archStr != "common" {
1365 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1366 }
1367 }
1368 if host {
1369 makeOs := fi.module.Target().Os.String()
1370 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1371 makeOs = "linux"
1372 }
1373 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1374 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1375 }
1376 }
1377 if fi.class == javaSharedLib {
1378 javaModule := fi.module.(*java.Library)
1379 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1380 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1381 // we will have foo.jar.jar
1382 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1383 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1384 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1385 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1386 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1387 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1388 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1389 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001390 if cc, ok := fi.module.(*cc.Module); ok {
1391 if cc.UnstrippedOutputFile() != nil {
1392 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1393 }
1394 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001395 if cc.CoverageOutputFile().Valid() {
1396 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1397 }
Jiyong Park94427262019-02-05 23:18:47 +09001398 }
1399 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1400 } else {
1401 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1402 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1403 }
1404 }
1405 return moduleNames
1406}
1407
Alex Light5098a612018-11-29 17:12:15 -08001408func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001409 return android.AndroidMkData{
1410 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1411 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001412 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001413 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001414 }
1415
Jiyong Park719b4462019-01-13 00:39:51 +09001416 if a.flattened && apexType.image() {
1417 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001418 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1419 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1420 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001421 if len(moduleNames) > 0 {
1422 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1423 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001424 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001425 } else {
Alex Light5098a612018-11-29 17:12:15 -08001426 // zip-apex is the less common type so have the name refer to the image-apex
1427 // only and use {name}.zip if you want the zip-apex
1428 if apexType == zipApex && a.apexTypes == both {
1429 name = name + ".zip"
1430 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001431 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1432 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1433 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1434 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001435 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001436 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001437 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001438 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001439 if len(moduleNames) > 0 {
1440 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1441 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001442 if len(a.externalDeps) > 0 {
1443 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1444 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001445 if a.prebuiltFileToDelete != "" {
1446 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1447 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1448 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001449 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001450
Alex Light5098a612018-11-29 17:12:15 -08001451 if apexType == imageApex {
1452 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1453 }
Jiyong Park719b4462019-01-13 00:39:51 +09001454 }
1455 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001456}
1457
Alex Light0851b882019-02-07 13:20:53 -08001458func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001459 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001460}
1461
1462func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001463 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001464}
1465
1466func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001467 module := &apexBundle{
1468 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001469 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001470 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001471 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001472 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001473 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001474 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1475 })
Alex Light5098a612018-11-29 17:12:15 -08001476 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001477 android.InitDefaultableModule(module)
1478 return module
1479}
Jiyong Park30ca9372019-02-07 16:27:23 +09001480
1481//
1482// Defaults
1483//
1484type Defaults struct {
1485 android.ModuleBase
1486 android.DefaultsModuleBase
1487}
1488
Jiyong Park30ca9372019-02-07 16:27:23 +09001489func defaultsFactory() android.Module {
1490 return DefaultsFactory()
1491}
1492
1493func DefaultsFactory(props ...interface{}) android.Module {
1494 module := &Defaults{}
1495
1496 module.AddProperties(props...)
1497 module.AddProperties(
1498 &apexBundleProperties{},
1499 &apexTargetBundleProperties{},
1500 )
1501
1502 android.InitDefaultsModule(module)
1503 return module
1504}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001505
1506//
1507// Prebuilt APEX
1508//
1509type Prebuilt struct {
1510 android.ModuleBase
1511 prebuilt android.Prebuilt
1512
1513 properties PrebuiltProperties
1514
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001515 inputApex android.Path
1516 installDir android.OutputPath
1517 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001518 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001519}
1520
1521type PrebuiltProperties struct {
1522 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001523 Source string `blueprint:"mutated"`
1524 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001525
1526 Src *string
1527 Arch struct {
1528 Arm struct {
1529 Src *string
1530 }
1531 Arm64 struct {
1532 Src *string
1533 }
1534 X86 struct {
1535 Src *string
1536 }
1537 X86_64 struct {
1538 Src *string
1539 }
1540 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001541
1542 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001543 // Optional name for the installed apex. If unspecified, name of the
1544 // module is used as the file name
1545 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001546
1547 // Names of modules to be overridden. Listed modules can only be other binaries
1548 // (in Make or Soong).
1549 // This does not completely prevent installation of the overridden binaries, but if both
1550 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1551 // from PRODUCT_PACKAGES.
1552 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001553}
1554
1555func (p *Prebuilt) installable() bool {
1556 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001557}
1558
1559func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001560 // If the device is configured to use flattened APEX, force disable the prebuilt because
1561 // the prebuilt is a non-flattened one.
1562 forceDisable := ctx.Config().FlattenApex()
1563
1564 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1565 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001566 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001567
Kun Niu10c9f832019-07-29 16:28:57 -07001568 // Force disable the prebuilts when coverage is enabled.
1569 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1570 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1571
Jiyong Park50b81e52019-07-11 11:24:41 +09001572 // b/137216042 don't use prebuilts when address sanitizer is on
1573 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1574 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1575
1576 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001577 p.properties.ForceDisable = true
1578 return
1579 }
1580
Jiyong Parkc95714e2019-03-29 14:23:10 +09001581 // This is called before prebuilt_select and prebuilt_postdeps mutators
1582 // The mutators requires that src to be set correctly for each arch so that
1583 // arch variants are disabled when src is not provided for the arch.
1584 if len(ctx.MultiTargets()) != 1 {
1585 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1586 return
1587 }
1588 var src string
1589 switch ctx.MultiTargets()[0].Arch.ArchType {
1590 case android.Arm:
1591 src = String(p.properties.Arch.Arm.Src)
1592 case android.Arm64:
1593 src = String(p.properties.Arch.Arm64.Src)
1594 case android.X86:
1595 src = String(p.properties.Arch.X86.Src)
1596 case android.X86_64:
1597 src = String(p.properties.Arch.X86_64.Src)
1598 default:
1599 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1600 return
1601 }
1602 if src == "" {
1603 src = String(p.properties.Src)
1604 }
1605 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001606}
1607
Jiyong Park03b68dd2019-07-26 23:20:40 +09001608func (p *Prebuilt) isForceDisabled() bool {
1609 return p.properties.ForceDisable
1610}
1611
Colin Cross41955e82019-05-29 14:40:35 -07001612func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1613 switch tag {
1614 case "":
1615 return android.Paths{p.outputApex}, nil
1616 default:
1617 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1618 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001619}
1620
Jiyong Park4d277042019-04-23 18:00:10 +09001621func (p *Prebuilt) InstallFilename() string {
1622 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1623}
1624
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001625func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001626 if p.properties.ForceDisable {
1627 return
1628 }
1629
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001630 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001631 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001632 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001633 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001634 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1635 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1636 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001637 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1638 ctx.Build(pctx, android.BuildParams{
1639 Rule: android.Cp,
1640 Input: p.inputApex,
1641 Output: p.outputApex,
1642 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001643 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001644 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001645 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001646}
1647
1648func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1649 return &p.prebuilt
1650}
1651
1652func (p *Prebuilt) Name() string {
1653 return p.prebuilt.Name(p.ModuleBase.Name())
1654}
1655
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001656func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1657 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001658 Class: "ETC",
1659 OutputFile: android.OptionalPathForPath(p.inputApex),
1660 Include: "$(BUILD_PREBUILT)",
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001661 AddCustomEntries: func(name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
1662 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1663 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1664 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1665 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001666 },
1667 }
1668}
1669
1670// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1671func PrebuiltFactory() android.Module {
1672 module := &Prebuilt{}
1673 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001674 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001675 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001676 return module
1677}