blob: 4cbd762caeaed4f3745c0e5a30eba1c58b9a4a19 [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
Alex Light9670d332019-01-29 18:07:33 -0800355type apexNativeDependencies struct {
356 // List of native libraries
357 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900358
Alex Light9670d332019-01-29 18:07:33 -0800359 // List of native executables
360 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900361
Roland Levillain630846d2019-06-26 12:48:34 +0100362 // List of native tests
363 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800364}
Jooyung Han344d5432019-08-23 11:17:39 +0900365
Alex Light9670d332019-01-29 18:07:33 -0800366type apexMultilibProperties struct {
367 // Native dependencies whose compile_multilib is "first"
368 First apexNativeDependencies
369
370 // Native dependencies whose compile_multilib is "both"
371 Both apexNativeDependencies
372
373 // Native dependencies whose compile_multilib is "prefer32"
374 Prefer32 apexNativeDependencies
375
376 // Native dependencies whose compile_multilib is "32"
377 Lib32 apexNativeDependencies
378
379 // Native dependencies whose compile_multilib is "64"
380 Lib64 apexNativeDependencies
381}
382
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900383type apexBundleProperties struct {
384 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000385 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800386 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900387
Jiyong Park40e26a22019-02-08 02:53:06 +0900388 // AndroidManifest.xml file used for the zip container of this APEX bundle.
389 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800390 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900391
Roland Levillain411c5842019-09-19 16:37:20 +0100392 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
393 // device (/apex/<apex_name>).
394 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900395 Apex_name *string
396
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900397 // Determines the file contexts file for setting security context to each file in this APEX bundle.
398 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
399 // used.
400 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900401 File_contexts *string
402
403 // List of native shared libs that are embedded inside this APEX bundle
404 Native_shared_libs []string
405
Roland Levillain630846d2019-06-26 12:48:34 +0100406 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900407 Binaries []string
408
409 // List of java libraries that are embedded inside this APEX bundle
410 Java_libs []string
411
412 // List of prebuilt files that are embedded inside this APEX bundle
413 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900414
Roland Levillain630846d2019-06-26 12:48:34 +0100415 // List of tests that are embedded inside this APEX bundle
416 Tests []string
417
Jiyong Parkff1458f2018-10-12 21:49:38 +0900418 // Name of the apex_key module that provides the private key to sign APEX
419 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900420
Alex Light5098a612018-11-29 17:12:15 -0800421 // The type of APEX to build. Controls what the APEX payload is. Either
422 // 'image', 'zip' or 'both'. Default: 'image'.
423 Payload_type *string
424
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900425 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
426 // or an android_app_certificate module name in the form ":module".
427 Certificate *string
428
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900429 // Whether this APEX is installable to one of the partitions. Default: true.
430 Installable *bool
431
Jiyong Parkda6eb592018-12-19 17:12:36 +0900432 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
433 // Default is false.
434 Use_vendor *bool
435
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800436 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
437 Ignore_system_library_special_case *bool
438
Alex Light9670d332019-01-29 18:07:33 -0800439 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900440
Jiyong Parkf97782b2019-02-13 20:28:58 +0900441 // List of sanitizer names that this APEX is enabled for
442 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900443
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900444 PreventInstall bool `blueprint:"mutated"`
445
446 HideFromMake bool `blueprint:"mutated"`
447
Jooyung Han5c998b92019-06-27 11:30:33 +0900448 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
449 Provide_cpp_shared_libs *bool
450
451 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
452 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100453
454 // A txt file containing list of files that are whitelisted to be included in this APEX.
455 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900456
457 // List of APKs to package inside APEX
458 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900459
Sundong Ahnabb64432019-10-22 13:58:29 +0900460 // package format of this apex variant; could be non-flattened, flattened, or zip.
461 // imageApex, zipApex or flattened
462 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900463
Jiyong Parkd1063c12019-07-17 20:08:41 +0900464 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
465 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
466 // is implied. This value affects all modules included in this APEX. In other words, they are
467 // also built with the SDKs specified here.
468 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800469}
470
471type apexTargetBundleProperties struct {
472 Target struct {
473 // Multilib properties only for android.
474 Android struct {
475 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900476 }
Jooyung Han344d5432019-08-23 11:17:39 +0900477
Alex Light9670d332019-01-29 18:07:33 -0800478 // Multilib properties only for host.
479 Host struct {
480 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900481 }
Jooyung Han344d5432019-08-23 11:17:39 +0900482
Alex Light9670d332019-01-29 18:07:33 -0800483 // Multilib properties only for host linux_bionic.
484 Linux_bionic struct {
485 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900486 }
Jooyung Han344d5432019-08-23 11:17:39 +0900487
Alex Light9670d332019-01-29 18:07:33 -0800488 // Multilib properties only for host linux_glibc.
489 Linux_glibc struct {
490 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900491 }
492 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900493}
494
Jooyung Han344d5432019-08-23 11:17:39 +0900495type apexVndkProperties struct {
496 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
497 Vndk_version *string
498}
499
Jiyong Park8fd61922018-11-08 02:50:25 +0900500type apexFileClass int
501
502const (
503 etc apexFileClass = iota
504 nativeSharedLib
505 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900506 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800507 pyBinary
508 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900509 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100510 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900511 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900512)
513
Alex Light5098a612018-11-29 17:12:15 -0800514type apexPackaging int
515
516const (
517 imageApex apexPackaging = iota
518 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900519 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800520)
521
Sundong Ahnabb64432019-10-22 13:58:29 +0900522// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800523func (a apexPackaging) suffix() string {
524 switch a {
525 case imageApex:
526 return imageApexSuffix
527 case zipApex:
528 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800529 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100530 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800531 }
532}
533
534func (a apexPackaging) name() string {
535 switch a {
536 case imageApex:
537 return imageApexType
538 case zipApex:
539 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800540 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100541 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800542 }
543}
544
Jiyong Park8fd61922018-11-08 02:50:25 +0900545func (class apexFileClass) NameInMake() string {
546 switch class {
547 case etc:
548 return "ETC"
549 case nativeSharedLib:
550 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800551 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900552 return "EXECUTABLES"
553 case javaSharedLib:
554 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100555 case nativeTest:
556 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900557 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900558 // b/142537672 Why isn't this APP? We want to have full control over
559 // the paths and file names of the apk file under the flattend APEX.
560 // If this is set to APP, then the paths and file names are modified
561 // by the Make build system. For example, it is installed to
562 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
563 // /system/apex/<apexname>/app/<Appname> because the build system automatically
564 // appends module name (which is <apexname>.<Appname> to the path.
565 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900566 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100567 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900568 }
569}
570
571type apexFile struct {
572 builtFile android.Path
573 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900574 installDir string
575 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900576 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800577 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900578}
579
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900580type apexBundle struct {
581 android.ModuleBase
582 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900583 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900584
Alex Light9670d332019-01-29 18:07:33 -0800585 properties apexBundleProperties
586 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900587 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900588
Colin Crossa4925902018-11-16 11:36:28 -0800589 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900590 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700591 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900592
Jiyong Park03b68dd2019-07-26 23:20:40 +0900593 prebuiltFileToDelete string
594
Jiyong Park42cca6c2019-04-01 11:15:50 +0900595 public_key_file android.Path
596 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900597
598 container_certificate_file android.Path
599 container_private_key_file android.Path
600
Jiyong Park8fd61922018-11-08 02:50:25 +0900601 // list of files to be included in this apex
602 filesInfo []apexFile
603
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900604 // list of module names that this APEX is depending on
605 externalDeps []string
606
Sundong Ahnabb64432019-10-22 13:58:29 +0900607 testApex bool
608 vndkApex bool
609 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900610
611 // intermediate path for apex_manifest.json
612 manifestOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900613
614 // list of commands to create symlinks for backward compatibility
615 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
616 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
617 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
618 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900619
620 // Suffix of module name in Android.mk
621 // ".flattened", ".apex", ".zipapex", or ""
622 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900623}
624
Jiyong Park397e55e2018-10-24 21:09:55 +0900625func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100626 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700627 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900628 // Use *FarVariation* to be able to depend on modules having
629 // conflicting variations with this module. This is required since
630 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
631 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700632 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900633 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900634 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900635 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700636 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900637
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700638 ctx.AddFarVariationDependencies(append(target.Variations(),
639 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
640 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100641
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700642 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100643 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100644 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700645 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900646}
647
Alex Light9670d332019-01-29 18:07:33 -0800648func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
649 if ctx.Os().Class == android.Device {
650 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
651 } else {
652 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
653 if ctx.Os().Bionic() {
654 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
655 } else {
656 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
657 }
658 }
659}
660
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900661func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900662 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900663 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800664
665 a.combineProperties(ctx)
666
Jiyong Park397e55e2018-10-24 21:09:55 +0900667 has32BitTarget := false
668 for _, target := range targets {
669 if target.Arch.ArchType.Multilib == "lib32" {
670 has32BitTarget = true
671 }
672 }
673 for i, target := range targets {
674 // When multilib.* is omitted for native_shared_libs, it implies
675 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700676 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900677 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900678 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700679 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900680
Roland Levillain630846d2019-06-26 12:48:34 +0100681 // When multilib.* is omitted for tests, it implies
682 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700683 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100684 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100685 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700686 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100687
Jiyong Park397e55e2018-10-24 21:09:55 +0900688 // Add native modules targetting both ABIs
689 addDependenciesForNativeModules(ctx,
690 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100691 a.properties.Multilib.Both.Binaries,
692 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700693 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900694 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900695
Alex Light3d673592019-01-18 14:37:31 -0800696 isPrimaryAbi := i == 0
697 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900698 // When multilib.* is omitted for binaries, it implies
699 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700700 ctx.AddFarVariationDependencies(append(target.Variations(),
701 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
702 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900703
704 // Add native modules targetting the first ABI
705 addDependenciesForNativeModules(ctx,
706 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100707 a.properties.Multilib.First.Binaries,
708 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700709 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900710 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800711
712 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700713 ctx.AddFarVariationDependencies(target.Variations(),
714 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900715 }
716
717 switch target.Arch.ArchType.Multilib {
718 case "lib32":
719 // Add native modules targetting 32-bit ABI
720 addDependenciesForNativeModules(ctx,
721 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100722 a.properties.Multilib.Lib32.Binaries,
723 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700724 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900725 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900726
727 addDependenciesForNativeModules(ctx,
728 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100729 a.properties.Multilib.Prefer32.Binaries,
730 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700731 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900732 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900733 case "lib64":
734 // Add native modules targetting 64-bit ABI
735 addDependenciesForNativeModules(ctx,
736 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100737 a.properties.Multilib.Lib64.Binaries,
738 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700739 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900740 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900741
742 if !has32BitTarget {
743 addDependenciesForNativeModules(ctx,
744 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100745 a.properties.Multilib.Prefer32.Binaries,
746 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700747 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900748 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900749 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700750
751 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
752 for _, sanitizer := range ctx.Config().SanitizeDevice() {
753 if sanitizer == "hwaddress" {
754 addDependenciesForNativeModules(ctx,
755 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700756 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700757 break
758 }
759 }
760 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900761 }
762
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900763 }
764
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700765 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
766 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900767
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700768 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
769 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900770
Jiyong Park23c52b02019-02-02 13:13:47 +0900771 if String(a.properties.Key) == "" {
772 ctx.ModuleErrorf("key is missing")
773 return
774 }
775 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900776
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900777 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900778 if cert != "" {
779 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900780 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900781
782 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
783 if len(a.properties.Uses_sdks) > 0 {
784 sdkRefs := []android.SdkRef{}
785 for _, str := range a.properties.Uses_sdks {
786 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
787 sdkRefs = append(sdkRefs, parsed)
788 }
789 a.BuildWithSdks(sdkRefs)
790 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900791}
792
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900793func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
794 // direct deps of an APEX bundle are all part of the APEX bundle
795 return true
796}
797
Colin Cross0ea8ba82019-06-06 14:33:29 -0700798func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900799 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
800 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000801 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900802 }
803 return String(a.properties.Certificate)
804}
805
Colin Cross41955e82019-05-29 14:40:35 -0700806func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
807 switch tag {
808 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900809 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700810 default:
811 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900812 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900813}
814
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900815func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900816 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900817}
818
Jiyong Park7c1dc612019-01-05 11:15:24 +0900819func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900820 if a.vndkApex {
821 return "vendor." + a.vndkVersion(config)
822 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900823 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900824 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900825 } else {
826 return "core"
827 }
828}
829
Jiyong Parkf97782b2019-02-13 20:28:58 +0900830func (a *apexBundle) EnableSanitizer(sanitizerName string) {
831 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
832 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
833 }
834}
835
Jiyong Park388ef3f2019-01-28 19:47:32 +0900836func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900837 if android.InList(sanitizerName, a.properties.SanitizerNames) {
838 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900839 }
840
841 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900842 globalSanitizerNames := []string{}
843 if a.Host() {
844 globalSanitizerNames = ctx.Config().SanitizeHost()
845 } else {
846 arches := ctx.Config().SanitizeDeviceArch()
847 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
848 globalSanitizerNames = ctx.Config().SanitizeDevice()
849 }
850 }
851 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900852}
853
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900854func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
855 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
856}
857
858func (a *apexBundle) PreventInstall() {
859 a.properties.PreventInstall = true
860}
861
862func (a *apexBundle) HideFromMake() {
863 a.properties.HideFromMake = true
864}
865
Martin Stjernholm279de572019-09-10 23:18:20 +0100866func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900867 // Decide the APEX-local directory by the multilib of the library
868 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100869 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900870 case "lib32":
871 dirInApex = "lib"
872 case "lib64":
873 dirInApex = "lib64"
874 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100875 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700876 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100877 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900878 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100879 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
880 // Special case for Bionic libs and other libs installed with them. This is
881 // to prevent those libs from being included in the search path
882 // /apex/com.android.runtime/${LIB}. This exclusion is required because
883 // those libs in the Runtime APEX are available via the legacy paths in
884 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
885 // to the legacy paths and thus will be loaded into the default linker
886 // namespace (aka "platform" namespace). If the libs are directly in
887 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
888 // into the runtime linker namespace, which will result in double loading of
889 // them, which isn't supported.
890 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900891 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900892
Martin Stjernholm279de572019-09-10 23:18:20 +0100893 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900894 return
895}
896
897func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900898 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700899 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200900 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900901 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900902 fileToCopy = cc.OutputFile().Path()
903 return
904}
905
Alex Light778127a2019-02-27 14:19:50 -0800906func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
907 dirInApex = "bin"
908 fileToCopy = py.HostToolPath().Path()
909 return
910}
911func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
912 dirInApex = "bin"
913 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
914 if err != nil {
915 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
916 return
917 }
918 fileToCopy = android.PathForOutput(ctx, s)
919 return
920}
921
Jiyong Park04480cf2019-02-06 00:16:29 +0900922func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
923 dirInApex = filepath.Join("bin", sh.SubDir())
924 fileToCopy = sh.OutputFile()
925 return
926}
927
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900928func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
929 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900930 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900931 return
932}
933
Jiyong Park9e6c2422019-08-09 20:39:45 +0900934func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
935 dirInApex = "javalib"
936 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
937 implJars := java.ImplementationJars()
938 if len(implJars) != 1 {
939 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
940 strings.Join(implJars.Strings(), ", ")))
941 }
942 fileToCopy = implJars[0]
943 return
944}
945
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900946func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
947 dirInApex = filepath.Join("etc", prebuilt.SubDir())
948 fileToCopy = prebuilt.OutputFile()
949 return
950}
951
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900952func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +0900953 appDir := "app"
954 if app.Privileged() {
955 appDir = "priv-app"
956 }
957 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900958 fileToCopy = app.OutputFile()
959 return
960}
961
Dario Frenicde2a032019-10-27 00:29:22 +0100962func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
963 appDir := "app"
964 if app.Privileged() {
965 appDir = "priv-app"
966 }
967 dirInApex = filepath.Join(appDir, pkgName)
968 fileToCopy = app.OutputFile()
969 return
970}
971
Roland Levillain935639d2019-08-13 14:55:28 +0100972// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
973type flattenedApexContext struct {
974 android.ModuleContext
975}
976
977func (c *flattenedApexContext) InstallBypassMake() bool {
978 return true
979}
980
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900981func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900982 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900983
Sundong Ahnabb64432019-10-22 13:58:29 +0900984 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
985 switch a.properties.ApexType {
986 case imageApex:
987 if buildFlattenedAsDefault {
988 a.suffix = imageApexSuffix
989 } else {
990 a.suffix = ""
991 a.primaryApexType = true
992 }
993 case zipApex:
994 if proptools.String(a.properties.Payload_type) == "zip" {
995 a.suffix = ""
996 a.primaryApexType = true
997 } else {
998 a.suffix = zipApexSuffix
999 }
1000 case flattenedApex:
1001 if buildFlattenedAsDefault {
1002 a.suffix = ""
1003 a.primaryApexType = true
1004 } else {
1005 a.suffix = flattenedSuffix
1006 }
Alex Light5098a612018-11-29 17:12:15 -08001007 }
1008
Roland Levillain630846d2019-06-26 12:48:34 +01001009 if len(a.properties.Tests) > 0 && !a.testApex {
1010 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1011 return
1012 }
1013
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001014 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1015
Jooyung Hane1633032019-08-01 17:41:43 +09001016 // native lib dependencies
1017 var provideNativeLibs []string
1018 var requireNativeLibs []string
1019
Jooyung Han5c998b92019-06-27 11:30:33 +09001020 // Check if "uses" requirements are met with dependent apexBundles
1021 var providedNativeSharedLibs []string
1022 useVendor := proptools.Bool(a.properties.Use_vendor)
1023 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1024 if ctx.OtherModuleDependencyTag(m) != usesTag {
1025 return
1026 }
1027 otherName := ctx.OtherModuleName(m)
1028 other, ok := m.(*apexBundle)
1029 if !ok {
1030 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1031 return
1032 }
1033 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1034 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1035 return
1036 }
1037 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1038 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1039 return
1040 }
1041 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1042 })
1043
Alex Light778127a2019-02-27 14:19:50 -08001044 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001045 depTag := ctx.OtherModuleDependencyTag(child)
1046 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001047 if _, ok := parent.(*apexBundle); ok {
1048 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001049 switch depTag {
1050 case sharedLibTag:
1051 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001052 if cc.HasStubsVariants() {
1053 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1054 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001055 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001056 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001057 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001058 } else {
1059 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001060 }
1061 case executableTag:
1062 if cc, ok := child.(*cc.Module); ok {
1063 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001064 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001065 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001066 } else if sh, ok := child.(*android.ShBinary); ok {
1067 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001068 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001069 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1070 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1071 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1072 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1073 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1074 // NB: Since go binaries are static we don't need the module for anything here, which is
1075 // good since the go tool is a blueprint.Module not an android.Module like we would
1076 // normally use.
1077 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001078 } else {
Alex Light778127a2019-02-27 14:19:50 -08001079 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 +09001080 }
1081 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001082 if javaLib, ok := child.(*java.Library); ok {
1083 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001084 if fileToCopy == nil {
1085 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1086 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001087 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1088 }
1089 return true
1090 } else if javaLib, ok := child.(*java.Import); ok {
1091 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1092 if fileToCopy == nil {
1093 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1094 } else {
1095 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001096 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001097 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001098 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001099 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001100 }
1101 case prebuiltTag:
1102 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1103 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001104 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001105 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001106 } else {
1107 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1108 }
Roland Levillain630846d2019-06-26 12:48:34 +01001109 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001110 if ccTest, ok := child.(*cc.Module); ok {
1111 if ccTest.IsTestPerSrcAllTestsVariation() {
1112 // Multiple-output test module (where `test_per_src: true`).
1113 //
1114 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1115 // We do not add this variation to `filesInfo`, as it has no output;
1116 // however, we do add the other variations of this module as indirect
1117 // dependencies (see below).
1118 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001119 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001120 // Single-output test module (where `test_per_src: false`).
1121 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1122 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001123 }
Roland Levillain630846d2019-06-26 12:48:34 +01001124 return true
1125 } else {
1126 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1127 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001128 case keyTag:
1129 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001130 a.private_key_file = key.private_key_file
1131 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001132 return false
1133 } else {
1134 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001135 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001136 case certificateTag:
1137 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001138 a.container_certificate_file = dep.Certificate.Pem
1139 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001140 return false
1141 } else {
1142 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1143 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001144 case android.PrebuiltDepTag:
1145 // If the prebuilt is force disabled, remember to delete the prebuilt file
1146 // that might have been installed in the previous builds
1147 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1148 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1149 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001150 case androidAppTag:
1151 if ap, ok := child.(*java.AndroidApp); ok {
1152 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1153 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1154 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001155 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1156 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1157 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001158 } else {
1159 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1160 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001161 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001162 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001163 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001164 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001165 // We cannot use a switch statement on `depTag` here as the checked
1166 // tags used below are private (e.g. `cc.sharedDepTag`).
1167 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1168 if cc, ok := child.(*cc.Module); ok {
1169 if android.InList(cc.Name(), providedNativeSharedLibs) {
1170 // If we're using a shared library which is provided from other APEX,
1171 // don't include it in this APEX
1172 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001173 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001174 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1175 // If the dependency is a stubs lib, don't include it in this APEX,
1176 // but make sure that the lib is installed on the device.
1177 // In case no APEX is having the lib, the lib is installed to the system
1178 // partition.
1179 //
1180 // Always include if we are a host-apex however since those won't have any
1181 // system libraries.
1182 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1183 a.externalDeps = append(a.externalDeps, cc.Name())
1184 }
Jooyung Hane1633032019-08-01 17:41:43 +09001185 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001186 // Don't track further
1187 return false
1188 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001189 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001190 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1191 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001192 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001193 } else if cc.IsTestPerSrcDepTag(depTag) {
1194 if cc, ok := child.(*cc.Module); ok {
1195 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1196 // Handle modules created as `test_per_src` variations of a single test module:
1197 // use the name of the generated test binary (`fileToCopy`) instead of the name
1198 // of the original test module (`depName`, shared by all `test_per_src`
1199 // variations of that module).
1200 moduleName := filepath.Base(fileToCopy.String())
1201 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1202 return true
1203 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001204 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001205 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001206 }
1207 }
1208 }
1209 return false
1210 })
1211
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001212 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001213 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1214 return
1215 }
1216
Jiyong Park8fd61922018-11-08 02:50:25 +09001217 // remove duplicates in filesInfo
1218 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001219 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001220 result := []apexFile{}
1221 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001222 dest := filepath.Join(f.installDir, f.builtFile.Base())
1223 if !encountered[dest] {
1224 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001225 result = append(result, f)
1226 }
1227 }
1228 return result
1229 }
1230 filesInfo = removeDup(filesInfo)
1231
1232 // to have consistent build rules
1233 sort.Slice(filesInfo, func(i, j int) bool {
1234 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1235 })
1236
Jiyong Park127b40b2019-09-30 16:04:35 +09001237 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001238 if !ctx.Host() {
1239 for _, fi := range filesInfo {
1240 if am, ok := fi.module.(android.ApexModule); ok {
1241 if !am.AvailableFor(ctx.ModuleName()) {
1242 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1243 return
1244 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001245 }
1246 }
1247 }
1248
Jiyong Park8fd61922018-11-08 02:50:25 +09001249 // prepend the name of this APEX to the module names. These names will be the names of
1250 // modules that will be defined if the APEX is flattened.
1251 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001252 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001253 }
1254
Jiyong Park8fd61922018-11-08 02:50:25 +09001255 a.installDir = android.PathForModuleInstall(ctx, "apex")
1256 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001257
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001258 // prepare apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001259 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
Jooyung Hane1633032019-08-01 17:41:43 +09001260 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001261
1262 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001263 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1264 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001265
1266 // apex name can be overridden
1267 optCommands := []string{}
1268 if a.properties.Apex_name != nil {
1269 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1270 }
1271
Jooyung Hane1633032019-08-01 17:41:43 +09001272 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001273 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001274 Input: manifestSrc,
1275 Output: a.manifestOut,
1276 Args: map[string]string{
1277 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1278 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001279 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001280 },
1281 })
1282
Sundong Ahnabb64432019-10-22 13:58:29 +09001283 a.setCertificateAndPrivateKey(ctx)
1284 if a.properties.ApexType == flattenedApex {
Jiyong Park23c52b02019-02-02 13:13:47 +09001285 a.buildFlattenedApex(ctx)
Sundong Ahnabb64432019-10-22 13:58:29 +09001286 } else {
1287 a.buildUnflattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001288 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001289
Sundong Ahnabb64432019-10-22 13:58:29 +09001290 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001291 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001292}
1293
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001294func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001295 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001296 for _, f := range a.filesInfo {
1297 if f.module != nil {
1298 notice := f.module.NoticeFile()
1299 if notice.Valid() {
1300 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001301 }
1302 }
1303 }
1304 // append the notice file specified in the apex module itself
1305 if a.NoticeFile().Valid() {
1306 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001307 }
1308
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001309 if len(noticeFiles) == 0 {
1310 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001311 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001312
Jaewoong Jung98772792019-07-01 17:15:13 -07001313 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001314}
1315
Sundong Ahnabb64432019-10-22 13:58:29 +09001316func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001317 var abis []string
1318 for _, target := range ctx.MultiTargets() {
1319 if len(target.Arch.Abi) > 0 {
1320 abis = append(abis, target.Arch.Abi[0])
1321 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001322 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001323
Alex Light5098a612018-11-29 17:12:15 -08001324 abis = android.FirstUniqueStrings(abis)
1325
Sundong Ahnabb64432019-10-22 13:58:29 +09001326 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001327 suffix := apexType.suffix()
1328 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001329
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001330 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001331 for _, f := range a.filesInfo {
1332 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001333 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001334
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001335 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001336 emitCommands := []string{}
1337 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1338 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001339 for i, src := range filesToCopy {
1340 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001341 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001342 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001343 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1344 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001345 for _, sym := range a.filesInfo[i].symlinks {
1346 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1347 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1348 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001349 }
Dario Frenie4235822019-10-28 14:49:27 +00001350 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1351
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001352 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001353 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001354
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001355 if a.properties.Whitelisted_files != nil {
1356 ctx.Build(pctx, android.BuildParams{
1357 Rule: emitApexContentRule,
1358 Implicits: implicitInputs,
1359 Output: imageContentFile,
1360 Description: "emit apex image content",
1361 Args: map[string]string{
1362 "emit_commands": strings.Join(emitCommands, " && "),
1363 },
1364 })
1365 implicitInputs = append(implicitInputs, imageContentFile)
1366 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1367
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001368 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001369 ctx.Build(pctx, android.BuildParams{
1370 Rule: diffApexContentRule,
1371 Implicits: implicitInputs,
1372 Output: phonyOutput,
1373 Description: "diff apex image content",
1374 Args: map[string]string{
1375 "whitelisted_files_file": whitelistedFilesFile.String(),
1376 "image_content_file": imageContentFile.String(),
1377 "apex_module_name": ctx.ModuleName(),
1378 },
1379 })
1380
1381 implicitInputs = append(implicitInputs, phonyOutput)
1382 }
1383
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001384 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1385 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001386
Sundong Ahnabb64432019-10-22 13:58:29 +09001387 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001388 // files and dirs that will be created in APEX
1389 var readOnlyPaths []string
1390 var executablePaths []string // this also includes dirs
1391 for _, f := range a.filesInfo {
1392 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001393 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001394 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001395 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001396 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001397 }
Alex Light5098a612018-11-29 17:12:15 -08001398 } else {
1399 readOnlyPaths = append(readOnlyPaths, pathInApex)
1400 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001401 dir := f.installDir
1402 for !android.InList(dir, executablePaths) && dir != "" {
1403 executablePaths = append(executablePaths, dir)
1404 dir, _ = filepath.Split(dir) // move up to the parent
1405 if len(dir) > 0 {
1406 // remove trailing slash
1407 dir = dir[:len(dir)-1]
1408 }
Alex Light5098a612018-11-29 17:12:15 -08001409 }
1410 }
1411 sort.Strings(readOnlyPaths)
1412 sort.Strings(executablePaths)
1413 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1414 ctx.Build(pctx, android.BuildParams{
1415 Rule: generateFsConfig,
1416 Output: cannedFsConfig,
1417 Description: "generate fs config",
1418 Args: map[string]string{
1419 "ro_paths": strings.Join(readOnlyPaths, " "),
1420 "exec_paths": strings.Join(executablePaths, " "),
1421 },
1422 })
1423
1424 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1425 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1426 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1427 if !fileContextsOptionalPath.Valid() {
1428 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1429 return
1430 }
1431 fileContexts := fileContextsOptionalPath.Path()
1432
Jiyong Park835d82b2018-12-27 16:04:18 +09001433 optFlags := []string{}
1434
Alex Light5098a612018-11-29 17:12:15 -08001435 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001436 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1437 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001438
Jiyong Park7f67f482019-01-05 12:57:48 +09001439 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1440 if overridden {
1441 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1442 }
1443
Jiyong Park40e26a22019-02-08 02:53:06 +09001444 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001445 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001446 implicitInputs = append(implicitInputs, androidManifestFile)
1447 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1448 }
1449
Jiyong Park71b519d2019-04-18 17:25:49 +09001450 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1451 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1452 ctx.Config().UnbundledBuild() &&
1453 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1454 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1455 apiFingerprint := java.ApiFingerprintPath(ctx)
1456 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1457 implicitInputs = append(implicitInputs, apiFingerprint)
1458 }
1459 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1460
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001461 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1462 if noticeFile.Valid() {
1463 // If there's a NOTICE file, embed it as an asset file in the APEX.
1464 implicitInputs = append(implicitInputs, noticeFile.Path())
1465 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1466 }
1467
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001468 if !ctx.Config().UnbundledBuild() && a.installable() {
1469 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1470 // don't need hashtree for activation. Therefore, by removing hashtree from
1471 // apex bundle (filesystem image in it, to be specific), we can save storage.
1472 optFlags = append(optFlags, "--no_hashtree")
1473 }
1474
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001475 if a.properties.Apex_name != nil {
1476 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1477 // Note that apex_manifest is also mended.
1478 optFlags = append(optFlags, "--do_not_check_keyname")
1479 }
1480
Alex Light5098a612018-11-29 17:12:15 -08001481 ctx.Build(pctx, android.BuildParams{
1482 Rule: apexRule,
1483 Implicits: implicitInputs,
1484 Output: unsignedOutputFile,
1485 Description: "apex (" + apexType.name() + ")",
1486 Args: map[string]string{
1487 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1488 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1489 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001490 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001491 "file_contexts": fileContexts.String(),
1492 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001493 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001494 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001495 },
1496 })
1497
1498 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1499 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1500 a.bundleModuleFile = bundleModuleFile
1501
1502 ctx.Build(pctx, android.BuildParams{
1503 Rule: apexProtoConvertRule,
1504 Input: unsignedOutputFile,
1505 Output: apexProtoFile,
1506 Description: "apex proto convert",
1507 })
1508
1509 ctx.Build(pctx, android.BuildParams{
1510 Rule: apexBundleRule,
1511 Input: apexProtoFile,
1512 Output: a.bundleModuleFile,
1513 Description: "apex bundle module",
1514 Args: map[string]string{
1515 "abi": strings.Join(abis, "."),
1516 },
1517 })
1518 } else {
1519 ctx.Build(pctx, android.BuildParams{
1520 Rule: zipApexRule,
1521 Implicits: implicitInputs,
1522 Output: unsignedOutputFile,
1523 Description: "apex (" + apexType.name() + ")",
1524 Args: map[string]string{
1525 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1526 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1527 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001528 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001529 },
1530 })
Colin Crossa4925902018-11-16 11:36:28 -08001531 }
Colin Crossa4925902018-11-16 11:36:28 -08001532
Sundong Ahnabb64432019-10-22 13:58:29 +09001533 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001534 ctx.Build(pctx, android.BuildParams{
1535 Rule: java.Signapk,
1536 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001537 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001538 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001539 Implicits: []android.Path{
1540 a.container_certificate_file,
1541 a.container_private_key_file,
1542 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001543 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001544 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001545 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001546 },
1547 })
Alex Light5098a612018-11-29 17:12:15 -08001548
1549 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001550 if a.installable() {
1551 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001552 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001553 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001554}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001555
Jiyong Park8fd61922018-11-08 02:50:25 +09001556func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001557 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1558 // reply true to `InstallBypassMake()` (thus making the call
1559 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1560 // instead of `android.PathForOutput`) to return the correct path to the flattened
1561 // APEX (as its contents is installed by Make, not Soong).
1562 factx := flattenedApexContext{ctx}
1563 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1564 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1565
1566 a.buildFilesInfo(ctx)
1567}
1568
1569func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1570 cert := String(a.properties.Certificate)
1571 if cert != "" && android.SrcIsModule(cert) == "" {
1572 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1573 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1574 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1575 } else if cert == "" {
1576 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1577 a.container_certificate_file = pem
1578 a.container_private_key_file = key
1579 }
1580}
1581
1582func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001583 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001584 // 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 +09001585 // with other ordinary files.
Sundong Ahnabb64432019-10-22 13:58:29 +09001586 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 +09001587
Jiyong Park42cca6c2019-04-01 11:15:50 +09001588 // rename to apex_pubkey
1589 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1590 ctx.Build(pctx, android.BuildParams{
1591 Rule: android.Cp,
1592 Input: a.public_key_file,
1593 Output: copiedPubkey,
1594 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001595 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001596
Sundong Ahnabb64432019-10-22 13:58:29 +09001597 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001598 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001599 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001600 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001601 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1602 for _, sym := range fi.symlinks {
1603 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1604 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001605 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001606 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001607 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001608}
1609
1610func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001611 if a.properties.HideFromMake {
1612 return android.AndroidMkData{
1613 Disabled: true,
1614 }
1615 }
Alex Light5098a612018-11-29 17:12:15 -08001616 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001617 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001618 return android.AndroidMkData{
1619 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1620 for _, data := range writers {
1621 data.Custom(w, name, prefix, moduleDir, data)
1622 }
1623 }}
1624}
1625
Sundong Ahnabb64432019-10-22 13:58:29 +09001626func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001627 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001628 apexType := a.properties.ApexType
1629 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1630 // to install symbol files in $(PRODUCT_OUT}/apex.
1631 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1632 if !a.primaryApexType && apexType != flattenedApex {
1633 return moduleNames
1634 }
Jiyong Park94427262019-02-05 23:18:47 +09001635
1636 for _, fi := range a.filesInfo {
1637 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1638 continue
1639 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001640
1641 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001642 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001643 }
1644
Jiyong Park94427262019-02-05 23:18:47 +09001645 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1646 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001647 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001648 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001649 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001650 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001651 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001652 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001653 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001654 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001655 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1656 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001657 if len(fi.symlinks) > 0 {
1658 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1659 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001660
1661 if fi.module != nil && fi.module.NoticeFile().Valid() {
1662 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1663 }
Jiyong Park94427262019-02-05 23:18:47 +09001664 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001665 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001666 }
1667 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1668 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1669 if fi.module != nil {
1670 archStr := fi.module.Target().Arch.ArchType.String()
1671 host := false
1672 switch fi.module.Target().Os.Class {
1673 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001674 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001675 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1676 }
1677 host = true
1678 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001679 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001680 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1681 }
1682 host = true
1683 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001684 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001685 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1686 }
1687 }
1688 if host {
1689 makeOs := fi.module.Target().Os.String()
1690 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1691 makeOs = "linux"
1692 }
1693 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1694 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1695 }
1696 }
1697 if fi.class == javaSharedLib {
1698 javaModule := fi.module.(*java.Library)
1699 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1700 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1701 // we will have foo.jar.jar
1702 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1703 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1704 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1705 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1706 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1707 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001708 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001709 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001710 if cc, ok := fi.module.(*cc.Module); ok {
1711 if cc.UnstrippedOutputFile() != nil {
1712 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1713 }
1714 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001715 if cc.CoverageOutputFile().Valid() {
1716 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1717 }
Jiyong Park94427262019-02-05 23:18:47 +09001718 }
1719 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1720 } else {
1721 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001722 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001723 if a.primaryApexType && fi.builtFile.Base() == "apex_manifest.json" && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001724 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1725 }
Jiyong Park94427262019-02-05 23:18:47 +09001726 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1727 }
1728 }
1729 return moduleNames
1730}
1731
Sundong Ahnabb64432019-10-22 13:58:29 +09001732func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001733 return android.AndroidMkData{
1734 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1735 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001736 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001737 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001738 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001739 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001740 }
1741
Sundong Ahnabb64432019-10-22 13:58:29 +09001742 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001743 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001744 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1745 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001746 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001747 if len(moduleNames) > 0 {
1748 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1749 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001750 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001751 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001752
Sundong Ahnabb64432019-10-22 13:58:29 +09001753 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001754 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1755 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001756 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001757 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001758 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001759 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001760 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001761 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001762 if len(moduleNames) > 0 {
1763 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1764 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001765 if len(a.externalDeps) > 0 {
1766 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1767 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001768 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001769 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001770 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001771 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001772 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001773 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1774 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1775 if len(postInstallCommands) > 0 {
1776 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1777 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001778 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001779
Alex Light5098a612018-11-29 17:12:15 -08001780 if apexType == imageApex {
1781 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1782 }
Jiyong Park719b4462019-01-13 00:39:51 +09001783 }
1784 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001785}
1786
Jooyung Han344d5432019-08-23 11:17:39 +09001787func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001788 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001789 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001790 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001791 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001792 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1793 })
Alex Light5098a612018-11-29 17:12:15 -08001794 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001795 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001796 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001797 return module
1798}
Jiyong Park30ca9372019-02-07 16:27:23 +09001799
Nicolas Geoffray24babe32019-10-30 11:26:52 +00001800func ApexBundleFactory(testApex bool) android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001801 bundle := newApexBundle()
1802 bundle.testApex = testApex
1803 return bundle
1804}
1805
1806func testApexBundleFactory() android.Module {
1807 bundle := newApexBundle()
1808 bundle.testApex = true
1809 return bundle
1810}
1811
Jiyong Parkd1063c12019-07-17 20:08:41 +09001812func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001813 return newApexBundle()
1814}
1815
1816// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1817// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1818// If not specified, then the "current" versions are gathered.
1819func vndkApexBundleFactory() android.Module {
1820 bundle := newApexBundle()
1821 bundle.vndkApex = true
1822 bundle.AddProperties(&bundle.vndkProperties)
1823 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1824 ctx.AppendProperties(&struct {
1825 Compile_multilib *string
1826 }{
1827 proptools.StringPtr("both"),
1828 })
1829 })
1830 return bundle
1831}
1832
Jooyung Han31c470b2019-10-18 16:26:59 +09001833func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1834 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1835 if vndkVersion == "current" {
1836 vndkVersion = config.PlatformVndkVersion()
1837 }
1838 return vndkVersion
1839}
1840
Jiyong Park30ca9372019-02-07 16:27:23 +09001841//
1842// Defaults
1843//
1844type Defaults struct {
1845 android.ModuleBase
1846 android.DefaultsModuleBase
1847}
1848
Jiyong Park30ca9372019-02-07 16:27:23 +09001849func defaultsFactory() android.Module {
1850 return DefaultsFactory()
1851}
1852
1853func DefaultsFactory(props ...interface{}) android.Module {
1854 module := &Defaults{}
1855
1856 module.AddProperties(props...)
1857 module.AddProperties(
1858 &apexBundleProperties{},
1859 &apexTargetBundleProperties{},
1860 )
1861
1862 android.InitDefaultsModule(module)
1863 return module
1864}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001865
1866//
1867// Prebuilt APEX
1868//
1869type Prebuilt struct {
1870 android.ModuleBase
1871 prebuilt android.Prebuilt
1872
1873 properties PrebuiltProperties
1874
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001875 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001876 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001877 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001878 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001879}
1880
1881type PrebuiltProperties struct {
1882 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001883 Source string `blueprint:"mutated"`
1884 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001885
1886 Src *string
1887 Arch struct {
1888 Arm struct {
1889 Src *string
1890 }
1891 Arm64 struct {
1892 Src *string
1893 }
1894 X86 struct {
1895 Src *string
1896 }
1897 X86_64 struct {
1898 Src *string
1899 }
1900 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001901
1902 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001903 // Optional name for the installed apex. If unspecified, name of the
1904 // module is used as the file name
1905 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001906
1907 // Names of modules to be overridden. Listed modules can only be other binaries
1908 // (in Make or Soong).
1909 // This does not completely prevent installation of the overridden binaries, but if both
1910 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1911 // from PRODUCT_PACKAGES.
1912 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001913}
1914
1915func (p *Prebuilt) installable() bool {
1916 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001917}
1918
1919func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001920 // If the device is configured to use flattened APEX, force disable the prebuilt because
1921 // the prebuilt is a non-flattened one.
1922 forceDisable := ctx.Config().FlattenApex()
1923
1924 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1925 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001926 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001927
Kun Niu10c9f832019-07-29 16:28:57 -07001928 // Force disable the prebuilts when coverage is enabled.
1929 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1930 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1931
Jiyong Park50b81e52019-07-11 11:24:41 +09001932 // b/137216042 don't use prebuilts when address sanitizer is on
1933 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1934 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1935
1936 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001937 p.properties.ForceDisable = true
1938 return
1939 }
1940
Jiyong Parkc95714e2019-03-29 14:23:10 +09001941 // This is called before prebuilt_select and prebuilt_postdeps mutators
1942 // The mutators requires that src to be set correctly for each arch so that
1943 // arch variants are disabled when src is not provided for the arch.
1944 if len(ctx.MultiTargets()) != 1 {
1945 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1946 return
1947 }
1948 var src string
1949 switch ctx.MultiTargets()[0].Arch.ArchType {
1950 case android.Arm:
1951 src = String(p.properties.Arch.Arm.Src)
1952 case android.Arm64:
1953 src = String(p.properties.Arch.Arm64.Src)
1954 case android.X86:
1955 src = String(p.properties.Arch.X86.Src)
1956 case android.X86_64:
1957 src = String(p.properties.Arch.X86_64.Src)
1958 default:
1959 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1960 return
1961 }
1962 if src == "" {
1963 src = String(p.properties.Src)
1964 }
1965 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001966}
1967
Jiyong Park03b68dd2019-07-26 23:20:40 +09001968func (p *Prebuilt) isForceDisabled() bool {
1969 return p.properties.ForceDisable
1970}
1971
Colin Cross41955e82019-05-29 14:40:35 -07001972func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1973 switch tag {
1974 case "":
1975 return android.Paths{p.outputApex}, nil
1976 default:
1977 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1978 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001979}
1980
Jiyong Park4d277042019-04-23 18:00:10 +09001981func (p *Prebuilt) InstallFilename() string {
1982 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1983}
1984
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001985func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001986 if p.properties.ForceDisable {
1987 return
1988 }
1989
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001990 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001991 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001992 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001993 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001994 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1995 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1996 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001997 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1998 ctx.Build(pctx, android.BuildParams{
1999 Rule: android.Cp,
2000 Input: p.inputApex,
2001 Output: p.outputApex,
2002 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002003 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002004 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002005 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002006
2007 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002008}
2009
2010func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2011 return &p.prebuilt
2012}
2013
2014func (p *Prebuilt) Name() string {
2015 return p.prebuilt.Name(p.ModuleBase.Name())
2016}
2017
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002018func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2019 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002020 Class: "ETC",
2021 OutputFile: android.OptionalPathForPath(p.inputApex),
2022 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002023 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2024 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002025 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002026 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2027 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2028 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2029 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002030 },
2031 }
2032}
2033
2034// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2035func PrebuiltFactory() android.Module {
2036 module := &Prebuilt{}
2037 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002038 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002039 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002040 return module
2041}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002042
2043func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2044 // small helper to add symlink commands
2045 addSymlink := func(target, dir, linkName string) {
2046 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2047 link := filepath.Join(outDir, linkName)
2048 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2049 }
2050
2051 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2052 // When all hard-coded references are fixed, remove symbolic links
2053 // Note that we should keep following symlinks for older VNDKs (<=29)
2054 // Since prebuilt vndk libs still depend on system/lib/vndk path
2055 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2056 // the name of vndk apex is formatted "com.android.vndk.v" + version
2057 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2058 if ctx.Config().Android64() {
2059 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2060 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2061 }
2062 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2063 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2064 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2065 }
2066 }
2067 return
2068}