blob: df85f510874d3ad1fb89cc118901d374ea94e453 [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"
Alex Light778127a2019-02-27 14:19:50 -080028 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090029
30 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080031 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090032 "github.com/google/blueprint/proptools"
33)
34
35var (
36 pctx = android.NewPackageContext("android/apex")
37
38 // Create a canned fs config file where all files and directories are
39 // by default set to (uid/gid/mode) = (1000/1000/0644)
40 // TODO(b/113082813) make this configurable using config.fs syntax
41 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000042 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000043 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090044 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090045 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090047 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090048
49 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
50 // against the binary policy using sefcontext_compiler -p <policy>.
51
52 // TODO(b/114327326): automate the generation of file_contexts
53 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
54 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
55 `(${copy_commands}) && ` +
56 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090057 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090058 `--file_contexts ${file_contexts} ` +
59 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080060 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090061 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090062 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
63 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000064 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Jiyong Park48ca7dc2018-10-10 14:01:00 +090065 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090066 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080067
Alex Light5098a612018-11-29 17:12:15 -080068 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
69 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
70 `(${copy_commands}) && ` +
71 `APEXER_TOOL_PATH=${tool_path} ` +
72 `${apexer} --force --manifest ${manifest} ` +
73 `--payload_type zip ` +
74 `${image_dir} ${out} `,
75 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
76 Description: "ZipAPEX ${image_dir} => ${out}",
77 }, "tool_path", "image_dir", "copy_commands", "manifest")
78
Colin Crossa4925902018-11-16 11:36:28 -080079 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
80 blueprint.RuleParams{
81 Command: `${aapt2} convert --output-format proto $in -o $out`,
82 CommandDeps: []string{"${aapt2}"},
83 })
84
85 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090086 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000087 `apex_payload.img:apex/${abi}.img ` +
88 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +000089 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -080090 CommandDeps: []string{"${zip2zip}"},
91 Description: "app bundle",
92 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090093)
94
Alex Light5098a612018-11-29 17:12:15 -080095var imageApexSuffix = ".apex"
96var zipApexSuffix = ".zipapex"
97
98var imageApexType = "image"
99var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900100
101type dependencyTag struct {
102 blueprint.BaseDependencyTag
103 name string
104}
105
106var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900107 sharedLibTag = dependencyTag{name: "sharedLib"}
108 executableTag = dependencyTag{name: "executable"}
109 javaLibTag = dependencyTag{name: "javaLib"}
110 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100111 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900112 keyTag = dependencyTag{name: "key"}
113 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900114 usesTag = dependencyTag{name: "uses"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900115)
116
117func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700118 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900119 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900120 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100121 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
122 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
123 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
124 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000125 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100126 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
127 } else {
128 return pctx.HostBinToolPath(ctx, tool).String()
129 }
130 })
131 }
132 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900133 pctx.HostBinToolVariable("avbtool", "avbtool")
134 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
135 pctx.HostBinToolVariable("merge_zips", "merge_zips")
136 pctx.HostBinToolVariable("mke2fs", "mke2fs")
137 pctx.HostBinToolVariable("resize2fs", "resize2fs")
138 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
139 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800140 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900141 pctx.HostBinToolVariable("zipalign", "zipalign")
142
Alex Light0851b882019-02-07 13:20:53 -0800143 android.RegisterModuleType("apex", apexBundleFactory)
144 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900145 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700146 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900147
148 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
149 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700150 ctx.BottomUp("apex", apexMutator).Parallel()
Jooyung Han5c998b92019-06-27 11:30:33 +0900151 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900152 })
153}
154
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900155// Mark the direct and transitive dependencies of apex bundles so that they
156// can be built for the apex bundles.
157func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800158 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800159 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900161 depName := mctx.OtherModuleName(child)
162 // If the parent is apexBundle, this child is directly depended.
163 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800164 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800165 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
166 // non-installable apex's cannot be installed and so should not prevent libraries from being
167 // installed to the system.
168 android.UpdateApexDependency(apexBundleName, depName, directDep)
169 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900170
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900171 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900172 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900173 return true
174 } else {
175 return false
176 }
177 })
178 }
179}
180
181// Create apex variations if a module is included in APEX(s).
182func apexMutator(mctx android.BottomUpMutatorContext) {
183 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900184 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900185 } else if _, ok := mctx.Module().(*apexBundle); ok {
186 // apex bundle itself is mutated so that it and its modules have same
187 // apex variant.
188 apexBundleName := mctx.ModuleName()
189 mctx.CreateVariations(apexBundleName)
190 }
191}
Jooyung Han5c998b92019-06-27 11:30:33 +0900192func apexUsesMutator(mctx android.BottomUpMutatorContext) {
193 if ab, ok := mctx.Module().(*apexBundle); ok {
194 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
195 }
196}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900197
Alex Light9670d332019-01-29 18:07:33 -0800198type apexNativeDependencies struct {
199 // List of native libraries
200 Native_shared_libs []string
201 // List of native executables
202 Binaries []string
Roland Levillain630846d2019-06-26 12:48:34 +0100203 // List of native tests
204 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800205}
206type apexMultilibProperties struct {
207 // Native dependencies whose compile_multilib is "first"
208 First apexNativeDependencies
209
210 // Native dependencies whose compile_multilib is "both"
211 Both apexNativeDependencies
212
213 // Native dependencies whose compile_multilib is "prefer32"
214 Prefer32 apexNativeDependencies
215
216 // Native dependencies whose compile_multilib is "32"
217 Lib32 apexNativeDependencies
218
219 // Native dependencies whose compile_multilib is "64"
220 Lib64 apexNativeDependencies
221}
222
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900223type apexBundleProperties struct {
224 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000225 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800226 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900227
Jiyong Park40e26a22019-02-08 02:53:06 +0900228 // AndroidManifest.xml file used for the zip container of this APEX bundle.
229 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800230 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900231
Jiyong Park05e70dd2019-03-18 14:26:32 +0900232 // Canonical name of the APEX bundle in the manifest file.
233 // If unspecified, defaults to the value of name
234 Apex_name *string
235
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900236 // Determines the file contexts file for setting security context to each file in this APEX bundle.
237 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
238 // used.
239 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900240 File_contexts *string
241
242 // List of native shared libs that are embedded inside this APEX bundle
243 Native_shared_libs []string
244
Roland Levillain630846d2019-06-26 12:48:34 +0100245 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900246 Binaries []string
247
248 // List of java libraries that are embedded inside this APEX bundle
249 Java_libs []string
250
251 // List of prebuilt files that are embedded inside this APEX bundle
252 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900253
Roland Levillain630846d2019-06-26 12:48:34 +0100254 // List of tests that are embedded inside this APEX bundle
255 Tests []string
256
Jiyong Parkff1458f2018-10-12 21:49:38 +0900257 // Name of the apex_key module that provides the private key to sign APEX
258 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900259
Alex Light5098a612018-11-29 17:12:15 -0800260 // The type of APEX to build. Controls what the APEX payload is. Either
261 // 'image', 'zip' or 'both'. Default: 'image'.
262 Payload_type *string
263
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900264 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
265 // or an android_app_certificate module name in the form ":module".
266 Certificate *string
267
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900268 // Whether this APEX is installable to one of the partitions. Default: true.
269 Installable *bool
270
Jiyong Parkda6eb592018-12-19 17:12:36 +0900271 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
272 // Default is false.
273 Use_vendor *bool
274
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800275 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
276 Ignore_system_library_special_case *bool
277
Alex Light9670d332019-01-29 18:07:33 -0800278 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900279
Jiyong Parkf97782b2019-02-13 20:28:58 +0900280 // List of sanitizer names that this APEX is enabled for
281 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900282
283 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
284 Provide_cpp_shared_libs *bool
285
286 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
287 Uses []string
Alex Light9670d332019-01-29 18:07:33 -0800288}
289
290type apexTargetBundleProperties struct {
291 Target struct {
292 // Multilib properties only for android.
293 Android struct {
294 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900295 }
Alex Light9670d332019-01-29 18:07:33 -0800296 // Multilib properties only for host.
297 Host struct {
298 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900299 }
Alex Light9670d332019-01-29 18:07:33 -0800300 // Multilib properties only for host linux_bionic.
301 Linux_bionic struct {
302 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900303 }
Alex Light9670d332019-01-29 18:07:33 -0800304 // Multilib properties only for host linux_glibc.
305 Linux_glibc struct {
306 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900307 }
308 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900309}
310
Jiyong Park8fd61922018-11-08 02:50:25 +0900311type apexFileClass int
312
313const (
314 etc apexFileClass = iota
315 nativeSharedLib
316 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900317 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800318 pyBinary
319 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900320 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100321 nativeTest
Jiyong Park8fd61922018-11-08 02:50:25 +0900322)
323
Alex Light5098a612018-11-29 17:12:15 -0800324type apexPackaging int
325
326const (
327 imageApex apexPackaging = iota
328 zipApex
329 both
330)
331
332func (a apexPackaging) image() bool {
333 switch a {
334 case imageApex, both:
335 return true
336 }
337 return false
338}
339
340func (a apexPackaging) zip() bool {
341 switch a {
342 case zipApex, both:
343 return true
344 }
345 return false
346}
347
348func (a apexPackaging) suffix() string {
349 switch a {
350 case imageApex:
351 return imageApexSuffix
352 case zipApex:
353 return zipApexSuffix
354 case both:
355 panic(fmt.Errorf("must be either zip or image"))
356 default:
357 panic(fmt.Errorf("unkonwn APEX type %d", a))
358 }
359}
360
361func (a apexPackaging) name() string {
362 switch a {
363 case imageApex:
364 return imageApexType
365 case zipApex:
366 return zipApexType
367 case both:
368 panic(fmt.Errorf("must be either zip or image"))
369 default:
370 panic(fmt.Errorf("unkonwn APEX type %d", a))
371 }
372}
373
Jiyong Park8fd61922018-11-08 02:50:25 +0900374func (class apexFileClass) NameInMake() string {
375 switch class {
376 case etc:
377 return "ETC"
378 case nativeSharedLib:
379 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800380 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900381 return "EXECUTABLES"
382 case javaSharedLib:
383 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100384 case nativeTest:
385 return "NATIVE_TESTS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900386 default:
387 panic(fmt.Errorf("unkonwn class %d", class))
388 }
389}
390
391type apexFile struct {
392 builtFile android.Path
393 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900394 installDir string
395 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900396 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800397 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900398}
399
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900400type apexBundle struct {
401 android.ModuleBase
402 android.DefaultableModuleBase
403
Alex Light9670d332019-01-29 18:07:33 -0800404 properties apexBundleProperties
405 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900406
Alex Light5098a612018-11-29 17:12:15 -0800407 apexTypes apexPackaging
408
Colin Crossa4925902018-11-16 11:36:28 -0800409 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800410 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800411 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900412
Jiyong Park42cca6c2019-04-01 11:15:50 +0900413 public_key_file android.Path
414 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900415
416 container_certificate_file android.Path
417 container_private_key_file android.Path
418
Jiyong Park8fd61922018-11-08 02:50:25 +0900419 // list of files to be included in this apex
420 filesInfo []apexFile
421
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900422 // list of module names that this APEX is depending on
423 externalDeps []string
424
Jiyong Park8fd61922018-11-08 02:50:25 +0900425 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800426
427 testApex bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900428}
429
Jiyong Park397e55e2018-10-24 21:09:55 +0900430func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100431 native_shared_libs []string, binaries []string, tests []string,
432 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900433 // Use *FarVariation* to be able to depend on modules having
434 // conflicting variations with this module. This is required since
435 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
436 // for native shared libs.
437 ctx.AddFarVariationDependencies([]blueprint.Variation{
438 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900439 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900440 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900441 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900442 }, sharedLibTag, native_shared_libs...)
443
444 ctx.AddFarVariationDependencies([]blueprint.Variation{
445 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900446 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900447 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100448
449 ctx.AddFarVariationDependencies([]blueprint.Variation{
450 {Mutator: "arch", Variation: arch},
451 {Mutator: "image", Variation: imageVariation},
452 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900453}
454
Alex Light9670d332019-01-29 18:07:33 -0800455func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
456 if ctx.Os().Class == android.Device {
457 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
458 } else {
459 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
460 if ctx.Os().Bionic() {
461 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
462 } else {
463 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
464 }
465 }
466}
467
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900468func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800469
Jiyong Park397e55e2018-10-24 21:09:55 +0900470 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900471 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800472
473 a.combineProperties(ctx)
474
Jiyong Park397e55e2018-10-24 21:09:55 +0900475 has32BitTarget := false
476 for _, target := range targets {
477 if target.Arch.ArchType.Multilib == "lib32" {
478 has32BitTarget = true
479 }
480 }
481 for i, target := range targets {
482 // When multilib.* is omitted for native_shared_libs, it implies
483 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900484 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900485 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900486 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900487 {Mutator: "link", Variation: "shared"},
488 }, sharedLibTag, a.properties.Native_shared_libs...)
489
Roland Levillain630846d2019-06-26 12:48:34 +0100490 // When multilib.* is omitted for tests, it implies
491 // multilib.both.
492 ctx.AddFarVariationDependencies([]blueprint.Variation{
493 {Mutator: "arch", Variation: target.String()},
494 {Mutator: "image", Variation: a.getImageVariation(config)},
495 }, testTag, a.properties.Tests...)
496
Jiyong Park397e55e2018-10-24 21:09:55 +0900497 // Add native modules targetting both ABIs
498 addDependenciesForNativeModules(ctx,
499 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100500 a.properties.Multilib.Both.Binaries,
501 a.properties.Multilib.Both.Tests,
502 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900503 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900504
Alex Light3d673592019-01-18 14:37:31 -0800505 isPrimaryAbi := i == 0
506 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900507 // When multilib.* is omitted for binaries, it implies
508 // multilib.first.
509 ctx.AddFarVariationDependencies([]blueprint.Variation{
510 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900511 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900512 }, executableTag, a.properties.Binaries...)
513
514 // Add native modules targetting the first ABI
515 addDependenciesForNativeModules(ctx,
516 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100517 a.properties.Multilib.First.Binaries,
518 a.properties.Multilib.First.Tests,
519 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900520 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800521
522 // When multilib.* is omitted for prebuilts, it implies multilib.first.
523 ctx.AddFarVariationDependencies([]blueprint.Variation{
524 {Mutator: "arch", Variation: target.String()},
525 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900526 }
527
528 switch target.Arch.ArchType.Multilib {
529 case "lib32":
530 // Add native modules targetting 32-bit ABI
531 addDependenciesForNativeModules(ctx,
532 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100533 a.properties.Multilib.Lib32.Binaries,
534 a.properties.Multilib.Lib32.Tests,
535 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900536 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900537
538 addDependenciesForNativeModules(ctx,
539 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100540 a.properties.Multilib.Prefer32.Binaries,
541 a.properties.Multilib.Prefer32.Tests,
542 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900543 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900544 case "lib64":
545 // Add native modules targetting 64-bit ABI
546 addDependenciesForNativeModules(ctx,
547 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100548 a.properties.Multilib.Lib64.Binaries,
549 a.properties.Multilib.Lib64.Tests,
550 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900551 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900552
553 if !has32BitTarget {
554 addDependenciesForNativeModules(ctx,
555 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100556 a.properties.Multilib.Prefer32.Binaries,
557 a.properties.Multilib.Prefer32.Tests,
558 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900559 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900560 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700561
562 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
563 for _, sanitizer := range ctx.Config().SanitizeDevice() {
564 if sanitizer == "hwaddress" {
565 addDependenciesForNativeModules(ctx,
566 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100567 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700568 break
569 }
570 }
571 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900572 }
573
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900574 }
575
Jiyong Parkff1458f2018-10-12 21:49:38 +0900576 ctx.AddFarVariationDependencies([]blueprint.Variation{
577 {Mutator: "arch", Variation: "android_common"},
578 }, javaLibTag, a.properties.Java_libs...)
579
Jiyong Park23c52b02019-02-02 13:13:47 +0900580 if String(a.properties.Key) == "" {
581 ctx.ModuleErrorf("key is missing")
582 return
583 }
584 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900585
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900586 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900587 if cert != "" {
588 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900589 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900590}
591
Colin Cross0ea8ba82019-06-06 14:33:29 -0700592func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900593 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
594 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000595 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900596 }
597 return String(a.properties.Certificate)
598}
599
Colin Cross41955e82019-05-29 14:40:35 -0700600func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
601 switch tag {
602 case "":
603 if file, ok := a.outputFiles[imageApex]; ok {
604 return android.Paths{file}, nil
605 } else {
606 return nil, nil
607 }
608 default:
609 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900610 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900611}
612
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900613func (a *apexBundle) installable() bool {
614 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
615}
616
Jiyong Park7c1dc612019-01-05 11:15:24 +0900617func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
618 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900619 return "vendor"
620 } else {
621 return "core"
622 }
623}
624
Jiyong Parkf97782b2019-02-13 20:28:58 +0900625func (a *apexBundle) EnableSanitizer(sanitizerName string) {
626 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
627 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
628 }
629}
630
Jiyong Park388ef3f2019-01-28 19:47:32 +0900631func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900632 if android.InList(sanitizerName, a.properties.SanitizerNames) {
633 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900634 }
635
636 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900637 globalSanitizerNames := []string{}
638 if a.Host() {
639 globalSanitizerNames = ctx.Config().SanitizeHost()
640 } else {
641 arches := ctx.Config().SanitizeDeviceArch()
642 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
643 globalSanitizerNames = ctx.Config().SanitizeDevice()
644 }
645 }
646 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900647}
648
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800649func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900650 // Decide the APEX-local directory by the multilib of the library
651 // In the future, we may query this to the module.
652 switch cc.Arch().ArchType.Multilib {
653 case "lib32":
654 dirInApex = "lib"
655 case "lib64":
656 dirInApex = "lib64"
657 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900658 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900659 if cc.Target().NativeBridge == android.NativeBridgeEnabled || !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900660 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
661 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800662 if handleSpecialLibs {
663 switch cc.Name() {
664 case "libc", "libm", "libdl":
665 // Special case for bionic libs. This is to prevent the bionic libs
666 // from being included in the search path /apex/com.android.apex/lib.
667 // This exclusion is required because bionic libs in the runtime APEX
668 // are available via the legacy paths /system/lib/libc.so, etc. By the
669 // init process, the bionic libs in the APEX are bind-mounted to the
670 // legacy paths and thus will be loaded into the default linker namespace.
671 // If the bionic libs are directly in /apex/com.android.apex/lib then
672 // the same libs will be again loaded to the runtime linker namespace,
673 // which will result double loading of bionic libs that isn't supported.
674 dirInApex = filepath.Join(dirInApex, "bionic")
675 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900676 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900677
678 fileToCopy = cc.OutputFile().Path()
679 return
680}
681
682func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900683 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900684 if cc.Target().NativeBridge == android.NativeBridgeEnabled || !cc.Arch().Native {
685 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
686 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900687 fileToCopy = cc.OutputFile().Path()
688 return
689}
690
Alex Light778127a2019-02-27 14:19:50 -0800691func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
692 dirInApex = "bin"
693 fileToCopy = py.HostToolPath().Path()
694 return
695}
696func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
697 dirInApex = "bin"
698 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
699 if err != nil {
700 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
701 return
702 }
703 fileToCopy = android.PathForOutput(ctx, s)
704 return
705}
706
Jiyong Park04480cf2019-02-06 00:16:29 +0900707func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
708 dirInApex = filepath.Join("bin", sh.SubDir())
709 fileToCopy = sh.OutputFile()
710 return
711}
712
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900713func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
714 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900715 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900716 return
717}
718
719func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
720 dirInApex = filepath.Join("etc", prebuilt.SubDir())
721 fileToCopy = prebuilt.OutputFile()
722 return
723}
724
725func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900726 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900727
Alex Light5098a612018-11-29 17:12:15 -0800728 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
729 a.apexTypes = imageApex
730 } else if *a.properties.Payload_type == "zip" {
731 a.apexTypes = zipApex
732 } else if *a.properties.Payload_type == "both" {
733 a.apexTypes = both
734 } else {
735 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
736 return
737 }
738
Roland Levillain630846d2019-06-26 12:48:34 +0100739 if len(a.properties.Tests) > 0 && !a.testApex {
740 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
741 return
742 }
743
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800744 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
745
Jooyung Han5c998b92019-06-27 11:30:33 +0900746 // Check if "uses" requirements are met with dependent apexBundles
747 var providedNativeSharedLibs []string
748 useVendor := proptools.Bool(a.properties.Use_vendor)
749 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
750 if ctx.OtherModuleDependencyTag(m) != usesTag {
751 return
752 }
753 otherName := ctx.OtherModuleName(m)
754 other, ok := m.(*apexBundle)
755 if !ok {
756 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
757 return
758 }
759 if proptools.Bool(other.properties.Use_vendor) != useVendor {
760 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
761 return
762 }
763 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
764 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
765 return
766 }
767 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
768 })
769
Alex Light778127a2019-02-27 14:19:50 -0800770 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900771 if _, ok := parent.(*apexBundle); ok {
772 // direct dependencies
773 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900774 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900775 switch depTag {
776 case sharedLibTag:
777 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800778 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900779 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900780 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900781 } else {
782 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900783 }
784 case executableTag:
785 if cc, ok := child.(*cc.Module); ok {
786 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900787 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900788 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900789 } else if sh, ok := child.(*android.ShBinary); ok {
790 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
791 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800792 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
793 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
794 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
795 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
796 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
797 // NB: Since go binaries are static we don't need the module for anything here, which is
798 // good since the go tool is a blueprint.Module not an android.Module like we would
799 // normally use.
800 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900801 } else {
Alex Light778127a2019-02-27 14:19:50 -0800802 ctx.PropertyErrorf("binaries", "%q is neither cc_binary, (embedded) py_binary, (host) blueprint_go_binary, (host) bootstrap_go_binary, nor sh_binary", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900803 }
804 case javaLibTag:
805 if java, ok := child.(*java.Library); ok {
806 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900807 if fileToCopy == nil {
808 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
809 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900810 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900811 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900812 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900813 } else {
814 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900815 }
816 case prebuiltTag:
817 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
818 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900819 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900820 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900821 } else {
822 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
823 }
Roland Levillain630846d2019-06-26 12:48:34 +0100824 case testTag:
825 if cc, ok := child.(*cc.Module); ok {
826 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
827 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, cc, nil})
828 return true
829 } else {
830 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
831 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900832 case keyTag:
833 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900834 a.private_key_file = key.private_key_file
835 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900836 return false
837 } else {
838 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900839 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900840 case certificateTag:
841 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900842 a.container_certificate_file = dep.Certificate.Pem
843 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900844 return false
845 } else {
846 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
847 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900848 }
849 } else {
850 // indirect dependencies
851 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
852 if cc, ok := child.(*cc.Module); ok {
Jooyung Han5c998b92019-06-27 11:30:33 +0900853 if android.InList(cc.Name(), providedNativeSharedLibs) {
854 // If we're using a shared library which is provided from other APEX,
855 // don't include it in this APEX
856 return false
857 }
Alex Light49ae3d92019-02-21 14:02:46 -0800858 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900859 // If the dependency is a stubs lib, don't include it in this APEX,
860 // but make sure that the lib is installed on the device.
861 // In case no APEX is having the lib, the lib is installed to the system
862 // partition.
Alex Light49ae3d92019-02-21 14:02:46 -0800863 //
864 // Always include if we are a host-apex however since those won't have any
865 // system libraries.
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900866 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
867 a.externalDeps = append(a.externalDeps, cc.Name())
868 }
869 // Don't track further
Jiyong Park25fc6a92018-11-18 18:02:45 +0900870 return false
871 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900872 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800873 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900874 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900875 return true
876 }
877 }
878 }
879 return false
880 })
881
Jiyong Park9335a262018-12-24 11:31:58 +0900882 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900883 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900884 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
885 return
886 }
887
Jiyong Park8fd61922018-11-08 02:50:25 +0900888 // remove duplicates in filesInfo
889 removeDup := func(filesInfo []apexFile) []apexFile {
890 encountered := make(map[android.Path]bool)
891 result := []apexFile{}
892 for _, f := range filesInfo {
893 if !encountered[f.builtFile] {
894 encountered[f.builtFile] = true
895 result = append(result, f)
896 }
897 }
898 return result
899 }
900 filesInfo = removeDup(filesInfo)
901
902 // to have consistent build rules
903 sort.Slice(filesInfo, func(i, j int) bool {
904 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
905 })
906
907 // prepend the name of this APEX to the module names. These names will be the names of
908 // modules that will be defined if the APEX is flattened.
909 for i := range filesInfo {
910 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
911 }
912
Jiyong Park8fd61922018-11-08 02:50:25 +0900913 a.installDir = android.PathForModuleInstall(ctx, "apex")
914 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800915
916 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900917 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800918 }
919 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900920 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
921 // is true. This is to support referencing APEX via ":<module_name" syntax
922 // in other modules. It is in AndroidMk where the selection of flattened
923 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900924 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +0900925 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900926 }
927}
928
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700929func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +0900930 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900931 for _, f := range a.filesInfo {
932 if f.module != nil {
933 notice := f.module.NoticeFile()
934 if notice.Valid() {
935 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900936 }
937 }
938 }
939 // append the notice file specified in the apex module itself
940 if a.NoticeFile().Valid() {
941 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900942 }
943
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700944 if len(noticeFiles) == 0 {
945 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900946 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700947
Jaewoong Jung98772792019-07-01 17:15:13 -0700948 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +0900949}
950
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900951func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900952 cert := String(a.properties.Certificate)
953 if cert != "" && android.SrcIsModule(cert) == "" {
954 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900955 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
956 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900957 } else if cert == "" {
958 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900959 a.container_certificate_file = pem
960 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900961 }
962
Colin Cross8a497952019-03-05 22:25:09 -0800963 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900964
Alex Light5098a612018-11-29 17:12:15 -0800965 var abis []string
966 for _, target := range ctx.MultiTargets() {
967 if len(target.Arch.Abi) > 0 {
968 abis = append(abis, target.Arch.Abi[0])
969 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900970 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900971
Alex Light5098a612018-11-29 17:12:15 -0800972 abis = android.FirstUniqueStrings(abis)
973
974 suffix := apexType.suffix()
975 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900976
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900977 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900978 for _, f := range a.filesInfo {
979 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900980 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900981
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900982 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900983 for i, src := range filesToCopy {
984 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800985 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900986 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
987 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800988 for _, sym := range a.filesInfo[i].symlinks {
989 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
990 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
991 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900992 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900993 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800994 implicitInputs = append(implicitInputs, manifest)
995
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900996 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
997 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900998
Alex Light5098a612018-11-29 17:12:15 -0800999 if apexType.image() {
1000 // files and dirs that will be created in APEX
1001 var readOnlyPaths []string
1002 var executablePaths []string // this also includes dirs
1003 for _, f := range a.filesInfo {
1004 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001005 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001006 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001007 for _, s := range f.symlinks {
1008 executablePaths = append(executablePaths, filepath.Join("bin", s))
1009 }
Alex Light5098a612018-11-29 17:12:15 -08001010 } else {
1011 readOnlyPaths = append(readOnlyPaths, pathInApex)
1012 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001013 dir := f.installDir
1014 for !android.InList(dir, executablePaths) && dir != "" {
1015 executablePaths = append(executablePaths, dir)
1016 dir, _ = filepath.Split(dir) // move up to the parent
1017 if len(dir) > 0 {
1018 // remove trailing slash
1019 dir = dir[:len(dir)-1]
1020 }
Alex Light5098a612018-11-29 17:12:15 -08001021 }
1022 }
1023 sort.Strings(readOnlyPaths)
1024 sort.Strings(executablePaths)
1025 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1026 ctx.Build(pctx, android.BuildParams{
1027 Rule: generateFsConfig,
1028 Output: cannedFsConfig,
1029 Description: "generate fs config",
1030 Args: map[string]string{
1031 "ro_paths": strings.Join(readOnlyPaths, " "),
1032 "exec_paths": strings.Join(executablePaths, " "),
1033 },
1034 })
1035
1036 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1037 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1038 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1039 if !fileContextsOptionalPath.Valid() {
1040 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1041 return
1042 }
1043 fileContexts := fileContextsOptionalPath.Path()
1044
Jiyong Park835d82b2018-12-27 16:04:18 +09001045 optFlags := []string{}
1046
Alex Light5098a612018-11-29 17:12:15 -08001047 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001048 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1049 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001050
Jiyong Park7f67f482019-01-05 12:57:48 +09001051 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1052 if overridden {
1053 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1054 }
1055
Jiyong Park40e26a22019-02-08 02:53:06 +09001056 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001057 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001058 implicitInputs = append(implicitInputs, androidManifestFile)
1059 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1060 }
1061
Jiyong Park71b519d2019-04-18 17:25:49 +09001062 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1063 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1064 ctx.Config().UnbundledBuild() &&
1065 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1066 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1067 apiFingerprint := java.ApiFingerprintPath(ctx)
1068 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1069 implicitInputs = append(implicitInputs, apiFingerprint)
1070 }
1071 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1072
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001073 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1074 if noticeFile.Valid() {
1075 // If there's a NOTICE file, embed it as an asset file in the APEX.
1076 implicitInputs = append(implicitInputs, noticeFile.Path())
1077 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1078 }
1079
Alex Light5098a612018-11-29 17:12:15 -08001080 ctx.Build(pctx, android.BuildParams{
1081 Rule: apexRule,
1082 Implicits: implicitInputs,
1083 Output: unsignedOutputFile,
1084 Description: "apex (" + apexType.name() + ")",
1085 Args: map[string]string{
1086 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1087 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1088 "copy_commands": strings.Join(copyCommands, " && "),
1089 "manifest": manifest.String(),
1090 "file_contexts": fileContexts.String(),
1091 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001092 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001093 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001094 },
1095 })
1096
1097 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1098 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1099 a.bundleModuleFile = bundleModuleFile
1100
1101 ctx.Build(pctx, android.BuildParams{
1102 Rule: apexProtoConvertRule,
1103 Input: unsignedOutputFile,
1104 Output: apexProtoFile,
1105 Description: "apex proto convert",
1106 })
1107
1108 ctx.Build(pctx, android.BuildParams{
1109 Rule: apexBundleRule,
1110 Input: apexProtoFile,
1111 Output: a.bundleModuleFile,
1112 Description: "apex bundle module",
1113 Args: map[string]string{
1114 "abi": strings.Join(abis, "."),
1115 },
1116 })
1117 } else {
1118 ctx.Build(pctx, android.BuildParams{
1119 Rule: zipApexRule,
1120 Implicits: implicitInputs,
1121 Output: unsignedOutputFile,
1122 Description: "apex (" + apexType.name() + ")",
1123 Args: map[string]string{
1124 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1125 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1126 "copy_commands": strings.Join(copyCommands, " && "),
1127 "manifest": manifest.String(),
1128 },
1129 })
Colin Crossa4925902018-11-16 11:36:28 -08001130 }
Colin Crossa4925902018-11-16 11:36:28 -08001131
Alex Light5098a612018-11-29 17:12:15 -08001132 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001133 ctx.Build(pctx, android.BuildParams{
1134 Rule: java.Signapk,
1135 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001136 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001137 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001138 Implicits: []android.Path{
1139 a.container_certificate_file,
1140 a.container_private_key_file,
1141 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001142 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001143 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001144 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001145 },
1146 })
Alex Light5098a612018-11-29 17:12:15 -08001147
1148 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001149 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001150 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001151 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001152}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001153
Jiyong Park8fd61922018-11-08 02:50:25 +09001154func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001155 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001156 // For flattened APEX, do nothing but make sure that apex_manifest.json and apex_pubkey are also copied along
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001157 // with other ordinary files.
Colin Cross8a497952019-03-05 22:25:09 -08001158 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +09001159
1160 // rename to apex_manifest.json
1161 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
1162 ctx.Build(pctx, android.BuildParams{
1163 Rule: android.Cp,
1164 Input: manifest,
1165 Output: copiedManifest,
1166 })
Jiyong Park719b4462019-01-13 00:39:51 +09001167 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001168
Jiyong Park42cca6c2019-04-01 11:15:50 +09001169 // rename to apex_pubkey
1170 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1171 ctx.Build(pctx, android.BuildParams{
1172 Rule: android.Cp,
1173 Input: a.public_key_file,
1174 Output: copiedPubkey,
1175 })
1176 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1177
Jiyong Park23c52b02019-02-02 13:13:47 +09001178 if ctx.Config().FlattenApex() {
1179 for _, fi := range a.filesInfo {
1180 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001181 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1182 for _, sym := range fi.symlinks {
1183 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1184 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001185 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001186 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001187 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001188}
1189
1190func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -08001191 writers := []android.AndroidMkData{}
1192 if a.apexTypes.image() {
1193 writers = append(writers, a.androidMkForType(imageApex))
1194 }
1195 if a.apexTypes.zip() {
1196 writers = append(writers, a.androidMkForType(zipApex))
1197 }
1198 return android.AndroidMkData{
1199 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1200 for _, data := range writers {
1201 data.Custom(w, name, prefix, moduleDir, data)
1202 }
1203 }}
1204}
1205
Alex Lightf1801bc2019-02-13 11:10:07 -08001206func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001207 moduleNames := []string{}
1208
1209 for _, fi := range a.filesInfo {
1210 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1211 continue
1212 }
1213 if !android.InList(fi.moduleName, moduleNames) {
1214 moduleNames = append(moduleNames, fi.moduleName)
1215 }
1216 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1217 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1218 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001219 // /apex/<name>/{lib|framework|...}
1220 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1221 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001222 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001223 // /system/apex/<name>/{lib|framework|...}
1224 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1225 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001226 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001227 if len(fi.symlinks) > 0 {
1228 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1229 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001230
1231 if fi.module != nil && fi.module.NoticeFile().Valid() {
1232 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1233 }
Jiyong Park94427262019-02-05 23:18:47 +09001234 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001235 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001236 }
1237 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1238 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1239 if fi.module != nil {
1240 archStr := fi.module.Target().Arch.ArchType.String()
1241 host := false
1242 switch fi.module.Target().Os.Class {
1243 case android.Host:
1244 if archStr != "common" {
1245 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1246 }
1247 host = true
1248 case android.HostCross:
1249 if archStr != "common" {
1250 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1251 }
1252 host = true
1253 case android.Device:
1254 if archStr != "common" {
1255 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1256 }
1257 }
1258 if host {
1259 makeOs := fi.module.Target().Os.String()
1260 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1261 makeOs = "linux"
1262 }
1263 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1264 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1265 }
1266 }
1267 if fi.class == javaSharedLib {
1268 javaModule := fi.module.(*java.Library)
1269 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1270 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1271 // we will have foo.jar.jar
1272 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1273 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1274 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1275 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1276 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1277 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1278 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1279 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001280 if cc, ok := fi.module.(*cc.Module); ok {
1281 if cc.UnstrippedOutputFile() != nil {
1282 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1283 }
1284 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Park94427262019-02-05 23:18:47 +09001285 }
1286 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1287 } else {
1288 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1289 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1290 }
1291 }
1292 return moduleNames
1293}
1294
Alex Light5098a612018-11-29 17:12:15 -08001295func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001296 return android.AndroidMkData{
1297 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1298 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001299 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001300 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001301 }
1302
Jiyong Park719b4462019-01-13 00:39:51 +09001303 if a.flattened && apexType.image() {
1304 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001305 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1306 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1307 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001308 if len(moduleNames) > 0 {
1309 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1310 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001311 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001312 } else {
Alex Light5098a612018-11-29 17:12:15 -08001313 // zip-apex is the less common type so have the name refer to the image-apex
1314 // only and use {name}.zip if you want the zip-apex
1315 if apexType == zipApex && a.apexTypes == both {
1316 name = name + ".zip"
1317 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001318 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1319 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1320 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1321 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001322 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001323 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001324 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001325 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001326 if len(moduleNames) > 0 {
1327 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1328 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001329 if len(a.externalDeps) > 0 {
1330 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1331 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001332 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001333
Alex Light5098a612018-11-29 17:12:15 -08001334 if apexType == imageApex {
1335 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1336 }
Jiyong Park719b4462019-01-13 00:39:51 +09001337 }
1338 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001339}
1340
Alex Light0851b882019-02-07 13:20:53 -08001341func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001342 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001343}
1344
1345func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001346 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001347}
1348
1349func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001350 module := &apexBundle{
1351 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001352 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001353 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001354 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001355 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001356 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001357 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1358 })
Alex Light5098a612018-11-29 17:12:15 -08001359 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001360 android.InitDefaultableModule(module)
1361 return module
1362}
Jiyong Park30ca9372019-02-07 16:27:23 +09001363
1364//
1365// Defaults
1366//
1367type Defaults struct {
1368 android.ModuleBase
1369 android.DefaultsModuleBase
1370}
1371
Jiyong Park30ca9372019-02-07 16:27:23 +09001372func defaultsFactory() android.Module {
1373 return DefaultsFactory()
1374}
1375
1376func DefaultsFactory(props ...interface{}) android.Module {
1377 module := &Defaults{}
1378
1379 module.AddProperties(props...)
1380 module.AddProperties(
1381 &apexBundleProperties{},
1382 &apexTargetBundleProperties{},
1383 )
1384
1385 android.InitDefaultsModule(module)
1386 return module
1387}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001388
1389//
1390// Prebuilt APEX
1391//
1392type Prebuilt struct {
1393 android.ModuleBase
1394 prebuilt android.Prebuilt
1395
1396 properties PrebuiltProperties
1397
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001398 inputApex android.Path
1399 installDir android.OutputPath
1400 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001401 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001402}
1403
1404type PrebuiltProperties struct {
1405 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001406 Source string `blueprint:"mutated"`
1407 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001408
1409 Src *string
1410 Arch struct {
1411 Arm struct {
1412 Src *string
1413 }
1414 Arm64 struct {
1415 Src *string
1416 }
1417 X86 struct {
1418 Src *string
1419 }
1420 X86_64 struct {
1421 Src *string
1422 }
1423 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001424
1425 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001426 // Optional name for the installed apex. If unspecified, name of the
1427 // module is used as the file name
1428 Filename *string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001429}
1430
1431func (p *Prebuilt) installable() bool {
1432 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001433}
1434
1435func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001436 // If the device is configured to use flattened APEX, force disable the prebuilt because
1437 // the prebuilt is a non-flattened one.
1438 forceDisable := ctx.Config().FlattenApex()
1439
1440 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1441 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001442 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001443
1444 // b/137216042 don't use prebuilts when address sanitizer is on
1445 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1446 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1447
1448 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001449 p.properties.ForceDisable = true
1450 return
1451 }
1452
Jiyong Parkc95714e2019-03-29 14:23:10 +09001453 // This is called before prebuilt_select and prebuilt_postdeps mutators
1454 // The mutators requires that src to be set correctly for each arch so that
1455 // arch variants are disabled when src is not provided for the arch.
1456 if len(ctx.MultiTargets()) != 1 {
1457 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1458 return
1459 }
1460 var src string
1461 switch ctx.MultiTargets()[0].Arch.ArchType {
1462 case android.Arm:
1463 src = String(p.properties.Arch.Arm.Src)
1464 case android.Arm64:
1465 src = String(p.properties.Arch.Arm64.Src)
1466 case android.X86:
1467 src = String(p.properties.Arch.X86.Src)
1468 case android.X86_64:
1469 src = String(p.properties.Arch.X86_64.Src)
1470 default:
1471 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1472 return
1473 }
1474 if src == "" {
1475 src = String(p.properties.Src)
1476 }
1477 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001478}
1479
Colin Cross41955e82019-05-29 14:40:35 -07001480func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1481 switch tag {
1482 case "":
1483 return android.Paths{p.outputApex}, nil
1484 default:
1485 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1486 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001487}
1488
Jiyong Park4d277042019-04-23 18:00:10 +09001489func (p *Prebuilt) InstallFilename() string {
1490 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1491}
1492
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001493func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001494 if p.properties.ForceDisable {
1495 return
1496 }
1497
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001498 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001499 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001500 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001501 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001502 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1503 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1504 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001505 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1506 ctx.Build(pctx, android.BuildParams{
1507 Rule: android.Cp,
1508 Input: p.inputApex,
1509 Output: p.outputApex,
1510 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001511 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001512 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001513 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001514}
1515
1516func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1517 return &p.prebuilt
1518}
1519
1520func (p *Prebuilt) Name() string {
1521 return p.prebuilt.Name(p.ModuleBase.Name())
1522}
1523
1524func (p *Prebuilt) AndroidMk() android.AndroidMkData {
1525 return android.AndroidMkData{
1526 Class: "ETC",
1527 OutputFile: android.OptionalPathForPath(p.inputApex),
1528 Include: "$(BUILD_PREBUILT)",
1529 Extra: []android.AndroidMkExtraFunc{
1530 func(w io.Writer, outputFile android.Path) {
1531 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001532 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", p.installFilename)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001533 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.installable())
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001534 },
1535 },
1536 }
1537}
1538
1539// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1540func PrebuiltFactory() android.Module {
1541 module := &Prebuilt{}
1542 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001543 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001544 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001545 return module
1546}