blob: 315e09f8668775242f4d93553b75b4ad9647a5a1 [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
Jooyung Hane1633032019-08-01 17:41:43 +090049 injectApexDependency = pctx.StaticRule("injectApexDependency", blueprint.RuleParams{
50 Command: `rm -f $out && ${jsonmodify} $in ` +
51 `-a provideNativeLibs ${provideNativeLibs} ` +
52 `-a requireNativeLibs ${requireNativeLibs} -o $out`,
53 CommandDeps: []string{"${jsonmodify}"},
54 Description: "Inject dependency into ${out}",
55 }, "provideNativeLibs", "requireNativeLibs")
56
Jiyong Park48ca7dc2018-10-10 14:01:00 +090057 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
58 // against the binary policy using sefcontext_compiler -p <policy>.
59
60 // TODO(b/114327326): automate the generation of file_contexts
61 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
62 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010063 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090064 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090065 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090066 `--file_contexts ${file_contexts} ` +
67 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080068 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090069 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090070 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
71 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000072 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010073 Rspfile: "${out}.copy_commands",
74 RspfileContent: "${copy_commands}",
75 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090076 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080077
Alex Light5098a612018-11-29 17:12:15 -080078 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
79 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010080 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080081 `APEXER_TOOL_PATH=${tool_path} ` +
82 `${apexer} --force --manifest ${manifest} ` +
83 `--payload_type zip ` +
84 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010085 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
86 Rspfile: "${out}.copy_commands",
87 RspfileContent: "${copy_commands}",
88 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080089 }, "tool_path", "image_dir", "copy_commands", "manifest")
90
Colin Crossa4925902018-11-16 11:36:28 -080091 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
92 blueprint.RuleParams{
93 Command: `${aapt2} convert --output-format proto $in -o $out`,
94 CommandDeps: []string{"${aapt2}"},
95 })
96
97 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090098 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000099 `apex_payload.img:apex/${abi}.img ` +
100 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +0000101 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -0800102 CommandDeps: []string{"${zip2zip}"},
103 Description: "app bundle",
104 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100105
106 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
107 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
108 Rspfile: "${out}.emit_commands",
109 RspfileContent: "${emit_commands}",
110 Description: "Emit APEX image content",
111 }, "emit_commands")
112
113 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
114 Command: `diff --unchanged-group-format='' \` +
115 `--changed-group-format='%<' \` +
116 `${image_content_file} ${whitelisted_files_file} || (` +
117 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
118 ` "To fix the build run following command:" && ` +
119 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
120 `exit 1)`,
121 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
122 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900123)
124
Alex Light5098a612018-11-29 17:12:15 -0800125var imageApexSuffix = ".apex"
126var zipApexSuffix = ".zipapex"
127
128var imageApexType = "image"
129var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900130
131type dependencyTag struct {
132 blueprint.BaseDependencyTag
133 name string
134}
135
136var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900137 sharedLibTag = dependencyTag{name: "sharedLib"}
138 executableTag = dependencyTag{name: "executable"}
139 javaLibTag = dependencyTag{name: "javaLib"}
140 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100141 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900142 keyTag = dependencyTag{name: "key"}
143 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900144 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900145 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900146)
147
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900148var (
149 whitelistNoApex = map[string][]string{
150 "apex_test_build_features": []string{"libbinder"},
151 "com.android.neuralnetworks": []string{"libbinder"},
152 "com.android.media": []string{"libbinder"},
153 "com.android.media.swcodec": []string{"libbinder"},
154 "test_com.android.media.swcodec": []string{"libbinder"},
155 }
156)
157
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900158func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700159 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900160 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900161 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100162 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
163 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
164 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
165 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000166 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100167 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
168 } else {
169 return pctx.HostBinToolPath(ctx, tool).String()
170 }
171 })
172 }
173 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900174 pctx.HostBinToolVariable("avbtool", "avbtool")
175 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
176 pctx.HostBinToolVariable("merge_zips", "merge_zips")
177 pctx.HostBinToolVariable("mke2fs", "mke2fs")
178 pctx.HostBinToolVariable("resize2fs", "resize2fs")
179 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
180 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800181 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900182 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900183 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900184
Alex Light0851b882019-02-07 13:20:53 -0800185 android.RegisterModuleType("apex", apexBundleFactory)
186 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900187 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700188 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900189
190 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
191 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700192 ctx.BottomUp("apex", apexMutator).Parallel()
Sundong Ahne9b55722019-09-06 17:37:42 +0900193 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
Jooyung Han5c998b92019-06-27 11:30:33 +0900194 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900195 })
196}
197
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900198// Mark the direct and transitive dependencies of apex bundles so that they
199// can be built for the apex bundles.
200func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800201 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800202 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900203 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900204 depName := mctx.OtherModuleName(child)
205 // If the parent is apexBundle, this child is directly depended.
206 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800207 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800208 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
209 // non-installable apex's cannot be installed and so should not prevent libraries from being
210 // installed to the system.
211 android.UpdateApexDependency(apexBundleName, depName, directDep)
212 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900213
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900214 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900215 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900216 return true
217 } else {
218 return false
219 }
220 })
221 }
222}
223
224// Create apex variations if a module is included in APEX(s).
225func apexMutator(mctx android.BottomUpMutatorContext) {
226 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900227 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900228 } else if _, ok := mctx.Module().(*apexBundle); ok {
229 // apex bundle itself is mutated so that it and its modules have same
230 // apex variant.
231 apexBundleName := mctx.ModuleName()
232 mctx.CreateVariations(apexBundleName)
233 }
234}
Sundong Ahne9b55722019-09-06 17:37:42 +0900235
236func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
237 if _, ok := mctx.Module().(*apexBundle); ok {
238 if !mctx.Config().FlattenApex() || mctx.Config().UnbundledBuild() {
239 modules := mctx.CreateLocalVariations("", "flattened")
240 modules[0].(*apexBundle).SetFlattened(false)
241 modules[1].(*apexBundle).SetFlattened(true)
242 }
243 }
244}
245
Jooyung Han5c998b92019-06-27 11:30:33 +0900246func apexUsesMutator(mctx android.BottomUpMutatorContext) {
247 if ab, ok := mctx.Module().(*apexBundle); ok {
248 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
249 }
250}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900251
Alex Light9670d332019-01-29 18:07:33 -0800252type apexNativeDependencies struct {
253 // List of native libraries
254 Native_shared_libs []string
255 // List of native executables
256 Binaries []string
Roland Levillain630846d2019-06-26 12:48:34 +0100257 // List of native tests
258 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800259}
260type apexMultilibProperties struct {
261 // Native dependencies whose compile_multilib is "first"
262 First apexNativeDependencies
263
264 // Native dependencies whose compile_multilib is "both"
265 Both apexNativeDependencies
266
267 // Native dependencies whose compile_multilib is "prefer32"
268 Prefer32 apexNativeDependencies
269
270 // Native dependencies whose compile_multilib is "32"
271 Lib32 apexNativeDependencies
272
273 // Native dependencies whose compile_multilib is "64"
274 Lib64 apexNativeDependencies
275}
276
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900277type apexBundleProperties struct {
278 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000279 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800280 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900281
Jiyong Park40e26a22019-02-08 02:53:06 +0900282 // AndroidManifest.xml file used for the zip container of this APEX bundle.
283 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800284 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900285
Jiyong Park05e70dd2019-03-18 14:26:32 +0900286 // Canonical name of the APEX bundle in the manifest file.
287 // If unspecified, defaults to the value of name
288 Apex_name *string
289
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900290 // Determines the file contexts file for setting security context to each file in this APEX bundle.
291 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
292 // used.
293 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900294 File_contexts *string
295
296 // List of native shared libs that are embedded inside this APEX bundle
297 Native_shared_libs []string
298
Roland Levillain630846d2019-06-26 12:48:34 +0100299 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900300 Binaries []string
301
302 // List of java libraries that are embedded inside this APEX bundle
303 Java_libs []string
304
305 // List of prebuilt files that are embedded inside this APEX bundle
306 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900307
Roland Levillain630846d2019-06-26 12:48:34 +0100308 // List of tests that are embedded inside this APEX bundle
309 Tests []string
310
Jiyong Parkff1458f2018-10-12 21:49:38 +0900311 // Name of the apex_key module that provides the private key to sign APEX
312 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900313
Alex Light5098a612018-11-29 17:12:15 -0800314 // The type of APEX to build. Controls what the APEX payload is. Either
315 // 'image', 'zip' or 'both'. Default: 'image'.
316 Payload_type *string
317
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900318 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
319 // or an android_app_certificate module name in the form ":module".
320 Certificate *string
321
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900322 // Whether this APEX is installable to one of the partitions. Default: true.
323 Installable *bool
324
Jiyong Parkda6eb592018-12-19 17:12:36 +0900325 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
326 // Default is false.
327 Use_vendor *bool
328
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800329 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
330 Ignore_system_library_special_case *bool
331
Alex Light9670d332019-01-29 18:07:33 -0800332 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900333
Jiyong Parkf97782b2019-02-13 20:28:58 +0900334 // List of sanitizer names that this APEX is enabled for
335 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900336
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900337 PreventInstall bool `blueprint:"mutated"`
338
339 HideFromMake bool `blueprint:"mutated"`
340
Jooyung Han5c998b92019-06-27 11:30:33 +0900341 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
342 Provide_cpp_shared_libs *bool
343
344 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
345 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100346
347 // A txt file containing list of files that are whitelisted to be included in this APEX.
348 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900349
350 // List of APKs to package inside APEX
351 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900352
353 // To distinguish between flattened and non-flattened variants.
354 // if set true, then this variant is flattened variant.
355 Flattened bool `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800356}
357
358type apexTargetBundleProperties struct {
359 Target struct {
360 // Multilib properties only for android.
361 Android struct {
362 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900363 }
Alex Light9670d332019-01-29 18:07:33 -0800364 // Multilib properties only for host.
365 Host struct {
366 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900367 }
Alex Light9670d332019-01-29 18:07:33 -0800368 // Multilib properties only for host linux_bionic.
369 Linux_bionic struct {
370 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900371 }
Alex Light9670d332019-01-29 18:07:33 -0800372 // Multilib properties only for host linux_glibc.
373 Linux_glibc struct {
374 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900375 }
376 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900377}
378
Jiyong Park8fd61922018-11-08 02:50:25 +0900379type apexFileClass int
380
381const (
382 etc apexFileClass = iota
383 nativeSharedLib
384 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900385 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800386 pyBinary
387 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900388 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100389 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900390 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900391)
392
Alex Light5098a612018-11-29 17:12:15 -0800393type apexPackaging int
394
395const (
396 imageApex apexPackaging = iota
397 zipApex
398 both
399)
400
401func (a apexPackaging) image() bool {
402 switch a {
403 case imageApex, both:
404 return true
405 }
406 return false
407}
408
409func (a apexPackaging) zip() bool {
410 switch a {
411 case zipApex, both:
412 return true
413 }
414 return false
415}
416
417func (a apexPackaging) suffix() string {
418 switch a {
419 case imageApex:
420 return imageApexSuffix
421 case zipApex:
422 return zipApexSuffix
423 case both:
424 panic(fmt.Errorf("must be either zip or image"))
425 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100426 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800427 }
428}
429
430func (a apexPackaging) name() string {
431 switch a {
432 case imageApex:
433 return imageApexType
434 case zipApex:
435 return zipApexType
436 case both:
437 panic(fmt.Errorf("must be either zip or image"))
438 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100439 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800440 }
441}
442
Jiyong Park8fd61922018-11-08 02:50:25 +0900443func (class apexFileClass) NameInMake() string {
444 switch class {
445 case etc:
446 return "ETC"
447 case nativeSharedLib:
448 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800449 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900450 return "EXECUTABLES"
451 case javaSharedLib:
452 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100453 case nativeTest:
454 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900455 case app:
456 return "APPS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900457 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100458 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900459 }
460}
461
462type apexFile struct {
463 builtFile android.Path
464 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900465 installDir string
466 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900467 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800468 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900469}
470
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900471type apexBundle struct {
472 android.ModuleBase
473 android.DefaultableModuleBase
474
Alex Light9670d332019-01-29 18:07:33 -0800475 properties apexBundleProperties
476 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900477
Alex Light5098a612018-11-29 17:12:15 -0800478 apexTypes apexPackaging
479
Colin Crossa4925902018-11-16 11:36:28 -0800480 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800481 outputFiles map[apexPackaging]android.WritablePath
Roland Levillain935639d2019-08-13 14:55:28 +0100482 flattenedOutput android.OutputPath
Colin Crossa4925902018-11-16 11:36:28 -0800483 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900484
Jiyong Park03b68dd2019-07-26 23:20:40 +0900485 prebuiltFileToDelete string
486
Jiyong Park42cca6c2019-04-01 11:15:50 +0900487 public_key_file android.Path
488 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900489
490 container_certificate_file android.Path
491 container_private_key_file android.Path
492
Jiyong Park8fd61922018-11-08 02:50:25 +0900493 // list of files to be included in this apex
494 filesInfo []apexFile
495
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900496 // list of module names that this APEX is depending on
497 externalDeps []string
498
Alex Light0851b882019-02-07 13:20:53 -0800499 testApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900500
501 // intermediate path for apex_manifest.json
502 manifestOut android.WritablePath
Sundong Ahne9b55722019-09-06 17:37:42 +0900503
504 // A config value of (TARGET_FLATTEN_APEX && !TARGET_BUILD_APPS)
505 flattenedConfigValue bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900506}
507
Jiyong Park397e55e2018-10-24 21:09:55 +0900508func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100509 native_shared_libs []string, binaries []string, tests []string,
510 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900511 // Use *FarVariation* to be able to depend on modules having
512 // conflicting variations with this module. This is required since
513 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
514 // for native shared libs.
515 ctx.AddFarVariationDependencies([]blueprint.Variation{
516 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900517 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900518 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900519 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900520 }, sharedLibTag, native_shared_libs...)
521
522 ctx.AddFarVariationDependencies([]blueprint.Variation{
523 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900524 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900525 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100526
527 ctx.AddFarVariationDependencies([]blueprint.Variation{
528 {Mutator: "arch", Variation: arch},
529 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100530 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100531 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900532}
533
Alex Light9670d332019-01-29 18:07:33 -0800534func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
535 if ctx.Os().Class == android.Device {
536 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
537 } else {
538 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
539 if ctx.Os().Bionic() {
540 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
541 } else {
542 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
543 }
544 }
545}
546
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900547func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800548
Jiyong Park397e55e2018-10-24 21:09:55 +0900549 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900550 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800551
552 a.combineProperties(ctx)
553
Jiyong Park397e55e2018-10-24 21:09:55 +0900554 has32BitTarget := false
555 for _, target := range targets {
556 if target.Arch.ArchType.Multilib == "lib32" {
557 has32BitTarget = true
558 }
559 }
560 for i, target := range targets {
561 // When multilib.* is omitted for native_shared_libs, it implies
562 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900563 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900564 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900565 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900566 {Mutator: "link", Variation: "shared"},
567 }, sharedLibTag, a.properties.Native_shared_libs...)
568
Roland Levillain630846d2019-06-26 12:48:34 +0100569 // When multilib.* is omitted for tests, it implies
570 // multilib.both.
571 ctx.AddFarVariationDependencies([]blueprint.Variation{
572 {Mutator: "arch", Variation: target.String()},
573 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100574 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100575 }, testTag, a.properties.Tests...)
576
Jiyong Park397e55e2018-10-24 21:09:55 +0900577 // Add native modules targetting both ABIs
578 addDependenciesForNativeModules(ctx,
579 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100580 a.properties.Multilib.Both.Binaries,
581 a.properties.Multilib.Both.Tests,
582 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900583 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900584
Alex Light3d673592019-01-18 14:37:31 -0800585 isPrimaryAbi := i == 0
586 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900587 // When multilib.* is omitted for binaries, it implies
588 // multilib.first.
589 ctx.AddFarVariationDependencies([]blueprint.Variation{
590 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900591 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900592 }, executableTag, a.properties.Binaries...)
593
594 // Add native modules targetting the first ABI
595 addDependenciesForNativeModules(ctx,
596 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100597 a.properties.Multilib.First.Binaries,
598 a.properties.Multilib.First.Tests,
599 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900600 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800601
602 // When multilib.* is omitted for prebuilts, it implies multilib.first.
603 ctx.AddFarVariationDependencies([]blueprint.Variation{
604 {Mutator: "arch", Variation: target.String()},
605 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900606 }
607
608 switch target.Arch.ArchType.Multilib {
609 case "lib32":
610 // Add native modules targetting 32-bit ABI
611 addDependenciesForNativeModules(ctx,
612 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100613 a.properties.Multilib.Lib32.Binaries,
614 a.properties.Multilib.Lib32.Tests,
615 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900616 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900617
618 addDependenciesForNativeModules(ctx,
619 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100620 a.properties.Multilib.Prefer32.Binaries,
621 a.properties.Multilib.Prefer32.Tests,
622 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900623 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900624 case "lib64":
625 // Add native modules targetting 64-bit ABI
626 addDependenciesForNativeModules(ctx,
627 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100628 a.properties.Multilib.Lib64.Binaries,
629 a.properties.Multilib.Lib64.Tests,
630 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900631 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900632
633 if !has32BitTarget {
634 addDependenciesForNativeModules(ctx,
635 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100636 a.properties.Multilib.Prefer32.Binaries,
637 a.properties.Multilib.Prefer32.Tests,
638 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900639 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900640 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700641
642 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
643 for _, sanitizer := range ctx.Config().SanitizeDevice() {
644 if sanitizer == "hwaddress" {
645 addDependenciesForNativeModules(ctx,
646 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100647 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700648 break
649 }
650 }
651 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900652 }
653
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900654 }
655
Jiyong Parkff1458f2018-10-12 21:49:38 +0900656 ctx.AddFarVariationDependencies([]blueprint.Variation{
657 {Mutator: "arch", Variation: "android_common"},
658 }, javaLibTag, a.properties.Java_libs...)
659
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900660 ctx.AddFarVariationDependencies([]blueprint.Variation{
661 {Mutator: "arch", Variation: "android_common"},
662 }, androidAppTag, a.properties.Apps...)
663
Jiyong Park23c52b02019-02-02 13:13:47 +0900664 if String(a.properties.Key) == "" {
665 ctx.ModuleErrorf("key is missing")
666 return
667 }
668 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900669
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900670 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900671 if cert != "" {
672 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900673 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900674}
675
Colin Cross0ea8ba82019-06-06 14:33:29 -0700676func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900677 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
678 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000679 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900680 }
681 return String(a.properties.Certificate)
682}
683
Colin Cross41955e82019-05-29 14:40:35 -0700684func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
685 switch tag {
686 case "":
687 if file, ok := a.outputFiles[imageApex]; ok {
688 return android.Paths{file}, nil
689 } else {
690 return nil, nil
691 }
Roland Levillain935639d2019-08-13 14:55:28 +0100692 case ".flattened":
Sundong Ahne9b55722019-09-06 17:37:42 +0900693 if a.properties.Flattened {
Roland Levillain935639d2019-08-13 14:55:28 +0100694 flattenedApexPath := a.flattenedOutput
695 return android.Paths{flattenedApexPath}, nil
696 } else {
697 return nil, nil
698 }
Colin Cross41955e82019-05-29 14:40:35 -0700699 default:
700 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900701 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900702}
703
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900704func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900705 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900706}
707
Jiyong Park7c1dc612019-01-05 11:15:24 +0900708func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
709 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900710 return "vendor"
711 } else {
712 return "core"
713 }
714}
715
Jiyong Parkf97782b2019-02-13 20:28:58 +0900716func (a *apexBundle) EnableSanitizer(sanitizerName string) {
717 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
718 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
719 }
720}
721
Jiyong Park388ef3f2019-01-28 19:47:32 +0900722func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900723 if android.InList(sanitizerName, a.properties.SanitizerNames) {
724 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900725 }
726
727 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900728 globalSanitizerNames := []string{}
729 if a.Host() {
730 globalSanitizerNames = ctx.Config().SanitizeHost()
731 } else {
732 arches := ctx.Config().SanitizeDeviceArch()
733 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
734 globalSanitizerNames = ctx.Config().SanitizeDevice()
735 }
736 }
737 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900738}
739
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900740func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
741 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
742}
743
744func (a *apexBundle) PreventInstall() {
745 a.properties.PreventInstall = true
746}
747
748func (a *apexBundle) HideFromMake() {
749 a.properties.HideFromMake = true
750}
751
Sundong Ahne9b55722019-09-06 17:37:42 +0900752func (a *apexBundle) SetFlattened(flattened bool) {
753 a.properties.Flattened = flattened
754}
755
Martin Stjernholm279de572019-09-10 23:18:20 +0100756func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900757 // Decide the APEX-local directory by the multilib of the library
758 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100759 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900760 case "lib32":
761 dirInApex = "lib"
762 case "lib64":
763 dirInApex = "lib64"
764 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100765 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
766 if !ccMod.Arch().Native {
767 dirInApex = filepath.Join(dirInApex, ccMod.Arch().ArchType.String())
768 } else if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
769 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900770 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100771 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
772 // Special case for Bionic libs and other libs installed with them. This is
773 // to prevent those libs from being included in the search path
774 // /apex/com.android.runtime/${LIB}. This exclusion is required because
775 // those libs in the Runtime APEX are available via the legacy paths in
776 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
777 // to the legacy paths and thus will be loaded into the default linker
778 // namespace (aka "platform" namespace). If the libs are directly in
779 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
780 // into the runtime linker namespace, which will result in double loading of
781 // them, which isn't supported.
782 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900783 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900784
Martin Stjernholm279de572019-09-10 23:18:20 +0100785 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900786 return
787}
788
789func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900790 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200791 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900792 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200793 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
794 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900795 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900796 fileToCopy = cc.OutputFile().Path()
797 return
798}
799
Alex Light778127a2019-02-27 14:19:50 -0800800func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
801 dirInApex = "bin"
802 fileToCopy = py.HostToolPath().Path()
803 return
804}
805func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
806 dirInApex = "bin"
807 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
808 if err != nil {
809 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
810 return
811 }
812 fileToCopy = android.PathForOutput(ctx, s)
813 return
814}
815
Jiyong Park04480cf2019-02-06 00:16:29 +0900816func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
817 dirInApex = filepath.Join("bin", sh.SubDir())
818 fileToCopy = sh.OutputFile()
819 return
820}
821
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900822func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
823 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900824 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900825 return
826}
827
Jiyong Park9e6c2422019-08-09 20:39:45 +0900828func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
829 dirInApex = "javalib"
830 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
831 implJars := java.ImplementationJars()
832 if len(implJars) != 1 {
833 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
834 strings.Join(implJars.Strings(), ", ")))
835 }
836 fileToCopy = implJars[0]
837 return
838}
839
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900840func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
841 dirInApex = filepath.Join("etc", prebuilt.SubDir())
842 fileToCopy = prebuilt.OutputFile()
843 return
844}
845
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900846func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
847 dirInApex = filepath.Join("app", pkgName)
848 fileToCopy = app.OutputFile()
849 return
850}
851
Roland Levillain935639d2019-08-13 14:55:28 +0100852// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
853type flattenedApexContext struct {
854 android.ModuleContext
855}
856
857func (c *flattenedApexContext) InstallBypassMake() bool {
858 return true
859}
860
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900861func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900862 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900863
Alex Light5098a612018-11-29 17:12:15 -0800864 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
865 a.apexTypes = imageApex
866 } else if *a.properties.Payload_type == "zip" {
867 a.apexTypes = zipApex
868 } else if *a.properties.Payload_type == "both" {
869 a.apexTypes = both
870 } else {
871 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
872 return
873 }
874
Roland Levillain630846d2019-06-26 12:48:34 +0100875 if len(a.properties.Tests) > 0 && !a.testApex {
876 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
877 return
878 }
879
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800880 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
881
Jooyung Hane1633032019-08-01 17:41:43 +0900882 // native lib dependencies
883 var provideNativeLibs []string
884 var requireNativeLibs []string
885
Jooyung Han5c998b92019-06-27 11:30:33 +0900886 // Check if "uses" requirements are met with dependent apexBundles
887 var providedNativeSharedLibs []string
888 useVendor := proptools.Bool(a.properties.Use_vendor)
889 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
890 if ctx.OtherModuleDependencyTag(m) != usesTag {
891 return
892 }
893 otherName := ctx.OtherModuleName(m)
894 other, ok := m.(*apexBundle)
895 if !ok {
896 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
897 return
898 }
899 if proptools.Bool(other.properties.Use_vendor) != useVendor {
900 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
901 return
902 }
903 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
904 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
905 return
906 }
907 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
908 })
909
Alex Light778127a2019-02-27 14:19:50 -0800910 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100911 depTag := ctx.OtherModuleDependencyTag(child)
912 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900913 if _, ok := parent.(*apexBundle); ok {
914 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900915 switch depTag {
916 case sharedLibTag:
917 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900918 if cc.HasStubsVariants() {
919 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
920 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100921 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900922 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900923 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900924 } else {
925 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900926 }
927 case executableTag:
928 if cc, ok := child.(*cc.Module); ok {
929 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900930 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900931 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900932 } else if sh, ok := child.(*android.ShBinary); ok {
933 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
934 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800935 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
936 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
937 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
938 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
939 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
940 // NB: Since go binaries are static we don't need the module for anything here, which is
941 // good since the go tool is a blueprint.Module not an android.Module like we would
942 // normally use.
943 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900944 } else {
Alex Light778127a2019-02-27 14:19:50 -0800945 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 +0900946 }
947 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +0900948 if javaLib, ok := child.(*java.Library); ok {
949 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +0900950 if fileToCopy == nil {
951 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
952 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900953 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
954 }
955 return true
956 } else if javaLib, ok := child.(*java.Import); ok {
957 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
958 if fileToCopy == nil {
959 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
960 } else {
961 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900962 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900963 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900964 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900965 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900966 }
967 case prebuiltTag:
968 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
969 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900970 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900971 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900972 } else {
973 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
974 }
Roland Levillain630846d2019-06-26 12:48:34 +0100975 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100976 if ccTest, ok := child.(*cc.Module); ok {
977 if ccTest.IsTestPerSrcAllTestsVariation() {
978 // Multiple-output test module (where `test_per_src: true`).
979 //
980 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
981 // We do not add this variation to `filesInfo`, as it has no output;
982 // however, we do add the other variations of this module as indirect
983 // dependencies (see below).
984 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100985 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100986 // Single-output test module (where `test_per_src: false`).
987 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
988 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100989 }
Roland Levillain630846d2019-06-26 12:48:34 +0100990 return true
991 } else {
992 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
993 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900994 case keyTag:
995 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900996 a.private_key_file = key.private_key_file
997 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900998 return false
999 } else {
1000 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001001 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001002 case certificateTag:
1003 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001004 a.container_certificate_file = dep.Certificate.Pem
1005 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001006 return false
1007 } else {
1008 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1009 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001010 case android.PrebuiltDepTag:
1011 // If the prebuilt is force disabled, remember to delete the prebuilt file
1012 // that might have been installed in the previous builds
1013 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1014 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1015 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001016 case androidAppTag:
1017 if ap, ok := child.(*java.AndroidApp); ok {
1018 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1019 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1020 return true
1021 } else {
1022 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1023 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001024 }
1025 } else {
1026 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001027 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001028 // We cannot use a switch statement on `depTag` here as the checked
1029 // tags used below are private (e.g. `cc.sharedDepTag`).
1030 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1031 if cc, ok := child.(*cc.Module); ok {
1032 if android.InList(cc.Name(), providedNativeSharedLibs) {
1033 // If we're using a shared library which is provided from other APEX,
1034 // don't include it in this APEX
1035 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001036 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001037 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1038 // If the dependency is a stubs lib, don't include it in this APEX,
1039 // but make sure that the lib is installed on the device.
1040 // In case no APEX is having the lib, the lib is installed to the system
1041 // partition.
1042 //
1043 // Always include if we are a host-apex however since those won't have any
1044 // system libraries.
1045 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1046 a.externalDeps = append(a.externalDeps, cc.Name())
1047 }
Jooyung Hane1633032019-08-01 17:41:43 +09001048 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001049 // Don't track further
1050 return false
1051 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001052 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001053 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1054 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001055 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001056 } else if cc.IsTestPerSrcDepTag(depTag) {
1057 if cc, ok := child.(*cc.Module); ok {
1058 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1059 // Handle modules created as `test_per_src` variations of a single test module:
1060 // use the name of the generated test binary (`fileToCopy`) instead of the name
1061 // of the original test module (`depName`, shared by all `test_per_src`
1062 // variations of that module).
1063 moduleName := filepath.Base(fileToCopy.String())
1064 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1065 return true
1066 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001067 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001068 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Sundong Ahn2db7f462019-08-27 18:53:12 +09001069 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
1070 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001071 }
1072 }
1073 }
1074 return false
1075 })
1076
Sundong Ahne9b55722019-09-06 17:37:42 +09001077 a.flattenedConfigValue = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1078 if a.flattenedConfigValue {
1079 a.properties.Flattened = true
1080 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001081 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001082 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1083 return
1084 }
1085
Jiyong Park8fd61922018-11-08 02:50:25 +09001086 // remove duplicates in filesInfo
1087 removeDup := func(filesInfo []apexFile) []apexFile {
1088 encountered := make(map[android.Path]bool)
1089 result := []apexFile{}
1090 for _, f := range filesInfo {
1091 if !encountered[f.builtFile] {
1092 encountered[f.builtFile] = true
1093 result = append(result, f)
1094 }
1095 }
1096 return result
1097 }
1098 filesInfo = removeDup(filesInfo)
1099
1100 // to have consistent build rules
1101 sort.Slice(filesInfo, func(i, j int) bool {
1102 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1103 })
1104
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001105 // check no_apex modules
1106 whitelist := whitelistNoApex[ctx.ModuleName()]
1107 for i := range filesInfo {
1108 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1109 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1110 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1111 }
1112 }
1113 }
1114
Jiyong Park8fd61922018-11-08 02:50:25 +09001115 // prepend the name of this APEX to the module names. These names will be the names of
1116 // modules that will be defined if the APEX is flattened.
1117 for i := range filesInfo {
1118 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1119 }
1120
Jiyong Park8fd61922018-11-08 02:50:25 +09001121 a.installDir = android.PathForModuleInstall(ctx, "apex")
1122 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001123
Jooyung Hane1633032019-08-01 17:41:43 +09001124 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1125 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1126 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1127 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1128 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1129 ctx.Build(pctx, android.BuildParams{
1130 Rule: injectApexDependency,
1131 Input: manifestSrc,
1132 Output: a.manifestOut,
1133 Args: map[string]string{
1134 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1135 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1136 },
1137 })
1138
Roland Levillain935639d2019-08-13 14:55:28 +01001139 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1140 // reply true to `InstallBypassMake()` (thus making the call
1141 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1142 // instead of `android.PathForOutput`) to return the correct path to the flattened
1143 // APEX (as its contents is installed by Make, not Soong).
1144 factx := flattenedApexContext{ctx}
1145 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1146
Alex Light5098a612018-11-29 17:12:15 -08001147 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001148 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001149 }
1150 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001151 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001152 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001153 // in other modules. It is in AndroidMk where the selection of flattened
1154 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001155 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001156 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001157 }
1158}
1159
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001160func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001161 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001162 for _, f := range a.filesInfo {
1163 if f.module != nil {
1164 notice := f.module.NoticeFile()
1165 if notice.Valid() {
1166 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001167 }
1168 }
1169 }
1170 // append the notice file specified in the apex module itself
1171 if a.NoticeFile().Valid() {
1172 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001173 }
1174
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001175 if len(noticeFiles) == 0 {
1176 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001177 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001178
Jaewoong Jung98772792019-07-01 17:15:13 -07001179 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001180}
1181
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001182func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001183 cert := String(a.properties.Certificate)
1184 if cert != "" && android.SrcIsModule(cert) == "" {
1185 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001186 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1187 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001188 } else if cert == "" {
1189 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001190 a.container_certificate_file = pem
1191 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001192 }
1193
Alex Light5098a612018-11-29 17:12:15 -08001194 var abis []string
1195 for _, target := range ctx.MultiTargets() {
1196 if len(target.Arch.Abi) > 0 {
1197 abis = append(abis, target.Arch.Abi[0])
1198 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001199 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001200
Alex Light5098a612018-11-29 17:12:15 -08001201 abis = android.FirstUniqueStrings(abis)
1202
1203 suffix := apexType.suffix()
1204 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001205
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001206 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001207 for _, f := range a.filesInfo {
1208 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001209 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001210
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001211 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001212 emitCommands := []string{}
1213 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1214 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001215 for i, src := range filesToCopy {
1216 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001217 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001218 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001219 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1220 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001221 for _, sym := range a.filesInfo[i].symlinks {
1222 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1223 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1224 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001225 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001226 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001227 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001228
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001229 if a.properties.Whitelisted_files != nil {
1230 ctx.Build(pctx, android.BuildParams{
1231 Rule: emitApexContentRule,
1232 Implicits: implicitInputs,
1233 Output: imageContentFile,
1234 Description: "emit apex image content",
1235 Args: map[string]string{
1236 "emit_commands": strings.Join(emitCommands, " && "),
1237 },
1238 })
1239 implicitInputs = append(implicitInputs, imageContentFile)
1240 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1241
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001242 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001243 ctx.Build(pctx, android.BuildParams{
1244 Rule: diffApexContentRule,
1245 Implicits: implicitInputs,
1246 Output: phonyOutput,
1247 Description: "diff apex image content",
1248 Args: map[string]string{
1249 "whitelisted_files_file": whitelistedFilesFile.String(),
1250 "image_content_file": imageContentFile.String(),
1251 "apex_module_name": ctx.ModuleName(),
1252 },
1253 })
1254
1255 implicitInputs = append(implicitInputs, phonyOutput)
1256 }
1257
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001258 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1259 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001260
Alex Light5098a612018-11-29 17:12:15 -08001261 if apexType.image() {
1262 // files and dirs that will be created in APEX
1263 var readOnlyPaths []string
1264 var executablePaths []string // this also includes dirs
1265 for _, f := range a.filesInfo {
1266 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001267 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001268 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001269 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001270 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001271 }
Alex Light5098a612018-11-29 17:12:15 -08001272 } else {
1273 readOnlyPaths = append(readOnlyPaths, pathInApex)
1274 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001275 dir := f.installDir
1276 for !android.InList(dir, executablePaths) && dir != "" {
1277 executablePaths = append(executablePaths, dir)
1278 dir, _ = filepath.Split(dir) // move up to the parent
1279 if len(dir) > 0 {
1280 // remove trailing slash
1281 dir = dir[:len(dir)-1]
1282 }
Alex Light5098a612018-11-29 17:12:15 -08001283 }
1284 }
1285 sort.Strings(readOnlyPaths)
1286 sort.Strings(executablePaths)
1287 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1288 ctx.Build(pctx, android.BuildParams{
1289 Rule: generateFsConfig,
1290 Output: cannedFsConfig,
1291 Description: "generate fs config",
1292 Args: map[string]string{
1293 "ro_paths": strings.Join(readOnlyPaths, " "),
1294 "exec_paths": strings.Join(executablePaths, " "),
1295 },
1296 })
1297
1298 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1299 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1300 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1301 if !fileContextsOptionalPath.Valid() {
1302 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1303 return
1304 }
1305 fileContexts := fileContextsOptionalPath.Path()
1306
Jiyong Park835d82b2018-12-27 16:04:18 +09001307 optFlags := []string{}
1308
Alex Light5098a612018-11-29 17:12:15 -08001309 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001310 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1311 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001312
Jiyong Park7f67f482019-01-05 12:57:48 +09001313 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1314 if overridden {
1315 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1316 }
1317
Jiyong Park40e26a22019-02-08 02:53:06 +09001318 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001319 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001320 implicitInputs = append(implicitInputs, androidManifestFile)
1321 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1322 }
1323
Jiyong Park71b519d2019-04-18 17:25:49 +09001324 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1325 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1326 ctx.Config().UnbundledBuild() &&
1327 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1328 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1329 apiFingerprint := java.ApiFingerprintPath(ctx)
1330 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1331 implicitInputs = append(implicitInputs, apiFingerprint)
1332 }
1333 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1334
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001335 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1336 if noticeFile.Valid() {
1337 // If there's a NOTICE file, embed it as an asset file in the APEX.
1338 implicitInputs = append(implicitInputs, noticeFile.Path())
1339 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1340 }
1341
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001342 if !ctx.Config().UnbundledBuild() && a.installable() {
1343 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1344 // don't need hashtree for activation. Therefore, by removing hashtree from
1345 // apex bundle (filesystem image in it, to be specific), we can save storage.
1346 optFlags = append(optFlags, "--no_hashtree")
1347 }
1348
Alex Light5098a612018-11-29 17:12:15 -08001349 ctx.Build(pctx, android.BuildParams{
1350 Rule: apexRule,
1351 Implicits: implicitInputs,
1352 Output: unsignedOutputFile,
1353 Description: "apex (" + apexType.name() + ")",
1354 Args: map[string]string{
1355 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1356 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1357 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001358 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001359 "file_contexts": fileContexts.String(),
1360 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001361 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001362 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001363 },
1364 })
1365
1366 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1367 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1368 a.bundleModuleFile = bundleModuleFile
1369
1370 ctx.Build(pctx, android.BuildParams{
1371 Rule: apexProtoConvertRule,
1372 Input: unsignedOutputFile,
1373 Output: apexProtoFile,
1374 Description: "apex proto convert",
1375 })
1376
1377 ctx.Build(pctx, android.BuildParams{
1378 Rule: apexBundleRule,
1379 Input: apexProtoFile,
1380 Output: a.bundleModuleFile,
1381 Description: "apex bundle module",
1382 Args: map[string]string{
1383 "abi": strings.Join(abis, "."),
1384 },
1385 })
1386 } else {
1387 ctx.Build(pctx, android.BuildParams{
1388 Rule: zipApexRule,
1389 Implicits: implicitInputs,
1390 Output: unsignedOutputFile,
1391 Description: "apex (" + apexType.name() + ")",
1392 Args: map[string]string{
1393 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1394 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1395 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001396 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001397 },
1398 })
Colin Crossa4925902018-11-16 11:36:28 -08001399 }
Colin Crossa4925902018-11-16 11:36:28 -08001400
Alex Light5098a612018-11-29 17:12:15 -08001401 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001402 ctx.Build(pctx, android.BuildParams{
1403 Rule: java.Signapk,
1404 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001405 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001406 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001407 Implicits: []android.Path{
1408 a.container_certificate_file,
1409 a.container_private_key_file,
1410 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001411 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001412 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001413 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001414 },
1415 })
Alex Light5098a612018-11-29 17:12:15 -08001416
1417 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahne9b55722019-09-06 17:37:42 +09001418 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && !a.properties.Flattened {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001419 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001420 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001421}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001422
Jiyong Park8fd61922018-11-08 02:50:25 +09001423func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001424 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001425 // 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 +09001426 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001427 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001428
Jiyong Park42cca6c2019-04-01 11:15:50 +09001429 // rename to apex_pubkey
1430 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1431 ctx.Build(pctx, android.BuildParams{
1432 Rule: android.Cp,
1433 Input: a.public_key_file,
1434 Output: copiedPubkey,
1435 })
1436 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1437
Jiyong Park23c52b02019-02-02 13:13:47 +09001438 if ctx.Config().FlattenApex() {
1439 for _, fi := range a.filesInfo {
1440 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001441 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1442 for _, sym := range fi.symlinks {
1443 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1444 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001445 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001446 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001447 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001448}
1449
1450func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001451 if a.properties.HideFromMake {
1452 return android.AndroidMkData{
1453 Disabled: true,
1454 }
1455 }
Alex Light5098a612018-11-29 17:12:15 -08001456 writers := []android.AndroidMkData{}
1457 if a.apexTypes.image() {
1458 writers = append(writers, a.androidMkForType(imageApex))
1459 }
1460 if a.apexTypes.zip() {
1461 writers = append(writers, a.androidMkForType(zipApex))
1462 }
1463 return android.AndroidMkData{
1464 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1465 for _, data := range writers {
1466 data.Custom(w, name, prefix, moduleDir, data)
1467 }
1468 }}
1469}
1470
Alex Lightf1801bc2019-02-13 11:10:07 -08001471func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001472 moduleNames := []string{}
1473
1474 for _, fi := range a.filesInfo {
1475 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1476 continue
1477 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001478 if a.properties.Flattened && !apexType.image() {
1479 continue
Jiyong Park94427262019-02-05 23:18:47 +09001480 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001481
1482 var suffix string
1483 if a.properties.Flattened && !a.flattenedConfigValue {
1484 suffix = ".flattened"
1485 }
1486
1487 if !android.InList(fi.moduleName, moduleNames) {
1488 moduleNames = append(moduleNames, fi.moduleName+suffix)
1489 }
1490
Jiyong Park94427262019-02-05 23:18:47 +09001491 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1492 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001493 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName+suffix)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001494 // /apex/<name>/{lib|framework|...}
1495 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1496 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001497 if a.properties.Flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001498 // /system/apex/<name>/{lib|framework|...}
1499 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1500 a.installDir.RelPathString(), name, fi.installDir))
Sundong Ahne9b55722019-09-06 17:37:42 +09001501 if a.flattenedConfigValue {
1502 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1503 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001504 if len(fi.symlinks) > 0 {
1505 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1506 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001507
1508 if fi.module != nil && fi.module.NoticeFile().Valid() {
1509 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1510 }
Jiyong Park94427262019-02-05 23:18:47 +09001511 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001512 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001513 }
1514 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1515 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1516 if fi.module != nil {
1517 archStr := fi.module.Target().Arch.ArchType.String()
1518 host := false
1519 switch fi.module.Target().Os.Class {
1520 case android.Host:
1521 if archStr != "common" {
1522 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1523 }
1524 host = true
1525 case android.HostCross:
1526 if archStr != "common" {
1527 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1528 }
1529 host = true
1530 case android.Device:
1531 if archStr != "common" {
1532 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1533 }
1534 }
1535 if host {
1536 makeOs := fi.module.Target().Os.String()
1537 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1538 makeOs = "linux"
1539 }
1540 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1541 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1542 }
1543 }
1544 if fi.class == javaSharedLib {
1545 javaModule := fi.module.(*java.Library)
1546 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1547 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1548 // we will have foo.jar.jar
1549 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1550 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1551 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1552 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1553 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1554 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001555 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001556 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001557 if cc, ok := fi.module.(*cc.Module); ok {
1558 if cc.UnstrippedOutputFile() != nil {
1559 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1560 }
1561 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001562 if cc.CoverageOutputFile().Valid() {
1563 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1564 }
Jiyong Park94427262019-02-05 23:18:47 +09001565 }
1566 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1567 } else {
1568 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1569 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1570 }
1571 }
1572 return moduleNames
1573}
1574
Alex Light5098a612018-11-29 17:12:15 -08001575func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001576 return android.AndroidMkData{
1577 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1578 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001579 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001580 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001581 }
1582
Sundong Ahne9b55722019-09-06 17:37:42 +09001583 if a.properties.Flattened && !a.flattenedConfigValue {
1584 name = name + ".flattened"
1585 }
1586
1587 if a.properties.Flattened && apexType.image() {
Jiyong Park719b4462019-01-13 00:39:51 +09001588 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001589 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1590 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1591 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001592 if len(moduleNames) > 0 {
1593 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1594 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001595 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001596 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1597
Sundong Ahne9b55722019-09-06 17:37:42 +09001598 } else if !a.properties.Flattened {
Alex Light5098a612018-11-29 17:12:15 -08001599 // zip-apex is the less common type so have the name refer to the image-apex
1600 // only and use {name}.zip if you want the zip-apex
1601 if apexType == zipApex && a.apexTypes == both {
1602 name = name + ".zip"
1603 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001604 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1605 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1606 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1607 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001608 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001609 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001610 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001611 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001612 if len(moduleNames) > 0 {
1613 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1614 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001615 if len(a.externalDeps) > 0 {
1616 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1617 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001618 if a.prebuiltFileToDelete != "" {
1619 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1620 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1621 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001622 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001623
Alex Light5098a612018-11-29 17:12:15 -08001624 if apexType == imageApex {
1625 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1626 }
Jiyong Park719b4462019-01-13 00:39:51 +09001627 }
1628 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001629}
1630
Alex Light0851b882019-02-07 13:20:53 -08001631func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001632 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001633}
1634
1635func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001636 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001637}
1638
1639func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001640 module := &apexBundle{
1641 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001642 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001643 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001644 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001645 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001646 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001647 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1648 })
Alex Light5098a612018-11-29 17:12:15 -08001649 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001650 android.InitDefaultableModule(module)
1651 return module
1652}
Jiyong Park30ca9372019-02-07 16:27:23 +09001653
1654//
1655// Defaults
1656//
1657type Defaults struct {
1658 android.ModuleBase
1659 android.DefaultsModuleBase
1660}
1661
Jiyong Park30ca9372019-02-07 16:27:23 +09001662func defaultsFactory() android.Module {
1663 return DefaultsFactory()
1664}
1665
1666func DefaultsFactory(props ...interface{}) android.Module {
1667 module := &Defaults{}
1668
1669 module.AddProperties(props...)
1670 module.AddProperties(
1671 &apexBundleProperties{},
1672 &apexTargetBundleProperties{},
1673 )
1674
1675 android.InitDefaultsModule(module)
1676 return module
1677}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001678
1679//
1680// Prebuilt APEX
1681//
1682type Prebuilt struct {
1683 android.ModuleBase
1684 prebuilt android.Prebuilt
1685
1686 properties PrebuiltProperties
1687
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001688 inputApex android.Path
1689 installDir android.OutputPath
1690 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001691 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001692}
1693
1694type PrebuiltProperties struct {
1695 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001696 Source string `blueprint:"mutated"`
1697 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001698
1699 Src *string
1700 Arch struct {
1701 Arm struct {
1702 Src *string
1703 }
1704 Arm64 struct {
1705 Src *string
1706 }
1707 X86 struct {
1708 Src *string
1709 }
1710 X86_64 struct {
1711 Src *string
1712 }
1713 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001714
1715 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001716 // Optional name for the installed apex. If unspecified, name of the
1717 // module is used as the file name
1718 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001719
1720 // Names of modules to be overridden. Listed modules can only be other binaries
1721 // (in Make or Soong).
1722 // This does not completely prevent installation of the overridden binaries, but if both
1723 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1724 // from PRODUCT_PACKAGES.
1725 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001726}
1727
1728func (p *Prebuilt) installable() bool {
1729 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001730}
1731
1732func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001733 // If the device is configured to use flattened APEX, force disable the prebuilt because
1734 // the prebuilt is a non-flattened one.
1735 forceDisable := ctx.Config().FlattenApex()
1736
1737 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1738 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001739 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001740
Kun Niu10c9f832019-07-29 16:28:57 -07001741 // Force disable the prebuilts when coverage is enabled.
1742 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1743 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1744
Jiyong Park50b81e52019-07-11 11:24:41 +09001745 // b/137216042 don't use prebuilts when address sanitizer is on
1746 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1747 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1748
1749 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001750 p.properties.ForceDisable = true
1751 return
1752 }
1753
Jiyong Parkc95714e2019-03-29 14:23:10 +09001754 // This is called before prebuilt_select and prebuilt_postdeps mutators
1755 // The mutators requires that src to be set correctly for each arch so that
1756 // arch variants are disabled when src is not provided for the arch.
1757 if len(ctx.MultiTargets()) != 1 {
1758 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1759 return
1760 }
1761 var src string
1762 switch ctx.MultiTargets()[0].Arch.ArchType {
1763 case android.Arm:
1764 src = String(p.properties.Arch.Arm.Src)
1765 case android.Arm64:
1766 src = String(p.properties.Arch.Arm64.Src)
1767 case android.X86:
1768 src = String(p.properties.Arch.X86.Src)
1769 case android.X86_64:
1770 src = String(p.properties.Arch.X86_64.Src)
1771 default:
1772 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1773 return
1774 }
1775 if src == "" {
1776 src = String(p.properties.Src)
1777 }
1778 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001779}
1780
Jiyong Park03b68dd2019-07-26 23:20:40 +09001781func (p *Prebuilt) isForceDisabled() bool {
1782 return p.properties.ForceDisable
1783}
1784
Colin Cross41955e82019-05-29 14:40:35 -07001785func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1786 switch tag {
1787 case "":
1788 return android.Paths{p.outputApex}, nil
1789 default:
1790 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1791 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001792}
1793
Jiyong Park4d277042019-04-23 18:00:10 +09001794func (p *Prebuilt) InstallFilename() string {
1795 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1796}
1797
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001798func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001799 if p.properties.ForceDisable {
1800 return
1801 }
1802
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001803 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001804 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001805 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001806 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001807 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1808 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1809 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001810 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1811 ctx.Build(pctx, android.BuildParams{
1812 Rule: android.Cp,
1813 Input: p.inputApex,
1814 Output: p.outputApex,
1815 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001816 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001817 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001818 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001819}
1820
1821func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1822 return &p.prebuilt
1823}
1824
1825func (p *Prebuilt) Name() string {
1826 return p.prebuilt.Name(p.ModuleBase.Name())
1827}
1828
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001829func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1830 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001831 Class: "ETC",
1832 OutputFile: android.OptionalPathForPath(p.inputApex),
1833 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001834 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1835 func(entries *android.AndroidMkEntries) {
1836 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1837 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1838 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1839 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1840 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001841 },
1842 }
1843}
1844
1845// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1846func PrebuiltFactory() android.Module {
1847 module := &Prebuilt{}
1848 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001849 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001850 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001851 return module
1852}