blob: 541d715553f0402218e003387218de226964b9cb [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"
24
25 "android/soong/android"
26 "android/soong/cc"
27 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080028 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090029
30 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080031 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090032 "github.com/google/blueprint/proptools"
33)
34
35var (
36 pctx = android.NewPackageContext("android/apex")
37
38 // Create a canned fs config file where all files and directories are
39 // by default set to (uid/gid/mode) = (1000/1000/0644)
40 // TODO(b/113082813) make this configurable using config.fs syntax
41 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000042 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000043 `echo '/apex_manifest.json 1000 1000 0644' >> ${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 Hane1633032019-08-01 17:41:43 +090049 injectApexDependency = pctx.StaticRule("injectApexDependency", blueprint.RuleParams{
50 Command: `rm -f $out && ${jsonmodify} $in ` +
51 `-a provideNativeLibs ${provideNativeLibs} ` +
52 `-a requireNativeLibs ${requireNativeLibs} -o $out`,
53 CommandDeps: []string{"${jsonmodify}"},
54 Description: "Inject dependency into ${out}",
55 }, "provideNativeLibs", "requireNativeLibs")
56
Jiyong Park48ca7dc2018-10-10 14:01:00 +090057 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
58 // against the binary policy using sefcontext_compiler -p <policy>.
59
60 // TODO(b/114327326): automate the generation of file_contexts
61 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
62 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010063 `(. ${out}.copy_commands) && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090064 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090065 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090066 `--file_contexts ${file_contexts} ` +
67 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080068 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090069 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090070 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
71 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000072 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Roland Levillain96cf4d42019-07-30 19:56:56 +010073 Rspfile: "${out}.copy_commands",
74 RspfileContent: "${copy_commands}",
75 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090076 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080077
Alex Light5098a612018-11-29 17:12:15 -080078 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
79 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
Roland Levillain96cf4d42019-07-30 19:56:56 +010080 `(. ${out}.copy_commands) && ` +
Alex Light5098a612018-11-29 17:12:15 -080081 `APEXER_TOOL_PATH=${tool_path} ` +
82 `${apexer} --force --manifest ${manifest} ` +
83 `--payload_type zip ` +
84 `${image_dir} ${out} `,
Roland Levillain96cf4d42019-07-30 19:56:56 +010085 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
86 Rspfile: "${out}.copy_commands",
87 RspfileContent: "${copy_commands}",
88 Description: "ZipAPEX ${image_dir} => ${out}",
Alex Light5098a612018-11-29 17:12:15 -080089 }, "tool_path", "image_dir", "copy_commands", "manifest")
90
Colin Crossa4925902018-11-16 11:36:28 -080091 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
92 blueprint.RuleParams{
93 Command: `${aapt2} convert --output-format proto $in -o $out`,
94 CommandDeps: []string{"${aapt2}"},
95 })
96
97 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090098 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000099 `apex_payload.img:apex/${abi}.img ` +
100 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +0000101 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -0800102 CommandDeps: []string{"${zip2zip}"},
103 Description: "app bundle",
104 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900105)
106
Alex Light5098a612018-11-29 17:12:15 -0800107var imageApexSuffix = ".apex"
108var zipApexSuffix = ".zipapex"
109
110var imageApexType = "image"
111var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900112
113type dependencyTag struct {
114 blueprint.BaseDependencyTag
115 name string
116}
117
118var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900119 sharedLibTag = dependencyTag{name: "sharedLib"}
120 executableTag = dependencyTag{name: "executable"}
121 javaLibTag = dependencyTag{name: "javaLib"}
122 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100123 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900124 keyTag = dependencyTag{name: "key"}
125 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +0900126 usesTag = dependencyTag{name: "uses"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900127)
128
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900129var (
130 whitelistNoApex = map[string][]string{
131 "apex_test_build_features": []string{"libbinder"},
132 "com.android.neuralnetworks": []string{"libbinder"},
133 "com.android.media": []string{"libbinder"},
134 "com.android.media.swcodec": []string{"libbinder"},
135 "test_com.android.media.swcodec": []string{"libbinder"},
136 }
137)
138
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900139func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700140 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900141 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900142 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100143 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
144 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
145 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
146 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000147 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100148 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
149 } else {
150 return pctx.HostBinToolPath(ctx, tool).String()
151 }
152 })
153 }
154 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900155 pctx.HostBinToolVariable("avbtool", "avbtool")
156 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
157 pctx.HostBinToolVariable("merge_zips", "merge_zips")
158 pctx.HostBinToolVariable("mke2fs", "mke2fs")
159 pctx.HostBinToolVariable("resize2fs", "resize2fs")
160 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
161 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800162 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900163 pctx.HostBinToolVariable("zipalign", "zipalign")
Jooyung Hane1633032019-08-01 17:41:43 +0900164 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900165
Alex Light0851b882019-02-07 13:20:53 -0800166 android.RegisterModuleType("apex", apexBundleFactory)
167 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900168 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700169 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900170
171 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
172 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700173 ctx.BottomUp("apex", apexMutator).Parallel()
Jooyung Han5c998b92019-06-27 11:30:33 +0900174 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900175 })
176}
177
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900178// Mark the direct and transitive dependencies of apex bundles so that they
179// can be built for the apex bundles.
180func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800181 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800182 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900183 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900184 depName := mctx.OtherModuleName(child)
185 // If the parent is apexBundle, this child is directly depended.
186 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800187 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800188 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
189 // non-installable apex's cannot be installed and so should not prevent libraries from being
190 // installed to the system.
191 android.UpdateApexDependency(apexBundleName, depName, directDep)
192 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900193
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900194 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900195 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900196 return true
197 } else {
198 return false
199 }
200 })
201 }
202}
203
204// Create apex variations if a module is included in APEX(s).
205func apexMutator(mctx android.BottomUpMutatorContext) {
206 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900207 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900208 } else if _, ok := mctx.Module().(*apexBundle); ok {
209 // apex bundle itself is mutated so that it and its modules have same
210 // apex variant.
211 apexBundleName := mctx.ModuleName()
212 mctx.CreateVariations(apexBundleName)
213 }
214}
Jooyung Han5c998b92019-06-27 11:30:33 +0900215func apexUsesMutator(mctx android.BottomUpMutatorContext) {
216 if ab, ok := mctx.Module().(*apexBundle); ok {
217 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
218 }
219}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900220
Alex Light9670d332019-01-29 18:07:33 -0800221type apexNativeDependencies struct {
222 // List of native libraries
223 Native_shared_libs []string
224 // List of native executables
225 Binaries []string
Roland Levillain630846d2019-06-26 12:48:34 +0100226 // List of native tests
227 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800228}
229type apexMultilibProperties struct {
230 // Native dependencies whose compile_multilib is "first"
231 First apexNativeDependencies
232
233 // Native dependencies whose compile_multilib is "both"
234 Both apexNativeDependencies
235
236 // Native dependencies whose compile_multilib is "prefer32"
237 Prefer32 apexNativeDependencies
238
239 // Native dependencies whose compile_multilib is "32"
240 Lib32 apexNativeDependencies
241
242 // Native dependencies whose compile_multilib is "64"
243 Lib64 apexNativeDependencies
244}
245
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900246type apexBundleProperties struct {
247 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000248 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800249 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900250
Jiyong Park40e26a22019-02-08 02:53:06 +0900251 // AndroidManifest.xml file used for the zip container of this APEX bundle.
252 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800253 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900254
Jiyong Park05e70dd2019-03-18 14:26:32 +0900255 // Canonical name of the APEX bundle in the manifest file.
256 // If unspecified, defaults to the value of name
257 Apex_name *string
258
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900259 // Determines the file contexts file for setting security context to each file in this APEX bundle.
260 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
261 // used.
262 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900263 File_contexts *string
264
265 // List of native shared libs that are embedded inside this APEX bundle
266 Native_shared_libs []string
267
Roland Levillain630846d2019-06-26 12:48:34 +0100268 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900269 Binaries []string
270
271 // List of java libraries that are embedded inside this APEX bundle
272 Java_libs []string
273
274 // List of prebuilt files that are embedded inside this APEX bundle
275 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900276
Roland Levillain630846d2019-06-26 12:48:34 +0100277 // List of tests that are embedded inside this APEX bundle
278 Tests []string
279
Jiyong Parkff1458f2018-10-12 21:49:38 +0900280 // Name of the apex_key module that provides the private key to sign APEX
281 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900282
Alex Light5098a612018-11-29 17:12:15 -0800283 // The type of APEX to build. Controls what the APEX payload is. Either
284 // 'image', 'zip' or 'both'. Default: 'image'.
285 Payload_type *string
286
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900287 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
288 // or an android_app_certificate module name in the form ":module".
289 Certificate *string
290
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900291 // Whether this APEX is installable to one of the partitions. Default: true.
292 Installable *bool
293
Jiyong Parkda6eb592018-12-19 17:12:36 +0900294 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
295 // Default is false.
296 Use_vendor *bool
297
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800298 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
299 Ignore_system_library_special_case *bool
300
Alex Light9670d332019-01-29 18:07:33 -0800301 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900302
Jiyong Parkf97782b2019-02-13 20:28:58 +0900303 // List of sanitizer names that this APEX is enabled for
304 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900305
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900306 PreventInstall bool `blueprint:"mutated"`
307
308 HideFromMake bool `blueprint:"mutated"`
309
Jooyung Han5c998b92019-06-27 11:30:33 +0900310 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
311 Provide_cpp_shared_libs *bool
312
313 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
314 Uses []string
Alex Light9670d332019-01-29 18:07:33 -0800315}
316
317type apexTargetBundleProperties struct {
318 Target struct {
319 // Multilib properties only for android.
320 Android struct {
321 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900322 }
Alex Light9670d332019-01-29 18:07:33 -0800323 // Multilib properties only for host.
324 Host struct {
325 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900326 }
Alex Light9670d332019-01-29 18:07:33 -0800327 // Multilib properties only for host linux_bionic.
328 Linux_bionic struct {
329 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900330 }
Alex Light9670d332019-01-29 18:07:33 -0800331 // Multilib properties only for host linux_glibc.
332 Linux_glibc struct {
333 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900334 }
335 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900336}
337
Jiyong Park8fd61922018-11-08 02:50:25 +0900338type apexFileClass int
339
340const (
341 etc apexFileClass = iota
342 nativeSharedLib
343 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900344 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800345 pyBinary
346 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900347 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100348 nativeTest
Jiyong Park8fd61922018-11-08 02:50:25 +0900349)
350
Alex Light5098a612018-11-29 17:12:15 -0800351type apexPackaging int
352
353const (
354 imageApex apexPackaging = iota
355 zipApex
356 both
357)
358
359func (a apexPackaging) image() bool {
360 switch a {
361 case imageApex, both:
362 return true
363 }
364 return false
365}
366
367func (a apexPackaging) zip() bool {
368 switch a {
369 case zipApex, both:
370 return true
371 }
372 return false
373}
374
375func (a apexPackaging) suffix() string {
376 switch a {
377 case imageApex:
378 return imageApexSuffix
379 case zipApex:
380 return zipApexSuffix
381 case both:
382 panic(fmt.Errorf("must be either zip or image"))
383 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100384 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800385 }
386}
387
388func (a apexPackaging) name() string {
389 switch a {
390 case imageApex:
391 return imageApexType
392 case zipApex:
393 return zipApexType
394 case both:
395 panic(fmt.Errorf("must be either zip or image"))
396 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100397 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800398 }
399}
400
Jiyong Park8fd61922018-11-08 02:50:25 +0900401func (class apexFileClass) NameInMake() string {
402 switch class {
403 case etc:
404 return "ETC"
405 case nativeSharedLib:
406 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800407 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900408 return "EXECUTABLES"
409 case javaSharedLib:
410 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100411 case nativeTest:
412 return "NATIVE_TESTS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900413 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100414 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900415 }
416}
417
418type apexFile struct {
419 builtFile android.Path
420 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900421 installDir string
422 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900423 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800424 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900425}
426
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900427type apexBundle struct {
428 android.ModuleBase
429 android.DefaultableModuleBase
430
Alex Light9670d332019-01-29 18:07:33 -0800431 properties apexBundleProperties
432 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900433
Alex Light5098a612018-11-29 17:12:15 -0800434 apexTypes apexPackaging
435
Colin Crossa4925902018-11-16 11:36:28 -0800436 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800437 outputFiles map[apexPackaging]android.WritablePath
Roland Levillain935639d2019-08-13 14:55:28 +0100438 flattenedOutput android.OutputPath
Colin Crossa4925902018-11-16 11:36:28 -0800439 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900440
Jiyong Park03b68dd2019-07-26 23:20:40 +0900441 prebuiltFileToDelete string
442
Jiyong Park42cca6c2019-04-01 11:15:50 +0900443 public_key_file android.Path
444 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900445
446 container_certificate_file android.Path
447 container_private_key_file android.Path
448
Jiyong Park8fd61922018-11-08 02:50:25 +0900449 // list of files to be included in this apex
450 filesInfo []apexFile
451
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900452 // list of module names that this APEX is depending on
453 externalDeps []string
454
Jiyong Park8fd61922018-11-08 02:50:25 +0900455 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800456
457 testApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900458
459 // intermediate path for apex_manifest.json
460 manifestOut android.WritablePath
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900461}
462
Jiyong Park397e55e2018-10-24 21:09:55 +0900463func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100464 native_shared_libs []string, binaries []string, tests []string,
465 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900466 // Use *FarVariation* to be able to depend on modules having
467 // conflicting variations with this module. This is required since
468 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
469 // for native shared libs.
470 ctx.AddFarVariationDependencies([]blueprint.Variation{
471 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900472 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900473 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900474 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900475 }, sharedLibTag, native_shared_libs...)
476
477 ctx.AddFarVariationDependencies([]blueprint.Variation{
478 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900479 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900480 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100481
482 ctx.AddFarVariationDependencies([]blueprint.Variation{
483 {Mutator: "arch", Variation: arch},
484 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100485 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100486 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900487}
488
Alex Light9670d332019-01-29 18:07:33 -0800489func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
490 if ctx.Os().Class == android.Device {
491 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
492 } else {
493 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
494 if ctx.Os().Bionic() {
495 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
496 } else {
497 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
498 }
499 }
500}
501
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900502func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800503
Jiyong Park397e55e2018-10-24 21:09:55 +0900504 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900505 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800506
507 a.combineProperties(ctx)
508
Jiyong Park397e55e2018-10-24 21:09:55 +0900509 has32BitTarget := false
510 for _, target := range targets {
511 if target.Arch.ArchType.Multilib == "lib32" {
512 has32BitTarget = true
513 }
514 }
515 for i, target := range targets {
516 // When multilib.* is omitted for native_shared_libs, it implies
517 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900518 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900519 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900520 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900521 {Mutator: "link", Variation: "shared"},
522 }, sharedLibTag, a.properties.Native_shared_libs...)
523
Roland Levillain630846d2019-06-26 12:48:34 +0100524 // When multilib.* is omitted for tests, it implies
525 // multilib.both.
526 ctx.AddFarVariationDependencies([]blueprint.Variation{
527 {Mutator: "arch", Variation: target.String()},
528 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100529 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100530 }, testTag, a.properties.Tests...)
531
Jiyong Park397e55e2018-10-24 21:09:55 +0900532 // Add native modules targetting both ABIs
533 addDependenciesForNativeModules(ctx,
534 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100535 a.properties.Multilib.Both.Binaries,
536 a.properties.Multilib.Both.Tests,
537 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900538 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900539
Alex Light3d673592019-01-18 14:37:31 -0800540 isPrimaryAbi := i == 0
541 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900542 // When multilib.* is omitted for binaries, it implies
543 // multilib.first.
544 ctx.AddFarVariationDependencies([]blueprint.Variation{
545 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900546 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900547 }, executableTag, a.properties.Binaries...)
548
549 // Add native modules targetting the first ABI
550 addDependenciesForNativeModules(ctx,
551 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100552 a.properties.Multilib.First.Binaries,
553 a.properties.Multilib.First.Tests,
554 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900555 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800556
557 // When multilib.* is omitted for prebuilts, it implies multilib.first.
558 ctx.AddFarVariationDependencies([]blueprint.Variation{
559 {Mutator: "arch", Variation: target.String()},
560 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900561 }
562
563 switch target.Arch.ArchType.Multilib {
564 case "lib32":
565 // Add native modules targetting 32-bit ABI
566 addDependenciesForNativeModules(ctx,
567 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100568 a.properties.Multilib.Lib32.Binaries,
569 a.properties.Multilib.Lib32.Tests,
570 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900571 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900572
573 addDependenciesForNativeModules(ctx,
574 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100575 a.properties.Multilib.Prefer32.Binaries,
576 a.properties.Multilib.Prefer32.Tests,
577 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900578 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900579 case "lib64":
580 // Add native modules targetting 64-bit ABI
581 addDependenciesForNativeModules(ctx,
582 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100583 a.properties.Multilib.Lib64.Binaries,
584 a.properties.Multilib.Lib64.Tests,
585 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900586 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900587
588 if !has32BitTarget {
589 addDependenciesForNativeModules(ctx,
590 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100591 a.properties.Multilib.Prefer32.Binaries,
592 a.properties.Multilib.Prefer32.Tests,
593 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900594 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900595 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700596
597 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
598 for _, sanitizer := range ctx.Config().SanitizeDevice() {
599 if sanitizer == "hwaddress" {
600 addDependenciesForNativeModules(ctx,
601 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100602 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700603 break
604 }
605 }
606 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900607 }
608
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900609 }
610
Jiyong Parkff1458f2018-10-12 21:49:38 +0900611 ctx.AddFarVariationDependencies([]blueprint.Variation{
612 {Mutator: "arch", Variation: "android_common"},
613 }, javaLibTag, a.properties.Java_libs...)
614
Jiyong Park23c52b02019-02-02 13:13:47 +0900615 if String(a.properties.Key) == "" {
616 ctx.ModuleErrorf("key is missing")
617 return
618 }
619 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900620
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900621 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900622 if cert != "" {
623 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900624 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900625}
626
Colin Cross0ea8ba82019-06-06 14:33:29 -0700627func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900628 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
629 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000630 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900631 }
632 return String(a.properties.Certificate)
633}
634
Colin Cross41955e82019-05-29 14:40:35 -0700635func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
636 switch tag {
637 case "":
638 if file, ok := a.outputFiles[imageApex]; ok {
639 return android.Paths{file}, nil
640 } else {
641 return nil, nil
642 }
Roland Levillain935639d2019-08-13 14:55:28 +0100643 case ".flattened":
644 if a.flattened {
645 flattenedApexPath := a.flattenedOutput
646 return android.Paths{flattenedApexPath}, nil
647 } else {
648 return nil, nil
649 }
Colin Cross41955e82019-05-29 14:40:35 -0700650 default:
651 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900652 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900653}
654
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900655func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900656 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900657}
658
Jiyong Park7c1dc612019-01-05 11:15:24 +0900659func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
660 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900661 return "vendor"
662 } else {
663 return "core"
664 }
665}
666
Jiyong Parkf97782b2019-02-13 20:28:58 +0900667func (a *apexBundle) EnableSanitizer(sanitizerName string) {
668 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
669 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
670 }
671}
672
Jiyong Park388ef3f2019-01-28 19:47:32 +0900673func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900674 if android.InList(sanitizerName, a.properties.SanitizerNames) {
675 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900676 }
677
678 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900679 globalSanitizerNames := []string{}
680 if a.Host() {
681 globalSanitizerNames = ctx.Config().SanitizeHost()
682 } else {
683 arches := ctx.Config().SanitizeDeviceArch()
684 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
685 globalSanitizerNames = ctx.Config().SanitizeDevice()
686 }
687 }
688 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900689}
690
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900691func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
692 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
693}
694
695func (a *apexBundle) PreventInstall() {
696 a.properties.PreventInstall = true
697}
698
699func (a *apexBundle) HideFromMake() {
700 a.properties.HideFromMake = true
701}
702
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800703func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900704 // Decide the APEX-local directory by the multilib of the library
705 // In the future, we may query this to the module.
706 switch cc.Arch().ArchType.Multilib {
707 case "lib32":
708 dirInApex = "lib"
709 case "lib64":
710 dirInApex = "lib64"
711 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900712 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200713 if !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900714 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200715 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
716 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900717 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800718 if handleSpecialLibs {
719 switch cc.Name() {
720 case "libc", "libm", "libdl":
721 // Special case for bionic libs. This is to prevent the bionic libs
722 // from being included in the search path /apex/com.android.apex/lib.
723 // This exclusion is required because bionic libs in the runtime APEX
724 // are available via the legacy paths /system/lib/libc.so, etc. By the
725 // init process, the bionic libs in the APEX are bind-mounted to the
726 // legacy paths and thus will be loaded into the default linker namespace.
727 // If the bionic libs are directly in /apex/com.android.apex/lib then
728 // the same libs will be again loaded to the runtime linker namespace,
729 // which will result double loading of bionic libs that isn't supported.
730 dirInApex = filepath.Join(dirInApex, "bionic")
731 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900732 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900733
734 fileToCopy = cc.OutputFile().Path()
735 return
736}
737
738func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900739 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200740 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900741 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200742 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
743 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900744 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900745 fileToCopy = cc.OutputFile().Path()
746 return
747}
748
Alex Light778127a2019-02-27 14:19:50 -0800749func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
750 dirInApex = "bin"
751 fileToCopy = py.HostToolPath().Path()
752 return
753}
754func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
755 dirInApex = "bin"
756 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
757 if err != nil {
758 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
759 return
760 }
761 fileToCopy = android.PathForOutput(ctx, s)
762 return
763}
764
Jiyong Park04480cf2019-02-06 00:16:29 +0900765func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
766 dirInApex = filepath.Join("bin", sh.SubDir())
767 fileToCopy = sh.OutputFile()
768 return
769}
770
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900771func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
772 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900773 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900774 return
775}
776
Jiyong Park9e6c2422019-08-09 20:39:45 +0900777func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
778 dirInApex = "javalib"
779 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
780 implJars := java.ImplementationJars()
781 if len(implJars) != 1 {
782 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
783 strings.Join(implJars.Strings(), ", ")))
784 }
785 fileToCopy = implJars[0]
786 return
787}
788
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900789func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
790 dirInApex = filepath.Join("etc", prebuilt.SubDir())
791 fileToCopy = prebuilt.OutputFile()
792 return
793}
794
Roland Levillain935639d2019-08-13 14:55:28 +0100795// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
796type flattenedApexContext struct {
797 android.ModuleContext
798}
799
800func (c *flattenedApexContext) InstallBypassMake() bool {
801 return true
802}
803
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900804func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900805 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900806
Alex Light5098a612018-11-29 17:12:15 -0800807 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
808 a.apexTypes = imageApex
809 } else if *a.properties.Payload_type == "zip" {
810 a.apexTypes = zipApex
811 } else if *a.properties.Payload_type == "both" {
812 a.apexTypes = both
813 } else {
814 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
815 return
816 }
817
Roland Levillain630846d2019-06-26 12:48:34 +0100818 if len(a.properties.Tests) > 0 && !a.testApex {
819 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
820 return
821 }
822
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800823 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
824
Jooyung Hane1633032019-08-01 17:41:43 +0900825 // native lib dependencies
826 var provideNativeLibs []string
827 var requireNativeLibs []string
828
Jooyung Han5c998b92019-06-27 11:30:33 +0900829 // Check if "uses" requirements are met with dependent apexBundles
830 var providedNativeSharedLibs []string
831 useVendor := proptools.Bool(a.properties.Use_vendor)
832 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
833 if ctx.OtherModuleDependencyTag(m) != usesTag {
834 return
835 }
836 otherName := ctx.OtherModuleName(m)
837 other, ok := m.(*apexBundle)
838 if !ok {
839 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
840 return
841 }
842 if proptools.Bool(other.properties.Use_vendor) != useVendor {
843 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
844 return
845 }
846 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
847 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
848 return
849 }
850 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
851 })
852
Alex Light778127a2019-02-27 14:19:50 -0800853 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100854 depTag := ctx.OtherModuleDependencyTag(child)
855 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900856 if _, ok := parent.(*apexBundle); ok {
857 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900858 switch depTag {
859 case sharedLibTag:
860 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900861 if cc.HasStubsVariants() {
862 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
863 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800864 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900865 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900866 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900867 } else {
868 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900869 }
870 case executableTag:
871 if cc, ok := child.(*cc.Module); ok {
872 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900873 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900874 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900875 } else if sh, ok := child.(*android.ShBinary); ok {
876 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
877 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800878 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
879 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
880 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
881 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
882 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
883 // NB: Since go binaries are static we don't need the module for anything here, which is
884 // good since the go tool is a blueprint.Module not an android.Module like we would
885 // normally use.
886 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900887 } else {
Alex Light778127a2019-02-27 14:19:50 -0800888 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 +0900889 }
890 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +0900891 if javaLib, ok := child.(*java.Library); ok {
892 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +0900893 if fileToCopy == nil {
894 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
895 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900896 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
897 }
898 return true
899 } else if javaLib, ok := child.(*java.Import); ok {
900 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
901 if fileToCopy == nil {
902 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
903 } else {
904 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900905 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900906 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900907 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900908 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900909 }
910 case prebuiltTag:
911 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
912 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900913 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900914 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900915 } else {
916 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
917 }
Roland Levillain630846d2019-06-26 12:48:34 +0100918 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100919 if ccTest, ok := child.(*cc.Module); ok {
920 if ccTest.IsTestPerSrcAllTestsVariation() {
921 // Multiple-output test module (where `test_per_src: true`).
922 //
923 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
924 // We do not add this variation to `filesInfo`, as it has no output;
925 // however, we do add the other variations of this module as indirect
926 // dependencies (see below).
927 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100928 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100929 // Single-output test module (where `test_per_src: false`).
930 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
931 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100932 }
Roland Levillain630846d2019-06-26 12:48:34 +0100933 return true
934 } else {
935 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
936 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900937 case keyTag:
938 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900939 a.private_key_file = key.private_key_file
940 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900941 return false
942 } else {
943 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900944 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900945 case certificateTag:
946 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900947 a.container_certificate_file = dep.Certificate.Pem
948 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900949 return false
950 } else {
951 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
952 }
Jiyong Park03b68dd2019-07-26 23:20:40 +0900953 case android.PrebuiltDepTag:
954 // If the prebuilt is force disabled, remember to delete the prebuilt file
955 // that might have been installed in the previous builds
956 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
957 a.prebuiltFileToDelete = prebuilt.InstallFilename()
958 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900959 }
960 } else {
961 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +0900962 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +0100963 // We cannot use a switch statement on `depTag` here as the checked
964 // tags used below are private (e.g. `cc.sharedDepTag`).
965 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
966 if cc, ok := child.(*cc.Module); ok {
967 if android.InList(cc.Name(), providedNativeSharedLibs) {
968 // If we're using a shared library which is provided from other APEX,
969 // don't include it in this APEX
970 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900971 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100972 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
973 // If the dependency is a stubs lib, don't include it in this APEX,
974 // but make sure that the lib is installed on the device.
975 // In case no APEX is having the lib, the lib is installed to the system
976 // partition.
977 //
978 // Always include if we are a host-apex however since those won't have any
979 // system libraries.
980 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
981 a.externalDeps = append(a.externalDeps, cc.Name())
982 }
Jooyung Hane1633032019-08-01 17:41:43 +0900983 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +0100984 // Don't track further
985 return false
986 }
987 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
988 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
989 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +0900990 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100991 } else if cc.IsTestPerSrcDepTag(depTag) {
992 if cc, ok := child.(*cc.Module); ok {
993 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
994 // Handle modules created as `test_per_src` variations of a single test module:
995 // use the name of the generated test binary (`fileToCopy`) instead of the name
996 // of the original test module (`depName`, shared by all `test_per_src`
997 // variations of that module).
998 moduleName := filepath.Base(fileToCopy.String())
999 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
1000 return true
1001 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001002 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001003 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001004 }
1005 }
1006 }
1007 return false
1008 })
1009
Jiyong Park9335a262018-12-24 11:31:58 +09001010 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001011 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001012 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1013 return
1014 }
1015
Jiyong Park8fd61922018-11-08 02:50:25 +09001016 // remove duplicates in filesInfo
1017 removeDup := func(filesInfo []apexFile) []apexFile {
1018 encountered := make(map[android.Path]bool)
1019 result := []apexFile{}
1020 for _, f := range filesInfo {
1021 if !encountered[f.builtFile] {
1022 encountered[f.builtFile] = true
1023 result = append(result, f)
1024 }
1025 }
1026 return result
1027 }
1028 filesInfo = removeDup(filesInfo)
1029
1030 // to have consistent build rules
1031 sort.Slice(filesInfo, func(i, j int) bool {
1032 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1033 })
1034
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001035 // check no_apex modules
1036 whitelist := whitelistNoApex[ctx.ModuleName()]
1037 for i := range filesInfo {
1038 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1039 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1040 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1041 }
1042 }
1043 }
1044
Jiyong Park8fd61922018-11-08 02:50:25 +09001045 // prepend the name of this APEX to the module names. These names will be the names of
1046 // modules that will be defined if the APEX is flattened.
1047 for i := range filesInfo {
1048 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1049 }
1050
Jiyong Park8fd61922018-11-08 02:50:25 +09001051 a.installDir = android.PathForModuleInstall(ctx, "apex")
1052 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001053
Jooyung Hane1633032019-08-01 17:41:43 +09001054 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1055 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1056 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1057 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1058 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1059 ctx.Build(pctx, android.BuildParams{
1060 Rule: injectApexDependency,
1061 Input: manifestSrc,
1062 Output: a.manifestOut,
1063 Args: map[string]string{
1064 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1065 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1066 },
1067 })
1068
Roland Levillain935639d2019-08-13 14:55:28 +01001069 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
1070 // reply true to `InstallBypassMake()` (thus making the call
1071 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
1072 // instead of `android.PathForOutput`) to return the correct path to the flattened
1073 // APEX (as its contents is installed by Make, not Soong).
1074 factx := flattenedApexContext{ctx}
1075 a.flattenedOutput = android.PathForModuleInstall(&factx, "apex", factx.ModuleName())
1076
Alex Light5098a612018-11-29 17:12:15 -08001077 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001078 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001079 }
1080 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001081 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001082 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001083 // in other modules. It is in AndroidMk where the selection of flattened
1084 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001085 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001086 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001087 }
1088}
1089
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001090func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001091 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001092 for _, f := range a.filesInfo {
1093 if f.module != nil {
1094 notice := f.module.NoticeFile()
1095 if notice.Valid() {
1096 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001097 }
1098 }
1099 }
1100 // append the notice file specified in the apex module itself
1101 if a.NoticeFile().Valid() {
1102 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001103 }
1104
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001105 if len(noticeFiles) == 0 {
1106 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001107 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001108
Jaewoong Jung98772792019-07-01 17:15:13 -07001109 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001110}
1111
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001112func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001113 cert := String(a.properties.Certificate)
1114 if cert != "" && android.SrcIsModule(cert) == "" {
1115 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001116 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1117 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001118 } else if cert == "" {
1119 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001120 a.container_certificate_file = pem
1121 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001122 }
1123
Alex Light5098a612018-11-29 17:12:15 -08001124 var abis []string
1125 for _, target := range ctx.MultiTargets() {
1126 if len(target.Arch.Abi) > 0 {
1127 abis = append(abis, target.Arch.Abi[0])
1128 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001129 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001130
Alex Light5098a612018-11-29 17:12:15 -08001131 abis = android.FirstUniqueStrings(abis)
1132
1133 suffix := apexType.suffix()
1134 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001135
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001136 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001137 for _, f := range a.filesInfo {
1138 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001139 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001140
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001141 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001142 for i, src := range filesToCopy {
1143 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -08001144 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001145 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1146 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001147 for _, sym := range a.filesInfo[i].symlinks {
1148 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1149 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1150 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001151 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001152 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001153 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001154
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001155 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1156 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001157
Alex Light5098a612018-11-29 17:12:15 -08001158 if apexType.image() {
1159 // files and dirs that will be created in APEX
1160 var readOnlyPaths []string
1161 var executablePaths []string // this also includes dirs
1162 for _, f := range a.filesInfo {
1163 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001164 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001165 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001166 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001167 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001168 }
Alex Light5098a612018-11-29 17:12:15 -08001169 } else {
1170 readOnlyPaths = append(readOnlyPaths, pathInApex)
1171 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001172 dir := f.installDir
1173 for !android.InList(dir, executablePaths) && dir != "" {
1174 executablePaths = append(executablePaths, dir)
1175 dir, _ = filepath.Split(dir) // move up to the parent
1176 if len(dir) > 0 {
1177 // remove trailing slash
1178 dir = dir[:len(dir)-1]
1179 }
Alex Light5098a612018-11-29 17:12:15 -08001180 }
1181 }
1182 sort.Strings(readOnlyPaths)
1183 sort.Strings(executablePaths)
1184 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1185 ctx.Build(pctx, android.BuildParams{
1186 Rule: generateFsConfig,
1187 Output: cannedFsConfig,
1188 Description: "generate fs config",
1189 Args: map[string]string{
1190 "ro_paths": strings.Join(readOnlyPaths, " "),
1191 "exec_paths": strings.Join(executablePaths, " "),
1192 },
1193 })
1194
1195 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1196 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1197 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1198 if !fileContextsOptionalPath.Valid() {
1199 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1200 return
1201 }
1202 fileContexts := fileContextsOptionalPath.Path()
1203
Jiyong Park835d82b2018-12-27 16:04:18 +09001204 optFlags := []string{}
1205
Alex Light5098a612018-11-29 17:12:15 -08001206 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001207 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1208 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001209
Jiyong Park7f67f482019-01-05 12:57:48 +09001210 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1211 if overridden {
1212 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1213 }
1214
Jiyong Park40e26a22019-02-08 02:53:06 +09001215 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001216 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001217 implicitInputs = append(implicitInputs, androidManifestFile)
1218 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1219 }
1220
Jiyong Park71b519d2019-04-18 17:25:49 +09001221 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1222 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1223 ctx.Config().UnbundledBuild() &&
1224 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1225 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1226 apiFingerprint := java.ApiFingerprintPath(ctx)
1227 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1228 implicitInputs = append(implicitInputs, apiFingerprint)
1229 }
1230 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1231
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001232 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1233 if noticeFile.Valid() {
1234 // If there's a NOTICE file, embed it as an asset file in the APEX.
1235 implicitInputs = append(implicitInputs, noticeFile.Path())
1236 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1237 }
1238
Alex Light5098a612018-11-29 17:12:15 -08001239 ctx.Build(pctx, android.BuildParams{
1240 Rule: apexRule,
1241 Implicits: implicitInputs,
1242 Output: unsignedOutputFile,
1243 Description: "apex (" + apexType.name() + ")",
1244 Args: map[string]string{
1245 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1246 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1247 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001248 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001249 "file_contexts": fileContexts.String(),
1250 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001251 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001252 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001253 },
1254 })
1255
1256 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1257 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1258 a.bundleModuleFile = bundleModuleFile
1259
1260 ctx.Build(pctx, android.BuildParams{
1261 Rule: apexProtoConvertRule,
1262 Input: unsignedOutputFile,
1263 Output: apexProtoFile,
1264 Description: "apex proto convert",
1265 })
1266
1267 ctx.Build(pctx, android.BuildParams{
1268 Rule: apexBundleRule,
1269 Input: apexProtoFile,
1270 Output: a.bundleModuleFile,
1271 Description: "apex bundle module",
1272 Args: map[string]string{
1273 "abi": strings.Join(abis, "."),
1274 },
1275 })
1276 } else {
1277 ctx.Build(pctx, android.BuildParams{
1278 Rule: zipApexRule,
1279 Implicits: implicitInputs,
1280 Output: unsignedOutputFile,
1281 Description: "apex (" + apexType.name() + ")",
1282 Args: map[string]string{
1283 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1284 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1285 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001286 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001287 },
1288 })
Colin Crossa4925902018-11-16 11:36:28 -08001289 }
Colin Crossa4925902018-11-16 11:36:28 -08001290
Alex Light5098a612018-11-29 17:12:15 -08001291 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001292 ctx.Build(pctx, android.BuildParams{
1293 Rule: java.Signapk,
1294 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001295 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001296 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001297 Implicits: []android.Path{
1298 a.container_certificate_file,
1299 a.container_private_key_file,
1300 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001301 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001302 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001303 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001304 },
1305 })
Alex Light5098a612018-11-29 17:12:15 -08001306
1307 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001308 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001309 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001310 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001311}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001312
Jiyong Park8fd61922018-11-08 02:50:25 +09001313func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001314 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001315 // 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 +09001316 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001317 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001318
Jiyong Park42cca6c2019-04-01 11:15:50 +09001319 // rename to apex_pubkey
1320 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1321 ctx.Build(pctx, android.BuildParams{
1322 Rule: android.Cp,
1323 Input: a.public_key_file,
1324 Output: copiedPubkey,
1325 })
1326 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1327
Jiyong Park23c52b02019-02-02 13:13:47 +09001328 if ctx.Config().FlattenApex() {
1329 for _, fi := range a.filesInfo {
1330 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001331 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1332 for _, sym := range fi.symlinks {
1333 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1334 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001335 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001336 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001337 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001338}
1339
1340func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001341 if a.properties.HideFromMake {
1342 return android.AndroidMkData{
1343 Disabled: true,
1344 }
1345 }
Alex Light5098a612018-11-29 17:12:15 -08001346 writers := []android.AndroidMkData{}
1347 if a.apexTypes.image() {
1348 writers = append(writers, a.androidMkForType(imageApex))
1349 }
1350 if a.apexTypes.zip() {
1351 writers = append(writers, a.androidMkForType(zipApex))
1352 }
1353 return android.AndroidMkData{
1354 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1355 for _, data := range writers {
1356 data.Custom(w, name, prefix, moduleDir, data)
1357 }
1358 }}
1359}
1360
Alex Lightf1801bc2019-02-13 11:10:07 -08001361func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001362 moduleNames := []string{}
1363
1364 for _, fi := range a.filesInfo {
1365 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1366 continue
1367 }
1368 if !android.InList(fi.moduleName, moduleNames) {
1369 moduleNames = append(moduleNames, fi.moduleName)
1370 }
1371 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1372 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1373 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001374 // /apex/<name>/{lib|framework|...}
1375 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1376 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001377 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001378 // /system/apex/<name>/{lib|framework|...}
1379 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1380 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001381 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001382 if len(fi.symlinks) > 0 {
1383 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1384 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001385
1386 if fi.module != nil && fi.module.NoticeFile().Valid() {
1387 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1388 }
Jiyong Park94427262019-02-05 23:18:47 +09001389 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001390 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001391 }
1392 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1393 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1394 if fi.module != nil {
1395 archStr := fi.module.Target().Arch.ArchType.String()
1396 host := false
1397 switch fi.module.Target().Os.Class {
1398 case android.Host:
1399 if archStr != "common" {
1400 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1401 }
1402 host = true
1403 case android.HostCross:
1404 if archStr != "common" {
1405 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1406 }
1407 host = true
1408 case android.Device:
1409 if archStr != "common" {
1410 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1411 }
1412 }
1413 if host {
1414 makeOs := fi.module.Target().Os.String()
1415 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1416 makeOs = "linux"
1417 }
1418 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1419 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1420 }
1421 }
1422 if fi.class == javaSharedLib {
1423 javaModule := fi.module.(*java.Library)
1424 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1425 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1426 // we will have foo.jar.jar
1427 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1428 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1429 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1430 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1431 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1432 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1433 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1434 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001435 if cc, ok := fi.module.(*cc.Module); ok {
1436 if cc.UnstrippedOutputFile() != nil {
1437 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1438 }
1439 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001440 if cc.CoverageOutputFile().Valid() {
1441 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1442 }
Jiyong Park94427262019-02-05 23:18:47 +09001443 }
1444 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1445 } else {
1446 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1447 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1448 }
1449 }
1450 return moduleNames
1451}
1452
Alex Light5098a612018-11-29 17:12:15 -08001453func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001454 return android.AndroidMkData{
1455 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1456 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001457 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001458 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001459 }
1460
Jiyong Park719b4462019-01-13 00:39:51 +09001461 if a.flattened && apexType.image() {
1462 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001463 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1464 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1465 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001466 if len(moduleNames) > 0 {
1467 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1468 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001469 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Roland Levillain935639d2019-08-13 14:55:28 +01001470 fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.flattenedOutput.String())
1471
Jiyong Park719b4462019-01-13 00:39:51 +09001472 } else {
Alex Light5098a612018-11-29 17:12:15 -08001473 // zip-apex is the less common type so have the name refer to the image-apex
1474 // only and use {name}.zip if you want the zip-apex
1475 if apexType == zipApex && a.apexTypes == both {
1476 name = name + ".zip"
1477 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001478 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1479 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1480 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1481 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001482 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001483 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001484 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001485 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001486 if len(moduleNames) > 0 {
1487 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1488 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001489 if len(a.externalDeps) > 0 {
1490 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1491 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001492 if a.prebuiltFileToDelete != "" {
1493 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1494 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1495 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001496 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001497
Alex Light5098a612018-11-29 17:12:15 -08001498 if apexType == imageApex {
1499 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1500 }
Jiyong Park719b4462019-01-13 00:39:51 +09001501 }
1502 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001503}
1504
Alex Light0851b882019-02-07 13:20:53 -08001505func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001506 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001507}
1508
1509func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001510 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001511}
1512
1513func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001514 module := &apexBundle{
1515 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001516 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001517 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001518 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001519 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001520 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001521 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1522 })
Alex Light5098a612018-11-29 17:12:15 -08001523 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001524 android.InitDefaultableModule(module)
1525 return module
1526}
Jiyong Park30ca9372019-02-07 16:27:23 +09001527
1528//
1529// Defaults
1530//
1531type Defaults struct {
1532 android.ModuleBase
1533 android.DefaultsModuleBase
1534}
1535
Jiyong Park30ca9372019-02-07 16:27:23 +09001536func defaultsFactory() android.Module {
1537 return DefaultsFactory()
1538}
1539
1540func DefaultsFactory(props ...interface{}) android.Module {
1541 module := &Defaults{}
1542
1543 module.AddProperties(props...)
1544 module.AddProperties(
1545 &apexBundleProperties{},
1546 &apexTargetBundleProperties{},
1547 )
1548
1549 android.InitDefaultsModule(module)
1550 return module
1551}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001552
1553//
1554// Prebuilt APEX
1555//
1556type Prebuilt struct {
1557 android.ModuleBase
1558 prebuilt android.Prebuilt
1559
1560 properties PrebuiltProperties
1561
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001562 inputApex android.Path
1563 installDir android.OutputPath
1564 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001565 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001566}
1567
1568type PrebuiltProperties struct {
1569 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001570 Source string `blueprint:"mutated"`
1571 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001572
1573 Src *string
1574 Arch struct {
1575 Arm struct {
1576 Src *string
1577 }
1578 Arm64 struct {
1579 Src *string
1580 }
1581 X86 struct {
1582 Src *string
1583 }
1584 X86_64 struct {
1585 Src *string
1586 }
1587 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001588
1589 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001590 // Optional name for the installed apex. If unspecified, name of the
1591 // module is used as the file name
1592 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001593
1594 // Names of modules to be overridden. Listed modules can only be other binaries
1595 // (in Make or Soong).
1596 // This does not completely prevent installation of the overridden binaries, but if both
1597 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1598 // from PRODUCT_PACKAGES.
1599 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001600}
1601
1602func (p *Prebuilt) installable() bool {
1603 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001604}
1605
1606func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001607 // If the device is configured to use flattened APEX, force disable the prebuilt because
1608 // the prebuilt is a non-flattened one.
1609 forceDisable := ctx.Config().FlattenApex()
1610
1611 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1612 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001613 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001614
Kun Niu10c9f832019-07-29 16:28:57 -07001615 // Force disable the prebuilts when coverage is enabled.
1616 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1617 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1618
Jiyong Park50b81e52019-07-11 11:24:41 +09001619 // b/137216042 don't use prebuilts when address sanitizer is on
1620 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1621 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1622
1623 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001624 p.properties.ForceDisable = true
1625 return
1626 }
1627
Jiyong Parkc95714e2019-03-29 14:23:10 +09001628 // This is called before prebuilt_select and prebuilt_postdeps mutators
1629 // The mutators requires that src to be set correctly for each arch so that
1630 // arch variants are disabled when src is not provided for the arch.
1631 if len(ctx.MultiTargets()) != 1 {
1632 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1633 return
1634 }
1635 var src string
1636 switch ctx.MultiTargets()[0].Arch.ArchType {
1637 case android.Arm:
1638 src = String(p.properties.Arch.Arm.Src)
1639 case android.Arm64:
1640 src = String(p.properties.Arch.Arm64.Src)
1641 case android.X86:
1642 src = String(p.properties.Arch.X86.Src)
1643 case android.X86_64:
1644 src = String(p.properties.Arch.X86_64.Src)
1645 default:
1646 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1647 return
1648 }
1649 if src == "" {
1650 src = String(p.properties.Src)
1651 }
1652 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001653}
1654
Jiyong Park03b68dd2019-07-26 23:20:40 +09001655func (p *Prebuilt) isForceDisabled() bool {
1656 return p.properties.ForceDisable
1657}
1658
Colin Cross41955e82019-05-29 14:40:35 -07001659func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1660 switch tag {
1661 case "":
1662 return android.Paths{p.outputApex}, nil
1663 default:
1664 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1665 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001666}
1667
Jiyong Park4d277042019-04-23 18:00:10 +09001668func (p *Prebuilt) InstallFilename() string {
1669 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1670}
1671
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001672func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001673 if p.properties.ForceDisable {
1674 return
1675 }
1676
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001677 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001678 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001679 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001680 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001681 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1682 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1683 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001684 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1685 ctx.Build(pctx, android.BuildParams{
1686 Rule: android.Cp,
1687 Input: p.inputApex,
1688 Output: p.outputApex,
1689 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001690 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001691 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001692 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001693}
1694
1695func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1696 return &p.prebuilt
1697}
1698
1699func (p *Prebuilt) Name() string {
1700 return p.prebuilt.Name(p.ModuleBase.Name())
1701}
1702
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001703func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1704 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001705 Class: "ETC",
1706 OutputFile: android.OptionalPathForPath(p.inputApex),
1707 Include: "$(BUILD_PREBUILT)",
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001708 AddCustomEntries: func(name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
1709 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1710 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1711 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1712 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001713 },
1714 }
1715}
1716
1717// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1718func PrebuiltFactory() android.Module {
1719 module := &Prebuilt{}
1720 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001721 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001722 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001723 return module
1724}