blob: 1b5cbe5d9baeabf00c777f2c230d9aa4aeac2958 [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()
Sundong Ahne9b55722019-09-06 17:37:42 +0900194 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
Jooyung Han5c998b92019-06-27 11:30:33 +0900195 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900196 })
197}
198
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900199// Mark the direct and transitive dependencies of apex bundles so that they
200// can be built for the apex bundles.
201func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800202 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800203 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900204 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900205 depName := mctx.OtherModuleName(child)
206 // If the parent is apexBundle, this child is directly depended.
207 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800208 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800209 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
210 // non-installable apex's cannot be installed and so should not prevent libraries from being
211 // installed to the system.
212 android.UpdateApexDependency(apexBundleName, depName, directDep)
213 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900214
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900215 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900216 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900217 return true
218 } else {
219 return false
220 }
221 })
222 }
223}
224
225// Create apex variations if a module is included in APEX(s).
226func apexMutator(mctx android.BottomUpMutatorContext) {
227 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900228 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900229 } else if _, ok := mctx.Module().(*apexBundle); ok {
230 // apex bundle itself is mutated so that it and its modules have same
231 // apex variant.
232 apexBundleName := mctx.ModuleName()
233 mctx.CreateVariations(apexBundleName)
234 }
235}
Sundong Ahne9b55722019-09-06 17:37:42 +0900236
237func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
238 if _, ok := mctx.Module().(*apexBundle); ok {
239 if !mctx.Config().FlattenApex() || mctx.Config().UnbundledBuild() {
240 modules := mctx.CreateLocalVariations("", "flattened")
241 modules[0].(*apexBundle).SetFlattened(false)
242 modules[1].(*apexBundle).SetFlattened(true)
243 }
244 }
245}
246
Jooyung Han5c998b92019-06-27 11:30:33 +0900247func apexUsesMutator(mctx android.BottomUpMutatorContext) {
248 if ab, ok := mctx.Module().(*apexBundle); ok {
249 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
250 }
251}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900252
Alex Light9670d332019-01-29 18:07:33 -0800253type apexNativeDependencies struct {
254 // List of native libraries
255 Native_shared_libs []string
256 // List of native executables
257 Binaries []string
Roland Levillain630846d2019-06-26 12:48:34 +0100258 // List of native tests
259 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800260}
261type apexMultilibProperties struct {
262 // Native dependencies whose compile_multilib is "first"
263 First apexNativeDependencies
264
265 // Native dependencies whose compile_multilib is "both"
266 Both apexNativeDependencies
267
268 // Native dependencies whose compile_multilib is "prefer32"
269 Prefer32 apexNativeDependencies
270
271 // Native dependencies whose compile_multilib is "32"
272 Lib32 apexNativeDependencies
273
274 // Native dependencies whose compile_multilib is "64"
275 Lib64 apexNativeDependencies
276}
277
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900278type apexBundleProperties struct {
279 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000280 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800281 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900282
Jiyong Park40e26a22019-02-08 02:53:06 +0900283 // AndroidManifest.xml file used for the zip container of this APEX bundle.
284 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800285 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900286
Jiyong Park05e70dd2019-03-18 14:26:32 +0900287 // Canonical name of the APEX bundle in the manifest file.
288 // If unspecified, defaults to the value of name
289 Apex_name *string
290
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900291 // Determines the file contexts file for setting security context to each file in this APEX bundle.
292 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
293 // used.
294 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900295 File_contexts *string
296
297 // List of native shared libs that are embedded inside this APEX bundle
298 Native_shared_libs []string
299
Roland Levillain630846d2019-06-26 12:48:34 +0100300 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900301 Binaries []string
302
303 // List of java libraries that are embedded inside this APEX bundle
304 Java_libs []string
305
306 // List of prebuilt files that are embedded inside this APEX bundle
307 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900308
Roland Levillain630846d2019-06-26 12:48:34 +0100309 // List of tests that are embedded inside this APEX bundle
310 Tests []string
311
Jiyong Parkff1458f2018-10-12 21:49:38 +0900312 // Name of the apex_key module that provides the private key to sign APEX
313 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900314
Alex Light5098a612018-11-29 17:12:15 -0800315 // The type of APEX to build. Controls what the APEX payload is. Either
316 // 'image', 'zip' or 'both'. Default: 'image'.
317 Payload_type *string
318
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900319 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
320 // or an android_app_certificate module name in the form ":module".
321 Certificate *string
322
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900323 // Whether this APEX is installable to one of the partitions. Default: true.
324 Installable *bool
325
Jiyong Parkda6eb592018-12-19 17:12:36 +0900326 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
327 // Default is false.
328 Use_vendor *bool
329
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800330 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
331 Ignore_system_library_special_case *bool
332
Alex Light9670d332019-01-29 18:07:33 -0800333 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900334
Jiyong Parkf97782b2019-02-13 20:28:58 +0900335 // List of sanitizer names that this APEX is enabled for
336 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900337
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900338 PreventInstall bool `blueprint:"mutated"`
339
340 HideFromMake bool `blueprint:"mutated"`
341
Jooyung Han5c998b92019-06-27 11:30:33 +0900342 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
343 Provide_cpp_shared_libs *bool
344
345 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
346 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100347
348 // A txt file containing list of files that are whitelisted to be included in this APEX.
349 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900350
351 // List of APKs to package inside APEX
352 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900353
354 // To distinguish between flattened and non-flattened variants.
355 // if set true, then this variant is flattened variant.
356 Flattened bool `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800357}
358
359type apexTargetBundleProperties struct {
360 Target struct {
361 // Multilib properties only for android.
362 Android struct {
363 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900364 }
Alex Light9670d332019-01-29 18:07:33 -0800365 // Multilib properties only for host.
366 Host struct {
367 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900368 }
Alex Light9670d332019-01-29 18:07:33 -0800369 // Multilib properties only for host linux_bionic.
370 Linux_bionic struct {
371 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900372 }
Alex Light9670d332019-01-29 18:07:33 -0800373 // Multilib properties only for host linux_glibc.
374 Linux_glibc struct {
375 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900376 }
377 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900378}
379
Jiyong Park8fd61922018-11-08 02:50:25 +0900380type apexFileClass int
381
382const (
383 etc apexFileClass = iota
384 nativeSharedLib
385 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900386 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800387 pyBinary
388 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900389 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100390 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900391 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900392)
393
Alex Light5098a612018-11-29 17:12:15 -0800394type apexPackaging int
395
396const (
397 imageApex apexPackaging = iota
398 zipApex
399 both
400)
401
402func (a apexPackaging) image() bool {
403 switch a {
404 case imageApex, both:
405 return true
406 }
407 return false
408}
409
410func (a apexPackaging) zip() bool {
411 switch a {
412 case zipApex, both:
413 return true
414 }
415 return false
416}
417
418func (a apexPackaging) suffix() string {
419 switch a {
420 case imageApex:
421 return imageApexSuffix
422 case zipApex:
423 return zipApexSuffix
424 case both:
425 panic(fmt.Errorf("must be either zip or image"))
426 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100427 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800428 }
429}
430
431func (a apexPackaging) name() string {
432 switch a {
433 case imageApex:
434 return imageApexType
435 case zipApex:
436 return zipApexType
437 case both:
438 panic(fmt.Errorf("must be either zip or image"))
439 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100440 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800441 }
442}
443
Jiyong Park8fd61922018-11-08 02:50:25 +0900444func (class apexFileClass) NameInMake() string {
445 switch class {
446 case etc:
447 return "ETC"
448 case nativeSharedLib:
449 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800450 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900451 return "EXECUTABLES"
452 case javaSharedLib:
453 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100454 case nativeTest:
455 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900456 case app:
457 return "APPS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900458 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100459 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900460 }
461}
462
463type apexFile struct {
464 builtFile android.Path
465 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900466 installDir string
467 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900468 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800469 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900470}
471
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900472type apexBundle struct {
473 android.ModuleBase
474 android.DefaultableModuleBase
475
Alex Light9670d332019-01-29 18:07:33 -0800476 properties apexBundleProperties
477 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900478
Alex Light5098a612018-11-29 17:12:15 -0800479 apexTypes apexPackaging
480
Colin Crossa4925902018-11-16 11:36:28 -0800481 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800482 outputFiles map[apexPackaging]android.WritablePath
Roland Levillain935639d2019-08-13 14:55:28 +0100483 flattenedOutput android.OutputPath
Colin Crossa4925902018-11-16 11:36:28 -0800484 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900485
Jiyong Park03b68dd2019-07-26 23:20:40 +0900486 prebuiltFileToDelete string
487
Jiyong Park42cca6c2019-04-01 11:15:50 +0900488 public_key_file android.Path
489 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900490
491 container_certificate_file android.Path
492 container_private_key_file android.Path
493
Jiyong Park8fd61922018-11-08 02:50:25 +0900494 // list of files to be included in this apex
495 filesInfo []apexFile
496
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900497 // list of module names that this APEX is depending on
498 externalDeps []string
499
Alex Light0851b882019-02-07 13:20:53 -0800500 testApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900501
502 // intermediate path for apex_manifest.json
503 manifestOut android.WritablePath
Sundong Ahne9b55722019-09-06 17:37:42 +0900504
505 // A config value of (TARGET_FLATTEN_APEX && !TARGET_BUILD_APPS)
506 flattenedConfigValue bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900507}
508
Jiyong Park397e55e2018-10-24 21:09:55 +0900509func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100510 native_shared_libs []string, binaries []string, tests []string,
511 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900512 // Use *FarVariation* to be able to depend on modules having
513 // conflicting variations with this module. This is required since
514 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
515 // for native shared libs.
516 ctx.AddFarVariationDependencies([]blueprint.Variation{
517 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900518 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900519 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900520 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900521 }, sharedLibTag, native_shared_libs...)
522
523 ctx.AddFarVariationDependencies([]blueprint.Variation{
524 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900525 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900526 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100527
528 ctx.AddFarVariationDependencies([]blueprint.Variation{
529 {Mutator: "arch", Variation: arch},
530 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100531 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100532 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900533}
534
Alex Light9670d332019-01-29 18:07:33 -0800535func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
536 if ctx.Os().Class == android.Device {
537 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
538 } else {
539 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
540 if ctx.Os().Bionic() {
541 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
542 } else {
543 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
544 }
545 }
546}
547
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900548func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800549
Jiyong Park397e55e2018-10-24 21:09:55 +0900550 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900551 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800552
553 a.combineProperties(ctx)
554
Jiyong Park397e55e2018-10-24 21:09:55 +0900555 has32BitTarget := false
556 for _, target := range targets {
557 if target.Arch.ArchType.Multilib == "lib32" {
558 has32BitTarget = true
559 }
560 }
561 for i, target := range targets {
562 // When multilib.* is omitted for native_shared_libs, it implies
563 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900564 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900565 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900566 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900567 {Mutator: "link", Variation: "shared"},
568 }, sharedLibTag, a.properties.Native_shared_libs...)
569
Roland Levillain630846d2019-06-26 12:48:34 +0100570 // When multilib.* is omitted for tests, it implies
571 // multilib.both.
572 ctx.AddFarVariationDependencies([]blueprint.Variation{
573 {Mutator: "arch", Variation: target.String()},
574 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100575 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100576 }, testTag, a.properties.Tests...)
577
Jiyong Park397e55e2018-10-24 21:09:55 +0900578 // Add native modules targetting both ABIs
579 addDependenciesForNativeModules(ctx,
580 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100581 a.properties.Multilib.Both.Binaries,
582 a.properties.Multilib.Both.Tests,
583 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900584 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900585
Alex Light3d673592019-01-18 14:37:31 -0800586 isPrimaryAbi := i == 0
587 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900588 // When multilib.* is omitted for binaries, it implies
589 // multilib.first.
590 ctx.AddFarVariationDependencies([]blueprint.Variation{
591 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900592 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900593 }, executableTag, a.properties.Binaries...)
594
595 // Add native modules targetting the first ABI
596 addDependenciesForNativeModules(ctx,
597 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100598 a.properties.Multilib.First.Binaries,
599 a.properties.Multilib.First.Tests,
600 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900601 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800602
603 // When multilib.* is omitted for prebuilts, it implies multilib.first.
604 ctx.AddFarVariationDependencies([]blueprint.Variation{
605 {Mutator: "arch", Variation: target.String()},
606 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900607 }
608
609 switch target.Arch.ArchType.Multilib {
610 case "lib32":
611 // Add native modules targetting 32-bit ABI
612 addDependenciesForNativeModules(ctx,
613 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100614 a.properties.Multilib.Lib32.Binaries,
615 a.properties.Multilib.Lib32.Tests,
616 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900617 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900618
619 addDependenciesForNativeModules(ctx,
620 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100621 a.properties.Multilib.Prefer32.Binaries,
622 a.properties.Multilib.Prefer32.Tests,
623 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900624 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900625 case "lib64":
626 // Add native modules targetting 64-bit ABI
627 addDependenciesForNativeModules(ctx,
628 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100629 a.properties.Multilib.Lib64.Binaries,
630 a.properties.Multilib.Lib64.Tests,
631 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900632 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900633
634 if !has32BitTarget {
635 addDependenciesForNativeModules(ctx,
636 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100637 a.properties.Multilib.Prefer32.Binaries,
638 a.properties.Multilib.Prefer32.Tests,
639 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900640 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900641 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700642
643 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
644 for _, sanitizer := range ctx.Config().SanitizeDevice() {
645 if sanitizer == "hwaddress" {
646 addDependenciesForNativeModules(ctx,
647 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100648 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700649 break
650 }
651 }
652 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900653 }
654
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900655 }
656
Jiyong Parkff1458f2018-10-12 21:49:38 +0900657 ctx.AddFarVariationDependencies([]blueprint.Variation{
658 {Mutator: "arch", Variation: "android_common"},
659 }, javaLibTag, a.properties.Java_libs...)
660
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900661 ctx.AddFarVariationDependencies([]blueprint.Variation{
662 {Mutator: "arch", Variation: "android_common"},
663 }, androidAppTag, a.properties.Apps...)
664
Jiyong Park23c52b02019-02-02 13:13:47 +0900665 if String(a.properties.Key) == "" {
666 ctx.ModuleErrorf("key is missing")
667 return
668 }
669 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900670
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900671 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900672 if cert != "" {
673 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900674 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900675}
676
Colin Cross0ea8ba82019-06-06 14:33:29 -0700677func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900678 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
679 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000680 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900681 }
682 return String(a.properties.Certificate)
683}
684
Colin Cross41955e82019-05-29 14:40:35 -0700685func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
686 switch tag {
687 case "":
688 if file, ok := a.outputFiles[imageApex]; ok {
689 return android.Paths{file}, nil
690 } else {
691 return nil, nil
692 }
Roland Levillain935639d2019-08-13 14:55:28 +0100693 case ".flattened":
Sundong Ahne9b55722019-09-06 17:37:42 +0900694 if a.properties.Flattened {
Roland Levillain935639d2019-08-13 14:55:28 +0100695 flattenedApexPath := a.flattenedOutput
696 return android.Paths{flattenedApexPath}, nil
697 } else {
698 return nil, nil
699 }
Colin Cross41955e82019-05-29 14:40:35 -0700700 default:
701 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900702 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900703}
704
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900705func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900706 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900707}
708
Jiyong Park7c1dc612019-01-05 11:15:24 +0900709func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
710 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900711 return "vendor"
712 } else {
713 return "core"
714 }
715}
716
Jiyong Parkf97782b2019-02-13 20:28:58 +0900717func (a *apexBundle) EnableSanitizer(sanitizerName string) {
718 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
719 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
720 }
721}
722
Jiyong Park388ef3f2019-01-28 19:47:32 +0900723func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900724 if android.InList(sanitizerName, a.properties.SanitizerNames) {
725 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900726 }
727
728 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900729 globalSanitizerNames := []string{}
730 if a.Host() {
731 globalSanitizerNames = ctx.Config().SanitizeHost()
732 } else {
733 arches := ctx.Config().SanitizeDeviceArch()
734 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
735 globalSanitizerNames = ctx.Config().SanitizeDevice()
736 }
737 }
738 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900739}
740
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900741func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
742 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
743}
744
745func (a *apexBundle) PreventInstall() {
746 a.properties.PreventInstall = true
747}
748
749func (a *apexBundle) HideFromMake() {
750 a.properties.HideFromMake = true
751}
752
Sundong Ahne9b55722019-09-06 17:37:42 +0900753func (a *apexBundle) SetFlattened(flattened bool) {
754 a.properties.Flattened = flattened
755}
756
Martin Stjernholm279de572019-09-10 23:18:20 +0100757func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900758 // Decide the APEX-local directory by the multilib of the library
759 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100760 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900761 case "lib32":
762 dirInApex = "lib"
763 case "lib64":
764 dirInApex = "lib64"
765 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100766 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
767 if !ccMod.Arch().Native {
768 dirInApex = filepath.Join(dirInApex, ccMod.Arch().ArchType.String())
769 } else if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
770 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900771 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100772 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
773 // Special case for Bionic libs and other libs installed with them. This is
774 // to prevent those libs from being included in the search path
775 // /apex/com.android.runtime/${LIB}. This exclusion is required because
776 // those libs in the Runtime APEX are available via the legacy paths in
777 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
778 // to the legacy paths and thus will be loaded into the default linker
779 // namespace (aka "platform" namespace). If the libs are directly in
780 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
781 // into the runtime linker namespace, which will result in double loading of
782 // them, which isn't supported.
783 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900784 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900785
Martin Stjernholm279de572019-09-10 23:18:20 +0100786 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900787 return
788}
789
790func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900791 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200792 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900793 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200794 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
795 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900796 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900797 fileToCopy = cc.OutputFile().Path()
798 return
799}
800
Alex Light778127a2019-02-27 14:19:50 -0800801func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
802 dirInApex = "bin"
803 fileToCopy = py.HostToolPath().Path()
804 return
805}
806func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
807 dirInApex = "bin"
808 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
809 if err != nil {
810 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
811 return
812 }
813 fileToCopy = android.PathForOutput(ctx, s)
814 return
815}
816
Jiyong Park04480cf2019-02-06 00:16:29 +0900817func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
818 dirInApex = filepath.Join("bin", sh.SubDir())
819 fileToCopy = sh.OutputFile()
820 return
821}
822
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900823func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
824 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900825 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900826 return
827}
828
Jiyong Park9e6c2422019-08-09 20:39:45 +0900829func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
830 dirInApex = "javalib"
831 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
832 implJars := java.ImplementationJars()
833 if len(implJars) != 1 {
834 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
835 strings.Join(implJars.Strings(), ", ")))
836 }
837 fileToCopy = implJars[0]
838 return
839}
840
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900841func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
842 dirInApex = filepath.Join("etc", prebuilt.SubDir())
843 fileToCopy = prebuilt.OutputFile()
844 return
845}
846
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900847func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
848 dirInApex = filepath.Join("app", pkgName)
849 fileToCopy = app.OutputFile()
850 return
851}
852
Roland Levillain935639d2019-08-13 14:55:28 +0100853// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
854type flattenedApexContext struct {
855 android.ModuleContext
856}
857
858func (c *flattenedApexContext) InstallBypassMake() bool {
859 return true
860}
861
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900862func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900863 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900864
Alex Light5098a612018-11-29 17:12:15 -0800865 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
866 a.apexTypes = imageApex
867 } else if *a.properties.Payload_type == "zip" {
868 a.apexTypes = zipApex
869 } else if *a.properties.Payload_type == "both" {
870 a.apexTypes = both
871 } else {
872 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
873 return
874 }
875
Roland Levillain630846d2019-06-26 12:48:34 +0100876 if len(a.properties.Tests) > 0 && !a.testApex {
877 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
878 return
879 }
880
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800881 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
882
Jooyung Hane1633032019-08-01 17:41:43 +0900883 // native lib dependencies
884 var provideNativeLibs []string
885 var requireNativeLibs []string
886
Jooyung Han5c998b92019-06-27 11:30:33 +0900887 // Check if "uses" requirements are met with dependent apexBundles
888 var providedNativeSharedLibs []string
889 useVendor := proptools.Bool(a.properties.Use_vendor)
890 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
891 if ctx.OtherModuleDependencyTag(m) != usesTag {
892 return
893 }
894 otherName := ctx.OtherModuleName(m)
895 other, ok := m.(*apexBundle)
896 if !ok {
897 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
898 return
899 }
900 if proptools.Bool(other.properties.Use_vendor) != useVendor {
901 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
902 return
903 }
904 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
905 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
906 return
907 }
908 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
909 })
910
Alex Light778127a2019-02-27 14:19:50 -0800911 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100912 depTag := ctx.OtherModuleDependencyTag(child)
913 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900914 if _, ok := parent.(*apexBundle); ok {
915 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900916 switch depTag {
917 case sharedLibTag:
918 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900919 if cc.HasStubsVariants() {
920 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
921 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100922 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900923 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900924 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900925 } else {
926 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900927 }
928 case executableTag:
929 if cc, ok := child.(*cc.Module); ok {
930 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900931 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900932 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900933 } else if sh, ok := child.(*android.ShBinary); ok {
934 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
935 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800936 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
937 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
938 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
939 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
940 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
941 // NB: Since go binaries are static we don't need the module for anything here, which is
942 // good since the go tool is a blueprint.Module not an android.Module like we would
943 // normally use.
944 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900945 } else {
Alex Light778127a2019-02-27 14:19:50 -0800946 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 +0900947 }
948 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +0900949 if javaLib, ok := child.(*java.Library); ok {
950 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +0900951 if fileToCopy == nil {
952 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
953 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900954 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
955 }
956 return true
957 } else if javaLib, ok := child.(*java.Import); ok {
958 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
959 if fileToCopy == nil {
960 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
961 } else {
962 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900963 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900964 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900965 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900966 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900967 }
968 case prebuiltTag:
969 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
970 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900971 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900972 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900973 } else {
974 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
975 }
Roland Levillain630846d2019-06-26 12:48:34 +0100976 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100977 if ccTest, ok := child.(*cc.Module); ok {
978 if ccTest.IsTestPerSrcAllTestsVariation() {
979 // Multiple-output test module (where `test_per_src: true`).
980 //
981 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
982 // We do not add this variation to `filesInfo`, as it has no output;
983 // however, we do add the other variations of this module as indirect
984 // dependencies (see below).
985 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100986 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100987 // Single-output test module (where `test_per_src: false`).
988 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
989 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100990 }
Roland Levillain630846d2019-06-26 12:48:34 +0100991 return true
992 } else {
993 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
994 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900995 case keyTag:
996 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900997 a.private_key_file = key.private_key_file
998 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900999 return false
1000 } else {
1001 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001002 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001003 case certificateTag:
1004 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001005 a.container_certificate_file = dep.Certificate.Pem
1006 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001007 return false
1008 } else {
1009 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1010 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001011 case android.PrebuiltDepTag:
1012 // If the prebuilt is force disabled, remember to delete the prebuilt file
1013 // that might have been installed in the previous builds
1014 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1015 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1016 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001017 case androidAppTag:
1018 if ap, ok := child.(*java.AndroidApp); ok {
1019 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1020 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1021 return true
1022 } else {
1023 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1024 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001025 }
1026 } else {
1027 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001028 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001029 // We cannot use a switch statement on `depTag` here as the checked
1030 // tags used below are private (e.g. `cc.sharedDepTag`).
1031 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1032 if cc, ok := child.(*cc.Module); ok {
1033 if android.InList(cc.Name(), providedNativeSharedLibs) {
1034 // If we're using a shared library which is provided from other APEX,
1035 // don't include it in this APEX
1036 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001037 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001038 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1039 // If the dependency is a stubs lib, don't include it in this APEX,
1040 // but make sure that the lib is installed on the device.
1041 // In case no APEX is having the lib, the lib is installed to the system
1042 // partition.
1043 //
1044 // Always include if we are a host-apex however since those won't have any
1045 // system libraries.
1046 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1047 a.externalDeps = append(a.externalDeps, cc.Name())
1048 }
Jooyung Hane1633032019-08-01 17:41:43 +09001049 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001050 // Don't track further
1051 return false
1052 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001053 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001054 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1055 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001056 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001057 } else if cc.IsTestPerSrcDepTag(depTag) {
1058 if cc, ok := child.(*cc.Module); ok {
1059 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1060 // Handle modules created as `test_per_src` variations of a single test module:
1061 // use the name of the generated test binary (`fileToCopy`) instead of the name
1062 // of the original test module (`depName`, shared by all `test_per_src`
1063 // variations of that module).
1064 moduleName := filepath.Base(fileToCopy.String())
1065 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1066 return true
1067 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001068 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001069 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Sundong Ahn2db7f462019-08-27 18:53:12 +09001070 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
1071 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001072 }
1073 }
1074 }
1075 return false
1076 })
1077
Sundong Ahne9b55722019-09-06 17:37:42 +09001078 a.flattenedConfigValue = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1079 if a.flattenedConfigValue {
1080 a.properties.Flattened = true
1081 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001082 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001083 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1084 return
1085 }
1086
Jiyong Park8fd61922018-11-08 02:50:25 +09001087 // remove duplicates in filesInfo
1088 removeDup := func(filesInfo []apexFile) []apexFile {
1089 encountered := make(map[android.Path]bool)
1090 result := []apexFile{}
1091 for _, f := range filesInfo {
1092 if !encountered[f.builtFile] {
1093 encountered[f.builtFile] = true
1094 result = append(result, f)
1095 }
1096 }
1097 return result
1098 }
1099 filesInfo = removeDup(filesInfo)
1100
1101 // to have consistent build rules
1102 sort.Slice(filesInfo, func(i, j int) bool {
1103 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1104 })
1105
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001106 // check no_apex modules
1107 whitelist := whitelistNoApex[ctx.ModuleName()]
1108 for i := range filesInfo {
1109 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1110 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1111 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1112 }
1113 }
1114 }
1115
Jiyong Park8fd61922018-11-08 02:50:25 +09001116 // prepend the name of this APEX to the module names. These names will be the names of
1117 // modules that will be defined if the APEX is flattened.
1118 for i := range filesInfo {
1119 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1120 }
1121
Jiyong Park8fd61922018-11-08 02:50:25 +09001122 a.installDir = android.PathForModuleInstall(ctx, "apex")
1123 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001124
Jooyung Hane1633032019-08-01 17:41:43 +09001125 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1126 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1127 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1128 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1129 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1130 ctx.Build(pctx, android.BuildParams{
1131 Rule: injectApexDependency,
1132 Input: manifestSrc,
1133 Output: a.manifestOut,
1134 Args: map[string]string{
1135 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1136 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1137 },
1138 })
1139
Roland Levillain935639d2019-08-13 14:55:28 +01001140 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1141 // reply true to `InstallBypassMake()` (thus making the call
1142 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1143 // instead of `android.PathForOutput`) to return the correct path to the flattened
1144 // APEX (as its contents is installed by Make, not Soong).
1145 factx := flattenedApexContext{ctx}
1146 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1147
Alex Light5098a612018-11-29 17:12:15 -08001148 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001149 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001150 }
1151 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001152 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001153 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001154 // in other modules. It is in AndroidMk where the selection of flattened
1155 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001156 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001157 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001158 }
1159}
1160
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001161func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001162 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001163 for _, f := range a.filesInfo {
1164 if f.module != nil {
1165 notice := f.module.NoticeFile()
1166 if notice.Valid() {
1167 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001168 }
1169 }
1170 }
1171 // append the notice file specified in the apex module itself
1172 if a.NoticeFile().Valid() {
1173 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001174 }
1175
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001176 if len(noticeFiles) == 0 {
1177 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001178 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001179
Jaewoong Jung98772792019-07-01 17:15:13 -07001180 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001181}
1182
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001183func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001184 cert := String(a.properties.Certificate)
1185 if cert != "" && android.SrcIsModule(cert) == "" {
1186 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001187 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1188 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001189 } else if cert == "" {
1190 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001191 a.container_certificate_file = pem
1192 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001193 }
1194
Alex Light5098a612018-11-29 17:12:15 -08001195 var abis []string
1196 for _, target := range ctx.MultiTargets() {
1197 if len(target.Arch.Abi) > 0 {
1198 abis = append(abis, target.Arch.Abi[0])
1199 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001200 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001201
Alex Light5098a612018-11-29 17:12:15 -08001202 abis = android.FirstUniqueStrings(abis)
1203
1204 suffix := apexType.suffix()
1205 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001206
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001207 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001208 for _, f := range a.filesInfo {
1209 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001210 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001211
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001212 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001213 emitCommands := []string{}
1214 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1215 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001216 for i, src := range filesToCopy {
1217 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001218 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001219 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001220 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1221 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001222 for _, sym := range a.filesInfo[i].symlinks {
1223 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1224 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1225 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001226 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001227 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001228 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001229
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001230 if a.properties.Whitelisted_files != nil {
1231 ctx.Build(pctx, android.BuildParams{
1232 Rule: emitApexContentRule,
1233 Implicits: implicitInputs,
1234 Output: imageContentFile,
1235 Description: "emit apex image content",
1236 Args: map[string]string{
1237 "emit_commands": strings.Join(emitCommands, " && "),
1238 },
1239 })
1240 implicitInputs = append(implicitInputs, imageContentFile)
1241 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1242
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001243 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001244 ctx.Build(pctx, android.BuildParams{
1245 Rule: diffApexContentRule,
1246 Implicits: implicitInputs,
1247 Output: phonyOutput,
1248 Description: "diff apex image content",
1249 Args: map[string]string{
1250 "whitelisted_files_file": whitelistedFilesFile.String(),
1251 "image_content_file": imageContentFile.String(),
1252 "apex_module_name": ctx.ModuleName(),
1253 },
1254 })
1255
1256 implicitInputs = append(implicitInputs, phonyOutput)
1257 }
1258
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001259 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1260 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001261
Alex Light5098a612018-11-29 17:12:15 -08001262 if apexType.image() {
1263 // files and dirs that will be created in APEX
1264 var readOnlyPaths []string
1265 var executablePaths []string // this also includes dirs
1266 for _, f := range a.filesInfo {
1267 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001268 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001269 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001270 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001271 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001272 }
Alex Light5098a612018-11-29 17:12:15 -08001273 } else {
1274 readOnlyPaths = append(readOnlyPaths, pathInApex)
1275 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001276 dir := f.installDir
1277 for !android.InList(dir, executablePaths) && dir != "" {
1278 executablePaths = append(executablePaths, dir)
1279 dir, _ = filepath.Split(dir) // move up to the parent
1280 if len(dir) > 0 {
1281 // remove trailing slash
1282 dir = dir[:len(dir)-1]
1283 }
Alex Light5098a612018-11-29 17:12:15 -08001284 }
1285 }
1286 sort.Strings(readOnlyPaths)
1287 sort.Strings(executablePaths)
1288 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1289 ctx.Build(pctx, android.BuildParams{
1290 Rule: generateFsConfig,
1291 Output: cannedFsConfig,
1292 Description: "generate fs config",
1293 Args: map[string]string{
1294 "ro_paths": strings.Join(readOnlyPaths, " "),
1295 "exec_paths": strings.Join(executablePaths, " "),
1296 },
1297 })
1298
1299 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1300 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1301 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1302 if !fileContextsOptionalPath.Valid() {
1303 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1304 return
1305 }
1306 fileContexts := fileContextsOptionalPath.Path()
1307
Jiyong Park835d82b2018-12-27 16:04:18 +09001308 optFlags := []string{}
1309
Alex Light5098a612018-11-29 17:12:15 -08001310 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001311 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1312 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001313
Jiyong Park7f67f482019-01-05 12:57:48 +09001314 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1315 if overridden {
1316 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1317 }
1318
Jiyong Park40e26a22019-02-08 02:53:06 +09001319 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001320 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001321 implicitInputs = append(implicitInputs, androidManifestFile)
1322 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1323 }
1324
Jiyong Park71b519d2019-04-18 17:25:49 +09001325 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1326 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1327 ctx.Config().UnbundledBuild() &&
1328 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1329 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1330 apiFingerprint := java.ApiFingerprintPath(ctx)
1331 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1332 implicitInputs = append(implicitInputs, apiFingerprint)
1333 }
1334 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1335
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001336 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1337 if noticeFile.Valid() {
1338 // If there's a NOTICE file, embed it as an asset file in the APEX.
1339 implicitInputs = append(implicitInputs, noticeFile.Path())
1340 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1341 }
1342
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001343 if !ctx.Config().UnbundledBuild() && a.installable() {
1344 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1345 // don't need hashtree for activation. Therefore, by removing hashtree from
1346 // apex bundle (filesystem image in it, to be specific), we can save storage.
1347 optFlags = append(optFlags, "--no_hashtree")
1348 }
1349
Alex Light5098a612018-11-29 17:12:15 -08001350 ctx.Build(pctx, android.BuildParams{
1351 Rule: apexRule,
1352 Implicits: implicitInputs,
1353 Output: unsignedOutputFile,
1354 Description: "apex (" + apexType.name() + ")",
1355 Args: map[string]string{
1356 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1357 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1358 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001359 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001360 "file_contexts": fileContexts.String(),
1361 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001362 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001363 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001364 },
1365 })
1366
1367 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1368 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1369 a.bundleModuleFile = bundleModuleFile
1370
1371 ctx.Build(pctx, android.BuildParams{
1372 Rule: apexProtoConvertRule,
1373 Input: unsignedOutputFile,
1374 Output: apexProtoFile,
1375 Description: "apex proto convert",
1376 })
1377
1378 ctx.Build(pctx, android.BuildParams{
1379 Rule: apexBundleRule,
1380 Input: apexProtoFile,
1381 Output: a.bundleModuleFile,
1382 Description: "apex bundle module",
1383 Args: map[string]string{
1384 "abi": strings.Join(abis, "."),
1385 },
1386 })
1387 } else {
1388 ctx.Build(pctx, android.BuildParams{
1389 Rule: zipApexRule,
1390 Implicits: implicitInputs,
1391 Output: unsignedOutputFile,
1392 Description: "apex (" + apexType.name() + ")",
1393 Args: map[string]string{
1394 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1395 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1396 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001397 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001398 },
1399 })
Colin Crossa4925902018-11-16 11:36:28 -08001400 }
Colin Crossa4925902018-11-16 11:36:28 -08001401
Alex Light5098a612018-11-29 17:12:15 -08001402 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001403 ctx.Build(pctx, android.BuildParams{
1404 Rule: java.Signapk,
1405 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001406 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001407 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001408 Implicits: []android.Path{
1409 a.container_certificate_file,
1410 a.container_private_key_file,
1411 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001412 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001413 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001414 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001415 },
1416 })
Alex Light5098a612018-11-29 17:12:15 -08001417
1418 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahn72f1f3e2019-09-12 22:53:00 +09001419 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && (!a.properties.Flattened || a.flattenedConfigValue) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001420 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001421 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001422}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001423
Jiyong Park8fd61922018-11-08 02:50:25 +09001424func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001425 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001426 // 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 +09001427 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001428 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001429
Jiyong Park42cca6c2019-04-01 11:15:50 +09001430 // rename to apex_pubkey
1431 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1432 ctx.Build(pctx, android.BuildParams{
1433 Rule: android.Cp,
1434 Input: a.public_key_file,
1435 Output: copiedPubkey,
1436 })
1437 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1438
Jiyong Park23c52b02019-02-02 13:13:47 +09001439 if ctx.Config().FlattenApex() {
1440 for _, fi := range a.filesInfo {
1441 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001442 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1443 for _, sym := range fi.symlinks {
1444 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1445 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001446 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001447 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001448 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001449}
1450
1451func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001452 if a.properties.HideFromMake {
1453 return android.AndroidMkData{
1454 Disabled: true,
1455 }
1456 }
Alex Light5098a612018-11-29 17:12:15 -08001457 writers := []android.AndroidMkData{}
1458 if a.apexTypes.image() {
1459 writers = append(writers, a.androidMkForType(imageApex))
1460 }
1461 if a.apexTypes.zip() {
1462 writers = append(writers, a.androidMkForType(zipApex))
1463 }
1464 return android.AndroidMkData{
1465 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1466 for _, data := range writers {
1467 data.Custom(w, name, prefix, moduleDir, data)
1468 }
1469 }}
1470}
1471
Alex Lightf1801bc2019-02-13 11:10:07 -08001472func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001473 moduleNames := []string{}
1474
1475 for _, fi := range a.filesInfo {
1476 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1477 continue
1478 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001479 if a.properties.Flattened && !apexType.image() {
1480 continue
Jiyong Park94427262019-02-05 23:18:47 +09001481 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001482
1483 var suffix string
1484 if a.properties.Flattened && !a.flattenedConfigValue {
1485 suffix = ".flattened"
1486 }
1487
1488 if !android.InList(fi.moduleName, moduleNames) {
1489 moduleNames = append(moduleNames, fi.moduleName+suffix)
1490 }
1491
Jiyong Park94427262019-02-05 23:18:47 +09001492 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1493 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001494 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName+suffix)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001495 // /apex/<name>/{lib|framework|...}
1496 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1497 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001498 if a.properties.Flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001499 // /system/apex/<name>/{lib|framework|...}
1500 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1501 a.installDir.RelPathString(), name, fi.installDir))
Sundong Ahne9b55722019-09-06 17:37:42 +09001502 if a.flattenedConfigValue {
1503 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1504 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001505 if len(fi.symlinks) > 0 {
1506 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1507 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001508
1509 if fi.module != nil && fi.module.NoticeFile().Valid() {
1510 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1511 }
Jiyong Park94427262019-02-05 23:18:47 +09001512 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001513 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001514 }
1515 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1516 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1517 if fi.module != nil {
1518 archStr := fi.module.Target().Arch.ArchType.String()
1519 host := false
1520 switch fi.module.Target().Os.Class {
1521 case android.Host:
1522 if archStr != "common" {
1523 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1524 }
1525 host = true
1526 case android.HostCross:
1527 if archStr != "common" {
1528 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1529 }
1530 host = true
1531 case android.Device:
1532 if archStr != "common" {
1533 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1534 }
1535 }
1536 if host {
1537 makeOs := fi.module.Target().Os.String()
1538 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1539 makeOs = "linux"
1540 }
1541 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1542 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1543 }
1544 }
1545 if fi.class == javaSharedLib {
1546 javaModule := fi.module.(*java.Library)
1547 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1548 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1549 // we will have foo.jar.jar
1550 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1551 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1552 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1553 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1554 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1555 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001556 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001557 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001558 if cc, ok := fi.module.(*cc.Module); ok {
1559 if cc.UnstrippedOutputFile() != nil {
1560 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1561 }
1562 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001563 if cc.CoverageOutputFile().Valid() {
1564 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1565 }
Jiyong Park94427262019-02-05 23:18:47 +09001566 }
1567 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1568 } else {
1569 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1570 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1571 }
1572 }
1573 return moduleNames
1574}
1575
Alex Light5098a612018-11-29 17:12:15 -08001576func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001577 return android.AndroidMkData{
1578 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1579 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001580 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001581 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001582 }
1583
Sundong Ahne9b55722019-09-06 17:37:42 +09001584 if a.properties.Flattened && !a.flattenedConfigValue {
1585 name = name + ".flattened"
1586 }
1587
1588 if a.properties.Flattened && apexType.image() {
Jiyong Park719b4462019-01-13 00:39:51 +09001589 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001590 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1591 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1592 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001593 if len(moduleNames) > 0 {
1594 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1595 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001596 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001597 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1598
Sundong Ahn72f1f3e2019-09-12 22:53:00 +09001599 } else if !a.properties.Flattened || a.flattenedConfigValue {
Alex Light5098a612018-11-29 17:12:15 -08001600 // zip-apex is the less common type so have the name refer to the image-apex
1601 // only and use {name}.zip if you want the zip-apex
1602 if apexType == zipApex && a.apexTypes == both {
1603 name = name + ".zip"
1604 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001605 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1606 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1607 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1608 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001609 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001610 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001611 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001612 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001613 if len(moduleNames) > 0 {
1614 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1615 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001616 if len(a.externalDeps) > 0 {
1617 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1618 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001619 if a.prebuiltFileToDelete != "" {
1620 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1621 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1622 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001623 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001624
Alex Light5098a612018-11-29 17:12:15 -08001625 if apexType == imageApex {
1626 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1627 }
Jiyong Park719b4462019-01-13 00:39:51 +09001628 }
1629 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001630}
1631
Alex Light0851b882019-02-07 13:20:53 -08001632func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001633 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001634}
1635
1636func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001637 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001638}
1639
1640func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001641 module := &apexBundle{
1642 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001643 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001644 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001645 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001646 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001647 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001648 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1649 })
Alex Light5098a612018-11-29 17:12:15 -08001650 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001651 android.InitDefaultableModule(module)
1652 return module
1653}
Jiyong Park30ca9372019-02-07 16:27:23 +09001654
1655//
1656// Defaults
1657//
1658type Defaults struct {
1659 android.ModuleBase
1660 android.DefaultsModuleBase
1661}
1662
Jiyong Park30ca9372019-02-07 16:27:23 +09001663func defaultsFactory() android.Module {
1664 return DefaultsFactory()
1665}
1666
1667func DefaultsFactory(props ...interface{}) android.Module {
1668 module := &Defaults{}
1669
1670 module.AddProperties(props...)
1671 module.AddProperties(
1672 &apexBundleProperties{},
1673 &apexTargetBundleProperties{},
1674 )
1675
1676 android.InitDefaultsModule(module)
1677 return module
1678}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001679
1680//
1681// Prebuilt APEX
1682//
1683type Prebuilt struct {
1684 android.ModuleBase
1685 prebuilt android.Prebuilt
1686
1687 properties PrebuiltProperties
1688
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001689 inputApex android.Path
1690 installDir android.OutputPath
1691 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001692 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001693}
1694
1695type PrebuiltProperties struct {
1696 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001697 Source string `blueprint:"mutated"`
1698 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001699
1700 Src *string
1701 Arch struct {
1702 Arm struct {
1703 Src *string
1704 }
1705 Arm64 struct {
1706 Src *string
1707 }
1708 X86 struct {
1709 Src *string
1710 }
1711 X86_64 struct {
1712 Src *string
1713 }
1714 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001715
1716 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001717 // Optional name for the installed apex. If unspecified, name of the
1718 // module is used as the file name
1719 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001720
1721 // Names of modules to be overridden. Listed modules can only be other binaries
1722 // (in Make or Soong).
1723 // This does not completely prevent installation of the overridden binaries, but if both
1724 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1725 // from PRODUCT_PACKAGES.
1726 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001727}
1728
1729func (p *Prebuilt) installable() bool {
1730 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001731}
1732
1733func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001734 // If the device is configured to use flattened APEX, force disable the prebuilt because
1735 // the prebuilt is a non-flattened one.
1736 forceDisable := ctx.Config().FlattenApex()
1737
1738 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1739 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001740 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001741
Kun Niu10c9f832019-07-29 16:28:57 -07001742 // Force disable the prebuilts when coverage is enabled.
1743 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1744 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1745
Jiyong Park50b81e52019-07-11 11:24:41 +09001746 // b/137216042 don't use prebuilts when address sanitizer is on
1747 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1748 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1749
1750 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001751 p.properties.ForceDisable = true
1752 return
1753 }
1754
Jiyong Parkc95714e2019-03-29 14:23:10 +09001755 // This is called before prebuilt_select and prebuilt_postdeps mutators
1756 // The mutators requires that src to be set correctly for each arch so that
1757 // arch variants are disabled when src is not provided for the arch.
1758 if len(ctx.MultiTargets()) != 1 {
1759 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1760 return
1761 }
1762 var src string
1763 switch ctx.MultiTargets()[0].Arch.ArchType {
1764 case android.Arm:
1765 src = String(p.properties.Arch.Arm.Src)
1766 case android.Arm64:
1767 src = String(p.properties.Arch.Arm64.Src)
1768 case android.X86:
1769 src = String(p.properties.Arch.X86.Src)
1770 case android.X86_64:
1771 src = String(p.properties.Arch.X86_64.Src)
1772 default:
1773 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1774 return
1775 }
1776 if src == "" {
1777 src = String(p.properties.Src)
1778 }
1779 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001780}
1781
Jiyong Park03b68dd2019-07-26 23:20:40 +09001782func (p *Prebuilt) isForceDisabled() bool {
1783 return p.properties.ForceDisable
1784}
1785
Colin Cross41955e82019-05-29 14:40:35 -07001786func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1787 switch tag {
1788 case "":
1789 return android.Paths{p.outputApex}, nil
1790 default:
1791 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1792 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001793}
1794
Jiyong Park4d277042019-04-23 18:00:10 +09001795func (p *Prebuilt) InstallFilename() string {
1796 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1797}
1798
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001799func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001800 if p.properties.ForceDisable {
1801 return
1802 }
1803
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001804 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001805 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001806 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001807 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001808 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1809 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1810 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001811 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1812 ctx.Build(pctx, android.BuildParams{
1813 Rule: android.Cp,
1814 Input: p.inputApex,
1815 Output: p.outputApex,
1816 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001817 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001818 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001819 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001820}
1821
1822func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1823 return &p.prebuilt
1824}
1825
1826func (p *Prebuilt) Name() string {
1827 return p.prebuilt.Name(p.ModuleBase.Name())
1828}
1829
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001830func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1831 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001832 Class: "ETC",
1833 OutputFile: android.OptionalPathForPath(p.inputApex),
1834 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001835 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1836 func(entries *android.AndroidMkEntries) {
1837 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1838 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1839 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1840 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1841 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001842 },
1843 }
1844}
1845
1846// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1847func PrebuiltFactory() android.Module {
1848 module := &Prebuilt{}
1849 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001850 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001851 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001852 return module
1853}