blob: e4fad83e97de1e13d0edf1993f769ed1cb48f855 [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
296 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
297 Provide_cpp_shared_libs *bool
298
299 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
300 Uses []string
Alex Light9670d332019-01-29 18:07:33 -0800301}
302
303type apexTargetBundleProperties struct {
304 Target struct {
305 // Multilib properties only for android.
306 Android struct {
307 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900308 }
Alex Light9670d332019-01-29 18:07:33 -0800309 // Multilib properties only for host.
310 Host 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 linux_bionic.
314 Linux_bionic 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_glibc.
318 Linux_glibc struct {
319 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900320 }
321 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900322}
323
Jiyong Park8fd61922018-11-08 02:50:25 +0900324type apexFileClass int
325
326const (
327 etc apexFileClass = iota
328 nativeSharedLib
329 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900330 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800331 pyBinary
332 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900333 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100334 nativeTest
Jiyong Park8fd61922018-11-08 02:50:25 +0900335)
336
Alex Light5098a612018-11-29 17:12:15 -0800337type apexPackaging int
338
339const (
340 imageApex apexPackaging = iota
341 zipApex
342 both
343)
344
345func (a apexPackaging) image() bool {
346 switch a {
347 case imageApex, both:
348 return true
349 }
350 return false
351}
352
353func (a apexPackaging) zip() bool {
354 switch a {
355 case zipApex, both:
356 return true
357 }
358 return false
359}
360
361func (a apexPackaging) suffix() string {
362 switch a {
363 case imageApex:
364 return imageApexSuffix
365 case zipApex:
366 return zipApexSuffix
367 case both:
368 panic(fmt.Errorf("must be either zip or image"))
369 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100370 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800371 }
372}
373
374func (a apexPackaging) name() string {
375 switch a {
376 case imageApex:
377 return imageApexType
378 case zipApex:
379 return zipApexType
380 case both:
381 panic(fmt.Errorf("must be either zip or image"))
382 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100383 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800384 }
385}
386
Jiyong Park8fd61922018-11-08 02:50:25 +0900387func (class apexFileClass) NameInMake() string {
388 switch class {
389 case etc:
390 return "ETC"
391 case nativeSharedLib:
392 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800393 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900394 return "EXECUTABLES"
395 case javaSharedLib:
396 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100397 case nativeTest:
398 return "NATIVE_TESTS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900399 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100400 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900401 }
402}
403
404type apexFile struct {
405 builtFile android.Path
406 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900407 installDir string
408 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900409 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800410 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900411}
412
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900413type apexBundle struct {
414 android.ModuleBase
415 android.DefaultableModuleBase
416
Alex Light9670d332019-01-29 18:07:33 -0800417 properties apexBundleProperties
418 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900419
Alex Light5098a612018-11-29 17:12:15 -0800420 apexTypes apexPackaging
421
Colin Crossa4925902018-11-16 11:36:28 -0800422 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800423 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800424 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900425
Jiyong Park03b68dd2019-07-26 23:20:40 +0900426 prebuiltFileToDelete string
427
Jiyong Park42cca6c2019-04-01 11:15:50 +0900428 public_key_file android.Path
429 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900430
431 container_certificate_file android.Path
432 container_private_key_file android.Path
433
Jiyong Park8fd61922018-11-08 02:50:25 +0900434 // list of files to be included in this apex
435 filesInfo []apexFile
436
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900437 // list of module names that this APEX is depending on
438 externalDeps []string
439
Jiyong Park8fd61922018-11-08 02:50:25 +0900440 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800441
442 testApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900443
444 // intermediate path for apex_manifest.json
445 manifestOut android.WritablePath
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900446}
447
Jiyong Park397e55e2018-10-24 21:09:55 +0900448func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100449 native_shared_libs []string, binaries []string, tests []string,
450 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900451 // Use *FarVariation* to be able to depend on modules having
452 // conflicting variations with this module. This is required since
453 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
454 // for native shared libs.
455 ctx.AddFarVariationDependencies([]blueprint.Variation{
456 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900457 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900458 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900459 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900460 }, sharedLibTag, native_shared_libs...)
461
462 ctx.AddFarVariationDependencies([]blueprint.Variation{
463 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900464 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900465 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100466
467 ctx.AddFarVariationDependencies([]blueprint.Variation{
468 {Mutator: "arch", Variation: arch},
469 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100470 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100471 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900472}
473
Alex Light9670d332019-01-29 18:07:33 -0800474func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
475 if ctx.Os().Class == android.Device {
476 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
477 } else {
478 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
479 if ctx.Os().Bionic() {
480 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
481 } else {
482 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
483 }
484 }
485}
486
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900487func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800488
Jiyong Park397e55e2018-10-24 21:09:55 +0900489 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900490 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800491
492 a.combineProperties(ctx)
493
Jiyong Park397e55e2018-10-24 21:09:55 +0900494 has32BitTarget := false
495 for _, target := range targets {
496 if target.Arch.ArchType.Multilib == "lib32" {
497 has32BitTarget = true
498 }
499 }
500 for i, target := range targets {
501 // When multilib.* is omitted for native_shared_libs, it implies
502 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900503 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900504 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900505 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900506 {Mutator: "link", Variation: "shared"},
507 }, sharedLibTag, a.properties.Native_shared_libs...)
508
Roland Levillain630846d2019-06-26 12:48:34 +0100509 // When multilib.* is omitted for tests, it implies
510 // multilib.both.
511 ctx.AddFarVariationDependencies([]blueprint.Variation{
512 {Mutator: "arch", Variation: target.String()},
513 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100514 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100515 }, testTag, a.properties.Tests...)
516
Jiyong Park397e55e2018-10-24 21:09:55 +0900517 // Add native modules targetting both ABIs
518 addDependenciesForNativeModules(ctx,
519 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100520 a.properties.Multilib.Both.Binaries,
521 a.properties.Multilib.Both.Tests,
522 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900523 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900524
Alex Light3d673592019-01-18 14:37:31 -0800525 isPrimaryAbi := i == 0
526 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900527 // When multilib.* is omitted for binaries, it implies
528 // multilib.first.
529 ctx.AddFarVariationDependencies([]blueprint.Variation{
530 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900531 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900532 }, executableTag, a.properties.Binaries...)
533
534 // Add native modules targetting the first ABI
535 addDependenciesForNativeModules(ctx,
536 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100537 a.properties.Multilib.First.Binaries,
538 a.properties.Multilib.First.Tests,
539 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900540 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800541
542 // When multilib.* is omitted for prebuilts, it implies multilib.first.
543 ctx.AddFarVariationDependencies([]blueprint.Variation{
544 {Mutator: "arch", Variation: target.String()},
545 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900546 }
547
548 switch target.Arch.ArchType.Multilib {
549 case "lib32":
550 // Add native modules targetting 32-bit ABI
551 addDependenciesForNativeModules(ctx,
552 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100553 a.properties.Multilib.Lib32.Binaries,
554 a.properties.Multilib.Lib32.Tests,
555 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900556 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900557
558 addDependenciesForNativeModules(ctx,
559 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100560 a.properties.Multilib.Prefer32.Binaries,
561 a.properties.Multilib.Prefer32.Tests,
562 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900563 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900564 case "lib64":
565 // Add native modules targetting 64-bit ABI
566 addDependenciesForNativeModules(ctx,
567 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100568 a.properties.Multilib.Lib64.Binaries,
569 a.properties.Multilib.Lib64.Tests,
570 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900571 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900572
573 if !has32BitTarget {
574 addDependenciesForNativeModules(ctx,
575 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100576 a.properties.Multilib.Prefer32.Binaries,
577 a.properties.Multilib.Prefer32.Tests,
578 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900579 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900580 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700581
582 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
583 for _, sanitizer := range ctx.Config().SanitizeDevice() {
584 if sanitizer == "hwaddress" {
585 addDependenciesForNativeModules(ctx,
586 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100587 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700588 break
589 }
590 }
591 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900592 }
593
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900594 }
595
Jiyong Parkff1458f2018-10-12 21:49:38 +0900596 ctx.AddFarVariationDependencies([]blueprint.Variation{
597 {Mutator: "arch", Variation: "android_common"},
598 }, javaLibTag, a.properties.Java_libs...)
599
Jiyong Park23c52b02019-02-02 13:13:47 +0900600 if String(a.properties.Key) == "" {
601 ctx.ModuleErrorf("key is missing")
602 return
603 }
604 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900605
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900606 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900607 if cert != "" {
608 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900609 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900610}
611
Colin Cross0ea8ba82019-06-06 14:33:29 -0700612func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900613 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
614 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000615 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900616 }
617 return String(a.properties.Certificate)
618}
619
Colin Cross41955e82019-05-29 14:40:35 -0700620func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
621 switch tag {
622 case "":
623 if file, ok := a.outputFiles[imageApex]; ok {
624 return android.Paths{file}, nil
625 } else {
626 return nil, nil
627 }
628 default:
629 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900630 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900631}
632
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900633func (a *apexBundle) installable() bool {
634 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
635}
636
Jiyong Park7c1dc612019-01-05 11:15:24 +0900637func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
638 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900639 return "vendor"
640 } else {
641 return "core"
642 }
643}
644
Jiyong Parkf97782b2019-02-13 20:28:58 +0900645func (a *apexBundle) EnableSanitizer(sanitizerName string) {
646 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
647 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
648 }
649}
650
Jiyong Park388ef3f2019-01-28 19:47:32 +0900651func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900652 if android.InList(sanitizerName, a.properties.SanitizerNames) {
653 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900654 }
655
656 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900657 globalSanitizerNames := []string{}
658 if a.Host() {
659 globalSanitizerNames = ctx.Config().SanitizeHost()
660 } else {
661 arches := ctx.Config().SanitizeDeviceArch()
662 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
663 globalSanitizerNames = ctx.Config().SanitizeDevice()
664 }
665 }
666 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900667}
668
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800669func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900670 // Decide the APEX-local directory by the multilib of the library
671 // In the future, we may query this to the module.
672 switch cc.Arch().ArchType.Multilib {
673 case "lib32":
674 dirInApex = "lib"
675 case "lib64":
676 dirInApex = "lib64"
677 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900678 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200679 if !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900680 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200681 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
682 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900683 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800684 if handleSpecialLibs {
685 switch cc.Name() {
686 case "libc", "libm", "libdl":
687 // Special case for bionic libs. This is to prevent the bionic libs
688 // from being included in the search path /apex/com.android.apex/lib.
689 // This exclusion is required because bionic libs in the runtime APEX
690 // are available via the legacy paths /system/lib/libc.so, etc. By the
691 // init process, the bionic libs in the APEX are bind-mounted to the
692 // legacy paths and thus will be loaded into the default linker namespace.
693 // If the bionic libs are directly in /apex/com.android.apex/lib then
694 // the same libs will be again loaded to the runtime linker namespace,
695 // which will result double loading of bionic libs that isn't supported.
696 dirInApex = filepath.Join(dirInApex, "bionic")
697 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900698 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900699
700 fileToCopy = cc.OutputFile().Path()
701 return
702}
703
704func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900705 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200706 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900707 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200708 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
709 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900710 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900711 fileToCopy = cc.OutputFile().Path()
712 return
713}
714
Alex Light778127a2019-02-27 14:19:50 -0800715func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
716 dirInApex = "bin"
717 fileToCopy = py.HostToolPath().Path()
718 return
719}
720func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
721 dirInApex = "bin"
722 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
723 if err != nil {
724 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
725 return
726 }
727 fileToCopy = android.PathForOutput(ctx, s)
728 return
729}
730
Jiyong Park04480cf2019-02-06 00:16:29 +0900731func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
732 dirInApex = filepath.Join("bin", sh.SubDir())
733 fileToCopy = sh.OutputFile()
734 return
735}
736
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900737func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
738 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900739 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900740 return
741}
742
743func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
744 dirInApex = filepath.Join("etc", prebuilt.SubDir())
745 fileToCopy = prebuilt.OutputFile()
746 return
747}
748
749func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900750 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900751
Alex Light5098a612018-11-29 17:12:15 -0800752 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
753 a.apexTypes = imageApex
754 } else if *a.properties.Payload_type == "zip" {
755 a.apexTypes = zipApex
756 } else if *a.properties.Payload_type == "both" {
757 a.apexTypes = both
758 } else {
759 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
760 return
761 }
762
Roland Levillain630846d2019-06-26 12:48:34 +0100763 if len(a.properties.Tests) > 0 && !a.testApex {
764 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
765 return
766 }
767
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800768 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
769
Jooyung Hane1633032019-08-01 17:41:43 +0900770 // native lib dependencies
771 var provideNativeLibs []string
772 var requireNativeLibs []string
773
Jooyung Han5c998b92019-06-27 11:30:33 +0900774 // Check if "uses" requirements are met with dependent apexBundles
775 var providedNativeSharedLibs []string
776 useVendor := proptools.Bool(a.properties.Use_vendor)
777 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
778 if ctx.OtherModuleDependencyTag(m) != usesTag {
779 return
780 }
781 otherName := ctx.OtherModuleName(m)
782 other, ok := m.(*apexBundle)
783 if !ok {
784 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
785 return
786 }
787 if proptools.Bool(other.properties.Use_vendor) != useVendor {
788 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
789 return
790 }
791 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
792 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
793 return
794 }
795 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
796 })
797
Alex Light778127a2019-02-27 14:19:50 -0800798 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100799 depTag := ctx.OtherModuleDependencyTag(child)
800 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900801 if _, ok := parent.(*apexBundle); ok {
802 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900803 switch depTag {
804 case sharedLibTag:
805 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900806 if cc.HasStubsVariants() {
807 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
808 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800809 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900810 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900811 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900812 } else {
813 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900814 }
815 case executableTag:
816 if cc, ok := child.(*cc.Module); ok {
817 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900818 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900819 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900820 } else if sh, ok := child.(*android.ShBinary); ok {
821 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
822 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800823 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
824 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
825 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
826 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
827 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
828 // NB: Since go binaries are static we don't need the module for anything here, which is
829 // good since the go tool is a blueprint.Module not an android.Module like we would
830 // normally use.
831 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900832 } else {
Alex Light778127a2019-02-27 14:19:50 -0800833 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 +0900834 }
835 case javaLibTag:
836 if java, ok := child.(*java.Library); ok {
837 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900838 if fileToCopy == nil {
839 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
840 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900841 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900842 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900843 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900844 } else {
845 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900846 }
847 case prebuiltTag:
848 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
849 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900850 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900851 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900852 } else {
853 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
854 }
Roland Levillain630846d2019-06-26 12:48:34 +0100855 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100856 if ccTest, ok := child.(*cc.Module); ok {
857 if ccTest.IsTestPerSrcAllTestsVariation() {
858 // Multiple-output test module (where `test_per_src: true`).
859 //
860 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
861 // We do not add this variation to `filesInfo`, as it has no output;
862 // however, we do add the other variations of this module as indirect
863 // dependencies (see below).
864 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100865 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100866 // Single-output test module (where `test_per_src: false`).
867 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
868 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100869 }
Roland Levillain630846d2019-06-26 12:48:34 +0100870 return true
871 } else {
872 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
873 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900874 case keyTag:
875 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900876 a.private_key_file = key.private_key_file
877 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900878 return false
879 } else {
880 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900881 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900882 case certificateTag:
883 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900884 a.container_certificate_file = dep.Certificate.Pem
885 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900886 return false
887 } else {
888 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
889 }
Jiyong Park03b68dd2019-07-26 23:20:40 +0900890 case android.PrebuiltDepTag:
891 // If the prebuilt is force disabled, remember to delete the prebuilt file
892 // that might have been installed in the previous builds
893 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
894 a.prebuiltFileToDelete = prebuilt.InstallFilename()
895 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900896 }
897 } else {
898 // indirect dependencies
899 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +0100900 // We cannot use a switch statement on `depTag` here as the checked
901 // tags used below are private (e.g. `cc.sharedDepTag`).
902 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
903 if cc, ok := child.(*cc.Module); ok {
904 if android.InList(cc.Name(), providedNativeSharedLibs) {
905 // If we're using a shared library which is provided from other APEX,
906 // don't include it in this APEX
907 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900908 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100909 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
910 // If the dependency is a stubs lib, don't include it in this APEX,
911 // but make sure that the lib is installed on the device.
912 // In case no APEX is having the lib, the lib is installed to the system
913 // partition.
914 //
915 // Always include if we are a host-apex however since those won't have any
916 // system libraries.
917 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
918 a.externalDeps = append(a.externalDeps, cc.Name())
919 }
Jooyung Hane1633032019-08-01 17:41:43 +0900920 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +0100921 // Don't track further
922 return false
923 }
924 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
925 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
926 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100928 } else if cc.IsTestPerSrcDepTag(depTag) {
929 if cc, ok := child.(*cc.Module); ok {
930 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
931 // Handle modules created as `test_per_src` variations of a single test module:
932 // use the name of the generated test binary (`fileToCopy`) instead of the name
933 // of the original test module (`depName`, shared by all `test_per_src`
934 // variations of that module).
935 moduleName := filepath.Base(fileToCopy.String())
936 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
937 return true
938 }
939 } else {
940 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900941 }
942 }
943 }
944 return false
945 })
946
Jiyong Park9335a262018-12-24 11:31:58 +0900947 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900948 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900949 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
950 return
951 }
952
Jiyong Park8fd61922018-11-08 02:50:25 +0900953 // remove duplicates in filesInfo
954 removeDup := func(filesInfo []apexFile) []apexFile {
955 encountered := make(map[android.Path]bool)
956 result := []apexFile{}
957 for _, f := range filesInfo {
958 if !encountered[f.builtFile] {
959 encountered[f.builtFile] = true
960 result = append(result, f)
961 }
962 }
963 return result
964 }
965 filesInfo = removeDup(filesInfo)
966
967 // to have consistent build rules
968 sort.Slice(filesInfo, func(i, j int) bool {
969 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
970 })
971
972 // prepend the name of this APEX to the module names. These names will be the names of
973 // modules that will be defined if the APEX is flattened.
974 for i := range filesInfo {
975 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
976 }
977
Jiyong Park8fd61922018-11-08 02:50:25 +0900978 a.installDir = android.PathForModuleInstall(ctx, "apex")
979 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800980
Jooyung Hane1633032019-08-01 17:41:43 +0900981 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
982 // put dependency({provide|require}NativeLibs) in apex_manifest.json
983 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
984 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
985 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
986 ctx.Build(pctx, android.BuildParams{
987 Rule: injectApexDependency,
988 Input: manifestSrc,
989 Output: a.manifestOut,
990 Args: map[string]string{
991 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
992 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
993 },
994 })
995
Alex Light5098a612018-11-29 17:12:15 -0800996 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900997 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800998 }
999 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001000 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001001 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001002 // in other modules. It is in AndroidMk where the selection of flattened
1003 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001004 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001005 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001006 }
1007}
1008
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001009func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001010 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001011 for _, f := range a.filesInfo {
1012 if f.module != nil {
1013 notice := f.module.NoticeFile()
1014 if notice.Valid() {
1015 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001016 }
1017 }
1018 }
1019 // append the notice file specified in the apex module itself
1020 if a.NoticeFile().Valid() {
1021 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001022 }
1023
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001024 if len(noticeFiles) == 0 {
1025 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001026 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001027
Jaewoong Jung98772792019-07-01 17:15:13 -07001028 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001029}
1030
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001031func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001032 cert := String(a.properties.Certificate)
1033 if cert != "" && android.SrcIsModule(cert) == "" {
1034 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001035 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1036 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001037 } else if cert == "" {
1038 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001039 a.container_certificate_file = pem
1040 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001041 }
1042
Alex Light5098a612018-11-29 17:12:15 -08001043 var abis []string
1044 for _, target := range ctx.MultiTargets() {
1045 if len(target.Arch.Abi) > 0 {
1046 abis = append(abis, target.Arch.Abi[0])
1047 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001048 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001049
Alex Light5098a612018-11-29 17:12:15 -08001050 abis = android.FirstUniqueStrings(abis)
1051
1052 suffix := apexType.suffix()
1053 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001054
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001055 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001056 for _, f := range a.filesInfo {
1057 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001058 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001059
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001060 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001061 for i, src := range filesToCopy {
1062 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -08001063 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001064 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1065 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001066 for _, sym := range a.filesInfo[i].symlinks {
1067 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1068 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1069 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001070 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001071 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001072 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001073
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001074 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1075 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001076
Alex Light5098a612018-11-29 17:12:15 -08001077 if apexType.image() {
1078 // files and dirs that will be created in APEX
1079 var readOnlyPaths []string
1080 var executablePaths []string // this also includes dirs
1081 for _, f := range a.filesInfo {
1082 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001083 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001084 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001085 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001086 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001087 }
Alex Light5098a612018-11-29 17:12:15 -08001088 } else {
1089 readOnlyPaths = append(readOnlyPaths, pathInApex)
1090 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001091 dir := f.installDir
1092 for !android.InList(dir, executablePaths) && dir != "" {
1093 executablePaths = append(executablePaths, dir)
1094 dir, _ = filepath.Split(dir) // move up to the parent
1095 if len(dir) > 0 {
1096 // remove trailing slash
1097 dir = dir[:len(dir)-1]
1098 }
Alex Light5098a612018-11-29 17:12:15 -08001099 }
1100 }
1101 sort.Strings(readOnlyPaths)
1102 sort.Strings(executablePaths)
1103 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1104 ctx.Build(pctx, android.BuildParams{
1105 Rule: generateFsConfig,
1106 Output: cannedFsConfig,
1107 Description: "generate fs config",
1108 Args: map[string]string{
1109 "ro_paths": strings.Join(readOnlyPaths, " "),
1110 "exec_paths": strings.Join(executablePaths, " "),
1111 },
1112 })
1113
1114 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1115 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1116 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1117 if !fileContextsOptionalPath.Valid() {
1118 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1119 return
1120 }
1121 fileContexts := fileContextsOptionalPath.Path()
1122
Jiyong Park835d82b2018-12-27 16:04:18 +09001123 optFlags := []string{}
1124
Alex Light5098a612018-11-29 17:12:15 -08001125 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001126 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1127 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001128
Jiyong Park7f67f482019-01-05 12:57:48 +09001129 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1130 if overridden {
1131 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1132 }
1133
Jiyong Park40e26a22019-02-08 02:53:06 +09001134 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001135 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001136 implicitInputs = append(implicitInputs, androidManifestFile)
1137 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1138 }
1139
Jiyong Park71b519d2019-04-18 17:25:49 +09001140 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1141 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1142 ctx.Config().UnbundledBuild() &&
1143 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1144 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1145 apiFingerprint := java.ApiFingerprintPath(ctx)
1146 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1147 implicitInputs = append(implicitInputs, apiFingerprint)
1148 }
1149 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1150
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001151 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1152 if noticeFile.Valid() {
1153 // If there's a NOTICE file, embed it as an asset file in the APEX.
1154 implicitInputs = append(implicitInputs, noticeFile.Path())
1155 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1156 }
1157
Alex Light5098a612018-11-29 17:12:15 -08001158 ctx.Build(pctx, android.BuildParams{
1159 Rule: apexRule,
1160 Implicits: implicitInputs,
1161 Output: unsignedOutputFile,
1162 Description: "apex (" + apexType.name() + ")",
1163 Args: map[string]string{
1164 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1165 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1166 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001167 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001168 "file_contexts": fileContexts.String(),
1169 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001170 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001171 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001172 },
1173 })
1174
1175 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1176 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1177 a.bundleModuleFile = bundleModuleFile
1178
1179 ctx.Build(pctx, android.BuildParams{
1180 Rule: apexProtoConvertRule,
1181 Input: unsignedOutputFile,
1182 Output: apexProtoFile,
1183 Description: "apex proto convert",
1184 })
1185
1186 ctx.Build(pctx, android.BuildParams{
1187 Rule: apexBundleRule,
1188 Input: apexProtoFile,
1189 Output: a.bundleModuleFile,
1190 Description: "apex bundle module",
1191 Args: map[string]string{
1192 "abi": strings.Join(abis, "."),
1193 },
1194 })
1195 } else {
1196 ctx.Build(pctx, android.BuildParams{
1197 Rule: zipApexRule,
1198 Implicits: implicitInputs,
1199 Output: unsignedOutputFile,
1200 Description: "apex (" + apexType.name() + ")",
1201 Args: map[string]string{
1202 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1203 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1204 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001205 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001206 },
1207 })
Colin Crossa4925902018-11-16 11:36:28 -08001208 }
Colin Crossa4925902018-11-16 11:36:28 -08001209
Alex Light5098a612018-11-29 17:12:15 -08001210 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001211 ctx.Build(pctx, android.BuildParams{
1212 Rule: java.Signapk,
1213 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001214 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001215 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001216 Implicits: []android.Path{
1217 a.container_certificate_file,
1218 a.container_private_key_file,
1219 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001220 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001221 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001222 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001223 },
1224 })
Alex Light5098a612018-11-29 17:12:15 -08001225
1226 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001227 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001228 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001229 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001230}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001231
Jiyong Park8fd61922018-11-08 02:50:25 +09001232func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001233 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001234 // 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 +09001235 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001236 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001237
Jiyong Park42cca6c2019-04-01 11:15:50 +09001238 // rename to apex_pubkey
1239 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1240 ctx.Build(pctx, android.BuildParams{
1241 Rule: android.Cp,
1242 Input: a.public_key_file,
1243 Output: copiedPubkey,
1244 })
1245 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1246
Jiyong Park23c52b02019-02-02 13:13:47 +09001247 if ctx.Config().FlattenApex() {
1248 for _, fi := range a.filesInfo {
1249 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001250 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1251 for _, sym := range fi.symlinks {
1252 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1253 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001254 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001255 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001256 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001257}
1258
1259func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -08001260 writers := []android.AndroidMkData{}
1261 if a.apexTypes.image() {
1262 writers = append(writers, a.androidMkForType(imageApex))
1263 }
1264 if a.apexTypes.zip() {
1265 writers = append(writers, a.androidMkForType(zipApex))
1266 }
1267 return android.AndroidMkData{
1268 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1269 for _, data := range writers {
1270 data.Custom(w, name, prefix, moduleDir, data)
1271 }
1272 }}
1273}
1274
Alex Lightf1801bc2019-02-13 11:10:07 -08001275func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001276 moduleNames := []string{}
1277
1278 for _, fi := range a.filesInfo {
1279 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1280 continue
1281 }
1282 if !android.InList(fi.moduleName, moduleNames) {
1283 moduleNames = append(moduleNames, fi.moduleName)
1284 }
1285 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1286 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1287 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001288 // /apex/<name>/{lib|framework|...}
1289 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1290 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001291 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001292 // /system/apex/<name>/{lib|framework|...}
1293 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1294 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001295 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001296 if len(fi.symlinks) > 0 {
1297 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1298 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001299
1300 if fi.module != nil && fi.module.NoticeFile().Valid() {
1301 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1302 }
Jiyong Park94427262019-02-05 23:18:47 +09001303 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001304 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001305 }
1306 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1307 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1308 if fi.module != nil {
1309 archStr := fi.module.Target().Arch.ArchType.String()
1310 host := false
1311 switch fi.module.Target().Os.Class {
1312 case android.Host:
1313 if archStr != "common" {
1314 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1315 }
1316 host = true
1317 case android.HostCross:
1318 if archStr != "common" {
1319 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1320 }
1321 host = true
1322 case android.Device:
1323 if archStr != "common" {
1324 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1325 }
1326 }
1327 if host {
1328 makeOs := fi.module.Target().Os.String()
1329 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1330 makeOs = "linux"
1331 }
1332 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1333 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1334 }
1335 }
1336 if fi.class == javaSharedLib {
1337 javaModule := fi.module.(*java.Library)
1338 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1339 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1340 // we will have foo.jar.jar
1341 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1342 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1343 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1344 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1345 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1346 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1347 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1348 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001349 if cc, ok := fi.module.(*cc.Module); ok {
1350 if cc.UnstrippedOutputFile() != nil {
1351 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1352 }
1353 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Park94427262019-02-05 23:18:47 +09001354 }
1355 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1356 } else {
1357 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1358 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1359 }
1360 }
1361 return moduleNames
1362}
1363
Alex Light5098a612018-11-29 17:12:15 -08001364func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001365 return android.AndroidMkData{
1366 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1367 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001368 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001369 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001370 }
1371
Jiyong Park719b4462019-01-13 00:39:51 +09001372 if a.flattened && apexType.image() {
1373 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001374 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1375 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1376 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001377 if len(moduleNames) > 0 {
1378 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1379 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001380 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001381 } else {
Alex Light5098a612018-11-29 17:12:15 -08001382 // zip-apex is the less common type so have the name refer to the image-apex
1383 // only and use {name}.zip if you want the zip-apex
1384 if apexType == zipApex && a.apexTypes == both {
1385 name = name + ".zip"
1386 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001387 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1388 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1389 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1390 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001391 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001392 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001393 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001394 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001395 if len(moduleNames) > 0 {
1396 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1397 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001398 if len(a.externalDeps) > 0 {
1399 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1400 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001401 if a.prebuiltFileToDelete != "" {
1402 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1403 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1404 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001405 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001406
Alex Light5098a612018-11-29 17:12:15 -08001407 if apexType == imageApex {
1408 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1409 }
Jiyong Park719b4462019-01-13 00:39:51 +09001410 }
1411 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001412}
1413
Alex Light0851b882019-02-07 13:20:53 -08001414func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001415 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001416}
1417
1418func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001419 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001420}
1421
1422func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001423 module := &apexBundle{
1424 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001425 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001426 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001427 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001428 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001429 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001430 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1431 })
Alex Light5098a612018-11-29 17:12:15 -08001432 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001433 android.InitDefaultableModule(module)
1434 return module
1435}
Jiyong Park30ca9372019-02-07 16:27:23 +09001436
1437//
1438// Defaults
1439//
1440type Defaults struct {
1441 android.ModuleBase
1442 android.DefaultsModuleBase
1443}
1444
Jiyong Park30ca9372019-02-07 16:27:23 +09001445func defaultsFactory() android.Module {
1446 return DefaultsFactory()
1447}
1448
1449func DefaultsFactory(props ...interface{}) android.Module {
1450 module := &Defaults{}
1451
1452 module.AddProperties(props...)
1453 module.AddProperties(
1454 &apexBundleProperties{},
1455 &apexTargetBundleProperties{},
1456 )
1457
1458 android.InitDefaultsModule(module)
1459 return module
1460}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001461
1462//
1463// Prebuilt APEX
1464//
1465type Prebuilt struct {
1466 android.ModuleBase
1467 prebuilt android.Prebuilt
1468
1469 properties PrebuiltProperties
1470
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001471 inputApex android.Path
1472 installDir android.OutputPath
1473 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001474 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001475}
1476
1477type PrebuiltProperties struct {
1478 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001479 Source string `blueprint:"mutated"`
1480 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001481
1482 Src *string
1483 Arch struct {
1484 Arm struct {
1485 Src *string
1486 }
1487 Arm64 struct {
1488 Src *string
1489 }
1490 X86 struct {
1491 Src *string
1492 }
1493 X86_64 struct {
1494 Src *string
1495 }
1496 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001497
1498 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001499 // Optional name for the installed apex. If unspecified, name of the
1500 // module is used as the file name
1501 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001502
1503 // Names of modules to be overridden. Listed modules can only be other binaries
1504 // (in Make or Soong).
1505 // This does not completely prevent installation of the overridden binaries, but if both
1506 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1507 // from PRODUCT_PACKAGES.
1508 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001509}
1510
1511func (p *Prebuilt) installable() bool {
1512 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001513}
1514
1515func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001516 // If the device is configured to use flattened APEX, force disable the prebuilt because
1517 // the prebuilt is a non-flattened one.
1518 forceDisable := ctx.Config().FlattenApex()
1519
1520 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1521 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001522 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001523
Kun Niu10c9f832019-07-29 16:28:57 -07001524 // Force disable the prebuilts when coverage is enabled.
1525 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1526 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1527
Jiyong Park50b81e52019-07-11 11:24:41 +09001528 // b/137216042 don't use prebuilts when address sanitizer is on
1529 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1530 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1531
1532 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001533 p.properties.ForceDisable = true
1534 return
1535 }
1536
Jiyong Parkc95714e2019-03-29 14:23:10 +09001537 // This is called before prebuilt_select and prebuilt_postdeps mutators
1538 // The mutators requires that src to be set correctly for each arch so that
1539 // arch variants are disabled when src is not provided for the arch.
1540 if len(ctx.MultiTargets()) != 1 {
1541 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1542 return
1543 }
1544 var src string
1545 switch ctx.MultiTargets()[0].Arch.ArchType {
1546 case android.Arm:
1547 src = String(p.properties.Arch.Arm.Src)
1548 case android.Arm64:
1549 src = String(p.properties.Arch.Arm64.Src)
1550 case android.X86:
1551 src = String(p.properties.Arch.X86.Src)
1552 case android.X86_64:
1553 src = String(p.properties.Arch.X86_64.Src)
1554 default:
1555 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1556 return
1557 }
1558 if src == "" {
1559 src = String(p.properties.Src)
1560 }
1561 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001562}
1563
Jiyong Park03b68dd2019-07-26 23:20:40 +09001564func (p *Prebuilt) isForceDisabled() bool {
1565 return p.properties.ForceDisable
1566}
1567
Colin Cross41955e82019-05-29 14:40:35 -07001568func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1569 switch tag {
1570 case "":
1571 return android.Paths{p.outputApex}, nil
1572 default:
1573 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1574 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001575}
1576
Jiyong Park4d277042019-04-23 18:00:10 +09001577func (p *Prebuilt) InstallFilename() string {
1578 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1579}
1580
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001581func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001582 if p.properties.ForceDisable {
1583 return
1584 }
1585
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001586 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001587 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001588 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001589 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001590 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1591 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1592 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001593 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1594 ctx.Build(pctx, android.BuildParams{
1595 Rule: android.Cp,
1596 Input: p.inputApex,
1597 Output: p.outputApex,
1598 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001599 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001600 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001601 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001602}
1603
1604func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1605 return &p.prebuilt
1606}
1607
1608func (p *Prebuilt) Name() string {
1609 return p.prebuilt.Name(p.ModuleBase.Name())
1610}
1611
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001612func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1613 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001614 Class: "ETC",
1615 OutputFile: android.OptionalPathForPath(p.inputApex),
1616 Include: "$(BUILD_PREBUILT)",
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001617 AddCustomEntries: func(name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
1618 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1619 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1620 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1621 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001622 },
1623 }
1624}
1625
1626// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1627func PrebuiltFactory() android.Module {
1628 module := &Prebuilt{}
1629 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001630 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001631 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001632 return module
1633}