blob: 4e6827f6273d8719b1aa25de1a72c7d91a837aeb [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 }
252 }
253}
254
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900255// Mark the direct and transitive dependencies of apex bundles so that they
256// can be built for the apex bundles.
257func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800258 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800259 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900260 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900261 depName := mctx.OtherModuleName(child)
262 // If the parent is apexBundle, this child is directly depended.
263 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800264 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800265 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
266 // non-installable apex's cannot be installed and so should not prevent libraries from being
267 // installed to the system.
268 android.UpdateApexDependency(apexBundleName, depName, directDep)
269 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900270
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900271 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900272 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900273 return true
274 } else {
275 return false
276 }
277 })
278 }
279}
280
281// Create apex variations if a module is included in APEX(s).
282func apexMutator(mctx android.BottomUpMutatorContext) {
283 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900284 am.CreateApexVariations(mctx)
Jooyung Han7a78a922019-10-08 21:59:58 +0900285 } else if a, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900286 // apex bundle itself is mutated so that it and its modules have same
287 // apex variant.
288 apexBundleName := mctx.ModuleName()
289 mctx.CreateVariations(apexBundleName)
Jooyung Han7a78a922019-10-08 21:59:58 +0900290
291 // collects APEX list
292 if mctx.Device() && a.installable() {
293 addApexFileContextsInfos(mctx, a)
294 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900295 }
296}
Sundong Ahne9b55722019-09-06 17:37:42 +0900297
Jooyung Han7a78a922019-10-08 21:59:58 +0900298var (
299 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
300 apexFileContextsInfosMutex sync.Mutex
301)
302
303func apexFileContextsInfos(config android.Config) *[]string {
304 return config.Once(apexFileContextsInfosKey, func() interface{} {
305 return &[]string{}
306 }).(*[]string)
307}
308
309func addApexFileContextsInfos(ctx android.BaseModuleContext, a *apexBundle) {
310 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
311 fileContextsName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
312
313 apexFileContextsInfosMutex.Lock()
314 defer apexFileContextsInfosMutex.Unlock()
315 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
316 *apexFileContextsInfos = append(*apexFileContextsInfos, apexName+":"+fileContextsName)
317}
318
Sundong Ahne9b55722019-09-06 17:37:42 +0900319func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900320 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +0900321 var variants []string
322 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
323 case "image":
324 variants = append(variants, imageApexType, flattenedApexType)
325 case "zip":
326 variants = append(variants, zipApexType)
327 case "both":
328 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
329 default:
330 mctx.PropertyErrorf("type", "%q is not one of \"image\" or \"zip\".", *ab.properties.Payload_type)
331 return
332 }
333
334 modules := mctx.CreateLocalVariations(variants...)
335
336 for i, v := range variants {
337 switch v {
338 case imageApexType:
339 modules[i].(*apexBundle).properties.ApexType = imageApex
340 case zipApexType:
341 modules[i].(*apexBundle).properties.ApexType = zipApex
342 case flattenedApexType:
343 modules[i].(*apexBundle).properties.ApexType = flattenedApex
344 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900345 }
346 }
347}
348
Jooyung Han5c998b92019-06-27 11:30:33 +0900349func apexUsesMutator(mctx android.BottomUpMutatorContext) {
350 if ab, ok := mctx.Module().(*apexBundle); ok {
351 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
352 }
353}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900354
Jooyung Handc782442019-11-01 03:14:38 +0900355var (
356 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
357)
358
359// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
360// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
361// which may cause compatibility issues. (e.g. libbinder)
362// Even though libbinder restricts its availability via 'apex_available' property and relies on
363// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
364// to avoid similar problems.
365func useVendorWhitelist(config android.Config) []string {
366 return config.Once(useVendorWhitelistKey, func() interface{} {
367 return []string{
368 // swcodec uses "vendor" variants for smaller size
369 "com.android.media.swcodec",
370 "test_com.android.media.swcodec",
371 }
372 }).([]string)
373}
374
375// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
376// called before the first call to useVendorWhitelist()
377func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
378 config.Once(useVendorWhitelistKey, func() interface{} {
379 return whitelist
380 })
381}
382
Alex Light9670d332019-01-29 18:07:33 -0800383type apexNativeDependencies struct {
384 // List of native libraries
385 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900386
Alex Light9670d332019-01-29 18:07:33 -0800387 // List of native executables
388 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900389
Roland Levillain630846d2019-06-26 12:48:34 +0100390 // List of native tests
391 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800392}
Jooyung Han344d5432019-08-23 11:17:39 +0900393
Alex Light9670d332019-01-29 18:07:33 -0800394type apexMultilibProperties struct {
395 // Native dependencies whose compile_multilib is "first"
396 First apexNativeDependencies
397
398 // Native dependencies whose compile_multilib is "both"
399 Both apexNativeDependencies
400
401 // Native dependencies whose compile_multilib is "prefer32"
402 Prefer32 apexNativeDependencies
403
404 // Native dependencies whose compile_multilib is "32"
405 Lib32 apexNativeDependencies
406
407 // Native dependencies whose compile_multilib is "64"
408 Lib64 apexNativeDependencies
409}
410
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900411type apexBundleProperties struct {
412 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000413 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800414 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900415
Jiyong Park40e26a22019-02-08 02:53:06 +0900416 // AndroidManifest.xml file used for the zip container of this APEX bundle.
417 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800418 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900419
Roland Levillain411c5842019-09-19 16:37:20 +0100420 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
421 // device (/apex/<apex_name>).
422 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900423 Apex_name *string
424
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900425 // Determines the file contexts file for setting security context to each file in this APEX bundle.
426 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
427 // used.
428 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900429 File_contexts *string
430
431 // List of native shared libs that are embedded inside this APEX bundle
432 Native_shared_libs []string
433
Roland Levillain630846d2019-06-26 12:48:34 +0100434 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900435 Binaries []string
436
437 // List of java libraries that are embedded inside this APEX bundle
438 Java_libs []string
439
440 // List of prebuilt files that are embedded inside this APEX bundle
441 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900442
Roland Levillain630846d2019-06-26 12:48:34 +0100443 // List of tests that are embedded inside this APEX bundle
444 Tests []string
445
Jiyong Parkff1458f2018-10-12 21:49:38 +0900446 // Name of the apex_key module that provides the private key to sign APEX
447 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900448
Alex Light5098a612018-11-29 17:12:15 -0800449 // The type of APEX to build. Controls what the APEX payload is. Either
450 // 'image', 'zip' or 'both'. Default: 'image'.
451 Payload_type *string
452
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900453 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
454 // or an android_app_certificate module name in the form ":module".
455 Certificate *string
456
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900457 // Whether this APEX is installable to one of the partitions. Default: true.
458 Installable *bool
459
Jiyong Parkda6eb592018-12-19 17:12:36 +0900460 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
461 // Default is false.
462 Use_vendor *bool
463
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800464 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
465 Ignore_system_library_special_case *bool
466
Alex Light9670d332019-01-29 18:07:33 -0800467 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900468
Jiyong Parkf97782b2019-02-13 20:28:58 +0900469 // List of sanitizer names that this APEX is enabled for
470 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900471
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900472 PreventInstall bool `blueprint:"mutated"`
473
474 HideFromMake bool `blueprint:"mutated"`
475
Jooyung Han5c998b92019-06-27 11:30:33 +0900476 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
477 Provide_cpp_shared_libs *bool
478
479 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
480 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100481
482 // A txt file containing list of files that are whitelisted to be included in this APEX.
483 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900484
485 // List of APKs to package inside APEX
486 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900487
Sundong Ahnabb64432019-10-22 13:58:29 +0900488 // package format of this apex variant; could be non-flattened, flattened, or zip.
489 // imageApex, zipApex or flattened
490 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900491
Jiyong Parkd1063c12019-07-17 20:08:41 +0900492 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
493 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
494 // is implied. This value affects all modules included in this APEX. In other words, they are
495 // also built with the SDKs specified here.
496 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800497}
498
499type apexTargetBundleProperties struct {
500 Target struct {
501 // Multilib properties only for android.
502 Android struct {
503 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900504 }
Jooyung Han344d5432019-08-23 11:17:39 +0900505
Alex Light9670d332019-01-29 18:07:33 -0800506 // Multilib properties only for host.
507 Host struct {
508 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900509 }
Jooyung Han344d5432019-08-23 11:17:39 +0900510
Alex Light9670d332019-01-29 18:07:33 -0800511 // Multilib properties only for host linux_bionic.
512 Linux_bionic struct {
513 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900514 }
Jooyung Han344d5432019-08-23 11:17:39 +0900515
Alex Light9670d332019-01-29 18:07:33 -0800516 // Multilib properties only for host linux_glibc.
517 Linux_glibc struct {
518 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900519 }
520 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900521}
522
Jooyung Han344d5432019-08-23 11:17:39 +0900523type apexVndkProperties struct {
524 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
525 Vndk_version *string
526}
527
Jiyong Park8fd61922018-11-08 02:50:25 +0900528type apexFileClass int
529
530const (
531 etc apexFileClass = iota
532 nativeSharedLib
533 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900534 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800535 pyBinary
536 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900537 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100538 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900539 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900540)
541
Alex Light5098a612018-11-29 17:12:15 -0800542type apexPackaging int
543
544const (
545 imageApex apexPackaging = iota
546 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900547 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800548)
549
Sundong Ahnabb64432019-10-22 13:58:29 +0900550// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800551func (a apexPackaging) suffix() string {
552 switch a {
553 case imageApex:
554 return imageApexSuffix
555 case zipApex:
556 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800557 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100558 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800559 }
560}
561
562func (a apexPackaging) name() string {
563 switch a {
564 case imageApex:
565 return imageApexType
566 case zipApex:
567 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800568 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100569 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800570 }
571}
572
Jiyong Park8fd61922018-11-08 02:50:25 +0900573func (class apexFileClass) NameInMake() string {
574 switch class {
575 case etc:
576 return "ETC"
577 case nativeSharedLib:
578 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800579 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900580 return "EXECUTABLES"
581 case javaSharedLib:
582 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100583 case nativeTest:
584 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900585 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900586 // b/142537672 Why isn't this APP? We want to have full control over
587 // the paths and file names of the apk file under the flattend APEX.
588 // If this is set to APP, then the paths and file names are modified
589 // by the Make build system. For example, it is installed to
590 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
591 // /system/apex/<apexname>/app/<Appname> because the build system automatically
592 // appends module name (which is <apexname>.<Appname> to the path.
593 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900594 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100595 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900596 }
597}
598
599type apexFile struct {
600 builtFile android.Path
601 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900602 installDir string
603 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900604 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800605 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900606}
607
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900608type apexBundle struct {
609 android.ModuleBase
610 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900611 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900612
Alex Light9670d332019-01-29 18:07:33 -0800613 properties apexBundleProperties
614 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900615 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900616
Colin Crossa4925902018-11-16 11:36:28 -0800617 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900618 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700619 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900620
Jiyong Park03b68dd2019-07-26 23:20:40 +0900621 prebuiltFileToDelete string
622
Jiyong Park42cca6c2019-04-01 11:15:50 +0900623 public_key_file android.Path
624 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900625
626 container_certificate_file android.Path
627 container_private_key_file android.Path
628
Jiyong Park8fd61922018-11-08 02:50:25 +0900629 // list of files to be included in this apex
630 filesInfo []apexFile
631
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900632 // list of module names that this APEX is depending on
633 externalDeps []string
634
Sundong Ahnabb64432019-10-22 13:58:29 +0900635 testApex bool
636 vndkApex bool
637 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900638
639 // intermediate path for apex_manifest.json
640 manifestOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900641
642 // list of commands to create symlinks for backward compatibility
643 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
644 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
645 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
646 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900647
648 // Suffix of module name in Android.mk
649 // ".flattened", ".apex", ".zipapex", or ""
650 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900651}
652
Jiyong Park397e55e2018-10-24 21:09:55 +0900653func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100654 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700655 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900656 // Use *FarVariation* to be able to depend on modules having
657 // conflicting variations with this module. This is required since
658 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
659 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700660 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900661 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900662 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900663 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700664 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900665
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700666 ctx.AddFarVariationDependencies(append(target.Variations(),
667 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
668 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100669
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700670 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100671 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100672 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700673 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900674}
675
Alex Light9670d332019-01-29 18:07:33 -0800676func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
677 if ctx.Os().Class == android.Device {
678 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
679 } else {
680 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
681 if ctx.Os().Bionic() {
682 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
683 } else {
684 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
685 }
686 }
687}
688
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900689func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900690 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
691 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
692 }
693
Jiyong Park397e55e2018-10-24 21:09:55 +0900694 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900695 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800696
697 a.combineProperties(ctx)
698
Jiyong Park397e55e2018-10-24 21:09:55 +0900699 has32BitTarget := false
700 for _, target := range targets {
701 if target.Arch.ArchType.Multilib == "lib32" {
702 has32BitTarget = true
703 }
704 }
705 for i, target := range targets {
706 // When multilib.* is omitted for native_shared_libs, it implies
707 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700708 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900709 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900710 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700711 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900712
Roland Levillain630846d2019-06-26 12:48:34 +0100713 // When multilib.* is omitted for tests, it implies
714 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700715 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100716 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100717 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700718 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100719
Jiyong Park397e55e2018-10-24 21:09:55 +0900720 // Add native modules targetting both ABIs
721 addDependenciesForNativeModules(ctx,
722 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100723 a.properties.Multilib.Both.Binaries,
724 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700725 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900726 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900727
Alex Light3d673592019-01-18 14:37:31 -0800728 isPrimaryAbi := i == 0
729 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900730 // When multilib.* is omitted for binaries, it implies
731 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700732 ctx.AddFarVariationDependencies(append(target.Variations(),
733 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
734 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900735
736 // Add native modules targetting the first ABI
737 addDependenciesForNativeModules(ctx,
738 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100739 a.properties.Multilib.First.Binaries,
740 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700741 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900742 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800743
744 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700745 ctx.AddFarVariationDependencies(target.Variations(),
746 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900747 }
748
749 switch target.Arch.ArchType.Multilib {
750 case "lib32":
751 // Add native modules targetting 32-bit ABI
752 addDependenciesForNativeModules(ctx,
753 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100754 a.properties.Multilib.Lib32.Binaries,
755 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700756 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900757 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900758
759 addDependenciesForNativeModules(ctx,
760 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100761 a.properties.Multilib.Prefer32.Binaries,
762 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700763 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900764 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900765 case "lib64":
766 // Add native modules targetting 64-bit ABI
767 addDependenciesForNativeModules(ctx,
768 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100769 a.properties.Multilib.Lib64.Binaries,
770 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700771 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900772 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900773
774 if !has32BitTarget {
775 addDependenciesForNativeModules(ctx,
776 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100777 a.properties.Multilib.Prefer32.Binaries,
778 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700779 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900780 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900781 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700782
783 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
784 for _, sanitizer := range ctx.Config().SanitizeDevice() {
785 if sanitizer == "hwaddress" {
786 addDependenciesForNativeModules(ctx,
787 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700788 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700789 break
790 }
791 }
792 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900793 }
794
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900795 }
796
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700797 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
798 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900799
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700800 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
801 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900802
Jiyong Park23c52b02019-02-02 13:13:47 +0900803 if String(a.properties.Key) == "" {
804 ctx.ModuleErrorf("key is missing")
805 return
806 }
807 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900808
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900809 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900810 if cert != "" {
811 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900812 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900813
814 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
815 if len(a.properties.Uses_sdks) > 0 {
816 sdkRefs := []android.SdkRef{}
817 for _, str := range a.properties.Uses_sdks {
818 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
819 sdkRefs = append(sdkRefs, parsed)
820 }
821 a.BuildWithSdks(sdkRefs)
822 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900823}
824
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900825func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
826 // direct deps of an APEX bundle are all part of the APEX bundle
827 return true
828}
829
Colin Cross0ea8ba82019-06-06 14:33:29 -0700830func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900831 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
832 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000833 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900834 }
835 return String(a.properties.Certificate)
836}
837
Colin Cross41955e82019-05-29 14:40:35 -0700838func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
839 switch tag {
840 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900841 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700842 default:
843 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900844 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900845}
846
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900847func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900848 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900849}
850
Jiyong Park7c1dc612019-01-05 11:15:24 +0900851func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900852 if a.vndkApex {
853 return "vendor." + a.vndkVersion(config)
854 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900855 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900856 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900857 } else {
858 return "core"
859 }
860}
861
Jiyong Parkf97782b2019-02-13 20:28:58 +0900862func (a *apexBundle) EnableSanitizer(sanitizerName string) {
863 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
864 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
865 }
866}
867
Jiyong Park388ef3f2019-01-28 19:47:32 +0900868func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900869 if android.InList(sanitizerName, a.properties.SanitizerNames) {
870 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900871 }
872
873 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900874 globalSanitizerNames := []string{}
875 if a.Host() {
876 globalSanitizerNames = ctx.Config().SanitizeHost()
877 } else {
878 arches := ctx.Config().SanitizeDeviceArch()
879 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
880 globalSanitizerNames = ctx.Config().SanitizeDevice()
881 }
882 }
883 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900884}
885
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900886func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
887 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
888}
889
890func (a *apexBundle) PreventInstall() {
891 a.properties.PreventInstall = true
892}
893
894func (a *apexBundle) HideFromMake() {
895 a.properties.HideFromMake = true
896}
897
Martin Stjernholm279de572019-09-10 23:18:20 +0100898func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900899 // Decide the APEX-local directory by the multilib of the library
900 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100901 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900902 case "lib32":
903 dirInApex = "lib"
904 case "lib64":
905 dirInApex = "lib64"
906 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100907 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700908 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100909 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900910 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100911 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
912 // Special case for Bionic libs and other libs installed with them. This is
913 // to prevent those libs from being included in the search path
914 // /apex/com.android.runtime/${LIB}. This exclusion is required because
915 // those libs in the Runtime APEX are available via the legacy paths in
916 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
917 // to the legacy paths and thus will be loaded into the default linker
918 // namespace (aka "platform" namespace). If the libs are directly in
919 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
920 // into the runtime linker namespace, which will result in double loading of
921 // them, which isn't supported.
922 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900923 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900924
Martin Stjernholm279de572019-09-10 23:18:20 +0100925 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900926 return
927}
928
929func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900930 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700931 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200932 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900933 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900934 fileToCopy = cc.OutputFile().Path()
935 return
936}
937
Alex Light778127a2019-02-27 14:19:50 -0800938func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
939 dirInApex = "bin"
940 fileToCopy = py.HostToolPath().Path()
941 return
942}
943func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
944 dirInApex = "bin"
945 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
946 if err != nil {
947 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
948 return
949 }
950 fileToCopy = android.PathForOutput(ctx, s)
951 return
952}
953
Jiyong Park04480cf2019-02-06 00:16:29 +0900954func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
955 dirInApex = filepath.Join("bin", sh.SubDir())
956 fileToCopy = sh.OutputFile()
957 return
958}
959
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900960func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
961 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900962 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900963 return
964}
965
Jiyong Park9e6c2422019-08-09 20:39:45 +0900966func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
967 dirInApex = "javalib"
968 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
969 implJars := java.ImplementationJars()
970 if len(implJars) != 1 {
971 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
972 strings.Join(implJars.Strings(), ", ")))
973 }
974 fileToCopy = implJars[0]
975 return
976}
977
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900978func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
979 dirInApex = filepath.Join("etc", prebuilt.SubDir())
980 fileToCopy = prebuilt.OutputFile()
981 return
982}
983
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900984func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +0900985 appDir := "app"
986 if app.Privileged() {
987 appDir = "priv-app"
988 }
989 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900990 fileToCopy = app.OutputFile()
991 return
992}
993
Dario Frenicde2a032019-10-27 00:29:22 +0100994func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
995 appDir := "app"
996 if app.Privileged() {
997 appDir = "priv-app"
998 }
999 dirInApex = filepath.Join(appDir, pkgName)
1000 fileToCopy = app.OutputFile()
1001 return
1002}
1003
Roland Levillain935639d2019-08-13 14:55:28 +01001004// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1005type flattenedApexContext struct {
1006 android.ModuleContext
1007}
1008
1009func (c *flattenedApexContext) InstallBypassMake() bool {
1010 return true
1011}
1012
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001013func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001014 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001015
Sundong Ahnabb64432019-10-22 13:58:29 +09001016 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1017 switch a.properties.ApexType {
1018 case imageApex:
1019 if buildFlattenedAsDefault {
1020 a.suffix = imageApexSuffix
1021 } else {
1022 a.suffix = ""
1023 a.primaryApexType = true
1024 }
1025 case zipApex:
1026 if proptools.String(a.properties.Payload_type) == "zip" {
1027 a.suffix = ""
1028 a.primaryApexType = true
1029 } else {
1030 a.suffix = zipApexSuffix
1031 }
1032 case flattenedApex:
1033 if buildFlattenedAsDefault {
1034 a.suffix = ""
1035 a.primaryApexType = true
1036 } else {
1037 a.suffix = flattenedSuffix
1038 }
Alex Light5098a612018-11-29 17:12:15 -08001039 }
1040
Roland Levillain630846d2019-06-26 12:48:34 +01001041 if len(a.properties.Tests) > 0 && !a.testApex {
1042 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1043 return
1044 }
1045
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001046 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1047
Jooyung Hane1633032019-08-01 17:41:43 +09001048 // native lib dependencies
1049 var provideNativeLibs []string
1050 var requireNativeLibs []string
1051
Jooyung Han5c998b92019-06-27 11:30:33 +09001052 // Check if "uses" requirements are met with dependent apexBundles
1053 var providedNativeSharedLibs []string
1054 useVendor := proptools.Bool(a.properties.Use_vendor)
1055 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1056 if ctx.OtherModuleDependencyTag(m) != usesTag {
1057 return
1058 }
1059 otherName := ctx.OtherModuleName(m)
1060 other, ok := m.(*apexBundle)
1061 if !ok {
1062 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1063 return
1064 }
1065 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1066 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1067 return
1068 }
1069 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1070 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1071 return
1072 }
1073 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1074 })
1075
Alex Light778127a2019-02-27 14:19:50 -08001076 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001077 depTag := ctx.OtherModuleDependencyTag(child)
1078 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001079 if _, ok := parent.(*apexBundle); ok {
1080 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001081 switch depTag {
1082 case sharedLibTag:
1083 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001084 if cc.HasStubsVariants() {
1085 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1086 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001087 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001088 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001089 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001090 } else {
1091 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001092 }
1093 case executableTag:
1094 if cc, ok := child.(*cc.Module); ok {
1095 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001096 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001097 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001098 } else if sh, ok := child.(*android.ShBinary); ok {
1099 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001100 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001101 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1102 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1103 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1104 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1105 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1106 // NB: Since go binaries are static we don't need the module for anything here, which is
1107 // good since the go tool is a blueprint.Module not an android.Module like we would
1108 // normally use.
1109 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001110 } else {
Alex Light778127a2019-02-27 14:19:50 -08001111 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 +09001112 }
1113 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001114 if javaLib, ok := child.(*java.Library); ok {
1115 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001116 if fileToCopy == nil {
1117 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1118 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001119 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1120 }
1121 return true
1122 } else if javaLib, ok := child.(*java.Import); ok {
1123 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1124 if fileToCopy == nil {
1125 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1126 } else {
1127 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001128 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001129 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001130 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001131 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001132 }
1133 case prebuiltTag:
1134 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1135 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001136 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001137 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001138 } else {
1139 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1140 }
Roland Levillain630846d2019-06-26 12:48:34 +01001141 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001142 if ccTest, ok := child.(*cc.Module); ok {
1143 if ccTest.IsTestPerSrcAllTestsVariation() {
1144 // Multiple-output test module (where `test_per_src: true`).
1145 //
1146 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1147 // We do not add this variation to `filesInfo`, as it has no output;
1148 // however, we do add the other variations of this module as indirect
1149 // dependencies (see below).
1150 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001151 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001152 // Single-output test module (where `test_per_src: false`).
1153 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1154 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001155 }
Roland Levillain630846d2019-06-26 12:48:34 +01001156 return true
1157 } else {
1158 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1159 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001160 case keyTag:
1161 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001162 a.private_key_file = key.private_key_file
1163 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001164 return false
1165 } else {
1166 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001167 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001168 case certificateTag:
1169 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001170 a.container_certificate_file = dep.Certificate.Pem
1171 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001172 return false
1173 } else {
1174 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1175 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001176 case android.PrebuiltDepTag:
1177 // If the prebuilt is force disabled, remember to delete the prebuilt file
1178 // that might have been installed in the previous builds
1179 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1180 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1181 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001182 case androidAppTag:
1183 if ap, ok := child.(*java.AndroidApp); ok {
1184 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1185 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1186 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001187 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1188 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1189 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001190 } else {
1191 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1192 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001193 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001194 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001195 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001196 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001197 // We cannot use a switch statement on `depTag` here as the checked
1198 // tags used below are private (e.g. `cc.sharedDepTag`).
1199 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1200 if cc, ok := child.(*cc.Module); ok {
1201 if android.InList(cc.Name(), providedNativeSharedLibs) {
1202 // If we're using a shared library which is provided from other APEX,
1203 // don't include it in this APEX
1204 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001205 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001206 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1207 // If the dependency is a stubs lib, don't include it in this APEX,
1208 // but make sure that the lib is installed on the device.
1209 // In case no APEX is having the lib, the lib is installed to the system
1210 // partition.
1211 //
1212 // Always include if we are a host-apex however since those won't have any
1213 // system libraries.
1214 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1215 a.externalDeps = append(a.externalDeps, cc.Name())
1216 }
Jooyung Hane1633032019-08-01 17:41:43 +09001217 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001218 // Don't track further
1219 return false
1220 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001221 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001222 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1223 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001224 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001225 } else if cc.IsTestPerSrcDepTag(depTag) {
1226 if cc, ok := child.(*cc.Module); ok {
1227 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1228 // Handle modules created as `test_per_src` variations of a single test module:
1229 // use the name of the generated test binary (`fileToCopy`) instead of the name
1230 // of the original test module (`depName`, shared by all `test_per_src`
1231 // variations of that module).
1232 moduleName := filepath.Base(fileToCopy.String())
1233 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1234 return true
1235 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001236 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001237 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001238 }
1239 }
1240 }
1241 return false
1242 })
1243
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001244 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001245 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1246 return
1247 }
1248
Jiyong Park8fd61922018-11-08 02:50:25 +09001249 // remove duplicates in filesInfo
1250 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001251 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001252 result := []apexFile{}
1253 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001254 dest := filepath.Join(f.installDir, f.builtFile.Base())
1255 if !encountered[dest] {
1256 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001257 result = append(result, f)
1258 }
1259 }
1260 return result
1261 }
1262 filesInfo = removeDup(filesInfo)
1263
1264 // to have consistent build rules
1265 sort.Slice(filesInfo, func(i, j int) bool {
1266 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1267 })
1268
Jiyong Park127b40b2019-09-30 16:04:35 +09001269 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001270 if !ctx.Host() {
1271 for _, fi := range filesInfo {
1272 if am, ok := fi.module.(android.ApexModule); ok {
1273 if !am.AvailableFor(ctx.ModuleName()) {
1274 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1275 return
1276 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001277 }
1278 }
1279 }
1280
Jiyong Park8fd61922018-11-08 02:50:25 +09001281 // prepend the name of this APEX to the module names. These names will be the names of
1282 // modules that will be defined if the APEX is flattened.
1283 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001284 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001285 }
1286
Jiyong Park8fd61922018-11-08 02:50:25 +09001287 a.installDir = android.PathForModuleInstall(ctx, "apex")
1288 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001289
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001290 // prepare apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001291 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
Jooyung Hane1633032019-08-01 17:41:43 +09001292 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001293
1294 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001295 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1296 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001297
1298 // apex name can be overridden
1299 optCommands := []string{}
1300 if a.properties.Apex_name != nil {
1301 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1302 }
1303
Jooyung Hane1633032019-08-01 17:41:43 +09001304 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001305 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001306 Input: manifestSrc,
1307 Output: a.manifestOut,
1308 Args: map[string]string{
1309 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1310 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001311 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001312 },
1313 })
1314
Sundong Ahnabb64432019-10-22 13:58:29 +09001315 a.setCertificateAndPrivateKey(ctx)
1316 if a.properties.ApexType == flattenedApex {
Jiyong Park23c52b02019-02-02 13:13:47 +09001317 a.buildFlattenedApex(ctx)
Sundong Ahnabb64432019-10-22 13:58:29 +09001318 } else {
1319 a.buildUnflattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001320 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001321
Sundong Ahnabb64432019-10-22 13:58:29 +09001322 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001323 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001324}
1325
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001326func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001327 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001328 for _, f := range a.filesInfo {
1329 if f.module != nil {
1330 notice := f.module.NoticeFile()
1331 if notice.Valid() {
1332 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001333 }
1334 }
1335 }
1336 // append the notice file specified in the apex module itself
1337 if a.NoticeFile().Valid() {
1338 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001339 }
1340
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001341 if len(noticeFiles) == 0 {
1342 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001343 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001344
Jaewoong Jung98772792019-07-01 17:15:13 -07001345 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001346}
1347
Sundong Ahnabb64432019-10-22 13:58:29 +09001348func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001349 var abis []string
1350 for _, target := range ctx.MultiTargets() {
1351 if len(target.Arch.Abi) > 0 {
1352 abis = append(abis, target.Arch.Abi[0])
1353 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001354 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001355
Alex Light5098a612018-11-29 17:12:15 -08001356 abis = android.FirstUniqueStrings(abis)
1357
Sundong Ahnabb64432019-10-22 13:58:29 +09001358 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001359 suffix := apexType.suffix()
1360 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001361
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001362 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001363 for _, f := range a.filesInfo {
1364 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001365 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001366
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001367 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001368 emitCommands := []string{}
1369 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1370 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001371 for i, src := range filesToCopy {
1372 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001373 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001374 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001375 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1376 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001377 for _, sym := range a.filesInfo[i].symlinks {
1378 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1379 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1380 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001381 }
Dario Frenie4235822019-10-28 14:49:27 +00001382 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1383
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001384 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001385 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001386
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001387 if a.properties.Whitelisted_files != nil {
1388 ctx.Build(pctx, android.BuildParams{
1389 Rule: emitApexContentRule,
1390 Implicits: implicitInputs,
1391 Output: imageContentFile,
1392 Description: "emit apex image content",
1393 Args: map[string]string{
1394 "emit_commands": strings.Join(emitCommands, " && "),
1395 },
1396 })
1397 implicitInputs = append(implicitInputs, imageContentFile)
1398 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1399
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001400 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001401 ctx.Build(pctx, android.BuildParams{
1402 Rule: diffApexContentRule,
1403 Implicits: implicitInputs,
1404 Output: phonyOutput,
1405 Description: "diff apex image content",
1406 Args: map[string]string{
1407 "whitelisted_files_file": whitelistedFilesFile.String(),
1408 "image_content_file": imageContentFile.String(),
1409 "apex_module_name": ctx.ModuleName(),
1410 },
1411 })
1412
1413 implicitInputs = append(implicitInputs, phonyOutput)
1414 }
1415
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001416 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1417 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001418
Sundong Ahnabb64432019-10-22 13:58:29 +09001419 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001420 // files and dirs that will be created in APEX
1421 var readOnlyPaths []string
1422 var executablePaths []string // this also includes dirs
1423 for _, f := range a.filesInfo {
1424 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001425 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001426 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001427 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001428 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001429 }
Alex Light5098a612018-11-29 17:12:15 -08001430 } else {
1431 readOnlyPaths = append(readOnlyPaths, pathInApex)
1432 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001433 dir := f.installDir
1434 for !android.InList(dir, executablePaths) && dir != "" {
1435 executablePaths = append(executablePaths, dir)
1436 dir, _ = filepath.Split(dir) // move up to the parent
1437 if len(dir) > 0 {
1438 // remove trailing slash
1439 dir = dir[:len(dir)-1]
1440 }
Alex Light5098a612018-11-29 17:12:15 -08001441 }
1442 }
1443 sort.Strings(readOnlyPaths)
1444 sort.Strings(executablePaths)
1445 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1446 ctx.Build(pctx, android.BuildParams{
1447 Rule: generateFsConfig,
1448 Output: cannedFsConfig,
1449 Description: "generate fs config",
1450 Args: map[string]string{
1451 "ro_paths": strings.Join(readOnlyPaths, " "),
1452 "exec_paths": strings.Join(executablePaths, " "),
1453 },
1454 })
1455
1456 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1457 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1458 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1459 if !fileContextsOptionalPath.Valid() {
1460 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1461 return
1462 }
1463 fileContexts := fileContextsOptionalPath.Path()
1464
Jiyong Park835d82b2018-12-27 16:04:18 +09001465 optFlags := []string{}
1466
Alex Light5098a612018-11-29 17:12:15 -08001467 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001468 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1469 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001470
Jiyong Park7f67f482019-01-05 12:57:48 +09001471 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1472 if overridden {
1473 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1474 }
1475
Jiyong Park40e26a22019-02-08 02:53:06 +09001476 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001477 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001478 implicitInputs = append(implicitInputs, androidManifestFile)
1479 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1480 }
1481
Jiyong Park71b519d2019-04-18 17:25:49 +09001482 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1483 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1484 ctx.Config().UnbundledBuild() &&
1485 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1486 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1487 apiFingerprint := java.ApiFingerprintPath(ctx)
1488 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1489 implicitInputs = append(implicitInputs, apiFingerprint)
1490 }
1491 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1492
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001493 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1494 if noticeFile.Valid() {
1495 // If there's a NOTICE file, embed it as an asset file in the APEX.
1496 implicitInputs = append(implicitInputs, noticeFile.Path())
1497 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1498 }
1499
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001500 if !ctx.Config().UnbundledBuild() && a.installable() {
1501 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1502 // don't need hashtree for activation. Therefore, by removing hashtree from
1503 // apex bundle (filesystem image in it, to be specific), we can save storage.
1504 optFlags = append(optFlags, "--no_hashtree")
1505 }
1506
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001507 if a.properties.Apex_name != nil {
1508 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1509 // Note that apex_manifest is also mended.
1510 optFlags = append(optFlags, "--do_not_check_keyname")
1511 }
1512
Alex Light5098a612018-11-29 17:12:15 -08001513 ctx.Build(pctx, android.BuildParams{
1514 Rule: apexRule,
1515 Implicits: implicitInputs,
1516 Output: unsignedOutputFile,
1517 Description: "apex (" + apexType.name() + ")",
1518 Args: map[string]string{
1519 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1520 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1521 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001522 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001523 "file_contexts": fileContexts.String(),
1524 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001525 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001526 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001527 },
1528 })
1529
1530 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1531 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1532 a.bundleModuleFile = bundleModuleFile
1533
1534 ctx.Build(pctx, android.BuildParams{
1535 Rule: apexProtoConvertRule,
1536 Input: unsignedOutputFile,
1537 Output: apexProtoFile,
1538 Description: "apex proto convert",
1539 })
1540
1541 ctx.Build(pctx, android.BuildParams{
1542 Rule: apexBundleRule,
1543 Input: apexProtoFile,
1544 Output: a.bundleModuleFile,
1545 Description: "apex bundle module",
1546 Args: map[string]string{
1547 "abi": strings.Join(abis, "."),
1548 },
1549 })
1550 } else {
1551 ctx.Build(pctx, android.BuildParams{
1552 Rule: zipApexRule,
1553 Implicits: implicitInputs,
1554 Output: unsignedOutputFile,
1555 Description: "apex (" + apexType.name() + ")",
1556 Args: map[string]string{
1557 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1558 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1559 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001560 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001561 },
1562 })
Colin Crossa4925902018-11-16 11:36:28 -08001563 }
Colin Crossa4925902018-11-16 11:36:28 -08001564
Sundong Ahnabb64432019-10-22 13:58:29 +09001565 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001566 ctx.Build(pctx, android.BuildParams{
1567 Rule: java.Signapk,
1568 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001569 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001570 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001571 Implicits: []android.Path{
1572 a.container_certificate_file,
1573 a.container_private_key_file,
1574 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001575 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001576 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001577 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001578 },
1579 })
Alex Light5098a612018-11-29 17:12:15 -08001580
1581 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001582 if a.installable() {
1583 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001584 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001585 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001586}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001587
Jiyong Park8fd61922018-11-08 02:50:25 +09001588func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001589 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1590 // reply true to `InstallBypassMake()` (thus making the call
1591 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1592 // instead of `android.PathForOutput`) to return the correct path to the flattened
1593 // APEX (as its contents is installed by Make, not Soong).
1594 factx := flattenedApexContext{ctx}
1595 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1596 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1597
1598 a.buildFilesInfo(ctx)
1599}
1600
1601func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1602 cert := String(a.properties.Certificate)
1603 if cert != "" && android.SrcIsModule(cert) == "" {
1604 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1605 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1606 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1607 } else if cert == "" {
1608 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1609 a.container_certificate_file = pem
1610 a.container_private_key_file = key
1611 }
1612}
1613
1614func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001615 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001616 // 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 +09001617 // with other ordinary files.
Sundong Ahnabb64432019-10-22 13:58:29 +09001618 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 +09001619
Jiyong Park42cca6c2019-04-01 11:15:50 +09001620 // rename to apex_pubkey
1621 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1622 ctx.Build(pctx, android.BuildParams{
1623 Rule: android.Cp,
1624 Input: a.public_key_file,
1625 Output: copiedPubkey,
1626 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001627 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001628
Sundong Ahnabb64432019-10-22 13:58:29 +09001629 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001630 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001631 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001632 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001633 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1634 for _, sym := range fi.symlinks {
1635 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1636 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001637 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001638 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001639 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001640}
1641
1642func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001643 if a.properties.HideFromMake {
1644 return android.AndroidMkData{
1645 Disabled: true,
1646 }
1647 }
Alex Light5098a612018-11-29 17:12:15 -08001648 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001649 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001650 return android.AndroidMkData{
1651 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1652 for _, data := range writers {
1653 data.Custom(w, name, prefix, moduleDir, data)
1654 }
1655 }}
1656}
1657
Sundong Ahnabb64432019-10-22 13:58:29 +09001658func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001659 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001660 apexType := a.properties.ApexType
1661 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1662 // to install symbol files in $(PRODUCT_OUT}/apex.
1663 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1664 if !a.primaryApexType && apexType != flattenedApex {
1665 return moduleNames
1666 }
Jiyong Park94427262019-02-05 23:18:47 +09001667
1668 for _, fi := range a.filesInfo {
1669 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1670 continue
1671 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001672
1673 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001674 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001675 }
1676
Jiyong Park94427262019-02-05 23:18:47 +09001677 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1678 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001679 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001680 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001681 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001682 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001683 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001684 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001685 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001686 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001687 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1688 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001689 if len(fi.symlinks) > 0 {
1690 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1691 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001692
1693 if fi.module != nil && fi.module.NoticeFile().Valid() {
1694 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1695 }
Jiyong Park94427262019-02-05 23:18:47 +09001696 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001697 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001698 }
1699 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1700 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1701 if fi.module != nil {
1702 archStr := fi.module.Target().Arch.ArchType.String()
1703 host := false
1704 switch fi.module.Target().Os.Class {
1705 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001706 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001707 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1708 }
1709 host = true
1710 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001711 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001712 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1713 }
1714 host = true
1715 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001716 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001717 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1718 }
1719 }
1720 if host {
1721 makeOs := fi.module.Target().Os.String()
1722 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1723 makeOs = "linux"
1724 }
1725 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1726 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1727 }
1728 }
1729 if fi.class == javaSharedLib {
1730 javaModule := fi.module.(*java.Library)
1731 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1732 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1733 // we will have foo.jar.jar
1734 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1735 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1736 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1737 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1738 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1739 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001740 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001741 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001742 if cc, ok := fi.module.(*cc.Module); ok {
1743 if cc.UnstrippedOutputFile() != nil {
1744 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1745 }
1746 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001747 if cc.CoverageOutputFile().Valid() {
1748 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1749 }
Jiyong Park94427262019-02-05 23:18:47 +09001750 }
1751 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1752 } else {
1753 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001754 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001755 if a.primaryApexType && fi.builtFile.Base() == "apex_manifest.json" && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001756 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1757 }
Jiyong Park94427262019-02-05 23:18:47 +09001758 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1759 }
1760 }
1761 return moduleNames
1762}
1763
Sundong Ahnabb64432019-10-22 13:58:29 +09001764func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001765 return android.AndroidMkData{
1766 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1767 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001768 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001769 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001770 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001771 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001772 }
1773
Sundong Ahnabb64432019-10-22 13:58:29 +09001774 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001775 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001776 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1777 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001778 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001779 if len(moduleNames) > 0 {
1780 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1781 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001782 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001783 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001784
Sundong Ahnabb64432019-10-22 13:58:29 +09001785 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001786 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1787 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001788 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001789 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001790 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001791 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001792 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001793 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001794 if len(moduleNames) > 0 {
1795 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1796 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001797 if len(a.externalDeps) > 0 {
1798 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1799 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001800 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001801 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001802 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001803 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001804 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001805 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1806 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1807 if len(postInstallCommands) > 0 {
1808 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1809 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001810 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001811
Alex Light5098a612018-11-29 17:12:15 -08001812 if apexType == imageApex {
1813 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1814 }
Jiyong Park719b4462019-01-13 00:39:51 +09001815 }
1816 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001817}
1818
Jooyung Han344d5432019-08-23 11:17:39 +09001819func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001820 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001821 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001822 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001823 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001824 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1825 })
Alex Light5098a612018-11-29 17:12:15 -08001826 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001827 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001828 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001829 return module
1830}
Jiyong Park30ca9372019-02-07 16:27:23 +09001831
Jooyung Han344d5432019-08-23 11:17:39 +09001832func ApexBundleFactory(testApex bool) android.Module {
1833 bundle := newApexBundle()
1834 bundle.testApex = testApex
1835 return bundle
1836}
1837
1838func testApexBundleFactory() android.Module {
1839 bundle := newApexBundle()
1840 bundle.testApex = true
1841 return bundle
1842}
1843
Jiyong Parkd1063c12019-07-17 20:08:41 +09001844func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001845 return newApexBundle()
1846}
1847
1848// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1849// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1850// If not specified, then the "current" versions are gathered.
1851func vndkApexBundleFactory() android.Module {
1852 bundle := newApexBundle()
1853 bundle.vndkApex = true
1854 bundle.AddProperties(&bundle.vndkProperties)
1855 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1856 ctx.AppendProperties(&struct {
1857 Compile_multilib *string
1858 }{
1859 proptools.StringPtr("both"),
1860 })
1861 })
1862 return bundle
1863}
1864
Jooyung Han31c470b2019-10-18 16:26:59 +09001865func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1866 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1867 if vndkVersion == "current" {
1868 vndkVersion = config.PlatformVndkVersion()
1869 }
1870 return vndkVersion
1871}
1872
Jiyong Park30ca9372019-02-07 16:27:23 +09001873//
1874// Defaults
1875//
1876type Defaults struct {
1877 android.ModuleBase
1878 android.DefaultsModuleBase
1879}
1880
Jiyong Park30ca9372019-02-07 16:27:23 +09001881func defaultsFactory() android.Module {
1882 return DefaultsFactory()
1883}
1884
1885func DefaultsFactory(props ...interface{}) android.Module {
1886 module := &Defaults{}
1887
1888 module.AddProperties(props...)
1889 module.AddProperties(
1890 &apexBundleProperties{},
1891 &apexTargetBundleProperties{},
1892 )
1893
1894 android.InitDefaultsModule(module)
1895 return module
1896}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001897
1898//
1899// Prebuilt APEX
1900//
1901type Prebuilt struct {
1902 android.ModuleBase
1903 prebuilt android.Prebuilt
1904
1905 properties PrebuiltProperties
1906
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001907 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001908 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001909 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001910 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001911}
1912
1913type PrebuiltProperties struct {
1914 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001915 Source string `blueprint:"mutated"`
1916 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001917
1918 Src *string
1919 Arch struct {
1920 Arm struct {
1921 Src *string
1922 }
1923 Arm64 struct {
1924 Src *string
1925 }
1926 X86 struct {
1927 Src *string
1928 }
1929 X86_64 struct {
1930 Src *string
1931 }
1932 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001933
1934 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001935 // Optional name for the installed apex. If unspecified, name of the
1936 // module is used as the file name
1937 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001938
1939 // Names of modules to be overridden. Listed modules can only be other binaries
1940 // (in Make or Soong).
1941 // This does not completely prevent installation of the overridden binaries, but if both
1942 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1943 // from PRODUCT_PACKAGES.
1944 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001945}
1946
1947func (p *Prebuilt) installable() bool {
1948 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001949}
1950
1951func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001952 // If the device is configured to use flattened APEX, force disable the prebuilt because
1953 // the prebuilt is a non-flattened one.
1954 forceDisable := ctx.Config().FlattenApex()
1955
1956 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1957 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001958 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001959
Kun Niu10c9f832019-07-29 16:28:57 -07001960 // Force disable the prebuilts when coverage is enabled.
1961 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1962 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1963
Jiyong Park50b81e52019-07-11 11:24:41 +09001964 // b/137216042 don't use prebuilts when address sanitizer is on
1965 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1966 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1967
1968 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001969 p.properties.ForceDisable = true
1970 return
1971 }
1972
Jiyong Parkc95714e2019-03-29 14:23:10 +09001973 // This is called before prebuilt_select and prebuilt_postdeps mutators
1974 // The mutators requires that src to be set correctly for each arch so that
1975 // arch variants are disabled when src is not provided for the arch.
1976 if len(ctx.MultiTargets()) != 1 {
1977 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1978 return
1979 }
1980 var src string
1981 switch ctx.MultiTargets()[0].Arch.ArchType {
1982 case android.Arm:
1983 src = String(p.properties.Arch.Arm.Src)
1984 case android.Arm64:
1985 src = String(p.properties.Arch.Arm64.Src)
1986 case android.X86:
1987 src = String(p.properties.Arch.X86.Src)
1988 case android.X86_64:
1989 src = String(p.properties.Arch.X86_64.Src)
1990 default:
1991 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1992 return
1993 }
1994 if src == "" {
1995 src = String(p.properties.Src)
1996 }
1997 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001998}
1999
Jiyong Park03b68dd2019-07-26 23:20:40 +09002000func (p *Prebuilt) isForceDisabled() bool {
2001 return p.properties.ForceDisable
2002}
2003
Colin Cross41955e82019-05-29 14:40:35 -07002004func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2005 switch tag {
2006 case "":
2007 return android.Paths{p.outputApex}, nil
2008 default:
2009 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2010 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002011}
2012
Jiyong Park4d277042019-04-23 18:00:10 +09002013func (p *Prebuilt) InstallFilename() string {
2014 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2015}
2016
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002017func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002018 if p.properties.ForceDisable {
2019 return
2020 }
2021
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002022 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002023 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002024 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002025 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002026 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2027 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2028 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002029 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2030 ctx.Build(pctx, android.BuildParams{
2031 Rule: android.Cp,
2032 Input: p.inputApex,
2033 Output: p.outputApex,
2034 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002035 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002036 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002037 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002038
2039 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002040}
2041
2042func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2043 return &p.prebuilt
2044}
2045
2046func (p *Prebuilt) Name() string {
2047 return p.prebuilt.Name(p.ModuleBase.Name())
2048}
2049
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002050func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2051 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002052 Class: "ETC",
2053 OutputFile: android.OptionalPathForPath(p.inputApex),
2054 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002055 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2056 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002057 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002058 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2059 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2060 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2061 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002062 },
2063 }
2064}
2065
2066// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2067func PrebuiltFactory() android.Module {
2068 module := &Prebuilt{}
2069 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002070 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002071 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002072 return module
2073}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002074
2075func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2076 // small helper to add symlink commands
2077 addSymlink := func(target, dir, linkName string) {
2078 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2079 link := filepath.Join(outDir, linkName)
2080 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2081 }
2082
2083 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2084 // When all hard-coded references are fixed, remove symbolic links
2085 // Note that we should keep following symlinks for older VNDKs (<=29)
2086 // Since prebuilt vndk libs still depend on system/lib/vndk path
2087 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2088 // the name of vndk apex is formatted "com.android.vndk.v" + version
2089 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2090 if ctx.Config().Android64() {
2091 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2092 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2093 }
2094 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2095 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2096 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2097 }
2098 }
2099 return
2100}