blob: 06e6c7840de5284e3a2fc68e606c9ba5a041d473 [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 Park48ca7dc2018-10-10 14:01:00 +0900140
141 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
142 ctx.TopDown("apex_deps", apexDepsMutator)
143 ctx.BottomUp("apex", apexMutator)
144 })
145}
146
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900147// Mark the direct and transitive dependencies of apex bundles so that they
148// can be built for the apex bundles.
149func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800150 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800151 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900152 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900153 depName := mctx.OtherModuleName(child)
154 // If the parent is apexBundle, this child is directly depended.
155 _, directDep := parent.(*apexBundle)
Alex Lightf98087f2019-02-04 14:45:06 -0800156 if a.installable() {
157 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
158 // non-installable apex's cannot be installed and so should not prevent libraries from being
159 // installed to the system.
160 android.UpdateApexDependency(apexBundleName, depName, directDep)
161 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900162
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900163 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900164 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900165 return true
166 } else {
167 return false
168 }
169 })
170 }
171}
172
173// Create apex variations if a module is included in APEX(s).
174func apexMutator(mctx android.BottomUpMutatorContext) {
175 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900176 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900177 } else if _, ok := mctx.Module().(*apexBundle); ok {
178 // apex bundle itself is mutated so that it and its modules have same
179 // apex variant.
180 apexBundleName := mctx.ModuleName()
181 mctx.CreateVariations(apexBundleName)
182 }
183}
184
Alex Light9670d332019-01-29 18:07:33 -0800185type apexNativeDependencies struct {
186 // List of native libraries
187 Native_shared_libs []string
188 // List of native executables
189 Binaries []string
190}
191type apexMultilibProperties struct {
192 // Native dependencies whose compile_multilib is "first"
193 First apexNativeDependencies
194
195 // Native dependencies whose compile_multilib is "both"
196 Both apexNativeDependencies
197
198 // Native dependencies whose compile_multilib is "prefer32"
199 Prefer32 apexNativeDependencies
200
201 // Native dependencies whose compile_multilib is "32"
202 Lib32 apexNativeDependencies
203
204 // Native dependencies whose compile_multilib is "64"
205 Lib64 apexNativeDependencies
206}
207
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900208type apexBundleProperties struct {
209 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000210 // "apex_manifest.json"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900211 Manifest *string
212
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900213 // Determines the file contexts file for setting security context to each file in this APEX bundle.
214 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
215 // used.
216 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900217 File_contexts *string
218
219 // List of native shared libs that are embedded inside this APEX bundle
220 Native_shared_libs []string
221
222 // List of native executables that are embedded inside this APEX bundle
223 Binaries []string
224
225 // List of java libraries that are embedded inside this APEX bundle
226 Java_libs []string
227
228 // List of prebuilt files that are embedded inside this APEX bundle
229 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900230
231 // Name of the apex_key module that provides the private key to sign APEX
232 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900233
Alex Light5098a612018-11-29 17:12:15 -0800234 // The type of APEX to build. Controls what the APEX payload is. Either
235 // 'image', 'zip' or 'both'. Default: 'image'.
236 Payload_type *string
237
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900238 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
239 // or an android_app_certificate module name in the form ":module".
240 Certificate *string
241
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900242 // Whether this APEX is installable to one of the partitions. Default: true.
243 Installable *bool
244
Jiyong Parkda6eb592018-12-19 17:12:36 +0900245 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
246 // Default is false.
247 Use_vendor *bool
248
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800249 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
250 Ignore_system_library_special_case *bool
251
Alex Light9670d332019-01-29 18:07:33 -0800252 Multilib apexMultilibProperties
253}
254
255type apexTargetBundleProperties struct {
256 Target struct {
257 // Multilib properties only for android.
258 Android struct {
259 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900260 }
Alex Light9670d332019-01-29 18:07:33 -0800261 // Multilib properties only for host.
262 Host struct {
263 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900264 }
Alex Light9670d332019-01-29 18:07:33 -0800265 // Multilib properties only for host linux_bionic.
266 Linux_bionic struct {
267 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900268 }
Alex Light9670d332019-01-29 18:07:33 -0800269 // Multilib properties only for host linux_glibc.
270 Linux_glibc struct {
271 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900272 }
273 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900274}
275
Jiyong Park8fd61922018-11-08 02:50:25 +0900276type apexFileClass int
277
278const (
279 etc apexFileClass = iota
280 nativeSharedLib
281 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900282 shBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900283 javaSharedLib
284)
285
Alex Light5098a612018-11-29 17:12:15 -0800286type apexPackaging int
287
288const (
289 imageApex apexPackaging = iota
290 zipApex
291 both
292)
293
294func (a apexPackaging) image() bool {
295 switch a {
296 case imageApex, both:
297 return true
298 }
299 return false
300}
301
302func (a apexPackaging) zip() bool {
303 switch a {
304 case zipApex, both:
305 return true
306 }
307 return false
308}
309
310func (a apexPackaging) suffix() string {
311 switch a {
312 case imageApex:
313 return imageApexSuffix
314 case zipApex:
315 return zipApexSuffix
316 case both:
317 panic(fmt.Errorf("must be either zip or image"))
318 default:
319 panic(fmt.Errorf("unkonwn APEX type %d", a))
320 }
321}
322
323func (a apexPackaging) name() string {
324 switch a {
325 case imageApex:
326 return imageApexType
327 case zipApex:
328 return zipApexType
329 case both:
330 panic(fmt.Errorf("must be either zip or image"))
331 default:
332 panic(fmt.Errorf("unkonwn APEX type %d", a))
333 }
334}
335
Jiyong Park8fd61922018-11-08 02:50:25 +0900336func (class apexFileClass) NameInMake() string {
337 switch class {
338 case etc:
339 return "ETC"
340 case nativeSharedLib:
341 return "SHARED_LIBRARIES"
Jiyong Park04480cf2019-02-06 00:16:29 +0900342 case nativeExecutable, shBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900343 return "EXECUTABLES"
344 case javaSharedLib:
345 return "JAVA_LIBRARIES"
346 default:
347 panic(fmt.Errorf("unkonwn class %d", class))
348 }
349}
350
351type apexFile struct {
352 builtFile android.Path
353 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900354 installDir string
355 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900356 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800357 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900358}
359
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900360type apexBundle struct {
361 android.ModuleBase
362 android.DefaultableModuleBase
363
Alex Light9670d332019-01-29 18:07:33 -0800364 properties apexBundleProperties
365 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900366
Alex Light5098a612018-11-29 17:12:15 -0800367 apexTypes apexPackaging
368
Colin Crossa4925902018-11-16 11:36:28 -0800369 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800370 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800371 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900372
373 // list of files to be included in this apex
374 filesInfo []apexFile
375
376 flattened bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900377}
378
Jiyong Park397e55e2018-10-24 21:09:55 +0900379func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900380 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900381 // Use *FarVariation* to be able to depend on modules having
382 // conflicting variations with this module. This is required since
383 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
384 // for native shared libs.
385 ctx.AddFarVariationDependencies([]blueprint.Variation{
386 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900387 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900388 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900389 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900390 }, sharedLibTag, native_shared_libs...)
391
392 ctx.AddFarVariationDependencies([]blueprint.Variation{
393 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900394 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900395 }, executableTag, binaries...)
396}
397
Alex Light9670d332019-01-29 18:07:33 -0800398func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
399 if ctx.Os().Class == android.Device {
400 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
401 } else {
402 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
403 if ctx.Os().Bionic() {
404 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
405 } else {
406 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
407 }
408 }
409}
410
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900411func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800412
Jiyong Park397e55e2018-10-24 21:09:55 +0900413 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900414 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800415
416 a.combineProperties(ctx)
417
Jiyong Park397e55e2018-10-24 21:09:55 +0900418 has32BitTarget := false
419 for _, target := range targets {
420 if target.Arch.ArchType.Multilib == "lib32" {
421 has32BitTarget = true
422 }
423 }
424 for i, target := range targets {
425 // When multilib.* is omitted for native_shared_libs, it implies
426 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900427 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900428 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900429 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900430 {Mutator: "link", Variation: "shared"},
431 }, sharedLibTag, a.properties.Native_shared_libs...)
432
Jiyong Park397e55e2018-10-24 21:09:55 +0900433 // Add native modules targetting both ABIs
434 addDependenciesForNativeModules(ctx,
435 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900436 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900437 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900438
Alex Light3d673592019-01-18 14:37:31 -0800439 isPrimaryAbi := i == 0
440 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900441 // When multilib.* is omitted for binaries, it implies
442 // multilib.first.
443 ctx.AddFarVariationDependencies([]blueprint.Variation{
444 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900445 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900446 }, executableTag, a.properties.Binaries...)
447
448 // Add native modules targetting the first ABI
449 addDependenciesForNativeModules(ctx,
450 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900451 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900452 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800453
454 // When multilib.* is omitted for prebuilts, it implies multilib.first.
455 ctx.AddFarVariationDependencies([]blueprint.Variation{
456 {Mutator: "arch", Variation: target.String()},
457 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900458 }
459
460 switch target.Arch.ArchType.Multilib {
461 case "lib32":
462 // Add native modules targetting 32-bit ABI
463 addDependenciesForNativeModules(ctx,
464 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900465 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900466 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900467
468 addDependenciesForNativeModules(ctx,
469 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900470 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900471 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900472 case "lib64":
473 // Add native modules targetting 64-bit ABI
474 addDependenciesForNativeModules(ctx,
475 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900476 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900477 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900478
479 if !has32BitTarget {
480 addDependenciesForNativeModules(ctx,
481 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900482 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900483 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900484 }
485 }
486
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900487 }
488
Jiyong Parkff1458f2018-10-12 21:49:38 +0900489 ctx.AddFarVariationDependencies([]blueprint.Variation{
490 {Mutator: "arch", Variation: "android_common"},
491 }, javaLibTag, a.properties.Java_libs...)
492
Jiyong Park23c52b02019-02-02 13:13:47 +0900493 if String(a.properties.Key) == "" {
494 ctx.ModuleErrorf("key is missing")
495 return
496 }
497 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900498
Jiyong Park23c52b02019-02-02 13:13:47 +0900499 cert := android.SrcIsModule(String(a.properties.Certificate))
500 if cert != "" {
501 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900502 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900503}
504
Jiyong Park74e240b2018-11-27 21:27:08 +0900505func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900506 if file, ok := a.outputFiles[imageApex]; ok {
507 return android.Paths{file}
508 } else {
509 return nil
510 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900511}
512
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900513func (a *apexBundle) installable() bool {
514 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
515}
516
Jiyong Park7c1dc612019-01-05 11:15:24 +0900517func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
518 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900519 return "vendor"
520 } else {
521 return "core"
522 }
523}
524
Jiyong Park388ef3f2019-01-28 19:47:32 +0900525func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
526 globalSanitizerNames := []string{}
527 if a.Host() {
528 globalSanitizerNames = ctx.Config().SanitizeHost()
529 } else {
530 arches := ctx.Config().SanitizeDeviceArch()
531 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
532 globalSanitizerNames = ctx.Config().SanitizeDevice()
533 }
534 }
535 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900536}
537
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800538func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900539 // Decide the APEX-local directory by the multilib of the library
540 // In the future, we may query this to the module.
541 switch cc.Arch().ArchType.Multilib {
542 case "lib32":
543 dirInApex = "lib"
544 case "lib64":
545 dirInApex = "lib64"
546 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900547 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900548 if !cc.Arch().Native {
549 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
550 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800551 if handleSpecialLibs {
552 switch cc.Name() {
553 case "libc", "libm", "libdl":
554 // Special case for bionic libs. This is to prevent the bionic libs
555 // from being included in the search path /apex/com.android.apex/lib.
556 // This exclusion is required because bionic libs in the runtime APEX
557 // are available via the legacy paths /system/lib/libc.so, etc. By the
558 // init process, the bionic libs in the APEX are bind-mounted to the
559 // legacy paths and thus will be loaded into the default linker namespace.
560 // If the bionic libs are directly in /apex/com.android.apex/lib then
561 // the same libs will be again loaded to the runtime linker namespace,
562 // which will result double loading of bionic libs that isn't supported.
563 dirInApex = filepath.Join(dirInApex, "bionic")
564 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900565 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900566
567 fileToCopy = cc.OutputFile().Path()
568 return
569}
570
571func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900572 // TODO(b/123721777) respect relative_install_path also for binaries
573 // dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900574 dirInApex = "bin"
575 fileToCopy = cc.OutputFile().Path()
576 return
577}
578
Jiyong Park04480cf2019-02-06 00:16:29 +0900579func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
580 dirInApex = filepath.Join("bin", sh.SubDir())
581 fileToCopy = sh.OutputFile()
582 return
583}
584
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900585func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
586 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900587 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900588 return
589}
590
591func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
592 dirInApex = filepath.Join("etc", prebuilt.SubDir())
593 fileToCopy = prebuilt.OutputFile()
594 return
595}
596
597func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900598 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900599
Jiyong Parkff1458f2018-10-12 21:49:38 +0900600 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900601 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900602 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900603
Alex Light5098a612018-11-29 17:12:15 -0800604 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
605 a.apexTypes = imageApex
606 } else if *a.properties.Payload_type == "zip" {
607 a.apexTypes = zipApex
608 } else if *a.properties.Payload_type == "both" {
609 a.apexTypes = both
610 } else {
611 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
612 return
613 }
614
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800615 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
616
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900617 ctx.WalkDeps(func(child, parent android.Module) bool {
618 if _, ok := parent.(*apexBundle); ok {
619 // direct dependencies
620 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900621 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900622 switch depTag {
623 case sharedLibTag:
624 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800625 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900626 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900627 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900628 } else {
629 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900630 }
631 case executableTag:
632 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800633 if !cc.Arch().Native {
634 // There is only one 'bin' directory so we shouldn't bother copying in
635 // native-bridge'd binaries and only use main ones.
636 return true
637 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900638 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900639 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900640 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900641 } else if sh, ok := child.(*android.ShBinary); ok {
642 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
643 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900644 } else {
Jiyong Park04480cf2019-02-06 00:16:29 +0900645 ctx.PropertyErrorf("binaries", "%q is neithher cc_binary nor sh_binary", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900646 }
647 case javaLibTag:
648 if java, ok := child.(*java.Library); ok {
649 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900650 if fileToCopy == nil {
651 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
652 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900653 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900654 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900655 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900656 } else {
657 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900658 }
659 case prebuiltTag:
660 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
661 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900662 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900663 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900664 } else {
665 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
666 }
667 case keyTag:
668 if key, ok := child.(*apexKey); ok {
669 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900670 if !key.installable() && ctx.Config().Debuggable() {
671 // If the key is not installed, bundled it with the APEX.
672 // Note: this bundled key is valid only for non-production builds
673 // (eng/userdebug).
674 pubKeyFile = key.public_key_file
675 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900676 return false
677 } else {
678 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900679 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900680 case certificateTag:
681 if dep, ok := child.(*java.AndroidAppCertificate); ok {
682 certificate = dep.Certificate
683 return false
684 } else {
685 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
686 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900687 }
688 } else {
689 // indirect dependencies
690 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
691 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900692 if cc.IsStubs() || cc.HasStubsVariants() {
693 return false
694 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900695 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800696 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900697 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900698 return true
699 }
700 }
701 }
702 return false
703 })
704
Jiyong Park9335a262018-12-24 11:31:58 +0900705 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park23c52b02019-02-02 13:13:47 +0900706 if keyFile == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900707 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
708 return
709 }
710
Jiyong Park8fd61922018-11-08 02:50:25 +0900711 // remove duplicates in filesInfo
712 removeDup := func(filesInfo []apexFile) []apexFile {
713 encountered := make(map[android.Path]bool)
714 result := []apexFile{}
715 for _, f := range filesInfo {
716 if !encountered[f.builtFile] {
717 encountered[f.builtFile] = true
718 result = append(result, f)
719 }
720 }
721 return result
722 }
723 filesInfo = removeDup(filesInfo)
724
725 // to have consistent build rules
726 sort.Slice(filesInfo, func(i, j int) bool {
727 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
728 })
729
730 // prepend the name of this APEX to the module names. These names will be the names of
731 // modules that will be defined if the APEX is flattened.
732 for i := range filesInfo {
733 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
734 }
735
Jiyong Park8fd61922018-11-08 02:50:25 +0900736 a.installDir = android.PathForModuleInstall(ctx, "apex")
737 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800738
739 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900740 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800741 }
742 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900743 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
744 // is true. This is to support referencing APEX via ":<module_name" syntax
745 // in other modules. It is in AndroidMk where the selection of flattened
746 // or unflattened APEX is made.
747 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
748 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900749 }
750}
751
Jiyong Park835d82b2018-12-27 16:04:18 +0900752func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
753 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900754 cert := String(a.properties.Certificate)
755 if cert != "" && android.SrcIsModule(cert) == "" {
756 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
757 certificate = java.Certificate{
758 defaultDir.Join(ctx, cert+".x509.pem"),
759 defaultDir.Join(ctx, cert+".pk8"),
760 }
761 } else if cert == "" {
762 pem, key := ctx.Config().DefaultAppCertificate(ctx)
763 certificate = java.Certificate{pem, key}
764 }
765
Dario Freni4abb1dc2018-11-20 18:04:58 +0000766 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900767
Alex Light5098a612018-11-29 17:12:15 -0800768 var abis []string
769 for _, target := range ctx.MultiTargets() {
770 if len(target.Arch.Abi) > 0 {
771 abis = append(abis, target.Arch.Abi[0])
772 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900773 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900774
Alex Light5098a612018-11-29 17:12:15 -0800775 abis = android.FirstUniqueStrings(abis)
776
777 suffix := apexType.suffix()
778 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900779
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900780 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900781 for _, f := range a.filesInfo {
782 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900783 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900784
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900785 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900786 for i, src := range filesToCopy {
787 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800788 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900789 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
790 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800791 for _, sym := range a.filesInfo[i].symlinks {
792 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
793 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
794 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900795 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900796 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800797 implicitInputs = append(implicitInputs, manifest)
798
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900799 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
800 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900801
Alex Light5098a612018-11-29 17:12:15 -0800802 if apexType.image() {
803 // files and dirs that will be created in APEX
804 var readOnlyPaths []string
805 var executablePaths []string // this also includes dirs
806 for _, f := range a.filesInfo {
807 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
808 if f.installDir == "bin" {
809 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800810 for _, s := range f.symlinks {
811 executablePaths = append(executablePaths, filepath.Join("bin", s))
812 }
Alex Light5098a612018-11-29 17:12:15 -0800813 } else {
814 readOnlyPaths = append(readOnlyPaths, pathInApex)
815 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900816 dir := f.installDir
817 for !android.InList(dir, executablePaths) && dir != "" {
818 executablePaths = append(executablePaths, dir)
819 dir, _ = filepath.Split(dir) // move up to the parent
820 if len(dir) > 0 {
821 // remove trailing slash
822 dir = dir[:len(dir)-1]
823 }
Alex Light5098a612018-11-29 17:12:15 -0800824 }
825 }
826 sort.Strings(readOnlyPaths)
827 sort.Strings(executablePaths)
828 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
829 ctx.Build(pctx, android.BuildParams{
830 Rule: generateFsConfig,
831 Output: cannedFsConfig,
832 Description: "generate fs config",
833 Args: map[string]string{
834 "ro_paths": strings.Join(readOnlyPaths, " "),
835 "exec_paths": strings.Join(executablePaths, " "),
836 },
837 })
838
839 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
840 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
841 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
842 if !fileContextsOptionalPath.Valid() {
843 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
844 return
845 }
846 fileContexts := fileContextsOptionalPath.Path()
847
Jiyong Park835d82b2018-12-27 16:04:18 +0900848 optFlags := []string{}
849
Alex Light5098a612018-11-29 17:12:15 -0800850 // Additional implicit inputs.
851 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900852 if pubKeyFile != nil {
853 implicitInputs = append(implicitInputs, pubKeyFile)
854 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
855 }
Alex Light5098a612018-11-29 17:12:15 -0800856
Jiyong Park7f67f482019-01-05 12:57:48 +0900857 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
858 if overridden {
859 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
860 }
861
Alex Light5098a612018-11-29 17:12:15 -0800862 ctx.Build(pctx, android.BuildParams{
863 Rule: apexRule,
864 Implicits: implicitInputs,
865 Output: unsignedOutputFile,
866 Description: "apex (" + apexType.name() + ")",
867 Args: map[string]string{
868 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
869 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
870 "copy_commands": strings.Join(copyCommands, " && "),
871 "manifest": manifest.String(),
872 "file_contexts": fileContexts.String(),
873 "canned_fs_config": cannedFsConfig.String(),
874 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900875 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800876 },
877 })
878
879 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
880 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
881 a.bundleModuleFile = bundleModuleFile
882
883 ctx.Build(pctx, android.BuildParams{
884 Rule: apexProtoConvertRule,
885 Input: unsignedOutputFile,
886 Output: apexProtoFile,
887 Description: "apex proto convert",
888 })
889
890 ctx.Build(pctx, android.BuildParams{
891 Rule: apexBundleRule,
892 Input: apexProtoFile,
893 Output: a.bundleModuleFile,
894 Description: "apex bundle module",
895 Args: map[string]string{
896 "abi": strings.Join(abis, "."),
897 },
898 })
899 } else {
900 ctx.Build(pctx, android.BuildParams{
901 Rule: zipApexRule,
902 Implicits: implicitInputs,
903 Output: unsignedOutputFile,
904 Description: "apex (" + apexType.name() + ")",
905 Args: map[string]string{
906 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
907 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
908 "copy_commands": strings.Join(copyCommands, " && "),
909 "manifest": manifest.String(),
910 },
911 })
Colin Crossa4925902018-11-16 11:36:28 -0800912 }
Colin Crossa4925902018-11-16 11:36:28 -0800913
Alex Light5098a612018-11-29 17:12:15 -0800914 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900915 ctx.Build(pctx, android.BuildParams{
916 Rule: java.Signapk,
917 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800918 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900919 Input: unsignedOutputFile,
920 Args: map[string]string{
921 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900922 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900923 },
924 })
Alex Light5098a612018-11-29 17:12:15 -0800925
926 // Install to $OUT/soong/{target,host}/.../apex
Jiyong Park23c52b02019-02-02 13:13:47 +0900927 if a.installable() && !ctx.Config().FlattenApex() {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900928 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
929 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900930}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900931
Jiyong Park8fd61922018-11-08 02:50:25 +0900932func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900933 if a.installable() {
934 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
935 // with other ordinary files.
936 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +0900937
938 // rename to apex_manifest.json
939 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
940 ctx.Build(pctx, android.BuildParams{
941 Rule: android.Cp,
942 Input: manifest,
943 Output: copiedManifest,
944 })
Jiyong Park719b4462019-01-13 00:39:51 +0900945 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900946
Jiyong Park23c52b02019-02-02 13:13:47 +0900947 if ctx.Config().FlattenApex() {
948 for _, fi := range a.filesInfo {
949 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
950 ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
951 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900952 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900953 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900954}
955
956func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -0800957 writers := []android.AndroidMkData{}
958 if a.apexTypes.image() {
959 writers = append(writers, a.androidMkForType(imageApex))
960 }
961 if a.apexTypes.zip() {
962 writers = append(writers, a.androidMkForType(zipApex))
963 }
964 return android.AndroidMkData{
965 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
966 for _, data := range writers {
967 data.Custom(w, name, prefix, moduleDir, data)
968 }
969 }}
970}
971
Jiyong Park94427262019-02-05 23:18:47 +0900972func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string) []string {
973 moduleNames := []string{}
974
975 for _, fi := range a.filesInfo {
976 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
977 continue
978 }
979 if !android.InList(fi.moduleName, moduleNames) {
980 moduleNames = append(moduleNames, fi.moduleName)
981 }
982 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
983 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
984 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
985 if a.flattened {
986 // /system/apex/<name>/{lib|framework|...}
987 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
988 a.installDir.RelPathString(), name, fi.installDir))
989 } else {
990 // /apex/<name>/{lib|framework|...}
991 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(PRODUCT_OUT)",
992 "apex", name, fi.installDir))
993 }
994 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
995 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
996 if fi.module != nil {
997 archStr := fi.module.Target().Arch.ArchType.String()
998 host := false
999 switch fi.module.Target().Os.Class {
1000 case android.Host:
1001 if archStr != "common" {
1002 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1003 }
1004 host = true
1005 case android.HostCross:
1006 if archStr != "common" {
1007 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1008 }
1009 host = true
1010 case android.Device:
1011 if archStr != "common" {
1012 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1013 }
1014 }
1015 if host {
1016 makeOs := fi.module.Target().Os.String()
1017 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1018 makeOs = "linux"
1019 }
1020 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1021 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1022 }
1023 }
1024 if fi.class == javaSharedLib {
1025 javaModule := fi.module.(*java.Library)
1026 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1027 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1028 // we will have foo.jar.jar
1029 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1030 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1031 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1032 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1033 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1034 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1035 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1036 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1037 if cc, ok := fi.module.(*cc.Module); ok && cc.UnstrippedOutputFile() != nil {
1038 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1039 }
1040 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1041 } else {
1042 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1043 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1044 }
1045 }
1046 return moduleNames
1047}
1048
Alex Light5098a612018-11-29 17:12:15 -08001049func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001050 return android.AndroidMkData{
1051 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1052 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001053 if a.installable() {
Jiyong Park41229f52019-02-07 16:46:59 +09001054 moduleNames = a.androidMkForFiles(w, name, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001055 }
1056
Jiyong Park719b4462019-01-13 00:39:51 +09001057 if a.flattened && apexType.image() {
1058 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001059 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1060 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1061 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001062 if len(moduleNames) > 0 {
1063 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1064 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001065 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001066 } else {
Alex Light5098a612018-11-29 17:12:15 -08001067 // zip-apex is the less common type so have the name refer to the image-apex
1068 // only and use {name}.zip if you want the zip-apex
1069 if apexType == zipApex && a.apexTypes == both {
1070 name = name + ".zip"
1071 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001072 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1073 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1074 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1075 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001076 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001077 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001078 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001079 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +09001080 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park94427262019-02-05 23:18:47 +09001081 if len(moduleNames) > 0 {
1082 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1083 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001084 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001085
Alex Light5098a612018-11-29 17:12:15 -08001086 if apexType == imageApex {
1087 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1088 }
Jiyong Park719b4462019-01-13 00:39:51 +09001089 }
1090 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001091}
1092
Alex Lightee250722018-12-06 14:00:02 -08001093func ApexBundleFactory() android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001094 module := &apexBundle{
1095 outputFiles: map[apexPackaging]android.WritablePath{},
1096 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001097 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001098 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001099 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001100 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1101 })
Alex Light5098a612018-11-29 17:12:15 -08001102 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001103 android.InitDefaultableModule(module)
1104 return module
1105}