blob: 832188d81d2ef91f1d2273db7ce4ccdf4bc4faf1 [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
Colin Crossa4925902018-11-16 11:36:28 -0800438 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900439
Jiyong Park03b68dd2019-07-26 23:20:40 +0900440 prebuiltFileToDelete string
441
Jiyong Park42cca6c2019-04-01 11:15:50 +0900442 public_key_file android.Path
443 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900444
445 container_certificate_file android.Path
446 container_private_key_file android.Path
447
Jiyong Park8fd61922018-11-08 02:50:25 +0900448 // list of files to be included in this apex
449 filesInfo []apexFile
450
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900451 // list of module names that this APEX is depending on
452 externalDeps []string
453
Jiyong Park8fd61922018-11-08 02:50:25 +0900454 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800455
456 testApex bool
Jooyung Hane1633032019-08-01 17:41:43 +0900457
458 // intermediate path for apex_manifest.json
459 manifestOut android.WritablePath
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900460}
461
Jiyong Park397e55e2018-10-24 21:09:55 +0900462func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100463 native_shared_libs []string, binaries []string, tests []string,
464 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900465 // Use *FarVariation* to be able to depend on modules having
466 // conflicting variations with this module. This is required since
467 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
468 // for native shared libs.
469 ctx.AddFarVariationDependencies([]blueprint.Variation{
470 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900471 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900472 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900473 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900474 }, sharedLibTag, native_shared_libs...)
475
476 ctx.AddFarVariationDependencies([]blueprint.Variation{
477 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900478 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900479 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100480
481 ctx.AddFarVariationDependencies([]blueprint.Variation{
482 {Mutator: "arch", Variation: arch},
483 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100484 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100485 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900486}
487
Alex Light9670d332019-01-29 18:07:33 -0800488func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
489 if ctx.Os().Class == android.Device {
490 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
491 } else {
492 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
493 if ctx.Os().Bionic() {
494 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
495 } else {
496 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
497 }
498 }
499}
500
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900501func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800502
Jiyong Park397e55e2018-10-24 21:09:55 +0900503 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900504 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800505
506 a.combineProperties(ctx)
507
Jiyong Park397e55e2018-10-24 21:09:55 +0900508 has32BitTarget := false
509 for _, target := range targets {
510 if target.Arch.ArchType.Multilib == "lib32" {
511 has32BitTarget = true
512 }
513 }
514 for i, target := range targets {
515 // When multilib.* is omitted for native_shared_libs, it implies
516 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900517 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900518 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900519 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900520 {Mutator: "link", Variation: "shared"},
521 }, sharedLibTag, a.properties.Native_shared_libs...)
522
Roland Levillain630846d2019-06-26 12:48:34 +0100523 // When multilib.* is omitted for tests, it implies
524 // multilib.both.
525 ctx.AddFarVariationDependencies([]blueprint.Variation{
526 {Mutator: "arch", Variation: target.String()},
527 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100528 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Roland Levillain630846d2019-06-26 12:48:34 +0100529 }, testTag, a.properties.Tests...)
530
Jiyong Park397e55e2018-10-24 21:09:55 +0900531 // Add native modules targetting both ABIs
532 addDependenciesForNativeModules(ctx,
533 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100534 a.properties.Multilib.Both.Binaries,
535 a.properties.Multilib.Both.Tests,
536 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900537 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900538
Alex Light3d673592019-01-18 14:37:31 -0800539 isPrimaryAbi := i == 0
540 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900541 // When multilib.* is omitted for binaries, it implies
542 // multilib.first.
543 ctx.AddFarVariationDependencies([]blueprint.Variation{
544 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900545 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900546 }, executableTag, a.properties.Binaries...)
547
548 // Add native modules targetting the first ABI
549 addDependenciesForNativeModules(ctx,
550 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100551 a.properties.Multilib.First.Binaries,
552 a.properties.Multilib.First.Tests,
553 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900554 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800555
556 // When multilib.* is omitted for prebuilts, it implies multilib.first.
557 ctx.AddFarVariationDependencies([]blueprint.Variation{
558 {Mutator: "arch", Variation: target.String()},
559 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900560 }
561
562 switch target.Arch.ArchType.Multilib {
563 case "lib32":
564 // Add native modules targetting 32-bit ABI
565 addDependenciesForNativeModules(ctx,
566 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100567 a.properties.Multilib.Lib32.Binaries,
568 a.properties.Multilib.Lib32.Tests,
569 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900570 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900571
572 addDependenciesForNativeModules(ctx,
573 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100574 a.properties.Multilib.Prefer32.Binaries,
575 a.properties.Multilib.Prefer32.Tests,
576 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900577 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900578 case "lib64":
579 // Add native modules targetting 64-bit ABI
580 addDependenciesForNativeModules(ctx,
581 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100582 a.properties.Multilib.Lib64.Binaries,
583 a.properties.Multilib.Lib64.Tests,
584 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900585 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900586
587 if !has32BitTarget {
588 addDependenciesForNativeModules(ctx,
589 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100590 a.properties.Multilib.Prefer32.Binaries,
591 a.properties.Multilib.Prefer32.Tests,
592 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900593 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900594 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700595
596 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
597 for _, sanitizer := range ctx.Config().SanitizeDevice() {
598 if sanitizer == "hwaddress" {
599 addDependenciesForNativeModules(ctx,
600 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100601 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700602 break
603 }
604 }
605 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900606 }
607
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900608 }
609
Jiyong Parkff1458f2018-10-12 21:49:38 +0900610 ctx.AddFarVariationDependencies([]blueprint.Variation{
611 {Mutator: "arch", Variation: "android_common"},
612 }, javaLibTag, a.properties.Java_libs...)
613
Jiyong Park23c52b02019-02-02 13:13:47 +0900614 if String(a.properties.Key) == "" {
615 ctx.ModuleErrorf("key is missing")
616 return
617 }
618 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900619
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900620 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900621 if cert != "" {
622 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900623 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900624}
625
Colin Cross0ea8ba82019-06-06 14:33:29 -0700626func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900627 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
628 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000629 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900630 }
631 return String(a.properties.Certificate)
632}
633
Colin Cross41955e82019-05-29 14:40:35 -0700634func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
635 switch tag {
636 case "":
637 if file, ok := a.outputFiles[imageApex]; ok {
638 return android.Paths{file}, nil
639 } else {
640 return nil, nil
641 }
642 default:
643 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900644 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900645}
646
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900647func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900648 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900649}
650
Jiyong Park7c1dc612019-01-05 11:15:24 +0900651func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
652 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900653 return "vendor"
654 } else {
655 return "core"
656 }
657}
658
Jiyong Parkf97782b2019-02-13 20:28:58 +0900659func (a *apexBundle) EnableSanitizer(sanitizerName string) {
660 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
661 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
662 }
663}
664
Jiyong Park388ef3f2019-01-28 19:47:32 +0900665func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900666 if android.InList(sanitizerName, a.properties.SanitizerNames) {
667 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900668 }
669
670 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900671 globalSanitizerNames := []string{}
672 if a.Host() {
673 globalSanitizerNames = ctx.Config().SanitizeHost()
674 } else {
675 arches := ctx.Config().SanitizeDeviceArch()
676 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
677 globalSanitizerNames = ctx.Config().SanitizeDevice()
678 }
679 }
680 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900681}
682
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900683func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
684 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
685}
686
687func (a *apexBundle) PreventInstall() {
688 a.properties.PreventInstall = true
689}
690
691func (a *apexBundle) HideFromMake() {
692 a.properties.HideFromMake = true
693}
694
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800695func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900696 // Decide the APEX-local directory by the multilib of the library
697 // In the future, we may query this to the module.
698 switch cc.Arch().ArchType.Multilib {
699 case "lib32":
700 dirInApex = "lib"
701 case "lib64":
702 dirInApex = "lib64"
703 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900704 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200705 if !cc.Arch().Native {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900706 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200707 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
708 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900709 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800710 if handleSpecialLibs {
711 switch cc.Name() {
712 case "libc", "libm", "libdl":
713 // Special case for bionic libs. This is to prevent the bionic libs
714 // from being included in the search path /apex/com.android.apex/lib.
715 // This exclusion is required because bionic libs in the runtime APEX
716 // are available via the legacy paths /system/lib/libc.so, etc. By the
717 // init process, the bionic libs in the APEX are bind-mounted to the
718 // legacy paths and thus will be loaded into the default linker namespace.
719 // If the bionic libs are directly in /apex/com.android.apex/lib then
720 // the same libs will be again loaded to the runtime linker namespace,
721 // which will result double loading of bionic libs that isn't supported.
722 dirInApex = filepath.Join(dirInApex, "bionic")
723 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900724 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900725
726 fileToCopy = cc.OutputFile().Path()
727 return
728}
729
730func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900731 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
dimitry8d6dde82019-07-11 10:23:53 +0200732 if !cc.Arch().Native {
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900733 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
dimitry8d6dde82019-07-11 10:23:53 +0200734 } else if cc.Target().NativeBridge == android.NativeBridgeEnabled {
735 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900736 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900737 fileToCopy = cc.OutputFile().Path()
738 return
739}
740
Alex Light778127a2019-02-27 14:19:50 -0800741func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
742 dirInApex = "bin"
743 fileToCopy = py.HostToolPath().Path()
744 return
745}
746func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
747 dirInApex = "bin"
748 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
749 if err != nil {
750 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
751 return
752 }
753 fileToCopy = android.PathForOutput(ctx, s)
754 return
755}
756
Jiyong Park04480cf2019-02-06 00:16:29 +0900757func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
758 dirInApex = filepath.Join("bin", sh.SubDir())
759 fileToCopy = sh.OutputFile()
760 return
761}
762
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900763func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
764 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900765 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900766 return
767}
768
Jiyong Park9e6c2422019-08-09 20:39:45 +0900769func getCopyManifestForPrebuiltJavaLibrary(java *java.Import) (fileToCopy android.Path, dirInApex string) {
770 dirInApex = "javalib"
771 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
772 implJars := java.ImplementationJars()
773 if len(implJars) != 1 {
774 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
775 strings.Join(implJars.Strings(), ", ")))
776 }
777 fileToCopy = implJars[0]
778 return
779}
780
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900781func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
782 dirInApex = filepath.Join("etc", prebuilt.SubDir())
783 fileToCopy = prebuilt.OutputFile()
784 return
785}
786
787func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900788 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900789
Alex Light5098a612018-11-29 17:12:15 -0800790 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
791 a.apexTypes = imageApex
792 } else if *a.properties.Payload_type == "zip" {
793 a.apexTypes = zipApex
794 } else if *a.properties.Payload_type == "both" {
795 a.apexTypes = both
796 } else {
797 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
798 return
799 }
800
Roland Levillain630846d2019-06-26 12:48:34 +0100801 if len(a.properties.Tests) > 0 && !a.testApex {
802 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
803 return
804 }
805
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800806 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
807
Jooyung Hane1633032019-08-01 17:41:43 +0900808 // native lib dependencies
809 var provideNativeLibs []string
810 var requireNativeLibs []string
811
Jooyung Han5c998b92019-06-27 11:30:33 +0900812 // Check if "uses" requirements are met with dependent apexBundles
813 var providedNativeSharedLibs []string
814 useVendor := proptools.Bool(a.properties.Use_vendor)
815 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
816 if ctx.OtherModuleDependencyTag(m) != usesTag {
817 return
818 }
819 otherName := ctx.OtherModuleName(m)
820 other, ok := m.(*apexBundle)
821 if !ok {
822 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
823 return
824 }
825 if proptools.Bool(other.properties.Use_vendor) != useVendor {
826 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
827 return
828 }
829 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
830 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
831 return
832 }
833 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
834 })
835
Alex Light778127a2019-02-27 14:19:50 -0800836 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100837 depTag := ctx.OtherModuleDependencyTag(child)
838 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900839 if _, ok := parent.(*apexBundle); ok {
840 // direct dependencies
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900841 switch depTag {
842 case sharedLibTag:
843 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900844 if cc.HasStubsVariants() {
845 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
846 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800847 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900848 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900849 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900850 } else {
851 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900852 }
853 case executableTag:
854 if cc, ok := child.(*cc.Module); ok {
855 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900856 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900857 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900858 } else if sh, ok := child.(*android.ShBinary); ok {
859 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
860 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800861 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
862 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
863 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
864 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
865 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
866 // NB: Since go binaries are static we don't need the module for anything here, which is
867 // good since the go tool is a blueprint.Module not an android.Module like we would
868 // normally use.
869 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900870 } else {
Alex Light778127a2019-02-27 14:19:50 -0800871 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 +0900872 }
873 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +0900874 if javaLib, ok := child.(*java.Library); ok {
875 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(javaLib)
Jiyong Park8fd61922018-11-08 02:50:25 +0900876 if fileToCopy == nil {
877 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
878 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900879 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
880 }
881 return true
882 } else if javaLib, ok := child.(*java.Import); ok {
883 fileToCopy, dirInApex := getCopyManifestForPrebuiltJavaLibrary(javaLib)
884 if fileToCopy == nil {
885 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
886 } else {
887 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, javaLib, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900888 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900889 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900890 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +0900891 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900892 }
893 case prebuiltTag:
894 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
895 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900896 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900897 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900898 } else {
899 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
900 }
Roland Levillain630846d2019-06-26 12:48:34 +0100901 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +0100902 if ccTest, ok := child.(*cc.Module); ok {
903 if ccTest.IsTestPerSrcAllTestsVariation() {
904 // Multiple-output test module (where `test_per_src: true`).
905 //
906 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
907 // We do not add this variation to `filesInfo`, as it has no output;
908 // however, we do add the other variations of this module as indirect
909 // dependencies (see below).
910 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +0100911 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +0100912 // Single-output test module (where `test_per_src: false`).
913 fileToCopy, dirInApex := getCopyManifestForExecutable(ccTest)
914 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, ccTest, nil})
Roland Levillain9b5fde92019-06-28 15:41:19 +0100915 }
Roland Levillain630846d2019-06-26 12:48:34 +0100916 return true
917 } else {
918 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
919 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900920 case keyTag:
921 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900922 a.private_key_file = key.private_key_file
923 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900924 return false
925 } else {
926 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900927 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900928 case certificateTag:
929 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900930 a.container_certificate_file = dep.Certificate.Pem
931 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900932 return false
933 } else {
934 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
935 }
Jiyong Park03b68dd2019-07-26 23:20:40 +0900936 case android.PrebuiltDepTag:
937 // If the prebuilt is force disabled, remember to delete the prebuilt file
938 // that might have been installed in the previous builds
939 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
940 a.prebuiltFileToDelete = prebuilt.InstallFilename()
941 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900942 }
943 } else {
944 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +0900945 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +0100946 // We cannot use a switch statement on `depTag` here as the checked
947 // tags used below are private (e.g. `cc.sharedDepTag`).
948 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
949 if cc, ok := child.(*cc.Module); ok {
950 if android.InList(cc.Name(), providedNativeSharedLibs) {
951 // If we're using a shared library which is provided from other APEX,
952 // don't include it in this APEX
953 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900954 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100955 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
956 // If the dependency is a stubs lib, don't include it in this APEX,
957 // but make sure that the lib is installed on the device.
958 // In case no APEX is having the lib, the lib is installed to the system
959 // partition.
960 //
961 // Always include if we are a host-apex however since those won't have any
962 // system libraries.
963 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
964 a.externalDeps = append(a.externalDeps, cc.Name())
965 }
Jooyung Hane1633032019-08-01 17:41:43 +0900966 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +0100967 // Don't track further
968 return false
969 }
970 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
971 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
972 return true
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973 }
Roland Levillainf89cd092019-07-29 16:22:59 +0100974 } else if cc.IsTestPerSrcDepTag(depTag) {
975 if cc, ok := child.(*cc.Module); ok {
976 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
977 // Handle modules created as `test_per_src` variations of a single test module:
978 // use the name of the generated test binary (`fileToCopy`) instead of the name
979 // of the original test module (`depName`, shared by all `test_per_src`
980 // variations of that module).
981 moduleName := filepath.Base(fileToCopy.String())
982 filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
983 return true
984 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900985 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +0100986 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Sundong Ahn2db7f462019-08-27 18:53:12 +0900987 } else if am.NoApex() && !android.InList(depName, whitelistNoApex[ctx.ModuleName()]) {
988 ctx.ModuleErrorf("tries to include no_apex module %s", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900989 }
990 }
991 }
992 return false
993 })
994
Jiyong Park9335a262018-12-24 11:31:58 +0900995 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900996 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900997 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
998 return
999 }
1000
Jiyong Park8fd61922018-11-08 02:50:25 +09001001 // remove duplicates in filesInfo
1002 removeDup := func(filesInfo []apexFile) []apexFile {
1003 encountered := make(map[android.Path]bool)
1004 result := []apexFile{}
1005 for _, f := range filesInfo {
1006 if !encountered[f.builtFile] {
1007 encountered[f.builtFile] = true
1008 result = append(result, f)
1009 }
1010 }
1011 return result
1012 }
1013 filesInfo = removeDup(filesInfo)
1014
1015 // to have consistent build rules
1016 sort.Slice(filesInfo, func(i, j int) bool {
1017 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1018 })
1019
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001020 // check no_apex modules
1021 whitelist := whitelistNoApex[ctx.ModuleName()]
1022 for i := range filesInfo {
1023 if am, ok := filesInfo[i].module.(android.ApexModule); ok {
1024 if am.NoApex() && !android.InList(filesInfo[i].moduleName, whitelist) {
1025 ctx.ModuleErrorf("tries to include no_apex module %s", filesInfo[i].moduleName)
1026 }
1027 }
1028 }
1029
Jiyong Park8fd61922018-11-08 02:50:25 +09001030 // prepend the name of this APEX to the module names. These names will be the names of
1031 // modules that will be defined if the APEX is flattened.
1032 for i := range filesInfo {
1033 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
1034 }
1035
Jiyong Park8fd61922018-11-08 02:50:25 +09001036 a.installDir = android.PathForModuleInstall(ctx, "apex")
1037 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001038
Jooyung Hane1633032019-08-01 17:41:43 +09001039 a.manifestOut = android.PathForModuleOut(ctx, "apex_manifest.json")
1040 // put dependency({provide|require}NativeLibs) in apex_manifest.json
1041 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
1042 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
1043 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
1044 ctx.Build(pctx, android.BuildParams{
1045 Rule: injectApexDependency,
1046 Input: manifestSrc,
1047 Output: a.manifestOut,
1048 Args: map[string]string{
1049 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
1050 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
1051 },
1052 })
1053
Alex Light5098a612018-11-29 17:12:15 -08001054 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001055 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -08001056 }
1057 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +09001058 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
Roland Levillaindfe75b32019-07-23 16:53:32 +01001059 // is true. This is to support referencing APEX via ":<module_name>" syntax
Jiyong Park23c52b02019-02-02 13:13:47 +09001060 // in other modules. It is in AndroidMk where the selection of flattened
1061 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001062 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +09001063 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +09001064 }
1065}
1066
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001067func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +09001068 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001069 for _, f := range a.filesInfo {
1070 if f.module != nil {
1071 notice := f.module.NoticeFile()
1072 if notice.Valid() {
1073 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001074 }
1075 }
1076 }
1077 // append the notice file specified in the apex module itself
1078 if a.NoticeFile().Valid() {
1079 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +09001080 }
1081
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001082 if len(noticeFiles) == 0 {
1083 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +09001084 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001085
Jaewoong Jung98772792019-07-01 17:15:13 -07001086 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
Jiyong Park52818fc2019-03-18 12:01:38 +09001087}
1088
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001089func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001090 cert := String(a.properties.Certificate)
1091 if cert != "" && android.SrcIsModule(cert) == "" {
1092 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001093 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
1094 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001095 } else if cert == "" {
1096 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001097 a.container_certificate_file = pem
1098 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001099 }
1100
Alex Light5098a612018-11-29 17:12:15 -08001101 var abis []string
1102 for _, target := range ctx.MultiTargets() {
1103 if len(target.Arch.Abi) > 0 {
1104 abis = append(abis, target.Arch.Abi[0])
1105 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +09001106 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001107
Alex Light5098a612018-11-29 17:12:15 -08001108 abis = android.FirstUniqueStrings(abis)
1109
1110 suffix := apexType.suffix()
1111 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001112
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001113 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001114 for _, f := range a.filesInfo {
1115 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001116 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001117
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001118 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +09001119 for i, src := range filesToCopy {
1120 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -08001121 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001122 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
1123 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -08001124 for _, sym := range a.filesInfo[i].symlinks {
1125 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
1126 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
1127 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001128 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +09001129 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Hane1633032019-08-01 17:41:43 +09001130 implicitInputs = append(implicitInputs, a.manifestOut)
Alex Light5098a612018-11-29 17:12:15 -08001131
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001132 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
1133 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001134
Alex Light5098a612018-11-29 17:12:15 -08001135 if apexType.image() {
1136 // files and dirs that will be created in APEX
1137 var readOnlyPaths []string
1138 var executablePaths []string // this also includes dirs
1139 for _, f := range a.filesInfo {
1140 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +01001141 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -08001142 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -08001143 for _, s := range f.symlinks {
Jiyong Parkc80b5fa2019-07-20 14:24:33 +09001144 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
Alex Light3d673592019-01-18 14:37:31 -08001145 }
Alex Light5098a612018-11-29 17:12:15 -08001146 } else {
1147 readOnlyPaths = append(readOnlyPaths, pathInApex)
1148 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001149 dir := f.installDir
1150 for !android.InList(dir, executablePaths) && dir != "" {
1151 executablePaths = append(executablePaths, dir)
1152 dir, _ = filepath.Split(dir) // move up to the parent
1153 if len(dir) > 0 {
1154 // remove trailing slash
1155 dir = dir[:len(dir)-1]
1156 }
Alex Light5098a612018-11-29 17:12:15 -08001157 }
1158 }
1159 sort.Strings(readOnlyPaths)
1160 sort.Strings(executablePaths)
1161 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1162 ctx.Build(pctx, android.BuildParams{
1163 Rule: generateFsConfig,
1164 Output: cannedFsConfig,
1165 Description: "generate fs config",
1166 Args: map[string]string{
1167 "ro_paths": strings.Join(readOnlyPaths, " "),
1168 "exec_paths": strings.Join(executablePaths, " "),
1169 },
1170 })
1171
1172 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
1173 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
1174 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1175 if !fileContextsOptionalPath.Valid() {
1176 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1177 return
1178 }
1179 fileContexts := fileContextsOptionalPath.Path()
1180
Jiyong Park835d82b2018-12-27 16:04:18 +09001181 optFlags := []string{}
1182
Alex Light5098a612018-11-29 17:12:15 -08001183 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001184 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1185 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001186
Jiyong Park7f67f482019-01-05 12:57:48 +09001187 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1188 if overridden {
1189 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1190 }
1191
Jiyong Park40e26a22019-02-08 02:53:06 +09001192 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001193 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001194 implicitInputs = append(implicitInputs, androidManifestFile)
1195 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1196 }
1197
Jiyong Park71b519d2019-04-18 17:25:49 +09001198 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1199 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1200 ctx.Config().UnbundledBuild() &&
1201 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1202 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1203 apiFingerprint := java.ApiFingerprintPath(ctx)
1204 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1205 implicitInputs = append(implicitInputs, apiFingerprint)
1206 }
1207 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1208
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001209 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1210 if noticeFile.Valid() {
1211 // If there's a NOTICE file, embed it as an asset file in the APEX.
1212 implicitInputs = append(implicitInputs, noticeFile.Path())
1213 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1214 }
1215
Jooyung Hane65ed7c2019-08-28 00:27:35 +09001216 if !ctx.Config().UnbundledBuild() && a.installable() {
1217 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
1218 // don't need hashtree for activation. Therefore, by removing hashtree from
1219 // apex bundle (filesystem image in it, to be specific), we can save storage.
1220 optFlags = append(optFlags, "--no_hashtree")
1221 }
1222
Alex Light5098a612018-11-29 17:12:15 -08001223 ctx.Build(pctx, android.BuildParams{
1224 Rule: apexRule,
1225 Implicits: implicitInputs,
1226 Output: unsignedOutputFile,
1227 Description: "apex (" + apexType.name() + ")",
1228 Args: map[string]string{
1229 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1230 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1231 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001232 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001233 "file_contexts": fileContexts.String(),
1234 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001235 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001236 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001237 },
1238 })
1239
1240 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1241 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1242 a.bundleModuleFile = bundleModuleFile
1243
1244 ctx.Build(pctx, android.BuildParams{
1245 Rule: apexProtoConvertRule,
1246 Input: unsignedOutputFile,
1247 Output: apexProtoFile,
1248 Description: "apex proto convert",
1249 })
1250
1251 ctx.Build(pctx, android.BuildParams{
1252 Rule: apexBundleRule,
1253 Input: apexProtoFile,
1254 Output: a.bundleModuleFile,
1255 Description: "apex bundle module",
1256 Args: map[string]string{
1257 "abi": strings.Join(abis, "."),
1258 },
1259 })
1260 } else {
1261 ctx.Build(pctx, android.BuildParams{
1262 Rule: zipApexRule,
1263 Implicits: implicitInputs,
1264 Output: unsignedOutputFile,
1265 Description: "apex (" + apexType.name() + ")",
1266 Args: map[string]string{
1267 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1268 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1269 "copy_commands": strings.Join(copyCommands, " && "),
Jooyung Hane1633032019-08-01 17:41:43 +09001270 "manifest": a.manifestOut.String(),
Alex Light5098a612018-11-29 17:12:15 -08001271 },
1272 })
Colin Crossa4925902018-11-16 11:36:28 -08001273 }
Colin Crossa4925902018-11-16 11:36:28 -08001274
Alex Light5098a612018-11-29 17:12:15 -08001275 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001276 ctx.Build(pctx, android.BuildParams{
1277 Rule: java.Signapk,
1278 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001279 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001280 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001281 Implicits: []android.Path{
1282 a.container_certificate_file,
1283 a.container_private_key_file,
1284 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001285 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001286 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001287 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001288 },
1289 })
Alex Light5098a612018-11-29 17:12:15 -08001290
1291 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001292 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001293 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001294 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001295}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001296
Jiyong Park8fd61922018-11-08 02:50:25 +09001297func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001298 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001299 // 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 +09001300 // with other ordinary files.
Jooyung Hane1633032019-08-01 17:41:43 +09001301 a.filesInfo = append(a.filesInfo, apexFile{a.manifestOut, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001302
Jiyong Park42cca6c2019-04-01 11:15:50 +09001303 // rename to apex_pubkey
1304 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1305 ctx.Build(pctx, android.BuildParams{
1306 Rule: android.Cp,
1307 Input: a.public_key_file,
1308 Output: copiedPubkey,
1309 })
1310 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1311
Jiyong Park23c52b02019-02-02 13:13:47 +09001312 if ctx.Config().FlattenApex() {
1313 for _, fi := range a.filesInfo {
1314 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001315 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1316 for _, sym := range fi.symlinks {
1317 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1318 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001319 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001320 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001321 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001322}
1323
1324func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001325 if a.properties.HideFromMake {
1326 return android.AndroidMkData{
1327 Disabled: true,
1328 }
1329 }
Alex Light5098a612018-11-29 17:12:15 -08001330 writers := []android.AndroidMkData{}
1331 if a.apexTypes.image() {
1332 writers = append(writers, a.androidMkForType(imageApex))
1333 }
1334 if a.apexTypes.zip() {
1335 writers = append(writers, a.androidMkForType(zipApex))
1336 }
1337 return android.AndroidMkData{
1338 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1339 for _, data := range writers {
1340 data.Custom(w, name, prefix, moduleDir, data)
1341 }
1342 }}
1343}
1344
Alex Lightf1801bc2019-02-13 11:10:07 -08001345func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001346 moduleNames := []string{}
1347
1348 for _, fi := range a.filesInfo {
1349 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1350 continue
1351 }
1352 if !android.InList(fi.moduleName, moduleNames) {
1353 moduleNames = append(moduleNames, fi.moduleName)
1354 }
1355 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1356 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1357 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001358 // /apex/<name>/{lib|framework|...}
1359 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1360 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001361 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001362 // /system/apex/<name>/{lib|framework|...}
1363 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1364 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001365 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001366 if len(fi.symlinks) > 0 {
1367 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1368 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001369
1370 if fi.module != nil && fi.module.NoticeFile().Valid() {
1371 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1372 }
Jiyong Park94427262019-02-05 23:18:47 +09001373 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001374 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001375 }
1376 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1377 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1378 if fi.module != nil {
1379 archStr := fi.module.Target().Arch.ArchType.String()
1380 host := false
1381 switch fi.module.Target().Os.Class {
1382 case android.Host:
1383 if archStr != "common" {
1384 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1385 }
1386 host = true
1387 case android.HostCross:
1388 if archStr != "common" {
1389 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1390 }
1391 host = true
1392 case android.Device:
1393 if archStr != "common" {
1394 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1395 }
1396 }
1397 if host {
1398 makeOs := fi.module.Target().Os.String()
1399 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1400 makeOs = "linux"
1401 }
1402 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1403 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1404 }
1405 }
1406 if fi.class == javaSharedLib {
1407 javaModule := fi.module.(*java.Library)
1408 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1409 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1410 // we will have foo.jar.jar
1411 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1412 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1413 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1414 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1415 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1416 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1417 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1418 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001419 if cc, ok := fi.module.(*cc.Module); ok {
1420 if cc.UnstrippedOutputFile() != nil {
1421 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1422 }
1423 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001424 if cc.CoverageOutputFile().Valid() {
1425 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1426 }
Jiyong Park94427262019-02-05 23:18:47 +09001427 }
1428 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1429 } else {
1430 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1431 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1432 }
1433 }
1434 return moduleNames
1435}
1436
Alex Light5098a612018-11-29 17:12:15 -08001437func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001438 return android.AndroidMkData{
1439 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1440 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001441 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001442 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001443 }
1444
Jiyong Park719b4462019-01-13 00:39:51 +09001445 if a.flattened && apexType.image() {
1446 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001447 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1448 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1449 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001450 if len(moduleNames) > 0 {
1451 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1452 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001453 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001454 } else {
Alex Light5098a612018-11-29 17:12:15 -08001455 // zip-apex is the less common type so have the name refer to the image-apex
1456 // only and use {name}.zip if you want the zip-apex
1457 if apexType == zipApex && a.apexTypes == both {
1458 name = name + ".zip"
1459 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001460 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1461 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1462 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1463 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001464 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001465 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001466 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001467 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001468 if len(moduleNames) > 0 {
1469 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1470 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001471 if len(a.externalDeps) > 0 {
1472 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1473 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001474 if a.prebuiltFileToDelete != "" {
1475 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", "rm -rf "+
1476 filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), a.prebuiltFileToDelete))
1477 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001478 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001479
Alex Light5098a612018-11-29 17:12:15 -08001480 if apexType == imageApex {
1481 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1482 }
Jiyong Park719b4462019-01-13 00:39:51 +09001483 }
1484 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001485}
1486
Alex Light0851b882019-02-07 13:20:53 -08001487func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001488 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001489}
1490
1491func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001492 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001493}
1494
1495func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001496 module := &apexBundle{
1497 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001498 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001499 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001500 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001501 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001502 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001503 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1504 })
Alex Light5098a612018-11-29 17:12:15 -08001505 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001506 android.InitDefaultableModule(module)
1507 return module
1508}
Jiyong Park30ca9372019-02-07 16:27:23 +09001509
1510//
1511// Defaults
1512//
1513type Defaults struct {
1514 android.ModuleBase
1515 android.DefaultsModuleBase
1516}
1517
Jiyong Park30ca9372019-02-07 16:27:23 +09001518func defaultsFactory() android.Module {
1519 return DefaultsFactory()
1520}
1521
1522func DefaultsFactory(props ...interface{}) android.Module {
1523 module := &Defaults{}
1524
1525 module.AddProperties(props...)
1526 module.AddProperties(
1527 &apexBundleProperties{},
1528 &apexTargetBundleProperties{},
1529 )
1530
1531 android.InitDefaultsModule(module)
1532 return module
1533}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001534
1535//
1536// Prebuilt APEX
1537//
1538type Prebuilt struct {
1539 android.ModuleBase
1540 prebuilt android.Prebuilt
1541
1542 properties PrebuiltProperties
1543
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001544 inputApex android.Path
1545 installDir android.OutputPath
1546 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001547 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001548}
1549
1550type PrebuiltProperties struct {
1551 // the path to the prebuilt .apex file to import.
Jiyong Park2cb52882019-07-07 12:39:16 +09001552 Source string `blueprint:"mutated"`
1553 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001554
1555 Src *string
1556 Arch struct {
1557 Arm struct {
1558 Src *string
1559 }
1560 Arm64 struct {
1561 Src *string
1562 }
1563 X86 struct {
1564 Src *string
1565 }
1566 X86_64 struct {
1567 Src *string
1568 }
1569 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001570
1571 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001572 // Optional name for the installed apex. If unspecified, name of the
1573 // module is used as the file name
1574 Filename *string
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001575
1576 // Names of modules to be overridden. Listed modules can only be other binaries
1577 // (in Make or Soong).
1578 // This does not completely prevent installation of the overridden binaries, but if both
1579 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1580 // from PRODUCT_PACKAGES.
1581 Overrides []string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001582}
1583
1584func (p *Prebuilt) installable() bool {
1585 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001586}
1587
1588func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +09001589 // If the device is configured to use flattened APEX, force disable the prebuilt because
1590 // the prebuilt is a non-flattened one.
1591 forceDisable := ctx.Config().FlattenApex()
1592
1593 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1594 // to build the prebuilts themselves.
Jiyong Parkca8992e2019-07-17 08:21:36 +09001595 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park50b81e52019-07-11 11:24:41 +09001596
Kun Niu10c9f832019-07-29 16:28:57 -07001597 // Force disable the prebuilts when coverage is enabled.
1598 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1599 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1600
Jiyong Park50b81e52019-07-11 11:24:41 +09001601 // b/137216042 don't use prebuilts when address sanitizer is on
1602 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1603 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1604
1605 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park2cb52882019-07-07 12:39:16 +09001606 p.properties.ForceDisable = true
1607 return
1608 }
1609
Jiyong Parkc95714e2019-03-29 14:23:10 +09001610 // This is called before prebuilt_select and prebuilt_postdeps mutators
1611 // The mutators requires that src to be set correctly for each arch so that
1612 // arch variants are disabled when src is not provided for the arch.
1613 if len(ctx.MultiTargets()) != 1 {
1614 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1615 return
1616 }
1617 var src string
1618 switch ctx.MultiTargets()[0].Arch.ArchType {
1619 case android.Arm:
1620 src = String(p.properties.Arch.Arm.Src)
1621 case android.Arm64:
1622 src = String(p.properties.Arch.Arm64.Src)
1623 case android.X86:
1624 src = String(p.properties.Arch.X86.Src)
1625 case android.X86_64:
1626 src = String(p.properties.Arch.X86_64.Src)
1627 default:
1628 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1629 return
1630 }
1631 if src == "" {
1632 src = String(p.properties.Src)
1633 }
1634 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001635}
1636
Jiyong Park03b68dd2019-07-26 23:20:40 +09001637func (p *Prebuilt) isForceDisabled() bool {
1638 return p.properties.ForceDisable
1639}
1640
Colin Cross41955e82019-05-29 14:40:35 -07001641func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1642 switch tag {
1643 case "":
1644 return android.Paths{p.outputApex}, nil
1645 default:
1646 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1647 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001648}
1649
Jiyong Park4d277042019-04-23 18:00:10 +09001650func (p *Prebuilt) InstallFilename() string {
1651 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1652}
1653
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001654func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park2cb52882019-07-07 12:39:16 +09001655 if p.properties.ForceDisable {
1656 return
1657 }
1658
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001659 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001660 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001661 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001662 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001663 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1664 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1665 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001666 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1667 ctx.Build(pctx, android.BuildParams{
1668 Rule: android.Cp,
1669 Input: p.inputApex,
1670 Output: p.outputApex,
1671 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001672 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001673 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001674 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001675}
1676
1677func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1678 return &p.prebuilt
1679}
1680
1681func (p *Prebuilt) Name() string {
1682 return p.prebuilt.Name(p.ModuleBase.Name())
1683}
1684
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001685func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1686 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001687 Class: "ETC",
1688 OutputFile: android.OptionalPathForPath(p.inputApex),
1689 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -07001690 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1691 func(entries *android.AndroidMkEntries) {
1692 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1693 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1694 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1695 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1696 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001697 },
1698 }
1699}
1700
1701// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1702func PrebuiltFactory() android.Module {
1703 module := &Prebuilt{}
1704 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001705 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001706 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001707 return module
1708}