blob: 4debe25021b9fa821ac197658eda13b30db78b23 [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 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900267 } else if a, ok := mctx.Module().(*apexBundle); ok && a.vndkApex {
268 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
269 mctx.AddDependency(mctx.Module(), prebuiltTag, cc.VndkLibrariesTxtModules(vndkVersion)...)
Jooyung Han344d5432019-08-23 11:17:39 +0900270 }
271}
272
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900273// Mark the direct and transitive dependencies of apex bundles so that they
274// can be built for the apex bundles.
275func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800276 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800277 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900278 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900279 depName := mctx.OtherModuleName(child)
280 // If the parent is apexBundle, this child is directly depended.
281 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800282 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800283 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
284 // non-installable apex's cannot be installed and so should not prevent libraries from being
285 // installed to the system.
286 android.UpdateApexDependency(apexBundleName, depName, directDep)
287 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900288
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900289 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900290 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900291 return true
292 } else {
293 return false
294 }
295 })
296 }
297}
298
299// Create apex variations if a module is included in APEX(s).
300func apexMutator(mctx android.BottomUpMutatorContext) {
301 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900302 am.CreateApexVariations(mctx)
Jooyung Han7a78a922019-10-08 21:59:58 +0900303 } else if a, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900304 // apex bundle itself is mutated so that it and its modules have same
305 // apex variant.
306 apexBundleName := mctx.ModuleName()
307 mctx.CreateVariations(apexBundleName)
Jooyung Han7a78a922019-10-08 21:59:58 +0900308
309 // collects APEX list
310 if mctx.Device() && a.installable() {
311 addApexFileContextsInfos(mctx, a)
312 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900313 }
314}
Sundong Ahne9b55722019-09-06 17:37:42 +0900315
Jooyung Han7a78a922019-10-08 21:59:58 +0900316var (
317 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
318 apexFileContextsInfosMutex sync.Mutex
319)
320
321func apexFileContextsInfos(config android.Config) *[]string {
322 return config.Once(apexFileContextsInfosKey, func() interface{} {
323 return &[]string{}
324 }).(*[]string)
325}
326
327func addApexFileContextsInfos(ctx android.BaseModuleContext, a *apexBundle) {
328 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
329 fileContextsName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
330
331 apexFileContextsInfosMutex.Lock()
332 defer apexFileContextsInfosMutex.Unlock()
333 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
334 *apexFileContextsInfos = append(*apexFileContextsInfos, apexName+":"+fileContextsName)
335}
336
Sundong Ahne9b55722019-09-06 17:37:42 +0900337func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900338 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +0900339 var variants []string
340 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
341 case "image":
342 variants = append(variants, imageApexType, flattenedApexType)
343 case "zip":
344 variants = append(variants, zipApexType)
345 case "both":
346 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
347 default:
348 mctx.PropertyErrorf("type", "%q is not one of \"image\" or \"zip\".", *ab.properties.Payload_type)
349 return
350 }
351
352 modules := mctx.CreateLocalVariations(variants...)
353
354 for i, v := range variants {
355 switch v {
356 case imageApexType:
357 modules[i].(*apexBundle).properties.ApexType = imageApex
358 case zipApexType:
359 modules[i].(*apexBundle).properties.ApexType = zipApex
360 case flattenedApexType:
361 modules[i].(*apexBundle).properties.ApexType = flattenedApex
362 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900363 }
364 }
365}
366
Jooyung Han5c998b92019-06-27 11:30:33 +0900367func apexUsesMutator(mctx android.BottomUpMutatorContext) {
368 if ab, ok := mctx.Module().(*apexBundle); ok {
369 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
370 }
371}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900372
Jooyung Handc782442019-11-01 03:14:38 +0900373var (
374 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
375)
376
377// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
378// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
379// which may cause compatibility issues. (e.g. libbinder)
380// Even though libbinder restricts its availability via 'apex_available' property and relies on
381// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
382// to avoid similar problems.
383func useVendorWhitelist(config android.Config) []string {
384 return config.Once(useVendorWhitelistKey, func() interface{} {
385 return []string{
386 // swcodec uses "vendor" variants for smaller size
387 "com.android.media.swcodec",
388 "test_com.android.media.swcodec",
389 }
390 }).([]string)
391}
392
393// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
394// called before the first call to useVendorWhitelist()
395func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
396 config.Once(useVendorWhitelistKey, func() interface{} {
397 return whitelist
398 })
399}
400
Alex Light9670d332019-01-29 18:07:33 -0800401type apexNativeDependencies struct {
402 // List of native libraries
403 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900404
Alex Light9670d332019-01-29 18:07:33 -0800405 // List of native executables
406 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900407
Roland Levillain630846d2019-06-26 12:48:34 +0100408 // List of native tests
409 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800410}
Jooyung Han344d5432019-08-23 11:17:39 +0900411
Alex Light9670d332019-01-29 18:07:33 -0800412type apexMultilibProperties struct {
413 // Native dependencies whose compile_multilib is "first"
414 First apexNativeDependencies
415
416 // Native dependencies whose compile_multilib is "both"
417 Both apexNativeDependencies
418
419 // Native dependencies whose compile_multilib is "prefer32"
420 Prefer32 apexNativeDependencies
421
422 // Native dependencies whose compile_multilib is "32"
423 Lib32 apexNativeDependencies
424
425 // Native dependencies whose compile_multilib is "64"
426 Lib64 apexNativeDependencies
427}
428
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900429type apexBundleProperties struct {
430 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000431 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800432 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900433
Jiyong Park40e26a22019-02-08 02:53:06 +0900434 // AndroidManifest.xml file used for the zip container of this APEX bundle.
435 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800436 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900437
Roland Levillain411c5842019-09-19 16:37:20 +0100438 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
439 // device (/apex/<apex_name>).
440 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900441 Apex_name *string
442
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900443 // Determines the file contexts file for setting security context to each file in this APEX bundle.
444 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
445 // used.
446 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900447 File_contexts *string
448
449 // List of native shared libs that are embedded inside this APEX bundle
450 Native_shared_libs []string
451
Roland Levillain630846d2019-06-26 12:48:34 +0100452 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900453 Binaries []string
454
455 // List of java libraries that are embedded inside this APEX bundle
456 Java_libs []string
457
458 // List of prebuilt files that are embedded inside this APEX bundle
459 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900460
Roland Levillain630846d2019-06-26 12:48:34 +0100461 // List of tests that are embedded inside this APEX bundle
462 Tests []string
463
Jiyong Parkff1458f2018-10-12 21:49:38 +0900464 // Name of the apex_key module that provides the private key to sign APEX
465 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900466
Alex Light5098a612018-11-29 17:12:15 -0800467 // The type of APEX to build. Controls what the APEX payload is. Either
468 // 'image', 'zip' or 'both'. Default: 'image'.
469 Payload_type *string
470
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900471 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
472 // or an android_app_certificate module name in the form ":module".
473 Certificate *string
474
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900475 // Whether this APEX is installable to one of the partitions. Default: true.
476 Installable *bool
477
Jiyong Parkda6eb592018-12-19 17:12:36 +0900478 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
479 // Default is false.
480 Use_vendor *bool
481
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800482 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
483 Ignore_system_library_special_case *bool
484
Alex Light9670d332019-01-29 18:07:33 -0800485 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900486
Jiyong Parkf97782b2019-02-13 20:28:58 +0900487 // List of sanitizer names that this APEX is enabled for
488 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900489
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900490 PreventInstall bool `blueprint:"mutated"`
491
492 HideFromMake bool `blueprint:"mutated"`
493
Jooyung Han5c998b92019-06-27 11:30:33 +0900494 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
495 Provide_cpp_shared_libs *bool
496
497 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
498 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100499
500 // A txt file containing list of files that are whitelisted to be included in this APEX.
501 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900502
503 // List of APKs to package inside APEX
504 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900505
Sundong Ahnabb64432019-10-22 13:58:29 +0900506 // package format of this apex variant; could be non-flattened, flattened, or zip.
507 // imageApex, zipApex or flattened
508 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900509
Jiyong Parkd1063c12019-07-17 20:08:41 +0900510 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
511 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
512 // is implied. This value affects all modules included in this APEX. In other words, they are
513 // also built with the SDKs specified here.
514 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800515}
516
517type apexTargetBundleProperties struct {
518 Target struct {
519 // Multilib properties only for android.
520 Android struct {
521 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900522 }
Jooyung Han344d5432019-08-23 11:17:39 +0900523
Alex Light9670d332019-01-29 18:07:33 -0800524 // Multilib properties only for host.
525 Host struct {
526 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900527 }
Jooyung Han344d5432019-08-23 11:17:39 +0900528
Alex Light9670d332019-01-29 18:07:33 -0800529 // Multilib properties only for host linux_bionic.
530 Linux_bionic struct {
531 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900532 }
Jooyung Han344d5432019-08-23 11:17:39 +0900533
Alex Light9670d332019-01-29 18:07:33 -0800534 // Multilib properties only for host linux_glibc.
535 Linux_glibc struct {
536 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900537 }
538 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900539}
540
Jooyung Han344d5432019-08-23 11:17:39 +0900541type apexVndkProperties struct {
542 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
543 Vndk_version *string
544}
545
Jiyong Park8fd61922018-11-08 02:50:25 +0900546type apexFileClass int
547
548const (
549 etc apexFileClass = iota
550 nativeSharedLib
551 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900552 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800553 pyBinary
554 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900555 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100556 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900557 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900558)
559
Alex Light5098a612018-11-29 17:12:15 -0800560type apexPackaging int
561
562const (
563 imageApex apexPackaging = iota
564 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900565 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800566)
567
Sundong Ahnabb64432019-10-22 13:58:29 +0900568// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800569func (a apexPackaging) suffix() string {
570 switch a {
571 case imageApex:
572 return imageApexSuffix
573 case zipApex:
574 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800575 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100576 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800577 }
578}
579
580func (a apexPackaging) name() string {
581 switch a {
582 case imageApex:
583 return imageApexType
584 case zipApex:
585 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800586 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100587 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800588 }
589}
590
Jiyong Park8fd61922018-11-08 02:50:25 +0900591func (class apexFileClass) NameInMake() string {
592 switch class {
593 case etc:
594 return "ETC"
595 case nativeSharedLib:
596 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800597 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900598 return "EXECUTABLES"
599 case javaSharedLib:
600 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100601 case nativeTest:
602 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900603 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900604 // b/142537672 Why isn't this APP? We want to have full control over
605 // the paths and file names of the apk file under the flattend APEX.
606 // If this is set to APP, then the paths and file names are modified
607 // by the Make build system. For example, it is installed to
608 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
609 // /system/apex/<apexname>/app/<Appname> because the build system automatically
610 // appends module name (which is <apexname>.<Appname> to the path.
611 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900612 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100613 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900614 }
615}
616
617type apexFile struct {
618 builtFile android.Path
619 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900620 installDir string
621 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900622 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800623 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900624}
625
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626type apexBundle struct {
627 android.ModuleBase
628 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900629 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900630
Alex Light9670d332019-01-29 18:07:33 -0800631 properties apexBundleProperties
632 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900633 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900634
Colin Crossa4925902018-11-16 11:36:28 -0800635 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900636 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700637 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900638
Jiyong Park03b68dd2019-07-26 23:20:40 +0900639 prebuiltFileToDelete string
640
Jiyong Park42cca6c2019-04-01 11:15:50 +0900641 public_key_file android.Path
642 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900643
644 container_certificate_file android.Path
645 container_private_key_file android.Path
646
Jiyong Park8fd61922018-11-08 02:50:25 +0900647 // list of files to be included in this apex
648 filesInfo []apexFile
649
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900650 // list of module names that this APEX is depending on
651 externalDeps []string
652
Sundong Ahnabb64432019-10-22 13:58:29 +0900653 testApex bool
654 vndkApex bool
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000655 artApex bool
Sundong Ahnabb64432019-10-22 13:58:29 +0900656 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900657
658 // intermediate path for apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +0900659 manifestJsonOut android.WritablePath
660 manifestJsonFullOut android.WritablePath
661 manifestPbOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900662
663 // list of commands to create symlinks for backward compatibility
664 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
665 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
666 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
667 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900668
669 // Suffix of module name in Android.mk
670 // ".flattened", ".apex", ".zipapex", or ""
671 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900672}
673
Jiyong Park397e55e2018-10-24 21:09:55 +0900674func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100675 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700676 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900677 // Use *FarVariation* to be able to depend on modules having
678 // conflicting variations with this module. This is required since
679 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
680 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700681 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900682 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900683 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900684 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700685 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900686
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700687 ctx.AddFarVariationDependencies(append(target.Variations(),
688 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
689 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100690
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700691 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100692 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100693 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700694 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900695}
696
Alex Light9670d332019-01-29 18:07:33 -0800697func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
698 if ctx.Os().Class == android.Device {
699 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
700 } else {
701 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
702 if ctx.Os().Bionic() {
703 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
704 } else {
705 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
706 }
707 }
708}
709
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900710func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900711 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
712 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
713 }
714
Jiyong Park397e55e2018-10-24 21:09:55 +0900715 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900716 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800717
718 a.combineProperties(ctx)
719
Jiyong Park397e55e2018-10-24 21:09:55 +0900720 has32BitTarget := false
721 for _, target := range targets {
722 if target.Arch.ArchType.Multilib == "lib32" {
723 has32BitTarget = true
724 }
725 }
726 for i, target := range targets {
727 // When multilib.* is omitted for native_shared_libs, it implies
728 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700729 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900730 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900731 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700732 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900733
Roland Levillain630846d2019-06-26 12:48:34 +0100734 // When multilib.* is omitted for tests, it implies
735 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700736 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100737 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100738 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700739 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100740
Jiyong Park397e55e2018-10-24 21:09:55 +0900741 // Add native modules targetting both ABIs
742 addDependenciesForNativeModules(ctx,
743 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100744 a.properties.Multilib.Both.Binaries,
745 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700746 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900747 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900748
Alex Light3d673592019-01-18 14:37:31 -0800749 isPrimaryAbi := i == 0
750 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900751 // When multilib.* is omitted for binaries, it implies
752 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700753 ctx.AddFarVariationDependencies(append(target.Variations(),
754 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
755 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900756
757 // Add native modules targetting the first ABI
758 addDependenciesForNativeModules(ctx,
759 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100760 a.properties.Multilib.First.Binaries,
761 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700762 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900763 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800764
765 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700766 ctx.AddFarVariationDependencies(target.Variations(),
767 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900768 }
769
770 switch target.Arch.ArchType.Multilib {
771 case "lib32":
772 // Add native modules targetting 32-bit ABI
773 addDependenciesForNativeModules(ctx,
774 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100775 a.properties.Multilib.Lib32.Binaries,
776 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700777 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900778 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900779
780 addDependenciesForNativeModules(ctx,
781 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100782 a.properties.Multilib.Prefer32.Binaries,
783 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700784 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900785 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900786 case "lib64":
787 // Add native modules targetting 64-bit ABI
788 addDependenciesForNativeModules(ctx,
789 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100790 a.properties.Multilib.Lib64.Binaries,
791 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700792 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900793 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900794
795 if !has32BitTarget {
796 addDependenciesForNativeModules(ctx,
797 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100798 a.properties.Multilib.Prefer32.Binaries,
799 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700800 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900801 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900802 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700803
804 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
805 for _, sanitizer := range ctx.Config().SanitizeDevice() {
806 if sanitizer == "hwaddress" {
807 addDependenciesForNativeModules(ctx,
808 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700809 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700810 break
811 }
812 }
813 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900814 }
815
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900816 }
817
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700818 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
819 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900820
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700821 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
822 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900823
Jiyong Park23c52b02019-02-02 13:13:47 +0900824 if String(a.properties.Key) == "" {
825 ctx.ModuleErrorf("key is missing")
826 return
827 }
828 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900829
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900830 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900831 if cert != "" {
832 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900833 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900834
835 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
836 if len(a.properties.Uses_sdks) > 0 {
837 sdkRefs := []android.SdkRef{}
838 for _, str := range a.properties.Uses_sdks {
839 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
840 sdkRefs = append(sdkRefs, parsed)
841 }
842 a.BuildWithSdks(sdkRefs)
843 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900844}
845
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900846func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
847 // direct deps of an APEX bundle are all part of the APEX bundle
848 return true
849}
850
Colin Cross0ea8ba82019-06-06 14:33:29 -0700851func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900852 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
853 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000854 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900855 }
856 return String(a.properties.Certificate)
857}
858
Colin Cross41955e82019-05-29 14:40:35 -0700859func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
860 switch tag {
861 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900862 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700863 default:
864 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900865 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900866}
867
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900868func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900869 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900870}
871
Jiyong Park7c1dc612019-01-05 11:15:24 +0900872func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900873 if a.vndkApex {
874 return "vendor." + a.vndkVersion(config)
875 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900876 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900877 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900878 } else {
879 return "core"
880 }
881}
882
Jiyong Parkf97782b2019-02-13 20:28:58 +0900883func (a *apexBundle) EnableSanitizer(sanitizerName string) {
884 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
885 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
886 }
887}
888
Jiyong Park388ef3f2019-01-28 19:47:32 +0900889func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900890 if android.InList(sanitizerName, a.properties.SanitizerNames) {
891 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900892 }
893
894 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900895 globalSanitizerNames := []string{}
896 if a.Host() {
897 globalSanitizerNames = ctx.Config().SanitizeHost()
898 } else {
899 arches := ctx.Config().SanitizeDeviceArch()
900 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
901 globalSanitizerNames = ctx.Config().SanitizeDevice()
902 }
903 }
904 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900905}
906
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900907func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
908 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
909}
910
911func (a *apexBundle) PreventInstall() {
912 a.properties.PreventInstall = true
913}
914
915func (a *apexBundle) HideFromMake() {
916 a.properties.HideFromMake = true
917}
918
Martin Stjernholm279de572019-09-10 23:18:20 +0100919func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900920 // Decide the APEX-local directory by the multilib of the library
921 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100922 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900923 case "lib32":
924 dirInApex = "lib"
925 case "lib64":
926 dirInApex = "lib64"
927 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100928 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700929 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100930 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900931 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100932 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
933 // Special case for Bionic libs and other libs installed with them. This is
934 // to prevent those libs from being included in the search path
935 // /apex/com.android.runtime/${LIB}. This exclusion is required because
936 // those libs in the Runtime APEX are available via the legacy paths in
937 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
938 // to the legacy paths and thus will be loaded into the default linker
939 // namespace (aka "platform" namespace). If the libs are directly in
940 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
941 // into the runtime linker namespace, which will result in double loading of
942 // them, which isn't supported.
943 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900944 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900945
Martin Stjernholm279de572019-09-10 23:18:20 +0100946 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900947 return
948}
949
950func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900951 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700952 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200953 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900954 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900955 fileToCopy = cc.OutputFile().Path()
956 return
957}
958
Alex Light778127a2019-02-27 14:19:50 -0800959func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
960 dirInApex = "bin"
961 fileToCopy = py.HostToolPath().Path()
962 return
963}
964func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
965 dirInApex = "bin"
966 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
967 if err != nil {
968 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
969 return
970 }
971 fileToCopy = android.PathForOutput(ctx, s)
972 return
973}
974
Jiyong Park04480cf2019-02-06 00:16:29 +0900975func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
976 dirInApex = filepath.Join("bin", sh.SubDir())
977 fileToCopy = sh.OutputFile()
978 return
979}
980
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900981func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
982 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900983 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900984 return
985}
986
Jiyong Park9e6c2422019-08-09 20:39:45 +0900987func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
988 dirInApex = "javalib"
989 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
990 implJars := java.ImplementationJars()
991 if len(implJars) != 1 {
992 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
993 strings.Join(implJars.Strings(), ", ")))
994 }
995 fileToCopy = implJars[0]
996 return
997}
998
Jooyung Han39edb6c2019-11-06 16:53:07 +0900999func getCopyManifestForPrebuiltEtc(prebuilt android.PrebuiltEtcModule) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001000 dirInApex = filepath.Join("etc", prebuilt.SubDir())
1001 fileToCopy = prebuilt.OutputFile()
1002 return
1003}
1004
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001005func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +09001006 appDir := "app"
1007 if app.Privileged() {
1008 appDir = "priv-app"
1009 }
1010 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001011 fileToCopy = app.OutputFile()
1012 return
1013}
1014
Dario Frenicde2a032019-10-27 00:29:22 +01001015func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
1016 appDir := "app"
1017 if app.Privileged() {
1018 appDir = "priv-app"
1019 }
1020 dirInApex = filepath.Join(appDir, pkgName)
1021 fileToCopy = app.OutputFile()
1022 return
1023}
1024
Roland Levillain935639d2019-08-13 14:55:28 +01001025// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1026type flattenedApexContext struct {
1027 android.ModuleContext
1028}
1029
1030func (c *flattenedApexContext) InstallBypassMake() bool {
1031 return true
1032}
1033
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001034func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001035 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001036
Sundong Ahnabb64432019-10-22 13:58:29 +09001037 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1038 switch a.properties.ApexType {
1039 case imageApex:
1040 if buildFlattenedAsDefault {
1041 a.suffix = imageApexSuffix
1042 } else {
1043 a.suffix = ""
1044 a.primaryApexType = true
1045 }
1046 case zipApex:
1047 if proptools.String(a.properties.Payload_type) == "zip" {
1048 a.suffix = ""
1049 a.primaryApexType = true
1050 } else {
1051 a.suffix = zipApexSuffix
1052 }
1053 case flattenedApex:
1054 if buildFlattenedAsDefault {
1055 a.suffix = ""
1056 a.primaryApexType = true
1057 } else {
1058 a.suffix = flattenedSuffix
1059 }
Alex Light5098a612018-11-29 17:12:15 -08001060 }
1061
Roland Levillain630846d2019-06-26 12:48:34 +01001062 if len(a.properties.Tests) > 0 && !a.testApex {
1063 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1064 return
1065 }
1066
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001067 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1068
Jooyung Hane1633032019-08-01 17:41:43 +09001069 // native lib dependencies
1070 var provideNativeLibs []string
1071 var requireNativeLibs []string
1072
Jooyung Han5c998b92019-06-27 11:30:33 +09001073 // Check if "uses" requirements are met with dependent apexBundles
1074 var providedNativeSharedLibs []string
1075 useVendor := proptools.Bool(a.properties.Use_vendor)
1076 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1077 if ctx.OtherModuleDependencyTag(m) != usesTag {
1078 return
1079 }
1080 otherName := ctx.OtherModuleName(m)
1081 other, ok := m.(*apexBundle)
1082 if !ok {
1083 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1084 return
1085 }
1086 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1087 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1088 return
1089 }
1090 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1091 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1092 return
1093 }
1094 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1095 })
1096
Alex Light778127a2019-02-27 14:19:50 -08001097 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001098 depTag := ctx.OtherModuleDependencyTag(child)
1099 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001100 if _, ok := parent.(*apexBundle); ok {
1101 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001102 switch depTag {
1103 case sharedLibTag:
1104 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001105 if cc.HasStubsVariants() {
1106 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1107 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001108 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001109 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001110 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001111 } else {
1112 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001113 }
1114 case executableTag:
1115 if cc, ok := child.(*cc.Module); ok {
1116 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001117 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001118 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001119 } else if sh, ok := child.(*android.ShBinary); ok {
1120 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001121 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001122 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1123 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1124 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1125 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1126 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1127 // NB: Since go binaries are static we don't need the module for anything here, which is
1128 // good since the go tool is a blueprint.Module not an android.Module like we would
1129 // normally use.
1130 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001131 } else {
Alex Light778127a2019-02-27 14:19:50 -08001132 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 +09001133 }
1134 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001135 if javaLib, ok := child.(*java.Library); ok {
1136 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001137 if fileToCopy == nil {
1138 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1139 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001140 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1141 }
1142 return true
1143 } else if javaLib, ok := child.(*java.Import); ok {
1144 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1145 if fileToCopy == nil {
1146 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1147 } else {
1148 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001149 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001150 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001151 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001152 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001153 }
1154 case prebuiltTag:
Jooyung Han39edb6c2019-11-06 16:53:07 +09001155 if prebuilt, ok := child.(android.PrebuiltEtcModule); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001156 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001157 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001158 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001159 } else {
1160 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1161 }
Roland Levillain630846d2019-06-26 12:48:34 +01001162 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001163 if ccTest, ok := child.(*cc.Module); ok {
1164 if ccTest.IsTestPerSrcAllTestsVariation() {
1165 // Multiple-output test module (where `test_per_src: true`).
1166 //
1167 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1168 // We do not add this variation to `filesInfo`, as it has no output;
1169 // however, we do add the other variations of this module as indirect
1170 // dependencies (see below).
1171 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001172 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001173 // Single-output test module (where `test_per_src: false`).
1174 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1175 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001176 }
Roland Levillain630846d2019-06-26 12:48:34 +01001177 return true
1178 } else {
1179 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1180 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001181 case keyTag:
1182 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001183 a.private_key_file = key.private_key_file
1184 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001185 return false
1186 } else {
1187 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001188 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001189 case certificateTag:
1190 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001191 a.container_certificate_file = dep.Certificate.Pem
1192 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001193 return false
1194 } else {
1195 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1196 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001197 case android.PrebuiltDepTag:
1198 // If the prebuilt is force disabled, remember to delete the prebuilt file
1199 // that might have been installed in the previous builds
1200 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1201 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1202 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001203 case androidAppTag:
1204 if ap, ok := child.(*java.AndroidApp); ok {
1205 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1206 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1207 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001208 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1209 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1210 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001211 } else {
1212 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1213 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001214 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001215 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001216 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001217 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001218 // We cannot use a switch statement on `depTag` here as the checked
1219 // tags used below are private (e.g. `cc.sharedDepTag`).
Jiyong Parkd237ad62019-11-11 10:14:32 +09001220 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001221 if cc, ok := child.(*cc.Module); ok {
1222 if android.InList(cc.Name(), providedNativeSharedLibs) {
1223 // If we're using a shared library which is provided from other APEX,
1224 // don't include it in this APEX
1225 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001226 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001227 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1228 // If the dependency is a stubs lib, don't include it in this APEX,
1229 // but make sure that the lib is installed on the device.
1230 // In case no APEX is having the lib, the lib is installed to the system
1231 // partition.
1232 //
1233 // Always include if we are a host-apex however since those won't have any
1234 // system libraries.
1235 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1236 a.externalDeps = append(a.externalDeps, cc.Name())
1237 }
Jooyung Hane1633032019-08-01 17:41:43 +09001238 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001239 // Don't track further
1240 return false
1241 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001242 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001243 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1244 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001245 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001246 } else if cc.IsTestPerSrcDepTag(depTag) {
1247 if cc, ok := child.(*cc.Module); ok {
1248 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1249 // Handle modules created as `test_per_src` variations of a single test module:
1250 // use the name of the generated test binary (`fileToCopy`) instead of the name
1251 // of the original test module (`depName`, shared by all `test_per_src`
1252 // variations of that module).
1253 moduleName := filepath.Base(fileToCopy.String())
1254 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1255 return true
1256 }
Jiyong Parkd237ad62019-11-11 10:14:32 +09001257 } else if java.IsJniDepTag(depTag) {
1258 // Do nothing for JNI dep. JNI libraries are always embedded in APK-in-APEX.
Jooyung Han9c80bae2019-08-20 17:30:57 +09001259 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001260 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001261 }
1262 }
1263 }
1264 return false
1265 })
1266
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001267 // Specific to the ART apex: dexpreopt artifacts for libcore Java libraries.
1268 // Build rules are generated by the dexpreopt singleton, and here we access build artifacts
1269 // via the global boot image config.
1270 if a.artApex {
1271 for arch, files := range java.DexpreoptedArtApexJars(ctx) {
1272 dirInApex := filepath.Join("javalib", arch.String())
1273 for _, f := range files {
1274 localModule := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
1275 filesInfo = append(filesInfo, apexFile{f, localModule, dirInApex, etc, nil, nil})
1276 }
1277 }
1278 }
1279
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001280 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001281 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1282 return
1283 }
1284
Jiyong Park8fd61922018-11-08 02:50:25 +09001285 // remove duplicates in filesInfo
1286 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001287 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001288 result := []apexFile{}
1289 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001290 dest := filepath.Join(f.installDir, f.builtFile.Base())
1291 if !encountered[dest] {
1292 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001293 result = append(result, f)
1294 }
1295 }
1296 return result
1297 }
1298 filesInfo = removeDup(filesInfo)
1299
1300 // to have consistent build rules
1301 sort.Slice(filesInfo, func(i, j int) bool {
1302 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1303 })
1304
Jiyong Park127b40b2019-09-30 16:04:35 +09001305 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001306 if !ctx.Host() {
1307 for _, fi := range filesInfo {
1308 if am, ok := fi.module.(android.ApexModule); ok {
1309 if !am.AvailableFor(ctx.ModuleName()) {
1310 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1311 return
1312 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001313 }
1314 }
1315 }
1316
Jiyong Park8fd61922018-11-08 02:50:25 +09001317 // prepend the name of this APEX to the module names. These names will be the names of
1318 // modules that will be defined if the APEX is flattened.
1319 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001320 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001321 }
1322
Jiyong Park8fd61922018-11-08 02:50:25 +09001323 a.installDir = android.PathForModuleInstall(ctx, "apex")
1324 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001325
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001326 // prepare apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +09001327 a.buildManifest(ctx, provideNativeLibs, requireNativeLibs)
1328
1329 a.setCertificateAndPrivateKey(ctx)
1330 if a.properties.ApexType == flattenedApex {
1331 a.buildFlattenedApex(ctx)
1332 } else {
1333 a.buildUnflattenedApex(ctx)
1334 }
1335
1336 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1337 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
1338}
1339
1340func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
Jooyung Hane1633032019-08-01 17:41:43 +09001341 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001342
Jooyung Han01a3ee22019-11-02 02:52:25 +09001343 a.manifestJsonFullOut = android.PathForModuleOut(ctx, "apex_manifest_full.json")
1344
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001345 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001346 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1347 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001348
1349 // apex name can be overridden
1350 optCommands := []string{}
1351 if a.properties.Apex_name != nil {
1352 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1353 }
1354
Jooyung Hane1633032019-08-01 17:41:43 +09001355 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001356 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001357 Input: manifestSrc,
Jooyung Han01a3ee22019-11-02 02:52:25 +09001358 Output: a.manifestJsonFullOut,
Jooyung Hane1633032019-08-01 17:41:43 +09001359 Args: map[string]string{
1360 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1361 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001362 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001363 },
1364 })
1365
Jooyung Han01a3ee22019-11-02 02:52:25 +09001366 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
Roland Levillain8ac05572019-11-13 10:45:55 +00001367 // prepare stripped-down version so that APEX modules built from R+ can be installed to Q
Jooyung Han01a3ee22019-11-02 02:52:25 +09001368 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1369 ctx.Build(pctx, android.BuildParams{
1370 Rule: stripApexManifestRule,
1371 Input: a.manifestJsonFullOut,
1372 Output: a.manifestJsonOut,
1373 })
Jooyung Han72bd2f82019-10-23 16:46:38 +09001374
Jooyung Han01a3ee22019-11-02 02:52:25 +09001375 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
1376 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
1377 ctx.Build(pctx, android.BuildParams{
1378 Rule: pbApexManifestRule,
1379 Input: a.manifestJsonFullOut,
1380 Output: a.manifestPbOut,
1381 })
Jiyong Park8fd61922018-11-08 02:50:25 +09001382}
1383
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001384func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001385 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001386 for _, f := range a.filesInfo {
1387 if f.module != nil {
1388 notice := f.module.NoticeFile()
1389 if notice.Valid() {
1390 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001391 }
1392 }
1393 }
1394 // append the notice file specified in the apex module itself
1395 if a.NoticeFile().Valid() {
1396 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001397 }
1398
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001399 if len(noticeFiles) == 0 {
1400 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001401 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001402
Jaewoong Jung98772792019-07-01 17:15:13 -07001403 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001404}
1405
Sundong Ahnabb64432019-10-22 13:58:29 +09001406func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001407 var abis []string
1408 for _, target := range ctx.MultiTargets() {
1409 if len(target.Arch.Abi) > 0 {
1410 abis = append(abis, target.Arch.Abi[0])
1411 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001412 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001413
Alex Light5098a612018-11-29 17:12:15 -08001414 abis = android.FirstUniqueStrings(abis)
1415
Sundong Ahnabb64432019-10-22 13:58:29 +09001416 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001417 suffix := apexType.suffix()
1418 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001419
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001420 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001421 for _, f := range a.filesInfo {
1422 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001423 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001424
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001425 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001426 emitCommands := []string{}
1427 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1428 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001429 for i, src := range filesToCopy {
1430 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001431 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001432 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001433 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1434 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001435 for _, sym := range a.filesInfo[i].symlinks {
1436 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1437 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1438 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001439 }
Dario Frenie4235822019-10-28 14:49:27 +00001440 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1441
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001442 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Han01a3ee22019-11-02 02:52:25 +09001443 implicitInputs = append(implicitInputs, a.manifestPbOut, a.manifestJsonFullOut, a.manifestJsonOut)
Alex Light5098a612018-11-29 17:12:15 -08001444
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001445 if a.properties.Whitelisted_files != nil {
1446 ctx.Build(pctx, android.BuildParams{
1447 Rule: emitApexContentRule,
1448 Implicits: implicitInputs,
1449 Output: imageContentFile,
1450 Description: "emit apex image content",
1451 Args: map[string]string{
1452 "emit_commands": strings.Join(emitCommands, " && "),
1453 },
1454 })
1455 implicitInputs = append(implicitInputs, imageContentFile)
1456 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1457
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001458 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001459 ctx.Build(pctx, android.BuildParams{
1460 Rule: diffApexContentRule,
1461 Implicits: implicitInputs,
1462 Output: phonyOutput,
1463 Description: "diff apex image content",
1464 Args: map[string]string{
1465 "whitelisted_files_file": whitelistedFilesFile.String(),
1466 "image_content_file": imageContentFile.String(),
1467 "apex_module_name": ctx.ModuleName(),
1468 },
1469 })
1470
1471 implicitInputs = append(implicitInputs, phonyOutput)
1472 }
1473
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001474 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1475 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001476
Sundong Ahnabb64432019-10-22 13:58:29 +09001477 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001478 // files and dirs that will be created in APEX
Jooyung Han01a3ee22019-11-02 02:52:25 +09001479 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
Alex Light5098a612018-11-29 17:12:15 -08001480 var executablePaths []string // this also includes dirs
1481 for _, f := range a.filesInfo {
1482 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001483 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001484 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001485 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001486 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001487 }
Alex Light5098a612018-11-29 17:12:15 -08001488 } else {
1489 readOnlyPaths = append(readOnlyPaths, pathInApex)
1490 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001491 dir := f.installDir
1492 for !android.InList(dir, executablePaths) && dir != "" {
1493 executablePaths = append(executablePaths, dir)
1494 dir, _ = filepath.Split(dir) // move up to the parent
1495 if len(dir) > 0 {
1496 // remove trailing slash
1497 dir = dir[:len(dir)-1]
1498 }
Alex Light5098a612018-11-29 17:12:15 -08001499 }
1500 }
1501 sort.Strings(readOnlyPaths)
1502 sort.Strings(executablePaths)
1503 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1504 ctx.Build(pctx, android.BuildParams{
1505 Rule: generateFsConfig,
1506 Output: cannedFsConfig,
1507 Description: "generate fs config",
1508 Args: map[string]string{
1509 "ro_paths": strings.Join(readOnlyPaths, " "),
1510 "exec_paths": strings.Join(executablePaths, " "),
1511 },
1512 })
1513
1514 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1515 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1516 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1517 if !fileContextsOptionalPath.Valid() {
1518 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1519 return
1520 }
1521 fileContexts := fileContextsOptionalPath.Path()
1522
Jiyong Park835d82b2018-12-27 16:04:18 +09001523 optFlags := []string{}
1524
Alex Light5098a612018-11-29 17:12:15 -08001525 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001526 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1527 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001528
Jiyong Park7f67f482019-01-05 12:57:48 +09001529 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1530 if overridden {
1531 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1532 }
1533
Jiyong Park40e26a22019-02-08 02:53:06 +09001534 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001535 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001536 implicitInputs = append(implicitInputs, androidManifestFile)
1537 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1538 }
1539
Jiyong Park71b519d2019-04-18 17:25:49 +09001540 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1541 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1542 ctx.Config().UnbundledBuild() &&
1543 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1544 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1545 apiFingerprint := java.ApiFingerprintPath(ctx)
1546 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1547 implicitInputs = append(implicitInputs, apiFingerprint)
1548 }
1549 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1550
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001551 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1552 if noticeFile.Valid() {
1553 // If there's a NOTICE file, embed it as an asset file in the APEX.
1554 implicitInputs = append(implicitInputs, noticeFile.Path())
1555 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1556 }
1557
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001558 if !ctx.Config().UnbundledBuild() && a.installable() {
1559 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1560 // don't need hashtree for activation. Therefore, by removing hashtree from
1561 // apex bundle (filesystem image in it, to be specific), we can save storage.
1562 optFlags = append(optFlags, "--no_hashtree")
1563 }
1564
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001565 if a.properties.Apex_name != nil {
1566 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1567 // Note that apex_manifest is also mended.
1568 optFlags = append(optFlags, "--do_not_check_keyname")
1569 }
1570
Alex Light5098a612018-11-29 17:12:15 -08001571 ctx.Build(pctx, android.BuildParams{
1572 Rule: apexRule,
1573 Implicits: implicitInputs,
1574 Output: unsignedOutputFile,
1575 Description: "apex (" + apexType.name() + ")",
1576 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001577 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1578 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1579 "copy_commands": strings.Join(copyCommands, " && "),
1580 "manifest_json_full": a.manifestJsonFullOut.String(),
1581 "manifest_json": a.manifestJsonOut.String(),
1582 "manifest": a.manifestPbOut.String(),
1583 "file_contexts": fileContexts.String(),
1584 "canned_fs_config": cannedFsConfig.String(),
1585 "key": a.private_key_file.String(),
1586 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001587 },
1588 })
1589
1590 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1591 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1592 a.bundleModuleFile = bundleModuleFile
1593
1594 ctx.Build(pctx, android.BuildParams{
1595 Rule: apexProtoConvertRule,
1596 Input: unsignedOutputFile,
1597 Output: apexProtoFile,
1598 Description: "apex proto convert",
1599 })
1600
1601 ctx.Build(pctx, android.BuildParams{
1602 Rule: apexBundleRule,
1603 Input: apexProtoFile,
1604 Output: a.bundleModuleFile,
1605 Description: "apex bundle module",
1606 Args: map[string]string{
1607 "abi": strings.Join(abis, "."),
1608 },
1609 })
1610 } else {
1611 ctx.Build(pctx, android.BuildParams{
1612 Rule: zipApexRule,
1613 Implicits: implicitInputs,
1614 Output: unsignedOutputFile,
1615 Description: "apex (" + apexType.name() + ")",
1616 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001617 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1618 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1619 "copy_commands": strings.Join(copyCommands, " && "),
1620 "manifest": a.manifestPbOut.String(),
1621 "manifest_json_full": a.manifestJsonFullOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001622 },
1623 })
Colin Crossa4925902018-11-16 11:36:28 -08001624 }
Colin Crossa4925902018-11-16 11:36:28 -08001625
Sundong Ahnabb64432019-10-22 13:58:29 +09001626 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001627 ctx.Build(pctx, android.BuildParams{
1628 Rule: java.Signapk,
1629 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001630 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001631 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001632 Implicits: []android.Path{
1633 a.container_certificate_file,
1634 a.container_private_key_file,
1635 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001636 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001637 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001638 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001639 },
1640 })
Alex Light5098a612018-11-29 17:12:15 -08001641
1642 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001643 if a.installable() {
1644 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001645 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001646 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001647}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001648
Jiyong Park8fd61922018-11-08 02:50:25 +09001649func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001650 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1651 // reply true to `InstallBypassMake()` (thus making the call
1652 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1653 // instead of `android.PathForOutput`) to return the correct path to the flattened
1654 // APEX (as its contents is installed by Make, not Soong).
1655 factx := flattenedApexContext{ctx}
1656 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1657 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1658
1659 a.buildFilesInfo(ctx)
1660}
1661
1662func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1663 cert := String(a.properties.Certificate)
1664 if cert != "" && android.SrcIsModule(cert) == "" {
1665 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1666 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1667 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1668 } else if cert == "" {
1669 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1670 a.container_certificate_file = pem
1671 a.container_private_key_file = key
1672 }
1673}
1674
1675func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001676 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001677 // 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 +09001678 // with other ordinary files.
Jooyung Han01a3ee22019-11-02 02:52:25 +09001679 a.filesInfo = append(a.filesInfo, apexFile{a.manifestJsonOut, "apex_manifest.json." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
1680 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 +09001681
Jiyong Park42cca6c2019-04-01 11:15:50 +09001682 // rename to apex_pubkey
1683 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1684 ctx.Build(pctx, android.BuildParams{
1685 Rule: android.Cp,
1686 Input: a.public_key_file,
1687 Output: copiedPubkey,
1688 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001689 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001690
Sundong Ahnabb64432019-10-22 13:58:29 +09001691 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001692 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001693 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001694 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001695 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1696 for _, sym := range fi.symlinks {
1697 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1698 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001699 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001700 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001701 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001702}
1703
1704func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001705 if a.properties.HideFromMake {
1706 return android.AndroidMkData{
1707 Disabled: true,
1708 }
1709 }
Alex Light5098a612018-11-29 17:12:15 -08001710 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001711 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001712 return android.AndroidMkData{
1713 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1714 for _, data := range writers {
1715 data.Custom(w, name, prefix, moduleDir, data)
1716 }
1717 }}
1718}
1719
Sundong Ahnabb64432019-10-22 13:58:29 +09001720func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001721 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001722 apexType := a.properties.ApexType
1723 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1724 // to install symbol files in $(PRODUCT_OUT}/apex.
1725 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1726 if !a.primaryApexType && apexType != flattenedApex {
1727 return moduleNames
1728 }
Jiyong Park94427262019-02-05 23:18:47 +09001729
1730 for _, fi := range a.filesInfo {
1731 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1732 continue
1733 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001734
1735 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001736 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001737 }
1738
Jiyong Park94427262019-02-05 23:18:47 +09001739 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1740 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001741 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001742 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001743 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001744 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001745 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001746 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001747 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001748 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001749 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1750 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001751 if len(fi.symlinks) > 0 {
1752 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1753 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001754
1755 if fi.module != nil && fi.module.NoticeFile().Valid() {
1756 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1757 }
Jiyong Park94427262019-02-05 23:18:47 +09001758 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001759 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001760 }
1761 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1762 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1763 if fi.module != nil {
1764 archStr := fi.module.Target().Arch.ArchType.String()
1765 host := false
1766 switch fi.module.Target().Os.Class {
1767 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001768 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001769 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1770 }
1771 host = true
1772 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001773 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001774 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1775 }
1776 host = true
1777 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001778 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001779 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1780 }
1781 }
1782 if host {
1783 makeOs := fi.module.Target().Os.String()
1784 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1785 makeOs = "linux"
1786 }
1787 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1788 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1789 }
1790 }
1791 if fi.class == javaSharedLib {
1792 javaModule := fi.module.(*java.Library)
1793 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1794 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1795 // we will have foo.jar.jar
1796 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1797 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1798 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1799 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1800 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1801 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001802 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001803 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001804 if cc, ok := fi.module.(*cc.Module); ok {
1805 if cc.UnstrippedOutputFile() != nil {
1806 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1807 }
1808 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001809 if cc.CoverageOutputFile().Valid() {
1810 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1811 }
Jiyong Park94427262019-02-05 23:18:47 +09001812 }
1813 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1814 } else {
1815 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001816 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Jooyung Han01a3ee22019-11-02 02:52:25 +09001817 if a.primaryApexType && fi.builtFile == a.manifestPbOut && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001818 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1819 }
Jiyong Park94427262019-02-05 23:18:47 +09001820 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1821 }
1822 }
1823 return moduleNames
1824}
1825
Sundong Ahnabb64432019-10-22 13:58:29 +09001826func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001827 return android.AndroidMkData{
1828 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1829 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001830 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001831 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001832 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001833 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001834 }
1835
Sundong Ahnabb64432019-10-22 13:58:29 +09001836 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001837 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001838 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1839 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001840 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001841 if len(moduleNames) > 0 {
1842 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1843 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001844 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001845 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001846
Sundong Ahnabb64432019-10-22 13:58:29 +09001847 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001848 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1849 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001850 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001851 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001852 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001853 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001854 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001855 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001856 if len(moduleNames) > 0 {
1857 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1858 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001859 if len(a.externalDeps) > 0 {
1860 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1861 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001862 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001863 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001864 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001865 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001866 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001867 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1868 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1869 if len(postInstallCommands) > 0 {
1870 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1871 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001872 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001873
Alex Light5098a612018-11-29 17:12:15 -08001874 if apexType == imageApex {
1875 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1876 }
Jiyong Park719b4462019-01-13 00:39:51 +09001877 }
1878 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001879}
1880
Jooyung Han344d5432019-08-23 11:17:39 +09001881func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001882 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001883 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001884 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001885 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001886 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1887 })
Alex Light5098a612018-11-29 17:12:15 -08001888 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001889 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001890 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001891 return module
1892}
Jiyong Park30ca9372019-02-07 16:27:23 +09001893
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001894func ApexBundleFactory(testApex bool, artApex bool) android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001895 bundle := newApexBundle()
1896 bundle.testApex = testApex
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001897 bundle.artApex = artApex
Jooyung Han344d5432019-08-23 11:17:39 +09001898 return bundle
1899}
1900
1901func testApexBundleFactory() android.Module {
1902 bundle := newApexBundle()
1903 bundle.testApex = true
1904 return bundle
1905}
1906
Jiyong Parkd1063c12019-07-17 20:08:41 +09001907func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001908 return newApexBundle()
1909}
1910
1911// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1912// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1913// If not specified, then the "current" versions are gathered.
1914func vndkApexBundleFactory() android.Module {
1915 bundle := newApexBundle()
1916 bundle.vndkApex = true
1917 bundle.AddProperties(&bundle.vndkProperties)
1918 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1919 ctx.AppendProperties(&struct {
1920 Compile_multilib *string
1921 }{
1922 proptools.StringPtr("both"),
1923 })
1924 })
1925 return bundle
1926}
1927
Jooyung Han31c470b2019-10-18 16:26:59 +09001928func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1929 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1930 if vndkVersion == "current" {
1931 vndkVersion = config.PlatformVndkVersion()
1932 }
1933 return vndkVersion
1934}
1935
Jiyong Park30ca9372019-02-07 16:27:23 +09001936//
1937// Defaults
1938//
1939type Defaults struct {
1940 android.ModuleBase
1941 android.DefaultsModuleBase
1942}
1943
Jiyong Park30ca9372019-02-07 16:27:23 +09001944func defaultsFactory() android.Module {
1945 return DefaultsFactory()
1946}
1947
1948func DefaultsFactory(props ...interface{}) android.Module {
1949 module := &Defaults{}
1950
1951 module.AddProperties(props...)
1952 module.AddProperties(
1953 &apexBundleProperties{},
1954 &apexTargetBundleProperties{},
1955 )
1956
1957 android.InitDefaultsModule(module)
1958 return module
1959}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001960
1961//
1962// Prebuilt APEX
1963//
1964type Prebuilt struct {
1965 android.ModuleBase
1966 prebuilt android.Prebuilt
1967
1968 properties PrebuiltProperties
1969
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001970 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001971 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001972 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001973 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001974}
1975
1976type PrebuiltProperties struct {
1977 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001978 Source string `blueprint:"mutated"`
1979 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001980
1981 Src *string
1982 Arch struct {
1983 Arm struct {
1984 Src *string
1985 }
1986 Arm64 struct {
1987 Src *string
1988 }
1989 X86 struct {
1990 Src *string
1991 }
1992 X86_64 struct {
1993 Src *string
1994 }
1995 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001996
1997 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001998 // Optional name for the installed apex. If unspecified, name of the
1999 // module is used as the file name
2000 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002001
2002 // Names of modules to be overridden. Listed modules can only be other binaries
2003 // (in Make or Soong).
2004 // This does not completely prevent installation of the overridden binaries, but if both
2005 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
2006 // from PRODUCT_PACKAGES.
2007 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002008}
2009
2010func (p *Prebuilt) installable() bool {
2011 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002012}
2013
2014func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09002015 // If the device is configured to use flattened APEX, force disable the prebuilt because
2016 // the prebuilt is a non-flattened one.
2017 forceDisable := ctx.Config().FlattenApex()
2018
2019 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
2020 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09002021 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09002022
Kun Niu10c9f832019-07-29 16:28:57 -07002023 // Force disable the prebuilts when coverage is enabled.
2024 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
2025 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
2026
Jiyong Park50b81e52019-07-11 11:24:41 +09002027 // b/137216042 don't use prebuilts when address sanitizer is on
2028 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
2029 android.InList("hwaddress", ctx.Config().SanitizeDevice())
2030
2031 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09002032 p.properties.ForceDisable = true
2033 return
2034 }
2035
Jiyong Parkc95714e2019-03-29 14:23:10 +09002036 // This is called before prebuilt_select and prebuilt_postdeps mutators
2037 // The mutators requires that src to be set correctly for each arch so that
2038 // arch variants are disabled when src is not provided for the arch.
2039 if len(ctx.MultiTargets()) != 1 {
2040 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
2041 return
2042 }
2043 var src string
2044 switch ctx.MultiTargets()[0].Arch.ArchType {
2045 case android.Arm:
2046 src = String(p.properties.Arch.Arm.Src)
2047 case android.Arm64:
2048 src = String(p.properties.Arch.Arm64.Src)
2049 case android.X86:
2050 src = String(p.properties.Arch.X86.Src)
2051 case android.X86_64:
2052 src = String(p.properties.Arch.X86_64.Src)
2053 default:
2054 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
2055 return
2056 }
2057 if src == "" {
2058 src = String(p.properties.Src)
2059 }
2060 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002061}
2062
Jiyong Park03b68dd2019-07-26 23:20:40 +09002063func (p *Prebuilt) isForceDisabled() bool {
2064 return p.properties.ForceDisable
2065}
2066
Colin Cross41955e82019-05-29 14:40:35 -07002067func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2068 switch tag {
2069 case "":
2070 return android.Paths{p.outputApex}, nil
2071 default:
2072 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2073 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002074}
2075
Jiyong Park4d277042019-04-23 18:00:10 +09002076func (p *Prebuilt) InstallFilename() string {
2077 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2078}
2079
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002080func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002081 if p.properties.ForceDisable {
2082 return
2083 }
2084
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002085 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002086 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002087 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002088 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002089 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2090 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2091 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002092 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2093 ctx.Build(pctx, android.BuildParams{
2094 Rule: android.Cp,
2095 Input: p.inputApex,
2096 Output: p.outputApex,
2097 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002098 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002099 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002100 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002101
2102 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002103}
2104
2105func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2106 return &p.prebuilt
2107}
2108
2109func (p *Prebuilt) Name() string {
2110 return p.prebuilt.Name(p.ModuleBase.Name())
2111}
2112
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002113func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2114 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002115 Class: "ETC",
2116 OutputFile: android.OptionalPathForPath(p.inputApex),
2117 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002118 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2119 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002120 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002121 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2122 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2123 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2124 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002125 },
2126 }
2127}
2128
2129// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2130func PrebuiltFactory() android.Module {
2131 module := &Prebuilt{}
2132 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002133 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002134 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002135 return module
2136}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002137
2138func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2139 // small helper to add symlink commands
2140 addSymlink := func(target, dir, linkName string) {
2141 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2142 link := filepath.Join(outDir, linkName)
2143 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2144 }
2145
2146 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2147 // When all hard-coded references are fixed, remove symbolic links
2148 // Note that we should keep following symlinks for older VNDKs (<=29)
2149 // Since prebuilt vndk libs still depend on system/lib/vndk path
2150 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2151 // the name of vndk apex is formatted "com.android.vndk.v" + version
2152 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2153 if ctx.Config().Android64() {
2154 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2155 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2156 }
2157 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2158 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2159 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2160 }
2161 }
2162 return
2163}