blob: 3b06a99581cd3c6a058c319d6b7441918d6ea724 [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
24
25 "android/soong/android"
26 "android/soong/cc"
27 "android/soong/java"
28
29 "github.com/google/blueprint"
30 "github.com/google/blueprint/proptools"
31)
32
33var (
34 pctx = android.NewPackageContext("android/apex")
35
36 // Create a canned fs config file where all files and directories are
37 // by default set to (uid/gid/mode) = (1000/1000/0644)
38 // TODO(b/113082813) make this configurable using config.fs syntax
39 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000040 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000041 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090042 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090043 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090044 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090045 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046
47 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
48 // against the binary policy using sefcontext_compiler -p <policy>.
49
50 // TODO(b/114327326): automate the generation of file_contexts
51 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
52 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
53 `(${copy_commands}) && ` +
54 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090055 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090056 `--file_contexts ${file_contexts} ` +
57 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080058 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090059 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090060 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
61 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
62 "${soong_zip}", "${zipalign}", "${aapt2}"},
63 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090064 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080065
Alex Light5098a612018-11-29 17:12:15 -080066 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
67 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
68 `(${copy_commands}) && ` +
69 `APEXER_TOOL_PATH=${tool_path} ` +
70 `${apexer} --force --manifest ${manifest} ` +
71 `--payload_type zip ` +
72 `${image_dir} ${out} `,
73 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
74 Description: "ZipAPEX ${image_dir} => ${out}",
75 }, "tool_path", "image_dir", "copy_commands", "manifest")
76
Colin Crossa4925902018-11-16 11:36:28 -080077 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
78 blueprint.RuleParams{
79 Command: `${aapt2} convert --output-format proto $in -o $out`,
80 CommandDeps: []string{"${aapt2}"},
81 })
82
83 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090084 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000085 `apex_payload.img:apex/${abi}.img ` +
86 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +000087 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -080088 CommandDeps: []string{"${zip2zip}"},
89 Description: "app bundle",
90 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090091)
92
Alex Light5098a612018-11-29 17:12:15 -080093var imageApexSuffix = ".apex"
94var zipApexSuffix = ".zipapex"
95
96var imageApexType = "image"
97var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090098
99type dependencyTag struct {
100 blueprint.BaseDependencyTag
101 name string
102}
103
104var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900105 sharedLibTag = dependencyTag{name: "sharedLib"}
106 executableTag = dependencyTag{name: "executable"}
107 javaLibTag = dependencyTag{name: "javaLib"}
108 prebuiltTag = dependencyTag{name: "prebuilt"}
109 keyTag = dependencyTag{name: "key"}
110 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900111)
112
113func init() {
114 pctx.Import("android/soong/common")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900115 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900116 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100117 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
118 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
119 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
120 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000121 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100122 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
123 } else {
124 return pctx.HostBinToolPath(ctx, tool).String()
125 }
126 })
127 }
128 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900129 pctx.HostBinToolVariable("avbtool", "avbtool")
130 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
131 pctx.HostBinToolVariable("merge_zips", "merge_zips")
132 pctx.HostBinToolVariable("mke2fs", "mke2fs")
133 pctx.HostBinToolVariable("resize2fs", "resize2fs")
134 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
135 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800136 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900137 pctx.HostBinToolVariable("zipalign", "zipalign")
138
Alex Light0851b882019-02-07 13:20:53 -0800139 android.RegisterModuleType("apex", apexBundleFactory)
140 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900141 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900142
143 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
144 ctx.TopDown("apex_deps", apexDepsMutator)
145 ctx.BottomUp("apex", apexMutator)
146 })
147}
148
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900149// Mark the direct and transitive dependencies of apex bundles so that they
150// can be built for the apex bundles.
151func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800152 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800153 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900154 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900155 depName := mctx.OtherModuleName(child)
156 // If the parent is apexBundle, this child is directly depended.
157 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800158 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800159 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
160 // non-installable apex's cannot be installed and so should not prevent libraries from being
161 // installed to the system.
162 android.UpdateApexDependency(apexBundleName, depName, directDep)
163 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900164
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900165 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900166 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900167 return true
168 } else {
169 return false
170 }
171 })
172 }
173}
174
175// Create apex variations if a module is included in APEX(s).
176func apexMutator(mctx android.BottomUpMutatorContext) {
177 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900178 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900179 } else if _, ok := mctx.Module().(*apexBundle); ok {
180 // apex bundle itself is mutated so that it and its modules have same
181 // apex variant.
182 apexBundleName := mctx.ModuleName()
183 mctx.CreateVariations(apexBundleName)
184 }
185}
186
Alex Light9670d332019-01-29 18:07:33 -0800187type apexNativeDependencies struct {
188 // List of native libraries
189 Native_shared_libs []string
190 // List of native executables
191 Binaries []string
192}
193type apexMultilibProperties struct {
194 // Native dependencies whose compile_multilib is "first"
195 First apexNativeDependencies
196
197 // Native dependencies whose compile_multilib is "both"
198 Both apexNativeDependencies
199
200 // Native dependencies whose compile_multilib is "prefer32"
201 Prefer32 apexNativeDependencies
202
203 // Native dependencies whose compile_multilib is "32"
204 Lib32 apexNativeDependencies
205
206 // Native dependencies whose compile_multilib is "64"
207 Lib64 apexNativeDependencies
208}
209
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900210type apexBundleProperties struct {
211 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000212 // "apex_manifest.json"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900213 Manifest *string
214
Jiyong Park40e26a22019-02-08 02:53:06 +0900215 // AndroidManifest.xml file used for the zip container of this APEX bundle.
216 // If unspecified, a default one is automatically generated.
217 AndroidManifest *string
218
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900219 // Determines the file contexts file for setting security context to each file in this APEX bundle.
220 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
221 // used.
222 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900223 File_contexts *string
224
225 // List of native shared libs that are embedded inside this APEX bundle
226 Native_shared_libs []string
227
228 // List of native executables that are embedded inside this APEX bundle
229 Binaries []string
230
231 // List of java libraries that are embedded inside this APEX bundle
232 Java_libs []string
233
234 // List of prebuilt files that are embedded inside this APEX bundle
235 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900236
237 // Name of the apex_key module that provides the private key to sign APEX
238 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900239
Alex Light5098a612018-11-29 17:12:15 -0800240 // The type of APEX to build. Controls what the APEX payload is. Either
241 // 'image', 'zip' or 'both'. Default: 'image'.
242 Payload_type *string
243
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900244 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
245 // or an android_app_certificate module name in the form ":module".
246 Certificate *string
247
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900248 // Whether this APEX is installable to one of the partitions. Default: true.
249 Installable *bool
250
Jiyong Parkda6eb592018-12-19 17:12:36 +0900251 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
252 // Default is false.
253 Use_vendor *bool
254
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800255 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
256 Ignore_system_library_special_case *bool
257
Alex Light9670d332019-01-29 18:07:33 -0800258 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900259
Jiyong Parkf97782b2019-02-13 20:28:58 +0900260 // List of sanitizer names that this APEX is enabled for
261 SanitizerNames []string `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800262}
263
264type apexTargetBundleProperties struct {
265 Target struct {
266 // Multilib properties only for android.
267 Android struct {
268 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900269 }
Alex Light9670d332019-01-29 18:07:33 -0800270 // Multilib properties only for host.
271 Host struct {
272 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900273 }
Alex Light9670d332019-01-29 18:07:33 -0800274 // Multilib properties only for host linux_bionic.
275 Linux_bionic struct {
276 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900277 }
Alex Light9670d332019-01-29 18:07:33 -0800278 // Multilib properties only for host linux_glibc.
279 Linux_glibc struct {
280 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900281 }
282 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900283}
284
Jiyong Park8fd61922018-11-08 02:50:25 +0900285type apexFileClass int
286
287const (
288 etc apexFileClass = iota
289 nativeSharedLib
290 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900291 shBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900292 javaSharedLib
293)
294
Alex Light5098a612018-11-29 17:12:15 -0800295type apexPackaging int
296
297const (
298 imageApex apexPackaging = iota
299 zipApex
300 both
301)
302
303func (a apexPackaging) image() bool {
304 switch a {
305 case imageApex, both:
306 return true
307 }
308 return false
309}
310
311func (a apexPackaging) zip() bool {
312 switch a {
313 case zipApex, both:
314 return true
315 }
316 return false
317}
318
319func (a apexPackaging) suffix() string {
320 switch a {
321 case imageApex:
322 return imageApexSuffix
323 case zipApex:
324 return zipApexSuffix
325 case both:
326 panic(fmt.Errorf("must be either zip or image"))
327 default:
328 panic(fmt.Errorf("unkonwn APEX type %d", a))
329 }
330}
331
332func (a apexPackaging) name() string {
333 switch a {
334 case imageApex:
335 return imageApexType
336 case zipApex:
337 return zipApexType
338 case both:
339 panic(fmt.Errorf("must be either zip or image"))
340 default:
341 panic(fmt.Errorf("unkonwn APEX type %d", a))
342 }
343}
344
Jiyong Park8fd61922018-11-08 02:50:25 +0900345func (class apexFileClass) NameInMake() string {
346 switch class {
347 case etc:
348 return "ETC"
349 case nativeSharedLib:
350 return "SHARED_LIBRARIES"
Jiyong Park04480cf2019-02-06 00:16:29 +0900351 case nativeExecutable, shBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900352 return "EXECUTABLES"
353 case javaSharedLib:
354 return "JAVA_LIBRARIES"
355 default:
356 panic(fmt.Errorf("unkonwn class %d", class))
357 }
358}
359
360type apexFile struct {
361 builtFile android.Path
362 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900363 installDir string
364 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900365 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800366 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900367}
368
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900369type apexBundle struct {
370 android.ModuleBase
371 android.DefaultableModuleBase
372
Alex Light9670d332019-01-29 18:07:33 -0800373 properties apexBundleProperties
374 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900375
Alex Light5098a612018-11-29 17:12:15 -0800376 apexTypes apexPackaging
377
Colin Crossa4925902018-11-16 11:36:28 -0800378 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800379 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800380 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900381
382 // list of files to be included in this apex
383 filesInfo []apexFile
384
385 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800386
387 testApex bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900388}
389
Jiyong Park397e55e2018-10-24 21:09:55 +0900390func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900391 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900392 // Use *FarVariation* to be able to depend on modules having
393 // conflicting variations with this module. This is required since
394 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
395 // for native shared libs.
396 ctx.AddFarVariationDependencies([]blueprint.Variation{
397 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900398 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900399 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900400 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900401 }, sharedLibTag, native_shared_libs...)
402
403 ctx.AddFarVariationDependencies([]blueprint.Variation{
404 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900405 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900406 }, executableTag, binaries...)
407}
408
Alex Light9670d332019-01-29 18:07:33 -0800409func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
410 if ctx.Os().Class == android.Device {
411 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
412 } else {
413 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
414 if ctx.Os().Bionic() {
415 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
416 } else {
417 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
418 }
419 }
420}
421
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900422func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800423
Jiyong Park397e55e2018-10-24 21:09:55 +0900424 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900425 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800426
427 a.combineProperties(ctx)
428
Jiyong Park397e55e2018-10-24 21:09:55 +0900429 has32BitTarget := false
430 for _, target := range targets {
431 if target.Arch.ArchType.Multilib == "lib32" {
432 has32BitTarget = true
433 }
434 }
435 for i, target := range targets {
436 // When multilib.* is omitted for native_shared_libs, it implies
437 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900438 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900439 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900440 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900441 {Mutator: "link", Variation: "shared"},
442 }, sharedLibTag, a.properties.Native_shared_libs...)
443
Jiyong Park397e55e2018-10-24 21:09:55 +0900444 // Add native modules targetting both ABIs
445 addDependenciesForNativeModules(ctx,
446 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900447 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900448 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900449
Alex Light3d673592019-01-18 14:37:31 -0800450 isPrimaryAbi := i == 0
451 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900452 // When multilib.* is omitted for binaries, it implies
453 // multilib.first.
454 ctx.AddFarVariationDependencies([]blueprint.Variation{
455 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900456 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900457 }, executableTag, a.properties.Binaries...)
458
459 // Add native modules targetting the first ABI
460 addDependenciesForNativeModules(ctx,
461 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900462 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900463 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800464
465 // When multilib.* is omitted for prebuilts, it implies multilib.first.
466 ctx.AddFarVariationDependencies([]blueprint.Variation{
467 {Mutator: "arch", Variation: target.String()},
468 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900469 }
470
471 switch target.Arch.ArchType.Multilib {
472 case "lib32":
473 // Add native modules targetting 32-bit ABI
474 addDependenciesForNativeModules(ctx,
475 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900476 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900477 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900478
479 addDependenciesForNativeModules(ctx,
480 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900481 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900482 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900483 case "lib64":
484 // Add native modules targetting 64-bit ABI
485 addDependenciesForNativeModules(ctx,
486 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900487 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900488 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900489
490 if !has32BitTarget {
491 addDependenciesForNativeModules(ctx,
492 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900493 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900494 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900495 }
496 }
497
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900498 }
499
Jiyong Parkff1458f2018-10-12 21:49:38 +0900500 ctx.AddFarVariationDependencies([]blueprint.Variation{
501 {Mutator: "arch", Variation: "android_common"},
502 }, javaLibTag, a.properties.Java_libs...)
503
Jiyong Park23c52b02019-02-02 13:13:47 +0900504 if String(a.properties.Key) == "" {
505 ctx.ModuleErrorf("key is missing")
506 return
507 }
508 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900509
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900510 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900511 if cert != "" {
512 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900513 }
Jiyong Park809bb722019-02-13 21:33:49 +0900514
515 if String(a.properties.Manifest) != "" {
516 android.ExtractSourceDeps(ctx, a.properties.Manifest)
517 }
518
519 if String(a.properties.AndroidManifest) != "" {
520 android.ExtractSourceDeps(ctx, a.properties.AndroidManifest)
521 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900522}
523
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900524func (a *apexBundle) getCertString(ctx android.BaseContext) string {
525 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
526 if overridden {
527 return ":" + certificate
528 }
529 return String(a.properties.Certificate)
530}
531
Jiyong Park74e240b2018-11-27 21:27:08 +0900532func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900533 if file, ok := a.outputFiles[imageApex]; ok {
534 return android.Paths{file}
535 } else {
536 return nil
537 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900538}
539
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900540func (a *apexBundle) installable() bool {
541 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
542}
543
Jiyong Park7c1dc612019-01-05 11:15:24 +0900544func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
545 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900546 return "vendor"
547 } else {
548 return "core"
549 }
550}
551
Jiyong Parkf97782b2019-02-13 20:28:58 +0900552func (a *apexBundle) EnableSanitizer(sanitizerName string) {
553 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
554 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
555 }
556}
557
Jiyong Park388ef3f2019-01-28 19:47:32 +0900558func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900559 if android.InList(sanitizerName, a.properties.SanitizerNames) {
560 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900561 }
562
563 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900564 globalSanitizerNames := []string{}
565 if a.Host() {
566 globalSanitizerNames = ctx.Config().SanitizeHost()
567 } else {
568 arches := ctx.Config().SanitizeDeviceArch()
569 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
570 globalSanitizerNames = ctx.Config().SanitizeDevice()
571 }
572 }
573 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900574}
575
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800576func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900577 // Decide the APEX-local directory by the multilib of the library
578 // In the future, we may query this to the module.
579 switch cc.Arch().ArchType.Multilib {
580 case "lib32":
581 dirInApex = "lib"
582 case "lib64":
583 dirInApex = "lib64"
584 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900585 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900586 if !cc.Arch().Native {
587 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
588 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800589 if handleSpecialLibs {
590 switch cc.Name() {
591 case "libc", "libm", "libdl":
592 // Special case for bionic libs. This is to prevent the bionic libs
593 // from being included in the search path /apex/com.android.apex/lib.
594 // This exclusion is required because bionic libs in the runtime APEX
595 // are available via the legacy paths /system/lib/libc.so, etc. By the
596 // init process, the bionic libs in the APEX are bind-mounted to the
597 // legacy paths and thus will be loaded into the default linker namespace.
598 // If the bionic libs are directly in /apex/com.android.apex/lib then
599 // the same libs will be again loaded to the runtime linker namespace,
600 // which will result double loading of bionic libs that isn't supported.
601 dirInApex = filepath.Join(dirInApex, "bionic")
602 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900603 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900604
605 fileToCopy = cc.OutputFile().Path()
606 return
607}
608
609func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900610 // TODO(b/123721777) respect relative_install_path also for binaries
611 // dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900612 dirInApex = "bin"
613 fileToCopy = cc.OutputFile().Path()
614 return
615}
616
Jiyong Park04480cf2019-02-06 00:16:29 +0900617func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
618 dirInApex = filepath.Join("bin", sh.SubDir())
619 fileToCopy = sh.OutputFile()
620 return
621}
622
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900623func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
624 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900625 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626 return
627}
628
629func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
630 dirInApex = filepath.Join("etc", prebuilt.SubDir())
631 fileToCopy = prebuilt.OutputFile()
632 return
633}
634
635func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900636 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900637
Jiyong Parkff1458f2018-10-12 21:49:38 +0900638 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900639 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900640 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900641
Alex Light5098a612018-11-29 17:12:15 -0800642 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
643 a.apexTypes = imageApex
644 } else if *a.properties.Payload_type == "zip" {
645 a.apexTypes = zipApex
646 } else if *a.properties.Payload_type == "both" {
647 a.apexTypes = both
648 } else {
649 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
650 return
651 }
652
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800653 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
654
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900655 ctx.WalkDeps(func(child, parent android.Module) bool {
656 if _, ok := parent.(*apexBundle); ok {
657 // direct dependencies
658 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900659 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900660 switch depTag {
661 case sharedLibTag:
662 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800663 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900664 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900665 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900666 } else {
667 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900668 }
669 case executableTag:
670 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800671 if !cc.Arch().Native {
672 // There is only one 'bin' directory so we shouldn't bother copying in
673 // native-bridge'd binaries and only use main ones.
674 return true
675 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900676 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900677 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900678 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900679 } else if sh, ok := child.(*android.ShBinary); ok {
680 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
681 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900682 } else {
Jiyong Park04480cf2019-02-06 00:16:29 +0900683 ctx.PropertyErrorf("binaries", "%q is neithher cc_binary nor sh_binary", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900684 }
685 case javaLibTag:
686 if java, ok := child.(*java.Library); ok {
687 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900688 if fileToCopy == nil {
689 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
690 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900691 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900692 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900693 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900694 } else {
695 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900696 }
697 case prebuiltTag:
698 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
699 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900700 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900701 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900702 } else {
703 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
704 }
705 case keyTag:
706 if key, ok := child.(*apexKey); ok {
707 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900708 if !key.installable() && ctx.Config().Debuggable() {
709 // If the key is not installed, bundled it with the APEX.
710 // Note: this bundled key is valid only for non-production builds
711 // (eng/userdebug).
712 pubKeyFile = key.public_key_file
713 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900714 return false
715 } else {
716 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900717 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900718 case certificateTag:
719 if dep, ok := child.(*java.AndroidAppCertificate); ok {
720 certificate = dep.Certificate
721 return false
722 } else {
723 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
724 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900725 }
726 } else {
727 // indirect dependencies
728 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
729 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900730 if cc.IsStubs() || cc.HasStubsVariants() {
731 return false
732 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900733 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800734 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900735 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900736 return true
737 }
738 }
739 }
740 return false
741 })
742
Jiyong Park9335a262018-12-24 11:31:58 +0900743 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park23c52b02019-02-02 13:13:47 +0900744 if keyFile == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900745 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
746 return
747 }
748
Jiyong Park8fd61922018-11-08 02:50:25 +0900749 // remove duplicates in filesInfo
750 removeDup := func(filesInfo []apexFile) []apexFile {
751 encountered := make(map[android.Path]bool)
752 result := []apexFile{}
753 for _, f := range filesInfo {
754 if !encountered[f.builtFile] {
755 encountered[f.builtFile] = true
756 result = append(result, f)
757 }
758 }
759 return result
760 }
761 filesInfo = removeDup(filesInfo)
762
763 // to have consistent build rules
764 sort.Slice(filesInfo, func(i, j int) bool {
765 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
766 })
767
768 // prepend the name of this APEX to the module names. These names will be the names of
769 // modules that will be defined if the APEX is flattened.
770 for i := range filesInfo {
771 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
772 }
773
Jiyong Park8fd61922018-11-08 02:50:25 +0900774 a.installDir = android.PathForModuleInstall(ctx, "apex")
775 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800776
777 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900778 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800779 }
780 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900781 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
782 // is true. This is to support referencing APEX via ":<module_name" syntax
783 // in other modules. It is in AndroidMk where the selection of flattened
784 // or unflattened APEX is made.
785 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
786 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900787 }
788}
789
Jiyong Park835d82b2018-12-27 16:04:18 +0900790func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
791 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900792 cert := String(a.properties.Certificate)
793 if cert != "" && android.SrcIsModule(cert) == "" {
794 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
795 certificate = java.Certificate{
796 defaultDir.Join(ctx, cert+".x509.pem"),
797 defaultDir.Join(ctx, cert+".pk8"),
798 }
799 } else if cert == "" {
800 pem, key := ctx.Config().DefaultAppCertificate(ctx)
801 certificate = java.Certificate{pem, key}
802 }
803
Jiyong Park809bb722019-02-13 21:33:49 +0900804 manifest := ctx.ExpandSource(proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"), "manifest")
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900805
Alex Light5098a612018-11-29 17:12:15 -0800806 var abis []string
807 for _, target := range ctx.MultiTargets() {
808 if len(target.Arch.Abi) > 0 {
809 abis = append(abis, target.Arch.Abi[0])
810 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900811 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900812
Alex Light5098a612018-11-29 17:12:15 -0800813 abis = android.FirstUniqueStrings(abis)
814
815 suffix := apexType.suffix()
816 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900817
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900818 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900819 for _, f := range a.filesInfo {
820 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900821 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900822
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900823 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900824 for i, src := range filesToCopy {
825 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800826 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900827 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
828 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800829 for _, sym := range a.filesInfo[i].symlinks {
830 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
831 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
832 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900833 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900834 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800835 implicitInputs = append(implicitInputs, manifest)
836
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900837 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
838 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900839
Alex Light5098a612018-11-29 17:12:15 -0800840 if apexType.image() {
841 // files and dirs that will be created in APEX
842 var readOnlyPaths []string
843 var executablePaths []string // this also includes dirs
844 for _, f := range a.filesInfo {
845 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
846 if f.installDir == "bin" {
847 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800848 for _, s := range f.symlinks {
849 executablePaths = append(executablePaths, filepath.Join("bin", s))
850 }
Alex Light5098a612018-11-29 17:12:15 -0800851 } else {
852 readOnlyPaths = append(readOnlyPaths, pathInApex)
853 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900854 dir := f.installDir
855 for !android.InList(dir, executablePaths) && dir != "" {
856 executablePaths = append(executablePaths, dir)
857 dir, _ = filepath.Split(dir) // move up to the parent
858 if len(dir) > 0 {
859 // remove trailing slash
860 dir = dir[:len(dir)-1]
861 }
Alex Light5098a612018-11-29 17:12:15 -0800862 }
863 }
864 sort.Strings(readOnlyPaths)
865 sort.Strings(executablePaths)
866 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
867 ctx.Build(pctx, android.BuildParams{
868 Rule: generateFsConfig,
869 Output: cannedFsConfig,
870 Description: "generate fs config",
871 Args: map[string]string{
872 "ro_paths": strings.Join(readOnlyPaths, " "),
873 "exec_paths": strings.Join(executablePaths, " "),
874 },
875 })
876
877 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
878 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
879 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
880 if !fileContextsOptionalPath.Valid() {
881 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
882 return
883 }
884 fileContexts := fileContextsOptionalPath.Path()
885
Jiyong Park835d82b2018-12-27 16:04:18 +0900886 optFlags := []string{}
887
Alex Light5098a612018-11-29 17:12:15 -0800888 // Additional implicit inputs.
889 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900890 if pubKeyFile != nil {
891 implicitInputs = append(implicitInputs, pubKeyFile)
892 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
893 }
Alex Light5098a612018-11-29 17:12:15 -0800894
Jiyong Park7f67f482019-01-05 12:57:48 +0900895 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
896 if overridden {
897 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
898 }
899
Jiyong Park40e26a22019-02-08 02:53:06 +0900900 if a.properties.AndroidManifest != nil {
Jiyong Park809bb722019-02-13 21:33:49 +0900901 androidManifestFile := ctx.ExpandSource(proptools.String(a.properties.AndroidManifest), "androidManifest")
Jiyong Park40e26a22019-02-08 02:53:06 +0900902 implicitInputs = append(implicitInputs, androidManifestFile)
903 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
904 }
905
Alex Light5098a612018-11-29 17:12:15 -0800906 ctx.Build(pctx, android.BuildParams{
907 Rule: apexRule,
908 Implicits: implicitInputs,
909 Output: unsignedOutputFile,
910 Description: "apex (" + apexType.name() + ")",
911 Args: map[string]string{
912 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
913 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
914 "copy_commands": strings.Join(copyCommands, " && "),
915 "manifest": manifest.String(),
916 "file_contexts": fileContexts.String(),
917 "canned_fs_config": cannedFsConfig.String(),
918 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900919 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800920 },
921 })
922
923 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
924 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
925 a.bundleModuleFile = bundleModuleFile
926
927 ctx.Build(pctx, android.BuildParams{
928 Rule: apexProtoConvertRule,
929 Input: unsignedOutputFile,
930 Output: apexProtoFile,
931 Description: "apex proto convert",
932 })
933
934 ctx.Build(pctx, android.BuildParams{
935 Rule: apexBundleRule,
936 Input: apexProtoFile,
937 Output: a.bundleModuleFile,
938 Description: "apex bundle module",
939 Args: map[string]string{
940 "abi": strings.Join(abis, "."),
941 },
942 })
943 } else {
944 ctx.Build(pctx, android.BuildParams{
945 Rule: zipApexRule,
946 Implicits: implicitInputs,
947 Output: unsignedOutputFile,
948 Description: "apex (" + apexType.name() + ")",
949 Args: map[string]string{
950 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
951 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
952 "copy_commands": strings.Join(copyCommands, " && "),
953 "manifest": manifest.String(),
954 },
955 })
Colin Crossa4925902018-11-16 11:36:28 -0800956 }
Colin Crossa4925902018-11-16 11:36:28 -0800957
Alex Light5098a612018-11-29 17:12:15 -0800958 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900959 ctx.Build(pctx, android.BuildParams{
960 Rule: java.Signapk,
961 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800962 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900963 Input: unsignedOutputFile,
964 Args: map[string]string{
965 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900966 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900967 },
968 })
Alex Light5098a612018-11-29 17:12:15 -0800969
970 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -0800971 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900972 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
973 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900974}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900975
Jiyong Park8fd61922018-11-08 02:50:25 +0900976func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900977 if a.installable() {
978 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
979 // with other ordinary files.
Jiyong Park809bb722019-02-13 21:33:49 +0900980 manifest := ctx.ExpandSource(proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"), "manifest")
Jiyong Parkd699cb92019-01-10 00:23:16 +0900981
982 // rename to apex_manifest.json
983 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
984 ctx.Build(pctx, android.BuildParams{
985 Rule: android.Cp,
986 Input: manifest,
987 Output: copiedManifest,
988 })
Jiyong Park719b4462019-01-13 00:39:51 +0900989 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900990
Jiyong Park23c52b02019-02-02 13:13:47 +0900991 if ctx.Config().FlattenApex() {
992 for _, fi := range a.filesInfo {
993 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
994 ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
995 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900996 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900997 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900998}
999
1000func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -08001001 writers := []android.AndroidMkData{}
1002 if a.apexTypes.image() {
1003 writers = append(writers, a.androidMkForType(imageApex))
1004 }
1005 if a.apexTypes.zip() {
1006 writers = append(writers, a.androidMkForType(zipApex))
1007 }
1008 return android.AndroidMkData{
1009 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1010 for _, data := range writers {
1011 data.Custom(w, name, prefix, moduleDir, data)
1012 }
1013 }}
1014}
1015
Alex Lightf1801bc2019-02-13 11:10:07 -08001016func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001017 moduleNames := []string{}
1018
1019 for _, fi := range a.filesInfo {
1020 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1021 continue
1022 }
1023 if !android.InList(fi.moduleName, moduleNames) {
1024 moduleNames = append(moduleNames, fi.moduleName)
1025 }
1026 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1027 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1028 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Alex Lightf1801bc2019-02-13 11:10:07 -08001029 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001030 // /system/apex/<name>/{lib|framework|...}
1031 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1032 a.installDir.RelPathString(), name, fi.installDir))
1033 } else {
1034 // /apex/<name>/{lib|framework|...}
1035 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(PRODUCT_OUT)",
1036 "apex", name, fi.installDir))
1037 }
1038 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1039 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1040 if fi.module != nil {
1041 archStr := fi.module.Target().Arch.ArchType.String()
1042 host := false
1043 switch fi.module.Target().Os.Class {
1044 case android.Host:
1045 if archStr != "common" {
1046 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1047 }
1048 host = true
1049 case android.HostCross:
1050 if archStr != "common" {
1051 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1052 }
1053 host = true
1054 case android.Device:
1055 if archStr != "common" {
1056 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1057 }
1058 }
1059 if host {
1060 makeOs := fi.module.Target().Os.String()
1061 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1062 makeOs = "linux"
1063 }
1064 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1065 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1066 }
1067 }
1068 if fi.class == javaSharedLib {
1069 javaModule := fi.module.(*java.Library)
1070 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1071 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1072 // we will have foo.jar.jar
1073 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1074 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1075 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1076 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1077 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1078 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1079 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1080 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1081 if cc, ok := fi.module.(*cc.Module); ok && cc.UnstrippedOutputFile() != nil {
1082 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1083 }
1084 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1085 } else {
1086 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1087 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1088 }
1089 }
1090 return moduleNames
1091}
1092
Alex Light5098a612018-11-29 17:12:15 -08001093func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001094 return android.AndroidMkData{
1095 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1096 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001097 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001098 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001099 }
1100
Jiyong Park719b4462019-01-13 00:39:51 +09001101 if a.flattened && apexType.image() {
1102 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001103 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1104 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1105 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001106 if len(moduleNames) > 0 {
1107 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1108 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001109 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001110 } else {
Alex Light5098a612018-11-29 17:12:15 -08001111 // zip-apex is the less common type so have the name refer to the image-apex
1112 // only and use {name}.zip if you want the zip-apex
1113 if apexType == zipApex && a.apexTypes == both {
1114 name = name + ".zip"
1115 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001116 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1117 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1118 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1119 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001120 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001121 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001122 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001123 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +09001124 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park94427262019-02-05 23:18:47 +09001125 if len(moduleNames) > 0 {
1126 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1127 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001128 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001129
Alex Light5098a612018-11-29 17:12:15 -08001130 if apexType == imageApex {
1131 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1132 }
Jiyong Park719b4462019-01-13 00:39:51 +09001133 }
1134 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001135}
1136
Alex Light0851b882019-02-07 13:20:53 -08001137func testApexBundleFactory() android.Module {
1138 return ApexBundleFactory( /*testApex*/ true)
1139}
1140
1141func apexBundleFactory() android.Module {
1142 return ApexBundleFactory( /*testApex*/ false)
1143}
1144
1145func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001146 module := &apexBundle{
1147 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001148 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001149 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001150 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001151 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001152 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001153 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1154 })
Alex Light5098a612018-11-29 17:12:15 -08001155 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001156 android.InitDefaultableModule(module)
1157 return module
1158}
Jiyong Park30ca9372019-02-07 16:27:23 +09001159
1160//
1161// Defaults
1162//
1163type Defaults struct {
1164 android.ModuleBase
1165 android.DefaultsModuleBase
1166}
1167
1168func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1169}
1170
1171func defaultsFactory() android.Module {
1172 return DefaultsFactory()
1173}
1174
1175func DefaultsFactory(props ...interface{}) android.Module {
1176 module := &Defaults{}
1177
1178 module.AddProperties(props...)
1179 module.AddProperties(
1180 &apexBundleProperties{},
1181 &apexTargetBundleProperties{},
1182 )
1183
1184 android.InitDefaultsModule(module)
1185 return module
1186}