blob: e3f3a783a1424d2fbe253c10cbee72dc72cb789c [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) {
Jiyong Parka308ea12019-11-15 10:38:39 +0900222 ctx.BottomUp("apex_deps", apexDepsMutator)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900223 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.
Jiyong Parka308ea12019-11-15 10:38:39 +0900275func apexDepsMutator(mctx android.BottomUpMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800276 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800277 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900278 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900279 depName := mctx.OtherModuleName(child)
280 // If the parent is apexBundle, this child is directly depended.
281 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800282 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800283 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
284 // non-installable apex's cannot be installed and so should not prevent libraries from being
285 // installed to the system.
286 android.UpdateApexDependency(apexBundleName, depName, directDep)
287 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900288
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900289 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900290 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900291 return true
292 } else {
293 return false
294 }
295 })
296 }
297}
298
299// Create apex variations if a module is included in APEX(s).
300func apexMutator(mctx android.BottomUpMutatorContext) {
301 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900302 am.CreateApexVariations(mctx)
Jooyung Han7a78a922019-10-08 21:59:58 +0900303 } else if a, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900304 // apex bundle itself is mutated so that it and its modules have same
305 // apex variant.
306 apexBundleName := mctx.ModuleName()
307 mctx.CreateVariations(apexBundleName)
Jooyung Han7a78a922019-10-08 21:59:58 +0900308
309 // collects APEX list
310 if mctx.Device() && a.installable() {
311 addApexFileContextsInfos(mctx, a)
312 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900313 }
314}
Sundong Ahne9b55722019-09-06 17:37:42 +0900315
Jooyung Han7a78a922019-10-08 21:59:58 +0900316var (
317 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
318 apexFileContextsInfosMutex sync.Mutex
319)
320
321func apexFileContextsInfos(config android.Config) *[]string {
322 return config.Once(apexFileContextsInfosKey, func() interface{} {
323 return &[]string{}
324 }).(*[]string)
325}
326
327func addApexFileContextsInfos(ctx android.BaseModuleContext, a *apexBundle) {
328 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
329 fileContextsName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
330
331 apexFileContextsInfosMutex.Lock()
332 defer apexFileContextsInfosMutex.Unlock()
333 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
334 *apexFileContextsInfos = append(*apexFileContextsInfos, apexName+":"+fileContextsName)
335}
336
Sundong Ahne9b55722019-09-06 17:37:42 +0900337func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900338 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +0900339 var variants []string
340 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
341 case "image":
342 variants = append(variants, imageApexType, flattenedApexType)
343 case "zip":
344 variants = append(variants, zipApexType)
345 case "both":
346 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
347 default:
348 mctx.PropertyErrorf("type", "%q is not one of \"image\" or \"zip\".", *ab.properties.Payload_type)
349 return
350 }
351
352 modules := mctx.CreateLocalVariations(variants...)
353
354 for i, v := range variants {
355 switch v {
356 case imageApexType:
357 modules[i].(*apexBundle).properties.ApexType = imageApex
358 case zipApexType:
359 modules[i].(*apexBundle).properties.ApexType = zipApex
360 case flattenedApexType:
361 modules[i].(*apexBundle).properties.ApexType = flattenedApex
362 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900363 }
364 }
365}
366
Jooyung Han5c998b92019-06-27 11:30:33 +0900367func apexUsesMutator(mctx android.BottomUpMutatorContext) {
368 if ab, ok := mctx.Module().(*apexBundle); ok {
369 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
370 }
371}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900372
Jooyung Handc782442019-11-01 03:14:38 +0900373var (
374 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
375)
376
377// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
378// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
379// which may cause compatibility issues. (e.g. libbinder)
380// Even though libbinder restricts its availability via 'apex_available' property and relies on
381// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
382// to avoid similar problems.
383func useVendorWhitelist(config android.Config) []string {
384 return config.Once(useVendorWhitelistKey, func() interface{} {
385 return []string{
386 // swcodec uses "vendor" variants for smaller size
387 "com.android.media.swcodec",
388 "test_com.android.media.swcodec",
389 }
390 }).([]string)
391}
392
393// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
394// called before the first call to useVendorWhitelist()
395func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
396 config.Once(useVendorWhitelistKey, func() interface{} {
397 return whitelist
398 })
399}
400
Alex Light9670d332019-01-29 18:07:33 -0800401type apexNativeDependencies struct {
402 // List of native libraries
403 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900404
Alex Light9670d332019-01-29 18:07:33 -0800405 // List of native executables
406 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900407
Roland Levillain630846d2019-06-26 12:48:34 +0100408 // List of native tests
409 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800410}
Jooyung Han344d5432019-08-23 11:17:39 +0900411
Alex Light9670d332019-01-29 18:07:33 -0800412type apexMultilibProperties struct {
413 // Native dependencies whose compile_multilib is "first"
414 First apexNativeDependencies
415
416 // Native dependencies whose compile_multilib is "both"
417 Both apexNativeDependencies
418
419 // Native dependencies whose compile_multilib is "prefer32"
420 Prefer32 apexNativeDependencies
421
422 // Native dependencies whose compile_multilib is "32"
423 Lib32 apexNativeDependencies
424
425 // Native dependencies whose compile_multilib is "64"
426 Lib64 apexNativeDependencies
427}
428
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900429type apexBundleProperties struct {
430 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000431 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800432 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900433
Jiyong Park40e26a22019-02-08 02:53:06 +0900434 // AndroidManifest.xml file used for the zip container of this APEX bundle.
435 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800436 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900437
Roland Levillain411c5842019-09-19 16:37:20 +0100438 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
439 // device (/apex/<apex_name>).
440 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900441 Apex_name *string
442
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900443 // Determines the file contexts file for setting security context to each file in this APEX bundle.
444 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
445 // used.
446 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900447 File_contexts *string
448
449 // List of native shared libs that are embedded inside this APEX bundle
450 Native_shared_libs []string
451
Roland Levillain630846d2019-06-26 12:48:34 +0100452 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900453 Binaries []string
454
455 // List of java libraries that are embedded inside this APEX bundle
456 Java_libs []string
457
458 // List of prebuilt files that are embedded inside this APEX bundle
459 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900460
Roland Levillain630846d2019-06-26 12:48:34 +0100461 // List of tests that are embedded inside this APEX bundle
462 Tests []string
463
Jiyong Parkff1458f2018-10-12 21:49:38 +0900464 // Name of the apex_key module that provides the private key to sign APEX
465 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900466
Alex Light5098a612018-11-29 17:12:15 -0800467 // The type of APEX to build. Controls what the APEX payload is. Either
468 // 'image', 'zip' or 'both'. Default: 'image'.
469 Payload_type *string
470
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900471 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
472 // or an android_app_certificate module name in the form ":module".
473 Certificate *string
474
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900475 // Whether this APEX is installable to one of the partitions. Default: true.
476 Installable *bool
477
Jiyong Parkda6eb592018-12-19 17:12:36 +0900478 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
479 // Default is false.
480 Use_vendor *bool
481
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800482 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
483 Ignore_system_library_special_case *bool
484
Alex Light9670d332019-01-29 18:07:33 -0800485 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900486
Jiyong Parkf97782b2019-02-13 20:28:58 +0900487 // List of sanitizer names that this APEX is enabled for
488 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900489
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900490 PreventInstall bool `blueprint:"mutated"`
491
492 HideFromMake bool `blueprint:"mutated"`
493
Jooyung Han5c998b92019-06-27 11:30:33 +0900494 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
495 Provide_cpp_shared_libs *bool
496
497 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
498 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100499
500 // A txt file containing list of files that are whitelisted to be included in this APEX.
501 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900502
503 // List of APKs to package inside APEX
504 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900505
Sundong Ahnabb64432019-10-22 13:58:29 +0900506 // package format of this apex variant; could be non-flattened, flattened, or zip.
507 // imageApex, zipApex or flattened
508 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900509
Jiyong Parkd1063c12019-07-17 20:08:41 +0900510 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
511 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
512 // is implied. This value affects all modules included in this APEX. In other words, they are
513 // also built with the SDKs specified here.
514 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800515}
516
517type apexTargetBundleProperties struct {
518 Target struct {
519 // Multilib properties only for android.
520 Android struct {
521 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900522 }
Jooyung Han344d5432019-08-23 11:17:39 +0900523
Alex Light9670d332019-01-29 18:07:33 -0800524 // Multilib properties only for host.
525 Host struct {
526 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900527 }
Jooyung Han344d5432019-08-23 11:17:39 +0900528
Alex Light9670d332019-01-29 18:07:33 -0800529 // Multilib properties only for host linux_bionic.
530 Linux_bionic struct {
531 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900532 }
Jooyung Han344d5432019-08-23 11:17:39 +0900533
Alex Light9670d332019-01-29 18:07:33 -0800534 // Multilib properties only for host linux_glibc.
535 Linux_glibc struct {
536 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900537 }
538 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900539}
540
Jooyung Han344d5432019-08-23 11:17:39 +0900541type apexVndkProperties struct {
542 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
543 Vndk_version *string
544}
545
Jiyong Park8fd61922018-11-08 02:50:25 +0900546type apexFileClass int
547
548const (
549 etc apexFileClass = iota
550 nativeSharedLib
551 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900552 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800553 pyBinary
554 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900555 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100556 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900557 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900558)
559
Alex Light5098a612018-11-29 17:12:15 -0800560type apexPackaging int
561
562const (
563 imageApex apexPackaging = iota
564 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900565 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800566)
567
Sundong Ahnabb64432019-10-22 13:58:29 +0900568// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800569func (a apexPackaging) suffix() string {
570 switch a {
571 case imageApex:
572 return imageApexSuffix
573 case zipApex:
574 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800575 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100576 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800577 }
578}
579
580func (a apexPackaging) name() string {
581 switch a {
582 case imageApex:
583 return imageApexType
584 case zipApex:
585 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800586 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100587 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800588 }
589}
590
Jiyong Park8fd61922018-11-08 02:50:25 +0900591func (class apexFileClass) NameInMake() string {
592 switch class {
593 case etc:
594 return "ETC"
595 case nativeSharedLib:
596 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800597 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900598 return "EXECUTABLES"
599 case javaSharedLib:
600 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100601 case nativeTest:
602 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900603 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900604 // b/142537672 Why isn't this APP? We want to have full control over
605 // the paths and file names of the apk file under the flattend APEX.
606 // If this is set to APP, then the paths and file names are modified
607 // by the Make build system. For example, it is installed to
608 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
609 // /system/apex/<apexname>/app/<Appname> because the build system automatically
610 // appends module name (which is <apexname>.<Appname> to the path.
611 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900612 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100613 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900614 }
615}
616
617type apexFile struct {
618 builtFile android.Path
619 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900620 installDir string
621 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900622 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800623 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900624}
625
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626type apexBundle struct {
627 android.ModuleBase
628 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900629 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900630
Alex Light9670d332019-01-29 18:07:33 -0800631 properties apexBundleProperties
632 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900633 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900634
Colin Crossa4925902018-11-16 11:36:28 -0800635 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900636 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700637 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900638
Jiyong Park03b68dd2019-07-26 23:20:40 +0900639 prebuiltFileToDelete string
640
Jiyong Park42cca6c2019-04-01 11:15:50 +0900641 public_key_file android.Path
642 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900643
644 container_certificate_file android.Path
645 container_private_key_file android.Path
646
Jiyong Park8fd61922018-11-08 02:50:25 +0900647 // list of files to be included in this apex
648 filesInfo []apexFile
649
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900650 // list of module names that this APEX is depending on
651 externalDeps []string
652
Sundong Ahnabb64432019-10-22 13:58:29 +0900653 testApex bool
654 vndkApex bool
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000655 artApex bool
Sundong Ahnabb64432019-10-22 13:58:29 +0900656 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900657
658 // intermediate path for apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +0900659 manifestJsonOut android.WritablePath
660 manifestJsonFullOut android.WritablePath
661 manifestPbOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900662
663 // list of commands to create symlinks for backward compatibility
664 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
665 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
666 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
667 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900668
669 // Suffix of module name in Android.mk
670 // ".flattened", ".apex", ".zipapex", or ""
671 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900672}
673
Jiyong Park397e55e2018-10-24 21:09:55 +0900674func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100675 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700676 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900677 // Use *FarVariation* to be able to depend on modules having
678 // conflicting variations with this module. This is required since
679 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
680 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700681 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900682 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900683 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900684 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700685 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900686
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700687 ctx.AddFarVariationDependencies(append(target.Variations(),
688 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
689 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100690
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700691 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100692 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100693 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700694 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900695}
696
Alex Light9670d332019-01-29 18:07:33 -0800697func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
698 if ctx.Os().Class == android.Device {
699 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
700 } else {
701 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
702 if ctx.Os().Bionic() {
703 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
704 } else {
705 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
706 }
707 }
708}
709
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900710func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900711 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
712 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
713 }
714
Jiyong Park397e55e2018-10-24 21:09:55 +0900715 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900716 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800717
718 a.combineProperties(ctx)
719
Jiyong Park397e55e2018-10-24 21:09:55 +0900720 has32BitTarget := false
721 for _, target := range targets {
722 if target.Arch.ArchType.Multilib == "lib32" {
723 has32BitTarget = true
724 }
725 }
726 for i, target := range targets {
727 // When multilib.* is omitted for native_shared_libs, it implies
728 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700729 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900730 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900731 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700732 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900733
Roland Levillain630846d2019-06-26 12:48:34 +0100734 // When multilib.* is omitted for tests, it implies
735 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700736 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100737 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100738 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700739 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100740
Jiyong Park397e55e2018-10-24 21:09:55 +0900741 // Add native modules targetting both ABIs
742 addDependenciesForNativeModules(ctx,
743 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100744 a.properties.Multilib.Both.Binaries,
745 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700746 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900747 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900748
Alex Light3d673592019-01-18 14:37:31 -0800749 isPrimaryAbi := i == 0
750 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900751 // When multilib.* is omitted for binaries, it implies
752 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700753 ctx.AddFarVariationDependencies(append(target.Variations(),
754 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
755 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900756
757 // Add native modules targetting the first ABI
758 addDependenciesForNativeModules(ctx,
759 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100760 a.properties.Multilib.First.Binaries,
761 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700762 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900763 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800764
765 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700766 ctx.AddFarVariationDependencies(target.Variations(),
767 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900768 }
769
770 switch target.Arch.ArchType.Multilib {
771 case "lib32":
772 // Add native modules targetting 32-bit ABI
773 addDependenciesForNativeModules(ctx,
774 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100775 a.properties.Multilib.Lib32.Binaries,
776 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700777 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900778 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900779
780 addDependenciesForNativeModules(ctx,
781 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100782 a.properties.Multilib.Prefer32.Binaries,
783 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700784 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900785 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900786 case "lib64":
787 // Add native modules targetting 64-bit ABI
788 addDependenciesForNativeModules(ctx,
789 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100790 a.properties.Multilib.Lib64.Binaries,
791 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700792 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900793 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900794
795 if !has32BitTarget {
796 addDependenciesForNativeModules(ctx,
797 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100798 a.properties.Multilib.Prefer32.Binaries,
799 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700800 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900801 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900802 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700803
804 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
805 for _, sanitizer := range ctx.Config().SanitizeDevice() {
806 if sanitizer == "hwaddress" {
807 addDependenciesForNativeModules(ctx,
808 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700809 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700810 break
811 }
812 }
813 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900814 }
815
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900816 }
817
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700818 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
819 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900820
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700821 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
822 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900823
Jiyong Park23c52b02019-02-02 13:13:47 +0900824 if String(a.properties.Key) == "" {
825 ctx.ModuleErrorf("key is missing")
826 return
827 }
828 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900829
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900830 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900831 if cert != "" {
832 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900833 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900834
835 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
836 if len(a.properties.Uses_sdks) > 0 {
837 sdkRefs := []android.SdkRef{}
838 for _, str := range a.properties.Uses_sdks {
839 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
840 sdkRefs = append(sdkRefs, parsed)
841 }
842 a.BuildWithSdks(sdkRefs)
843 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900844}
845
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900846func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
847 // direct deps of an APEX bundle are all part of the APEX bundle
848 return true
849}
850
Colin Cross0ea8ba82019-06-06 14:33:29 -0700851func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900852 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
853 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000854 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900855 }
856 return String(a.properties.Certificate)
857}
858
Colin Cross41955e82019-05-29 14:40:35 -0700859func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
860 switch tag {
861 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900862 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700863 default:
864 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900865 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900866}
867
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900868func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900869 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900870}
871
Jiyong Park7c1dc612019-01-05 11:15:24 +0900872func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900873 if a.vndkApex {
874 return "vendor." + a.vndkVersion(config)
875 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900876 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900877 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900878 } else {
879 return "core"
880 }
881}
882
Jiyong Parkf97782b2019-02-13 20:28:58 +0900883func (a *apexBundle) EnableSanitizer(sanitizerName string) {
884 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
885 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
886 }
887}
888
Jiyong Park388ef3f2019-01-28 19:47:32 +0900889func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900890 if android.InList(sanitizerName, a.properties.SanitizerNames) {
891 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900892 }
893
894 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900895 globalSanitizerNames := []string{}
896 if a.Host() {
897 globalSanitizerNames = ctx.Config().SanitizeHost()
898 } else {
899 arches := ctx.Config().SanitizeDeviceArch()
900 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
901 globalSanitizerNames = ctx.Config().SanitizeDevice()
902 }
903 }
904 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900905}
906
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900907func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
908 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
909}
910
911func (a *apexBundle) PreventInstall() {
912 a.properties.PreventInstall = true
913}
914
915func (a *apexBundle) HideFromMake() {
916 a.properties.HideFromMake = true
917}
918
Martin Stjernholm279de572019-09-10 23:18:20 +0100919func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900920 // Decide the APEX-local directory by the multilib of the library
921 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100922 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900923 case "lib32":
924 dirInApex = "lib"
925 case "lib64":
926 dirInApex = "lib64"
927 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100928 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700929 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100930 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900931 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100932 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
933 // Special case for Bionic libs and other libs installed with them. This is
934 // to prevent those libs from being included in the search path
935 // /apex/com.android.runtime/${LIB}. This exclusion is required because
936 // those libs in the Runtime APEX are available via the legacy paths in
937 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
938 // to the legacy paths and thus will be loaded into the default linker
939 // namespace (aka "platform" namespace). If the libs are directly in
940 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
941 // into the runtime linker namespace, which will result in double loading of
942 // them, which isn't supported.
943 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900944 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900945
Martin Stjernholm279de572019-09-10 23:18:20 +0100946 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900947 return
948}
949
950func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900951 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700952 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200953 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900954 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900955 fileToCopy = cc.OutputFile().Path()
956 return
957}
958
Alex Light778127a2019-02-27 14:19:50 -0800959func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
960 dirInApex = "bin"
961 fileToCopy = py.HostToolPath().Path()
962 return
963}
964func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
965 dirInApex = "bin"
966 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
967 if err != nil {
968 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
969 return
970 }
971 fileToCopy = android.PathForOutput(ctx, s)
972 return
973}
974
Jiyong Park04480cf2019-02-06 00:16:29 +0900975func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
976 dirInApex = filepath.Join("bin", sh.SubDir())
977 fileToCopy = sh.OutputFile()
978 return
979}
980
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900981func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
982 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900983 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900984 return
985}
986
Jiyong Park9e6c2422019-08-09 20:39:45 +0900987func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
988 dirInApex = "javalib"
989 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
990 implJars := java.ImplementationJars()
991 if len(implJars) != 1 {
992 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
993 strings.Join(implJars.Strings(), ", ")))
994 }
995 fileToCopy = implJars[0]
996 return
997}
998
Jooyung Han39edb6c2019-11-06 16:53:07 +0900999func getCopyManifestForPrebuiltEtc(prebuilt android.PrebuiltEtcModule) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001000 dirInApex = filepath.Join("etc", prebuilt.SubDir())
1001 fileToCopy = prebuilt.OutputFile()
1002 return
1003}
1004
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001005func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +09001006 appDir := "app"
1007 if app.Privileged() {
1008 appDir = "priv-app"
1009 }
1010 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001011 fileToCopy = app.OutputFile()
1012 return
1013}
1014
Dario Frenicde2a032019-10-27 00:29:22 +01001015func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
1016 appDir := "app"
1017 if app.Privileged() {
1018 appDir = "priv-app"
1019 }
1020 dirInApex = filepath.Join(appDir, pkgName)
1021 fileToCopy = app.OutputFile()
1022 return
1023}
1024
Roland Levillain935639d2019-08-13 14:55:28 +01001025// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1026type flattenedApexContext struct {
1027 android.ModuleContext
1028}
1029
1030func (c *flattenedApexContext) InstallBypassMake() bool {
1031 return true
1032}
1033
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001034func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001035 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001036
Sundong Ahnabb64432019-10-22 13:58:29 +09001037 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1038 switch a.properties.ApexType {
1039 case imageApex:
1040 if buildFlattenedAsDefault {
1041 a.suffix = imageApexSuffix
1042 } else {
1043 a.suffix = ""
1044 a.primaryApexType = true
1045 }
1046 case zipApex:
1047 if proptools.String(a.properties.Payload_type) == "zip" {
1048 a.suffix = ""
1049 a.primaryApexType = true
1050 } else {
1051 a.suffix = zipApexSuffix
1052 }
1053 case flattenedApex:
1054 if buildFlattenedAsDefault {
1055 a.suffix = ""
1056 a.primaryApexType = true
1057 } else {
1058 a.suffix = flattenedSuffix
1059 }
Alex Light5098a612018-11-29 17:12:15 -08001060 }
1061
Roland Levillain630846d2019-06-26 12:48:34 +01001062 if len(a.properties.Tests) > 0 && !a.testApex {
1063 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1064 return
1065 }
1066
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001067 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1068
Jooyung Hane1633032019-08-01 17:41:43 +09001069 // native lib dependencies
1070 var provideNativeLibs []string
1071 var requireNativeLibs []string
1072
Jooyung Han5c998b92019-06-27 11:30:33 +09001073 // Check if "uses" requirements are met with dependent apexBundles
1074 var providedNativeSharedLibs []string
1075 useVendor := proptools.Bool(a.properties.Use_vendor)
1076 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1077 if ctx.OtherModuleDependencyTag(m) != usesTag {
1078 return
1079 }
1080 otherName := ctx.OtherModuleName(m)
1081 other, ok := m.(*apexBundle)
1082 if !ok {
1083 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1084 return
1085 }
1086 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1087 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1088 return
1089 }
1090 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1091 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1092 return
1093 }
1094 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1095 })
1096
Alex Light778127a2019-02-27 14:19:50 -08001097 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001098 depTag := ctx.OtherModuleDependencyTag(child)
1099 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001100 if _, ok := parent.(*apexBundle); ok {
1101 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001102 switch depTag {
1103 case sharedLibTag:
1104 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001105 if cc.HasStubsVariants() {
1106 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1107 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001108 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001109 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001110 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001111 } else {
1112 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001113 }
1114 case executableTag:
1115 if cc, ok := child.(*cc.Module); ok {
1116 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001117 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001118 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001119 } else if sh, ok := child.(*android.ShBinary); ok {
1120 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001121 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001122 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1123 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1124 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1125 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1126 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1127 // NB: Since go binaries are static we don't need the module for anything here, which is
1128 // good since the go tool is a blueprint.Module not an android.Module like we would
1129 // normally use.
1130 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001131 } else {
Alex Light778127a2019-02-27 14:19:50 -08001132 ctx.PropertyErrorf("binaries", "%q is neither cc_binary, (embedded) py_binary, (host) blueprint_go_binary, (host) bootstrap_go_binary, nor sh_binary", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001133 }
1134 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001135 if javaLib, ok := child.(*java.Library); ok {
1136 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001137 if fileToCopy == nil {
1138 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1139 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001140 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1141 }
1142 return true
1143 } else if javaLib, ok := child.(*java.Import); ok {
1144 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1145 if fileToCopy == nil {
1146 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1147 } else {
1148 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001149 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001150 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001151 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001152 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001153 }
1154 case prebuiltTag:
Jooyung Han39edb6c2019-11-06 16:53:07 +09001155 if prebuilt, ok := child.(android.PrebuiltEtcModule); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001156 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001157 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001158 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001159 } else {
1160 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1161 }
Roland Levillain630846d2019-06-26 12:48:34 +01001162 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001163 if ccTest, ok := child.(*cc.Module); ok {
1164 if ccTest.IsTestPerSrcAllTestsVariation() {
1165 // Multiple-output test module (where `test_per_src: true`).
1166 //
1167 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1168 // We do not add this variation to `filesInfo`, as it has no output;
1169 // however, we do add the other variations of this module as indirect
1170 // dependencies (see below).
1171 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001172 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001173 // Single-output test module (where `test_per_src: false`).
1174 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1175 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001176 }
Roland Levillain630846d2019-06-26 12:48:34 +01001177 return true
1178 } else {
1179 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1180 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001181 case keyTag:
1182 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001183 a.private_key_file = key.private_key_file
1184 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001185 return false
1186 } else {
1187 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001188 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001189 case certificateTag:
1190 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001191 a.container_certificate_file = dep.Certificate.Pem
1192 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001193 return false
1194 } else {
1195 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1196 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001197 case android.PrebuiltDepTag:
1198 // If the prebuilt is force disabled, remember to delete the prebuilt file
1199 // that might have been installed in the previous builds
1200 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1201 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1202 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001203 case androidAppTag:
1204 if ap, ok := child.(*java.AndroidApp); ok {
1205 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1206 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1207 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001208 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1209 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1210 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001211 } else {
1212 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1213 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001214 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001215 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001216 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001217 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001218 // We cannot use a switch statement on `depTag` here as the checked
1219 // tags used below are private (e.g. `cc.sharedDepTag`).
Sam Sacconee3c22f72019-11-13 15:23:05 +00001220 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) || java.IsJniDepTag(depTag) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001221 if cc, ok := child.(*cc.Module); ok {
1222 if android.InList(cc.Name(), providedNativeSharedLibs) {
1223 // If we're using a shared library which is provided from other APEX,
1224 // don't include it in this APEX
1225 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001226 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001227 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1228 // If the dependency is a stubs lib, don't include it in this APEX,
1229 // but make sure that the lib is installed on the device.
1230 // In case no APEX is having the lib, the lib is installed to the system
1231 // partition.
1232 //
1233 // Always include if we are a host-apex however since those won't have any
1234 // system libraries.
1235 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1236 a.externalDeps = append(a.externalDeps, cc.Name())
1237 }
Jooyung Hane1633032019-08-01 17:41:43 +09001238 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001239 // Don't track further
1240 return false
1241 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001242 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001243 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1244 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001245 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001246 } else if cc.IsTestPerSrcDepTag(depTag) {
1247 if cc, ok := child.(*cc.Module); ok {
1248 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1249 // Handle modules created as `test_per_src` variations of a single test module:
1250 // use the name of the generated test binary (`fileToCopy`) instead of the name
1251 // of the original test module (`depName`, shared by all `test_per_src`
1252 // variations of that module).
1253 moduleName := filepath.Base(fileToCopy.String())
1254 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1255 return true
1256 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001257 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001258 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001259 }
1260 }
1261 }
1262 return false
1263 })
1264
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001265 // Specific to the ART apex: dexpreopt artifacts for libcore Java libraries.
1266 // Build rules are generated by the dexpreopt singleton, and here we access build artifacts
1267 // via the global boot image config.
1268 if a.artApex {
1269 for arch, files := range java.DexpreoptedArtApexJars(ctx) {
1270 dirInApex := filepath.Join("javalib", arch.String())
1271 for _, f := range files {
1272 localModule := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
1273 filesInfo = append(filesInfo, apexFile{f, localModule, dirInApex, etc, nil, nil})
1274 }
1275 }
1276 }
1277
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001278 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001279 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1280 return
1281 }
1282
Jiyong Park8fd61922018-11-08 02:50:25 +09001283 // remove duplicates in filesInfo
1284 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001285 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001286 result := []apexFile{}
1287 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001288 dest := filepath.Join(f.installDir, f.builtFile.Base())
1289 if !encountered[dest] {
1290 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001291 result = append(result, f)
1292 }
1293 }
1294 return result
1295 }
1296 filesInfo = removeDup(filesInfo)
1297
1298 // to have consistent build rules
1299 sort.Slice(filesInfo, func(i, j int) bool {
1300 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1301 })
1302
Jiyong Park127b40b2019-09-30 16:04:35 +09001303 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001304 if !ctx.Host() {
1305 for _, fi := range filesInfo {
1306 if am, ok := fi.module.(android.ApexModule); ok {
1307 if !am.AvailableFor(ctx.ModuleName()) {
1308 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1309 return
1310 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001311 }
1312 }
1313 }
1314
Jiyong Park8fd61922018-11-08 02:50:25 +09001315 // prepend the name of this APEX to the module names. These names will be the names of
1316 // modules that will be defined if the APEX is flattened.
1317 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001318 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001319 }
1320
Jiyong Park8fd61922018-11-08 02:50:25 +09001321 a.installDir = android.PathForModuleInstall(ctx, "apex")
1322 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001323
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001324 // prepare apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +09001325 a.buildManifest(ctx, provideNativeLibs, requireNativeLibs)
1326
1327 a.setCertificateAndPrivateKey(ctx)
1328 if a.properties.ApexType == flattenedApex {
1329 a.buildFlattenedApex(ctx)
1330 } else {
1331 a.buildUnflattenedApex(ctx)
1332 }
1333
1334 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1335 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
1336}
1337
1338func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
Jooyung Hane1633032019-08-01 17:41:43 +09001339 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001340
Jooyung Han01a3ee22019-11-02 02:52:25 +09001341 a.manifestJsonFullOut = android.PathForModuleOut(ctx, "apex_manifest_full.json")
1342
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001343 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001344 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1345 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001346
1347 // apex name can be overridden
1348 optCommands := []string{}
1349 if a.properties.Apex_name != nil {
1350 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1351 }
1352
Jooyung Hane1633032019-08-01 17:41:43 +09001353 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001354 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001355 Input: manifestSrc,
Jooyung Han01a3ee22019-11-02 02:52:25 +09001356 Output: a.manifestJsonFullOut,
Jooyung Hane1633032019-08-01 17:41:43 +09001357 Args: map[string]string{
1358 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1359 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001360 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001361 },
1362 })
1363
Jooyung Han01a3ee22019-11-02 02:52:25 +09001364 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
1365 // prepare stripp-downed version so that APEX modules built from R+ can be installed to Q
1366 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1367 ctx.Build(pctx, android.BuildParams{
1368 Rule: stripApexManifestRule,
1369 Input: a.manifestJsonFullOut,
1370 Output: a.manifestJsonOut,
1371 })
Jooyung Han72bd2f82019-10-23 16:46:38 +09001372
Jooyung Han01a3ee22019-11-02 02:52:25 +09001373 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
1374 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
1375 ctx.Build(pctx, android.BuildParams{
1376 Rule: pbApexManifestRule,
1377 Input: a.manifestJsonFullOut,
1378 Output: a.manifestPbOut,
1379 })
Jiyong Park8fd61922018-11-08 02:50:25 +09001380}
1381
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001382func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001383 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001384 for _, f := range a.filesInfo {
1385 if f.module != nil {
1386 notice := f.module.NoticeFile()
1387 if notice.Valid() {
1388 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001389 }
1390 }
1391 }
1392 // append the notice file specified in the apex module itself
1393 if a.NoticeFile().Valid() {
1394 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001395 }
1396
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001397 if len(noticeFiles) == 0 {
1398 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001399 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001400
Jaewoong Jung98772792019-07-01 17:15:13 -07001401 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001402}
1403
Sundong Ahnabb64432019-10-22 13:58:29 +09001404func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001405 var abis []string
1406 for _, target := range ctx.MultiTargets() {
1407 if len(target.Arch.Abi) > 0 {
1408 abis = append(abis, target.Arch.Abi[0])
1409 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001410 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001411
Alex Light5098a612018-11-29 17:12:15 -08001412 abis = android.FirstUniqueStrings(abis)
1413
Sundong Ahnabb64432019-10-22 13:58:29 +09001414 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001415 suffix := apexType.suffix()
1416 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001417
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001418 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001419 for _, f := range a.filesInfo {
1420 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001421 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001422
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001423 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001424 emitCommands := []string{}
1425 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1426 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001427 for i, src := range filesToCopy {
1428 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001429 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001430 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001431 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1432 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001433 for _, sym := range a.filesInfo[i].symlinks {
1434 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1435 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1436 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001437 }
Dario Frenie4235822019-10-28 14:49:27 +00001438 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1439
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001440 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Han01a3ee22019-11-02 02:52:25 +09001441 implicitInputs = append(implicitInputs, a.manifestPbOut, a.manifestJsonFullOut, a.manifestJsonOut)
Alex Light5098a612018-11-29 17:12:15 -08001442
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001443 if a.properties.Whitelisted_files != nil {
1444 ctx.Build(pctx, android.BuildParams{
1445 Rule: emitApexContentRule,
1446 Implicits: implicitInputs,
1447 Output: imageContentFile,
1448 Description: "emit apex image content",
1449 Args: map[string]string{
1450 "emit_commands": strings.Join(emitCommands, " && "),
1451 },
1452 })
1453 implicitInputs = append(implicitInputs, imageContentFile)
1454 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1455
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001456 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001457 ctx.Build(pctx, android.BuildParams{
1458 Rule: diffApexContentRule,
1459 Implicits: implicitInputs,
1460 Output: phonyOutput,
1461 Description: "diff apex image content",
1462 Args: map[string]string{
1463 "whitelisted_files_file": whitelistedFilesFile.String(),
1464 "image_content_file": imageContentFile.String(),
1465 "apex_module_name": ctx.ModuleName(),
1466 },
1467 })
1468
1469 implicitInputs = append(implicitInputs, phonyOutput)
1470 }
1471
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001472 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1473 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001474
Sundong Ahnabb64432019-10-22 13:58:29 +09001475 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001476 // files and dirs that will be created in APEX
Jooyung Han01a3ee22019-11-02 02:52:25 +09001477 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
Alex Light5098a612018-11-29 17:12:15 -08001478 var executablePaths []string // this also includes dirs
1479 for _, f := range a.filesInfo {
1480 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001481 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001482 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001483 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001484 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001485 }
Alex Light5098a612018-11-29 17:12:15 -08001486 } else {
1487 readOnlyPaths = append(readOnlyPaths, pathInApex)
1488 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001489 dir := f.installDir
1490 for !android.InList(dir, executablePaths) && dir != "" {
1491 executablePaths = append(executablePaths, dir)
1492 dir, _ = filepath.Split(dir) // move up to the parent
1493 if len(dir) > 0 {
1494 // remove trailing slash
1495 dir = dir[:len(dir)-1]
1496 }
Alex Light5098a612018-11-29 17:12:15 -08001497 }
1498 }
1499 sort.Strings(readOnlyPaths)
1500 sort.Strings(executablePaths)
1501 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1502 ctx.Build(pctx, android.BuildParams{
1503 Rule: generateFsConfig,
1504 Output: cannedFsConfig,
1505 Description: "generate fs config",
1506 Args: map[string]string{
1507 "ro_paths": strings.Join(readOnlyPaths, " "),
1508 "exec_paths": strings.Join(executablePaths, " "),
1509 },
1510 })
1511
1512 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1513 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1514 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1515 if !fileContextsOptionalPath.Valid() {
1516 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1517 return
1518 }
1519 fileContexts := fileContextsOptionalPath.Path()
1520
Jiyong Park835d82b2018-12-27 16:04:18 +09001521 optFlags := []string{}
1522
Alex Light5098a612018-11-29 17:12:15 -08001523 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001524 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1525 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001526
Jiyong Park7f67f482019-01-05 12:57:48 +09001527 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1528 if overridden {
1529 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1530 }
1531
Jiyong Park40e26a22019-02-08 02:53:06 +09001532 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001533 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001534 implicitInputs = append(implicitInputs, androidManifestFile)
1535 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1536 }
1537
Jiyong Park71b519d2019-04-18 17:25:49 +09001538 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1539 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1540 ctx.Config().UnbundledBuild() &&
1541 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1542 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1543 apiFingerprint := java.ApiFingerprintPath(ctx)
1544 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1545 implicitInputs = append(implicitInputs, apiFingerprint)
1546 }
1547 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1548
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001549 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1550 if noticeFile.Valid() {
1551 // If there's a NOTICE file, embed it as an asset file in the APEX.
1552 implicitInputs = append(implicitInputs, noticeFile.Path())
1553 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1554 }
1555
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001556 if !ctx.Config().UnbundledBuild() && a.installable() {
1557 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1558 // don't need hashtree for activation. Therefore, by removing hashtree from
1559 // apex bundle (filesystem image in it, to be specific), we can save storage.
1560 optFlags = append(optFlags, "--no_hashtree")
1561 }
1562
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001563 if a.properties.Apex_name != nil {
1564 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1565 // Note that apex_manifest is also mended.
1566 optFlags = append(optFlags, "--do_not_check_keyname")
1567 }
1568
Alex Light5098a612018-11-29 17:12:15 -08001569 ctx.Build(pctx, android.BuildParams{
1570 Rule: apexRule,
1571 Implicits: implicitInputs,
1572 Output: unsignedOutputFile,
1573 Description: "apex (" + apexType.name() + ")",
1574 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001575 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1576 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1577 "copy_commands": strings.Join(copyCommands, " && "),
1578 "manifest_json_full": a.manifestJsonFullOut.String(),
1579 "manifest_json": a.manifestJsonOut.String(),
1580 "manifest": a.manifestPbOut.String(),
1581 "file_contexts": fileContexts.String(),
1582 "canned_fs_config": cannedFsConfig.String(),
1583 "key": a.private_key_file.String(),
1584 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001585 },
1586 })
1587
1588 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1589 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1590 a.bundleModuleFile = bundleModuleFile
1591
1592 ctx.Build(pctx, android.BuildParams{
1593 Rule: apexProtoConvertRule,
1594 Input: unsignedOutputFile,
1595 Output: apexProtoFile,
1596 Description: "apex proto convert",
1597 })
1598
1599 ctx.Build(pctx, android.BuildParams{
1600 Rule: apexBundleRule,
1601 Input: apexProtoFile,
1602 Output: a.bundleModuleFile,
1603 Description: "apex bundle module",
1604 Args: map[string]string{
1605 "abi": strings.Join(abis, "."),
1606 },
1607 })
1608 } else {
1609 ctx.Build(pctx, android.BuildParams{
1610 Rule: zipApexRule,
1611 Implicits: implicitInputs,
1612 Output: unsignedOutputFile,
1613 Description: "apex (" + apexType.name() + ")",
1614 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001615 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1616 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1617 "copy_commands": strings.Join(copyCommands, " && "),
1618 "manifest": a.manifestPbOut.String(),
1619 "manifest_json_full": a.manifestJsonFullOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001620 },
1621 })
Colin Crossa4925902018-11-16 11:36:28 -08001622 }
Colin Crossa4925902018-11-16 11:36:28 -08001623
Sundong Ahnabb64432019-10-22 13:58:29 +09001624 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001625 ctx.Build(pctx, android.BuildParams{
1626 Rule: java.Signapk,
1627 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001628 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001629 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001630 Implicits: []android.Path{
1631 a.container_certificate_file,
1632 a.container_private_key_file,
1633 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001634 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001635 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001636 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001637 },
1638 })
Alex Light5098a612018-11-29 17:12:15 -08001639
1640 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001641 if a.installable() {
1642 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001643 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001644 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001645}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001646
Jiyong Park8fd61922018-11-08 02:50:25 +09001647func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001648 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1649 // reply true to `InstallBypassMake()` (thus making the call
1650 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1651 // instead of `android.PathForOutput`) to return the correct path to the flattened
1652 // APEX (as its contents is installed by Make, not Soong).
1653 factx := flattenedApexContext{ctx}
1654 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1655 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1656
1657 a.buildFilesInfo(ctx)
1658}
1659
1660func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1661 cert := String(a.properties.Certificate)
1662 if cert != "" && android.SrcIsModule(cert) == "" {
1663 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1664 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1665 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1666 } else if cert == "" {
1667 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1668 a.container_certificate_file = pem
1669 a.container_private_key_file = key
1670 }
1671}
1672
1673func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001674 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001675 // 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 +09001676 // with other ordinary files.
Jooyung Han01a3ee22019-11-02 02:52:25 +09001677 a.filesInfo = append(a.filesInfo, apexFile{a.manifestJsonOut, "apex_manifest.json." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
1678 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 +09001679
Jiyong Park42cca6c2019-04-01 11:15:50 +09001680 // rename to apex_pubkey
1681 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1682 ctx.Build(pctx, android.BuildParams{
1683 Rule: android.Cp,
1684 Input: a.public_key_file,
1685 Output: copiedPubkey,
1686 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001687 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001688
Sundong Ahnabb64432019-10-22 13:58:29 +09001689 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001690 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001691 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001692 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001693 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1694 for _, sym := range fi.symlinks {
1695 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1696 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001697 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001698 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001699 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001700}
1701
1702func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001703 if a.properties.HideFromMake {
1704 return android.AndroidMkData{
1705 Disabled: true,
1706 }
1707 }
Alex Light5098a612018-11-29 17:12:15 -08001708 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001709 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001710 return android.AndroidMkData{
1711 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1712 for _, data := range writers {
1713 data.Custom(w, name, prefix, moduleDir, data)
1714 }
1715 }}
1716}
1717
Sundong Ahnabb64432019-10-22 13:58:29 +09001718func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001719 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001720 apexType := a.properties.ApexType
1721 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1722 // to install symbol files in $(PRODUCT_OUT}/apex.
1723 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1724 if !a.primaryApexType && apexType != flattenedApex {
1725 return moduleNames
1726 }
Jiyong Park94427262019-02-05 23:18:47 +09001727
1728 for _, fi := range a.filesInfo {
1729 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1730 continue
1731 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001732
1733 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001734 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001735 }
1736
Jiyong Park94427262019-02-05 23:18:47 +09001737 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1738 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001739 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001740 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001741 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001742 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001743 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001744 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001745 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001746 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001747 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1748 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001749 if len(fi.symlinks) > 0 {
1750 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1751 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001752
1753 if fi.module != nil && fi.module.NoticeFile().Valid() {
1754 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1755 }
Jiyong Park94427262019-02-05 23:18:47 +09001756 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001757 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001758 }
1759 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1760 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1761 if fi.module != nil {
1762 archStr := fi.module.Target().Arch.ArchType.String()
1763 host := false
1764 switch fi.module.Target().Os.Class {
1765 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001766 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001767 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1768 }
1769 host = true
1770 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001771 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001772 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1773 }
1774 host = true
1775 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001776 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001777 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1778 }
1779 }
1780 if host {
1781 makeOs := fi.module.Target().Os.String()
1782 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1783 makeOs = "linux"
1784 }
1785 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1786 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1787 }
1788 }
1789 if fi.class == javaSharedLib {
1790 javaModule := fi.module.(*java.Library)
1791 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1792 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1793 // we will have foo.jar.jar
1794 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1795 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1796 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1797 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1798 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1799 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001800 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001801 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001802 if cc, ok := fi.module.(*cc.Module); ok {
1803 if cc.UnstrippedOutputFile() != nil {
1804 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1805 }
1806 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001807 if cc.CoverageOutputFile().Valid() {
1808 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1809 }
Jiyong Park94427262019-02-05 23:18:47 +09001810 }
1811 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1812 } else {
1813 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001814 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Jooyung Han01a3ee22019-11-02 02:52:25 +09001815 if a.primaryApexType && fi.builtFile == a.manifestPbOut && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001816 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1817 }
Jiyong Park94427262019-02-05 23:18:47 +09001818 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1819 }
1820 }
1821 return moduleNames
1822}
1823
Sundong Ahnabb64432019-10-22 13:58:29 +09001824func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001825 return android.AndroidMkData{
1826 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1827 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001828 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001829 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001830 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001831 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001832 }
1833
Sundong Ahnabb64432019-10-22 13:58:29 +09001834 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001835 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001836 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1837 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001838 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001839 if len(moduleNames) > 0 {
1840 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1841 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001842 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001843 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001844
Sundong Ahnabb64432019-10-22 13:58:29 +09001845 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001846 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1847 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001848 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001849 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001850 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001851 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001852 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001853 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001854 if len(moduleNames) > 0 {
1855 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1856 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001857 if len(a.externalDeps) > 0 {
1858 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1859 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001860 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001861 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001862 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001863 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001864 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001865 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1866 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1867 if len(postInstallCommands) > 0 {
1868 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1869 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001870 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001871
Alex Light5098a612018-11-29 17:12:15 -08001872 if apexType == imageApex {
1873 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1874 }
Jiyong Park719b4462019-01-13 00:39:51 +09001875 }
1876 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001877}
1878
Jooyung Han344d5432019-08-23 11:17:39 +09001879func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001880 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001881 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001882 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001883 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001884 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1885 })
Alex Light5098a612018-11-29 17:12:15 -08001886 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001887 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001888 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001889 return module
1890}
Jiyong Park30ca9372019-02-07 16:27:23 +09001891
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001892func ApexBundleFactory(testApex bool, artApex bool) android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001893 bundle := newApexBundle()
1894 bundle.testApex = testApex
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001895 bundle.artApex = artApex
Jooyung Han344d5432019-08-23 11:17:39 +09001896 return bundle
1897}
1898
1899func testApexBundleFactory() android.Module {
1900 bundle := newApexBundle()
1901 bundle.testApex = true
1902 return bundle
1903}
1904
Jiyong Parkd1063c12019-07-17 20:08:41 +09001905func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001906 return newApexBundle()
1907}
1908
1909// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1910// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1911// If not specified, then the "current" versions are gathered.
1912func vndkApexBundleFactory() android.Module {
1913 bundle := newApexBundle()
1914 bundle.vndkApex = true
1915 bundle.AddProperties(&bundle.vndkProperties)
1916 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1917 ctx.AppendProperties(&struct {
1918 Compile_multilib *string
1919 }{
1920 proptools.StringPtr("both"),
1921 })
1922 })
1923 return bundle
1924}
1925
Jooyung Han31c470b2019-10-18 16:26:59 +09001926func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1927 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1928 if vndkVersion == "current" {
1929 vndkVersion = config.PlatformVndkVersion()
1930 }
1931 return vndkVersion
1932}
1933
Jiyong Park30ca9372019-02-07 16:27:23 +09001934//
1935// Defaults
1936//
1937type Defaults struct {
1938 android.ModuleBase
1939 android.DefaultsModuleBase
1940}
1941
Jiyong Park30ca9372019-02-07 16:27:23 +09001942func defaultsFactory() android.Module {
1943 return DefaultsFactory()
1944}
1945
1946func DefaultsFactory(props ...interface{}) android.Module {
1947 module := &Defaults{}
1948
1949 module.AddProperties(props...)
1950 module.AddProperties(
1951 &apexBundleProperties{},
1952 &apexTargetBundleProperties{},
1953 )
1954
1955 android.InitDefaultsModule(module)
1956 return module
1957}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001958
1959//
1960// Prebuilt APEX
1961//
1962type Prebuilt struct {
1963 android.ModuleBase
1964 prebuilt android.Prebuilt
1965
1966 properties PrebuiltProperties
1967
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001968 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001969 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001970 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001971 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001972}
1973
1974type PrebuiltProperties struct {
1975 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001976 Source string `blueprint:"mutated"`
1977 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001978
1979 Src *string
1980 Arch struct {
1981 Arm struct {
1982 Src *string
1983 }
1984 Arm64 struct {
1985 Src *string
1986 }
1987 X86 struct {
1988 Src *string
1989 }
1990 X86_64 struct {
1991 Src *string
1992 }
1993 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001994
1995 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001996 // Optional name for the installed apex. If unspecified, name of the
1997 // module is used as the file name
1998 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001999
2000 // Names of modules to be overridden. Listed modules can only be other binaries
2001 // (in Make or Soong).
2002 // This does not completely prevent installation of the overridden binaries, but if both
2003 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
2004 // from PRODUCT_PACKAGES.
2005 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002006}
2007
2008func (p *Prebuilt) installable() bool {
2009 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002010}
2011
2012func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09002013 // If the device is configured to use flattened APEX, force disable the prebuilt because
2014 // the prebuilt is a non-flattened one.
2015 forceDisable := ctx.Config().FlattenApex()
2016
2017 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
2018 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09002019 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09002020
Kun Niu10c9f832019-07-29 16:28:57 -07002021 // Force disable the prebuilts when coverage is enabled.
2022 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
2023 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
2024
Jiyong Park50b81e52019-07-11 11:24:41 +09002025 // b/137216042 don't use prebuilts when address sanitizer is on
2026 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
2027 android.InList("hwaddress", ctx.Config().SanitizeDevice())
2028
2029 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09002030 p.properties.ForceDisable = true
2031 return
2032 }
2033
Jiyong Parkc95714e2019-03-29 14:23:10 +09002034 // This is called before prebuilt_select and prebuilt_postdeps mutators
2035 // The mutators requires that src to be set correctly for each arch so that
2036 // arch variants are disabled when src is not provided for the arch.
2037 if len(ctx.MultiTargets()) != 1 {
2038 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
2039 return
2040 }
2041 var src string
2042 switch ctx.MultiTargets()[0].Arch.ArchType {
2043 case android.Arm:
2044 src = String(p.properties.Arch.Arm.Src)
2045 case android.Arm64:
2046 src = String(p.properties.Arch.Arm64.Src)
2047 case android.X86:
2048 src = String(p.properties.Arch.X86.Src)
2049 case android.X86_64:
2050 src = String(p.properties.Arch.X86_64.Src)
2051 default:
2052 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
2053 return
2054 }
2055 if src == "" {
2056 src = String(p.properties.Src)
2057 }
2058 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002059}
2060
Jiyong Park03b68dd2019-07-26 23:20:40 +09002061func (p *Prebuilt) isForceDisabled() bool {
2062 return p.properties.ForceDisable
2063}
2064
Colin Cross41955e82019-05-29 14:40:35 -07002065func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2066 switch tag {
2067 case "":
2068 return android.Paths{p.outputApex}, nil
2069 default:
2070 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2071 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002072}
2073
Jiyong Park4d277042019-04-23 18:00:10 +09002074func (p *Prebuilt) InstallFilename() string {
2075 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2076}
2077
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002078func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002079 if p.properties.ForceDisable {
2080 return
2081 }
2082
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002083 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002084 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002085 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002086 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002087 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2088 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2089 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002090 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2091 ctx.Build(pctx, android.BuildParams{
2092 Rule: android.Cp,
2093 Input: p.inputApex,
2094 Output: p.outputApex,
2095 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002096 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002097 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002098 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002099
2100 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002101}
2102
2103func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2104 return &p.prebuilt
2105}
2106
2107func (p *Prebuilt) Name() string {
2108 return p.prebuilt.Name(p.ModuleBase.Name())
2109}
2110
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002111func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2112 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002113 Class: "ETC",
2114 OutputFile: android.OptionalPathForPath(p.inputApex),
2115 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002116 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2117 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002118 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002119 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2120 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2121 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2122 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002123 },
2124 }
2125}
2126
2127// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2128func PrebuiltFactory() android.Module {
2129 module := &Prebuilt{}
2130 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002131 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002132 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002133 return module
2134}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002135
2136func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2137 // small helper to add symlink commands
2138 addSymlink := func(target, dir, linkName string) {
2139 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2140 link := filepath.Join(outDir, linkName)
2141 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2142 }
2143
2144 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2145 // When all hard-coded references are fixed, remove symbolic links
2146 // Note that we should keep following symlinks for older VNDKs (<=29)
2147 // Since prebuilt vndk libs still depend on system/lib/vndk path
2148 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2149 // the name of vndk apex is formatted "com.android.vndk.v" + version
2150 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2151 if ctx.Config().Android64() {
2152 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2153 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2154 }
2155 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2156 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2157 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2158 }
2159 }
2160 return
2161}