blob: b02346a7e76cd4949ab0ab255b8bc02e4d875971 [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 Hand15aa1f2019-09-27 00:38:03 +090050 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
Jooyung Hane1633032019-08-01 17:41:43 +090051 Command: `rm -f $out && ${jsonmodify} $in ` +
52 `-a provideNativeLibs ${provideNativeLibs} ` +
Jooyung Hand15aa1f2019-09-27 00:38:03 +090053 `-a requireNativeLibs ${requireNativeLibs} ` +
54 `${opt} ` +
55 `-o $out`,
Jooyung Hane1633032019-08-01 17:41:43 +090056 CommandDeps: []string{"${jsonmodify}"},
Jooyung Hand15aa1f2019-09-27 00:38:03 +090057 Description: "prepare ${out}",
58 }, "provideNativeLibs", "requireNativeLibs", "opt")
Jooyung Hane1633032019-08-01 17:41:43 +090059
Jiyong Park48ca7dc2018-10-10 14:01:00 +090060 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
61 // against the binary policy using sefcontext_compiler -p <policy>.
62
63 // TODO(b/114327326): automate the generation of file_contexts
64 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
65 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010066 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090067 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090068 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090069 `--file_contexts ${file_contexts} ` +
70 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080071 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090072 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090073 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
74 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000075 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010076 Rspfile: "${out}.copy_commands",
77 RspfileContent: "${copy_commands}",
78 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090079 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080080
Alex Light5098a612018-11-29 17:12:15 -080081 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
82 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010083 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080084 `APEXER_TOOL_PATH=${tool_path} ` +
85 `${apexer} --force --manifest ${manifest} ` +
86 `--payload_type zip ` +
87 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010088 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
89 Rspfile: "${out}.copy_commands",
90 RspfileContent: "${copy_commands}",
91 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080092 }, "tool_path", "image_dir", "copy_commands", "manifest")
93
Colin Crossa4925902018-11-16 11:36:28 -080094 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
95 blueprint.RuleParams{
96 Command: `${aapt2} convert --output-format proto $in -o $out`,
97 CommandDeps: []string{"${aapt2}"},
98 })
99
100 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +0900101 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +0000102 `apex_payload.img:apex/${abi}.img ` +
103 `apex_manifest.json:root/apex_manifest.json ` +
Jaewoong Jungb00c1fb2019-09-04 13:26:18 -0700104 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
105 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -0800106 CommandDeps: []string{"${zip2zip}"},
107 Description: "app bundle",
108 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100109
110 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
111 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
112 Rspfile: "${out}.emit_commands",
113 RspfileContent: "${emit_commands}",
114 Description: "Emit APEX image content",
115 }, "emit_commands")
116
117 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
118 Command: `diff --unchanged-group-format='' \` +
119 `--changed-group-format='%<' \` +
120 `${image_content_file} ${whitelisted_files_file} || (` +
121 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
122 ` "To fix the build run following command:" && ` +
123 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
124 `exit 1)`,
125 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
126 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900127)
128
Alex Light5098a612018-11-29 17:12:15 -0800129var imageApexSuffix = ".apex"
130var zipApexSuffix = ".zipapex"
131
132var imageApexType = "image"
133var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900134
135type dependencyTag struct {
136 blueprint.BaseDependencyTag
137 name string
138}
139
140var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900141 sharedLibTag = dependencyTag{name: "sharedLib"}
142 executableTag = dependencyTag{name: "executable"}
143 javaLibTag = dependencyTag{name: "javaLib"}
144 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100145 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900146 keyTag = dependencyTag{name: "key"}
147 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900148 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900149 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900150)
151
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900152var (
153 whitelistNoApex = map[string][]string{
154 "apex_test_build_features": []string{"libbinder"},
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900155 "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
Jiyong Parkd1063c12019-07-17 20:08:41 +0900188 android.RegisterModuleType("apex", BundleFactory)
Alex Light0851b882019-02-07 13:20:53 -0800189 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 Parkd1063c12019-07-17 20:08:41 +0900198 android.PostDepsMutators(RegisterPostDepsMutators)
199}
200
201func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
202 ctx.TopDown("apex_deps", apexDepsMutator)
203 ctx.BottomUp("apex", apexMutator).Parallel()
204 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
205 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900206}
207
Jooyung Han344d5432019-08-23 11:17:39 +0900208var (
209 vndkApexListKey = android.NewOnceKey("vndkApexList")
210 vndkApexListMutex sync.Mutex
211)
212
213func vndkApexList(config android.Config) map[string]*apexBundle {
214 return config.Once(vndkApexListKey, func() interface{} {
215 return map[string]*apexBundle{}
216 }).(map[string]*apexBundle)
217}
218
219// apexVndkGatherMutator gathers "apex_vndk" modules and puts them in a map with vndk_version as a key.
220func apexVndkGatherMutator(mctx android.TopDownMutatorContext) {
221 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
222 if ab.IsNativeBridgeSupported() {
223 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
224 }
Jooyung Han90eee022019-10-01 20:02:42 +0900225
226 vndkVersion := proptools.String(ab.vndkProperties.Vndk_version)
227
Jooyung Han344d5432019-08-23 11:17:39 +0900228 vndkApexListMutex.Lock()
229 defer vndkApexListMutex.Unlock()
230 vndkApexList := vndkApexList(mctx.Config())
231 if other, ok := vndkApexList[vndkVersion]; ok {
Jooyung Han90eee022019-10-01 20:02:42 +0900232 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other.BaseModuleName())
Jooyung Han344d5432019-08-23 11:17:39 +0900233 }
234 vndkApexList[vndkVersion] = ab
235 }
236}
237
238// apexVndkAddDepsMutator adds (reverse) dependencies from vndk libs to apex_vndk modules.
239// It filters only libs with matching targets.
240func apexVndkAddDepsMutator(mctx android.BottomUpMutatorContext) {
241 if cc, ok := mctx.Module().(*cc.Module); ok && cc.IsVndkOnSystem() {
242 vndkApexList := vndkApexList(mctx.Config())
243 if ab, ok := vndkApexList[cc.VndkVersion()]; ok {
244 targetArch := cc.Target().String()
245 for _, target := range ab.MultiTargets() {
246 if target.String() == targetArch {
247 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, ab.Name())
248 break
249 }
250 }
251 }
252 }
253}
254
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900255// Mark the direct and transitive dependencies of apex bundles so that they
256// can be built for the apex bundles.
257func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800258 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800259 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900260 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900261 depName := mctx.OtherModuleName(child)
262 // If the parent is apexBundle, this child is directly depended.
263 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800264 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800265 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
266 // non-installable apex's cannot be installed and so should not prevent libraries from being
267 // installed to the system.
268 android.UpdateApexDependency(apexBundleName, depName, directDep)
269 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900270
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900271 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900272 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900273 return true
274 } else {
275 return false
276 }
277 })
278 }
279}
280
281// Create apex variations if a module is included in APEX(s).
282func apexMutator(mctx android.BottomUpMutatorContext) {
283 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900284 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900285 } else if _, ok := mctx.Module().(*apexBundle); ok {
286 // apex bundle itself is mutated so that it and its modules have same
287 // apex variant.
288 apexBundleName := mctx.ModuleName()
289 mctx.CreateVariations(apexBundleName)
290 }
291}
Sundong Ahne9b55722019-09-06 17:37:42 +0900292
293func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900294 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahne9b55722019-09-06 17:37:42 +0900295 if !mctx.Config().FlattenApex() || mctx.Config().UnbundledBuild() {
296 modules := mctx.CreateLocalVariations("", "flattened")
297 modules[0].(*apexBundle).SetFlattened(false)
298 modules[1].(*apexBundle).SetFlattened(true)
Sundong Ahne8fb7242019-09-17 13:50:45 +0900299 } else {
300 ab.SetFlattened(true)
301 ab.SetFlattenedConfigValue()
Sundong Ahne9b55722019-09-06 17:37:42 +0900302 }
303 }
304}
305
Jooyung Han5c998b92019-06-27 11:30:33 +0900306func apexUsesMutator(mctx android.BottomUpMutatorContext) {
307 if ab, ok := mctx.Module().(*apexBundle); ok {
308 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
309 }
310}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900311
Alex Light9670d332019-01-29 18:07:33 -0800312type apexNativeDependencies struct {
313 // List of native libraries
314 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900315
Alex Light9670d332019-01-29 18:07:33 -0800316 // List of native executables
317 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900318
Roland Levillain630846d2019-06-26 12:48:34 +0100319 // List of native tests
320 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800321}
Jooyung Han344d5432019-08-23 11:17:39 +0900322
Alex Light9670d332019-01-29 18:07:33 -0800323type apexMultilibProperties struct {
324 // Native dependencies whose compile_multilib is "first"
325 First apexNativeDependencies
326
327 // Native dependencies whose compile_multilib is "both"
328 Both apexNativeDependencies
329
330 // Native dependencies whose compile_multilib is "prefer32"
331 Prefer32 apexNativeDependencies
332
333 // Native dependencies whose compile_multilib is "32"
334 Lib32 apexNativeDependencies
335
336 // Native dependencies whose compile_multilib is "64"
337 Lib64 apexNativeDependencies
338}
339
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900340type apexBundleProperties struct {
341 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000342 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800343 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900344
Jiyong Park40e26a22019-02-08 02:53:06 +0900345 // AndroidManifest.xml file used for the zip container of this APEX bundle.
346 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800347 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900348
Roland Levillain411c5842019-09-19 16:37:20 +0100349 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
350 // device (/apex/<apex_name>).
351 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900352 Apex_name *string
353
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900354 // Determines the file contexts file for setting security context to each file in this APEX bundle.
355 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
356 // used.
357 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900358 File_contexts *string
359
360 // List of native shared libs that are embedded inside this APEX bundle
361 Native_shared_libs []string
362
Roland Levillain630846d2019-06-26 12:48:34 +0100363 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900364 Binaries []string
365
366 // List of java libraries that are embedded inside this APEX bundle
367 Java_libs []string
368
369 // List of prebuilt files that are embedded inside this APEX bundle
370 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900371
Roland Levillain630846d2019-06-26 12:48:34 +0100372 // List of tests that are embedded inside this APEX bundle
373 Tests []string
374
Jiyong Parkff1458f2018-10-12 21:49:38 +0900375 // Name of the apex_key module that provides the private key to sign APEX
376 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900377
Alex Light5098a612018-11-29 17:12:15 -0800378 // The type of APEX to build. Controls what the APEX payload is. Either
379 // 'image', 'zip' or 'both'. Default: 'image'.
380 Payload_type *string
381
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900382 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
383 // or an android_app_certificate module name in the form ":module".
384 Certificate *string
385
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900386 // Whether this APEX is installable to one of the partitions. Default: true.
387 Installable *bool
388
Jiyong Parkda6eb592018-12-19 17:12:36 +0900389 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
390 // Default is false.
391 Use_vendor *bool
392
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800393 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
394 Ignore_system_library_special_case *bool
395
Alex Light9670d332019-01-29 18:07:33 -0800396 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900397
Jiyong Parkf97782b2019-02-13 20:28:58 +0900398 // List of sanitizer names that this APEX is enabled for
399 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900400
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900401 PreventInstall bool `blueprint:"mutated"`
402
403 HideFromMake bool `blueprint:"mutated"`
404
Jooyung Han5c998b92019-06-27 11:30:33 +0900405 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
406 Provide_cpp_shared_libs *bool
407
408 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
409 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100410
411 // A txt file containing list of files that are whitelisted to be included in this APEX.
412 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900413
414 // List of APKs to package inside APEX
415 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900416
Sundong Ahne8fb7242019-09-17 13:50:45 +0900417 // To distinguish between flattened and non-flattened apex.
418 // if set true, then output files are flattened.
Sundong Ahne9b55722019-09-06 17:37:42 +0900419 Flattened bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +0900420
Sundong Ahne8fb7242019-09-17 13:50:45 +0900421 // if true, it means that TARGET_FLATTEN_APEX is true and
422 // TARGET_BUILD_APPS is false
423 FlattenedConfigValue bool `blueprint:"mutated"`
424
Jiyong Parkd1063c12019-07-17 20:08:41 +0900425 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
426 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
427 // is implied. This value affects all modules included in this APEX. In other words, they are
428 // also built with the SDKs specified here.
429 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800430}
431
432type apexTargetBundleProperties struct {
433 Target struct {
434 // Multilib properties only for android.
435 Android struct {
436 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900437 }
Jooyung Han344d5432019-08-23 11:17:39 +0900438
Alex Light9670d332019-01-29 18:07:33 -0800439 // Multilib properties only for host.
440 Host struct {
441 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900442 }
Jooyung Han344d5432019-08-23 11:17:39 +0900443
Alex Light9670d332019-01-29 18:07:33 -0800444 // Multilib properties only for host linux_bionic.
445 Linux_bionic struct {
446 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900447 }
Jooyung Han344d5432019-08-23 11:17:39 +0900448
Alex Light9670d332019-01-29 18:07:33 -0800449 // Multilib properties only for host linux_glibc.
450 Linux_glibc struct {
451 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900452 }
453 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900454}
455
Jooyung Han344d5432019-08-23 11:17:39 +0900456type apexVndkProperties struct {
457 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
458 Vndk_version *string
459}
460
Jiyong Park8fd61922018-11-08 02:50:25 +0900461type apexFileClass int
462
463const (
464 etc apexFileClass = iota
465 nativeSharedLib
466 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900467 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800468 pyBinary
469 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900470 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100471 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900472 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900473)
474
Alex Light5098a612018-11-29 17:12:15 -0800475type apexPackaging int
476
477const (
478 imageApex apexPackaging = iota
479 zipApex
480 both
481)
482
483func (a apexPackaging) image() bool {
484 switch a {
485 case imageApex, both:
486 return true
487 }
488 return false
489}
490
491func (a apexPackaging) zip() bool {
492 switch a {
493 case zipApex, both:
494 return true
495 }
496 return false
497}
498
499func (a apexPackaging) suffix() string {
500 switch a {
501 case imageApex:
502 return imageApexSuffix
503 case zipApex:
504 return zipApexSuffix
505 case both:
506 panic(fmt.Errorf("must be either zip or image"))
507 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100508 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800509 }
510}
511
512func (a apexPackaging) name() string {
513 switch a {
514 case imageApex:
515 return imageApexType
516 case zipApex:
517 return zipApexType
518 case both:
519 panic(fmt.Errorf("must be either zip or image"))
520 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100521 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800522 }
523}
524
Jiyong Park8fd61922018-11-08 02:50:25 +0900525func (class apexFileClass) NameInMake() string {
526 switch class {
527 case etc:
528 return "ETC"
529 case nativeSharedLib:
530 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800531 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900532 return "EXECUTABLES"
533 case javaSharedLib:
534 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100535 case nativeTest:
536 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900537 case app:
538 return "APPS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900539 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100540 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900541 }
542}
543
544type apexFile struct {
545 builtFile android.Path
546 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900547 installDir string
548 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900549 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800550 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900551}
552
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900553type apexBundle struct {
554 android.ModuleBase
555 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900556 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900557
Alex Light9670d332019-01-29 18:07:33 -0800558 properties apexBundleProperties
559 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900560 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900561
Alex Light5098a612018-11-29 17:12:15 -0800562 apexTypes apexPackaging
563
Colin Crossa4925902018-11-16 11:36:28 -0800564 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800565 outputFiles map[apexPackaging]android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700566 flattenedOutput android.InstallPath
567 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900568
Jiyong Park03b68dd2019-07-26 23:20:40 +0900569 prebuiltFileToDelete string
570
Jiyong Park42cca6c2019-04-01 11:15:50 +0900571 public_key_file android.Path
572 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900573
574 container_certificate_file android.Path
575 container_private_key_file android.Path
576
Jiyong Park8fd61922018-11-08 02:50:25 +0900577 // list of files to be included in this apex
578 filesInfo []apexFile
579
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900580 // list of module names that this APEX is depending on
581 externalDeps []string
582
Alex Light0851b882019-02-07 13:20:53 -0800583 testApex bool
Jooyung Han344d5432019-08-23 11:17:39 +0900584 vndkApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900585
586 // intermediate path for apex_manifest.json
587 manifestOut android.WritablePath
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900588}
589
Jiyong Park397e55e2018-10-24 21:09:55 +0900590func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100591 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700592 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900593 // Use *FarVariation* to be able to depend on modules having
594 // conflicting variations with this module. This is required since
595 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
596 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700597 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900598 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900599 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900600 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700601 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900602
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700603 ctx.AddFarVariationDependencies(append(target.Variations(),
604 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
605 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100606
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700607 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100608 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100609 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700610 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900611}
612
Alex Light9670d332019-01-29 18:07:33 -0800613func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
614 if ctx.Os().Class == android.Device {
615 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
616 } else {
617 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
618 if ctx.Os().Bionic() {
619 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
620 } else {
621 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
622 }
623 }
624}
625
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800627
Jiyong Park397e55e2018-10-24 21:09:55 +0900628 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900629 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800630
631 a.combineProperties(ctx)
632
Jiyong Park397e55e2018-10-24 21:09:55 +0900633 has32BitTarget := false
634 for _, target := range targets {
635 if target.Arch.ArchType.Multilib == "lib32" {
636 has32BitTarget = true
637 }
638 }
639 for i, target := range targets {
640 // When multilib.* is omitted for native_shared_libs, it implies
641 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700642 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900643 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900644 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700645 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900646
Roland Levillain630846d2019-06-26 12:48:34 +0100647 // When multilib.* is omitted for tests, it implies
648 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700649 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100650 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100651 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700652 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100653
Jiyong Park397e55e2018-10-24 21:09:55 +0900654 // Add native modules targetting both ABIs
655 addDependenciesForNativeModules(ctx,
656 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100657 a.properties.Multilib.Both.Binaries,
658 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700659 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900660 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900661
Alex Light3d673592019-01-18 14:37:31 -0800662 isPrimaryAbi := i == 0
663 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900664 // When multilib.* is omitted for binaries, it implies
665 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700666 ctx.AddFarVariationDependencies(append(target.Variations(),
667 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
668 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900669
670 // Add native modules targetting the first ABI
671 addDependenciesForNativeModules(ctx,
672 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100673 a.properties.Multilib.First.Binaries,
674 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700675 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900676 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800677
678 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700679 ctx.AddFarVariationDependencies(target.Variations(),
680 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900681 }
682
683 switch target.Arch.ArchType.Multilib {
684 case "lib32":
685 // Add native modules targetting 32-bit ABI
686 addDependenciesForNativeModules(ctx,
687 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100688 a.properties.Multilib.Lib32.Binaries,
689 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700690 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900691 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900692
693 addDependenciesForNativeModules(ctx,
694 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100695 a.properties.Multilib.Prefer32.Binaries,
696 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700697 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900698 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900699 case "lib64":
700 // Add native modules targetting 64-bit ABI
701 addDependenciesForNativeModules(ctx,
702 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100703 a.properties.Multilib.Lib64.Binaries,
704 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700705 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900706 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900707
708 if !has32BitTarget {
709 addDependenciesForNativeModules(ctx,
710 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100711 a.properties.Multilib.Prefer32.Binaries,
712 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700713 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900714 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900715 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700716
717 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
718 for _, sanitizer := range ctx.Config().SanitizeDevice() {
719 if sanitizer == "hwaddress" {
720 addDependenciesForNativeModules(ctx,
721 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700722 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700723 break
724 }
725 }
726 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900727 }
728
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900729 }
730
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700731 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
732 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900733
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700734 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
735 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900736
Jiyong Park23c52b02019-02-02 13:13:47 +0900737 if String(a.properties.Key) == "" {
738 ctx.ModuleErrorf("key is missing")
739 return
740 }
741 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900742
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900743 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900744 if cert != "" {
745 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900746 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900747
748 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
749 if len(a.properties.Uses_sdks) > 0 {
750 sdkRefs := []android.SdkRef{}
751 for _, str := range a.properties.Uses_sdks {
752 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
753 sdkRefs = append(sdkRefs, parsed)
754 }
755 a.BuildWithSdks(sdkRefs)
756 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900757}
758
Colin Cross0ea8ba82019-06-06 14:33:29 -0700759func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900760 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
761 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000762 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900763 }
764 return String(a.properties.Certificate)
765}
766
Colin Cross41955e82019-05-29 14:40:35 -0700767func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
768 switch tag {
769 case "":
770 if file, ok := a.outputFiles[imageApex]; ok {
771 return android.Paths{file}, nil
772 } else {
773 return nil, nil
774 }
Roland Levillain935639d2019-08-13 14:55:28 +0100775 case ".flattened":
Sundong Ahne9b55722019-09-06 17:37:42 +0900776 if a.properties.Flattened {
Roland Levillain935639d2019-08-13 14:55:28 +0100777 flattenedApexPath := a.flattenedOutput
778 return android.Paths{flattenedApexPath}, nil
779 } else {
780 return nil, nil
781 }
Colin Cross41955e82019-05-29 14:40:35 -0700782 default:
783 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900784 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900785}
786
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900787func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900788 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900789}
790
Jiyong Park7c1dc612019-01-05 11:15:24 +0900791func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
792 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900793 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900794 } else {
795 return "core"
796 }
797}
798
Jiyong Parkf97782b2019-02-13 20:28:58 +0900799func (a *apexBundle) EnableSanitizer(sanitizerName string) {
800 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
801 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
802 }
803}
804
Jiyong Park388ef3f2019-01-28 19:47:32 +0900805func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900806 if android.InList(sanitizerName, a.properties.SanitizerNames) {
807 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900808 }
809
810 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900811 globalSanitizerNames := []string{}
812 if a.Host() {
813 globalSanitizerNames = ctx.Config().SanitizeHost()
814 } else {
815 arches := ctx.Config().SanitizeDeviceArch()
816 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
817 globalSanitizerNames = ctx.Config().SanitizeDevice()
818 }
819 }
820 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900821}
822
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900823func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
824 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
825}
826
827func (a *apexBundle) PreventInstall() {
828 a.properties.PreventInstall = true
829}
830
831func (a *apexBundle) HideFromMake() {
832 a.properties.HideFromMake = true
833}
834
Sundong Ahne9b55722019-09-06 17:37:42 +0900835func (a *apexBundle) SetFlattened(flattened bool) {
836 a.properties.Flattened = flattened
837}
838
Sundong Ahne8fb7242019-09-17 13:50:45 +0900839func (a *apexBundle) SetFlattenedConfigValue() {
840 a.properties.FlattenedConfigValue = true
841}
842
843// isFlattenedVariant returns true when the current module is the flattened
844// variant of an apex that has both a flattened and an unflattened variant.
845// It returns false when the current module is flattened but there is no
846// unflattened variant, which occurs when ctx.Config().FlattenedApex() returns
847// true. It can be used to avoid collisions between the install paths of the
848// flattened and unflattened variants.
849func (a *apexBundle) isFlattenedVariant() bool {
850 return a.properties.Flattened && !a.properties.FlattenedConfigValue
851}
852
Martin Stjernholm279de572019-09-10 23:18:20 +0100853func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900854 // Decide the APEX-local directory by the multilib of the library
855 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100856 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900857 case "lib32":
858 dirInApex = "lib"
859 case "lib64":
860 dirInApex = "lib64"
861 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100862 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700863 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100864 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900865 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100866 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
867 // Special case for Bionic libs and other libs installed with them. This is
868 // to prevent those libs from being included in the search path
869 // /apex/com.android.runtime/${LIB}. This exclusion is required because
870 // those libs in the Runtime APEX are available via the legacy paths in
871 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
872 // to the legacy paths and thus will be loaded into the default linker
873 // namespace (aka "platform" namespace). If the libs are directly in
874 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
875 // into the runtime linker namespace, which will result in double loading of
876 // them, which isn't supported.
877 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900878 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900879
Martin Stjernholm279de572019-09-10 23:18:20 +0100880 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900881 return
882}
883
884func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900885 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700886 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200887 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900888 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900889 fileToCopy = cc.OutputFile().Path()
890 return
891}
892
Alex Light778127a2019-02-27 14:19:50 -0800893func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
894 dirInApex = "bin"
895 fileToCopy = py.HostToolPath().Path()
896 return
897}
898func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
899 dirInApex = "bin"
900 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
901 if err != nil {
902 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
903 return
904 }
905 fileToCopy = android.PathForOutput(ctx, s)
906 return
907}
908
Jiyong Park04480cf2019-02-06 00:16:29 +0900909func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
910 dirInApex = filepath.Join("bin", sh.SubDir())
911 fileToCopy = sh.OutputFile()
912 return
913}
914
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900915func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
916 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900917 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900918 return
919}
920
Jiyong Park9e6c2422019-08-09 20:39:45 +0900921func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
922 dirInApex = "javalib"
923 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
924 implJars := java.ImplementationJars()
925 if len(implJars) != 1 {
926 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
927 strings.Join(implJars.Strings(), ", ")))
928 }
929 fileToCopy = implJars[0]
930 return
931}
932
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900933func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
934 dirInApex = filepath.Join("etc", prebuilt.SubDir())
935 fileToCopy = prebuilt.OutputFile()
936 return
937}
938
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900939func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
940 dirInApex = filepath.Join("app", pkgName)
941 fileToCopy = app.OutputFile()
942 return
943}
944
Roland Levillain935639d2019-08-13 14:55:28 +0100945// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
946type flattenedApexContext struct {
947 android.ModuleContext
948}
949
950func (c *flattenedApexContext) InstallBypassMake() bool {
951 return true
952}
953
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900954func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900955 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900956
Alex Light5098a612018-11-29 17:12:15 -0800957 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
958 a.apexTypes = imageApex
959 } else if *a.properties.Payload_type == "zip" {
960 a.apexTypes = zipApex
961 } else if *a.properties.Payload_type == "both" {
962 a.apexTypes = both
963 } else {
964 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
965 return
966 }
967
Roland Levillain630846d2019-06-26 12:48:34 +0100968 if len(a.properties.Tests) > 0 && !a.testApex {
969 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
970 return
971 }
972
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800973 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
974
Jooyung Hane1633032019-08-01 17:41:43 +0900975 // native lib dependencies
976 var provideNativeLibs []string
977 var requireNativeLibs []string
978
Jooyung Han5c998b92019-06-27 11:30:33 +0900979 // Check if "uses" requirements are met with dependent apexBundles
980 var providedNativeSharedLibs []string
981 useVendor := proptools.Bool(a.properties.Use_vendor)
982 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
983 if ctx.OtherModuleDependencyTag(m) != usesTag {
984 return
985 }
986 otherName := ctx.OtherModuleName(m)
987 other, ok := m.(*apexBundle)
988 if !ok {
989 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
990 return
991 }
992 if proptools.Bool(other.properties.Use_vendor) != useVendor {
993 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
994 return
995 }
996 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
997 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
998 return
999 }
1000 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1001 })
1002
Alex Light778127a2019-02-27 14:19:50 -08001003 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001004 depTag := ctx.OtherModuleDependencyTag(child)
1005 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001006 if _, ok := parent.(*apexBundle); ok {
1007 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001008 switch depTag {
1009 case sharedLibTag:
1010 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001011 if cc.HasStubsVariants() {
1012 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1013 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001014 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001015 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001016 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001017 } else {
1018 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001019 }
1020 case executableTag:
1021 if cc, ok := child.(*cc.Module); ok {
1022 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001023 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001024 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001025 } else if sh, ok := child.(*android.ShBinary); ok {
1026 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001027 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001028 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1029 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1030 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1031 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1032 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1033 // NB: Since go binaries are static we don't need the module for anything here, which is
1034 // good since the go tool is a blueprint.Module not an android.Module like we would
1035 // normally use.
1036 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001037 } else {
Alex Light778127a2019-02-27 14:19:50 -08001038 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 +09001039 }
1040 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001041 if javaLib, ok := child.(*java.Library); ok {
1042 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001043 if fileToCopy == nil {
1044 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1045 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001046 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1047 }
1048 return true
1049 } else if javaLib, ok := child.(*java.Import); ok {
1050 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1051 if fileToCopy == nil {
1052 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1053 } else {
1054 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001055 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001056 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001057 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001058 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001059 }
1060 case prebuiltTag:
1061 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1062 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001063 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001064 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001065 } else {
1066 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1067 }
Roland Levillain630846d2019-06-26 12:48:34 +01001068 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001069 if ccTest, ok := child.(*cc.Module); ok {
1070 if ccTest.IsTestPerSrcAllTestsVariation() {
1071 // Multiple-output test module (where `test_per_src: true`).
1072 //
1073 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1074 // We do not add this variation to `filesInfo`, as it has no output;
1075 // however, we do add the other variations of this module as indirect
1076 // dependencies (see below).
1077 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001078 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001079 // Single-output test module (where `test_per_src: false`).
1080 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1081 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001082 }
Roland Levillain630846d2019-06-26 12:48:34 +01001083 return true
1084 } else {
1085 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1086 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001087 case keyTag:
1088 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001089 a.private_key_file = key.private_key_file
1090 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001091 return false
1092 } else {
1093 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001094 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001095 case certificateTag:
1096 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001097 a.container_certificate_file = dep.Certificate.Pem
1098 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001099 return false
1100 } else {
1101 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1102 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001103 case android.PrebuiltDepTag:
1104 // If the prebuilt is force disabled, remember to delete the prebuilt file
1105 // that might have been installed in the previous builds
1106 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1107 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1108 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001109 case androidAppTag:
1110 if ap, ok := child.(*java.AndroidApp); ok {
1111 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1112 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1113 return true
1114 } else {
1115 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1116 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001117 }
1118 } else {
1119 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001120 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001121 // We cannot use a switch statement on `depTag` here as the checked
1122 // tags used below are private (e.g. `cc.sharedDepTag`).
1123 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1124 if cc, ok := child.(*cc.Module); ok {
1125 if android.InList(cc.Name(), providedNativeSharedLibs) {
1126 // If we're using a shared library which is provided from other APEX,
1127 // don't include it in this APEX
1128 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001129 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001130 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1131 // If the dependency is a stubs lib, don't include it in this APEX,
1132 // but make sure that the lib is installed on the device.
1133 // In case no APEX is having the lib, the lib is installed to the system
1134 // partition.
1135 //
1136 // Always include if we are a host-apex however since those won't have any
1137 // system libraries.
1138 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1139 a.externalDeps = append(a.externalDeps, cc.Name())
1140 }
Jooyung Hane1633032019-08-01 17:41:43 +09001141 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001142 // Don't track further
1143 return false
1144 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001145 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001146 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1147 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001148 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001149 } else if cc.IsTestPerSrcDepTag(depTag) {
1150 if cc, ok := child.(*cc.Module); ok {
1151 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1152 // Handle modules created as `test_per_src` variations of a single test module:
1153 // use the name of the generated test binary (`fileToCopy`) instead of the name
1154 // of the original test module (`depName`, shared by all `test_per_src`
1155 // variations of that module).
1156 moduleName := filepath.Base(fileToCopy.String())
1157 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1158 return true
1159 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001160 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001161 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jooyung Hancc372c52019-09-25 15:18:44 +09001162 } else if depTag == android.DefaultsDepTag {
1163 return false
Sundong Ahn2db7f462019-08-27 18:53:12 +09001164 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
1165 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001166 }
1167 }
1168 }
1169 return false
1170 })
1171
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001172 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001173 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1174 return
1175 }
1176
Jiyong Park8fd61922018-11-08 02:50:25 +09001177 // remove duplicates in filesInfo
1178 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001179 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001180 result := []apexFile{}
1181 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001182 dest := filepath.Join(f.installDir, f.builtFile.Base())
1183 if !encountered[dest] {
1184 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001185 result = append(result, f)
1186 }
1187 }
1188 return result
1189 }
1190 filesInfo = removeDup(filesInfo)
1191
1192 // to have consistent build rules
1193 sort.Slice(filesInfo, func(i, j int) bool {
1194 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1195 })
1196
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001197 // check no_apex modules
1198 whitelist := whitelistNoApex[ctx.ModuleName()]
1199 for i := range filesInfo {
1200 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1201 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1202 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1203 }
1204 }
1205 }
1206
Jiyong Park127b40b2019-09-30 16:04:35 +09001207 // check apex_available requirements
1208 for _, fi := range filesInfo {
1209 if am, ok := fi.module.(android.ApexModule); ok {
1210 if !am.AvailableFor(ctx.ModuleName()) {
1211 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1212 return
1213 }
1214 }
1215 }
1216
Jiyong Park8fd61922018-11-08 02:50:25 +09001217 // prepend the name of this APEX to the module names. These names will be the names of
1218 // modules that will be defined if the APEX is flattened.
1219 for i := range filesInfo {
1220 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1221 }
1222
Jiyong Park8fd61922018-11-08 02:50:25 +09001223 a.installDir = android.PathForModuleInstall(ctx, "apex")
1224 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001225
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001226 // prepare apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001227 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
Jooyung Hane1633032019-08-01 17:41:43 +09001228 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001229
1230 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001231 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1232 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001233
1234 // apex name can be overridden
1235 optCommands := []string{}
1236 if a.properties.Apex_name != nil {
1237 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1238 }
1239
Jooyung Hane1633032019-08-01 17:41:43 +09001240 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001241 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001242 Input: manifestSrc,
1243 Output: a.manifestOut,
1244 Args: map[string]string{
1245 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1246 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001247 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001248 },
1249 })
1250
Roland Levillain935639d2019-08-13 14:55:28 +01001251 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1252 // reply true to `InstallBypassMake()` (thus making the call
1253 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1254 // instead of `android.PathForOutput`) to return the correct path to the flattened
1255 // APEX (as its contents is installed by Make, not Soong).
1256 factx := flattenedApexContext{ctx}
1257 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1258
Alex Light5098a612018-11-29 17:12:15 -08001259 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001260 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001261 }
1262 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001263 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001264 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001265 // in other modules. It is in AndroidMk where the selection of flattened
1266 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001267 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001268 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001269 }
1270}
1271
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001272func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001273 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001274 for _, f := range a.filesInfo {
1275 if f.module != nil {
1276 notice := f.module.NoticeFile()
1277 if notice.Valid() {
1278 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001279 }
1280 }
1281 }
1282 // append the notice file specified in the apex module itself
1283 if a.NoticeFile().Valid() {
1284 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001285 }
1286
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001287 if len(noticeFiles) == 0 {
1288 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001289 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001290
Jaewoong Jung98772792019-07-01 17:15:13 -07001291 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001292}
1293
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001294func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001295 cert := String(a.properties.Certificate)
1296 if cert != "" && android.SrcIsModule(cert) == "" {
1297 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001298 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1299 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001300 } else if cert == "" {
1301 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001302 a.container_certificate_file = pem
1303 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001304 }
1305
Alex Light5098a612018-11-29 17:12:15 -08001306 var abis []string
1307 for _, target := range ctx.MultiTargets() {
1308 if len(target.Arch.Abi) > 0 {
1309 abis = append(abis, target.Arch.Abi[0])
1310 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001311 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001312
Alex Light5098a612018-11-29 17:12:15 -08001313 abis = android.FirstUniqueStrings(abis)
1314
1315 suffix := apexType.suffix()
1316 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001317
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001318 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001319 for _, f := range a.filesInfo {
1320 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001321 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001322
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001323 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001324 emitCommands := []string{}
1325 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1326 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001327 for i, src := range filesToCopy {
1328 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001329 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001330 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001331 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1332 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001333 for _, sym := range a.filesInfo[i].symlinks {
1334 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1335 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1336 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001337 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001338 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001339 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001340
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001341 if a.properties.Whitelisted_files != nil {
1342 ctx.Build(pctx, android.BuildParams{
1343 Rule: emitApexContentRule,
1344 Implicits: implicitInputs,
1345 Output: imageContentFile,
1346 Description: "emit apex image content",
1347 Args: map[string]string{
1348 "emit_commands": strings.Join(emitCommands, " && "),
1349 },
1350 })
1351 implicitInputs = append(implicitInputs, imageContentFile)
1352 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1353
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001354 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001355 ctx.Build(pctx, android.BuildParams{
1356 Rule: diffApexContentRule,
1357 Implicits: implicitInputs,
1358 Output: phonyOutput,
1359 Description: "diff apex image content",
1360 Args: map[string]string{
1361 "whitelisted_files_file": whitelistedFilesFile.String(),
1362 "image_content_file": imageContentFile.String(),
1363 "apex_module_name": ctx.ModuleName(),
1364 },
1365 })
1366
1367 implicitInputs = append(implicitInputs, phonyOutput)
1368 }
1369
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001370 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1371 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001372
Alex Light5098a612018-11-29 17:12:15 -08001373 if apexType.image() {
1374 // files and dirs that will be created in APEX
1375 var readOnlyPaths []string
1376 var executablePaths []string // this also includes dirs
1377 for _, f := range a.filesInfo {
1378 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001379 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001380 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001381 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001382 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001383 }
Alex Light5098a612018-11-29 17:12:15 -08001384 } else {
1385 readOnlyPaths = append(readOnlyPaths, pathInApex)
1386 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001387 dir := f.installDir
1388 for !android.InList(dir, executablePaths) && dir != "" {
1389 executablePaths = append(executablePaths, dir)
1390 dir, _ = filepath.Split(dir) // move up to the parent
1391 if len(dir) > 0 {
1392 // remove trailing slash
1393 dir = dir[:len(dir)-1]
1394 }
Alex Light5098a612018-11-29 17:12:15 -08001395 }
1396 }
1397 sort.Strings(readOnlyPaths)
1398 sort.Strings(executablePaths)
1399 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1400 ctx.Build(pctx, android.BuildParams{
1401 Rule: generateFsConfig,
1402 Output: cannedFsConfig,
1403 Description: "generate fs config",
1404 Args: map[string]string{
1405 "ro_paths": strings.Join(readOnlyPaths, " "),
1406 "exec_paths": strings.Join(executablePaths, " "),
1407 },
1408 })
1409
1410 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1411 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1412 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1413 if !fileContextsOptionalPath.Valid() {
1414 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1415 return
1416 }
1417 fileContexts := fileContextsOptionalPath.Path()
1418
Jiyong Park835d82b2018-12-27 16:04:18 +09001419 optFlags := []string{}
1420
Alex Light5098a612018-11-29 17:12:15 -08001421 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001422 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1423 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001424
Jiyong Park7f67f482019-01-05 12:57:48 +09001425 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1426 if overridden {
1427 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1428 }
1429
Jiyong Park40e26a22019-02-08 02:53:06 +09001430 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001431 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001432 implicitInputs = append(implicitInputs, androidManifestFile)
1433 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1434 }
1435
Jiyong Park71b519d2019-04-18 17:25:49 +09001436 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1437 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1438 ctx.Config().UnbundledBuild() &&
1439 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1440 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1441 apiFingerprint := java.ApiFingerprintPath(ctx)
1442 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1443 implicitInputs = append(implicitInputs, apiFingerprint)
1444 }
1445 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1446
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001447 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1448 if noticeFile.Valid() {
1449 // If there's a NOTICE file, embed it as an asset file in the APEX.
1450 implicitInputs = append(implicitInputs, noticeFile.Path())
1451 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1452 }
1453
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001454 if !ctx.Config().UnbundledBuild() && a.installable() {
1455 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1456 // don't need hashtree for activation. Therefore, by removing hashtree from
1457 // apex bundle (filesystem image in it, to be specific), we can save storage.
1458 optFlags = append(optFlags, "--no_hashtree")
1459 }
1460
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001461 if a.properties.Apex_name != nil {
1462 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1463 // Note that apex_manifest is also mended.
1464 optFlags = append(optFlags, "--do_not_check_keyname")
1465 }
1466
Alex Light5098a612018-11-29 17:12:15 -08001467 ctx.Build(pctx, android.BuildParams{
1468 Rule: apexRule,
1469 Implicits: implicitInputs,
1470 Output: unsignedOutputFile,
1471 Description: "apex (" + apexType.name() + ")",
1472 Args: map[string]string{
1473 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1474 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1475 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001476 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001477 "file_contexts": fileContexts.String(),
1478 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001479 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001480 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001481 },
1482 })
1483
1484 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1485 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1486 a.bundleModuleFile = bundleModuleFile
1487
1488 ctx.Build(pctx, android.BuildParams{
1489 Rule: apexProtoConvertRule,
1490 Input: unsignedOutputFile,
1491 Output: apexProtoFile,
1492 Description: "apex proto convert",
1493 })
1494
1495 ctx.Build(pctx, android.BuildParams{
1496 Rule: apexBundleRule,
1497 Input: apexProtoFile,
1498 Output: a.bundleModuleFile,
1499 Description: "apex bundle module",
1500 Args: map[string]string{
1501 "abi": strings.Join(abis, "."),
1502 },
1503 })
1504 } else {
1505 ctx.Build(pctx, android.BuildParams{
1506 Rule: zipApexRule,
1507 Implicits: implicitInputs,
1508 Output: unsignedOutputFile,
1509 Description: "apex (" + apexType.name() + ")",
1510 Args: map[string]string{
1511 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1512 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1513 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001514 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001515 },
1516 })
Colin Crossa4925902018-11-16 11:36:28 -08001517 }
Colin Crossa4925902018-11-16 11:36:28 -08001518
Alex Light5098a612018-11-29 17:12:15 -08001519 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001520 ctx.Build(pctx, android.BuildParams{
1521 Rule: java.Signapk,
1522 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001523 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001524 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001525 Implicits: []android.Path{
1526 a.container_certificate_file,
1527 a.container_private_key_file,
1528 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001529 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001530 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001531 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001532 },
1533 })
Alex Light5098a612018-11-29 17:12:15 -08001534
1535 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahne8fb7242019-09-17 13:50:45 +09001536 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && !a.isFlattenedVariant() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001537 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001538 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001539}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001540
Jiyong Park8fd61922018-11-08 02:50:25 +09001541func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001542 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001543 // 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 +09001544 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001545 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001546
Jiyong Park42cca6c2019-04-01 11:15:50 +09001547 // rename to apex_pubkey
1548 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1549 ctx.Build(pctx, android.BuildParams{
1550 Rule: android.Cp,
1551 Input: a.public_key_file,
1552 Output: copiedPubkey,
1553 })
1554 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1555
Jiyong Park23c52b02019-02-02 13:13:47 +09001556 if ctx.Config().FlattenApex() {
1557 for _, fi := range a.filesInfo {
1558 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001559 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1560 for _, sym := range fi.symlinks {
1561 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1562 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001563 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001564 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001565 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001566}
1567
1568func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001569 if a.properties.HideFromMake {
1570 return android.AndroidMkData{
1571 Disabled: true,
1572 }
1573 }
Alex Light5098a612018-11-29 17:12:15 -08001574 writers := []android.AndroidMkData{}
1575 if a.apexTypes.image() {
1576 writers = append(writers, a.androidMkForType(imageApex))
1577 }
1578 if a.apexTypes.zip() {
1579 writers = append(writers, a.androidMkForType(zipApex))
1580 }
1581 return android.AndroidMkData{
1582 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1583 for _, data := range writers {
1584 data.Custom(w, name, prefix, moduleDir, data)
1585 }
1586 }}
1587}
1588
Alex Lightf1801bc2019-02-13 11:10:07 -08001589func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001590 moduleNames := []string{}
1591
1592 for _, fi := range a.filesInfo {
1593 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1594 continue
1595 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001596 if a.properties.Flattened && !apexType.image() {
1597 continue
Jiyong Park94427262019-02-05 23:18:47 +09001598 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001599
1600 var suffix string
Sundong Ahne8fb7242019-09-17 13:50:45 +09001601 if a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001602 suffix = ".flattened"
1603 }
1604
1605 if !android.InList(fi.moduleName, moduleNames) {
1606 moduleNames = append(moduleNames, fi.moduleName+suffix)
1607 }
1608
Jiyong Park94427262019-02-05 23:18:47 +09001609 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1610 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001611 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName+suffix)
Roland Levillain411c5842019-09-19 16:37:20 +01001612 // /apex/<apex_name>/{lib|framework|...}
Jiyong Park05e70dd2019-03-18 14:26:32 +09001613 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1614 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001615 if a.properties.Flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001616 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001617 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
1618 name, fi.installDir))
Sundong Ahne8fb7242019-09-17 13:50:45 +09001619 if !a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001620 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1621 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001622 if len(fi.symlinks) > 0 {
1623 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1624 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001625
1626 if fi.module != nil && fi.module.NoticeFile().Valid() {
1627 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1628 }
Jiyong Park94427262019-02-05 23:18:47 +09001629 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001630 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001631 }
1632 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1633 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1634 if fi.module != nil {
1635 archStr := fi.module.Target().Arch.ArchType.String()
1636 host := false
1637 switch fi.module.Target().Os.Class {
1638 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001639 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001640 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1641 }
1642 host = true
1643 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001644 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001645 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1646 }
1647 host = true
1648 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001649 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001650 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1651 }
1652 }
1653 if host {
1654 makeOs := fi.module.Target().Os.String()
1655 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1656 makeOs = "linux"
1657 }
1658 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1659 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1660 }
1661 }
1662 if fi.class == javaSharedLib {
1663 javaModule := fi.module.(*java.Library)
1664 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1665 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1666 // we will have foo.jar.jar
1667 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1668 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1669 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1670 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1671 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1672 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001673 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001674 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001675 if cc, ok := fi.module.(*cc.Module); ok {
1676 if cc.UnstrippedOutputFile() != nil {
1677 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1678 }
1679 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001680 if cc.CoverageOutputFile().Valid() {
1681 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1682 }
Jiyong Park94427262019-02-05 23:18:47 +09001683 }
1684 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1685 } else {
1686 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1687 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1688 }
1689 }
1690 return moduleNames
1691}
1692
Alex Light5098a612018-11-29 17:12:15 -08001693func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001694 return android.AndroidMkData{
1695 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1696 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001697 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001698 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001699 }
1700
Sundong Ahne8fb7242019-09-17 13:50:45 +09001701 if a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001702 name = name + ".flattened"
1703 }
1704
1705 if a.properties.Flattened && apexType.image() {
Jiyong Park719b4462019-01-13 00:39:51 +09001706 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001707 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1708 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1709 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001710 if len(moduleNames) > 0 {
1711 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1712 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001713 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001714 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1715
Sundong Ahne8fb7242019-09-17 13:50:45 +09001716 } else if !a.isFlattenedVariant() {
Alex Light5098a612018-11-29 17:12:15 -08001717 // zip-apex is the less common type so have the name refer to the image-apex
1718 // only and use {name}.zip if you want the zip-apex
1719 if apexType == zipApex && a.apexTypes == both {
1720 name = name + ".zip"
1721 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001722 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1723 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1724 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1725 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001726 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001727 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001728 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001729 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001730 if len(moduleNames) > 0 {
1731 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1732 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001733 if len(a.externalDeps) > 0 {
1734 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1735 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001736 if a.prebuiltFileToDelete != "" {
1737 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001738 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001739 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001740 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001741
Alex Light5098a612018-11-29 17:12:15 -08001742 if apexType == imageApex {
1743 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1744 }
Jiyong Park719b4462019-01-13 00:39:51 +09001745 }
1746 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001747}
1748
Jooyung Han344d5432019-08-23 11:17:39 +09001749func newApexBundle() *apexBundle {
Alex Light5098a612018-11-29 17:12:15 -08001750 module := &apexBundle{
1751 outputFiles: map[apexPackaging]android.WritablePath{},
1752 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001753 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001754 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001755 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001756 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1757 })
Alex Light5098a612018-11-29 17:12:15 -08001758 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001759 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001760 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001761 return module
1762}
Jiyong Park30ca9372019-02-07 16:27:23 +09001763
Jooyung Han344d5432019-08-23 11:17:39 +09001764func ApexBundleFactory(testApex bool) android.Module {
1765 bundle := newApexBundle()
1766 bundle.testApex = testApex
1767 return bundle
1768}
1769
1770func testApexBundleFactory() android.Module {
1771 bundle := newApexBundle()
1772 bundle.testApex = true
1773 return bundle
1774}
1775
Jiyong Parkd1063c12019-07-17 20:08:41 +09001776func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001777 return newApexBundle()
1778}
1779
1780// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1781// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1782// If not specified, then the "current" versions are gathered.
1783func vndkApexBundleFactory() android.Module {
1784 bundle := newApexBundle()
1785 bundle.vndkApex = true
1786 bundle.AddProperties(&bundle.vndkProperties)
1787 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1788 ctx.AppendProperties(&struct {
1789 Compile_multilib *string
1790 }{
1791 proptools.StringPtr("both"),
1792 })
Jooyung Han90eee022019-10-01 20:02:42 +09001793
1794 vndkVersion := proptools.StringDefault(bundle.vndkProperties.Vndk_version, "current")
1795 if vndkVersion == "current" {
1796 vndkVersion = ctx.DeviceConfig().PlatformVndkVersion()
1797 bundle.vndkProperties.Vndk_version = proptools.StringPtr(vndkVersion)
1798 }
1799
1800 // Ensure VNDK APEX mount point is formatted as com.android.vndk.v###
1801 bundle.properties.Apex_name = proptools.StringPtr("com.android.vndk.v" + vndkVersion)
Jooyung Han344d5432019-08-23 11:17:39 +09001802 })
1803 return bundle
1804}
1805
Jiyong Park30ca9372019-02-07 16:27:23 +09001806//
1807// Defaults
1808//
1809type Defaults struct {
1810 android.ModuleBase
1811 android.DefaultsModuleBase
1812}
1813
Jiyong Park30ca9372019-02-07 16:27:23 +09001814func defaultsFactory() android.Module {
1815 return DefaultsFactory()
1816}
1817
1818func DefaultsFactory(props ...interface{}) android.Module {
1819 module := &Defaults{}
1820
1821 module.AddProperties(props...)
1822 module.AddProperties(
1823 &apexBundleProperties{},
1824 &apexTargetBundleProperties{},
1825 )
1826
1827 android.InitDefaultsModule(module)
1828 return module
1829}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001830
1831//
1832// Prebuilt APEX
1833//
1834type Prebuilt struct {
1835 android.ModuleBase
1836 prebuilt android.Prebuilt
1837
1838 properties PrebuiltProperties
1839
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001840 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001841 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001842 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001843 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001844}
1845
1846type PrebuiltProperties struct {
1847 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001848 Source string `blueprint:"mutated"`
1849 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001850
1851 Src *string
1852 Arch struct {
1853 Arm struct {
1854 Src *string
1855 }
1856 Arm64 struct {
1857 Src *string
1858 }
1859 X86 struct {
1860 Src *string
1861 }
1862 X86_64 struct {
1863 Src *string
1864 }
1865 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001866
1867 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001868 // Optional name for the installed apex. If unspecified, name of the
1869 // module is used as the file name
1870 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001871
1872 // Names of modules to be overridden. Listed modules can only be other binaries
1873 // (in Make or Soong).
1874 // This does not completely prevent installation of the overridden binaries, but if both
1875 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1876 // from PRODUCT_PACKAGES.
1877 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001878}
1879
1880func (p *Prebuilt) installable() bool {
1881 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001882}
1883
1884func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001885 // If the device is configured to use flattened APEX, force disable the prebuilt because
1886 // the prebuilt is a non-flattened one.
1887 forceDisable := ctx.Config().FlattenApex()
1888
1889 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1890 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001891 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001892
Kun Niu10c9f832019-07-29 16:28:57 -07001893 // Force disable the prebuilts when coverage is enabled.
1894 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1895 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1896
Jiyong Park50b81e52019-07-11 11:24:41 +09001897 // b/137216042 don't use prebuilts when address sanitizer is on
1898 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1899 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1900
1901 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001902 p.properties.ForceDisable = true
1903 return
1904 }
1905
Jiyong Parkc95714e2019-03-29 14:23:10 +09001906 // This is called before prebuilt_select and prebuilt_postdeps mutators
1907 // The mutators requires that src to be set correctly for each arch so that
1908 // arch variants are disabled when src is not provided for the arch.
1909 if len(ctx.MultiTargets()) != 1 {
1910 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1911 return
1912 }
1913 var src string
1914 switch ctx.MultiTargets()[0].Arch.ArchType {
1915 case android.Arm:
1916 src = String(p.properties.Arch.Arm.Src)
1917 case android.Arm64:
1918 src = String(p.properties.Arch.Arm64.Src)
1919 case android.X86:
1920 src = String(p.properties.Arch.X86.Src)
1921 case android.X86_64:
1922 src = String(p.properties.Arch.X86_64.Src)
1923 default:
1924 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1925 return
1926 }
1927 if src == "" {
1928 src = String(p.properties.Src)
1929 }
1930 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001931}
1932
Jiyong Park03b68dd2019-07-26 23:20:40 +09001933func (p *Prebuilt) isForceDisabled() bool {
1934 return p.properties.ForceDisable
1935}
1936
Colin Cross41955e82019-05-29 14:40:35 -07001937func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1938 switch tag {
1939 case "":
1940 return android.Paths{p.outputApex}, nil
1941 default:
1942 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1943 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001944}
1945
Jiyong Park4d277042019-04-23 18:00:10 +09001946func (p *Prebuilt) InstallFilename() string {
1947 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1948}
1949
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001950func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001951 if p.properties.ForceDisable {
1952 return
1953 }
1954
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001955 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001956 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001957 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001958 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001959 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1960 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1961 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001962 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1963 ctx.Build(pctx, android.BuildParams{
1964 Rule: android.Cp,
1965 Input: p.inputApex,
1966 Output: p.outputApex,
1967 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001968 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001969 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001970 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001971}
1972
1973func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1974 return &p.prebuilt
1975}
1976
1977func (p *Prebuilt) Name() string {
1978 return p.prebuilt.Name(p.ModuleBase.Name())
1979}
1980
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001981func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1982 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001983 Class: "ETC",
1984 OutputFile: android.OptionalPathForPath(p.inputApex),
1985 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001986 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1987 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07001988 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001989 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1990 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1991 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1992 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001993 },
1994 }
1995}
1996
1997// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1998func PrebuiltFactory() android.Module {
1999 module := &Prebuilt{}
2000 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002001 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002002 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002003 return module
2004}