blob: 950fb1e6ed61c2eaa22d868fc8b2d054e15fa8d3 [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 ` +
Jaewoong Jungb00c1fb2019-09-04 13:26:18 -0700101 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
102 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -0800103 CommandDeps: []string{"${zip2zip}"},
104 Description: "app bundle",
105 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100106
107 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
108 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
109 Rspfile: "${out}.emit_commands",
110 RspfileContent: "${emit_commands}",
111 Description: "Emit APEX image content",
112 }, "emit_commands")
113
114 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
115 Command: `diff --unchanged-group-format='' \` +
116 `--changed-group-format='%<' \` +
117 `${image_content_file} ${whitelisted_files_file} || (` +
118 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
119 ` "To fix the build run following command:" && ` +
120 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
121 `exit 1)`,
122 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
123 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900124)
125
Alex Light5098a612018-11-29 17:12:15 -0800126var imageApexSuffix = ".apex"
127var zipApexSuffix = ".zipapex"
128
129var imageApexType = "image"
130var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900131
132type dependencyTag struct {
133 blueprint.BaseDependencyTag
134 name string
135}
136
137var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900138 sharedLibTag = dependencyTag{name: "sharedLib"}
139 executableTag = dependencyTag{name: "executable"}
140 javaLibTag = dependencyTag{name: "javaLib"}
141 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100142 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900143 keyTag = dependencyTag{name: "key"}
144 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900145 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900146 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900147)
148
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900149var (
150 whitelistNoApex = map[string][]string{
151 "apex_test_build_features": []string{"libbinder"},
152 "com.android.neuralnetworks": []string{"libbinder"},
153 "com.android.media": []string{"libbinder"},
154 "com.android.media.swcodec": []string{"libbinder"},
155 "test_com.android.media.swcodec": []string{"libbinder"},
156 }
157)
158
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900159func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700160 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900161 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900162 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100163 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
164 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
165 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
166 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000167 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100168 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
169 } else {
170 return pctx.HostBinToolPath(ctx, tool).String()
171 }
172 })
173 }
174 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900175 pctx.HostBinToolVariable("avbtool", "avbtool")
176 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
177 pctx.HostBinToolVariable("merge_zips", "merge_zips")
178 pctx.HostBinToolVariable("mke2fs", "mke2fs")
179 pctx.HostBinToolVariable("resize2fs", "resize2fs")
180 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
181 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800182 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900183 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900184 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900185
Alex Light0851b882019-02-07 13:20:53 -0800186 android.RegisterModuleType("apex", apexBundleFactory)
187 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900188 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700189 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900190
191 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
192 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700193 ctx.BottomUp("apex", apexMutator).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}
Jooyung Han5c998b92019-06-27 11:30:33 +0900235func apexUsesMutator(mctx android.BottomUpMutatorContext) {
236 if ab, ok := mctx.Module().(*apexBundle); ok {
237 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
238 }
239}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900240
Alex Light9670d332019-01-29 18:07:33 -0800241type apexNativeDependencies struct {
242 // List of native libraries
243 Native_shared_libs []string
244 // List of native executables
245 Binaries []string
Roland Levillain630846d2019-06-26 12:48:34 +0100246 // List of native tests
247 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800248}
249type apexMultilibProperties struct {
250 // Native dependencies whose compile_multilib is "first"
251 First apexNativeDependencies
252
253 // Native dependencies whose compile_multilib is "both"
254 Both apexNativeDependencies
255
256 // Native dependencies whose compile_multilib is "prefer32"
257 Prefer32 apexNativeDependencies
258
259 // Native dependencies whose compile_multilib is "32"
260 Lib32 apexNativeDependencies
261
262 // Native dependencies whose compile_multilib is "64"
263 Lib64 apexNativeDependencies
264}
265
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900266type apexBundleProperties struct {
267 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000268 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800269 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900270
Jiyong Park40e26a22019-02-08 02:53:06 +0900271 // AndroidManifest.xml file used for the zip container of this APEX bundle.
272 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800273 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900274
Jiyong Park05e70dd2019-03-18 14:26:32 +0900275 // Canonical name of the APEX bundle in the manifest file.
276 // If unspecified, defaults to the value of name
277 Apex_name *string
278
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900279 // Determines the file contexts file for setting security context to each file in this APEX bundle.
280 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
281 // used.
282 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900283 File_contexts *string
284
285 // List of native shared libs that are embedded inside this APEX bundle
286 Native_shared_libs []string
287
Roland Levillain630846d2019-06-26 12:48:34 +0100288 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900289 Binaries []string
290
291 // List of java libraries that are embedded inside this APEX bundle
292 Java_libs []string
293
294 // List of prebuilt files that are embedded inside this APEX bundle
295 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900296
Roland Levillain630846d2019-06-26 12:48:34 +0100297 // List of tests that are embedded inside this APEX bundle
298 Tests []string
299
Jiyong Parkff1458f2018-10-12 21:49:38 +0900300 // Name of the apex_key module that provides the private key to sign APEX
301 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900302
Alex Light5098a612018-11-29 17:12:15 -0800303 // The type of APEX to build. Controls what the APEX payload is. Either
304 // 'image', 'zip' or 'both'. Default: 'image'.
305 Payload_type *string
306
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900307 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
308 // or an android_app_certificate module name in the form ":module".
309 Certificate *string
310
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900311 // Whether this APEX is installable to one of the partitions. Default: true.
312 Installable *bool
313
Jiyong Parkda6eb592018-12-19 17:12:36 +0900314 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
315 // Default is false.
316 Use_vendor *bool
317
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800318 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
319 Ignore_system_library_special_case *bool
320
Alex Light9670d332019-01-29 18:07:33 -0800321 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900322
Jiyong Parkf97782b2019-02-13 20:28:58 +0900323 // List of sanitizer names that this APEX is enabled for
324 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900325
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900326 PreventInstall bool `blueprint:"mutated"`
327
328 HideFromMake bool `blueprint:"mutated"`
329
Jooyung Han5c998b92019-06-27 11:30:33 +0900330 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
331 Provide_cpp_shared_libs *bool
332
333 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
334 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100335
336 // A txt file containing list of files that are whitelisted to be included in this APEX.
337 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900338
339 // List of APKs to package inside APEX
340 Apps []string
Alex Light9670d332019-01-29 18:07:33 -0800341}
342
343type apexTargetBundleProperties struct {
344 Target struct {
345 // Multilib properties only for android.
346 Android struct {
347 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900348 }
Alex Light9670d332019-01-29 18:07:33 -0800349 // Multilib properties only for host.
350 Host struct {
351 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900352 }
Alex Light9670d332019-01-29 18:07:33 -0800353 // Multilib properties only for host linux_bionic.
354 Linux_bionic struct {
355 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900356 }
Alex Light9670d332019-01-29 18:07:33 -0800357 // Multilib properties only for host linux_glibc.
358 Linux_glibc struct {
359 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900360 }
361 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900362}
363
Jiyong Park8fd61922018-11-08 02:50:25 +0900364type apexFileClass int
365
366const (
367 etc apexFileClass = iota
368 nativeSharedLib
369 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900370 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800371 pyBinary
372 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900373 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100374 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900375 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900376)
377
Alex Light5098a612018-11-29 17:12:15 -0800378type apexPackaging int
379
380const (
381 imageApex apexPackaging = iota
382 zipApex
383 both
384)
385
386func (a apexPackaging) image() bool {
387 switch a {
388 case imageApex, both:
389 return true
390 }
391 return false
392}
393
394func (a apexPackaging) zip() bool {
395 switch a {
396 case zipApex, both:
397 return true
398 }
399 return false
400}
401
402func (a apexPackaging) suffix() string {
403 switch a {
404 case imageApex:
405 return imageApexSuffix
406 case zipApex:
407 return zipApexSuffix
408 case both:
409 panic(fmt.Errorf("must be either zip or image"))
410 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100411 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800412 }
413}
414
415func (a apexPackaging) name() string {
416 switch a {
417 case imageApex:
418 return imageApexType
419 case zipApex:
420 return zipApexType
421 case both:
422 panic(fmt.Errorf("must be either zip or image"))
423 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100424 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800425 }
426}
427
Jiyong Park8fd61922018-11-08 02:50:25 +0900428func (class apexFileClass) NameInMake() string {
429 switch class {
430 case etc:
431 return "ETC"
432 case nativeSharedLib:
433 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800434 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900435 return "EXECUTABLES"
436 case javaSharedLib:
437 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100438 case nativeTest:
439 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900440 case app:
441 return "APPS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900442 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100443 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900444 }
445}
446
447type apexFile struct {
448 builtFile android.Path
449 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900450 installDir string
451 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900452 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800453 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900454}
455
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900456type apexBundle struct {
457 android.ModuleBase
458 android.DefaultableModuleBase
459
Alex Light9670d332019-01-29 18:07:33 -0800460 properties apexBundleProperties
461 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900462
Alex Light5098a612018-11-29 17:12:15 -0800463 apexTypes apexPackaging
464
Colin Crossa4925902018-11-16 11:36:28 -0800465 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800466 outputFiles map[apexPackaging]android.WritablePath
Roland Levillain935639d2019-08-13 14:55:28 +0100467 flattenedOutput android.OutputPath
Colin Crossa4925902018-11-16 11:36:28 -0800468 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900469
Jiyong Park03b68dd2019-07-26 23:20:40 +0900470 prebuiltFileToDelete string
471
Jiyong Park42cca6c2019-04-01 11:15:50 +0900472 public_key_file android.Path
473 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900474
475 container_certificate_file android.Path
476 container_private_key_file android.Path
477
Jiyong Park8fd61922018-11-08 02:50:25 +0900478 // list of files to be included in this apex
479 filesInfo []apexFile
480
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900481 // list of module names that this APEX is depending on
482 externalDeps []string
483
Jiyong Park8fd61922018-11-08 02:50:25 +0900484 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800485
486 testApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900487
488 // intermediate path for apex_manifest.json
489 manifestOut android.WritablePath
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900490}
491
Jiyong Park397e55e2018-10-24 21:09:55 +0900492func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100493 native_shared_libs []string, binaries []string, tests []string,
494 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900495 // Use *FarVariation* to be able to depend on modules having
496 // conflicting variations with this module. This is required since
497 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
498 // for native shared libs.
499 ctx.AddFarVariationDependencies([]blueprint.Variation{
500 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900501 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900502 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900503 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900504 }, sharedLibTag, native_shared_libs...)
505
506 ctx.AddFarVariationDependencies([]blueprint.Variation{
507 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900508 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900509 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100510
511 ctx.AddFarVariationDependencies([]blueprint.Variation{
512 {Mutator: "arch", Variation: arch},
513 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100514 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100515 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900516}
517
Alex Light9670d332019-01-29 18:07:33 -0800518func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
519 if ctx.Os().Class == android.Device {
520 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
521 } else {
522 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
523 if ctx.Os().Bionic() {
524 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
525 } else {
526 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
527 }
528 }
529}
530
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900531func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800532
Jiyong Park397e55e2018-10-24 21:09:55 +0900533 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900534 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800535
536 a.combineProperties(ctx)
537
Jiyong Park397e55e2018-10-24 21:09:55 +0900538 has32BitTarget := false
539 for _, target := range targets {
540 if target.Arch.ArchType.Multilib == "lib32" {
541 has32BitTarget = true
542 }
543 }
544 for i, target := range targets {
545 // When multilib.* is omitted for native_shared_libs, it implies
546 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900547 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900548 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900549 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900550 {Mutator: "link", Variation: "shared"},
551 }, sharedLibTag, a.properties.Native_shared_libs...)
552
Roland Levillain630846d2019-06-26 12:48:34 +0100553 // When multilib.* is omitted for tests, it implies
554 // multilib.both.
555 ctx.AddFarVariationDependencies([]blueprint.Variation{
556 {Mutator: "arch", Variation: target.String()},
557 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100558 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100559 }, testTag, a.properties.Tests...)
560
Jiyong Park397e55e2018-10-24 21:09:55 +0900561 // Add native modules targetting both ABIs
562 addDependenciesForNativeModules(ctx,
563 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100564 a.properties.Multilib.Both.Binaries,
565 a.properties.Multilib.Both.Tests,
566 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900567 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900568
Alex Light3d673592019-01-18 14:37:31 -0800569 isPrimaryAbi := i == 0
570 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900571 // When multilib.* is omitted for binaries, it implies
572 // multilib.first.
573 ctx.AddFarVariationDependencies([]blueprint.Variation{
574 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900575 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900576 }, executableTag, a.properties.Binaries...)
577
578 // Add native modules targetting the first ABI
579 addDependenciesForNativeModules(ctx,
580 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100581 a.properties.Multilib.First.Binaries,
582 a.properties.Multilib.First.Tests,
583 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900584 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800585
586 // When multilib.* is omitted for prebuilts, it implies multilib.first.
587 ctx.AddFarVariationDependencies([]blueprint.Variation{
588 {Mutator: "arch", Variation: target.String()},
589 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900590 }
591
592 switch target.Arch.ArchType.Multilib {
593 case "lib32":
594 // Add native modules targetting 32-bit ABI
595 addDependenciesForNativeModules(ctx,
596 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100597 a.properties.Multilib.Lib32.Binaries,
598 a.properties.Multilib.Lib32.Tests,
599 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900600 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900601
602 addDependenciesForNativeModules(ctx,
603 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100604 a.properties.Multilib.Prefer32.Binaries,
605 a.properties.Multilib.Prefer32.Tests,
606 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900607 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900608 case "lib64":
609 // Add native modules targetting 64-bit ABI
610 addDependenciesForNativeModules(ctx,
611 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100612 a.properties.Multilib.Lib64.Binaries,
613 a.properties.Multilib.Lib64.Tests,
614 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900615 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900616
617 if !has32BitTarget {
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 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700625
626 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
627 for _, sanitizer := range ctx.Config().SanitizeDevice() {
628 if sanitizer == "hwaddress" {
629 addDependenciesForNativeModules(ctx,
630 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100631 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700632 break
633 }
634 }
635 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900636 }
637
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900638 }
639
Jiyong Parkff1458f2018-10-12 21:49:38 +0900640 ctx.AddFarVariationDependencies([]blueprint.Variation{
641 {Mutator: "arch", Variation: "android_common"},
642 }, javaLibTag, a.properties.Java_libs...)
643
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900644 ctx.AddFarVariationDependencies([]blueprint.Variation{
645 {Mutator: "arch", Variation: "android_common"},
646 }, androidAppTag, a.properties.Apps...)
647
Jiyong Park23c52b02019-02-02 13:13:47 +0900648 if String(a.properties.Key) == "" {
649 ctx.ModuleErrorf("key is missing")
650 return
651 }
652 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900653
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900654 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900655 if cert != "" {
656 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900657 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900658}
659
Colin Cross0ea8ba82019-06-06 14:33:29 -0700660func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900661 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
662 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000663 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900664 }
665 return String(a.properties.Certificate)
666}
667
Colin Cross41955e82019-05-29 14:40:35 -0700668func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
669 switch tag {
670 case "":
671 if file, ok := a.outputFiles[imageApex]; ok {
672 return android.Paths{file}, nil
673 } else {
674 return nil, nil
675 }
Roland Levillain935639d2019-08-13 14:55:28 +0100676 case ".flattened":
677 if a.flattened {
678 flattenedApexPath := a.flattenedOutput
679 return android.Paths{flattenedApexPath}, nil
680 } else {
681 return nil, nil
682 }
Colin Cross41955e82019-05-29 14:40:35 -0700683 default:
684 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900685 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900686}
687
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900688func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900689 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900690}
691
Jiyong Park7c1dc612019-01-05 11:15:24 +0900692func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
693 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900694 return "vendor"
695 } else {
696 return "core"
697 }
698}
699
Jiyong Parkf97782b2019-02-13 20:28:58 +0900700func (a *apexBundle) EnableSanitizer(sanitizerName string) {
701 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
702 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
703 }
704}
705
Jiyong Park388ef3f2019-01-28 19:47:32 +0900706func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900707 if android.InList(sanitizerName, a.properties.SanitizerNames) {
708 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900709 }
710
711 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900712 globalSanitizerNames := []string{}
713 if a.Host() {
714 globalSanitizerNames = ctx.Config().SanitizeHost()
715 } else {
716 arches := ctx.Config().SanitizeDeviceArch()
717 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
718 globalSanitizerNames = ctx.Config().SanitizeDevice()
719 }
720 }
721 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900722}
723
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900724func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
725 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
726}
727
728func (a *apexBundle) PreventInstall() {
729 a.properties.PreventInstall = true
730}
731
732func (a *apexBundle) HideFromMake() {
733 a.properties.HideFromMake = true
734}
735
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800736func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900737 // Decide the APEX-local directory by the multilib of the library
738 // In the future, we may query this to the module.
739 switch cc.Arch().ArchType.Multilib {
740 case "lib32":
741 dirInApex = "lib"
742 case "lib64":
743 dirInApex = "lib64"
744 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900745 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200746 if !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900747 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200748 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
749 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900750 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800751 if handleSpecialLibs {
752 switch cc.Name() {
753 case "libc", "libm", "libdl":
754 // Special case for bionic libs. This is to prevent the bionic libs
755 // from being included in the search path /apex/com.android.apex/lib.
756 // This exclusion is required because bionic libs in the runtime APEX
757 // are available via the legacy paths /system/lib/libc.so, etc. By the
758 // init process, the bionic libs in the APEX are bind-mounted to the
759 // legacy paths and thus will be loaded into the default linker namespace.
760 // If the bionic libs are directly in /apex/com.android.apex/lib then
761 // the same libs will be again loaded to the runtime linker namespace,
762 // which will result double loading of bionic libs that isn't supported.
763 dirInApex = filepath.Join(dirInApex, "bionic")
764 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900765 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900766
767 fileToCopy = cc.OutputFile().Path()
768 return
769}
770
771func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900772 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200773 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900774 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200775 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
776 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900777 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900778 fileToCopy = cc.OutputFile().Path()
779 return
780}
781
Alex Light778127a2019-02-27 14:19:50 -0800782func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
783 dirInApex = "bin"
784 fileToCopy = py.HostToolPath().Path()
785 return
786}
787func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
788 dirInApex = "bin"
789 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
790 if err != nil {
791 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
792 return
793 }
794 fileToCopy = android.PathForOutput(ctx, s)
795 return
796}
797
Jiyong Park04480cf2019-02-06 00:16:29 +0900798func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
799 dirInApex = filepath.Join("bin", sh.SubDir())
800 fileToCopy = sh.OutputFile()
801 return
802}
803
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900804func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
805 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900806 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900807 return
808}
809
Jiyong Park9e6c2422019-08-09 20:39:45 +0900810func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
811 dirInApex = "javalib"
812 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
813 implJars := java.ImplementationJars()
814 if len(implJars) != 1 {
815 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
816 strings.Join(implJars.Strings(), ", ")))
817 }
818 fileToCopy = implJars[0]
819 return
820}
821
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900822func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
823 dirInApex = filepath.Join("etc", prebuilt.SubDir())
824 fileToCopy = prebuilt.OutputFile()
825 return
826}
827
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900828func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
829 dirInApex = filepath.Join("app", pkgName)
830 fileToCopy = app.OutputFile()
831 return
832}
833
Roland Levillain935639d2019-08-13 14:55:28 +0100834// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
835type flattenedApexContext struct {
836 android.ModuleContext
837}
838
839func (c *flattenedApexContext) InstallBypassMake() bool {
840 return true
841}
842
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900843func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900844 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900845
Alex Light5098a612018-11-29 17:12:15 -0800846 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
847 a.apexTypes = imageApex
848 } else if *a.properties.Payload_type == "zip" {
849 a.apexTypes = zipApex
850 } else if *a.properties.Payload_type == "both" {
851 a.apexTypes = both
852 } else {
853 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
854 return
855 }
856
Roland Levillain630846d2019-06-26 12:48:34 +0100857 if len(a.properties.Tests) > 0 && !a.testApex {
858 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
859 return
860 }
861
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800862 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
863
Jooyung Hane1633032019-08-01 17:41:43 +0900864 // native lib dependencies
865 var provideNativeLibs []string
866 var requireNativeLibs []string
867
Jooyung Han5c998b92019-06-27 11:30:33 +0900868 // Check if "uses" requirements are met with dependent apexBundles
869 var providedNativeSharedLibs []string
870 useVendor := proptools.Bool(a.properties.Use_vendor)
871 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
872 if ctx.OtherModuleDependencyTag(m) != usesTag {
873 return
874 }
875 otherName := ctx.OtherModuleName(m)
876 other, ok := m.(*apexBundle)
877 if !ok {
878 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
879 return
880 }
881 if proptools.Bool(other.properties.Use_vendor) != useVendor {
882 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
883 return
884 }
885 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
886 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
887 return
888 }
889 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
890 })
891
Alex Light778127a2019-02-27 14:19:50 -0800892 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100893 depTag := ctx.OtherModuleDependencyTag(child)
894 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900895 if _, ok := parent.(*apexBundle); ok {
896 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900897 switch depTag {
898 case sharedLibTag:
899 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900900 if cc.HasStubsVariants() {
901 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
902 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800903 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900904 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900905 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900906 } else {
907 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900908 }
909 case executableTag:
910 if cc, ok := child.(*cc.Module); ok {
911 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900912 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900913 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900914 } else if sh, ok := child.(*android.ShBinary); ok {
915 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
916 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800917 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
918 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
919 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
920 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
921 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
922 // NB: Since go binaries are static we don't need the module for anything here, which is
923 // good since the go tool is a blueprint.Module not an android.Module like we would
924 // normally use.
925 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900926 } else {
Alex Light778127a2019-02-27 14:19:50 -0800927 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 +0900928 }
929 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +0900930 if javaLib, ok := child.(*java.Library); ok {
931 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +0900932 if fileToCopy == nil {
933 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
934 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900935 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
936 }
937 return true
938 } else if javaLib, ok := child.(*java.Import); ok {
939 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
940 if fileToCopy == nil {
941 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
942 } else {
943 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900944 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900945 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900946 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900947 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900948 }
949 case prebuiltTag:
950 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
951 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900952 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900953 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900954 } else {
955 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
956 }
Roland Levillain630846d2019-06-26 12:48:34 +0100957 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100958 if ccTest, ok := child.(*cc.Module); ok {
959 if ccTest.IsTestPerSrcAllTestsVariation() {
960 // Multiple-output test module (where `test_per_src: true`).
961 //
962 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
963 // We do not add this variation to `filesInfo`, as it has no output;
964 // however, we do add the other variations of this module as indirect
965 // dependencies (see below).
966 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100967 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100968 // Single-output test module (where `test_per_src: false`).
969 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
970 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100971 }
Roland Levillain630846d2019-06-26 12:48:34 +0100972 return true
973 } else {
974 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
975 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900976 case keyTag:
977 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900978 a.private_key_file = key.private_key_file
979 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900980 return false
981 } else {
982 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900983 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900984 case certificateTag:
985 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900986 a.container_certificate_file = dep.Certificate.Pem
987 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900988 return false
989 } else {
990 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
991 }
Jiyong Park03b68dd2019-07-26 23:20:40 +0900992 case android.PrebuiltDepTag:
993 // If the prebuilt is force disabled, remember to delete the prebuilt file
994 // that might have been installed in the previous builds
995 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
996 a.prebuiltFileToDelete = prebuilt.InstallFilename()
997 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900998 case androidAppTag:
999 if ap, ok := child.(*java.AndroidApp); ok {
1000 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1001 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1002 return true
1003 } else {
1004 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1005 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001006 }
1007 } else {
1008 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001009 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001010 // We cannot use a switch statement on `depTag` here as the checked
1011 // tags used below are private (e.g. `cc.sharedDepTag`).
1012 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1013 if cc, ok := child.(*cc.Module); ok {
1014 if android.InList(cc.Name(), providedNativeSharedLibs) {
1015 // If we're using a shared library which is provided from other APEX,
1016 // don't include it in this APEX
1017 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001018 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001019 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1020 // If the dependency is a stubs lib, don't include it in this APEX,
1021 // but make sure that the lib is installed on the device.
1022 // In case no APEX is having the lib, the lib is installed to the system
1023 // partition.
1024 //
1025 // Always include if we are a host-apex however since those won't have any
1026 // system libraries.
1027 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1028 a.externalDeps = append(a.externalDeps, cc.Name())
1029 }
Jooyung Hane1633032019-08-01 17:41:43 +09001030 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001031 // Don't track further
1032 return false
1033 }
1034 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
1035 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1036 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001037 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001038 } else if cc.IsTestPerSrcDepTag(depTag) {
1039 if cc, ok := child.(*cc.Module); ok {
1040 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1041 // Handle modules created as `test_per_src` variations of a single test module:
1042 // use the name of the generated test binary (`fileToCopy`) instead of the name
1043 // of the original test module (`depName`, shared by all `test_per_src`
1044 // variations of that module).
1045 moduleName := filepath.Base(fileToCopy.String())
1046 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1047 return true
1048 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001049 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001050 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Sundong Ahn2db7f462019-08-27 18:53:12 +09001051 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
1052 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001053 }
1054 }
1055 }
1056 return false
1057 })
1058
Jiyong Park9335a262018-12-24 11:31:58 +09001059 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001060 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001061 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1062 return
1063 }
1064
Jiyong Park8fd61922018-11-08 02:50:25 +09001065 // remove duplicates in filesInfo
1066 removeDup := func(filesInfo []apexFile) []apexFile {
1067 encountered := make(map[android.Path]bool)
1068 result := []apexFile{}
1069 for _, f := range filesInfo {
1070 if !encountered[f.builtFile] {
1071 encountered[f.builtFile] = true
1072 result = append(result, f)
1073 }
1074 }
1075 return result
1076 }
1077 filesInfo = removeDup(filesInfo)
1078
1079 // to have consistent build rules
1080 sort.Slice(filesInfo, func(i, j int) bool {
1081 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1082 })
1083
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001084 // check no_apex modules
1085 whitelist := whitelistNoApex[ctx.ModuleName()]
1086 for i := range filesInfo {
1087 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1088 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1089 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1090 }
1091 }
1092 }
1093
Jiyong Park8fd61922018-11-08 02:50:25 +09001094 // prepend the name of this APEX to the module names. These names will be the names of
1095 // modules that will be defined if the APEX is flattened.
1096 for i := range filesInfo {
1097 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1098 }
1099
Jiyong Park8fd61922018-11-08 02:50:25 +09001100 a.installDir = android.PathForModuleInstall(ctx, "apex")
1101 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001102
Jooyung Hane1633032019-08-01 17:41:43 +09001103 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1104 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1105 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1106 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1107 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1108 ctx.Build(pctx, android.BuildParams{
1109 Rule: injectApexDependency,
1110 Input: manifestSrc,
1111 Output: a.manifestOut,
1112 Args: map[string]string{
1113 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1114 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1115 },
1116 })
1117
Roland Levillain935639d2019-08-13 14:55:28 +01001118 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1119 // reply true to `InstallBypassMake()` (thus making the call
1120 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1121 // instead of `android.PathForOutput`) to return the correct path to the flattened
1122 // APEX (as its contents is installed by Make, not Soong).
1123 factx := flattenedApexContext{ctx}
1124 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1125
Alex Light5098a612018-11-29 17:12:15 -08001126 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001127 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001128 }
1129 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001130 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001131 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001132 // in other modules. It is in AndroidMk where the selection of flattened
1133 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001134 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001135 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001136 }
1137}
1138
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001139func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001140 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001141 for _, f := range a.filesInfo {
1142 if f.module != nil {
1143 notice := f.module.NoticeFile()
1144 if notice.Valid() {
1145 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001146 }
1147 }
1148 }
1149 // append the notice file specified in the apex module itself
1150 if a.NoticeFile().Valid() {
1151 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001152 }
1153
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001154 if len(noticeFiles) == 0 {
1155 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001156 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001157
Jaewoong Jung98772792019-07-01 17:15:13 -07001158 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001159}
1160
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001161func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001162 cert := String(a.properties.Certificate)
1163 if cert != "" && android.SrcIsModule(cert) == "" {
1164 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001165 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1166 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001167 } else if cert == "" {
1168 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001169 a.container_certificate_file = pem
1170 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001171 }
1172
Alex Light5098a612018-11-29 17:12:15 -08001173 var abis []string
1174 for _, target := range ctx.MultiTargets() {
1175 if len(target.Arch.Abi) > 0 {
1176 abis = append(abis, target.Arch.Abi[0])
1177 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001178 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001179
Alex Light5098a612018-11-29 17:12:15 -08001180 abis = android.FirstUniqueStrings(abis)
1181
1182 suffix := apexType.suffix()
1183 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001184
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001185 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001186 for _, f := range a.filesInfo {
1187 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001188 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001189
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001190 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001191 emitCommands := []string{}
1192 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1193 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001194 for i, src := range filesToCopy {
1195 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001196 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001197 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001198 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1199 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001200 for _, sym := range a.filesInfo[i].symlinks {
1201 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1202 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1203 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001204 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001205 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001206 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001207
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001208 if a.properties.Whitelisted_files != nil {
1209 ctx.Build(pctx, android.BuildParams{
1210 Rule: emitApexContentRule,
1211 Implicits: implicitInputs,
1212 Output: imageContentFile,
1213 Description: "emit apex image content",
1214 Args: map[string]string{
1215 "emit_commands": strings.Join(emitCommands, " && "),
1216 },
1217 })
1218 implicitInputs = append(implicitInputs, imageContentFile)
1219 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1220
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001221 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001222 ctx.Build(pctx, android.BuildParams{
1223 Rule: diffApexContentRule,
1224 Implicits: implicitInputs,
1225 Output: phonyOutput,
1226 Description: "diff apex image content",
1227 Args: map[string]string{
1228 "whitelisted_files_file": whitelistedFilesFile.String(),
1229 "image_content_file": imageContentFile.String(),
1230 "apex_module_name": ctx.ModuleName(),
1231 },
1232 })
1233
1234 implicitInputs = append(implicitInputs, phonyOutput)
1235 }
1236
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001237 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1238 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001239
Alex Light5098a612018-11-29 17:12:15 -08001240 if apexType.image() {
1241 // files and dirs that will be created in APEX
1242 var readOnlyPaths []string
1243 var executablePaths []string // this also includes dirs
1244 for _, f := range a.filesInfo {
1245 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001246 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001247 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001248 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001249 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001250 }
Alex Light5098a612018-11-29 17:12:15 -08001251 } else {
1252 readOnlyPaths = append(readOnlyPaths, pathInApex)
1253 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001254 dir := f.installDir
1255 for !android.InList(dir, executablePaths) && dir != "" {
1256 executablePaths = append(executablePaths, dir)
1257 dir, _ = filepath.Split(dir) // move up to the parent
1258 if len(dir) > 0 {
1259 // remove trailing slash
1260 dir = dir[:len(dir)-1]
1261 }
Alex Light5098a612018-11-29 17:12:15 -08001262 }
1263 }
1264 sort.Strings(readOnlyPaths)
1265 sort.Strings(executablePaths)
1266 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1267 ctx.Build(pctx, android.BuildParams{
1268 Rule: generateFsConfig,
1269 Output: cannedFsConfig,
1270 Description: "generate fs config",
1271 Args: map[string]string{
1272 "ro_paths": strings.Join(readOnlyPaths, " "),
1273 "exec_paths": strings.Join(executablePaths, " "),
1274 },
1275 })
1276
1277 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1278 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1279 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1280 if !fileContextsOptionalPath.Valid() {
1281 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1282 return
1283 }
1284 fileContexts := fileContextsOptionalPath.Path()
1285
Jiyong Park835d82b2018-12-27 16:04:18 +09001286 optFlags := []string{}
1287
Alex Light5098a612018-11-29 17:12:15 -08001288 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001289 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1290 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001291
Jiyong Park7f67f482019-01-05 12:57:48 +09001292 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1293 if overridden {
1294 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1295 }
1296
Jiyong Park40e26a22019-02-08 02:53:06 +09001297 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001298 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001299 implicitInputs = append(implicitInputs, androidManifestFile)
1300 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1301 }
1302
Jiyong Park71b519d2019-04-18 17:25:49 +09001303 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1304 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1305 ctx.Config().UnbundledBuild() &&
1306 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1307 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1308 apiFingerprint := java.ApiFingerprintPath(ctx)
1309 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1310 implicitInputs = append(implicitInputs, apiFingerprint)
1311 }
1312 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1313
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001314 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1315 if noticeFile.Valid() {
1316 // If there's a NOTICE file, embed it as an asset file in the APEX.
1317 implicitInputs = append(implicitInputs, noticeFile.Path())
1318 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1319 }
1320
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001321 if !ctx.Config().UnbundledBuild() && a.installable() {
1322 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1323 // don't need hashtree for activation. Therefore, by removing hashtree from
1324 // apex bundle (filesystem image in it, to be specific), we can save storage.
1325 optFlags = append(optFlags, "--no_hashtree")
1326 }
1327
Alex Light5098a612018-11-29 17:12:15 -08001328 ctx.Build(pctx, android.BuildParams{
1329 Rule: apexRule,
1330 Implicits: implicitInputs,
1331 Output: unsignedOutputFile,
1332 Description: "apex (" + apexType.name() + ")",
1333 Args: map[string]string{
1334 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1335 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1336 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001337 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001338 "file_contexts": fileContexts.String(),
1339 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001340 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001341 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001342 },
1343 })
1344
1345 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1346 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1347 a.bundleModuleFile = bundleModuleFile
1348
1349 ctx.Build(pctx, android.BuildParams{
1350 Rule: apexProtoConvertRule,
1351 Input: unsignedOutputFile,
1352 Output: apexProtoFile,
1353 Description: "apex proto convert",
1354 })
1355
1356 ctx.Build(pctx, android.BuildParams{
1357 Rule: apexBundleRule,
1358 Input: apexProtoFile,
1359 Output: a.bundleModuleFile,
1360 Description: "apex bundle module",
1361 Args: map[string]string{
1362 "abi": strings.Join(abis, "."),
1363 },
1364 })
1365 } else {
1366 ctx.Build(pctx, android.BuildParams{
1367 Rule: zipApexRule,
1368 Implicits: implicitInputs,
1369 Output: unsignedOutputFile,
1370 Description: "apex (" + apexType.name() + ")",
1371 Args: map[string]string{
1372 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1373 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1374 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001375 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001376 },
1377 })
Colin Crossa4925902018-11-16 11:36:28 -08001378 }
Colin Crossa4925902018-11-16 11:36:28 -08001379
Alex Light5098a612018-11-29 17:12:15 -08001380 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001381 ctx.Build(pctx, android.BuildParams{
1382 Rule: java.Signapk,
1383 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001384 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001385 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001386 Implicits: []android.Path{
1387 a.container_certificate_file,
1388 a.container_private_key_file,
1389 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001390 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001391 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001392 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001393 },
1394 })
Alex Light5098a612018-11-29 17:12:15 -08001395
1396 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001397 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001398 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001399 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001400}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001401
Jiyong Park8fd61922018-11-08 02:50:25 +09001402func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001403 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001404 // 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 +09001405 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001406 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001407
Jiyong Park42cca6c2019-04-01 11:15:50 +09001408 // rename to apex_pubkey
1409 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1410 ctx.Build(pctx, android.BuildParams{
1411 Rule: android.Cp,
1412 Input: a.public_key_file,
1413 Output: copiedPubkey,
1414 })
1415 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1416
Jiyong Park23c52b02019-02-02 13:13:47 +09001417 if ctx.Config().FlattenApex() {
1418 for _, fi := range a.filesInfo {
1419 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001420 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1421 for _, sym := range fi.symlinks {
1422 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1423 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001424 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001425 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001426 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001427}
1428
1429func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001430 if a.properties.HideFromMake {
1431 return android.AndroidMkData{
1432 Disabled: true,
1433 }
1434 }
Alex Light5098a612018-11-29 17:12:15 -08001435 writers := []android.AndroidMkData{}
1436 if a.apexTypes.image() {
1437 writers = append(writers, a.androidMkForType(imageApex))
1438 }
1439 if a.apexTypes.zip() {
1440 writers = append(writers, a.androidMkForType(zipApex))
1441 }
1442 return android.AndroidMkData{
1443 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1444 for _, data := range writers {
1445 data.Custom(w, name, prefix, moduleDir, data)
1446 }
1447 }}
1448}
1449
Alex Lightf1801bc2019-02-13 11:10:07 -08001450func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001451 moduleNames := []string{}
1452
1453 for _, fi := range a.filesInfo {
1454 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1455 continue
1456 }
1457 if !android.InList(fi.moduleName, moduleNames) {
1458 moduleNames = append(moduleNames, fi.moduleName)
1459 }
1460 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1461 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1462 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001463 // /apex/<name>/{lib|framework|...}
1464 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1465 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001466 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001467 // /system/apex/<name>/{lib|framework|...}
1468 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1469 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001470 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001471 if len(fi.symlinks) > 0 {
1472 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1473 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001474
1475 if fi.module != nil && fi.module.NoticeFile().Valid() {
1476 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1477 }
Jiyong Park94427262019-02-05 23:18:47 +09001478 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001479 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001480 }
1481 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1482 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1483 if fi.module != nil {
1484 archStr := fi.module.Target().Arch.ArchType.String()
1485 host := false
1486 switch fi.module.Target().Os.Class {
1487 case android.Host:
1488 if archStr != "common" {
1489 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1490 }
1491 host = true
1492 case android.HostCross:
1493 if archStr != "common" {
1494 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1495 }
1496 host = true
1497 case android.Device:
1498 if archStr != "common" {
1499 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1500 }
1501 }
1502 if host {
1503 makeOs := fi.module.Target().Os.String()
1504 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1505 makeOs = "linux"
1506 }
1507 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1508 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1509 }
1510 }
1511 if fi.class == javaSharedLib {
1512 javaModule := fi.module.(*java.Library)
1513 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1514 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1515 // we will have foo.jar.jar
1516 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1517 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1518 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1519 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1520 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1521 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1522 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1523 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001524 if cc, ok := fi.module.(*cc.Module); ok {
1525 if cc.UnstrippedOutputFile() != nil {
1526 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1527 }
1528 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001529 if cc.CoverageOutputFile().Valid() {
1530 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1531 }
Jiyong Park94427262019-02-05 23:18:47 +09001532 }
1533 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1534 } else {
1535 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1536 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1537 }
1538 }
1539 return moduleNames
1540}
1541
Alex Light5098a612018-11-29 17:12:15 -08001542func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001543 return android.AndroidMkData{
1544 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1545 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001546 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001547 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001548 }
1549
Jiyong Park719b4462019-01-13 00:39:51 +09001550 if a.flattened && apexType.image() {
1551 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001552 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1553 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1554 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001555 if len(moduleNames) > 0 {
1556 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1557 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001558 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001559 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1560
Jiyong Park719b4462019-01-13 00:39:51 +09001561 } else {
Alex Light5098a612018-11-29 17:12:15 -08001562 // zip-apex is the less common type so have the name refer to the image-apex
1563 // only and use {name}.zip if you want the zip-apex
1564 if apexType == zipApex && a.apexTypes == both {
1565 name = name + ".zip"
1566 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001567 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1568 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1569 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1570 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001571 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001572 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001573 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001574 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001575 if len(moduleNames) > 0 {
1576 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1577 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001578 if len(a.externalDeps) > 0 {
1579 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1580 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001581 if a.prebuiltFileToDelete != "" {
1582 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1583 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1584 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001585 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001586
Alex Light5098a612018-11-29 17:12:15 -08001587 if apexType == imageApex {
1588 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1589 }
Jiyong Park719b4462019-01-13 00:39:51 +09001590 }
1591 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001592}
1593
Alex Light0851b882019-02-07 13:20:53 -08001594func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001595 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001596}
1597
1598func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001599 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001600}
1601
1602func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001603 module := &apexBundle{
1604 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001605 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001606 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001607 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001608 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001609 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001610 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1611 })
Alex Light5098a612018-11-29 17:12:15 -08001612 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001613 android.InitDefaultableModule(module)
1614 return module
1615}
Jiyong Park30ca9372019-02-07 16:27:23 +09001616
1617//
1618// Defaults
1619//
1620type Defaults struct {
1621 android.ModuleBase
1622 android.DefaultsModuleBase
1623}
1624
Jiyong Park30ca9372019-02-07 16:27:23 +09001625func defaultsFactory() android.Module {
1626 return DefaultsFactory()
1627}
1628
1629func DefaultsFactory(props ...interface{}) android.Module {
1630 module := &Defaults{}
1631
1632 module.AddProperties(props...)
1633 module.AddProperties(
1634 &apexBundleProperties{},
1635 &apexTargetBundleProperties{},
1636 )
1637
1638 android.InitDefaultsModule(module)
1639 return module
1640}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001641
1642//
1643// Prebuilt APEX
1644//
1645type Prebuilt struct {
1646 android.ModuleBase
1647 prebuilt android.Prebuilt
1648
1649 properties PrebuiltProperties
1650
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001651 inputApex android.Path
1652 installDir android.OutputPath
1653 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001654 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001655}
1656
1657type PrebuiltProperties struct {
1658 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001659 Source string `blueprint:"mutated"`
1660 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001661
1662 Src *string
1663 Arch struct {
1664 Arm struct {
1665 Src *string
1666 }
1667 Arm64 struct {
1668 Src *string
1669 }
1670 X86 struct {
1671 Src *string
1672 }
1673 X86_64 struct {
1674 Src *string
1675 }
1676 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001677
1678 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001679 // Optional name for the installed apex. If unspecified, name of the
1680 // module is used as the file name
1681 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001682
1683 // Names of modules to be overridden. Listed modules can only be other binaries
1684 // (in Make or Soong).
1685 // This does not completely prevent installation of the overridden binaries, but if both
1686 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1687 // from PRODUCT_PACKAGES.
1688 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001689}
1690
1691func (p *Prebuilt) installable() bool {
1692 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001693}
1694
1695func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001696 // If the device is configured to use flattened APEX, force disable the prebuilt because
1697 // the prebuilt is a non-flattened one.
1698 forceDisable := ctx.Config().FlattenApex()
1699
1700 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1701 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001702 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001703
Kun Niu10c9f832019-07-29 16:28:57 -07001704 // Force disable the prebuilts when coverage is enabled.
1705 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1706 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1707
Jiyong Park50b81e52019-07-11 11:24:41 +09001708 // b/137216042 don't use prebuilts when address sanitizer is on
1709 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1710 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1711
1712 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001713 p.properties.ForceDisable = true
1714 return
1715 }
1716
Jiyong Parkc95714e2019-03-29 14:23:10 +09001717 // This is called before prebuilt_select and prebuilt_postdeps mutators
1718 // The mutators requires that src to be set correctly for each arch so that
1719 // arch variants are disabled when src is not provided for the arch.
1720 if len(ctx.MultiTargets()) != 1 {
1721 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1722 return
1723 }
1724 var src string
1725 switch ctx.MultiTargets()[0].Arch.ArchType {
1726 case android.Arm:
1727 src = String(p.properties.Arch.Arm.Src)
1728 case android.Arm64:
1729 src = String(p.properties.Arch.Arm64.Src)
1730 case android.X86:
1731 src = String(p.properties.Arch.X86.Src)
1732 case android.X86_64:
1733 src = String(p.properties.Arch.X86_64.Src)
1734 default:
1735 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1736 return
1737 }
1738 if src == "" {
1739 src = String(p.properties.Src)
1740 }
1741 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001742}
1743
Jiyong Park03b68dd2019-07-26 23:20:40 +09001744func (p *Prebuilt) isForceDisabled() bool {
1745 return p.properties.ForceDisable
1746}
1747
Colin Cross41955e82019-05-29 14:40:35 -07001748func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1749 switch tag {
1750 case "":
1751 return android.Paths{p.outputApex}, nil
1752 default:
1753 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1754 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001755}
1756
Jiyong Park4d277042019-04-23 18:00:10 +09001757func (p *Prebuilt) InstallFilename() string {
1758 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1759}
1760
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001761func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001762 if p.properties.ForceDisable {
1763 return
1764 }
1765
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001766 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001767 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001768 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001769 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001770 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1771 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1772 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001773 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1774 ctx.Build(pctx, android.BuildParams{
1775 Rule: android.Cp,
1776 Input: p.inputApex,
1777 Output: p.outputApex,
1778 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001779 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001780 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001781 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001782}
1783
1784func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1785 return &p.prebuilt
1786}
1787
1788func (p *Prebuilt) Name() string {
1789 return p.prebuilt.Name(p.ModuleBase.Name())
1790}
1791
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001792func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1793 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001794 Class: "ETC",
1795 OutputFile: android.OptionalPathForPath(p.inputApex),
1796 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001797 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1798 func(entries *android.AndroidMkEntries) {
1799 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1800 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1801 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1802 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1803 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001804 },
1805 }
1806}
1807
1808// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1809func PrebuiltFactory() android.Module {
1810 module := &Prebuilt{}
1811 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001812 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001813 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001814 return module
1815}