blob: 08ba293f5761518656786bf517fb4d709fa3829f [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
Jooyung Han344d5432019-08-23 11:17:39 +090024 "sync"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090025
26 "android/soong/android"
27 "android/soong/cc"
28 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080029 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030
31 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080032 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090033 "github.com/google/blueprint/proptools"
34)
35
36var (
37 pctx = android.NewPackageContext("android/apex")
38
39 // Create a canned fs config file where all files and directories are
40 // by default set to (uid/gid/mode) = (1000/1000/0644)
41 // TODO(b/113082813) make this configurable using config.fs syntax
42 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000043 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000044 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090045 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090046 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090047 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090048 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090049
Jooyung Hand15aa1f2019-09-27 00:38:03 +090050 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
Jooyung Hane1633032019-08-01 17:41:43 +090051 Command: `rm -f $out && ${jsonmodify} $in ` +
52 `-a provideNativeLibs ${provideNativeLibs} ` +
Jooyung Hand15aa1f2019-09-27 00:38:03 +090053 `-a requireNativeLibs ${requireNativeLibs} ` +
54 `${opt} ` +
55 `-o $out`,
Jooyung Hane1633032019-08-01 17:41:43 +090056 CommandDeps: []string{"${jsonmodify}"},
Jooyung Hand15aa1f2019-09-27 00:38:03 +090057 Description: "prepare ${out}",
58 }, "provideNativeLibs", "requireNativeLibs", "opt")
Jooyung Hane1633032019-08-01 17:41:43 +090059
Jiyong Park48ca7dc2018-10-10 14:01:00 +090060 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
61 // against the binary policy using sefcontext_compiler -p <policy>.
62
63 // TODO(b/114327326): automate the generation of file_contexts
64 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
65 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010066 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090067 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090068 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090069 `--file_contexts ${file_contexts} ` +
70 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080071 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090072 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090073 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
74 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000075 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010076 Rspfile: "${out}.copy_commands",
77 RspfileContent: "${copy_commands}",
78 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090079 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080080
Alex Light5098a612018-11-29 17:12:15 -080081 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
82 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010083 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080084 `APEXER_TOOL_PATH=${tool_path} ` +
85 `${apexer} --force --manifest ${manifest} ` +
86 `--payload_type zip ` +
87 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010088 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
89 Rspfile: "${out}.copy_commands",
90 RspfileContent: "${copy_commands}",
91 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080092 }, "tool_path", "image_dir", "copy_commands", "manifest")
93
Colin Crossa4925902018-11-16 11:36:28 -080094 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
95 blueprint.RuleParams{
96 Command: `${aapt2} convert --output-format proto $in -o $out`,
97 CommandDeps: []string{"${aapt2}"},
98 })
99
100 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +0900101 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +0000102 `apex_payload.img:apex/${abi}.img ` +
103 `apex_manifest.json:root/apex_manifest.json ` +
Jaewoong Jungb00c1fb2019-09-04 13:26:18 -0700104 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
105 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -0800106 CommandDeps: []string{"${zip2zip}"},
107 Description: "app bundle",
108 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100109
110 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
111 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
112 Rspfile: "${out}.emit_commands",
113 RspfileContent: "${emit_commands}",
114 Description: "Emit APEX image content",
115 }, "emit_commands")
116
117 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
118 Command: `diff --unchanged-group-format='' \` +
119 `--changed-group-format='%<' \` +
120 `${image_content_file} ${whitelisted_files_file} || (` +
121 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
122 ` "To fix the build run following command:" && ` +
123 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
124 `exit 1)`,
125 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
126 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900127)
128
Jooyung Han72bd2f82019-10-23 16:46:38 +0900129const (
130 imageApexSuffix = ".apex"
131 zipApexSuffix = ".zipapex"
Sundong Ahnabb64432019-10-22 13:58:29 +0900132 flattenedSuffix = ".flattened"
Alex Light5098a612018-11-29 17:12:15 -0800133
Sundong Ahnabb64432019-10-22 13:58:29 +0900134 imageApexType = "image"
135 zipApexType = "zip"
136 flattenedApexType = "flattened"
Jooyung Han72bd2f82019-10-23 16:46:38 +0900137
138 vndkApexNamePrefix = "com.android.vndk.v"
139)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900140
141type dependencyTag struct {
142 blueprint.BaseDependencyTag
143 name string
144}
145
146var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900147 sharedLibTag = dependencyTag{name: "sharedLib"}
148 executableTag = dependencyTag{name: "executable"}
149 javaLibTag = dependencyTag{name: "javaLib"}
150 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100151 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900152 keyTag = dependencyTag{name: "key"}
153 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900154 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900155 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900156)
157
158func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700159 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900160 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900161 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100162 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
163 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
164 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
165 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000166 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100167 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
168 } else {
169 return pctx.HostBinToolPath(ctx, tool).String()
170 }
171 })
172 }
173 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900174 pctx.HostBinToolVariable("avbtool", "avbtool")
175 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
176 pctx.HostBinToolVariable("merge_zips", "merge_zips")
177 pctx.HostBinToolVariable("mke2fs", "mke2fs")
178 pctx.HostBinToolVariable("resize2fs", "resize2fs")
179 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
180 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800181 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900182 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900183 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900184
Jiyong Parkd1063c12019-07-17 20:08:41 +0900185 android.RegisterModuleType("apex", BundleFactory)
Alex Light0851b882019-02-07 13:20:53 -0800186 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jooyung Han344d5432019-08-23 11:17:39 +0900187 android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900188 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700189 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900190
Jooyung Han31c470b2019-10-18 16:26:59 +0900191 android.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900192 android.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han7a78a922019-10-08 21:59:58 +0900193
194 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
195 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
196 sort.Strings(*apexFileContextsInfos)
197 ctx.Strict("APEX_FILE_CONTEXTS_INFOS", strings.Join(*apexFileContextsInfos, " "))
198 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900199}
200
Jooyung Han31c470b2019-10-18 16:26:59 +0900201func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
202 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
203 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
204}
205
Jiyong Parkd1063c12019-07-17 20:08:41 +0900206func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
207 ctx.TopDown("apex_deps", apexDepsMutator)
208 ctx.BottomUp("apex", apexMutator).Parallel()
209 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
210 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900211}
212
Jooyung Han344d5432019-08-23 11:17:39 +0900213var (
214 vndkApexListKey = android.NewOnceKey("vndkApexList")
215 vndkApexListMutex sync.Mutex
216)
217
Jooyung Han31c470b2019-10-18 16:26:59 +0900218func vndkApexList(config android.Config) map[string]string {
Jooyung Han344d5432019-08-23 11:17:39 +0900219 return config.Once(vndkApexListKey, func() interface{} {
Jooyung Han31c470b2019-10-18 16:26:59 +0900220 return map[string]string{}
221 }).(map[string]string)
Jooyung Han344d5432019-08-23 11:17:39 +0900222}
223
Jooyung Han31c470b2019-10-18 16:26:59 +0900224func apexVndkMutator(mctx android.TopDownMutatorContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900225 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
226 if ab.IsNativeBridgeSupported() {
227 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
228 }
Jooyung Han90eee022019-10-01 20:02:42 +0900229
Jooyung Han31c470b2019-10-18 16:26:59 +0900230 vndkVersion := ab.vndkVersion(mctx.DeviceConfig())
231 // Ensure VNDK APEX mount point is formatted as com.android.vndk.v###
Jooyung Han72bd2f82019-10-23 16:46:38 +0900232 ab.properties.Apex_name = proptools.StringPtr(vndkApexNamePrefix + vndkVersion)
Jooyung Han90eee022019-10-01 20:02:42 +0900233
Jooyung Han31c470b2019-10-18 16:26:59 +0900234 // vndk_version should be unique
Jooyung Han344d5432019-08-23 11:17:39 +0900235 vndkApexListMutex.Lock()
236 defer vndkApexListMutex.Unlock()
237 vndkApexList := vndkApexList(mctx.Config())
238 if other, ok := vndkApexList[vndkVersion]; ok {
Jooyung Han31c470b2019-10-18 16:26:59 +0900239 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other)
Jooyung Han344d5432019-08-23 11:17:39 +0900240 }
Jooyung Han31c470b2019-10-18 16:26:59 +0900241 vndkApexList[vndkVersion] = mctx.ModuleName()
Jooyung Han344d5432019-08-23 11:17:39 +0900242 }
243}
244
Jooyung Han31c470b2019-10-18 16:26:59 +0900245func apexVndkDepsMutator(mctx android.BottomUpMutatorContext) {
246 if m, ok := mctx.Module().(*cc.Module); ok && cc.IsForVndkApex(mctx, m) {
247 vndkVersion := m.VndkVersion()
Jooyung Han344d5432019-08-23 11:17:39 +0900248 vndkApexList := vndkApexList(mctx.Config())
Jooyung Han31c470b2019-10-18 16:26:59 +0900249 if vndkApex, ok := vndkApexList[vndkVersion]; ok {
250 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, vndkApex)
Jooyung Han344d5432019-08-23 11:17:39 +0900251 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900252 } else if a, ok := mctx.Module().(*apexBundle); ok && a.vndkApex {
253 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
254 mctx.AddDependency(mctx.Module(), prebuiltTag, cc.VndkLibrariesTxtModules(vndkVersion)...)
Jooyung Han344d5432019-08-23 11:17:39 +0900255 }
256}
257
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900258// Mark the direct and transitive dependencies of apex bundles so that they
259// can be built for the apex bundles.
260func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800261 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800262 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900263 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900264 depName := mctx.OtherModuleName(child)
265 // If the parent is apexBundle, this child is directly depended.
266 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800267 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800268 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
269 // non-installable apex's cannot be installed and so should not prevent libraries from being
270 // installed to the system.
271 android.UpdateApexDependency(apexBundleName, depName, directDep)
272 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900273
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900274 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900275 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900276 return true
277 } else {
278 return false
279 }
280 })
281 }
282}
283
284// Create apex variations if a module is included in APEX(s).
285func apexMutator(mctx android.BottomUpMutatorContext) {
286 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900287 am.CreateApexVariations(mctx)
Jooyung Han7a78a922019-10-08 21:59:58 +0900288 } else if a, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900289 // apex bundle itself is mutated so that it and its modules have same
290 // apex variant.
291 apexBundleName := mctx.ModuleName()
292 mctx.CreateVariations(apexBundleName)
Jooyung Han7a78a922019-10-08 21:59:58 +0900293
294 // collects APEX list
295 if mctx.Device() && a.installable() {
296 addApexFileContextsInfos(mctx, a)
297 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900298 }
299}
Sundong Ahne9b55722019-09-06 17:37:42 +0900300
Jooyung Han7a78a922019-10-08 21:59:58 +0900301var (
302 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
303 apexFileContextsInfosMutex sync.Mutex
304)
305
306func apexFileContextsInfos(config android.Config) *[]string {
307 return config.Once(apexFileContextsInfosKey, func() interface{} {
308 return &[]string{}
309 }).(*[]string)
310}
311
312func addApexFileContextsInfos(ctx android.BaseModuleContext, a *apexBundle) {
313 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
314 fileContextsName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
315
316 apexFileContextsInfosMutex.Lock()
317 defer apexFileContextsInfosMutex.Unlock()
318 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
319 *apexFileContextsInfos = append(*apexFileContextsInfos, apexName+":"+fileContextsName)
320}
321
Sundong Ahne9b55722019-09-06 17:37:42 +0900322func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900323 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +0900324 var variants []string
325 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
326 case "image":
327 variants = append(variants, imageApexType, flattenedApexType)
328 case "zip":
329 variants = append(variants, zipApexType)
330 case "both":
331 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
332 default:
333 mctx.PropertyErrorf("type", "%q is not one of \"image\" or \"zip\".", *ab.properties.Payload_type)
334 return
335 }
336
337 modules := mctx.CreateLocalVariations(variants...)
338
339 for i, v := range variants {
340 switch v {
341 case imageApexType:
342 modules[i].(*apexBundle).properties.ApexType = imageApex
343 case zipApexType:
344 modules[i].(*apexBundle).properties.ApexType = zipApex
345 case flattenedApexType:
346 modules[i].(*apexBundle).properties.ApexType = flattenedApex
347 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900348 }
349 }
350}
351
Jooyung Han5c998b92019-06-27 11:30:33 +0900352func apexUsesMutator(mctx android.BottomUpMutatorContext) {
353 if ab, ok := mctx.Module().(*apexBundle); ok {
354 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
355 }
356}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900357
Jooyung Handc782442019-11-01 03:14:38 +0900358var (
359 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
360)
361
362// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
363// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
364// which may cause compatibility issues. (e.g. libbinder)
365// Even though libbinder restricts its availability via 'apex_available' property and relies on
366// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
367// to avoid similar problems.
368func useVendorWhitelist(config android.Config) []string {
369 return config.Once(useVendorWhitelistKey, func() interface{} {
370 return []string{
371 // swcodec uses "vendor" variants for smaller size
372 "com.android.media.swcodec",
373 "test_com.android.media.swcodec",
374 }
375 }).([]string)
376}
377
378// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
379// called before the first call to useVendorWhitelist()
380func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
381 config.Once(useVendorWhitelistKey, func() interface{} {
382 return whitelist
383 })
384}
385
Alex Light9670d332019-01-29 18:07:33 -0800386type apexNativeDependencies struct {
387 // List of native libraries
388 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900389
Alex Light9670d332019-01-29 18:07:33 -0800390 // List of native executables
391 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900392
Roland Levillain630846d2019-06-26 12:48:34 +0100393 // List of native tests
394 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800395}
Jooyung Han344d5432019-08-23 11:17:39 +0900396
Alex Light9670d332019-01-29 18:07:33 -0800397type apexMultilibProperties struct {
398 // Native dependencies whose compile_multilib is "first"
399 First apexNativeDependencies
400
401 // Native dependencies whose compile_multilib is "both"
402 Both apexNativeDependencies
403
404 // Native dependencies whose compile_multilib is "prefer32"
405 Prefer32 apexNativeDependencies
406
407 // Native dependencies whose compile_multilib is "32"
408 Lib32 apexNativeDependencies
409
410 // Native dependencies whose compile_multilib is "64"
411 Lib64 apexNativeDependencies
412}
413
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900414type apexBundleProperties struct {
415 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000416 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800417 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900418
Jiyong Park40e26a22019-02-08 02:53:06 +0900419 // AndroidManifest.xml file used for the zip container of this APEX bundle.
420 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800421 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900422
Roland Levillain411c5842019-09-19 16:37:20 +0100423 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
424 // device (/apex/<apex_name>).
425 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900426 Apex_name *string
427
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900428 // Determines the file contexts file for setting security context to each file in this APEX bundle.
429 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
430 // used.
431 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900432 File_contexts *string
433
434 // List of native shared libs that are embedded inside this APEX bundle
435 Native_shared_libs []string
436
Roland Levillain630846d2019-06-26 12:48:34 +0100437 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900438 Binaries []string
439
440 // List of java libraries that are embedded inside this APEX bundle
441 Java_libs []string
442
443 // List of prebuilt files that are embedded inside this APEX bundle
444 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900445
Roland Levillain630846d2019-06-26 12:48:34 +0100446 // List of tests that are embedded inside this APEX bundle
447 Tests []string
448
Jiyong Parkff1458f2018-10-12 21:49:38 +0900449 // Name of the apex_key module that provides the private key to sign APEX
450 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900451
Alex Light5098a612018-11-29 17:12:15 -0800452 // The type of APEX to build. Controls what the APEX payload is. Either
453 // 'image', 'zip' or 'both'. Default: 'image'.
454 Payload_type *string
455
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900456 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
457 // or an android_app_certificate module name in the form ":module".
458 Certificate *string
459
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900460 // Whether this APEX is installable to one of the partitions. Default: true.
461 Installable *bool
462
Jiyong Parkda6eb592018-12-19 17:12:36 +0900463 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
464 // Default is false.
465 Use_vendor *bool
466
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800467 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
468 Ignore_system_library_special_case *bool
469
Alex Light9670d332019-01-29 18:07:33 -0800470 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900471
Jiyong Parkf97782b2019-02-13 20:28:58 +0900472 // List of sanitizer names that this APEX is enabled for
473 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900474
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900475 PreventInstall bool `blueprint:"mutated"`
476
477 HideFromMake bool `blueprint:"mutated"`
478
Jooyung Han5c998b92019-06-27 11:30:33 +0900479 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
480 Provide_cpp_shared_libs *bool
481
482 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
483 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100484
485 // A txt file containing list of files that are whitelisted to be included in this APEX.
486 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900487
488 // List of APKs to package inside APEX
489 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900490
Sundong Ahnabb64432019-10-22 13:58:29 +0900491 // package format of this apex variant; could be non-flattened, flattened, or zip.
492 // imageApex, zipApex or flattened
493 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900494
Jiyong Parkd1063c12019-07-17 20:08:41 +0900495 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
496 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
497 // is implied. This value affects all modules included in this APEX. In other words, they are
498 // also built with the SDKs specified here.
499 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800500}
501
502type apexTargetBundleProperties struct {
503 Target struct {
504 // Multilib properties only for android.
505 Android struct {
506 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900507 }
Jooyung Han344d5432019-08-23 11:17:39 +0900508
Alex Light9670d332019-01-29 18:07:33 -0800509 // Multilib properties only for host.
510 Host struct {
511 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900512 }
Jooyung Han344d5432019-08-23 11:17:39 +0900513
Alex Light9670d332019-01-29 18:07:33 -0800514 // Multilib properties only for host linux_bionic.
515 Linux_bionic struct {
516 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900517 }
Jooyung Han344d5432019-08-23 11:17:39 +0900518
Alex Light9670d332019-01-29 18:07:33 -0800519 // Multilib properties only for host linux_glibc.
520 Linux_glibc struct {
521 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900522 }
523 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900524}
525
Jooyung Han344d5432019-08-23 11:17:39 +0900526type apexVndkProperties struct {
527 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
528 Vndk_version *string
529}
530
Jiyong Park8fd61922018-11-08 02:50:25 +0900531type apexFileClass int
532
533const (
534 etc apexFileClass = iota
535 nativeSharedLib
536 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900537 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800538 pyBinary
539 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900540 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100541 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900542 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900543)
544
Alex Light5098a612018-11-29 17:12:15 -0800545type apexPackaging int
546
547const (
548 imageApex apexPackaging = iota
549 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900550 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800551)
552
Sundong Ahnabb64432019-10-22 13:58:29 +0900553// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800554func (a apexPackaging) suffix() string {
555 switch a {
556 case imageApex:
557 return imageApexSuffix
558 case zipApex:
559 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800560 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100561 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800562 }
563}
564
565func (a apexPackaging) name() string {
566 switch a {
567 case imageApex:
568 return imageApexType
569 case zipApex:
570 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800571 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100572 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800573 }
574}
575
Jiyong Park8fd61922018-11-08 02:50:25 +0900576func (class apexFileClass) NameInMake() string {
577 switch class {
578 case etc:
579 return "ETC"
580 case nativeSharedLib:
581 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800582 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900583 return "EXECUTABLES"
584 case javaSharedLib:
585 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100586 case nativeTest:
587 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900588 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900589 // b/142537672 Why isn't this APP? We want to have full control over
590 // the paths and file names of the apk file under the flattend APEX.
591 // If this is set to APP, then the paths and file names are modified
592 // by the Make build system. For example, it is installed to
593 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
594 // /system/apex/<apexname>/app/<Appname> because the build system automatically
595 // appends module name (which is <apexname>.<Appname> to the path.
596 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900597 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100598 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900599 }
600}
601
602type apexFile struct {
603 builtFile android.Path
604 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900605 installDir string
606 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900607 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800608 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900609}
610
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900611type apexBundle struct {
612 android.ModuleBase
613 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900614 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900615
Alex Light9670d332019-01-29 18:07:33 -0800616 properties apexBundleProperties
617 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900618 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900619
Colin Crossa4925902018-11-16 11:36:28 -0800620 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900621 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700622 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900623
Jiyong Park03b68dd2019-07-26 23:20:40 +0900624 prebuiltFileToDelete string
625
Jiyong Park42cca6c2019-04-01 11:15:50 +0900626 public_key_file android.Path
627 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900628
629 container_certificate_file android.Path
630 container_private_key_file android.Path
631
Jiyong Park8fd61922018-11-08 02:50:25 +0900632 // list of files to be included in this apex
633 filesInfo []apexFile
634
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900635 // list of module names that this APEX is depending on
636 externalDeps []string
637
Sundong Ahnabb64432019-10-22 13:58:29 +0900638 testApex bool
639 vndkApex bool
640 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900641
642 // intermediate path for apex_manifest.json
643 manifestOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900644
645 // list of commands to create symlinks for backward compatibility
646 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
647 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
648 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
649 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900650
651 // Suffix of module name in Android.mk
652 // ".flattened", ".apex", ".zipapex", or ""
653 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900654}
655
Jiyong Park397e55e2018-10-24 21:09:55 +0900656func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100657 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700658 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900659 // Use *FarVariation* to be able to depend on modules having
660 // conflicting variations with this module. This is required since
661 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
662 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700663 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900664 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900665 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900666 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700667 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900668
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700669 ctx.AddFarVariationDependencies(append(target.Variations(),
670 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
671 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100672
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700673 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100674 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100675 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700676 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900677}
678
Alex Light9670d332019-01-29 18:07:33 -0800679func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
680 if ctx.Os().Class == android.Device {
681 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
682 } else {
683 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
684 if ctx.Os().Bionic() {
685 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
686 } else {
687 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
688 }
689 }
690}
691
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900692func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900693 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
694 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
695 }
696
Jiyong Park397e55e2018-10-24 21:09:55 +0900697 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900698 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800699
700 a.combineProperties(ctx)
701
Jiyong Park397e55e2018-10-24 21:09:55 +0900702 has32BitTarget := false
703 for _, target := range targets {
704 if target.Arch.ArchType.Multilib == "lib32" {
705 has32BitTarget = true
706 }
707 }
708 for i, target := range targets {
709 // When multilib.* is omitted for native_shared_libs, it implies
710 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700711 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900712 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900713 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700714 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900715
Roland Levillain630846d2019-06-26 12:48:34 +0100716 // When multilib.* is omitted for tests, it implies
717 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700718 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100719 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100720 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700721 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100722
Jiyong Park397e55e2018-10-24 21:09:55 +0900723 // Add native modules targetting both ABIs
724 addDependenciesForNativeModules(ctx,
725 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100726 a.properties.Multilib.Both.Binaries,
727 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700728 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900729 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900730
Alex Light3d673592019-01-18 14:37:31 -0800731 isPrimaryAbi := i == 0
732 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900733 // When multilib.* is omitted for binaries, it implies
734 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700735 ctx.AddFarVariationDependencies(append(target.Variations(),
736 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
737 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900738
739 // Add native modules targetting the first ABI
740 addDependenciesForNativeModules(ctx,
741 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100742 a.properties.Multilib.First.Binaries,
743 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700744 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900745 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800746
747 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700748 ctx.AddFarVariationDependencies(target.Variations(),
749 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900750 }
751
752 switch target.Arch.ArchType.Multilib {
753 case "lib32":
754 // Add native modules targetting 32-bit ABI
755 addDependenciesForNativeModules(ctx,
756 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100757 a.properties.Multilib.Lib32.Binaries,
758 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700759 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900760 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900761
762 addDependenciesForNativeModules(ctx,
763 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100764 a.properties.Multilib.Prefer32.Binaries,
765 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700766 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900767 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900768 case "lib64":
769 // Add native modules targetting 64-bit ABI
770 addDependenciesForNativeModules(ctx,
771 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100772 a.properties.Multilib.Lib64.Binaries,
773 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700774 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900775 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900776
777 if !has32BitTarget {
778 addDependenciesForNativeModules(ctx,
779 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100780 a.properties.Multilib.Prefer32.Binaries,
781 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700782 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900783 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900784 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700785
786 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
787 for _, sanitizer := range ctx.Config().SanitizeDevice() {
788 if sanitizer == "hwaddress" {
789 addDependenciesForNativeModules(ctx,
790 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700791 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700792 break
793 }
794 }
795 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900796 }
797
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900798 }
799
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700800 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
801 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900802
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700803 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
804 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900805
Jiyong Park23c52b02019-02-02 13:13:47 +0900806 if String(a.properties.Key) == "" {
807 ctx.ModuleErrorf("key is missing")
808 return
809 }
810 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900811
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900812 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900813 if cert != "" {
814 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900815 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900816
817 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
818 if len(a.properties.Uses_sdks) > 0 {
819 sdkRefs := []android.SdkRef{}
820 for _, str := range a.properties.Uses_sdks {
821 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
822 sdkRefs = append(sdkRefs, parsed)
823 }
824 a.BuildWithSdks(sdkRefs)
825 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900826}
827
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900828func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
829 // direct deps of an APEX bundle are all part of the APEX bundle
830 return true
831}
832
Colin Cross0ea8ba82019-06-06 14:33:29 -0700833func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900834 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
835 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000836 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900837 }
838 return String(a.properties.Certificate)
839}
840
Colin Cross41955e82019-05-29 14:40:35 -0700841func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
842 switch tag {
843 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900844 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700845 default:
846 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900847 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900848}
849
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900850func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900851 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900852}
853
Jiyong Park7c1dc612019-01-05 11:15:24 +0900854func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900855 if a.vndkApex {
856 return "vendor." + a.vndkVersion(config)
857 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900858 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900859 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900860 } else {
861 return "core"
862 }
863}
864
Jiyong Parkf97782b2019-02-13 20:28:58 +0900865func (a *apexBundle) EnableSanitizer(sanitizerName string) {
866 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
867 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
868 }
869}
870
Jiyong Park388ef3f2019-01-28 19:47:32 +0900871func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900872 if android.InList(sanitizerName, a.properties.SanitizerNames) {
873 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900874 }
875
876 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900877 globalSanitizerNames := []string{}
878 if a.Host() {
879 globalSanitizerNames = ctx.Config().SanitizeHost()
880 } else {
881 arches := ctx.Config().SanitizeDeviceArch()
882 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
883 globalSanitizerNames = ctx.Config().SanitizeDevice()
884 }
885 }
886 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900887}
888
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900889func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
890 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
891}
892
893func (a *apexBundle) PreventInstall() {
894 a.properties.PreventInstall = true
895}
896
897func (a *apexBundle) HideFromMake() {
898 a.properties.HideFromMake = true
899}
900
Martin Stjernholm279de572019-09-10 23:18:20 +0100901func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900902 // Decide the APEX-local directory by the multilib of the library
903 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100904 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900905 case "lib32":
906 dirInApex = "lib"
907 case "lib64":
908 dirInApex = "lib64"
909 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100910 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700911 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100912 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900913 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100914 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
915 // Special case for Bionic libs and other libs installed with them. This is
916 // to prevent those libs from being included in the search path
917 // /apex/com.android.runtime/${LIB}. This exclusion is required because
918 // those libs in the Runtime APEX are available via the legacy paths in
919 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
920 // to the legacy paths and thus will be loaded into the default linker
921 // namespace (aka "platform" namespace). If the libs are directly in
922 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
923 // into the runtime linker namespace, which will result in double loading of
924 // them, which isn't supported.
925 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900926 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900927
Martin Stjernholm279de572019-09-10 23:18:20 +0100928 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900929 return
930}
931
932func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900933 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700934 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200935 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900936 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900937 fileToCopy = cc.OutputFile().Path()
938 return
939}
940
Alex Light778127a2019-02-27 14:19:50 -0800941func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
942 dirInApex = "bin"
943 fileToCopy = py.HostToolPath().Path()
944 return
945}
946func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
947 dirInApex = "bin"
948 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
949 if err != nil {
950 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
951 return
952 }
953 fileToCopy = android.PathForOutput(ctx, s)
954 return
955}
956
Jiyong Park04480cf2019-02-06 00:16:29 +0900957func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
958 dirInApex = filepath.Join("bin", sh.SubDir())
959 fileToCopy = sh.OutputFile()
960 return
961}
962
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900963func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
964 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900965 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900966 return
967}
968
Jiyong Park9e6c2422019-08-09 20:39:45 +0900969func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
970 dirInApex = "javalib"
971 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
972 implJars := java.ImplementationJars()
973 if len(implJars) != 1 {
974 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
975 strings.Join(implJars.Strings(), ", ")))
976 }
977 fileToCopy = implJars[0]
978 return
979}
980
Jooyung Han39edb6c2019-11-06 16:53:07 +0900981func getCopyManifestForPrebuiltEtc(prebuilt android.PrebuiltEtcModule) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900982 dirInApex = filepath.Join("etc", prebuilt.SubDir())
983 fileToCopy = prebuilt.OutputFile()
984 return
985}
986
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900987func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +0900988 appDir := "app"
989 if app.Privileged() {
990 appDir = "priv-app"
991 }
992 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900993 fileToCopy = app.OutputFile()
994 return
995}
996
Dario Frenicde2a032019-10-27 00:29:22 +0100997func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
998 appDir := "app"
999 if app.Privileged() {
1000 appDir = "priv-app"
1001 }
1002 dirInApex = filepath.Join(appDir, pkgName)
1003 fileToCopy = app.OutputFile()
1004 return
1005}
1006
Roland Levillain935639d2019-08-13 14:55:28 +01001007// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1008type flattenedApexContext struct {
1009 android.ModuleContext
1010}
1011
1012func (c *flattenedApexContext) InstallBypassMake() bool {
1013 return true
1014}
1015
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001016func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001017 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001018
Sundong Ahnabb64432019-10-22 13:58:29 +09001019 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1020 switch a.properties.ApexType {
1021 case imageApex:
1022 if buildFlattenedAsDefault {
1023 a.suffix = imageApexSuffix
1024 } else {
1025 a.suffix = ""
1026 a.primaryApexType = true
1027 }
1028 case zipApex:
1029 if proptools.String(a.properties.Payload_type) == "zip" {
1030 a.suffix = ""
1031 a.primaryApexType = true
1032 } else {
1033 a.suffix = zipApexSuffix
1034 }
1035 case flattenedApex:
1036 if buildFlattenedAsDefault {
1037 a.suffix = ""
1038 a.primaryApexType = true
1039 } else {
1040 a.suffix = flattenedSuffix
1041 }
Alex Light5098a612018-11-29 17:12:15 -08001042 }
1043
Roland Levillain630846d2019-06-26 12:48:34 +01001044 if len(a.properties.Tests) > 0 && !a.testApex {
1045 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1046 return
1047 }
1048
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001049 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1050
Jooyung Hane1633032019-08-01 17:41:43 +09001051 // native lib dependencies
1052 var provideNativeLibs []string
1053 var requireNativeLibs []string
1054
Jooyung Han5c998b92019-06-27 11:30:33 +09001055 // Check if "uses" requirements are met with dependent apexBundles
1056 var providedNativeSharedLibs []string
1057 useVendor := proptools.Bool(a.properties.Use_vendor)
1058 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1059 if ctx.OtherModuleDependencyTag(m) != usesTag {
1060 return
1061 }
1062 otherName := ctx.OtherModuleName(m)
1063 other, ok := m.(*apexBundle)
1064 if !ok {
1065 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1066 return
1067 }
1068 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1069 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1070 return
1071 }
1072 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1073 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1074 return
1075 }
1076 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1077 })
1078
Alex Light778127a2019-02-27 14:19:50 -08001079 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001080 depTag := ctx.OtherModuleDependencyTag(child)
1081 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001082 if _, ok := parent.(*apexBundle); ok {
1083 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001084 switch depTag {
1085 case sharedLibTag:
1086 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001087 if cc.HasStubsVariants() {
1088 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1089 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001090 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001091 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001092 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001093 } else {
1094 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001095 }
1096 case executableTag:
1097 if cc, ok := child.(*cc.Module); ok {
1098 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001099 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001100 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001101 } else if sh, ok := child.(*android.ShBinary); ok {
1102 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001103 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001104 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1105 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1106 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1107 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1108 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1109 // NB: Since go binaries are static we don't need the module for anything here, which is
1110 // good since the go tool is a blueprint.Module not an android.Module like we would
1111 // normally use.
1112 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001113 } else {
Alex Light778127a2019-02-27 14:19:50 -08001114 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 +09001115 }
1116 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001117 if javaLib, ok := child.(*java.Library); ok {
1118 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001119 if fileToCopy == nil {
1120 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1121 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001122 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1123 }
1124 return true
1125 } else if javaLib, ok := child.(*java.Import); ok {
1126 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1127 if fileToCopy == nil {
1128 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1129 } else {
1130 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001131 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001132 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001133 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001134 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001135 }
1136 case prebuiltTag:
Jooyung Han39edb6c2019-11-06 16:53:07 +09001137 if prebuilt, ok := child.(android.PrebuiltEtcModule); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001138 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001139 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001140 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001141 } else {
1142 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1143 }
Roland Levillain630846d2019-06-26 12:48:34 +01001144 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001145 if ccTest, ok := child.(*cc.Module); ok {
1146 if ccTest.IsTestPerSrcAllTestsVariation() {
1147 // Multiple-output test module (where `test_per_src: true`).
1148 //
1149 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1150 // We do not add this variation to `filesInfo`, as it has no output;
1151 // however, we do add the other variations of this module as indirect
1152 // dependencies (see below).
1153 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001154 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001155 // Single-output test module (where `test_per_src: false`).
1156 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1157 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001158 }
Roland Levillain630846d2019-06-26 12:48:34 +01001159 return true
1160 } else {
1161 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1162 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001163 case keyTag:
1164 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001165 a.private_key_file = key.private_key_file
1166 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001167 return false
1168 } else {
1169 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001170 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001171 case certificateTag:
1172 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001173 a.container_certificate_file = dep.Certificate.Pem
1174 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001175 return false
1176 } else {
1177 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1178 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001179 case android.PrebuiltDepTag:
1180 // If the prebuilt is force disabled, remember to delete the prebuilt file
1181 // that might have been installed in the previous builds
1182 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1183 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1184 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001185 case androidAppTag:
1186 if ap, ok := child.(*java.AndroidApp); ok {
1187 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1188 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1189 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001190 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1191 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1192 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001193 } else {
1194 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1195 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001196 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001197 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001198 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001199 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001200 // We cannot use a switch statement on `depTag` here as the checked
1201 // tags used below are private (e.g. `cc.sharedDepTag`).
1202 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
1203 if cc, ok := child.(*cc.Module); ok {
1204 if android.InList(cc.Name(), providedNativeSharedLibs) {
1205 // If we're using a shared library which is provided from other APEX,
1206 // don't include it in this APEX
1207 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001208 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001209 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1210 // If the dependency is a stubs lib, don't include it in this APEX,
1211 // but make sure that the lib is installed on the device.
1212 // In case no APEX is having the lib, the lib is installed to the system
1213 // partition.
1214 //
1215 // Always include if we are a host-apex however since those won't have any
1216 // system libraries.
1217 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1218 a.externalDeps = append(a.externalDeps, cc.Name())
1219 }
Jooyung Hane1633032019-08-01 17:41:43 +09001220 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001221 // Don't track further
1222 return false
1223 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001224 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001225 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1226 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001227 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001228 } else if cc.IsTestPerSrcDepTag(depTag) {
1229 if cc, ok := child.(*cc.Module); ok {
1230 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1231 // Handle modules created as `test_per_src` variations of a single test module:
1232 // use the name of the generated test binary (`fileToCopy`) instead of the name
1233 // of the original test module (`depName`, shared by all `test_per_src`
1234 // variations of that module).
1235 moduleName := filepath.Base(fileToCopy.String())
1236 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1237 return true
1238 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001239 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001240 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001241 }
1242 }
1243 }
1244 return false
1245 })
1246
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001247 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001248 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1249 return
1250 }
1251
Jiyong Park8fd61922018-11-08 02:50:25 +09001252 // remove duplicates in filesInfo
1253 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001254 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001255 result := []apexFile{}
1256 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001257 dest := filepath.Join(f.installDir, f.builtFile.Base())
1258 if !encountered[dest] {
1259 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001260 result = append(result, f)
1261 }
1262 }
1263 return result
1264 }
1265 filesInfo = removeDup(filesInfo)
1266
1267 // to have consistent build rules
1268 sort.Slice(filesInfo, func(i, j int) bool {
1269 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1270 })
1271
Jiyong Park127b40b2019-09-30 16:04:35 +09001272 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001273 if !ctx.Host() {
1274 for _, fi := range filesInfo {
1275 if am, ok := fi.module.(android.ApexModule); ok {
1276 if !am.AvailableFor(ctx.ModuleName()) {
1277 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1278 return
1279 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001280 }
1281 }
1282 }
1283
Jiyong Park8fd61922018-11-08 02:50:25 +09001284 // prepend the name of this APEX to the module names. These names will be the names of
1285 // modules that will be defined if the APEX is flattened.
1286 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001287 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001288 }
1289
Jiyong Park8fd61922018-11-08 02:50:25 +09001290 a.installDir = android.PathForModuleInstall(ctx, "apex")
1291 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001292
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001293 // prepare apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001294 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
Jooyung Hane1633032019-08-01 17:41:43 +09001295 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001296
1297 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001298 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1299 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001300
1301 // apex name can be overridden
1302 optCommands := []string{}
1303 if a.properties.Apex_name != nil {
1304 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1305 }
1306
Jooyung Hane1633032019-08-01 17:41:43 +09001307 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001308 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001309 Input: manifestSrc,
1310 Output: a.manifestOut,
1311 Args: map[string]string{
1312 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1313 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001314 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001315 },
1316 })
1317
Sundong Ahnabb64432019-10-22 13:58:29 +09001318 a.setCertificateAndPrivateKey(ctx)
1319 if a.properties.ApexType == flattenedApex {
Jiyong Park23c52b02019-02-02 13:13:47 +09001320 a.buildFlattenedApex(ctx)
Sundong Ahnabb64432019-10-22 13:58:29 +09001321 } else {
1322 a.buildUnflattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001323 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001324
Sundong Ahnabb64432019-10-22 13:58:29 +09001325 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001326 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001327}
1328
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001329func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001330 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001331 for _, f := range a.filesInfo {
1332 if f.module != nil {
1333 notice := f.module.NoticeFile()
1334 if notice.Valid() {
1335 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001336 }
1337 }
1338 }
1339 // append the notice file specified in the apex module itself
1340 if a.NoticeFile().Valid() {
1341 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001342 }
1343
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001344 if len(noticeFiles) == 0 {
1345 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001346 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001347
Jaewoong Jung98772792019-07-01 17:15:13 -07001348 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001349}
1350
Sundong Ahnabb64432019-10-22 13:58:29 +09001351func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001352 var abis []string
1353 for _, target := range ctx.MultiTargets() {
1354 if len(target.Arch.Abi) > 0 {
1355 abis = append(abis, target.Arch.Abi[0])
1356 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001357 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001358
Alex Light5098a612018-11-29 17:12:15 -08001359 abis = android.FirstUniqueStrings(abis)
1360
Sundong Ahnabb64432019-10-22 13:58:29 +09001361 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001362 suffix := apexType.suffix()
1363 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001364
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001365 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001366 for _, f := range a.filesInfo {
1367 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001368 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001369
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001370 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001371 emitCommands := []string{}
1372 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1373 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001374 for i, src := range filesToCopy {
1375 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001376 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001377 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001378 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1379 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001380 for _, sym := range a.filesInfo[i].symlinks {
1381 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1382 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1383 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001384 }
Dario Frenie4235822019-10-28 14:49:27 +00001385 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1386
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001387 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001388 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001389
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001390 if a.properties.Whitelisted_files != nil {
1391 ctx.Build(pctx, android.BuildParams{
1392 Rule: emitApexContentRule,
1393 Implicits: implicitInputs,
1394 Output: imageContentFile,
1395 Description: "emit apex image content",
1396 Args: map[string]string{
1397 "emit_commands": strings.Join(emitCommands, " && "),
1398 },
1399 })
1400 implicitInputs = append(implicitInputs, imageContentFile)
1401 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1402
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001403 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001404 ctx.Build(pctx, android.BuildParams{
1405 Rule: diffApexContentRule,
1406 Implicits: implicitInputs,
1407 Output: phonyOutput,
1408 Description: "diff apex image content",
1409 Args: map[string]string{
1410 "whitelisted_files_file": whitelistedFilesFile.String(),
1411 "image_content_file": imageContentFile.String(),
1412 "apex_module_name": ctx.ModuleName(),
1413 },
1414 })
1415
1416 implicitInputs = append(implicitInputs, phonyOutput)
1417 }
1418
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001419 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1420 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001421
Sundong Ahnabb64432019-10-22 13:58:29 +09001422 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001423 // files and dirs that will be created in APEX
1424 var readOnlyPaths []string
1425 var executablePaths []string // this also includes dirs
1426 for _, f := range a.filesInfo {
1427 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001428 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001429 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001430 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001431 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001432 }
Alex Light5098a612018-11-29 17:12:15 -08001433 } else {
1434 readOnlyPaths = append(readOnlyPaths, pathInApex)
1435 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001436 dir := f.installDir
1437 for !android.InList(dir, executablePaths) && dir != "" {
1438 executablePaths = append(executablePaths, dir)
1439 dir, _ = filepath.Split(dir) // move up to the parent
1440 if len(dir) > 0 {
1441 // remove trailing slash
1442 dir = dir[:len(dir)-1]
1443 }
Alex Light5098a612018-11-29 17:12:15 -08001444 }
1445 }
1446 sort.Strings(readOnlyPaths)
1447 sort.Strings(executablePaths)
1448 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1449 ctx.Build(pctx, android.BuildParams{
1450 Rule: generateFsConfig,
1451 Output: cannedFsConfig,
1452 Description: "generate fs config",
1453 Args: map[string]string{
1454 "ro_paths": strings.Join(readOnlyPaths, " "),
1455 "exec_paths": strings.Join(executablePaths, " "),
1456 },
1457 })
1458
1459 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1460 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1461 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1462 if !fileContextsOptionalPath.Valid() {
1463 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1464 return
1465 }
1466 fileContexts := fileContextsOptionalPath.Path()
1467
Jiyong Park835d82b2018-12-27 16:04:18 +09001468 optFlags := []string{}
1469
Alex Light5098a612018-11-29 17:12:15 -08001470 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001471 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1472 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001473
Jiyong Park7f67f482019-01-05 12:57:48 +09001474 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1475 if overridden {
1476 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1477 }
1478
Jiyong Park40e26a22019-02-08 02:53:06 +09001479 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001480 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001481 implicitInputs = append(implicitInputs, androidManifestFile)
1482 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1483 }
1484
Jiyong Park71b519d2019-04-18 17:25:49 +09001485 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1486 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1487 ctx.Config().UnbundledBuild() &&
1488 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1489 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1490 apiFingerprint := java.ApiFingerprintPath(ctx)
1491 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1492 implicitInputs = append(implicitInputs, apiFingerprint)
1493 }
1494 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1495
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001496 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1497 if noticeFile.Valid() {
1498 // If there's a NOTICE file, embed it as an asset file in the APEX.
1499 implicitInputs = append(implicitInputs, noticeFile.Path())
1500 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1501 }
1502
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001503 if !ctx.Config().UnbundledBuild() && a.installable() {
1504 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1505 // don't need hashtree for activation. Therefore, by removing hashtree from
1506 // apex bundle (filesystem image in it, to be specific), we can save storage.
1507 optFlags = append(optFlags, "--no_hashtree")
1508 }
1509
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001510 if a.properties.Apex_name != nil {
1511 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1512 // Note that apex_manifest is also mended.
1513 optFlags = append(optFlags, "--do_not_check_keyname")
1514 }
1515
Alex Light5098a612018-11-29 17:12:15 -08001516 ctx.Build(pctx, android.BuildParams{
1517 Rule: apexRule,
1518 Implicits: implicitInputs,
1519 Output: unsignedOutputFile,
1520 Description: "apex (" + apexType.name() + ")",
1521 Args: map[string]string{
1522 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1523 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1524 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001525 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001526 "file_contexts": fileContexts.String(),
1527 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001528 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001529 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001530 },
1531 })
1532
1533 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1534 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1535 a.bundleModuleFile = bundleModuleFile
1536
1537 ctx.Build(pctx, android.BuildParams{
1538 Rule: apexProtoConvertRule,
1539 Input: unsignedOutputFile,
1540 Output: apexProtoFile,
1541 Description: "apex proto convert",
1542 })
1543
1544 ctx.Build(pctx, android.BuildParams{
1545 Rule: apexBundleRule,
1546 Input: apexProtoFile,
1547 Output: a.bundleModuleFile,
1548 Description: "apex bundle module",
1549 Args: map[string]string{
1550 "abi": strings.Join(abis, "."),
1551 },
1552 })
1553 } else {
1554 ctx.Build(pctx, android.BuildParams{
1555 Rule: zipApexRule,
1556 Implicits: implicitInputs,
1557 Output: unsignedOutputFile,
1558 Description: "apex (" + apexType.name() + ")",
1559 Args: map[string]string{
1560 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1561 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1562 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001563 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001564 },
1565 })
Colin Crossa4925902018-11-16 11:36:28 -08001566 }
Colin Crossa4925902018-11-16 11:36:28 -08001567
Sundong Ahnabb64432019-10-22 13:58:29 +09001568 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001569 ctx.Build(pctx, android.BuildParams{
1570 Rule: java.Signapk,
1571 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001572 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001573 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001574 Implicits: []android.Path{
1575 a.container_certificate_file,
1576 a.container_private_key_file,
1577 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001578 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001579 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001580 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001581 },
1582 })
Alex Light5098a612018-11-29 17:12:15 -08001583
1584 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001585 if a.installable() {
1586 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001587 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001588 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001589}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001590
Jiyong Park8fd61922018-11-08 02:50:25 +09001591func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001592 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1593 // reply true to `InstallBypassMake()` (thus making the call
1594 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1595 // instead of `android.PathForOutput`) to return the correct path to the flattened
1596 // APEX (as its contents is installed by Make, not Soong).
1597 factx := flattenedApexContext{ctx}
1598 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1599 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1600
1601 a.buildFilesInfo(ctx)
1602}
1603
1604func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1605 cert := String(a.properties.Certificate)
1606 if cert != "" && android.SrcIsModule(cert) == "" {
1607 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1608 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1609 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1610 } else if cert == "" {
1611 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1612 a.container_certificate_file = pem
1613 a.container_private_key_file = key
1614 }
1615}
1616
1617func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001618 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001619 // 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 +09001620 // with other ordinary files.
Sundong Ahnabb64432019-10-22 13:58:29 +09001621 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, "apex_manifest.json." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001622
Jiyong Park42cca6c2019-04-01 11:15:50 +09001623 // rename to apex_pubkey
1624 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1625 ctx.Build(pctx, android.BuildParams{
1626 Rule: android.Cp,
1627 Input: a.public_key_file,
1628 Output: copiedPubkey,
1629 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001630 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001631
Sundong Ahnabb64432019-10-22 13:58:29 +09001632 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001633 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001634 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001635 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001636 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1637 for _, sym := range fi.symlinks {
1638 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1639 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001640 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001641 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001642 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001643}
1644
1645func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001646 if a.properties.HideFromMake {
1647 return android.AndroidMkData{
1648 Disabled: true,
1649 }
1650 }
Alex Light5098a612018-11-29 17:12:15 -08001651 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001652 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001653 return android.AndroidMkData{
1654 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1655 for _, data := range writers {
1656 data.Custom(w, name, prefix, moduleDir, data)
1657 }
1658 }}
1659}
1660
Sundong Ahnabb64432019-10-22 13:58:29 +09001661func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001662 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001663 apexType := a.properties.ApexType
1664 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1665 // to install symbol files in $(PRODUCT_OUT}/apex.
1666 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1667 if !a.primaryApexType && apexType != flattenedApex {
1668 return moduleNames
1669 }
Jiyong Park94427262019-02-05 23:18:47 +09001670
1671 for _, fi := range a.filesInfo {
1672 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1673 continue
1674 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001675
1676 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001677 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001678 }
1679
Jiyong Park94427262019-02-05 23:18:47 +09001680 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1681 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001682 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001683 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001684 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001685 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001686 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001687 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001688 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001689 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001690 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1691 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001692 if len(fi.symlinks) > 0 {
1693 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1694 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001695
1696 if fi.module != nil && fi.module.NoticeFile().Valid() {
1697 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1698 }
Jiyong Park94427262019-02-05 23:18:47 +09001699 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001700 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001701 }
1702 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1703 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1704 if fi.module != nil {
1705 archStr := fi.module.Target().Arch.ArchType.String()
1706 host := false
1707 switch fi.module.Target().Os.Class {
1708 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001709 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001710 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1711 }
1712 host = true
1713 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001714 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001715 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1716 }
1717 host = true
1718 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001719 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001720 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1721 }
1722 }
1723 if host {
1724 makeOs := fi.module.Target().Os.String()
1725 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1726 makeOs = "linux"
1727 }
1728 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1729 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1730 }
1731 }
1732 if fi.class == javaSharedLib {
1733 javaModule := fi.module.(*java.Library)
1734 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1735 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1736 // we will have foo.jar.jar
1737 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1738 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1739 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1740 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1741 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1742 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001743 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001744 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001745 if cc, ok := fi.module.(*cc.Module); ok {
1746 if cc.UnstrippedOutputFile() != nil {
1747 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1748 }
1749 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001750 if cc.CoverageOutputFile().Valid() {
1751 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1752 }
Jiyong Park94427262019-02-05 23:18:47 +09001753 }
1754 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1755 } else {
1756 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001757 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001758 if a.primaryApexType && fi.builtFile.Base() == "apex_manifest.json" && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001759 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1760 }
Jiyong Park94427262019-02-05 23:18:47 +09001761 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1762 }
1763 }
1764 return moduleNames
1765}
1766
Sundong Ahnabb64432019-10-22 13:58:29 +09001767func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001768 return android.AndroidMkData{
1769 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1770 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001771 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001772 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001773 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001774 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001775 }
1776
Sundong Ahnabb64432019-10-22 13:58:29 +09001777 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001778 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001779 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1780 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001781 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001782 if len(moduleNames) > 0 {
1783 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1784 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001785 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001786 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001787
Sundong Ahnabb64432019-10-22 13:58:29 +09001788 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001789 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1790 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001791 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001792 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001793 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001794 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001795 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001796 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001797 if len(moduleNames) > 0 {
1798 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1799 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001800 if len(a.externalDeps) > 0 {
1801 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1802 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001803 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001804 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001805 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001806 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001807 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001808 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1809 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1810 if len(postInstallCommands) > 0 {
1811 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1812 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001813 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001814
Alex Light5098a612018-11-29 17:12:15 -08001815 if apexType == imageApex {
1816 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1817 }
Jiyong Park719b4462019-01-13 00:39:51 +09001818 }
1819 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001820}
1821
Jooyung Han344d5432019-08-23 11:17:39 +09001822func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001823 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001824 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001825 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001826 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001827 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1828 })
Alex Light5098a612018-11-29 17:12:15 -08001829 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001830 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001831 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001832 return module
1833}
Jiyong Park30ca9372019-02-07 16:27:23 +09001834
Jooyung Han344d5432019-08-23 11:17:39 +09001835func ApexBundleFactory(testApex bool) android.Module {
1836 bundle := newApexBundle()
1837 bundle.testApex = testApex
1838 return bundle
1839}
1840
1841func testApexBundleFactory() android.Module {
1842 bundle := newApexBundle()
1843 bundle.testApex = true
1844 return bundle
1845}
1846
Jiyong Parkd1063c12019-07-17 20:08:41 +09001847func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001848 return newApexBundle()
1849}
1850
1851// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1852// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1853// If not specified, then the "current" versions are gathered.
1854func vndkApexBundleFactory() android.Module {
1855 bundle := newApexBundle()
1856 bundle.vndkApex = true
1857 bundle.AddProperties(&bundle.vndkProperties)
1858 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1859 ctx.AppendProperties(&struct {
1860 Compile_multilib *string
1861 }{
1862 proptools.StringPtr("both"),
1863 })
1864 })
1865 return bundle
1866}
1867
Jooyung Han31c470b2019-10-18 16:26:59 +09001868func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1869 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1870 if vndkVersion == "current" {
1871 vndkVersion = config.PlatformVndkVersion()
1872 }
1873 return vndkVersion
1874}
1875
Jiyong Park30ca9372019-02-07 16:27:23 +09001876//
1877// Defaults
1878//
1879type Defaults struct {
1880 android.ModuleBase
1881 android.DefaultsModuleBase
1882}
1883
Jiyong Park30ca9372019-02-07 16:27:23 +09001884func defaultsFactory() android.Module {
1885 return DefaultsFactory()
1886}
1887
1888func DefaultsFactory(props ...interface{}) android.Module {
1889 module := &Defaults{}
1890
1891 module.AddProperties(props...)
1892 module.AddProperties(
1893 &apexBundleProperties{},
1894 &apexTargetBundleProperties{},
1895 )
1896
1897 android.InitDefaultsModule(module)
1898 return module
1899}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001900
1901//
1902// Prebuilt APEX
1903//
1904type Prebuilt struct {
1905 android.ModuleBase
1906 prebuilt android.Prebuilt
1907
1908 properties PrebuiltProperties
1909
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001910 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001911 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001912 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001913 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001914}
1915
1916type PrebuiltProperties struct {
1917 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001918 Source string `blueprint:"mutated"`
1919 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001920
1921 Src *string
1922 Arch struct {
1923 Arm struct {
1924 Src *string
1925 }
1926 Arm64 struct {
1927 Src *string
1928 }
1929 X86 struct {
1930 Src *string
1931 }
1932 X86_64 struct {
1933 Src *string
1934 }
1935 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001936
1937 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001938 // Optional name for the installed apex. If unspecified, name of the
1939 // module is used as the file name
1940 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001941
1942 // Names of modules to be overridden. Listed modules can only be other binaries
1943 // (in Make or Soong).
1944 // This does not completely prevent installation of the overridden binaries, but if both
1945 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1946 // from PRODUCT_PACKAGES.
1947 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001948}
1949
1950func (p *Prebuilt) installable() bool {
1951 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001952}
1953
1954func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001955 // If the device is configured to use flattened APEX, force disable the prebuilt because
1956 // the prebuilt is a non-flattened one.
1957 forceDisable := ctx.Config().FlattenApex()
1958
1959 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1960 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001961 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001962
Kun Niu10c9f832019-07-29 16:28:57 -07001963 // Force disable the prebuilts when coverage is enabled.
1964 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1965 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1966
Jiyong Park50b81e52019-07-11 11:24:41 +09001967 // b/137216042 don't use prebuilts when address sanitizer is on
1968 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1969 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1970
1971 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001972 p.properties.ForceDisable = true
1973 return
1974 }
1975
Jiyong Parkc95714e2019-03-29 14:23:10 +09001976 // This is called before prebuilt_select and prebuilt_postdeps mutators
1977 // The mutators requires that src to be set correctly for each arch so that
1978 // arch variants are disabled when src is not provided for the arch.
1979 if len(ctx.MultiTargets()) != 1 {
1980 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1981 return
1982 }
1983 var src string
1984 switch ctx.MultiTargets()[0].Arch.ArchType {
1985 case android.Arm:
1986 src = String(p.properties.Arch.Arm.Src)
1987 case android.Arm64:
1988 src = String(p.properties.Arch.Arm64.Src)
1989 case android.X86:
1990 src = String(p.properties.Arch.X86.Src)
1991 case android.X86_64:
1992 src = String(p.properties.Arch.X86_64.Src)
1993 default:
1994 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1995 return
1996 }
1997 if src == "" {
1998 src = String(p.properties.Src)
1999 }
2000 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002001}
2002
Jiyong Park03b68dd2019-07-26 23:20:40 +09002003func (p *Prebuilt) isForceDisabled() bool {
2004 return p.properties.ForceDisable
2005}
2006
Colin Cross41955e82019-05-29 14:40:35 -07002007func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2008 switch tag {
2009 case "":
2010 return android.Paths{p.outputApex}, nil
2011 default:
2012 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2013 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002014}
2015
Jiyong Park4d277042019-04-23 18:00:10 +09002016func (p *Prebuilt) InstallFilename() string {
2017 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2018}
2019
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002020func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002021 if p.properties.ForceDisable {
2022 return
2023 }
2024
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002025 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002026 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002027 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002028 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002029 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2030 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2031 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002032 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2033 ctx.Build(pctx, android.BuildParams{
2034 Rule: android.Cp,
2035 Input: p.inputApex,
2036 Output: p.outputApex,
2037 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002038 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002039 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002040 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002041
2042 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002043}
2044
2045func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2046 return &p.prebuilt
2047}
2048
2049func (p *Prebuilt) Name() string {
2050 return p.prebuilt.Name(p.ModuleBase.Name())
2051}
2052
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002053func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2054 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002055 Class: "ETC",
2056 OutputFile: android.OptionalPathForPath(p.inputApex),
2057 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002058 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2059 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002060 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002061 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2062 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2063 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2064 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002065 },
2066 }
2067}
2068
2069// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2070func PrebuiltFactory() android.Module {
2071 module := &Prebuilt{}
2072 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002073 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002074 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002075 return module
2076}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002077
2078func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2079 // small helper to add symlink commands
2080 addSymlink := func(target, dir, linkName string) {
2081 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2082 link := filepath.Join(outDir, linkName)
2083 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2084 }
2085
2086 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2087 // When all hard-coded references are fixed, remove symbolic links
2088 // Note that we should keep following symlinks for older VNDKs (<=29)
2089 // Since prebuilt vndk libs still depend on system/lib/vndk path
2090 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2091 // the name of vndk apex is formatted "com.android.vndk.v" + version
2092 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2093 if ctx.Config().Android64() {
2094 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2095 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2096 }
2097 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2098 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2099 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2100 }
2101 }
2102 return
2103}