blob: 629cbbf2e99118bf8f5d8f68a83bd7b40afb663a [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
Jooyung Hane1633032019-08-01 17:41:43 +0900616
617 // intermediate path for apex_manifest.json
618 manifestOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900619
620 // list of commands to create symlinks for backward compatibility
621 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
622 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
623 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
624 compatSymlinks []string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900625}
626
Jiyong Park397e55e2018-10-24 21:09:55 +0900627func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100628 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700629 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900630 // Use *FarVariation* to be able to depend on modules having
631 // conflicting variations with this module. This is required since
632 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
633 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700634 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900635 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900636 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900637 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700638 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900639
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700640 ctx.AddFarVariationDependencies(append(target.Variations(),
641 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
642 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100643
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700644 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100645 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100646 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700647 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900648}
649
Alex Light9670d332019-01-29 18:07:33 -0800650func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
651 if ctx.Os().Class == android.Device {
652 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
653 } else {
654 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
655 if ctx.Os().Bionic() {
656 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
657 } else {
658 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
659 }
660 }
661}
662
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900663func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900664 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900665 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800666
667 a.combineProperties(ctx)
668
Jiyong Park397e55e2018-10-24 21:09:55 +0900669 has32BitTarget := false
670 for _, target := range targets {
671 if target.Arch.ArchType.Multilib == "lib32" {
672 has32BitTarget = true
673 }
674 }
675 for i, target := range targets {
676 // When multilib.* is omitted for native_shared_libs, it implies
677 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700678 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900679 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900680 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700681 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900682
Roland Levillain630846d2019-06-26 12:48:34 +0100683 // When multilib.* is omitted for tests, it implies
684 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700685 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100686 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100687 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700688 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100689
Jiyong Park397e55e2018-10-24 21:09:55 +0900690 // Add native modules targetting both ABIs
691 addDependenciesForNativeModules(ctx,
692 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100693 a.properties.Multilib.Both.Binaries,
694 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700695 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900696 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900697
Alex Light3d673592019-01-18 14:37:31 -0800698 isPrimaryAbi := i == 0
699 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900700 // When multilib.* is omitted for binaries, it implies
701 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700702 ctx.AddFarVariationDependencies(append(target.Variations(),
703 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
704 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900705
706 // Add native modules targetting the first ABI
707 addDependenciesForNativeModules(ctx,
708 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100709 a.properties.Multilib.First.Binaries,
710 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700711 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900712 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800713
714 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700715 ctx.AddFarVariationDependencies(target.Variations(),
716 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900717 }
718
719 switch target.Arch.ArchType.Multilib {
720 case "lib32":
721 // Add native modules targetting 32-bit ABI
722 addDependenciesForNativeModules(ctx,
723 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100724 a.properties.Multilib.Lib32.Binaries,
725 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700726 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900727 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900728
729 addDependenciesForNativeModules(ctx,
730 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100731 a.properties.Multilib.Prefer32.Binaries,
732 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700733 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900734 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900735 case "lib64":
736 // Add native modules targetting 64-bit ABI
737 addDependenciesForNativeModules(ctx,
738 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100739 a.properties.Multilib.Lib64.Binaries,
740 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700741 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900742 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900743
744 if !has32BitTarget {
745 addDependenciesForNativeModules(ctx,
746 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100747 a.properties.Multilib.Prefer32.Binaries,
748 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700749 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900750 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900751 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700752
753 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
754 for _, sanitizer := range ctx.Config().SanitizeDevice() {
755 if sanitizer == "hwaddress" {
756 addDependenciesForNativeModules(ctx,
757 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700758 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700759 break
760 }
761 }
762 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900763 }
764
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900765 }
766
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700767 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
768 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900769
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700770 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
771 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900772
Jiyong Park23c52b02019-02-02 13:13:47 +0900773 if String(a.properties.Key) == "" {
774 ctx.ModuleErrorf("key is missing")
775 return
776 }
777 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900778
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900779 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900780 if cert != "" {
781 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900782 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900783
784 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
785 if len(a.properties.Uses_sdks) > 0 {
786 sdkRefs := []android.SdkRef{}
787 for _, str := range a.properties.Uses_sdks {
788 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
789 sdkRefs = append(sdkRefs, parsed)
790 }
791 a.BuildWithSdks(sdkRefs)
792 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900793}
794
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900795func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
796 // direct deps of an APEX bundle are all part of the APEX bundle
797 return true
798}
799
Colin Cross0ea8ba82019-06-06 14:33:29 -0700800func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900801 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
802 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000803 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900804 }
805 return String(a.properties.Certificate)
806}
807
Colin Cross41955e82019-05-29 14:40:35 -0700808func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
809 switch tag {
810 case "":
811 if file, ok := a.outputFiles[imageApex]; ok {
812 return android.Paths{file}, nil
813 } else {
814 return nil, nil
815 }
Roland Levillain935639d2019-08-13 14:55:28 +0100816 case ".flattened":
Sundong Ahne9b55722019-09-06 17:37:42 +0900817 if a.properties.Flattened {
Roland Levillain935639d2019-08-13 14:55:28 +0100818 flattenedApexPath := a.flattenedOutput
819 return android.Paths{flattenedApexPath}, nil
820 } else {
821 return nil, nil
822 }
Colin Cross41955e82019-05-29 14:40:35 -0700823 default:
824 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900825 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900826}
827
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900828func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900829 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900830}
831
Jiyong Park7c1dc612019-01-05 11:15:24 +0900832func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900833 if a.vndkApex {
834 return "vendor." + a.vndkVersion(config)
835 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900836 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900837 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900838 } else {
839 return "core"
840 }
841}
842
Jiyong Parkf97782b2019-02-13 20:28:58 +0900843func (a *apexBundle) EnableSanitizer(sanitizerName string) {
844 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
845 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
846 }
847}
848
Jiyong Park388ef3f2019-01-28 19:47:32 +0900849func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900850 if android.InList(sanitizerName, a.properties.SanitizerNames) {
851 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900852 }
853
854 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900855 globalSanitizerNames := []string{}
856 if a.Host() {
857 globalSanitizerNames = ctx.Config().SanitizeHost()
858 } else {
859 arches := ctx.Config().SanitizeDeviceArch()
860 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
861 globalSanitizerNames = ctx.Config().SanitizeDevice()
862 }
863 }
864 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900865}
866
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900867func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
868 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
869}
870
871func (a *apexBundle) PreventInstall() {
872 a.properties.PreventInstall = true
873}
874
875func (a *apexBundle) HideFromMake() {
876 a.properties.HideFromMake = true
877}
878
Sundong Ahne9b55722019-09-06 17:37:42 +0900879func (a *apexBundle) SetFlattened(flattened bool) {
880 a.properties.Flattened = flattened
881}
882
Sundong Ahne8fb7242019-09-17 13:50:45 +0900883func (a *apexBundle) SetFlattenedConfigValue() {
884 a.properties.FlattenedConfigValue = true
885}
886
887// isFlattenedVariant returns true when the current module is the flattened
888// variant of an apex that has both a flattened and an unflattened variant.
889// It returns false when the current module is flattened but there is no
890// unflattened variant, which occurs when ctx.Config().FlattenedApex() returns
891// true. It can be used to avoid collisions between the install paths of the
892// flattened and unflattened variants.
893func (a *apexBundle) isFlattenedVariant() bool {
894 return a.properties.Flattened && !a.properties.FlattenedConfigValue
895}
896
Martin Stjernholm279de572019-09-10 23:18:20 +0100897func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900898 // Decide the APEX-local directory by the multilib of the library
899 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100900 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900901 case "lib32":
902 dirInApex = "lib"
903 case "lib64":
904 dirInApex = "lib64"
905 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100906 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700907 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100908 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900909 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100910 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
911 // Special case for Bionic libs and other libs installed with them. This is
912 // to prevent those libs from being included in the search path
913 // /apex/com.android.runtime/${LIB}. This exclusion is required because
914 // those libs in the Runtime APEX are available via the legacy paths in
915 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
916 // to the legacy paths and thus will be loaded into the default linker
917 // namespace (aka "platform" namespace). If the libs are directly in
918 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
919 // into the runtime linker namespace, which will result in double loading of
920 // them, which isn't supported.
921 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900922 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900923
Martin Stjernholm279de572019-09-10 23:18:20 +0100924 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900925 return
926}
927
928func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900929 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700930 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200931 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900932 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900933 fileToCopy = cc.OutputFile().Path()
934 return
935}
936
Alex Light778127a2019-02-27 14:19:50 -0800937func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
938 dirInApex = "bin"
939 fileToCopy = py.HostToolPath().Path()
940 return
941}
942func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
943 dirInApex = "bin"
944 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
945 if err != nil {
946 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
947 return
948 }
949 fileToCopy = android.PathForOutput(ctx, s)
950 return
951}
952
Jiyong Park04480cf2019-02-06 00:16:29 +0900953func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
954 dirInApex = filepath.Join("bin", sh.SubDir())
955 fileToCopy = sh.OutputFile()
956 return
957}
958
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900959func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
960 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900961 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900962 return
963}
964
Jiyong Park9e6c2422019-08-09 20:39:45 +0900965func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
966 dirInApex = "javalib"
967 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
968 implJars := java.ImplementationJars()
969 if len(implJars) != 1 {
970 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
971 strings.Join(implJars.Strings(), ", ")))
972 }
973 fileToCopy = implJars[0]
974 return
975}
976
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900977func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
978 dirInApex = filepath.Join("etc", prebuilt.SubDir())
979 fileToCopy = prebuilt.OutputFile()
980 return
981}
982
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900983func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +0900984 appDir := "app"
985 if app.Privileged() {
986 appDir = "priv-app"
987 }
988 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900989 fileToCopy = app.OutputFile()
990 return
991}
992
Roland Levillain935639d2019-08-13 14:55:28 +0100993// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
994type flattenedApexContext struct {
995 android.ModuleContext
996}
997
998func (c *flattenedApexContext) InstallBypassMake() bool {
999 return true
1000}
1001
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001002func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001003 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001004
Alex Light5098a612018-11-29 17:12:15 -08001005 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
1006 a.apexTypes = imageApex
1007 } else if *a.properties.Payload_type == "zip" {
1008 a.apexTypes = zipApex
1009 } else if *a.properties.Payload_type == "both" {
1010 a.apexTypes = both
1011 } else {
1012 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
1013 return
1014 }
1015
Roland Levillain630846d2019-06-26 12:48:34 +01001016 if len(a.properties.Tests) > 0 && !a.testApex {
1017 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1018 return
1019 }
1020
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001021 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1022
Jooyung Hane1633032019-08-01 17:41:43 +09001023 // native lib dependencies
1024 var provideNativeLibs []string
1025 var requireNativeLibs []string
1026
Jooyung Han5c998b92019-06-27 11:30:33 +09001027 // Check if "uses" requirements are met with dependent apexBundles
1028 var providedNativeSharedLibs []string
1029 useVendor := proptools.Bool(a.properties.Use_vendor)
1030 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1031 if ctx.OtherModuleDependencyTag(m) != usesTag {
1032 return
1033 }
1034 otherName := ctx.OtherModuleName(m)
1035 other, ok := m.(*apexBundle)
1036 if !ok {
1037 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1038 return
1039 }
1040 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1041 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1042 return
1043 }
1044 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1045 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1046 return
1047 }
1048 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1049 })
1050
Alex Light778127a2019-02-27 14:19:50 -08001051 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001052 depTag := ctx.OtherModuleDependencyTag(child)
1053 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001054 if _, ok := parent.(*apexBundle); ok {
1055 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001056 switch depTag {
1057 case sharedLibTag:
1058 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001059 if cc.HasStubsVariants() {
1060 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1061 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001062 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001063 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001064 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001065 } else {
1066 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001067 }
1068 case executableTag:
1069 if cc, ok := child.(*cc.Module); ok {
1070 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001071 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001072 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001073 } else if sh, ok := child.(*android.ShBinary); ok {
1074 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001075 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001076 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1077 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1078 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1079 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1080 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1081 // NB: Since go binaries are static we don't need the module for anything here, which is
1082 // good since the go tool is a blueprint.Module not an android.Module like we would
1083 // normally use.
1084 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001085 } else {
Alex Light778127a2019-02-27 14:19:50 -08001086 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 +09001087 }
1088 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001089 if javaLib, ok := child.(*java.Library); ok {
1090 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001091 if fileToCopy == nil {
1092 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1093 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001094 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1095 }
1096 return true
1097 } else if javaLib, ok := child.(*java.Import); ok {
1098 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1099 if fileToCopy == nil {
1100 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1101 } else {
1102 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001103 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001104 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001105 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001106 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001107 }
1108 case prebuiltTag:
1109 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1110 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001111 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001112 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001113 } else {
1114 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1115 }
Roland Levillain630846d2019-06-26 12:48:34 +01001116 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001117 if ccTest, ok := child.(*cc.Module); ok {
1118 if ccTest.IsTestPerSrcAllTestsVariation() {
1119 // Multiple-output test module (where `test_per_src: true`).
1120 //
1121 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1122 // We do not add this variation to `filesInfo`, as it has no output;
1123 // however, we do add the other variations of this module as indirect
1124 // dependencies (see below).
1125 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001126 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001127 // Single-output test module (where `test_per_src: false`).
1128 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1129 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001130 }
Roland Levillain630846d2019-06-26 12:48:34 +01001131 return true
1132 } else {
1133 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1134 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001135 case keyTag:
1136 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001137 a.private_key_file = key.private_key_file
1138 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001139 return false
1140 } else {
1141 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001142 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001143 case certificateTag:
1144 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001145 a.container_certificate_file = dep.Certificate.Pem
1146 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001147 return false
1148 } else {
1149 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1150 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001151 case android.PrebuiltDepTag:
1152 // If the prebuilt is force disabled, remember to delete the prebuilt file
1153 // that might have been installed in the previous builds
1154 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1155 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1156 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001157 case androidAppTag:
1158 if ap, ok := child.(*java.AndroidApp); ok {
1159 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1160 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1161 return true
1162 } else {
1163 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1164 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001165 }
1166 } else {
1167 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001168 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001169 // We cannot use a switch statement on `depTag` here as the checked
1170 // tags used below are private (e.g. `cc.sharedDepTag`).
1171 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1172 if cc, ok := child.(*cc.Module); ok {
1173 if android.InList(cc.Name(), providedNativeSharedLibs) {
1174 // If we're using a shared library which is provided from other APEX,
1175 // don't include it in this APEX
1176 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001177 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001178 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1179 // If the dependency is a stubs lib, don't include it in this APEX,
1180 // but make sure that the lib is installed on the device.
1181 // In case no APEX is having the lib, the lib is installed to the system
1182 // partition.
1183 //
1184 // Always include if we are a host-apex however since those won't have any
1185 // system libraries.
1186 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1187 a.externalDeps = append(a.externalDeps, cc.Name())
1188 }
Jooyung Hane1633032019-08-01 17:41:43 +09001189 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001190 // Don't track further
1191 return false
1192 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001193 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001194 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1195 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001196 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001197 } else if cc.IsTestPerSrcDepTag(depTag) {
1198 if cc, ok := child.(*cc.Module); ok {
1199 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1200 // Handle modules created as `test_per_src` variations of a single test module:
1201 // use the name of the generated test binary (`fileToCopy`) instead of the name
1202 // of the original test module (`depName`, shared by all `test_per_src`
1203 // variations of that module).
1204 moduleName := filepath.Base(fileToCopy.String())
1205 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1206 return true
1207 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001208 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001209 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001210 }
1211 }
1212 }
1213 return false
1214 })
1215
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001216 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001217 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1218 return
1219 }
1220
Jiyong Park8fd61922018-11-08 02:50:25 +09001221 // remove duplicates in filesInfo
1222 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001223 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001224 result := []apexFile{}
1225 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001226 dest := filepath.Join(f.installDir, f.builtFile.Base())
1227 if !encountered[dest] {
1228 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001229 result = append(result, f)
1230 }
1231 }
1232 return result
1233 }
1234 filesInfo = removeDup(filesInfo)
1235
1236 // to have consistent build rules
1237 sort.Slice(filesInfo, func(i, j int) bool {
1238 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1239 })
1240
Jiyong Park127b40b2019-09-30 16:04:35 +09001241 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001242 if !ctx.Host() {
1243 for _, fi := range filesInfo {
1244 if am, ok := fi.module.(android.ApexModule); ok {
1245 if !am.AvailableFor(ctx.ModuleName()) {
1246 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1247 return
1248 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001249 }
1250 }
1251 }
1252
Jiyong Park8fd61922018-11-08 02:50:25 +09001253 // prepend the name of this APEX to the module names. These names will be the names of
1254 // modules that will be defined if the APEX is flattened.
1255 for i := range filesInfo {
Jooyung Han31c470b2019-10-18 16:26:59 +09001256 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName()
Jiyong Park8fd61922018-11-08 02:50:25 +09001257 }
1258
Jiyong Park8fd61922018-11-08 02:50:25 +09001259 a.installDir = android.PathForModuleInstall(ctx, "apex")
1260 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001261
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001262 // prepare apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001263 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
Jooyung Hane1633032019-08-01 17:41:43 +09001264 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001265
1266 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001267 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1268 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001269
1270 // apex name can be overridden
1271 optCommands := []string{}
1272 if a.properties.Apex_name != nil {
1273 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1274 }
1275
Jooyung Hane1633032019-08-01 17:41:43 +09001276 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001277 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001278 Input: manifestSrc,
1279 Output: a.manifestOut,
1280 Args: map[string]string{
1281 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1282 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001283 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001284 },
1285 })
1286
Roland Levillain935639d2019-08-13 14:55:28 +01001287 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1288 // reply true to `InstallBypassMake()` (thus making the call
1289 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1290 // instead of `android.PathForOutput`) to return the correct path to the flattened
1291 // APEX (as its contents is installed by Make, not Soong).
1292 factx := flattenedApexContext{ctx}
Jooyung Han7a78a922019-10-08 21:59:58 +09001293 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1294 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", apexName)
Roland Levillain935639d2019-08-13 14:55:28 +01001295
Alex Light5098a612018-11-29 17:12:15 -08001296 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001297 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001298 }
1299 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001300 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001301 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001302 // in other modules. It is in AndroidMk where the selection of flattened
1303 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001304 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001305 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001306 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001307
1308 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001309}
1310
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001311func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001312 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001313 for _, f := range a.filesInfo {
1314 if f.module != nil {
1315 notice := f.module.NoticeFile()
1316 if notice.Valid() {
1317 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001318 }
1319 }
1320 }
1321 // append the notice file specified in the apex module itself
1322 if a.NoticeFile().Valid() {
1323 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001324 }
1325
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001326 if len(noticeFiles) == 0 {
1327 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001328 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001329
Jaewoong Jung98772792019-07-01 17:15:13 -07001330 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001331}
1332
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001333func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001334 cert := String(a.properties.Certificate)
1335 if cert != "" && android.SrcIsModule(cert) == "" {
1336 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001337 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1338 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001339 } else if cert == "" {
1340 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001341 a.container_certificate_file = pem
1342 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001343 }
1344
Alex Light5098a612018-11-29 17:12:15 -08001345 var abis []string
1346 for _, target := range ctx.MultiTargets() {
1347 if len(target.Arch.Abi) > 0 {
1348 abis = append(abis, target.Arch.Abi[0])
1349 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001350 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001351
Alex Light5098a612018-11-29 17:12:15 -08001352 abis = android.FirstUniqueStrings(abis)
1353
1354 suffix := apexType.suffix()
1355 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001356
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001357 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001358 for _, f := range a.filesInfo {
1359 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001360 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001361
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001362 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001363 emitCommands := []string{}
1364 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1365 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001366 for i, src := range filesToCopy {
1367 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001368 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001369 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001370 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1371 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001372 for _, sym := range a.filesInfo[i].symlinks {
1373 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1374 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1375 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001376 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001377 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001378 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001379
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001380 if a.properties.Whitelisted_files != nil {
1381 ctx.Build(pctx, android.BuildParams{
1382 Rule: emitApexContentRule,
1383 Implicits: implicitInputs,
1384 Output: imageContentFile,
1385 Description: "emit apex image content",
1386 Args: map[string]string{
1387 "emit_commands": strings.Join(emitCommands, " && "),
1388 },
1389 })
1390 implicitInputs = append(implicitInputs, imageContentFile)
1391 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1392
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001393 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001394 ctx.Build(pctx, android.BuildParams{
1395 Rule: diffApexContentRule,
1396 Implicits: implicitInputs,
1397 Output: phonyOutput,
1398 Description: "diff apex image content",
1399 Args: map[string]string{
1400 "whitelisted_files_file": whitelistedFilesFile.String(),
1401 "image_content_file": imageContentFile.String(),
1402 "apex_module_name": ctx.ModuleName(),
1403 },
1404 })
1405
1406 implicitInputs = append(implicitInputs, phonyOutput)
1407 }
1408
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001409 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1410 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001411
Alex Light5098a612018-11-29 17:12:15 -08001412 if apexType.image() {
1413 // files and dirs that will be created in APEX
1414 var readOnlyPaths []string
1415 var executablePaths []string // this also includes dirs
1416 for _, f := range a.filesInfo {
1417 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001418 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001419 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001420 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001421 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001422 }
Alex Light5098a612018-11-29 17:12:15 -08001423 } else {
1424 readOnlyPaths = append(readOnlyPaths, pathInApex)
1425 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001426 dir := f.installDir
1427 for !android.InList(dir, executablePaths) && dir != "" {
1428 executablePaths = append(executablePaths, dir)
1429 dir, _ = filepath.Split(dir) // move up to the parent
1430 if len(dir) > 0 {
1431 // remove trailing slash
1432 dir = dir[:len(dir)-1]
1433 }
Alex Light5098a612018-11-29 17:12:15 -08001434 }
1435 }
1436 sort.Strings(readOnlyPaths)
1437 sort.Strings(executablePaths)
1438 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1439 ctx.Build(pctx, android.BuildParams{
1440 Rule: generateFsConfig,
1441 Output: cannedFsConfig,
1442 Description: "generate fs config",
1443 Args: map[string]string{
1444 "ro_paths": strings.Join(readOnlyPaths, " "),
1445 "exec_paths": strings.Join(executablePaths, " "),
1446 },
1447 })
1448
1449 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1450 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1451 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1452 if !fileContextsOptionalPath.Valid() {
1453 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1454 return
1455 }
1456 fileContexts := fileContextsOptionalPath.Path()
1457
Jiyong Park835d82b2018-12-27 16:04:18 +09001458 optFlags := []string{}
1459
Alex Light5098a612018-11-29 17:12:15 -08001460 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001461 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1462 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001463
Jiyong Park7f67f482019-01-05 12:57:48 +09001464 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1465 if overridden {
1466 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1467 }
1468
Jiyong Park40e26a22019-02-08 02:53:06 +09001469 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001470 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001471 implicitInputs = append(implicitInputs, androidManifestFile)
1472 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1473 }
1474
Jiyong Park71b519d2019-04-18 17:25:49 +09001475 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1476 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1477 ctx.Config().UnbundledBuild() &&
1478 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1479 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1480 apiFingerprint := java.ApiFingerprintPath(ctx)
1481 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1482 implicitInputs = append(implicitInputs, apiFingerprint)
1483 }
1484 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1485
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001486 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1487 if noticeFile.Valid() {
1488 // If there's a NOTICE file, embed it as an asset file in the APEX.
1489 implicitInputs = append(implicitInputs, noticeFile.Path())
1490 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1491 }
1492
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001493 if !ctx.Config().UnbundledBuild() && a.installable() {
1494 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1495 // don't need hashtree for activation. Therefore, by removing hashtree from
1496 // apex bundle (filesystem image in it, to be specific), we can save storage.
1497 optFlags = append(optFlags, "--no_hashtree")
1498 }
1499
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001500 if a.properties.Apex_name != nil {
1501 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1502 // Note that apex_manifest is also mended.
1503 optFlags = append(optFlags, "--do_not_check_keyname")
1504 }
1505
Alex Light5098a612018-11-29 17:12:15 -08001506 ctx.Build(pctx, android.BuildParams{
1507 Rule: apexRule,
1508 Implicits: implicitInputs,
1509 Output: unsignedOutputFile,
1510 Description: "apex (" + apexType.name() + ")",
1511 Args: map[string]string{
1512 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1513 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1514 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001515 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001516 "file_contexts": fileContexts.String(),
1517 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001518 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001519 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001520 },
1521 })
1522
1523 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1524 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1525 a.bundleModuleFile = bundleModuleFile
1526
1527 ctx.Build(pctx, android.BuildParams{
1528 Rule: apexProtoConvertRule,
1529 Input: unsignedOutputFile,
1530 Output: apexProtoFile,
1531 Description: "apex proto convert",
1532 })
1533
1534 ctx.Build(pctx, android.BuildParams{
1535 Rule: apexBundleRule,
1536 Input: apexProtoFile,
1537 Output: a.bundleModuleFile,
1538 Description: "apex bundle module",
1539 Args: map[string]string{
1540 "abi": strings.Join(abis, "."),
1541 },
1542 })
1543 } else {
1544 ctx.Build(pctx, android.BuildParams{
1545 Rule: zipApexRule,
1546 Implicits: implicitInputs,
1547 Output: unsignedOutputFile,
1548 Description: "apex (" + apexType.name() + ")",
1549 Args: map[string]string{
1550 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1551 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1552 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001553 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001554 },
1555 })
Colin Crossa4925902018-11-16 11:36:28 -08001556 }
Colin Crossa4925902018-11-16 11:36:28 -08001557
Alex Light5098a612018-11-29 17:12:15 -08001558 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001559 ctx.Build(pctx, android.BuildParams{
1560 Rule: java.Signapk,
1561 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001562 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001563 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001564 Implicits: []android.Path{
1565 a.container_certificate_file,
1566 a.container_private_key_file,
1567 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001568 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001569 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001570 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001571 },
1572 })
Alex Light5098a612018-11-29 17:12:15 -08001573
1574 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahne8fb7242019-09-17 13:50:45 +09001575 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) && !a.isFlattenedVariant() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001576 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001577 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001578}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001579
Jiyong Park8fd61922018-11-08 02:50:25 +09001580func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001581 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001582 // 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 +09001583 // with other ordinary files.
Jooyung Han31c470b2019-10-18 16:26:59 +09001584 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, "apex_manifest.json." + ctx.ModuleName(), ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001585
Jiyong Park42cca6c2019-04-01 11:15:50 +09001586 // rename to apex_pubkey
1587 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1588 ctx.Build(pctx, android.BuildParams{
1589 Rule: android.Cp,
1590 Input: a.public_key_file,
1591 Output: copiedPubkey,
1592 })
Jooyung Han31c470b2019-10-18 16:26:59 +09001593 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName(), ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001594
Jiyong Park23c52b02019-02-02 13:13:47 +09001595 if ctx.Config().FlattenApex() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001596 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001597 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001598 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001599 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1600 for _, sym := range fi.symlinks {
1601 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1602 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001603 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001604 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001605 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001606}
1607
1608func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001609 if a.properties.HideFromMake {
1610 return android.AndroidMkData{
1611 Disabled: true,
1612 }
1613 }
Alex Light5098a612018-11-29 17:12:15 -08001614 writers := []android.AndroidMkData{}
1615 if a.apexTypes.image() {
1616 writers = append(writers, a.androidMkForType(imageApex))
1617 }
1618 if a.apexTypes.zip() {
1619 writers = append(writers, a.androidMkForType(zipApex))
1620 }
1621 return android.AndroidMkData{
1622 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1623 for _, data := range writers {
1624 data.Custom(w, name, prefix, moduleDir, data)
1625 }
1626 }}
1627}
1628
Jooyung Han7a78a922019-10-08 21:59:58 +09001629func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001630 moduleNames := []string{}
1631
1632 for _, fi := range a.filesInfo {
1633 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1634 continue
1635 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001636 if a.properties.Flattened && !apexType.image() {
1637 continue
Jiyong Park94427262019-02-05 23:18:47 +09001638 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001639
1640 var suffix string
Sundong Ahne8fb7242019-09-17 13:50:45 +09001641 if a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001642 suffix = ".flattened"
1643 }
1644
1645 if !android.InList(fi.moduleName, moduleNames) {
1646 moduleNames = append(moduleNames, fi.moduleName+suffix)
1647 }
1648
Jiyong Park94427262019-02-05 23:18:47 +09001649 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1650 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001651 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName+suffix)
Roland Levillain411c5842019-09-19 16:37:20 +01001652 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001653 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahne9b55722019-09-06 17:37:42 +09001654 if a.properties.Flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001655 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001656 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001657 apexName, fi.installDir))
Sundong Ahne8fb7242019-09-17 13:50:45 +09001658 if !a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001659 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1660 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001661 if len(fi.symlinks) > 0 {
1662 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1663 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001664
1665 if fi.module != nil && fi.module.NoticeFile().Valid() {
1666 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1667 }
Jiyong Park94427262019-02-05 23:18:47 +09001668 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001669 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001670 }
1671 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1672 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1673 if fi.module != nil {
1674 archStr := fi.module.Target().Arch.ArchType.String()
1675 host := false
1676 switch fi.module.Target().Os.Class {
1677 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001678 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001679 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1680 }
1681 host = true
1682 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001683 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001684 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1685 }
1686 host = true
1687 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001688 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001689 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1690 }
1691 }
1692 if host {
1693 makeOs := fi.module.Target().Os.String()
1694 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1695 makeOs = "linux"
1696 }
1697 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1698 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1699 }
1700 }
1701 if fi.class == javaSharedLib {
1702 javaModule := fi.module.(*java.Library)
1703 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1704 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1705 // we will have foo.jar.jar
1706 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1707 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1708 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1709 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1710 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1711 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001712 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001713 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001714 if cc, ok := fi.module.(*cc.Module); ok {
1715 if cc.UnstrippedOutputFile() != nil {
1716 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1717 }
1718 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001719 if cc.CoverageOutputFile().Valid() {
1720 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1721 }
Jiyong Park94427262019-02-05 23:18:47 +09001722 }
1723 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1724 } else {
1725 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001726 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
1727 if !a.isFlattenedVariant() && fi.builtFile.Base() == "apex_manifest.json" && len(a.compatSymlinks) > 0 {
1728 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1729 }
Jiyong Park94427262019-02-05 23:18:47 +09001730 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1731 }
1732 }
1733 return moduleNames
1734}
1735
Alex Light5098a612018-11-29 17:12:15 -08001736func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001737 return android.AndroidMkData{
1738 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1739 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001740 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001741 apexName := proptools.StringDefault(a.properties.Apex_name, name)
1742 moduleNames = a.androidMkForFiles(w, apexName, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001743 }
1744
Sundong Ahne8fb7242019-09-17 13:50:45 +09001745 if a.isFlattenedVariant() {
Sundong Ahne9b55722019-09-06 17:37:42 +09001746 name = name + ".flattened"
1747 }
1748
1749 if a.properties.Flattened && apexType.image() {
Jiyong Park719b4462019-01-13 00:39:51 +09001750 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001751 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1752 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1753 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001754 if len(moduleNames) > 0 {
1755 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1756 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001757 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001758 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1759
Sundong Ahne8fb7242019-09-17 13:50:45 +09001760 } else if !a.isFlattenedVariant() {
Alex Light5098a612018-11-29 17:12:15 -08001761 // zip-apex is the less common type so have the name refer to the image-apex
1762 // only and use {name}.zip if you want the zip-apex
1763 if apexType == zipApex && a.apexTypes == both {
1764 name = name + ".zip"
1765 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001766 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1767 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1768 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1769 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001770 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001771 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001772 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001773 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001774 if len(moduleNames) > 0 {
1775 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1776 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001777 if len(a.externalDeps) > 0 {
1778 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1779 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001780 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001781 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001782 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001783 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001784 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001785 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1786 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1787 if len(postInstallCommands) > 0 {
1788 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1789 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001790 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001791
Alex Light5098a612018-11-29 17:12:15 -08001792 if apexType == imageApex {
1793 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1794 }
Jiyong Park719b4462019-01-13 00:39:51 +09001795 }
1796 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001797}
1798
Jooyung Han344d5432019-08-23 11:17:39 +09001799func newApexBundle() *apexBundle {
Alex Light5098a612018-11-29 17:12:15 -08001800 module := &apexBundle{
1801 outputFiles: map[apexPackaging]android.WritablePath{},
1802 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001803 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001804 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001805 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001806 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1807 })
Alex Light5098a612018-11-29 17:12:15 -08001808 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001809 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001810 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001811 return module
1812}
Jiyong Park30ca9372019-02-07 16:27:23 +09001813
Jooyung Han344d5432019-08-23 11:17:39 +09001814func ApexBundleFactory(testApex bool) android.Module {
1815 bundle := newApexBundle()
1816 bundle.testApex = testApex
1817 return bundle
1818}
1819
1820func testApexBundleFactory() android.Module {
1821 bundle := newApexBundle()
1822 bundle.testApex = true
1823 return bundle
1824}
1825
Jiyong Parkd1063c12019-07-17 20:08:41 +09001826func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001827 return newApexBundle()
1828}
1829
1830// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1831// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1832// If not specified, then the "current" versions are gathered.
1833func vndkApexBundleFactory() android.Module {
1834 bundle := newApexBundle()
1835 bundle.vndkApex = true
1836 bundle.AddProperties(&bundle.vndkProperties)
1837 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1838 ctx.AppendProperties(&struct {
1839 Compile_multilib *string
1840 }{
1841 proptools.StringPtr("both"),
1842 })
1843 })
1844 return bundle
1845}
1846
Jooyung Han31c470b2019-10-18 16:26:59 +09001847func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1848 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1849 if vndkVersion == "current" {
1850 vndkVersion = config.PlatformVndkVersion()
1851 }
1852 return vndkVersion
1853}
1854
Jiyong Park30ca9372019-02-07 16:27:23 +09001855//
1856// Defaults
1857//
1858type Defaults struct {
1859 android.ModuleBase
1860 android.DefaultsModuleBase
1861}
1862
Jiyong Park30ca9372019-02-07 16:27:23 +09001863func defaultsFactory() android.Module {
1864 return DefaultsFactory()
1865}
1866
1867func DefaultsFactory(props ...interface{}) android.Module {
1868 module := &Defaults{}
1869
1870 module.AddProperties(props...)
1871 module.AddProperties(
1872 &apexBundleProperties{},
1873 &apexTargetBundleProperties{},
1874 )
1875
1876 android.InitDefaultsModule(module)
1877 return module
1878}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001879
1880//
1881// Prebuilt APEX
1882//
1883type Prebuilt struct {
1884 android.ModuleBase
1885 prebuilt android.Prebuilt
1886
1887 properties PrebuiltProperties
1888
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001889 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001890 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001891 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001892 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001893}
1894
1895type PrebuiltProperties struct {
1896 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001897 Source string `blueprint:"mutated"`
1898 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001899
1900 Src *string
1901 Arch struct {
1902 Arm struct {
1903 Src *string
1904 }
1905 Arm64 struct {
1906 Src *string
1907 }
1908 X86 struct {
1909 Src *string
1910 }
1911 X86_64 struct {
1912 Src *string
1913 }
1914 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001915
1916 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001917 // Optional name for the installed apex. If unspecified, name of the
1918 // module is used as the file name
1919 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001920
1921 // Names of modules to be overridden. Listed modules can only be other binaries
1922 // (in Make or Soong).
1923 // This does not completely prevent installation of the overridden binaries, but if both
1924 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1925 // from PRODUCT_PACKAGES.
1926 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001927}
1928
1929func (p *Prebuilt) installable() bool {
1930 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001931}
1932
1933func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001934 // If the device is configured to use flattened APEX, force disable the prebuilt because
1935 // the prebuilt is a non-flattened one.
1936 forceDisable := ctx.Config().FlattenApex()
1937
1938 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1939 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001940 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001941
Kun Niu10c9f832019-07-29 16:28:57 -07001942 // Force disable the prebuilts when coverage is enabled.
1943 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1944 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1945
Jiyong Park50b81e52019-07-11 11:24:41 +09001946 // b/137216042 don't use prebuilts when address sanitizer is on
1947 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1948 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1949
1950 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001951 p.properties.ForceDisable = true
1952 return
1953 }
1954
Jiyong Parkc95714e2019-03-29 14:23:10 +09001955 // This is called before prebuilt_select and prebuilt_postdeps mutators
1956 // The mutators requires that src to be set correctly for each arch so that
1957 // arch variants are disabled when src is not provided for the arch.
1958 if len(ctx.MultiTargets()) != 1 {
1959 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1960 return
1961 }
1962 var src string
1963 switch ctx.MultiTargets()[0].Arch.ArchType {
1964 case android.Arm:
1965 src = String(p.properties.Arch.Arm.Src)
1966 case android.Arm64:
1967 src = String(p.properties.Arch.Arm64.Src)
1968 case android.X86:
1969 src = String(p.properties.Arch.X86.Src)
1970 case android.X86_64:
1971 src = String(p.properties.Arch.X86_64.Src)
1972 default:
1973 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1974 return
1975 }
1976 if src == "" {
1977 src = String(p.properties.Src)
1978 }
1979 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001980}
1981
Jiyong Park03b68dd2019-07-26 23:20:40 +09001982func (p *Prebuilt) isForceDisabled() bool {
1983 return p.properties.ForceDisable
1984}
1985
Colin Cross41955e82019-05-29 14:40:35 -07001986func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1987 switch tag {
1988 case "":
1989 return android.Paths{p.outputApex}, nil
1990 default:
1991 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1992 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001993}
1994
Jiyong Park4d277042019-04-23 18:00:10 +09001995func (p *Prebuilt) InstallFilename() string {
1996 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1997}
1998
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001999func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002000 if p.properties.ForceDisable {
2001 return
2002 }
2003
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002004 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002005 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002006 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002007 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002008 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2009 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2010 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002011 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2012 ctx.Build(pctx, android.BuildParams{
2013 Rule: android.Cp,
2014 Input: p.inputApex,
2015 Output: p.outputApex,
2016 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002017 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002018 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002019 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002020
2021 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002022}
2023
2024func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2025 return &p.prebuilt
2026}
2027
2028func (p *Prebuilt) Name() string {
2029 return p.prebuilt.Name(p.ModuleBase.Name())
2030}
2031
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002032func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2033 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002034 Class: "ETC",
2035 OutputFile: android.OptionalPathForPath(p.inputApex),
2036 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002037 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2038 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002039 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002040 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2041 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2042 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2043 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002044 },
2045 }
2046}
2047
2048// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2049func PrebuiltFactory() android.Module {
2050 module := &Prebuilt{}
2051 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002052 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002053 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002054 return module
2055}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002056
2057func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2058 // small helper to add symlink commands
2059 addSymlink := func(target, dir, linkName string) {
2060 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2061 link := filepath.Join(outDir, linkName)
2062 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2063 }
2064
2065 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2066 // When all hard-coded references are fixed, remove symbolic links
2067 // Note that we should keep following symlinks for older VNDKs (<=29)
2068 // Since prebuilt vndk libs still depend on system/lib/vndk path
2069 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2070 // the name of vndk apex is formatted "com.android.vndk.v" + version
2071 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2072 if ctx.Config().Android64() {
2073 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2074 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2075 }
2076 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2077 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2078 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2079 }
2080 }
2081 return
2082}