blob: 23f6d37297f33d44f649890fa725f712bccb19d1 [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
Jiyong Park05e70dd2019-03-18 14:26:32 +0900342 // Canonical name of the APEX bundle in the manifest file.
343 // If unspecified, defaults to the value of name
344 Apex_name *string
345
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900346 // Determines the file contexts file for setting security context to each file in this APEX bundle.
347 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
348 // used.
349 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900350 File_contexts *string
351
352 // List of native shared libs that are embedded inside this APEX bundle
353 Native_shared_libs []string
354
Roland Levillain630846d2019-06-26 12:48:34 +0100355 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900356 Binaries []string
357
358 // List of java libraries that are embedded inside this APEX bundle
359 Java_libs []string
360
361 // List of prebuilt files that are embedded inside this APEX bundle
362 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900363
Roland Levillain630846d2019-06-26 12:48:34 +0100364 // List of tests that are embedded inside this APEX bundle
365 Tests []string
366
Jiyong Parkff1458f2018-10-12 21:49:38 +0900367 // Name of the apex_key module that provides the private key to sign APEX
368 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900369
Alex Light5098a612018-11-29 17:12:15 -0800370 // The type of APEX to build. Controls what the APEX payload is. Either
371 // 'image', 'zip' or 'both'. Default: 'image'.
372 Payload_type *string
373
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900374 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
375 // or an android_app_certificate module name in the form ":module".
376 Certificate *string
377
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900378 // Whether this APEX is installable to one of the partitions. Default: true.
379 Installable *bool
380
Jiyong Parkda6eb592018-12-19 17:12:36 +0900381 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
382 // Default is false.
383 Use_vendor *bool
384
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800385 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
386 Ignore_system_library_special_case *bool
387
Alex Light9670d332019-01-29 18:07:33 -0800388 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900389
Jiyong Parkf97782b2019-02-13 20:28:58 +0900390 // List of sanitizer names that this APEX is enabled for
391 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900392
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900393 PreventInstall bool `blueprint:"mutated"`
394
395 HideFromMake bool `blueprint:"mutated"`
396
Jooyung Han5c998b92019-06-27 11:30:33 +0900397 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
398 Provide_cpp_shared_libs *bool
399
400 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
401 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100402
403 // A txt file containing list of files that are whitelisted to be included in this APEX.
404 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900405
406 // List of APKs to package inside APEX
407 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900408
409 // To distinguish between flattened and non-flattened variants.
410 // if set true, then this variant is flattened variant.
411 Flattened bool `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800412}
413
414type apexTargetBundleProperties struct {
415 Target struct {
416 // Multilib properties only for android.
417 Android struct {
418 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900419 }
Jooyung Han344d5432019-08-23 11:17:39 +0900420
Alex Light9670d332019-01-29 18:07:33 -0800421 // Multilib properties only for host.
422 Host struct {
423 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900424 }
Jooyung Han344d5432019-08-23 11:17:39 +0900425
Alex Light9670d332019-01-29 18:07:33 -0800426 // Multilib properties only for host linux_bionic.
427 Linux_bionic struct {
428 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900429 }
Jooyung Han344d5432019-08-23 11:17:39 +0900430
Alex Light9670d332019-01-29 18:07:33 -0800431 // Multilib properties only for host linux_glibc.
432 Linux_glibc struct {
433 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900434 }
435 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900436}
437
Jooyung Han344d5432019-08-23 11:17:39 +0900438type apexVndkProperties struct {
439 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
440 Vndk_version *string
441}
442
Jiyong Park8fd61922018-11-08 02:50:25 +0900443type apexFileClass int
444
445const (
446 etc apexFileClass = iota
447 nativeSharedLib
448 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900449 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800450 pyBinary
451 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900452 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100453 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900454 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900455)
456
Alex Light5098a612018-11-29 17:12:15 -0800457type apexPackaging int
458
459const (
460 imageApex apexPackaging = iota
461 zipApex
462 both
463)
464
465func (a apexPackaging) image() bool {
466 switch a {
467 case imageApex, both:
468 return true
469 }
470 return false
471}
472
473func (a apexPackaging) zip() bool {
474 switch a {
475 case zipApex, both:
476 return true
477 }
478 return false
479}
480
481func (a apexPackaging) suffix() string {
482 switch a {
483 case imageApex:
484 return imageApexSuffix
485 case zipApex:
486 return zipApexSuffix
487 case both:
488 panic(fmt.Errorf("must be either zip or image"))
489 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100490 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800491 }
492}
493
494func (a apexPackaging) name() string {
495 switch a {
496 case imageApex:
497 return imageApexType
498 case zipApex:
499 return zipApexType
500 case both:
501 panic(fmt.Errorf("must be either zip or image"))
502 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100503 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800504 }
505}
506
Jiyong Park8fd61922018-11-08 02:50:25 +0900507func (class apexFileClass) NameInMake() string {
508 switch class {
509 case etc:
510 return "ETC"
511 case nativeSharedLib:
512 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800513 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900514 return "EXECUTABLES"
515 case javaSharedLib:
516 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100517 case nativeTest:
518 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900519 case app:
520 return "APPS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900521 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100522 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900523 }
524}
525
526type apexFile struct {
527 builtFile android.Path
528 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900529 installDir string
530 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900531 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800532 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900533}
534
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900535type apexBundle struct {
536 android.ModuleBase
537 android.DefaultableModuleBase
538
Alex Light9670d332019-01-29 18:07:33 -0800539 properties apexBundleProperties
540 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900541 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900542
Alex Light5098a612018-11-29 17:12:15 -0800543 apexTypes apexPackaging
544
Colin Crossa4925902018-11-16 11:36:28 -0800545 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800546 outputFiles map[apexPackaging]android.WritablePath
Roland Levillain935639d2019-08-13 14:55:28 +0100547 flattenedOutput android.OutputPath
Colin Crossa4925902018-11-16 11:36:28 -0800548 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900549
Jiyong Park03b68dd2019-07-26 23:20:40 +0900550 prebuiltFileToDelete string
551
Jiyong Park42cca6c2019-04-01 11:15:50 +0900552 public_key_file android.Path
553 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900554
555 container_certificate_file android.Path
556 container_private_key_file android.Path
557
Jiyong Park8fd61922018-11-08 02:50:25 +0900558 // list of files to be included in this apex
559 filesInfo []apexFile
560
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900561 // list of module names that this APEX is depending on
562 externalDeps []string
563
Alex Light0851b882019-02-07 13:20:53 -0800564 testApex bool
Jooyung Han344d5432019-08-23 11:17:39 +0900565 vndkApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900566
567 // intermediate path for apex_manifest.json
568 manifestOut android.WritablePath
Sundong Ahne9b55722019-09-06 17:37:42 +0900569
570 // A config value of (TARGET_FLATTEN_APEX && !TARGET_BUILD_APPS)
571 flattenedConfigValue bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900572}
573
Jiyong Park397e55e2018-10-24 21:09:55 +0900574func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100575 native_shared_libs []string, binaries []string, tests []string,
576 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900577 // Use *FarVariation* to be able to depend on modules having
578 // conflicting variations with this module. This is required since
579 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
580 // for native shared libs.
581 ctx.AddFarVariationDependencies([]blueprint.Variation{
582 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900583 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900584 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900585 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900586 }, sharedLibTag, native_shared_libs...)
587
588 ctx.AddFarVariationDependencies([]blueprint.Variation{
589 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900590 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900591 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100592
593 ctx.AddFarVariationDependencies([]blueprint.Variation{
594 {Mutator: "arch", Variation: arch},
595 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100596 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100597 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900598}
599
Alex Light9670d332019-01-29 18:07:33 -0800600func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
601 if ctx.Os().Class == android.Device {
602 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
603 } else {
604 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
605 if ctx.Os().Bionic() {
606 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
607 } else {
608 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
609 }
610 }
611}
612
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900613func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800614
Jiyong Park397e55e2018-10-24 21:09:55 +0900615 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900616 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800617
618 a.combineProperties(ctx)
619
Jiyong Park397e55e2018-10-24 21:09:55 +0900620 has32BitTarget := false
621 for _, target := range targets {
622 if target.Arch.ArchType.Multilib == "lib32" {
623 has32BitTarget = true
624 }
625 }
626 for i, target := range targets {
627 // When multilib.* is omitted for native_shared_libs, it implies
628 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900629 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900630 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900631 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900632 {Mutator: "link", Variation: "shared"},
633 }, sharedLibTag, a.properties.Native_shared_libs...)
634
Roland Levillain630846d2019-06-26 12:48:34 +0100635 // When multilib.* is omitted for tests, it implies
636 // multilib.both.
637 ctx.AddFarVariationDependencies([]blueprint.Variation{
638 {Mutator: "arch", Variation: target.String()},
639 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100640 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100641 }, testTag, a.properties.Tests...)
642
Jiyong Park397e55e2018-10-24 21:09:55 +0900643 // Add native modules targetting both ABIs
644 addDependenciesForNativeModules(ctx,
645 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100646 a.properties.Multilib.Both.Binaries,
647 a.properties.Multilib.Both.Tests,
648 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900649 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900650
Alex Light3d673592019-01-18 14:37:31 -0800651 isPrimaryAbi := i == 0
652 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900653 // When multilib.* is omitted for binaries, it implies
654 // multilib.first.
655 ctx.AddFarVariationDependencies([]blueprint.Variation{
656 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900657 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900658 }, executableTag, a.properties.Binaries...)
659
660 // Add native modules targetting the first ABI
661 addDependenciesForNativeModules(ctx,
662 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100663 a.properties.Multilib.First.Binaries,
664 a.properties.Multilib.First.Tests,
665 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900666 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800667
668 // When multilib.* is omitted for prebuilts, it implies multilib.first.
669 ctx.AddFarVariationDependencies([]blueprint.Variation{
670 {Mutator: "arch", Variation: target.String()},
671 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900672 }
673
674 switch target.Arch.ArchType.Multilib {
675 case "lib32":
676 // Add native modules targetting 32-bit ABI
677 addDependenciesForNativeModules(ctx,
678 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100679 a.properties.Multilib.Lib32.Binaries,
680 a.properties.Multilib.Lib32.Tests,
681 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900682 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900683
684 addDependenciesForNativeModules(ctx,
685 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100686 a.properties.Multilib.Prefer32.Binaries,
687 a.properties.Multilib.Prefer32.Tests,
688 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900689 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900690 case "lib64":
691 // Add native modules targetting 64-bit ABI
692 addDependenciesForNativeModules(ctx,
693 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100694 a.properties.Multilib.Lib64.Binaries,
695 a.properties.Multilib.Lib64.Tests,
696 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900697 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900698
699 if !has32BitTarget {
700 addDependenciesForNativeModules(ctx,
701 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100702 a.properties.Multilib.Prefer32.Binaries,
703 a.properties.Multilib.Prefer32.Tests,
704 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900705 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900706 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700707
708 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
709 for _, sanitizer := range ctx.Config().SanitizeDevice() {
710 if sanitizer == "hwaddress" {
711 addDependenciesForNativeModules(ctx,
712 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100713 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700714 break
715 }
716 }
717 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900718 }
719
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900720 }
721
Jiyong Parkff1458f2018-10-12 21:49:38 +0900722 ctx.AddFarVariationDependencies([]blueprint.Variation{
723 {Mutator: "arch", Variation: "android_common"},
724 }, javaLibTag, a.properties.Java_libs...)
725
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900726 ctx.AddFarVariationDependencies([]blueprint.Variation{
727 {Mutator: "arch", Variation: "android_common"},
728 }, androidAppTag, a.properties.Apps...)
729
Jiyong Park23c52b02019-02-02 13:13:47 +0900730 if String(a.properties.Key) == "" {
731 ctx.ModuleErrorf("key is missing")
732 return
733 }
734 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900735
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900736 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900737 if cert != "" {
738 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900739 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900740}
741
Colin Cross0ea8ba82019-06-06 14:33:29 -0700742func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900743 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
744 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000745 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900746 }
747 return String(a.properties.Certificate)
748}
749
Colin Cross41955e82019-05-29 14:40:35 -0700750func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
751 switch tag {
752 case "":
753 if file, ok := a.outputFiles[imageApex]; ok {
754 return android.Paths{file}, nil
755 } else {
756 return nil, nil
757 }
Roland Levillain935639d2019-08-13 14:55:28 +0100758 case ".flattened":
Sundong Ahne9b55722019-09-06 17:37:42 +0900759 if a.properties.Flattened {
Roland Levillain935639d2019-08-13 14:55:28 +0100760 flattenedApexPath := a.flattenedOutput
761 return android.Paths{flattenedApexPath}, nil
762 } else {
763 return nil, nil
764 }
Colin Cross41955e82019-05-29 14:40:35 -0700765 default:
766 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900767 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900768}
769
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900770func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900771 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900772}
773
Jiyong Park7c1dc612019-01-05 11:15:24 +0900774func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
775 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900776 return "vendor"
777 } else {
778 return "core"
779 }
780}
781
Jiyong Parkf97782b2019-02-13 20:28:58 +0900782func (a *apexBundle) EnableSanitizer(sanitizerName string) {
783 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
784 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
785 }
786}
787
Jiyong Park388ef3f2019-01-28 19:47:32 +0900788func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900789 if android.InList(sanitizerName, a.properties.SanitizerNames) {
790 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900791 }
792
793 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900794 globalSanitizerNames := []string{}
795 if a.Host() {
796 globalSanitizerNames = ctx.Config().SanitizeHost()
797 } else {
798 arches := ctx.Config().SanitizeDeviceArch()
799 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
800 globalSanitizerNames = ctx.Config().SanitizeDevice()
801 }
802 }
803 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900804}
805
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900806func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
807 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
808}
809
810func (a *apexBundle) PreventInstall() {
811 a.properties.PreventInstall = true
812}
813
814func (a *apexBundle) HideFromMake() {
815 a.properties.HideFromMake = true
816}
817
Sundong Ahne9b55722019-09-06 17:37:42 +0900818func (a *apexBundle) SetFlattened(flattened bool) {
819 a.properties.Flattened = flattened
820}
821
Martin Stjernholm279de572019-09-10 23:18:20 +0100822func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900823 // Decide the APEX-local directory by the multilib of the library
824 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100825 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900826 case "lib32":
827 dirInApex = "lib"
828 case "lib64":
829 dirInApex = "lib64"
830 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100831 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700832 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100833 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900834 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100835 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
836 // Special case for Bionic libs and other libs installed with them. This is
837 // to prevent those libs from being included in the search path
838 // /apex/com.android.runtime/${LIB}. This exclusion is required because
839 // those libs in the Runtime APEX are available via the legacy paths in
840 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
841 // to the legacy paths and thus will be loaded into the default linker
842 // namespace (aka "platform" namespace). If the libs are directly in
843 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
844 // into the runtime linker namespace, which will result in double loading of
845 // them, which isn't supported.
846 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900847 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900848
Martin Stjernholm279de572019-09-10 23:18:20 +0100849 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900850 return
851}
852
853func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900854 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700855 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200856 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900857 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900858 fileToCopy = cc.OutputFile().Path()
859 return
860}
861
Alex Light778127a2019-02-27 14:19:50 -0800862func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
863 dirInApex = "bin"
864 fileToCopy = py.HostToolPath().Path()
865 return
866}
867func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
868 dirInApex = "bin"
869 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
870 if err != nil {
871 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
872 return
873 }
874 fileToCopy = android.PathForOutput(ctx, s)
875 return
876}
877
Jiyong Park04480cf2019-02-06 00:16:29 +0900878func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
879 dirInApex = filepath.Join("bin", sh.SubDir())
880 fileToCopy = sh.OutputFile()
881 return
882}
883
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900884func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
885 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900886 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900887 return
888}
889
Jiyong Park9e6c2422019-08-09 20:39:45 +0900890func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
891 dirInApex = "javalib"
892 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
893 implJars := java.ImplementationJars()
894 if len(implJars) != 1 {
895 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
896 strings.Join(implJars.Strings(), ", ")))
897 }
898 fileToCopy = implJars[0]
899 return
900}
901
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900902func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
903 dirInApex = filepath.Join("etc", prebuilt.SubDir())
904 fileToCopy = prebuilt.OutputFile()
905 return
906}
907
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900908func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
909 dirInApex = filepath.Join("app", pkgName)
910 fileToCopy = app.OutputFile()
911 return
912}
913
Roland Levillain935639d2019-08-13 14:55:28 +0100914// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
915type flattenedApexContext struct {
916 android.ModuleContext
917}
918
919func (c *flattenedApexContext) InstallBypassMake() bool {
920 return true
921}
922
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900923func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900924 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900925
Alex Light5098a612018-11-29 17:12:15 -0800926 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
927 a.apexTypes = imageApex
928 } else if *a.properties.Payload_type == "zip" {
929 a.apexTypes = zipApex
930 } else if *a.properties.Payload_type == "both" {
931 a.apexTypes = both
932 } else {
933 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
934 return
935 }
936
Roland Levillain630846d2019-06-26 12:48:34 +0100937 if len(a.properties.Tests) > 0 && !a.testApex {
938 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
939 return
940 }
941
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800942 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
943
Jooyung Hane1633032019-08-01 17:41:43 +0900944 // native lib dependencies
945 var provideNativeLibs []string
946 var requireNativeLibs []string
947
Jooyung Han5c998b92019-06-27 11:30:33 +0900948 // Check if "uses" requirements are met with dependent apexBundles
949 var providedNativeSharedLibs []string
950 useVendor := proptools.Bool(a.properties.Use_vendor)
951 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
952 if ctx.OtherModuleDependencyTag(m) != usesTag {
953 return
954 }
955 otherName := ctx.OtherModuleName(m)
956 other, ok := m.(*apexBundle)
957 if !ok {
958 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
959 return
960 }
961 if proptools.Bool(other.properties.Use_vendor) != useVendor {
962 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
963 return
964 }
965 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
966 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
967 return
968 }
969 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
970 })
971
Alex Light778127a2019-02-27 14:19:50 -0800972 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100973 depTag := ctx.OtherModuleDependencyTag(child)
974 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900975 if _, ok := parent.(*apexBundle); ok {
976 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900977 switch depTag {
978 case sharedLibTag:
979 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900980 if cc.HasStubsVariants() {
981 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
982 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100983 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900984 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900985 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900986 } else {
987 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900988 }
989 case executableTag:
990 if cc, ok := child.(*cc.Module); ok {
991 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900992 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900993 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900994 } else if sh, ok := child.(*android.ShBinary); ok {
995 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
996 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800997 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
998 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
999 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1000 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1001 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1002 // NB: Since go binaries are static we don't need the module for anything here, which is
1003 // good since the go tool is a blueprint.Module not an android.Module like we would
1004 // normally use.
1005 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001006 } else {
Alex Light778127a2019-02-27 14:19:50 -08001007 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 +09001008 }
1009 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001010 if javaLib, ok := child.(*java.Library); ok {
1011 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001012 if fileToCopy == nil {
1013 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1014 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001015 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1016 }
1017 return true
1018 } else if javaLib, ok := child.(*java.Import); ok {
1019 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1020 if fileToCopy == nil {
1021 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1022 } else {
1023 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001024 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001025 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001026 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001027 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001028 }
1029 case prebuiltTag:
1030 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1031 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001032 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001033 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001034 } else {
1035 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1036 }
Roland Levillain630846d2019-06-26 12:48:34 +01001037 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001038 if ccTest, ok := child.(*cc.Module); ok {
1039 if ccTest.IsTestPerSrcAllTestsVariation() {
1040 // Multiple-output test module (where `test_per_src: true`).
1041 //
1042 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1043 // We do not add this variation to `filesInfo`, as it has no output;
1044 // however, we do add the other variations of this module as indirect
1045 // dependencies (see below).
1046 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001047 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001048 // Single-output test module (where `test_per_src: false`).
1049 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1050 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001051 }
Roland Levillain630846d2019-06-26 12:48:34 +01001052 return true
1053 } else {
1054 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1055 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001056 case keyTag:
1057 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001058 a.private_key_file = key.private_key_file
1059 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001060 return false
1061 } else {
1062 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001063 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001064 case certificateTag:
1065 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001066 a.container_certificate_file = dep.Certificate.Pem
1067 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001068 return false
1069 } else {
1070 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1071 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001072 case android.PrebuiltDepTag:
1073 // If the prebuilt is force disabled, remember to delete the prebuilt file
1074 // that might have been installed in the previous builds
1075 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1076 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1077 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001078 case androidAppTag:
1079 if ap, ok := child.(*java.AndroidApp); ok {
1080 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1081 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1082 return true
1083 } else {
1084 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1085 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001086 }
1087 } else {
1088 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001089 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001090 // We cannot use a switch statement on `depTag` here as the checked
1091 // tags used below are private (e.g. `cc.sharedDepTag`).
1092 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1093 if cc, ok := child.(*cc.Module); ok {
1094 if android.InList(cc.Name(), providedNativeSharedLibs) {
1095 // If we're using a shared library which is provided from other APEX,
1096 // don't include it in this APEX
1097 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001098 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001099 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1100 // If the dependency is a stubs lib, don't include it in this APEX,
1101 // but make sure that the lib is installed on the device.
1102 // In case no APEX is having the lib, the lib is installed to the system
1103 // partition.
1104 //
1105 // Always include if we are a host-apex however since those won't have any
1106 // system libraries.
1107 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1108 a.externalDeps = append(a.externalDeps, cc.Name())
1109 }
Jooyung Hane1633032019-08-01 17:41:43 +09001110 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001111 // Don't track further
1112 return false
1113 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001114 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001115 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1116 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001117 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001118 } else if cc.IsTestPerSrcDepTag(depTag) {
1119 if cc, ok := child.(*cc.Module); ok {
1120 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1121 // Handle modules created as `test_per_src` variations of a single test module:
1122 // use the name of the generated test binary (`fileToCopy`) instead of the name
1123 // of the original test module (`depName`, shared by all `test_per_src`
1124 // variations of that module).
1125 moduleName := filepath.Base(fileToCopy.String())
1126 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1127 return true
1128 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001129 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001130 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Sundong Ahn2db7f462019-08-27 18:53:12 +09001131 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
1132 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001133 }
1134 }
1135 }
1136 return false
1137 })
1138
Sundong Ahne9b55722019-09-06 17:37:42 +09001139 a.flattenedConfigValue = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1140 if a.flattenedConfigValue {
1141 a.properties.Flattened = true
1142 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001143 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001144 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1145 return
1146 }
1147
Jiyong Park8fd61922018-11-08 02:50:25 +09001148 // remove duplicates in filesInfo
1149 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001150 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001151 result := []apexFile{}
1152 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001153 dest := filepath.Join(f.installDir, f.builtFile.Base())
1154 if !encountered[dest] {
1155 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001156 result = append(result, f)
1157 }
1158 }
1159 return result
1160 }
1161 filesInfo = removeDup(filesInfo)
1162
1163 // to have consistent build rules
1164 sort.Slice(filesInfo, func(i, j int) bool {
1165 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1166 })
1167
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001168 // check no_apex modules
1169 whitelist := whitelistNoApex[ctx.ModuleName()]
1170 for i := range filesInfo {
1171 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1172 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1173 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1174 }
1175 }
1176 }
1177
Jiyong Park8fd61922018-11-08 02:50:25 +09001178 // prepend the name of this APEX to the module names. These names will be the names of
1179 // modules that will be defined if the APEX is flattened.
1180 for i := range filesInfo {
1181 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1182 }
1183
Jiyong Park8fd61922018-11-08 02:50:25 +09001184 a.installDir = android.PathForModuleInstall(ctx, "apex")
1185 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001186
Jooyung Hane1633032019-08-01 17:41:43 +09001187 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1188 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1189 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1190 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1191 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1192 ctx.Build(pctx, android.BuildParams{
1193 Rule: injectApexDependency,
1194 Input: manifestSrc,
1195 Output: a.manifestOut,
1196 Args: map[string]string{
1197 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1198 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1199 },
1200 })
1201
Roland Levillain935639d2019-08-13 14:55:28 +01001202 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1203 // reply true to `InstallBypassMake()` (thus making the call
1204 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1205 // instead of `android.PathForOutput`) to return the correct path to the flattened
1206 // APEX (as its contents is installed by Make, not Soong).
1207 factx := flattenedApexContext{ctx}
1208 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1209
Alex Light5098a612018-11-29 17:12:15 -08001210 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001211 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001212 }
1213 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001214 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001215 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001216 // in other modules. It is in AndroidMk where the selection of flattened
1217 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001218 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001219 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001220 }
1221}
1222
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001223func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001224 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001225 for _, f := range a.filesInfo {
1226 if f.module != nil {
1227 notice := f.module.NoticeFile()
1228 if notice.Valid() {
1229 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001230 }
1231 }
1232 }
1233 // append the notice file specified in the apex module itself
1234 if a.NoticeFile().Valid() {
1235 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001236 }
1237
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001238 if len(noticeFiles) == 0 {
1239 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001240 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001241
Jaewoong Jung98772792019-07-01 17:15:13 -07001242 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001243}
1244
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001245func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001246 cert := String(a.properties.Certificate)
1247 if cert != "" && android.SrcIsModule(cert) == "" {
1248 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001249 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1250 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001251 } else if cert == "" {
1252 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001253 a.container_certificate_file = pem
1254 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001255 }
1256
Alex Light5098a612018-11-29 17:12:15 -08001257 var abis []string
1258 for _, target := range ctx.MultiTargets() {
1259 if len(target.Arch.Abi) > 0 {
1260 abis = append(abis, target.Arch.Abi[0])
1261 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001262 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001263
Alex Light5098a612018-11-29 17:12:15 -08001264 abis = android.FirstUniqueStrings(abis)
1265
1266 suffix := apexType.suffix()
1267 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001268
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001269 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001270 for _, f := range a.filesInfo {
1271 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001272 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001273
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001274 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001275 emitCommands := []string{}
1276 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1277 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001278 for i, src := range filesToCopy {
1279 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001280 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001281 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001282 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1283 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001284 for _, sym := range a.filesInfo[i].symlinks {
1285 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1286 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1287 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001288 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001289 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001290 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001291
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001292 if a.properties.Whitelisted_files != nil {
1293 ctx.Build(pctx, android.BuildParams{
1294 Rule: emitApexContentRule,
1295 Implicits: implicitInputs,
1296 Output: imageContentFile,
1297 Description: "emit apex image content",
1298 Args: map[string]string{
1299 "emit_commands": strings.Join(emitCommands, " && "),
1300 },
1301 })
1302 implicitInputs = append(implicitInputs, imageContentFile)
1303 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1304
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001305 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001306 ctx.Build(pctx, android.BuildParams{
1307 Rule: diffApexContentRule,
1308 Implicits: implicitInputs,
1309 Output: phonyOutput,
1310 Description: "diff apex image content",
1311 Args: map[string]string{
1312 "whitelisted_files_file": whitelistedFilesFile.String(),
1313 "image_content_file": imageContentFile.String(),
1314 "apex_module_name": ctx.ModuleName(),
1315 },
1316 })
1317
1318 implicitInputs = append(implicitInputs, phonyOutput)
1319 }
1320
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001321 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1322 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001323
Alex Light5098a612018-11-29 17:12:15 -08001324 if apexType.image() {
1325 // files and dirs that will be created in APEX
1326 var readOnlyPaths []string
1327 var executablePaths []string // this also includes dirs
1328 for _, f := range a.filesInfo {
1329 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001330 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001331 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001332 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001333 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001334 }
Alex Light5098a612018-11-29 17:12:15 -08001335 } else {
1336 readOnlyPaths = append(readOnlyPaths, pathInApex)
1337 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001338 dir := f.installDir
1339 for !android.InList(dir, executablePaths) && dir != "" {
1340 executablePaths = append(executablePaths, dir)
1341 dir, _ = filepath.Split(dir) // move up to the parent
1342 if len(dir) > 0 {
1343 // remove trailing slash
1344 dir = dir[:len(dir)-1]
1345 }
Alex Light5098a612018-11-29 17:12:15 -08001346 }
1347 }
1348 sort.Strings(readOnlyPaths)
1349 sort.Strings(executablePaths)
1350 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1351 ctx.Build(pctx, android.BuildParams{
1352 Rule: generateFsConfig,
1353 Output: cannedFsConfig,
1354 Description: "generate fs config",
1355 Args: map[string]string{
1356 "ro_paths": strings.Join(readOnlyPaths, " "),
1357 "exec_paths": strings.Join(executablePaths, " "),
1358 },
1359 })
1360
1361 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1362 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1363 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1364 if !fileContextsOptionalPath.Valid() {
1365 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1366 return
1367 }
1368 fileContexts := fileContextsOptionalPath.Path()
1369
Jiyong Park835d82b2018-12-27 16:04:18 +09001370 optFlags := []string{}
1371
Alex Light5098a612018-11-29 17:12:15 -08001372 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001373 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1374 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001375
Jiyong Park7f67f482019-01-05 12:57:48 +09001376 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1377 if overridden {
1378 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1379 }
1380
Jiyong Park40e26a22019-02-08 02:53:06 +09001381 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001382 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001383 implicitInputs = append(implicitInputs, androidManifestFile)
1384 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1385 }
1386
Jiyong Park71b519d2019-04-18 17:25:49 +09001387 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1388 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1389 ctx.Config().UnbundledBuild() &&
1390 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1391 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1392 apiFingerprint := java.ApiFingerprintPath(ctx)
1393 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1394 implicitInputs = append(implicitInputs, apiFingerprint)
1395 }
1396 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1397
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001398 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1399 if noticeFile.Valid() {
1400 // If there's a NOTICE file, embed it as an asset file in the APEX.
1401 implicitInputs = append(implicitInputs, noticeFile.Path())
1402 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1403 }
1404
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001405 if !ctx.Config().UnbundledBuild() && a.installable() {
1406 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1407 // don't need hashtree for activation. Therefore, by removing hashtree from
1408 // apex bundle (filesystem image in it, to be specific), we can save storage.
1409 optFlags = append(optFlags, "--no_hashtree")
1410 }
1411
Alex Light5098a612018-11-29 17:12:15 -08001412 ctx.Build(pctx, android.BuildParams{
1413 Rule: apexRule,
1414 Implicits: implicitInputs,
1415 Output: unsignedOutputFile,
1416 Description: "apex (" + apexType.name() + ")",
1417 Args: map[string]string{
1418 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1419 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1420 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001421 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001422 "file_contexts": fileContexts.String(),
1423 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001424 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001425 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001426 },
1427 })
1428
1429 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1430 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1431 a.bundleModuleFile = bundleModuleFile
1432
1433 ctx.Build(pctx, android.BuildParams{
1434 Rule: apexProtoConvertRule,
1435 Input: unsignedOutputFile,
1436 Output: apexProtoFile,
1437 Description: "apex proto convert",
1438 })
1439
1440 ctx.Build(pctx, android.BuildParams{
1441 Rule: apexBundleRule,
1442 Input: apexProtoFile,
1443 Output: a.bundleModuleFile,
1444 Description: "apex bundle module",
1445 Args: map[string]string{
1446 "abi": strings.Join(abis, "."),
1447 },
1448 })
1449 } else {
1450 ctx.Build(pctx, android.BuildParams{
1451 Rule: zipApexRule,
1452 Implicits: implicitInputs,
1453 Output: unsignedOutputFile,
1454 Description: "apex (" + apexType.name() + ")",
1455 Args: map[string]string{
1456 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1457 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1458 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001459 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001460 },
1461 })
Colin Crossa4925902018-11-16 11:36:28 -08001462 }
Colin Crossa4925902018-11-16 11:36:28 -08001463
Alex Light5098a612018-11-29 17:12:15 -08001464 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001465 ctx.Build(pctx, android.BuildParams{
1466 Rule: java.Signapk,
1467 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001468 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001469 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001470 Implicits: []android.Path{
1471 a.container_certificate_file,
1472 a.container_private_key_file,
1473 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001474 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001475 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001476 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001477 },
1478 })
Alex Light5098a612018-11-29 17:12:15 -08001479
1480 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahn72f1f3e2019-09-12 22:53:00 +09001481 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && (!a.properties.Flattened || a.flattenedConfigValue) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001482 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001483 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001484}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001485
Jiyong Park8fd61922018-11-08 02:50:25 +09001486func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001487 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001488 // 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 +09001489 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001490 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001491
Jiyong Park42cca6c2019-04-01 11:15:50 +09001492 // rename to apex_pubkey
1493 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1494 ctx.Build(pctx, android.BuildParams{
1495 Rule: android.Cp,
1496 Input: a.public_key_file,
1497 Output: copiedPubkey,
1498 })
1499 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1500
Jiyong Park23c52b02019-02-02 13:13:47 +09001501 if ctx.Config().FlattenApex() {
1502 for _, fi := range a.filesInfo {
1503 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001504 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1505 for _, sym := range fi.symlinks {
1506 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1507 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001508 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001509 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001510 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001511}
1512
1513func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001514 if a.properties.HideFromMake {
1515 return android.AndroidMkData{
1516 Disabled: true,
1517 }
1518 }
Alex Light5098a612018-11-29 17:12:15 -08001519 writers := []android.AndroidMkData{}
1520 if a.apexTypes.image() {
1521 writers = append(writers, a.androidMkForType(imageApex))
1522 }
1523 if a.apexTypes.zip() {
1524 writers = append(writers, a.androidMkForType(zipApex))
1525 }
1526 return android.AndroidMkData{
1527 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1528 for _, data := range writers {
1529 data.Custom(w, name, prefix, moduleDir, data)
1530 }
1531 }}
1532}
1533
Alex Lightf1801bc2019-02-13 11:10:07 -08001534func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001535 moduleNames := []string{}
1536
1537 for _, fi := range a.filesInfo {
1538 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1539 continue
1540 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001541 if a.properties.Flattened && !apexType.image() {
1542 continue
Jiyong Park94427262019-02-05 23:18:47 +09001543 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001544
1545 var suffix string
1546 if a.properties.Flattened && !a.flattenedConfigValue {
1547 suffix = ".flattened"
1548 }
1549
1550 if !android.InList(fi.moduleName, moduleNames) {
1551 moduleNames = append(moduleNames, fi.moduleName+suffix)
1552 }
1553
Jiyong Park94427262019-02-05 23:18:47 +09001554 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1555 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001556 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName+suffix)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001557 // /apex/<name>/{lib|framework|...}
1558 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1559 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001560 if a.properties.Flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001561 // /system/apex/<name>/{lib|framework|...}
1562 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1563 a.installDir.RelPathString(), name, fi.installDir))
Sundong Ahne9b55722019-09-06 17:37:42 +09001564 if a.flattenedConfigValue {
1565 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1566 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001567 if len(fi.symlinks) > 0 {
1568 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1569 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001570
1571 if fi.module != nil && fi.module.NoticeFile().Valid() {
1572 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1573 }
Jiyong Park94427262019-02-05 23:18:47 +09001574 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001575 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001576 }
1577 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1578 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1579 if fi.module != nil {
1580 archStr := fi.module.Target().Arch.ArchType.String()
1581 host := false
1582 switch fi.module.Target().Os.Class {
1583 case android.Host:
1584 if archStr != "common" {
1585 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1586 }
1587 host = true
1588 case android.HostCross:
1589 if archStr != "common" {
1590 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1591 }
1592 host = true
1593 case android.Device:
1594 if archStr != "common" {
1595 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1596 }
1597 }
1598 if host {
1599 makeOs := fi.module.Target().Os.String()
1600 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1601 makeOs = "linux"
1602 }
1603 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1604 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1605 }
1606 }
1607 if fi.class == javaSharedLib {
1608 javaModule := fi.module.(*java.Library)
1609 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1610 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1611 // we will have foo.jar.jar
1612 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1613 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1614 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1615 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1616 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1617 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001618 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001619 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001620 if cc, ok := fi.module.(*cc.Module); ok {
1621 if cc.UnstrippedOutputFile() != nil {
1622 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1623 }
1624 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001625 if cc.CoverageOutputFile().Valid() {
1626 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1627 }
Jiyong Park94427262019-02-05 23:18:47 +09001628 }
1629 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1630 } else {
1631 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1632 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1633 }
1634 }
1635 return moduleNames
1636}
1637
Alex Light5098a612018-11-29 17:12:15 -08001638func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001639 return android.AndroidMkData{
1640 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1641 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001642 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001643 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001644 }
1645
Sundong Ahne9b55722019-09-06 17:37:42 +09001646 if a.properties.Flattened && !a.flattenedConfigValue {
1647 name = name + ".flattened"
1648 }
1649
1650 if a.properties.Flattened && apexType.image() {
Jiyong Park719b4462019-01-13 00:39:51 +09001651 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001652 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1653 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1654 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001655 if len(moduleNames) > 0 {
1656 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1657 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001658 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001659 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1660
Sundong Ahn72f1f3e2019-09-12 22:53:00 +09001661 } else if !a.properties.Flattened || a.flattenedConfigValue {
Alex Light5098a612018-11-29 17:12:15 -08001662 // zip-apex is the less common type so have the name refer to the image-apex
1663 // only and use {name}.zip if you want the zip-apex
1664 if apexType == zipApex && a.apexTypes == both {
1665 name = name + ".zip"
1666 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001667 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1668 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1669 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1670 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001671 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001672 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001673 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001674 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001675 if len(moduleNames) > 0 {
1676 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1677 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001678 if len(a.externalDeps) > 0 {
1679 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1680 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001681 if a.prebuiltFileToDelete != "" {
1682 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1683 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1684 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001685 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001686
Alex Light5098a612018-11-29 17:12:15 -08001687 if apexType == imageApex {
1688 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1689 }
Jiyong Park719b4462019-01-13 00:39:51 +09001690 }
1691 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001692}
1693
Jooyung Han344d5432019-08-23 11:17:39 +09001694func newApexBundle() *apexBundle {
Alex Light5098a612018-11-29 17:12:15 -08001695 module := &apexBundle{
1696 outputFiles: map[apexPackaging]android.WritablePath{},
1697 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001698 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001699 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001700 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001701 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1702 })
Alex Light5098a612018-11-29 17:12:15 -08001703 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001704 android.InitDefaultableModule(module)
1705 return module
1706}
Jiyong Park30ca9372019-02-07 16:27:23 +09001707
Jooyung Han344d5432019-08-23 11:17:39 +09001708func ApexBundleFactory(testApex bool) android.Module {
1709 bundle := newApexBundle()
1710 bundle.testApex = testApex
1711 return bundle
1712}
1713
1714func testApexBundleFactory() android.Module {
1715 bundle := newApexBundle()
1716 bundle.testApex = true
1717 return bundle
1718}
1719
1720func apexBundleFactory() android.Module {
1721 return newApexBundle()
1722}
1723
1724// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1725// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1726// If not specified, then the "current" versions are gathered.
1727func vndkApexBundleFactory() android.Module {
1728 bundle := newApexBundle()
1729 bundle.vndkApex = true
1730 bundle.AddProperties(&bundle.vndkProperties)
1731 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1732 ctx.AppendProperties(&struct {
1733 Compile_multilib *string
1734 }{
1735 proptools.StringPtr("both"),
1736 })
1737 })
1738 return bundle
1739}
1740
Jiyong Park30ca9372019-02-07 16:27:23 +09001741//
1742// Defaults
1743//
1744type Defaults struct {
1745 android.ModuleBase
1746 android.DefaultsModuleBase
1747}
1748
Jiyong Park30ca9372019-02-07 16:27:23 +09001749func defaultsFactory() android.Module {
1750 return DefaultsFactory()
1751}
1752
1753func DefaultsFactory(props ...interface{}) android.Module {
1754 module := &Defaults{}
1755
1756 module.AddProperties(props...)
1757 module.AddProperties(
1758 &apexBundleProperties{},
1759 &apexTargetBundleProperties{},
1760 )
1761
1762 android.InitDefaultsModule(module)
1763 return module
1764}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001765
1766//
1767// Prebuilt APEX
1768//
1769type Prebuilt struct {
1770 android.ModuleBase
1771 prebuilt android.Prebuilt
1772
1773 properties PrebuiltProperties
1774
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001775 inputApex android.Path
1776 installDir android.OutputPath
1777 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001778 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001779}
1780
1781type PrebuiltProperties struct {
1782 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001783 Source string `blueprint:"mutated"`
1784 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001785
1786 Src *string
1787 Arch struct {
1788 Arm struct {
1789 Src *string
1790 }
1791 Arm64 struct {
1792 Src *string
1793 }
1794 X86 struct {
1795 Src *string
1796 }
1797 X86_64 struct {
1798 Src *string
1799 }
1800 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001801
1802 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001803 // Optional name for the installed apex. If unspecified, name of the
1804 // module is used as the file name
1805 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001806
1807 // Names of modules to be overridden. Listed modules can only be other binaries
1808 // (in Make or Soong).
1809 // This does not completely prevent installation of the overridden binaries, but if both
1810 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1811 // from PRODUCT_PACKAGES.
1812 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001813}
1814
1815func (p *Prebuilt) installable() bool {
1816 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001817}
1818
1819func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001820 // If the device is configured to use flattened APEX, force disable the prebuilt because
1821 // the prebuilt is a non-flattened one.
1822 forceDisable := ctx.Config().FlattenApex()
1823
1824 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1825 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001826 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001827
Kun Niu10c9f832019-07-29 16:28:57 -07001828 // Force disable the prebuilts when coverage is enabled.
1829 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1830 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1831
Jiyong Park50b81e52019-07-11 11:24:41 +09001832 // b/137216042 don't use prebuilts when address sanitizer is on
1833 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1834 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1835
1836 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001837 p.properties.ForceDisable = true
1838 return
1839 }
1840
Jiyong Parkc95714e2019-03-29 14:23:10 +09001841 // This is called before prebuilt_select and prebuilt_postdeps mutators
1842 // The mutators requires that src to be set correctly for each arch so that
1843 // arch variants are disabled when src is not provided for the arch.
1844 if len(ctx.MultiTargets()) != 1 {
1845 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1846 return
1847 }
1848 var src string
1849 switch ctx.MultiTargets()[0].Arch.ArchType {
1850 case android.Arm:
1851 src = String(p.properties.Arch.Arm.Src)
1852 case android.Arm64:
1853 src = String(p.properties.Arch.Arm64.Src)
1854 case android.X86:
1855 src = String(p.properties.Arch.X86.Src)
1856 case android.X86_64:
1857 src = String(p.properties.Arch.X86_64.Src)
1858 default:
1859 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1860 return
1861 }
1862 if src == "" {
1863 src = String(p.properties.Src)
1864 }
1865 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001866}
1867
Jiyong Park03b68dd2019-07-26 23:20:40 +09001868func (p *Prebuilt) isForceDisabled() bool {
1869 return p.properties.ForceDisable
1870}
1871
Colin Cross41955e82019-05-29 14:40:35 -07001872func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1873 switch tag {
1874 case "":
1875 return android.Paths{p.outputApex}, nil
1876 default:
1877 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1878 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001879}
1880
Jiyong Park4d277042019-04-23 18:00:10 +09001881func (p *Prebuilt) InstallFilename() string {
1882 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1883}
1884
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001885func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001886 if p.properties.ForceDisable {
1887 return
1888 }
1889
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001890 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001891 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001892 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001893 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001894 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1895 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1896 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001897 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1898 ctx.Build(pctx, android.BuildParams{
1899 Rule: android.Cp,
1900 Input: p.inputApex,
1901 Output: p.outputApex,
1902 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001903 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001904 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001905 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001906}
1907
1908func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1909 return &p.prebuilt
1910}
1911
1912func (p *Prebuilt) Name() string {
1913 return p.prebuilt.Name(p.ModuleBase.Name())
1914}
1915
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001916func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1917 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001918 Class: "ETC",
1919 OutputFile: android.OptionalPathForPath(p.inputApex),
1920 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001921 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1922 func(entries *android.AndroidMkEntries) {
1923 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1924 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1925 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1926 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1927 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001928 },
1929 }
1930}
1931
1932// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1933func PrebuiltFactory() android.Module {
1934 module := &Prebuilt{}
1935 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001936 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001937 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001938 return module
1939}