blob: c3f6a74fa4dd137d111a15e35b1da632f0ff18c7 [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"
Jooyung Han344d5432019-08-23 11:17:39 +090024 "sync"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090025
26 "android/soong/android"
27 "android/soong/cc"
28 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080029 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030
31 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080032 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090033 "github.com/google/blueprint/proptools"
34)
35
36var (
37 pctx = android.NewPackageContext("android/apex")
38
39 // Create a canned fs config file where all files and directories are
40 // by default set to (uid/gid/mode) = (1000/1000/0644)
41 // TODO(b/113082813) make this configurable using config.fs syntax
42 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000043 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000044 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090045 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090046 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090047 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090048 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090049
Jooyung Hane1633032019-08-01 17:41:43 +090050 injectApexDependency = pctx.StaticRule("injectApexDependency", blueprint.RuleParams{
51 Command: `rm -f $out && ${jsonmodify} $in ` +
52 `-a provideNativeLibs ${provideNativeLibs} ` +
53 `-a requireNativeLibs ${requireNativeLibs} -o $out`,
54 CommandDeps: []string{"${jsonmodify}"},
55 Description: "Inject dependency into ${out}",
56 }, "provideNativeLibs", "requireNativeLibs")
57
Jiyong Park48ca7dc2018-10-10 14:01:00 +090058 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
59 // against the binary policy using sefcontext_compiler -p <policy>.
60
61 // TODO(b/114327326): automate the generation of file_contexts
62 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
63 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010064 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090065 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090066 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090067 `--file_contexts ${file_contexts} ` +
68 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080069 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090070 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090071 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
72 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000073 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010074 Rspfile: "${out}.copy_commands",
75 RspfileContent: "${copy_commands}",
76 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090077 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080078
Alex Light5098a612018-11-29 17:12:15 -080079 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
80 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010081 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080082 `APEXER_TOOL_PATH=${tool_path} ` +
83 `${apexer} --force --manifest ${manifest} ` +
84 `--payload_type zip ` +
85 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010086 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
87 Rspfile: "${out}.copy_commands",
88 RspfileContent: "${copy_commands}",
89 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080090 }, "tool_path", "image_dir", "copy_commands", "manifest")
91
Colin Crossa4925902018-11-16 11:36:28 -080092 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
93 blueprint.RuleParams{
94 Command: `${aapt2} convert --output-format proto $in -o $out`,
95 CommandDeps: []string{"${aapt2}"},
96 })
97
98 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090099 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +0000100 `apex_payload.img:apex/${abi}.img ` +
101 `apex_manifest.json:root/apex_manifest.json ` +
Jaewoong Jungb00c1fb2019-09-04 13:26:18 -0700102 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
103 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -0800104 CommandDeps: []string{"${zip2zip}"},
105 Description: "app bundle",
106 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100107
108 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
109 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
110 Rspfile: "${out}.emit_commands",
111 RspfileContent: "${emit_commands}",
112 Description: "Emit APEX image content",
113 }, "emit_commands")
114
115 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
116 Command: `diff --unchanged-group-format='' \` +
117 `--changed-group-format='%<' \` +
118 `${image_content_file} ${whitelisted_files_file} || (` +
119 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
120 ` "To fix the build run following command:" && ` +
121 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
122 `exit 1)`,
123 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
124 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900125)
126
Alex Light5098a612018-11-29 17:12:15 -0800127var imageApexSuffix = ".apex"
128var zipApexSuffix = ".zipapex"
129
130var imageApexType = "image"
131var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900132
133type dependencyTag struct {
134 blueprint.BaseDependencyTag
135 name string
136}
137
138var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900139 sharedLibTag = dependencyTag{name: "sharedLib"}
140 executableTag = dependencyTag{name: "executable"}
141 javaLibTag = dependencyTag{name: "javaLib"}
142 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100143 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900144 keyTag = dependencyTag{name: "key"}
145 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900146 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900147 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900148)
149
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900150var (
151 whitelistNoApex = map[string][]string{
152 "apex_test_build_features": []string{"libbinder"},
153 "com.android.neuralnetworks": []string{"libbinder"},
154 "com.android.media": []string{"libbinder"},
155 "com.android.media.swcodec": []string{"libbinder"},
156 "test_com.android.media.swcodec": []string{"libbinder"},
Jooyung Han344d5432019-08-23 11:17:39 +0900157 "com.android.vndk": []string{"libbinder"},
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900158 }
159)
160
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900161func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700162 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900163 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900164 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100165 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
166 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
167 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
168 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000169 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100170 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
171 } else {
172 return pctx.HostBinToolPath(ctx, tool).String()
173 }
174 })
175 }
176 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900177 pctx.HostBinToolVariable("avbtool", "avbtool")
178 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
179 pctx.HostBinToolVariable("merge_zips", "merge_zips")
180 pctx.HostBinToolVariable("mke2fs", "mke2fs")
181 pctx.HostBinToolVariable("resize2fs", "resize2fs")
182 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
183 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800184 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900185 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900186 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900187
Alex Light0851b882019-02-07 13:20:53 -0800188 android.RegisterModuleType("apex", apexBundleFactory)
189 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jooyung Han344d5432019-08-23 11:17:39 +0900190 android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900191 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700192 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900193
Jooyung Han344d5432019-08-23 11:17:39 +0900194 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
195 ctx.TopDown("apex_vndk_gather", apexVndkGatherMutator).Parallel()
196 ctx.BottomUp("apex_vndk_add_deps", apexVndkAddDepsMutator).Parallel()
197 })
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900198 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
199 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700200 ctx.BottomUp("apex", apexMutator).Parallel()
Sundong Ahne9b55722019-09-06 17:37:42 +0900201 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
Jooyung Han5c998b92019-06-27 11:30:33 +0900202 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900203 })
204}
205
Jooyung Han344d5432019-08-23 11:17:39 +0900206var (
207 vndkApexListKey = android.NewOnceKey("vndkApexList")
208 vndkApexListMutex sync.Mutex
209)
210
211func vndkApexList(config android.Config) map[string]*apexBundle {
212 return config.Once(vndkApexListKey, func() interface{} {
213 return map[string]*apexBundle{}
214 }).(map[string]*apexBundle)
215}
216
217// apexVndkGatherMutator gathers "apex_vndk" modules and puts them in a map with vndk_version as a key.
218func apexVndkGatherMutator(mctx android.TopDownMutatorContext) {
219 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
220 if ab.IsNativeBridgeSupported() {
221 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
222 }
223 vndkVersion := proptools.StringDefault(ab.vndkProperties.Vndk_version, mctx.DeviceConfig().PlatformVndkVersion())
224 vndkApexListMutex.Lock()
225 defer vndkApexListMutex.Unlock()
226 vndkApexList := vndkApexList(mctx.Config())
227 if other, ok := vndkApexList[vndkVersion]; ok {
228 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other.Name())
229 }
230 vndkApexList[vndkVersion] = ab
231 }
232}
233
234// apexVndkAddDepsMutator adds (reverse) dependencies from vndk libs to apex_vndk modules.
235// It filters only libs with matching targets.
236func apexVndkAddDepsMutator(mctx android.BottomUpMutatorContext) {
237 if cc, ok := mctx.Module().(*cc.Module); ok && cc.IsVndkOnSystem() {
238 vndkApexList := vndkApexList(mctx.Config())
239 if ab, ok := vndkApexList[cc.VndkVersion()]; ok {
240 targetArch := cc.Target().String()
241 for _, target := range ab.MultiTargets() {
242 if target.String() == targetArch {
243 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, ab.Name())
244 break
245 }
246 }
247 }
248 }
249}
250
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900251// Mark the direct and transitive dependencies of apex bundles so that they
252// can be built for the apex bundles.
253func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800254 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800255 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900256 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900257 depName := mctx.OtherModuleName(child)
258 // If the parent is apexBundle, this child is directly depended.
259 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800260 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800261 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
262 // non-installable apex's cannot be installed and so should not prevent libraries from being
263 // installed to the system.
264 android.UpdateApexDependency(apexBundleName, depName, directDep)
265 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900266
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900267 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900268 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900269 return true
270 } else {
271 return false
272 }
273 })
274 }
275}
276
277// Create apex variations if a module is included in APEX(s).
278func apexMutator(mctx android.BottomUpMutatorContext) {
279 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900280 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900281 } else if _, ok := mctx.Module().(*apexBundle); ok {
282 // apex bundle itself is mutated so that it and its modules have same
283 // apex variant.
284 apexBundleName := mctx.ModuleName()
285 mctx.CreateVariations(apexBundleName)
286 }
287}
Sundong Ahne9b55722019-09-06 17:37:42 +0900288
289func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
290 if _, ok := mctx.Module().(*apexBundle); ok {
291 if !mctx.Config().FlattenApex() || mctx.Config().UnbundledBuild() {
292 modules := mctx.CreateLocalVariations("", "flattened")
293 modules[0].(*apexBundle).SetFlattened(false)
294 modules[1].(*apexBundle).SetFlattened(true)
295 }
296 }
297}
298
Jooyung Han5c998b92019-06-27 11:30:33 +0900299func apexUsesMutator(mctx android.BottomUpMutatorContext) {
300 if ab, ok := mctx.Module().(*apexBundle); ok {
301 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
302 }
303}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900304
Alex Light9670d332019-01-29 18:07:33 -0800305type apexNativeDependencies struct {
306 // List of native libraries
307 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900308
Alex Light9670d332019-01-29 18:07:33 -0800309 // List of native executables
310 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900311
Roland Levillain630846d2019-06-26 12:48:34 +0100312 // List of native tests
313 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800314}
Jooyung Han344d5432019-08-23 11:17:39 +0900315
Alex Light9670d332019-01-29 18:07:33 -0800316type apexMultilibProperties struct {
317 // Native dependencies whose compile_multilib is "first"
318 First apexNativeDependencies
319
320 // Native dependencies whose compile_multilib is "both"
321 Both apexNativeDependencies
322
323 // Native dependencies whose compile_multilib is "prefer32"
324 Prefer32 apexNativeDependencies
325
326 // Native dependencies whose compile_multilib is "32"
327 Lib32 apexNativeDependencies
328
329 // Native dependencies whose compile_multilib is "64"
330 Lib64 apexNativeDependencies
331}
332
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900333type apexBundleProperties struct {
334 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000335 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800336 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900337
Jiyong Park40e26a22019-02-08 02:53:06 +0900338 // AndroidManifest.xml file used for the zip container of this APEX bundle.
339 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800340 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900341
Roland Levillain411c5842019-09-19 16:37:20 +0100342 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
343 // device (/apex/<apex_name>).
344 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900345 Apex_name *string
346
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900347 // Determines the file contexts file for setting security context to each file in this APEX bundle.
348 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
349 // used.
350 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900351 File_contexts *string
352
353 // List of native shared libs that are embedded inside this APEX bundle
354 Native_shared_libs []string
355
Roland Levillain630846d2019-06-26 12:48:34 +0100356 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900357 Binaries []string
358
359 // List of java libraries that are embedded inside this APEX bundle
360 Java_libs []string
361
362 // List of prebuilt files that are embedded inside this APEX bundle
363 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900364
Roland Levillain630846d2019-06-26 12:48:34 +0100365 // List of tests that are embedded inside this APEX bundle
366 Tests []string
367
Jiyong Parkff1458f2018-10-12 21:49:38 +0900368 // Name of the apex_key module that provides the private key to sign APEX
369 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900370
Alex Light5098a612018-11-29 17:12:15 -0800371 // The type of APEX to build. Controls what the APEX payload is. Either
372 // 'image', 'zip' or 'both'. Default: 'image'.
373 Payload_type *string
374
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900375 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
376 // or an android_app_certificate module name in the form ":module".
377 Certificate *string
378
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900379 // Whether this APEX is installable to one of the partitions. Default: true.
380 Installable *bool
381
Jiyong Parkda6eb592018-12-19 17:12:36 +0900382 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
383 // Default is false.
384 Use_vendor *bool
385
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800386 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
387 Ignore_system_library_special_case *bool
388
Alex Light9670d332019-01-29 18:07:33 -0800389 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900390
Jiyong Parkf97782b2019-02-13 20:28:58 +0900391 // List of sanitizer names that this APEX is enabled for
392 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900393
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900394 PreventInstall bool `blueprint:"mutated"`
395
396 HideFromMake bool `blueprint:"mutated"`
397
Jooyung Han5c998b92019-06-27 11:30:33 +0900398 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
399 Provide_cpp_shared_libs *bool
400
401 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
402 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100403
404 // A txt file containing list of files that are whitelisted to be included in this APEX.
405 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900406
407 // List of APKs to package inside APEX
408 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900409
410 // To distinguish between flattened and non-flattened variants.
411 // if set true, then this variant is flattened variant.
412 Flattened bool `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800413}
414
415type apexTargetBundleProperties struct {
416 Target struct {
417 // Multilib properties only for android.
418 Android struct {
419 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900420 }
Jooyung Han344d5432019-08-23 11:17:39 +0900421
Alex Light9670d332019-01-29 18:07:33 -0800422 // Multilib properties only for host.
423 Host struct {
424 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900425 }
Jooyung Han344d5432019-08-23 11:17:39 +0900426
Alex Light9670d332019-01-29 18:07:33 -0800427 // Multilib properties only for host linux_bionic.
428 Linux_bionic struct {
429 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900430 }
Jooyung Han344d5432019-08-23 11:17:39 +0900431
Alex Light9670d332019-01-29 18:07:33 -0800432 // Multilib properties only for host linux_glibc.
433 Linux_glibc struct {
434 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900435 }
436 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900437}
438
Jooyung Han344d5432019-08-23 11:17:39 +0900439type apexVndkProperties struct {
440 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
441 Vndk_version *string
442}
443
Jiyong Park8fd61922018-11-08 02:50:25 +0900444type apexFileClass int
445
446const (
447 etc apexFileClass = iota
448 nativeSharedLib
449 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900450 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800451 pyBinary
452 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900453 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100454 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900455 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900456)
457
Alex Light5098a612018-11-29 17:12:15 -0800458type apexPackaging int
459
460const (
461 imageApex apexPackaging = iota
462 zipApex
463 both
464)
465
466func (a apexPackaging) image() bool {
467 switch a {
468 case imageApex, both:
469 return true
470 }
471 return false
472}
473
474func (a apexPackaging) zip() bool {
475 switch a {
476 case zipApex, both:
477 return true
478 }
479 return false
480}
481
482func (a apexPackaging) suffix() string {
483 switch a {
484 case imageApex:
485 return imageApexSuffix
486 case zipApex:
487 return zipApexSuffix
488 case both:
489 panic(fmt.Errorf("must be either zip or image"))
490 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100491 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800492 }
493}
494
495func (a apexPackaging) name() string {
496 switch a {
497 case imageApex:
498 return imageApexType
499 case zipApex:
500 return zipApexType
501 case both:
502 panic(fmt.Errorf("must be either zip or image"))
503 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100504 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800505 }
506}
507
Jiyong Park8fd61922018-11-08 02:50:25 +0900508func (class apexFileClass) NameInMake() string {
509 switch class {
510 case etc:
511 return "ETC"
512 case nativeSharedLib:
513 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800514 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900515 return "EXECUTABLES"
516 case javaSharedLib:
517 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100518 case nativeTest:
519 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900520 case app:
521 return "APPS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900522 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100523 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900524 }
525}
526
527type apexFile struct {
528 builtFile android.Path
529 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900530 installDir string
531 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900532 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800533 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900534}
535
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900536type apexBundle struct {
537 android.ModuleBase
538 android.DefaultableModuleBase
539
Alex Light9670d332019-01-29 18:07:33 -0800540 properties apexBundleProperties
541 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900542 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900543
Alex Light5098a612018-11-29 17:12:15 -0800544 apexTypes apexPackaging
545
Colin Crossa4925902018-11-16 11:36:28 -0800546 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800547 outputFiles map[apexPackaging]android.WritablePath
Roland Levillain935639d2019-08-13 14:55:28 +0100548 flattenedOutput android.OutputPath
Colin Crossa4925902018-11-16 11:36:28 -0800549 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900550
Jiyong Park03b68dd2019-07-26 23:20:40 +0900551 prebuiltFileToDelete string
552
Jiyong Park42cca6c2019-04-01 11:15:50 +0900553 public_key_file android.Path
554 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900555
556 container_certificate_file android.Path
557 container_private_key_file android.Path
558
Jiyong Park8fd61922018-11-08 02:50:25 +0900559 // list of files to be included in this apex
560 filesInfo []apexFile
561
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900562 // list of module names that this APEX is depending on
563 externalDeps []string
564
Alex Light0851b882019-02-07 13:20:53 -0800565 testApex bool
Jooyung Han344d5432019-08-23 11:17:39 +0900566 vndkApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900567
568 // intermediate path for apex_manifest.json
569 manifestOut android.WritablePath
Sundong Ahne9b55722019-09-06 17:37:42 +0900570
571 // A config value of (TARGET_FLATTEN_APEX && !TARGET_BUILD_APPS)
572 flattenedConfigValue bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900573}
574
Jiyong Park397e55e2018-10-24 21:09:55 +0900575func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100576 native_shared_libs []string, binaries []string, tests []string,
577 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900578 // Use *FarVariation* to be able to depend on modules having
579 // conflicting variations with this module. This is required since
580 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
581 // for native shared libs.
582 ctx.AddFarVariationDependencies([]blueprint.Variation{
583 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900584 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900585 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900586 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900587 }, sharedLibTag, native_shared_libs...)
588
589 ctx.AddFarVariationDependencies([]blueprint.Variation{
590 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900591 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900592 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100593
594 ctx.AddFarVariationDependencies([]blueprint.Variation{
595 {Mutator: "arch", Variation: arch},
596 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100597 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100598 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900599}
600
Alex Light9670d332019-01-29 18:07:33 -0800601func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
602 if ctx.Os().Class == android.Device {
603 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
604 } else {
605 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
606 if ctx.Os().Bionic() {
607 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
608 } else {
609 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
610 }
611 }
612}
613
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900614func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800615
Jiyong Park397e55e2018-10-24 21:09:55 +0900616 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900617 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800618
619 a.combineProperties(ctx)
620
Jiyong Park397e55e2018-10-24 21:09:55 +0900621 has32BitTarget := false
622 for _, target := range targets {
623 if target.Arch.ArchType.Multilib == "lib32" {
624 has32BitTarget = true
625 }
626 }
627 for i, target := range targets {
628 // When multilib.* is omitted for native_shared_libs, it implies
629 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900630 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900631 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900632 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900633 {Mutator: "link", Variation: "shared"},
634 }, sharedLibTag, a.properties.Native_shared_libs...)
635
Roland Levillain630846d2019-06-26 12:48:34 +0100636 // When multilib.* is omitted for tests, it implies
637 // multilib.both.
638 ctx.AddFarVariationDependencies([]blueprint.Variation{
639 {Mutator: "arch", Variation: target.String()},
640 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100641 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100642 }, testTag, a.properties.Tests...)
643
Jiyong Park397e55e2018-10-24 21:09:55 +0900644 // Add native modules targetting both ABIs
645 addDependenciesForNativeModules(ctx,
646 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100647 a.properties.Multilib.Both.Binaries,
648 a.properties.Multilib.Both.Tests,
649 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900650 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900651
Alex Light3d673592019-01-18 14:37:31 -0800652 isPrimaryAbi := i == 0
653 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900654 // When multilib.* is omitted for binaries, it implies
655 // multilib.first.
656 ctx.AddFarVariationDependencies([]blueprint.Variation{
657 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900658 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900659 }, executableTag, a.properties.Binaries...)
660
661 // Add native modules targetting the first ABI
662 addDependenciesForNativeModules(ctx,
663 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100664 a.properties.Multilib.First.Binaries,
665 a.properties.Multilib.First.Tests,
666 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900667 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800668
669 // When multilib.* is omitted for prebuilts, it implies multilib.first.
670 ctx.AddFarVariationDependencies([]blueprint.Variation{
671 {Mutator: "arch", Variation: target.String()},
672 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900673 }
674
675 switch target.Arch.ArchType.Multilib {
676 case "lib32":
677 // Add native modules targetting 32-bit ABI
678 addDependenciesForNativeModules(ctx,
679 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100680 a.properties.Multilib.Lib32.Binaries,
681 a.properties.Multilib.Lib32.Tests,
682 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900683 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900684
685 addDependenciesForNativeModules(ctx,
686 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100687 a.properties.Multilib.Prefer32.Binaries,
688 a.properties.Multilib.Prefer32.Tests,
689 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900690 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900691 case "lib64":
692 // Add native modules targetting 64-bit ABI
693 addDependenciesForNativeModules(ctx,
694 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100695 a.properties.Multilib.Lib64.Binaries,
696 a.properties.Multilib.Lib64.Tests,
697 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900698 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900699
700 if !has32BitTarget {
701 addDependenciesForNativeModules(ctx,
702 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100703 a.properties.Multilib.Prefer32.Binaries,
704 a.properties.Multilib.Prefer32.Tests,
705 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900706 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900707 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700708
709 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
710 for _, sanitizer := range ctx.Config().SanitizeDevice() {
711 if sanitizer == "hwaddress" {
712 addDependenciesForNativeModules(ctx,
713 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100714 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700715 break
716 }
717 }
718 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900719 }
720
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900721 }
722
Jiyong Parkff1458f2018-10-12 21:49:38 +0900723 ctx.AddFarVariationDependencies([]blueprint.Variation{
724 {Mutator: "arch", Variation: "android_common"},
725 }, javaLibTag, a.properties.Java_libs...)
726
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900727 ctx.AddFarVariationDependencies([]blueprint.Variation{
728 {Mutator: "arch", Variation: "android_common"},
729 }, androidAppTag, a.properties.Apps...)
730
Jiyong Park23c52b02019-02-02 13:13:47 +0900731 if String(a.properties.Key) == "" {
732 ctx.ModuleErrorf("key is missing")
733 return
734 }
735 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900736
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900737 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900738 if cert != "" {
739 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900740 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900741}
742
Colin Cross0ea8ba82019-06-06 14:33:29 -0700743func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900744 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
745 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000746 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900747 }
748 return String(a.properties.Certificate)
749}
750
Colin Cross41955e82019-05-29 14:40:35 -0700751func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
752 switch tag {
753 case "":
754 if file, ok := a.outputFiles[imageApex]; ok {
755 return android.Paths{file}, nil
756 } else {
757 return nil, nil
758 }
Roland Levillain935639d2019-08-13 14:55:28 +0100759 case ".flattened":
Sundong Ahne9b55722019-09-06 17:37:42 +0900760 if a.properties.Flattened {
Roland Levillain935639d2019-08-13 14:55:28 +0100761 flattenedApexPath := a.flattenedOutput
762 return android.Paths{flattenedApexPath}, nil
763 } else {
764 return nil, nil
765 }
Colin Cross41955e82019-05-29 14:40:35 -0700766 default:
767 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900768 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900769}
770
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900771func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900772 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900773}
774
Jiyong Park7c1dc612019-01-05 11:15:24 +0900775func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
776 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900777 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900778 } else {
779 return "core"
780 }
781}
782
Jiyong Parkf97782b2019-02-13 20:28:58 +0900783func (a *apexBundle) EnableSanitizer(sanitizerName string) {
784 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
785 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
786 }
787}
788
Jiyong Park388ef3f2019-01-28 19:47:32 +0900789func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900790 if android.InList(sanitizerName, a.properties.SanitizerNames) {
791 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900792 }
793
794 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900795 globalSanitizerNames := []string{}
796 if a.Host() {
797 globalSanitizerNames = ctx.Config().SanitizeHost()
798 } else {
799 arches := ctx.Config().SanitizeDeviceArch()
800 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
801 globalSanitizerNames = ctx.Config().SanitizeDevice()
802 }
803 }
804 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900805}
806
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900807func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
808 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
809}
810
811func (a *apexBundle) PreventInstall() {
812 a.properties.PreventInstall = true
813}
814
815func (a *apexBundle) HideFromMake() {
816 a.properties.HideFromMake = true
817}
818
Sundong Ahne9b55722019-09-06 17:37:42 +0900819func (a *apexBundle) SetFlattened(flattened bool) {
820 a.properties.Flattened = flattened
821}
822
Martin Stjernholm279de572019-09-10 23:18:20 +0100823func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900824 // Decide the APEX-local directory by the multilib of the library
825 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100826 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900827 case "lib32":
828 dirInApex = "lib"
829 case "lib64":
830 dirInApex = "lib64"
831 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100832 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
833 if !ccMod.Arch().Native {
834 dirInApex = filepath.Join(dirInApex, ccMod.Arch().ArchType.String())
835 } else if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
836 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900837 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100838 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
839 // Special case for Bionic libs and other libs installed with them. This is
840 // to prevent those libs from being included in the search path
841 // /apex/com.android.runtime/${LIB}. This exclusion is required because
842 // those libs in the Runtime APEX are available via the legacy paths in
843 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
844 // to the legacy paths and thus will be loaded into the default linker
845 // namespace (aka "platform" namespace). If the libs are directly in
846 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
847 // into the runtime linker namespace, which will result in double loading of
848 // them, which isn't supported.
849 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900850 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900851
Martin Stjernholm279de572019-09-10 23:18:20 +0100852 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900853 return
854}
855
856func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900857 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200858 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900859 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200860 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
861 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900862 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900863 fileToCopy = cc.OutputFile().Path()
864 return
865}
866
Alex Light778127a2019-02-27 14:19:50 -0800867func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
868 dirInApex = "bin"
869 fileToCopy = py.HostToolPath().Path()
870 return
871}
872func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
873 dirInApex = "bin"
874 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
875 if err != nil {
876 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
877 return
878 }
879 fileToCopy = android.PathForOutput(ctx, s)
880 return
881}
882
Jiyong Park04480cf2019-02-06 00:16:29 +0900883func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
884 dirInApex = filepath.Join("bin", sh.SubDir())
885 fileToCopy = sh.OutputFile()
886 return
887}
888
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900889func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
890 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900891 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900892 return
893}
894
Jiyong Park9e6c2422019-08-09 20:39:45 +0900895func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
896 dirInApex = "javalib"
897 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
898 implJars := java.ImplementationJars()
899 if len(implJars) != 1 {
900 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
901 strings.Join(implJars.Strings(), ", ")))
902 }
903 fileToCopy = implJars[0]
904 return
905}
906
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900907func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
908 dirInApex = filepath.Join("etc", prebuilt.SubDir())
909 fileToCopy = prebuilt.OutputFile()
910 return
911}
912
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900913func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
914 dirInApex = filepath.Join("app", pkgName)
915 fileToCopy = app.OutputFile()
916 return
917}
918
Roland Levillain935639d2019-08-13 14:55:28 +0100919// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
920type flattenedApexContext struct {
921 android.ModuleContext
922}
923
924func (c *flattenedApexContext) InstallBypassMake() bool {
925 return true
926}
927
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900928func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900929 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900930
Alex Light5098a612018-11-29 17:12:15 -0800931 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
932 a.apexTypes = imageApex
933 } else if *a.properties.Payload_type == "zip" {
934 a.apexTypes = zipApex
935 } else if *a.properties.Payload_type == "both" {
936 a.apexTypes = both
937 } else {
938 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
939 return
940 }
941
Roland Levillain630846d2019-06-26 12:48:34 +0100942 if len(a.properties.Tests) > 0 && !a.testApex {
943 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
944 return
945 }
946
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800947 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
948
Jooyung Hane1633032019-08-01 17:41:43 +0900949 // native lib dependencies
950 var provideNativeLibs []string
951 var requireNativeLibs []string
952
Jooyung Han5c998b92019-06-27 11:30:33 +0900953 // Check if "uses" requirements are met with dependent apexBundles
954 var providedNativeSharedLibs []string
955 useVendor := proptools.Bool(a.properties.Use_vendor)
956 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
957 if ctx.OtherModuleDependencyTag(m) != usesTag {
958 return
959 }
960 otherName := ctx.OtherModuleName(m)
961 other, ok := m.(*apexBundle)
962 if !ok {
963 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
964 return
965 }
966 if proptools.Bool(other.properties.Use_vendor) != useVendor {
967 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
968 return
969 }
970 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
971 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
972 return
973 }
974 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
975 })
976
Alex Light778127a2019-02-27 14:19:50 -0800977 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100978 depTag := ctx.OtherModuleDependencyTag(child)
979 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900980 if _, ok := parent.(*apexBundle); ok {
981 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900982 switch depTag {
983 case sharedLibTag:
984 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900985 if cc.HasStubsVariants() {
986 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
987 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100988 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900989 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900990 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900991 } else {
992 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900993 }
994 case executableTag:
995 if cc, ok := child.(*cc.Module); ok {
996 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900997 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900998 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900999 } else if sh, ok := child.(*android.ShBinary); ok {
1000 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
1001 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -08001002 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1003 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1004 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1005 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1006 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1007 // NB: Since go binaries are static we don't need the module for anything here, which is
1008 // good since the go tool is a blueprint.Module not an android.Module like we would
1009 // normally use.
1010 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001011 } else {
Alex Light778127a2019-02-27 14:19:50 -08001012 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 +09001013 }
1014 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001015 if javaLib, ok := child.(*java.Library); ok {
1016 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001017 if fileToCopy == nil {
1018 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1019 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001020 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1021 }
1022 return true
1023 } else if javaLib, ok := child.(*java.Import); ok {
1024 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1025 if fileToCopy == nil {
1026 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1027 } else {
1028 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001029 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001030 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001031 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001032 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001033 }
1034 case prebuiltTag:
1035 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1036 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001037 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001038 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001039 } else {
1040 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1041 }
Roland Levillain630846d2019-06-26 12:48:34 +01001042 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001043 if ccTest, ok := child.(*cc.Module); ok {
1044 if ccTest.IsTestPerSrcAllTestsVariation() {
1045 // Multiple-output test module (where `test_per_src: true`).
1046 //
1047 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1048 // We do not add this variation to `filesInfo`, as it has no output;
1049 // however, we do add the other variations of this module as indirect
1050 // dependencies (see below).
1051 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001052 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001053 // Single-output test module (where `test_per_src: false`).
1054 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1055 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001056 }
Roland Levillain630846d2019-06-26 12:48:34 +01001057 return true
1058 } else {
1059 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1060 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001061 case keyTag:
1062 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001063 a.private_key_file = key.private_key_file
1064 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001065 return false
1066 } else {
1067 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001068 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001069 case certificateTag:
1070 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001071 a.container_certificate_file = dep.Certificate.Pem
1072 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001073 return false
1074 } else {
1075 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1076 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001077 case android.PrebuiltDepTag:
1078 // If the prebuilt is force disabled, remember to delete the prebuilt file
1079 // that might have been installed in the previous builds
1080 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1081 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1082 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001083 case androidAppTag:
1084 if ap, ok := child.(*java.AndroidApp); ok {
1085 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1086 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1087 return true
1088 } else {
1089 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1090 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001091 }
1092 } else {
1093 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001094 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001095 // We cannot use a switch statement on `depTag` here as the checked
1096 // tags used below are private (e.g. `cc.sharedDepTag`).
1097 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1098 if cc, ok := child.(*cc.Module); ok {
1099 if android.InList(cc.Name(), providedNativeSharedLibs) {
1100 // If we're using a shared library which is provided from other APEX,
1101 // don't include it in this APEX
1102 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001103 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001104 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1105 // If the dependency is a stubs lib, don't include it in this APEX,
1106 // but make sure that the lib is installed on the device.
1107 // In case no APEX is having the lib, the lib is installed to the system
1108 // partition.
1109 //
1110 // Always include if we are a host-apex however since those won't have any
1111 // system libraries.
1112 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1113 a.externalDeps = append(a.externalDeps, cc.Name())
1114 }
Jooyung Hane1633032019-08-01 17:41:43 +09001115 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001116 // Don't track further
1117 return false
1118 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001119 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001120 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1121 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001122 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001123 } else if cc.IsTestPerSrcDepTag(depTag) {
1124 if cc, ok := child.(*cc.Module); ok {
1125 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1126 // Handle modules created as `test_per_src` variations of a single test module:
1127 // use the name of the generated test binary (`fileToCopy`) instead of the name
1128 // of the original test module (`depName`, shared by all `test_per_src`
1129 // variations of that module).
1130 moduleName := filepath.Base(fileToCopy.String())
1131 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1132 return true
1133 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001134 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001135 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Sundong Ahn2db7f462019-08-27 18:53:12 +09001136 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
1137 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001138 }
1139 }
1140 }
1141 return false
1142 })
1143
Sundong Ahne9b55722019-09-06 17:37:42 +09001144 a.flattenedConfigValue = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1145 if a.flattenedConfigValue {
1146 a.properties.Flattened = true
1147 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001148 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001149 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1150 return
1151 }
1152
Jiyong Park8fd61922018-11-08 02:50:25 +09001153 // remove duplicates in filesInfo
1154 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001155 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001156 result := []apexFile{}
1157 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001158 dest := filepath.Join(f.installDir, f.builtFile.Base())
1159 if !encountered[dest] {
1160 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001161 result = append(result, f)
1162 }
1163 }
1164 return result
1165 }
1166 filesInfo = removeDup(filesInfo)
1167
1168 // to have consistent build rules
1169 sort.Slice(filesInfo, func(i, j int) bool {
1170 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1171 })
1172
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001173 // check no_apex modules
1174 whitelist := whitelistNoApex[ctx.ModuleName()]
1175 for i := range filesInfo {
1176 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1177 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1178 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1179 }
1180 }
1181 }
1182
Jiyong Park8fd61922018-11-08 02:50:25 +09001183 // prepend the name of this APEX to the module names. These names will be the names of
1184 // modules that will be defined if the APEX is flattened.
1185 for i := range filesInfo {
1186 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1187 }
1188
Jiyong Park8fd61922018-11-08 02:50:25 +09001189 a.installDir = android.PathForModuleInstall(ctx, "apex")
1190 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001191
Jooyung Hane1633032019-08-01 17:41:43 +09001192 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1193 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1194 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1195 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1196 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1197 ctx.Build(pctx, android.BuildParams{
1198 Rule: injectApexDependency,
1199 Input: manifestSrc,
1200 Output: a.manifestOut,
1201 Args: map[string]string{
1202 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1203 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1204 },
1205 })
1206
Roland Levillain935639d2019-08-13 14:55:28 +01001207 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1208 // reply true to `InstallBypassMake()` (thus making the call
1209 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1210 // instead of `android.PathForOutput`) to return the correct path to the flattened
1211 // APEX (as its contents is installed by Make, not Soong).
1212 factx := flattenedApexContext{ctx}
1213 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1214
Alex Light5098a612018-11-29 17:12:15 -08001215 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001216 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001217 }
1218 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001219 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001220 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001221 // in other modules. It is in AndroidMk where the selection of flattened
1222 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001223 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001224 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001225 }
1226}
1227
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001228func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001229 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001230 for _, f := range a.filesInfo {
1231 if f.module != nil {
1232 notice := f.module.NoticeFile()
1233 if notice.Valid() {
1234 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001235 }
1236 }
1237 }
1238 // append the notice file specified in the apex module itself
1239 if a.NoticeFile().Valid() {
1240 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001241 }
1242
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001243 if len(noticeFiles) == 0 {
1244 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001245 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001246
Jaewoong Jung98772792019-07-01 17:15:13 -07001247 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001248}
1249
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001250func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001251 cert := String(a.properties.Certificate)
1252 if cert != "" && android.SrcIsModule(cert) == "" {
1253 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001254 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1255 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001256 } else if cert == "" {
1257 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001258 a.container_certificate_file = pem
1259 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001260 }
1261
Alex Light5098a612018-11-29 17:12:15 -08001262 var abis []string
1263 for _, target := range ctx.MultiTargets() {
1264 if len(target.Arch.Abi) > 0 {
1265 abis = append(abis, target.Arch.Abi[0])
1266 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001267 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001268
Alex Light5098a612018-11-29 17:12:15 -08001269 abis = android.FirstUniqueStrings(abis)
1270
1271 suffix := apexType.suffix()
1272 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001273
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001274 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001275 for _, f := range a.filesInfo {
1276 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001277 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001278
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001279 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001280 emitCommands := []string{}
1281 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1282 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001283 for i, src := range filesToCopy {
1284 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001285 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001286 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001287 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1288 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001289 for _, sym := range a.filesInfo[i].symlinks {
1290 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1291 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1292 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001293 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001294 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001295 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001296
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001297 if a.properties.Whitelisted_files != nil {
1298 ctx.Build(pctx, android.BuildParams{
1299 Rule: emitApexContentRule,
1300 Implicits: implicitInputs,
1301 Output: imageContentFile,
1302 Description: "emit apex image content",
1303 Args: map[string]string{
1304 "emit_commands": strings.Join(emitCommands, " && "),
1305 },
1306 })
1307 implicitInputs = append(implicitInputs, imageContentFile)
1308 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1309
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001310 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001311 ctx.Build(pctx, android.BuildParams{
1312 Rule: diffApexContentRule,
1313 Implicits: implicitInputs,
1314 Output: phonyOutput,
1315 Description: "diff apex image content",
1316 Args: map[string]string{
1317 "whitelisted_files_file": whitelistedFilesFile.String(),
1318 "image_content_file": imageContentFile.String(),
1319 "apex_module_name": ctx.ModuleName(),
1320 },
1321 })
1322
1323 implicitInputs = append(implicitInputs, phonyOutput)
1324 }
1325
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001326 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1327 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001328
Alex Light5098a612018-11-29 17:12:15 -08001329 if apexType.image() {
1330 // files and dirs that will be created in APEX
1331 var readOnlyPaths []string
1332 var executablePaths []string // this also includes dirs
1333 for _, f := range a.filesInfo {
1334 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001335 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001336 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001337 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001338 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001339 }
Alex Light5098a612018-11-29 17:12:15 -08001340 } else {
1341 readOnlyPaths = append(readOnlyPaths, pathInApex)
1342 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001343 dir := f.installDir
1344 for !android.InList(dir, executablePaths) && dir != "" {
1345 executablePaths = append(executablePaths, dir)
1346 dir, _ = filepath.Split(dir) // move up to the parent
1347 if len(dir) > 0 {
1348 // remove trailing slash
1349 dir = dir[:len(dir)-1]
1350 }
Alex Light5098a612018-11-29 17:12:15 -08001351 }
1352 }
1353 sort.Strings(readOnlyPaths)
1354 sort.Strings(executablePaths)
1355 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1356 ctx.Build(pctx, android.BuildParams{
1357 Rule: generateFsConfig,
1358 Output: cannedFsConfig,
1359 Description: "generate fs config",
1360 Args: map[string]string{
1361 "ro_paths": strings.Join(readOnlyPaths, " "),
1362 "exec_paths": strings.Join(executablePaths, " "),
1363 },
1364 })
1365
1366 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1367 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1368 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1369 if !fileContextsOptionalPath.Valid() {
1370 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1371 return
1372 }
1373 fileContexts := fileContextsOptionalPath.Path()
1374
Jiyong Park835d82b2018-12-27 16:04:18 +09001375 optFlags := []string{}
1376
Alex Light5098a612018-11-29 17:12:15 -08001377 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001378 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1379 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001380
Jiyong Park7f67f482019-01-05 12:57:48 +09001381 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1382 if overridden {
1383 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1384 }
1385
Jiyong Park40e26a22019-02-08 02:53:06 +09001386 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001387 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001388 implicitInputs = append(implicitInputs, androidManifestFile)
1389 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1390 }
1391
Jiyong Park71b519d2019-04-18 17:25:49 +09001392 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1393 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1394 ctx.Config().UnbundledBuild() &&
1395 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1396 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1397 apiFingerprint := java.ApiFingerprintPath(ctx)
1398 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1399 implicitInputs = append(implicitInputs, apiFingerprint)
1400 }
1401 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1402
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001403 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1404 if noticeFile.Valid() {
1405 // If there's a NOTICE file, embed it as an asset file in the APEX.
1406 implicitInputs = append(implicitInputs, noticeFile.Path())
1407 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1408 }
1409
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001410 if !ctx.Config().UnbundledBuild() && a.installable() {
1411 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1412 // don't need hashtree for activation. Therefore, by removing hashtree from
1413 // apex bundle (filesystem image in it, to be specific), we can save storage.
1414 optFlags = append(optFlags, "--no_hashtree")
1415 }
1416
Alex Light5098a612018-11-29 17:12:15 -08001417 ctx.Build(pctx, android.BuildParams{
1418 Rule: apexRule,
1419 Implicits: implicitInputs,
1420 Output: unsignedOutputFile,
1421 Description: "apex (" + apexType.name() + ")",
1422 Args: map[string]string{
1423 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1424 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1425 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001426 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001427 "file_contexts": fileContexts.String(),
1428 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001429 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001430 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001431 },
1432 })
1433
1434 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1435 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1436 a.bundleModuleFile = bundleModuleFile
1437
1438 ctx.Build(pctx, android.BuildParams{
1439 Rule: apexProtoConvertRule,
1440 Input: unsignedOutputFile,
1441 Output: apexProtoFile,
1442 Description: "apex proto convert",
1443 })
1444
1445 ctx.Build(pctx, android.BuildParams{
1446 Rule: apexBundleRule,
1447 Input: apexProtoFile,
1448 Output: a.bundleModuleFile,
1449 Description: "apex bundle module",
1450 Args: map[string]string{
1451 "abi": strings.Join(abis, "."),
1452 },
1453 })
1454 } else {
1455 ctx.Build(pctx, android.BuildParams{
1456 Rule: zipApexRule,
1457 Implicits: implicitInputs,
1458 Output: unsignedOutputFile,
1459 Description: "apex (" + apexType.name() + ")",
1460 Args: map[string]string{
1461 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1462 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1463 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001464 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001465 },
1466 })
Colin Crossa4925902018-11-16 11:36:28 -08001467 }
Colin Crossa4925902018-11-16 11:36:28 -08001468
Alex Light5098a612018-11-29 17:12:15 -08001469 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001470 ctx.Build(pctx, android.BuildParams{
1471 Rule: java.Signapk,
1472 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001473 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001474 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001475 Implicits: []android.Path{
1476 a.container_certificate_file,
1477 a.container_private_key_file,
1478 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001479 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001480 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001481 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001482 },
1483 })
Alex Light5098a612018-11-29 17:12:15 -08001484
1485 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahn72f1f3e2019-09-12 22:53:00 +09001486 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && (!a.properties.Flattened || a.flattenedConfigValue) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001487 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001488 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001489}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001490
Jiyong Park8fd61922018-11-08 02:50:25 +09001491func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001492 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001493 // 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 +09001494 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001495 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001496
Jiyong Park42cca6c2019-04-01 11:15:50 +09001497 // rename to apex_pubkey
1498 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1499 ctx.Build(pctx, android.BuildParams{
1500 Rule: android.Cp,
1501 Input: a.public_key_file,
1502 Output: copiedPubkey,
1503 })
1504 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1505
Jiyong Park23c52b02019-02-02 13:13:47 +09001506 if ctx.Config().FlattenApex() {
1507 for _, fi := range a.filesInfo {
1508 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001509 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1510 for _, sym := range fi.symlinks {
1511 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1512 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001513 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001514 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001515 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001516}
1517
1518func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001519 if a.properties.HideFromMake {
1520 return android.AndroidMkData{
1521 Disabled: true,
1522 }
1523 }
Alex Light5098a612018-11-29 17:12:15 -08001524 writers := []android.AndroidMkData{}
1525 if a.apexTypes.image() {
1526 writers = append(writers, a.androidMkForType(imageApex))
1527 }
1528 if a.apexTypes.zip() {
1529 writers = append(writers, a.androidMkForType(zipApex))
1530 }
1531 return android.AndroidMkData{
1532 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1533 for _, data := range writers {
1534 data.Custom(w, name, prefix, moduleDir, data)
1535 }
1536 }}
1537}
1538
Alex Lightf1801bc2019-02-13 11:10:07 -08001539func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001540 moduleNames := []string{}
1541
1542 for _, fi := range a.filesInfo {
1543 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1544 continue
1545 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001546 if a.properties.Flattened && !apexType.image() {
1547 continue
Jiyong Park94427262019-02-05 23:18:47 +09001548 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001549
1550 var suffix string
1551 if a.properties.Flattened && !a.flattenedConfigValue {
1552 suffix = ".flattened"
1553 }
1554
1555 if !android.InList(fi.moduleName, moduleNames) {
1556 moduleNames = append(moduleNames, fi.moduleName+suffix)
1557 }
1558
Jiyong Park94427262019-02-05 23:18:47 +09001559 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1560 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001561 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName+suffix)
Roland Levillain411c5842019-09-19 16:37:20 +01001562 // /apex/<apex_name>/{lib|framework|...}
Jiyong Park05e70dd2019-03-18 14:26:32 +09001563 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1564 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001565 if a.properties.Flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001566 // /system/apex/<name>/{lib|framework|...}
1567 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1568 a.installDir.RelPathString(), name, fi.installDir))
Sundong Ahne9b55722019-09-06 17:37:42 +09001569 if a.flattenedConfigValue {
1570 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1571 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001572 if len(fi.symlinks) > 0 {
1573 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1574 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001575
1576 if fi.module != nil && fi.module.NoticeFile().Valid() {
1577 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1578 }
Jiyong Park94427262019-02-05 23:18:47 +09001579 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001580 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001581 }
1582 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1583 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1584 if fi.module != nil {
1585 archStr := fi.module.Target().Arch.ArchType.String()
1586 host := false
1587 switch fi.module.Target().Os.Class {
1588 case android.Host:
1589 if archStr != "common" {
1590 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1591 }
1592 host = true
1593 case android.HostCross:
1594 if archStr != "common" {
1595 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1596 }
1597 host = true
1598 case android.Device:
1599 if archStr != "common" {
1600 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1601 }
1602 }
1603 if host {
1604 makeOs := fi.module.Target().Os.String()
1605 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1606 makeOs = "linux"
1607 }
1608 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1609 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1610 }
1611 }
1612 if fi.class == javaSharedLib {
1613 javaModule := fi.module.(*java.Library)
1614 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1615 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1616 // we will have foo.jar.jar
1617 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1618 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1619 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1620 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1621 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1622 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001623 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001624 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001625 if cc, ok := fi.module.(*cc.Module); ok {
1626 if cc.UnstrippedOutputFile() != nil {
1627 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1628 }
1629 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001630 if cc.CoverageOutputFile().Valid() {
1631 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1632 }
Jiyong Park94427262019-02-05 23:18:47 +09001633 }
1634 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1635 } else {
1636 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1637 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1638 }
1639 }
1640 return moduleNames
1641}
1642
Alex Light5098a612018-11-29 17:12:15 -08001643func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001644 return android.AndroidMkData{
1645 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1646 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001647 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001648 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001649 }
1650
Sundong Ahne9b55722019-09-06 17:37:42 +09001651 if a.properties.Flattened && !a.flattenedConfigValue {
1652 name = name + ".flattened"
1653 }
1654
1655 if a.properties.Flattened && apexType.image() {
Jiyong Park719b4462019-01-13 00:39:51 +09001656 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001657 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1658 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1659 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001660 if len(moduleNames) > 0 {
1661 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1662 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001663 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001664 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1665
Sundong Ahn72f1f3e2019-09-12 22:53:00 +09001666 } else if !a.properties.Flattened || a.flattenedConfigValue {
Alex Light5098a612018-11-29 17:12:15 -08001667 // zip-apex is the less common type so have the name refer to the image-apex
1668 // only and use {name}.zip if you want the zip-apex
1669 if apexType == zipApex && a.apexTypes == both {
1670 name = name + ".zip"
1671 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001672 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1673 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1674 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1675 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001676 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001677 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001678 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001679 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001680 if len(moduleNames) > 0 {
1681 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1682 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001683 if len(a.externalDeps) > 0 {
1684 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1685 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001686 if a.prebuiltFileToDelete != "" {
1687 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1688 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1689 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001690 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001691
Alex Light5098a612018-11-29 17:12:15 -08001692 if apexType == imageApex {
1693 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1694 }
Jiyong Park719b4462019-01-13 00:39:51 +09001695 }
1696 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001697}
1698
Jooyung Han344d5432019-08-23 11:17:39 +09001699func newApexBundle() *apexBundle {
Alex Light5098a612018-11-29 17:12:15 -08001700 module := &apexBundle{
1701 outputFiles: map[apexPackaging]android.WritablePath{},
1702 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001703 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001704 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001705 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001706 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1707 })
Alex Light5098a612018-11-29 17:12:15 -08001708 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001709 android.InitDefaultableModule(module)
1710 return module
1711}
Jiyong Park30ca9372019-02-07 16:27:23 +09001712
Jooyung Han344d5432019-08-23 11:17:39 +09001713func ApexBundleFactory(testApex bool) android.Module {
1714 bundle := newApexBundle()
1715 bundle.testApex = testApex
1716 return bundle
1717}
1718
1719func testApexBundleFactory() android.Module {
1720 bundle := newApexBundle()
1721 bundle.testApex = true
1722 return bundle
1723}
1724
1725func apexBundleFactory() android.Module {
1726 return newApexBundle()
1727}
1728
1729// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1730// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1731// If not specified, then the "current" versions are gathered.
1732func vndkApexBundleFactory() android.Module {
1733 bundle := newApexBundle()
1734 bundle.vndkApex = true
1735 bundle.AddProperties(&bundle.vndkProperties)
1736 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1737 ctx.AppendProperties(&struct {
1738 Compile_multilib *string
1739 }{
1740 proptools.StringPtr("both"),
1741 })
1742 })
1743 return bundle
1744}
1745
Jiyong Park30ca9372019-02-07 16:27:23 +09001746//
1747// Defaults
1748//
1749type Defaults struct {
1750 android.ModuleBase
1751 android.DefaultsModuleBase
1752}
1753
Jiyong Park30ca9372019-02-07 16:27:23 +09001754func defaultsFactory() android.Module {
1755 return DefaultsFactory()
1756}
1757
1758func DefaultsFactory(props ...interface{}) android.Module {
1759 module := &Defaults{}
1760
1761 module.AddProperties(props...)
1762 module.AddProperties(
1763 &apexBundleProperties{},
1764 &apexTargetBundleProperties{},
1765 )
1766
1767 android.InitDefaultsModule(module)
1768 return module
1769}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001770
1771//
1772// Prebuilt APEX
1773//
1774type Prebuilt struct {
1775 android.ModuleBase
1776 prebuilt android.Prebuilt
1777
1778 properties PrebuiltProperties
1779
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001780 inputApex android.Path
1781 installDir android.OutputPath
1782 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001783 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001784}
1785
1786type PrebuiltProperties struct {
1787 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001788 Source string `blueprint:"mutated"`
1789 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001790
1791 Src *string
1792 Arch struct {
1793 Arm struct {
1794 Src *string
1795 }
1796 Arm64 struct {
1797 Src *string
1798 }
1799 X86 struct {
1800 Src *string
1801 }
1802 X86_64 struct {
1803 Src *string
1804 }
1805 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001806
1807 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001808 // Optional name for the installed apex. If unspecified, name of the
1809 // module is used as the file name
1810 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001811
1812 // Names of modules to be overridden. Listed modules can only be other binaries
1813 // (in Make or Soong).
1814 // This does not completely prevent installation of the overridden binaries, but if both
1815 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1816 // from PRODUCT_PACKAGES.
1817 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001818}
1819
1820func (p *Prebuilt) installable() bool {
1821 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001822}
1823
1824func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001825 // If the device is configured to use flattened APEX, force disable the prebuilt because
1826 // the prebuilt is a non-flattened one.
1827 forceDisable := ctx.Config().FlattenApex()
1828
1829 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1830 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001831 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001832
Kun Niu10c9f832019-07-29 16:28:57 -07001833 // Force disable the prebuilts when coverage is enabled.
1834 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1835 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1836
Jiyong Park50b81e52019-07-11 11:24:41 +09001837 // b/137216042 don't use prebuilts when address sanitizer is on
1838 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1839 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1840
1841 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001842 p.properties.ForceDisable = true
1843 return
1844 }
1845
Jiyong Parkc95714e2019-03-29 14:23:10 +09001846 // This is called before prebuilt_select and prebuilt_postdeps mutators
1847 // The mutators requires that src to be set correctly for each arch so that
1848 // arch variants are disabled when src is not provided for the arch.
1849 if len(ctx.MultiTargets()) != 1 {
1850 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1851 return
1852 }
1853 var src string
1854 switch ctx.MultiTargets()[0].Arch.ArchType {
1855 case android.Arm:
1856 src = String(p.properties.Arch.Arm.Src)
1857 case android.Arm64:
1858 src = String(p.properties.Arch.Arm64.Src)
1859 case android.X86:
1860 src = String(p.properties.Arch.X86.Src)
1861 case android.X86_64:
1862 src = String(p.properties.Arch.X86_64.Src)
1863 default:
1864 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1865 return
1866 }
1867 if src == "" {
1868 src = String(p.properties.Src)
1869 }
1870 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001871}
1872
Jiyong Park03b68dd2019-07-26 23:20:40 +09001873func (p *Prebuilt) isForceDisabled() bool {
1874 return p.properties.ForceDisable
1875}
1876
Colin Cross41955e82019-05-29 14:40:35 -07001877func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1878 switch tag {
1879 case "":
1880 return android.Paths{p.outputApex}, nil
1881 default:
1882 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1883 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001884}
1885
Jiyong Park4d277042019-04-23 18:00:10 +09001886func (p *Prebuilt) InstallFilename() string {
1887 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1888}
1889
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001890func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001891 if p.properties.ForceDisable {
1892 return
1893 }
1894
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001895 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001896 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001897 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001898 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001899 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1900 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1901 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001902 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1903 ctx.Build(pctx, android.BuildParams{
1904 Rule: android.Cp,
1905 Input: p.inputApex,
1906 Output: p.outputApex,
1907 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001908 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001909 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001910 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001911}
1912
1913func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1914 return &p.prebuilt
1915}
1916
1917func (p *Prebuilt) Name() string {
1918 return p.prebuilt.Name(p.ModuleBase.Name())
1919}
1920
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001921func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1922 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001923 Class: "ETC",
1924 OutputFile: android.OptionalPathForPath(p.inputApex),
1925 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001926 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1927 func(entries *android.AndroidMkEntries) {
1928 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1929 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1930 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1931 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1932 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001933 },
1934 }
1935}
1936
1937// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1938func PrebuiltFactory() android.Module {
1939 module := &Prebuilt{}
1940 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001941 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001942 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001943 return module
1944}