blob: 95cee0cd65eaa7da3807ab1b3d15f13dc33fd8a6 [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 Light0851b882019-02-07 13:20:53 -0800139 android.RegisterModuleType("apex", apexBundleFactory)
140 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900141 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900142
143 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
144 ctx.TopDown("apex_deps", apexDepsMutator)
145 ctx.BottomUp("apex", apexMutator)
146 })
147}
148
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900149// Mark the direct and transitive dependencies of apex bundles so that they
150// can be built for the apex bundles.
151func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800152 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800153 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900154 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900155 depName := mctx.OtherModuleName(child)
156 // If the parent is apexBundle, this child is directly depended.
157 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800158 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800159 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
160 // non-installable apex's cannot be installed and so should not prevent libraries from being
161 // installed to the system.
162 android.UpdateApexDependency(apexBundleName, depName, directDep)
163 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900164
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900165 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900166 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900167 return true
168 } else {
169 return false
170 }
171 })
172 }
173}
174
175// Create apex variations if a module is included in APEX(s).
176func apexMutator(mctx android.BottomUpMutatorContext) {
177 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900178 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900179 } else if _, ok := mctx.Module().(*apexBundle); ok {
180 // apex bundle itself is mutated so that it and its modules have same
181 // apex variant.
182 apexBundleName := mctx.ModuleName()
183 mctx.CreateVariations(apexBundleName)
184 }
185}
186
Alex Light9670d332019-01-29 18:07:33 -0800187type apexNativeDependencies struct {
188 // List of native libraries
189 Native_shared_libs []string
190 // List of native executables
191 Binaries []string
192}
193type apexMultilibProperties struct {
194 // Native dependencies whose compile_multilib is "first"
195 First apexNativeDependencies
196
197 // Native dependencies whose compile_multilib is "both"
198 Both apexNativeDependencies
199
200 // Native dependencies whose compile_multilib is "prefer32"
201 Prefer32 apexNativeDependencies
202
203 // Native dependencies whose compile_multilib is "32"
204 Lib32 apexNativeDependencies
205
206 // Native dependencies whose compile_multilib is "64"
207 Lib64 apexNativeDependencies
208}
209
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900210type apexBundleProperties struct {
211 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000212 // "apex_manifest.json"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900213 Manifest *string
214
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900215 // Determines the file contexts file for setting security context to each file in this APEX bundle.
216 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
217 // used.
218 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900219 File_contexts *string
220
221 // List of native shared libs that are embedded inside this APEX bundle
222 Native_shared_libs []string
223
224 // List of native executables that are embedded inside this APEX bundle
225 Binaries []string
226
227 // List of java libraries that are embedded inside this APEX bundle
228 Java_libs []string
229
230 // List of prebuilt files that are embedded inside this APEX bundle
231 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900232
233 // Name of the apex_key module that provides the private key to sign APEX
234 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900235
Alex Light5098a612018-11-29 17:12:15 -0800236 // The type of APEX to build. Controls what the APEX payload is. Either
237 // 'image', 'zip' or 'both'. Default: 'image'.
238 Payload_type *string
239
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900240 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
241 // or an android_app_certificate module name in the form ":module".
242 Certificate *string
243
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900244 // Whether this APEX is installable to one of the partitions. Default: true.
245 Installable *bool
246
Jiyong Parkda6eb592018-12-19 17:12:36 +0900247 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
248 // Default is false.
249 Use_vendor *bool
250
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800251 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
252 Ignore_system_library_special_case *bool
253
Alex Light9670d332019-01-29 18:07:33 -0800254 Multilib apexMultilibProperties
255}
256
257type apexTargetBundleProperties struct {
258 Target struct {
259 // Multilib properties only for android.
260 Android struct {
261 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900262 }
Alex Light9670d332019-01-29 18:07:33 -0800263 // Multilib properties only for host.
264 Host struct {
265 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900266 }
Alex Light9670d332019-01-29 18:07:33 -0800267 // Multilib properties only for host linux_bionic.
268 Linux_bionic struct {
269 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900270 }
Alex Light9670d332019-01-29 18:07:33 -0800271 // Multilib properties only for host linux_glibc.
272 Linux_glibc struct {
273 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900274 }
275 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900276}
277
Jiyong Park8fd61922018-11-08 02:50:25 +0900278type apexFileClass int
279
280const (
281 etc apexFileClass = iota
282 nativeSharedLib
283 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900284 shBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900285 javaSharedLib
286)
287
Alex Light5098a612018-11-29 17:12:15 -0800288type apexPackaging int
289
290const (
291 imageApex apexPackaging = iota
292 zipApex
293 both
294)
295
296func (a apexPackaging) image() bool {
297 switch a {
298 case imageApex, both:
299 return true
300 }
301 return false
302}
303
304func (a apexPackaging) zip() bool {
305 switch a {
306 case zipApex, both:
307 return true
308 }
309 return false
310}
311
312func (a apexPackaging) suffix() string {
313 switch a {
314 case imageApex:
315 return imageApexSuffix
316 case zipApex:
317 return zipApexSuffix
318 case both:
319 panic(fmt.Errorf("must be either zip or image"))
320 default:
321 panic(fmt.Errorf("unkonwn APEX type %d", a))
322 }
323}
324
325func (a apexPackaging) name() string {
326 switch a {
327 case imageApex:
328 return imageApexType
329 case zipApex:
330 return zipApexType
331 case both:
332 panic(fmt.Errorf("must be either zip or image"))
333 default:
334 panic(fmt.Errorf("unkonwn APEX type %d", a))
335 }
336}
337
Jiyong Park8fd61922018-11-08 02:50:25 +0900338func (class apexFileClass) NameInMake() string {
339 switch class {
340 case etc:
341 return "ETC"
342 case nativeSharedLib:
343 return "SHARED_LIBRARIES"
Jiyong Park04480cf2019-02-06 00:16:29 +0900344 case nativeExecutable, shBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900345 return "EXECUTABLES"
346 case javaSharedLib:
347 return "JAVA_LIBRARIES"
348 default:
349 panic(fmt.Errorf("unkonwn class %d", class))
350 }
351}
352
353type apexFile struct {
354 builtFile android.Path
355 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900356 installDir string
357 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900358 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800359 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900360}
361
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900362type apexBundle struct {
363 android.ModuleBase
364 android.DefaultableModuleBase
365
Alex Light9670d332019-01-29 18:07:33 -0800366 properties apexBundleProperties
367 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900368
Alex Light5098a612018-11-29 17:12:15 -0800369 apexTypes apexPackaging
370
Colin Crossa4925902018-11-16 11:36:28 -0800371 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800372 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800373 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900374
375 // list of files to be included in this apex
376 filesInfo []apexFile
377
378 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800379
380 testApex bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900381}
382
Jiyong Park397e55e2018-10-24 21:09:55 +0900383func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900384 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900385 // Use *FarVariation* to be able to depend on modules having
386 // conflicting variations with this module. This is required since
387 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
388 // for native shared libs.
389 ctx.AddFarVariationDependencies([]blueprint.Variation{
390 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900391 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900392 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900393 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900394 }, sharedLibTag, native_shared_libs...)
395
396 ctx.AddFarVariationDependencies([]blueprint.Variation{
397 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900398 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900399 }, executableTag, binaries...)
400}
401
Alex Light9670d332019-01-29 18:07:33 -0800402func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
403 if ctx.Os().Class == android.Device {
404 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
405 } else {
406 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
407 if ctx.Os().Bionic() {
408 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
409 } else {
410 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
411 }
412 }
413}
414
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900415func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800416
Jiyong Park397e55e2018-10-24 21:09:55 +0900417 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900418 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800419
420 a.combineProperties(ctx)
421
Jiyong Park397e55e2018-10-24 21:09:55 +0900422 has32BitTarget := false
423 for _, target := range targets {
424 if target.Arch.ArchType.Multilib == "lib32" {
425 has32BitTarget = true
426 }
427 }
428 for i, target := range targets {
429 // When multilib.* is omitted for native_shared_libs, it implies
430 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900431 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900432 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900433 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900434 {Mutator: "link", Variation: "shared"},
435 }, sharedLibTag, a.properties.Native_shared_libs...)
436
Jiyong Park397e55e2018-10-24 21:09:55 +0900437 // Add native modules targetting both ABIs
438 addDependenciesForNativeModules(ctx,
439 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900440 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900441 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900442
Alex Light3d673592019-01-18 14:37:31 -0800443 isPrimaryAbi := i == 0
444 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900445 // When multilib.* is omitted for binaries, it implies
446 // multilib.first.
447 ctx.AddFarVariationDependencies([]blueprint.Variation{
448 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900449 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900450 }, executableTag, a.properties.Binaries...)
451
452 // Add native modules targetting the first ABI
453 addDependenciesForNativeModules(ctx,
454 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900455 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900456 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800457
458 // When multilib.* is omitted for prebuilts, it implies multilib.first.
459 ctx.AddFarVariationDependencies([]blueprint.Variation{
460 {Mutator: "arch", Variation: target.String()},
461 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900462 }
463
464 switch target.Arch.ArchType.Multilib {
465 case "lib32":
466 // Add native modules targetting 32-bit ABI
467 addDependenciesForNativeModules(ctx,
468 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900469 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900470 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900471
472 addDependenciesForNativeModules(ctx,
473 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900474 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900475 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900476 case "lib64":
477 // Add native modules targetting 64-bit ABI
478 addDependenciesForNativeModules(ctx,
479 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900480 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900481 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900482
483 if !has32BitTarget {
484 addDependenciesForNativeModules(ctx,
485 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900486 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900487 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900488 }
489 }
490
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900491 }
492
Jiyong Parkff1458f2018-10-12 21:49:38 +0900493 ctx.AddFarVariationDependencies([]blueprint.Variation{
494 {Mutator: "arch", Variation: "android_common"},
495 }, javaLibTag, a.properties.Java_libs...)
496
Jiyong Park23c52b02019-02-02 13:13:47 +0900497 if String(a.properties.Key) == "" {
498 ctx.ModuleErrorf("key is missing")
499 return
500 }
501 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900502
Jiyong Park23c52b02019-02-02 13:13:47 +0900503 cert := android.SrcIsModule(String(a.properties.Certificate))
504 if cert != "" {
505 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900506 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900507}
508
Jiyong Park74e240b2018-11-27 21:27:08 +0900509func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900510 if file, ok := a.outputFiles[imageApex]; ok {
511 return android.Paths{file}
512 } else {
513 return nil
514 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900515}
516
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900517func (a *apexBundle) installable() bool {
518 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
519}
520
Jiyong Park7c1dc612019-01-05 11:15:24 +0900521func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
522 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900523 return "vendor"
524 } else {
525 return "core"
526 }
527}
528
Jiyong Park388ef3f2019-01-28 19:47:32 +0900529func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
530 globalSanitizerNames := []string{}
531 if a.Host() {
532 globalSanitizerNames = ctx.Config().SanitizeHost()
533 } else {
534 arches := ctx.Config().SanitizeDeviceArch()
535 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
536 globalSanitizerNames = ctx.Config().SanitizeDevice()
537 }
538 }
539 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900540}
541
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800542func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900543 // Decide the APEX-local directory by the multilib of the library
544 // In the future, we may query this to the module.
545 switch cc.Arch().ArchType.Multilib {
546 case "lib32":
547 dirInApex = "lib"
548 case "lib64":
549 dirInApex = "lib64"
550 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900551 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900552 if !cc.Arch().Native {
553 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
554 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800555 if handleSpecialLibs {
556 switch cc.Name() {
557 case "libc", "libm", "libdl":
558 // Special case for bionic libs. This is to prevent the bionic libs
559 // from being included in the search path /apex/com.android.apex/lib.
560 // This exclusion is required because bionic libs in the runtime APEX
561 // are available via the legacy paths /system/lib/libc.so, etc. By the
562 // init process, the bionic libs in the APEX are bind-mounted to the
563 // legacy paths and thus will be loaded into the default linker namespace.
564 // If the bionic libs are directly in /apex/com.android.apex/lib then
565 // the same libs will be again loaded to the runtime linker namespace,
566 // which will result double loading of bionic libs that isn't supported.
567 dirInApex = filepath.Join(dirInApex, "bionic")
568 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900569 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900570
571 fileToCopy = cc.OutputFile().Path()
572 return
573}
574
575func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900576 // TODO(b/123721777) respect relative_install_path also for binaries
577 // dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900578 dirInApex = "bin"
579 fileToCopy = cc.OutputFile().Path()
580 return
581}
582
Jiyong Park04480cf2019-02-06 00:16:29 +0900583func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
584 dirInApex = filepath.Join("bin", sh.SubDir())
585 fileToCopy = sh.OutputFile()
586 return
587}
588
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900589func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
590 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900591 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900592 return
593}
594
595func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
596 dirInApex = filepath.Join("etc", prebuilt.SubDir())
597 fileToCopy = prebuilt.OutputFile()
598 return
599}
600
601func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900602 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900603
Jiyong Parkff1458f2018-10-12 21:49:38 +0900604 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900605 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900606 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900607
Alex Light5098a612018-11-29 17:12:15 -0800608 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
609 a.apexTypes = imageApex
610 } else if *a.properties.Payload_type == "zip" {
611 a.apexTypes = zipApex
612 } else if *a.properties.Payload_type == "both" {
613 a.apexTypes = both
614 } else {
615 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
616 return
617 }
618
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800619 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
620
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900621 ctx.WalkDeps(func(child, parent android.Module) bool {
622 if _, ok := parent.(*apexBundle); ok {
623 // direct dependencies
624 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900625 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626 switch depTag {
627 case sharedLibTag:
628 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800629 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900630 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900631 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900632 } else {
633 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900634 }
635 case executableTag:
636 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800637 if !cc.Arch().Native {
638 // There is only one 'bin' directory so we shouldn't bother copying in
639 // native-bridge'd binaries and only use main ones.
640 return true
641 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900642 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900643 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900644 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900645 } else if sh, ok := child.(*android.ShBinary); ok {
646 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
647 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900648 } else {
Jiyong Park04480cf2019-02-06 00:16:29 +0900649 ctx.PropertyErrorf("binaries", "%q is neithher cc_binary nor sh_binary", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900650 }
651 case javaLibTag:
652 if java, ok := child.(*java.Library); ok {
653 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900654 if fileToCopy == nil {
655 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
656 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900657 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900658 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900659 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900660 } else {
661 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900662 }
663 case prebuiltTag:
664 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
665 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900666 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900667 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900668 } else {
669 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
670 }
671 case keyTag:
672 if key, ok := child.(*apexKey); ok {
673 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900674 if !key.installable() && ctx.Config().Debuggable() {
675 // If the key is not installed, bundled it with the APEX.
676 // Note: this bundled key is valid only for non-production builds
677 // (eng/userdebug).
678 pubKeyFile = key.public_key_file
679 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900680 return false
681 } else {
682 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900683 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900684 case certificateTag:
685 if dep, ok := child.(*java.AndroidAppCertificate); ok {
686 certificate = dep.Certificate
687 return false
688 } else {
689 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
690 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900691 }
692 } else {
693 // indirect dependencies
694 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
695 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900696 if cc.IsStubs() || cc.HasStubsVariants() {
697 return false
698 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900699 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800700 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900701 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900702 return true
703 }
704 }
705 }
706 return false
707 })
708
Jiyong Park9335a262018-12-24 11:31:58 +0900709 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park23c52b02019-02-02 13:13:47 +0900710 if keyFile == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900711 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
712 return
713 }
714
Jiyong Park8fd61922018-11-08 02:50:25 +0900715 // remove duplicates in filesInfo
716 removeDup := func(filesInfo []apexFile) []apexFile {
717 encountered := make(map[android.Path]bool)
718 result := []apexFile{}
719 for _, f := range filesInfo {
720 if !encountered[f.builtFile] {
721 encountered[f.builtFile] = true
722 result = append(result, f)
723 }
724 }
725 return result
726 }
727 filesInfo = removeDup(filesInfo)
728
729 // to have consistent build rules
730 sort.Slice(filesInfo, func(i, j int) bool {
731 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
732 })
733
734 // prepend the name of this APEX to the module names. These names will be the names of
735 // modules that will be defined if the APEX is flattened.
736 for i := range filesInfo {
737 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
738 }
739
Jiyong Park8fd61922018-11-08 02:50:25 +0900740 a.installDir = android.PathForModuleInstall(ctx, "apex")
741 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800742
743 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900744 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800745 }
746 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900747 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
748 // is true. This is to support referencing APEX via ":<module_name" syntax
749 // in other modules. It is in AndroidMk where the selection of flattened
750 // or unflattened APEX is made.
751 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
752 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900753 }
754}
755
Jiyong Park835d82b2018-12-27 16:04:18 +0900756func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
757 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900758 cert := String(a.properties.Certificate)
759 if cert != "" && android.SrcIsModule(cert) == "" {
760 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
761 certificate = java.Certificate{
762 defaultDir.Join(ctx, cert+".x509.pem"),
763 defaultDir.Join(ctx, cert+".pk8"),
764 }
765 } else if cert == "" {
766 pem, key := ctx.Config().DefaultAppCertificate(ctx)
767 certificate = java.Certificate{pem, key}
768 }
769
Dario Freni4abb1dc2018-11-20 18:04:58 +0000770 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900771
Alex Light5098a612018-11-29 17:12:15 -0800772 var abis []string
773 for _, target := range ctx.MultiTargets() {
774 if len(target.Arch.Abi) > 0 {
775 abis = append(abis, target.Arch.Abi[0])
776 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900777 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900778
Alex Light5098a612018-11-29 17:12:15 -0800779 abis = android.FirstUniqueStrings(abis)
780
781 suffix := apexType.suffix()
782 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900783
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900784 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900785 for _, f := range a.filesInfo {
786 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900787 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900788
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900789 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900790 for i, src := range filesToCopy {
791 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800792 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900793 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
794 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800795 for _, sym := range a.filesInfo[i].symlinks {
796 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
797 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
798 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900799 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900800 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800801 implicitInputs = append(implicitInputs, manifest)
802
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900803 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
804 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900805
Alex Light5098a612018-11-29 17:12:15 -0800806 if apexType.image() {
807 // files and dirs that will be created in APEX
808 var readOnlyPaths []string
809 var executablePaths []string // this also includes dirs
810 for _, f := range a.filesInfo {
811 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
812 if f.installDir == "bin" {
813 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800814 for _, s := range f.symlinks {
815 executablePaths = append(executablePaths, filepath.Join("bin", s))
816 }
Alex Light5098a612018-11-29 17:12:15 -0800817 } else {
818 readOnlyPaths = append(readOnlyPaths, pathInApex)
819 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900820 dir := f.installDir
821 for !android.InList(dir, executablePaths) && dir != "" {
822 executablePaths = append(executablePaths, dir)
823 dir, _ = filepath.Split(dir) // move up to the parent
824 if len(dir) > 0 {
825 // remove trailing slash
826 dir = dir[:len(dir)-1]
827 }
Alex Light5098a612018-11-29 17:12:15 -0800828 }
829 }
830 sort.Strings(readOnlyPaths)
831 sort.Strings(executablePaths)
832 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
833 ctx.Build(pctx, android.BuildParams{
834 Rule: generateFsConfig,
835 Output: cannedFsConfig,
836 Description: "generate fs config",
837 Args: map[string]string{
838 "ro_paths": strings.Join(readOnlyPaths, " "),
839 "exec_paths": strings.Join(executablePaths, " "),
840 },
841 })
842
843 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
844 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
845 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
846 if !fileContextsOptionalPath.Valid() {
847 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
848 return
849 }
850 fileContexts := fileContextsOptionalPath.Path()
851
Jiyong Park835d82b2018-12-27 16:04:18 +0900852 optFlags := []string{}
853
Alex Light5098a612018-11-29 17:12:15 -0800854 // Additional implicit inputs.
855 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900856 if pubKeyFile != nil {
857 implicitInputs = append(implicitInputs, pubKeyFile)
858 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
859 }
Alex Light5098a612018-11-29 17:12:15 -0800860
Jiyong Park7f67f482019-01-05 12:57:48 +0900861 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
862 if overridden {
863 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
864 }
865
Alex Light5098a612018-11-29 17:12:15 -0800866 ctx.Build(pctx, android.BuildParams{
867 Rule: apexRule,
868 Implicits: implicitInputs,
869 Output: unsignedOutputFile,
870 Description: "apex (" + apexType.name() + ")",
871 Args: map[string]string{
872 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
873 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
874 "copy_commands": strings.Join(copyCommands, " && "),
875 "manifest": manifest.String(),
876 "file_contexts": fileContexts.String(),
877 "canned_fs_config": cannedFsConfig.String(),
878 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900879 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800880 },
881 })
882
883 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
884 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
885 a.bundleModuleFile = bundleModuleFile
886
887 ctx.Build(pctx, android.BuildParams{
888 Rule: apexProtoConvertRule,
889 Input: unsignedOutputFile,
890 Output: apexProtoFile,
891 Description: "apex proto convert",
892 })
893
894 ctx.Build(pctx, android.BuildParams{
895 Rule: apexBundleRule,
896 Input: apexProtoFile,
897 Output: a.bundleModuleFile,
898 Description: "apex bundle module",
899 Args: map[string]string{
900 "abi": strings.Join(abis, "."),
901 },
902 })
903 } else {
904 ctx.Build(pctx, android.BuildParams{
905 Rule: zipApexRule,
906 Implicits: implicitInputs,
907 Output: unsignedOutputFile,
908 Description: "apex (" + apexType.name() + ")",
909 Args: map[string]string{
910 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
911 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
912 "copy_commands": strings.Join(copyCommands, " && "),
913 "manifest": manifest.String(),
914 },
915 })
Colin Crossa4925902018-11-16 11:36:28 -0800916 }
Colin Crossa4925902018-11-16 11:36:28 -0800917
Alex Light5098a612018-11-29 17:12:15 -0800918 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900919 ctx.Build(pctx, android.BuildParams{
920 Rule: java.Signapk,
921 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800922 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900923 Input: unsignedOutputFile,
924 Args: map[string]string{
925 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900926 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900927 },
928 })
Alex Light5098a612018-11-29 17:12:15 -0800929
930 // Install to $OUT/soong/{target,host}/.../apex
Jiyong Park23c52b02019-02-02 13:13:47 +0900931 if a.installable() && !ctx.Config().FlattenApex() {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900932 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
933 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900934}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900935
Jiyong Park8fd61922018-11-08 02:50:25 +0900936func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900937 if a.installable() {
938 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
939 // with other ordinary files.
940 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +0900941
942 // rename to apex_manifest.json
943 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
944 ctx.Build(pctx, android.BuildParams{
945 Rule: android.Cp,
946 Input: manifest,
947 Output: copiedManifest,
948 })
Jiyong Park719b4462019-01-13 00:39:51 +0900949 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900950
Jiyong Park23c52b02019-02-02 13:13:47 +0900951 if ctx.Config().FlattenApex() {
952 for _, fi := range a.filesInfo {
953 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
954 ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
955 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900956 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900957 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900958}
959
960func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -0800961 writers := []android.AndroidMkData{}
962 if a.apexTypes.image() {
963 writers = append(writers, a.androidMkForType(imageApex))
964 }
965 if a.apexTypes.zip() {
966 writers = append(writers, a.androidMkForType(zipApex))
967 }
968 return android.AndroidMkData{
969 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
970 for _, data := range writers {
971 data.Custom(w, name, prefix, moduleDir, data)
972 }
973 }}
974}
975
Jiyong Park94427262019-02-05 23:18:47 +0900976func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string) []string {
977 moduleNames := []string{}
978
979 for _, fi := range a.filesInfo {
980 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
981 continue
982 }
983 if !android.InList(fi.moduleName, moduleNames) {
984 moduleNames = append(moduleNames, fi.moduleName)
985 }
986 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
987 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
988 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
989 if a.flattened {
990 // /system/apex/<name>/{lib|framework|...}
991 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
992 a.installDir.RelPathString(), name, fi.installDir))
993 } else {
994 // /apex/<name>/{lib|framework|...}
995 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(PRODUCT_OUT)",
996 "apex", name, fi.installDir))
997 }
998 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
999 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1000 if fi.module != nil {
1001 archStr := fi.module.Target().Arch.ArchType.String()
1002 host := false
1003 switch fi.module.Target().Os.Class {
1004 case android.Host:
1005 if archStr != "common" {
1006 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1007 }
1008 host = true
1009 case android.HostCross:
1010 if archStr != "common" {
1011 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1012 }
1013 host = true
1014 case android.Device:
1015 if archStr != "common" {
1016 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1017 }
1018 }
1019 if host {
1020 makeOs := fi.module.Target().Os.String()
1021 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1022 makeOs = "linux"
1023 }
1024 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1025 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1026 }
1027 }
1028 if fi.class == javaSharedLib {
1029 javaModule := fi.module.(*java.Library)
1030 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1031 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1032 // we will have foo.jar.jar
1033 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1034 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1035 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1036 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1037 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1038 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1039 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1040 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1041 if cc, ok := fi.module.(*cc.Module); ok && cc.UnstrippedOutputFile() != nil {
1042 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1043 }
1044 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1045 } else {
1046 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1047 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1048 }
1049 }
1050 return moduleNames
1051}
1052
Alex Light5098a612018-11-29 17:12:15 -08001053func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001054 return android.AndroidMkData{
1055 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1056 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001057 if a.installable() {
Jiyong Park41229f52019-02-07 16:46:59 +09001058 moduleNames = a.androidMkForFiles(w, name, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001059 }
1060
Jiyong Park719b4462019-01-13 00:39:51 +09001061 if a.flattened && apexType.image() {
1062 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001063 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1064 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1065 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001066 if len(moduleNames) > 0 {
1067 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1068 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001069 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001070 } else {
Alex Light5098a612018-11-29 17:12:15 -08001071 // zip-apex is the less common type so have the name refer to the image-apex
1072 // only and use {name}.zip if you want the zip-apex
1073 if apexType == zipApex && a.apexTypes == both {
1074 name = name + ".zip"
1075 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001076 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1077 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1078 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1079 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001080 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001081 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001082 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001083 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +09001084 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park94427262019-02-05 23:18:47 +09001085 if len(moduleNames) > 0 {
1086 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1087 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001088 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001089
Alex Light5098a612018-11-29 17:12:15 -08001090 if apexType == imageApex {
1091 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1092 }
Jiyong Park719b4462019-01-13 00:39:51 +09001093 }
1094 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001095}
1096
Alex Light0851b882019-02-07 13:20:53 -08001097func testApexBundleFactory() android.Module {
1098 return ApexBundleFactory( /*testApex*/ true)
1099}
1100
1101func apexBundleFactory() android.Module {
1102 return ApexBundleFactory( /*testApex*/ false)
1103}
1104
1105func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001106 module := &apexBundle{
1107 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001108 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001109 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001110 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001111 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001112 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001113 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1114 })
Alex Light5098a612018-11-29 17:12:15 -08001115 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001116 android.InitDefaultableModule(module)
1117 return module
1118}
Jiyong Park30ca9372019-02-07 16:27:23 +09001119
1120//
1121// Defaults
1122//
1123type Defaults struct {
1124 android.ModuleBase
1125 android.DefaultsModuleBase
1126}
1127
1128func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1129}
1130
1131func defaultsFactory() android.Module {
1132 return DefaultsFactory()
1133}
1134
1135func DefaultsFactory(props ...interface{}) android.Module {
1136 module := &Defaults{}
1137
1138 module.AddProperties(props...)
1139 module.AddProperties(
1140 &apexBundleProperties{},
1141 &apexTargetBundleProperties{},
1142 )
1143
1144 android.InitDefaultsModule(module)
1145 return module
1146}