blob: 9bec4a8cfbbc42e6151a1eb3e6a17fea472a7b8f [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
759func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
760 dirInApex = filepath.Join("etc", prebuilt.SubDir())
761 fileToCopy = prebuilt.OutputFile()
762 return
763}
764
765func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900766 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900767
Alex Light5098a612018-11-29 17:12:15 -0800768 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
769 a.apexTypes = imageApex
770 } else if *a.properties.Payload_type == "zip" {
771 a.apexTypes = zipApex
772 } else if *a.properties.Payload_type == "both" {
773 a.apexTypes = both
774 } else {
775 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
776 return
777 }
778
Roland Levillain630846d2019-06-26 12:48:34 +0100779 if len(a.properties.Tests) > 0 && !a.testApex {
780 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
781 return
782 }
783
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800784 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
785
Jooyung Hane1633032019-08-01 17:41:43 +0900786 // native lib dependencies
787 var provideNativeLibs []string
788 var requireNativeLibs []string
789
Jooyung Han5c998b92019-06-27 11:30:33 +0900790 // Check if "uses" requirements are met with dependent apexBundles
791 var providedNativeSharedLibs []string
792 useVendor := proptools.Bool(a.properties.Use_vendor)
793 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
794 if ctx.OtherModuleDependencyTag(m) != usesTag {
795 return
796 }
797 otherName := ctx.OtherModuleName(m)
798 other, ok := m.(*apexBundle)
799 if !ok {
800 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
801 return
802 }
803 if proptools.Bool(other.properties.Use_vendor) != useVendor {
804 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
805 return
806 }
807 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
808 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
809 return
810 }
811 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
812 })
813
Alex Light778127a2019-02-27 14:19:50 -0800814 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100815 depTag := ctx.OtherModuleDependencyTag(child)
816 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900817 if _, ok := parent.(*apexBundle); ok {
818 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900819 switch depTag {
820 case sharedLibTag:
821 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900822 if cc.HasStubsVariants() {
823 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
824 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800825 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900826 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900827 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900828 } else {
829 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900830 }
831 case executableTag:
832 if cc, ok := child.(*cc.Module); ok {
833 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900834 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900835 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900836 } else if sh, ok := child.(*android.ShBinary); ok {
837 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
838 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800839 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
840 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
841 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
842 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
843 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
844 // NB: Since go binaries are static we don't need the module for anything here, which is
845 // good since the go tool is a blueprint.Module not an android.Module like we would
846 // normally use.
847 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900848 } else {
Alex Light778127a2019-02-27 14:19:50 -0800849 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 +0900850 }
851 case javaLibTag:
852 if java, ok := child.(*java.Library); ok {
853 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900854 if fileToCopy == nil {
855 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
856 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900857 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900858 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900859 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900860 } else {
861 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900862 }
863 case prebuiltTag:
864 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
865 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900866 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900867 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900868 } else {
869 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
870 }
Roland Levillain630846d2019-06-26 12:48:34 +0100871 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100872 if ccTest, ok := child.(*cc.Module); ok {
873 if ccTest.IsTestPerSrcAllTestsVariation() {
874 // Multiple-output test module (where `test_per_src: true`).
875 //
876 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
877 // We do not add this variation to `filesInfo`, as it has no output;
878 // however, we do add the other variations of this module as indirect
879 // dependencies (see below).
880 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100881 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100882 // Single-output test module (where `test_per_src: false`).
883 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
884 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100885 }
Roland Levillain630846d2019-06-26 12:48:34 +0100886 return true
887 } else {
888 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
889 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900890 case keyTag:
891 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900892 a.private_key_file = key.private_key_file
893 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900894 return false
895 } else {
896 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900897 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900898 case certificateTag:
899 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900900 a.container_certificate_file = dep.Certificate.Pem
901 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900902 return false
903 } else {
904 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
905 }
Jiyong Park03b68dd2019-07-26 23:20:40 +0900906 case android.PrebuiltDepTag:
907 // If the prebuilt is force disabled, remember to delete the prebuilt file
908 // that might have been installed in the previous builds
909 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
910 a.prebuiltFileToDelete = prebuilt.InstallFilename()
911 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900912 }
913 } else {
914 // indirect dependencies
915 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +0100916 // We cannot use a switch statement on `depTag` here as the checked
917 // tags used below are private (e.g. `cc.sharedDepTag`).
918 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
919 if cc, ok := child.(*cc.Module); ok {
920 if android.InList(cc.Name(), providedNativeSharedLibs) {
921 // If we're using a shared library which is provided from other APEX,
922 // don't include it in this APEX
923 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900924 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100925 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
926 // If the dependency is a stubs lib, don't include it in this APEX,
927 // but make sure that the lib is installed on the device.
928 // In case no APEX is having the lib, the lib is installed to the system
929 // partition.
930 //
931 // Always include if we are a host-apex however since those won't have any
932 // system libraries.
933 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
934 a.externalDeps = append(a.externalDeps, cc.Name())
935 }
Jooyung Hane1633032019-08-01 17:41:43 +0900936 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +0100937 // Don't track further
938 return false
939 }
940 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
941 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
942 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +0900943 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100944 } else if cc.IsTestPerSrcDepTag(depTag) {
945 if cc, ok := child.(*cc.Module); ok {
946 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
947 // Handle modules created as `test_per_src` variations of a single test module:
948 // use the name of the generated test binary (`fileToCopy`) instead of the name
949 // of the original test module (`depName`, shared by all `test_per_src`
950 // variations of that module).
951 moduleName := filepath.Base(fileToCopy.String())
952 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
953 return true
954 }
955 } else {
956 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900957 }
958 }
959 }
960 return false
961 })
962
Jiyong Park9335a262018-12-24 11:31:58 +0900963 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900964 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900965 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
966 return
967 }
968
Jiyong Park8fd61922018-11-08 02:50:25 +0900969 // remove duplicates in filesInfo
970 removeDup := func(filesInfo []apexFile) []apexFile {
971 encountered := make(map[android.Path]bool)
972 result := []apexFile{}
973 for _, f := range filesInfo {
974 if !encountered[f.builtFile] {
975 encountered[f.builtFile] = true
976 result = append(result, f)
977 }
978 }
979 return result
980 }
981 filesInfo = removeDup(filesInfo)
982
983 // to have consistent build rules
984 sort.Slice(filesInfo, func(i, j int) bool {
985 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
986 })
987
988 // prepend the name of this APEX to the module names. These names will be the names of
989 // modules that will be defined if the APEX is flattened.
990 for i := range filesInfo {
991 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
992 }
993
Jiyong Park8fd61922018-11-08 02:50:25 +0900994 a.installDir = android.PathForModuleInstall(ctx, "apex")
995 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800996
Jooyung Hane1633032019-08-01 17:41:43 +0900997 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
998 // put dependency({provide|require}NativeLibs) in apex_manifest.json
999 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1000 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1001 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1002 ctx.Build(pctx, android.BuildParams{
1003 Rule: injectApexDependency,
1004 Input: manifestSrc,
1005 Output: a.manifestOut,
1006 Args: map[string]string{
1007 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1008 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1009 },
1010 })
1011
Alex Light5098a612018-11-29 17:12:15 -08001012 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001013 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001014 }
1015 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001016 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001017 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001018 // in other modules. It is in AndroidMk where the selection of flattened
1019 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001020 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001021 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001022 }
1023}
1024
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001025func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001026 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001027 for _, f := range a.filesInfo {
1028 if f.module != nil {
1029 notice := f.module.NoticeFile()
1030 if notice.Valid() {
1031 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001032 }
1033 }
1034 }
1035 // append the notice file specified in the apex module itself
1036 if a.NoticeFile().Valid() {
1037 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001038 }
1039
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001040 if len(noticeFiles) == 0 {
1041 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001042 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001043
Jaewoong Jung98772792019-07-01 17:15:13 -07001044 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001045}
1046
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001047func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001048 cert := String(a.properties.Certificate)
1049 if cert != "" && android.SrcIsModule(cert) == "" {
1050 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001051 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1052 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001053 } else if cert == "" {
1054 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001055 a.container_certificate_file = pem
1056 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001057 }
1058
Alex Light5098a612018-11-29 17:12:15 -08001059 var abis []string
1060 for _, target := range ctx.MultiTargets() {
1061 if len(target.Arch.Abi) > 0 {
1062 abis = append(abis, target.Arch.Abi[0])
1063 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001064 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001065
Alex Light5098a612018-11-29 17:12:15 -08001066 abis = android.FirstUniqueStrings(abis)
1067
1068 suffix := apexType.suffix()
1069 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001070
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001071 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001072 for _, f := range a.filesInfo {
1073 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001074 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001075
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001076 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001077 for i, src := range filesToCopy {
1078 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -08001079 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001080 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1081 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001082 for _, sym := range a.filesInfo[i].symlinks {
1083 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1084 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1085 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001086 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001087 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001088 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001089
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001090 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1091 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001092
Alex Light5098a612018-11-29 17:12:15 -08001093 if apexType.image() {
1094 // files and dirs that will be created in APEX
1095 var readOnlyPaths []string
1096 var executablePaths []string // this also includes dirs
1097 for _, f := range a.filesInfo {
1098 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001099 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001100 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001101 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001102 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001103 }
Alex Light5098a612018-11-29 17:12:15 -08001104 } else {
1105 readOnlyPaths = append(readOnlyPaths, pathInApex)
1106 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001107 dir := f.installDir
1108 for !android.InList(dir, executablePaths) && dir != "" {
1109 executablePaths = append(executablePaths, dir)
1110 dir, _ = filepath.Split(dir) // move up to the parent
1111 if len(dir) > 0 {
1112 // remove trailing slash
1113 dir = dir[:len(dir)-1]
1114 }
Alex Light5098a612018-11-29 17:12:15 -08001115 }
1116 }
1117 sort.Strings(readOnlyPaths)
1118 sort.Strings(executablePaths)
1119 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1120 ctx.Build(pctx, android.BuildParams{
1121 Rule: generateFsConfig,
1122 Output: cannedFsConfig,
1123 Description: "generate fs config",
1124 Args: map[string]string{
1125 "ro_paths": strings.Join(readOnlyPaths, " "),
1126 "exec_paths": strings.Join(executablePaths, " "),
1127 },
1128 })
1129
1130 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1131 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1132 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1133 if !fileContextsOptionalPath.Valid() {
1134 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1135 return
1136 }
1137 fileContexts := fileContextsOptionalPath.Path()
1138
Jiyong Park835d82b2018-12-27 16:04:18 +09001139 optFlags := []string{}
1140
Alex Light5098a612018-11-29 17:12:15 -08001141 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001142 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1143 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001144
Jiyong Park7f67f482019-01-05 12:57:48 +09001145 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1146 if overridden {
1147 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1148 }
1149
Jiyong Park40e26a22019-02-08 02:53:06 +09001150 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001151 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001152 implicitInputs = append(implicitInputs, androidManifestFile)
1153 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1154 }
1155
Jiyong Park71b519d2019-04-18 17:25:49 +09001156 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1157 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1158 ctx.Config().UnbundledBuild() &&
1159 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1160 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1161 apiFingerprint := java.ApiFingerprintPath(ctx)
1162 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1163 implicitInputs = append(implicitInputs, apiFingerprint)
1164 }
1165 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1166
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001167 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1168 if noticeFile.Valid() {
1169 // If there's a NOTICE file, embed it as an asset file in the APEX.
1170 implicitInputs = append(implicitInputs, noticeFile.Path())
1171 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1172 }
1173
Alex Light5098a612018-11-29 17:12:15 -08001174 ctx.Build(pctx, android.BuildParams{
1175 Rule: apexRule,
1176 Implicits: implicitInputs,
1177 Output: unsignedOutputFile,
1178 Description: "apex (" + apexType.name() + ")",
1179 Args: map[string]string{
1180 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1181 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1182 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001183 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001184 "file_contexts": fileContexts.String(),
1185 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001186 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001187 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001188 },
1189 })
1190
1191 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1192 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1193 a.bundleModuleFile = bundleModuleFile
1194
1195 ctx.Build(pctx, android.BuildParams{
1196 Rule: apexProtoConvertRule,
1197 Input: unsignedOutputFile,
1198 Output: apexProtoFile,
1199 Description: "apex proto convert",
1200 })
1201
1202 ctx.Build(pctx, android.BuildParams{
1203 Rule: apexBundleRule,
1204 Input: apexProtoFile,
1205 Output: a.bundleModuleFile,
1206 Description: "apex bundle module",
1207 Args: map[string]string{
1208 "abi": strings.Join(abis, "."),
1209 },
1210 })
1211 } else {
1212 ctx.Build(pctx, android.BuildParams{
1213 Rule: zipApexRule,
1214 Implicits: implicitInputs,
1215 Output: unsignedOutputFile,
1216 Description: "apex (" + apexType.name() + ")",
1217 Args: map[string]string{
1218 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1219 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1220 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001221 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001222 },
1223 })
Colin Crossa4925902018-11-16 11:36:28 -08001224 }
Colin Crossa4925902018-11-16 11:36:28 -08001225
Alex Light5098a612018-11-29 17:12:15 -08001226 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001227 ctx.Build(pctx, android.BuildParams{
1228 Rule: java.Signapk,
1229 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001230 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001231 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001232 Implicits: []android.Path{
1233 a.container_certificate_file,
1234 a.container_private_key_file,
1235 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001236 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001237 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001238 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001239 },
1240 })
Alex Light5098a612018-11-29 17:12:15 -08001241
1242 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001243 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001244 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001245 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001246}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001247
Jiyong Park8fd61922018-11-08 02:50:25 +09001248func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001249 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001250 // 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 +09001251 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001252 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001253
Jiyong Park42cca6c2019-04-01 11:15:50 +09001254 // rename to apex_pubkey
1255 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1256 ctx.Build(pctx, android.BuildParams{
1257 Rule: android.Cp,
1258 Input: a.public_key_file,
1259 Output: copiedPubkey,
1260 })
1261 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1262
Jiyong Park23c52b02019-02-02 13:13:47 +09001263 if ctx.Config().FlattenApex() {
1264 for _, fi := range a.filesInfo {
1265 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001266 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1267 for _, sym := range fi.symlinks {
1268 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1269 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001270 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001271 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001272 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001273}
1274
1275func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001276 if a.properties.HideFromMake {
1277 return android.AndroidMkData{
1278 Disabled: true,
1279 }
1280 }
Alex Light5098a612018-11-29 17:12:15 -08001281 writers := []android.AndroidMkData{}
1282 if a.apexTypes.image() {
1283 writers = append(writers, a.androidMkForType(imageApex))
1284 }
1285 if a.apexTypes.zip() {
1286 writers = append(writers, a.androidMkForType(zipApex))
1287 }
1288 return android.AndroidMkData{
1289 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1290 for _, data := range writers {
1291 data.Custom(w, name, prefix, moduleDir, data)
1292 }
1293 }}
1294}
1295
Alex Lightf1801bc2019-02-13 11:10:07 -08001296func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001297 moduleNames := []string{}
1298
1299 for _, fi := range a.filesInfo {
1300 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1301 continue
1302 }
1303 if !android.InList(fi.moduleName, moduleNames) {
1304 moduleNames = append(moduleNames, fi.moduleName)
1305 }
1306 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1307 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1308 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001309 // /apex/<name>/{lib|framework|...}
1310 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1311 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001312 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001313 // /system/apex/<name>/{lib|framework|...}
1314 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1315 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001316 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001317 if len(fi.symlinks) > 0 {
1318 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1319 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001320
1321 if fi.module != nil && fi.module.NoticeFile().Valid() {
1322 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1323 }
Jiyong Park94427262019-02-05 23:18:47 +09001324 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001325 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001326 }
1327 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1328 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1329 if fi.module != nil {
1330 archStr := fi.module.Target().Arch.ArchType.String()
1331 host := false
1332 switch fi.module.Target().Os.Class {
1333 case android.Host:
1334 if archStr != "common" {
1335 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1336 }
1337 host = true
1338 case android.HostCross:
1339 if archStr != "common" {
1340 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1341 }
1342 host = true
1343 case android.Device:
1344 if archStr != "common" {
1345 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1346 }
1347 }
1348 if host {
1349 makeOs := fi.module.Target().Os.String()
1350 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1351 makeOs = "linux"
1352 }
1353 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1354 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1355 }
1356 }
1357 if fi.class == javaSharedLib {
1358 javaModule := fi.module.(*java.Library)
1359 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1360 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1361 // we will have foo.jar.jar
1362 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1363 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1364 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1365 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1366 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1367 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1368 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1369 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001370 if cc, ok := fi.module.(*cc.Module); ok {
1371 if cc.UnstrippedOutputFile() != nil {
1372 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1373 }
1374 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001375 if cc.CoverageOutputFile().Valid() {
1376 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1377 }
Jiyong Park94427262019-02-05 23:18:47 +09001378 }
1379 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1380 } else {
1381 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1382 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1383 }
1384 }
1385 return moduleNames
1386}
1387
Alex Light5098a612018-11-29 17:12:15 -08001388func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001389 return android.AndroidMkData{
1390 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1391 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001392 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001393 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001394 }
1395
Jiyong Park719b4462019-01-13 00:39:51 +09001396 if a.flattened && apexType.image() {
1397 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001398 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1399 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1400 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001401 if len(moduleNames) > 0 {
1402 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1403 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001404 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001405 } else {
Alex Light5098a612018-11-29 17:12:15 -08001406 // zip-apex is the less common type so have the name refer to the image-apex
1407 // only and use {name}.zip if you want the zip-apex
1408 if apexType == zipApex && a.apexTypes == both {
1409 name = name + ".zip"
1410 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001411 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1412 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1413 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1414 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001415 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001416 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001417 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001418 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001419 if len(moduleNames) > 0 {
1420 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1421 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001422 if len(a.externalDeps) > 0 {
1423 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1424 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001425 if a.prebuiltFileToDelete != "" {
1426 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1427 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1428 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001429 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001430
Alex Light5098a612018-11-29 17:12:15 -08001431 if apexType == imageApex {
1432 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1433 }
Jiyong Park719b4462019-01-13 00:39:51 +09001434 }
1435 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001436}
1437
Alex Light0851b882019-02-07 13:20:53 -08001438func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001439 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001440}
1441
1442func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001443 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001444}
1445
1446func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001447 module := &apexBundle{
1448 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001449 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001450 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001451 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001452 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001453 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001454 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1455 })
Alex Light5098a612018-11-29 17:12:15 -08001456 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001457 android.InitDefaultableModule(module)
1458 return module
1459}
Jiyong Park30ca9372019-02-07 16:27:23 +09001460
1461//
1462// Defaults
1463//
1464type Defaults struct {
1465 android.ModuleBase
1466 android.DefaultsModuleBase
1467}
1468
Jiyong Park30ca9372019-02-07 16:27:23 +09001469func defaultsFactory() android.Module {
1470 return DefaultsFactory()
1471}
1472
1473func DefaultsFactory(props ...interface{}) android.Module {
1474 module := &Defaults{}
1475
1476 module.AddProperties(props...)
1477 module.AddProperties(
1478 &apexBundleProperties{},
1479 &apexTargetBundleProperties{},
1480 )
1481
1482 android.InitDefaultsModule(module)
1483 return module
1484}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001485
1486//
1487// Prebuilt APEX
1488//
1489type Prebuilt struct {
1490 android.ModuleBase
1491 prebuilt android.Prebuilt
1492
1493 properties PrebuiltProperties
1494
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001495 inputApex android.Path
1496 installDir android.OutputPath
1497 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001498 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001499}
1500
1501type PrebuiltProperties struct {
1502 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001503 Source string `blueprint:"mutated"`
1504 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001505
1506 Src *string
1507 Arch struct {
1508 Arm struct {
1509 Src *string
1510 }
1511 Arm64 struct {
1512 Src *string
1513 }
1514 X86 struct {
1515 Src *string
1516 }
1517 X86_64 struct {
1518 Src *string
1519 }
1520 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001521
1522 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001523 // Optional name for the installed apex. If unspecified, name of the
1524 // module is used as the file name
1525 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001526
1527 // Names of modules to be overridden. Listed modules can only be other binaries
1528 // (in Make or Soong).
1529 // This does not completely prevent installation of the overridden binaries, but if both
1530 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1531 // from PRODUCT_PACKAGES.
1532 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001533}
1534
1535func (p *Prebuilt) installable() bool {
1536 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001537}
1538
1539func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001540 // If the device is configured to use flattened APEX, force disable the prebuilt because
1541 // the prebuilt is a non-flattened one.
1542 forceDisable := ctx.Config().FlattenApex()
1543
1544 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1545 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001546 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001547
Kun Niu10c9f832019-07-29 16:28:57 -07001548 // Force disable the prebuilts when coverage is enabled.
1549 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1550 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1551
Jiyong Park50b81e52019-07-11 11:24:41 +09001552 // b/137216042 don't use prebuilts when address sanitizer is on
1553 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1554 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1555
1556 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001557 p.properties.ForceDisable = true
1558 return
1559 }
1560
Jiyong Parkc95714e2019-03-29 14:23:10 +09001561 // This is called before prebuilt_select and prebuilt_postdeps mutators
1562 // The mutators requires that src to be set correctly for each arch so that
1563 // arch variants are disabled when src is not provided for the arch.
1564 if len(ctx.MultiTargets()) != 1 {
1565 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1566 return
1567 }
1568 var src string
1569 switch ctx.MultiTargets()[0].Arch.ArchType {
1570 case android.Arm:
1571 src = String(p.properties.Arch.Arm.Src)
1572 case android.Arm64:
1573 src = String(p.properties.Arch.Arm64.Src)
1574 case android.X86:
1575 src = String(p.properties.Arch.X86.Src)
1576 case android.X86_64:
1577 src = String(p.properties.Arch.X86_64.Src)
1578 default:
1579 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1580 return
1581 }
1582 if src == "" {
1583 src = String(p.properties.Src)
1584 }
1585 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001586}
1587
Jiyong Park03b68dd2019-07-26 23:20:40 +09001588func (p *Prebuilt) isForceDisabled() bool {
1589 return p.properties.ForceDisable
1590}
1591
Colin Cross41955e82019-05-29 14:40:35 -07001592func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1593 switch tag {
1594 case "":
1595 return android.Paths{p.outputApex}, nil
1596 default:
1597 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1598 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001599}
1600
Jiyong Park4d277042019-04-23 18:00:10 +09001601func (p *Prebuilt) InstallFilename() string {
1602 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1603}
1604
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001605func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001606 if p.properties.ForceDisable {
1607 return
1608 }
1609
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001610 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001611 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001612 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001613 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001614 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1615 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1616 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001617 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1618 ctx.Build(pctx, android.BuildParams{
1619 Rule: android.Cp,
1620 Input: p.inputApex,
1621 Output: p.outputApex,
1622 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001623 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001624 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001625 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001626}
1627
1628func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1629 return &p.prebuilt
1630}
1631
1632func (p *Prebuilt) Name() string {
1633 return p.prebuilt.Name(p.ModuleBase.Name())
1634}
1635
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001636func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1637 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001638 Class: "ETC",
1639 OutputFile: android.OptionalPathForPath(p.inputApex),
1640 Include: "$(BUILD_PREBUILT)",
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001641 AddCustomEntries: func(name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
1642 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1643 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1644 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1645 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001646 },
1647 }
1648}
1649
1650// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1651func PrebuiltFactory() android.Module {
1652 module := &Prebuilt{}
1653 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001654 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001655 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001656 return module
1657}