blob: f4b2ed724275a5738296e4c62547e213443f1bfa [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"
Sundong Ahnabb64432019-10-22 13:58:29 +0900132 flattenedSuffix = ".flattened"
Alex Light5098a612018-11-29 17:12:15 -0800133
Sundong Ahnabb64432019-10-22 13:58:29 +0900134 imageApexType = "image"
135 zipApexType = "zip"
136 flattenedApexType = "flattened"
Jooyung Han72bd2f82019-10-23 16:46:38 +0900137
138 vndkApexNamePrefix = "com.android.vndk.v"
139)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900140
141type dependencyTag struct {
142 blueprint.BaseDependencyTag
143 name string
144}
145
146var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900147 sharedLibTag = dependencyTag{name: "sharedLib"}
148 executableTag = dependencyTag{name: "executable"}
149 javaLibTag = dependencyTag{name: "javaLib"}
150 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100151 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900152 keyTag = dependencyTag{name: "key"}
153 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900154 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900155 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900156)
157
158func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700159 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900160 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900161 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100162 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
163 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
164 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
165 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000166 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100167 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
168 } else {
169 return pctx.HostBinToolPath(ctx, tool).String()
170 }
171 })
172 }
173 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900174 pctx.HostBinToolVariable("avbtool", "avbtool")
175 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
176 pctx.HostBinToolVariable("merge_zips", "merge_zips")
177 pctx.HostBinToolVariable("mke2fs", "mke2fs")
178 pctx.HostBinToolVariable("resize2fs", "resize2fs")
179 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
180 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800181 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900182 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900183 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900184
Jiyong Parkd1063c12019-07-17 20:08:41 +0900185 android.RegisterModuleType("apex", BundleFactory)
Alex Light0851b882019-02-07 13:20:53 -0800186 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jooyung Han344d5432019-08-23 11:17:39 +0900187 android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900188 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700189 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900190
Jooyung Han31c470b2019-10-18 16:26:59 +0900191 android.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900192 android.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han7a78a922019-10-08 21:59:58 +0900193
194 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
195 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
196 sort.Strings(*apexFileContextsInfos)
197 ctx.Strict("APEX_FILE_CONTEXTS_INFOS", strings.Join(*apexFileContextsInfos, " "))
198 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900199}
200
Jooyung Han31c470b2019-10-18 16:26:59 +0900201func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
202 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
203 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
204}
205
Jiyong Parkd1063c12019-07-17 20:08:41 +0900206func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
207 ctx.TopDown("apex_deps", apexDepsMutator)
208 ctx.BottomUp("apex", apexMutator).Parallel()
209 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
210 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900211}
212
Jooyung Han344d5432019-08-23 11:17:39 +0900213var (
214 vndkApexListKey = android.NewOnceKey("vndkApexList")
215 vndkApexListMutex sync.Mutex
216)
217
Jooyung Han31c470b2019-10-18 16:26:59 +0900218func vndkApexList(config android.Config) map[string]string {
Jooyung Han344d5432019-08-23 11:17:39 +0900219 return config.Once(vndkApexListKey, func() interface{} {
Jooyung Han31c470b2019-10-18 16:26:59 +0900220 return map[string]string{}
221 }).(map[string]string)
Jooyung Han344d5432019-08-23 11:17:39 +0900222}
223
Jooyung Han31c470b2019-10-18 16:26:59 +0900224func apexVndkMutator(mctx android.TopDownMutatorContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900225 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
226 if ab.IsNativeBridgeSupported() {
227 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
228 }
Jooyung Han90eee022019-10-01 20:02:42 +0900229
Jooyung Han31c470b2019-10-18 16:26:59 +0900230 vndkVersion := ab.vndkVersion(mctx.DeviceConfig())
231 // Ensure VNDK APEX mount point is formatted as com.android.vndk.v###
Jooyung Han72bd2f82019-10-23 16:46:38 +0900232 ab.properties.Apex_name = proptools.StringPtr(vndkApexNamePrefix + vndkVersion)
Jooyung Han90eee022019-10-01 20:02:42 +0900233
Jooyung Han31c470b2019-10-18 16:26:59 +0900234 // vndk_version should be unique
Jooyung Han344d5432019-08-23 11:17:39 +0900235 vndkApexListMutex.Lock()
236 defer vndkApexListMutex.Unlock()
237 vndkApexList := vndkApexList(mctx.Config())
238 if other, ok := vndkApexList[vndkVersion]; ok {
Jooyung Han31c470b2019-10-18 16:26:59 +0900239 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other)
Jooyung Han344d5432019-08-23 11:17:39 +0900240 }
Jooyung Han31c470b2019-10-18 16:26:59 +0900241 vndkApexList[vndkVersion] = mctx.ModuleName()
Jooyung Han344d5432019-08-23 11:17:39 +0900242 }
243}
244
Jooyung Han31c470b2019-10-18 16:26:59 +0900245func apexVndkDepsMutator(mctx android.BottomUpMutatorContext) {
246 if m, ok := mctx.Module().(*cc.Module); ok && cc.IsForVndkApex(mctx, m) {
247 vndkVersion := m.VndkVersion()
Jooyung Han344d5432019-08-23 11:17:39 +0900248 vndkApexList := vndkApexList(mctx.Config())
Jooyung Han31c470b2019-10-18 16:26:59 +0900249 if vndkApex, ok := vndkApexList[vndkVersion]; ok {
250 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, vndkApex)
Jooyung Han344d5432019-08-23 11:17:39 +0900251 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900252 } else if a, ok := mctx.Module().(*apexBundle); ok && a.vndkApex {
253 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
254 mctx.AddDependency(mctx.Module(), prebuiltTag, cc.VndkLibrariesTxtModules(vndkVersion)...)
Jooyung Han344d5432019-08-23 11:17:39 +0900255 }
256}
257
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900258// Mark the direct and transitive dependencies of apex bundles so that they
259// can be built for the apex bundles.
260func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800261 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800262 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900263 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900264 depName := mctx.OtherModuleName(child)
265 // If the parent is apexBundle, this child is directly depended.
266 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800267 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800268 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
269 // non-installable apex's cannot be installed and so should not prevent libraries from being
270 // installed to the system.
271 android.UpdateApexDependency(apexBundleName, depName, directDep)
272 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900273
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900274 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900275 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900276 return true
277 } else {
278 return false
279 }
280 })
281 }
282}
283
284// Create apex variations if a module is included in APEX(s).
285func apexMutator(mctx android.BottomUpMutatorContext) {
286 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900287 am.CreateApexVariations(mctx)
Jooyung Han7a78a922019-10-08 21:59:58 +0900288 } else if a, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900289 // apex bundle itself is mutated so that it and its modules have same
290 // apex variant.
291 apexBundleName := mctx.ModuleName()
292 mctx.CreateVariations(apexBundleName)
Jooyung Han7a78a922019-10-08 21:59:58 +0900293
294 // collects APEX list
295 if mctx.Device() && a.installable() {
296 addApexFileContextsInfos(mctx, a)
297 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900298 }
299}
Sundong Ahne9b55722019-09-06 17:37:42 +0900300
Jooyung Han7a78a922019-10-08 21:59:58 +0900301var (
302 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
303 apexFileContextsInfosMutex sync.Mutex
304)
305
306func apexFileContextsInfos(config android.Config) *[]string {
307 return config.Once(apexFileContextsInfosKey, func() interface{} {
308 return &[]string{}
309 }).(*[]string)
310}
311
312func addApexFileContextsInfos(ctx android.BaseModuleContext, a *apexBundle) {
313 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
314 fileContextsName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
315
316 apexFileContextsInfosMutex.Lock()
317 defer apexFileContextsInfosMutex.Unlock()
318 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
319 *apexFileContextsInfos = append(*apexFileContextsInfos, apexName+":"+fileContextsName)
320}
321
Sundong Ahne9b55722019-09-06 17:37:42 +0900322func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900323 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +0900324 var variants []string
325 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
326 case "image":
327 variants = append(variants, imageApexType, flattenedApexType)
328 case "zip":
329 variants = append(variants, zipApexType)
330 case "both":
331 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
332 default:
333 mctx.PropertyErrorf("type", "%q is not one of \"image\" or \"zip\".", *ab.properties.Payload_type)
334 return
335 }
336
337 modules := mctx.CreateLocalVariations(variants...)
338
339 for i, v := range variants {
340 switch v {
341 case imageApexType:
342 modules[i].(*apexBundle).properties.ApexType = imageApex
343 case zipApexType:
344 modules[i].(*apexBundle).properties.ApexType = zipApex
345 case flattenedApexType:
346 modules[i].(*apexBundle).properties.ApexType = flattenedApex
347 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900348 }
349 }
350}
351
Jooyung Han5c998b92019-06-27 11:30:33 +0900352func apexUsesMutator(mctx android.BottomUpMutatorContext) {
353 if ab, ok := mctx.Module().(*apexBundle); ok {
354 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
355 }
356}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900357
Jooyung Handc782442019-11-01 03:14:38 +0900358var (
359 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
360)
361
362// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
363// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
364// which may cause compatibility issues. (e.g. libbinder)
365// Even though libbinder restricts its availability via 'apex_available' property and relies on
366// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
367// to avoid similar problems.
368func useVendorWhitelist(config android.Config) []string {
369 return config.Once(useVendorWhitelistKey, func() interface{} {
370 return []string{
371 // swcodec uses "vendor" variants for smaller size
372 "com.android.media.swcodec",
373 "test_com.android.media.swcodec",
374 }
375 }).([]string)
376}
377
378// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
379// called before the first call to useVendorWhitelist()
380func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
381 config.Once(useVendorWhitelistKey, func() interface{} {
382 return whitelist
383 })
384}
385
Alex Light9670d332019-01-29 18:07:33 -0800386type apexNativeDependencies struct {
387 // List of native libraries
388 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900389
Alex Light9670d332019-01-29 18:07:33 -0800390 // List of native executables
391 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900392
Roland Levillain630846d2019-06-26 12:48:34 +0100393 // List of native tests
394 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800395}
Jooyung Han344d5432019-08-23 11:17:39 +0900396
Alex Light9670d332019-01-29 18:07:33 -0800397type apexMultilibProperties struct {
398 // Native dependencies whose compile_multilib is "first"
399 First apexNativeDependencies
400
401 // Native dependencies whose compile_multilib is "both"
402 Both apexNativeDependencies
403
404 // Native dependencies whose compile_multilib is "prefer32"
405 Prefer32 apexNativeDependencies
406
407 // Native dependencies whose compile_multilib is "32"
408 Lib32 apexNativeDependencies
409
410 // Native dependencies whose compile_multilib is "64"
411 Lib64 apexNativeDependencies
412}
413
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900414type apexBundleProperties struct {
415 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000416 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800417 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900418
Jiyong Park40e26a22019-02-08 02:53:06 +0900419 // AndroidManifest.xml file used for the zip container of this APEX bundle.
420 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800421 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900422
Roland Levillain411c5842019-09-19 16:37:20 +0100423 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
424 // device (/apex/<apex_name>).
425 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900426 Apex_name *string
427
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900428 // Determines the file contexts file for setting security context to each file in this APEX bundle.
429 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
430 // used.
431 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900432 File_contexts *string
433
434 // List of native shared libs that are embedded inside this APEX bundle
435 Native_shared_libs []string
436
Roland Levillain630846d2019-06-26 12:48:34 +0100437 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900438 Binaries []string
439
440 // List of java libraries that are embedded inside this APEX bundle
441 Java_libs []string
442
443 // List of prebuilt files that are embedded inside this APEX bundle
444 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900445
Roland Levillain630846d2019-06-26 12:48:34 +0100446 // List of tests that are embedded inside this APEX bundle
447 Tests []string
448
Jiyong Parkff1458f2018-10-12 21:49:38 +0900449 // Name of the apex_key module that provides the private key to sign APEX
450 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900451
Alex Light5098a612018-11-29 17:12:15 -0800452 // The type of APEX to build. Controls what the APEX payload is. Either
453 // 'image', 'zip' or 'both'. Default: 'image'.
454 Payload_type *string
455
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900456 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
457 // or an android_app_certificate module name in the form ":module".
458 Certificate *string
459
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900460 // Whether this APEX is installable to one of the partitions. Default: true.
461 Installable *bool
462
Jiyong Parkda6eb592018-12-19 17:12:36 +0900463 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
464 // Default is false.
465 Use_vendor *bool
466
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800467 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
468 Ignore_system_library_special_case *bool
469
Alex Light9670d332019-01-29 18:07:33 -0800470 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900471
Jiyong Parkf97782b2019-02-13 20:28:58 +0900472 // List of sanitizer names that this APEX is enabled for
473 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900474
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900475 PreventInstall bool `blueprint:"mutated"`
476
477 HideFromMake bool `blueprint:"mutated"`
478
Jooyung Han5c998b92019-06-27 11:30:33 +0900479 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
480 Provide_cpp_shared_libs *bool
481
482 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
483 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100484
485 // A txt file containing list of files that are whitelisted to be included in this APEX.
486 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900487
488 // List of APKs to package inside APEX
489 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900490
Sundong Ahnabb64432019-10-22 13:58:29 +0900491 // package format of this apex variant; could be non-flattened, flattened, or zip.
492 // imageApex, zipApex or flattened
493 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900494
Jiyong Parkd1063c12019-07-17 20:08:41 +0900495 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
496 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
497 // is implied. This value affects all modules included in this APEX. In other words, they are
498 // also built with the SDKs specified here.
499 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800500}
501
502type apexTargetBundleProperties struct {
503 Target struct {
504 // Multilib properties only for android.
505 Android struct {
506 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900507 }
Jooyung Han344d5432019-08-23 11:17:39 +0900508
Alex Light9670d332019-01-29 18:07:33 -0800509 // Multilib properties only for host.
510 Host struct {
511 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900512 }
Jooyung Han344d5432019-08-23 11:17:39 +0900513
Alex Light9670d332019-01-29 18:07:33 -0800514 // Multilib properties only for host linux_bionic.
515 Linux_bionic struct {
516 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900517 }
Jooyung Han344d5432019-08-23 11:17:39 +0900518
Alex Light9670d332019-01-29 18:07:33 -0800519 // Multilib properties only for host linux_glibc.
520 Linux_glibc struct {
521 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900522 }
523 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900524}
525
Jooyung Han344d5432019-08-23 11:17:39 +0900526type apexVndkProperties struct {
527 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
528 Vndk_version *string
529}
530
Jiyong Park8fd61922018-11-08 02:50:25 +0900531type apexFileClass int
532
533const (
534 etc apexFileClass = iota
535 nativeSharedLib
536 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900537 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800538 pyBinary
539 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900540 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100541 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900542 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900543)
544
Alex Light5098a612018-11-29 17:12:15 -0800545type apexPackaging int
546
547const (
548 imageApex apexPackaging = iota
549 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900550 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800551)
552
Sundong Ahnabb64432019-10-22 13:58:29 +0900553// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800554func (a apexPackaging) suffix() string {
555 switch a {
556 case imageApex:
557 return imageApexSuffix
558 case zipApex:
559 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800560 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100561 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800562 }
563}
564
565func (a apexPackaging) name() string {
566 switch a {
567 case imageApex:
568 return imageApexType
569 case zipApex:
570 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800571 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100572 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800573 }
574}
575
Jiyong Park8fd61922018-11-08 02:50:25 +0900576func (class apexFileClass) NameInMake() string {
577 switch class {
578 case etc:
579 return "ETC"
580 case nativeSharedLib:
581 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800582 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900583 return "EXECUTABLES"
584 case javaSharedLib:
585 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100586 case nativeTest:
587 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900588 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900589 // b/142537672 Why isn't this APP? We want to have full control over
590 // the paths and file names of the apk file under the flattend APEX.
591 // If this is set to APP, then the paths and file names are modified
592 // by the Make build system. For example, it is installed to
593 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
594 // /system/apex/<apexname>/app/<Appname> because the build system automatically
595 // appends module name (which is <apexname>.<Appname> to the path.
596 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900597 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100598 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900599 }
600}
601
602type apexFile struct {
603 builtFile android.Path
604 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900605 installDir string
606 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900607 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800608 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900609}
610
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900611type apexBundle struct {
612 android.ModuleBase
613 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900614 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900615
Alex Light9670d332019-01-29 18:07:33 -0800616 properties apexBundleProperties
617 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900618 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900619
Colin Crossa4925902018-11-16 11:36:28 -0800620 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900621 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700622 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900623
Jiyong Park03b68dd2019-07-26 23:20:40 +0900624 prebuiltFileToDelete string
625
Jiyong Park42cca6c2019-04-01 11:15:50 +0900626 public_key_file android.Path
627 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900628
629 container_certificate_file android.Path
630 container_private_key_file android.Path
631
Jiyong Park8fd61922018-11-08 02:50:25 +0900632 // list of files to be included in this apex
633 filesInfo []apexFile
634
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900635 // list of module names that this APEX is depending on
636 externalDeps []string
637
Sundong Ahnabb64432019-10-22 13:58:29 +0900638 testApex bool
639 vndkApex bool
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000640 artApex bool
Sundong Ahnabb64432019-10-22 13:58:29 +0900641 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900642
643 // intermediate path for apex_manifest.json
644 manifestOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900645
646 // list of commands to create symlinks for backward compatibility
647 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
648 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
649 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
650 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900651
652 // Suffix of module name in Android.mk
653 // ".flattened", ".apex", ".zipapex", or ""
654 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900655}
656
Jiyong Park397e55e2018-10-24 21:09:55 +0900657func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100658 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700659 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900660 // Use *FarVariation* to be able to depend on modules having
661 // conflicting variations with this module. This is required since
662 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
663 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700664 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900665 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900666 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900667 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700668 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900669
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700670 ctx.AddFarVariationDependencies(append(target.Variations(),
671 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
672 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100673
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700674 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100675 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100676 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700677 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900678}
679
Alex Light9670d332019-01-29 18:07:33 -0800680func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
681 if ctx.Os().Class == android.Device {
682 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
683 } else {
684 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
685 if ctx.Os().Bionic() {
686 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
687 } else {
688 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
689 }
690 }
691}
692
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900693func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900694 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
695 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
696 }
697
Jiyong Park397e55e2018-10-24 21:09:55 +0900698 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900699 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800700
701 a.combineProperties(ctx)
702
Jiyong Park397e55e2018-10-24 21:09:55 +0900703 has32BitTarget := false
704 for _, target := range targets {
705 if target.Arch.ArchType.Multilib == "lib32" {
706 has32BitTarget = true
707 }
708 }
709 for i, target := range targets {
710 // When multilib.* is omitted for native_shared_libs, it implies
711 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700712 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900713 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900714 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700715 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900716
Roland Levillain630846d2019-06-26 12:48:34 +0100717 // When multilib.* is omitted for tests, it implies
718 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700719 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100720 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100721 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700722 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100723
Jiyong Park397e55e2018-10-24 21:09:55 +0900724 // Add native modules targetting both ABIs
725 addDependenciesForNativeModules(ctx,
726 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100727 a.properties.Multilib.Both.Binaries,
728 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700729 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900730 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900731
Alex Light3d673592019-01-18 14:37:31 -0800732 isPrimaryAbi := i == 0
733 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900734 // When multilib.* is omitted for binaries, it implies
735 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700736 ctx.AddFarVariationDependencies(append(target.Variations(),
737 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
738 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900739
740 // Add native modules targetting the first ABI
741 addDependenciesForNativeModules(ctx,
742 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100743 a.properties.Multilib.First.Binaries,
744 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700745 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900746 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800747
748 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700749 ctx.AddFarVariationDependencies(target.Variations(),
750 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900751 }
752
753 switch target.Arch.ArchType.Multilib {
754 case "lib32":
755 // Add native modules targetting 32-bit ABI
756 addDependenciesForNativeModules(ctx,
757 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100758 a.properties.Multilib.Lib32.Binaries,
759 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700760 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900761 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900762
763 addDependenciesForNativeModules(ctx,
764 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100765 a.properties.Multilib.Prefer32.Binaries,
766 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700767 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900768 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900769 case "lib64":
770 // Add native modules targetting 64-bit ABI
771 addDependenciesForNativeModules(ctx,
772 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100773 a.properties.Multilib.Lib64.Binaries,
774 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700775 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900776 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900777
778 if !has32BitTarget {
779 addDependenciesForNativeModules(ctx,
780 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100781 a.properties.Multilib.Prefer32.Binaries,
782 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700783 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900784 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900785 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700786
787 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
788 for _, sanitizer := range ctx.Config().SanitizeDevice() {
789 if sanitizer == "hwaddress" {
790 addDependenciesForNativeModules(ctx,
791 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700792 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700793 break
794 }
795 }
796 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900797 }
798
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900799 }
800
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700801 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
802 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900803
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700804 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
805 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900806
Jiyong Park23c52b02019-02-02 13:13:47 +0900807 if String(a.properties.Key) == "" {
808 ctx.ModuleErrorf("key is missing")
809 return
810 }
811 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900812
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900813 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900814 if cert != "" {
815 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900816 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900817
818 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
819 if len(a.properties.Uses_sdks) > 0 {
820 sdkRefs := []android.SdkRef{}
821 for _, str := range a.properties.Uses_sdks {
822 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
823 sdkRefs = append(sdkRefs, parsed)
824 }
825 a.BuildWithSdks(sdkRefs)
826 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900827}
828
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900829func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
830 // direct deps of an APEX bundle are all part of the APEX bundle
831 return true
832}
833
Colin Cross0ea8ba82019-06-06 14:33:29 -0700834func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900835 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
836 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000837 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900838 }
839 return String(a.properties.Certificate)
840}
841
Colin Cross41955e82019-05-29 14:40:35 -0700842func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
843 switch tag {
844 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900845 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700846 default:
847 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900848 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900849}
850
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900851func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900852 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900853}
854
Jiyong Park7c1dc612019-01-05 11:15:24 +0900855func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900856 if a.vndkApex {
857 return "vendor." + a.vndkVersion(config)
858 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900859 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900860 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900861 } else {
862 return "core"
863 }
864}
865
Jiyong Parkf97782b2019-02-13 20:28:58 +0900866func (a *apexBundle) EnableSanitizer(sanitizerName string) {
867 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
868 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
869 }
870}
871
Jiyong Park388ef3f2019-01-28 19:47:32 +0900872func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900873 if android.InList(sanitizerName, a.properties.SanitizerNames) {
874 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900875 }
876
877 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900878 globalSanitizerNames := []string{}
879 if a.Host() {
880 globalSanitizerNames = ctx.Config().SanitizeHost()
881 } else {
882 arches := ctx.Config().SanitizeDeviceArch()
883 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
884 globalSanitizerNames = ctx.Config().SanitizeDevice()
885 }
886 }
887 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900888}
889
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900890func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
891 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
892}
893
894func (a *apexBundle) PreventInstall() {
895 a.properties.PreventInstall = true
896}
897
898func (a *apexBundle) HideFromMake() {
899 a.properties.HideFromMake = true
900}
901
Martin Stjernholm279de572019-09-10 23:18:20 +0100902func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900903 // Decide the APEX-local directory by the multilib of the library
904 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100905 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900906 case "lib32":
907 dirInApex = "lib"
908 case "lib64":
909 dirInApex = "lib64"
910 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100911 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700912 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100913 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900914 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100915 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
916 // Special case for Bionic libs and other libs installed with them. This is
917 // to prevent those libs from being included in the search path
918 // /apex/com.android.runtime/${LIB}. This exclusion is required because
919 // those libs in the Runtime APEX are available via the legacy paths in
920 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
921 // to the legacy paths and thus will be loaded into the default linker
922 // namespace (aka "platform" namespace). If the libs are directly in
923 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
924 // into the runtime linker namespace, which will result in double loading of
925 // them, which isn't supported.
926 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900927 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900928
Martin Stjernholm279de572019-09-10 23:18:20 +0100929 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900930 return
931}
932
933func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900934 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700935 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200936 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900937 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900938 fileToCopy = cc.OutputFile().Path()
939 return
940}
941
Alex Light778127a2019-02-27 14:19:50 -0800942func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
943 dirInApex = "bin"
944 fileToCopy = py.HostToolPath().Path()
945 return
946}
947func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
948 dirInApex = "bin"
949 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
950 if err != nil {
951 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
952 return
953 }
954 fileToCopy = android.PathForOutput(ctx, s)
955 return
956}
957
Jiyong Park04480cf2019-02-06 00:16:29 +0900958func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
959 dirInApex = filepath.Join("bin", sh.SubDir())
960 fileToCopy = sh.OutputFile()
961 return
962}
963
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900964func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
965 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900966 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900967 return
968}
969
Jiyong Park9e6c2422019-08-09 20:39:45 +0900970func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
971 dirInApex = "javalib"
972 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
973 implJars := java.ImplementationJars()
974 if len(implJars) != 1 {
975 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
976 strings.Join(implJars.Strings(), ", ")))
977 }
978 fileToCopy = implJars[0]
979 return
980}
981
Jooyung Han39edb6c2019-11-06 16:53:07 +0900982func getCopyManifestForPrebuiltEtc(prebuilt android.PrebuiltEtcModule) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900983 dirInApex = filepath.Join("etc", prebuilt.SubDir())
984 fileToCopy = prebuilt.OutputFile()
985 return
986}
987
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900988func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +0900989 appDir := "app"
990 if app.Privileged() {
991 appDir = "priv-app"
992 }
993 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900994 fileToCopy = app.OutputFile()
995 return
996}
997
Dario Frenicde2a032019-10-27 00:29:22 +0100998func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
999 appDir := "app"
1000 if app.Privileged() {
1001 appDir = "priv-app"
1002 }
1003 dirInApex = filepath.Join(appDir, pkgName)
1004 fileToCopy = app.OutputFile()
1005 return
1006}
1007
Roland Levillain935639d2019-08-13 14:55:28 +01001008// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1009type flattenedApexContext struct {
1010 android.ModuleContext
1011}
1012
1013func (c *flattenedApexContext) InstallBypassMake() bool {
1014 return true
1015}
1016
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001017func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001018 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001019
Sundong Ahnabb64432019-10-22 13:58:29 +09001020 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1021 switch a.properties.ApexType {
1022 case imageApex:
1023 if buildFlattenedAsDefault {
1024 a.suffix = imageApexSuffix
1025 } else {
1026 a.suffix = ""
1027 a.primaryApexType = true
1028 }
1029 case zipApex:
1030 if proptools.String(a.properties.Payload_type) == "zip" {
1031 a.suffix = ""
1032 a.primaryApexType = true
1033 } else {
1034 a.suffix = zipApexSuffix
1035 }
1036 case flattenedApex:
1037 if buildFlattenedAsDefault {
1038 a.suffix = ""
1039 a.primaryApexType = true
1040 } else {
1041 a.suffix = flattenedSuffix
1042 }
Alex Light5098a612018-11-29 17:12:15 -08001043 }
1044
Roland Levillain630846d2019-06-26 12:48:34 +01001045 if len(a.properties.Tests) > 0 && !a.testApex {
1046 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1047 return
1048 }
1049
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001050 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1051
Jooyung Hane1633032019-08-01 17:41:43 +09001052 // native lib dependencies
1053 var provideNativeLibs []string
1054 var requireNativeLibs []string
1055
Jooyung Han5c998b92019-06-27 11:30:33 +09001056 // Check if "uses" requirements are met with dependent apexBundles
1057 var providedNativeSharedLibs []string
1058 useVendor := proptools.Bool(a.properties.Use_vendor)
1059 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1060 if ctx.OtherModuleDependencyTag(m) != usesTag {
1061 return
1062 }
1063 otherName := ctx.OtherModuleName(m)
1064 other, ok := m.(*apexBundle)
1065 if !ok {
1066 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1067 return
1068 }
1069 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1070 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1071 return
1072 }
1073 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1074 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1075 return
1076 }
1077 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1078 })
1079
Alex Light778127a2019-02-27 14:19:50 -08001080 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001081 depTag := ctx.OtherModuleDependencyTag(child)
1082 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001083 if _, ok := parent.(*apexBundle); ok {
1084 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001085 switch depTag {
1086 case sharedLibTag:
1087 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001088 if cc.HasStubsVariants() {
1089 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1090 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001091 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001092 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001093 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001094 } else {
1095 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001096 }
1097 case executableTag:
1098 if cc, ok := child.(*cc.Module); ok {
1099 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001100 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001101 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001102 } else if sh, ok := child.(*android.ShBinary); ok {
1103 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001104 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001105 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1106 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1107 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1108 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1109 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1110 // NB: Since go binaries are static we don't need the module for anything here, which is
1111 // good since the go tool is a blueprint.Module not an android.Module like we would
1112 // normally use.
1113 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001114 } else {
Alex Light778127a2019-02-27 14:19:50 -08001115 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 +09001116 }
1117 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001118 if javaLib, ok := child.(*java.Library); ok {
1119 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001120 if fileToCopy == nil {
1121 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1122 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001123 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1124 }
1125 return true
1126 } else if javaLib, ok := child.(*java.Import); ok {
1127 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1128 if fileToCopy == nil {
1129 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1130 } else {
1131 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001132 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001133 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001134 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001135 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001136 }
1137 case prebuiltTag:
Jooyung Han39edb6c2019-11-06 16:53:07 +09001138 if prebuilt, ok := child.(android.PrebuiltEtcModule); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001139 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001140 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001141 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001142 } else {
1143 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1144 }
Roland Levillain630846d2019-06-26 12:48:34 +01001145 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001146 if ccTest, ok := child.(*cc.Module); ok {
1147 if ccTest.IsTestPerSrcAllTestsVariation() {
1148 // Multiple-output test module (where `test_per_src: true`).
1149 //
1150 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1151 // We do not add this variation to `filesInfo`, as it has no output;
1152 // however, we do add the other variations of this module as indirect
1153 // dependencies (see below).
1154 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001155 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001156 // Single-output test module (where `test_per_src: false`).
1157 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1158 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001159 }
Roland Levillain630846d2019-06-26 12:48:34 +01001160 return true
1161 } else {
1162 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1163 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001164 case keyTag:
1165 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001166 a.private_key_file = key.private_key_file
1167 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001168 return false
1169 } else {
1170 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001171 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001172 case certificateTag:
1173 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001174 a.container_certificate_file = dep.Certificate.Pem
1175 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001176 return false
1177 } else {
1178 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1179 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001180 case android.PrebuiltDepTag:
1181 // If the prebuilt is force disabled, remember to delete the prebuilt file
1182 // that might have been installed in the previous builds
1183 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1184 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1185 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001186 case androidAppTag:
1187 if ap, ok := child.(*java.AndroidApp); ok {
1188 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1189 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1190 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001191 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1192 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1193 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001194 } else {
1195 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1196 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001197 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001198 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001199 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001200 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001201 // We cannot use a switch statement on `depTag` here as the checked
1202 // tags used below are private (e.g. `cc.sharedDepTag`).
1203 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1204 if cc, ok := child.(*cc.Module); ok {
1205 if android.InList(cc.Name(), providedNativeSharedLibs) {
1206 // If we're using a shared library which is provided from other APEX,
1207 // don't include it in this APEX
1208 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001209 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001210 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1211 // If the dependency is a stubs lib, don't include it in this APEX,
1212 // but make sure that the lib is installed on the device.
1213 // In case no APEX is having the lib, the lib is installed to the system
1214 // partition.
1215 //
1216 // Always include if we are a host-apex however since those won't have any
1217 // system libraries.
1218 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1219 a.externalDeps = append(a.externalDeps, cc.Name())
1220 }
Jooyung Hane1633032019-08-01 17:41:43 +09001221 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001222 // Don't track further
1223 return false
1224 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001225 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001226 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1227 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001228 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001229 } else if cc.IsTestPerSrcDepTag(depTag) {
1230 if cc, ok := child.(*cc.Module); ok {
1231 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1232 // Handle modules created as `test_per_src` variations of a single test module:
1233 // use the name of the generated test binary (`fileToCopy`) instead of the name
1234 // of the original test module (`depName`, shared by all `test_per_src`
1235 // variations of that module).
1236 moduleName := filepath.Base(fileToCopy.String())
1237 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1238 return true
1239 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001240 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001241 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001242 }
1243 }
1244 }
1245 return false
1246 })
1247
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001248 // Specific to the ART apex: dexpreopt artifacts for libcore Java libraries.
1249 // Build rules are generated by the dexpreopt singleton, and here we access build artifacts
1250 // via the global boot image config.
1251 if a.artApex {
1252 for arch, files := range java.DexpreoptedArtApexJars(ctx) {
1253 dirInApex := filepath.Join("javalib", arch.String())
1254 for _, f := range files {
1255 localModule := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
1256 filesInfo = append(filesInfo, apexFile{f, localModule, dirInApex, etc, nil, nil})
1257 }
1258 }
1259 }
1260
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001261 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001262 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1263 return
1264 }
1265
Jiyong Park8fd61922018-11-08 02:50:25 +09001266 // remove duplicates in filesInfo
1267 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001268 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001269 result := []apexFile{}
1270 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001271 dest := filepath.Join(f.installDir, f.builtFile.Base())
1272 if !encountered[dest] {
1273 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001274 result = append(result, f)
1275 }
1276 }
1277 return result
1278 }
1279 filesInfo = removeDup(filesInfo)
1280
1281 // to have consistent build rules
1282 sort.Slice(filesInfo, func(i, j int) bool {
1283 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1284 })
1285
Jiyong Park127b40b2019-09-30 16:04:35 +09001286 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001287 if !ctx.Host() {
1288 for _, fi := range filesInfo {
1289 if am, ok := fi.module.(android.ApexModule); ok {
1290 if !am.AvailableFor(ctx.ModuleName()) {
1291 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1292 return
1293 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001294 }
1295 }
1296 }
1297
Jiyong Park8fd61922018-11-08 02:50:25 +09001298 // prepend the name of this APEX to the module names. These names will be the names of
1299 // modules that will be defined if the APEX is flattened.
1300 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001301 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001302 }
1303
Jiyong Park8fd61922018-11-08 02:50:25 +09001304 a.installDir = android.PathForModuleInstall(ctx, "apex")
1305 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001306
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001307 // prepare apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001308 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
Jooyung Hane1633032019-08-01 17:41:43 +09001309 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001310
1311 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001312 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1313 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001314
1315 // apex name can be overridden
1316 optCommands := []string{}
1317 if a.properties.Apex_name != nil {
1318 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1319 }
1320
Jooyung Hane1633032019-08-01 17:41:43 +09001321 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001322 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001323 Input: manifestSrc,
1324 Output: a.manifestOut,
1325 Args: map[string]string{
1326 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1327 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001328 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001329 },
1330 })
1331
Sundong Ahnabb64432019-10-22 13:58:29 +09001332 a.setCertificateAndPrivateKey(ctx)
1333 if a.properties.ApexType == flattenedApex {
Jiyong Park23c52b02019-02-02 13:13:47 +09001334 a.buildFlattenedApex(ctx)
Sundong Ahnabb64432019-10-22 13:58:29 +09001335 } else {
1336 a.buildUnflattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001337 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001338
Sundong Ahnabb64432019-10-22 13:58:29 +09001339 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001340 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001341}
1342
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001343func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001344 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001345 for _, f := range a.filesInfo {
1346 if f.module != nil {
1347 notice := f.module.NoticeFile()
1348 if notice.Valid() {
1349 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001350 }
1351 }
1352 }
1353 // append the notice file specified in the apex module itself
1354 if a.NoticeFile().Valid() {
1355 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001356 }
1357
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001358 if len(noticeFiles) == 0 {
1359 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001360 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001361
Jaewoong Jung98772792019-07-01 17:15:13 -07001362 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001363}
1364
Sundong Ahnabb64432019-10-22 13:58:29 +09001365func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001366 var abis []string
1367 for _, target := range ctx.MultiTargets() {
1368 if len(target.Arch.Abi) > 0 {
1369 abis = append(abis, target.Arch.Abi[0])
1370 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001371 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001372
Alex Light5098a612018-11-29 17:12:15 -08001373 abis = android.FirstUniqueStrings(abis)
1374
Sundong Ahnabb64432019-10-22 13:58:29 +09001375 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001376 suffix := apexType.suffix()
1377 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001378
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001379 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001380 for _, f := range a.filesInfo {
1381 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001382 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001383
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001384 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001385 emitCommands := []string{}
1386 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1387 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001388 for i, src := range filesToCopy {
1389 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001390 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001391 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001392 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1393 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001394 for _, sym := range a.filesInfo[i].symlinks {
1395 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1396 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1397 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001398 }
Dario Frenie4235822019-10-28 14:49:27 +00001399 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1400
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001401 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001402 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001403
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001404 if a.properties.Whitelisted_files != nil {
1405 ctx.Build(pctx, android.BuildParams{
1406 Rule: emitApexContentRule,
1407 Implicits: implicitInputs,
1408 Output: imageContentFile,
1409 Description: "emit apex image content",
1410 Args: map[string]string{
1411 "emit_commands": strings.Join(emitCommands, " && "),
1412 },
1413 })
1414 implicitInputs = append(implicitInputs, imageContentFile)
1415 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1416
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001417 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001418 ctx.Build(pctx, android.BuildParams{
1419 Rule: diffApexContentRule,
1420 Implicits: implicitInputs,
1421 Output: phonyOutput,
1422 Description: "diff apex image content",
1423 Args: map[string]string{
1424 "whitelisted_files_file": whitelistedFilesFile.String(),
1425 "image_content_file": imageContentFile.String(),
1426 "apex_module_name": ctx.ModuleName(),
1427 },
1428 })
1429
1430 implicitInputs = append(implicitInputs, phonyOutput)
1431 }
1432
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001433 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1434 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001435
Sundong Ahnabb64432019-10-22 13:58:29 +09001436 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001437 // files and dirs that will be created in APEX
1438 var readOnlyPaths []string
1439 var executablePaths []string // this also includes dirs
1440 for _, f := range a.filesInfo {
1441 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001442 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001443 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001444 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001445 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001446 }
Alex Light5098a612018-11-29 17:12:15 -08001447 } else {
1448 readOnlyPaths = append(readOnlyPaths, pathInApex)
1449 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001450 dir := f.installDir
1451 for !android.InList(dir, executablePaths) && dir != "" {
1452 executablePaths = append(executablePaths, dir)
1453 dir, _ = filepath.Split(dir) // move up to the parent
1454 if len(dir) > 0 {
1455 // remove trailing slash
1456 dir = dir[:len(dir)-1]
1457 }
Alex Light5098a612018-11-29 17:12:15 -08001458 }
1459 }
1460 sort.Strings(readOnlyPaths)
1461 sort.Strings(executablePaths)
1462 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1463 ctx.Build(pctx, android.BuildParams{
1464 Rule: generateFsConfig,
1465 Output: cannedFsConfig,
1466 Description: "generate fs config",
1467 Args: map[string]string{
1468 "ro_paths": strings.Join(readOnlyPaths, " "),
1469 "exec_paths": strings.Join(executablePaths, " "),
1470 },
1471 })
1472
1473 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1474 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1475 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1476 if !fileContextsOptionalPath.Valid() {
1477 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1478 return
1479 }
1480 fileContexts := fileContextsOptionalPath.Path()
1481
Jiyong Park835d82b2018-12-27 16:04:18 +09001482 optFlags := []string{}
1483
Alex Light5098a612018-11-29 17:12:15 -08001484 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001485 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1486 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001487
Jiyong Park7f67f482019-01-05 12:57:48 +09001488 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1489 if overridden {
1490 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1491 }
1492
Jiyong Park40e26a22019-02-08 02:53:06 +09001493 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001494 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001495 implicitInputs = append(implicitInputs, androidManifestFile)
1496 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1497 }
1498
Jiyong Park71b519d2019-04-18 17:25:49 +09001499 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1500 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1501 ctx.Config().UnbundledBuild() &&
1502 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1503 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1504 apiFingerprint := java.ApiFingerprintPath(ctx)
1505 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1506 implicitInputs = append(implicitInputs, apiFingerprint)
1507 }
1508 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1509
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001510 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1511 if noticeFile.Valid() {
1512 // If there's a NOTICE file, embed it as an asset file in the APEX.
1513 implicitInputs = append(implicitInputs, noticeFile.Path())
1514 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1515 }
1516
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001517 if !ctx.Config().UnbundledBuild() && a.installable() {
1518 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1519 // don't need hashtree for activation. Therefore, by removing hashtree from
1520 // apex bundle (filesystem image in it, to be specific), we can save storage.
1521 optFlags = append(optFlags, "--no_hashtree")
1522 }
1523
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001524 if a.properties.Apex_name != nil {
1525 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1526 // Note that apex_manifest is also mended.
1527 optFlags = append(optFlags, "--do_not_check_keyname")
1528 }
1529
Alex Light5098a612018-11-29 17:12:15 -08001530 ctx.Build(pctx, android.BuildParams{
1531 Rule: apexRule,
1532 Implicits: implicitInputs,
1533 Output: unsignedOutputFile,
1534 Description: "apex (" + apexType.name() + ")",
1535 Args: map[string]string{
1536 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1537 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1538 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001539 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001540 "file_contexts": fileContexts.String(),
1541 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001542 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001543 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001544 },
1545 })
1546
1547 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1548 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1549 a.bundleModuleFile = bundleModuleFile
1550
1551 ctx.Build(pctx, android.BuildParams{
1552 Rule: apexProtoConvertRule,
1553 Input: unsignedOutputFile,
1554 Output: apexProtoFile,
1555 Description: "apex proto convert",
1556 })
1557
1558 ctx.Build(pctx, android.BuildParams{
1559 Rule: apexBundleRule,
1560 Input: apexProtoFile,
1561 Output: a.bundleModuleFile,
1562 Description: "apex bundle module",
1563 Args: map[string]string{
1564 "abi": strings.Join(abis, "."),
1565 },
1566 })
1567 } else {
1568 ctx.Build(pctx, android.BuildParams{
1569 Rule: zipApexRule,
1570 Implicits: implicitInputs,
1571 Output: unsignedOutputFile,
1572 Description: "apex (" + apexType.name() + ")",
1573 Args: map[string]string{
1574 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1575 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1576 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001577 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001578 },
1579 })
Colin Crossa4925902018-11-16 11:36:28 -08001580 }
Colin Crossa4925902018-11-16 11:36:28 -08001581
Sundong Ahnabb64432019-10-22 13:58:29 +09001582 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001583 ctx.Build(pctx, android.BuildParams{
1584 Rule: java.Signapk,
1585 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001586 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001587 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001588 Implicits: []android.Path{
1589 a.container_certificate_file,
1590 a.container_private_key_file,
1591 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001592 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001593 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001594 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001595 },
1596 })
Alex Light5098a612018-11-29 17:12:15 -08001597
1598 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001599 if a.installable() {
1600 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001601 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001602 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001603}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001604
Jiyong Park8fd61922018-11-08 02:50:25 +09001605func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001606 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1607 // reply true to `InstallBypassMake()` (thus making the call
1608 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1609 // instead of `android.PathForOutput`) to return the correct path to the flattened
1610 // APEX (as its contents is installed by Make, not Soong).
1611 factx := flattenedApexContext{ctx}
1612 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1613 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1614
1615 a.buildFilesInfo(ctx)
1616}
1617
1618func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1619 cert := String(a.properties.Certificate)
1620 if cert != "" && android.SrcIsModule(cert) == "" {
1621 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1622 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1623 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1624 } else if cert == "" {
1625 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1626 a.container_certificate_file = pem
1627 a.container_private_key_file = key
1628 }
1629}
1630
1631func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001632 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001633 // 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 +09001634 // with other ordinary files.
Sundong Ahnabb64432019-10-22 13:58:29 +09001635 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, "apex_manifest.json." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001636
Jiyong Park42cca6c2019-04-01 11:15:50 +09001637 // rename to apex_pubkey
1638 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1639 ctx.Build(pctx, android.BuildParams{
1640 Rule: android.Cp,
1641 Input: a.public_key_file,
1642 Output: copiedPubkey,
1643 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001644 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001645
Sundong Ahnabb64432019-10-22 13:58:29 +09001646 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001647 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001648 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001649 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001650 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1651 for _, sym := range fi.symlinks {
1652 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1653 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001654 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001655 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001656 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001657}
1658
1659func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001660 if a.properties.HideFromMake {
1661 return android.AndroidMkData{
1662 Disabled: true,
1663 }
1664 }
Alex Light5098a612018-11-29 17:12:15 -08001665 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001666 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001667 return android.AndroidMkData{
1668 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1669 for _, data := range writers {
1670 data.Custom(w, name, prefix, moduleDir, data)
1671 }
1672 }}
1673}
1674
Sundong Ahnabb64432019-10-22 13:58:29 +09001675func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001676 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001677 apexType := a.properties.ApexType
1678 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1679 // to install symbol files in $(PRODUCT_OUT}/apex.
1680 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1681 if !a.primaryApexType && apexType != flattenedApex {
1682 return moduleNames
1683 }
Jiyong Park94427262019-02-05 23:18:47 +09001684
1685 for _, fi := range a.filesInfo {
1686 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1687 continue
1688 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001689
1690 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001691 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001692 }
1693
Jiyong Park94427262019-02-05 23:18:47 +09001694 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1695 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001696 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001697 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001698 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001699 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001700 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001701 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001702 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001703 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001704 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1705 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001706 if len(fi.symlinks) > 0 {
1707 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1708 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001709
1710 if fi.module != nil && fi.module.NoticeFile().Valid() {
1711 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1712 }
Jiyong Park94427262019-02-05 23:18:47 +09001713 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001714 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001715 }
1716 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1717 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1718 if fi.module != nil {
1719 archStr := fi.module.Target().Arch.ArchType.String()
1720 host := false
1721 switch fi.module.Target().Os.Class {
1722 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001723 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001724 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1725 }
1726 host = true
1727 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001728 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001729 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1730 }
1731 host = true
1732 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001733 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001734 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1735 }
1736 }
1737 if host {
1738 makeOs := fi.module.Target().Os.String()
1739 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1740 makeOs = "linux"
1741 }
1742 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1743 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1744 }
1745 }
1746 if fi.class == javaSharedLib {
1747 javaModule := fi.module.(*java.Library)
1748 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1749 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1750 // we will have foo.jar.jar
1751 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1752 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1753 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1754 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1755 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1756 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001757 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001758 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001759 if cc, ok := fi.module.(*cc.Module); ok {
1760 if cc.UnstrippedOutputFile() != nil {
1761 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1762 }
1763 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001764 if cc.CoverageOutputFile().Valid() {
1765 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1766 }
Jiyong Park94427262019-02-05 23:18:47 +09001767 }
1768 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1769 } else {
1770 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001771 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001772 if a.primaryApexType && fi.builtFile.Base() == "apex_manifest.json" && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001773 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1774 }
Jiyong Park94427262019-02-05 23:18:47 +09001775 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1776 }
1777 }
1778 return moduleNames
1779}
1780
Sundong Ahnabb64432019-10-22 13:58:29 +09001781func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001782 return android.AndroidMkData{
1783 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1784 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001785 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001786 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001787 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001788 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001789 }
1790
Sundong Ahnabb64432019-10-22 13:58:29 +09001791 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001792 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001793 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1794 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001795 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001796 if len(moduleNames) > 0 {
1797 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1798 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001799 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001800 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001801
Sundong Ahnabb64432019-10-22 13:58:29 +09001802 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001803 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1804 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001805 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001806 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001807 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001808 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001809 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001810 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001811 if len(moduleNames) > 0 {
1812 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1813 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001814 if len(a.externalDeps) > 0 {
1815 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1816 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001817 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001818 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001819 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001820 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001821 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001822 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1823 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1824 if len(postInstallCommands) > 0 {
1825 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1826 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001827 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001828
Alex Light5098a612018-11-29 17:12:15 -08001829 if apexType == imageApex {
1830 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1831 }
Jiyong Park719b4462019-01-13 00:39:51 +09001832 }
1833 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001834}
1835
Jooyung Han344d5432019-08-23 11:17:39 +09001836func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001837 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001838 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001839 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001840 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001841 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1842 })
Alex Light5098a612018-11-29 17:12:15 -08001843 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001844 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001845 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001846 return module
1847}
Jiyong Park30ca9372019-02-07 16:27:23 +09001848
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001849func ApexBundleFactory(testApex bool, artApex bool) android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001850 bundle := newApexBundle()
1851 bundle.testApex = testApex
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001852 bundle.artApex = artApex
Jooyung Han344d5432019-08-23 11:17:39 +09001853 return bundle
1854}
1855
1856func testApexBundleFactory() android.Module {
1857 bundle := newApexBundle()
1858 bundle.testApex = true
1859 return bundle
1860}
1861
Jiyong Parkd1063c12019-07-17 20:08:41 +09001862func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001863 return newApexBundle()
1864}
1865
1866// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1867// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1868// If not specified, then the "current" versions are gathered.
1869func vndkApexBundleFactory() android.Module {
1870 bundle := newApexBundle()
1871 bundle.vndkApex = true
1872 bundle.AddProperties(&bundle.vndkProperties)
1873 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1874 ctx.AppendProperties(&struct {
1875 Compile_multilib *string
1876 }{
1877 proptools.StringPtr("both"),
1878 })
1879 })
1880 return bundle
1881}
1882
Jooyung Han31c470b2019-10-18 16:26:59 +09001883func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1884 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1885 if vndkVersion == "current" {
1886 vndkVersion = config.PlatformVndkVersion()
1887 }
1888 return vndkVersion
1889}
1890
Jiyong Park30ca9372019-02-07 16:27:23 +09001891//
1892// Defaults
1893//
1894type Defaults struct {
1895 android.ModuleBase
1896 android.DefaultsModuleBase
1897}
1898
Jiyong Park30ca9372019-02-07 16:27:23 +09001899func defaultsFactory() android.Module {
1900 return DefaultsFactory()
1901}
1902
1903func DefaultsFactory(props ...interface{}) android.Module {
1904 module := &Defaults{}
1905
1906 module.AddProperties(props...)
1907 module.AddProperties(
1908 &apexBundleProperties{},
1909 &apexTargetBundleProperties{},
1910 )
1911
1912 android.InitDefaultsModule(module)
1913 return module
1914}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001915
1916//
1917// Prebuilt APEX
1918//
1919type Prebuilt struct {
1920 android.ModuleBase
1921 prebuilt android.Prebuilt
1922
1923 properties PrebuiltProperties
1924
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001925 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001926 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001927 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001928 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001929}
1930
1931type PrebuiltProperties struct {
1932 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001933 Source string `blueprint:"mutated"`
1934 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001935
1936 Src *string
1937 Arch struct {
1938 Arm struct {
1939 Src *string
1940 }
1941 Arm64 struct {
1942 Src *string
1943 }
1944 X86 struct {
1945 Src *string
1946 }
1947 X86_64 struct {
1948 Src *string
1949 }
1950 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001951
1952 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001953 // Optional name for the installed apex. If unspecified, name of the
1954 // module is used as the file name
1955 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001956
1957 // Names of modules to be overridden. Listed modules can only be other binaries
1958 // (in Make or Soong).
1959 // This does not completely prevent installation of the overridden binaries, but if both
1960 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1961 // from PRODUCT_PACKAGES.
1962 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001963}
1964
1965func (p *Prebuilt) installable() bool {
1966 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001967}
1968
1969func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001970 // If the device is configured to use flattened APEX, force disable the prebuilt because
1971 // the prebuilt is a non-flattened one.
1972 forceDisable := ctx.Config().FlattenApex()
1973
1974 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1975 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001976 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001977
Kun Niu10c9f832019-07-29 16:28:57 -07001978 // Force disable the prebuilts when coverage is enabled.
1979 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1980 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1981
Jiyong Park50b81e52019-07-11 11:24:41 +09001982 // b/137216042 don't use prebuilts when address sanitizer is on
1983 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1984 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1985
1986 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001987 p.properties.ForceDisable = true
1988 return
1989 }
1990
Jiyong Parkc95714e2019-03-29 14:23:10 +09001991 // This is called before prebuilt_select and prebuilt_postdeps mutators
1992 // The mutators requires that src to be set correctly for each arch so that
1993 // arch variants are disabled when src is not provided for the arch.
1994 if len(ctx.MultiTargets()) != 1 {
1995 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1996 return
1997 }
1998 var src string
1999 switch ctx.MultiTargets()[0].Arch.ArchType {
2000 case android.Arm:
2001 src = String(p.properties.Arch.Arm.Src)
2002 case android.Arm64:
2003 src = String(p.properties.Arch.Arm64.Src)
2004 case android.X86:
2005 src = String(p.properties.Arch.X86.Src)
2006 case android.X86_64:
2007 src = String(p.properties.Arch.X86_64.Src)
2008 default:
2009 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
2010 return
2011 }
2012 if src == "" {
2013 src = String(p.properties.Src)
2014 }
2015 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002016}
2017
Jiyong Park03b68dd2019-07-26 23:20:40 +09002018func (p *Prebuilt) isForceDisabled() bool {
2019 return p.properties.ForceDisable
2020}
2021
Colin Cross41955e82019-05-29 14:40:35 -07002022func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2023 switch tag {
2024 case "":
2025 return android.Paths{p.outputApex}, nil
2026 default:
2027 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2028 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002029}
2030
Jiyong Park4d277042019-04-23 18:00:10 +09002031func (p *Prebuilt) InstallFilename() string {
2032 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2033}
2034
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002035func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002036 if p.properties.ForceDisable {
2037 return
2038 }
2039
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002040 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002041 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002042 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002043 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002044 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2045 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2046 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002047 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2048 ctx.Build(pctx, android.BuildParams{
2049 Rule: android.Cp,
2050 Input: p.inputApex,
2051 Output: p.outputApex,
2052 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002053 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002054 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002055 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002056
2057 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002058}
2059
2060func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2061 return &p.prebuilt
2062}
2063
2064func (p *Prebuilt) Name() string {
2065 return p.prebuilt.Name(p.ModuleBase.Name())
2066}
2067
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002068func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2069 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002070 Class: "ETC",
2071 OutputFile: android.OptionalPathForPath(p.inputApex),
2072 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002073 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2074 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002075 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002076 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2077 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2078 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2079 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002080 },
2081 }
2082}
2083
2084// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2085func PrebuiltFactory() android.Module {
2086 module := &Prebuilt{}
2087 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002088 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002089 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002090 return module
2091}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002092
2093func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2094 // small helper to add symlink commands
2095 addSymlink := func(target, dir, linkName string) {
2096 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2097 link := filepath.Join(outDir, linkName)
2098 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2099 }
2100
2101 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2102 // When all hard-coded references are fixed, remove symbolic links
2103 // Note that we should keep following symlinks for older VNDKs (<=29)
2104 // Since prebuilt vndk libs still depend on system/lib/vndk path
2105 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2106 // the name of vndk apex is formatted "com.android.vndk.v" + version
2107 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2108 if ctx.Config().Android64() {
2109 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2110 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2111 }
2112 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2113 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2114 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2115 }
2116 }
2117 return
2118}