blob: a5556d1a1f4c78db723878f9f4cc44c740863b49 [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) {
150 if _, 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)
156 android.UpdateApexDependency(apexBundleName, depName, directDep)
157
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900158 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900159 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160 return true
161 } else {
162 return false
163 }
164 })
165 }
166}
167
168// Create apex variations if a module is included in APEX(s).
169func apexMutator(mctx android.BottomUpMutatorContext) {
170 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900171 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900172 } else if _, ok := mctx.Module().(*apexBundle); ok {
173 // apex bundle itself is mutated so that it and its modules have same
174 // apex variant.
175 apexBundleName := mctx.ModuleName()
176 mctx.CreateVariations(apexBundleName)
177 }
178}
179
Alex Light9670d332019-01-29 18:07:33 -0800180type apexNativeDependencies struct {
181 // List of native libraries
182 Native_shared_libs []string
183 // List of native executables
184 Binaries []string
185}
186type apexMultilibProperties struct {
187 // Native dependencies whose compile_multilib is "first"
188 First apexNativeDependencies
189
190 // Native dependencies whose compile_multilib is "both"
191 Both apexNativeDependencies
192
193 // Native dependencies whose compile_multilib is "prefer32"
194 Prefer32 apexNativeDependencies
195
196 // Native dependencies whose compile_multilib is "32"
197 Lib32 apexNativeDependencies
198
199 // Native dependencies whose compile_multilib is "64"
200 Lib64 apexNativeDependencies
201}
202
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900203type apexBundleProperties struct {
204 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000205 // "apex_manifest.json"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900206 Manifest *string
207
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900208 // Determines the file contexts file for setting security context to each file in this APEX bundle.
209 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
210 // used.
211 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900212 File_contexts *string
213
214 // List of native shared libs that are embedded inside this APEX bundle
215 Native_shared_libs []string
216
217 // List of native executables that are embedded inside this APEX bundle
218 Binaries []string
219
220 // List of java libraries that are embedded inside this APEX bundle
221 Java_libs []string
222
223 // List of prebuilt files that are embedded inside this APEX bundle
224 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900225
226 // Name of the apex_key module that provides the private key to sign APEX
227 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900228
Alex Light5098a612018-11-29 17:12:15 -0800229 // The type of APEX to build. Controls what the APEX payload is. Either
230 // 'image', 'zip' or 'both'. Default: 'image'.
231 Payload_type *string
232
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900233 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
234 // or an android_app_certificate module name in the form ":module".
235 Certificate *string
236
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900237 // Whether this APEX is installable to one of the partitions. Default: true.
238 Installable *bool
239
Jiyong Parkda6eb592018-12-19 17:12:36 +0900240 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
241 // Default is false.
242 Use_vendor *bool
243
Alex Light9670d332019-01-29 18:07:33 -0800244 Multilib apexMultilibProperties
245}
246
247type apexTargetBundleProperties struct {
248 Target struct {
249 // Multilib properties only for android.
250 Android struct {
251 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900252 }
Alex Light9670d332019-01-29 18:07:33 -0800253 // Multilib properties only for host.
254 Host struct {
255 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900256 }
Alex Light9670d332019-01-29 18:07:33 -0800257 // Multilib properties only for host linux_bionic.
258 Linux_bionic 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 linux_glibc.
262 Linux_glibc struct {
263 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900264 }
265 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900266}
267
Jiyong Park8fd61922018-11-08 02:50:25 +0900268type apexFileClass int
269
270const (
271 etc apexFileClass = iota
272 nativeSharedLib
273 nativeExecutable
274 javaSharedLib
275)
276
Alex Light5098a612018-11-29 17:12:15 -0800277type apexPackaging int
278
279const (
280 imageApex apexPackaging = iota
281 zipApex
282 both
283)
284
285func (a apexPackaging) image() bool {
286 switch a {
287 case imageApex, both:
288 return true
289 }
290 return false
291}
292
293func (a apexPackaging) zip() bool {
294 switch a {
295 case zipApex, both:
296 return true
297 }
298 return false
299}
300
301func (a apexPackaging) suffix() string {
302 switch a {
303 case imageApex:
304 return imageApexSuffix
305 case zipApex:
306 return zipApexSuffix
307 case both:
308 panic(fmt.Errorf("must be either zip or image"))
309 default:
310 panic(fmt.Errorf("unkonwn APEX type %d", a))
311 }
312}
313
314func (a apexPackaging) name() string {
315 switch a {
316 case imageApex:
317 return imageApexType
318 case zipApex:
319 return zipApexType
320 case both:
321 panic(fmt.Errorf("must be either zip or image"))
322 default:
323 panic(fmt.Errorf("unkonwn APEX type %d", a))
324 }
325}
326
Jiyong Park8fd61922018-11-08 02:50:25 +0900327func (class apexFileClass) NameInMake() string {
328 switch class {
329 case etc:
330 return "ETC"
331 case nativeSharedLib:
332 return "SHARED_LIBRARIES"
333 case nativeExecutable:
334 return "EXECUTABLES"
335 case javaSharedLib:
336 return "JAVA_LIBRARIES"
337 default:
338 panic(fmt.Errorf("unkonwn class %d", class))
339 }
340}
341
342type apexFile struct {
343 builtFile android.Path
344 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900345 installDir string
346 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900347 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800348 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900349}
350
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900351type apexBundle struct {
352 android.ModuleBase
353 android.DefaultableModuleBase
354
Alex Light9670d332019-01-29 18:07:33 -0800355 properties apexBundleProperties
356 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900357
Alex Light5098a612018-11-29 17:12:15 -0800358 apexTypes apexPackaging
359
Colin Crossa4925902018-11-16 11:36:28 -0800360 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800361 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800362 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900363
364 // list of files to be included in this apex
365 filesInfo []apexFile
366
367 flattened bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900368}
369
Jiyong Park397e55e2018-10-24 21:09:55 +0900370func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900371 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900372 // Use *FarVariation* to be able to depend on modules having
373 // conflicting variations with this module. This is required since
374 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
375 // for native shared libs.
376 ctx.AddFarVariationDependencies([]blueprint.Variation{
377 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900378 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900379 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900380 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900381 }, sharedLibTag, native_shared_libs...)
382
383 ctx.AddFarVariationDependencies([]blueprint.Variation{
384 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900385 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900386 }, executableTag, binaries...)
387}
388
Alex Light9670d332019-01-29 18:07:33 -0800389func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
390 if ctx.Os().Class == android.Device {
391 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
392 } else {
393 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
394 if ctx.Os().Bionic() {
395 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
396 } else {
397 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
398 }
399 }
400}
401
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900402func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800403
Jiyong Park397e55e2018-10-24 21:09:55 +0900404 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900405 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800406
407 a.combineProperties(ctx)
408
Jiyong Park397e55e2018-10-24 21:09:55 +0900409 has32BitTarget := false
410 for _, target := range targets {
411 if target.Arch.ArchType.Multilib == "lib32" {
412 has32BitTarget = true
413 }
414 }
415 for i, target := range targets {
416 // When multilib.* is omitted for native_shared_libs, it implies
417 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900418 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900419 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900420 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900421 {Mutator: "link", Variation: "shared"},
422 }, sharedLibTag, a.properties.Native_shared_libs...)
423
Jiyong Park397e55e2018-10-24 21:09:55 +0900424 // Add native modules targetting both ABIs
425 addDependenciesForNativeModules(ctx,
426 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900427 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900428 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900429
Alex Light3d673592019-01-18 14:37:31 -0800430 isPrimaryAbi := i == 0
431 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900432 // When multilib.* is omitted for binaries, it implies
433 // multilib.first.
434 ctx.AddFarVariationDependencies([]blueprint.Variation{
435 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900436 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900437 }, executableTag, a.properties.Binaries...)
438
439 // Add native modules targetting the first ABI
440 addDependenciesForNativeModules(ctx,
441 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900442 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900443 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800444
445 // When multilib.* is omitted for prebuilts, it implies multilib.first.
446 ctx.AddFarVariationDependencies([]blueprint.Variation{
447 {Mutator: "arch", Variation: target.String()},
448 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900449 }
450
451 switch target.Arch.ArchType.Multilib {
452 case "lib32":
453 // Add native modules targetting 32-bit ABI
454 addDependenciesForNativeModules(ctx,
455 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900456 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900457 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900458
459 addDependenciesForNativeModules(ctx,
460 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900461 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900462 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900463 case "lib64":
464 // Add native modules targetting 64-bit ABI
465 addDependenciesForNativeModules(ctx,
466 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900467 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900468 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900469
470 if !has32BitTarget {
471 addDependenciesForNativeModules(ctx,
472 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900473 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900474 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900475 }
476 }
477
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900478 }
479
Jiyong Parkff1458f2018-10-12 21:49:38 +0900480 ctx.AddFarVariationDependencies([]blueprint.Variation{
481 {Mutator: "arch", Variation: "android_common"},
482 }, javaLibTag, a.properties.Java_libs...)
483
Jiyong Park9335a262018-12-24 11:31:58 +0900484 if !ctx.Config().FlattenApex() || ctx.Config().UnbundledBuild() {
485 if String(a.properties.Key) == "" {
486 ctx.ModuleErrorf("key is missing")
487 return
488 }
489 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900490
Jiyong Park9335a262018-12-24 11:31:58 +0900491 cert := android.SrcIsModule(String(a.properties.Certificate))
492 if cert != "" {
493 ctx.AddDependency(ctx.Module(), certificateTag, cert)
494 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900495 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900496}
497
Jiyong Park74e240b2018-11-27 21:27:08 +0900498func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900499 if file, ok := a.outputFiles[imageApex]; ok {
500 return android.Paths{file}
501 } else {
502 return nil
503 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900504}
505
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900506func (a *apexBundle) installable() bool {
507 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
508}
509
Jiyong Park7c1dc612019-01-05 11:15:24 +0900510func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
511 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900512 return "vendor"
513 } else {
514 return "core"
515 }
516}
517
Jiyong Park388ef3f2019-01-28 19:47:32 +0900518func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
519 globalSanitizerNames := []string{}
520 if a.Host() {
521 globalSanitizerNames = ctx.Config().SanitizeHost()
522 } else {
523 arches := ctx.Config().SanitizeDeviceArch()
524 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
525 globalSanitizerNames = ctx.Config().SanitizeDevice()
526 }
527 }
528 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900529}
530
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900531func getCopyManifestForNativeLibrary(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
532 // Decide the APEX-local directory by the multilib of the library
533 // In the future, we may query this to the module.
534 switch cc.Arch().ArchType.Multilib {
535 case "lib32":
536 dirInApex = "lib"
537 case "lib64":
538 dirInApex = "lib64"
539 }
540 if !cc.Arch().Native {
541 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
542 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900543 switch cc.Name() {
544 case "libc", "libm", "libdl":
545 // Special case for bionic libs. This is to prevent the bionic libs
546 // from being included in the search path /apex/com.android.apex/lib.
547 // This exclusion is required because bionic libs in the runtime APEX
548 // are available via the legacy paths /system/lib/libc.so, etc. By the
549 // init process, the bionic libs in the APEX are bind-mounted to the
550 // legacy paths and thus will be loaded into the default linker namespace.
551 // If the bionic libs are directly in /apex/com.android.apex/lib then
552 // the same libs will be again loaded to the runtime linker namespace,
553 // which will result double loading of bionic libs that isn't supported.
554 dirInApex = filepath.Join(dirInApex, "bionic")
555 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900556
557 fileToCopy = cc.OutputFile().Path()
558 return
559}
560
561func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
562 dirInApex = "bin"
563 fileToCopy = cc.OutputFile().Path()
564 return
565}
566
567func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
568 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900569 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900570 return
571}
572
573func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
574 dirInApex = filepath.Join("etc", prebuilt.SubDir())
575 fileToCopy = prebuilt.OutputFile()
576 return
577}
578
579func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900580 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900581
Jiyong Parkff1458f2018-10-12 21:49:38 +0900582 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900583 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900584 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900585
Alex Light5098a612018-11-29 17:12:15 -0800586 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
587 a.apexTypes = imageApex
588 } else if *a.properties.Payload_type == "zip" {
589 a.apexTypes = zipApex
590 } else if *a.properties.Payload_type == "both" {
591 a.apexTypes = both
592 } else {
593 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
594 return
595 }
596
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900597 ctx.WalkDeps(func(child, parent android.Module) bool {
598 if _, ok := parent.(*apexBundle); ok {
599 // direct dependencies
600 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900601 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900602 switch depTag {
603 case sharedLibTag:
604 if cc, ok := child.(*cc.Module); ok {
605 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900606 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900607 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900608 } else {
609 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900610 }
611 case executableTag:
612 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800613 if !cc.Arch().Native {
614 // There is only one 'bin' directory so we shouldn't bother copying in
615 // native-bridge'd binaries and only use main ones.
616 return true
617 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900618 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900619 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900620 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900621 } else {
622 ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900623 }
624 case javaLibTag:
625 if java, ok := child.(*java.Library); ok {
626 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900627 if fileToCopy == nil {
628 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
629 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900630 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900631 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900632 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900633 } else {
634 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900635 }
636 case prebuiltTag:
637 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
638 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900639 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900640 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900641 } else {
642 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
643 }
644 case keyTag:
645 if key, ok := child.(*apexKey); ok {
646 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900647 if !key.installable() && ctx.Config().Debuggable() {
648 // If the key is not installed, bundled it with the APEX.
649 // Note: this bundled key is valid only for non-production builds
650 // (eng/userdebug).
651 pubKeyFile = key.public_key_file
652 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900653 return false
654 } else {
655 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900656 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900657 case certificateTag:
658 if dep, ok := child.(*java.AndroidAppCertificate); ok {
659 certificate = dep.Certificate
660 return false
661 } else {
662 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
663 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900664 }
665 } else {
666 // indirect dependencies
667 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
668 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669 if cc.IsStubs() || cc.HasStubsVariants() {
670 return false
671 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900672 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900673 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900674 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900675 return true
676 }
677 }
678 }
679 return false
680 })
681
Jiyong Park9335a262018-12-24 11:31:58 +0900682 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
683 if !a.flattened && keyFile == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900684 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
685 return
686 }
687
Jiyong Park8fd61922018-11-08 02:50:25 +0900688 // remove duplicates in filesInfo
689 removeDup := func(filesInfo []apexFile) []apexFile {
690 encountered := make(map[android.Path]bool)
691 result := []apexFile{}
692 for _, f := range filesInfo {
693 if !encountered[f.builtFile] {
694 encountered[f.builtFile] = true
695 result = append(result, f)
696 }
697 }
698 return result
699 }
700 filesInfo = removeDup(filesInfo)
701
702 // to have consistent build rules
703 sort.Slice(filesInfo, func(i, j int) bool {
704 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
705 })
706
707 // prepend the name of this APEX to the module names. These names will be the names of
708 // modules that will be defined if the APEX is flattened.
709 for i := range filesInfo {
710 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
711 }
712
Jiyong Park8fd61922018-11-08 02:50:25 +0900713 a.installDir = android.PathForModuleInstall(ctx, "apex")
714 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800715
716 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900717 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800718 }
719 if a.apexTypes.image() {
720 if ctx.Config().FlattenApex() {
721 a.buildFlattenedApex(ctx)
722 } else {
Jiyong Park835d82b2018-12-27 16:04:18 +0900723 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
Alex Light5098a612018-11-29 17:12:15 -0800724 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900725 }
726}
727
Jiyong Park835d82b2018-12-27 16:04:18 +0900728func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
729 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900730 cert := String(a.properties.Certificate)
731 if cert != "" && android.SrcIsModule(cert) == "" {
732 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
733 certificate = java.Certificate{
734 defaultDir.Join(ctx, cert+".x509.pem"),
735 defaultDir.Join(ctx, cert+".pk8"),
736 }
737 } else if cert == "" {
738 pem, key := ctx.Config().DefaultAppCertificate(ctx)
739 certificate = java.Certificate{pem, key}
740 }
741
Dario Freni4abb1dc2018-11-20 18:04:58 +0000742 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900743
Alex Light5098a612018-11-29 17:12:15 -0800744 var abis []string
745 for _, target := range ctx.MultiTargets() {
746 if len(target.Arch.Abi) > 0 {
747 abis = append(abis, target.Arch.Abi[0])
748 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900749 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900750
Alex Light5098a612018-11-29 17:12:15 -0800751 abis = android.FirstUniqueStrings(abis)
752
753 suffix := apexType.suffix()
754 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900755
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900756 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900757 for _, f := range a.filesInfo {
758 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900759 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900760
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900761 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900762 for i, src := range filesToCopy {
763 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800764 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900765 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
766 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800767 for _, sym := range a.filesInfo[i].symlinks {
768 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
769 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
770 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900771 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900772 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800773 implicitInputs = append(implicitInputs, manifest)
774
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900775 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
776 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900777
Alex Light5098a612018-11-29 17:12:15 -0800778 if apexType.image() {
779 // files and dirs that will be created in APEX
780 var readOnlyPaths []string
781 var executablePaths []string // this also includes dirs
782 for _, f := range a.filesInfo {
783 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
784 if f.installDir == "bin" {
785 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800786 for _, s := range f.symlinks {
787 executablePaths = append(executablePaths, filepath.Join("bin", s))
788 }
Alex Light5098a612018-11-29 17:12:15 -0800789 } else {
790 readOnlyPaths = append(readOnlyPaths, pathInApex)
791 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900792 dir := f.installDir
793 for !android.InList(dir, executablePaths) && dir != "" {
794 executablePaths = append(executablePaths, dir)
795 dir, _ = filepath.Split(dir) // move up to the parent
796 if len(dir) > 0 {
797 // remove trailing slash
798 dir = dir[:len(dir)-1]
799 }
Alex Light5098a612018-11-29 17:12:15 -0800800 }
801 }
802 sort.Strings(readOnlyPaths)
803 sort.Strings(executablePaths)
804 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
805 ctx.Build(pctx, android.BuildParams{
806 Rule: generateFsConfig,
807 Output: cannedFsConfig,
808 Description: "generate fs config",
809 Args: map[string]string{
810 "ro_paths": strings.Join(readOnlyPaths, " "),
811 "exec_paths": strings.Join(executablePaths, " "),
812 },
813 })
814
815 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
816 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
817 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
818 if !fileContextsOptionalPath.Valid() {
819 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
820 return
821 }
822 fileContexts := fileContextsOptionalPath.Path()
823
Jiyong Park835d82b2018-12-27 16:04:18 +0900824 optFlags := []string{}
825
Alex Light5098a612018-11-29 17:12:15 -0800826 // Additional implicit inputs.
827 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900828 if pubKeyFile != nil {
829 implicitInputs = append(implicitInputs, pubKeyFile)
830 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
831 }
Alex Light5098a612018-11-29 17:12:15 -0800832
Jiyong Park7f67f482019-01-05 12:57:48 +0900833 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
834 if overridden {
835 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
836 }
837
Alex Light5098a612018-11-29 17:12:15 -0800838 ctx.Build(pctx, android.BuildParams{
839 Rule: apexRule,
840 Implicits: implicitInputs,
841 Output: unsignedOutputFile,
842 Description: "apex (" + apexType.name() + ")",
843 Args: map[string]string{
844 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
845 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
846 "copy_commands": strings.Join(copyCommands, " && "),
847 "manifest": manifest.String(),
848 "file_contexts": fileContexts.String(),
849 "canned_fs_config": cannedFsConfig.String(),
850 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900851 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800852 },
853 })
854
855 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
856 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
857 a.bundleModuleFile = bundleModuleFile
858
859 ctx.Build(pctx, android.BuildParams{
860 Rule: apexProtoConvertRule,
861 Input: unsignedOutputFile,
862 Output: apexProtoFile,
863 Description: "apex proto convert",
864 })
865
866 ctx.Build(pctx, android.BuildParams{
867 Rule: apexBundleRule,
868 Input: apexProtoFile,
869 Output: a.bundleModuleFile,
870 Description: "apex bundle module",
871 Args: map[string]string{
872 "abi": strings.Join(abis, "."),
873 },
874 })
875 } else {
876 ctx.Build(pctx, android.BuildParams{
877 Rule: zipApexRule,
878 Implicits: implicitInputs,
879 Output: unsignedOutputFile,
880 Description: "apex (" + apexType.name() + ")",
881 Args: map[string]string{
882 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
883 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
884 "copy_commands": strings.Join(copyCommands, " && "),
885 "manifest": manifest.String(),
886 },
887 })
Colin Crossa4925902018-11-16 11:36:28 -0800888 }
Colin Crossa4925902018-11-16 11:36:28 -0800889
Alex Light5098a612018-11-29 17:12:15 -0800890 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900891 ctx.Build(pctx, android.BuildParams{
892 Rule: java.Signapk,
893 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800894 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900895 Input: unsignedOutputFile,
896 Args: map[string]string{
897 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900898 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900899 },
900 })
Alex Light5098a612018-11-29 17:12:15 -0800901
902 // Install to $OUT/soong/{target,host}/.../apex
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900903 if a.installable() {
904 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
905 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900906}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900907
Jiyong Park8fd61922018-11-08 02:50:25 +0900908func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900909 if a.installable() {
910 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
911 // with other ordinary files.
912 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +0900913
914 // rename to apex_manifest.json
915 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
916 ctx.Build(pctx, android.BuildParams{
917 Rule: android.Cp,
918 Input: manifest,
919 Output: copiedManifest,
920 })
Jiyong Park719b4462019-01-13 00:39:51 +0900921 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900922
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900923 for _, fi := range a.filesInfo {
924 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
925 ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
926 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900927 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900928}
929
930func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -0800931 writers := []android.AndroidMkData{}
932 if a.apexTypes.image() {
933 writers = append(writers, a.androidMkForType(imageApex))
934 }
935 if a.apexTypes.zip() {
936 writers = append(writers, a.androidMkForType(zipApex))
937 }
938 return android.AndroidMkData{
939 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
940 for _, data := range writers {
941 data.Custom(w, name, prefix, moduleDir, data)
942 }
943 }}
944}
945
946func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +0900947 return android.AndroidMkData{
948 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
949 moduleNames := []string{}
950 for _, fi := range a.filesInfo {
951 if !android.InList(fi.moduleName, moduleNames) {
952 moduleNames = append(moduleNames, fi.moduleName)
953 }
954 }
955
956 for _, fi := range a.filesInfo {
957 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
958 continue
959 }
960 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
961 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
962 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
963 if a.flattened {
964 // /system/apex/<name>/{lib|framework|...}
965 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
966 a.installDir.RelPathString(), name, fi.installDir))
967 } else {
968 // /apex/<name>/{lib|framework|...}
969 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(PRODUCT_OUT)",
970 "apex", name, fi.installDir))
971 }
972 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
973 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
974 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
975 if fi.module != nil {
976 archStr := fi.module.Target().Arch.ArchType.String()
977 host := false
978 switch fi.module.Target().Os.Class {
979 case android.Host:
980 if archStr != "common" {
981 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
982 }
983 host = true
984 case android.HostCross:
985 if archStr != "common" {
986 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
987 }
988 host = true
989 case android.Device:
990 if archStr != "common" {
991 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
992 }
993 }
994 if host {
995 makeOs := fi.module.Target().Os.String()
996 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
997 makeOs = "linux"
998 }
999 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1000 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
Jiyong Park8fd61922018-11-08 02:50:25 +09001001 }
1002 }
Jiyong Park719b4462019-01-13 00:39:51 +09001003 if fi.class == javaSharedLib {
1004 javaModule := fi.module.(*java.Library)
1005 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1006 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1007 // we will have foo.jar.jar
1008 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1009 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1010 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1011 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1012 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1013 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1014 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1015 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1016 if cc, ok := fi.module.(*cc.Module); ok && cc.UnstrippedOutputFile() != nil {
1017 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1018 }
1019 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1020 } else {
1021 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1022 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1023 }
1024 }
1025 if a.flattened && apexType.image() {
1026 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001027 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1028 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1029 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1030 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1031 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001032 } else {
Alex Light5098a612018-11-29 17:12:15 -08001033 // zip-apex is the less common type so have the name refer to the image-apex
1034 // only and use {name}.zip if you want the zip-apex
1035 if apexType == zipApex && a.apexTypes == both {
1036 name = name + ".zip"
1037 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001038 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1039 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1040 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1041 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001042 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001043 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001044 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001045 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +09001046 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park719b4462019-01-13 00:39:51 +09001047 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
Jiyong Park8fd61922018-11-08 02:50:25 +09001048 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001049
Alex Light5098a612018-11-29 17:12:15 -08001050 if apexType == imageApex {
1051 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1052 }
Jiyong Park719b4462019-01-13 00:39:51 +09001053 }
1054 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001055}
1056
Alex Lightee250722018-12-06 14:00:02 -08001057func ApexBundleFactory() android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001058 module := &apexBundle{
1059 outputFiles: map[apexPackaging]android.WritablePath{},
1060 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001061 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001062 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001063 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001064 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1065 })
Alex Light5098a612018-11-29 17:12:15 -08001066 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001067 android.InitDefaultableModule(module)
1068 return module
1069}