blob: 8ee8035b95d3880ad759def2f766bbb89672e47f [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"
Jaewoong Jung9c49b282020-05-14 14:15:24 -070023 "strconv"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090024 "strings"
25
26 "android/soong/android"
27 "android/soong/cc"
28 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080029 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030
31 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080032 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090033 "github.com/google/blueprint/proptools"
34)
35
36var (
37 pctx = android.NewPackageContext("android/apex")
38
39 // Create a canned fs config file where all files and directories are
40 // by default set to (uid/gid/mode) = (1000/1000/0644)
41 // TODO(b/113082813) make this configurable using config.fs syntax
42 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000043 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000044 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090045 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090046 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090047 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090048 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090049
50 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
51 // against the binary policy using sefcontext_compiler -p <policy>.
52
53 // TODO(b/114327326): automate the generation of file_contexts
54 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
55 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
56 `(${copy_commands}) && ` +
57 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090058 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090059 `--file_contexts ${file_contexts} ` +
60 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080061 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090062 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090063 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
64 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
65 "${soong_zip}", "${zipalign}", "${aapt2}"},
66 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090067 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080068
Alex Light5098a612018-11-29 17:12:15 -080069 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
70 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
71 `(${copy_commands}) && ` +
72 `APEXER_TOOL_PATH=${tool_path} ` +
73 `${apexer} --force --manifest ${manifest} ` +
74 `--payload_type zip ` +
75 `${image_dir} ${out} `,
76 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
77 Description: "ZipAPEX ${image_dir} => ${out}",
78 }, "tool_path", "image_dir", "copy_commands", "manifest")
79
Colin Crossa4925902018-11-16 11:36:28 -080080 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
81 blueprint.RuleParams{
82 Command: `${aapt2} convert --output-format proto $in -o $out`,
83 CommandDeps: []string{"${aapt2}"},
84 })
85
86 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090087 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000088 `apex_payload.img:apex/${abi}.img ` +
89 `apex_manifest.json:root/apex_manifest.json ` +
Jaewoong Jung105e1662019-09-04 13:26:18 -070090 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
91 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -080092 CommandDeps: []string{"${zip2zip}"},
93 Description: "app bundle",
94 }, "abi")
Jaewoong Jung9c49b282020-05-14 14:15:24 -070095
96 extractMatchingApex = pctx.StaticRule(
97 "extractMatchingApex",
98 blueprint.RuleParams{
99 Command: `rm -rf "$out" && ` +
100 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
101 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
102 `${in}`,
103 CommandDeps: []string{"${extract_apks}"},
104 },
105 "abis", "allow-prereleased", "sdk-version")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900106)
107
Alex Light5098a612018-11-29 17:12:15 -0800108var imageApexSuffix = ".apex"
109var zipApexSuffix = ".zipapex"
110
111var imageApexType = "image"
112var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900113
114type dependencyTag struct {
115 blueprint.BaseDependencyTag
116 name string
117}
118
119var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900120 sharedLibTag = dependencyTag{name: "sharedLib"}
121 executableTag = dependencyTag{name: "executable"}
122 javaLibTag = dependencyTag{name: "javaLib"}
123 prebuiltTag = dependencyTag{name: "prebuilt"}
124 keyTag = dependencyTag{name: "key"}
125 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900126)
127
128func init() {
Colin Cross713ef2b2019-04-02 16:14:11 -0700129 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900130 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900131 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100132 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
133 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
134 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
135 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000136 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100137 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
138 } else {
139 return pctx.HostBinToolPath(ctx, tool).String()
140 }
141 })
142 }
143 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900144 pctx.HostBinToolVariable("avbtool", "avbtool")
145 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
146 pctx.HostBinToolVariable("merge_zips", "merge_zips")
147 pctx.HostBinToolVariable("mke2fs", "mke2fs")
148 pctx.HostBinToolVariable("resize2fs", "resize2fs")
149 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
150 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800151 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900152 pctx.HostBinToolVariable("zipalign", "zipalign")
Jaewoong Jung9c49b282020-05-14 14:15:24 -0700153 pctx.HostBinToolVariable("extract_apks", "extract_apks")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900154
Alex Light0851b882019-02-07 13:20:53 -0800155 android.RegisterModuleType("apex", apexBundleFactory)
156 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900157 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700158 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jaewoong Jung9c49b282020-05-14 14:15:24 -0700159 android.RegisterModuleType("apex_set", apexSetFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160
161 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
162 ctx.TopDown("apex_deps", apexDepsMutator)
163 ctx.BottomUp("apex", apexMutator)
164 })
165}
166
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900167// Mark the direct and transitive dependencies of apex bundles so that they
168// can be built for the apex bundles.
169func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800170 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800171 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900172 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900173 depName := mctx.OtherModuleName(child)
174 // If the parent is apexBundle, this child is directly depended.
175 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800176 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800177 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
178 // non-installable apex's cannot be installed and so should not prevent libraries from being
179 // installed to the system.
180 android.UpdateApexDependency(apexBundleName, depName, directDep)
181 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900182
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900183 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900184 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900185 return true
186 } else {
187 return false
188 }
189 })
190 }
191}
192
193// Create apex variations if a module is included in APEX(s).
194func apexMutator(mctx android.BottomUpMutatorContext) {
195 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900196 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900197 } else if _, ok := mctx.Module().(*apexBundle); ok {
198 // apex bundle itself is mutated so that it and its modules have same
199 // apex variant.
200 apexBundleName := mctx.ModuleName()
201 mctx.CreateVariations(apexBundleName)
202 }
203}
204
Alex Light9670d332019-01-29 18:07:33 -0800205type apexNativeDependencies struct {
206 // List of native libraries
207 Native_shared_libs []string
208 // List of native executables
209 Binaries []string
210}
211type apexMultilibProperties struct {
212 // Native dependencies whose compile_multilib is "first"
213 First apexNativeDependencies
214
215 // Native dependencies whose compile_multilib is "both"
216 Both apexNativeDependencies
217
218 // Native dependencies whose compile_multilib is "prefer32"
219 Prefer32 apexNativeDependencies
220
221 // Native dependencies whose compile_multilib is "32"
222 Lib32 apexNativeDependencies
223
224 // Native dependencies whose compile_multilib is "64"
225 Lib64 apexNativeDependencies
226}
227
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900228type apexBundleProperties struct {
229 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000230 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800231 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900232
Jiyong Park40e26a22019-02-08 02:53:06 +0900233 // AndroidManifest.xml file used for the zip container of this APEX bundle.
234 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800235 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900236
Jiyong Park05e70dd2019-03-18 14:26:32 +0900237 // Canonical name of the APEX bundle in the manifest file.
238 // If unspecified, defaults to the value of name
239 Apex_name *string
240
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900241 // Determines the file contexts file for setting security context to each file in this APEX bundle.
242 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
243 // used.
244 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900245 File_contexts *string
246
247 // List of native shared libs that are embedded inside this APEX bundle
248 Native_shared_libs []string
249
250 // List of native executables that are embedded inside this APEX bundle
251 Binaries []string
252
253 // List of java libraries that are embedded inside this APEX bundle
254 Java_libs []string
255
256 // List of prebuilt files that are embedded inside this APEX bundle
257 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900258
259 // Name of the apex_key module that provides the private key to sign APEX
260 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900261
Alex Light5098a612018-11-29 17:12:15 -0800262 // The type of APEX to build. Controls what the APEX payload is. Either
263 // 'image', 'zip' or 'both'. Default: 'image'.
264 Payload_type *string
265
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900266 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
267 // or an android_app_certificate module name in the form ":module".
268 Certificate *string
269
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900270 // Whether this APEX is installable to one of the partitions. Default: true.
271 Installable *bool
272
Jiyong Parkda6eb592018-12-19 17:12:36 +0900273 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
274 // Default is false.
275 Use_vendor *bool
276
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800277 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
278 Ignore_system_library_special_case *bool
279
Alex Light9670d332019-01-29 18:07:33 -0800280 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900281
Jiyong Parkf97782b2019-02-13 20:28:58 +0900282 // List of sanitizer names that this APEX is enabled for
283 SanitizerNames []string `blueprint:"mutated"`
Jiyong Park49932f32019-08-09 14:44:36 +0900284
285 PreventInstall bool `blueprint:"mutated"`
286
287 HideFromMake bool `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800288}
289
290type apexTargetBundleProperties struct {
291 Target struct {
292 // Multilib properties only for android.
293 Android struct {
294 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900295 }
Alex Light9670d332019-01-29 18:07:33 -0800296 // Multilib properties only for host.
297 Host struct {
298 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900299 }
Alex Light9670d332019-01-29 18:07:33 -0800300 // Multilib properties only for host linux_bionic.
301 Linux_bionic struct {
302 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900303 }
Alex Light9670d332019-01-29 18:07:33 -0800304 // Multilib properties only for host linux_glibc.
305 Linux_glibc struct {
306 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900307 }
308 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900309}
310
Jiyong Park8fd61922018-11-08 02:50:25 +0900311type apexFileClass int
312
313const (
314 etc apexFileClass = iota
315 nativeSharedLib
316 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900317 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800318 pyBinary
319 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900320 javaSharedLib
321)
322
Alex Light5098a612018-11-29 17:12:15 -0800323type apexPackaging int
324
325const (
326 imageApex apexPackaging = iota
327 zipApex
328 both
329)
330
331func (a apexPackaging) image() bool {
332 switch a {
333 case imageApex, both:
334 return true
335 }
336 return false
337}
338
339func (a apexPackaging) zip() bool {
340 switch a {
341 case zipApex, both:
342 return true
343 }
344 return false
345}
346
347func (a apexPackaging) suffix() string {
348 switch a {
349 case imageApex:
350 return imageApexSuffix
351 case zipApex:
352 return zipApexSuffix
353 case both:
354 panic(fmt.Errorf("must be either zip or image"))
355 default:
356 panic(fmt.Errorf("unkonwn APEX type %d", a))
357 }
358}
359
360func (a apexPackaging) name() string {
361 switch a {
362 case imageApex:
363 return imageApexType
364 case zipApex:
365 return zipApexType
366 case both:
367 panic(fmt.Errorf("must be either zip or image"))
368 default:
369 panic(fmt.Errorf("unkonwn APEX type %d", a))
370 }
371}
372
Jiyong Park8fd61922018-11-08 02:50:25 +0900373func (class apexFileClass) NameInMake() string {
374 switch class {
375 case etc:
376 return "ETC"
377 case nativeSharedLib:
378 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800379 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900380 return "EXECUTABLES"
381 case javaSharedLib:
382 return "JAVA_LIBRARIES"
383 default:
384 panic(fmt.Errorf("unkonwn class %d", class))
385 }
386}
387
388type apexFile struct {
389 builtFile android.Path
390 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900391 installDir string
392 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900393 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800394 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900395}
396
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900397type apexBundle struct {
398 android.ModuleBase
399 android.DefaultableModuleBase
400
Alex Light9670d332019-01-29 18:07:33 -0800401 properties apexBundleProperties
402 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900403
Alex Light5098a612018-11-29 17:12:15 -0800404 apexTypes apexPackaging
405
Colin Crossa4925902018-11-16 11:36:28 -0800406 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800407 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800408 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900409
Jiyong Park42cca6c2019-04-01 11:15:50 +0900410 public_key_file android.Path
411 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900412
413 container_certificate_file android.Path
414 container_private_key_file android.Path
415
Jiyong Park8fd61922018-11-08 02:50:25 +0900416 // list of files to be included in this apex
417 filesInfo []apexFile
418
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900419 // list of module names that this APEX is depending on
420 externalDeps []string
421
Jiyong Park8fd61922018-11-08 02:50:25 +0900422 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800423
424 testApex bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900425}
426
Jiyong Park397e55e2018-10-24 21:09:55 +0900427func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900428 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900429 // Use *FarVariation* to be able to depend on modules having
430 // conflicting variations with this module. This is required since
431 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
432 // for native shared libs.
433 ctx.AddFarVariationDependencies([]blueprint.Variation{
434 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900435 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900436 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900437 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900438 }, sharedLibTag, native_shared_libs...)
439
440 ctx.AddFarVariationDependencies([]blueprint.Variation{
441 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900442 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900443 }, executableTag, binaries...)
444}
445
Alex Light9670d332019-01-29 18:07:33 -0800446func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
447 if ctx.Os().Class == android.Device {
448 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
449 } else {
450 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
451 if ctx.Os().Bionic() {
452 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
453 } else {
454 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
455 }
456 }
457}
458
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900459func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800460
Jiyong Park397e55e2018-10-24 21:09:55 +0900461 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900462 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800463
464 a.combineProperties(ctx)
465
Jiyong Park397e55e2018-10-24 21:09:55 +0900466 has32BitTarget := false
467 for _, target := range targets {
468 if target.Arch.ArchType.Multilib == "lib32" {
469 has32BitTarget = true
470 }
471 }
472 for i, target := range targets {
473 // When multilib.* is omitted for native_shared_libs, it implies
474 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900475 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900476 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900477 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900478 {Mutator: "link", Variation: "shared"},
479 }, sharedLibTag, a.properties.Native_shared_libs...)
480
Jiyong Park397e55e2018-10-24 21:09:55 +0900481 // Add native modules targetting both ABIs
482 addDependenciesForNativeModules(ctx,
483 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900484 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900485 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900486
Alex Light3d673592019-01-18 14:37:31 -0800487 isPrimaryAbi := i == 0
488 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900489 // When multilib.* is omitted for binaries, it implies
490 // multilib.first.
491 ctx.AddFarVariationDependencies([]blueprint.Variation{
492 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900493 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900494 }, executableTag, a.properties.Binaries...)
495
496 // Add native modules targetting the first ABI
497 addDependenciesForNativeModules(ctx,
498 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900499 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900500 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800501
502 // When multilib.* is omitted for prebuilts, it implies multilib.first.
503 ctx.AddFarVariationDependencies([]blueprint.Variation{
504 {Mutator: "arch", Variation: target.String()},
505 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900506 }
507
508 switch target.Arch.ArchType.Multilib {
509 case "lib32":
510 // Add native modules targetting 32-bit ABI
511 addDependenciesForNativeModules(ctx,
512 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900513 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900514 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900515
516 addDependenciesForNativeModules(ctx,
517 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900518 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900519 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900520 case "lib64":
521 // Add native modules targetting 64-bit ABI
522 addDependenciesForNativeModules(ctx,
523 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900524 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900525 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900526
527 if !has32BitTarget {
528 addDependenciesForNativeModules(ctx,
529 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900530 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900531 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900532 }
Peter Collingbournee1629272019-04-24 14:41:12 -0700533
534 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
535 for _, sanitizer := range ctx.Config().SanitizeDevice() {
536 if sanitizer == "hwaddress" {
537 addDependenciesForNativeModules(ctx,
538 []string{"libclang_rt.hwasan-aarch64-android"},
539 nil, target.String(), a.getImageVariation(config))
540 break
541 }
542 }
543 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900544 }
545
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900546 }
547
Jiyong Parkff1458f2018-10-12 21:49:38 +0900548 ctx.AddFarVariationDependencies([]blueprint.Variation{
549 {Mutator: "arch", Variation: "android_common"},
550 }, javaLibTag, a.properties.Java_libs...)
551
Jiyong Park23c52b02019-02-02 13:13:47 +0900552 if String(a.properties.Key) == "" {
553 ctx.ModuleErrorf("key is missing")
554 return
555 }
556 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900557
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900558 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900559 if cert != "" {
560 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900561 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900562}
563
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900564func (a *apexBundle) getCertString(ctx android.BaseContext) string {
565 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
566 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000567 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900568 }
569 return String(a.properties.Certificate)
570}
571
Jiyong Park74e240b2018-11-27 21:27:08 +0900572func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900573 if file, ok := a.outputFiles[imageApex]; ok {
574 return android.Paths{file}
575 } else {
576 return nil
577 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900578}
579
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900580func (a *apexBundle) installable() bool {
Jiyong Park49932f32019-08-09 14:44:36 +0900581 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900582}
583
Jiyong Park7c1dc612019-01-05 11:15:24 +0900584func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
585 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900586 return "vendor"
587 } else {
588 return "core"
589 }
590}
591
Jiyong Parkf97782b2019-02-13 20:28:58 +0900592func (a *apexBundle) EnableSanitizer(sanitizerName string) {
593 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
594 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
595 }
596}
597
Jiyong Park388ef3f2019-01-28 19:47:32 +0900598func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900599 if android.InList(sanitizerName, a.properties.SanitizerNames) {
600 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900601 }
602
603 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900604 globalSanitizerNames := []string{}
605 if a.Host() {
606 globalSanitizerNames = ctx.Config().SanitizeHost()
607 } else {
608 arches := ctx.Config().SanitizeDeviceArch()
609 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
610 globalSanitizerNames = ctx.Config().SanitizeDevice()
611 }
612 }
613 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900614}
615
Jiyong Park49932f32019-08-09 14:44:36 +0900616func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseContext) bool {
617 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
618}
619
620func (a *apexBundle) PreventInstall() {
621 a.properties.PreventInstall = true
622}
623
624func (a *apexBundle) HideFromMake() {
625 a.properties.HideFromMake = true
626}
627
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800628func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900629 // Decide the APEX-local directory by the multilib of the library
630 // In the future, we may query this to the module.
631 switch cc.Arch().ArchType.Multilib {
632 case "lib32":
633 dirInApex = "lib"
634 case "lib64":
635 dirInApex = "lib64"
636 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900637 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900638 if !cc.Arch().Native {
639 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
640 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800641 if handleSpecialLibs {
642 switch cc.Name() {
643 case "libc", "libm", "libdl":
644 // Special case for bionic libs. This is to prevent the bionic libs
645 // from being included in the search path /apex/com.android.apex/lib.
646 // This exclusion is required because bionic libs in the runtime APEX
647 // are available via the legacy paths /system/lib/libc.so, etc. By the
648 // init process, the bionic libs in the APEX are bind-mounted to the
649 // legacy paths and thus will be loaded into the default linker namespace.
650 // If the bionic libs are directly in /apex/com.android.apex/lib then
651 // the same libs will be again loaded to the runtime linker namespace,
652 // which will result double loading of bionic libs that isn't supported.
653 dirInApex = filepath.Join(dirInApex, "bionic")
654 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900655 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900656
657 fileToCopy = cc.OutputFile().Path()
658 return
659}
660
661func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900662 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900663 fileToCopy = cc.OutputFile().Path()
664 return
665}
666
Alex Light778127a2019-02-27 14:19:50 -0800667func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
668 dirInApex = "bin"
669 fileToCopy = py.HostToolPath().Path()
670 return
671}
672func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
673 dirInApex = "bin"
674 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
675 if err != nil {
676 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
677 return
678 }
679 fileToCopy = android.PathForOutput(ctx, s)
680 return
681}
682
Jiyong Park04480cf2019-02-06 00:16:29 +0900683func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
684 dirInApex = filepath.Join("bin", sh.SubDir())
685 fileToCopy = sh.OutputFile()
686 return
687}
688
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900689func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
690 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900691 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900692 return
693}
694
695func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
696 dirInApex = filepath.Join("etc", prebuilt.SubDir())
697 fileToCopy = prebuilt.OutputFile()
698 return
699}
700
701func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900702 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900703
Alex Light5098a612018-11-29 17:12:15 -0800704 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
705 a.apexTypes = imageApex
706 } else if *a.properties.Payload_type == "zip" {
707 a.apexTypes = zipApex
708 } else if *a.properties.Payload_type == "both" {
709 a.apexTypes = both
710 } else {
711 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
712 return
713 }
714
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800715 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
716
Alex Light778127a2019-02-27 14:19:50 -0800717 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900718 if _, ok := parent.(*apexBundle); ok {
719 // direct dependencies
720 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900721 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900722 switch depTag {
723 case sharedLibTag:
724 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800725 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900726 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900727 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900728 } else {
729 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900730 }
731 case executableTag:
732 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800733 if !cc.Arch().Native {
734 // There is only one 'bin' directory so we shouldn't bother copying in
735 // native-bridge'd binaries and only use main ones.
736 return true
737 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900738 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900739 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900740 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900741 } else if sh, ok := child.(*android.ShBinary); ok {
742 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
743 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800744 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
745 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
746 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
747 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
748 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
749 // NB: Since go binaries are static we don't need the module for anything here, which is
750 // good since the go tool is a blueprint.Module not an android.Module like we would
751 // normally use.
752 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900753 } else {
Alex Light778127a2019-02-27 14:19:50 -0800754 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 +0900755 }
756 case javaLibTag:
757 if java, ok := child.(*java.Library); ok {
758 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900759 if fileToCopy == nil {
760 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
761 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900762 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900763 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900764 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900765 } else {
766 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900767 }
768 case prebuiltTag:
769 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
770 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900771 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900772 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900773 } else {
774 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
775 }
776 case keyTag:
777 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900778 a.private_key_file = key.private_key_file
779 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900780 return false
781 } else {
782 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900783 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900784 case certificateTag:
785 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900786 a.container_certificate_file = dep.Certificate.Pem
787 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900788 return false
789 } else {
790 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
791 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900792 }
793 } else {
794 // indirect dependencies
795 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
796 if cc, ok := child.(*cc.Module); ok {
Alex Light49ae3d92019-02-21 14:02:46 -0800797 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900798 // If the dependency is a stubs lib, don't include it in this APEX,
799 // but make sure that the lib is installed on the device.
800 // In case no APEX is having the lib, the lib is installed to the system
801 // partition.
Alex Light49ae3d92019-02-21 14:02:46 -0800802 //
803 // Always include if we are a host-apex however since those won't have any
804 // system libraries.
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900805 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
806 a.externalDeps = append(a.externalDeps, cc.Name())
807 }
808 // Don't track further
Jiyong Park25fc6a92018-11-18 18:02:45 +0900809 return false
810 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900811 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800812 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900813 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900814 return true
815 }
816 }
817 }
818 return false
819 })
820
Jiyong Park9335a262018-12-24 11:31:58 +0900821 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900822 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900823 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
824 return
825 }
826
Jiyong Park8fd61922018-11-08 02:50:25 +0900827 // remove duplicates in filesInfo
828 removeDup := func(filesInfo []apexFile) []apexFile {
829 encountered := make(map[android.Path]bool)
830 result := []apexFile{}
831 for _, f := range filesInfo {
832 if !encountered[f.builtFile] {
833 encountered[f.builtFile] = true
834 result = append(result, f)
835 }
836 }
837 return result
838 }
839 filesInfo = removeDup(filesInfo)
840
841 // to have consistent build rules
842 sort.Slice(filesInfo, func(i, j int) bool {
843 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
844 })
845
846 // prepend the name of this APEX to the module names. These names will be the names of
847 // modules that will be defined if the APEX is flattened.
848 for i := range filesInfo {
849 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
850 }
851
Jiyong Park8fd61922018-11-08 02:50:25 +0900852 a.installDir = android.PathForModuleInstall(ctx, "apex")
853 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800854
855 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900856 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800857 }
858 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900859 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
860 // is true. This is to support referencing APEX via ":<module_name" syntax
861 // in other modules. It is in AndroidMk where the selection of flattened
862 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900863 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +0900864 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900865 }
866}
867
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700868func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +0900869 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900870 for _, f := range a.filesInfo {
871 if f.module != nil {
872 notice := f.module.NoticeFile()
873 if notice.Valid() {
874 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900875 }
876 }
877 }
878 // append the notice file specified in the apex module itself
879 if a.NoticeFile().Valid() {
880 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900881 }
882
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700883 if len(noticeFiles) == 0 {
884 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900885 }
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700886
887 return android.OptionalPathForPath(
888 android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)))
Jiyong Park52818fc2019-03-18 12:01:38 +0900889}
890
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900891func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900892 cert := String(a.properties.Certificate)
893 if cert != "" && android.SrcIsModule(cert) == "" {
894 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900895 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
896 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900897 } else if cert == "" {
898 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900899 a.container_certificate_file = pem
900 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900901 }
902
Colin Cross8a497952019-03-05 22:25:09 -0800903 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900904
Alex Light5098a612018-11-29 17:12:15 -0800905 var abis []string
906 for _, target := range ctx.MultiTargets() {
907 if len(target.Arch.Abi) > 0 {
908 abis = append(abis, target.Arch.Abi[0])
909 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900910 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900911
Alex Light5098a612018-11-29 17:12:15 -0800912 abis = android.FirstUniqueStrings(abis)
913
914 suffix := apexType.suffix()
915 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900916
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900917 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900918 for _, f := range a.filesInfo {
919 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900920 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900921
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900922 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900923 for i, src := range filesToCopy {
924 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800925 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900926 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
927 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800928 for _, sym := range a.filesInfo[i].symlinks {
929 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
930 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
931 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900932 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900933 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800934 implicitInputs = append(implicitInputs, manifest)
935
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900936 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
937 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900938
Alex Light5098a612018-11-29 17:12:15 -0800939 if apexType.image() {
940 // files and dirs that will be created in APEX
941 var readOnlyPaths []string
942 var executablePaths []string // this also includes dirs
943 for _, f := range a.filesInfo {
944 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
945 if f.installDir == "bin" {
946 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800947 for _, s := range f.symlinks {
948 executablePaths = append(executablePaths, filepath.Join("bin", s))
949 }
Alex Light5098a612018-11-29 17:12:15 -0800950 } else {
951 readOnlyPaths = append(readOnlyPaths, pathInApex)
952 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900953 dir := f.installDir
954 for !android.InList(dir, executablePaths) && dir != "" {
955 executablePaths = append(executablePaths, dir)
956 dir, _ = filepath.Split(dir) // move up to the parent
957 if len(dir) > 0 {
958 // remove trailing slash
959 dir = dir[:len(dir)-1]
960 }
Alex Light5098a612018-11-29 17:12:15 -0800961 }
962 }
963 sort.Strings(readOnlyPaths)
964 sort.Strings(executablePaths)
965 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
966 ctx.Build(pctx, android.BuildParams{
967 Rule: generateFsConfig,
968 Output: cannedFsConfig,
969 Description: "generate fs config",
970 Args: map[string]string{
971 "ro_paths": strings.Join(readOnlyPaths, " "),
972 "exec_paths": strings.Join(executablePaths, " "),
973 },
974 })
975
976 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
977 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
978 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
979 if !fileContextsOptionalPath.Valid() {
980 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
981 return
982 }
983 fileContexts := fileContextsOptionalPath.Path()
984
Jiyong Park835d82b2018-12-27 16:04:18 +0900985 optFlags := []string{}
986
Alex Light5098a612018-11-29 17:12:15 -0800987 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +0900988 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
989 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -0800990
Jiyong Park7f67f482019-01-05 12:57:48 +0900991 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
992 if overridden {
993 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
994 }
995
Jiyong Park40e26a22019-02-08 02:53:06 +0900996 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800997 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +0900998 implicitInputs = append(implicitInputs, androidManifestFile)
999 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1000 }
1001
Jiyong Parkd37a8822019-04-18 17:25:49 +09001002 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1003 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1004 ctx.Config().UnbundledBuild() &&
1005 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1006 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1007 apiFingerprint := java.ApiFingerprintPath(ctx)
1008 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1009 implicitInputs = append(implicitInputs, apiFingerprint)
1010 }
1011 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1012
Jaewoong Jungd6585fe2019-06-18 13:09:13 -07001013 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1014 if noticeFile.Valid() {
1015 // If there's a NOTICE file, embed it as an asset file in the APEX.
1016 implicitInputs = append(implicitInputs, noticeFile.Path())
1017 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1018 }
1019
Alex Light5098a612018-11-29 17:12:15 -08001020 ctx.Build(pctx, android.BuildParams{
1021 Rule: apexRule,
1022 Implicits: implicitInputs,
1023 Output: unsignedOutputFile,
1024 Description: "apex (" + apexType.name() + ")",
1025 Args: map[string]string{
1026 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1027 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1028 "copy_commands": strings.Join(copyCommands, " && "),
1029 "manifest": manifest.String(),
1030 "file_contexts": fileContexts.String(),
1031 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001032 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001033 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001034 },
1035 })
1036
1037 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1038 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1039 a.bundleModuleFile = bundleModuleFile
1040
1041 ctx.Build(pctx, android.BuildParams{
1042 Rule: apexProtoConvertRule,
1043 Input: unsignedOutputFile,
1044 Output: apexProtoFile,
1045 Description: "apex proto convert",
1046 })
1047
1048 ctx.Build(pctx, android.BuildParams{
1049 Rule: apexBundleRule,
1050 Input: apexProtoFile,
1051 Output: a.bundleModuleFile,
1052 Description: "apex bundle module",
1053 Args: map[string]string{
1054 "abi": strings.Join(abis, "."),
1055 },
1056 })
1057 } else {
1058 ctx.Build(pctx, android.BuildParams{
1059 Rule: zipApexRule,
1060 Implicits: implicitInputs,
1061 Output: unsignedOutputFile,
1062 Description: "apex (" + apexType.name() + ")",
1063 Args: map[string]string{
1064 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1065 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1066 "copy_commands": strings.Join(copyCommands, " && "),
1067 "manifest": manifest.String(),
1068 },
1069 })
Colin Crossa4925902018-11-16 11:36:28 -08001070 }
Colin Crossa4925902018-11-16 11:36:28 -08001071
Alex Light5098a612018-11-29 17:12:15 -08001072 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001073 ctx.Build(pctx, android.BuildParams{
1074 Rule: java.Signapk,
1075 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001076 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001077 Input: unsignedOutputFile,
1078 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001079 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001080 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001081 },
1082 })
Alex Light5098a612018-11-29 17:12:15 -08001083
1084 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001085 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001086 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001087 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001088}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001089
Jiyong Park8fd61922018-11-08 02:50:25 +09001090func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001091 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001092 // 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 +09001093 // with other ordinary files.
Colin Cross8a497952019-03-05 22:25:09 -08001094 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +09001095
1096 // rename to apex_manifest.json
1097 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
1098 ctx.Build(pctx, android.BuildParams{
1099 Rule: android.Cp,
1100 Input: manifest,
1101 Output: copiedManifest,
1102 })
Jiyong Park719b4462019-01-13 00:39:51 +09001103 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001104
Jiyong Park42cca6c2019-04-01 11:15:50 +09001105 // rename to apex_pubkey
1106 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1107 ctx.Build(pctx, android.BuildParams{
1108 Rule: android.Cp,
1109 Input: a.public_key_file,
1110 Output: copiedPubkey,
1111 })
1112 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1113
Jiyong Park23c52b02019-02-02 13:13:47 +09001114 if ctx.Config().FlattenApex() {
1115 for _, fi := range a.filesInfo {
1116 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001117 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1118 for _, sym := range fi.symlinks {
1119 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1120 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001121 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001122 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001123 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001124}
1125
1126func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Park49932f32019-08-09 14:44:36 +09001127 if a.properties.HideFromMake {
1128 return android.AndroidMkData{
1129 Disabled: true,
1130 }
1131 }
Alex Light5098a612018-11-29 17:12:15 -08001132 writers := []android.AndroidMkData{}
1133 if a.apexTypes.image() {
1134 writers = append(writers, a.androidMkForType(imageApex))
1135 }
1136 if a.apexTypes.zip() {
1137 writers = append(writers, a.androidMkForType(zipApex))
1138 }
1139 return android.AndroidMkData{
1140 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1141 for _, data := range writers {
1142 data.Custom(w, name, prefix, moduleDir, data)
1143 }
1144 }}
1145}
1146
Alex Lightf1801bc2019-02-13 11:10:07 -08001147func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001148 moduleNames := []string{}
1149
1150 for _, fi := range a.filesInfo {
1151 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1152 continue
1153 }
1154 if !android.InList(fi.moduleName, moduleNames) {
1155 moduleNames = append(moduleNames, fi.moduleName)
1156 }
1157 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1158 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1159 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001160 // /apex/<name>/{lib|framework|...}
1161 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1162 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001163 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001164 // /system/apex/<name>/{lib|framework|...}
1165 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1166 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001167 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001168 if len(fi.symlinks) > 0 {
1169 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1170 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001171
1172 if fi.module != nil && fi.module.NoticeFile().Valid() {
1173 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1174 }
Jiyong Park94427262019-02-05 23:18:47 +09001175 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001176 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001177 }
1178 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1179 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1180 if fi.module != nil {
1181 archStr := fi.module.Target().Arch.ArchType.String()
1182 host := false
1183 switch fi.module.Target().Os.Class {
1184 case android.Host:
1185 if archStr != "common" {
1186 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1187 }
1188 host = true
1189 case android.HostCross:
1190 if archStr != "common" {
1191 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1192 }
1193 host = true
1194 case android.Device:
1195 if archStr != "common" {
1196 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1197 }
1198 }
1199 if host {
1200 makeOs := fi.module.Target().Os.String()
1201 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1202 makeOs = "linux"
1203 }
1204 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1205 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1206 }
1207 }
1208 if fi.class == javaSharedLib {
1209 javaModule := fi.module.(*java.Library)
1210 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1211 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1212 // we will have foo.jar.jar
1213 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1214 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1215 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1216 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1217 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1218 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1219 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1220 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jiyong Park49932f32019-08-09 14:44:36 +09001221 if cc, ok := fi.module.(*cc.Module); ok {
1222 if cc.UnstrippedOutputFile() != nil {
1223 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1224 }
1225 if cc.CoverageOutputFile().Valid() {
1226 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1227 }
Jiyong Park94427262019-02-05 23:18:47 +09001228 }
1229 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1230 } else {
1231 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1232 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1233 }
1234 }
1235 return moduleNames
1236}
1237
Alex Light5098a612018-11-29 17:12:15 -08001238func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001239 return android.AndroidMkData{
1240 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1241 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001242 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001243 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001244 }
1245
Jiyong Park719b4462019-01-13 00:39:51 +09001246 if a.flattened && apexType.image() {
1247 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001248 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1249 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1250 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001251 if len(moduleNames) > 0 {
1252 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1253 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001254 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001255 } else {
Alex Light5098a612018-11-29 17:12:15 -08001256 // zip-apex is the less common type so have the name refer to the image-apex
1257 // only and use {name}.zip if you want the zip-apex
1258 if apexType == zipApex && a.apexTypes == both {
1259 name = name + ".zip"
1260 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001261 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1262 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1263 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1264 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001265 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001266 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001267 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001268 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001269 if len(moduleNames) > 0 {
1270 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1271 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001272 if len(a.externalDeps) > 0 {
1273 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1274 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001275 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001276
Alex Light5098a612018-11-29 17:12:15 -08001277 if apexType == imageApex {
1278 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1279 }
Jiyong Park719b4462019-01-13 00:39:51 +09001280 }
1281 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001282}
1283
Alex Light0851b882019-02-07 13:20:53 -08001284func testApexBundleFactory() android.Module {
1285 return ApexBundleFactory( /*testApex*/ true)
1286}
1287
1288func apexBundleFactory() android.Module {
1289 return ApexBundleFactory( /*testApex*/ false)
1290}
1291
1292func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001293 module := &apexBundle{
1294 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001295 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001296 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001297 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001298 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001299 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001300 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1301 })
Alex Light5098a612018-11-29 17:12:15 -08001302 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001303 android.InitDefaultableModule(module)
1304 return module
1305}
Jiyong Park30ca9372019-02-07 16:27:23 +09001306
1307//
1308// Defaults
1309//
1310type Defaults struct {
1311 android.ModuleBase
1312 android.DefaultsModuleBase
1313}
1314
1315func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1316}
1317
1318func defaultsFactory() android.Module {
1319 return DefaultsFactory()
1320}
1321
1322func DefaultsFactory(props ...interface{}) android.Module {
1323 module := &Defaults{}
1324
1325 module.AddProperties(props...)
1326 module.AddProperties(
1327 &apexBundleProperties{},
1328 &apexTargetBundleProperties{},
1329 )
1330
1331 android.InitDefaultsModule(module)
1332 return module
1333}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001334
1335//
1336// Prebuilt APEX
1337//
1338type Prebuilt struct {
1339 android.ModuleBase
1340 prebuilt android.Prebuilt
1341
1342 properties PrebuiltProperties
1343
Nikita Ioffeed75f612019-04-04 18:09:48 +01001344 inputApex android.Path
1345 installDir android.OutputPath
1346 installFilename string
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001347 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001348}
1349
1350type PrebuiltProperties struct {
1351 // the path to the prebuilt .apex file to import.
Jiyong Park0a573d72019-07-07 12:39:16 +09001352 Source string `blueprint:"mutated"`
1353 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001354
1355 Src *string
1356 Arch struct {
1357 Arm struct {
1358 Src *string
1359 }
1360 Arm64 struct {
1361 Src *string
1362 }
1363 X86 struct {
1364 Src *string
1365 }
1366 X86_64 struct {
1367 Src *string
1368 }
1369 }
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001370
1371 Installable *bool
Nikita Ioffeed75f612019-04-04 18:09:48 +01001372 // Optional name for the installed apex. If unspecified, name of the
1373 // module is used as the file name
1374 Filename *string
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001375}
1376
1377func (p *Prebuilt) installable() bool {
1378 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001379}
1380
1381func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park53554e22019-07-15 15:31:16 +09001382 // If the device is configured to use flattened APEX, force disable the prebuilt because
1383 // the prebuilt is a non-flattened one.
1384 forceDisable := ctx.Config().FlattenApex()
1385
1386 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1387 // to build the prebuilts themselves.
Jiyong Park895e2242019-07-17 08:21:36 +09001388 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park3b98a502019-07-11 11:24:41 +09001389
Kun Niu1bc40c52019-07-29 16:28:57 -07001390 // Force disable the prebuilts when coverage is enabled.
1391 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1392 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1393
Jiyong Park3b98a502019-07-11 11:24:41 +09001394 // b/137216042 don't use prebuilts when address sanitizer is on
1395 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1396 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1397
1398 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park0a573d72019-07-07 12:39:16 +09001399 p.properties.ForceDisable = true
1400 return
1401 }
1402
Jiyong Parkc95714e2019-03-29 14:23:10 +09001403 // This is called before prebuilt_select and prebuilt_postdeps mutators
1404 // The mutators requires that src to be set correctly for each arch so that
1405 // arch variants are disabled when src is not provided for the arch.
1406 if len(ctx.MultiTargets()) != 1 {
1407 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1408 return
1409 }
1410 var src string
1411 switch ctx.MultiTargets()[0].Arch.ArchType {
1412 case android.Arm:
1413 src = String(p.properties.Arch.Arm.Src)
1414 case android.Arm64:
1415 src = String(p.properties.Arch.Arm64.Src)
1416 case android.X86:
1417 src = String(p.properties.Arch.X86.Src)
1418 case android.X86_64:
1419 src = String(p.properties.Arch.X86_64.Src)
1420 default:
1421 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1422 return
1423 }
1424 if src == "" {
1425 src = String(p.properties.Src)
1426 }
1427 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001428}
1429
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001430func (p *Prebuilt) Srcs() android.Paths {
1431 return android.Paths{p.outputApex}
1432}
1433
Jiyong Parka41f12a2019-04-23 18:00:10 +09001434func (p *Prebuilt) InstallFilename() string {
1435 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1436}
1437
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001438func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park0a573d72019-07-07 12:39:16 +09001439 if p.properties.ForceDisable {
1440 return
1441 }
1442
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001443 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001444 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001445 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Parka41f12a2019-04-23 18:00:10 +09001446 p.installFilename = p.InstallFilename()
Nikita Ioffeed75f612019-04-04 18:09:48 +01001447 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1448 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1449 }
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001450 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1451 ctx.Build(pctx, android.BuildParams{
1452 Rule: android.Cp,
1453 Input: p.inputApex,
1454 Output: p.outputApex,
1455 })
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001456 if p.installable() {
Nikita Ioffeed75f612019-04-04 18:09:48 +01001457 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001458 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001459}
1460
1461func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1462 return &p.prebuilt
1463}
1464
1465func (p *Prebuilt) Name() string {
1466 return p.prebuilt.Name(p.ModuleBase.Name())
1467}
1468
1469func (p *Prebuilt) AndroidMk() android.AndroidMkData {
1470 return android.AndroidMkData{
1471 Class: "ETC",
1472 OutputFile: android.OptionalPathForPath(p.inputApex),
1473 Include: "$(BUILD_PREBUILT)",
1474 Extra: []android.AndroidMkExtraFunc{
1475 func(w io.Writer, outputFile android.Path) {
1476 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
Nikita Ioffeed75f612019-04-04 18:09:48 +01001477 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", p.installFilename)
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001478 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.installable())
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001479 },
1480 },
1481 }
1482}
1483
1484// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1485func PrebuiltFactory() android.Module {
1486 module := &Prebuilt{}
1487 module.AddProperties(&module.properties)
Jiyong Parkc95714e2019-03-29 14:23:10 +09001488 android.InitSingleSourcePrebuiltModule(module, &module.properties.Source)
1489 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001490 return module
1491}
Jaewoong Jung9c49b282020-05-14 14:15:24 -07001492
1493type ApexSet struct {
1494 android.ModuleBase
1495 prebuilt android.Prebuilt
1496
1497 properties ApexSetProperties
1498
1499 installDir android.OutputPath
1500 installFilename string
1501 outputApex android.WritablePath
1502}
1503
1504type ApexSetProperties struct {
1505 // the .apks file path that contains prebuilt apex files to be extracted.
1506 Set string
1507
1508 // whether the extracted apex file installable.
1509 Installable *bool
1510
1511 // optional name for the installed apex. If unspecified, name of the
1512 // module is used as the file name
1513 Filename *string
1514
1515 // names of modules to be overridden. Listed modules can only be other binaries
1516 // (in Make or Soong).
1517 // This does not completely prevent installation of the overridden binaries, but if both
1518 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1519 // from PRODUCT_PACKAGES.
1520 Overrides []string
1521
1522 // apexes in this set use prerelease SDK version
1523 Prerelease *bool
1524}
1525
1526func (a *ApexSet) installable() bool {
1527 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
1528}
1529
1530func (a *ApexSet) InstallFilename() string {
1531 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
1532}
1533
1534func (a *ApexSet) Prebuilt() *android.Prebuilt {
1535 return &a.prebuilt
1536}
1537
1538func (a *ApexSet) Name() string {
1539 return a.prebuilt.Name(a.ModuleBase.Name())
1540}
1541
Jiyong Park4a9f5122020-06-12 17:26:31 +09001542func (a *ApexSet) Overrides() []string {
1543 return a.properties.Overrides
1544}
1545
Jaewoong Jung9c49b282020-05-14 14:15:24 -07001546// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1547func apexSetFactory() android.Module {
1548 module := &ApexSet{}
1549 module.AddProperties(&module.properties)
1550 android.InitSingleSourcePrebuiltModule(module, &module.properties.Set)
1551 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1552 return module
1553}
1554
1555func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1556 a.installFilename = a.InstallFilename()
1557 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
1558 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
1559 }
1560
1561 apexSet := a.prebuilt.SingleSourcePath(ctx)
1562 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
1563 ctx.Build(pctx,
1564 android.BuildParams{
1565 Rule: extractMatchingApex,
1566 Description: "Extract an apex from an apex set",
1567 Inputs: android.Paths{apexSet},
1568 Output: a.outputApex,
1569 Args: map[string]string{
1570 "abis": strings.Join(java.SupportedAbis(ctx), ","),
1571 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
1572 "sdk-version": ctx.Config().PlatformSdkVersion(),
1573 },
1574 })
1575 a.installDir = android.PathForModuleInstall(ctx, "apex")
1576 if a.installable() {
1577 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
1578 }
1579}
1580
1581func (a *ApexSet) AndroidMk() android.AndroidMkData {
1582 return android.AndroidMkData{
1583 Class: "ETC",
1584 OutputFile: android.OptionalPathForPath(a.outputApex),
1585 Include: "$(BUILD_PREBUILT)",
1586 Extra: []android.AndroidMkExtraFunc{
1587 func(w io.Writer, outputFile android.Path) {
1588 fmt.Fprintln(w, "LOCAL_MODULE_PATH := ", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
1589 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", a.installFilename)
1590 if !a.installable() {
1591 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
1592 }
1593 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.properties.Overrides, " "))
1594 },
1595 },
1596 }
1597}