blob: 4dca961535fd4daf7fdab7544663f5d17eb7f962 [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 }
533 }
534
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900535 }
536
Jiyong Parkff1458f2018-10-12 21:49:38 +0900537 ctx.AddFarVariationDependencies([]blueprint.Variation{
538 {Mutator: "arch", Variation: "android_common"},
539 }, javaLibTag, a.properties.Java_libs...)
540
Jiyong Park23c52b02019-02-02 13:13:47 +0900541 if String(a.properties.Key) == "" {
542 ctx.ModuleErrorf("key is missing")
543 return
544 }
545 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900546
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900547 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900548 if cert != "" {
549 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900550 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900551}
552
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900553func (a *apexBundle) getCertString(ctx android.BaseContext) string {
554 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
555 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000556 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900557 }
558 return String(a.properties.Certificate)
559}
560
Jiyong Park74e240b2018-11-27 21:27:08 +0900561func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900562 if file, ok := a.outputFiles[imageApex]; ok {
563 return android.Paths{file}
564 } else {
565 return nil
566 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900567}
568
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900569func (a *apexBundle) installable() bool {
Jiyong Park49932f32019-08-09 14:44:36 +0900570 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900571}
572
Jiyong Park7c1dc612019-01-05 11:15:24 +0900573func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
574 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900575 return "vendor"
576 } else {
577 return "core"
578 }
579}
580
Jiyong Parkf97782b2019-02-13 20:28:58 +0900581func (a *apexBundle) EnableSanitizer(sanitizerName string) {
582 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
583 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
584 }
585}
586
Jiyong Park388ef3f2019-01-28 19:47:32 +0900587func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900588 if android.InList(sanitizerName, a.properties.SanitizerNames) {
589 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900590 }
591
592 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900593 globalSanitizerNames := []string{}
594 if a.Host() {
595 globalSanitizerNames = ctx.Config().SanitizeHost()
596 } else {
597 arches := ctx.Config().SanitizeDeviceArch()
598 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
599 globalSanitizerNames = ctx.Config().SanitizeDevice()
600 }
601 }
602 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900603}
604
Jiyong Park49932f32019-08-09 14:44:36 +0900605func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseContext) bool {
606 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
607}
608
609func (a *apexBundle) PreventInstall() {
610 a.properties.PreventInstall = true
611}
612
613func (a *apexBundle) HideFromMake() {
614 a.properties.HideFromMake = true
615}
616
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800617func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900618 // Decide the APEX-local directory by the multilib of the library
619 // In the future, we may query this to the module.
620 switch cc.Arch().ArchType.Multilib {
621 case "lib32":
622 dirInApex = "lib"
623 case "lib64":
624 dirInApex = "lib64"
625 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900626 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900627 if !cc.Arch().Native {
628 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
629 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800630 if handleSpecialLibs {
631 switch cc.Name() {
632 case "libc", "libm", "libdl":
633 // Special case for bionic libs. This is to prevent the bionic libs
634 // from being included in the search path /apex/com.android.apex/lib.
635 // This exclusion is required because bionic libs in the runtime APEX
636 // are available via the legacy paths /system/lib/libc.so, etc. By the
637 // init process, the bionic libs in the APEX are bind-mounted to the
638 // legacy paths and thus will be loaded into the default linker namespace.
639 // If the bionic libs are directly in /apex/com.android.apex/lib then
640 // the same libs will be again loaded to the runtime linker namespace,
641 // which will result double loading of bionic libs that isn't supported.
642 dirInApex = filepath.Join(dirInApex, "bionic")
643 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900644 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900645
646 fileToCopy = cc.OutputFile().Path()
647 return
648}
649
650func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900651 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900652 fileToCopy = cc.OutputFile().Path()
653 return
654}
655
Alex Light778127a2019-02-27 14:19:50 -0800656func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
657 dirInApex = "bin"
658 fileToCopy = py.HostToolPath().Path()
659 return
660}
661func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
662 dirInApex = "bin"
663 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
664 if err != nil {
665 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
666 return
667 }
668 fileToCopy = android.PathForOutput(ctx, s)
669 return
670}
671
Jiyong Park04480cf2019-02-06 00:16:29 +0900672func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
673 dirInApex = filepath.Join("bin", sh.SubDir())
674 fileToCopy = sh.OutputFile()
675 return
676}
677
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900678func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
679 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900680 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900681 return
682}
683
684func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
685 dirInApex = filepath.Join("etc", prebuilt.SubDir())
686 fileToCopy = prebuilt.OutputFile()
687 return
688}
689
690func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900691 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900692
Alex Light5098a612018-11-29 17:12:15 -0800693 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
694 a.apexTypes = imageApex
695 } else if *a.properties.Payload_type == "zip" {
696 a.apexTypes = zipApex
697 } else if *a.properties.Payload_type == "both" {
698 a.apexTypes = both
699 } else {
700 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
701 return
702 }
703
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800704 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
705
Alex Light778127a2019-02-27 14:19:50 -0800706 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900707 if _, ok := parent.(*apexBundle); ok {
708 // direct dependencies
709 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900710 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900711 switch depTag {
712 case sharedLibTag:
713 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800714 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900715 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900716 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900717 } else {
718 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900719 }
720 case executableTag:
721 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800722 if !cc.Arch().Native {
723 // There is only one 'bin' directory so we shouldn't bother copying in
724 // native-bridge'd binaries and only use main ones.
725 return true
726 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900727 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900728 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900729 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900730 } else if sh, ok := child.(*android.ShBinary); ok {
731 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
732 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800733 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
734 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
735 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
736 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
737 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
738 // NB: Since go binaries are static we don't need the module for anything here, which is
739 // good since the go tool is a blueprint.Module not an android.Module like we would
740 // normally use.
741 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900742 } else {
Alex Light778127a2019-02-27 14:19:50 -0800743 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 +0900744 }
745 case javaLibTag:
746 if java, ok := child.(*java.Library); ok {
747 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900748 if fileToCopy == nil {
749 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
750 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900751 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900752 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900753 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900754 } else {
755 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900756 }
757 case prebuiltTag:
758 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
759 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900760 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900761 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900762 } else {
763 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
764 }
765 case keyTag:
766 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900767 a.private_key_file = key.private_key_file
768 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900769 return false
770 } else {
771 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900772 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900773 case certificateTag:
774 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900775 a.container_certificate_file = dep.Certificate.Pem
776 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900777 return false
778 } else {
779 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
780 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900781 }
782 } else {
783 // indirect dependencies
784 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
785 if cc, ok := child.(*cc.Module); ok {
Alex Light49ae3d92019-02-21 14:02:46 -0800786 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900787 // If the dependency is a stubs lib, don't include it in this APEX,
788 // but make sure that the lib is installed on the device.
789 // In case no APEX is having the lib, the lib is installed to the system
790 // partition.
Alex Light49ae3d92019-02-21 14:02:46 -0800791 //
792 // Always include if we are a host-apex however since those won't have any
793 // system libraries.
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900794 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
795 a.externalDeps = append(a.externalDeps, cc.Name())
796 }
797 // Don't track further
Jiyong Park25fc6a92018-11-18 18:02:45 +0900798 return false
799 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900800 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800801 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900802 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900803 return true
804 }
805 }
806 }
807 return false
808 })
809
Jiyong Park9335a262018-12-24 11:31:58 +0900810 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900811 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900812 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
813 return
814 }
815
Jiyong Park8fd61922018-11-08 02:50:25 +0900816 // remove duplicates in filesInfo
817 removeDup := func(filesInfo []apexFile) []apexFile {
818 encountered := make(map[android.Path]bool)
819 result := []apexFile{}
820 for _, f := range filesInfo {
821 if !encountered[f.builtFile] {
822 encountered[f.builtFile] = true
823 result = append(result, f)
824 }
825 }
826 return result
827 }
828 filesInfo = removeDup(filesInfo)
829
830 // to have consistent build rules
831 sort.Slice(filesInfo, func(i, j int) bool {
832 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
833 })
834
835 // prepend the name of this APEX to the module names. These names will be the names of
836 // modules that will be defined if the APEX is flattened.
837 for i := range filesInfo {
838 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
839 }
840
Jiyong Park8fd61922018-11-08 02:50:25 +0900841 a.installDir = android.PathForModuleInstall(ctx, "apex")
842 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800843
844 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900845 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800846 }
847 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900848 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
849 // is true. This is to support referencing APEX via ":<module_name" syntax
850 // in other modules. It is in AndroidMk where the selection of flattened
851 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900852 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +0900853 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900854 }
855}
856
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700857func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +0900858 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900859 for _, f := range a.filesInfo {
860 if f.module != nil {
861 notice := f.module.NoticeFile()
862 if notice.Valid() {
863 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900864 }
865 }
866 }
867 // append the notice file specified in the apex module itself
868 if a.NoticeFile().Valid() {
869 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900870 }
871
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700872 if len(noticeFiles) == 0 {
873 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900874 }
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700875
876 return android.OptionalPathForPath(
877 android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)))
Jiyong Park52818fc2019-03-18 12:01:38 +0900878}
879
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900880func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900881 cert := String(a.properties.Certificate)
882 if cert != "" && android.SrcIsModule(cert) == "" {
883 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900884 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
885 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900886 } else if cert == "" {
887 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900888 a.container_certificate_file = pem
889 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900890 }
891
Colin Cross8a497952019-03-05 22:25:09 -0800892 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900893
Alex Light5098a612018-11-29 17:12:15 -0800894 var abis []string
895 for _, target := range ctx.MultiTargets() {
896 if len(target.Arch.Abi) > 0 {
897 abis = append(abis, target.Arch.Abi[0])
898 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900899 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900900
Alex Light5098a612018-11-29 17:12:15 -0800901 abis = android.FirstUniqueStrings(abis)
902
903 suffix := apexType.suffix()
904 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900905
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900906 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900907 for _, f := range a.filesInfo {
908 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900909 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900910
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900911 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900912 for i, src := range filesToCopy {
913 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800914 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900915 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
916 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800917 for _, sym := range a.filesInfo[i].symlinks {
918 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
919 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
920 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900921 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900922 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800923 implicitInputs = append(implicitInputs, manifest)
924
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900925 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
926 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900927
Alex Light5098a612018-11-29 17:12:15 -0800928 if apexType.image() {
929 // files and dirs that will be created in APEX
930 var readOnlyPaths []string
931 var executablePaths []string // this also includes dirs
932 for _, f := range a.filesInfo {
933 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
934 if f.installDir == "bin" {
935 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800936 for _, s := range f.symlinks {
937 executablePaths = append(executablePaths, filepath.Join("bin", s))
938 }
Alex Light5098a612018-11-29 17:12:15 -0800939 } else {
940 readOnlyPaths = append(readOnlyPaths, pathInApex)
941 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900942 dir := f.installDir
943 for !android.InList(dir, executablePaths) && dir != "" {
944 executablePaths = append(executablePaths, dir)
945 dir, _ = filepath.Split(dir) // move up to the parent
946 if len(dir) > 0 {
947 // remove trailing slash
948 dir = dir[:len(dir)-1]
949 }
Alex Light5098a612018-11-29 17:12:15 -0800950 }
951 }
952 sort.Strings(readOnlyPaths)
953 sort.Strings(executablePaths)
954 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
955 ctx.Build(pctx, android.BuildParams{
956 Rule: generateFsConfig,
957 Output: cannedFsConfig,
958 Description: "generate fs config",
959 Args: map[string]string{
960 "ro_paths": strings.Join(readOnlyPaths, " "),
961 "exec_paths": strings.Join(executablePaths, " "),
962 },
963 })
964
965 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
966 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
967 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
968 if !fileContextsOptionalPath.Valid() {
969 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
970 return
971 }
972 fileContexts := fileContextsOptionalPath.Path()
973
Jiyong Park835d82b2018-12-27 16:04:18 +0900974 optFlags := []string{}
975
Alex Light5098a612018-11-29 17:12:15 -0800976 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +0900977 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
978 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -0800979
Jiyong Park7f67f482019-01-05 12:57:48 +0900980 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
981 if overridden {
982 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
983 }
984
Jiyong Park40e26a22019-02-08 02:53:06 +0900985 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800986 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +0900987 implicitInputs = append(implicitInputs, androidManifestFile)
988 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
989 }
990
Jiyong Parkd37a8822019-04-18 17:25:49 +0900991 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
992 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
993 ctx.Config().UnbundledBuild() &&
994 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
995 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
996 apiFingerprint := java.ApiFingerprintPath(ctx)
997 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
998 implicitInputs = append(implicitInputs, apiFingerprint)
999 }
1000 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1001
Jaewoong Jungd6585fe2019-06-18 13:09:13 -07001002 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1003 if noticeFile.Valid() {
1004 // If there's a NOTICE file, embed it as an asset file in the APEX.
1005 implicitInputs = append(implicitInputs, noticeFile.Path())
1006 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1007 }
1008
Alex Light5098a612018-11-29 17:12:15 -08001009 ctx.Build(pctx, android.BuildParams{
1010 Rule: apexRule,
1011 Implicits: implicitInputs,
1012 Output: unsignedOutputFile,
1013 Description: "apex (" + apexType.name() + ")",
1014 Args: map[string]string{
1015 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1016 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1017 "copy_commands": strings.Join(copyCommands, " && "),
1018 "manifest": manifest.String(),
1019 "file_contexts": fileContexts.String(),
1020 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001021 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001022 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001023 },
1024 })
1025
1026 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1027 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1028 a.bundleModuleFile = bundleModuleFile
1029
1030 ctx.Build(pctx, android.BuildParams{
1031 Rule: apexProtoConvertRule,
1032 Input: unsignedOutputFile,
1033 Output: apexProtoFile,
1034 Description: "apex proto convert",
1035 })
1036
1037 ctx.Build(pctx, android.BuildParams{
1038 Rule: apexBundleRule,
1039 Input: apexProtoFile,
1040 Output: a.bundleModuleFile,
1041 Description: "apex bundle module",
1042 Args: map[string]string{
1043 "abi": strings.Join(abis, "."),
1044 },
1045 })
1046 } else {
1047 ctx.Build(pctx, android.BuildParams{
1048 Rule: zipApexRule,
1049 Implicits: implicitInputs,
1050 Output: unsignedOutputFile,
1051 Description: "apex (" + apexType.name() + ")",
1052 Args: map[string]string{
1053 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1054 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1055 "copy_commands": strings.Join(copyCommands, " && "),
1056 "manifest": manifest.String(),
1057 },
1058 })
Colin Crossa4925902018-11-16 11:36:28 -08001059 }
Colin Crossa4925902018-11-16 11:36:28 -08001060
Alex Light5098a612018-11-29 17:12:15 -08001061 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001062 ctx.Build(pctx, android.BuildParams{
1063 Rule: java.Signapk,
1064 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001065 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001066 Input: unsignedOutputFile,
1067 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001068 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001069 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001070 },
1071 })
Alex Light5098a612018-11-29 17:12:15 -08001072
1073 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001074 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001075 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001076 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001077}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001078
Jiyong Park8fd61922018-11-08 02:50:25 +09001079func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001080 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001081 // 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 +09001082 // with other ordinary files.
Colin Cross8a497952019-03-05 22:25:09 -08001083 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +09001084
1085 // rename to apex_manifest.json
1086 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
1087 ctx.Build(pctx, android.BuildParams{
1088 Rule: android.Cp,
1089 Input: manifest,
1090 Output: copiedManifest,
1091 })
Jiyong Park719b4462019-01-13 00:39:51 +09001092 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001093
Jiyong Park42cca6c2019-04-01 11:15:50 +09001094 // rename to apex_pubkey
1095 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1096 ctx.Build(pctx, android.BuildParams{
1097 Rule: android.Cp,
1098 Input: a.public_key_file,
1099 Output: copiedPubkey,
1100 })
1101 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1102
Jiyong Park23c52b02019-02-02 13:13:47 +09001103 if ctx.Config().FlattenApex() {
1104 for _, fi := range a.filesInfo {
1105 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001106 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1107 for _, sym := range fi.symlinks {
1108 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1109 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001110 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001111 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001112 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001113}
1114
1115func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Park49932f32019-08-09 14:44:36 +09001116 if a.properties.HideFromMake {
1117 return android.AndroidMkData{
1118 Disabled: true,
1119 }
1120 }
Alex Light5098a612018-11-29 17:12:15 -08001121 writers := []android.AndroidMkData{}
1122 if a.apexTypes.image() {
1123 writers = append(writers, a.androidMkForType(imageApex))
1124 }
1125 if a.apexTypes.zip() {
1126 writers = append(writers, a.androidMkForType(zipApex))
1127 }
1128 return android.AndroidMkData{
1129 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1130 for _, data := range writers {
1131 data.Custom(w, name, prefix, moduleDir, data)
1132 }
1133 }}
1134}
1135
Alex Lightf1801bc2019-02-13 11:10:07 -08001136func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001137 moduleNames := []string{}
1138
1139 for _, fi := range a.filesInfo {
1140 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1141 continue
1142 }
1143 if !android.InList(fi.moduleName, moduleNames) {
1144 moduleNames = append(moduleNames, fi.moduleName)
1145 }
1146 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1147 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1148 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001149 // /apex/<name>/{lib|framework|...}
1150 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1151 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001152 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001153 // /system/apex/<name>/{lib|framework|...}
1154 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1155 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001156 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001157 if len(fi.symlinks) > 0 {
1158 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1159 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001160
1161 if fi.module != nil && fi.module.NoticeFile().Valid() {
1162 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1163 }
Jiyong Park94427262019-02-05 23:18:47 +09001164 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001165 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001166 }
1167 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1168 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1169 if fi.module != nil {
1170 archStr := fi.module.Target().Arch.ArchType.String()
1171 host := false
1172 switch fi.module.Target().Os.Class {
1173 case android.Host:
1174 if archStr != "common" {
1175 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1176 }
1177 host = true
1178 case android.HostCross:
1179 if archStr != "common" {
1180 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1181 }
1182 host = true
1183 case android.Device:
1184 if archStr != "common" {
1185 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1186 }
1187 }
1188 if host {
1189 makeOs := fi.module.Target().Os.String()
1190 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1191 makeOs = "linux"
1192 }
1193 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1194 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1195 }
1196 }
1197 if fi.class == javaSharedLib {
1198 javaModule := fi.module.(*java.Library)
1199 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1200 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1201 // we will have foo.jar.jar
1202 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1203 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1204 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1205 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1206 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1207 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1208 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1209 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jiyong Park49932f32019-08-09 14:44:36 +09001210 if cc, ok := fi.module.(*cc.Module); ok {
1211 if cc.UnstrippedOutputFile() != nil {
1212 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1213 }
1214 if cc.CoverageOutputFile().Valid() {
1215 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1216 }
Jiyong Park94427262019-02-05 23:18:47 +09001217 }
1218 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1219 } else {
1220 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1221 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1222 }
1223 }
1224 return moduleNames
1225}
1226
Alex Light5098a612018-11-29 17:12:15 -08001227func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001228 return android.AndroidMkData{
1229 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1230 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001231 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001232 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001233 }
1234
Jiyong Park719b4462019-01-13 00:39:51 +09001235 if a.flattened && apexType.image() {
1236 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001237 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1238 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1239 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001240 if len(moduleNames) > 0 {
1241 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1242 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001243 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001244 } else {
Alex Light5098a612018-11-29 17:12:15 -08001245 // zip-apex is the less common type so have the name refer to the image-apex
1246 // only and use {name}.zip if you want the zip-apex
1247 if apexType == zipApex && a.apexTypes == both {
1248 name = name + ".zip"
1249 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001250 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1251 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1252 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1253 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001254 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001255 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001256 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001257 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001258 if len(moduleNames) > 0 {
1259 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1260 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001261 if len(a.externalDeps) > 0 {
1262 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1263 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001264 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001265
Alex Light5098a612018-11-29 17:12:15 -08001266 if apexType == imageApex {
1267 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1268 }
Jiyong Park719b4462019-01-13 00:39:51 +09001269 }
1270 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001271}
1272
Alex Light0851b882019-02-07 13:20:53 -08001273func testApexBundleFactory() android.Module {
1274 return ApexBundleFactory( /*testApex*/ true)
1275}
1276
1277func apexBundleFactory() android.Module {
1278 return ApexBundleFactory( /*testApex*/ false)
1279}
1280
1281func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001282 module := &apexBundle{
1283 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001284 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001285 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001286 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001287 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001288 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001289 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1290 })
Alex Light5098a612018-11-29 17:12:15 -08001291 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001292 android.InitDefaultableModule(module)
1293 return module
1294}
Jiyong Park30ca9372019-02-07 16:27:23 +09001295
1296//
1297// Defaults
1298//
1299type Defaults struct {
1300 android.ModuleBase
1301 android.DefaultsModuleBase
1302}
1303
1304func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1305}
1306
1307func defaultsFactory() android.Module {
1308 return DefaultsFactory()
1309}
1310
1311func DefaultsFactory(props ...interface{}) android.Module {
1312 module := &Defaults{}
1313
1314 module.AddProperties(props...)
1315 module.AddProperties(
1316 &apexBundleProperties{},
1317 &apexTargetBundleProperties{},
1318 )
1319
1320 android.InitDefaultsModule(module)
1321 return module
1322}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001323
1324//
1325// Prebuilt APEX
1326//
1327type Prebuilt struct {
1328 android.ModuleBase
1329 prebuilt android.Prebuilt
1330
1331 properties PrebuiltProperties
1332
Nikita Ioffeed75f612019-04-04 18:09:48 +01001333 inputApex android.Path
1334 installDir android.OutputPath
1335 installFilename string
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001336 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001337}
1338
1339type PrebuiltProperties struct {
1340 // the path to the prebuilt .apex file to import.
Jiyong Park0a573d72019-07-07 12:39:16 +09001341 Source string `blueprint:"mutated"`
1342 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001343
1344 Src *string
1345 Arch struct {
1346 Arm struct {
1347 Src *string
1348 }
1349 Arm64 struct {
1350 Src *string
1351 }
1352 X86 struct {
1353 Src *string
1354 }
1355 X86_64 struct {
1356 Src *string
1357 }
1358 }
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001359
1360 Installable *bool
Nikita Ioffeed75f612019-04-04 18:09:48 +01001361 // Optional name for the installed apex. If unspecified, name of the
1362 // module is used as the file name
1363 Filename *string
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001364}
1365
1366func (p *Prebuilt) installable() bool {
1367 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001368}
1369
1370func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park53554e22019-07-15 15:31:16 +09001371 // If the device is configured to use flattened APEX, force disable the prebuilt because
1372 // the prebuilt is a non-flattened one.
1373 forceDisable := ctx.Config().FlattenApex()
1374
1375 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1376 // to build the prebuilts themselves.
Jiyong Park895e2242019-07-17 08:21:36 +09001377 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Park3b98a502019-07-11 11:24:41 +09001378
Kun Niu1bc40c52019-07-29 16:28:57 -07001379 // Force disable the prebuilts when coverage is enabled.
1380 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1381 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1382
Jiyong Park3b98a502019-07-11 11:24:41 +09001383 // b/137216042 don't use prebuilts when address sanitizer is on
1384 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1385 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1386
1387 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park0a573d72019-07-07 12:39:16 +09001388 p.properties.ForceDisable = true
1389 return
1390 }
1391
Jiyong Parkc95714e2019-03-29 14:23:10 +09001392 // This is called before prebuilt_select and prebuilt_postdeps mutators
1393 // The mutators requires that src to be set correctly for each arch so that
1394 // arch variants are disabled when src is not provided for the arch.
1395 if len(ctx.MultiTargets()) != 1 {
1396 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1397 return
1398 }
1399 var src string
1400 switch ctx.MultiTargets()[0].Arch.ArchType {
1401 case android.Arm:
1402 src = String(p.properties.Arch.Arm.Src)
1403 case android.Arm64:
1404 src = String(p.properties.Arch.Arm64.Src)
1405 case android.X86:
1406 src = String(p.properties.Arch.X86.Src)
1407 case android.X86_64:
1408 src = String(p.properties.Arch.X86_64.Src)
1409 default:
1410 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1411 return
1412 }
1413 if src == "" {
1414 src = String(p.properties.Src)
1415 }
1416 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001417}
1418
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001419func (p *Prebuilt) Srcs() android.Paths {
1420 return android.Paths{p.outputApex}
1421}
1422
Jiyong Parka41f12a2019-04-23 18:00:10 +09001423func (p *Prebuilt) InstallFilename() string {
1424 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1425}
1426
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001427func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park0a573d72019-07-07 12:39:16 +09001428 if p.properties.ForceDisable {
1429 return
1430 }
1431
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001432 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001433 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001434 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Parka41f12a2019-04-23 18:00:10 +09001435 p.installFilename = p.InstallFilename()
Nikita Ioffeed75f612019-04-04 18:09:48 +01001436 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1437 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1438 }
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001439 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1440 ctx.Build(pctx, android.BuildParams{
1441 Rule: android.Cp,
1442 Input: p.inputApex,
1443 Output: p.outputApex,
1444 })
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001445 if p.installable() {
Nikita Ioffeed75f612019-04-04 18:09:48 +01001446 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001447 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001448}
1449
1450func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1451 return &p.prebuilt
1452}
1453
1454func (p *Prebuilt) Name() string {
1455 return p.prebuilt.Name(p.ModuleBase.Name())
1456}
1457
1458func (p *Prebuilt) AndroidMk() android.AndroidMkData {
1459 return android.AndroidMkData{
1460 Class: "ETC",
1461 OutputFile: android.OptionalPathForPath(p.inputApex),
1462 Include: "$(BUILD_PREBUILT)",
1463 Extra: []android.AndroidMkExtraFunc{
1464 func(w io.Writer, outputFile android.Path) {
1465 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
Nikita Ioffeed75f612019-04-04 18:09:48 +01001466 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", p.installFilename)
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001467 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.installable())
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001468 },
1469 },
1470 }
1471}
1472
1473// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1474func PrebuiltFactory() android.Module {
1475 module := &Prebuilt{}
1476 module.AddProperties(&module.properties)
Jiyong Parkc95714e2019-03-29 14:23:10 +09001477 android.InitSingleSourcePrebuiltModule(module, &module.properties.Source)
1478 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001479 return module
1480}
Jaewoong Jung9c49b282020-05-14 14:15:24 -07001481
1482type ApexSet struct {
1483 android.ModuleBase
1484 prebuilt android.Prebuilt
1485
1486 properties ApexSetProperties
1487
1488 installDir android.OutputPath
1489 installFilename string
1490 outputApex android.WritablePath
1491}
1492
1493type ApexSetProperties struct {
1494 // the .apks file path that contains prebuilt apex files to be extracted.
1495 Set string
1496
1497 // whether the extracted apex file installable.
1498 Installable *bool
1499
1500 // optional name for the installed apex. If unspecified, name of the
1501 // module is used as the file name
1502 Filename *string
1503
1504 // names of modules to be overridden. Listed modules can only be other binaries
1505 // (in Make or Soong).
1506 // This does not completely prevent installation of the overridden binaries, but if both
1507 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1508 // from PRODUCT_PACKAGES.
1509 Overrides []string
1510
1511 // apexes in this set use prerelease SDK version
1512 Prerelease *bool
1513}
1514
1515func (a *ApexSet) installable() bool {
1516 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
1517}
1518
1519func (a *ApexSet) InstallFilename() string {
1520 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
1521}
1522
1523func (a *ApexSet) Prebuilt() *android.Prebuilt {
1524 return &a.prebuilt
1525}
1526
1527func (a *ApexSet) Name() string {
1528 return a.prebuilt.Name(a.ModuleBase.Name())
1529}
1530
Jiyong Park4a9f5122020-06-12 17:26:31 +09001531func (a *ApexSet) Overrides() []string {
1532 return a.properties.Overrides
1533}
1534
Jaewoong Jung9c49b282020-05-14 14:15:24 -07001535// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1536func apexSetFactory() android.Module {
1537 module := &ApexSet{}
1538 module.AddProperties(&module.properties)
1539 android.InitSingleSourcePrebuiltModule(module, &module.properties.Set)
1540 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1541 return module
1542}
1543
1544func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1545 a.installFilename = a.InstallFilename()
1546 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
1547 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
1548 }
1549
1550 apexSet := a.prebuilt.SingleSourcePath(ctx)
1551 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
1552 ctx.Build(pctx,
1553 android.BuildParams{
1554 Rule: extractMatchingApex,
1555 Description: "Extract an apex from an apex set",
1556 Inputs: android.Paths{apexSet},
1557 Output: a.outputApex,
1558 Args: map[string]string{
1559 "abis": strings.Join(java.SupportedAbis(ctx), ","),
1560 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
1561 "sdk-version": ctx.Config().PlatformSdkVersion(),
1562 },
1563 })
1564 a.installDir = android.PathForModuleInstall(ctx, "apex")
1565 if a.installable() {
1566 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
1567 }
1568}
1569
1570func (a *ApexSet) AndroidMk() android.AndroidMkData {
1571 return android.AndroidMkData{
1572 Class: "ETC",
1573 OutputFile: android.OptionalPathForPath(a.outputApex),
1574 Include: "$(BUILD_PREBUILT)",
1575 Extra: []android.AndroidMkExtraFunc{
1576 func(w io.Writer, outputFile android.Path) {
1577 fmt.Fprintln(w, "LOCAL_MODULE_PATH := ", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
1578 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", a.installFilename)
1579 if !a.installable() {
1580 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
1581 }
1582 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.properties.Overrides, " "))
1583 },
1584 },
1585 }
1586}