blob: 092868e63c5f5c68ed4dd0f89d8a4819480f7b9c [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"
28
29 "github.com/google/blueprint"
30 "github.com/google/blueprint/proptools"
31)
32
33var (
34 pctx = android.NewPackageContext("android/apex")
35
36 // Create a canned fs config file where all files and directories are
37 // by default set to (uid/gid/mode) = (1000/1000/0644)
38 // TODO(b/113082813) make this configurable using config.fs syntax
39 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000040 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000041 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090042 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090043 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090044 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090045 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046
47 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
48 // against the binary policy using sefcontext_compiler -p <policy>.
49
50 // TODO(b/114327326): automate the generation of file_contexts
51 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
52 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
53 `(${copy_commands}) && ` +
54 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090055 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090056 `--file_contexts ${file_contexts} ` +
57 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080058 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090059 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090060 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
61 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
62 "${soong_zip}", "${zipalign}", "${aapt2}"},
63 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090064 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080065
Alex Light5098a612018-11-29 17:12:15 -080066 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
67 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
68 `(${copy_commands}) && ` +
69 `APEXER_TOOL_PATH=${tool_path} ` +
70 `${apexer} --force --manifest ${manifest} ` +
71 `--payload_type zip ` +
72 `${image_dir} ${out} `,
73 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
74 Description: "ZipAPEX ${image_dir} => ${out}",
75 }, "tool_path", "image_dir", "copy_commands", "manifest")
76
Colin Crossa4925902018-11-16 11:36:28 -080077 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
78 blueprint.RuleParams{
79 Command: `${aapt2} convert --output-format proto $in -o $out`,
80 CommandDeps: []string{"${aapt2}"},
81 })
82
83 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090084 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000085 `apex_payload.img:apex/${abi}.img ` +
86 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +000087 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -080088 CommandDeps: []string{"${zip2zip}"},
89 Description: "app bundle",
90 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090091)
92
Alex Light5098a612018-11-29 17:12:15 -080093var imageApexSuffix = ".apex"
94var zipApexSuffix = ".zipapex"
95
96var imageApexType = "image"
97var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090098
99type dependencyTag struct {
100 blueprint.BaseDependencyTag
101 name string
102}
103
104var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900105 sharedLibTag = dependencyTag{name: "sharedLib"}
106 executableTag = dependencyTag{name: "executable"}
107 javaLibTag = dependencyTag{name: "javaLib"}
108 prebuiltTag = dependencyTag{name: "prebuilt"}
109 keyTag = dependencyTag{name: "key"}
110 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900111)
112
113func init() {
114 pctx.Import("android/soong/common")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900115 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900116 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100117 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
118 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
119 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
120 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000121 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100122 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
123 } else {
124 return pctx.HostBinToolPath(ctx, tool).String()
125 }
126 })
127 }
128 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900129 pctx.HostBinToolVariable("avbtool", "avbtool")
130 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
131 pctx.HostBinToolVariable("merge_zips", "merge_zips")
132 pctx.HostBinToolVariable("mke2fs", "mke2fs")
133 pctx.HostBinToolVariable("resize2fs", "resize2fs")
134 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
135 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800136 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900137 pctx.HostBinToolVariable("zipalign", "zipalign")
138
Alex Lightee250722018-12-06 14:00:02 -0800139 android.RegisterModuleType("apex", ApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900140 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900141
142 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
143 ctx.TopDown("apex_deps", apexDepsMutator)
144 ctx.BottomUp("apex", apexMutator)
145 })
146}
147
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900148// Mark the direct and transitive dependencies of apex bundles so that they
149// can be built for the apex bundles.
150func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800151 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800152 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900153 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900154 depName := mctx.OtherModuleName(child)
155 // If the parent is apexBundle, this child is directly depended.
156 _, directDep := parent.(*apexBundle)
Alex Lightf98087f2019-02-04 14:45:06 -0800157 if a.installable() {
158 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
159 // non-installable apex's cannot be installed and so should not prevent libraries from being
160 // installed to the system.
161 android.UpdateApexDependency(apexBundleName, depName, directDep)
162 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900163
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900164 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900165 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900166 return true
167 } else {
168 return false
169 }
170 })
171 }
172}
173
174// Create apex variations if a module is included in APEX(s).
175func apexMutator(mctx android.BottomUpMutatorContext) {
176 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900177 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900178 } else if _, ok := mctx.Module().(*apexBundle); ok {
179 // apex bundle itself is mutated so that it and its modules have same
180 // apex variant.
181 apexBundleName := mctx.ModuleName()
182 mctx.CreateVariations(apexBundleName)
183 }
184}
185
Alex Light9670d332019-01-29 18:07:33 -0800186type apexNativeDependencies struct {
187 // List of native libraries
188 Native_shared_libs []string
189 // List of native executables
190 Binaries []string
191}
192type apexMultilibProperties struct {
193 // Native dependencies whose compile_multilib is "first"
194 First apexNativeDependencies
195
196 // Native dependencies whose compile_multilib is "both"
197 Both apexNativeDependencies
198
199 // Native dependencies whose compile_multilib is "prefer32"
200 Prefer32 apexNativeDependencies
201
202 // Native dependencies whose compile_multilib is "32"
203 Lib32 apexNativeDependencies
204
205 // Native dependencies whose compile_multilib is "64"
206 Lib64 apexNativeDependencies
207}
208
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900209type apexBundleProperties struct {
210 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000211 // "apex_manifest.json"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900212 Manifest *string
213
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900214 // Determines the file contexts file for setting security context to each file in this APEX bundle.
215 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
216 // used.
217 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900218 File_contexts *string
219
220 // List of native shared libs that are embedded inside this APEX bundle
221 Native_shared_libs []string
222
223 // List of native executables that are embedded inside this APEX bundle
224 Binaries []string
225
226 // List of java libraries that are embedded inside this APEX bundle
227 Java_libs []string
228
229 // List of prebuilt files that are embedded inside this APEX bundle
230 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900231
232 // Name of the apex_key module that provides the private key to sign APEX
233 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900234
Alex Light5098a612018-11-29 17:12:15 -0800235 // The type of APEX to build. Controls what the APEX payload is. Either
236 // 'image', 'zip' or 'both'. Default: 'image'.
237 Payload_type *string
238
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900239 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
240 // or an android_app_certificate module name in the form ":module".
241 Certificate *string
242
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900243 // Whether this APEX is installable to one of the partitions. Default: true.
244 Installable *bool
245
Jiyong Parkda6eb592018-12-19 17:12:36 +0900246 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
247 // Default is false.
248 Use_vendor *bool
249
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800250 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
251 Ignore_system_library_special_case *bool
252
Alex Light9670d332019-01-29 18:07:33 -0800253 Multilib apexMultilibProperties
254}
255
256type apexTargetBundleProperties struct {
257 Target struct {
258 // Multilib properties only for android.
259 Android struct {
260 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900261 }
Alex Light9670d332019-01-29 18:07:33 -0800262 // Multilib properties only for host.
263 Host struct {
264 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900265 }
Alex Light9670d332019-01-29 18:07:33 -0800266 // Multilib properties only for host linux_bionic.
267 Linux_bionic struct {
268 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900269 }
Alex Light9670d332019-01-29 18:07:33 -0800270 // Multilib properties only for host linux_glibc.
271 Linux_glibc struct {
272 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900273 }
274 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900275}
276
Jiyong Park8fd61922018-11-08 02:50:25 +0900277type apexFileClass int
278
279const (
280 etc apexFileClass = iota
281 nativeSharedLib
282 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900283 shBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900284 javaSharedLib
285)
286
Alex Light5098a612018-11-29 17:12:15 -0800287type apexPackaging int
288
289const (
290 imageApex apexPackaging = iota
291 zipApex
292 both
293)
294
295func (a apexPackaging) image() bool {
296 switch a {
297 case imageApex, both:
298 return true
299 }
300 return false
301}
302
303func (a apexPackaging) zip() bool {
304 switch a {
305 case zipApex, both:
306 return true
307 }
308 return false
309}
310
311func (a apexPackaging) suffix() string {
312 switch a {
313 case imageApex:
314 return imageApexSuffix
315 case zipApex:
316 return zipApexSuffix
317 case both:
318 panic(fmt.Errorf("must be either zip or image"))
319 default:
320 panic(fmt.Errorf("unkonwn APEX type %d", a))
321 }
322}
323
324func (a apexPackaging) name() string {
325 switch a {
326 case imageApex:
327 return imageApexType
328 case zipApex:
329 return zipApexType
330 case both:
331 panic(fmt.Errorf("must be either zip or image"))
332 default:
333 panic(fmt.Errorf("unkonwn APEX type %d", a))
334 }
335}
336
Jiyong Park8fd61922018-11-08 02:50:25 +0900337func (class apexFileClass) NameInMake() string {
338 switch class {
339 case etc:
340 return "ETC"
341 case nativeSharedLib:
342 return "SHARED_LIBRARIES"
Jiyong Park04480cf2019-02-06 00:16:29 +0900343 case nativeExecutable, shBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900344 return "EXECUTABLES"
345 case javaSharedLib:
346 return "JAVA_LIBRARIES"
347 default:
348 panic(fmt.Errorf("unkonwn class %d", class))
349 }
350}
351
352type apexFile struct {
353 builtFile android.Path
354 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900355 installDir string
356 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900357 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800358 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900359}
360
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900361type apexBundle struct {
362 android.ModuleBase
363 android.DefaultableModuleBase
364
Alex Light9670d332019-01-29 18:07:33 -0800365 properties apexBundleProperties
366 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900367
Alex Light5098a612018-11-29 17:12:15 -0800368 apexTypes apexPackaging
369
Colin Crossa4925902018-11-16 11:36:28 -0800370 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800371 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800372 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900373
374 // list of files to be included in this apex
375 filesInfo []apexFile
376
377 flattened bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900378}
379
Jiyong Park397e55e2018-10-24 21:09:55 +0900380func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900381 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900382 // Use *FarVariation* to be able to depend on modules having
383 // conflicting variations with this module. This is required since
384 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
385 // for native shared libs.
386 ctx.AddFarVariationDependencies([]blueprint.Variation{
387 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900388 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900389 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900390 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900391 }, sharedLibTag, native_shared_libs...)
392
393 ctx.AddFarVariationDependencies([]blueprint.Variation{
394 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900395 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900396 }, executableTag, binaries...)
397}
398
Alex Light9670d332019-01-29 18:07:33 -0800399func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
400 if ctx.Os().Class == android.Device {
401 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
402 } else {
403 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
404 if ctx.Os().Bionic() {
405 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
406 } else {
407 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
408 }
409 }
410}
411
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900412func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800413
Jiyong Park397e55e2018-10-24 21:09:55 +0900414 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900415 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800416
417 a.combineProperties(ctx)
418
Jiyong Park397e55e2018-10-24 21:09:55 +0900419 has32BitTarget := false
420 for _, target := range targets {
421 if target.Arch.ArchType.Multilib == "lib32" {
422 has32BitTarget = true
423 }
424 }
425 for i, target := range targets {
426 // When multilib.* is omitted for native_shared_libs, it implies
427 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900428 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900429 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900430 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900431 {Mutator: "link", Variation: "shared"},
432 }, sharedLibTag, a.properties.Native_shared_libs...)
433
Jiyong Park397e55e2018-10-24 21:09:55 +0900434 // Add native modules targetting both ABIs
435 addDependenciesForNativeModules(ctx,
436 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900437 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900438 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900439
Alex Light3d673592019-01-18 14:37:31 -0800440 isPrimaryAbi := i == 0
441 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900442 // When multilib.* is omitted for binaries, it implies
443 // multilib.first.
444 ctx.AddFarVariationDependencies([]blueprint.Variation{
445 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900446 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900447 }, executableTag, a.properties.Binaries...)
448
449 // Add native modules targetting the first ABI
450 addDependenciesForNativeModules(ctx,
451 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900452 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900453 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800454
455 // When multilib.* is omitted for prebuilts, it implies multilib.first.
456 ctx.AddFarVariationDependencies([]blueprint.Variation{
457 {Mutator: "arch", Variation: target.String()},
458 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900459 }
460
461 switch target.Arch.ArchType.Multilib {
462 case "lib32":
463 // Add native modules targetting 32-bit ABI
464 addDependenciesForNativeModules(ctx,
465 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900466 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900467 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900468
469 addDependenciesForNativeModules(ctx,
470 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900471 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900472 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900473 case "lib64":
474 // Add native modules targetting 64-bit ABI
475 addDependenciesForNativeModules(ctx,
476 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900477 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900478 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900479
480 if !has32BitTarget {
481 addDependenciesForNativeModules(ctx,
482 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900483 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900484 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900485 }
486 }
487
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900488 }
489
Jiyong Parkff1458f2018-10-12 21:49:38 +0900490 ctx.AddFarVariationDependencies([]blueprint.Variation{
491 {Mutator: "arch", Variation: "android_common"},
492 }, javaLibTag, a.properties.Java_libs...)
493
Jiyong Park23c52b02019-02-02 13:13:47 +0900494 if String(a.properties.Key) == "" {
495 ctx.ModuleErrorf("key is missing")
496 return
497 }
498 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900499
Jiyong Park23c52b02019-02-02 13:13:47 +0900500 cert := android.SrcIsModule(String(a.properties.Certificate))
501 if cert != "" {
502 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900503 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900504}
505
Jiyong Park74e240b2018-11-27 21:27:08 +0900506func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900507 if file, ok := a.outputFiles[imageApex]; ok {
508 return android.Paths{file}
509 } else {
510 return nil
511 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900512}
513
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900514func (a *apexBundle) installable() bool {
515 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
516}
517
Jiyong Park7c1dc612019-01-05 11:15:24 +0900518func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
519 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900520 return "vendor"
521 } else {
522 return "core"
523 }
524}
525
Jiyong Park388ef3f2019-01-28 19:47:32 +0900526func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
527 globalSanitizerNames := []string{}
528 if a.Host() {
529 globalSanitizerNames = ctx.Config().SanitizeHost()
530 } else {
531 arches := ctx.Config().SanitizeDeviceArch()
532 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
533 globalSanitizerNames = ctx.Config().SanitizeDevice()
534 }
535 }
536 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900537}
538
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800539func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900540 // Decide the APEX-local directory by the multilib of the library
541 // In the future, we may query this to the module.
542 switch cc.Arch().ArchType.Multilib {
543 case "lib32":
544 dirInApex = "lib"
545 case "lib64":
546 dirInApex = "lib64"
547 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900548 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900549 if !cc.Arch().Native {
550 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
551 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800552 if handleSpecialLibs {
553 switch cc.Name() {
554 case "libc", "libm", "libdl":
555 // Special case for bionic libs. This is to prevent the bionic libs
556 // from being included in the search path /apex/com.android.apex/lib.
557 // This exclusion is required because bionic libs in the runtime APEX
558 // are available via the legacy paths /system/lib/libc.so, etc. By the
559 // init process, the bionic libs in the APEX are bind-mounted to the
560 // legacy paths and thus will be loaded into the default linker namespace.
561 // If the bionic libs are directly in /apex/com.android.apex/lib then
562 // the same libs will be again loaded to the runtime linker namespace,
563 // which will result double loading of bionic libs that isn't supported.
564 dirInApex = filepath.Join(dirInApex, "bionic")
565 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900566 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900567
568 fileToCopy = cc.OutputFile().Path()
569 return
570}
571
572func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900573 // TODO(b/123721777) respect relative_install_path also for binaries
574 // dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900575 dirInApex = "bin"
576 fileToCopy = cc.OutputFile().Path()
577 return
578}
579
Jiyong Park04480cf2019-02-06 00:16:29 +0900580func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
581 dirInApex = filepath.Join("bin", sh.SubDir())
582 fileToCopy = sh.OutputFile()
583 return
584}
585
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900586func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
587 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900588 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900589 return
590}
591
592func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
593 dirInApex = filepath.Join("etc", prebuilt.SubDir())
594 fileToCopy = prebuilt.OutputFile()
595 return
596}
597
598func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900599 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900600
Jiyong Parkff1458f2018-10-12 21:49:38 +0900601 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900602 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900603 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900604
Alex Light5098a612018-11-29 17:12:15 -0800605 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
606 a.apexTypes = imageApex
607 } else if *a.properties.Payload_type == "zip" {
608 a.apexTypes = zipApex
609 } else if *a.properties.Payload_type == "both" {
610 a.apexTypes = both
611 } else {
612 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
613 return
614 }
615
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800616 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
617
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900618 ctx.WalkDeps(func(child, parent android.Module) bool {
619 if _, ok := parent.(*apexBundle); ok {
620 // direct dependencies
621 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900622 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900623 switch depTag {
624 case sharedLibTag:
625 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800626 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900627 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900628 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900629 } else {
630 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900631 }
632 case executableTag:
633 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800634 if !cc.Arch().Native {
635 // There is only one 'bin' directory so we shouldn't bother copying in
636 // native-bridge'd binaries and only use main ones.
637 return true
638 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900639 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900640 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900641 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900642 } else if sh, ok := child.(*android.ShBinary); ok {
643 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
644 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900645 } else {
Jiyong Park04480cf2019-02-06 00:16:29 +0900646 ctx.PropertyErrorf("binaries", "%q is neithher cc_binary nor sh_binary", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900647 }
648 case javaLibTag:
649 if java, ok := child.(*java.Library); ok {
650 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900651 if fileToCopy == nil {
652 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
653 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900654 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900655 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900656 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900657 } else {
658 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900659 }
660 case prebuiltTag:
661 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
662 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900663 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900664 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900665 } else {
666 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
667 }
668 case keyTag:
669 if key, ok := child.(*apexKey); ok {
670 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900671 if !key.installable() && ctx.Config().Debuggable() {
672 // If the key is not installed, bundled it with the APEX.
673 // Note: this bundled key is valid only for non-production builds
674 // (eng/userdebug).
675 pubKeyFile = key.public_key_file
676 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900677 return false
678 } else {
679 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900680 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900681 case certificateTag:
682 if dep, ok := child.(*java.AndroidAppCertificate); ok {
683 certificate = dep.Certificate
684 return false
685 } else {
686 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
687 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900688 }
689 } else {
690 // indirect dependencies
691 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
692 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900693 if cc.IsStubs() || cc.HasStubsVariants() {
694 return false
695 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900696 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800697 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900698 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900699 return true
700 }
701 }
702 }
703 return false
704 })
705
Jiyong Park9335a262018-12-24 11:31:58 +0900706 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park23c52b02019-02-02 13:13:47 +0900707 if keyFile == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900708 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
709 return
710 }
711
Jiyong Park8fd61922018-11-08 02:50:25 +0900712 // remove duplicates in filesInfo
713 removeDup := func(filesInfo []apexFile) []apexFile {
714 encountered := make(map[android.Path]bool)
715 result := []apexFile{}
716 for _, f := range filesInfo {
717 if !encountered[f.builtFile] {
718 encountered[f.builtFile] = true
719 result = append(result, f)
720 }
721 }
722 return result
723 }
724 filesInfo = removeDup(filesInfo)
725
726 // to have consistent build rules
727 sort.Slice(filesInfo, func(i, j int) bool {
728 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
729 })
730
731 // prepend the name of this APEX to the module names. These names will be the names of
732 // modules that will be defined if the APEX is flattened.
733 for i := range filesInfo {
734 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
735 }
736
Jiyong Park8fd61922018-11-08 02:50:25 +0900737 a.installDir = android.PathForModuleInstall(ctx, "apex")
738 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800739
740 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900741 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800742 }
743 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900744 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
745 // is true. This is to support referencing APEX via ":<module_name" syntax
746 // in other modules. It is in AndroidMk where the selection of flattened
747 // or unflattened APEX is made.
748 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
749 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900750 }
751}
752
Jiyong Park835d82b2018-12-27 16:04:18 +0900753func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
754 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900755 cert := String(a.properties.Certificate)
756 if cert != "" && android.SrcIsModule(cert) == "" {
757 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
758 certificate = java.Certificate{
759 defaultDir.Join(ctx, cert+".x509.pem"),
760 defaultDir.Join(ctx, cert+".pk8"),
761 }
762 } else if cert == "" {
763 pem, key := ctx.Config().DefaultAppCertificate(ctx)
764 certificate = java.Certificate{pem, key}
765 }
766
Dario Freni4abb1dc2018-11-20 18:04:58 +0000767 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900768
Alex Light5098a612018-11-29 17:12:15 -0800769 var abis []string
770 for _, target := range ctx.MultiTargets() {
771 if len(target.Arch.Abi) > 0 {
772 abis = append(abis, target.Arch.Abi[0])
773 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900774 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900775
Alex Light5098a612018-11-29 17:12:15 -0800776 abis = android.FirstUniqueStrings(abis)
777
778 suffix := apexType.suffix()
779 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900780
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900781 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900782 for _, f := range a.filesInfo {
783 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900784 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900785
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900786 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900787 for i, src := range filesToCopy {
788 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800789 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900790 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
791 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800792 for _, sym := range a.filesInfo[i].symlinks {
793 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
794 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
795 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900796 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900797 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800798 implicitInputs = append(implicitInputs, manifest)
799
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900800 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
801 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900802
Alex Light5098a612018-11-29 17:12:15 -0800803 if apexType.image() {
804 // files and dirs that will be created in APEX
805 var readOnlyPaths []string
806 var executablePaths []string // this also includes dirs
807 for _, f := range a.filesInfo {
808 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
809 if f.installDir == "bin" {
810 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800811 for _, s := range f.symlinks {
812 executablePaths = append(executablePaths, filepath.Join("bin", s))
813 }
Alex Light5098a612018-11-29 17:12:15 -0800814 } else {
815 readOnlyPaths = append(readOnlyPaths, pathInApex)
816 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900817 dir := f.installDir
818 for !android.InList(dir, executablePaths) && dir != "" {
819 executablePaths = append(executablePaths, dir)
820 dir, _ = filepath.Split(dir) // move up to the parent
821 if len(dir) > 0 {
822 // remove trailing slash
823 dir = dir[:len(dir)-1]
824 }
Alex Light5098a612018-11-29 17:12:15 -0800825 }
826 }
827 sort.Strings(readOnlyPaths)
828 sort.Strings(executablePaths)
829 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
830 ctx.Build(pctx, android.BuildParams{
831 Rule: generateFsConfig,
832 Output: cannedFsConfig,
833 Description: "generate fs config",
834 Args: map[string]string{
835 "ro_paths": strings.Join(readOnlyPaths, " "),
836 "exec_paths": strings.Join(executablePaths, " "),
837 },
838 })
839
840 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
841 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
842 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
843 if !fileContextsOptionalPath.Valid() {
844 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
845 return
846 }
847 fileContexts := fileContextsOptionalPath.Path()
848
Jiyong Park835d82b2018-12-27 16:04:18 +0900849 optFlags := []string{}
850
Alex Light5098a612018-11-29 17:12:15 -0800851 // Additional implicit inputs.
852 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900853 if pubKeyFile != nil {
854 implicitInputs = append(implicitInputs, pubKeyFile)
855 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
856 }
Alex Light5098a612018-11-29 17:12:15 -0800857
Jiyong Park7f67f482019-01-05 12:57:48 +0900858 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
859 if overridden {
860 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
861 }
862
Alex Light5098a612018-11-29 17:12:15 -0800863 ctx.Build(pctx, android.BuildParams{
864 Rule: apexRule,
865 Implicits: implicitInputs,
866 Output: unsignedOutputFile,
867 Description: "apex (" + apexType.name() + ")",
868 Args: map[string]string{
869 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
870 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
871 "copy_commands": strings.Join(copyCommands, " && "),
872 "manifest": manifest.String(),
873 "file_contexts": fileContexts.String(),
874 "canned_fs_config": cannedFsConfig.String(),
875 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900876 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800877 },
878 })
879
880 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
881 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
882 a.bundleModuleFile = bundleModuleFile
883
884 ctx.Build(pctx, android.BuildParams{
885 Rule: apexProtoConvertRule,
886 Input: unsignedOutputFile,
887 Output: apexProtoFile,
888 Description: "apex proto convert",
889 })
890
891 ctx.Build(pctx, android.BuildParams{
892 Rule: apexBundleRule,
893 Input: apexProtoFile,
894 Output: a.bundleModuleFile,
895 Description: "apex bundle module",
896 Args: map[string]string{
897 "abi": strings.Join(abis, "."),
898 },
899 })
900 } else {
901 ctx.Build(pctx, android.BuildParams{
902 Rule: zipApexRule,
903 Implicits: implicitInputs,
904 Output: unsignedOutputFile,
905 Description: "apex (" + apexType.name() + ")",
906 Args: map[string]string{
907 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
908 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
909 "copy_commands": strings.Join(copyCommands, " && "),
910 "manifest": manifest.String(),
911 },
912 })
Colin Crossa4925902018-11-16 11:36:28 -0800913 }
Colin Crossa4925902018-11-16 11:36:28 -0800914
Alex Light5098a612018-11-29 17:12:15 -0800915 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900916 ctx.Build(pctx, android.BuildParams{
917 Rule: java.Signapk,
918 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800919 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900920 Input: unsignedOutputFile,
921 Args: map[string]string{
922 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900923 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900924 },
925 })
Alex Light5098a612018-11-29 17:12:15 -0800926
927 // Install to $OUT/soong/{target,host}/.../apex
Jiyong Park23c52b02019-02-02 13:13:47 +0900928 if a.installable() && !ctx.Config().FlattenApex() {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900929 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
930 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900931}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900932
Jiyong Park8fd61922018-11-08 02:50:25 +0900933func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900934 if a.installable() {
935 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
936 // with other ordinary files.
937 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +0900938
939 // rename to apex_manifest.json
940 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
941 ctx.Build(pctx, android.BuildParams{
942 Rule: android.Cp,
943 Input: manifest,
944 Output: copiedManifest,
945 })
Jiyong Park719b4462019-01-13 00:39:51 +0900946 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900947
Jiyong Park23c52b02019-02-02 13:13:47 +0900948 if ctx.Config().FlattenApex() {
949 for _, fi := range a.filesInfo {
950 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
951 ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
952 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900953 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900954 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900955}
956
957func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -0800958 writers := []android.AndroidMkData{}
959 if a.apexTypes.image() {
960 writers = append(writers, a.androidMkForType(imageApex))
961 }
962 if a.apexTypes.zip() {
963 writers = append(writers, a.androidMkForType(zipApex))
964 }
965 return android.AndroidMkData{
966 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
967 for _, data := range writers {
968 data.Custom(w, name, prefix, moduleDir, data)
969 }
970 }}
971}
972
Jiyong Park94427262019-02-05 23:18:47 +0900973func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string) []string {
974 moduleNames := []string{}
975
976 for _, fi := range a.filesInfo {
977 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
978 continue
979 }
980 if !android.InList(fi.moduleName, moduleNames) {
981 moduleNames = append(moduleNames, fi.moduleName)
982 }
983 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
984 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
985 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
986 if a.flattened {
987 // /system/apex/<name>/{lib|framework|...}
988 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
989 a.installDir.RelPathString(), name, fi.installDir))
990 } else {
991 // /apex/<name>/{lib|framework|...}
992 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(PRODUCT_OUT)",
993 "apex", name, fi.installDir))
994 }
995 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
996 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
997 if fi.module != nil {
998 archStr := fi.module.Target().Arch.ArchType.String()
999 host := false
1000 switch fi.module.Target().Os.Class {
1001 case android.Host:
1002 if archStr != "common" {
1003 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1004 }
1005 host = true
1006 case android.HostCross:
1007 if archStr != "common" {
1008 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1009 }
1010 host = true
1011 case android.Device:
1012 if archStr != "common" {
1013 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1014 }
1015 }
1016 if host {
1017 makeOs := fi.module.Target().Os.String()
1018 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1019 makeOs = "linux"
1020 }
1021 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1022 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1023 }
1024 }
1025 if fi.class == javaSharedLib {
1026 javaModule := fi.module.(*java.Library)
1027 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1028 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1029 // we will have foo.jar.jar
1030 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1031 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1032 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1033 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1034 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1035 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1036 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1037 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1038 if cc, ok := fi.module.(*cc.Module); ok && cc.UnstrippedOutputFile() != nil {
1039 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1040 }
1041 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1042 } else {
1043 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1044 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1045 }
1046 }
1047 return moduleNames
1048}
1049
Alex Light5098a612018-11-29 17:12:15 -08001050func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001051 return android.AndroidMkData{
1052 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1053 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001054 if a.installable() {
Jiyong Park41229f52019-02-07 16:46:59 +09001055 moduleNames = a.androidMkForFiles(w, name, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001056 }
1057
Jiyong Park719b4462019-01-13 00:39:51 +09001058 if a.flattened && apexType.image() {
1059 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001060 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1061 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1062 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001063 if len(moduleNames) > 0 {
1064 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1065 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001066 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001067 } else {
Alex Light5098a612018-11-29 17:12:15 -08001068 // zip-apex is the less common type so have the name refer to the image-apex
1069 // only and use {name}.zip if you want the zip-apex
1070 if apexType == zipApex && a.apexTypes == both {
1071 name = name + ".zip"
1072 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001073 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1074 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1075 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1076 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001077 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001078 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001079 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001080 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +09001081 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park94427262019-02-05 23:18:47 +09001082 if len(moduleNames) > 0 {
1083 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1084 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001085 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001086
Alex Light5098a612018-11-29 17:12:15 -08001087 if apexType == imageApex {
1088 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1089 }
Jiyong Park719b4462019-01-13 00:39:51 +09001090 }
1091 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001092}
1093
Alex Lightee250722018-12-06 14:00:02 -08001094func ApexBundleFactory() android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001095 module := &apexBundle{
1096 outputFiles: map[apexPackaging]android.WritablePath{},
1097 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001098 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001099 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001100 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001101 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1102 })
Alex Light5098a612018-11-29 17:12:15 -08001103 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001104 android.InitDefaultableModule(module)
1105 return module
1106}
Jiyong Park30ca9372019-02-07 16:27:23 +09001107
1108//
1109// Defaults
1110//
1111type Defaults struct {
1112 android.ModuleBase
1113 android.DefaultsModuleBase
1114}
1115
1116func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1117}
1118
1119func defaultsFactory() android.Module {
1120 return DefaultsFactory()
1121}
1122
1123func DefaultsFactory(props ...interface{}) android.Module {
1124 module := &Defaults{}
1125
1126 module.AddProperties(props...)
1127 module.AddProperties(
1128 &apexBundleProperties{},
1129 &apexTargetBundleProperties{},
1130 )
1131
1132 android.InitDefaultsModule(module)
1133 return module
1134}