blob: 1dfd6dc15880ce18acb08305b607695cbb040798 [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
49 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
50 // against the binary policy using sefcontext_compiler -p <policy>.
51
52 // TODO(b/114327326): automate the generation of file_contexts
53 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
54 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
55 `(${copy_commands}) && ` +
56 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090057 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090058 `--file_contexts ${file_contexts} ` +
59 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080060 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090061 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090062 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
63 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000064 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Jiyong Park48ca7dc2018-10-10 14:01:00 +090065 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090066 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080067
Alex Light5098a612018-11-29 17:12:15 -080068 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
69 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
70 `(${copy_commands}) && ` +
71 `APEXER_TOOL_PATH=${tool_path} ` +
72 `${apexer} --force --manifest ${manifest} ` +
73 `--payload_type zip ` +
74 `${image_dir} ${out} `,
75 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
76 Description: "ZipAPEX ${image_dir} => ${out}",
77 }, "tool_path", "image_dir", "copy_commands", "manifest")
78
Colin Crossa4925902018-11-16 11:36:28 -080079 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
80 blueprint.RuleParams{
81 Command: `${aapt2} convert --output-format proto $in -o $out`,
82 CommandDeps: []string{"${aapt2}"},
83 })
84
85 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090086 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000087 `apex_payload.img:apex/${abi}.img ` +
88 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +000089 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -080090 CommandDeps: []string{"${zip2zip}"},
91 Description: "app bundle",
92 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090093)
94
Alex Light5098a612018-11-29 17:12:15 -080095var imageApexSuffix = ".apex"
96var zipApexSuffix = ".zipapex"
97
98var imageApexType = "image"
99var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900100
101type dependencyTag struct {
102 blueprint.BaseDependencyTag
103 name string
104}
105
106var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900107 sharedLibTag = dependencyTag{name: "sharedLib"}
108 executableTag = dependencyTag{name: "executable"}
109 javaLibTag = dependencyTag{name: "javaLib"}
110 prebuiltTag = dependencyTag{name: "prebuilt"}
111 keyTag = dependencyTag{name: "key"}
112 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900113)
114
115func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700116 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900117 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900118 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100119 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
120 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
121 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
122 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000123 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100124 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
125 } else {
126 return pctx.HostBinToolPath(ctx, tool).String()
127 }
128 })
129 }
130 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900131 pctx.HostBinToolVariable("avbtool", "avbtool")
132 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
133 pctx.HostBinToolVariable("merge_zips", "merge_zips")
134 pctx.HostBinToolVariable("mke2fs", "mke2fs")
135 pctx.HostBinToolVariable("resize2fs", "resize2fs")
136 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
137 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800138 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900139 pctx.HostBinToolVariable("zipalign", "zipalign")
140
Alex Light0851b882019-02-07 13:20:53 -0800141 android.RegisterModuleType("apex", apexBundleFactory)
142 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900143 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700144 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900145
146 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
147 ctx.TopDown("apex_deps", apexDepsMutator)
148 ctx.BottomUp("apex", apexMutator)
149 })
150}
151
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900152// Mark the direct and transitive dependencies of apex bundles so that they
153// can be built for the apex bundles.
154func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800155 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800156 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900157 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900158 depName := mctx.OtherModuleName(child)
159 // If the parent is apexBundle, this child is directly depended.
160 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800161 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800162 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
163 // non-installable apex's cannot be installed and so should not prevent libraries from being
164 // installed to the system.
165 android.UpdateApexDependency(apexBundleName, depName, directDep)
166 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900167
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900168 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900169 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900170 return true
171 } else {
172 return false
173 }
174 })
175 }
176}
177
178// Create apex variations if a module is included in APEX(s).
179func apexMutator(mctx android.BottomUpMutatorContext) {
180 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900181 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900182 } else if _, ok := mctx.Module().(*apexBundle); ok {
183 // apex bundle itself is mutated so that it and its modules have same
184 // apex variant.
185 apexBundleName := mctx.ModuleName()
186 mctx.CreateVariations(apexBundleName)
187 }
188}
189
Alex Light9670d332019-01-29 18:07:33 -0800190type apexNativeDependencies struct {
191 // List of native libraries
192 Native_shared_libs []string
193 // List of native executables
194 Binaries []string
195}
196type apexMultilibProperties struct {
197 // Native dependencies whose compile_multilib is "first"
198 First apexNativeDependencies
199
200 // Native dependencies whose compile_multilib is "both"
201 Both apexNativeDependencies
202
203 // Native dependencies whose compile_multilib is "prefer32"
204 Prefer32 apexNativeDependencies
205
206 // Native dependencies whose compile_multilib is "32"
207 Lib32 apexNativeDependencies
208
209 // Native dependencies whose compile_multilib is "64"
210 Lib64 apexNativeDependencies
211}
212
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900213type apexBundleProperties struct {
214 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000215 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800216 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900217
Jiyong Park40e26a22019-02-08 02:53:06 +0900218 // AndroidManifest.xml file used for the zip container of this APEX bundle.
219 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800220 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900221
Jiyong Park05e70dd2019-03-18 14:26:32 +0900222 // Canonical name of the APEX bundle in the manifest file.
223 // If unspecified, defaults to the value of name
224 Apex_name *string
225
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900226 // Determines the file contexts file for setting security context to each file in this APEX bundle.
227 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
228 // used.
229 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900230 File_contexts *string
231
232 // List of native shared libs that are embedded inside this APEX bundle
233 Native_shared_libs []string
234
235 // List of native executables that are embedded inside this APEX bundle
236 Binaries []string
237
238 // List of java libraries that are embedded inside this APEX bundle
239 Java_libs []string
240
241 // List of prebuilt files that are embedded inside this APEX bundle
242 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900243
244 // Name of the apex_key module that provides the private key to sign APEX
245 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900246
Alex Light5098a612018-11-29 17:12:15 -0800247 // The type of APEX to build. Controls what the APEX payload is. Either
248 // 'image', 'zip' or 'both'. Default: 'image'.
249 Payload_type *string
250
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900251 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
252 // or an android_app_certificate module name in the form ":module".
253 Certificate *string
254
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900255 // Whether this APEX is installable to one of the partitions. Default: true.
256 Installable *bool
257
Jiyong Parkda6eb592018-12-19 17:12:36 +0900258 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
259 // Default is false.
260 Use_vendor *bool
261
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800262 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
263 Ignore_system_library_special_case *bool
264
Alex Light9670d332019-01-29 18:07:33 -0800265 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900266
Jiyong Parkf97782b2019-02-13 20:28:58 +0900267 // List of sanitizer names that this APEX is enabled for
268 SanitizerNames []string `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800269}
270
271type apexTargetBundleProperties struct {
272 Target struct {
273 // Multilib properties only for android.
274 Android struct {
275 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900276 }
Alex Light9670d332019-01-29 18:07:33 -0800277 // Multilib properties only for host.
278 Host struct {
279 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900280 }
Alex Light9670d332019-01-29 18:07:33 -0800281 // Multilib properties only for host linux_bionic.
282 Linux_bionic struct {
283 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900284 }
Alex Light9670d332019-01-29 18:07:33 -0800285 // Multilib properties only for host linux_glibc.
286 Linux_glibc struct {
287 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900288 }
289 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900290}
291
Jiyong Park8fd61922018-11-08 02:50:25 +0900292type apexFileClass int
293
294const (
295 etc apexFileClass = iota
296 nativeSharedLib
297 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900298 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800299 pyBinary
300 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900301 javaSharedLib
302)
303
Alex Light5098a612018-11-29 17:12:15 -0800304type apexPackaging int
305
306const (
307 imageApex apexPackaging = iota
308 zipApex
309 both
310)
311
312func (a apexPackaging) image() bool {
313 switch a {
314 case imageApex, both:
315 return true
316 }
317 return false
318}
319
320func (a apexPackaging) zip() bool {
321 switch a {
322 case zipApex, both:
323 return true
324 }
325 return false
326}
327
328func (a apexPackaging) suffix() string {
329 switch a {
330 case imageApex:
331 return imageApexSuffix
332 case zipApex:
333 return zipApexSuffix
334 case both:
335 panic(fmt.Errorf("must be either zip or image"))
336 default:
337 panic(fmt.Errorf("unkonwn APEX type %d", a))
338 }
339}
340
341func (a apexPackaging) name() string {
342 switch a {
343 case imageApex:
344 return imageApexType
345 case zipApex:
346 return zipApexType
347 case both:
348 panic(fmt.Errorf("must be either zip or image"))
349 default:
350 panic(fmt.Errorf("unkonwn APEX type %d", a))
351 }
352}
353
Jiyong Park8fd61922018-11-08 02:50:25 +0900354func (class apexFileClass) NameInMake() string {
355 switch class {
356 case etc:
357 return "ETC"
358 case nativeSharedLib:
359 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800360 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900361 return "EXECUTABLES"
362 case javaSharedLib:
363 return "JAVA_LIBRARIES"
364 default:
365 panic(fmt.Errorf("unkonwn class %d", class))
366 }
367}
368
369type apexFile struct {
370 builtFile android.Path
371 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900372 installDir string
373 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900374 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800375 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900376}
377
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900378type apexBundle struct {
379 android.ModuleBase
380 android.DefaultableModuleBase
381
Alex Light9670d332019-01-29 18:07:33 -0800382 properties apexBundleProperties
383 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900384
Alex Light5098a612018-11-29 17:12:15 -0800385 apexTypes apexPackaging
386
Colin Crossa4925902018-11-16 11:36:28 -0800387 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800388 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800389 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900390
Jiyong Park42cca6c2019-04-01 11:15:50 +0900391 public_key_file android.Path
392 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900393
394 container_certificate_file android.Path
395 container_private_key_file android.Path
396
Jiyong Park52818fc2019-03-18 12:01:38 +0900397 mergedNoticeFile android.WritablePath
398
Jiyong Park8fd61922018-11-08 02:50:25 +0900399 // list of files to be included in this apex
400 filesInfo []apexFile
401
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900402 // list of module names that this APEX is depending on
403 externalDeps []string
404
Jiyong Park8fd61922018-11-08 02:50:25 +0900405 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800406
407 testApex bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900408}
409
Jiyong Park397e55e2018-10-24 21:09:55 +0900410func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900411 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900412 // Use *FarVariation* to be able to depend on modules having
413 // conflicting variations with this module. This is required since
414 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
415 // for native shared libs.
416 ctx.AddFarVariationDependencies([]blueprint.Variation{
417 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900418 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900419 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900420 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900421 }, sharedLibTag, native_shared_libs...)
422
423 ctx.AddFarVariationDependencies([]blueprint.Variation{
424 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900425 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900426 }, executableTag, binaries...)
427}
428
Alex Light9670d332019-01-29 18:07:33 -0800429func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
430 if ctx.Os().Class == android.Device {
431 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
432 } else {
433 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
434 if ctx.Os().Bionic() {
435 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
436 } else {
437 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
438 }
439 }
440}
441
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900442func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800443
Jiyong Park397e55e2018-10-24 21:09:55 +0900444 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900445 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800446
447 a.combineProperties(ctx)
448
Jiyong Park397e55e2018-10-24 21:09:55 +0900449 has32BitTarget := false
450 for _, target := range targets {
451 if target.Arch.ArchType.Multilib == "lib32" {
452 has32BitTarget = true
453 }
454 }
455 for i, target := range targets {
456 // When multilib.* is omitted for native_shared_libs, it implies
457 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900458 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900459 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900460 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900461 {Mutator: "link", Variation: "shared"},
462 }, sharedLibTag, a.properties.Native_shared_libs...)
463
Jiyong Park397e55e2018-10-24 21:09:55 +0900464 // Add native modules targetting both ABIs
465 addDependenciesForNativeModules(ctx,
466 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900467 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900468 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900469
Alex Light3d673592019-01-18 14:37:31 -0800470 isPrimaryAbi := i == 0
471 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900472 // When multilib.* is omitted for binaries, it implies
473 // multilib.first.
474 ctx.AddFarVariationDependencies([]blueprint.Variation{
475 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900476 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900477 }, executableTag, a.properties.Binaries...)
478
479 // Add native modules targetting the first ABI
480 addDependenciesForNativeModules(ctx,
481 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900482 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900483 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800484
485 // When multilib.* is omitted for prebuilts, it implies multilib.first.
486 ctx.AddFarVariationDependencies([]blueprint.Variation{
487 {Mutator: "arch", Variation: target.String()},
488 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900489 }
490
491 switch target.Arch.ArchType.Multilib {
492 case "lib32":
493 // Add native modules targetting 32-bit ABI
494 addDependenciesForNativeModules(ctx,
495 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900496 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900497 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900498
499 addDependenciesForNativeModules(ctx,
500 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900501 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900502 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900503 case "lib64":
504 // Add native modules targetting 64-bit ABI
505 addDependenciesForNativeModules(ctx,
506 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900507 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900508 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900509
510 if !has32BitTarget {
511 addDependenciesForNativeModules(ctx,
512 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900513 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900514 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900515 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700516
517 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
518 for _, sanitizer := range ctx.Config().SanitizeDevice() {
519 if sanitizer == "hwaddress" {
520 addDependenciesForNativeModules(ctx,
521 []string{"libclang_rt.hwasan-aarch64-android"},
522 nil, target.String(), a.getImageVariation(config))
523 break
524 }
525 }
526 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900527 }
528
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900529 }
530
Jiyong Parkff1458f2018-10-12 21:49:38 +0900531 ctx.AddFarVariationDependencies([]blueprint.Variation{
532 {Mutator: "arch", Variation: "android_common"},
533 }, javaLibTag, a.properties.Java_libs...)
534
Jiyong Park23c52b02019-02-02 13:13:47 +0900535 if String(a.properties.Key) == "" {
536 ctx.ModuleErrorf("key is missing")
537 return
538 }
539 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900540
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900541 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900542 if cert != "" {
543 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900544 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900545}
546
Colin Cross0ea8ba82019-06-06 14:33:29 -0700547func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900548 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
549 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000550 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900551 }
552 return String(a.properties.Certificate)
553}
554
Colin Cross41955e82019-05-29 14:40:35 -0700555func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
556 switch tag {
557 case "":
558 if file, ok := a.outputFiles[imageApex]; ok {
559 return android.Paths{file}, nil
560 } else {
561 return nil, nil
562 }
563 default:
564 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900565 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900566}
567
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900568func (a *apexBundle) installable() bool {
569 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
570}
571
Jiyong Park7c1dc612019-01-05 11:15:24 +0900572func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
573 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900574 return "vendor"
575 } else {
576 return "core"
577 }
578}
579
Jiyong Parkf97782b2019-02-13 20:28:58 +0900580func (a *apexBundle) EnableSanitizer(sanitizerName string) {
581 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
582 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
583 }
584}
585
Jiyong Park388ef3f2019-01-28 19:47:32 +0900586func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900587 if android.InList(sanitizerName, a.properties.SanitizerNames) {
588 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900589 }
590
591 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900592 globalSanitizerNames := []string{}
593 if a.Host() {
594 globalSanitizerNames = ctx.Config().SanitizeHost()
595 } else {
596 arches := ctx.Config().SanitizeDeviceArch()
597 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
598 globalSanitizerNames = ctx.Config().SanitizeDevice()
599 }
600 }
601 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900602}
603
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800604func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900605 // Decide the APEX-local directory by the multilib of the library
606 // In the future, we may query this to the module.
607 switch cc.Arch().ArchType.Multilib {
608 case "lib32":
609 dirInApex = "lib"
610 case "lib64":
611 dirInApex = "lib64"
612 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900613 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900614 if !cc.Arch().Native {
615 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
616 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800617 if handleSpecialLibs {
618 switch cc.Name() {
619 case "libc", "libm", "libdl":
620 // Special case for bionic libs. This is to prevent the bionic libs
621 // from being included in the search path /apex/com.android.apex/lib.
622 // This exclusion is required because bionic libs in the runtime APEX
623 // are available via the legacy paths /system/lib/libc.so, etc. By the
624 // init process, the bionic libs in the APEX are bind-mounted to the
625 // legacy paths and thus will be loaded into the default linker namespace.
626 // If the bionic libs are directly in /apex/com.android.apex/lib then
627 // the same libs will be again loaded to the runtime linker namespace,
628 // which will result double loading of bionic libs that isn't supported.
629 dirInApex = filepath.Join(dirInApex, "bionic")
630 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900631 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900632
633 fileToCopy = cc.OutputFile().Path()
634 return
635}
636
637func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900638 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900639 fileToCopy = cc.OutputFile().Path()
640 return
641}
642
Alex Light778127a2019-02-27 14:19:50 -0800643func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
644 dirInApex = "bin"
645 fileToCopy = py.HostToolPath().Path()
646 return
647}
648func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
649 dirInApex = "bin"
650 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
651 if err != nil {
652 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
653 return
654 }
655 fileToCopy = android.PathForOutput(ctx, s)
656 return
657}
658
Jiyong Park04480cf2019-02-06 00:16:29 +0900659func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
660 dirInApex = filepath.Join("bin", sh.SubDir())
661 fileToCopy = sh.OutputFile()
662 return
663}
664
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900665func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
666 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900667 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900668 return
669}
670
671func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
672 dirInApex = filepath.Join("etc", prebuilt.SubDir())
673 fileToCopy = prebuilt.OutputFile()
674 return
675}
676
677func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900678 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900679
Alex Light5098a612018-11-29 17:12:15 -0800680 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
681 a.apexTypes = imageApex
682 } else if *a.properties.Payload_type == "zip" {
683 a.apexTypes = zipApex
684 } else if *a.properties.Payload_type == "both" {
685 a.apexTypes = both
686 } else {
687 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
688 return
689 }
690
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800691 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
692
Alex Light778127a2019-02-27 14:19:50 -0800693 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900694 if _, ok := parent.(*apexBundle); ok {
695 // direct dependencies
696 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900697 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900698 switch depTag {
699 case sharedLibTag:
700 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800701 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900702 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900703 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900704 } else {
705 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900706 }
707 case executableTag:
708 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800709 if !cc.Arch().Native {
710 // There is only one 'bin' directory so we shouldn't bother copying in
711 // native-bridge'd binaries and only use main ones.
712 return true
713 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900714 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900715 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900716 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900717 } else if sh, ok := child.(*android.ShBinary); ok {
718 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
719 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800720 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
721 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
722 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
723 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
724 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
725 // NB: Since go binaries are static we don't need the module for anything here, which is
726 // good since the go tool is a blueprint.Module not an android.Module like we would
727 // normally use.
728 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900729 } else {
Alex Light778127a2019-02-27 14:19:50 -0800730 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 +0900731 }
732 case javaLibTag:
733 if java, ok := child.(*java.Library); ok {
734 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900735 if fileToCopy == nil {
736 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
737 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900738 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900739 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900740 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900741 } else {
742 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900743 }
744 case prebuiltTag:
745 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
746 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900747 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900748 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900749 } else {
750 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
751 }
752 case keyTag:
753 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900754 a.private_key_file = key.private_key_file
755 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900756 return false
757 } else {
758 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900759 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900760 case certificateTag:
761 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900762 a.container_certificate_file = dep.Certificate.Pem
763 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900764 return false
765 } else {
766 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
767 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900768 }
769 } else {
770 // indirect dependencies
771 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
772 if cc, ok := child.(*cc.Module); ok {
Alex Light49ae3d92019-02-21 14:02:46 -0800773 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900774 // If the dependency is a stubs lib, don't include it in this APEX,
775 // but make sure that the lib is installed on the device.
776 // In case no APEX is having the lib, the lib is installed to the system
777 // partition.
Alex Light49ae3d92019-02-21 14:02:46 -0800778 //
779 // Always include if we are a host-apex however since those won't have any
780 // system libraries.
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900781 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
782 a.externalDeps = append(a.externalDeps, cc.Name())
783 }
784 // Don't track further
Jiyong Park25fc6a92018-11-18 18:02:45 +0900785 return false
786 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900787 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800788 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900789 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900790 return true
791 }
792 }
793 }
794 return false
795 })
796
Jiyong Park9335a262018-12-24 11:31:58 +0900797 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900798 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900799 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
800 return
801 }
802
Jiyong Park8fd61922018-11-08 02:50:25 +0900803 // remove duplicates in filesInfo
804 removeDup := func(filesInfo []apexFile) []apexFile {
805 encountered := make(map[android.Path]bool)
806 result := []apexFile{}
807 for _, f := range filesInfo {
808 if !encountered[f.builtFile] {
809 encountered[f.builtFile] = true
810 result = append(result, f)
811 }
812 }
813 return result
814 }
815 filesInfo = removeDup(filesInfo)
816
817 // to have consistent build rules
818 sort.Slice(filesInfo, func(i, j int) bool {
819 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
820 })
821
822 // prepend the name of this APEX to the module names. These names will be the names of
823 // modules that will be defined if the APEX is flattened.
824 for i := range filesInfo {
825 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
826 }
827
Jiyong Park8fd61922018-11-08 02:50:25 +0900828 a.installDir = android.PathForModuleInstall(ctx, "apex")
829 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800830
Jiyong Park52818fc2019-03-18 12:01:38 +0900831 a.buildNoticeFile(ctx)
832
Alex Light5098a612018-11-29 17:12:15 -0800833 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900834 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800835 }
836 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900837 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
838 // is true. This is to support referencing APEX via ":<module_name" syntax
839 // in other modules. It is in AndroidMk where the selection of flattened
840 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900841 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +0900842 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900843 }
844}
845
Jiyong Park52818fc2019-03-18 12:01:38 +0900846func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext) {
847 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900848 for _, f := range a.filesInfo {
849 if f.module != nil {
850 notice := f.module.NoticeFile()
851 if notice.Valid() {
852 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900853 }
854 }
855 }
856 // append the notice file specified in the apex module itself
857 if a.NoticeFile().Valid() {
858 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900859 }
860
861 if len(noticeFiles) > 0 {
862 a.mergedNoticeFile = android.PathForModuleOut(ctx, "NOTICE")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700863 android.MergeNotices(ctx, a.mergedNoticeFile, noticeFiles)
Jiyong Park52818fc2019-03-18 12:01:38 +0900864 }
865}
866
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900867func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900868 cert := String(a.properties.Certificate)
869 if cert != "" && android.SrcIsModule(cert) == "" {
870 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900871 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
872 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900873 } else if cert == "" {
874 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900875 a.container_certificate_file = pem
876 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900877 }
878
Colin Cross8a497952019-03-05 22:25:09 -0800879 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900880
Alex Light5098a612018-11-29 17:12:15 -0800881 var abis []string
882 for _, target := range ctx.MultiTargets() {
883 if len(target.Arch.Abi) > 0 {
884 abis = append(abis, target.Arch.Abi[0])
885 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900886 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900887
Alex Light5098a612018-11-29 17:12:15 -0800888 abis = android.FirstUniqueStrings(abis)
889
890 suffix := apexType.suffix()
891 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900892
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900893 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900894 for _, f := range a.filesInfo {
895 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900896 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900897
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900898 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900899 for i, src := range filesToCopy {
900 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800901 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900902 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
903 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800904 for _, sym := range a.filesInfo[i].symlinks {
905 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
906 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
907 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900908 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900909 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800910 implicitInputs = append(implicitInputs, manifest)
911
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900912 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
913 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900914
Alex Light5098a612018-11-29 17:12:15 -0800915 if apexType.image() {
916 // files and dirs that will be created in APEX
917 var readOnlyPaths []string
918 var executablePaths []string // this also includes dirs
919 for _, f := range a.filesInfo {
920 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
921 if f.installDir == "bin" {
922 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800923 for _, s := range f.symlinks {
924 executablePaths = append(executablePaths, filepath.Join("bin", s))
925 }
Alex Light5098a612018-11-29 17:12:15 -0800926 } else {
927 readOnlyPaths = append(readOnlyPaths, pathInApex)
928 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900929 dir := f.installDir
930 for !android.InList(dir, executablePaths) && dir != "" {
931 executablePaths = append(executablePaths, dir)
932 dir, _ = filepath.Split(dir) // move up to the parent
933 if len(dir) > 0 {
934 // remove trailing slash
935 dir = dir[:len(dir)-1]
936 }
Alex Light5098a612018-11-29 17:12:15 -0800937 }
938 }
939 sort.Strings(readOnlyPaths)
940 sort.Strings(executablePaths)
941 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
942 ctx.Build(pctx, android.BuildParams{
943 Rule: generateFsConfig,
944 Output: cannedFsConfig,
945 Description: "generate fs config",
946 Args: map[string]string{
947 "ro_paths": strings.Join(readOnlyPaths, " "),
948 "exec_paths": strings.Join(executablePaths, " "),
949 },
950 })
951
952 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
953 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
954 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
955 if !fileContextsOptionalPath.Valid() {
956 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
957 return
958 }
959 fileContexts := fileContextsOptionalPath.Path()
960
Jiyong Park835d82b2018-12-27 16:04:18 +0900961 optFlags := []string{}
962
Alex Light5098a612018-11-29 17:12:15 -0800963 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +0900964 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
965 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -0800966
Jiyong Park7f67f482019-01-05 12:57:48 +0900967 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
968 if overridden {
969 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
970 }
971
Jiyong Park40e26a22019-02-08 02:53:06 +0900972 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800973 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +0900974 implicitInputs = append(implicitInputs, androidManifestFile)
975 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
976 }
977
Jiyong Park71b519d2019-04-18 17:25:49 +0900978 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
979 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
980 ctx.Config().UnbundledBuild() &&
981 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
982 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
983 apiFingerprint := java.ApiFingerprintPath(ctx)
984 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
985 implicitInputs = append(implicitInputs, apiFingerprint)
986 }
987 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
988
Alex Light5098a612018-11-29 17:12:15 -0800989 ctx.Build(pctx, android.BuildParams{
990 Rule: apexRule,
991 Implicits: implicitInputs,
992 Output: unsignedOutputFile,
993 Description: "apex (" + apexType.name() + ")",
994 Args: map[string]string{
995 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
996 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
997 "copy_commands": strings.Join(copyCommands, " && "),
998 "manifest": manifest.String(),
999 "file_contexts": fileContexts.String(),
1000 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001001 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001002 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001003 },
1004 })
1005
1006 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1007 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1008 a.bundleModuleFile = bundleModuleFile
1009
1010 ctx.Build(pctx, android.BuildParams{
1011 Rule: apexProtoConvertRule,
1012 Input: unsignedOutputFile,
1013 Output: apexProtoFile,
1014 Description: "apex proto convert",
1015 })
1016
1017 ctx.Build(pctx, android.BuildParams{
1018 Rule: apexBundleRule,
1019 Input: apexProtoFile,
1020 Output: a.bundleModuleFile,
1021 Description: "apex bundle module",
1022 Args: map[string]string{
1023 "abi": strings.Join(abis, "."),
1024 },
1025 })
1026 } else {
1027 ctx.Build(pctx, android.BuildParams{
1028 Rule: zipApexRule,
1029 Implicits: implicitInputs,
1030 Output: unsignedOutputFile,
1031 Description: "apex (" + apexType.name() + ")",
1032 Args: map[string]string{
1033 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1034 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1035 "copy_commands": strings.Join(copyCommands, " && "),
1036 "manifest": manifest.String(),
1037 },
1038 })
Colin Crossa4925902018-11-16 11:36:28 -08001039 }
Colin Crossa4925902018-11-16 11:36:28 -08001040
Alex Light5098a612018-11-29 17:12:15 -08001041 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001042 ctx.Build(pctx, android.BuildParams{
1043 Rule: java.Signapk,
1044 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001045 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001046 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001047 Implicits: []android.Path{
1048 a.container_certificate_file,
1049 a.container_private_key_file,
1050 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001051 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001052 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001053 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001054 },
1055 })
Alex Light5098a612018-11-29 17:12:15 -08001056
1057 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001058 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001059 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001060 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001061}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001062
Jiyong Park8fd61922018-11-08 02:50:25 +09001063func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001064 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001065 // 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 +09001066 // with other ordinary files.
Colin Cross8a497952019-03-05 22:25:09 -08001067 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +09001068
1069 // rename to apex_manifest.json
1070 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
1071 ctx.Build(pctx, android.BuildParams{
1072 Rule: android.Cp,
1073 Input: manifest,
1074 Output: copiedManifest,
1075 })
Jiyong Park719b4462019-01-13 00:39:51 +09001076 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001077
Jiyong Park42cca6c2019-04-01 11:15:50 +09001078 // rename to apex_pubkey
1079 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1080 ctx.Build(pctx, android.BuildParams{
1081 Rule: android.Cp,
1082 Input: a.public_key_file,
1083 Output: copiedPubkey,
1084 })
1085 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1086
Jiyong Park23c52b02019-02-02 13:13:47 +09001087 if ctx.Config().FlattenApex() {
1088 for _, fi := range a.filesInfo {
1089 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001090 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1091 for _, sym := range fi.symlinks {
1092 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1093 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001094 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001095 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001096 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001097}
1098
1099func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -08001100 writers := []android.AndroidMkData{}
1101 if a.apexTypes.image() {
1102 writers = append(writers, a.androidMkForType(imageApex))
1103 }
1104 if a.apexTypes.zip() {
1105 writers = append(writers, a.androidMkForType(zipApex))
1106 }
1107 return android.AndroidMkData{
1108 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1109 for _, data := range writers {
1110 data.Custom(w, name, prefix, moduleDir, data)
1111 }
1112 }}
1113}
1114
Alex Lightf1801bc2019-02-13 11:10:07 -08001115func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001116 moduleNames := []string{}
1117
1118 for _, fi := range a.filesInfo {
1119 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1120 continue
1121 }
1122 if !android.InList(fi.moduleName, moduleNames) {
1123 moduleNames = append(moduleNames, fi.moduleName)
1124 }
1125 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1126 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1127 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001128 // /apex/<name>/{lib|framework|...}
1129 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1130 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001131 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001132 // /system/apex/<name>/{lib|framework|...}
1133 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1134 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001135 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001136 if len(fi.symlinks) > 0 {
1137 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1138 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001139
1140 if fi.module != nil && fi.module.NoticeFile().Valid() {
1141 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1142 }
Jiyong Park94427262019-02-05 23:18:47 +09001143 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001144 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001145 }
1146 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1147 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1148 if fi.module != nil {
1149 archStr := fi.module.Target().Arch.ArchType.String()
1150 host := false
1151 switch fi.module.Target().Os.Class {
1152 case android.Host:
1153 if archStr != "common" {
1154 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1155 }
1156 host = true
1157 case android.HostCross:
1158 if archStr != "common" {
1159 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1160 }
1161 host = true
1162 case android.Device:
1163 if archStr != "common" {
1164 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1165 }
1166 }
1167 if host {
1168 makeOs := fi.module.Target().Os.String()
1169 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1170 makeOs = "linux"
1171 }
1172 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1173 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1174 }
1175 }
1176 if fi.class == javaSharedLib {
1177 javaModule := fi.module.(*java.Library)
1178 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1179 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1180 // we will have foo.jar.jar
1181 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1182 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1183 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1184 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1185 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1186 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1187 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1188 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001189 if cc, ok := fi.module.(*cc.Module); ok {
1190 if cc.UnstrippedOutputFile() != nil {
1191 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1192 }
1193 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Park94427262019-02-05 23:18:47 +09001194 }
1195 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1196 } else {
1197 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1198 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1199 }
1200 }
1201 return moduleNames
1202}
1203
Alex Light5098a612018-11-29 17:12:15 -08001204func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001205 return android.AndroidMkData{
1206 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1207 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001208 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001209 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001210 }
1211
Jiyong Park719b4462019-01-13 00:39:51 +09001212 if a.flattened && apexType.image() {
1213 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001214 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1215 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1216 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001217 if len(moduleNames) > 0 {
1218 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1219 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001220 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001221 } else {
Alex Light5098a612018-11-29 17:12:15 -08001222 // zip-apex is the less common type so have the name refer to the image-apex
1223 // only and use {name}.zip if you want the zip-apex
1224 if apexType == zipApex && a.apexTypes == both {
1225 name = name + ".zip"
1226 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001227 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1228 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1229 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1230 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001231 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001232 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001233 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001234 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park52818fc2019-03-18 12:01:38 +09001235 if a.installable() && a.mergedNoticeFile != nil {
1236 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", a.mergedNoticeFile.String())
1237 }
Jiyong Park94427262019-02-05 23:18:47 +09001238 if len(moduleNames) > 0 {
1239 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1240 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001241 if len(a.externalDeps) > 0 {
1242 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1243 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001244 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001245
Alex Light5098a612018-11-29 17:12:15 -08001246 if apexType == imageApex {
1247 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1248 }
Jiyong Park719b4462019-01-13 00:39:51 +09001249 }
1250 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001251}
1252
Alex Light0851b882019-02-07 13:20:53 -08001253func testApexBundleFactory() android.Module {
1254 return ApexBundleFactory( /*testApex*/ true)
1255}
1256
1257func apexBundleFactory() android.Module {
1258 return ApexBundleFactory( /*testApex*/ false)
1259}
1260
1261func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001262 module := &apexBundle{
1263 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001264 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001265 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001266 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001267 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001268 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001269 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1270 })
Alex Light5098a612018-11-29 17:12:15 -08001271 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001272 android.InitDefaultableModule(module)
1273 return module
1274}
Jiyong Park30ca9372019-02-07 16:27:23 +09001275
1276//
1277// Defaults
1278//
1279type Defaults struct {
1280 android.ModuleBase
1281 android.DefaultsModuleBase
1282}
1283
Jiyong Park30ca9372019-02-07 16:27:23 +09001284func defaultsFactory() android.Module {
1285 return DefaultsFactory()
1286}
1287
1288func DefaultsFactory(props ...interface{}) android.Module {
1289 module := &Defaults{}
1290
1291 module.AddProperties(props...)
1292 module.AddProperties(
1293 &apexBundleProperties{},
1294 &apexTargetBundleProperties{},
1295 )
1296
1297 android.InitDefaultsModule(module)
1298 return module
1299}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001300
1301//
1302// Prebuilt APEX
1303//
1304type Prebuilt struct {
1305 android.ModuleBase
1306 prebuilt android.Prebuilt
1307
1308 properties PrebuiltProperties
1309
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001310 inputApex android.Path
1311 installDir android.OutputPath
1312 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001313 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001314}
1315
1316type PrebuiltProperties struct {
1317 // the path to the prebuilt .apex file to import.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001318 Source string `blueprint:"mutated"`
1319
1320 Src *string
1321 Arch struct {
1322 Arm struct {
1323 Src *string
1324 }
1325 Arm64 struct {
1326 Src *string
1327 }
1328 X86 struct {
1329 Src *string
1330 }
1331 X86_64 struct {
1332 Src *string
1333 }
1334 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001335
1336 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001337 // Optional name for the installed apex. If unspecified, name of the
1338 // module is used as the file name
1339 Filename *string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001340}
1341
1342func (p *Prebuilt) installable() bool {
1343 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001344}
1345
1346func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc95714e2019-03-29 14:23:10 +09001347 // This is called before prebuilt_select and prebuilt_postdeps mutators
1348 // The mutators requires that src to be set correctly for each arch so that
1349 // arch variants are disabled when src is not provided for the arch.
1350 if len(ctx.MultiTargets()) != 1 {
1351 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1352 return
1353 }
1354 var src string
1355 switch ctx.MultiTargets()[0].Arch.ArchType {
1356 case android.Arm:
1357 src = String(p.properties.Arch.Arm.Src)
1358 case android.Arm64:
1359 src = String(p.properties.Arch.Arm64.Src)
1360 case android.X86:
1361 src = String(p.properties.Arch.X86.Src)
1362 case android.X86_64:
1363 src = String(p.properties.Arch.X86_64.Src)
1364 default:
1365 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1366 return
1367 }
1368 if src == "" {
1369 src = String(p.properties.Src)
1370 }
1371 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001372}
1373
Colin Cross41955e82019-05-29 14:40:35 -07001374func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1375 switch tag {
1376 case "":
1377 return android.Paths{p.outputApex}, nil
1378 default:
1379 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1380 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001381}
1382
Jiyong Park4d277042019-04-23 18:00:10 +09001383func (p *Prebuilt) InstallFilename() string {
1384 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1385}
1386
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001387func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1388 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001389 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001390 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001391 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001392 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1393 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1394 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001395 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1396 ctx.Build(pctx, android.BuildParams{
1397 Rule: android.Cp,
1398 Input: p.inputApex,
1399 Output: p.outputApex,
1400 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001401 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001402 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001403 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001404}
1405
1406func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1407 return &p.prebuilt
1408}
1409
1410func (p *Prebuilt) Name() string {
1411 return p.prebuilt.Name(p.ModuleBase.Name())
1412}
1413
1414func (p *Prebuilt) AndroidMk() android.AndroidMkData {
1415 return android.AndroidMkData{
1416 Class: "ETC",
1417 OutputFile: android.OptionalPathForPath(p.inputApex),
1418 Include: "$(BUILD_PREBUILT)",
1419 Extra: []android.AndroidMkExtraFunc{
1420 func(w io.Writer, outputFile android.Path) {
1421 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001422 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", p.installFilename)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001423 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.installable())
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001424 },
1425 },
1426 }
1427}
1428
1429// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1430func PrebuiltFactory() android.Module {
1431 module := &Prebuilt{}
1432 module.AddProperties(&module.properties)
Jiyong Parkc95714e2019-03-29 14:23:10 +09001433 android.InitSingleSourcePrebuiltModule(module, &module.properties.Source)
1434 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001435 return module
1436}