blob: a5925f27f8a3b114f1307fb4587e07a2694d89d7 [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:
Sundong Ahnd95aa2d2019-10-08 19:34:03 +0900348 mctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
Sundong Ahnabb64432019-10-22 13:58:29 +0900349 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
Sundong Ahnd95aa2d2019-10-08 19:34:03 +0900362 if !mctx.Config().FlattenApex() {
363 modules[i].(*apexBundle).MakeAsSystemExt()
364 }
Sundong Ahnabb64432019-10-22 13:58:29 +0900365 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900366 }
367 }
368}
369
Jooyung Han5c998b92019-06-27 11:30:33 +0900370func apexUsesMutator(mctx android.BottomUpMutatorContext) {
371 if ab, ok := mctx.Module().(*apexBundle); ok {
372 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
373 }
374}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900375
Jooyung Handc782442019-11-01 03:14:38 +0900376var (
377 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
378)
379
380// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
381// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
382// which may cause compatibility issues. (e.g. libbinder)
383// Even though libbinder restricts its availability via 'apex_available' property and relies on
384// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
385// to avoid similar problems.
386func useVendorWhitelist(config android.Config) []string {
387 return config.Once(useVendorWhitelistKey, func() interface{} {
388 return []string{
389 // swcodec uses "vendor" variants for smaller size
390 "com.android.media.swcodec",
391 "test_com.android.media.swcodec",
392 }
393 }).([]string)
394}
395
396// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
397// called before the first call to useVendorWhitelist()
398func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
399 config.Once(useVendorWhitelistKey, func() interface{} {
400 return whitelist
401 })
402}
403
Alex Light9670d332019-01-29 18:07:33 -0800404type apexNativeDependencies struct {
405 // List of native libraries
406 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900407
Alex Light9670d332019-01-29 18:07:33 -0800408 // List of native executables
409 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900410
Roland Levillain630846d2019-06-26 12:48:34 +0100411 // List of native tests
412 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800413}
Jooyung Han344d5432019-08-23 11:17:39 +0900414
Alex Light9670d332019-01-29 18:07:33 -0800415type apexMultilibProperties struct {
416 // Native dependencies whose compile_multilib is "first"
417 First apexNativeDependencies
418
419 // Native dependencies whose compile_multilib is "both"
420 Both apexNativeDependencies
421
422 // Native dependencies whose compile_multilib is "prefer32"
423 Prefer32 apexNativeDependencies
424
425 // Native dependencies whose compile_multilib is "32"
426 Lib32 apexNativeDependencies
427
428 // Native dependencies whose compile_multilib is "64"
429 Lib64 apexNativeDependencies
430}
431
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900432type apexBundleProperties struct {
433 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000434 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800435 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900436
Jiyong Park40e26a22019-02-08 02:53:06 +0900437 // AndroidManifest.xml file used for the zip container of this APEX bundle.
438 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800439 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900440
Roland Levillain411c5842019-09-19 16:37:20 +0100441 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
442 // device (/apex/<apex_name>).
443 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900444 Apex_name *string
445
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900446 // Determines the file contexts file for setting security context to each file in this APEX bundle.
447 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
448 // used.
449 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900450 File_contexts *string
451
452 // List of native shared libs that are embedded inside this APEX bundle
453 Native_shared_libs []string
454
Roland Levillain630846d2019-06-26 12:48:34 +0100455 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900456 Binaries []string
457
458 // List of java libraries that are embedded inside this APEX bundle
459 Java_libs []string
460
461 // List of prebuilt files that are embedded inside this APEX bundle
462 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900463
Roland Levillain630846d2019-06-26 12:48:34 +0100464 // List of tests that are embedded inside this APEX bundle
465 Tests []string
466
Jiyong Parkff1458f2018-10-12 21:49:38 +0900467 // Name of the apex_key module that provides the private key to sign APEX
468 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900469
Alex Light5098a612018-11-29 17:12:15 -0800470 // The type of APEX to build. Controls what the APEX payload is. Either
471 // 'image', 'zip' or 'both'. Default: 'image'.
472 Payload_type *string
473
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900474 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
475 // or an android_app_certificate module name in the form ":module".
476 Certificate *string
477
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900478 // Whether this APEX is installable to one of the partitions. Default: true.
479 Installable *bool
480
Jiyong Parkda6eb592018-12-19 17:12:36 +0900481 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
482 // Default is false.
483 Use_vendor *bool
484
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800485 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
486 Ignore_system_library_special_case *bool
487
Alex Light9670d332019-01-29 18:07:33 -0800488 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900489
Jiyong Parkf97782b2019-02-13 20:28:58 +0900490 // List of sanitizer names that this APEX is enabled for
491 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900492
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900493 PreventInstall bool `blueprint:"mutated"`
494
495 HideFromMake bool `blueprint:"mutated"`
496
Jooyung Han5c998b92019-06-27 11:30:33 +0900497 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
498 Provide_cpp_shared_libs *bool
499
500 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
501 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100502
503 // A txt file containing list of files that are whitelisted to be included in this APEX.
504 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900505
506 // List of APKs to package inside APEX
507 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900508
Sundong Ahnabb64432019-10-22 13:58:29 +0900509 // package format of this apex variant; could be non-flattened, flattened, or zip.
510 // imageApex, zipApex or flattened
511 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900512
Jiyong Parkd1063c12019-07-17 20:08:41 +0900513 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
514 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
515 // is implied. This value affects all modules included in this APEX. In other words, they are
516 // also built with the SDKs specified here.
517 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800518}
519
520type apexTargetBundleProperties struct {
521 Target struct {
522 // Multilib properties only for android.
523 Android struct {
524 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900525 }
Jooyung Han344d5432019-08-23 11:17:39 +0900526
Alex Light9670d332019-01-29 18:07:33 -0800527 // Multilib properties only for host.
528 Host struct {
529 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900530 }
Jooyung Han344d5432019-08-23 11:17:39 +0900531
Alex Light9670d332019-01-29 18:07:33 -0800532 // Multilib properties only for host linux_bionic.
533 Linux_bionic struct {
534 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900535 }
Jooyung Han344d5432019-08-23 11:17:39 +0900536
Alex Light9670d332019-01-29 18:07:33 -0800537 // Multilib properties only for host linux_glibc.
538 Linux_glibc struct {
539 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900540 }
541 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900542}
543
Jooyung Han344d5432019-08-23 11:17:39 +0900544type apexVndkProperties struct {
545 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
546 Vndk_version *string
547}
548
Jiyong Park8fd61922018-11-08 02:50:25 +0900549type apexFileClass int
550
551const (
552 etc apexFileClass = iota
553 nativeSharedLib
554 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900555 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800556 pyBinary
557 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900558 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100559 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900560 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900561)
562
Alex Light5098a612018-11-29 17:12:15 -0800563type apexPackaging int
564
565const (
566 imageApex apexPackaging = iota
567 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900568 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800569)
570
Sundong Ahnabb64432019-10-22 13:58:29 +0900571// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800572func (a apexPackaging) suffix() string {
573 switch a {
574 case imageApex:
575 return imageApexSuffix
576 case zipApex:
577 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800578 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100579 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800580 }
581}
582
583func (a apexPackaging) name() string {
584 switch a {
585 case imageApex:
586 return imageApexType
587 case zipApex:
588 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800589 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100590 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800591 }
592}
593
Jiyong Park8fd61922018-11-08 02:50:25 +0900594func (class apexFileClass) NameInMake() string {
595 switch class {
596 case etc:
597 return "ETC"
598 case nativeSharedLib:
599 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800600 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900601 return "EXECUTABLES"
602 case javaSharedLib:
603 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100604 case nativeTest:
605 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900606 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900607 // b/142537672 Why isn't this APP? We want to have full control over
608 // the paths and file names of the apk file under the flattend APEX.
609 // If this is set to APP, then the paths and file names are modified
610 // by the Make build system. For example, it is installed to
611 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
612 // /system/apex/<apexname>/app/<Appname> because the build system automatically
613 // appends module name (which is <apexname>.<Appname> to the path.
614 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900615 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100616 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900617 }
618}
619
620type apexFile struct {
621 builtFile android.Path
622 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900623 installDir string
624 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900625 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800626 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900627}
628
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900629type apexBundle struct {
630 android.ModuleBase
631 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900632 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900633
Alex Light9670d332019-01-29 18:07:33 -0800634 properties apexBundleProperties
635 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900636 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900637
Colin Crossa4925902018-11-16 11:36:28 -0800638 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900639 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700640 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900641
Jiyong Park03b68dd2019-07-26 23:20:40 +0900642 prebuiltFileToDelete string
643
Jiyong Park42cca6c2019-04-01 11:15:50 +0900644 public_key_file android.Path
645 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900646
647 container_certificate_file android.Path
648 container_private_key_file android.Path
649
Jiyong Park8fd61922018-11-08 02:50:25 +0900650 // list of files to be included in this apex
651 filesInfo []apexFile
652
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900653 // list of module names that this APEX is depending on
654 externalDeps []string
655
Sundong Ahnabb64432019-10-22 13:58:29 +0900656 testApex bool
657 vndkApex bool
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000658 artApex bool
Sundong Ahnabb64432019-10-22 13:58:29 +0900659 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900660
661 // intermediate path for apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +0900662 manifestJsonOut android.WritablePath
663 manifestJsonFullOut android.WritablePath
664 manifestPbOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900665
666 // list of commands to create symlinks for backward compatibility
667 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
668 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
669 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
670 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900671
672 // Suffix of module name in Android.mk
673 // ".flattened", ".apex", ".zipapex", or ""
674 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900675}
676
Jiyong Park397e55e2018-10-24 21:09:55 +0900677func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100678 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700679 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900680 // Use *FarVariation* to be able to depend on modules having
681 // conflicting variations with this module. This is required since
682 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
683 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700684 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900685 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900686 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900687 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700688 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900689
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700690 ctx.AddFarVariationDependencies(append(target.Variations(),
691 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
692 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100693
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700694 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100695 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100696 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700697 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900698}
699
Alex Light9670d332019-01-29 18:07:33 -0800700func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
701 if ctx.Os().Class == android.Device {
702 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
703 } else {
704 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
705 if ctx.Os().Bionic() {
706 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
707 } else {
708 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
709 }
710 }
711}
712
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900713func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900714 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
715 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
716 }
717
Jiyong Park397e55e2018-10-24 21:09:55 +0900718 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900719 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800720
721 a.combineProperties(ctx)
722
Jiyong Park397e55e2018-10-24 21:09:55 +0900723 has32BitTarget := false
724 for _, target := range targets {
725 if target.Arch.ArchType.Multilib == "lib32" {
726 has32BitTarget = true
727 }
728 }
729 for i, target := range targets {
730 // When multilib.* is omitted for native_shared_libs, it implies
731 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700732 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900733 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900734 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700735 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900736
Roland Levillain630846d2019-06-26 12:48:34 +0100737 // When multilib.* is omitted for tests, it implies
738 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700739 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100740 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100741 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700742 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100743
Jiyong Park397e55e2018-10-24 21:09:55 +0900744 // Add native modules targetting both ABIs
745 addDependenciesForNativeModules(ctx,
746 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100747 a.properties.Multilib.Both.Binaries,
748 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700749 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900750 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900751
Alex Light3d673592019-01-18 14:37:31 -0800752 isPrimaryAbi := i == 0
753 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900754 // When multilib.* is omitted for binaries, it implies
755 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700756 ctx.AddFarVariationDependencies(append(target.Variations(),
757 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
758 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900759
760 // Add native modules targetting the first ABI
761 addDependenciesForNativeModules(ctx,
762 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100763 a.properties.Multilib.First.Binaries,
764 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700765 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900766 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800767
768 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700769 ctx.AddFarVariationDependencies(target.Variations(),
770 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900771 }
772
773 switch target.Arch.ArchType.Multilib {
774 case "lib32":
775 // Add native modules targetting 32-bit ABI
776 addDependenciesForNativeModules(ctx,
777 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100778 a.properties.Multilib.Lib32.Binaries,
779 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700780 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900781 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900782
783 addDependenciesForNativeModules(ctx,
784 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100785 a.properties.Multilib.Prefer32.Binaries,
786 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700787 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900788 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900789 case "lib64":
790 // Add native modules targetting 64-bit ABI
791 addDependenciesForNativeModules(ctx,
792 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100793 a.properties.Multilib.Lib64.Binaries,
794 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700795 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900796 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900797
798 if !has32BitTarget {
799 addDependenciesForNativeModules(ctx,
800 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100801 a.properties.Multilib.Prefer32.Binaries,
802 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700803 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900804 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900805 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700806
807 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
808 for _, sanitizer := range ctx.Config().SanitizeDevice() {
809 if sanitizer == "hwaddress" {
810 addDependenciesForNativeModules(ctx,
811 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700812 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700813 break
814 }
815 }
816 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900817 }
818
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900819 }
820
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700821 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
822 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900823
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700824 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
825 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900826
Jiyong Park23c52b02019-02-02 13:13:47 +0900827 if String(a.properties.Key) == "" {
828 ctx.ModuleErrorf("key is missing")
829 return
830 }
831 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900832
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900833 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900834 if cert != "" {
835 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900836 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900837
838 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
839 if len(a.properties.Uses_sdks) > 0 {
840 sdkRefs := []android.SdkRef{}
841 for _, str := range a.properties.Uses_sdks {
842 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
843 sdkRefs = append(sdkRefs, parsed)
844 }
845 a.BuildWithSdks(sdkRefs)
846 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900847}
848
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900849func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
850 // direct deps of an APEX bundle are all part of the APEX bundle
851 return true
852}
853
Colin Cross0ea8ba82019-06-06 14:33:29 -0700854func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900855 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
856 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000857 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900858 }
859 return String(a.properties.Certificate)
860}
861
Colin Cross41955e82019-05-29 14:40:35 -0700862func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
863 switch tag {
864 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900865 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700866 default:
867 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900868 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900869}
870
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900871func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900872 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900873}
874
Jiyong Park7c1dc612019-01-05 11:15:24 +0900875func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900876 if a.vndkApex {
877 return "vendor." + a.vndkVersion(config)
878 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900879 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900880 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900881 } else {
882 return "core"
883 }
884}
885
Jiyong Parkf97782b2019-02-13 20:28:58 +0900886func (a *apexBundle) EnableSanitizer(sanitizerName string) {
887 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
888 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
889 }
890}
891
Jiyong Park388ef3f2019-01-28 19:47:32 +0900892func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900893 if android.InList(sanitizerName, a.properties.SanitizerNames) {
894 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900895 }
896
897 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900898 globalSanitizerNames := []string{}
899 if a.Host() {
900 globalSanitizerNames = ctx.Config().SanitizeHost()
901 } else {
902 arches := ctx.Config().SanitizeDeviceArch()
903 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
904 globalSanitizerNames = ctx.Config().SanitizeDevice()
905 }
906 }
907 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900908}
909
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900910func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
911 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
912}
913
914func (a *apexBundle) PreventInstall() {
915 a.properties.PreventInstall = true
916}
917
918func (a *apexBundle) HideFromMake() {
919 a.properties.HideFromMake = true
920}
921
Martin Stjernholm279de572019-09-10 23:18:20 +0100922func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900923 // Decide the APEX-local directory by the multilib of the library
924 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100925 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900926 case "lib32":
927 dirInApex = "lib"
928 case "lib64":
929 dirInApex = "lib64"
930 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100931 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700932 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100933 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900934 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100935 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
936 // Special case for Bionic libs and other libs installed with them. This is
937 // to prevent those libs from being included in the search path
938 // /apex/com.android.runtime/${LIB}. This exclusion is required because
939 // those libs in the Runtime APEX are available via the legacy paths in
940 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
941 // to the legacy paths and thus will be loaded into the default linker
942 // namespace (aka "platform" namespace). If the libs are directly in
943 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
944 // into the runtime linker namespace, which will result in double loading of
945 // them, which isn't supported.
946 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900947 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900948
Martin Stjernholm279de572019-09-10 23:18:20 +0100949 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900950 return
951}
952
953func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900954 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700955 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200956 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900957 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900958 fileToCopy = cc.OutputFile().Path()
959 return
960}
961
Alex Light778127a2019-02-27 14:19:50 -0800962func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
963 dirInApex = "bin"
964 fileToCopy = py.HostToolPath().Path()
965 return
966}
967func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
968 dirInApex = "bin"
969 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
970 if err != nil {
971 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
972 return
973 }
974 fileToCopy = android.PathForOutput(ctx, s)
975 return
976}
977
Jiyong Park04480cf2019-02-06 00:16:29 +0900978func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
979 dirInApex = filepath.Join("bin", sh.SubDir())
980 fileToCopy = sh.OutputFile()
981 return
982}
983
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900984func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
985 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900986 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900987 return
988}
989
Jiyong Park9e6c2422019-08-09 20:39:45 +0900990func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
991 dirInApex = "javalib"
992 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
993 implJars := java.ImplementationJars()
994 if len(implJars) != 1 {
995 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
996 strings.Join(implJars.Strings(), ", ")))
997 }
998 fileToCopy = implJars[0]
999 return
1000}
1001
Jooyung Han39edb6c2019-11-06 16:53:07 +09001002func getCopyManifestForPrebuiltEtc(prebuilt android.PrebuiltEtcModule) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001003 dirInApex = filepath.Join("etc", prebuilt.SubDir())
1004 fileToCopy = prebuilt.OutputFile()
1005 return
1006}
1007
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001008func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +09001009 appDir := "app"
1010 if app.Privileged() {
1011 appDir = "priv-app"
1012 }
1013 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001014 fileToCopy = app.OutputFile()
1015 return
1016}
1017
Dario Frenicde2a032019-10-27 00:29:22 +01001018func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
1019 appDir := "app"
1020 if app.Privileged() {
1021 appDir = "priv-app"
1022 }
1023 dirInApex = filepath.Join(appDir, pkgName)
1024 fileToCopy = app.OutputFile()
1025 return
1026}
1027
Roland Levillain935639d2019-08-13 14:55:28 +01001028// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1029type flattenedApexContext struct {
1030 android.ModuleContext
1031}
1032
1033func (c *flattenedApexContext) InstallBypassMake() bool {
1034 return true
1035}
1036
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001037func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001038 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001039
Sundong Ahnabb64432019-10-22 13:58:29 +09001040 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1041 switch a.properties.ApexType {
1042 case imageApex:
1043 if buildFlattenedAsDefault {
1044 a.suffix = imageApexSuffix
1045 } else {
1046 a.suffix = ""
1047 a.primaryApexType = true
1048 }
1049 case zipApex:
1050 if proptools.String(a.properties.Payload_type) == "zip" {
1051 a.suffix = ""
1052 a.primaryApexType = true
1053 } else {
1054 a.suffix = zipApexSuffix
1055 }
1056 case flattenedApex:
1057 if buildFlattenedAsDefault {
1058 a.suffix = ""
1059 a.primaryApexType = true
1060 } else {
1061 a.suffix = flattenedSuffix
1062 }
Alex Light5098a612018-11-29 17:12:15 -08001063 }
1064
Roland Levillain630846d2019-06-26 12:48:34 +01001065 if len(a.properties.Tests) > 0 && !a.testApex {
1066 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1067 return
1068 }
1069
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001070 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1071
Jooyung Hane1633032019-08-01 17:41:43 +09001072 // native lib dependencies
1073 var provideNativeLibs []string
1074 var requireNativeLibs []string
1075
Jooyung Han5c998b92019-06-27 11:30:33 +09001076 // Check if "uses" requirements are met with dependent apexBundles
1077 var providedNativeSharedLibs []string
1078 useVendor := proptools.Bool(a.properties.Use_vendor)
1079 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1080 if ctx.OtherModuleDependencyTag(m) != usesTag {
1081 return
1082 }
1083 otherName := ctx.OtherModuleName(m)
1084 other, ok := m.(*apexBundle)
1085 if !ok {
1086 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1087 return
1088 }
1089 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1090 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1091 return
1092 }
1093 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1094 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1095 return
1096 }
1097 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1098 })
1099
Alex Light778127a2019-02-27 14:19:50 -08001100 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001101 depTag := ctx.OtherModuleDependencyTag(child)
1102 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001103 if _, ok := parent.(*apexBundle); ok {
1104 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001105 switch depTag {
1106 case sharedLibTag:
1107 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001108 if cc.HasStubsVariants() {
1109 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1110 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001111 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001112 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001113 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001114 } else {
1115 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001116 }
1117 case executableTag:
1118 if cc, ok := child.(*cc.Module); ok {
1119 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001120 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001121 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001122 } else if sh, ok := child.(*android.ShBinary); ok {
1123 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001124 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001125 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1126 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1127 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1128 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1129 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1130 // NB: Since go binaries are static we don't need the module for anything here, which is
1131 // good since the go tool is a blueprint.Module not an android.Module like we would
1132 // normally use.
1133 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001134 } else {
Alex Light778127a2019-02-27 14:19:50 -08001135 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 +09001136 }
1137 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001138 if javaLib, ok := child.(*java.Library); ok {
1139 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001140 if fileToCopy == nil {
1141 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1142 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001143 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1144 }
1145 return true
1146 } else if javaLib, ok := child.(*java.Import); ok {
1147 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1148 if fileToCopy == nil {
1149 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1150 } else {
1151 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001152 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001153 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001154 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001155 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001156 }
1157 case prebuiltTag:
Jooyung Han39edb6c2019-11-06 16:53:07 +09001158 if prebuilt, ok := child.(android.PrebuiltEtcModule); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001159 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001160 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001161 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001162 } else {
1163 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1164 }
Roland Levillain630846d2019-06-26 12:48:34 +01001165 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001166 if ccTest, ok := child.(*cc.Module); ok {
1167 if ccTest.IsTestPerSrcAllTestsVariation() {
1168 // Multiple-output test module (where `test_per_src: true`).
1169 //
1170 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1171 // We do not add this variation to `filesInfo`, as it has no output;
1172 // however, we do add the other variations of this module as indirect
1173 // dependencies (see below).
1174 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001175 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001176 // Single-output test module (where `test_per_src: false`).
1177 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1178 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001179 }
Roland Levillain630846d2019-06-26 12:48:34 +01001180 return true
1181 } else {
1182 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1183 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001184 case keyTag:
1185 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001186 a.private_key_file = key.private_key_file
1187 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001188 return false
1189 } else {
1190 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001191 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001192 case certificateTag:
1193 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001194 a.container_certificate_file = dep.Certificate.Pem
1195 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001196 return false
1197 } else {
1198 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1199 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001200 case android.PrebuiltDepTag:
1201 // If the prebuilt is force disabled, remember to delete the prebuilt file
1202 // that might have been installed in the previous builds
1203 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1204 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1205 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001206 case androidAppTag:
1207 if ap, ok := child.(*java.AndroidApp); ok {
1208 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1209 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1210 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001211 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1212 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1213 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001214 } else {
1215 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1216 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001217 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001218 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001219 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001220 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001221 // We cannot use a switch statement on `depTag` here as the checked
1222 // tags used below are private (e.g. `cc.sharedDepTag`).
Sam Sacconee3c22f72019-11-13 15:23:05 +00001223 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) || java.IsJniDepTag(depTag) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001224 if cc, ok := child.(*cc.Module); ok {
1225 if android.InList(cc.Name(), providedNativeSharedLibs) {
1226 // If we're using a shared library which is provided from other APEX,
1227 // don't include it in this APEX
1228 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001229 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001230 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1231 // If the dependency is a stubs lib, don't include it in this APEX,
1232 // but make sure that the lib is installed on the device.
1233 // In case no APEX is having the lib, the lib is installed to the system
1234 // partition.
1235 //
1236 // Always include if we are a host-apex however since those won't have any
1237 // system libraries.
1238 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1239 a.externalDeps = append(a.externalDeps, cc.Name())
1240 }
Jooyung Hane1633032019-08-01 17:41:43 +09001241 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001242 // Don't track further
1243 return false
1244 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001245 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001246 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1247 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001248 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001249 } else if cc.IsTestPerSrcDepTag(depTag) {
1250 if cc, ok := child.(*cc.Module); ok {
1251 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1252 // Handle modules created as `test_per_src` variations of a single test module:
1253 // use the name of the generated test binary (`fileToCopy`) instead of the name
1254 // of the original test module (`depName`, shared by all `test_per_src`
1255 // variations of that module).
1256 moduleName := filepath.Base(fileToCopy.String())
1257 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1258 return true
1259 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001260 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001261 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001262 }
1263 }
1264 }
1265 return false
1266 })
1267
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001268 // Specific to the ART apex: dexpreopt artifacts for libcore Java libraries.
1269 // Build rules are generated by the dexpreopt singleton, and here we access build artifacts
1270 // via the global boot image config.
1271 if a.artApex {
1272 for arch, files := range java.DexpreoptedArtApexJars(ctx) {
1273 dirInApex := filepath.Join("javalib", arch.String())
1274 for _, f := range files {
1275 localModule := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
1276 filesInfo = append(filesInfo, apexFile{f, localModule, dirInApex, etc, nil, nil})
1277 }
1278 }
1279 }
1280
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001281 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001282 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1283 return
1284 }
1285
Jiyong Park8fd61922018-11-08 02:50:25 +09001286 // remove duplicates in filesInfo
1287 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001288 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001289 result := []apexFile{}
1290 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001291 dest := filepath.Join(f.installDir, f.builtFile.Base())
1292 if !encountered[dest] {
1293 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001294 result = append(result, f)
1295 }
1296 }
1297 return result
1298 }
1299 filesInfo = removeDup(filesInfo)
1300
1301 // to have consistent build rules
1302 sort.Slice(filesInfo, func(i, j int) bool {
1303 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1304 })
1305
Jiyong Park127b40b2019-09-30 16:04:35 +09001306 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001307 if !ctx.Host() {
1308 for _, fi := range filesInfo {
1309 if am, ok := fi.module.(android.ApexModule); ok {
1310 if !am.AvailableFor(ctx.ModuleName()) {
1311 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1312 return
1313 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001314 }
1315 }
1316 }
1317
Jiyong Park8fd61922018-11-08 02:50:25 +09001318 // prepend the name of this APEX to the module names. These names will be the names of
1319 // modules that will be defined if the APEX is flattened.
1320 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001321 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001322 }
1323
Jiyong Park8fd61922018-11-08 02:50:25 +09001324 a.installDir = android.PathForModuleInstall(ctx, "apex")
1325 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001326
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001327 // prepare apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +09001328 a.buildManifest(ctx, provideNativeLibs, requireNativeLibs)
1329
1330 a.setCertificateAndPrivateKey(ctx)
1331 if a.properties.ApexType == flattenedApex {
1332 a.buildFlattenedApex(ctx)
1333 } else {
1334 a.buildUnflattenedApex(ctx)
1335 }
1336
1337 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1338 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
1339}
1340
1341func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
Jooyung Hane1633032019-08-01 17:41:43 +09001342 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001343
Jooyung Han01a3ee22019-11-02 02:52:25 +09001344 a.manifestJsonFullOut = android.PathForModuleOut(ctx, "apex_manifest_full.json")
1345
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001346 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001347 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1348 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001349
1350 // apex name can be overridden
1351 optCommands := []string{}
1352 if a.properties.Apex_name != nil {
1353 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1354 }
1355
Jooyung Hane1633032019-08-01 17:41:43 +09001356 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001357 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001358 Input: manifestSrc,
Jooyung Han01a3ee22019-11-02 02:52:25 +09001359 Output: a.manifestJsonFullOut,
Jooyung Hane1633032019-08-01 17:41:43 +09001360 Args: map[string]string{
1361 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1362 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001363 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001364 },
1365 })
1366
Jooyung Han01a3ee22019-11-02 02:52:25 +09001367 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
Roland Levillain8ac05572019-11-13 10:45:55 +00001368 // prepare stripped-down version so that APEX modules built from R+ can be installed to Q
Jooyung Han01a3ee22019-11-02 02:52:25 +09001369 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1370 ctx.Build(pctx, android.BuildParams{
1371 Rule: stripApexManifestRule,
1372 Input: a.manifestJsonFullOut,
1373 Output: a.manifestJsonOut,
1374 })
Jooyung Han72bd2f82019-10-23 16:46:38 +09001375
Jooyung Han01a3ee22019-11-02 02:52:25 +09001376 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
1377 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
1378 ctx.Build(pctx, android.BuildParams{
1379 Rule: pbApexManifestRule,
1380 Input: a.manifestJsonFullOut,
1381 Output: a.manifestPbOut,
1382 })
Jiyong Park8fd61922018-11-08 02:50:25 +09001383}
1384
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001385func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001386 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001387 for _, f := range a.filesInfo {
1388 if f.module != nil {
1389 notice := f.module.NoticeFile()
1390 if notice.Valid() {
1391 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001392 }
1393 }
1394 }
1395 // append the notice file specified in the apex module itself
1396 if a.NoticeFile().Valid() {
1397 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001398 }
1399
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001400 if len(noticeFiles) == 0 {
1401 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001402 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001403
Jaewoong Jung98772792019-07-01 17:15:13 -07001404 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001405}
1406
Sundong Ahnabb64432019-10-22 13:58:29 +09001407func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001408 var abis []string
1409 for _, target := range ctx.MultiTargets() {
1410 if len(target.Arch.Abi) > 0 {
1411 abis = append(abis, target.Arch.Abi[0])
1412 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001413 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001414
Alex Light5098a612018-11-29 17:12:15 -08001415 abis = android.FirstUniqueStrings(abis)
1416
Sundong Ahnabb64432019-10-22 13:58:29 +09001417 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001418 suffix := apexType.suffix()
1419 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001420
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001421 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001422 for _, f := range a.filesInfo {
1423 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001424 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001425
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001426 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001427 emitCommands := []string{}
1428 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1429 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001430 for i, src := range filesToCopy {
1431 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001432 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001433 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001434 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1435 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001436 for _, sym := range a.filesInfo[i].symlinks {
1437 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1438 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1439 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001440 }
Dario Frenie4235822019-10-28 14:49:27 +00001441 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1442
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001443 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Han01a3ee22019-11-02 02:52:25 +09001444 implicitInputs = append(implicitInputs, a.manifestPbOut, a.manifestJsonFullOut, a.manifestJsonOut)
Alex Light5098a612018-11-29 17:12:15 -08001445
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001446 if a.properties.Whitelisted_files != nil {
1447 ctx.Build(pctx, android.BuildParams{
1448 Rule: emitApexContentRule,
1449 Implicits: implicitInputs,
1450 Output: imageContentFile,
1451 Description: "emit apex image content",
1452 Args: map[string]string{
1453 "emit_commands": strings.Join(emitCommands, " && "),
1454 },
1455 })
1456 implicitInputs = append(implicitInputs, imageContentFile)
1457 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1458
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001459 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001460 ctx.Build(pctx, android.BuildParams{
1461 Rule: diffApexContentRule,
1462 Implicits: implicitInputs,
1463 Output: phonyOutput,
1464 Description: "diff apex image content",
1465 Args: map[string]string{
1466 "whitelisted_files_file": whitelistedFilesFile.String(),
1467 "image_content_file": imageContentFile.String(),
1468 "apex_module_name": ctx.ModuleName(),
1469 },
1470 })
1471
1472 implicitInputs = append(implicitInputs, phonyOutput)
1473 }
1474
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001475 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1476 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001477
Sundong Ahnabb64432019-10-22 13:58:29 +09001478 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001479 // files and dirs that will be created in APEX
Jooyung Han01a3ee22019-11-02 02:52:25 +09001480 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
Alex Light5098a612018-11-29 17:12:15 -08001481 var executablePaths []string // this also includes dirs
1482 for _, f := range a.filesInfo {
1483 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001484 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001485 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001486 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001487 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001488 }
Alex Light5098a612018-11-29 17:12:15 -08001489 } else {
1490 readOnlyPaths = append(readOnlyPaths, pathInApex)
1491 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001492 dir := f.installDir
1493 for !android.InList(dir, executablePaths) && dir != "" {
1494 executablePaths = append(executablePaths, dir)
1495 dir, _ = filepath.Split(dir) // move up to the parent
1496 if len(dir) > 0 {
1497 // remove trailing slash
1498 dir = dir[:len(dir)-1]
1499 }
Alex Light5098a612018-11-29 17:12:15 -08001500 }
1501 }
1502 sort.Strings(readOnlyPaths)
1503 sort.Strings(executablePaths)
1504 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1505 ctx.Build(pctx, android.BuildParams{
1506 Rule: generateFsConfig,
1507 Output: cannedFsConfig,
1508 Description: "generate fs config",
1509 Args: map[string]string{
1510 "ro_paths": strings.Join(readOnlyPaths, " "),
1511 "exec_paths": strings.Join(executablePaths, " "),
1512 },
1513 })
1514
1515 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1516 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1517 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1518 if !fileContextsOptionalPath.Valid() {
1519 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1520 return
1521 }
1522 fileContexts := fileContextsOptionalPath.Path()
1523
Jiyong Park835d82b2018-12-27 16:04:18 +09001524 optFlags := []string{}
1525
Alex Light5098a612018-11-29 17:12:15 -08001526 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001527 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1528 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001529
Jiyong Park7f67f482019-01-05 12:57:48 +09001530 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1531 if overridden {
1532 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1533 }
1534
Jiyong Park40e26a22019-02-08 02:53:06 +09001535 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001536 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001537 implicitInputs = append(implicitInputs, androidManifestFile)
1538 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1539 }
1540
Jiyong Park71b519d2019-04-18 17:25:49 +09001541 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1542 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1543 ctx.Config().UnbundledBuild() &&
1544 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1545 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1546 apiFingerprint := java.ApiFingerprintPath(ctx)
1547 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1548 implicitInputs = append(implicitInputs, apiFingerprint)
1549 }
1550 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1551
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001552 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1553 if noticeFile.Valid() {
1554 // If there's a NOTICE file, embed it as an asset file in the APEX.
1555 implicitInputs = append(implicitInputs, noticeFile.Path())
1556 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1557 }
1558
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001559 if !ctx.Config().UnbundledBuild() && a.installable() {
1560 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1561 // don't need hashtree for activation. Therefore, by removing hashtree from
1562 // apex bundle (filesystem image in it, to be specific), we can save storage.
1563 optFlags = append(optFlags, "--no_hashtree")
1564 }
1565
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001566 if a.properties.Apex_name != nil {
1567 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1568 // Note that apex_manifest is also mended.
1569 optFlags = append(optFlags, "--do_not_check_keyname")
1570 }
1571
Alex Light5098a612018-11-29 17:12:15 -08001572 ctx.Build(pctx, android.BuildParams{
1573 Rule: apexRule,
1574 Implicits: implicitInputs,
1575 Output: unsignedOutputFile,
1576 Description: "apex (" + apexType.name() + ")",
1577 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001578 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1579 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1580 "copy_commands": strings.Join(copyCommands, " && "),
1581 "manifest_json_full": a.manifestJsonFullOut.String(),
1582 "manifest_json": a.manifestJsonOut.String(),
1583 "manifest": a.manifestPbOut.String(),
1584 "file_contexts": fileContexts.String(),
1585 "canned_fs_config": cannedFsConfig.String(),
1586 "key": a.private_key_file.String(),
1587 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001588 },
1589 })
1590
1591 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1592 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1593 a.bundleModuleFile = bundleModuleFile
1594
1595 ctx.Build(pctx, android.BuildParams{
1596 Rule: apexProtoConvertRule,
1597 Input: unsignedOutputFile,
1598 Output: apexProtoFile,
1599 Description: "apex proto convert",
1600 })
1601
1602 ctx.Build(pctx, android.BuildParams{
1603 Rule: apexBundleRule,
1604 Input: apexProtoFile,
1605 Output: a.bundleModuleFile,
1606 Description: "apex bundle module",
1607 Args: map[string]string{
1608 "abi": strings.Join(abis, "."),
1609 },
1610 })
1611 } else {
1612 ctx.Build(pctx, android.BuildParams{
1613 Rule: zipApexRule,
1614 Implicits: implicitInputs,
1615 Output: unsignedOutputFile,
1616 Description: "apex (" + apexType.name() + ")",
1617 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001618 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1619 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1620 "copy_commands": strings.Join(copyCommands, " && "),
1621 "manifest": a.manifestPbOut.String(),
1622 "manifest_json_full": a.manifestJsonFullOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001623 },
1624 })
Colin Crossa4925902018-11-16 11:36:28 -08001625 }
Colin Crossa4925902018-11-16 11:36:28 -08001626
Sundong Ahnabb64432019-10-22 13:58:29 +09001627 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001628 ctx.Build(pctx, android.BuildParams{
1629 Rule: java.Signapk,
1630 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001631 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001632 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001633 Implicits: []android.Path{
1634 a.container_certificate_file,
1635 a.container_private_key_file,
1636 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001637 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001638 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001639 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001640 },
1641 })
Alex Light5098a612018-11-29 17:12:15 -08001642
1643 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001644 if a.installable() {
1645 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001646 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001647 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001648}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001649
Jiyong Park8fd61922018-11-08 02:50:25 +09001650func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001651 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1652 // reply true to `InstallBypassMake()` (thus making the call
1653 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1654 // instead of `android.PathForOutput`) to return the correct path to the flattened
1655 // APEX (as its contents is installed by Make, not Soong).
1656 factx := flattenedApexContext{ctx}
1657 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1658 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1659
1660 a.buildFilesInfo(ctx)
1661}
1662
1663func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1664 cert := String(a.properties.Certificate)
1665 if cert != "" && android.SrcIsModule(cert) == "" {
1666 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1667 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1668 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1669 } else if cert == "" {
1670 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1671 a.container_certificate_file = pem
1672 a.container_private_key_file = key
1673 }
1674}
1675
1676func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001677 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001678 // 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 +09001679 // with other ordinary files.
Jooyung Han01a3ee22019-11-02 02:52:25 +09001680 a.filesInfo = append(a.filesInfo, apexFile{a.manifestJsonOut, "apex_manifest.json." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
1681 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 +09001682
Jiyong Park42cca6c2019-04-01 11:15:50 +09001683 // rename to apex_pubkey
1684 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1685 ctx.Build(pctx, android.BuildParams{
1686 Rule: android.Cp,
1687 Input: a.public_key_file,
1688 Output: copiedPubkey,
1689 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001690 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001691
Sundong Ahnabb64432019-10-22 13:58:29 +09001692 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001693 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001694 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001695 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001696 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1697 for _, sym := range fi.symlinks {
1698 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1699 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001700 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001701 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001702 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001703}
1704
1705func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001706 if a.properties.HideFromMake {
1707 return android.AndroidMkData{
1708 Disabled: true,
1709 }
1710 }
Alex Light5098a612018-11-29 17:12:15 -08001711 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001712 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001713 return android.AndroidMkData{
1714 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1715 for _, data := range writers {
1716 data.Custom(w, name, prefix, moduleDir, data)
1717 }
1718 }}
1719}
1720
Sundong Ahnabb64432019-10-22 13:58:29 +09001721func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001722 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001723 apexType := a.properties.ApexType
1724 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1725 // to install symbol files in $(PRODUCT_OUT}/apex.
1726 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1727 if !a.primaryApexType && apexType != flattenedApex {
1728 return moduleNames
1729 }
Jiyong Park94427262019-02-05 23:18:47 +09001730
1731 for _, fi := range a.filesInfo {
1732 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1733 continue
1734 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001735
1736 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001737 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001738 }
1739
Jiyong Park94427262019-02-05 23:18:47 +09001740 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1741 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001742 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001743 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001744 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001745 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001746 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001747 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001748 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001749 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001750 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1751 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001752 if len(fi.symlinks) > 0 {
1753 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1754 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001755
1756 if fi.module != nil && fi.module.NoticeFile().Valid() {
1757 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1758 }
Jiyong Park94427262019-02-05 23:18:47 +09001759 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001760 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001761 }
1762 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1763 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1764 if fi.module != nil {
1765 archStr := fi.module.Target().Arch.ArchType.String()
1766 host := false
1767 switch fi.module.Target().Os.Class {
1768 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001769 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001770 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1771 }
1772 host = true
1773 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001774 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001775 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1776 }
1777 host = true
1778 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001779 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001780 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1781 }
1782 }
1783 if host {
1784 makeOs := fi.module.Target().Os.String()
1785 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1786 makeOs = "linux"
1787 }
1788 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1789 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1790 }
1791 }
1792 if fi.class == javaSharedLib {
1793 javaModule := fi.module.(*java.Library)
1794 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1795 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1796 // we will have foo.jar.jar
1797 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1798 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1799 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1800 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1801 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1802 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001803 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001804 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001805 if cc, ok := fi.module.(*cc.Module); ok {
1806 if cc.UnstrippedOutputFile() != nil {
1807 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1808 }
1809 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001810 if cc.CoverageOutputFile().Valid() {
1811 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1812 }
Jiyong Park94427262019-02-05 23:18:47 +09001813 }
1814 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1815 } else {
1816 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001817 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Jooyung Han01a3ee22019-11-02 02:52:25 +09001818 if a.primaryApexType && fi.builtFile == a.manifestPbOut && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001819 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1820 }
Jiyong Park94427262019-02-05 23:18:47 +09001821 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1822 }
1823 }
1824 return moduleNames
1825}
1826
Sundong Ahnabb64432019-10-22 13:58:29 +09001827func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001828 return android.AndroidMkData{
1829 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1830 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001831 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001832 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001833 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001834 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001835 }
1836
Sundong Ahnabb64432019-10-22 13:58:29 +09001837 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001838 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001839 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1840 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001841 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001842 if len(moduleNames) > 0 {
1843 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1844 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001845 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001846 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001847
Sundong Ahnabb64432019-10-22 13:58:29 +09001848 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001849 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1850 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001851 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001852 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001853 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001854 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001855 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001856 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001857 if len(moduleNames) > 0 {
1858 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1859 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001860 if len(a.externalDeps) > 0 {
1861 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1862 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001863 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001864 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001865 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001866 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001867 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001868 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1869 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1870 if len(postInstallCommands) > 0 {
1871 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1872 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001873 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001874
Alex Light5098a612018-11-29 17:12:15 -08001875 if apexType == imageApex {
1876 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1877 }
Jiyong Park719b4462019-01-13 00:39:51 +09001878 }
1879 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001880}
1881
Jooyung Han344d5432019-08-23 11:17:39 +09001882func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001883 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001884 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001885 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001886 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001887 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1888 })
Alex Light5098a612018-11-29 17:12:15 -08001889 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001890 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001891 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001892 return module
1893}
Jiyong Park30ca9372019-02-07 16:27:23 +09001894
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001895func ApexBundleFactory(testApex bool, artApex bool) android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001896 bundle := newApexBundle()
1897 bundle.testApex = testApex
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001898 bundle.artApex = artApex
Jooyung Han344d5432019-08-23 11:17:39 +09001899 return bundle
1900}
1901
1902func testApexBundleFactory() android.Module {
1903 bundle := newApexBundle()
1904 bundle.testApex = true
1905 return bundle
1906}
1907
Jiyong Parkd1063c12019-07-17 20:08:41 +09001908func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001909 return newApexBundle()
1910}
1911
1912// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1913// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1914// If not specified, then the "current" versions are gathered.
1915func vndkApexBundleFactory() android.Module {
1916 bundle := newApexBundle()
1917 bundle.vndkApex = true
1918 bundle.AddProperties(&bundle.vndkProperties)
1919 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1920 ctx.AppendProperties(&struct {
1921 Compile_multilib *string
1922 }{
1923 proptools.StringPtr("both"),
1924 })
1925 })
1926 return bundle
1927}
1928
Jooyung Han31c470b2019-10-18 16:26:59 +09001929func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1930 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1931 if vndkVersion == "current" {
1932 vndkVersion = config.PlatformVndkVersion()
1933 }
1934 return vndkVersion
1935}
1936
Jiyong Park30ca9372019-02-07 16:27:23 +09001937//
1938// Defaults
1939//
1940type Defaults struct {
1941 android.ModuleBase
1942 android.DefaultsModuleBase
1943}
1944
Jiyong Park30ca9372019-02-07 16:27:23 +09001945func defaultsFactory() android.Module {
1946 return DefaultsFactory()
1947}
1948
1949func DefaultsFactory(props ...interface{}) android.Module {
1950 module := &Defaults{}
1951
1952 module.AddProperties(props...)
1953 module.AddProperties(
1954 &apexBundleProperties{},
1955 &apexTargetBundleProperties{},
1956 )
1957
1958 android.InitDefaultsModule(module)
1959 return module
1960}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001961
1962//
1963// Prebuilt APEX
1964//
1965type Prebuilt struct {
1966 android.ModuleBase
1967 prebuilt android.Prebuilt
1968
1969 properties PrebuiltProperties
1970
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001971 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001972 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001973 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001974 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001975}
1976
1977type PrebuiltProperties struct {
1978 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001979 Source string `blueprint:"mutated"`
1980 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001981
1982 Src *string
1983 Arch struct {
1984 Arm struct {
1985 Src *string
1986 }
1987 Arm64 struct {
1988 Src *string
1989 }
1990 X86 struct {
1991 Src *string
1992 }
1993 X86_64 struct {
1994 Src *string
1995 }
1996 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001997
1998 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001999 // Optional name for the installed apex. If unspecified, name of the
2000 // module is used as the file name
2001 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002002
2003 // Names of modules to be overridden. Listed modules can only be other binaries
2004 // (in Make or Soong).
2005 // This does not completely prevent installation of the overridden binaries, but if both
2006 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
2007 // from PRODUCT_PACKAGES.
2008 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002009}
2010
2011func (p *Prebuilt) installable() bool {
2012 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002013}
2014
2015func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09002016 // If the device is configured to use flattened APEX, force disable the prebuilt because
2017 // the prebuilt is a non-flattened one.
2018 forceDisable := ctx.Config().FlattenApex()
2019
2020 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
2021 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09002022 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09002023
Kun Niu10c9f832019-07-29 16:28:57 -07002024 // Force disable the prebuilts when coverage is enabled.
2025 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
2026 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
2027
Jiyong Park50b81e52019-07-11 11:24:41 +09002028 // b/137216042 don't use prebuilts when address sanitizer is on
2029 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
2030 android.InList("hwaddress", ctx.Config().SanitizeDevice())
2031
2032 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09002033 p.properties.ForceDisable = true
2034 return
2035 }
2036
Jiyong Parkc95714e2019-03-29 14:23:10 +09002037 // This is called before prebuilt_select and prebuilt_postdeps mutators
2038 // The mutators requires that src to be set correctly for each arch so that
2039 // arch variants are disabled when src is not provided for the arch.
2040 if len(ctx.MultiTargets()) != 1 {
2041 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
2042 return
2043 }
2044 var src string
2045 switch ctx.MultiTargets()[0].Arch.ArchType {
2046 case android.Arm:
2047 src = String(p.properties.Arch.Arm.Src)
2048 case android.Arm64:
2049 src = String(p.properties.Arch.Arm64.Src)
2050 case android.X86:
2051 src = String(p.properties.Arch.X86.Src)
2052 case android.X86_64:
2053 src = String(p.properties.Arch.X86_64.Src)
2054 default:
2055 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
2056 return
2057 }
2058 if src == "" {
2059 src = String(p.properties.Src)
2060 }
2061 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002062}
2063
Jiyong Park03b68dd2019-07-26 23:20:40 +09002064func (p *Prebuilt) isForceDisabled() bool {
2065 return p.properties.ForceDisable
2066}
2067
Colin Cross41955e82019-05-29 14:40:35 -07002068func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2069 switch tag {
2070 case "":
2071 return android.Paths{p.outputApex}, nil
2072 default:
2073 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2074 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002075}
2076
Jiyong Park4d277042019-04-23 18:00:10 +09002077func (p *Prebuilt) InstallFilename() string {
2078 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2079}
2080
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002081func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002082 if p.properties.ForceDisable {
2083 return
2084 }
2085
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002086 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002087 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002088 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002089 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002090 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2091 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2092 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002093 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2094 ctx.Build(pctx, android.BuildParams{
2095 Rule: android.Cp,
2096 Input: p.inputApex,
2097 Output: p.outputApex,
2098 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002099 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002100 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002101 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002102
2103 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002104}
2105
2106func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2107 return &p.prebuilt
2108}
2109
2110func (p *Prebuilt) Name() string {
2111 return p.prebuilt.Name(p.ModuleBase.Name())
2112}
2113
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002114func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2115 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002116 Class: "ETC",
2117 OutputFile: android.OptionalPathForPath(p.inputApex),
2118 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002119 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2120 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002121 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002122 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2123 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2124 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2125 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002126 },
2127 }
2128}
2129
2130// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2131func PrebuiltFactory() android.Module {
2132 module := &Prebuilt{}
2133 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002134 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002135 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002136 return module
2137}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002138
2139func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2140 // small helper to add symlink commands
2141 addSymlink := func(target, dir, linkName string) {
2142 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2143 link := filepath.Join(outDir, linkName)
2144 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2145 }
2146
2147 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2148 // When all hard-coded references are fixed, remove symbolic links
2149 // Note that we should keep following symlinks for older VNDKs (<=29)
2150 // Since prebuilt vndk libs still depend on system/lib/vndk path
2151 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2152 // the name of vndk apex is formatted "com.android.vndk.v" + version
2153 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2154 if ctx.Config().Android64() {
2155 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2156 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2157 }
2158 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2159 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2160 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2161 }
2162 }
2163 return
2164}