blob: 0186b85753e763f9b508abc1717d221984323799 [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
Jooyung Han344d5432019-08-23 11:17:39 +090024 "sync"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090025
26 "android/soong/android"
27 "android/soong/cc"
28 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080029 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030
31 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080032 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090033 "github.com/google/blueprint/proptools"
34)
35
36var (
37 pctx = android.NewPackageContext("android/apex")
38
39 // Create a canned fs config file where all files and directories are
40 // by default set to (uid/gid/mode) = (1000/1000/0644)
41 // TODO(b/113082813) make this configurable using config.fs syntax
42 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000043 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000044 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090045 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090046 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090047 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090048 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090049
Jooyung Hane1633032019-08-01 17:41:43 +090050 injectApexDependency = pctx.StaticRule("injectApexDependency", blueprint.RuleParams{
51 Command: `rm -f $out && ${jsonmodify} $in ` +
52 `-a provideNativeLibs ${provideNativeLibs} ` +
53 `-a requireNativeLibs ${requireNativeLibs} -o $out`,
54 CommandDeps: []string{"${jsonmodify}"},
55 Description: "Inject dependency into ${out}",
56 }, "provideNativeLibs", "requireNativeLibs")
57
Jiyong Park48ca7dc2018-10-10 14:01:00 +090058 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
59 // against the binary policy using sefcontext_compiler -p <policy>.
60
61 // TODO(b/114327326): automate the generation of file_contexts
62 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
63 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010064 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090065 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090066 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090067 `--file_contexts ${file_contexts} ` +
68 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080069 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090070 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090071 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
72 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000073 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010074 Rspfile: "${out}.copy_commands",
75 RspfileContent: "${copy_commands}",
76 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090077 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080078
Alex Light5098a612018-11-29 17:12:15 -080079 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
80 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010081 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080082 `APEXER_TOOL_PATH=${tool_path} ` +
83 `${apexer} --force --manifest ${manifest} ` +
84 `--payload_type zip ` +
85 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010086 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
87 Rspfile: "${out}.copy_commands",
88 RspfileContent: "${copy_commands}",
89 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080090 }, "tool_path", "image_dir", "copy_commands", "manifest")
91
Colin Crossa4925902018-11-16 11:36:28 -080092 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
93 blueprint.RuleParams{
94 Command: `${aapt2} convert --output-format proto $in -o $out`,
95 CommandDeps: []string{"${aapt2}"},
96 })
97
98 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090099 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +0000100 `apex_payload.img:apex/${abi}.img ` +
101 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +0000102 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -0800103 CommandDeps: []string{"${zip2zip}"},
104 Description: "app bundle",
105 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100106
107 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
108 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
109 Rspfile: "${out}.emit_commands",
110 RspfileContent: "${emit_commands}",
111 Description: "Emit APEX image content",
112 }, "emit_commands")
113
114 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
115 Command: `diff --unchanged-group-format='' \` +
116 `--changed-group-format='%<' \` +
117 `${image_content_file} ${whitelisted_files_file} || (` +
118 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
119 ` "To fix the build run following command:" && ` +
120 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
121 `exit 1)`,
122 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
123 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900124)
125
Alex Light5098a612018-11-29 17:12:15 -0800126var imageApexSuffix = ".apex"
127var zipApexSuffix = ".zipapex"
128
129var imageApexType = "image"
130var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900131
132type dependencyTag struct {
133 blueprint.BaseDependencyTag
134 name string
135}
136
137var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900138 sharedLibTag = dependencyTag{name: "sharedLib"}
139 executableTag = dependencyTag{name: "executable"}
140 javaLibTag = dependencyTag{name: "javaLib"}
141 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100142 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900143 keyTag = dependencyTag{name: "key"}
144 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900145 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900146 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900147)
148
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900149var (
150 whitelistNoApex = map[string][]string{
151 "apex_test_build_features": []string{"libbinder"},
152 "com.android.neuralnetworks": []string{"libbinder"},
153 "com.android.media": []string{"libbinder"},
154 "com.android.media.swcodec": []string{"libbinder"},
155 "test_com.android.media.swcodec": []string{"libbinder"},
Jooyung Han344d5432019-08-23 11:17:39 +0900156 "com.android.vndk": []string{"libbinder"},
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900157 }
158)
159
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700161 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900162 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900163 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100164 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
165 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
166 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
167 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000168 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100169 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
170 } else {
171 return pctx.HostBinToolPath(ctx, tool).String()
172 }
173 })
174 }
175 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900176 pctx.HostBinToolVariable("avbtool", "avbtool")
177 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
178 pctx.HostBinToolVariable("merge_zips", "merge_zips")
179 pctx.HostBinToolVariable("mke2fs", "mke2fs")
180 pctx.HostBinToolVariable("resize2fs", "resize2fs")
181 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
182 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800183 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900184 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900185 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900186
Alex Light0851b882019-02-07 13:20:53 -0800187 android.RegisterModuleType("apex", apexBundleFactory)
188 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jooyung Han344d5432019-08-23 11:17:39 +0900189 android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900190 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700191 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900192
Jooyung Han344d5432019-08-23 11:17:39 +0900193 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
194 ctx.TopDown("apex_vndk_gather", apexVndkGatherMutator).Parallel()
195 ctx.BottomUp("apex_vndk_add_deps", apexVndkAddDepsMutator).Parallel()
196 })
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900197 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
198 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700199 ctx.BottomUp("apex", apexMutator).Parallel()
Jooyung Han5c998b92019-06-27 11:30:33 +0900200 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900201 })
202}
203
Jooyung Han344d5432019-08-23 11:17:39 +0900204var (
205 vndkApexListKey = android.NewOnceKey("vndkApexList")
206 vndkApexListMutex sync.Mutex
207)
208
209func vndkApexList(config android.Config) map[string]*apexBundle {
210 return config.Once(vndkApexListKey, func() interface{} {
211 return map[string]*apexBundle{}
212 }).(map[string]*apexBundle)
213}
214
215// apexVndkGatherMutator gathers "apex_vndk" modules and puts them in a map with vndk_version as a key.
216func apexVndkGatherMutator(mctx android.TopDownMutatorContext) {
217 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
218 if ab.IsNativeBridgeSupported() {
219 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
220 }
221 vndkVersion := proptools.StringDefault(ab.vndkProperties.Vndk_version, mctx.DeviceConfig().PlatformVndkVersion())
222 vndkApexListMutex.Lock()
223 defer vndkApexListMutex.Unlock()
224 vndkApexList := vndkApexList(mctx.Config())
225 if other, ok := vndkApexList[vndkVersion]; ok {
226 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other.Name())
227 }
228 vndkApexList[vndkVersion] = ab
229 }
230}
231
232// apexVndkAddDepsMutator adds (reverse) dependencies from vndk libs to apex_vndk modules.
233// It filters only libs with matching targets.
234func apexVndkAddDepsMutator(mctx android.BottomUpMutatorContext) {
235 if cc, ok := mctx.Module().(*cc.Module); ok && cc.IsVndkOnSystem() {
236 vndkApexList := vndkApexList(mctx.Config())
237 if ab, ok := vndkApexList[cc.VndkVersion()]; ok {
238 targetArch := cc.Target().String()
239 for _, target := range ab.MultiTargets() {
240 if target.String() == targetArch {
241 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, ab.Name())
242 break
243 }
244 }
245 }
246 }
247}
248
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900249// Mark the direct and transitive dependencies of apex bundles so that they
250// can be built for the apex bundles.
251func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800252 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800253 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900254 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900255 depName := mctx.OtherModuleName(child)
256 // If the parent is apexBundle, this child is directly depended.
257 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800258 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800259 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
260 // non-installable apex's cannot be installed and so should not prevent libraries from being
261 // installed to the system.
262 android.UpdateApexDependency(apexBundleName, depName, directDep)
263 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900264
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900265 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900266 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900267 return true
268 } else {
269 return false
270 }
271 })
272 }
273}
274
275// Create apex variations if a module is included in APEX(s).
276func apexMutator(mctx android.BottomUpMutatorContext) {
277 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900278 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900279 } else if _, ok := mctx.Module().(*apexBundle); ok {
280 // apex bundle itself is mutated so that it and its modules have same
281 // apex variant.
282 apexBundleName := mctx.ModuleName()
283 mctx.CreateVariations(apexBundleName)
284 }
285}
Jooyung Han5c998b92019-06-27 11:30:33 +0900286func apexUsesMutator(mctx android.BottomUpMutatorContext) {
287 if ab, ok := mctx.Module().(*apexBundle); ok {
288 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
289 }
290}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900291
Alex Light9670d332019-01-29 18:07:33 -0800292type apexNativeDependencies struct {
293 // List of native libraries
294 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900295
Alex Light9670d332019-01-29 18:07:33 -0800296 // List of native executables
297 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900298
Roland Levillain630846d2019-06-26 12:48:34 +0100299 // List of native tests
300 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800301}
Jooyung Han344d5432019-08-23 11:17:39 +0900302
Alex Light9670d332019-01-29 18:07:33 -0800303type apexMultilibProperties struct {
304 // Native dependencies whose compile_multilib is "first"
305 First apexNativeDependencies
306
307 // Native dependencies whose compile_multilib is "both"
308 Both apexNativeDependencies
309
310 // Native dependencies whose compile_multilib is "prefer32"
311 Prefer32 apexNativeDependencies
312
313 // Native dependencies whose compile_multilib is "32"
314 Lib32 apexNativeDependencies
315
316 // Native dependencies whose compile_multilib is "64"
317 Lib64 apexNativeDependencies
318}
319
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900320type apexBundleProperties struct {
321 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000322 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800323 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900324
Jiyong Park40e26a22019-02-08 02:53:06 +0900325 // AndroidManifest.xml file used for the zip container of this APEX bundle.
326 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800327 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900328
Jiyong Park05e70dd2019-03-18 14:26:32 +0900329 // Canonical name of the APEX bundle in the manifest file.
330 // If unspecified, defaults to the value of name
331 Apex_name *string
332
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900333 // Determines the file contexts file for setting security context to each file in this APEX bundle.
334 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
335 // used.
336 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900337 File_contexts *string
338
339 // List of native shared libs that are embedded inside this APEX bundle
340 Native_shared_libs []string
341
Roland Levillain630846d2019-06-26 12:48:34 +0100342 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900343 Binaries []string
344
345 // List of java libraries that are embedded inside this APEX bundle
346 Java_libs []string
347
348 // List of prebuilt files that are embedded inside this APEX bundle
349 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900350
Roland Levillain630846d2019-06-26 12:48:34 +0100351 // List of tests that are embedded inside this APEX bundle
352 Tests []string
353
Jiyong Parkff1458f2018-10-12 21:49:38 +0900354 // Name of the apex_key module that provides the private key to sign APEX
355 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900356
Alex Light5098a612018-11-29 17:12:15 -0800357 // The type of APEX to build. Controls what the APEX payload is. Either
358 // 'image', 'zip' or 'both'. Default: 'image'.
359 Payload_type *string
360
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900361 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
362 // or an android_app_certificate module name in the form ":module".
363 Certificate *string
364
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900365 // Whether this APEX is installable to one of the partitions. Default: true.
366 Installable *bool
367
Jiyong Parkda6eb592018-12-19 17:12:36 +0900368 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
369 // Default is false.
370 Use_vendor *bool
371
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800372 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
373 Ignore_system_library_special_case *bool
374
Alex Light9670d332019-01-29 18:07:33 -0800375 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900376
Jiyong Parkf97782b2019-02-13 20:28:58 +0900377 // List of sanitizer names that this APEX is enabled for
378 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900379
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900380 PreventInstall bool `blueprint:"mutated"`
381
382 HideFromMake bool `blueprint:"mutated"`
383
Jooyung Han5c998b92019-06-27 11:30:33 +0900384 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
385 Provide_cpp_shared_libs *bool
386
387 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
388 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100389
390 // A txt file containing list of files that are whitelisted to be included in this APEX.
391 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900392
393 // List of APKs to package inside APEX
394 Apps []string
Alex Light9670d332019-01-29 18:07:33 -0800395}
396
397type apexTargetBundleProperties struct {
398 Target struct {
399 // Multilib properties only for android.
400 Android struct {
401 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900402 }
Jooyung Han344d5432019-08-23 11:17:39 +0900403
Alex Light9670d332019-01-29 18:07:33 -0800404 // Multilib properties only for host.
405 Host struct {
406 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900407 }
Jooyung Han344d5432019-08-23 11:17:39 +0900408
Alex Light9670d332019-01-29 18:07:33 -0800409 // Multilib properties only for host linux_bionic.
410 Linux_bionic struct {
411 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900412 }
Jooyung Han344d5432019-08-23 11:17:39 +0900413
Alex Light9670d332019-01-29 18:07:33 -0800414 // Multilib properties only for host linux_glibc.
415 Linux_glibc struct {
416 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900417 }
418 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900419}
420
Jooyung Han344d5432019-08-23 11:17:39 +0900421type apexVndkProperties struct {
422 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
423 Vndk_version *string
424}
425
Jiyong Park8fd61922018-11-08 02:50:25 +0900426type apexFileClass int
427
428const (
429 etc apexFileClass = iota
430 nativeSharedLib
431 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900432 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800433 pyBinary
434 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900435 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100436 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900437 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900438)
439
Alex Light5098a612018-11-29 17:12:15 -0800440type apexPackaging int
441
442const (
443 imageApex apexPackaging = iota
444 zipApex
445 both
446)
447
448func (a apexPackaging) image() bool {
449 switch a {
450 case imageApex, both:
451 return true
452 }
453 return false
454}
455
456func (a apexPackaging) zip() bool {
457 switch a {
458 case zipApex, both:
459 return true
460 }
461 return false
462}
463
464func (a apexPackaging) suffix() string {
465 switch a {
466 case imageApex:
467 return imageApexSuffix
468 case zipApex:
469 return zipApexSuffix
470 case both:
471 panic(fmt.Errorf("must be either zip or image"))
472 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100473 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800474 }
475}
476
477func (a apexPackaging) name() string {
478 switch a {
479 case imageApex:
480 return imageApexType
481 case zipApex:
482 return zipApexType
483 case both:
484 panic(fmt.Errorf("must be either zip or image"))
485 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100486 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800487 }
488}
489
Jiyong Park8fd61922018-11-08 02:50:25 +0900490func (class apexFileClass) NameInMake() string {
491 switch class {
492 case etc:
493 return "ETC"
494 case nativeSharedLib:
495 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800496 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900497 return "EXECUTABLES"
498 case javaSharedLib:
499 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100500 case nativeTest:
501 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900502 case app:
503 return "APPS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900504 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100505 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900506 }
507}
508
509type apexFile struct {
510 builtFile android.Path
511 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900512 installDir string
513 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900514 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800515 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900516}
517
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900518type apexBundle struct {
519 android.ModuleBase
520 android.DefaultableModuleBase
521
Alex Light9670d332019-01-29 18:07:33 -0800522 properties apexBundleProperties
523 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900524 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900525
Alex Light5098a612018-11-29 17:12:15 -0800526 apexTypes apexPackaging
527
Colin Crossa4925902018-11-16 11:36:28 -0800528 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800529 outputFiles map[apexPackaging]android.WritablePath
Roland Levillain935639d2019-08-13 14:55:28 +0100530 flattenedOutput android.OutputPath
Colin Crossa4925902018-11-16 11:36:28 -0800531 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900532
Jiyong Park03b68dd2019-07-26 23:20:40 +0900533 prebuiltFileToDelete string
534
Jiyong Park42cca6c2019-04-01 11:15:50 +0900535 public_key_file android.Path
536 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900537
538 container_certificate_file android.Path
539 container_private_key_file android.Path
540
Jiyong Park8fd61922018-11-08 02:50:25 +0900541 // list of files to be included in this apex
542 filesInfo []apexFile
543
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900544 // list of module names that this APEX is depending on
545 externalDeps []string
546
Jiyong Park8fd61922018-11-08 02:50:25 +0900547 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800548
549 testApex bool
Jooyung Han344d5432019-08-23 11:17:39 +0900550 vndkApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900551
552 // intermediate path for apex_manifest.json
553 manifestOut android.WritablePath
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900554}
555
Jiyong Park397e55e2018-10-24 21:09:55 +0900556func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100557 native_shared_libs []string, binaries []string, tests []string,
558 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900559 // Use *FarVariation* to be able to depend on modules having
560 // conflicting variations with this module. This is required since
561 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
562 // for native shared libs.
563 ctx.AddFarVariationDependencies([]blueprint.Variation{
564 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900565 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900566 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900567 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900568 }, sharedLibTag, native_shared_libs...)
569
570 ctx.AddFarVariationDependencies([]blueprint.Variation{
571 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900572 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900573 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100574
575 ctx.AddFarVariationDependencies([]blueprint.Variation{
576 {Mutator: "arch", Variation: arch},
577 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100578 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100579 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900580}
581
Alex Light9670d332019-01-29 18:07:33 -0800582func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
583 if ctx.Os().Class == android.Device {
584 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
585 } else {
586 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
587 if ctx.Os().Bionic() {
588 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
589 } else {
590 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
591 }
592 }
593}
594
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900595func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800596
Jiyong Park397e55e2018-10-24 21:09:55 +0900597 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900598 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800599
600 a.combineProperties(ctx)
601
Jiyong Park397e55e2018-10-24 21:09:55 +0900602 has32BitTarget := false
603 for _, target := range targets {
604 if target.Arch.ArchType.Multilib == "lib32" {
605 has32BitTarget = true
606 }
607 }
608 for i, target := range targets {
609 // When multilib.* is omitted for native_shared_libs, it implies
610 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900611 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900612 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900613 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900614 {Mutator: "link", Variation: "shared"},
615 }, sharedLibTag, a.properties.Native_shared_libs...)
616
Roland Levillain630846d2019-06-26 12:48:34 +0100617 // When multilib.* is omitted for tests, it implies
618 // multilib.both.
619 ctx.AddFarVariationDependencies([]blueprint.Variation{
620 {Mutator: "arch", Variation: target.String()},
621 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100622 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100623 }, testTag, a.properties.Tests...)
624
Jiyong Park397e55e2018-10-24 21:09:55 +0900625 // Add native modules targetting both ABIs
626 addDependenciesForNativeModules(ctx,
627 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100628 a.properties.Multilib.Both.Binaries,
629 a.properties.Multilib.Both.Tests,
630 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900631 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900632
Alex Light3d673592019-01-18 14:37:31 -0800633 isPrimaryAbi := i == 0
634 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900635 // When multilib.* is omitted for binaries, it implies
636 // multilib.first.
637 ctx.AddFarVariationDependencies([]blueprint.Variation{
638 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900639 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900640 }, executableTag, a.properties.Binaries...)
641
642 // Add native modules targetting the first ABI
643 addDependenciesForNativeModules(ctx,
644 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100645 a.properties.Multilib.First.Binaries,
646 a.properties.Multilib.First.Tests,
647 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900648 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800649
650 // When multilib.* is omitted for prebuilts, it implies multilib.first.
651 ctx.AddFarVariationDependencies([]blueprint.Variation{
652 {Mutator: "arch", Variation: target.String()},
653 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900654 }
655
656 switch target.Arch.ArchType.Multilib {
657 case "lib32":
658 // Add native modules targetting 32-bit ABI
659 addDependenciesForNativeModules(ctx,
660 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100661 a.properties.Multilib.Lib32.Binaries,
662 a.properties.Multilib.Lib32.Tests,
663 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900664 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900665
666 addDependenciesForNativeModules(ctx,
667 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100668 a.properties.Multilib.Prefer32.Binaries,
669 a.properties.Multilib.Prefer32.Tests,
670 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900671 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900672 case "lib64":
673 // Add native modules targetting 64-bit ABI
674 addDependenciesForNativeModules(ctx,
675 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100676 a.properties.Multilib.Lib64.Binaries,
677 a.properties.Multilib.Lib64.Tests,
678 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900679 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900680
681 if !has32BitTarget {
682 addDependenciesForNativeModules(ctx,
683 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100684 a.properties.Multilib.Prefer32.Binaries,
685 a.properties.Multilib.Prefer32.Tests,
686 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900687 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900688 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700689
690 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
691 for _, sanitizer := range ctx.Config().SanitizeDevice() {
692 if sanitizer == "hwaddress" {
693 addDependenciesForNativeModules(ctx,
694 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100695 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700696 break
697 }
698 }
699 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900700 }
701
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900702 }
703
Jiyong Parkff1458f2018-10-12 21:49:38 +0900704 ctx.AddFarVariationDependencies([]blueprint.Variation{
705 {Mutator: "arch", Variation: "android_common"},
706 }, javaLibTag, a.properties.Java_libs...)
707
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900708 ctx.AddFarVariationDependencies([]blueprint.Variation{
709 {Mutator: "arch", Variation: "android_common"},
710 }, androidAppTag, a.properties.Apps...)
711
Jiyong Park23c52b02019-02-02 13:13:47 +0900712 if String(a.properties.Key) == "" {
713 ctx.ModuleErrorf("key is missing")
714 return
715 }
716 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900717
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900718 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900719 if cert != "" {
720 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900721 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900722}
723
Colin Cross0ea8ba82019-06-06 14:33:29 -0700724func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900725 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
726 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000727 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900728 }
729 return String(a.properties.Certificate)
730}
731
Colin Cross41955e82019-05-29 14:40:35 -0700732func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
733 switch tag {
734 case "":
735 if file, ok := a.outputFiles[imageApex]; ok {
736 return android.Paths{file}, nil
737 } else {
738 return nil, nil
739 }
Roland Levillain935639d2019-08-13 14:55:28 +0100740 case ".flattened":
741 if a.flattened {
742 flattenedApexPath := a.flattenedOutput
743 return android.Paths{flattenedApexPath}, nil
744 } else {
745 return nil, nil
746 }
Colin Cross41955e82019-05-29 14:40:35 -0700747 default:
748 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900749 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900750}
751
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900752func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900753 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900754}
755
Jiyong Park7c1dc612019-01-05 11:15:24 +0900756func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
757 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900758 return "vendor"
759 } else {
760 return "core"
761 }
762}
763
Jiyong Parkf97782b2019-02-13 20:28:58 +0900764func (a *apexBundle) EnableSanitizer(sanitizerName string) {
765 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
766 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
767 }
768}
769
Jiyong Park388ef3f2019-01-28 19:47:32 +0900770func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900771 if android.InList(sanitizerName, a.properties.SanitizerNames) {
772 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900773 }
774
775 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900776 globalSanitizerNames := []string{}
777 if a.Host() {
778 globalSanitizerNames = ctx.Config().SanitizeHost()
779 } else {
780 arches := ctx.Config().SanitizeDeviceArch()
781 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
782 globalSanitizerNames = ctx.Config().SanitizeDevice()
783 }
784 }
785 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900786}
787
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900788func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
789 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
790}
791
792func (a *apexBundle) PreventInstall() {
793 a.properties.PreventInstall = true
794}
795
796func (a *apexBundle) HideFromMake() {
797 a.properties.HideFromMake = true
798}
799
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800800func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900801 // Decide the APEX-local directory by the multilib of the library
802 // In the future, we may query this to the module.
803 switch cc.Arch().ArchType.Multilib {
804 case "lib32":
805 dirInApex = "lib"
806 case "lib64":
807 dirInApex = "lib64"
808 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900809 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200810 if !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900811 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200812 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
813 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900814 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800815 if handleSpecialLibs {
816 switch cc.Name() {
817 case "libc", "libm", "libdl":
818 // Special case for bionic libs. This is to prevent the bionic libs
819 // from being included in the search path /apex/com.android.apex/lib.
820 // This exclusion is required because bionic libs in the runtime APEX
821 // are available via the legacy paths /system/lib/libc.so, etc. By the
822 // init process, the bionic libs in the APEX are bind-mounted to the
823 // legacy paths and thus will be loaded into the default linker namespace.
824 // If the bionic libs are directly in /apex/com.android.apex/lib then
825 // the same libs will be again loaded to the runtime linker namespace,
826 // which will result double loading of bionic libs that isn't supported.
827 dirInApex = filepath.Join(dirInApex, "bionic")
828 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900829 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900830
831 fileToCopy = cc.OutputFile().Path()
832 return
833}
834
835func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900836 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200837 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900838 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200839 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
840 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900841 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900842 fileToCopy = cc.OutputFile().Path()
843 return
844}
845
Alex Light778127a2019-02-27 14:19:50 -0800846func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
847 dirInApex = "bin"
848 fileToCopy = py.HostToolPath().Path()
849 return
850}
851func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
852 dirInApex = "bin"
853 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
854 if err != nil {
855 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
856 return
857 }
858 fileToCopy = android.PathForOutput(ctx, s)
859 return
860}
861
Jiyong Park04480cf2019-02-06 00:16:29 +0900862func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
863 dirInApex = filepath.Join("bin", sh.SubDir())
864 fileToCopy = sh.OutputFile()
865 return
866}
867
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900868func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
869 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900870 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900871 return
872}
873
Jiyong Park9e6c2422019-08-09 20:39:45 +0900874func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
875 dirInApex = "javalib"
876 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
877 implJars := java.ImplementationJars()
878 if len(implJars) != 1 {
879 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
880 strings.Join(implJars.Strings(), ", ")))
881 }
882 fileToCopy = implJars[0]
883 return
884}
885
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900886func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
887 dirInApex = filepath.Join("etc", prebuilt.SubDir())
888 fileToCopy = prebuilt.OutputFile()
889 return
890}
891
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900892func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
893 dirInApex = filepath.Join("app", pkgName)
894 fileToCopy = app.OutputFile()
895 return
896}
897
Roland Levillain935639d2019-08-13 14:55:28 +0100898// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
899type flattenedApexContext struct {
900 android.ModuleContext
901}
902
903func (c *flattenedApexContext) InstallBypassMake() bool {
904 return true
905}
906
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900907func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900908 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900909
Alex Light5098a612018-11-29 17:12:15 -0800910 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
911 a.apexTypes = imageApex
912 } else if *a.properties.Payload_type == "zip" {
913 a.apexTypes = zipApex
914 } else if *a.properties.Payload_type == "both" {
915 a.apexTypes = both
916 } else {
917 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
918 return
919 }
920
Roland Levillain630846d2019-06-26 12:48:34 +0100921 if len(a.properties.Tests) > 0 && !a.testApex {
922 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
923 return
924 }
925
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800926 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
927
Jooyung Hane1633032019-08-01 17:41:43 +0900928 // native lib dependencies
929 var provideNativeLibs []string
930 var requireNativeLibs []string
931
Jooyung Han5c998b92019-06-27 11:30:33 +0900932 // Check if "uses" requirements are met with dependent apexBundles
933 var providedNativeSharedLibs []string
934 useVendor := proptools.Bool(a.properties.Use_vendor)
935 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
936 if ctx.OtherModuleDependencyTag(m) != usesTag {
937 return
938 }
939 otherName := ctx.OtherModuleName(m)
940 other, ok := m.(*apexBundle)
941 if !ok {
942 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
943 return
944 }
945 if proptools.Bool(other.properties.Use_vendor) != useVendor {
946 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
947 return
948 }
949 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
950 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
951 return
952 }
953 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
954 })
955
Alex Light778127a2019-02-27 14:19:50 -0800956 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100957 depTag := ctx.OtherModuleDependencyTag(child)
958 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900959 if _, ok := parent.(*apexBundle); ok {
960 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900961 switch depTag {
962 case sharedLibTag:
963 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900964 if cc.HasStubsVariants() {
965 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
966 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800967 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900968 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900969 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900970 } else {
971 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900972 }
973 case executableTag:
974 if cc, ok := child.(*cc.Module); ok {
975 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900976 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900977 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900978 } else if sh, ok := child.(*android.ShBinary); ok {
979 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
980 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800981 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
982 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
983 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
984 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
985 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
986 // NB: Since go binaries are static we don't need the module for anything here, which is
987 // good since the go tool is a blueprint.Module not an android.Module like we would
988 // normally use.
989 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900990 } else {
Alex Light778127a2019-02-27 14:19:50 -0800991 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 +0900992 }
993 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +0900994 if javaLib, ok := child.(*java.Library); ok {
995 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +0900996 if fileToCopy == nil {
997 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
998 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900999 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1000 }
1001 return true
1002 } else if javaLib, ok := child.(*java.Import); ok {
1003 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1004 if fileToCopy == nil {
1005 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1006 } else {
1007 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001008 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001009 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001010 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001011 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001012 }
1013 case prebuiltTag:
1014 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1015 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001016 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001017 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001018 } else {
1019 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1020 }
Roland Levillain630846d2019-06-26 12:48:34 +01001021 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001022 if ccTest, ok := child.(*cc.Module); ok {
1023 if ccTest.IsTestPerSrcAllTestsVariation() {
1024 // Multiple-output test module (where `test_per_src: true`).
1025 //
1026 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1027 // We do not add this variation to `filesInfo`, as it has no output;
1028 // however, we do add the other variations of this module as indirect
1029 // dependencies (see below).
1030 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001031 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001032 // Single-output test module (where `test_per_src: false`).
1033 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1034 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001035 }
Roland Levillain630846d2019-06-26 12:48:34 +01001036 return true
1037 } else {
1038 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1039 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001040 case keyTag:
1041 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001042 a.private_key_file = key.private_key_file
1043 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001044 return false
1045 } else {
1046 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001047 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001048 case certificateTag:
1049 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001050 a.container_certificate_file = dep.Certificate.Pem
1051 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001052 return false
1053 } else {
1054 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1055 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001056 case android.PrebuiltDepTag:
1057 // If the prebuilt is force disabled, remember to delete the prebuilt file
1058 // that might have been installed in the previous builds
1059 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1060 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1061 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001062 case androidAppTag:
1063 if ap, ok := child.(*java.AndroidApp); ok {
1064 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1065 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1066 return true
1067 } else {
1068 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1069 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001070 }
1071 } else {
1072 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001073 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001074 // We cannot use a switch statement on `depTag` here as the checked
1075 // tags used below are private (e.g. `cc.sharedDepTag`).
1076 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1077 if cc, ok := child.(*cc.Module); ok {
1078 if android.InList(cc.Name(), providedNativeSharedLibs) {
1079 // If we're using a shared library which is provided from other APEX,
1080 // don't include it in this APEX
1081 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001082 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001083 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1084 // If the dependency is a stubs lib, don't include it in this APEX,
1085 // but make sure that the lib is installed on the device.
1086 // In case no APEX is having the lib, the lib is installed to the system
1087 // partition.
1088 //
1089 // Always include if we are a host-apex however since those won't have any
1090 // system libraries.
1091 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1092 a.externalDeps = append(a.externalDeps, cc.Name())
1093 }
Jooyung Hane1633032019-08-01 17:41:43 +09001094 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001095 // Don't track further
1096 return false
1097 }
1098 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
1099 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1100 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001101 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001102 } else if cc.IsTestPerSrcDepTag(depTag) {
1103 if cc, ok := child.(*cc.Module); ok {
1104 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1105 // Handle modules created as `test_per_src` variations of a single test module:
1106 // use the name of the generated test binary (`fileToCopy`) instead of the name
1107 // of the original test module (`depName`, shared by all `test_per_src`
1108 // variations of that module).
1109 moduleName := filepath.Base(fileToCopy.String())
1110 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1111 return true
1112 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001113 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001114 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Sundong Ahn2db7f462019-08-27 18:53:12 +09001115 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
1116 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001117 }
1118 }
1119 }
1120 return false
1121 })
1122
Jiyong Park9335a262018-12-24 11:31:58 +09001123 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001124 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001125 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1126 return
1127 }
1128
Jiyong Park8fd61922018-11-08 02:50:25 +09001129 // remove duplicates in filesInfo
1130 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001131 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001132 result := []apexFile{}
1133 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001134 dest := filepath.Join(f.installDir, f.builtFile.Base())
1135 if !encountered[dest] {
1136 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001137 result = append(result, f)
1138 }
1139 }
1140 return result
1141 }
1142 filesInfo = removeDup(filesInfo)
1143
1144 // to have consistent build rules
1145 sort.Slice(filesInfo, func(i, j int) bool {
1146 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1147 })
1148
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001149 // check no_apex modules
1150 whitelist := whitelistNoApex[ctx.ModuleName()]
1151 for i := range filesInfo {
1152 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1153 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1154 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1155 }
1156 }
1157 }
1158
Jiyong Park8fd61922018-11-08 02:50:25 +09001159 // prepend the name of this APEX to the module names. These names will be the names of
1160 // modules that will be defined if the APEX is flattened.
1161 for i := range filesInfo {
1162 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1163 }
1164
Jiyong Park8fd61922018-11-08 02:50:25 +09001165 a.installDir = android.PathForModuleInstall(ctx, "apex")
1166 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001167
Jooyung Hane1633032019-08-01 17:41:43 +09001168 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1169 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1170 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1171 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1172 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1173 ctx.Build(pctx, android.BuildParams{
1174 Rule: injectApexDependency,
1175 Input: manifestSrc,
1176 Output: a.manifestOut,
1177 Args: map[string]string{
1178 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1179 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1180 },
1181 })
1182
Roland Levillain935639d2019-08-13 14:55:28 +01001183 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1184 // reply true to `InstallBypassMake()` (thus making the call
1185 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1186 // instead of `android.PathForOutput`) to return the correct path to the flattened
1187 // APEX (as its contents is installed by Make, not Soong).
1188 factx := flattenedApexContext{ctx}
1189 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1190
Alex Light5098a612018-11-29 17:12:15 -08001191 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001192 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001193 }
1194 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001195 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001196 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001197 // in other modules. It is in AndroidMk where the selection of flattened
1198 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001199 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001200 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001201 }
1202}
1203
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001204func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001205 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001206 for _, f := range a.filesInfo {
1207 if f.module != nil {
1208 notice := f.module.NoticeFile()
1209 if notice.Valid() {
1210 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001211 }
1212 }
1213 }
1214 // append the notice file specified in the apex module itself
1215 if a.NoticeFile().Valid() {
1216 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001217 }
1218
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001219 if len(noticeFiles) == 0 {
1220 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001221 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001222
Jaewoong Jung98772792019-07-01 17:15:13 -07001223 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001224}
1225
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001226func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001227 cert := String(a.properties.Certificate)
1228 if cert != "" && android.SrcIsModule(cert) == "" {
1229 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001230 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1231 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001232 } else if cert == "" {
1233 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001234 a.container_certificate_file = pem
1235 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001236 }
1237
Alex Light5098a612018-11-29 17:12:15 -08001238 var abis []string
1239 for _, target := range ctx.MultiTargets() {
1240 if len(target.Arch.Abi) > 0 {
1241 abis = append(abis, target.Arch.Abi[0])
1242 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001243 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001244
Alex Light5098a612018-11-29 17:12:15 -08001245 abis = android.FirstUniqueStrings(abis)
1246
1247 suffix := apexType.suffix()
1248 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001249
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001250 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001251 for _, f := range a.filesInfo {
1252 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001253 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001254
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001255 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001256 emitCommands := []string{}
1257 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1258 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001259 for i, src := range filesToCopy {
1260 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001261 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001262 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001263 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1264 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001265 for _, sym := range a.filesInfo[i].symlinks {
1266 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1267 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1268 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001269 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001270 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001271 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001272
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001273 if a.properties.Whitelisted_files != nil {
1274 ctx.Build(pctx, android.BuildParams{
1275 Rule: emitApexContentRule,
1276 Implicits: implicitInputs,
1277 Output: imageContentFile,
1278 Description: "emit apex image content",
1279 Args: map[string]string{
1280 "emit_commands": strings.Join(emitCommands, " && "),
1281 },
1282 })
1283 implicitInputs = append(implicitInputs, imageContentFile)
1284 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1285
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001286 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001287 ctx.Build(pctx, android.BuildParams{
1288 Rule: diffApexContentRule,
1289 Implicits: implicitInputs,
1290 Output: phonyOutput,
1291 Description: "diff apex image content",
1292 Args: map[string]string{
1293 "whitelisted_files_file": whitelistedFilesFile.String(),
1294 "image_content_file": imageContentFile.String(),
1295 "apex_module_name": ctx.ModuleName(),
1296 },
1297 })
1298
1299 implicitInputs = append(implicitInputs, phonyOutput)
1300 }
1301
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001302 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1303 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001304
Alex Light5098a612018-11-29 17:12:15 -08001305 if apexType.image() {
1306 // files and dirs that will be created in APEX
1307 var readOnlyPaths []string
1308 var executablePaths []string // this also includes dirs
1309 for _, f := range a.filesInfo {
1310 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001311 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001312 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001313 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001314 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001315 }
Alex Light5098a612018-11-29 17:12:15 -08001316 } else {
1317 readOnlyPaths = append(readOnlyPaths, pathInApex)
1318 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001319 dir := f.installDir
1320 for !android.InList(dir, executablePaths) && dir != "" {
1321 executablePaths = append(executablePaths, dir)
1322 dir, _ = filepath.Split(dir) // move up to the parent
1323 if len(dir) > 0 {
1324 // remove trailing slash
1325 dir = dir[:len(dir)-1]
1326 }
Alex Light5098a612018-11-29 17:12:15 -08001327 }
1328 }
1329 sort.Strings(readOnlyPaths)
1330 sort.Strings(executablePaths)
1331 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1332 ctx.Build(pctx, android.BuildParams{
1333 Rule: generateFsConfig,
1334 Output: cannedFsConfig,
1335 Description: "generate fs config",
1336 Args: map[string]string{
1337 "ro_paths": strings.Join(readOnlyPaths, " "),
1338 "exec_paths": strings.Join(executablePaths, " "),
1339 },
1340 })
1341
1342 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1343 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1344 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1345 if !fileContextsOptionalPath.Valid() {
1346 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1347 return
1348 }
1349 fileContexts := fileContextsOptionalPath.Path()
1350
Jiyong Park835d82b2018-12-27 16:04:18 +09001351 optFlags := []string{}
1352
Alex Light5098a612018-11-29 17:12:15 -08001353 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001354 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1355 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001356
Jiyong Park7f67f482019-01-05 12:57:48 +09001357 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1358 if overridden {
1359 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1360 }
1361
Jiyong Park40e26a22019-02-08 02:53:06 +09001362 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001363 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001364 implicitInputs = append(implicitInputs, androidManifestFile)
1365 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1366 }
1367
Jiyong Park71b519d2019-04-18 17:25:49 +09001368 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1369 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1370 ctx.Config().UnbundledBuild() &&
1371 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1372 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1373 apiFingerprint := java.ApiFingerprintPath(ctx)
1374 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1375 implicitInputs = append(implicitInputs, apiFingerprint)
1376 }
1377 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1378
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001379 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1380 if noticeFile.Valid() {
1381 // If there's a NOTICE file, embed it as an asset file in the APEX.
1382 implicitInputs = append(implicitInputs, noticeFile.Path())
1383 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1384 }
1385
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001386 if !ctx.Config().UnbundledBuild() && a.installable() {
1387 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1388 // don't need hashtree for activation. Therefore, by removing hashtree from
1389 // apex bundle (filesystem image in it, to be specific), we can save storage.
1390 optFlags = append(optFlags, "--no_hashtree")
1391 }
1392
Alex Light5098a612018-11-29 17:12:15 -08001393 ctx.Build(pctx, android.BuildParams{
1394 Rule: apexRule,
1395 Implicits: implicitInputs,
1396 Output: unsignedOutputFile,
1397 Description: "apex (" + apexType.name() + ")",
1398 Args: map[string]string{
1399 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1400 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1401 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001402 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001403 "file_contexts": fileContexts.String(),
1404 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001405 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001406 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001407 },
1408 })
1409
1410 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1411 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1412 a.bundleModuleFile = bundleModuleFile
1413
1414 ctx.Build(pctx, android.BuildParams{
1415 Rule: apexProtoConvertRule,
1416 Input: unsignedOutputFile,
1417 Output: apexProtoFile,
1418 Description: "apex proto convert",
1419 })
1420
1421 ctx.Build(pctx, android.BuildParams{
1422 Rule: apexBundleRule,
1423 Input: apexProtoFile,
1424 Output: a.bundleModuleFile,
1425 Description: "apex bundle module",
1426 Args: map[string]string{
1427 "abi": strings.Join(abis, "."),
1428 },
1429 })
1430 } else {
1431 ctx.Build(pctx, android.BuildParams{
1432 Rule: zipApexRule,
1433 Implicits: implicitInputs,
1434 Output: unsignedOutputFile,
1435 Description: "apex (" + apexType.name() + ")",
1436 Args: map[string]string{
1437 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1438 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1439 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001440 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001441 },
1442 })
Colin Crossa4925902018-11-16 11:36:28 -08001443 }
Colin Crossa4925902018-11-16 11:36:28 -08001444
Alex Light5098a612018-11-29 17:12:15 -08001445 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001446 ctx.Build(pctx, android.BuildParams{
1447 Rule: java.Signapk,
1448 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001449 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001450 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001451 Implicits: []android.Path{
1452 a.container_certificate_file,
1453 a.container_private_key_file,
1454 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001455 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001456 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001457 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001458 },
1459 })
Alex Light5098a612018-11-29 17:12:15 -08001460
1461 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001462 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001463 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001464 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001465}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001466
Jiyong Park8fd61922018-11-08 02:50:25 +09001467func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001468 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001469 // 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 +09001470 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001471 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001472
Jiyong Park42cca6c2019-04-01 11:15:50 +09001473 // rename to apex_pubkey
1474 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1475 ctx.Build(pctx, android.BuildParams{
1476 Rule: android.Cp,
1477 Input: a.public_key_file,
1478 Output: copiedPubkey,
1479 })
1480 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1481
Jiyong Park23c52b02019-02-02 13:13:47 +09001482 if ctx.Config().FlattenApex() {
1483 for _, fi := range a.filesInfo {
1484 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001485 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1486 for _, sym := range fi.symlinks {
1487 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1488 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001489 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001490 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001491 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001492}
1493
1494func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001495 if a.properties.HideFromMake {
1496 return android.AndroidMkData{
1497 Disabled: true,
1498 }
1499 }
Alex Light5098a612018-11-29 17:12:15 -08001500 writers := []android.AndroidMkData{}
1501 if a.apexTypes.image() {
1502 writers = append(writers, a.androidMkForType(imageApex))
1503 }
1504 if a.apexTypes.zip() {
1505 writers = append(writers, a.androidMkForType(zipApex))
1506 }
1507 return android.AndroidMkData{
1508 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1509 for _, data := range writers {
1510 data.Custom(w, name, prefix, moduleDir, data)
1511 }
1512 }}
1513}
1514
Alex Lightf1801bc2019-02-13 11:10:07 -08001515func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001516 moduleNames := []string{}
1517
1518 for _, fi := range a.filesInfo {
1519 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1520 continue
1521 }
1522 if !android.InList(fi.moduleName, moduleNames) {
1523 moduleNames = append(moduleNames, fi.moduleName)
1524 }
1525 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1526 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1527 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001528 // /apex/<name>/{lib|framework|...}
1529 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1530 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001531 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001532 // /system/apex/<name>/{lib|framework|...}
1533 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1534 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001535 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001536 if len(fi.symlinks) > 0 {
1537 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1538 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001539
1540 if fi.module != nil && fi.module.NoticeFile().Valid() {
1541 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1542 }
Jiyong Park94427262019-02-05 23:18:47 +09001543 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001544 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001545 }
1546 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1547 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1548 if fi.module != nil {
1549 archStr := fi.module.Target().Arch.ArchType.String()
1550 host := false
1551 switch fi.module.Target().Os.Class {
1552 case android.Host:
1553 if archStr != "common" {
1554 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1555 }
1556 host = true
1557 case android.HostCross:
1558 if archStr != "common" {
1559 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1560 }
1561 host = true
1562 case android.Device:
1563 if archStr != "common" {
1564 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1565 }
1566 }
1567 if host {
1568 makeOs := fi.module.Target().Os.String()
1569 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1570 makeOs = "linux"
1571 }
1572 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1573 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1574 }
1575 }
1576 if fi.class == javaSharedLib {
1577 javaModule := fi.module.(*java.Library)
1578 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1579 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1580 // we will have foo.jar.jar
1581 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1582 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1583 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1584 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1585 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1586 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1587 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1588 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001589 if cc, ok := fi.module.(*cc.Module); ok {
1590 if cc.UnstrippedOutputFile() != nil {
1591 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1592 }
1593 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001594 if cc.CoverageOutputFile().Valid() {
1595 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1596 }
Jiyong Park94427262019-02-05 23:18:47 +09001597 }
1598 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1599 } else {
1600 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1601 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1602 }
1603 }
1604 return moduleNames
1605}
1606
Alex Light5098a612018-11-29 17:12:15 -08001607func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001608 return android.AndroidMkData{
1609 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1610 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001611 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001612 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001613 }
1614
Jiyong Park719b4462019-01-13 00:39:51 +09001615 if a.flattened && apexType.image() {
1616 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001617 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1618 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1619 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001620 if len(moduleNames) > 0 {
1621 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1622 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001623 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001624 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1625
Jiyong Park719b4462019-01-13 00:39:51 +09001626 } else {
Alex Light5098a612018-11-29 17:12:15 -08001627 // zip-apex is the less common type so have the name refer to the image-apex
1628 // only and use {name}.zip if you want the zip-apex
1629 if apexType == zipApex && a.apexTypes == both {
1630 name = name + ".zip"
1631 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001632 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1633 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1634 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1635 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001636 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001637 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001638 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001639 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001640 if len(moduleNames) > 0 {
1641 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1642 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001643 if len(a.externalDeps) > 0 {
1644 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1645 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001646 if a.prebuiltFileToDelete != "" {
1647 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1648 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1649 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001650 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001651
Alex Light5098a612018-11-29 17:12:15 -08001652 if apexType == imageApex {
1653 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1654 }
Jiyong Park719b4462019-01-13 00:39:51 +09001655 }
1656 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001657}
1658
Jooyung Han344d5432019-08-23 11:17:39 +09001659func newApexBundle() *apexBundle {
Alex Light5098a612018-11-29 17:12:15 -08001660 module := &apexBundle{
1661 outputFiles: map[apexPackaging]android.WritablePath{},
1662 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001663 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001664 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001665 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001666 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1667 })
Alex Light5098a612018-11-29 17:12:15 -08001668 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001669 android.InitDefaultableModule(module)
1670 return module
1671}
Jiyong Park30ca9372019-02-07 16:27:23 +09001672
Jooyung Han344d5432019-08-23 11:17:39 +09001673func ApexBundleFactory(testApex bool) android.Module {
1674 bundle := newApexBundle()
1675 bundle.testApex = testApex
1676 return bundle
1677}
1678
1679func testApexBundleFactory() android.Module {
1680 bundle := newApexBundle()
1681 bundle.testApex = true
1682 return bundle
1683}
1684
1685func apexBundleFactory() android.Module {
1686 return newApexBundle()
1687}
1688
1689// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1690// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1691// If not specified, then the "current" versions are gathered.
1692func vndkApexBundleFactory() android.Module {
1693 bundle := newApexBundle()
1694 bundle.vndkApex = true
1695 bundle.AddProperties(&bundle.vndkProperties)
1696 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1697 ctx.AppendProperties(&struct {
1698 Compile_multilib *string
1699 }{
1700 proptools.StringPtr("both"),
1701 })
1702 })
1703 return bundle
1704}
1705
Jiyong Park30ca9372019-02-07 16:27:23 +09001706//
1707// Defaults
1708//
1709type Defaults struct {
1710 android.ModuleBase
1711 android.DefaultsModuleBase
1712}
1713
Jiyong Park30ca9372019-02-07 16:27:23 +09001714func defaultsFactory() android.Module {
1715 return DefaultsFactory()
1716}
1717
1718func DefaultsFactory(props ...interface{}) android.Module {
1719 module := &Defaults{}
1720
1721 module.AddProperties(props...)
1722 module.AddProperties(
1723 &apexBundleProperties{},
1724 &apexTargetBundleProperties{},
1725 )
1726
1727 android.InitDefaultsModule(module)
1728 return module
1729}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001730
1731//
1732// Prebuilt APEX
1733//
1734type Prebuilt struct {
1735 android.ModuleBase
1736 prebuilt android.Prebuilt
1737
1738 properties PrebuiltProperties
1739
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001740 inputApex android.Path
1741 installDir android.OutputPath
1742 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001743 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001744}
1745
1746type PrebuiltProperties struct {
1747 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001748 Source string `blueprint:"mutated"`
1749 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001750
1751 Src *string
1752 Arch struct {
1753 Arm struct {
1754 Src *string
1755 }
1756 Arm64 struct {
1757 Src *string
1758 }
1759 X86 struct {
1760 Src *string
1761 }
1762 X86_64 struct {
1763 Src *string
1764 }
1765 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001766
1767 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001768 // Optional name for the installed apex. If unspecified, name of the
1769 // module is used as the file name
1770 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001771
1772 // Names of modules to be overridden. Listed modules can only be other binaries
1773 // (in Make or Soong).
1774 // This does not completely prevent installation of the overridden binaries, but if both
1775 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1776 // from PRODUCT_PACKAGES.
1777 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001778}
1779
1780func (p *Prebuilt) installable() bool {
1781 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001782}
1783
1784func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001785 // If the device is configured to use flattened APEX, force disable the prebuilt because
1786 // the prebuilt is a non-flattened one.
1787 forceDisable := ctx.Config().FlattenApex()
1788
1789 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1790 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001791 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001792
Kun Niu10c9f832019-07-29 16:28:57 -07001793 // Force disable the prebuilts when coverage is enabled.
1794 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1795 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1796
Jiyong Park50b81e52019-07-11 11:24:41 +09001797 // b/137216042 don't use prebuilts when address sanitizer is on
1798 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1799 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1800
1801 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001802 p.properties.ForceDisable = true
1803 return
1804 }
1805
Jiyong Parkc95714e2019-03-29 14:23:10 +09001806 // This is called before prebuilt_select and prebuilt_postdeps mutators
1807 // The mutators requires that src to be set correctly for each arch so that
1808 // arch variants are disabled when src is not provided for the arch.
1809 if len(ctx.MultiTargets()) != 1 {
1810 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1811 return
1812 }
1813 var src string
1814 switch ctx.MultiTargets()[0].Arch.ArchType {
1815 case android.Arm:
1816 src = String(p.properties.Arch.Arm.Src)
1817 case android.Arm64:
1818 src = String(p.properties.Arch.Arm64.Src)
1819 case android.X86:
1820 src = String(p.properties.Arch.X86.Src)
1821 case android.X86_64:
1822 src = String(p.properties.Arch.X86_64.Src)
1823 default:
1824 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1825 return
1826 }
1827 if src == "" {
1828 src = String(p.properties.Src)
1829 }
1830 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001831}
1832
Jiyong Park03b68dd2019-07-26 23:20:40 +09001833func (p *Prebuilt) isForceDisabled() bool {
1834 return p.properties.ForceDisable
1835}
1836
Colin Cross41955e82019-05-29 14:40:35 -07001837func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1838 switch tag {
1839 case "":
1840 return android.Paths{p.outputApex}, nil
1841 default:
1842 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1843 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001844}
1845
Jiyong Park4d277042019-04-23 18:00:10 +09001846func (p *Prebuilt) InstallFilename() string {
1847 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1848}
1849
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001850func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001851 if p.properties.ForceDisable {
1852 return
1853 }
1854
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001855 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001856 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001857 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001858 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001859 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1860 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1861 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001862 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1863 ctx.Build(pctx, android.BuildParams{
1864 Rule: android.Cp,
1865 Input: p.inputApex,
1866 Output: p.outputApex,
1867 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001868 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001869 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001870 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001871}
1872
1873func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1874 return &p.prebuilt
1875}
1876
1877func (p *Prebuilt) Name() string {
1878 return p.prebuilt.Name(p.ModuleBase.Name())
1879}
1880
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001881func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1882 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001883 Class: "ETC",
1884 OutputFile: android.OptionalPathForPath(p.inputApex),
1885 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001886 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1887 func(entries *android.AndroidMkEntries) {
1888 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1889 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1890 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1891 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1892 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001893 },
1894 }
1895}
1896
1897// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1898func PrebuiltFactory() android.Module {
1899 module := &Prebuilt{}
1900 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001901 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001902 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001903 return module
1904}