blob: ba73163f84c3d8de57b869fe654112f259581ea7 [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())
dimitry8d6dde82019-07-11 10:23:53 +0200659 if !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900660 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200661 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
662 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900663 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800664 if handleSpecialLibs {
665 switch cc.Name() {
666 case "libc", "libm", "libdl":
667 // Special case for bionic libs. This is to prevent the bionic libs
668 // from being included in the search path /apex/com.android.apex/lib.
669 // This exclusion is required because bionic libs in the runtime APEX
670 // are available via the legacy paths /system/lib/libc.so, etc. By the
671 // init process, the bionic libs in the APEX are bind-mounted to the
672 // legacy paths and thus will be loaded into the default linker namespace.
673 // If the bionic libs are directly in /apex/com.android.apex/lib then
674 // the same libs will be again loaded to the runtime linker namespace,
675 // which will result double loading of bionic libs that isn't supported.
676 dirInApex = filepath.Join(dirInApex, "bionic")
677 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900678 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900679
680 fileToCopy = cc.OutputFile().Path()
681 return
682}
683
684func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900685 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200686 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900687 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200688 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
689 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900690 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900691 fileToCopy = cc.OutputFile().Path()
692 return
693}
694
Alex Light778127a2019-02-27 14:19:50 -0800695func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
696 dirInApex = "bin"
697 fileToCopy = py.HostToolPath().Path()
698 return
699}
700func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
701 dirInApex = "bin"
702 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
703 if err != nil {
704 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
705 return
706 }
707 fileToCopy = android.PathForOutput(ctx, s)
708 return
709}
710
Jiyong Park04480cf2019-02-06 00:16:29 +0900711func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
712 dirInApex = filepath.Join("bin", sh.SubDir())
713 fileToCopy = sh.OutputFile()
714 return
715}
716
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900717func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
718 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900719 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900720 return
721}
722
723func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
724 dirInApex = filepath.Join("etc", prebuilt.SubDir())
725 fileToCopy = prebuilt.OutputFile()
726 return
727}
728
729func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900730 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900731
Alex Light5098a612018-11-29 17:12:15 -0800732 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
733 a.apexTypes = imageApex
734 } else if *a.properties.Payload_type == "zip" {
735 a.apexTypes = zipApex
736 } else if *a.properties.Payload_type == "both" {
737 a.apexTypes = both
738 } else {
739 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
740 return
741 }
742
Roland Levillain630846d2019-06-26 12:48:34 +0100743 if len(a.properties.Tests) > 0 && !a.testApex {
744 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
745 return
746 }
747
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800748 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
749
Jooyung Han5c998b92019-06-27 11:30:33 +0900750 // Check if "uses" requirements are met with dependent apexBundles
751 var providedNativeSharedLibs []string
752 useVendor := proptools.Bool(a.properties.Use_vendor)
753 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
754 if ctx.OtherModuleDependencyTag(m) != usesTag {
755 return
756 }
757 otherName := ctx.OtherModuleName(m)
758 other, ok := m.(*apexBundle)
759 if !ok {
760 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
761 return
762 }
763 if proptools.Bool(other.properties.Use_vendor) != useVendor {
764 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
765 return
766 }
767 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
768 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
769 return
770 }
771 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
772 })
773
Alex Light778127a2019-02-27 14:19:50 -0800774 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900775 if _, ok := parent.(*apexBundle); ok {
776 // direct dependencies
777 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900778 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900779 switch depTag {
780 case sharedLibTag:
781 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800782 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900783 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900784 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900785 } else {
786 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900787 }
788 case executableTag:
789 if cc, ok := child.(*cc.Module); ok {
790 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900791 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900792 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900793 } else if sh, ok := child.(*android.ShBinary); ok {
794 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
795 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800796 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
797 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
798 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
799 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
800 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
801 // NB: Since go binaries are static we don't need the module for anything here, which is
802 // good since the go tool is a blueprint.Module not an android.Module like we would
803 // normally use.
804 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900805 } else {
Alex Light778127a2019-02-27 14:19:50 -0800806 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 +0900807 }
808 case javaLibTag:
809 if java, ok := child.(*java.Library); ok {
810 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900811 if fileToCopy == nil {
812 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
813 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900814 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900815 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900816 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900817 } else {
818 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900819 }
820 case prebuiltTag:
821 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
822 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900823 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900824 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900825 } else {
826 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
827 }
Roland Levillain630846d2019-06-26 12:48:34 +0100828 case testTag:
829 if cc, ok := child.(*cc.Module); ok {
830 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
831 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, cc, nil})
832 return true
833 } else {
834 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
835 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900836 case keyTag:
837 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900838 a.private_key_file = key.private_key_file
839 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900840 return false
841 } else {
842 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900843 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900844 case certificateTag:
845 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900846 a.container_certificate_file = dep.Certificate.Pem
847 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900848 return false
849 } else {
850 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
851 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900852 }
853 } else {
854 // indirect dependencies
855 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
856 if cc, ok := child.(*cc.Module); ok {
Jooyung Han5c998b92019-06-27 11:30:33 +0900857 if android.InList(cc.Name(), providedNativeSharedLibs) {
858 // If we're using a shared library which is provided from other APEX,
859 // don't include it in this APEX
860 return false
861 }
Alex Light49ae3d92019-02-21 14:02:46 -0800862 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900863 // If the dependency is a stubs lib, don't include it in this APEX,
864 // but make sure that the lib is installed on the device.
865 // In case no APEX is having the lib, the lib is installed to the system
866 // partition.
Alex Light49ae3d92019-02-21 14:02:46 -0800867 //
868 // Always include if we are a host-apex however since those won't have any
869 // system libraries.
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900870 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
871 a.externalDeps = append(a.externalDeps, cc.Name())
872 }
873 // Don't track further
Jiyong Park25fc6a92018-11-18 18:02:45 +0900874 return false
875 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900876 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800877 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900878 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900879 return true
880 }
881 }
882 }
883 return false
884 })
885
Jiyong Park9335a262018-12-24 11:31:58 +0900886 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900887 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900888 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
889 return
890 }
891
Jiyong Park8fd61922018-11-08 02:50:25 +0900892 // remove duplicates in filesInfo
893 removeDup := func(filesInfo []apexFile) []apexFile {
894 encountered := make(map[android.Path]bool)
895 result := []apexFile{}
896 for _, f := range filesInfo {
897 if !encountered[f.builtFile] {
898 encountered[f.builtFile] = true
899 result = append(result, f)
900 }
901 }
902 return result
903 }
904 filesInfo = removeDup(filesInfo)
905
906 // to have consistent build rules
907 sort.Slice(filesInfo, func(i, j int) bool {
908 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
909 })
910
911 // prepend the name of this APEX to the module names. These names will be the names of
912 // modules that will be defined if the APEX is flattened.
913 for i := range filesInfo {
914 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
915 }
916
Jiyong Park8fd61922018-11-08 02:50:25 +0900917 a.installDir = android.PathForModuleInstall(ctx, "apex")
918 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800919
920 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900921 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800922 }
923 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900924 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
925 // is true. This is to support referencing APEX via ":<module_name" syntax
926 // in other modules. It is in AndroidMk where the selection of flattened
927 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900928 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +0900929 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900930 }
931}
932
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700933func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +0900934 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900935 for _, f := range a.filesInfo {
936 if f.module != nil {
937 notice := f.module.NoticeFile()
938 if notice.Valid() {
939 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900940 }
941 }
942 }
943 // append the notice file specified in the apex module itself
944 if a.NoticeFile().Valid() {
945 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900946 }
947
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700948 if len(noticeFiles) == 0 {
949 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900950 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700951
Jaewoong Jung98772792019-07-01 17:15:13 -0700952 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +0900953}
954
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900955func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900956 cert := String(a.properties.Certificate)
957 if cert != "" && android.SrcIsModule(cert) == "" {
958 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900959 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
960 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900961 } else if cert == "" {
962 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900963 a.container_certificate_file = pem
964 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900965 }
966
Colin Cross8a497952019-03-05 22:25:09 -0800967 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900968
Alex Light5098a612018-11-29 17:12:15 -0800969 var abis []string
970 for _, target := range ctx.MultiTargets() {
971 if len(target.Arch.Abi) > 0 {
972 abis = append(abis, target.Arch.Abi[0])
973 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900974 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900975
Alex Light5098a612018-11-29 17:12:15 -0800976 abis = android.FirstUniqueStrings(abis)
977
978 suffix := apexType.suffix()
979 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900980
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900981 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900982 for _, f := range a.filesInfo {
983 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900984 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900985
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900986 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900987 for i, src := range filesToCopy {
988 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800989 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900990 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
991 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800992 for _, sym := range a.filesInfo[i].symlinks {
993 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
994 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
995 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900996 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900997 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800998 implicitInputs = append(implicitInputs, manifest)
999
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001000 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1001 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001002
Alex Light5098a612018-11-29 17:12:15 -08001003 if apexType.image() {
1004 // files and dirs that will be created in APEX
1005 var readOnlyPaths []string
1006 var executablePaths []string // this also includes dirs
1007 for _, f := range a.filesInfo {
1008 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001009 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001010 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001011 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001012 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001013 }
Alex Light5098a612018-11-29 17:12:15 -08001014 } else {
1015 readOnlyPaths = append(readOnlyPaths, pathInApex)
1016 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001017 dir := f.installDir
1018 for !android.InList(dir, executablePaths) && dir != "" {
1019 executablePaths = append(executablePaths, dir)
1020 dir, _ = filepath.Split(dir) // move up to the parent
1021 if len(dir) > 0 {
1022 // remove trailing slash
1023 dir = dir[:len(dir)-1]
1024 }
Alex Light5098a612018-11-29 17:12:15 -08001025 }
1026 }
1027 sort.Strings(readOnlyPaths)
1028 sort.Strings(executablePaths)
1029 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1030 ctx.Build(pctx, android.BuildParams{
1031 Rule: generateFsConfig,
1032 Output: cannedFsConfig,
1033 Description: "generate fs config",
1034 Args: map[string]string{
1035 "ro_paths": strings.Join(readOnlyPaths, " "),
1036 "exec_paths": strings.Join(executablePaths, " "),
1037 },
1038 })
1039
1040 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1041 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1042 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1043 if !fileContextsOptionalPath.Valid() {
1044 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1045 return
1046 }
1047 fileContexts := fileContextsOptionalPath.Path()
1048
Jiyong Park835d82b2018-12-27 16:04:18 +09001049 optFlags := []string{}
1050
Alex Light5098a612018-11-29 17:12:15 -08001051 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001052 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1053 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001054
Jiyong Park7f67f482019-01-05 12:57:48 +09001055 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1056 if overridden {
1057 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1058 }
1059
Jiyong Park40e26a22019-02-08 02:53:06 +09001060 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001061 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001062 implicitInputs = append(implicitInputs, androidManifestFile)
1063 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1064 }
1065
Jiyong Park71b519d2019-04-18 17:25:49 +09001066 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1067 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1068 ctx.Config().UnbundledBuild() &&
1069 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1070 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1071 apiFingerprint := java.ApiFingerprintPath(ctx)
1072 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1073 implicitInputs = append(implicitInputs, apiFingerprint)
1074 }
1075 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1076
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001077 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1078 if noticeFile.Valid() {
1079 // If there's a NOTICE file, embed it as an asset file in the APEX.
1080 implicitInputs = append(implicitInputs, noticeFile.Path())
1081 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1082 }
1083
Alex Light5098a612018-11-29 17:12:15 -08001084 ctx.Build(pctx, android.BuildParams{
1085 Rule: apexRule,
1086 Implicits: implicitInputs,
1087 Output: unsignedOutputFile,
1088 Description: "apex (" + apexType.name() + ")",
1089 Args: map[string]string{
1090 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1091 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1092 "copy_commands": strings.Join(copyCommands, " && "),
1093 "manifest": manifest.String(),
1094 "file_contexts": fileContexts.String(),
1095 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001096 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001097 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001098 },
1099 })
1100
1101 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1102 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1103 a.bundleModuleFile = bundleModuleFile
1104
1105 ctx.Build(pctx, android.BuildParams{
1106 Rule: apexProtoConvertRule,
1107 Input: unsignedOutputFile,
1108 Output: apexProtoFile,
1109 Description: "apex proto convert",
1110 })
1111
1112 ctx.Build(pctx, android.BuildParams{
1113 Rule: apexBundleRule,
1114 Input: apexProtoFile,
1115 Output: a.bundleModuleFile,
1116 Description: "apex bundle module",
1117 Args: map[string]string{
1118 "abi": strings.Join(abis, "."),
1119 },
1120 })
1121 } else {
1122 ctx.Build(pctx, android.BuildParams{
1123 Rule: zipApexRule,
1124 Implicits: implicitInputs,
1125 Output: unsignedOutputFile,
1126 Description: "apex (" + apexType.name() + ")",
1127 Args: map[string]string{
1128 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1129 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1130 "copy_commands": strings.Join(copyCommands, " && "),
1131 "manifest": manifest.String(),
1132 },
1133 })
Colin Crossa4925902018-11-16 11:36:28 -08001134 }
Colin Crossa4925902018-11-16 11:36:28 -08001135
Alex Light5098a612018-11-29 17:12:15 -08001136 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001137 ctx.Build(pctx, android.BuildParams{
1138 Rule: java.Signapk,
1139 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001140 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001141 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001142 Implicits: []android.Path{
1143 a.container_certificate_file,
1144 a.container_private_key_file,
1145 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001146 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001147 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001148 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001149 },
1150 })
Alex Light5098a612018-11-29 17:12:15 -08001151
1152 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001153 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001154 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001155 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001156}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001157
Jiyong Park8fd61922018-11-08 02:50:25 +09001158func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001159 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001160 // 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 +09001161 // with other ordinary files.
Colin Cross8a497952019-03-05 22:25:09 -08001162 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +09001163
1164 // rename to apex_manifest.json
1165 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
1166 ctx.Build(pctx, android.BuildParams{
1167 Rule: android.Cp,
1168 Input: manifest,
1169 Output: copiedManifest,
1170 })
Jiyong Park719b4462019-01-13 00:39:51 +09001171 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001172
Jiyong Park42cca6c2019-04-01 11:15:50 +09001173 // rename to apex_pubkey
1174 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1175 ctx.Build(pctx, android.BuildParams{
1176 Rule: android.Cp,
1177 Input: a.public_key_file,
1178 Output: copiedPubkey,
1179 })
1180 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1181
Jiyong Park23c52b02019-02-02 13:13:47 +09001182 if ctx.Config().FlattenApex() {
1183 for _, fi := range a.filesInfo {
1184 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001185 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1186 for _, sym := range fi.symlinks {
1187 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1188 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001189 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001190 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001191 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001192}
1193
1194func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -08001195 writers := []android.AndroidMkData{}
1196 if a.apexTypes.image() {
1197 writers = append(writers, a.androidMkForType(imageApex))
1198 }
1199 if a.apexTypes.zip() {
1200 writers = append(writers, a.androidMkForType(zipApex))
1201 }
1202 return android.AndroidMkData{
1203 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1204 for _, data := range writers {
1205 data.Custom(w, name, prefix, moduleDir, data)
1206 }
1207 }}
1208}
1209
Alex Lightf1801bc2019-02-13 11:10:07 -08001210func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001211 moduleNames := []string{}
1212
1213 for _, fi := range a.filesInfo {
1214 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1215 continue
1216 }
1217 if !android.InList(fi.moduleName, moduleNames) {
1218 moduleNames = append(moduleNames, fi.moduleName)
1219 }
1220 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1221 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1222 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001223 // /apex/<name>/{lib|framework|...}
1224 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1225 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001226 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001227 // /system/apex/<name>/{lib|framework|...}
1228 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1229 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001230 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001231 if len(fi.symlinks) > 0 {
1232 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1233 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001234
1235 if fi.module != nil && fi.module.NoticeFile().Valid() {
1236 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1237 }
Jiyong Park94427262019-02-05 23:18:47 +09001238 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001239 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001240 }
1241 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1242 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1243 if fi.module != nil {
1244 archStr := fi.module.Target().Arch.ArchType.String()
1245 host := false
1246 switch fi.module.Target().Os.Class {
1247 case android.Host:
1248 if archStr != "common" {
1249 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1250 }
1251 host = true
1252 case android.HostCross:
1253 if archStr != "common" {
1254 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1255 }
1256 host = true
1257 case android.Device:
1258 if archStr != "common" {
1259 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1260 }
1261 }
1262 if host {
1263 makeOs := fi.module.Target().Os.String()
1264 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1265 makeOs = "linux"
1266 }
1267 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1268 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1269 }
1270 }
1271 if fi.class == javaSharedLib {
1272 javaModule := fi.module.(*java.Library)
1273 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1274 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1275 // we will have foo.jar.jar
1276 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1277 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1278 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1279 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1280 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1281 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1282 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1283 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001284 if cc, ok := fi.module.(*cc.Module); ok {
1285 if cc.UnstrippedOutputFile() != nil {
1286 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1287 }
1288 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Park94427262019-02-05 23:18:47 +09001289 }
1290 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1291 } else {
1292 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1293 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1294 }
1295 }
1296 return moduleNames
1297}
1298
Alex Light5098a612018-11-29 17:12:15 -08001299func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001300 return android.AndroidMkData{
1301 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1302 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001303 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001304 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001305 }
1306
Jiyong Park719b4462019-01-13 00:39:51 +09001307 if a.flattened && apexType.image() {
1308 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001309 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1310 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1311 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001312 if len(moduleNames) > 0 {
1313 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1314 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001315 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001316 } else {
Alex Light5098a612018-11-29 17:12:15 -08001317 // zip-apex is the less common type so have the name refer to the image-apex
1318 // only and use {name}.zip if you want the zip-apex
1319 if apexType == zipApex && a.apexTypes == both {
1320 name = name + ".zip"
1321 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001322 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1323 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1324 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1325 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001326 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001327 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001328 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001329 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001330 if len(moduleNames) > 0 {
1331 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1332 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001333 if len(a.externalDeps) > 0 {
1334 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1335 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001336 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001337
Alex Light5098a612018-11-29 17:12:15 -08001338 if apexType == imageApex {
1339 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1340 }
Jiyong Park719b4462019-01-13 00:39:51 +09001341 }
1342 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001343}
1344
Alex Light0851b882019-02-07 13:20:53 -08001345func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001346 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001347}
1348
1349func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001350 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001351}
1352
1353func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001354 module := &apexBundle{
1355 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001356 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001357 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001358 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001359 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001360 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001361 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1362 })
Alex Light5098a612018-11-29 17:12:15 -08001363 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001364 android.InitDefaultableModule(module)
1365 return module
1366}
Jiyong Park30ca9372019-02-07 16:27:23 +09001367
1368//
1369// Defaults
1370//
1371type Defaults struct {
1372 android.ModuleBase
1373 android.DefaultsModuleBase
1374}
1375
Jiyong Park30ca9372019-02-07 16:27:23 +09001376func defaultsFactory() android.Module {
1377 return DefaultsFactory()
1378}
1379
1380func DefaultsFactory(props ...interface{}) android.Module {
1381 module := &Defaults{}
1382
1383 module.AddProperties(props...)
1384 module.AddProperties(
1385 &apexBundleProperties{},
1386 &apexTargetBundleProperties{},
1387 )
1388
1389 android.InitDefaultsModule(module)
1390 return module
1391}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001392
1393//
1394// Prebuilt APEX
1395//
1396type Prebuilt struct {
1397 android.ModuleBase
1398 prebuilt android.Prebuilt
1399
1400 properties PrebuiltProperties
1401
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001402 inputApex android.Path
1403 installDir android.OutputPath
1404 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001405 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001406}
1407
1408type PrebuiltProperties struct {
1409 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001410 Source string `blueprint:"mutated"`
1411 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001412
1413 Src *string
1414 Arch struct {
1415 Arm struct {
1416 Src *string
1417 }
1418 Arm64 struct {
1419 Src *string
1420 }
1421 X86 struct {
1422 Src *string
1423 }
1424 X86_64 struct {
1425 Src *string
1426 }
1427 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001428
1429 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001430 // Optional name for the installed apex. If unspecified, name of the
1431 // module is used as the file name
1432 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001433
1434 // Names of modules to be overridden. Listed modules can only be other binaries
1435 // (in Make or Soong).
1436 // This does not completely prevent installation of the overridden binaries, but if both
1437 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1438 // from PRODUCT_PACKAGES.
1439 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001440}
1441
1442func (p *Prebuilt) installable() bool {
1443 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001444}
1445
1446func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001447 // If the device is configured to use flattened APEX, force disable the prebuilt because
1448 // the prebuilt is a non-flattened one.
1449 forceDisable := ctx.Config().FlattenApex()
1450
1451 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1452 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001453 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001454
1455 // b/137216042 don't use prebuilts when address sanitizer is on
1456 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1457 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1458
1459 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001460 p.properties.ForceDisable = true
1461 return
1462 }
1463
Jiyong Parkc95714e2019-03-29 14:23:10 +09001464 // This is called before prebuilt_select and prebuilt_postdeps mutators
1465 // The mutators requires that src to be set correctly for each arch so that
1466 // arch variants are disabled when src is not provided for the arch.
1467 if len(ctx.MultiTargets()) != 1 {
1468 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1469 return
1470 }
1471 var src string
1472 switch ctx.MultiTargets()[0].Arch.ArchType {
1473 case android.Arm:
1474 src = String(p.properties.Arch.Arm.Src)
1475 case android.Arm64:
1476 src = String(p.properties.Arch.Arm64.Src)
1477 case android.X86:
1478 src = String(p.properties.Arch.X86.Src)
1479 case android.X86_64:
1480 src = String(p.properties.Arch.X86_64.Src)
1481 default:
1482 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1483 return
1484 }
1485 if src == "" {
1486 src = String(p.properties.Src)
1487 }
1488 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001489}
1490
Colin Cross41955e82019-05-29 14:40:35 -07001491func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1492 switch tag {
1493 case "":
1494 return android.Paths{p.outputApex}, nil
1495 default:
1496 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1497 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001498}
1499
Jiyong Park4d277042019-04-23 18:00:10 +09001500func (p *Prebuilt) InstallFilename() string {
1501 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1502}
1503
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001504func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001505 if p.properties.ForceDisable {
1506 return
1507 }
1508
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001509 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001510 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001511 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001512 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001513 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1514 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1515 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001516 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1517 ctx.Build(pctx, android.BuildParams{
1518 Rule: android.Cp,
1519 Input: p.inputApex,
1520 Output: p.outputApex,
1521 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001522 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001523 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001524 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001525}
1526
1527func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1528 return &p.prebuilt
1529}
1530
1531func (p *Prebuilt) Name() string {
1532 return p.prebuilt.Name(p.ModuleBase.Name())
1533}
1534
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001535func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1536 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001537 Class: "ETC",
1538 OutputFile: android.OptionalPathForPath(p.inputApex),
1539 Include: "$(BUILD_PREBUILT)",
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001540 AddCustomEntries: func(name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
1541 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1542 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1543 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1544 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001545 },
1546 }
1547}
1548
1549// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1550func PrebuiltFactory() android.Module {
1551 module := &Prebuilt{}
1552 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001553 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001554 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001555 return module
1556}