blob: 477f917e6cc928ba7c5b295ce0b53fd20826cdba [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
Jooyung Han344d5432019-08-23 11:17:39 +090024 "sync"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090025
26 "android/soong/android"
27 "android/soong/cc"
28 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080029 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030
31 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080032 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090033 "github.com/google/blueprint/proptools"
34)
35
36var (
37 pctx = android.NewPackageContext("android/apex")
38
39 // Create a canned fs config file where all files and directories are
40 // by default set to (uid/gid/mode) = (1000/1000/0644)
41 // TODO(b/113082813) make this configurable using config.fs syntax
42 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000043 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090044 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090045 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090047 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090048
Jooyung Hand15aa1f2019-09-27 00:38:03 +090049 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
Jooyung Hane1633032019-08-01 17:41:43 +090050 Command: `rm -f $out && ${jsonmodify} $in ` +
51 `-a provideNativeLibs ${provideNativeLibs} ` +
Jooyung Hand15aa1f2019-09-27 00:38:03 +090052 `-a requireNativeLibs ${requireNativeLibs} ` +
53 `${opt} ` +
54 `-o $out`,
Jooyung Hane1633032019-08-01 17:41:43 +090055 CommandDeps: []string{"${jsonmodify}"},
Jooyung Hand15aa1f2019-09-27 00:38:03 +090056 Description: "prepare ${out}",
57 }, "provideNativeLibs", "requireNativeLibs", "opt")
Jooyung Hane1633032019-08-01 17:41:43 +090058
Jooyung Han01a3ee22019-11-02 02:52:25 +090059 stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{
60 Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`,
61 CommandDeps: []string{"${conv_apex_manifest}"},
62 Description: "strip ${in}=>${out}",
63 })
64
65 pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{
66 Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`,
67 CommandDeps: []string{"${conv_apex_manifest}"},
68 Description: "convert ${in}=>${out}",
69 })
70
Jiyong Park48ca7dc2018-10-10 14:01:00 +090071 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
72 // against the binary policy using sefcontext_compiler -p <policy>.
73
74 // TODO(b/114327326): automate the generation of file_contexts
75 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
76 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010077 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090078 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090079 `${apexer} --force --manifest ${manifest} ` +
Jooyung Han01a3ee22019-11-02 02:52:25 +090080 `--manifest_json ${manifest_json} --manifest_json_full ${manifest_json_full} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090081 `--file_contexts ${file_contexts} ` +
82 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080083 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090084 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090085 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
86 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000087 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010088 Rspfile: "${out}.copy_commands",
89 RspfileContent: "${copy_commands}",
90 Description: "APEX ${image_dir} => ${out}",
Jooyung Han01a3ee22019-11-02 02:52:25 +090091 }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags",
92 "manifest", "manifest_json", "manifest_json_full",
93 )
Colin Crossa4925902018-11-16 11:36:28 -080094
Alex Light5098a612018-11-29 17:12:15 -080095 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
96 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010097 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080098 `APEXER_TOOL_PATH=${tool_path} ` +
Jooyung Han01a3ee22019-11-02 02:52:25 +090099 `${apexer} --force --manifest ${manifest} --manifest_json_full ${manifest_json_full} ` +
Alex Light5098a612018-11-29 17:12:15 -0800100 `--payload_type zip ` +
101 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +0100102 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
103 Rspfile: "${out}.copy_commands",
104 RspfileContent: "${copy_commands}",
105 Description: "ZipAPEX ${image_dir} => ${out}",
Jooyung Han01a3ee22019-11-02 02:52:25 +0900106 }, "tool_path", "image_dir", "copy_commands", "manifest", "manifest_json_full")
Alex Light5098a612018-11-29 17:12:15 -0800107
Colin Crossa4925902018-11-16 11:36:28 -0800108 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
109 blueprint.RuleParams{
110 Command: `${aapt2} convert --output-format proto $in -o $out`,
111 CommandDeps: []string{"${aapt2}"},
112 })
113
114 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +0900115 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +0000116 `apex_payload.img:apex/${abi}.img ` +
117 `apex_manifest.json:root/apex_manifest.json ` +
Jaewoong Jungb00c1fb2019-09-04 13:26:18 -0700118 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
119 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -0800120 CommandDeps: []string{"${zip2zip}"},
121 Description: "app bundle",
122 }, "abi")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100123
124 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
125 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
126 Rspfile: "${out}.emit_commands",
127 RspfileContent: "${emit_commands}",
128 Description: "Emit APEX image content",
129 }, "emit_commands")
130
131 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
132 Command: `diff --unchanged-group-format='' \` +
133 `--changed-group-format='%<' \` +
134 `${image_content_file} ${whitelisted_files_file} || (` +
135 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
136 ` "To fix the build run following command:" && ` +
137 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
138 `exit 1)`,
139 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
140 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900141)
142
Jooyung Han72bd2f82019-10-23 16:46:38 +0900143const (
144 imageApexSuffix = ".apex"
145 zipApexSuffix = ".zipapex"
Sundong Ahnabb64432019-10-22 13:58:29 +0900146 flattenedSuffix = ".flattened"
Alex Light5098a612018-11-29 17:12:15 -0800147
Sundong Ahnabb64432019-10-22 13:58:29 +0900148 imageApexType = "image"
149 zipApexType = "zip"
150 flattenedApexType = "flattened"
Jooyung Han72bd2f82019-10-23 16:46:38 +0900151
152 vndkApexNamePrefix = "com.android.vndk.v"
153)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900154
155type dependencyTag struct {
156 blueprint.BaseDependencyTag
157 name string
158}
159
160var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900161 sharedLibTag = dependencyTag{name: "sharedLib"}
162 executableTag = dependencyTag{name: "executable"}
163 javaLibTag = dependencyTag{name: "javaLib"}
164 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100165 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900166 keyTag = dependencyTag{name: "key"}
167 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900168 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900169 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900170)
171
172func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700173 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900174 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900175 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100176 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
177 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
178 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
179 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000180 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100181 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
182 } else {
183 return pctx.HostBinToolPath(ctx, tool).String()
184 }
185 })
186 }
187 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900188 pctx.HostBinToolVariable("avbtool", "avbtool")
189 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
190 pctx.HostBinToolVariable("merge_zips", "merge_zips")
191 pctx.HostBinToolVariable("mke2fs", "mke2fs")
192 pctx.HostBinToolVariable("resize2fs", "resize2fs")
193 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
194 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800195 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900196 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900197 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jooyung Han01a3ee22019-11-02 02:52:25 +0900198 pctx.HostBinToolVariable("conv_apex_manifest", "conv_apex_manifest")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900199
Jiyong Parkd1063c12019-07-17 20:08:41 +0900200 android.RegisterModuleType("apex", BundleFactory)
Alex Light0851b882019-02-07 13:20:53 -0800201 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jooyung Han344d5432019-08-23 11:17:39 +0900202 android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900203 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700204 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900205
Jooyung Han31c470b2019-10-18 16:26:59 +0900206 android.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900207 android.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han7a78a922019-10-08 21:59:58 +0900208
209 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
210 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
211 sort.Strings(*apexFileContextsInfos)
212 ctx.Strict("APEX_FILE_CONTEXTS_INFOS", strings.Join(*apexFileContextsInfos, " "))
213 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900214}
215
Jooyung Han31c470b2019-10-18 16:26:59 +0900216func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
217 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
218 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
219}
220
Jiyong Parkd1063c12019-07-17 20:08:41 +0900221func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
222 ctx.TopDown("apex_deps", apexDepsMutator)
223 ctx.BottomUp("apex", apexMutator).Parallel()
224 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
225 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900226}
227
Jooyung Han344d5432019-08-23 11:17:39 +0900228var (
229 vndkApexListKey = android.NewOnceKey("vndkApexList")
230 vndkApexListMutex sync.Mutex
231)
232
Jooyung Han31c470b2019-10-18 16:26:59 +0900233func vndkApexList(config android.Config) map[string]string {
Jooyung Han344d5432019-08-23 11:17:39 +0900234 return config.Once(vndkApexListKey, func() interface{} {
Jooyung Han31c470b2019-10-18 16:26:59 +0900235 return map[string]string{}
236 }).(map[string]string)
Jooyung Han344d5432019-08-23 11:17:39 +0900237}
238
Jooyung Han31c470b2019-10-18 16:26:59 +0900239func apexVndkMutator(mctx android.TopDownMutatorContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900240 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
241 if ab.IsNativeBridgeSupported() {
242 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
243 }
Jooyung Han90eee022019-10-01 20:02:42 +0900244
Jooyung Han31c470b2019-10-18 16:26:59 +0900245 vndkVersion := ab.vndkVersion(mctx.DeviceConfig())
246 // Ensure VNDK APEX mount point is formatted as com.android.vndk.v###
Jooyung Han72bd2f82019-10-23 16:46:38 +0900247 ab.properties.Apex_name = proptools.StringPtr(vndkApexNamePrefix + vndkVersion)
Jooyung Han90eee022019-10-01 20:02:42 +0900248
Jooyung Han31c470b2019-10-18 16:26:59 +0900249 // vndk_version should be unique
Jooyung Han344d5432019-08-23 11:17:39 +0900250 vndkApexListMutex.Lock()
251 defer vndkApexListMutex.Unlock()
252 vndkApexList := vndkApexList(mctx.Config())
253 if other, ok := vndkApexList[vndkVersion]; ok {
Jooyung Han31c470b2019-10-18 16:26:59 +0900254 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other)
Jooyung Han344d5432019-08-23 11:17:39 +0900255 }
Jooyung Han31c470b2019-10-18 16:26:59 +0900256 vndkApexList[vndkVersion] = mctx.ModuleName()
Jooyung Han344d5432019-08-23 11:17:39 +0900257 }
258}
259
Jooyung Han31c470b2019-10-18 16:26:59 +0900260func apexVndkDepsMutator(mctx android.BottomUpMutatorContext) {
261 if m, ok := mctx.Module().(*cc.Module); ok && cc.IsForVndkApex(mctx, m) {
262 vndkVersion := m.VndkVersion()
Jooyung Han344d5432019-08-23 11:17:39 +0900263 vndkApexList := vndkApexList(mctx.Config())
Jooyung Han31c470b2019-10-18 16:26:59 +0900264 if vndkApex, ok := vndkApexList[vndkVersion]; ok {
265 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, vndkApex)
Jooyung Han344d5432019-08-23 11:17:39 +0900266 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900267 } else if a, ok := mctx.Module().(*apexBundle); ok && a.vndkApex {
268 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
269 mctx.AddDependency(mctx.Module(), prebuiltTag, cc.VndkLibrariesTxtModules(vndkVersion)...)
Jooyung Han344d5432019-08-23 11:17:39 +0900270 }
271}
272
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900273// Mark the direct and transitive dependencies of apex bundles so that they
274// can be built for the apex bundles.
275func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800276 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800277 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900278 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900279 depName := mctx.OtherModuleName(child)
280 // If the parent is apexBundle, this child is directly depended.
281 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800282 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800283 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
284 // non-installable apex's cannot be installed and so should not prevent libraries from being
285 // installed to the system.
286 android.UpdateApexDependency(apexBundleName, depName, directDep)
287 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900288
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900289 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900290 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900291 return true
292 } else {
293 return false
294 }
295 })
296 }
297}
298
299// Create apex variations if a module is included in APEX(s).
300func apexMutator(mctx android.BottomUpMutatorContext) {
301 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900302 am.CreateApexVariations(mctx)
Jooyung Han7a78a922019-10-08 21:59:58 +0900303 } else if a, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900304 // apex bundle itself is mutated so that it and its modules have same
305 // apex variant.
306 apexBundleName := mctx.ModuleName()
307 mctx.CreateVariations(apexBundleName)
Jooyung Han7a78a922019-10-08 21:59:58 +0900308
309 // collects APEX list
310 if mctx.Device() && a.installable() {
311 addApexFileContextsInfos(mctx, a)
312 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900313 }
314}
Sundong Ahne9b55722019-09-06 17:37:42 +0900315
Jooyung Han7a78a922019-10-08 21:59:58 +0900316var (
317 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
318 apexFileContextsInfosMutex sync.Mutex
319)
320
321func apexFileContextsInfos(config android.Config) *[]string {
322 return config.Once(apexFileContextsInfosKey, func() interface{} {
323 return &[]string{}
324 }).(*[]string)
325}
326
327func addApexFileContextsInfos(ctx android.BaseModuleContext, a *apexBundle) {
328 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
329 fileContextsName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
330
331 apexFileContextsInfosMutex.Lock()
332 defer apexFileContextsInfosMutex.Unlock()
333 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
334 *apexFileContextsInfos = append(*apexFileContextsInfos, apexName+":"+fileContextsName)
335}
336
Sundong Ahne9b55722019-09-06 17:37:42 +0900337func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900338 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +0900339 var variants []string
340 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
341 case "image":
342 variants = append(variants, imageApexType, flattenedApexType)
343 case "zip":
344 variants = append(variants, zipApexType)
345 case "both":
346 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
347 default:
348 mctx.PropertyErrorf("type", "%q is not one of \"image\" or \"zip\".", *ab.properties.Payload_type)
349 return
350 }
351
352 modules := mctx.CreateLocalVariations(variants...)
353
354 for i, v := range variants {
355 switch v {
356 case imageApexType:
357 modules[i].(*apexBundle).properties.ApexType = imageApex
358 case zipApexType:
359 modules[i].(*apexBundle).properties.ApexType = zipApex
360 case flattenedApexType:
361 modules[i].(*apexBundle).properties.ApexType = flattenedApex
362 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900363 }
364 }
365}
366
Jooyung Han5c998b92019-06-27 11:30:33 +0900367func apexUsesMutator(mctx android.BottomUpMutatorContext) {
368 if ab, ok := mctx.Module().(*apexBundle); ok {
369 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
370 }
371}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900372
Jooyung Handc782442019-11-01 03:14:38 +0900373var (
374 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
375)
376
377// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
378// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
379// which may cause compatibility issues. (e.g. libbinder)
380// Even though libbinder restricts its availability via 'apex_available' property and relies on
381// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
382// to avoid similar problems.
383func useVendorWhitelist(config android.Config) []string {
384 return config.Once(useVendorWhitelistKey, func() interface{} {
385 return []string{
386 // swcodec uses "vendor" variants for smaller size
387 "com.android.media.swcodec",
388 "test_com.android.media.swcodec",
389 }
390 }).([]string)
391}
392
393// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
394// called before the first call to useVendorWhitelist()
395func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
396 config.Once(useVendorWhitelistKey, func() interface{} {
397 return whitelist
398 })
399}
400
Alex Light9670d332019-01-29 18:07:33 -0800401type apexNativeDependencies struct {
402 // List of native libraries
403 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900404
Alex Light9670d332019-01-29 18:07:33 -0800405 // List of native executables
406 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900407
Roland Levillain630846d2019-06-26 12:48:34 +0100408 // List of native tests
409 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800410}
Jooyung Han344d5432019-08-23 11:17:39 +0900411
Alex Light9670d332019-01-29 18:07:33 -0800412type apexMultilibProperties struct {
413 // Native dependencies whose compile_multilib is "first"
414 First apexNativeDependencies
415
416 // Native dependencies whose compile_multilib is "both"
417 Both apexNativeDependencies
418
419 // Native dependencies whose compile_multilib is "prefer32"
420 Prefer32 apexNativeDependencies
421
422 // Native dependencies whose compile_multilib is "32"
423 Lib32 apexNativeDependencies
424
425 // Native dependencies whose compile_multilib is "64"
426 Lib64 apexNativeDependencies
427}
428
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900429type apexBundleProperties struct {
430 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000431 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800432 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900433
Jiyong Park40e26a22019-02-08 02:53:06 +0900434 // AndroidManifest.xml file used for the zip container of this APEX bundle.
435 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800436 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900437
Roland Levillain411c5842019-09-19 16:37:20 +0100438 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
439 // device (/apex/<apex_name>).
440 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900441 Apex_name *string
442
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900443 // Determines the file contexts file for setting security context to each file in this APEX bundle.
444 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
445 // used.
446 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900447 File_contexts *string
448
449 // List of native shared libs that are embedded inside this APEX bundle
450 Native_shared_libs []string
451
Roland Levillain630846d2019-06-26 12:48:34 +0100452 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900453 Binaries []string
454
455 // List of java libraries that are embedded inside this APEX bundle
456 Java_libs []string
457
458 // List of prebuilt files that are embedded inside this APEX bundle
459 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900460
Roland Levillain630846d2019-06-26 12:48:34 +0100461 // List of tests that are embedded inside this APEX bundle
462 Tests []string
463
Jiyong Parkff1458f2018-10-12 21:49:38 +0900464 // Name of the apex_key module that provides the private key to sign APEX
465 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900466
Alex Light5098a612018-11-29 17:12:15 -0800467 // The type of APEX to build. Controls what the APEX payload is. Either
468 // 'image', 'zip' or 'both'. Default: 'image'.
469 Payload_type *string
470
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900471 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
472 // or an android_app_certificate module name in the form ":module".
473 Certificate *string
474
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900475 // Whether this APEX is installable to one of the partitions. Default: true.
476 Installable *bool
477
Jiyong Parkda6eb592018-12-19 17:12:36 +0900478 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
479 // Default is false.
480 Use_vendor *bool
481
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800482 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
483 Ignore_system_library_special_case *bool
484
Alex Light9670d332019-01-29 18:07:33 -0800485 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900486
Jiyong Parkf97782b2019-02-13 20:28:58 +0900487 // List of sanitizer names that this APEX is enabled for
488 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900489
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900490 PreventInstall bool `blueprint:"mutated"`
491
492 HideFromMake bool `blueprint:"mutated"`
493
Jooyung Han5c998b92019-06-27 11:30:33 +0900494 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
495 Provide_cpp_shared_libs *bool
496
497 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
498 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100499
500 // A txt file containing list of files that are whitelisted to be included in this APEX.
501 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900502
503 // List of APKs to package inside APEX
504 Apps []string
Sundong Ahne9b55722019-09-06 17:37:42 +0900505
Sundong Ahnabb64432019-10-22 13:58:29 +0900506 // package format of this apex variant; could be non-flattened, flattened, or zip.
507 // imageApex, zipApex or flattened
508 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900509
Jiyong Parkd1063c12019-07-17 20:08:41 +0900510 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
511 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
512 // is implied. This value affects all modules included in this APEX. In other words, they are
513 // also built with the SDKs specified here.
514 Uses_sdks []string
Alex Light9670d332019-01-29 18:07:33 -0800515}
516
517type apexTargetBundleProperties struct {
518 Target struct {
519 // Multilib properties only for android.
520 Android struct {
521 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900522 }
Jooyung Han344d5432019-08-23 11:17:39 +0900523
Alex Light9670d332019-01-29 18:07:33 -0800524 // Multilib properties only for host.
525 Host struct {
526 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900527 }
Jooyung Han344d5432019-08-23 11:17:39 +0900528
Alex Light9670d332019-01-29 18:07:33 -0800529 // Multilib properties only for host linux_bionic.
530 Linux_bionic struct {
531 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900532 }
Jooyung Han344d5432019-08-23 11:17:39 +0900533
Alex Light9670d332019-01-29 18:07:33 -0800534 // Multilib properties only for host linux_glibc.
535 Linux_glibc struct {
536 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900537 }
538 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900539}
540
Jooyung Han344d5432019-08-23 11:17:39 +0900541type apexVndkProperties struct {
542 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
543 Vndk_version *string
544}
545
Jiyong Park8fd61922018-11-08 02:50:25 +0900546type apexFileClass int
547
548const (
549 etc apexFileClass = iota
550 nativeSharedLib
551 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900552 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800553 pyBinary
554 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900555 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100556 nativeTest
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900557 app
Jiyong Park8fd61922018-11-08 02:50:25 +0900558)
559
Alex Light5098a612018-11-29 17:12:15 -0800560type apexPackaging int
561
562const (
563 imageApex apexPackaging = iota
564 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900565 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800566)
567
Sundong Ahnabb64432019-10-22 13:58:29 +0900568// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800569func (a apexPackaging) suffix() string {
570 switch a {
571 case imageApex:
572 return imageApexSuffix
573 case zipApex:
574 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800575 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100576 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800577 }
578}
579
580func (a apexPackaging) name() string {
581 switch a {
582 case imageApex:
583 return imageApexType
584 case zipApex:
585 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800586 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100587 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800588 }
589}
590
Jiyong Park8fd61922018-11-08 02:50:25 +0900591func (class apexFileClass) NameInMake() string {
592 switch class {
593 case etc:
594 return "ETC"
595 case nativeSharedLib:
596 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800597 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900598 return "EXECUTABLES"
599 case javaSharedLib:
600 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100601 case nativeTest:
602 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900603 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900604 // b/142537672 Why isn't this APP? We want to have full control over
605 // the paths and file names of the apk file under the flattend APEX.
606 // If this is set to APP, then the paths and file names are modified
607 // by the Make build system. For example, it is installed to
608 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
609 // /system/apex/<apexname>/app/<Appname> because the build system automatically
610 // appends module name (which is <apexname>.<Appname> to the path.
611 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900612 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100613 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900614 }
615}
616
617type apexFile struct {
618 builtFile android.Path
619 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900620 installDir string
621 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900622 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800623 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900624}
625
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626type apexBundle struct {
627 android.ModuleBase
628 android.DefaultableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900629 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900630
Alex Light9670d332019-01-29 18:07:33 -0800631 properties apexBundleProperties
632 targetProperties apexTargetBundleProperties
Jooyung Han344d5432019-08-23 11:17:39 +0900633 vndkProperties apexVndkProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900634
Colin Crossa4925902018-11-16 11:36:28 -0800635 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900636 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700637 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900638
Jiyong Park03b68dd2019-07-26 23:20:40 +0900639 prebuiltFileToDelete string
640
Jiyong Park42cca6c2019-04-01 11:15:50 +0900641 public_key_file android.Path
642 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900643
644 container_certificate_file android.Path
645 container_private_key_file android.Path
646
Jiyong Park8fd61922018-11-08 02:50:25 +0900647 // list of files to be included in this apex
648 filesInfo []apexFile
649
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900650 // list of module names that this APEX is depending on
651 externalDeps []string
652
Sundong Ahnabb64432019-10-22 13:58:29 +0900653 testApex bool
654 vndkApex bool
655 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900656
657 // intermediate path for apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +0900658 manifestJsonOut android.WritablePath
659 manifestJsonFullOut android.WritablePath
660 manifestPbOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900661
662 // list of commands to create symlinks for backward compatibility
663 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
664 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
665 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
666 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900667
668 // Suffix of module name in Android.mk
669 // ".flattened", ".apex", ".zipapex", or ""
670 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900671}
672
Jiyong Park397e55e2018-10-24 21:09:55 +0900673func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100674 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700675 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900676 // Use *FarVariation* to be able to depend on modules having
677 // conflicting variations with this module. This is required since
678 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
679 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700680 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900681 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900682 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900683 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700684 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900685
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700686 ctx.AddFarVariationDependencies(append(target.Variations(),
687 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
688 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100689
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700690 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100691 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100692 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700693 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900694}
695
Alex Light9670d332019-01-29 18:07:33 -0800696func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
697 if ctx.Os().Class == android.Device {
698 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
699 } else {
700 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
701 if ctx.Os().Bionic() {
702 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
703 } else {
704 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
705 }
706 }
707}
708
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900709func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900710 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
711 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
712 }
713
Jiyong Park397e55e2018-10-24 21:09:55 +0900714 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900715 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800716
717 a.combineProperties(ctx)
718
Jiyong Park397e55e2018-10-24 21:09:55 +0900719 has32BitTarget := false
720 for _, target := range targets {
721 if target.Arch.ArchType.Multilib == "lib32" {
722 has32BitTarget = true
723 }
724 }
725 for i, target := range targets {
726 // When multilib.* is omitted for native_shared_libs, it implies
727 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700728 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900729 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900730 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700731 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900732
Roland Levillain630846d2019-06-26 12:48:34 +0100733 // When multilib.* is omitted for tests, it implies
734 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700735 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100736 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100737 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700738 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100739
Jiyong Park397e55e2018-10-24 21:09:55 +0900740 // Add native modules targetting both ABIs
741 addDependenciesForNativeModules(ctx,
742 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100743 a.properties.Multilib.Both.Binaries,
744 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700745 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900746 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900747
Alex Light3d673592019-01-18 14:37:31 -0800748 isPrimaryAbi := i == 0
749 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900750 // When multilib.* is omitted for binaries, it implies
751 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700752 ctx.AddFarVariationDependencies(append(target.Variations(),
753 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
754 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900755
756 // Add native modules targetting the first ABI
757 addDependenciesForNativeModules(ctx,
758 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100759 a.properties.Multilib.First.Binaries,
760 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700761 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900762 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800763
764 // When multilib.* is omitted for prebuilts, it implies multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700765 ctx.AddFarVariationDependencies(target.Variations(),
766 prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900767 }
768
769 switch target.Arch.ArchType.Multilib {
770 case "lib32":
771 // Add native modules targetting 32-bit ABI
772 addDependenciesForNativeModules(ctx,
773 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100774 a.properties.Multilib.Lib32.Binaries,
775 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700776 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900777 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900778
779 addDependenciesForNativeModules(ctx,
780 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100781 a.properties.Multilib.Prefer32.Binaries,
782 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700783 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900784 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900785 case "lib64":
786 // Add native modules targetting 64-bit ABI
787 addDependenciesForNativeModules(ctx,
788 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100789 a.properties.Multilib.Lib64.Binaries,
790 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700791 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900792 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900793
794 if !has32BitTarget {
795 addDependenciesForNativeModules(ctx,
796 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100797 a.properties.Multilib.Prefer32.Binaries,
798 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700799 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900800 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900801 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700802
803 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
804 for _, sanitizer := range ctx.Config().SanitizeDevice() {
805 if sanitizer == "hwaddress" {
806 addDependenciesForNativeModules(ctx,
807 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700808 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700809 break
810 }
811 }
812 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900813 }
814
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900815 }
816
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700817 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
818 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900819
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700820 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
821 androidAppTag, a.properties.Apps...)
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900822
Jiyong Park23c52b02019-02-02 13:13:47 +0900823 if String(a.properties.Key) == "" {
824 ctx.ModuleErrorf("key is missing")
825 return
826 }
827 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900828
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900829 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900830 if cert != "" {
831 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900832 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900833
834 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
835 if len(a.properties.Uses_sdks) > 0 {
836 sdkRefs := []android.SdkRef{}
837 for _, str := range a.properties.Uses_sdks {
838 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
839 sdkRefs = append(sdkRefs, parsed)
840 }
841 a.BuildWithSdks(sdkRefs)
842 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900843}
844
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900845func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
846 // direct deps of an APEX bundle are all part of the APEX bundle
847 return true
848}
849
Colin Cross0ea8ba82019-06-06 14:33:29 -0700850func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900851 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
852 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000853 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900854 }
855 return String(a.properties.Certificate)
856}
857
Colin Cross41955e82019-05-29 14:40:35 -0700858func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
859 switch tag {
860 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900861 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700862 default:
863 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900864 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900865}
866
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900867func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900868 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900869}
870
Jiyong Park7c1dc612019-01-05 11:15:24 +0900871func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900872 if a.vndkApex {
873 return "vendor." + a.vndkVersion(config)
874 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900875 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Inseob Kim64c43952019-08-26 16:52:35 +0900876 return "vendor." + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900877 } else {
878 return "core"
879 }
880}
881
Jiyong Parkf97782b2019-02-13 20:28:58 +0900882func (a *apexBundle) EnableSanitizer(sanitizerName string) {
883 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
884 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
885 }
886}
887
Jiyong Park388ef3f2019-01-28 19:47:32 +0900888func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900889 if android.InList(sanitizerName, a.properties.SanitizerNames) {
890 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900891 }
892
893 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900894 globalSanitizerNames := []string{}
895 if a.Host() {
896 globalSanitizerNames = ctx.Config().SanitizeHost()
897 } else {
898 arches := ctx.Config().SanitizeDeviceArch()
899 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
900 globalSanitizerNames = ctx.Config().SanitizeDevice()
901 }
902 }
903 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900904}
905
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900906func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
907 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
908}
909
910func (a *apexBundle) PreventInstall() {
911 a.properties.PreventInstall = true
912}
913
914func (a *apexBundle) HideFromMake() {
915 a.properties.HideFromMake = true
916}
917
Martin Stjernholm279de572019-09-10 23:18:20 +0100918func getCopyManifestForNativeLibrary(ccMod *cc.Module, config android.Config, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900919 // Decide the APEX-local directory by the multilib of the library
920 // In the future, we may query this to the module.
Martin Stjernholm279de572019-09-10 23:18:20 +0100921 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900922 case "lib32":
923 dirInApex = "lib"
924 case "lib64":
925 dirInApex = "lib64"
926 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100927 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700928 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100929 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900930 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100931 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), config) {
932 // Special case for Bionic libs and other libs installed with them. This is
933 // to prevent those libs from being included in the search path
934 // /apex/com.android.runtime/${LIB}. This exclusion is required because
935 // those libs in the Runtime APEX are available via the legacy paths in
936 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
937 // to the legacy paths and thus will be loaded into the default linker
938 // namespace (aka "platform" namespace). If the libs are directly in
939 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
940 // into the runtime linker namespace, which will result in double loading of
941 // them, which isn't supported.
942 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900943 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900944
Martin Stjernholm279de572019-09-10 23:18:20 +0100945 fileToCopy = ccMod.OutputFile().Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900946 return
947}
948
949func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900950 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700951 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200952 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900953 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900954 fileToCopy = cc.OutputFile().Path()
955 return
956}
957
Alex Light778127a2019-02-27 14:19:50 -0800958func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
959 dirInApex = "bin"
960 fileToCopy = py.HostToolPath().Path()
961 return
962}
963func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
964 dirInApex = "bin"
965 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
966 if err != nil {
967 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
968 return
969 }
970 fileToCopy = android.PathForOutput(ctx, s)
971 return
972}
973
Jiyong Park04480cf2019-02-06 00:16:29 +0900974func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
975 dirInApex = filepath.Join("bin", sh.SubDir())
976 fileToCopy = sh.OutputFile()
977 return
978}
979
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900980func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
981 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900982 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900983 return
984}
985
Jiyong Park9e6c2422019-08-09 20:39:45 +0900986func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
987 dirInApex = "javalib"
988 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
989 implJars := java.ImplementationJars()
990 if len(implJars) != 1 {
991 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
992 strings.Join(implJars.Strings(), ", ")))
993 }
994 fileToCopy = implJars[0]
995 return
996}
997
Jooyung Han39edb6c2019-11-06 16:53:07 +0900998func getCopyManifestForPrebuiltEtc(prebuilt android.PrebuiltEtcModule) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900999 dirInApex = filepath.Join("etc", prebuilt.SubDir())
1000 fileToCopy = prebuilt.OutputFile()
1001 return
1002}
1003
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001004func getCopyManifestForAndroidApp(app *java.AndroidApp, pkgName string) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkf7487312019-10-17 12:54:30 +09001005 appDir := "app"
1006 if app.Privileged() {
1007 appDir = "priv-app"
1008 }
1009 dirInApex = filepath.Join(appDir, pkgName)
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001010 fileToCopy = app.OutputFile()
1011 return
1012}
1013
Dario Frenicde2a032019-10-27 00:29:22 +01001014func getCopyManifestForAndroidAppImport(app *java.AndroidAppImport, pkgName string) (fileToCopy android.Path, dirInApex string) {
1015 appDir := "app"
1016 if app.Privileged() {
1017 appDir = "priv-app"
1018 }
1019 dirInApex = filepath.Join(appDir, pkgName)
1020 fileToCopy = app.OutputFile()
1021 return
1022}
1023
Roland Levillain935639d2019-08-13 14:55:28 +01001024// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1025type flattenedApexContext struct {
1026 android.ModuleContext
1027}
1028
1029func (c *flattenedApexContext) InstallBypassMake() bool {
1030 return true
1031}
1032
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001033func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +09001034 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001035
Sundong Ahnabb64432019-10-22 13:58:29 +09001036 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1037 switch a.properties.ApexType {
1038 case imageApex:
1039 if buildFlattenedAsDefault {
1040 a.suffix = imageApexSuffix
1041 } else {
1042 a.suffix = ""
1043 a.primaryApexType = true
1044 }
1045 case zipApex:
1046 if proptools.String(a.properties.Payload_type) == "zip" {
1047 a.suffix = ""
1048 a.primaryApexType = true
1049 } else {
1050 a.suffix = zipApexSuffix
1051 }
1052 case flattenedApex:
1053 if buildFlattenedAsDefault {
1054 a.suffix = ""
1055 a.primaryApexType = true
1056 } else {
1057 a.suffix = flattenedSuffix
1058 }
Alex Light5098a612018-11-29 17:12:15 -08001059 }
1060
Roland Levillain630846d2019-06-26 12:48:34 +01001061 if len(a.properties.Tests) > 0 && !a.testApex {
1062 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1063 return
1064 }
1065
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001066 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1067
Jooyung Hane1633032019-08-01 17:41:43 +09001068 // native lib dependencies
1069 var provideNativeLibs []string
1070 var requireNativeLibs []string
1071
Jooyung Han5c998b92019-06-27 11:30:33 +09001072 // Check if "uses" requirements are met with dependent apexBundles
1073 var providedNativeSharedLibs []string
1074 useVendor := proptools.Bool(a.properties.Use_vendor)
1075 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1076 if ctx.OtherModuleDependencyTag(m) != usesTag {
1077 return
1078 }
1079 otherName := ctx.OtherModuleName(m)
1080 other, ok := m.(*apexBundle)
1081 if !ok {
1082 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
1083 return
1084 }
1085 if proptools.Bool(other.properties.Use_vendor) != useVendor {
1086 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
1087 return
1088 }
1089 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
1090 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
1091 return
1092 }
1093 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
1094 })
1095
Alex Light778127a2019-02-27 14:19:50 -08001096 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001097 depTag := ctx.OtherModuleDependencyTag(child)
1098 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001099 if _, ok := parent.(*apexBundle); ok {
1100 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001101 switch depTag {
1102 case sharedLibTag:
1103 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +09001104 if cc.HasStubsVariants() {
1105 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
1106 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001107 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +09001108 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001109 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001110 } else {
1111 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001112 }
1113 case executableTag:
1114 if cc, ok := child.(*cc.Module); ok {
1115 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +09001116 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001117 return true
Jiyong Park04480cf2019-02-06 00:16:29 +09001118 } else if sh, ok := child.(*android.ShBinary); ok {
1119 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -07001120 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, sh.Symlinks()})
Alex Light778127a2019-02-27 14:19:50 -08001121 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
1122 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
1123 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
1124 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
1125 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
1126 // NB: Since go binaries are static we don't need the module for anything here, which is
1127 // good since the go tool is a blueprint.Module not an android.Module like we would
1128 // normally use.
1129 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +09001130 } else {
Alex Light778127a2019-02-27 14:19:50 -08001131 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 +09001132 }
1133 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001134 if javaLib, ok := child.(*java.Library); ok {
1135 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +09001136 if fileToCopy == nil {
1137 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1138 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001139 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
1140 }
1141 return true
1142 } else if javaLib, ok := child.(*java.Import); ok {
1143 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
1144 if fileToCopy == nil {
1145 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1146 } else {
1147 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001148 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001149 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001150 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001151 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001152 }
1153 case prebuiltTag:
Jooyung Han39edb6c2019-11-06 16:53:07 +09001154 if prebuilt, ok := child.(android.PrebuiltEtcModule); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001155 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +09001156 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001157 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +09001158 } else {
1159 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1160 }
Roland Levillain630846d2019-06-26 12:48:34 +01001161 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001162 if ccTest, ok := child.(*cc.Module); ok {
1163 if ccTest.IsTestPerSrcAllTestsVariation() {
1164 // Multiple-output test module (where `test_per_src: true`).
1165 //
1166 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1167 // We do not add this variation to `filesInfo`, as it has no output;
1168 // however, we do add the other variations of this module as indirect
1169 // dependencies (see below).
1170 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001171 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001172 // Single-output test module (where `test_per_src: false`).
1173 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
1174 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +01001175 }
Roland Levillain630846d2019-06-26 12:48:34 +01001176 return true
1177 } else {
1178 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1179 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001180 case keyTag:
1181 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001182 a.private_key_file = key.private_key_file
1183 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001184 return false
1185 } else {
1186 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001187 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001188 case certificateTag:
1189 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001190 a.container_certificate_file = dep.Certificate.Pem
1191 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001192 return false
1193 } else {
1194 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1195 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001196 case android.PrebuiltDepTag:
1197 // If the prebuilt is force disabled, remember to delete the prebuilt file
1198 // that might have been installed in the previous builds
1199 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1200 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1201 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001202 case androidAppTag:
1203 if ap, ok := child.(*java.AndroidApp); ok {
1204 fileToCopy, dirInApex := getCopyManifestForAndroidApp(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1205 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
1206 return true
Dario Frenicde2a032019-10-27 00:29:22 +01001207 } else if ap, ok := child.(*java.AndroidAppImport); ok {
1208 fileToCopy, dirInApex := getCopyManifestForAndroidAppImport(ap, ctx.DeviceConfig().OverridePackageNameFor(depName))
1209 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, app, ap, nil})
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001210 } else {
1211 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1212 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001213 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001214 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001215 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001216 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001217 // We cannot use a switch statement on `depTag` here as the checked
1218 // tags used below are private (e.g. `cc.sharedDepTag`).
Sam Sacconee3c22f72019-11-13 15:23:05 +00001219 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) || java.IsJniDepTag(depTag) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001220 if cc, ok := child.(*cc.Module); ok {
1221 if android.InList(cc.Name(), providedNativeSharedLibs) {
1222 // If we're using a shared library which is provided from other APEX,
1223 // don't include it in this APEX
1224 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001225 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001226 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
1227 // If the dependency is a stubs lib, don't include it in this APEX,
1228 // but make sure that the lib is installed on the device.
1229 // In case no APEX is having the lib, the lib is installed to the system
1230 // partition.
1231 //
1232 // Always include if we are a host-apex however since those won't have any
1233 // system libraries.
1234 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1235 a.externalDeps = append(a.externalDeps, cc.Name())
1236 }
Jooyung Hane1633032019-08-01 17:41:43 +09001237 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001238 // Don't track further
1239 return false
1240 }
Martin Stjernholm279de572019-09-10 23:18:20 +01001241 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, ctx.Config(), handleSpecialLibs)
Roland Levillainf89cd092019-07-29 16:22:59 +01001242 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
1243 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +09001244 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001245 } else if cc.IsTestPerSrcDepTag(depTag) {
1246 if cc, ok := child.(*cc.Module); ok {
1247 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
1248 // Handle modules created as `test_per_src` variations of a single test module:
1249 // use the name of the generated test binary (`fileToCopy`) instead of the name
1250 // of the original test module (`depName`, shared by all `test_per_src`
1251 // variations of that module).
1252 moduleName := filepath.Base(fileToCopy.String())
1253 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1254 return true
1255 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001256 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001257 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001258 }
1259 }
1260 }
1261 return false
1262 })
1263
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001264 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001265 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1266 return
1267 }
1268
Jiyong Park8fd61922018-11-08 02:50:25 +09001269 // remove duplicates in filesInfo
1270 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001271 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001272 result := []apexFile{}
1273 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001274 dest := filepath.Join(f.installDir, f.builtFile.Base())
1275 if !encountered[dest] {
1276 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001277 result = append(result, f)
1278 }
1279 }
1280 return result
1281 }
1282 filesInfo = removeDup(filesInfo)
1283
1284 // to have consistent build rules
1285 sort.Slice(filesInfo, func(i, j int) bool {
1286 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1287 })
1288
Jiyong Park127b40b2019-09-30 16:04:35 +09001289 // check apex_available requirements
Jiyong Park583a2262019-10-08 20:55:38 +09001290 if !ctx.Host() {
1291 for _, fi := range filesInfo {
1292 if am, ok := fi.module.(android.ApexModule); ok {
1293 if !am.AvailableFor(ctx.ModuleName()) {
1294 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
1295 return
1296 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001297 }
1298 }
1299 }
1300
Jiyong Park8fd61922018-11-08 02:50:25 +09001301 // prepend the name of this APEX to the module names. These names will be the names of
1302 // modules that will be defined if the APEX is flattened.
1303 for i := range filesInfo {
Sundong Ahnabb64432019-10-22 13:58:29 +09001304 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + ctx.ModuleName() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001305 }
1306
Jiyong Park8fd61922018-11-08 02:50:25 +09001307 a.installDir = android.PathForModuleInstall(ctx, "apex")
1308 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001309
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001310 // prepare apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +09001311 a.buildManifest(ctx, provideNativeLibs, requireNativeLibs)
1312
1313 a.setCertificateAndPrivateKey(ctx)
1314 if a.properties.ApexType == flattenedApex {
1315 a.buildFlattenedApex(ctx)
1316 } else {
1317 a.buildUnflattenedApex(ctx)
1318 }
1319
1320 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1321 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
1322}
1323
1324func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
Jooyung Hane1633032019-08-01 17:41:43 +09001325 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001326
Jooyung Han01a3ee22019-11-02 02:52:25 +09001327 a.manifestJsonFullOut = android.PathForModuleOut(ctx, "apex_manifest_full.json")
1328
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001329 // put dependency({provide|require}NativeLibs) in apex_manifest.json
Jooyung Hane1633032019-08-01 17:41:43 +09001330 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1331 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001332
1333 // apex name can be overridden
1334 optCommands := []string{}
1335 if a.properties.Apex_name != nil {
1336 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
1337 }
1338
Jooyung Hane1633032019-08-01 17:41:43 +09001339 ctx.Build(pctx, android.BuildParams{
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001340 Rule: apexManifestRule,
Jooyung Hane1633032019-08-01 17:41:43 +09001341 Input: manifestSrc,
Jooyung Han01a3ee22019-11-02 02:52:25 +09001342 Output: a.manifestJsonFullOut,
Jooyung Hane1633032019-08-01 17:41:43 +09001343 Args: map[string]string{
1344 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1345 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001346 "opt": strings.Join(optCommands, " "),
Jooyung Hane1633032019-08-01 17:41:43 +09001347 },
1348 })
1349
Jooyung Han01a3ee22019-11-02 02:52:25 +09001350 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
1351 // prepare stripp-downed version so that APEX modules built from R+ can be installed to Q
1352 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1353 ctx.Build(pctx, android.BuildParams{
1354 Rule: stripApexManifestRule,
1355 Input: a.manifestJsonFullOut,
1356 Output: a.manifestJsonOut,
1357 })
Jooyung Han72bd2f82019-10-23 16:46:38 +09001358
Jooyung Han01a3ee22019-11-02 02:52:25 +09001359 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
1360 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
1361 ctx.Build(pctx, android.BuildParams{
1362 Rule: pbApexManifestRule,
1363 Input: a.manifestJsonFullOut,
1364 Output: a.manifestPbOut,
1365 })
Jiyong Park8fd61922018-11-08 02:50:25 +09001366}
1367
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001368func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001369 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001370 for _, f := range a.filesInfo {
1371 if f.module != nil {
1372 notice := f.module.NoticeFile()
1373 if notice.Valid() {
1374 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001375 }
1376 }
1377 }
1378 // append the notice file specified in the apex module itself
1379 if a.NoticeFile().Valid() {
1380 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001381 }
1382
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001383 if len(noticeFiles) == 0 {
1384 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001385 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001386
Jaewoong Jung98772792019-07-01 17:15:13 -07001387 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001388}
1389
Sundong Ahnabb64432019-10-22 13:58:29 +09001390func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Alex Light5098a612018-11-29 17:12:15 -08001391 var abis []string
1392 for _, target := range ctx.MultiTargets() {
1393 if len(target.Arch.Abi) > 0 {
1394 abis = append(abis, target.Arch.Abi[0])
1395 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001396 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001397
Alex Light5098a612018-11-29 17:12:15 -08001398 abis = android.FirstUniqueStrings(abis)
1399
Sundong Ahnabb64432019-10-22 13:58:29 +09001400 apexType := a.properties.ApexType
Alex Light5098a612018-11-29 17:12:15 -08001401 suffix := apexType.suffix()
1402 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001403
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001404 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001405 for _, f := range a.filesInfo {
1406 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001407 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001408
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001409 copyCommands := []string{}
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001410 emitCommands := []string{}
1411 imageContentFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-content.txt")
1412 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001413 for i, src := range filesToCopy {
1414 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001415 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
Alex Light5098a612018-11-29 17:12:15 -08001416 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001417 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1418 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001419 for _, sym := range a.filesInfo[i].symlinks {
1420 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1421 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1422 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001423 }
Dario Frenie4235822019-10-28 14:49:27 +00001424 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
1425
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001426 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Han01a3ee22019-11-02 02:52:25 +09001427 implicitInputs = append(implicitInputs, a.manifestPbOut, a.manifestJsonFullOut, a.manifestJsonOut)
Alex Light5098a612018-11-29 17:12:15 -08001428
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001429 if a.properties.Whitelisted_files != nil {
1430 ctx.Build(pctx, android.BuildParams{
1431 Rule: emitApexContentRule,
1432 Implicits: implicitInputs,
1433 Output: imageContentFile,
1434 Description: "emit apex image content",
1435 Args: map[string]string{
1436 "emit_commands": strings.Join(emitCommands, " && "),
1437 },
1438 })
1439 implicitInputs = append(implicitInputs, imageContentFile)
1440 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
1441
Nikita Ioffe1acf6f92019-09-04 11:53:14 +01001442 phonyOutput := android.PathForModuleOut(ctx, ctx.ModuleName()+"-diff-phony-output")
Nikita Ioffe5d5ae762019-08-31 14:38:05 +01001443 ctx.Build(pctx, android.BuildParams{
1444 Rule: diffApexContentRule,
1445 Implicits: implicitInputs,
1446 Output: phonyOutput,
1447 Description: "diff apex image content",
1448 Args: map[string]string{
1449 "whitelisted_files_file": whitelistedFilesFile.String(),
1450 "image_content_file": imageContentFile.String(),
1451 "apex_module_name": ctx.ModuleName(),
1452 },
1453 })
1454
1455 implicitInputs = append(implicitInputs, phonyOutput)
1456 }
1457
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001458 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1459 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001460
Sundong Ahnabb64432019-10-22 13:58:29 +09001461 if apexType == imageApex {
Alex Light5098a612018-11-29 17:12:15 -08001462 // files and dirs that will be created in APEX
Jooyung Han01a3ee22019-11-02 02:52:25 +09001463 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
Alex Light5098a612018-11-29 17:12:15 -08001464 var executablePaths []string // this also includes dirs
1465 for _, f := range a.filesInfo {
1466 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001467 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001468 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001469 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001470 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001471 }
Alex Light5098a612018-11-29 17:12:15 -08001472 } else {
1473 readOnlyPaths = append(readOnlyPaths, pathInApex)
1474 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001475 dir := f.installDir
1476 for !android.InList(dir, executablePaths) && dir != "" {
1477 executablePaths = append(executablePaths, dir)
1478 dir, _ = filepath.Split(dir) // move up to the parent
1479 if len(dir) > 0 {
1480 // remove trailing slash
1481 dir = dir[:len(dir)-1]
1482 }
Alex Light5098a612018-11-29 17:12:15 -08001483 }
1484 }
1485 sort.Strings(readOnlyPaths)
1486 sort.Strings(executablePaths)
1487 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1488 ctx.Build(pctx, android.BuildParams{
1489 Rule: generateFsConfig,
1490 Output: cannedFsConfig,
1491 Description: "generate fs config",
1492 Args: map[string]string{
1493 "ro_paths": strings.Join(readOnlyPaths, " "),
1494 "exec_paths": strings.Join(executablePaths, " "),
1495 },
1496 })
1497
1498 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1499 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1500 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1501 if !fileContextsOptionalPath.Valid() {
1502 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1503 return
1504 }
1505 fileContexts := fileContextsOptionalPath.Path()
1506
Jiyong Park835d82b2018-12-27 16:04:18 +09001507 optFlags := []string{}
1508
Alex Light5098a612018-11-29 17:12:15 -08001509 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001510 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1511 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001512
Jiyong Park7f67f482019-01-05 12:57:48 +09001513 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1514 if overridden {
1515 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1516 }
1517
Jiyong Park40e26a22019-02-08 02:53:06 +09001518 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001519 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001520 implicitInputs = append(implicitInputs, androidManifestFile)
1521 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1522 }
1523
Jiyong Park71b519d2019-04-18 17:25:49 +09001524 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1525 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1526 ctx.Config().UnbundledBuild() &&
1527 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1528 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1529 apiFingerprint := java.ApiFingerprintPath(ctx)
1530 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1531 implicitInputs = append(implicitInputs, apiFingerprint)
1532 }
1533 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1534
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001535 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1536 if noticeFile.Valid() {
1537 // If there's a NOTICE file, embed it as an asset file in the APEX.
1538 implicitInputs = append(implicitInputs, noticeFile.Path())
1539 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1540 }
1541
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001542 if !ctx.Config().UnbundledBuild() && a.installable() {
1543 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1544 // don't need hashtree for activation. Therefore, by removing hashtree from
1545 // apex bundle (filesystem image in it, to be specific), we can save storage.
1546 optFlags = append(optFlags, "--no_hashtree")
1547 }
1548
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001549 if a.properties.Apex_name != nil {
1550 // If apex_name is set, apexer can skip checking if key name matches with apex name.
1551 // Note that apex_manifest is also mended.
1552 optFlags = append(optFlags, "--do_not_check_keyname")
1553 }
1554
Alex Light5098a612018-11-29 17:12:15 -08001555 ctx.Build(pctx, android.BuildParams{
1556 Rule: apexRule,
1557 Implicits: implicitInputs,
1558 Output: unsignedOutputFile,
1559 Description: "apex (" + apexType.name() + ")",
1560 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001561 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1562 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1563 "copy_commands": strings.Join(copyCommands, " && "),
1564 "manifest_json_full": a.manifestJsonFullOut.String(),
1565 "manifest_json": a.manifestJsonOut.String(),
1566 "manifest": a.manifestPbOut.String(),
1567 "file_contexts": fileContexts.String(),
1568 "canned_fs_config": cannedFsConfig.String(),
1569 "key": a.private_key_file.String(),
1570 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001571 },
1572 })
1573
1574 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1575 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1576 a.bundleModuleFile = bundleModuleFile
1577
1578 ctx.Build(pctx, android.BuildParams{
1579 Rule: apexProtoConvertRule,
1580 Input: unsignedOutputFile,
1581 Output: apexProtoFile,
1582 Description: "apex proto convert",
1583 })
1584
1585 ctx.Build(pctx, android.BuildParams{
1586 Rule: apexBundleRule,
1587 Input: apexProtoFile,
1588 Output: a.bundleModuleFile,
1589 Description: "apex bundle module",
1590 Args: map[string]string{
1591 "abi": strings.Join(abis, "."),
1592 },
1593 })
1594 } else {
1595 ctx.Build(pctx, android.BuildParams{
1596 Rule: zipApexRule,
1597 Implicits: implicitInputs,
1598 Output: unsignedOutputFile,
1599 Description: "apex (" + apexType.name() + ")",
1600 Args: map[string]string{
Jooyung Han01a3ee22019-11-02 02:52:25 +09001601 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1602 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1603 "copy_commands": strings.Join(copyCommands, " && "),
1604 "manifest": a.manifestPbOut.String(),
1605 "manifest_json_full": a.manifestJsonFullOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001606 },
1607 })
Colin Crossa4925902018-11-16 11:36:28 -08001608 }
Colin Crossa4925902018-11-16 11:36:28 -08001609
Sundong Ahnabb64432019-10-22 13:58:29 +09001610 a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001611 ctx.Build(pctx, android.BuildParams{
1612 Rule: java.Signapk,
1613 Description: "signapk",
Sundong Ahnabb64432019-10-22 13:58:29 +09001614 Output: a.outputFile,
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001615 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001616 Implicits: []android.Path{
1617 a.container_certificate_file,
1618 a.container_private_key_file,
1619 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001620 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001621 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001622 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001623 },
1624 })
Alex Light5098a612018-11-29 17:12:15 -08001625
1626 // Install to $OUT/soong/{target,host}/.../apex
Sundong Ahnabb64432019-10-22 13:58:29 +09001627 if a.installable() {
1628 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFile)
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001629 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001630 a.buildFilesInfo(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001631}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001632
Jiyong Park8fd61922018-11-08 02:50:25 +09001633func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001634 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1635 // reply true to `InstallBypassMake()` (thus making the call
1636 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1637 // instead of `android.PathForOutput`) to return the correct path to the flattened
1638 // APEX (as its contents is installed by Make, not Soong).
1639 factx := flattenedApexContext{ctx}
1640 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
1641 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
1642
1643 a.buildFilesInfo(ctx)
1644}
1645
1646func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
1647 cert := String(a.properties.Certificate)
1648 if cert != "" && android.SrcIsModule(cert) == "" {
1649 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
1650 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1651 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
1652 } else if cert == "" {
1653 pem, key := ctx.Config().DefaultAppCertificate(ctx)
1654 a.container_certificate_file = pem
1655 a.container_private_key_file = key
1656 }
1657}
1658
1659func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001660 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001661 // 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 +09001662 // with other ordinary files.
Jooyung Han01a3ee22019-11-02 02:52:25 +09001663 a.filesInfo = append(a.filesInfo, apexFile{a.manifestJsonOut, "apex_manifest.json." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
1664 a.filesInfo = append(a.filesInfo, apexFile{a.manifestPbOut, "apex_manifest.pb." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001665
Jiyong Park42cca6c2019-04-01 11:15:50 +09001666 // rename to apex_pubkey
1667 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1668 ctx.Build(pctx, android.BuildParams{
1669 Rule: android.Cp,
1670 Input: a.public_key_file,
1671 Output: copiedPubkey,
1672 })
Sundong Ahnabb64432019-10-22 13:58:29 +09001673 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, "apex_pubkey." + ctx.ModuleName() + a.suffix, ".", etc, nil, nil})
Jiyong Park42cca6c2019-04-01 11:15:50 +09001674
Sundong Ahnabb64432019-10-22 13:58:29 +09001675 if a.properties.ApexType == flattenedApex {
Jooyung Han7a78a922019-10-08 21:59:58 +09001676 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
Jiyong Park23c52b02019-02-02 13:13:47 +09001677 for _, fi := range a.filesInfo {
Jooyung Han7a78a922019-10-08 21:59:58 +09001678 dir := filepath.Join("apex", apexName, fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001679 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1680 for _, sym := range fi.symlinks {
1681 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1682 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001683 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001684 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001685 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001686}
1687
1688func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001689 if a.properties.HideFromMake {
1690 return android.AndroidMkData{
1691 Disabled: true,
1692 }
1693 }
Alex Light5098a612018-11-29 17:12:15 -08001694 writers := []android.AndroidMkData{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001695 writers = append(writers, a.androidMkForType())
Alex Light5098a612018-11-29 17:12:15 -08001696 return android.AndroidMkData{
1697 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1698 for _, data := range writers {
1699 data.Custom(w, name, prefix, moduleDir, data)
1700 }
1701 }}
1702}
1703
Sundong Ahnabb64432019-10-22 13:58:29 +09001704func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001705 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001706 apexType := a.properties.ApexType
1707 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
1708 // to install symbol files in $(PRODUCT_OUT}/apex.
1709 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
1710 if !a.primaryApexType && apexType != flattenedApex {
1711 return moduleNames
1712 }
Jiyong Park94427262019-02-05 23:18:47 +09001713
1714 for _, fi := range a.filesInfo {
1715 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1716 continue
1717 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001718
1719 if !android.InList(fi.moduleName, moduleNames) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001720 moduleNames = append(moduleNames, fi.moduleName)
Sundong Ahne9b55722019-09-06 17:37:42 +09001721 }
1722
Jiyong Park94427262019-02-05 23:18:47 +09001723 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1724 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001725 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Roland Levillain411c5842019-09-19 16:37:20 +01001726 // /apex/<apex_name>/{lib|framework|...}
Jooyung Han7a78a922019-10-08 21:59:58 +09001727 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001728 if apexType == flattenedApex {
Jiyong Park94427262019-02-05 23:18:47 +09001729 // /system/apex/<name>/{lib|framework|...}
Colin Crossff6c33d2019-10-02 16:01:35 -07001730 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jooyung Han7a78a922019-10-08 21:59:58 +09001731 apexName, fi.installDir))
Sundong Ahnabb64432019-10-22 13:58:29 +09001732 if a.primaryApexType {
Sundong Ahne9b55722019-09-06 17:37:42 +09001733 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
1734 }
Alex Lightf4857cf2019-02-22 13:00:04 -08001735 if len(fi.symlinks) > 0 {
1736 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1737 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001738
1739 if fi.module != nil && fi.module.NoticeFile().Valid() {
1740 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1741 }
Jiyong Park94427262019-02-05 23:18:47 +09001742 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001743 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001744 }
1745 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1746 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1747 if fi.module != nil {
1748 archStr := fi.module.Target().Arch.ArchType.String()
1749 host := false
1750 switch fi.module.Target().Os.Class {
1751 case android.Host:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001752 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001753 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1754 }
1755 host = true
1756 case android.HostCross:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001757 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001758 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1759 }
1760 host = true
1761 case android.Device:
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001762 if fi.module.Target().Arch.ArchType != android.Common {
Jiyong Park94427262019-02-05 23:18:47 +09001763 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1764 }
1765 }
1766 if host {
1767 makeOs := fi.module.Target().Os.String()
1768 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1769 makeOs = "linux"
1770 }
1771 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1772 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1773 }
1774 }
1775 if fi.class == javaSharedLib {
1776 javaModule := fi.module.(*java.Library)
1777 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1778 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1779 // we will have foo.jar.jar
1780 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1781 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1782 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1783 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1784 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1785 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Logan Chien0342c582019-09-10 09:08:24 -07001786 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
Jiyong Park94427262019-02-05 23:18:47 +09001787 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001788 if cc, ok := fi.module.(*cc.Module); ok {
1789 if cc.UnstrippedOutputFile() != nil {
1790 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1791 }
1792 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001793 if cc.CoverageOutputFile().Valid() {
1794 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1795 }
Jiyong Park94427262019-02-05 23:18:47 +09001796 }
1797 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1798 } else {
1799 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jooyung Han72bd2f82019-10-23 16:46:38 +09001800 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Jooyung Han01a3ee22019-11-02 02:52:25 +09001801 if a.primaryApexType && fi.builtFile == a.manifestPbOut && len(a.compatSymlinks) > 0 {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001802 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
1803 }
Jiyong Park94427262019-02-05 23:18:47 +09001804 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1805 }
1806 }
1807 return moduleNames
1808}
1809
Sundong Ahnabb64432019-10-22 13:58:29 +09001810func (a *apexBundle) androidMkForType() android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001811 return android.AndroidMkData{
1812 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1813 moduleNames := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001814 apexType := a.properties.ApexType
Jiyong Park94427262019-02-05 23:18:47 +09001815 if a.installable() {
Jooyung Han7a78a922019-10-08 21:59:58 +09001816 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Sundong Ahnabb64432019-10-22 13:58:29 +09001817 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001818 }
1819
Sundong Ahnabb64432019-10-22 13:58:29 +09001820 if apexType == flattenedApex {
Jiyong Park719b4462019-01-13 00:39:51 +09001821 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001822 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1823 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001824 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park94427262019-02-05 23:18:47 +09001825 if len(moduleNames) > 0 {
1826 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1827 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001828 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Sundong Ahnabb64432019-10-22 13:58:29 +09001829 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String())
Roland Levillain935639d2019-08-13 14:55:28 +01001830
Sundong Ahnabb64432019-10-22 13:58:29 +09001831 } else {
Jiyong Park8fd61922018-11-08 02:50:25 +09001832 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1833 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Sundong Ahnabb64432019-10-22 13:58:29 +09001834 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
Jiyong Park8fd61922018-11-08 02:50:25 +09001835 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Sundong Ahnabb64432019-10-22 13:58:29 +09001836 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
Colin Crossff6c33d2019-10-02 16:01:35 -07001837 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
Colin Cross189ff982019-01-02 22:32:27 -08001838 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001839 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001840 if len(moduleNames) > 0 {
1841 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1842 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001843 if len(a.externalDeps) > 0 {
1844 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1845 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001846 var postInstallCommands []string
Jiyong Park03b68dd2019-07-26 23:20:40 +09001847 if a.prebuiltFileToDelete != "" {
Jooyung Han72bd2f82019-10-23 16:46:38 +09001848 postInstallCommands = append(postInstallCommands, "rm -rf "+
Colin Crossff6c33d2019-10-02 16:01:35 -07001849 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
Jiyong Park03b68dd2019-07-26 23:20:40 +09001850 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09001851 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
1852 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
1853 if len(postInstallCommands) > 0 {
1854 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
1855 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001856 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001857
Alex Light5098a612018-11-29 17:12:15 -08001858 if apexType == imageApex {
1859 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1860 }
Jiyong Park719b4462019-01-13 00:39:51 +09001861 }
1862 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001863}
1864
Jooyung Han344d5432019-08-23 11:17:39 +09001865func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001866 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001867 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001868 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001869 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001870 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1871 })
Alex Light5098a612018-11-29 17:12:15 -08001872 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001873 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001874 android.InitSdkAwareModule(module)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001875 return module
1876}
Jiyong Park30ca9372019-02-07 16:27:23 +09001877
Jooyung Han344d5432019-08-23 11:17:39 +09001878func ApexBundleFactory(testApex bool) android.Module {
1879 bundle := newApexBundle()
1880 bundle.testApex = testApex
1881 return bundle
1882}
1883
1884func testApexBundleFactory() android.Module {
1885 bundle := newApexBundle()
1886 bundle.testApex = true
1887 return bundle
1888}
1889
Jiyong Parkd1063c12019-07-17 20:08:41 +09001890func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001891 return newApexBundle()
1892}
1893
1894// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
1895// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
1896// If not specified, then the "current" versions are gathered.
1897func vndkApexBundleFactory() android.Module {
1898 bundle := newApexBundle()
1899 bundle.vndkApex = true
1900 bundle.AddProperties(&bundle.vndkProperties)
1901 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
1902 ctx.AppendProperties(&struct {
1903 Compile_multilib *string
1904 }{
1905 proptools.StringPtr("both"),
1906 })
1907 })
1908 return bundle
1909}
1910
Jooyung Han31c470b2019-10-18 16:26:59 +09001911func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
1912 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
1913 if vndkVersion == "current" {
1914 vndkVersion = config.PlatformVndkVersion()
1915 }
1916 return vndkVersion
1917}
1918
Jiyong Park30ca9372019-02-07 16:27:23 +09001919//
1920// Defaults
1921//
1922type Defaults struct {
1923 android.ModuleBase
1924 android.DefaultsModuleBase
1925}
1926
Jiyong Park30ca9372019-02-07 16:27:23 +09001927func defaultsFactory() android.Module {
1928 return DefaultsFactory()
1929}
1930
1931func DefaultsFactory(props ...interface{}) android.Module {
1932 module := &Defaults{}
1933
1934 module.AddProperties(props...)
1935 module.AddProperties(
1936 &apexBundleProperties{},
1937 &apexTargetBundleProperties{},
1938 )
1939
1940 android.InitDefaultsModule(module)
1941 return module
1942}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001943
1944//
1945// Prebuilt APEX
1946//
1947type Prebuilt struct {
1948 android.ModuleBase
1949 prebuilt android.Prebuilt
1950
1951 properties PrebuiltProperties
1952
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001953 inputApex android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001954 installDir android.InstallPath
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001955 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001956 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001957}
1958
1959type PrebuiltProperties struct {
1960 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001961 Source string `blueprint:"mutated"`
1962 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001963
1964 Src *string
1965 Arch struct {
1966 Arm struct {
1967 Src *string
1968 }
1969 Arm64 struct {
1970 Src *string
1971 }
1972 X86 struct {
1973 Src *string
1974 }
1975 X86_64 struct {
1976 Src *string
1977 }
1978 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001979
1980 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001981 // Optional name for the installed apex. If unspecified, name of the
1982 // module is used as the file name
1983 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001984
1985 // Names of modules to be overridden. Listed modules can only be other binaries
1986 // (in Make or Soong).
1987 // This does not completely prevent installation of the overridden binaries, but if both
1988 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1989 // from PRODUCT_PACKAGES.
1990 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001991}
1992
1993func (p *Prebuilt) installable() bool {
1994 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001995}
1996
1997func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001998 // If the device is configured to use flattened APEX, force disable the prebuilt because
1999 // the prebuilt is a non-flattened one.
2000 forceDisable := ctx.Config().FlattenApex()
2001
2002 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
2003 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09002004 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09002005
Kun Niu10c9f832019-07-29 16:28:57 -07002006 // Force disable the prebuilts when coverage is enabled.
2007 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
2008 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
2009
Jiyong Park50b81e52019-07-11 11:24:41 +09002010 // b/137216042 don't use prebuilts when address sanitizer is on
2011 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
2012 android.InList("hwaddress", ctx.Config().SanitizeDevice())
2013
2014 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09002015 p.properties.ForceDisable = true
2016 return
2017 }
2018
Jiyong Parkc95714e2019-03-29 14:23:10 +09002019 // This is called before prebuilt_select and prebuilt_postdeps mutators
2020 // The mutators requires that src to be set correctly for each arch so that
2021 // arch variants are disabled when src is not provided for the arch.
2022 if len(ctx.MultiTargets()) != 1 {
2023 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
2024 return
2025 }
2026 var src string
2027 switch ctx.MultiTargets()[0].Arch.ArchType {
2028 case android.Arm:
2029 src = String(p.properties.Arch.Arm.Src)
2030 case android.Arm64:
2031 src = String(p.properties.Arch.Arm64.Src)
2032 case android.X86:
2033 src = String(p.properties.Arch.X86.Src)
2034 case android.X86_64:
2035 src = String(p.properties.Arch.X86_64.Src)
2036 default:
2037 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
2038 return
2039 }
2040 if src == "" {
2041 src = String(p.properties.Src)
2042 }
2043 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002044}
2045
Jiyong Park03b68dd2019-07-26 23:20:40 +09002046func (p *Prebuilt) isForceDisabled() bool {
2047 return p.properties.ForceDisable
2048}
2049
Colin Cross41955e82019-05-29 14:40:35 -07002050func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
2051 switch tag {
2052 case "":
2053 return android.Paths{p.outputApex}, nil
2054 default:
2055 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2056 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002057}
2058
Jiyong Park4d277042019-04-23 18:00:10 +09002059func (p *Prebuilt) InstallFilename() string {
2060 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
2061}
2062
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002063func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09002064 if p.properties.ForceDisable {
2065 return
2066 }
2067
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002068 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09002069 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002070 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09002071 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002072 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
2073 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
2074 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01002075 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
2076 ctx.Build(pctx, android.BuildParams{
2077 Rule: android.Cp,
2078 Input: p.inputApex,
2079 Output: p.outputApex,
2080 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002081 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002082 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01002083 }
Jooyung Han72bd2f82019-10-23 16:46:38 +09002084
2085 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002086}
2087
2088func (p *Prebuilt) Prebuilt() *android.Prebuilt {
2089 return &p.prebuilt
2090}
2091
2092func (p *Prebuilt) Name() string {
2093 return p.prebuilt.Name(p.ModuleBase.Name())
2094}
2095
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002096func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
2097 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002098 Class: "ETC",
2099 OutputFile: android.OptionalPathForPath(p.inputApex),
2100 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002101 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2102 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -07002103 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07002104 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
2105 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
2106 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
2107 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002108 },
2109 }
2110}
2111
2112// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
2113func PrebuiltFactory() android.Module {
2114 module := &Prebuilt{}
2115 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07002116 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09002117 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002118 return module
2119}
Jooyung Han72bd2f82019-10-23 16:46:38 +09002120
2121func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
2122 // small helper to add symlink commands
2123 addSymlink := func(target, dir, linkName string) {
2124 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
2125 link := filepath.Join(outDir, linkName)
2126 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
2127 }
2128
2129 // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
2130 // When all hard-coded references are fixed, remove symbolic links
2131 // Note that we should keep following symlinks for older VNDKs (<=29)
2132 // Since prebuilt vndk libs still depend on system/lib/vndk path
2133 if strings.HasPrefix(apexName, vndkApexNamePrefix) {
2134 // the name of vndk apex is formatted "com.android.vndk.v" + version
2135 vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix)
2136 if ctx.Config().Android64() {
2137 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
2138 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
2139 }
2140 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
2141 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
2142 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
2143 }
2144 }
2145 return
2146}