blob: 4a20936dbecc81338fb99ab8c56b5c337ac013e3 [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
24
25 "android/soong/android"
26 "android/soong/cc"
27 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080028 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090029
30 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080031 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090032 "github.com/google/blueprint/proptools"
33)
34
35var (
36 pctx = android.NewPackageContext("android/apex")
37
38 // Create a canned fs config file where all files and directories are
39 // by default set to (uid/gid/mode) = (1000/1000/0644)
40 // TODO(b/113082813) make this configurable using config.fs syntax
41 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000042 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000043 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090044 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090045 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090047 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090048
49 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
50 // against the binary policy using sefcontext_compiler -p <policy>.
51
52 // TODO(b/114327326): automate the generation of file_contexts
53 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
54 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
55 `(${copy_commands}) && ` +
56 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090057 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090058 `--file_contexts ${file_contexts} ` +
59 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080060 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090061 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090062 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
63 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
Dan Willemsendd651fa2019-06-13 04:48:54 +000064 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
Jiyong Park48ca7dc2018-10-10 14:01:00 +090065 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090066 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080067
Alex Light5098a612018-11-29 17:12:15 -080068 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
69 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
70 `(${copy_commands}) && ` +
71 `APEXER_TOOL_PATH=${tool_path} ` +
72 `${apexer} --force --manifest ${manifest} ` +
73 `--payload_type zip ` +
74 `${image_dir} ${out} `,
75 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
76 Description: "ZipAPEX ${image_dir} => ${out}",
77 }, "tool_path", "image_dir", "copy_commands", "manifest")
78
Colin Crossa4925902018-11-16 11:36:28 -080079 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
80 blueprint.RuleParams{
81 Command: `${aapt2} convert --output-format proto $in -o $out`,
82 CommandDeps: []string{"${aapt2}"},
83 })
84
85 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090086 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000087 `apex_payload.img:apex/${abi}.img ` +
88 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +000089 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -080090 CommandDeps: []string{"${zip2zip}"},
91 Description: "app bundle",
92 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090093)
94
Alex Light5098a612018-11-29 17:12:15 -080095var imageApexSuffix = ".apex"
96var zipApexSuffix = ".zipapex"
97
98var imageApexType = "image"
99var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900100
101type dependencyTag struct {
102 blueprint.BaseDependencyTag
103 name string
104}
105
106var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900107 sharedLibTag = dependencyTag{name: "sharedLib"}
108 executableTag = dependencyTag{name: "executable"}
109 javaLibTag = dependencyTag{name: "javaLib"}
110 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +0100111 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900112 keyTag = dependencyTag{name: "key"}
113 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900114)
115
116func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700117 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900118 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900119 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100120 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
121 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
122 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
123 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000124 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100125 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
126 } else {
127 return pctx.HostBinToolPath(ctx, tool).String()
128 }
129 })
130 }
131 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900132 pctx.HostBinToolVariable("avbtool", "avbtool")
133 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
134 pctx.HostBinToolVariable("merge_zips", "merge_zips")
135 pctx.HostBinToolVariable("mke2fs", "mke2fs")
136 pctx.HostBinToolVariable("resize2fs", "resize2fs")
137 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
138 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800139 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900140 pctx.HostBinToolVariable("zipalign", "zipalign")
141
Alex Light0851b882019-02-07 13:20:53 -0800142 android.RegisterModuleType("apex", apexBundleFactory)
143 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900144 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700145 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900146
147 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
148 ctx.TopDown("apex_deps", apexDepsMutator)
Colin Cross643614d2019-06-19 22:51:38 -0700149 ctx.BottomUp("apex", apexMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900150 })
151}
152
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900153// Mark the direct and transitive dependencies of apex bundles so that they
154// can be built for the apex bundles.
155func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800156 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800157 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900158 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900159 depName := mctx.OtherModuleName(child)
160 // If the parent is apexBundle, this child is directly depended.
161 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800162 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800163 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
164 // non-installable apex's cannot be installed and so should not prevent libraries from being
165 // installed to the system.
166 android.UpdateApexDependency(apexBundleName, depName, directDep)
167 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900168
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900169 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900170 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900171 return true
172 } else {
173 return false
174 }
175 })
176 }
177}
178
179// Create apex variations if a module is included in APEX(s).
180func apexMutator(mctx android.BottomUpMutatorContext) {
181 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900182 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900183 } else if _, ok := mctx.Module().(*apexBundle); ok {
184 // apex bundle itself is mutated so that it and its modules have same
185 // apex variant.
186 apexBundleName := mctx.ModuleName()
187 mctx.CreateVariations(apexBundleName)
188 }
189}
190
Alex Light9670d332019-01-29 18:07:33 -0800191type apexNativeDependencies struct {
192 // List of native libraries
193 Native_shared_libs []string
194 // List of native executables
195 Binaries []string
Roland Levillain630846d2019-06-26 12:48:34 +0100196 // List of native tests
197 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800198}
199type apexMultilibProperties struct {
200 // Native dependencies whose compile_multilib is "first"
201 First apexNativeDependencies
202
203 // Native dependencies whose compile_multilib is "both"
204 Both apexNativeDependencies
205
206 // Native dependencies whose compile_multilib is "prefer32"
207 Prefer32 apexNativeDependencies
208
209 // Native dependencies whose compile_multilib is "32"
210 Lib32 apexNativeDependencies
211
212 // Native dependencies whose compile_multilib is "64"
213 Lib64 apexNativeDependencies
214}
215
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900216type apexBundleProperties struct {
217 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000218 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800219 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900220
Jiyong Park40e26a22019-02-08 02:53:06 +0900221 // AndroidManifest.xml file used for the zip container of this APEX bundle.
222 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800223 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900224
Jiyong Park05e70dd2019-03-18 14:26:32 +0900225 // Canonical name of the APEX bundle in the manifest file.
226 // If unspecified, defaults to the value of name
227 Apex_name *string
228
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900229 // Determines the file contexts file for setting security context to each file in this APEX bundle.
230 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
231 // used.
232 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900233 File_contexts *string
234
235 // List of native shared libs that are embedded inside this APEX bundle
236 Native_shared_libs []string
237
Roland Levillain630846d2019-06-26 12:48:34 +0100238 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900239 Binaries []string
240
241 // List of java libraries that are embedded inside this APEX bundle
242 Java_libs []string
243
244 // List of prebuilt files that are embedded inside this APEX bundle
245 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900246
Roland Levillain630846d2019-06-26 12:48:34 +0100247 // List of tests that are embedded inside this APEX bundle
248 Tests []string
249
Jiyong Parkff1458f2018-10-12 21:49:38 +0900250 // Name of the apex_key module that provides the private key to sign APEX
251 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900252
Alex Light5098a612018-11-29 17:12:15 -0800253 // The type of APEX to build. Controls what the APEX payload is. Either
254 // 'image', 'zip' or 'both'. Default: 'image'.
255 Payload_type *string
256
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900257 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
258 // or an android_app_certificate module name in the form ":module".
259 Certificate *string
260
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900261 // Whether this APEX is installable to one of the partitions. Default: true.
262 Installable *bool
263
Jiyong Parkda6eb592018-12-19 17:12:36 +0900264 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
265 // Default is false.
266 Use_vendor *bool
267
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800268 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
269 Ignore_system_library_special_case *bool
270
Alex Light9670d332019-01-29 18:07:33 -0800271 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900272
Jiyong Parkf97782b2019-02-13 20:28:58 +0900273 // List of sanitizer names that this APEX is enabled for
274 SanitizerNames []string `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800275}
276
277type apexTargetBundleProperties struct {
278 Target struct {
279 // Multilib properties only for android.
280 Android struct {
281 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900282 }
Alex Light9670d332019-01-29 18:07:33 -0800283 // Multilib properties only for host.
284 Host struct {
285 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900286 }
Alex Light9670d332019-01-29 18:07:33 -0800287 // Multilib properties only for host linux_bionic.
288 Linux_bionic struct {
289 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900290 }
Alex Light9670d332019-01-29 18:07:33 -0800291 // Multilib properties only for host linux_glibc.
292 Linux_glibc struct {
293 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900294 }
295 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900296}
297
Jiyong Park8fd61922018-11-08 02:50:25 +0900298type apexFileClass int
299
300const (
301 etc apexFileClass = iota
302 nativeSharedLib
303 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900304 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800305 pyBinary
306 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900307 javaSharedLib
Roland Levillain630846d2019-06-26 12:48:34 +0100308 nativeTest
Jiyong Park8fd61922018-11-08 02:50:25 +0900309)
310
Alex Light5098a612018-11-29 17:12:15 -0800311type apexPackaging int
312
313const (
314 imageApex apexPackaging = iota
315 zipApex
316 both
317)
318
319func (a apexPackaging) image() bool {
320 switch a {
321 case imageApex, both:
322 return true
323 }
324 return false
325}
326
327func (a apexPackaging) zip() bool {
328 switch a {
329 case zipApex, both:
330 return true
331 }
332 return false
333}
334
335func (a apexPackaging) suffix() string {
336 switch a {
337 case imageApex:
338 return imageApexSuffix
339 case zipApex:
340 return zipApexSuffix
341 case both:
342 panic(fmt.Errorf("must be either zip or image"))
343 default:
344 panic(fmt.Errorf("unkonwn APEX type %d", a))
345 }
346}
347
348func (a apexPackaging) name() string {
349 switch a {
350 case imageApex:
351 return imageApexType
352 case zipApex:
353 return zipApexType
354 case both:
355 panic(fmt.Errorf("must be either zip or image"))
356 default:
357 panic(fmt.Errorf("unkonwn APEX type %d", a))
358 }
359}
360
Jiyong Park8fd61922018-11-08 02:50:25 +0900361func (class apexFileClass) NameInMake() string {
362 switch class {
363 case etc:
364 return "ETC"
365 case nativeSharedLib:
366 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800367 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900368 return "EXECUTABLES"
369 case javaSharedLib:
370 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100371 case nativeTest:
372 return "NATIVE_TESTS"
Jiyong Park8fd61922018-11-08 02:50:25 +0900373 default:
374 panic(fmt.Errorf("unkonwn class %d", class))
375 }
376}
377
378type apexFile struct {
379 builtFile android.Path
380 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900381 installDir string
382 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900383 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800384 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900385}
386
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900387type apexBundle struct {
388 android.ModuleBase
389 android.DefaultableModuleBase
390
Alex Light9670d332019-01-29 18:07:33 -0800391 properties apexBundleProperties
392 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900393
Alex Light5098a612018-11-29 17:12:15 -0800394 apexTypes apexPackaging
395
Colin Crossa4925902018-11-16 11:36:28 -0800396 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800397 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800398 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900399
Jiyong Park42cca6c2019-04-01 11:15:50 +0900400 public_key_file android.Path
401 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900402
403 container_certificate_file android.Path
404 container_private_key_file android.Path
405
Jiyong Park8fd61922018-11-08 02:50:25 +0900406 // list of files to be included in this apex
407 filesInfo []apexFile
408
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900409 // list of module names that this APEX is depending on
410 externalDeps []string
411
Jiyong Park8fd61922018-11-08 02:50:25 +0900412 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800413
414 testApex bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900415}
416
Jiyong Park397e55e2018-10-24 21:09:55 +0900417func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100418 native_shared_libs []string, binaries []string, tests []string,
419 arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900420 // Use *FarVariation* to be able to depend on modules having
421 // conflicting variations with this module. This is required since
422 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
423 // for native shared libs.
424 ctx.AddFarVariationDependencies([]blueprint.Variation{
425 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900426 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900427 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900428 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900429 }, sharedLibTag, native_shared_libs...)
430
431 ctx.AddFarVariationDependencies([]blueprint.Variation{
432 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900433 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900434 }, executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100435
436 ctx.AddFarVariationDependencies([]blueprint.Variation{
437 {Mutator: "arch", Variation: arch},
438 {Mutator: "image", Variation: imageVariation},
439 }, testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900440}
441
Alex Light9670d332019-01-29 18:07:33 -0800442func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
443 if ctx.Os().Class == android.Device {
444 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
445 } else {
446 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
447 if ctx.Os().Bionic() {
448 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
449 } else {
450 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
451 }
452 }
453}
454
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900455func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800456
Jiyong Park397e55e2018-10-24 21:09:55 +0900457 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900458 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800459
460 a.combineProperties(ctx)
461
Jiyong Park397e55e2018-10-24 21:09:55 +0900462 has32BitTarget := false
463 for _, target := range targets {
464 if target.Arch.ArchType.Multilib == "lib32" {
465 has32BitTarget = true
466 }
467 }
468 for i, target := range targets {
469 // When multilib.* is omitted for native_shared_libs, it implies
470 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900471 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900472 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900473 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900474 {Mutator: "link", Variation: "shared"},
475 }, sharedLibTag, a.properties.Native_shared_libs...)
476
Roland Levillain630846d2019-06-26 12:48:34 +0100477 // When multilib.* is omitted for tests, it implies
478 // multilib.both.
479 ctx.AddFarVariationDependencies([]blueprint.Variation{
480 {Mutator: "arch", Variation: target.String()},
481 {Mutator: "image", Variation: a.getImageVariation(config)},
482 }, testTag, a.properties.Tests...)
483
Jiyong Park397e55e2018-10-24 21:09:55 +0900484 // Add native modules targetting both ABIs
485 addDependenciesForNativeModules(ctx,
486 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100487 a.properties.Multilib.Both.Binaries,
488 a.properties.Multilib.Both.Tests,
489 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900490 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900491
Alex Light3d673592019-01-18 14:37:31 -0800492 isPrimaryAbi := i == 0
493 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900494 // When multilib.* is omitted for binaries, it implies
495 // multilib.first.
496 ctx.AddFarVariationDependencies([]blueprint.Variation{
497 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900498 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900499 }, executableTag, a.properties.Binaries...)
500
501 // Add native modules targetting the first ABI
502 addDependenciesForNativeModules(ctx,
503 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100504 a.properties.Multilib.First.Binaries,
505 a.properties.Multilib.First.Tests,
506 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900507 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800508
509 // When multilib.* is omitted for prebuilts, it implies multilib.first.
510 ctx.AddFarVariationDependencies([]blueprint.Variation{
511 {Mutator: "arch", Variation: target.String()},
512 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900513 }
514
515 switch target.Arch.ArchType.Multilib {
516 case "lib32":
517 // Add native modules targetting 32-bit ABI
518 addDependenciesForNativeModules(ctx,
519 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100520 a.properties.Multilib.Lib32.Binaries,
521 a.properties.Multilib.Lib32.Tests,
522 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900523 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900524
525 addDependenciesForNativeModules(ctx,
526 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100527 a.properties.Multilib.Prefer32.Binaries,
528 a.properties.Multilib.Prefer32.Tests,
529 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900530 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900531 case "lib64":
532 // Add native modules targetting 64-bit ABI
533 addDependenciesForNativeModules(ctx,
534 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100535 a.properties.Multilib.Lib64.Binaries,
536 a.properties.Multilib.Lib64.Tests,
537 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900538 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900539
540 if !has32BitTarget {
541 addDependenciesForNativeModules(ctx,
542 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100543 a.properties.Multilib.Prefer32.Binaries,
544 a.properties.Multilib.Prefer32.Tests,
545 target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900546 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900547 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700548
549 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
550 for _, sanitizer := range ctx.Config().SanitizeDevice() {
551 if sanitizer == "hwaddress" {
552 addDependenciesForNativeModules(ctx,
553 []string{"libclang_rt.hwasan-aarch64-android"},
Roland Levillain630846d2019-06-26 12:48:34 +0100554 nil, nil, target.String(), a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700555 break
556 }
557 }
558 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900559 }
560
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900561 }
562
Jiyong Parkff1458f2018-10-12 21:49:38 +0900563 ctx.AddFarVariationDependencies([]blueprint.Variation{
564 {Mutator: "arch", Variation: "android_common"},
565 }, javaLibTag, a.properties.Java_libs...)
566
Jiyong Park23c52b02019-02-02 13:13:47 +0900567 if String(a.properties.Key) == "" {
568 ctx.ModuleErrorf("key is missing")
569 return
570 }
571 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900572
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900573 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900574 if cert != "" {
575 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900576 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900577}
578
Colin Cross0ea8ba82019-06-06 14:33:29 -0700579func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900580 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
581 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000582 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900583 }
584 return String(a.properties.Certificate)
585}
586
Colin Cross41955e82019-05-29 14:40:35 -0700587func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
588 switch tag {
589 case "":
590 if file, ok := a.outputFiles[imageApex]; ok {
591 return android.Paths{file}, nil
592 } else {
593 return nil, nil
594 }
595 default:
596 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900597 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900598}
599
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900600func (a *apexBundle) installable() bool {
601 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
602}
603
Jiyong Park7c1dc612019-01-05 11:15:24 +0900604func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
605 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900606 return "vendor"
607 } else {
608 return "core"
609 }
610}
611
Jiyong Parkf97782b2019-02-13 20:28:58 +0900612func (a *apexBundle) EnableSanitizer(sanitizerName string) {
613 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
614 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
615 }
616}
617
Jiyong Park388ef3f2019-01-28 19:47:32 +0900618func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900619 if android.InList(sanitizerName, a.properties.SanitizerNames) {
620 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900621 }
622
623 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900624 globalSanitizerNames := []string{}
625 if a.Host() {
626 globalSanitizerNames = ctx.Config().SanitizeHost()
627 } else {
628 arches := ctx.Config().SanitizeDeviceArch()
629 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
630 globalSanitizerNames = ctx.Config().SanitizeDevice()
631 }
632 }
633 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900634}
635
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800636func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900637 // Decide the APEX-local directory by the multilib of the library
638 // In the future, we may query this to the module.
639 switch cc.Arch().ArchType.Multilib {
640 case "lib32":
641 dirInApex = "lib"
642 case "lib64":
643 dirInApex = "lib64"
644 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900645 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900646 if !cc.Arch().Native {
647 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
648 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800649 if handleSpecialLibs {
650 switch cc.Name() {
651 case "libc", "libm", "libdl":
652 // Special case for bionic libs. This is to prevent the bionic libs
653 // from being included in the search path /apex/com.android.apex/lib.
654 // This exclusion is required because bionic libs in the runtime APEX
655 // are available via the legacy paths /system/lib/libc.so, etc. By the
656 // init process, the bionic libs in the APEX are bind-mounted to the
657 // legacy paths and thus will be loaded into the default linker namespace.
658 // If the bionic libs are directly in /apex/com.android.apex/lib then
659 // the same libs will be again loaded to the runtime linker namespace,
660 // which will result double loading of bionic libs that isn't supported.
661 dirInApex = filepath.Join(dirInApex, "bionic")
662 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900663 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900664
665 fileToCopy = cc.OutputFile().Path()
666 return
667}
668
669func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900670 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900671 fileToCopy = cc.OutputFile().Path()
672 return
673}
674
Alex Light778127a2019-02-27 14:19:50 -0800675func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
676 dirInApex = "bin"
677 fileToCopy = py.HostToolPath().Path()
678 return
679}
680func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
681 dirInApex = "bin"
682 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
683 if err != nil {
684 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
685 return
686 }
687 fileToCopy = android.PathForOutput(ctx, s)
688 return
689}
690
Jiyong Park04480cf2019-02-06 00:16:29 +0900691func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
692 dirInApex = filepath.Join("bin", sh.SubDir())
693 fileToCopy = sh.OutputFile()
694 return
695}
696
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900697func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
698 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900699 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900700 return
701}
702
703func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
704 dirInApex = filepath.Join("etc", prebuilt.SubDir())
705 fileToCopy = prebuilt.OutputFile()
706 return
707}
708
709func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900710 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900711
Alex Light5098a612018-11-29 17:12:15 -0800712 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
713 a.apexTypes = imageApex
714 } else if *a.properties.Payload_type == "zip" {
715 a.apexTypes = zipApex
716 } else if *a.properties.Payload_type == "both" {
717 a.apexTypes = both
718 } else {
719 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
720 return
721 }
722
Roland Levillain630846d2019-06-26 12:48:34 +0100723 if len(a.properties.Tests) > 0 && !a.testApex {
724 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
725 return
726 }
727
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800728 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
729
Alex Light778127a2019-02-27 14:19:50 -0800730 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900731 if _, ok := parent.(*apexBundle); ok {
732 // direct dependencies
733 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900734 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900735 switch depTag {
736 case sharedLibTag:
737 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800738 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900739 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900740 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900741 } else {
742 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900743 }
744 case executableTag:
745 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800746 if !cc.Arch().Native {
747 // There is only one 'bin' directory so we shouldn't bother copying in
748 // native-bridge'd binaries and only use main ones.
749 return true
750 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900751 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900752 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900753 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900754 } else if sh, ok := child.(*android.ShBinary); ok {
755 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
756 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800757 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
758 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
759 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
760 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
761 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
762 // NB: Since go binaries are static we don't need the module for anything here, which is
763 // good since the go tool is a blueprint.Module not an android.Module like we would
764 // normally use.
765 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900766 } else {
Alex Light778127a2019-02-27 14:19:50 -0800767 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 +0900768 }
769 case javaLibTag:
770 if java, ok := child.(*java.Library); ok {
771 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900772 if fileToCopy == nil {
773 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
774 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900775 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900776 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900777 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900778 } else {
779 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900780 }
781 case prebuiltTag:
782 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
783 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900784 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900785 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900786 } else {
787 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
788 }
Roland Levillain630846d2019-06-26 12:48:34 +0100789 case testTag:
790 if cc, ok := child.(*cc.Module); ok {
791 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
792 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeTest, cc, nil})
793 return true
794 } else {
795 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
796 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900797 case keyTag:
798 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900799 a.private_key_file = key.private_key_file
800 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900801 return false
802 } else {
803 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900804 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900805 case certificateTag:
806 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900807 a.container_certificate_file = dep.Certificate.Pem
808 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900809 return false
810 } else {
811 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
812 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900813 }
814 } else {
815 // indirect dependencies
816 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
817 if cc, ok := child.(*cc.Module); ok {
Alex Light49ae3d92019-02-21 14:02:46 -0800818 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900819 // If the dependency is a stubs lib, don't include it in this APEX,
820 // but make sure that the lib is installed on the device.
821 // In case no APEX is having the lib, the lib is installed to the system
822 // partition.
Alex Light49ae3d92019-02-21 14:02:46 -0800823 //
824 // Always include if we are a host-apex however since those won't have any
825 // system libraries.
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900826 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
827 a.externalDeps = append(a.externalDeps, cc.Name())
828 }
829 // Don't track further
Jiyong Park25fc6a92018-11-18 18:02:45 +0900830 return false
831 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900832 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800833 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900834 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900835 return true
836 }
837 }
838 }
839 return false
840 })
841
Jiyong Park9335a262018-12-24 11:31:58 +0900842 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900843 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900844 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
845 return
846 }
847
Jiyong Park8fd61922018-11-08 02:50:25 +0900848 // remove duplicates in filesInfo
849 removeDup := func(filesInfo []apexFile) []apexFile {
850 encountered := make(map[android.Path]bool)
851 result := []apexFile{}
852 for _, f := range filesInfo {
853 if !encountered[f.builtFile] {
854 encountered[f.builtFile] = true
855 result = append(result, f)
856 }
857 }
858 return result
859 }
860 filesInfo = removeDup(filesInfo)
861
862 // to have consistent build rules
863 sort.Slice(filesInfo, func(i, j int) bool {
864 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
865 })
866
867 // prepend the name of this APEX to the module names. These names will be the names of
868 // modules that will be defined if the APEX is flattened.
869 for i := range filesInfo {
870 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
871 }
872
Jiyong Park8fd61922018-11-08 02:50:25 +0900873 a.installDir = android.PathForModuleInstall(ctx, "apex")
874 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800875
876 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900877 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800878 }
879 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900880 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
881 // is true. This is to support referencing APEX via ":<module_name" syntax
882 // in other modules. It is in AndroidMk where the selection of flattened
883 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900884 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +0900885 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900886 }
887}
888
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700889func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +0900890 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900891 for _, f := range a.filesInfo {
892 if f.module != nil {
893 notice := f.module.NoticeFile()
894 if notice.Valid() {
895 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900896 }
897 }
898 }
899 // append the notice file specified in the apex module itself
900 if a.NoticeFile().Valid() {
901 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900902 }
903
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700904 if len(noticeFiles) == 0 {
905 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900906 }
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700907
908 return android.OptionalPathForPath(
909 android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)))
Jiyong Park52818fc2019-03-18 12:01:38 +0900910}
911
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900912func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900913 cert := String(a.properties.Certificate)
914 if cert != "" && android.SrcIsModule(cert) == "" {
915 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900916 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
917 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900918 } else if cert == "" {
919 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900920 a.container_certificate_file = pem
921 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900922 }
923
Colin Cross8a497952019-03-05 22:25:09 -0800924 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900925
Alex Light5098a612018-11-29 17:12:15 -0800926 var abis []string
927 for _, target := range ctx.MultiTargets() {
928 if len(target.Arch.Abi) > 0 {
929 abis = append(abis, target.Arch.Abi[0])
930 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900931 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900932
Alex Light5098a612018-11-29 17:12:15 -0800933 abis = android.FirstUniqueStrings(abis)
934
935 suffix := apexType.suffix()
936 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900937
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900938 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900939 for _, f := range a.filesInfo {
940 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900941 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900942
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900943 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900944 for i, src := range filesToCopy {
945 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800946 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900947 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
948 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800949 for _, sym := range a.filesInfo[i].symlinks {
950 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
951 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
952 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900953 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900954 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800955 implicitInputs = append(implicitInputs, manifest)
956
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900957 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
958 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900959
Alex Light5098a612018-11-29 17:12:15 -0800960 if apexType.image() {
961 // files and dirs that will be created in APEX
962 var readOnlyPaths []string
963 var executablePaths []string // this also includes dirs
964 for _, f := range a.filesInfo {
965 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
Roland Levillain630846d2019-06-26 12:48:34 +0100966 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
Alex Light5098a612018-11-29 17:12:15 -0800967 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800968 for _, s := range f.symlinks {
969 executablePaths = append(executablePaths, filepath.Join("bin", s))
970 }
Alex Light5098a612018-11-29 17:12:15 -0800971 } else {
972 readOnlyPaths = append(readOnlyPaths, pathInApex)
973 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900974 dir := f.installDir
975 for !android.InList(dir, executablePaths) && dir != "" {
976 executablePaths = append(executablePaths, dir)
977 dir, _ = filepath.Split(dir) // move up to the parent
978 if len(dir) > 0 {
979 // remove trailing slash
980 dir = dir[:len(dir)-1]
981 }
Alex Light5098a612018-11-29 17:12:15 -0800982 }
983 }
984 sort.Strings(readOnlyPaths)
985 sort.Strings(executablePaths)
986 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
987 ctx.Build(pctx, android.BuildParams{
988 Rule: generateFsConfig,
989 Output: cannedFsConfig,
990 Description: "generate fs config",
991 Args: map[string]string{
992 "ro_paths": strings.Join(readOnlyPaths, " "),
993 "exec_paths": strings.Join(executablePaths, " "),
994 },
995 })
996
997 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
998 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
999 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
1000 if !fileContextsOptionalPath.Valid() {
1001 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
1002 return
1003 }
1004 fileContexts := fileContextsOptionalPath.Path()
1005
Jiyong Park835d82b2018-12-27 16:04:18 +09001006 optFlags := []string{}
1007
Alex Light5098a612018-11-29 17:12:15 -08001008 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +09001009 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
1010 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -08001011
Jiyong Park7f67f482019-01-05 12:57:48 +09001012 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1013 if overridden {
1014 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
1015 }
1016
Jiyong Park40e26a22019-02-08 02:53:06 +09001017 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001018 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +09001019 implicitInputs = append(implicitInputs, androidManifestFile)
1020 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
1021 }
1022
Jiyong Park71b519d2019-04-18 17:25:49 +09001023 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
1024 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
1025 ctx.Config().UnbundledBuild() &&
1026 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
1027 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
1028 apiFingerprint := java.ApiFingerprintPath(ctx)
1029 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
1030 implicitInputs = append(implicitInputs, apiFingerprint)
1031 }
1032 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
1033
Jaewoong Jung14f5ff62019-06-18 13:09:13 -07001034 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1035 if noticeFile.Valid() {
1036 // If there's a NOTICE file, embed it as an asset file in the APEX.
1037 implicitInputs = append(implicitInputs, noticeFile.Path())
1038 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1039 }
1040
Alex Light5098a612018-11-29 17:12:15 -08001041 ctx.Build(pctx, android.BuildParams{
1042 Rule: apexRule,
1043 Implicits: implicitInputs,
1044 Output: unsignedOutputFile,
1045 Description: "apex (" + apexType.name() + ")",
1046 Args: map[string]string{
1047 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1048 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1049 "copy_commands": strings.Join(copyCommands, " && "),
1050 "manifest": manifest.String(),
1051 "file_contexts": fileContexts.String(),
1052 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001053 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001054 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001055 },
1056 })
1057
1058 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1059 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1060 a.bundleModuleFile = bundleModuleFile
1061
1062 ctx.Build(pctx, android.BuildParams{
1063 Rule: apexProtoConvertRule,
1064 Input: unsignedOutputFile,
1065 Output: apexProtoFile,
1066 Description: "apex proto convert",
1067 })
1068
1069 ctx.Build(pctx, android.BuildParams{
1070 Rule: apexBundleRule,
1071 Input: apexProtoFile,
1072 Output: a.bundleModuleFile,
1073 Description: "apex bundle module",
1074 Args: map[string]string{
1075 "abi": strings.Join(abis, "."),
1076 },
1077 })
1078 } else {
1079 ctx.Build(pctx, android.BuildParams{
1080 Rule: zipApexRule,
1081 Implicits: implicitInputs,
1082 Output: unsignedOutputFile,
1083 Description: "apex (" + apexType.name() + ")",
1084 Args: map[string]string{
1085 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1086 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1087 "copy_commands": strings.Join(copyCommands, " && "),
1088 "manifest": manifest.String(),
1089 },
1090 })
Colin Crossa4925902018-11-16 11:36:28 -08001091 }
Colin Crossa4925902018-11-16 11:36:28 -08001092
Alex Light5098a612018-11-29 17:12:15 -08001093 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001094 ctx.Build(pctx, android.BuildParams{
1095 Rule: java.Signapk,
1096 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001097 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001098 Input: unsignedOutputFile,
Dan Willemsendd651fa2019-06-13 04:48:54 +00001099 Implicits: []android.Path{
1100 a.container_certificate_file,
1101 a.container_private_key_file,
1102 },
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001103 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001104 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001105 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001106 },
1107 })
Alex Light5098a612018-11-29 17:12:15 -08001108
1109 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001110 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001111 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001112 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001113}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001114
Jiyong Park8fd61922018-11-08 02:50:25 +09001115func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001116 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001117 // 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 +09001118 // with other ordinary files.
Colin Cross8a497952019-03-05 22:25:09 -08001119 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +09001120
1121 // rename to apex_manifest.json
1122 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
1123 ctx.Build(pctx, android.BuildParams{
1124 Rule: android.Cp,
1125 Input: manifest,
1126 Output: copiedManifest,
1127 })
Jiyong Park719b4462019-01-13 00:39:51 +09001128 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001129
Jiyong Park42cca6c2019-04-01 11:15:50 +09001130 // rename to apex_pubkey
1131 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1132 ctx.Build(pctx, android.BuildParams{
1133 Rule: android.Cp,
1134 Input: a.public_key_file,
1135 Output: copiedPubkey,
1136 })
1137 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1138
Jiyong Park23c52b02019-02-02 13:13:47 +09001139 if ctx.Config().FlattenApex() {
1140 for _, fi := range a.filesInfo {
1141 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001142 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1143 for _, sym := range fi.symlinks {
1144 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1145 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001146 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001147 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001148 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001149}
1150
1151func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -08001152 writers := []android.AndroidMkData{}
1153 if a.apexTypes.image() {
1154 writers = append(writers, a.androidMkForType(imageApex))
1155 }
1156 if a.apexTypes.zip() {
1157 writers = append(writers, a.androidMkForType(zipApex))
1158 }
1159 return android.AndroidMkData{
1160 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1161 for _, data := range writers {
1162 data.Custom(w, name, prefix, moduleDir, data)
1163 }
1164 }}
1165}
1166
Alex Lightf1801bc2019-02-13 11:10:07 -08001167func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001168 moduleNames := []string{}
1169
1170 for _, fi := range a.filesInfo {
1171 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1172 continue
1173 }
1174 if !android.InList(fi.moduleName, moduleNames) {
1175 moduleNames = append(moduleNames, fi.moduleName)
1176 }
1177 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1178 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1179 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001180 // /apex/<name>/{lib|framework|...}
1181 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1182 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001183 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001184 // /system/apex/<name>/{lib|framework|...}
1185 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1186 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001187 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001188 if len(fi.symlinks) > 0 {
1189 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1190 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001191
1192 if fi.module != nil && fi.module.NoticeFile().Valid() {
1193 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1194 }
Jiyong Park94427262019-02-05 23:18:47 +09001195 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001196 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001197 }
1198 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1199 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1200 if fi.module != nil {
1201 archStr := fi.module.Target().Arch.ArchType.String()
1202 host := false
1203 switch fi.module.Target().Os.Class {
1204 case android.Host:
1205 if archStr != "common" {
1206 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1207 }
1208 host = true
1209 case android.HostCross:
1210 if archStr != "common" {
1211 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1212 }
1213 host = true
1214 case android.Device:
1215 if archStr != "common" {
1216 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1217 }
1218 }
1219 if host {
1220 makeOs := fi.module.Target().Os.String()
1221 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1222 makeOs = "linux"
1223 }
1224 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1225 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1226 }
1227 }
1228 if fi.class == javaSharedLib {
1229 javaModule := fi.module.(*java.Library)
1230 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1231 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1232 // we will have foo.jar.jar
1233 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1234 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1235 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1236 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1237 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1238 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1239 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1240 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Logan Chien41eabe62019-04-10 13:33:58 +08001241 if cc, ok := fi.module.(*cc.Module); ok {
1242 if cc.UnstrippedOutputFile() != nil {
1243 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1244 }
1245 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
Jiyong Park94427262019-02-05 23:18:47 +09001246 }
1247 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1248 } else {
1249 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1250 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1251 }
1252 }
1253 return moduleNames
1254}
1255
Alex Light5098a612018-11-29 17:12:15 -08001256func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001257 return android.AndroidMkData{
1258 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1259 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001260 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001261 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001262 }
1263
Jiyong Park719b4462019-01-13 00:39:51 +09001264 if a.flattened && apexType.image() {
1265 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001266 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1267 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1268 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001269 if len(moduleNames) > 0 {
1270 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1271 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001272 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001273 } else {
Alex Light5098a612018-11-29 17:12:15 -08001274 // zip-apex is the less common type so have the name refer to the image-apex
1275 // only and use {name}.zip if you want the zip-apex
1276 if apexType == zipApex && a.apexTypes == both {
1277 name = name + ".zip"
1278 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001279 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1280 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1281 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1282 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001283 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001284 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001285 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001286 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001287 if len(moduleNames) > 0 {
1288 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1289 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001290 if len(a.externalDeps) > 0 {
1291 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1292 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001293 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001294
Alex Light5098a612018-11-29 17:12:15 -08001295 if apexType == imageApex {
1296 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1297 }
Jiyong Park719b4462019-01-13 00:39:51 +09001298 }
1299 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001300}
1301
Alex Light0851b882019-02-07 13:20:53 -08001302func testApexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001303 return ApexBundleFactory(true /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001304}
1305
1306func apexBundleFactory() android.Module {
Roland Levillain630846d2019-06-26 12:48:34 +01001307 return ApexBundleFactory(false /*testApex*/)
Alex Light0851b882019-02-07 13:20:53 -08001308}
1309
1310func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001311 module := &apexBundle{
1312 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001313 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001314 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001315 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001316 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001317 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001318 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1319 })
Alex Light5098a612018-11-29 17:12:15 -08001320 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001321 android.InitDefaultableModule(module)
1322 return module
1323}
Jiyong Park30ca9372019-02-07 16:27:23 +09001324
1325//
1326// Defaults
1327//
1328type Defaults struct {
1329 android.ModuleBase
1330 android.DefaultsModuleBase
1331}
1332
Jiyong Park30ca9372019-02-07 16:27:23 +09001333func defaultsFactory() android.Module {
1334 return DefaultsFactory()
1335}
1336
1337func DefaultsFactory(props ...interface{}) android.Module {
1338 module := &Defaults{}
1339
1340 module.AddProperties(props...)
1341 module.AddProperties(
1342 &apexBundleProperties{},
1343 &apexTargetBundleProperties{},
1344 )
1345
1346 android.InitDefaultsModule(module)
1347 return module
1348}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001349
1350//
1351// Prebuilt APEX
1352//
1353type Prebuilt struct {
1354 android.ModuleBase
1355 prebuilt android.Prebuilt
1356
1357 properties PrebuiltProperties
1358
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001359 inputApex android.Path
1360 installDir android.OutputPath
1361 installFilename string
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001362 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001363}
1364
1365type PrebuiltProperties struct {
1366 // the path to the prebuilt .apex file to import.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001367 Source string `blueprint:"mutated"`
1368
1369 Src *string
1370 Arch struct {
1371 Arm struct {
1372 Src *string
1373 }
1374 Arm64 struct {
1375 Src *string
1376 }
1377 X86 struct {
1378 Src *string
1379 }
1380 X86_64 struct {
1381 Src *string
1382 }
1383 }
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001384
1385 Installable *bool
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001386 // Optional name for the installed apex. If unspecified, name of the
1387 // module is used as the file name
1388 Filename *string
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001389}
1390
1391func (p *Prebuilt) installable() bool {
1392 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001393}
1394
1395func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc95714e2019-03-29 14:23:10 +09001396 // This is called before prebuilt_select and prebuilt_postdeps mutators
1397 // The mutators requires that src to be set correctly for each arch so that
1398 // arch variants are disabled when src is not provided for the arch.
1399 if len(ctx.MultiTargets()) != 1 {
1400 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1401 return
1402 }
1403 var src string
1404 switch ctx.MultiTargets()[0].Arch.ArchType {
1405 case android.Arm:
1406 src = String(p.properties.Arch.Arm.Src)
1407 case android.Arm64:
1408 src = String(p.properties.Arch.Arm64.Src)
1409 case android.X86:
1410 src = String(p.properties.Arch.X86.Src)
1411 case android.X86_64:
1412 src = String(p.properties.Arch.X86_64.Src)
1413 default:
1414 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1415 return
1416 }
1417 if src == "" {
1418 src = String(p.properties.Src)
1419 }
1420 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001421}
1422
Colin Cross41955e82019-05-29 14:40:35 -07001423func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
1424 switch tag {
1425 case "":
1426 return android.Paths{p.outputApex}, nil
1427 default:
1428 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1429 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001430}
1431
Jiyong Park4d277042019-04-23 18:00:10 +09001432func (p *Prebuilt) InstallFilename() string {
1433 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1434}
1435
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001436func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1437 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001438 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001439 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Park4d277042019-04-23 18:00:10 +09001440 p.installFilename = p.InstallFilename()
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001441 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1442 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1443 }
Nikita Ioffe89ecd592019-04-05 02:10:45 +01001444 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1445 ctx.Build(pctx, android.BuildParams{
1446 Rule: android.Cp,
1447 Input: p.inputApex,
1448 Output: p.outputApex,
1449 })
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001450 if p.installable() {
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001451 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001452 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001453}
1454
1455func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1456 return &p.prebuilt
1457}
1458
1459func (p *Prebuilt) Name() string {
1460 return p.prebuilt.Name(p.ModuleBase.Name())
1461}
1462
1463func (p *Prebuilt) AndroidMk() android.AndroidMkData {
1464 return android.AndroidMkData{
1465 Class: "ETC",
1466 OutputFile: android.OptionalPathForPath(p.inputApex),
1467 Include: "$(BUILD_PREBUILT)",
1468 Extra: []android.AndroidMkExtraFunc{
1469 func(w io.Writer, outputFile android.Path) {
1470 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001471 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", p.installFilename)
Nikita Ioffedd53e8b2019-04-04 13:42:00 +01001472 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.installable())
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001473 },
1474 },
1475 }
1476}
1477
1478// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1479func PrebuiltFactory() android.Module {
1480 module := &Prebuilt{}
1481 module.AddProperties(&module.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001482 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001483 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001484 return module
1485}