blob: 1529ea76b7473ad498259ec849452b6ac6f7bcba [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} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090044 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090045 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090047 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090048
Jooyung Hand15aa1f2019-09-27 00:38:03 +090049 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
Jooyung Hane1633032019-08-01 17:41:43 +090050 Command: `rm -f $out && ${jsonmodify} $in ` +
51 `-a provideNativeLibs ${provideNativeLibs} ` +
Jooyung Hand15aa1f2019-09-27 00:38:03 +090052 `-a requireNativeLibs ${requireNativeLibs} ` +
53 `${opt} ` +
54 `-o $out`,
Jooyung Hane1633032019-08-01 17:41:43 +090055 CommandDeps: []string{"${jsonmodify}"},
Jooyung Hand15aa1f2019-09-27 00:38:03 +090056 Description: "prepare ${out}",
57 }, "provideNativeLibs", "requireNativeLibs", "opt")
Jooyung Hane1633032019-08-01 17:41:43 +090058
Jooyung Han01a3ee22019-11-02 02:52:25 +090059 stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{
60 Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`,
61 CommandDeps: []string{"${conv_apex_manifest}"},
62 Description: "strip ${in}=>${out}",
63 })
64
65 pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{
66 Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`,
67 CommandDeps: []string{"${conv_apex_manifest}"},
68 Description: "convert ${in}=>${out}",
69 })
70
Jiyong Park48ca7dc2018-10-10 14:01:00 +090071 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
72 // against the binary policy using sefcontext_compiler -p <policy>.
73
74 // TODO(b/114327326): automate the generation of file_contexts
75 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
76 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010077 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090078 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090079 `${apexer} --force --manifest ${manifest} ` +
Jooyung Han01a3ee22019-11-02 02:52:25 +090080 `--manifest_json ${manifest_json} --manifest_json_full ${manifest_json_full} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090081 `--file_contexts ${file_contexts} ` +
82 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080083 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090084 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090085 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
86 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000087 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010088 Rspfile: "${out}.copy_commands",
89 RspfileContent: "${copy_commands}",
90 Description: "APEX ${image_dir} => ${out}",
Jooyung Han01a3ee22019-11-02 02:52:25 +090091 }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags",
92 "manifest", "manifest_json", "manifest_json_full",
93 )
Colin Crossa4925902018-11-16 11:36:28 -080094
Alex Light5098a612018-11-29 17:12:15 -080095 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
96 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010097 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080098 `APEXER_TOOL_PATH=${tool_path} ` +
Jooyung Han01a3ee22019-11-02 02:52:25 +090099 `${apexer} --force --manifest ${manifest} --manifest_json_full ${manifest_json_full} ` +
Alex Light5098a612018-11-29 17:12:15 -0800100 `--payload_type zip ` +
101 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +0100102 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
103 Rspfile: "${out}.copy_commands",
104 RspfileContent: "${copy_commands}",
105 Description: "ZipAPEX ${image_dir} => ${out}",
Jooyung Han01a3ee22019-11-02 02:52:25 +0900106 }, "tool_path", "image_dir", "copy_commands", "manifest", "manifest_json_full")
Alex Light5098a612018-11-29 17:12:15 -0800107
Colin Crossa4925902018-11-16 11:36:28 -0800108 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
109 blueprint.RuleParams{
110 Command: `${aapt2} convert --output-format proto $in -o $out`,
111 CommandDeps: []string{"${aapt2}"},
112 })
113
114 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +0900115 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +0000116 `apex_payload.img:apex/${abi}.img ` +
117 `apex_manifest.json:root/apex_manifest.json ` +
Jaewoong Jungb00c1fb2019-09-04 13:26:18 -0700118 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
119 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -0800120 CommandDeps: []string{"${zip2zip}"},
121 Description: "app bundle",
122 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100123
124 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
125 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
126 Rspfile: "${out}.emit_commands",
127 RspfileContent: "${emit_commands}",
128 Description: "Emit APEX image content",
129 }, "emit_commands")
130
131 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
132 Command: `diff --unchanged-group-format='' \` +
133 `--changed-group-format='%<' \` +
134 `${image_content_file} ${whitelisted_files_file} || (` +
135 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
136 ` "To fix the build run following command:" && ` +
137 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
138 `exit 1)`,
139 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
140 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900141)
142
Jooyung Han72bd2f82019-10-23 16:46:38 +0900143const (
144 imageApexSuffix = ".apex"
145 zipApexSuffix = ".zipapex"
Sundong Ahnabb64432019-10-22 13:58:29 +0900146 flattenedSuffix = ".flattened"
Alex Light5098a612018-11-29 17:12:15 -0800147
Sundong Ahnabb64432019-10-22 13:58:29 +0900148 imageApexType = "image"
149 zipApexType = "zip"
150 flattenedApexType = "flattened"
Jooyung Han72bd2f82019-10-23 16:46:38 +0900151
152 vndkApexNamePrefix = "com.android.vndk.v"
153)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900154
155type dependencyTag struct {
156 blueprint.BaseDependencyTag
157 name string
158}
159
160var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900161 sharedLibTag = dependencyTag{name: "sharedLib"}
162 executableTag = dependencyTag{name: "executable"}
163 javaLibTag = dependencyTag{name: "javaLib"}
164 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100165 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900166 keyTag = dependencyTag{name: "key"}
167 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900168 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900169 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900170)
171
172func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700173 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900174 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900175 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100176 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
177 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
178 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
179 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000180 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100181 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
182 } else {
183 return pctx.HostBinToolPath(ctx, tool).String()
184 }
185 })
186 }
187 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900188 pctx.HostBinToolVariable("avbtool", "avbtool")
189 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
190 pctx.HostBinToolVariable("merge_zips", "merge_zips")
191 pctx.HostBinToolVariable("mke2fs", "mke2fs")
192 pctx.HostBinToolVariable("resize2fs", "resize2fs")
193 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
194 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800195 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900196 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900197 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jooyung Han01a3ee22019-11-02 02:52:25 +0900198 pctx.HostBinToolVariable("conv_apex_manifest", "conv_apex_manifest")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900199
Jiyong Parkd1063c12019-07-17 20:08:41 +0900200 android.RegisterModuleType("apex", BundleFactory)
Alex Light0851b882019-02-07 13:20:53 -0800201 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jooyung Han344d5432019-08-23 11:17:39 +0900202 android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900203 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700204 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900205
Jooyung Han31c470b2019-10-18 16:26:59 +0900206 android.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900207 android.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han7a78a922019-10-08 21:59:58 +0900208
209 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
210 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
211 sort.Strings(*apexFileContextsInfos)
212 ctx.Strict("APEX_FILE_CONTEXTS_INFOS", strings.Join(*apexFileContextsInfos, " "))
213 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900214}
215
Jooyung Han31c470b2019-10-18 16:26:59 +0900216func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
217 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
218 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
219}
220
Jiyong Parkd1063c12019-07-17 20:08:41 +0900221func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
222 ctx.TopDown("apex_deps", apexDepsMutator)
223 ctx.BottomUp("apex", apexMutator).Parallel()
224 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
225 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900226}
227
Jooyung Han344d5432019-08-23 11:17:39 +0900228var (
229 vndkApexListKey = android.NewOnceKey("vndkApexList")
230 vndkApexListMutex sync.Mutex
231)
232
Jooyung Han31c470b2019-10-18 16:26:59 +0900233func vndkApexList(config android.Config) map[string]string {
Jooyung Han344d5432019-08-23 11:17:39 +0900234 return config.Once(vndkApexListKey, func() interface{} {
Jooyung Han31c470b2019-10-18 16:26:59 +0900235 return map[string]string{}
236 }).(map[string]string)
Jooyung Han344d5432019-08-23 11:17:39 +0900237}
238
Jooyung Han31c470b2019-10-18 16:26:59 +0900239func apexVndkMutator(mctx android.TopDownMutatorContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900240 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
241 if ab.IsNativeBridgeSupported() {
242 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
243 }
Jooyung Han90eee022019-10-01 20:02:42 +0900244
Jooyung Han31c470b2019-10-18 16:26:59 +0900245 vndkVersion := ab.vndkVersion(mctx.DeviceConfig())
246 // Ensure VNDK APEX mount point is formatted as com.android.vndk.v###
Jooyung Han72bd2f82019-10-23 16:46:38 +0900247 ab.properties.Apex_name = proptools.StringPtr(vndkApexNamePrefix + vndkVersion)
Jooyung Han90eee022019-10-01 20:02:42 +0900248
Jooyung Han31c470b2019-10-18 16:26:59 +0900249 // vndk_version should be unique
Jooyung Han344d5432019-08-23 11:17:39 +0900250 vndkApexListMutex.Lock()
251 defer vndkApexListMutex.Unlock()
252 vndkApexList := vndkApexList(mctx.Config())
253 if other, ok := vndkApexList[vndkVersion]; ok {
Jooyung Han31c470b2019-10-18 16:26:59 +0900254 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other)
Jooyung Han344d5432019-08-23 11:17:39 +0900255 }
Jooyung Han31c470b2019-10-18 16:26:59 +0900256 vndkApexList[vndkVersion] = mctx.ModuleName()
Jooyung Han344d5432019-08-23 11:17:39 +0900257 }
258}
259
Jooyung Han31c470b2019-10-18 16:26:59 +0900260func apexVndkDepsMutator(mctx android.BottomUpMutatorContext) {
261 if m, ok := mctx.Module().(*cc.Module); ok && cc.IsForVndkApex(mctx, m) {
262 vndkVersion := m.VndkVersion()
Jooyung Han344d5432019-08-23 11:17:39 +0900263 vndkApexList := vndkApexList(mctx.Config())
Jooyung Han31c470b2019-10-18 16:26:59 +0900264 if vndkApex, ok := vndkApexList[vndkVersion]; ok {
265 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, vndkApex)
Jooyung Han344d5432019-08-23 11:17:39 +0900266 }
267 }
268}
269
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900270// Mark the direct and transitive dependencies of apex bundles so that they
271// can be built for the apex bundles.
272func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800273 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800274 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900275 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900276 depName := mctx.OtherModuleName(child)
277 // If the parent is apexBundle, this child is directly depended.
278 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800279 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800280 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
281 // non-installable apex's cannot be installed and so should not prevent libraries from being
282 // installed to the system.
283 android.UpdateApexDependency(apexBundleName, depName, directDep)
284 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900285
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900286 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900287 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900288 return true
289 } else {
290 return false
291 }
292 })
293 }
294}
295
296// Create apex variations if a module is included in APEX(s).
297func apexMutator(mctx android.BottomUpMutatorContext) {
298 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900299 am.CreateApexVariations(mctx)
Jooyung Han7a78a922019-10-08 21:59:58 +0900300 } else if a, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900301 // apex bundle itself is mutated so that it and its modules have same
302 // apex variant.
303 apexBundleName := mctx.ModuleName()
304 mctx.CreateVariations(apexBundleName)
Jooyung Han7a78a922019-10-08 21:59:58 +0900305
306 // collects APEX list
307 if mctx.Device() && a.installable() {
308 addApexFileContextsInfos(mctx, a)
309 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900310 }
311}
Sundong Ahne9b55722019-09-06 17:37:42 +0900312
Jooyung Han7a78a922019-10-08 21:59:58 +0900313var (
314 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
315 apexFileContextsInfosMutex sync.Mutex
316)
317
318func apexFileContextsInfos(config android.Config) *[]string {
319 return config.Once(apexFileContextsInfosKey, func() interface{} {
320 return &[]string{}
321 }).(*[]string)
322}
323
324func addApexFileContextsInfos(ctx android.BaseModuleContext, a *apexBundle) {
325 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
326 fileContextsName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
327
328 apexFileContextsInfosMutex.Lock()
329 defer apexFileContextsInfosMutex.Unlock()
330 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
331 *apexFileContextsInfos = append(*apexFileContextsInfos, apexName+":"+fileContextsName)
332}
333
Sundong Ahne9b55722019-09-06 17:37:42 +0900334func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900335 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +0900336 var variants []string
337 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
338 case "image":
339 variants = append(variants, imageApexType, flattenedApexType)
340 case "zip":
341 variants = append(variants, zipApexType)
342 case "both":
343 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
344 default:
345 mctx.PropertyErrorf("type", "%q is not one of \"image\" or \"zip\".", *ab.properties.Payload_type)
346 return
347 }
348
349 modules := mctx.CreateLocalVariations(variants...)
350
351 for i, v := range variants {
352 switch v {
353 case imageApexType:
354 modules[i].(*apexBundle).properties.ApexType = imageApex
355 case zipApexType:
356 modules[i].(*apexBundle).properties.ApexType = zipApex
357 case flattenedApexType:
358 modules[i].(*apexBundle).properties.ApexType = flattenedApex
359 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900360 }
361 }
362}
363
Jooyung Han5c998b92019-06-27 11:30:33 +0900364func apexUsesMutator(mctx android.BottomUpMutatorContext) {
365 if ab, ok := mctx.Module().(*apexBundle); ok {
366 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
367 }
368}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900369
Jooyung Handc782442019-11-01 03:14:38 +0900370var (
371 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
372)
373
374// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
375// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
376// which may cause compatibility issues. (e.g. libbinder)
377// Even though libbinder restricts its availability via 'apex_available' property and relies on
378// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
379// to avoid similar problems.
380func useVendorWhitelist(config android.Config) []string {
381 return config.Once(useVendorWhitelistKey, func() interface{} {
382 return []string{
383 // swcodec uses "vendor" variants for smaller size
384 "com.android.media.swcodec",
385 "test_com.android.media.swcodec",
386 }
387 }).([]string)
388}
389
390// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
391// called before the first call to useVendorWhitelist()
392func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
393 config.Once(useVendorWhitelistKey, func() interface{} {
394 return whitelist
395 })
396}
397
Alex Light9670d332019-01-29 18:07:33 -0800398type apexNativeDependencies struct {
399 // List of native libraries
400 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900401
Alex Light9670d332019-01-29 18:07:33 -0800402 // List of native executables
403 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900404
Roland Levillain630846d2019-06-26 12:48:34 +0100405 // List of native tests
406 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800407}
Jooyung Han344d5432019-08-23 11:17:39 +0900408
Alex Light9670d332019-01-29 18:07:33 -0800409type apexMultilibProperties struct {
410 // Native dependencies whose compile_multilib is "first"
411 First apexNativeDependencies
412
413 // Native dependencies whose compile_multilib is "both"
414 Both apexNativeDependencies
415
416 // Native dependencies whose compile_multilib is "prefer32"
417 Prefer32 apexNativeDependencies
418
419 // Native dependencies whose compile_multilib is "32"
420 Lib32 apexNativeDependencies
421
422 // Native dependencies whose compile_multilib is "64"
423 Lib64 apexNativeDependencies
424}
425
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900426type apexBundleProperties struct {
427 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000428 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800429 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900430
Jiyong Park40e26a22019-02-08 02:53:06 +0900431 // AndroidManifest.xml file used for the zip container of this APEX bundle.
432 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800433 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900434
Roland Levillain411c5842019-09-19 16:37:20 +0100435 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
436 // device (/apex/<apex_name>).
437 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900438 Apex_name *string
439
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900440 // Determines the file contexts file for setting security context to each file in this APEX bundle.
441 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
442 // used.
443 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900444 File_contexts *string
445
446 // List of native shared libs that are embedded inside this APEX bundle
447 Native_shared_libs []string
448
Roland Levillain630846d2019-06-26 12:48:34 +0100449 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900450 Binaries []string
451
452 // List of java libraries that are embedded inside this APEX bundle
453 Java_libs []string
454
455 // List of prebuilt files that are embedded inside this APEX bundle
456 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900457
Roland Levillain630846d2019-06-26 12:48:34 +0100458 // List of tests that are embedded inside this APEX bundle
459 Tests []string
460
Jiyong Parkff1458f2018-10-12 21:49:38 +0900461 // Name of the apex_key module that provides the private key to sign APEX
462 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900463
Alex Light5098a612018-11-29 17:12:15 -0800464 // The type of APEX to build. Controls what the APEX payload is. Either
465 // 'image', 'zip' or 'both'. Default: 'image'.
466 Payload_type *string
467
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900468 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
469 // or an android_app_certificate module name in the form ":module".
470 Certificate *string
471
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900472 // Whether this APEX is installable to one of the partitions. Default: true.
473 Installable *bool
474
Jiyong Parkda6eb592018-12-19 17:12:36 +0900475 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
476 // Default is false.
477 Use_vendor *bool
478
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800479 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
480 Ignore_system_library_special_case *bool
481
Alex Light9670d332019-01-29 18:07:33 -0800482 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900483
Jiyong Parkf97782b2019-02-13 20:28:58 +0900484 // List of sanitizer names that this APEX is enabled for
485 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900486
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900487 PreventInstall bool `blueprint:"mutated"`
488
489 HideFromMake bool `blueprint:"mutated"`
490
Jooyung Han5c998b92019-06-27 11:30:33 +0900491 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
492 Provide_cpp_shared_libs *bool
493
494 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
495 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100496
497 // A txt file containing list of files that are whitelisted to be included in this APEX.
498 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900499
500 // List of APKs to package inside APEX
501 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900502
Sundong Ahnabb64432019-10-22 13:58:29 +0900503 // package format of this apex variant; could be non-flattened, flattened, or zip.
504 // imageApex, zipApex or flattened
505 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900506
Jiyong Parkd1063c12019-07-17 20:08:41 +0900507 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
508 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
509 // is implied. This value affects all modules included in this APEX. In other words, they are
510 // also built with the SDKs specified here.
511 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800512}
513
514type apexTargetBundleProperties struct {
515 Target struct {
516 // Multilib properties only for android.
517 Android struct {
518 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900519 }
Jooyung Han344d5432019-08-23 11:17:39 +0900520
Alex Light9670d332019-01-29 18:07:33 -0800521 // Multilib properties only for host.
522 Host struct {
523 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900524 }
Jooyung Han344d5432019-08-23 11:17:39 +0900525
Alex Light9670d332019-01-29 18:07:33 -0800526 // Multilib properties only for host linux_bionic.
527 Linux_bionic struct {
528 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900529 }
Jooyung Han344d5432019-08-23 11:17:39 +0900530
Alex Light9670d332019-01-29 18:07:33 -0800531 // Multilib properties only for host linux_glibc.
532 Linux_glibc struct {
533 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900534 }
535 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900536}
537
Jooyung Han344d5432019-08-23 11:17:39 +0900538type apexVndkProperties struct {
539 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
540 Vndk_version *string
541}
542
Jiyong Park8fd61922018-11-08 02:50:25 +0900543type apexFileClass int
544
545const (
546 etc apexFileClass = iota
547 nativeSharedLib
548 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900549 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800550 pyBinary
551 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900552 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100553 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900554 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900555)
556
Alex Light5098a612018-11-29 17:12:15 -0800557type apexPackaging int
558
559const (
560 imageApex apexPackaging = iota
561 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900562 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800563)
564
Sundong Ahnabb64432019-10-22 13:58:29 +0900565// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800566func (a apexPackaging) suffix() string {
567 switch a {
568 case imageApex:
569 return imageApexSuffix
570 case zipApex:
571 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800572 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100573 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800574 }
575}
576
577func (a apexPackaging) name() string {
578 switch a {
579 case imageApex:
580 return imageApexType
581 case zipApex:
582 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800583 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100584 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800585 }
586}
587
Jiyong Park8fd61922018-11-08 02:50:25 +0900588func (class apexFileClass) NameInMake() string {
589 switch class {
590 case etc:
591 return "ETC"
592 case nativeSharedLib:
593 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800594 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900595 return "EXECUTABLES"
596 case javaSharedLib:
597 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100598 case nativeTest:
599 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900600 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900601 // b/142537672 Why isn't this APP? We want to have full control over
602 // the paths and file names of the apk file under the flattend APEX.
603 // If this is set to APP, then the paths and file names are modified
604 // by the Make build system. For example, it is installed to
605 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
606 // /system/apex/<apexname>/app/<Appname> because the build system automatically
607 // appends module name (which is <apexname>.<Appname> to the path.
608 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900609 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100610 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900611 }
612}
613
614type apexFile struct {
615 builtFile android.Path
616 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900617 installDir string
618 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900619 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800620 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900621}
622
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900623type apexBundle struct {
624 android.ModuleBase
625 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900626 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900627
Alex Light9670d332019-01-29 18:07:33 -0800628 properties apexBundleProperties
629 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900630 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900631
Colin Crossa4925902018-11-16 11:36:28 -0800632 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900633 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700634 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900635
Jiyong Park03b68dd2019-07-26 23:20:40 +0900636 prebuiltFileToDelete string
637
Jiyong Park42cca6c2019-04-01 11:15:50 +0900638 public_key_file android.Path
639 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900640
641 container_certificate_file android.Path
642 container_private_key_file android.Path
643
Jiyong Park8fd61922018-11-08 02:50:25 +0900644 // list of files to be included in this apex
645 filesInfo []apexFile
646
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900647 // list of module names that this APEX is depending on
648 externalDeps []string
649
Sundong Ahnabb64432019-10-22 13:58:29 +0900650 testApex bool
651 vndkApex bool
652 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900653
654 // intermediate path for apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +0900655 manifestJsonOut android.WritablePath
656 manifestJsonFullOut android.WritablePath
657 manifestPbOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900658
659 // list of commands to create symlinks for backward compatibility
660 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
661 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
662 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
663 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900664
665 // Suffix of module name in Android.mk
666 // ".flattened", ".apex", ".zipapex", or ""
667 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900668}
669
Jiyong Park397e55e2018-10-24 21:09:55 +0900670func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100671 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700672 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900673 // Use *FarVariation* to be able to depend on modules having
674 // conflicting variations with this module. This is required since
675 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
676 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700677 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900678 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900679 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900680 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700681 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900682
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700683 ctx.AddFarVariationDependencies(append(target.Variations(),
684 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
685 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100686
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700687 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100688 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100689 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700690 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900691}
692
Alex Light9670d332019-01-29 18:07:33 -0800693func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
694 if ctx.Os().Class == android.Device {
695 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
696 } else {
697 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
698 if ctx.Os().Bionic() {
699 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
700 } else {
701 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
702 }
703 }
704}
705
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900706func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900707 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
708 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
709 }
710
Jiyong Park397e55e2018-10-24 21:09:55 +0900711 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900712 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800713
714 a.combineProperties(ctx)
715
Jiyong Park397e55e2018-10-24 21:09:55 +0900716 has32BitTarget := false
717 for _, target := range targets {
718 if target.Arch.ArchType.Multilib == "lib32" {
719 has32BitTarget = true
720 }
721 }
722 for i, target := range targets {
723 // When multilib.* is omitted for native_shared_libs, it implies
724 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700725 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900726 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900727 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700728 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900729
Roland Levillain630846d2019-06-26 12:48:34 +0100730 // When multilib.* is omitted for tests, it implies
731 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700732 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100733 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100734 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700735 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100736
Jiyong Park397e55e2018-10-24 21:09:55 +0900737 // Add native modules targetting both ABIs
738 addDependenciesForNativeModules(ctx,
739 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100740 a.properties.Multilib.Both.Binaries,
741 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700742 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900743 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900744
Alex Light3d673592019-01-18 14:37:31 -0800745 isPrimaryAbi := i == 0
746 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900747 // When multilib.* is omitted for binaries, it implies
748 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700749 ctx.AddFarVariationDependencies(append(target.Variations(),
750 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
751 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900752
753 // Add native modules targetting the first ABI
754 addDependenciesForNativeModules(ctx,
755 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100756 a.properties.Multilib.First.Binaries,
757 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700758 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900759 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800760
761 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700762 ctx.AddFarVariationDependencies(target.Variations(),
763 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900764 }
765
766 switch target.Arch.ArchType.Multilib {
767 case "lib32":
768 // Add native modules targetting 32-bit ABI
769 addDependenciesForNativeModules(ctx,
770 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100771 a.properties.Multilib.Lib32.Binaries,
772 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700773 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900774 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900775
776 addDependenciesForNativeModules(ctx,
777 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100778 a.properties.Multilib.Prefer32.Binaries,
779 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700780 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900781 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900782 case "lib64":
783 // Add native modules targetting 64-bit ABI
784 addDependenciesForNativeModules(ctx,
785 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100786 a.properties.Multilib.Lib64.Binaries,
787 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700788 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900789 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900790
791 if !has32BitTarget {
792 addDependenciesForNativeModules(ctx,
793 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100794 a.properties.Multilib.Prefer32.Binaries,
795 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700796 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900797 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900798 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700799
800 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
801 for _, sanitizer := range ctx.Config().SanitizeDevice() {
802 if sanitizer == "hwaddress" {
803 addDependenciesForNativeModules(ctx,
804 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700805 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700806 break
807 }
808 }
809 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900810 }
811
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900812 }
813
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700814 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
815 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900816
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700817 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
818 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900819
Jiyong Park23c52b02019-02-02 13:13:47 +0900820 if String(a.properties.Key) == "" {
821 ctx.ModuleErrorf("key is missing")
822 return
823 }
824 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900825
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900826 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900827 if cert != "" {
828 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900829 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900830
831 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
832 if len(a.properties.Uses_sdks) > 0 {
833 sdkRefs := []android.SdkRef{}
834 for _, str := range a.properties.Uses_sdks {
835 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
836 sdkRefs = append(sdkRefs, parsed)
837 }
838 a.BuildWithSdks(sdkRefs)
839 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900840}
841
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900842func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
843 // direct deps of an APEX bundle are all part of the APEX bundle
844 return true
845}
846
Colin Cross0ea8ba82019-06-06 14:33:29 -0700847func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900848 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
849 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000850 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900851 }
852 return String(a.properties.Certificate)
853}
854
Colin Cross41955e82019-05-29 14:40:35 -0700855func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
856 switch tag {
857 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900858 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700859 default:
860 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900861 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900862}
863
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900864func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900865 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900866}
867
Jiyong Park7c1dc612019-01-05 11:15:24 +0900868func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900869 if a.vndkApex {
870 return "vendor." + a.vndkVersion(config)
871 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900872 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900873 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900874 } else {
875 return "core"
876 }
877}
878
Jiyong Parkf97782b2019-02-13 20:28:58 +0900879func (a *apexBundle) EnableSanitizer(sanitizerName string) {
880 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
881 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
882 }
883}
884
Jiyong Park388ef3f2019-01-28 19:47:32 +0900885func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900886 if android.InList(sanitizerName, a.properties.SanitizerNames) {
887 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900888 }
889
890 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900891 globalSanitizerNames := []string{}
892 if a.Host() {
893 globalSanitizerNames = ctx.Config().SanitizeHost()
894 } else {
895 arches := ctx.Config().SanitizeDeviceArch()
896 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
897 globalSanitizerNames = ctx.Config().SanitizeDevice()
898 }
899 }
900 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900901}
902
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900903func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
904 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
905}
906
907func (a *apexBundle) PreventInstall() {
908 a.properties.PreventInstall = true
909}
910
911func (a *apexBundle) HideFromMake() {
912 a.properties.HideFromMake = true
913}
914
Martin Stjernholm279de572019-09-10 23:18:20 +0100915func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900916 // Decide the APEX-local directory by the multilib of the library
917 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100918 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900919 case "lib32":
920 dirInApex = "lib"
921 case "lib64":
922 dirInApex = "lib64"
923 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100924 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700925 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100926 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900927 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100928 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
929 // Special case for Bionic libs and other libs installed with them. This is
930 // to prevent those libs from being included in the search path
931 // /apex/com.android.runtime/${LIB}. This exclusion is required because
932 // those libs in the Runtime APEX are available via the legacy paths in
933 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
934 // to the legacy paths and thus will be loaded into the default linker
935 // namespace (aka "platform" namespace). If the libs are directly in
936 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
937 // into the runtime linker namespace, which will result in double loading of
938 // them, which isn't supported.
939 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900940 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900941
Martin Stjernholm279de572019-09-10 23:18:20 +0100942 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900943 return
944}
945
946func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900947 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700948 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200949 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900950 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900951 fileToCopy = cc.OutputFile().Path()
952 return
953}
954
Alex Light778127a2019-02-27 14:19:50 -0800955func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
956 dirInApex = "bin"
957 fileToCopy = py.HostToolPath().Path()
958 return
959}
960func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
961 dirInApex = "bin"
962 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
963 if err != nil {
964 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
965 return
966 }
967 fileToCopy = android.PathForOutput(ctx, s)
968 return
969}
970
Jiyong Park04480cf2019-02-06 00:16:29 +0900971func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
972 dirInApex = filepath.Join("bin", sh.SubDir())
973 fileToCopy = sh.OutputFile()
974 return
975}
976
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900977func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
978 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900979 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900980 return
981}
982
Jiyong Park9e6c2422019-08-09 20:39:45 +0900983func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
984 dirInApex = "javalib"
985 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
986 implJars := java.ImplementationJars()
987 if len(implJars) != 1 {
988 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
989 strings.Join(implJars.Strings(), ", ")))
990 }
991 fileToCopy = implJars[0]
992 return
993}
994
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900995func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
996 dirInApex = filepath.Join("etc", prebuilt.SubDir())
997 fileToCopy = prebuilt.OutputFile()
998 return
999}
1000
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001001func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +09001002 appDir := "app"
1003 if app.Privileged() {
1004 appDir = "priv-app"
1005 }
1006 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001007 fileToCopy = app.OutputFile()
1008 return
1009}
1010
Dario Frenicde2a032019-10-27 00:29:22 +01001011func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
1012 appDir := "app"
1013 if app.Privileged() {
1014 appDir = "priv-app"
1015 }
1016 dirInApex = filepath.Join(appDir, pkgName)
1017 fileToCopy = app.OutputFile()
1018 return
1019}
1020
Roland Levillain935639d2019-08-13 14:55:28 +01001021// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1022type flattenedApexContext struct {
1023 android.ModuleContext
1024}
1025
1026func (c *flattenedApexContext) InstallBypassMake() bool {
1027 return true
1028}
1029
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001030func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001031 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001032
Sundong Ahnabb64432019-10-22 13:58:29 +09001033 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1034 switch a.properties.ApexType {
1035 case imageApex:
1036 if buildFlattenedAsDefault {
1037 a.suffix = imageApexSuffix
1038 } else {
1039 a.suffix = ""
1040 a.primaryApexType = true
1041 }
1042 case zipApex:
1043 if proptools.String(a.properties.Payload_type) == "zip" {
1044 a.suffix = ""
1045 a.primaryApexType = true
1046 } else {
1047 a.suffix = zipApexSuffix
1048 }
1049 case flattenedApex:
1050 if buildFlattenedAsDefault {
1051 a.suffix = ""
1052 a.primaryApexType = true
1053 } else {
1054 a.suffix = flattenedSuffix
1055 }
Alex Light5098a612018-11-29 17:12:15 -08001056 }
1057
Roland Levillain630846d2019-06-26 12:48:34 +01001058 if len(a.properties.Tests) > 0 && !a.testApex {
1059 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1060 return
1061 }
1062
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001063 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1064
Jooyung Hane1633032019-08-01 17:41:43 +09001065 // native lib dependencies
1066 var provideNativeLibs []string
1067 var requireNativeLibs []string
1068
Jooyung Han5c998b92019-06-27 11:30:33 +09001069 // Check if "uses" requirements are met with dependent apexBundles
1070 var providedNativeSharedLibs []string
1071 useVendor := proptools.Bool(a.properties.Use_vendor)
1072 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1073 if ctx.OtherModuleDependencyTag(m) != usesTag {
1074 return
1075 }
1076 otherName := ctx.OtherModuleName(m)
1077 other, ok := m.(*apexBundle)
1078 if !ok {
1079 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1080 return
1081 }
1082 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1083 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1084 return
1085 }
1086 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1087 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1088 return
1089 }
1090 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1091 })
1092
Alex Light778127a2019-02-27 14:19:50 -08001093 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001094 depTag := ctx.OtherModuleDependencyTag(child)
1095 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001096 if _, ok := parent.(*apexBundle); ok {
1097 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001098 switch depTag {
1099 case sharedLibTag:
1100 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001101 if cc.HasStubsVariants() {
1102 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1103 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001104 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001105 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001106 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001107 } else {
1108 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001109 }
1110 case executableTag:
1111 if cc, ok := child.(*cc.Module); ok {
1112 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001113 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001114 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001115 } else if sh, ok := child.(*android.ShBinary); ok {
1116 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001117 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001118 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1119 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1120 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1121 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1122 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1123 // NB: Since go binaries are static we don't need the module for anything here, which is
1124 // good since the go tool is a blueprint.Module not an android.Module like we would
1125 // normally use.
1126 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001127 } else {
Alex Light778127a2019-02-27 14:19:50 -08001128 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 +09001129 }
1130 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001131 if javaLib, ok := child.(*java.Library); ok {
1132 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001133 if fileToCopy == nil {
1134 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1135 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001136 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1137 }
1138 return true
1139 } else if javaLib, ok := child.(*java.Import); ok {
1140 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1141 if fileToCopy == nil {
1142 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1143 } else {
1144 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001145 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001146 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001147 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001148 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001149 }
1150 case prebuiltTag:
1151 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
1152 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001153 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001154 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001155 } else {
1156 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1157 }
Roland Levillain630846d2019-06-26 12:48:34 +01001158 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001159 if ccTest, ok := child.(*cc.Module); ok {
1160 if ccTest.IsTestPerSrcAllTestsVariation() {
1161 // Multiple-output test module (where `test_per_src: true`).
1162 //
1163 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1164 // We do not add this variation to `filesInfo`, as it has no output;
1165 // however, we do add the other variations of this module as indirect
1166 // dependencies (see below).
1167 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001168 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001169 // Single-output test module (where `test_per_src: false`).
1170 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1171 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001172 }
Roland Levillain630846d2019-06-26 12:48:34 +01001173 return true
1174 } else {
1175 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1176 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001177 case keyTag:
1178 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001179 a.private_key_file = key.private_key_file
1180 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001181 return false
1182 } else {
1183 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001184 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001185 case certificateTag:
1186 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001187 a.container_certificate_file = dep.Certificate.Pem
1188 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001189 return false
1190 } else {
1191 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1192 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001193 case android.PrebuiltDepTag:
1194 // If the prebuilt is force disabled, remember to delete the prebuilt file
1195 // that might have been installed in the previous builds
1196 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1197 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1198 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001199 case androidAppTag:
1200 if ap, ok := child.(*java.AndroidApp); ok {
1201 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1202 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1203 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001204 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1205 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1206 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001207 } else {
1208 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1209 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001210 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001211 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001212 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001213 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001214 // We cannot use a switch statement on `depTag` here as the checked
1215 // tags used below are private (e.g. `cc.sharedDepTag`).
1216 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1217 if cc, ok := child.(*cc.Module); ok {
1218 if android.InList(cc.Name(), providedNativeSharedLibs) {
1219 // If we're using a shared library which is provided from other APEX,
1220 // don't include it in this APEX
1221 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001222 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001223 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1224 // If the dependency is a stubs lib, don't include it in this APEX,
1225 // but make sure that the lib is installed on the device.
1226 // In case no APEX is having the lib, the lib is installed to the system
1227 // partition.
1228 //
1229 // Always include if we are a host-apex however since those won't have any
1230 // system libraries.
1231 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1232 a.externalDeps = append(a.externalDeps, cc.Name())
1233 }
Jooyung Hane1633032019-08-01 17:41:43 +09001234 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001235 // Don't track further
1236 return false
1237 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001238 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001239 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1240 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001241 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001242 } else if cc.IsTestPerSrcDepTag(depTag) {
1243 if cc, ok := child.(*cc.Module); ok {
1244 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1245 // Handle modules created as `test_per_src` variations of a single test module:
1246 // use the name of the generated test binary (`fileToCopy`) instead of the name
1247 // of the original test module (`depName`, shared by all `test_per_src`
1248 // variations of that module).
1249 moduleName := filepath.Base(fileToCopy.String())
1250 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1251 return true
1252 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001253 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001254 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001255 }
1256 }
1257 }
1258 return false
1259 })
1260
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001261 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001262 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1263 return
1264 }
1265
Jiyong Park8fd61922018-11-08 02:50:25 +09001266 // remove duplicates in filesInfo
1267 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001268 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001269 result := []apexFile{}
1270 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001271 dest := filepath.Join(f.installDir, f.builtFile.Base())
1272 if !encountered[dest] {
1273 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001274 result = append(result, f)
1275 }
1276 }
1277 return result
1278 }
1279 filesInfo = removeDup(filesInfo)
1280
1281 // to have consistent build rules
1282 sort.Slice(filesInfo, func(i, j int) bool {
1283 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1284 })
1285
Jiyong Park127b40b2019-09-30 16:04:35 +09001286 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001287 if !ctx.Host() {
1288 for _, fi := range filesInfo {
1289 if am, ok := fi.module.(android.ApexModule); ok {
1290 if !am.AvailableFor(ctx.ModuleName()) {
1291 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1292 return
1293 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001294 }
1295 }
1296 }
1297
Jiyong Park8fd61922018-11-08 02:50:25 +09001298 // prepend the name of this APEX to the module names. These names will be the names of
1299 // modules that will be defined if the APEX is flattened.
1300 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001301 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001302 }
1303
Jiyong Park8fd61922018-11-08 02:50:25 +09001304 a.installDir = android.PathForModuleInstall(ctx, "apex")
1305 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001306
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001307 // prepare apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +09001308 a.buildManifest(ctx, provideNativeLibs, requireNativeLibs)
1309
1310 a.setCertificateAndPrivateKey(ctx)
1311 if a.properties.ApexType == flattenedApex {
1312 a.buildFlattenedApex(ctx)
1313 } else {
1314 a.buildUnflattenedApex(ctx)
1315 }
1316
1317 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1318 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
1319}
1320
1321func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
Jooyung Hane1633032019-08-01 17:41:43 +09001322 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001323
Jooyung Han01a3ee22019-11-02 02:52:25 +09001324 a.manifestJsonFullOut = android.PathForModuleOut(ctx, "apex_manifest_full.json")
1325
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001326 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001327 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1328 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001329
1330 // apex name can be overridden
1331 optCommands := []string{}
1332 if a.properties.Apex_name != nil {
1333 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1334 }
1335
Jooyung Hane1633032019-08-01 17:41:43 +09001336 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001337 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001338 Input: manifestSrc,
Jooyung Han01a3ee22019-11-02 02:52:25 +09001339 Output: a.manifestJsonFullOut,
Jooyung Hane1633032019-08-01 17:41:43 +09001340 Args: map[string]string{
1341 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1342 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001343 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001344 },
1345 })
1346
Jooyung Han01a3ee22019-11-02 02:52:25 +09001347 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
1348 // prepare stripp-downed version so that APEX modules built from R+ can be installed to Q
1349 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1350 ctx.Build(pctx, android.BuildParams{
1351 Rule: stripApexManifestRule,
1352 Input: a.manifestJsonFullOut,
1353 Output: a.manifestJsonOut,
1354 })
Jooyung Han72bd2f82019-10-23 16:46:38 +09001355
Jooyung Han01a3ee22019-11-02 02:52:25 +09001356 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
1357 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
1358 ctx.Build(pctx, android.BuildParams{
1359 Rule: pbApexManifestRule,
1360 Input: a.manifestJsonFullOut,
1361 Output: a.manifestPbOut,
1362 })
Jiyong Park8fd61922018-11-08 02:50:25 +09001363}
1364
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001365func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001366 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001367 for _, f := range a.filesInfo {
1368 if f.module != nil {
1369 notice := f.module.NoticeFile()
1370 if notice.Valid() {
1371 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001372 }
1373 }
1374 }
1375 // append the notice file specified in the apex module itself
1376 if a.NoticeFile().Valid() {
1377 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001378 }
1379
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001380 if len(noticeFiles) == 0 {
1381 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001382 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001383
Jaewoong Jung98772792019-07-01 17:15:13 -07001384 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001385}
1386
Sundong Ahnabb64432019-10-22 13:58:29 +09001387func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001388 var abis []string
1389 for _, target := range ctx.MultiTargets() {
1390 if len(target.Arch.Abi) > 0 {
1391 abis = append(abis, target.Arch.Abi[0])
1392 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001393 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001394
Alex Light5098a612018-11-29 17:12:15 -08001395 abis = android.FirstUniqueStrings(abis)
1396
Sundong Ahnabb64432019-10-22 13:58:29 +09001397 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001398 suffix := apexType.suffix()
1399 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001400
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001401 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001402 for _, f := range a.filesInfo {
1403 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001404 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001405
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001406 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001407 emitCommands := []string{}
1408 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1409 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001410 for i, src := range filesToCopy {
1411 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001412 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001413 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001414 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1415 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001416 for _, sym := range a.filesInfo[i].symlinks {
1417 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1418 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1419 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001420 }
Dario Frenie4235822019-10-28 14:49:27 +00001421 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1422
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001423 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Han01a3ee22019-11-02 02:52:25 +09001424 implicitInputs = append(implicitInputs, a.manifestPbOut, a.manifestJsonFullOut, a.manifestJsonOut)
Alex Light5098a612018-11-29 17:12:15 -08001425
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001426 if a.properties.Whitelisted_files != nil {
1427 ctx.Build(pctx, android.BuildParams{
1428 Rule: emitApexContentRule,
1429 Implicits: implicitInputs,
1430 Output: imageContentFile,
1431 Description: "emit apex image content",
1432 Args: map[string]string{
1433 "emit_commands": strings.Join(emitCommands, " && "),
1434 },
1435 })
1436 implicitInputs = append(implicitInputs, imageContentFile)
1437 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1438
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001439 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001440 ctx.Build(pctx, android.BuildParams{
1441 Rule: diffApexContentRule,
1442 Implicits: implicitInputs,
1443 Output: phonyOutput,
1444 Description: "diff apex image content",
1445 Args: map[string]string{
1446 "whitelisted_files_file": whitelistedFilesFile.String(),
1447 "image_content_file": imageContentFile.String(),
1448 "apex_module_name": ctx.ModuleName(),
1449 },
1450 })
1451
1452 implicitInputs = append(implicitInputs, phonyOutput)
1453 }
1454
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001455 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1456 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001457
Sundong Ahnabb64432019-10-22 13:58:29 +09001458 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001459 // files and dirs that will be created in APEX
Jooyung Han01a3ee22019-11-02 02:52:25 +09001460 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
Alex Light5098a612018-11-29 17:12:15 -08001461 var executablePaths []string // this also includes dirs
1462 for _, f := range a.filesInfo {
1463 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001464 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001465 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001466 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001467 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001468 }
Alex Light5098a612018-11-29 17:12:15 -08001469 } else {
1470 readOnlyPaths = append(readOnlyPaths, pathInApex)
1471 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001472 dir := f.installDir
1473 for !android.InList(dir, executablePaths) && dir != "" {
1474 executablePaths = append(executablePaths, dir)
1475 dir, _ = filepath.Split(dir) // move up to the parent
1476 if len(dir) > 0 {
1477 // remove trailing slash
1478 dir = dir[:len(dir)-1]
1479 }
Alex Light5098a612018-11-29 17:12:15 -08001480 }
1481 }
1482 sort.Strings(readOnlyPaths)
1483 sort.Strings(executablePaths)
1484 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1485 ctx.Build(pctx, android.BuildParams{
1486 Rule: generateFsConfig,
1487 Output: cannedFsConfig,
1488 Description: "generate fs config",
1489 Args: map[string]string{
1490 "ro_paths": strings.Join(readOnlyPaths, " "),
1491 "exec_paths": strings.Join(executablePaths, " "),
1492 },
1493 })
1494
1495 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1496 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1497 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1498 if !fileContextsOptionalPath.Valid() {
1499 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1500 return
1501 }
1502 fileContexts := fileContextsOptionalPath.Path()
1503
Jiyong Park835d82b2018-12-27 16:04:18 +09001504 optFlags := []string{}
1505
Alex Light5098a612018-11-29 17:12:15 -08001506 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001507 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1508 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001509
Jiyong Park7f67f482019-01-05 12:57:48 +09001510 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1511 if overridden {
1512 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1513 }
1514
Jiyong Park40e26a22019-02-08 02:53:06 +09001515 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001516 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001517 implicitInputs = append(implicitInputs, androidManifestFile)
1518 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1519 }
1520
Jiyong Park71b519d2019-04-18 17:25:49 +09001521 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1522 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1523 ctx.Config().UnbundledBuild() &&
1524 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1525 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1526 apiFingerprint := java.ApiFingerprintPath(ctx)
1527 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1528 implicitInputs = append(implicitInputs, apiFingerprint)
1529 }
1530 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1531
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001532 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1533 if noticeFile.Valid() {
1534 // If there's a NOTICE file, embed it as an asset file in the APEX.
1535 implicitInputs = append(implicitInputs, noticeFile.Path())
1536 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1537 }
1538
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001539 if !ctx.Config().UnbundledBuild() && a.installable() {
1540 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1541 // don't need hashtree for activation. Therefore, by removing hashtree from
1542 // apex bundle (filesystem image in it, to be specific), we can save storage.
1543 optFlags = append(optFlags, "--no_hashtree")
1544 }
1545
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001546 if a.properties.Apex_name != nil {
1547 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1548 // Note that apex_manifest is also mended.
1549 optFlags = append(optFlags, "--do_not_check_keyname")
1550 }
1551
Alex Light5098a612018-11-29 17:12:15 -08001552 ctx.Build(pctx, android.BuildParams{
1553 Rule: apexRule,
1554 Implicits: implicitInputs,
1555 Output: unsignedOutputFile,
1556 Description: "apex (" + apexType.name() + ")",
1557 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001558 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1559 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1560 "copy_commands": strings.Join(copyCommands, " && "),
1561 "manifest_json_full": a.manifestJsonFullOut.String(),
1562 "manifest_json": a.manifestJsonOut.String(),
1563 "manifest": a.manifestPbOut.String(),
1564 "file_contexts": fileContexts.String(),
1565 "canned_fs_config": cannedFsConfig.String(),
1566 "key": a.private_key_file.String(),
1567 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001568 },
1569 })
1570
1571 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1572 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1573 a.bundleModuleFile = bundleModuleFile
1574
1575 ctx.Build(pctx, android.BuildParams{
1576 Rule: apexProtoConvertRule,
1577 Input: unsignedOutputFile,
1578 Output: apexProtoFile,
1579 Description: "apex proto convert",
1580 })
1581
1582 ctx.Build(pctx, android.BuildParams{
1583 Rule: apexBundleRule,
1584 Input: apexProtoFile,
1585 Output: a.bundleModuleFile,
1586 Description: "apex bundle module",
1587 Args: map[string]string{
1588 "abi": strings.Join(abis, "."),
1589 },
1590 })
1591 } else {
1592 ctx.Build(pctx, android.BuildParams{
1593 Rule: zipApexRule,
1594 Implicits: implicitInputs,
1595 Output: unsignedOutputFile,
1596 Description: "apex (" + apexType.name() + ")",
1597 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001598 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1599 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1600 "copy_commands": strings.Join(copyCommands, " && "),
1601 "manifest": a.manifestPbOut.String(),
1602 "manifest_json_full": a.manifestJsonFullOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001603 },
1604 })
Colin Crossa4925902018-11-16 11:36:28 -08001605 }
Colin Crossa4925902018-11-16 11:36:28 -08001606
Sundong Ahnabb64432019-10-22 13:58:29 +09001607 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001608 ctx.Build(pctx, android.BuildParams{
1609 Rule: java.Signapk,
1610 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001611 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001612 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001613 Implicits: []android.Path{
1614 a.container_certificate_file,
1615 a.container_private_key_file,
1616 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001617 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001618 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001619 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001620 },
1621 })
Alex Light5098a612018-11-29 17:12:15 -08001622
1623 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001624 if a.installable() {
1625 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001626 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001627 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001628}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001629
Jiyong Park8fd61922018-11-08 02:50:25 +09001630func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001631 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1632 // reply true to `InstallBypassMake()` (thus making the call
1633 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1634 // instead of `android.PathForOutput`) to return the correct path to the flattened
1635 // APEX (as its contents is installed by Make, not Soong).
1636 factx := flattenedApexContext{ctx}
1637 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1638 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1639
1640 a.buildFilesInfo(ctx)
1641}
1642
1643func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1644 cert := String(a.properties.Certificate)
1645 if cert != "" && android.SrcIsModule(cert) == "" {
1646 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1647 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1648 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1649 } else if cert == "" {
1650 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1651 a.container_certificate_file = pem
1652 a.container_private_key_file = key
1653 }
1654}
1655
1656func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001657 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001658 // 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 +09001659 // with other ordinary files.
Jooyung Han01a3ee22019-11-02 02:52:25 +09001660 a.filesInfo = append(a.filesInfo, apexFile{a.manifestJsonOut, "apex_manifest.json." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
1661 a.filesInfo = append(a.filesInfo, apexFile{a.manifestPbOut, "apex_manifest.pb." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001662
Jiyong Park42cca6c2019-04-01 11:15:50 +09001663 // rename to apex_pubkey
1664 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1665 ctx.Build(pctx, android.BuildParams{
1666 Rule: android.Cp,
1667 Input: a.public_key_file,
1668 Output: copiedPubkey,
1669 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001670 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001671
Sundong Ahnabb64432019-10-22 13:58:29 +09001672 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001673 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001674 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001675 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001676 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1677 for _, sym := range fi.symlinks {
1678 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1679 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001680 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001681 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001682 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001683}
1684
1685func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001686 if a.properties.HideFromMake {
1687 return android.AndroidMkData{
1688 Disabled: true,
1689 }
1690 }
Alex Light5098a612018-11-29 17:12:15 -08001691 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001692 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001693 return android.AndroidMkData{
1694 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1695 for _, data := range writers {
1696 data.Custom(w, name, prefix, moduleDir, data)
1697 }
1698 }}
1699}
1700
Sundong Ahnabb64432019-10-22 13:58:29 +09001701func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001702 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001703 apexType := a.properties.ApexType
1704 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1705 // to install symbol files in $(PRODUCT_OUT}/apex.
1706 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1707 if !a.primaryApexType && apexType != flattenedApex {
1708 return moduleNames
1709 }
Jiyong Park94427262019-02-05 23:18:47 +09001710
1711 for _, fi := range a.filesInfo {
1712 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1713 continue
1714 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001715
1716 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001717 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001718 }
1719
Jiyong Park94427262019-02-05 23:18:47 +09001720 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1721 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001722 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001723 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001724 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001725 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001726 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001727 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001728 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001729 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001730 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1731 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001732 if len(fi.symlinks) > 0 {
1733 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1734 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001735
1736 if fi.module != nil && fi.module.NoticeFile().Valid() {
1737 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1738 }
Jiyong Park94427262019-02-05 23:18:47 +09001739 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001740 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001741 }
1742 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1743 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1744 if fi.module != nil {
1745 archStr := fi.module.Target().Arch.ArchType.String()
1746 host := false
1747 switch fi.module.Target().Os.Class {
1748 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001749 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001750 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1751 }
1752 host = true
1753 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001754 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001755 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1756 }
1757 host = true
1758 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001759 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001760 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1761 }
1762 }
1763 if host {
1764 makeOs := fi.module.Target().Os.String()
1765 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1766 makeOs = "linux"
1767 }
1768 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1769 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1770 }
1771 }
1772 if fi.class == javaSharedLib {
1773 javaModule := fi.module.(*java.Library)
1774 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1775 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1776 // we will have foo.jar.jar
1777 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1778 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1779 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1780 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1781 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1782 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001783 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001784 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001785 if cc, ok := fi.module.(*cc.Module); ok {
1786 if cc.UnstrippedOutputFile() != nil {
1787 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1788 }
1789 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001790 if cc.CoverageOutputFile().Valid() {
1791 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1792 }
Jiyong Park94427262019-02-05 23:18:47 +09001793 }
1794 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1795 } else {
1796 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001797 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Jooyung Han01a3ee22019-11-02 02:52:25 +09001798 if a.primaryApexType && fi.builtFile == a.manifestPbOut && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001799 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1800 }
Jiyong Park94427262019-02-05 23:18:47 +09001801 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1802 }
1803 }
1804 return moduleNames
1805}
1806
Sundong Ahnabb64432019-10-22 13:58:29 +09001807func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001808 return android.AndroidMkData{
1809 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1810 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001811 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001812 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001813 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001814 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001815 }
1816
Sundong Ahnabb64432019-10-22 13:58:29 +09001817 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001818 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001819 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1820 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001821 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001822 if len(moduleNames) > 0 {
1823 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1824 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001825 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001826 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001827
Sundong Ahnabb64432019-10-22 13:58:29 +09001828 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001829 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1830 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001831 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001832 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001833 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001834 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001835 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001836 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001837 if len(moduleNames) > 0 {
1838 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1839 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001840 if len(a.externalDeps) > 0 {
1841 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1842 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001843 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001844 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001845 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001846 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001847 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001848 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1849 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1850 if len(postInstallCommands) > 0 {
1851 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1852 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001853 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001854
Alex Light5098a612018-11-29 17:12:15 -08001855 if apexType == imageApex {
1856 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1857 }
Jiyong Park719b4462019-01-13 00:39:51 +09001858 }
1859 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001860}
1861
Jooyung Han344d5432019-08-23 11:17:39 +09001862func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001863 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001864 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001865 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001866 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001867 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1868 })
Alex Light5098a612018-11-29 17:12:15 -08001869 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001870 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001871 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001872 return module
1873}
Jiyong Park30ca9372019-02-07 16:27:23 +09001874
Jooyung Han344d5432019-08-23 11:17:39 +09001875func ApexBundleFactory(testApex bool) android.Module {
1876 bundle := newApexBundle()
1877 bundle.testApex = testApex
1878 return bundle
1879}
1880
1881func testApexBundleFactory() android.Module {
1882 bundle := newApexBundle()
1883 bundle.testApex = true
1884 return bundle
1885}
1886
Jiyong Parkd1063c12019-07-17 20:08:41 +09001887func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001888 return newApexBundle()
1889}
1890
1891// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1892// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1893// If not specified, then the "current" versions are gathered.
1894func vndkApexBundleFactory() android.Module {
1895 bundle := newApexBundle()
1896 bundle.vndkApex = true
1897 bundle.AddProperties(&bundle.vndkProperties)
1898 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1899 ctx.AppendProperties(&struct {
1900 Compile_multilib *string
1901 }{
1902 proptools.StringPtr("both"),
1903 })
1904 })
1905 return bundle
1906}
1907
Jooyung Han31c470b2019-10-18 16:26:59 +09001908func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1909 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1910 if vndkVersion == "current" {
1911 vndkVersion = config.PlatformVndkVersion()
1912 }
1913 return vndkVersion
1914}
1915
Jiyong Park30ca9372019-02-07 16:27:23 +09001916//
1917// Defaults
1918//
1919type Defaults struct {
1920 android.ModuleBase
1921 android.DefaultsModuleBase
1922}
1923
Jiyong Park30ca9372019-02-07 16:27:23 +09001924func defaultsFactory() android.Module {
1925 return DefaultsFactory()
1926}
1927
1928func DefaultsFactory(props ...interface{}) android.Module {
1929 module := &Defaults{}
1930
1931 module.AddProperties(props...)
1932 module.AddProperties(
1933 &apexBundleProperties{},
1934 &apexTargetBundleProperties{},
1935 )
1936
1937 android.InitDefaultsModule(module)
1938 return module
1939}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001940
1941//
1942// Prebuilt APEX
1943//
1944type Prebuilt struct {
1945 android.ModuleBase
1946 prebuilt android.Prebuilt
1947
1948 properties PrebuiltProperties
1949
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001950 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001951 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001952 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001953 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001954}
1955
1956type PrebuiltProperties struct {
1957 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001958 Source string `blueprint:"mutated"`
1959 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001960
1961 Src *string
1962 Arch struct {
1963 Arm struct {
1964 Src *string
1965 }
1966 Arm64 struct {
1967 Src *string
1968 }
1969 X86 struct {
1970 Src *string
1971 }
1972 X86_64 struct {
1973 Src *string
1974 }
1975 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001976
1977 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001978 // Optional name for the installed apex. If unspecified, name of the
1979 // module is used as the file name
1980 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001981
1982 // Names of modules to be overridden. Listed modules can only be other binaries
1983 // (in Make or Soong).
1984 // This does not completely prevent installation of the overridden binaries, but if both
1985 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1986 // from PRODUCT_PACKAGES.
1987 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001988}
1989
1990func (p *Prebuilt) installable() bool {
1991 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001992}
1993
1994func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001995 // If the device is configured to use flattened APEX, force disable the prebuilt because
1996 // the prebuilt is a non-flattened one.
1997 forceDisable := ctx.Config().FlattenApex()
1998
1999 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
2000 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09002001 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09002002
Kun Niu10c9f832019-07-29 16:28:57 -07002003 // Force disable the prebuilts when coverage is enabled.
2004 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
2005 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
2006
Jiyong Park50b81e52019-07-11 11:24:41 +09002007 // b/137216042 don't use prebuilts when address sanitizer is on
2008 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
2009 android.InList("hwaddress", ctx.Config().SanitizeDevice())
2010
2011 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09002012 p.properties.ForceDisable = true
2013 return
2014 }
2015
Jiyong Parkc95714e2019-03-29 14:23:10 +09002016 // This is called before prebuilt_select and prebuilt_postdeps mutators
2017 // The mutators requires that src to be set correctly for each arch so that
2018 // arch variants are disabled when src is not provided for the arch.
2019 if len(ctx.MultiTargets()) != 1 {
2020 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
2021 return
2022 }
2023 var src string
2024 switch ctx.MultiTargets()[0].Arch.ArchType {
2025 case android.Arm:
2026 src = String(p.properties.Arch.Arm.Src)
2027 case android.Arm64:
2028 src = String(p.properties.Arch.Arm64.Src)
2029 case android.X86:
2030 src = String(p.properties.Arch.X86.Src)
2031 case android.X86_64:
2032 src = String(p.properties.Arch.X86_64.Src)
2033 default:
2034 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
2035 return
2036 }
2037 if src == "" {
2038 src = String(p.properties.Src)
2039 }
2040 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002041}
2042
Jiyong Park03b68dd2019-07-26 23:20:40 +09002043func (p *Prebuilt) isForceDisabled() bool {
2044 return p.properties.ForceDisable
2045}
2046
Colin Cross41955e82019-05-29 14:40:35 -07002047func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2048 switch tag {
2049 case "":
2050 return android.Paths{p.outputApex}, nil
2051 default:
2052 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2053 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002054}
2055
Jiyong Park4d277042019-04-23 18:00:10 +09002056func (p *Prebuilt) InstallFilename() string {
2057 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2058}
2059
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002060func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002061 if p.properties.ForceDisable {
2062 return
2063 }
2064
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002065 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002066 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002067 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002068 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002069 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2070 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2071 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002072 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2073 ctx.Build(pctx, android.BuildParams{
2074 Rule: android.Cp,
2075 Input: p.inputApex,
2076 Output: p.outputApex,
2077 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002078 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002079 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002080 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002081
2082 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002083}
2084
2085func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2086 return &p.prebuilt
2087}
2088
2089func (p *Prebuilt) Name() string {
2090 return p.prebuilt.Name(p.ModuleBase.Name())
2091}
2092
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002093func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2094 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002095 Class: "ETC",
2096 OutputFile: android.OptionalPathForPath(p.inputApex),
2097 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002098 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2099 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002100 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002101 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2102 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2103 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2104 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002105 },
2106 }
2107}
2108
2109// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2110func PrebuiltFactory() android.Module {
2111 module := &Prebuilt{}
2112 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002113 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002114 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002115 return module
2116}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002117
2118func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2119 // small helper to add symlink commands
2120 addSymlink := func(target, dir, linkName string) {
2121 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2122 link := filepath.Join(outDir, linkName)
2123 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2124 }
2125
2126 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2127 // When all hard-coded references are fixed, remove symbolic links
2128 // Note that we should keep following symlinks for older VNDKs (<=29)
2129 // Since prebuilt vndk libs still depend on system/lib/vndk path
2130 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2131 // the name of vndk apex is formatted "com.android.vndk.v" + version
2132 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2133 if ctx.Config().Android64() {
2134 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2135 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2136 }
2137 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2138 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2139 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2140 }
2141 }
2142 return
2143}