blob: 28935f28ed281d44ca7c10d2676e00218b59553b [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
Jooyung Han344d5432019-08-23 11:17:39 +090024 "sync"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090025
26 "android/soong/android"
27 "android/soong/cc"
28 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080029 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030
31 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080032 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090033 "github.com/google/blueprint/proptools"
34)
35
36var (
37 pctx = android.NewPackageContext("android/apex")
38
39 // Create a canned fs config file where all files and directories are
40 // by default set to (uid/gid/mode) = (1000/1000/0644)
41 // TODO(b/113082813) make this configurable using config.fs syntax
42 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000043 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000044 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090045 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090046 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090047 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090048 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090049
Jooyung Hand15aa1f2019-09-27 00:38:03 +090050 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
Jooyung Hane1633032019-08-01 17:41:43 +090051 Command: `rm -f $out && ${jsonmodify} $in ` +
52 `-a provideNativeLibs ${provideNativeLibs} ` +
Jooyung Hand15aa1f2019-09-27 00:38:03 +090053 `-a requireNativeLibs ${requireNativeLibs} ` +
54 `${opt} ` +
55 `-o $out`,
Jooyung Hane1633032019-08-01 17:41:43 +090056 CommandDeps: []string{"${jsonmodify}"},
Jooyung Hand15aa1f2019-09-27 00:38:03 +090057 Description: "prepare ${out}",
58 }, "provideNativeLibs", "requireNativeLibs", "opt")
Jooyung Hane1633032019-08-01 17:41:43 +090059
Jiyong Park48ca7dc2018-10-10 14:01:00 +090060 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
61 // against the binary policy using sefcontext_compiler -p <policy>.
62
63 // TODO(b/114327326): automate the generation of file_contexts
64 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
65 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010066 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090067 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090068 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090069 `--file_contexts ${file_contexts} ` +
70 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080071 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090072 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090073 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
74 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000075 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010076 Rspfile: "${out}.copy_commands",
77 RspfileContent: "${copy_commands}",
78 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090079 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080080
Alex Light5098a612018-11-29 17:12:15 -080081 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
82 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010083 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080084 `APEXER_TOOL_PATH=${tool_path} ` +
85 `${apexer} --force --manifest ${manifest} ` +
86 `--payload_type zip ` +
87 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010088 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
89 Rspfile: "${out}.copy_commands",
90 RspfileContent: "${copy_commands}",
91 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080092 }, "tool_path", "image_dir", "copy_commands", "manifest")
93
Colin Crossa4925902018-11-16 11:36:28 -080094 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
95 blueprint.RuleParams{
96 Command: `${aapt2} convert --output-format proto $in -o $out`,
97 CommandDeps: []string{"${aapt2}"},
98 })
99
100 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +0900101 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +0000102 `apex_payload.img:apex/${abi}.img ` +
103 `apex_manifest.json:root/apex_manifest.json ` +
Jaewoong Jungb00c1fb2019-09-04 13:26:18 -0700104 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
105 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -0800106 CommandDeps: []string{"${zip2zip}"},
107 Description: "app bundle",
108 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100109
110 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
111 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
112 Rspfile: "${out}.emit_commands",
113 RspfileContent: "${emit_commands}",
114 Description: "Emit APEX image content",
115 }, "emit_commands")
116
117 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
118 Command: `diff --unchanged-group-format='' \` +
119 `--changed-group-format='%<' \` +
120 `${image_content_file} ${whitelisted_files_file} || (` +
121 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
122 ` "To fix the build run following command:" && ` +
123 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
124 `exit 1)`,
125 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
126 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900127)
128
Jooyung Han72bd2f82019-10-23 16:46:38 +0900129const (
130 imageApexSuffix = ".apex"
131 zipApexSuffix = ".zipapex"
Alex Light5098a612018-11-29 17:12:15 -0800132
Jooyung Han72bd2f82019-10-23 16:46:38 +0900133 imageApexType = "image"
134 zipApexType = "zip"
135
136 vndkApexNamePrefix = "com.android.vndk.v"
137)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900138
139type dependencyTag struct {
140 blueprint.BaseDependencyTag
141 name string
142}
143
144var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900145 sharedLibTag = dependencyTag{name: "sharedLib"}
146 executableTag = dependencyTag{name: "executable"}
147 javaLibTag = dependencyTag{name: "javaLib"}
148 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100149 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900150 keyTag = dependencyTag{name: "key"}
151 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900152 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900153 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900154)
155
156func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700157 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900158 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900159 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100160 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
161 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
162 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
163 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000164 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100165 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
166 } else {
167 return pctx.HostBinToolPath(ctx, tool).String()
168 }
169 })
170 }
171 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900172 pctx.HostBinToolVariable("avbtool", "avbtool")
173 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
174 pctx.HostBinToolVariable("merge_zips", "merge_zips")
175 pctx.HostBinToolVariable("mke2fs", "mke2fs")
176 pctx.HostBinToolVariable("resize2fs", "resize2fs")
177 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
178 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800179 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900180 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900181 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900182
Jiyong Parkd1063c12019-07-17 20:08:41 +0900183 android.RegisterModuleType("apex", BundleFactory)
Alex Light0851b882019-02-07 13:20:53 -0800184 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jooyung Han344d5432019-08-23 11:17:39 +0900185 android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900186 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700187 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900188
Jooyung Han31c470b2019-10-18 16:26:59 +0900189 android.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900190 android.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han7a78a922019-10-08 21:59:58 +0900191
192 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
193 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
194 sort.Strings(*apexFileContextsInfos)
195 ctx.Strict("APEX_FILE_CONTEXTS_INFOS", strings.Join(*apexFileContextsInfos, " "))
196 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900197}
198
Jooyung Han31c470b2019-10-18 16:26:59 +0900199func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
200 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
201 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
202}
203
Jiyong Parkd1063c12019-07-17 20:08:41 +0900204func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
205 ctx.TopDown("apex_deps", apexDepsMutator)
206 ctx.BottomUp("apex", apexMutator).Parallel()
207 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
208 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900209}
210
Jooyung Han344d5432019-08-23 11:17:39 +0900211var (
212 vndkApexListKey = android.NewOnceKey("vndkApexList")
213 vndkApexListMutex sync.Mutex
214)
215
Jooyung Han31c470b2019-10-18 16:26:59 +0900216func vndkApexList(config android.Config) map[string]string {
Jooyung Han344d5432019-08-23 11:17:39 +0900217 return config.Once(vndkApexListKey, func() interface{} {
Jooyung Han31c470b2019-10-18 16:26:59 +0900218 return map[string]string{}
219 }).(map[string]string)
Jooyung Han344d5432019-08-23 11:17:39 +0900220}
221
Jooyung Han31c470b2019-10-18 16:26:59 +0900222func apexVndkMutator(mctx android.TopDownMutatorContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900223 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
224 if ab.IsNativeBridgeSupported() {
225 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
226 }
Jooyung Han90eee022019-10-01 20:02:42 +0900227
Jooyung Han31c470b2019-10-18 16:26:59 +0900228 vndkVersion := ab.vndkVersion(mctx.DeviceConfig())
229 // Ensure VNDK APEX mount point is formatted as com.android.vndk.v###
Jooyung Han72bd2f82019-10-23 16:46:38 +0900230 ab.properties.Apex_name = proptools.StringPtr(vndkApexNamePrefix + vndkVersion)
Jooyung Han90eee022019-10-01 20:02:42 +0900231
Jooyung Han31c470b2019-10-18 16:26:59 +0900232 // vndk_version should be unique
Jooyung Han344d5432019-08-23 11:17:39 +0900233 vndkApexListMutex.Lock()
234 defer vndkApexListMutex.Unlock()
235 vndkApexList := vndkApexList(mctx.Config())
236 if other, ok := vndkApexList[vndkVersion]; ok {
Jooyung Han31c470b2019-10-18 16:26:59 +0900237 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other)
Jooyung Han344d5432019-08-23 11:17:39 +0900238 }
Jooyung Han31c470b2019-10-18 16:26:59 +0900239 vndkApexList[vndkVersion] = mctx.ModuleName()
Jooyung Han344d5432019-08-23 11:17:39 +0900240 }
241}
242
Jooyung Han31c470b2019-10-18 16:26:59 +0900243func apexVndkDepsMutator(mctx android.BottomUpMutatorContext) {
244 if m, ok := mctx.Module().(*cc.Module); ok && cc.IsForVndkApex(mctx, m) {
245 vndkVersion := m.VndkVersion()
Jooyung Han344d5432019-08-23 11:17:39 +0900246 vndkApexList := vndkApexList(mctx.Config())
Jooyung Han31c470b2019-10-18 16:26:59 +0900247 if vndkApex, ok := vndkApexList[vndkVersion]; ok {
248 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, vndkApex)
Jooyung Han344d5432019-08-23 11:17:39 +0900249 }
250 }
251}
252
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900253// Mark the direct and transitive dependencies of apex bundles so that they
254// can be built for the apex bundles.
255func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800256 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800257 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900258 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900259 depName := mctx.OtherModuleName(child)
260 // If the parent is apexBundle, this child is directly depended.
261 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800262 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800263 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
264 // non-installable apex's cannot be installed and so should not prevent libraries from being
265 // installed to the system.
266 android.UpdateApexDependency(apexBundleName, depName, directDep)
267 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900268
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900269 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900270 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900271 return true
272 } else {
273 return false
274 }
275 })
276 }
277}
278
279// Create apex variations if a module is included in APEX(s).
280func apexMutator(mctx android.BottomUpMutatorContext) {
281 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900282 am.CreateApexVariations(mctx)
Jooyung Han7a78a922019-10-08 21:59:58 +0900283 } else if a, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900284 // apex bundle itself is mutated so that it and its modules have same
285 // apex variant.
286 apexBundleName := mctx.ModuleName()
287 mctx.CreateVariations(apexBundleName)
Jooyung Han7a78a922019-10-08 21:59:58 +0900288
289 // collects APEX list
290 if mctx.Device() && a.installable() {
291 addApexFileContextsInfos(mctx, a)
292 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900293 }
294}
Sundong Ahne9b55722019-09-06 17:37:42 +0900295
Jooyung Han7a78a922019-10-08 21:59:58 +0900296var (
297 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
298 apexFileContextsInfosMutex sync.Mutex
299)
300
301func apexFileContextsInfos(config android.Config) *[]string {
302 return config.Once(apexFileContextsInfosKey, func() interface{} {
303 return &[]string{}
304 }).(*[]string)
305}
306
307func addApexFileContextsInfos(ctx android.BaseModuleContext, a *apexBundle) {
308 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
309 fileContextsName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
310
311 apexFileContextsInfosMutex.Lock()
312 defer apexFileContextsInfosMutex.Unlock()
313 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
314 *apexFileContextsInfos = append(*apexFileContextsInfos, apexName+":"+fileContextsName)
315}
316
Sundong Ahne9b55722019-09-06 17:37:42 +0900317func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900318 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahne9b55722019-09-06 17:37:42 +0900319 if !mctx.Config().FlattenApex() || mctx.Config().UnbundledBuild() {
320 modules := mctx.CreateLocalVariations("", "flattened")
321 modules[0].(*apexBundle).SetFlattened(false)
322 modules[1].(*apexBundle).SetFlattened(true)
Sundong Ahne8fb7242019-09-17 13:50:45 +0900323 } else {
324 ab.SetFlattened(true)
325 ab.SetFlattenedConfigValue()
Sundong Ahne9b55722019-09-06 17:37:42 +0900326 }
327 }
328}
329
Jooyung Han5c998b92019-06-27 11:30:33 +0900330func apexUsesMutator(mctx android.BottomUpMutatorContext) {
331 if ab, ok := mctx.Module().(*apexBundle); ok {
332 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
333 }
334}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900335
Alex Light9670d332019-01-29 18:07:33 -0800336type apexNativeDependencies struct {
337 // List of native libraries
338 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900339
Alex Light9670d332019-01-29 18:07:33 -0800340 // List of native executables
341 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900342
Roland Levillain630846d2019-06-26 12:48:34 +0100343 // List of native tests
344 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800345}
Jooyung Han344d5432019-08-23 11:17:39 +0900346
Alex Light9670d332019-01-29 18:07:33 -0800347type apexMultilibProperties struct {
348 // Native dependencies whose compile_multilib is "first"
349 First apexNativeDependencies
350
351 // Native dependencies whose compile_multilib is "both"
352 Both apexNativeDependencies
353
354 // Native dependencies whose compile_multilib is "prefer32"
355 Prefer32 apexNativeDependencies
356
357 // Native dependencies whose compile_multilib is "32"
358 Lib32 apexNativeDependencies
359
360 // Native dependencies whose compile_multilib is "64"
361 Lib64 apexNativeDependencies
362}
363
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900364type apexBundleProperties struct {
365 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000366 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800367 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900368
Jiyong Park40e26a22019-02-08 02:53:06 +0900369 // AndroidManifest.xml file used for the zip container of this APEX bundle.
370 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800371 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900372
Roland Levillain411c5842019-09-19 16:37:20 +0100373 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
374 // device (/apex/<apex_name>).
375 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900376 Apex_name *string
377
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900378 // Determines the file contexts file for setting security context to each file in this APEX bundle.
379 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
380 // used.
381 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900382 File_contexts *string
383
384 // List of native shared libs that are embedded inside this APEX bundle
385 Native_shared_libs []string
386
Roland Levillain630846d2019-06-26 12:48:34 +0100387 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900388 Binaries []string
389
390 // List of java libraries that are embedded inside this APEX bundle
391 Java_libs []string
392
393 // List of prebuilt files that are embedded inside this APEX bundle
394 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900395
Roland Levillain630846d2019-06-26 12:48:34 +0100396 // List of tests that are embedded inside this APEX bundle
397 Tests []string
398
Jiyong Parkff1458f2018-10-12 21:49:38 +0900399 // Name of the apex_key module that provides the private key to sign APEX
400 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900401
Alex Light5098a612018-11-29 17:12:15 -0800402 // The type of APEX to build. Controls what the APEX payload is. Either
403 // 'image', 'zip' or 'both'. Default: 'image'.
404 Payload_type *string
405
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900406 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
407 // or an android_app_certificate module name in the form ":module".
408 Certificate *string
409
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900410 // Whether this APEX is installable to one of the partitions. Default: true.
411 Installable *bool
412
Jiyong Parkda6eb592018-12-19 17:12:36 +0900413 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
414 // Default is false.
415 Use_vendor *bool
416
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800417 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
418 Ignore_system_library_special_case *bool
419
Alex Light9670d332019-01-29 18:07:33 -0800420 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900421
Jiyong Parkf97782b2019-02-13 20:28:58 +0900422 // List of sanitizer names that this APEX is enabled for
423 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900424
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900425 PreventInstall bool `blueprint:"mutated"`
426
427 HideFromMake bool `blueprint:"mutated"`
428
Jooyung Han5c998b92019-06-27 11:30:33 +0900429 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
430 Provide_cpp_shared_libs *bool
431
432 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
433 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100434
435 // A txt file containing list of files that are whitelisted to be included in this APEX.
436 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900437
438 // List of APKs to package inside APEX
439 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900440
Sundong Ahne8fb7242019-09-17 13:50:45 +0900441 // To distinguish between flattened and non-flattened apex.
442 // if set true, then output files are flattened.
Sundong Ahne9b55722019-09-06 17:37:42 +0900443 Flattened bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +0900444
Sundong Ahne8fb7242019-09-17 13:50:45 +0900445 // if true, it means that TARGET_FLATTEN_APEX is true and
446 // TARGET_BUILD_APPS is false
447 FlattenedConfigValue bool `blueprint:"mutated"`
448
Jiyong Parkd1063c12019-07-17 20:08:41 +0900449 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
450 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
451 // is implied. This value affects all modules included in this APEX. In other words, they are
452 // also built with the SDKs specified here.
453 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800454}
455
456type apexTargetBundleProperties struct {
457 Target struct {
458 // Multilib properties only for android.
459 Android struct {
460 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900461 }
Jooyung Han344d5432019-08-23 11:17:39 +0900462
Alex Light9670d332019-01-29 18:07:33 -0800463 // Multilib properties only for host.
464 Host struct {
465 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900466 }
Jooyung Han344d5432019-08-23 11:17:39 +0900467
Alex Light9670d332019-01-29 18:07:33 -0800468 // Multilib properties only for host linux_bionic.
469 Linux_bionic struct {
470 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900471 }
Jooyung Han344d5432019-08-23 11:17:39 +0900472
Alex Light9670d332019-01-29 18:07:33 -0800473 // Multilib properties only for host linux_glibc.
474 Linux_glibc struct {
475 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900476 }
477 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900478}
479
Jooyung Han344d5432019-08-23 11:17:39 +0900480type apexVndkProperties struct {
481 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
482 Vndk_version *string
483}
484
Jiyong Park8fd61922018-11-08 02:50:25 +0900485type apexFileClass int
486
487const (
488 etc apexFileClass = iota
489 nativeSharedLib
490 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900491 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800492 pyBinary
493 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900494 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100495 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900496 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900497)
498
Alex Light5098a612018-11-29 17:12:15 -0800499type apexPackaging int
500
501const (
502 imageApex apexPackaging = iota
503 zipApex
504 both
505)
506
507func (a apexPackaging) image() bool {
508 switch a {
509 case imageApex, both:
510 return true
511 }
512 return false
513}
514
515func (a apexPackaging) zip() bool {
516 switch a {
517 case zipApex, both:
518 return true
519 }
520 return false
521}
522
523func (a apexPackaging) suffix() string {
524 switch a {
525 case imageApex:
526 return imageApexSuffix
527 case zipApex:
528 return zipApexSuffix
529 case both:
530 panic(fmt.Errorf("must be either zip or image"))
531 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100532 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800533 }
534}
535
536func (a apexPackaging) name() string {
537 switch a {
538 case imageApex:
539 return imageApexType
540 case zipApex:
541 return zipApexType
542 case both:
543 panic(fmt.Errorf("must be either zip or image"))
544 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100545 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800546 }
547}
548
Jiyong Park8fd61922018-11-08 02:50:25 +0900549func (class apexFileClass) NameInMake() string {
550 switch class {
551 case etc:
552 return "ETC"
553 case nativeSharedLib:
554 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800555 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900556 return "EXECUTABLES"
557 case javaSharedLib:
558 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100559 case nativeTest:
560 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900561 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900562 // b/142537672 Why isn't this APP? We want to have full control over
563 // the paths and file names of the apk file under the flattend APEX.
564 // If this is set to APP, then the paths and file names are modified
565 // by the Make build system. For example, it is installed to
566 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
567 // /system/apex/<apexname>/app/<Appname> because the build system automatically
568 // appends module name (which is <apexname>.<Appname> to the path.
569 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900570 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100571 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900572 }
573}
574
575type apexFile struct {
576 builtFile android.Path
577 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900578 installDir string
579 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900580 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800581 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900582}
583
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900584type apexBundle struct {
585 android.ModuleBase
586 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900587 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900588
Alex Light9670d332019-01-29 18:07:33 -0800589 properties apexBundleProperties
590 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900591 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900592
Alex Light5098a612018-11-29 17:12:15 -0800593 apexTypes apexPackaging
594
Colin Crossa4925902018-11-16 11:36:28 -0800595 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800596 outputFiles map[apexPackaging]android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700597 flattenedOutput android.InstallPath
598 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900599
Jiyong Park03b68dd2019-07-26 23:20:40 +0900600 prebuiltFileToDelete string
601
Jiyong Park42cca6c2019-04-01 11:15:50 +0900602 public_key_file android.Path
603 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900604
605 container_certificate_file android.Path
606 container_private_key_file android.Path
607
Jiyong Park8fd61922018-11-08 02:50:25 +0900608 // list of files to be included in this apex
609 filesInfo []apexFile
610
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900611 // list of module names that this APEX is depending on
612 externalDeps []string
613
Alex Light0851b882019-02-07 13:20:53 -0800614 testApex bool
Jooyung Han344d5432019-08-23 11:17:39 +0900615 vndkApex bool
Ulya Trafimovichd5df9492019-10-24 17:29:50 +0100616 artApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900617
618 // intermediate path for apex_manifest.json
619 manifestOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900620
621 // list of commands to create symlinks for backward compatibility
622 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
623 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
624 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
625 compatSymlinks []string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626}
627
Jiyong Park397e55e2018-10-24 21:09:55 +0900628func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100629 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700630 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900631 // Use *FarVariation* to be able to depend on modules having
632 // conflicting variations with this module. This is required since
633 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
634 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700635 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900636 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900637 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900638 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700639 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900640
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700641 ctx.AddFarVariationDependencies(append(target.Variations(),
642 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
643 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100644
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700645 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100646 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100647 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700648 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900649}
650
Alex Light9670d332019-01-29 18:07:33 -0800651func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
652 if ctx.Os().Class == android.Device {
653 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
654 } else {
655 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
656 if ctx.Os().Bionic() {
657 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
658 } else {
659 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
660 }
661 }
662}
663
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900664func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900665 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900666 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800667
668 a.combineProperties(ctx)
669
Jiyong Park397e55e2018-10-24 21:09:55 +0900670 has32BitTarget := false
671 for _, target := range targets {
672 if target.Arch.ArchType.Multilib == "lib32" {
673 has32BitTarget = true
674 }
675 }
676 for i, target := range targets {
677 // When multilib.* is omitted for native_shared_libs, it implies
678 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700679 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900680 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900681 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700682 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900683
Roland Levillain630846d2019-06-26 12:48:34 +0100684 // When multilib.* is omitted for tests, it implies
685 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700686 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100687 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100688 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700689 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100690
Jiyong Park397e55e2018-10-24 21:09:55 +0900691 // Add native modules targetting both ABIs
692 addDependenciesForNativeModules(ctx,
693 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100694 a.properties.Multilib.Both.Binaries,
695 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700696 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900697 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900698
Alex Light3d673592019-01-18 14:37:31 -0800699 isPrimaryAbi := i == 0
700 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900701 // When multilib.* is omitted for binaries, it implies
702 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700703 ctx.AddFarVariationDependencies(append(target.Variations(),
704 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
705 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900706
707 // Add native modules targetting the first ABI
708 addDependenciesForNativeModules(ctx,
709 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100710 a.properties.Multilib.First.Binaries,
711 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700712 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900713 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800714
715 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700716 ctx.AddFarVariationDependencies(target.Variations(),
717 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900718 }
719
720 switch target.Arch.ArchType.Multilib {
721 case "lib32":
722 // Add native modules targetting 32-bit ABI
723 addDependenciesForNativeModules(ctx,
724 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100725 a.properties.Multilib.Lib32.Binaries,
726 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700727 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900728 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900729
730 addDependenciesForNativeModules(ctx,
731 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100732 a.properties.Multilib.Prefer32.Binaries,
733 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700734 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900735 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900736 case "lib64":
737 // Add native modules targetting 64-bit ABI
738 addDependenciesForNativeModules(ctx,
739 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100740 a.properties.Multilib.Lib64.Binaries,
741 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700742 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900743 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900744
745 if !has32BitTarget {
746 addDependenciesForNativeModules(ctx,
747 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100748 a.properties.Multilib.Prefer32.Binaries,
749 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700750 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900751 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900752 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700753
754 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
755 for _, sanitizer := range ctx.Config().SanitizeDevice() {
756 if sanitizer == "hwaddress" {
757 addDependenciesForNativeModules(ctx,
758 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700759 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700760 break
761 }
762 }
763 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900764 }
765
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900766 }
767
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700768 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
769 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900770
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700771 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
772 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900773
Jiyong Park23c52b02019-02-02 13:13:47 +0900774 if String(a.properties.Key) == "" {
775 ctx.ModuleErrorf("key is missing")
776 return
777 }
778 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900779
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900780 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900781 if cert != "" {
782 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900783 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900784
785 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
786 if len(a.properties.Uses_sdks) > 0 {
787 sdkRefs := []android.SdkRef{}
788 for _, str := range a.properties.Uses_sdks {
789 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
790 sdkRefs = append(sdkRefs, parsed)
791 }
792 a.BuildWithSdks(sdkRefs)
793 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900794}
795
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900796func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
797 // direct deps of an APEX bundle are all part of the APEX bundle
798 return true
799}
800
Colin Cross0ea8ba82019-06-06 14:33:29 -0700801func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900802 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
803 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000804 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900805 }
806 return String(a.properties.Certificate)
807}
808
Colin Cross41955e82019-05-29 14:40:35 -0700809func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
810 switch tag {
811 case "":
812 if file, ok := a.outputFiles[imageApex]; ok {
813 return android.Paths{file}, nil
814 } else {
815 return nil, nil
816 }
Roland Levillain935639d2019-08-13 14:55:28 +0100817 case ".flattened":
Sundong Ahne9b55722019-09-06 17:37:42 +0900818 if a.properties.Flattened {
Roland Levillain935639d2019-08-13 14:55:28 +0100819 flattenedApexPath := a.flattenedOutput
820 return android.Paths{flattenedApexPath}, nil
821 } else {
822 return nil, nil
823 }
Colin Cross41955e82019-05-29 14:40:35 -0700824 default:
825 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900826 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900827}
828
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900829func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900830 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900831}
832
Jiyong Park7c1dc612019-01-05 11:15:24 +0900833func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900834 if a.vndkApex {
835 return "vendor." + a.vndkVersion(config)
836 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900837 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900838 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900839 } else {
840 return "core"
841 }
842}
843
Jiyong Parkf97782b2019-02-13 20:28:58 +0900844func (a *apexBundle) EnableSanitizer(sanitizerName string) {
845 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
846 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
847 }
848}
849
Jiyong Park388ef3f2019-01-28 19:47:32 +0900850func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900851 if android.InList(sanitizerName, a.properties.SanitizerNames) {
852 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900853 }
854
855 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900856 globalSanitizerNames := []string{}
857 if a.Host() {
858 globalSanitizerNames = ctx.Config().SanitizeHost()
859 } else {
860 arches := ctx.Config().SanitizeDeviceArch()
861 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
862 globalSanitizerNames = ctx.Config().SanitizeDevice()
863 }
864 }
865 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900866}
867
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900868func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
869 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
870}
871
872func (a *apexBundle) PreventInstall() {
873 a.properties.PreventInstall = true
874}
875
876func (a *apexBundle) HideFromMake() {
877 a.properties.HideFromMake = true
878}
879
Sundong Ahne9b55722019-09-06 17:37:42 +0900880func (a *apexBundle) SetFlattened(flattened bool) {
881 a.properties.Flattened = flattened
882}
883
Sundong Ahne8fb7242019-09-17 13:50:45 +0900884func (a *apexBundle) SetFlattenedConfigValue() {
885 a.properties.FlattenedConfigValue = true
886}
887
888// isFlattenedVariant returns true when the current module is the flattened
889// variant of an apex that has both a flattened and an unflattened variant.
890// It returns false when the current module is flattened but there is no
891// unflattened variant, which occurs when ctx.Config().FlattenedApex() returns
892// true. It can be used to avoid collisions between the install paths of the
893// flattened and unflattened variants.
894func (a *apexBundle) isFlattenedVariant() bool {
895 return a.properties.Flattened && !a.properties.FlattenedConfigValue
896}
897
Martin Stjernholm279de572019-09-10 23:18:20 +0100898func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900899 // Decide the APEX-local directory by the multilib of the library
900 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100901 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900902 case "lib32":
903 dirInApex = "lib"
904 case "lib64":
905 dirInApex = "lib64"
906 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100907 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700908 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100909 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900910 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100911 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
912 // Special case for Bionic libs and other libs installed with them. This is
913 // to prevent those libs from being included in the search path
914 // /apex/com.android.runtime/${LIB}. This exclusion is required because
915 // those libs in the Runtime APEX are available via the legacy paths in
916 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
917 // to the legacy paths and thus will be loaded into the default linker
918 // namespace (aka "platform" namespace). If the libs are directly in
919 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
920 // into the runtime linker namespace, which will result in double loading of
921 // them, which isn't supported.
922 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900923 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900924
Martin Stjernholm279de572019-09-10 23:18:20 +0100925 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900926 return
927}
928
929func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900930 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700931 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200932 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900933 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900934 fileToCopy = cc.OutputFile().Path()
935 return
936}
937
Alex Light778127a2019-02-27 14:19:50 -0800938func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
939 dirInApex = "bin"
940 fileToCopy = py.HostToolPath().Path()
941 return
942}
943func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
944 dirInApex = "bin"
945 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
946 if err != nil {
947 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
948 return
949 }
950 fileToCopy = android.PathForOutput(ctx, s)
951 return
952}
953
Jiyong Park04480cf2019-02-06 00:16:29 +0900954func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
955 dirInApex = filepath.Join("bin", sh.SubDir())
956 fileToCopy = sh.OutputFile()
957 return
958}
959
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900960func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
961 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900962 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900963 return
964}
965
Jiyong Park9e6c2422019-08-09 20:39:45 +0900966func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
967 dirInApex = "javalib"
968 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
969 implJars := java.ImplementationJars()
970 if len(implJars) != 1 {
971 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
972 strings.Join(implJars.Strings(), ", ")))
973 }
974 fileToCopy = implJars[0]
975 return
976}
977
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900978func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
979 dirInApex = filepath.Join("etc", prebuilt.SubDir())
980 fileToCopy = prebuilt.OutputFile()
981 return
982}
983
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900984func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +0900985 appDir := "app"
986 if app.Privileged() {
987 appDir = "priv-app"
988 }
989 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900990 fileToCopy = app.OutputFile()
991 return
992}
993
Roland Levillain935639d2019-08-13 14:55:28 +0100994// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
995type flattenedApexContext struct {
996 android.ModuleContext
997}
998
999func (c *flattenedApexContext) InstallBypassMake() bool {
1000 return true
1001}
1002
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001003func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001004 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001005
Alex Light5098a612018-11-29 17:12:15 -08001006 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
1007 a.apexTypes = imageApex
1008 } else if *a.properties.Payload_type == "zip" {
1009 a.apexTypes = zipApex
1010 } else if *a.properties.Payload_type == "both" {
1011 a.apexTypes = both
1012 } else {
1013 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
1014 return
1015 }
1016
Roland Levillain630846d2019-06-26 12:48:34 +01001017 if len(a.properties.Tests) > 0 && !a.testApex {
1018 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1019 return
1020 }
1021
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001022 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1023
Jooyung Hane1633032019-08-01 17:41:43 +09001024 // native lib dependencies
1025 var provideNativeLibs []string
1026 var requireNativeLibs []string
1027
Jooyung Han5c998b92019-06-27 11:30:33 +09001028 // Check if "uses" requirements are met with dependent apexBundles
1029 var providedNativeSharedLibs []string
1030 useVendor := proptools.Bool(a.properties.Use_vendor)
1031 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1032 if ctx.OtherModuleDependencyTag(m) != usesTag {
1033 return
1034 }
1035 otherName := ctx.OtherModuleName(m)
1036 other, ok := m.(*apexBundle)
1037 if !ok {
1038 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1039 return
1040 }
1041 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1042 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1043 return
1044 }
1045 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1046 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1047 return
1048 }
1049 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1050 })
1051
Alex Light778127a2019-02-27 14:19:50 -08001052 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001053 depTag := ctx.OtherModuleDependencyTag(child)
1054 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001055 if _, ok := parent.(*apexBundle); ok {
1056 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001057 switch depTag {
1058 case sharedLibTag:
1059 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001060 if cc.HasStubsVariants() {
1061 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1062 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001063 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001064 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001065 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001066 } else {
1067 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001068 }
1069 case executableTag:
1070 if cc, ok := child.(*cc.Module); ok {
1071 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001072 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001073 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001074 } else if sh, ok := child.(*android.ShBinary); ok {
1075 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001076 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001077 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1078 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1079 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1080 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1081 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1082 // NB: Since go binaries are static we don't need the module for anything here, which is
1083 // good since the go tool is a blueprint.Module not an android.Module like we would
1084 // normally use.
1085 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001086 } else {
Alex Light778127a2019-02-27 14:19:50 -08001087 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 +09001088 }
1089 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001090 if javaLib, ok := child.(*java.Library); ok {
1091 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001092 if fileToCopy == nil {
1093 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1094 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001095 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1096 }
1097 return true
1098 } else if javaLib, ok := child.(*java.Import); ok {
1099 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1100 if fileToCopy == nil {
1101 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1102 } else {
1103 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001104 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001105 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001106 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001107 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001108 }
1109 case prebuiltTag:
1110 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1111 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001112 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001113 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001114 } else {
1115 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1116 }
Roland Levillain630846d2019-06-26 12:48:34 +01001117 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001118 if ccTest, ok := child.(*cc.Module); ok {
1119 if ccTest.IsTestPerSrcAllTestsVariation() {
1120 // Multiple-output test module (where `test_per_src: true`).
1121 //
1122 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1123 // We do not add this variation to `filesInfo`, as it has no output;
1124 // however, we do add the other variations of this module as indirect
1125 // dependencies (see below).
1126 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001127 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001128 // Single-output test module (where `test_per_src: false`).
1129 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1130 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001131 }
Roland Levillain630846d2019-06-26 12:48:34 +01001132 return true
1133 } else {
1134 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1135 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001136 case keyTag:
1137 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001138 a.private_key_file = key.private_key_file
1139 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001140 return false
1141 } else {
1142 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001143 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001144 case certificateTag:
1145 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001146 a.container_certificate_file = dep.Certificate.Pem
1147 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001148 return false
1149 } else {
1150 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1151 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001152 case android.PrebuiltDepTag:
1153 // If the prebuilt is force disabled, remember to delete the prebuilt file
1154 // that might have been installed in the previous builds
1155 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1156 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1157 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001158 case androidAppTag:
1159 if ap, ok := child.(*java.AndroidApp); ok {
1160 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1161 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1162 return true
1163 } else {
1164 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1165 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001166 }
1167 } else {
1168 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001169 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001170 // We cannot use a switch statement on `depTag` here as the checked
1171 // tags used below are private (e.g. `cc.sharedDepTag`).
1172 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1173 if cc, ok := child.(*cc.Module); ok {
1174 if android.InList(cc.Name(), providedNativeSharedLibs) {
1175 // If we're using a shared library which is provided from other APEX,
1176 // don't include it in this APEX
1177 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001178 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001179 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1180 // If the dependency is a stubs lib, don't include it in this APEX,
1181 // but make sure that the lib is installed on the device.
1182 // In case no APEX is having the lib, the lib is installed to the system
1183 // partition.
1184 //
1185 // Always include if we are a host-apex however since those won't have any
1186 // system libraries.
1187 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1188 a.externalDeps = append(a.externalDeps, cc.Name())
1189 }
Jooyung Hane1633032019-08-01 17:41:43 +09001190 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001191 // Don't track further
1192 return false
1193 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001194 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001195 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1196 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001197 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001198 } else if cc.IsTestPerSrcDepTag(depTag) {
1199 if cc, ok := child.(*cc.Module); ok {
1200 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1201 // Handle modules created as `test_per_src` variations of a single test module:
1202 // use the name of the generated test binary (`fileToCopy`) instead of the name
1203 // of the original test module (`depName`, shared by all `test_per_src`
1204 // variations of that module).
1205 moduleName := filepath.Base(fileToCopy.String())
1206 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1207 return true
1208 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001209 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001210 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001211 }
1212 }
1213 }
1214 return false
1215 })
1216
Ulya Trafimovichd5df9492019-10-24 17:29:50 +01001217 // Specific to the ART apex: dexpreopt artifacts for libcore Java libraries.
1218 // Build rules are generated by the dexpreopt singleton, and here we access build artifacts
1219 // via the global boot image config.
1220 if a.artApex {
1221 for arch, files := range java.DexpreoptedArtApexJars(ctx) {
1222 dirInApex := filepath.Join("dexpreopt", arch.String())
1223 for _, f := range files {
1224 localModule := "dexpreopt_" + arch.String() + "_" + filepath.Base(f.String())
1225 filesInfo = append(filesInfo, apexFile{f, localModule, dirInApex, etc, nil, nil})
1226 }
1227 }
1228 }
1229
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001230 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001231 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1232 return
1233 }
1234
Jiyong Park8fd61922018-11-08 02:50:25 +09001235 // remove duplicates in filesInfo
1236 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001237 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001238 result := []apexFile{}
1239 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001240 dest := filepath.Join(f.installDir, f.builtFile.Base())
1241 if !encountered[dest] {
1242 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001243 result = append(result, f)
1244 }
1245 }
1246 return result
1247 }
1248 filesInfo = removeDup(filesInfo)
1249
1250 // to have consistent build rules
1251 sort.Slice(filesInfo, func(i, j int) bool {
1252 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1253 })
1254
Jiyong Park127b40b2019-09-30 16:04:35 +09001255 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001256 if !ctx.Host() {
1257 for _, fi := range filesInfo {
1258 if am, ok := fi.module.(android.ApexModule); ok {
1259 if !am.AvailableFor(ctx.ModuleName()) {
1260 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1261 return
1262 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001263 }
1264 }
1265 }
1266
Jiyong Park8fd61922018-11-08 02:50:25 +09001267 // prepend the name of this APEX to the module names. These names will be the names of
1268 // modules that will be defined if the APEX is flattened.
1269 for i := range filesInfo {
Jooyung Han31c470b2019-10-18 16:26:59 +09001270 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName()
Jiyong Park8fd61922018-11-08 02:50:25 +09001271 }
1272
Jiyong Park8fd61922018-11-08 02:50:25 +09001273 a.installDir = android.PathForModuleInstall(ctx, "apex")
1274 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001275
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001276 // prepare apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001277 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
Jooyung Hane1633032019-08-01 17:41:43 +09001278 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001279
1280 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001281 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1282 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001283
1284 // apex name can be overridden
1285 optCommands := []string{}
1286 if a.properties.Apex_name != nil {
1287 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1288 }
1289
Jooyung Hane1633032019-08-01 17:41:43 +09001290 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001291 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001292 Input: manifestSrc,
1293 Output: a.manifestOut,
1294 Args: map[string]string{
1295 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1296 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001297 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001298 },
1299 })
1300
Roland Levillain935639d2019-08-13 14:55:28 +01001301 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1302 // reply true to `InstallBypassMake()` (thus making the call
1303 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1304 // instead of `android.PathForOutput`) to return the correct path to the flattened
1305 // APEX (as its contents is installed by Make, not Soong).
1306 factx := flattenedApexContext{ctx}
Jooyung Han7a78a922019-10-08 21:59:58 +09001307 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1308 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", apexName)
Roland Levillain935639d2019-08-13 14:55:28 +01001309
Alex Light5098a612018-11-29 17:12:15 -08001310 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001311 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001312 }
1313 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001314 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001315 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001316 // in other modules. It is in AndroidMk where the selection of flattened
1317 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001318 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001319 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001320 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001321
1322 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001323}
1324
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001325func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001326 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001327 for _, f := range a.filesInfo {
1328 if f.module != nil {
1329 notice := f.module.NoticeFile()
1330 if notice.Valid() {
1331 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001332 }
1333 }
1334 }
1335 // append the notice file specified in the apex module itself
1336 if a.NoticeFile().Valid() {
1337 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001338 }
1339
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001340 if len(noticeFiles) == 0 {
1341 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001342 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001343
Jaewoong Jung98772792019-07-01 17:15:13 -07001344 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001345}
1346
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001347func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001348 cert := String(a.properties.Certificate)
1349 if cert != "" && android.SrcIsModule(cert) == "" {
1350 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001351 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1352 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001353 } else if cert == "" {
1354 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001355 a.container_certificate_file = pem
1356 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001357 }
1358
Alex Light5098a612018-11-29 17:12:15 -08001359 var abis []string
1360 for _, target := range ctx.MultiTargets() {
1361 if len(target.Arch.Abi) > 0 {
1362 abis = append(abis, target.Arch.Abi[0])
1363 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001364 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001365
Alex Light5098a612018-11-29 17:12:15 -08001366 abis = android.FirstUniqueStrings(abis)
1367
1368 suffix := apexType.suffix()
1369 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001370
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001371 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001372 for _, f := range a.filesInfo {
1373 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001374 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001375
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001376 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001377 emitCommands := []string{}
1378 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1379 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001380 for i, src := range filesToCopy {
1381 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001382 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001383 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001384 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1385 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001386 for _, sym := range a.filesInfo[i].symlinks {
1387 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1388 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1389 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001390 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001391 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001392 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001393
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001394 if a.properties.Whitelisted_files != nil {
1395 ctx.Build(pctx, android.BuildParams{
1396 Rule: emitApexContentRule,
1397 Implicits: implicitInputs,
1398 Output: imageContentFile,
1399 Description: "emit apex image content",
1400 Args: map[string]string{
1401 "emit_commands": strings.Join(emitCommands, " && "),
1402 },
1403 })
1404 implicitInputs = append(implicitInputs, imageContentFile)
1405 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1406
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001407 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001408 ctx.Build(pctx, android.BuildParams{
1409 Rule: diffApexContentRule,
1410 Implicits: implicitInputs,
1411 Output: phonyOutput,
1412 Description: "diff apex image content",
1413 Args: map[string]string{
1414 "whitelisted_files_file": whitelistedFilesFile.String(),
1415 "image_content_file": imageContentFile.String(),
1416 "apex_module_name": ctx.ModuleName(),
1417 },
1418 })
1419
1420 implicitInputs = append(implicitInputs, phonyOutput)
1421 }
1422
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001423 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1424 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001425
Alex Light5098a612018-11-29 17:12:15 -08001426 if apexType.image() {
1427 // files and dirs that will be created in APEX
1428 var readOnlyPaths []string
1429 var executablePaths []string // this also includes dirs
1430 for _, f := range a.filesInfo {
1431 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001432 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001433 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001434 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001435 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001436 }
Alex Light5098a612018-11-29 17:12:15 -08001437 } else {
1438 readOnlyPaths = append(readOnlyPaths, pathInApex)
1439 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001440 dir := f.installDir
1441 for !android.InList(dir, executablePaths) && dir != "" {
1442 executablePaths = append(executablePaths, dir)
1443 dir, _ = filepath.Split(dir) // move up to the parent
1444 if len(dir) > 0 {
1445 // remove trailing slash
1446 dir = dir[:len(dir)-1]
1447 }
Alex Light5098a612018-11-29 17:12:15 -08001448 }
1449 }
1450 sort.Strings(readOnlyPaths)
1451 sort.Strings(executablePaths)
1452 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1453 ctx.Build(pctx, android.BuildParams{
1454 Rule: generateFsConfig,
1455 Output: cannedFsConfig,
1456 Description: "generate fs config",
1457 Args: map[string]string{
1458 "ro_paths": strings.Join(readOnlyPaths, " "),
1459 "exec_paths": strings.Join(executablePaths, " "),
1460 },
1461 })
1462
1463 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1464 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1465 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1466 if !fileContextsOptionalPath.Valid() {
1467 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1468 return
1469 }
1470 fileContexts := fileContextsOptionalPath.Path()
1471
Jiyong Park835d82b2018-12-27 16:04:18 +09001472 optFlags := []string{}
1473
Alex Light5098a612018-11-29 17:12:15 -08001474 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001475 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1476 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001477
Jiyong Park7f67f482019-01-05 12:57:48 +09001478 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1479 if overridden {
1480 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1481 }
1482
Jiyong Park40e26a22019-02-08 02:53:06 +09001483 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001484 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001485 implicitInputs = append(implicitInputs, androidManifestFile)
1486 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1487 }
1488
Jiyong Park71b519d2019-04-18 17:25:49 +09001489 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1490 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1491 ctx.Config().UnbundledBuild() &&
1492 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1493 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1494 apiFingerprint := java.ApiFingerprintPath(ctx)
1495 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1496 implicitInputs = append(implicitInputs, apiFingerprint)
1497 }
1498 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1499
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001500 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1501 if noticeFile.Valid() {
1502 // If there's a NOTICE file, embed it as an asset file in the APEX.
1503 implicitInputs = append(implicitInputs, noticeFile.Path())
1504 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1505 }
1506
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001507 if !ctx.Config().UnbundledBuild() && a.installable() {
1508 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1509 // don't need hashtree for activation. Therefore, by removing hashtree from
1510 // apex bundle (filesystem image in it, to be specific), we can save storage.
1511 optFlags = append(optFlags, "--no_hashtree")
1512 }
1513
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001514 if a.properties.Apex_name != nil {
1515 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1516 // Note that apex_manifest is also mended.
1517 optFlags = append(optFlags, "--do_not_check_keyname")
1518 }
1519
Alex Light5098a612018-11-29 17:12:15 -08001520 ctx.Build(pctx, android.BuildParams{
1521 Rule: apexRule,
1522 Implicits: implicitInputs,
1523 Output: unsignedOutputFile,
1524 Description: "apex (" + apexType.name() + ")",
1525 Args: map[string]string{
1526 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1527 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1528 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001529 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001530 "file_contexts": fileContexts.String(),
1531 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001532 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001533 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001534 },
1535 })
1536
1537 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1538 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1539 a.bundleModuleFile = bundleModuleFile
1540
1541 ctx.Build(pctx, android.BuildParams{
1542 Rule: apexProtoConvertRule,
1543 Input: unsignedOutputFile,
1544 Output: apexProtoFile,
1545 Description: "apex proto convert",
1546 })
1547
1548 ctx.Build(pctx, android.BuildParams{
1549 Rule: apexBundleRule,
1550 Input: apexProtoFile,
1551 Output: a.bundleModuleFile,
1552 Description: "apex bundle module",
1553 Args: map[string]string{
1554 "abi": strings.Join(abis, "."),
1555 },
1556 })
1557 } else {
1558 ctx.Build(pctx, android.BuildParams{
1559 Rule: zipApexRule,
1560 Implicits: implicitInputs,
1561 Output: unsignedOutputFile,
1562 Description: "apex (" + apexType.name() + ")",
1563 Args: map[string]string{
1564 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1565 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1566 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001567 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001568 },
1569 })
Colin Crossa4925902018-11-16 11:36:28 -08001570 }
Colin Crossa4925902018-11-16 11:36:28 -08001571
Alex Light5098a612018-11-29 17:12:15 -08001572 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001573 ctx.Build(pctx, android.BuildParams{
1574 Rule: java.Signapk,
1575 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001576 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001577 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001578 Implicits: []android.Path{
1579 a.container_certificate_file,
1580 a.container_private_key_file,
1581 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001582 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001583 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001584 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001585 },
1586 })
Alex Light5098a612018-11-29 17:12:15 -08001587
1588 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahne8fb7242019-09-17 13:50:45 +09001589 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && !a.isFlattenedVariant() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001590 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001591 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001592}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001593
Jiyong Park8fd61922018-11-08 02:50:25 +09001594func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001595 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001596 // 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 +09001597 // with other ordinary files.
Jooyung Han31c470b2019-10-18 16:26:59 +09001598 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, "apex_manifest.json." + ctx.ModuleName(), ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001599
Jiyong Park42cca6c2019-04-01 11:15:50 +09001600 // rename to apex_pubkey
1601 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1602 ctx.Build(pctx, android.BuildParams{
1603 Rule: android.Cp,
1604 Input: a.public_key_file,
1605 Output: copiedPubkey,
1606 })
Jooyung Han31c470b2019-10-18 16:26:59 +09001607 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName(), ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001608
Jiyong Park23c52b02019-02-02 13:13:47 +09001609 if ctx.Config().FlattenApex() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001610 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001611 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001612 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001613 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1614 for _, sym := range fi.symlinks {
1615 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1616 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001617 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001618 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001619 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001620}
1621
1622func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001623 if a.properties.HideFromMake {
1624 return android.AndroidMkData{
1625 Disabled: true,
1626 }
1627 }
Alex Light5098a612018-11-29 17:12:15 -08001628 writers := []android.AndroidMkData{}
1629 if a.apexTypes.image() {
1630 writers = append(writers, a.androidMkForType(imageApex))
1631 }
1632 if a.apexTypes.zip() {
1633 writers = append(writers, a.androidMkForType(zipApex))
1634 }
1635 return android.AndroidMkData{
1636 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1637 for _, data := range writers {
1638 data.Custom(w, name, prefix, moduleDir, data)
1639 }
1640 }}
1641}
1642
Jooyung Han7a78a922019-10-08 21:59:58 +09001643func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001644 moduleNames := []string{}
1645
1646 for _, fi := range a.filesInfo {
1647 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1648 continue
1649 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001650 if a.properties.Flattened && !apexType.image() {
1651 continue
Jiyong Park94427262019-02-05 23:18:47 +09001652 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001653
1654 var suffix string
Sundong Ahne8fb7242019-09-17 13:50:45 +09001655 if a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001656 suffix = ".flattened"
1657 }
1658
1659 if !android.InList(fi.moduleName, moduleNames) {
1660 moduleNames = append(moduleNames, fi.moduleName+suffix)
1661 }
1662
Jiyong Park94427262019-02-05 23:18:47 +09001663 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1664 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001665 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName+suffix)
Roland Levillain411c5842019-09-19 16:37:20 +01001666 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001667 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001668 if a.properties.Flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001669 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001670 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001671 apexName, fi.installDir))
Sundong Ahne8fb7242019-09-17 13:50:45 +09001672 if !a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001673 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1674 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001675 if len(fi.symlinks) > 0 {
1676 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1677 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001678
1679 if fi.module != nil && fi.module.NoticeFile().Valid() {
1680 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1681 }
Jiyong Park94427262019-02-05 23:18:47 +09001682 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001683 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001684 }
1685 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1686 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1687 if fi.module != nil {
1688 archStr := fi.module.Target().Arch.ArchType.String()
1689 host := false
1690 switch fi.module.Target().Os.Class {
1691 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001692 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001693 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1694 }
1695 host = true
1696 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001697 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001698 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1699 }
1700 host = true
1701 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001702 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001703 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1704 }
1705 }
1706 if host {
1707 makeOs := fi.module.Target().Os.String()
1708 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1709 makeOs = "linux"
1710 }
1711 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1712 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1713 }
1714 }
1715 if fi.class == javaSharedLib {
1716 javaModule := fi.module.(*java.Library)
1717 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1718 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1719 // we will have foo.jar.jar
1720 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1721 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1722 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1723 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1724 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1725 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001726 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001727 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001728 if cc, ok := fi.module.(*cc.Module); ok {
1729 if cc.UnstrippedOutputFile() != nil {
1730 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1731 }
1732 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001733 if cc.CoverageOutputFile().Valid() {
1734 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1735 }
Jiyong Park94427262019-02-05 23:18:47 +09001736 }
1737 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1738 } else {
1739 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001740 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
1741 if !a.isFlattenedVariant() && fi.builtFile.Base() == "apex_manifest.json" && len(a.compatSymlinks) > 0 {
1742 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1743 }
Jiyong Park94427262019-02-05 23:18:47 +09001744 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1745 }
1746 }
1747 return moduleNames
1748}
1749
Alex Light5098a612018-11-29 17:12:15 -08001750func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001751 return android.AndroidMkData{
1752 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1753 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001754 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001755 apexName := proptools.StringDefault(a.properties.Apex_name, name)
1756 moduleNames = a.androidMkForFiles(w, apexName, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001757 }
1758
Sundong Ahne8fb7242019-09-17 13:50:45 +09001759 if a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001760 name = name + ".flattened"
1761 }
1762
1763 if a.properties.Flattened && apexType.image() {
Jiyong Park719b4462019-01-13 00:39:51 +09001764 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001765 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1766 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1767 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001768 if len(moduleNames) > 0 {
1769 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1770 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001771 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001772 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1773
Sundong Ahne8fb7242019-09-17 13:50:45 +09001774 } else if !a.isFlattenedVariant() {
Alex Light5098a612018-11-29 17:12:15 -08001775 // zip-apex is the less common type so have the name refer to the image-apex
1776 // only and use {name}.zip if you want the zip-apex
1777 if apexType == zipApex && a.apexTypes == both {
1778 name = name + ".zip"
1779 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001780 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1781 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1782 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1783 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001784 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001785 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001786 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001787 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001788 if len(moduleNames) > 0 {
1789 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1790 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001791 if len(a.externalDeps) > 0 {
1792 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1793 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001794 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001795 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001796 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001797 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001798 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001799 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1800 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1801 if len(postInstallCommands) > 0 {
1802 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1803 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001804 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001805
Alex Light5098a612018-11-29 17:12:15 -08001806 if apexType == imageApex {
1807 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1808 }
Jiyong Park719b4462019-01-13 00:39:51 +09001809 }
1810 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001811}
1812
Jooyung Han344d5432019-08-23 11:17:39 +09001813func newApexBundle() *apexBundle {
Alex Light5098a612018-11-29 17:12:15 -08001814 module := &apexBundle{
1815 outputFiles: map[apexPackaging]android.WritablePath{},
1816 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001817 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001818 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001819 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001820 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1821 })
Alex Light5098a612018-11-29 17:12:15 -08001822 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001823 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001824 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001825 return module
1826}
Jiyong Park30ca9372019-02-07 16:27:23 +09001827
Ulya Trafimovichd5df9492019-10-24 17:29:50 +01001828func ApexBundleFactory(testApex bool, artApex bool) android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001829 bundle := newApexBundle()
1830 bundle.testApex = testApex
Ulya Trafimovichd5df9492019-10-24 17:29:50 +01001831 bundle.artApex = artApex
Jooyung Han344d5432019-08-23 11:17:39 +09001832 return bundle
1833}
1834
1835func testApexBundleFactory() android.Module {
1836 bundle := newApexBundle()
1837 bundle.testApex = true
1838 return bundle
1839}
1840
Jiyong Parkd1063c12019-07-17 20:08:41 +09001841func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001842 return newApexBundle()
1843}
1844
1845// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1846// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1847// If not specified, then the "current" versions are gathered.
1848func vndkApexBundleFactory() android.Module {
1849 bundle := newApexBundle()
1850 bundle.vndkApex = true
1851 bundle.AddProperties(&bundle.vndkProperties)
1852 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1853 ctx.AppendProperties(&struct {
1854 Compile_multilib *string
1855 }{
1856 proptools.StringPtr("both"),
1857 })
1858 })
1859 return bundle
1860}
1861
Jooyung Han31c470b2019-10-18 16:26:59 +09001862func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1863 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1864 if vndkVersion == "current" {
1865 vndkVersion = config.PlatformVndkVersion()
1866 }
1867 return vndkVersion
1868}
1869
Jiyong Park30ca9372019-02-07 16:27:23 +09001870//
1871// Defaults
1872//
1873type Defaults struct {
1874 android.ModuleBase
1875 android.DefaultsModuleBase
1876}
1877
Jiyong Park30ca9372019-02-07 16:27:23 +09001878func defaultsFactory() android.Module {
1879 return DefaultsFactory()
1880}
1881
1882func DefaultsFactory(props ...interface{}) android.Module {
1883 module := &Defaults{}
1884
1885 module.AddProperties(props...)
1886 module.AddProperties(
1887 &apexBundleProperties{},
1888 &apexTargetBundleProperties{},
1889 )
1890
1891 android.InitDefaultsModule(module)
1892 return module
1893}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001894
1895//
1896// Prebuilt APEX
1897//
1898type Prebuilt struct {
1899 android.ModuleBase
1900 prebuilt android.Prebuilt
1901
1902 properties PrebuiltProperties
1903
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001904 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001905 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001906 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001907 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001908}
1909
1910type PrebuiltProperties struct {
1911 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001912 Source string `blueprint:"mutated"`
1913 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001914
1915 Src *string
1916 Arch struct {
1917 Arm struct {
1918 Src *string
1919 }
1920 Arm64 struct {
1921 Src *string
1922 }
1923 X86 struct {
1924 Src *string
1925 }
1926 X86_64 struct {
1927 Src *string
1928 }
1929 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001930
1931 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001932 // Optional name for the installed apex. If unspecified, name of the
1933 // module is used as the file name
1934 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001935
1936 // Names of modules to be overridden. Listed modules can only be other binaries
1937 // (in Make or Soong).
1938 // This does not completely prevent installation of the overridden binaries, but if both
1939 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1940 // from PRODUCT_PACKAGES.
1941 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001942}
1943
1944func (p *Prebuilt) installable() bool {
1945 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001946}
1947
1948func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001949 // If the device is configured to use flattened APEX, force disable the prebuilt because
1950 // the prebuilt is a non-flattened one.
1951 forceDisable := ctx.Config().FlattenApex()
1952
1953 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1954 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001955 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001956
Kun Niu10c9f832019-07-29 16:28:57 -07001957 // Force disable the prebuilts when coverage is enabled.
1958 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1959 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1960
Jiyong Park50b81e52019-07-11 11:24:41 +09001961 // b/137216042 don't use prebuilts when address sanitizer is on
1962 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1963 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1964
1965 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001966 p.properties.ForceDisable = true
1967 return
1968 }
1969
Jiyong Parkc95714e2019-03-29 14:23:10 +09001970 // This is called before prebuilt_select and prebuilt_postdeps mutators
1971 // The mutators requires that src to be set correctly for each arch so that
1972 // arch variants are disabled when src is not provided for the arch.
1973 if len(ctx.MultiTargets()) != 1 {
1974 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1975 return
1976 }
1977 var src string
1978 switch ctx.MultiTargets()[0].Arch.ArchType {
1979 case android.Arm:
1980 src = String(p.properties.Arch.Arm.Src)
1981 case android.Arm64:
1982 src = String(p.properties.Arch.Arm64.Src)
1983 case android.X86:
1984 src = String(p.properties.Arch.X86.Src)
1985 case android.X86_64:
1986 src = String(p.properties.Arch.X86_64.Src)
1987 default:
1988 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1989 return
1990 }
1991 if src == "" {
1992 src = String(p.properties.Src)
1993 }
1994 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001995}
1996
Jiyong Park03b68dd2019-07-26 23:20:40 +09001997func (p *Prebuilt) isForceDisabled() bool {
1998 return p.properties.ForceDisable
1999}
2000
Colin Cross41955e82019-05-29 14:40:35 -07002001func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2002 switch tag {
2003 case "":
2004 return android.Paths{p.outputApex}, nil
2005 default:
2006 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2007 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002008}
2009
Jiyong Park4d277042019-04-23 18:00:10 +09002010func (p *Prebuilt) InstallFilename() string {
2011 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2012}
2013
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002014func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002015 if p.properties.ForceDisable {
2016 return
2017 }
2018
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002019 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002020 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002021 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002022 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002023 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2024 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2025 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002026 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2027 ctx.Build(pctx, android.BuildParams{
2028 Rule: android.Cp,
2029 Input: p.inputApex,
2030 Output: p.outputApex,
2031 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002032 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002033 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002034 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002035
2036 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002037}
2038
2039func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2040 return &p.prebuilt
2041}
2042
2043func (p *Prebuilt) Name() string {
2044 return p.prebuilt.Name(p.ModuleBase.Name())
2045}
2046
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002047func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2048 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002049 Class: "ETC",
2050 OutputFile: android.OptionalPathForPath(p.inputApex),
2051 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002052 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2053 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002054 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002055 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2056 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2057 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2058 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002059 },
2060 }
2061}
2062
2063// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2064func PrebuiltFactory() android.Module {
2065 module := &Prebuilt{}
2066 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002067 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002068 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002069 return module
2070}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002071
2072func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2073 // small helper to add symlink commands
2074 addSymlink := func(target, dir, linkName string) {
2075 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2076 link := filepath.Join(outDir, linkName)
2077 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2078 }
2079
2080 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2081 // When all hard-coded references are fixed, remove symbolic links
2082 // Note that we should keep following symlinks for older VNDKs (<=29)
2083 // Since prebuilt vndk libs still depend on system/lib/vndk path
2084 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2085 // the name of vndk apex is formatted "com.android.vndk.v" + version
2086 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2087 if ctx.Config().Android64() {
2088 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2089 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2090 }
2091 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2092 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2093 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2094 }
2095 }
2096 return
2097}