blob: 9c9d6aeac002dffc3880b2eed5942f82da98cbf0 [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
24
25 "android/soong/android"
26 "android/soong/cc"
27 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080028 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090029
30 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080031 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090032 "github.com/google/blueprint/proptools"
33)
34
35var (
36 pctx = android.NewPackageContext("android/apex")
37
38 // Create a canned fs config file where all files and directories are
39 // by default set to (uid/gid/mode) = (1000/1000/0644)
40 // TODO(b/113082813) make this configurable using config.fs syntax
41 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000042 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000043 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090044 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090045 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090047 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090048
Jooyung Hane1633032019-08-01 17:41:43 +090049 injectApexDependency = pctx.StaticRule("injectApexDependency", blueprint.RuleParams{
50 Command: `rm -f $out && ${jsonmodify} $in ` +
51 `-a provideNativeLibs ${provideNativeLibs} ` +
52 `-a requireNativeLibs ${requireNativeLibs} -o $out`,
53 CommandDeps: []string{"${jsonmodify}"},
54 Description: "Inject dependency into ${out}",
55 }, "provideNativeLibs", "requireNativeLibs")
56
Jiyong Park48ca7dc2018-10-10 14:01:00 +090057 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
58 // against the binary policy using sefcontext_compiler -p <policy>.
59
60 // TODO(b/114327326): automate the generation of file_contexts
61 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
62 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010063 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090064 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090065 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090066 `--file_contexts ${file_contexts} ` +
67 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080068 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090069 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090070 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
71 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000072 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010073 Rspfile: "${out}.copy_commands",
74 RspfileContent: "${copy_commands}",
75 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090076 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080077
Alex Light5098a612018-11-29 17:12:15 -080078 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
79 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010080 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080081 `APEXER_TOOL_PATH=${tool_path} ` +
82 `${apexer} --force --manifest ${manifest} ` +
83 `--payload_type zip ` +
84 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010085 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
86 Rspfile: "${out}.copy_commands",
87 RspfileContent: "${copy_commands}",
88 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080089 }, "tool_path", "image_dir", "copy_commands", "manifest")
90
Colin Crossa4925902018-11-16 11:36:28 -080091 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
92 blueprint.RuleParams{
93 Command: `${aapt2} convert --output-format proto $in -o $out`,
94 CommandDeps: []string{"${aapt2}"},
95 })
96
97 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090098 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000099 `apex_payload.img:apex/${abi}.img ` +
100 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +0000101 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -0800102 CommandDeps: []string{"${zip2zip}"},
103 Description: "app bundle",
104 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100105
106 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
107 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
108 Rspfile: "${out}.emit_commands",
109 RspfileContent: "${emit_commands}",
110 Description: "Emit APEX image content",
111 }, "emit_commands")
112
113 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
114 Command: `diff --unchanged-group-format='' \` +
115 `--changed-group-format='%<' \` +
116 `${image_content_file} ${whitelisted_files_file} || (` +
117 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
118 ` "To fix the build run following command:" && ` +
119 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
120 `exit 1)`,
121 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
122 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900123)
124
Alex Light5098a612018-11-29 17:12:15 -0800125var imageApexSuffix = ".apex"
126var zipApexSuffix = ".zipapex"
127
128var imageApexType = "image"
129var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900130
131type dependencyTag struct {
132 blueprint.BaseDependencyTag
133 name string
134}
135
136var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900137 sharedLibTag = dependencyTag{name: "sharedLib"}
138 executableTag = dependencyTag{name: "executable"}
139 javaLibTag = dependencyTag{name: "javaLib"}
140 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100141 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900142 keyTag = dependencyTag{name: "key"}
143 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900144 usesTag = dependencyTag{name: "uses"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900145)
146
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900147var (
148 whitelistNoApex = map[string][]string{
149 "apex_test_build_features": []string{"libbinder"},
150 "com.android.neuralnetworks": []string{"libbinder"},
151 "com.android.media": []string{"libbinder"},
152 "com.android.media.swcodec": []string{"libbinder"},
153 "test_com.android.media.swcodec": []string{"libbinder"},
154 }
155)
156
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900157func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700158 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900159 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100161 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
162 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
163 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
164 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000165 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100166 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
167 } else {
168 return pctx.HostBinToolPath(ctx, tool).String()
169 }
170 })
171 }
172 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900173 pctx.HostBinToolVariable("avbtool", "avbtool")
174 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
175 pctx.HostBinToolVariable("merge_zips", "merge_zips")
176 pctx.HostBinToolVariable("mke2fs", "mke2fs")
177 pctx.HostBinToolVariable("resize2fs", "resize2fs")
178 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
179 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800180 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900181 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900182 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900183
Alex Light0851b882019-02-07 13:20:53 -0800184 android.RegisterModuleType("apex", apexBundleFactory)
185 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900186 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700187 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900188
189 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
190 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700191 ctx.BottomUp("apex", apexMutator).Parallel()
Jooyung Han5c998b92019-06-27 11:30:33 +0900192 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900193 })
194}
195
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900196// Mark the direct and transitive dependencies of apex bundles so that they
197// can be built for the apex bundles.
198func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800199 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800200 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900201 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900202 depName := mctx.OtherModuleName(child)
203 // If the parent is apexBundle, this child is directly depended.
204 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800205 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800206 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
207 // non-installable apex's cannot be installed and so should not prevent libraries from being
208 // installed to the system.
209 android.UpdateApexDependency(apexBundleName, depName, directDep)
210 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900211
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900212 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900213 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900214 return true
215 } else {
216 return false
217 }
218 })
219 }
220}
221
222// Create apex variations if a module is included in APEX(s).
223func apexMutator(mctx android.BottomUpMutatorContext) {
224 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900225 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900226 } else if _, ok := mctx.Module().(*apexBundle); ok {
227 // apex bundle itself is mutated so that it and its modules have same
228 // apex variant.
229 apexBundleName := mctx.ModuleName()
230 mctx.CreateVariations(apexBundleName)
231 }
232}
Jooyung Han5c998b92019-06-27 11:30:33 +0900233func apexUsesMutator(mctx android.BottomUpMutatorContext) {
234 if ab, ok := mctx.Module().(*apexBundle); ok {
235 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
236 }
237}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900238
Alex Light9670d332019-01-29 18:07:33 -0800239type apexNativeDependencies struct {
240 // List of native libraries
241 Native_shared_libs []string
242 // List of native executables
243 Binaries []string
Roland Levillain630846d2019-06-26 12:48:34 +0100244 // List of native tests
245 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800246}
247type apexMultilibProperties struct {
248 // Native dependencies whose compile_multilib is "first"
249 First apexNativeDependencies
250
251 // Native dependencies whose compile_multilib is "both"
252 Both apexNativeDependencies
253
254 // Native dependencies whose compile_multilib is "prefer32"
255 Prefer32 apexNativeDependencies
256
257 // Native dependencies whose compile_multilib is "32"
258 Lib32 apexNativeDependencies
259
260 // Native dependencies whose compile_multilib is "64"
261 Lib64 apexNativeDependencies
262}
263
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900264type apexBundleProperties struct {
265 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000266 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800267 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900268
Jiyong Park40e26a22019-02-08 02:53:06 +0900269 // AndroidManifest.xml file used for the zip container of this APEX bundle.
270 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800271 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900272
Jiyong Park05e70dd2019-03-18 14:26:32 +0900273 // Canonical name of the APEX bundle in the manifest file.
274 // If unspecified, defaults to the value of name
275 Apex_name *string
276
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900277 // Determines the file contexts file for setting security context to each file in this APEX bundle.
278 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
279 // used.
280 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900281 File_contexts *string
282
283 // List of native shared libs that are embedded inside this APEX bundle
284 Native_shared_libs []string
285
Roland Levillain630846d2019-06-26 12:48:34 +0100286 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900287 Binaries []string
288
289 // List of java libraries that are embedded inside this APEX bundle
290 Java_libs []string
291
292 // List of prebuilt files that are embedded inside this APEX bundle
293 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900294
Roland Levillain630846d2019-06-26 12:48:34 +0100295 // List of tests that are embedded inside this APEX bundle
296 Tests []string
297
Jiyong Parkff1458f2018-10-12 21:49:38 +0900298 // Name of the apex_key module that provides the private key to sign APEX
299 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900300
Alex Light5098a612018-11-29 17:12:15 -0800301 // The type of APEX to build. Controls what the APEX payload is. Either
302 // 'image', 'zip' or 'both'. Default: 'image'.
303 Payload_type *string
304
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900305 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
306 // or an android_app_certificate module name in the form ":module".
307 Certificate *string
308
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900309 // Whether this APEX is installable to one of the partitions. Default: true.
310 Installable *bool
311
Jiyong Parkda6eb592018-12-19 17:12:36 +0900312 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
313 // Default is false.
314 Use_vendor *bool
315
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800316 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
317 Ignore_system_library_special_case *bool
318
Alex Light9670d332019-01-29 18:07:33 -0800319 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900320
Jiyong Parkf97782b2019-02-13 20:28:58 +0900321 // List of sanitizer names that this APEX is enabled for
322 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900323
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900324 PreventInstall bool `blueprint:"mutated"`
325
326 HideFromMake bool `blueprint:"mutated"`
327
Jooyung Han5c998b92019-06-27 11:30:33 +0900328 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
329 Provide_cpp_shared_libs *bool
330
331 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
332 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100333
334 // A txt file containing list of files that are whitelisted to be included in this APEX.
335 Whitelisted_files *string
Alex Light9670d332019-01-29 18:07:33 -0800336}
337
338type apexTargetBundleProperties struct {
339 Target struct {
340 // Multilib properties only for android.
341 Android struct {
342 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900343 }
Alex Light9670d332019-01-29 18:07:33 -0800344 // Multilib properties only for host.
345 Host struct {
346 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900347 }
Alex Light9670d332019-01-29 18:07:33 -0800348 // Multilib properties only for host linux_bionic.
349 Linux_bionic struct {
350 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900351 }
Alex Light9670d332019-01-29 18:07:33 -0800352 // Multilib properties only for host linux_glibc.
353 Linux_glibc struct {
354 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900355 }
356 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900357}
358
Jiyong Park8fd61922018-11-08 02:50:25 +0900359type apexFileClass int
360
361const (
362 etc apexFileClass = iota
363 nativeSharedLib
364 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900365 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800366 pyBinary
367 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900368 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100369 nativeTest
Jiyong Park8fd61922018-11-08 02:50:25 +0900370)
371
Alex Light5098a612018-11-29 17:12:15 -0800372type apexPackaging int
373
374const (
375 imageApex apexPackaging = iota
376 zipApex
377 both
378)
379
380func (a apexPackaging) image() bool {
381 switch a {
382 case imageApex, both:
383 return true
384 }
385 return false
386}
387
388func (a apexPackaging) zip() bool {
389 switch a {
390 case zipApex, both:
391 return true
392 }
393 return false
394}
395
396func (a apexPackaging) suffix() string {
397 switch a {
398 case imageApex:
399 return imageApexSuffix
400 case zipApex:
401 return zipApexSuffix
402 case both:
403 panic(fmt.Errorf("must be either zip or image"))
404 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100405 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800406 }
407}
408
409func (a apexPackaging) name() string {
410 switch a {
411 case imageApex:
412 return imageApexType
413 case zipApex:
414 return zipApexType
415 case both:
416 panic(fmt.Errorf("must be either zip or image"))
417 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100418 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800419 }
420}
421
Jiyong Park8fd61922018-11-08 02:50:25 +0900422func (class apexFileClass) NameInMake() string {
423 switch class {
424 case etc:
425 return "ETC"
426 case nativeSharedLib:
427 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800428 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900429 return "EXECUTABLES"
430 case javaSharedLib:
431 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100432 case nativeTest:
433 return "NATIVE_TESTS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900434 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100435 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900436 }
437}
438
439type apexFile struct {
440 builtFile android.Path
441 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900442 installDir string
443 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900444 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800445 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900446}
447
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900448type apexBundle struct {
449 android.ModuleBase
450 android.DefaultableModuleBase
451
Alex Light9670d332019-01-29 18:07:33 -0800452 properties apexBundleProperties
453 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900454
Alex Light5098a612018-11-29 17:12:15 -0800455 apexTypes apexPackaging
456
Colin Crossa4925902018-11-16 11:36:28 -0800457 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800458 outputFiles map[apexPackaging]android.WritablePath
Roland Levillain935639d2019-08-13 14:55:28 +0100459 flattenedOutput android.OutputPath
Colin Crossa4925902018-11-16 11:36:28 -0800460 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900461
Jiyong Park03b68dd2019-07-26 23:20:40 +0900462 prebuiltFileToDelete string
463
Jiyong Park42cca6c2019-04-01 11:15:50 +0900464 public_key_file android.Path
465 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900466
467 container_certificate_file android.Path
468 container_private_key_file android.Path
469
Jiyong Park8fd61922018-11-08 02:50:25 +0900470 // list of files to be included in this apex
471 filesInfo []apexFile
472
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900473 // list of module names that this APEX is depending on
474 externalDeps []string
475
Jiyong Park8fd61922018-11-08 02:50:25 +0900476 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800477
478 testApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900479
480 // intermediate path for apex_manifest.json
481 manifestOut android.WritablePath
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900482}
483
Jiyong Park397e55e2018-10-24 21:09:55 +0900484func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100485 native_shared_libs []string, binaries []string, tests []string,
486 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900487 // Use *FarVariation* to be able to depend on modules having
488 // conflicting variations with this module. This is required since
489 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
490 // for native shared libs.
491 ctx.AddFarVariationDependencies([]blueprint.Variation{
492 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900493 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900494 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900495 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900496 }, sharedLibTag, native_shared_libs...)
497
498 ctx.AddFarVariationDependencies([]blueprint.Variation{
499 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900500 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900501 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100502
503 ctx.AddFarVariationDependencies([]blueprint.Variation{
504 {Mutator: "arch", Variation: arch},
505 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100506 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100507 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900508}
509
Alex Light9670d332019-01-29 18:07:33 -0800510func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
511 if ctx.Os().Class == android.Device {
512 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
513 } else {
514 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
515 if ctx.Os().Bionic() {
516 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
517 } else {
518 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
519 }
520 }
521}
522
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900523func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800524
Jiyong Park397e55e2018-10-24 21:09:55 +0900525 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900526 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800527
528 a.combineProperties(ctx)
529
Jiyong Park397e55e2018-10-24 21:09:55 +0900530 has32BitTarget := false
531 for _, target := range targets {
532 if target.Arch.ArchType.Multilib == "lib32" {
533 has32BitTarget = true
534 }
535 }
536 for i, target := range targets {
537 // When multilib.* is omitted for native_shared_libs, it implies
538 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900539 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900540 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900541 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900542 {Mutator: "link", Variation: "shared"},
543 }, sharedLibTag, a.properties.Native_shared_libs...)
544
Roland Levillain630846d2019-06-26 12:48:34 +0100545 // When multilib.* is omitted for tests, it implies
546 // multilib.both.
547 ctx.AddFarVariationDependencies([]blueprint.Variation{
548 {Mutator: "arch", Variation: target.String()},
549 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100550 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100551 }, testTag, a.properties.Tests...)
552
Jiyong Park397e55e2018-10-24 21:09:55 +0900553 // Add native modules targetting both ABIs
554 addDependenciesForNativeModules(ctx,
555 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100556 a.properties.Multilib.Both.Binaries,
557 a.properties.Multilib.Both.Tests,
558 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900559 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900560
Alex Light3d673592019-01-18 14:37:31 -0800561 isPrimaryAbi := i == 0
562 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900563 // When multilib.* is omitted for binaries, it implies
564 // multilib.first.
565 ctx.AddFarVariationDependencies([]blueprint.Variation{
566 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900567 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900568 }, executableTag, a.properties.Binaries...)
569
570 // Add native modules targetting the first ABI
571 addDependenciesForNativeModules(ctx,
572 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100573 a.properties.Multilib.First.Binaries,
574 a.properties.Multilib.First.Tests,
575 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900576 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800577
578 // When multilib.* is omitted for prebuilts, it implies multilib.first.
579 ctx.AddFarVariationDependencies([]blueprint.Variation{
580 {Mutator: "arch", Variation: target.String()},
581 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900582 }
583
584 switch target.Arch.ArchType.Multilib {
585 case "lib32":
586 // Add native modules targetting 32-bit ABI
587 addDependenciesForNativeModules(ctx,
588 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100589 a.properties.Multilib.Lib32.Binaries,
590 a.properties.Multilib.Lib32.Tests,
591 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900592 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900593
594 addDependenciesForNativeModules(ctx,
595 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100596 a.properties.Multilib.Prefer32.Binaries,
597 a.properties.Multilib.Prefer32.Tests,
598 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900599 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900600 case "lib64":
601 // Add native modules targetting 64-bit ABI
602 addDependenciesForNativeModules(ctx,
603 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100604 a.properties.Multilib.Lib64.Binaries,
605 a.properties.Multilib.Lib64.Tests,
606 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900607 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900608
609 if !has32BitTarget {
610 addDependenciesForNativeModules(ctx,
611 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100612 a.properties.Multilib.Prefer32.Binaries,
613 a.properties.Multilib.Prefer32.Tests,
614 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900615 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900616 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700617
618 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
619 for _, sanitizer := range ctx.Config().SanitizeDevice() {
620 if sanitizer == "hwaddress" {
621 addDependenciesForNativeModules(ctx,
622 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100623 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700624 break
625 }
626 }
627 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900628 }
629
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900630 }
631
Jiyong Parkff1458f2018-10-12 21:49:38 +0900632 ctx.AddFarVariationDependencies([]blueprint.Variation{
633 {Mutator: "arch", Variation: "android_common"},
634 }, javaLibTag, a.properties.Java_libs...)
635
Jiyong Park23c52b02019-02-02 13:13:47 +0900636 if String(a.properties.Key) == "" {
637 ctx.ModuleErrorf("key is missing")
638 return
639 }
640 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900641
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900642 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900643 if cert != "" {
644 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900645 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900646}
647
Colin Cross0ea8ba82019-06-06 14:33:29 -0700648func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900649 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
650 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000651 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900652 }
653 return String(a.properties.Certificate)
654}
655
Colin Cross41955e82019-05-29 14:40:35 -0700656func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
657 switch tag {
658 case "":
659 if file, ok := a.outputFiles[imageApex]; ok {
660 return android.Paths{file}, nil
661 } else {
662 return nil, nil
663 }
Roland Levillain935639d2019-08-13 14:55:28 +0100664 case ".flattened":
665 if a.flattened {
666 flattenedApexPath := a.flattenedOutput
667 return android.Paths{flattenedApexPath}, nil
668 } else {
669 return nil, nil
670 }
Colin Cross41955e82019-05-29 14:40:35 -0700671 default:
672 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900673 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900674}
675
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900676func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900677 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900678}
679
Jiyong Park7c1dc612019-01-05 11:15:24 +0900680func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
681 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900682 return "vendor"
683 } else {
684 return "core"
685 }
686}
687
Jiyong Parkf97782b2019-02-13 20:28:58 +0900688func (a *apexBundle) EnableSanitizer(sanitizerName string) {
689 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
690 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
691 }
692}
693
Jiyong Park388ef3f2019-01-28 19:47:32 +0900694func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900695 if android.InList(sanitizerName, a.properties.SanitizerNames) {
696 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900697 }
698
699 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900700 globalSanitizerNames := []string{}
701 if a.Host() {
702 globalSanitizerNames = ctx.Config().SanitizeHost()
703 } else {
704 arches := ctx.Config().SanitizeDeviceArch()
705 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
706 globalSanitizerNames = ctx.Config().SanitizeDevice()
707 }
708 }
709 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900710}
711
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900712func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
713 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
714}
715
716func (a *apexBundle) PreventInstall() {
717 a.properties.PreventInstall = true
718}
719
720func (a *apexBundle) HideFromMake() {
721 a.properties.HideFromMake = true
722}
723
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800724func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900725 // Decide the APEX-local directory by the multilib of the library
726 // In the future, we may query this to the module.
727 switch cc.Arch().ArchType.Multilib {
728 case "lib32":
729 dirInApex = "lib"
730 case "lib64":
731 dirInApex = "lib64"
732 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900733 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200734 if !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900735 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200736 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
737 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900738 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800739 if handleSpecialLibs {
740 switch cc.Name() {
741 case "libc", "libm", "libdl":
742 // Special case for bionic libs. This is to prevent the bionic libs
743 // from being included in the search path /apex/com.android.apex/lib.
744 // This exclusion is required because bionic libs in the runtime APEX
745 // are available via the legacy paths /system/lib/libc.so, etc. By the
746 // init process, the bionic libs in the APEX are bind-mounted to the
747 // legacy paths and thus will be loaded into the default linker namespace.
748 // If the bionic libs are directly in /apex/com.android.apex/lib then
749 // the same libs will be again loaded to the runtime linker namespace,
750 // which will result double loading of bionic libs that isn't supported.
751 dirInApex = filepath.Join(dirInApex, "bionic")
752 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900753 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900754
755 fileToCopy = cc.OutputFile().Path()
756 return
757}
758
759func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900760 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200761 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900762 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200763 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
764 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900765 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900766 fileToCopy = cc.OutputFile().Path()
767 return
768}
769
Alex Light778127a2019-02-27 14:19:50 -0800770func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
771 dirInApex = "bin"
772 fileToCopy = py.HostToolPath().Path()
773 return
774}
775func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
776 dirInApex = "bin"
777 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
778 if err != nil {
779 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
780 return
781 }
782 fileToCopy = android.PathForOutput(ctx, s)
783 return
784}
785
Jiyong Park04480cf2019-02-06 00:16:29 +0900786func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
787 dirInApex = filepath.Join("bin", sh.SubDir())
788 fileToCopy = sh.OutputFile()
789 return
790}
791
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900792func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
793 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900794 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900795 return
796}
797
Jiyong Park9e6c2422019-08-09 20:39:45 +0900798func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
799 dirInApex = "javalib"
800 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
801 implJars := java.ImplementationJars()
802 if len(implJars) != 1 {
803 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
804 strings.Join(implJars.Strings(), ", ")))
805 }
806 fileToCopy = implJars[0]
807 return
808}
809
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900810func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
811 dirInApex = filepath.Join("etc", prebuilt.SubDir())
812 fileToCopy = prebuilt.OutputFile()
813 return
814}
815
Roland Levillain935639d2019-08-13 14:55:28 +0100816// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
817type flattenedApexContext struct {
818 android.ModuleContext
819}
820
821func (c *flattenedApexContext) InstallBypassMake() bool {
822 return true
823}
824
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900825func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900826 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900827
Alex Light5098a612018-11-29 17:12:15 -0800828 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
829 a.apexTypes = imageApex
830 } else if *a.properties.Payload_type == "zip" {
831 a.apexTypes = zipApex
832 } else if *a.properties.Payload_type == "both" {
833 a.apexTypes = both
834 } else {
835 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
836 return
837 }
838
Roland Levillain630846d2019-06-26 12:48:34 +0100839 if len(a.properties.Tests) > 0 && !a.testApex {
840 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
841 return
842 }
843
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800844 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
845
Jooyung Hane1633032019-08-01 17:41:43 +0900846 // native lib dependencies
847 var provideNativeLibs []string
848 var requireNativeLibs []string
849
Jooyung Han5c998b92019-06-27 11:30:33 +0900850 // Check if "uses" requirements are met with dependent apexBundles
851 var providedNativeSharedLibs []string
852 useVendor := proptools.Bool(a.properties.Use_vendor)
853 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
854 if ctx.OtherModuleDependencyTag(m) != usesTag {
855 return
856 }
857 otherName := ctx.OtherModuleName(m)
858 other, ok := m.(*apexBundle)
859 if !ok {
860 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
861 return
862 }
863 if proptools.Bool(other.properties.Use_vendor) != useVendor {
864 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
865 return
866 }
867 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
868 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
869 return
870 }
871 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
872 })
873
Alex Light778127a2019-02-27 14:19:50 -0800874 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100875 depTag := ctx.OtherModuleDependencyTag(child)
876 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900877 if _, ok := parent.(*apexBundle); ok {
878 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900879 switch depTag {
880 case sharedLibTag:
881 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900882 if cc.HasStubsVariants() {
883 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
884 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800885 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900886 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900887 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900888 } else {
889 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900890 }
891 case executableTag:
892 if cc, ok := child.(*cc.Module); ok {
893 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900894 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900895 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900896 } else if sh, ok := child.(*android.ShBinary); ok {
897 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
898 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800899 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
900 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
901 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
902 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
903 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
904 // NB: Since go binaries are static we don't need the module for anything here, which is
905 // good since the go tool is a blueprint.Module not an android.Module like we would
906 // normally use.
907 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900908 } else {
Alex Light778127a2019-02-27 14:19:50 -0800909 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 +0900910 }
911 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +0900912 if javaLib, ok := child.(*java.Library); ok {
913 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +0900914 if fileToCopy == nil {
915 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
916 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900917 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
918 }
919 return true
920 } else if javaLib, ok := child.(*java.Import); ok {
921 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
922 if fileToCopy == nil {
923 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
924 } else {
925 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900926 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900927 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900928 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900929 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900930 }
931 case prebuiltTag:
932 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
933 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900934 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900935 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900936 } else {
937 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
938 }
Roland Levillain630846d2019-06-26 12:48:34 +0100939 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100940 if ccTest, ok := child.(*cc.Module); ok {
941 if ccTest.IsTestPerSrcAllTestsVariation() {
942 // Multiple-output test module (where `test_per_src: true`).
943 //
944 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
945 // We do not add this variation to `filesInfo`, as it has no output;
946 // however, we do add the other variations of this module as indirect
947 // dependencies (see below).
948 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100949 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100950 // Single-output test module (where `test_per_src: false`).
951 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
952 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100953 }
Roland Levillain630846d2019-06-26 12:48:34 +0100954 return true
955 } else {
956 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
957 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900958 case keyTag:
959 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900960 a.private_key_file = key.private_key_file
961 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900962 return false
963 } else {
964 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900965 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900966 case certificateTag:
967 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900968 a.container_certificate_file = dep.Certificate.Pem
969 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900970 return false
971 } else {
972 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
973 }
Jiyong Park03b68dd2019-07-26 23:20:40 +0900974 case android.PrebuiltDepTag:
975 // If the prebuilt is force disabled, remember to delete the prebuilt file
976 // that might have been installed in the previous builds
977 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
978 a.prebuiltFileToDelete = prebuilt.InstallFilename()
979 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900980 }
981 } else {
982 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +0900983 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +0100984 // We cannot use a switch statement on `depTag` here as the checked
985 // tags used below are private (e.g. `cc.sharedDepTag`).
986 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
987 if cc, ok := child.(*cc.Module); ok {
988 if android.InList(cc.Name(), providedNativeSharedLibs) {
989 // If we're using a shared library which is provided from other APEX,
990 // don't include it in this APEX
991 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900992 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100993 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
994 // If the dependency is a stubs lib, don't include it in this APEX,
995 // but make sure that the lib is installed on the device.
996 // In case no APEX is having the lib, the lib is installed to the system
997 // partition.
998 //
999 // Always include if we are a host-apex however since those won't have any
1000 // system libraries.
1001 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1002 a.externalDeps = append(a.externalDeps, cc.Name())
1003 }
Jooyung Hane1633032019-08-01 17:41:43 +09001004 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001005 // Don't track further
1006 return false
1007 }
1008 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
1009 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1010 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001012 } else if cc.IsTestPerSrcDepTag(depTag) {
1013 if cc, ok := child.(*cc.Module); ok {
1014 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1015 // Handle modules created as `test_per_src` variations of a single test module:
1016 // use the name of the generated test binary (`fileToCopy`) instead of the name
1017 // of the original test module (`depName`, shared by all `test_per_src`
1018 // variations of that module).
1019 moduleName := filepath.Base(fileToCopy.String())
1020 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1021 return true
1022 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001023 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001024 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Sundong Ahn2db7f462019-08-27 18:53:12 +09001025 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
1026 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001027 }
1028 }
1029 }
1030 return false
1031 })
1032
Jiyong Park9335a262018-12-24 11:31:58 +09001033 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001034 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001035 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1036 return
1037 }
1038
Jiyong Park8fd61922018-11-08 02:50:25 +09001039 // remove duplicates in filesInfo
1040 removeDup := func(filesInfo []apexFile) []apexFile {
1041 encountered := make(map[android.Path]bool)
1042 result := []apexFile{}
1043 for _, f := range filesInfo {
1044 if !encountered[f.builtFile] {
1045 encountered[f.builtFile] = true
1046 result = append(result, f)
1047 }
1048 }
1049 return result
1050 }
1051 filesInfo = removeDup(filesInfo)
1052
1053 // to have consistent build rules
1054 sort.Slice(filesInfo, func(i, j int) bool {
1055 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1056 })
1057
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001058 // check no_apex modules
1059 whitelist := whitelistNoApex[ctx.ModuleName()]
1060 for i := range filesInfo {
1061 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1062 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1063 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1064 }
1065 }
1066 }
1067
Jiyong Park8fd61922018-11-08 02:50:25 +09001068 // prepend the name of this APEX to the module names. These names will be the names of
1069 // modules that will be defined if the APEX is flattened.
1070 for i := range filesInfo {
1071 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1072 }
1073
Jiyong Park8fd61922018-11-08 02:50:25 +09001074 a.installDir = android.PathForModuleInstall(ctx, "apex")
1075 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001076
Jooyung Hane1633032019-08-01 17:41:43 +09001077 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1078 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1079 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1080 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1081 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1082 ctx.Build(pctx, android.BuildParams{
1083 Rule: injectApexDependency,
1084 Input: manifestSrc,
1085 Output: a.manifestOut,
1086 Args: map[string]string{
1087 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1088 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1089 },
1090 })
1091
Roland Levillain935639d2019-08-13 14:55:28 +01001092 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1093 // reply true to `InstallBypassMake()` (thus making the call
1094 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1095 // instead of `android.PathForOutput`) to return the correct path to the flattened
1096 // APEX (as its contents is installed by Make, not Soong).
1097 factx := flattenedApexContext{ctx}
1098 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1099
Alex Light5098a612018-11-29 17:12:15 -08001100 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001101 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001102 }
1103 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001104 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001105 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001106 // in other modules. It is in AndroidMk where the selection of flattened
1107 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001108 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001109 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001110 }
1111}
1112
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001113func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001114 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001115 for _, f := range a.filesInfo {
1116 if f.module != nil {
1117 notice := f.module.NoticeFile()
1118 if notice.Valid() {
1119 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001120 }
1121 }
1122 }
1123 // append the notice file specified in the apex module itself
1124 if a.NoticeFile().Valid() {
1125 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001126 }
1127
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001128 if len(noticeFiles) == 0 {
1129 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001130 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001131
Jaewoong Jung98772792019-07-01 17:15:13 -07001132 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001133}
1134
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001135func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001136 cert := String(a.properties.Certificate)
1137 if cert != "" && android.SrcIsModule(cert) == "" {
1138 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001139 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1140 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001141 } else if cert == "" {
1142 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001143 a.container_certificate_file = pem
1144 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001145 }
1146
Alex Light5098a612018-11-29 17:12:15 -08001147 var abis []string
1148 for _, target := range ctx.MultiTargets() {
1149 if len(target.Arch.Abi) > 0 {
1150 abis = append(abis, target.Arch.Abi[0])
1151 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001152 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001153
Alex Light5098a612018-11-29 17:12:15 -08001154 abis = android.FirstUniqueStrings(abis)
1155
1156 suffix := apexType.suffix()
1157 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001158
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001159 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001160 for _, f := range a.filesInfo {
1161 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001162 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001163
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001164 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001165 emitCommands := []string{}
1166 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1167 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001168 for i, src := range filesToCopy {
1169 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001170 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001171 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001172 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1173 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001174 for _, sym := range a.filesInfo[i].symlinks {
1175 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1176 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1177 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001178 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001179 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001180 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001181
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001182 if a.properties.Whitelisted_files != nil {
1183 ctx.Build(pctx, android.BuildParams{
1184 Rule: emitApexContentRule,
1185 Implicits: implicitInputs,
1186 Output: imageContentFile,
1187 Description: "emit apex image content",
1188 Args: map[string]string{
1189 "emit_commands": strings.Join(emitCommands, " && "),
1190 },
1191 })
1192 implicitInputs = append(implicitInputs, imageContentFile)
1193 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1194
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001195 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001196 ctx.Build(pctx, android.BuildParams{
1197 Rule: diffApexContentRule,
1198 Implicits: implicitInputs,
1199 Output: phonyOutput,
1200 Description: "diff apex image content",
1201 Args: map[string]string{
1202 "whitelisted_files_file": whitelistedFilesFile.String(),
1203 "image_content_file": imageContentFile.String(),
1204 "apex_module_name": ctx.ModuleName(),
1205 },
1206 })
1207
1208 implicitInputs = append(implicitInputs, phonyOutput)
1209 }
1210
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001211 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1212 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001213
Alex Light5098a612018-11-29 17:12:15 -08001214 if apexType.image() {
1215 // files and dirs that will be created in APEX
1216 var readOnlyPaths []string
1217 var executablePaths []string // this also includes dirs
1218 for _, f := range a.filesInfo {
1219 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001220 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001221 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001222 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001223 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001224 }
Alex Light5098a612018-11-29 17:12:15 -08001225 } else {
1226 readOnlyPaths = append(readOnlyPaths, pathInApex)
1227 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001228 dir := f.installDir
1229 for !android.InList(dir, executablePaths) && dir != "" {
1230 executablePaths = append(executablePaths, dir)
1231 dir, _ = filepath.Split(dir) // move up to the parent
1232 if len(dir) > 0 {
1233 // remove trailing slash
1234 dir = dir[:len(dir)-1]
1235 }
Alex Light5098a612018-11-29 17:12:15 -08001236 }
1237 }
1238 sort.Strings(readOnlyPaths)
1239 sort.Strings(executablePaths)
1240 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1241 ctx.Build(pctx, android.BuildParams{
1242 Rule: generateFsConfig,
1243 Output: cannedFsConfig,
1244 Description: "generate fs config",
1245 Args: map[string]string{
1246 "ro_paths": strings.Join(readOnlyPaths, " "),
1247 "exec_paths": strings.Join(executablePaths, " "),
1248 },
1249 })
1250
1251 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1252 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1253 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1254 if !fileContextsOptionalPath.Valid() {
1255 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1256 return
1257 }
1258 fileContexts := fileContextsOptionalPath.Path()
1259
Jiyong Park835d82b2018-12-27 16:04:18 +09001260 optFlags := []string{}
1261
Alex Light5098a612018-11-29 17:12:15 -08001262 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001263 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1264 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001265
Jiyong Park7f67f482019-01-05 12:57:48 +09001266 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1267 if overridden {
1268 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1269 }
1270
Jiyong Park40e26a22019-02-08 02:53:06 +09001271 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001272 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001273 implicitInputs = append(implicitInputs, androidManifestFile)
1274 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1275 }
1276
Jiyong Park71b519d2019-04-18 17:25:49 +09001277 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1278 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1279 ctx.Config().UnbundledBuild() &&
1280 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1281 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1282 apiFingerprint := java.ApiFingerprintPath(ctx)
1283 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1284 implicitInputs = append(implicitInputs, apiFingerprint)
1285 }
1286 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1287
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001288 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1289 if noticeFile.Valid() {
1290 // If there's a NOTICE file, embed it as an asset file in the APEX.
1291 implicitInputs = append(implicitInputs, noticeFile.Path())
1292 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1293 }
1294
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001295 if !ctx.Config().UnbundledBuild() && a.installable() {
1296 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1297 // don't need hashtree for activation. Therefore, by removing hashtree from
1298 // apex bundle (filesystem image in it, to be specific), we can save storage.
1299 optFlags = append(optFlags, "--no_hashtree")
1300 }
1301
Alex Light5098a612018-11-29 17:12:15 -08001302 ctx.Build(pctx, android.BuildParams{
1303 Rule: apexRule,
1304 Implicits: implicitInputs,
1305 Output: unsignedOutputFile,
1306 Description: "apex (" + apexType.name() + ")",
1307 Args: map[string]string{
1308 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1309 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1310 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001311 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001312 "file_contexts": fileContexts.String(),
1313 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001314 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001315 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001316 },
1317 })
1318
1319 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1320 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1321 a.bundleModuleFile = bundleModuleFile
1322
1323 ctx.Build(pctx, android.BuildParams{
1324 Rule: apexProtoConvertRule,
1325 Input: unsignedOutputFile,
1326 Output: apexProtoFile,
1327 Description: "apex proto convert",
1328 })
1329
1330 ctx.Build(pctx, android.BuildParams{
1331 Rule: apexBundleRule,
1332 Input: apexProtoFile,
1333 Output: a.bundleModuleFile,
1334 Description: "apex bundle module",
1335 Args: map[string]string{
1336 "abi": strings.Join(abis, "."),
1337 },
1338 })
1339 } else {
1340 ctx.Build(pctx, android.BuildParams{
1341 Rule: zipApexRule,
1342 Implicits: implicitInputs,
1343 Output: unsignedOutputFile,
1344 Description: "apex (" + apexType.name() + ")",
1345 Args: map[string]string{
1346 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1347 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1348 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001349 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001350 },
1351 })
Colin Crossa4925902018-11-16 11:36:28 -08001352 }
Colin Crossa4925902018-11-16 11:36:28 -08001353
Alex Light5098a612018-11-29 17:12:15 -08001354 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001355 ctx.Build(pctx, android.BuildParams{
1356 Rule: java.Signapk,
1357 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001358 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001359 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001360 Implicits: []android.Path{
1361 a.container_certificate_file,
1362 a.container_private_key_file,
1363 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001364 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001365 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001366 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001367 },
1368 })
Alex Light5098a612018-11-29 17:12:15 -08001369
1370 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001371 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001372 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001373 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001374}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001375
Jiyong Park8fd61922018-11-08 02:50:25 +09001376func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001377 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001378 // 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 +09001379 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001380 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001381
Jiyong Park42cca6c2019-04-01 11:15:50 +09001382 // rename to apex_pubkey
1383 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1384 ctx.Build(pctx, android.BuildParams{
1385 Rule: android.Cp,
1386 Input: a.public_key_file,
1387 Output: copiedPubkey,
1388 })
1389 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1390
Jiyong Park23c52b02019-02-02 13:13:47 +09001391 if ctx.Config().FlattenApex() {
1392 for _, fi := range a.filesInfo {
1393 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001394 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1395 for _, sym := range fi.symlinks {
1396 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1397 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001398 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001399 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001400 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001401}
1402
1403func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001404 if a.properties.HideFromMake {
1405 return android.AndroidMkData{
1406 Disabled: true,
1407 }
1408 }
Alex Light5098a612018-11-29 17:12:15 -08001409 writers := []android.AndroidMkData{}
1410 if a.apexTypes.image() {
1411 writers = append(writers, a.androidMkForType(imageApex))
1412 }
1413 if a.apexTypes.zip() {
1414 writers = append(writers, a.androidMkForType(zipApex))
1415 }
1416 return android.AndroidMkData{
1417 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1418 for _, data := range writers {
1419 data.Custom(w, name, prefix, moduleDir, data)
1420 }
1421 }}
1422}
1423
Alex Lightf1801bc2019-02-13 11:10:07 -08001424func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001425 moduleNames := []string{}
1426
1427 for _, fi := range a.filesInfo {
1428 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1429 continue
1430 }
1431 if !android.InList(fi.moduleName, moduleNames) {
1432 moduleNames = append(moduleNames, fi.moduleName)
1433 }
1434 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1435 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1436 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001437 // /apex/<name>/{lib|framework|...}
1438 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1439 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001440 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001441 // /system/apex/<name>/{lib|framework|...}
1442 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1443 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001444 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001445 if len(fi.symlinks) > 0 {
1446 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1447 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001448
1449 if fi.module != nil && fi.module.NoticeFile().Valid() {
1450 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1451 }
Jiyong Park94427262019-02-05 23:18:47 +09001452 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001453 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001454 }
1455 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1456 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1457 if fi.module != nil {
1458 archStr := fi.module.Target().Arch.ArchType.String()
1459 host := false
1460 switch fi.module.Target().Os.Class {
1461 case android.Host:
1462 if archStr != "common" {
1463 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1464 }
1465 host = true
1466 case android.HostCross:
1467 if archStr != "common" {
1468 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1469 }
1470 host = true
1471 case android.Device:
1472 if archStr != "common" {
1473 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1474 }
1475 }
1476 if host {
1477 makeOs := fi.module.Target().Os.String()
1478 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1479 makeOs = "linux"
1480 }
1481 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1482 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1483 }
1484 }
1485 if fi.class == javaSharedLib {
1486 javaModule := fi.module.(*java.Library)
1487 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1488 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1489 // we will have foo.jar.jar
1490 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1491 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1492 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1493 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1494 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1495 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1496 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1497 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001498 if cc, ok := fi.module.(*cc.Module); ok {
1499 if cc.UnstrippedOutputFile() != nil {
1500 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1501 }
1502 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001503 if cc.CoverageOutputFile().Valid() {
1504 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1505 }
Jiyong Park94427262019-02-05 23:18:47 +09001506 }
1507 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1508 } else {
1509 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1510 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1511 }
1512 }
1513 return moduleNames
1514}
1515
Alex Light5098a612018-11-29 17:12:15 -08001516func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001517 return android.AndroidMkData{
1518 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1519 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001520 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001521 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001522 }
1523
Jiyong Park719b4462019-01-13 00:39:51 +09001524 if a.flattened && apexType.image() {
1525 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001526 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1527 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1528 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001529 if len(moduleNames) > 0 {
1530 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1531 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001532 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001533 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1534
Jiyong Park719b4462019-01-13 00:39:51 +09001535 } else {
Alex Light5098a612018-11-29 17:12:15 -08001536 // zip-apex is the less common type so have the name refer to the image-apex
1537 // only and use {name}.zip if you want the zip-apex
1538 if apexType == zipApex && a.apexTypes == both {
1539 name = name + ".zip"
1540 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001541 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1542 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1543 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1544 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001545 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001546 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001547 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001548 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001549 if len(moduleNames) > 0 {
1550 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1551 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001552 if len(a.externalDeps) > 0 {
1553 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1554 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001555 if a.prebuiltFileToDelete != "" {
1556 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1557 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1558 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001559 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001560
Alex Light5098a612018-11-29 17:12:15 -08001561 if apexType == imageApex {
1562 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1563 }
Jiyong Park719b4462019-01-13 00:39:51 +09001564 }
1565 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001566}
1567
Alex Light0851b882019-02-07 13:20:53 -08001568func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001569 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001570}
1571
1572func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001573 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001574}
1575
1576func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001577 module := &apexBundle{
1578 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001579 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001580 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001581 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001582 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001583 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001584 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1585 })
Alex Light5098a612018-11-29 17:12:15 -08001586 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001587 android.InitDefaultableModule(module)
1588 return module
1589}
Jiyong Park30ca9372019-02-07 16:27:23 +09001590
1591//
1592// Defaults
1593//
1594type Defaults struct {
1595 android.ModuleBase
1596 android.DefaultsModuleBase
1597}
1598
Jiyong Park30ca9372019-02-07 16:27:23 +09001599func defaultsFactory() android.Module {
1600 return DefaultsFactory()
1601}
1602
1603func DefaultsFactory(props ...interface{}) android.Module {
1604 module := &Defaults{}
1605
1606 module.AddProperties(props...)
1607 module.AddProperties(
1608 &apexBundleProperties{},
1609 &apexTargetBundleProperties{},
1610 )
1611
1612 android.InitDefaultsModule(module)
1613 return module
1614}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001615
1616//
1617// Prebuilt APEX
1618//
1619type Prebuilt struct {
1620 android.ModuleBase
1621 prebuilt android.Prebuilt
1622
1623 properties PrebuiltProperties
1624
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001625 inputApex android.Path
1626 installDir android.OutputPath
1627 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001628 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001629}
1630
1631type PrebuiltProperties struct {
1632 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001633 Source string `blueprint:"mutated"`
1634 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001635
1636 Src *string
1637 Arch struct {
1638 Arm struct {
1639 Src *string
1640 }
1641 Arm64 struct {
1642 Src *string
1643 }
1644 X86 struct {
1645 Src *string
1646 }
1647 X86_64 struct {
1648 Src *string
1649 }
1650 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001651
1652 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001653 // Optional name for the installed apex. If unspecified, name of the
1654 // module is used as the file name
1655 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001656
1657 // Names of modules to be overridden. Listed modules can only be other binaries
1658 // (in Make or Soong).
1659 // This does not completely prevent installation of the overridden binaries, but if both
1660 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1661 // from PRODUCT_PACKAGES.
1662 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001663}
1664
1665func (p *Prebuilt) installable() bool {
1666 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001667}
1668
1669func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001670 // If the device is configured to use flattened APEX, force disable the prebuilt because
1671 // the prebuilt is a non-flattened one.
1672 forceDisable := ctx.Config().FlattenApex()
1673
1674 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1675 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001676 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001677
Kun Niu10c9f832019-07-29 16:28:57 -07001678 // Force disable the prebuilts when coverage is enabled.
1679 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1680 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1681
Jiyong Park50b81e52019-07-11 11:24:41 +09001682 // b/137216042 don't use prebuilts when address sanitizer is on
1683 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1684 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1685
1686 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001687 p.properties.ForceDisable = true
1688 return
1689 }
1690
Jiyong Parkc95714e2019-03-29 14:23:10 +09001691 // This is called before prebuilt_select and prebuilt_postdeps mutators
1692 // The mutators requires that src to be set correctly for each arch so that
1693 // arch variants are disabled when src is not provided for the arch.
1694 if len(ctx.MultiTargets()) != 1 {
1695 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1696 return
1697 }
1698 var src string
1699 switch ctx.MultiTargets()[0].Arch.ArchType {
1700 case android.Arm:
1701 src = String(p.properties.Arch.Arm.Src)
1702 case android.Arm64:
1703 src = String(p.properties.Arch.Arm64.Src)
1704 case android.X86:
1705 src = String(p.properties.Arch.X86.Src)
1706 case android.X86_64:
1707 src = String(p.properties.Arch.X86_64.Src)
1708 default:
1709 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1710 return
1711 }
1712 if src == "" {
1713 src = String(p.properties.Src)
1714 }
1715 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001716}
1717
Jiyong Park03b68dd2019-07-26 23:20:40 +09001718func (p *Prebuilt) isForceDisabled() bool {
1719 return p.properties.ForceDisable
1720}
1721
Colin Cross41955e82019-05-29 14:40:35 -07001722func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1723 switch tag {
1724 case "":
1725 return android.Paths{p.outputApex}, nil
1726 default:
1727 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1728 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001729}
1730
Jiyong Park4d277042019-04-23 18:00:10 +09001731func (p *Prebuilt) InstallFilename() string {
1732 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1733}
1734
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001735func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001736 if p.properties.ForceDisable {
1737 return
1738 }
1739
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001740 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001741 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001742 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001743 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001744 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1745 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1746 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001747 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1748 ctx.Build(pctx, android.BuildParams{
1749 Rule: android.Cp,
1750 Input: p.inputApex,
1751 Output: p.outputApex,
1752 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001753 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001754 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001755 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001756}
1757
1758func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1759 return &p.prebuilt
1760}
1761
1762func (p *Prebuilt) Name() string {
1763 return p.prebuilt.Name(p.ModuleBase.Name())
1764}
1765
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001766func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1767 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001768 Class: "ETC",
1769 OutputFile: android.OptionalPathForPath(p.inputApex),
1770 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001771 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1772 func(entries *android.AndroidMkEntries) {
1773 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1774 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1775 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1776 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1777 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001778 },
1779 }
1780}
1781
1782// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1783func PrebuiltFactory() android.Module {
1784 module := &Prebuilt{}
1785 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001786 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001787 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001788 return module
1789}