blob: eb791d938d1e68be730af79a2c3b71bf7f99585d [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"
28
29 "github.com/google/blueprint"
30 "github.com/google/blueprint/proptools"
31)
32
33var (
34 pctx = android.NewPackageContext("android/apex")
35
36 // Create a canned fs config file where all files and directories are
37 // by default set to (uid/gid/mode) = (1000/1000/0644)
38 // TODO(b/113082813) make this configurable using config.fs syntax
39 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000040 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000041 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090042 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090043 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090044 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090045 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046
47 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
48 // against the binary policy using sefcontext_compiler -p <policy>.
49
50 // TODO(b/114327326): automate the generation of file_contexts
51 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
52 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
53 `(${copy_commands}) && ` +
54 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090055 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090056 `--file_contexts ${file_contexts} ` +
57 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080058 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090059 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090060 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
61 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
62 "${soong_zip}", "${zipalign}", "${aapt2}"},
63 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090064 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080065
Alex Light5098a612018-11-29 17:12:15 -080066 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
67 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
68 `(${copy_commands}) && ` +
69 `APEXER_TOOL_PATH=${tool_path} ` +
70 `${apexer} --force --manifest ${manifest} ` +
71 `--payload_type zip ` +
72 `${image_dir} ${out} `,
73 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
74 Description: "ZipAPEX ${image_dir} => ${out}",
75 }, "tool_path", "image_dir", "copy_commands", "manifest")
76
Colin Crossa4925902018-11-16 11:36:28 -080077 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
78 blueprint.RuleParams{
79 Command: `${aapt2} convert --output-format proto $in -o $out`,
80 CommandDeps: []string{"${aapt2}"},
81 })
82
83 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090084 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000085 `apex_payload.img:apex/${abi}.img ` +
86 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +000087 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -080088 CommandDeps: []string{"${zip2zip}"},
89 Description: "app bundle",
90 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090091)
92
Alex Light5098a612018-11-29 17:12:15 -080093var imageApexSuffix = ".apex"
94var zipApexSuffix = ".zipapex"
95
96var imageApexType = "image"
97var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090098
99type dependencyTag struct {
100 blueprint.BaseDependencyTag
101 name string
102}
103
104var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900105 sharedLibTag = dependencyTag{name: "sharedLib"}
106 executableTag = dependencyTag{name: "executable"}
107 javaLibTag = dependencyTag{name: "javaLib"}
108 prebuiltTag = dependencyTag{name: "prebuilt"}
109 keyTag = dependencyTag{name: "key"}
110 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900111)
112
113func init() {
114 pctx.Import("android/soong/common")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900115 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900116 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100117 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
118 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
119 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
120 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000121 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100122 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
123 } else {
124 return pctx.HostBinToolPath(ctx, tool).String()
125 }
126 })
127 }
128 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900129 pctx.HostBinToolVariable("avbtool", "avbtool")
130 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
131 pctx.HostBinToolVariable("merge_zips", "merge_zips")
132 pctx.HostBinToolVariable("mke2fs", "mke2fs")
133 pctx.HostBinToolVariable("resize2fs", "resize2fs")
134 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
135 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800136 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900137 pctx.HostBinToolVariable("zipalign", "zipalign")
138
Alex Lightee250722018-12-06 14:00:02 -0800139 android.RegisterModuleType("apex", ApexBundleFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900140
141 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
142 ctx.TopDown("apex_deps", apexDepsMutator)
143 ctx.BottomUp("apex", apexMutator)
144 })
145}
146
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900147// Mark the direct and transitive dependencies of apex bundles so that they
148// can be built for the apex bundles.
149func apexDepsMutator(mctx android.TopDownMutatorContext) {
150 if _, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800151 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900152 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900153 depName := mctx.OtherModuleName(child)
154 // If the parent is apexBundle, this child is directly depended.
155 _, directDep := parent.(*apexBundle)
156 android.UpdateApexDependency(apexBundleName, depName, directDep)
157
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900158 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900159 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160 return true
161 } else {
162 return false
163 }
164 })
165 }
166}
167
168// Create apex variations if a module is included in APEX(s).
169func apexMutator(mctx android.BottomUpMutatorContext) {
170 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900171 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900172 } else if _, ok := mctx.Module().(*apexBundle); ok {
173 // apex bundle itself is mutated so that it and its modules have same
174 // apex variant.
175 apexBundleName := mctx.ModuleName()
176 mctx.CreateVariations(apexBundleName)
177 }
178}
179
Alex Light9670d332019-01-29 18:07:33 -0800180type apexNativeDependencies struct {
181 // List of native libraries
182 Native_shared_libs []string
183 // List of native executables
184 Binaries []string
185}
186type apexMultilibProperties struct {
187 // Native dependencies whose compile_multilib is "first"
188 First apexNativeDependencies
189
190 // Native dependencies whose compile_multilib is "both"
191 Both apexNativeDependencies
192
193 // Native dependencies whose compile_multilib is "prefer32"
194 Prefer32 apexNativeDependencies
195
196 // Native dependencies whose compile_multilib is "32"
197 Lib32 apexNativeDependencies
198
199 // Native dependencies whose compile_multilib is "64"
200 Lib64 apexNativeDependencies
201}
202
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900203type apexBundleProperties struct {
204 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000205 // "apex_manifest.json"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900206 Manifest *string
207
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900208 // Determines the file contexts file for setting security context to each file in this APEX bundle.
209 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
210 // used.
211 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900212 File_contexts *string
213
214 // List of native shared libs that are embedded inside this APEX bundle
215 Native_shared_libs []string
216
217 // List of native executables that are embedded inside this APEX bundle
218 Binaries []string
219
220 // List of java libraries that are embedded inside this APEX bundle
221 Java_libs []string
222
223 // List of prebuilt files that are embedded inside this APEX bundle
224 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900225
226 // Name of the apex_key module that provides the private key to sign APEX
227 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900228
Alex Light5098a612018-11-29 17:12:15 -0800229 // The type of APEX to build. Controls what the APEX payload is. Either
230 // 'image', 'zip' or 'both'. Default: 'image'.
231 Payload_type *string
232
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900233 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
234 // or an android_app_certificate module name in the form ":module".
235 Certificate *string
236
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900237 // Whether this APEX is installable to one of the partitions. Default: true.
238 Installable *bool
239
Jiyong Parkda6eb592018-12-19 17:12:36 +0900240 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
241 // Default is false.
242 Use_vendor *bool
243
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800244 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
245 Ignore_system_library_special_case *bool
246
Alex Light9670d332019-01-29 18:07:33 -0800247 Multilib apexMultilibProperties
248}
249
250type apexTargetBundleProperties struct {
251 Target struct {
252 // Multilib properties only for android.
253 Android struct {
254 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900255 }
Alex Light9670d332019-01-29 18:07:33 -0800256 // Multilib properties only for host.
257 Host struct {
258 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900259 }
Alex Light9670d332019-01-29 18:07:33 -0800260 // Multilib properties only for host linux_bionic.
261 Linux_bionic struct {
262 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900263 }
Alex Light9670d332019-01-29 18:07:33 -0800264 // Multilib properties only for host linux_glibc.
265 Linux_glibc struct {
266 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900267 }
268 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900269}
270
Jiyong Park8fd61922018-11-08 02:50:25 +0900271type apexFileClass int
272
273const (
274 etc apexFileClass = iota
275 nativeSharedLib
276 nativeExecutable
277 javaSharedLib
278)
279
Alex Light5098a612018-11-29 17:12:15 -0800280type apexPackaging int
281
282const (
283 imageApex apexPackaging = iota
284 zipApex
285 both
286)
287
288func (a apexPackaging) image() bool {
289 switch a {
290 case imageApex, both:
291 return true
292 }
293 return false
294}
295
296func (a apexPackaging) zip() bool {
297 switch a {
298 case zipApex, both:
299 return true
300 }
301 return false
302}
303
304func (a apexPackaging) suffix() string {
305 switch a {
306 case imageApex:
307 return imageApexSuffix
308 case zipApex:
309 return zipApexSuffix
310 case both:
311 panic(fmt.Errorf("must be either zip or image"))
312 default:
313 panic(fmt.Errorf("unkonwn APEX type %d", a))
314 }
315}
316
317func (a apexPackaging) name() string {
318 switch a {
319 case imageApex:
320 return imageApexType
321 case zipApex:
322 return zipApexType
323 case both:
324 panic(fmt.Errorf("must be either zip or image"))
325 default:
326 panic(fmt.Errorf("unkonwn APEX type %d", a))
327 }
328}
329
Jiyong Park8fd61922018-11-08 02:50:25 +0900330func (class apexFileClass) NameInMake() string {
331 switch class {
332 case etc:
333 return "ETC"
334 case nativeSharedLib:
335 return "SHARED_LIBRARIES"
336 case nativeExecutable:
337 return "EXECUTABLES"
338 case javaSharedLib:
339 return "JAVA_LIBRARIES"
340 default:
341 panic(fmt.Errorf("unkonwn class %d", class))
342 }
343}
344
345type apexFile struct {
346 builtFile android.Path
347 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900348 installDir string
349 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900350 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800351 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900352}
353
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900354type apexBundle struct {
355 android.ModuleBase
356 android.DefaultableModuleBase
357
Alex Light9670d332019-01-29 18:07:33 -0800358 properties apexBundleProperties
359 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900360
Alex Light5098a612018-11-29 17:12:15 -0800361 apexTypes apexPackaging
362
Colin Crossa4925902018-11-16 11:36:28 -0800363 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800364 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800365 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900366
367 // list of files to be included in this apex
368 filesInfo []apexFile
369
370 flattened bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900371}
372
Jiyong Park397e55e2018-10-24 21:09:55 +0900373func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900374 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900375 // Use *FarVariation* to be able to depend on modules having
376 // conflicting variations with this module. This is required since
377 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
378 // for native shared libs.
379 ctx.AddFarVariationDependencies([]blueprint.Variation{
380 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900381 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900382 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900383 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900384 }, sharedLibTag, native_shared_libs...)
385
386 ctx.AddFarVariationDependencies([]blueprint.Variation{
387 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900388 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900389 }, executableTag, binaries...)
390}
391
Alex Light9670d332019-01-29 18:07:33 -0800392func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
393 if ctx.Os().Class == android.Device {
394 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
395 } else {
396 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
397 if ctx.Os().Bionic() {
398 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
399 } else {
400 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
401 }
402 }
403}
404
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900405func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800406
Jiyong Park397e55e2018-10-24 21:09:55 +0900407 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900408 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800409
410 a.combineProperties(ctx)
411
Jiyong Park397e55e2018-10-24 21:09:55 +0900412 has32BitTarget := false
413 for _, target := range targets {
414 if target.Arch.ArchType.Multilib == "lib32" {
415 has32BitTarget = true
416 }
417 }
418 for i, target := range targets {
419 // When multilib.* is omitted for native_shared_libs, it implies
420 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900421 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900422 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900423 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900424 {Mutator: "link", Variation: "shared"},
425 }, sharedLibTag, a.properties.Native_shared_libs...)
426
Jiyong Park397e55e2018-10-24 21:09:55 +0900427 // Add native modules targetting both ABIs
428 addDependenciesForNativeModules(ctx,
429 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900430 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900431 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900432
Alex Light3d673592019-01-18 14:37:31 -0800433 isPrimaryAbi := i == 0
434 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900435 // When multilib.* is omitted for binaries, it implies
436 // multilib.first.
437 ctx.AddFarVariationDependencies([]blueprint.Variation{
438 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900439 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900440 }, executableTag, a.properties.Binaries...)
441
442 // Add native modules targetting the first ABI
443 addDependenciesForNativeModules(ctx,
444 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900445 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900446 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800447
448 // When multilib.* is omitted for prebuilts, it implies multilib.first.
449 ctx.AddFarVariationDependencies([]blueprint.Variation{
450 {Mutator: "arch", Variation: target.String()},
451 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900452 }
453
454 switch target.Arch.ArchType.Multilib {
455 case "lib32":
456 // Add native modules targetting 32-bit ABI
457 addDependenciesForNativeModules(ctx,
458 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900459 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900460 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900461
462 addDependenciesForNativeModules(ctx,
463 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900464 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900465 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900466 case "lib64":
467 // Add native modules targetting 64-bit ABI
468 addDependenciesForNativeModules(ctx,
469 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900470 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900471 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900472
473 if !has32BitTarget {
474 addDependenciesForNativeModules(ctx,
475 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900476 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900477 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900478 }
479 }
480
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900481 }
482
Jiyong Parkff1458f2018-10-12 21:49:38 +0900483 ctx.AddFarVariationDependencies([]blueprint.Variation{
484 {Mutator: "arch", Variation: "android_common"},
485 }, javaLibTag, a.properties.Java_libs...)
486
Jiyong Park9335a262018-12-24 11:31:58 +0900487 if !ctx.Config().FlattenApex() || ctx.Config().UnbundledBuild() {
488 if String(a.properties.Key) == "" {
489 ctx.ModuleErrorf("key is missing")
490 return
491 }
492 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900493
Jiyong Park9335a262018-12-24 11:31:58 +0900494 cert := android.SrcIsModule(String(a.properties.Certificate))
495 if cert != "" {
496 ctx.AddDependency(ctx.Module(), certificateTag, cert)
497 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900498 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900499}
500
Jiyong Park74e240b2018-11-27 21:27:08 +0900501func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900502 if file, ok := a.outputFiles[imageApex]; ok {
503 return android.Paths{file}
504 } else {
505 return nil
506 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900507}
508
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900509func (a *apexBundle) installable() bool {
510 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
511}
512
Jiyong Park7c1dc612019-01-05 11:15:24 +0900513func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
514 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900515 return "vendor"
516 } else {
517 return "core"
518 }
519}
520
Jiyong Park388ef3f2019-01-28 19:47:32 +0900521func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
522 globalSanitizerNames := []string{}
523 if a.Host() {
524 globalSanitizerNames = ctx.Config().SanitizeHost()
525 } else {
526 arches := ctx.Config().SanitizeDeviceArch()
527 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
528 globalSanitizerNames = ctx.Config().SanitizeDevice()
529 }
530 }
531 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900532}
533
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800534func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900535 // Decide the APEX-local directory by the multilib of the library
536 // In the future, we may query this to the module.
537 switch cc.Arch().ArchType.Multilib {
538 case "lib32":
539 dirInApex = "lib"
540 case "lib64":
541 dirInApex = "lib64"
542 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900543 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900544 if !cc.Arch().Native {
545 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
546 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800547 if handleSpecialLibs {
548 switch cc.Name() {
549 case "libc", "libm", "libdl":
550 // Special case for bionic libs. This is to prevent the bionic libs
551 // from being included in the search path /apex/com.android.apex/lib.
552 // This exclusion is required because bionic libs in the runtime APEX
553 // are available via the legacy paths /system/lib/libc.so, etc. By the
554 // init process, the bionic libs in the APEX are bind-mounted to the
555 // legacy paths and thus will be loaded into the default linker namespace.
556 // If the bionic libs are directly in /apex/com.android.apex/lib then
557 // the same libs will be again loaded to the runtime linker namespace,
558 // which will result double loading of bionic libs that isn't supported.
559 dirInApex = filepath.Join(dirInApex, "bionic")
560 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900561 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900562
563 fileToCopy = cc.OutputFile().Path()
564 return
565}
566
567func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900568 // TODO(b/123721777) respect relative_install_path also for binaries
569 // dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900570 dirInApex = "bin"
571 fileToCopy = cc.OutputFile().Path()
572 return
573}
574
575func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
576 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900577 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900578 return
579}
580
581func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
582 dirInApex = filepath.Join("etc", prebuilt.SubDir())
583 fileToCopy = prebuilt.OutputFile()
584 return
585}
586
587func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900588 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900589
Jiyong Parkff1458f2018-10-12 21:49:38 +0900590 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900591 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900592 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900593
Alex Light5098a612018-11-29 17:12:15 -0800594 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
595 a.apexTypes = imageApex
596 } else if *a.properties.Payload_type == "zip" {
597 a.apexTypes = zipApex
598 } else if *a.properties.Payload_type == "both" {
599 a.apexTypes = both
600 } else {
601 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
602 return
603 }
604
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800605 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
606
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900607 ctx.WalkDeps(func(child, parent android.Module) bool {
608 if _, ok := parent.(*apexBundle); ok {
609 // direct dependencies
610 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900611 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900612 switch depTag {
613 case sharedLibTag:
614 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800615 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900616 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900617 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900618 } else {
619 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900620 }
621 case executableTag:
622 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800623 if !cc.Arch().Native {
624 // There is only one 'bin' directory so we shouldn't bother copying in
625 // native-bridge'd binaries and only use main ones.
626 return true
627 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900628 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900629 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900630 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900631 } else {
632 ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900633 }
634 case javaLibTag:
635 if java, ok := child.(*java.Library); ok {
636 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900637 if fileToCopy == nil {
638 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
639 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900640 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900641 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900642 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900643 } else {
644 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900645 }
646 case prebuiltTag:
647 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
648 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900649 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900650 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900651 } else {
652 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
653 }
654 case keyTag:
655 if key, ok := child.(*apexKey); ok {
656 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900657 if !key.installable() && ctx.Config().Debuggable() {
658 // If the key is not installed, bundled it with the APEX.
659 // Note: this bundled key is valid only for non-production builds
660 // (eng/userdebug).
661 pubKeyFile = key.public_key_file
662 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900663 return false
664 } else {
665 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900666 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900667 case certificateTag:
668 if dep, ok := child.(*java.AndroidAppCertificate); ok {
669 certificate = dep.Certificate
670 return false
671 } else {
672 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
673 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900674 }
675 } else {
676 // indirect dependencies
677 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
678 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900679 if cc.IsStubs() || cc.HasStubsVariants() {
680 return false
681 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900682 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800683 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900684 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900685 return true
686 }
687 }
688 }
689 return false
690 })
691
Jiyong Park9335a262018-12-24 11:31:58 +0900692 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
693 if !a.flattened && keyFile == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900694 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
695 return
696 }
697
Jiyong Park8fd61922018-11-08 02:50:25 +0900698 // remove duplicates in filesInfo
699 removeDup := func(filesInfo []apexFile) []apexFile {
700 encountered := make(map[android.Path]bool)
701 result := []apexFile{}
702 for _, f := range filesInfo {
703 if !encountered[f.builtFile] {
704 encountered[f.builtFile] = true
705 result = append(result, f)
706 }
707 }
708 return result
709 }
710 filesInfo = removeDup(filesInfo)
711
712 // to have consistent build rules
713 sort.Slice(filesInfo, func(i, j int) bool {
714 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
715 })
716
717 // prepend the name of this APEX to the module names. These names will be the names of
718 // modules that will be defined if the APEX is flattened.
719 for i := range filesInfo {
720 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
721 }
722
Jiyong Park8fd61922018-11-08 02:50:25 +0900723 a.installDir = android.PathForModuleInstall(ctx, "apex")
724 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800725
726 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900727 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800728 }
729 if a.apexTypes.image() {
730 if ctx.Config().FlattenApex() {
731 a.buildFlattenedApex(ctx)
732 } else {
Jiyong Park835d82b2018-12-27 16:04:18 +0900733 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
Alex Light5098a612018-11-29 17:12:15 -0800734 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900735 }
736}
737
Jiyong Park835d82b2018-12-27 16:04:18 +0900738func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
739 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900740 cert := String(a.properties.Certificate)
741 if cert != "" && android.SrcIsModule(cert) == "" {
742 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
743 certificate = java.Certificate{
744 defaultDir.Join(ctx, cert+".x509.pem"),
745 defaultDir.Join(ctx, cert+".pk8"),
746 }
747 } else if cert == "" {
748 pem, key := ctx.Config().DefaultAppCertificate(ctx)
749 certificate = java.Certificate{pem, key}
750 }
751
Dario Freni4abb1dc2018-11-20 18:04:58 +0000752 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900753
Alex Light5098a612018-11-29 17:12:15 -0800754 var abis []string
755 for _, target := range ctx.MultiTargets() {
756 if len(target.Arch.Abi) > 0 {
757 abis = append(abis, target.Arch.Abi[0])
758 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900759 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900760
Alex Light5098a612018-11-29 17:12:15 -0800761 abis = android.FirstUniqueStrings(abis)
762
763 suffix := apexType.suffix()
764 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900765
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900766 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900767 for _, f := range a.filesInfo {
768 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900769 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900770
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900771 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900772 for i, src := range filesToCopy {
773 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800774 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900775 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
776 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800777 for _, sym := range a.filesInfo[i].symlinks {
778 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
779 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
780 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900781 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900782 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800783 implicitInputs = append(implicitInputs, manifest)
784
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900785 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
786 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900787
Alex Light5098a612018-11-29 17:12:15 -0800788 if apexType.image() {
789 // files and dirs that will be created in APEX
790 var readOnlyPaths []string
791 var executablePaths []string // this also includes dirs
792 for _, f := range a.filesInfo {
793 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
794 if f.installDir == "bin" {
795 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800796 for _, s := range f.symlinks {
797 executablePaths = append(executablePaths, filepath.Join("bin", s))
798 }
Alex Light5098a612018-11-29 17:12:15 -0800799 } else {
800 readOnlyPaths = append(readOnlyPaths, pathInApex)
801 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900802 dir := f.installDir
803 for !android.InList(dir, executablePaths) && dir != "" {
804 executablePaths = append(executablePaths, dir)
805 dir, _ = filepath.Split(dir) // move up to the parent
806 if len(dir) > 0 {
807 // remove trailing slash
808 dir = dir[:len(dir)-1]
809 }
Alex Light5098a612018-11-29 17:12:15 -0800810 }
811 }
812 sort.Strings(readOnlyPaths)
813 sort.Strings(executablePaths)
814 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
815 ctx.Build(pctx, android.BuildParams{
816 Rule: generateFsConfig,
817 Output: cannedFsConfig,
818 Description: "generate fs config",
819 Args: map[string]string{
820 "ro_paths": strings.Join(readOnlyPaths, " "),
821 "exec_paths": strings.Join(executablePaths, " "),
822 },
823 })
824
825 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
826 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
827 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
828 if !fileContextsOptionalPath.Valid() {
829 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
830 return
831 }
832 fileContexts := fileContextsOptionalPath.Path()
833
Jiyong Park835d82b2018-12-27 16:04:18 +0900834 optFlags := []string{}
835
Alex Light5098a612018-11-29 17:12:15 -0800836 // Additional implicit inputs.
837 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900838 if pubKeyFile != nil {
839 implicitInputs = append(implicitInputs, pubKeyFile)
840 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
841 }
Alex Light5098a612018-11-29 17:12:15 -0800842
Jiyong Park7f67f482019-01-05 12:57:48 +0900843 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
844 if overridden {
845 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
846 }
847
Alex Light5098a612018-11-29 17:12:15 -0800848 ctx.Build(pctx, android.BuildParams{
849 Rule: apexRule,
850 Implicits: implicitInputs,
851 Output: unsignedOutputFile,
852 Description: "apex (" + apexType.name() + ")",
853 Args: map[string]string{
854 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
855 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
856 "copy_commands": strings.Join(copyCommands, " && "),
857 "manifest": manifest.String(),
858 "file_contexts": fileContexts.String(),
859 "canned_fs_config": cannedFsConfig.String(),
860 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900861 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800862 },
863 })
864
865 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
866 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
867 a.bundleModuleFile = bundleModuleFile
868
869 ctx.Build(pctx, android.BuildParams{
870 Rule: apexProtoConvertRule,
871 Input: unsignedOutputFile,
872 Output: apexProtoFile,
873 Description: "apex proto convert",
874 })
875
876 ctx.Build(pctx, android.BuildParams{
877 Rule: apexBundleRule,
878 Input: apexProtoFile,
879 Output: a.bundleModuleFile,
880 Description: "apex bundle module",
881 Args: map[string]string{
882 "abi": strings.Join(abis, "."),
883 },
884 })
885 } else {
886 ctx.Build(pctx, android.BuildParams{
887 Rule: zipApexRule,
888 Implicits: implicitInputs,
889 Output: unsignedOutputFile,
890 Description: "apex (" + apexType.name() + ")",
891 Args: map[string]string{
892 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
893 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
894 "copy_commands": strings.Join(copyCommands, " && "),
895 "manifest": manifest.String(),
896 },
897 })
Colin Crossa4925902018-11-16 11:36:28 -0800898 }
Colin Crossa4925902018-11-16 11:36:28 -0800899
Alex Light5098a612018-11-29 17:12:15 -0800900 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900901 ctx.Build(pctx, android.BuildParams{
902 Rule: java.Signapk,
903 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800904 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900905 Input: unsignedOutputFile,
906 Args: map[string]string{
907 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900908 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900909 },
910 })
Alex Light5098a612018-11-29 17:12:15 -0800911
912 // Install to $OUT/soong/{target,host}/.../apex
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900913 if a.installable() {
914 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
915 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900916}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900917
Jiyong Park8fd61922018-11-08 02:50:25 +0900918func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900919 if a.installable() {
920 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
921 // with other ordinary files.
922 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +0900923
924 // rename to apex_manifest.json
925 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
926 ctx.Build(pctx, android.BuildParams{
927 Rule: android.Cp,
928 Input: manifest,
929 Output: copiedManifest,
930 })
Jiyong Park719b4462019-01-13 00:39:51 +0900931 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900932
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900933 for _, fi := range a.filesInfo {
934 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
935 ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
936 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900937 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900938}
939
940func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -0800941 writers := []android.AndroidMkData{}
942 if a.apexTypes.image() {
943 writers = append(writers, a.androidMkForType(imageApex))
944 }
945 if a.apexTypes.zip() {
946 writers = append(writers, a.androidMkForType(zipApex))
947 }
948 return android.AndroidMkData{
949 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
950 for _, data := range writers {
951 data.Custom(w, name, prefix, moduleDir, data)
952 }
953 }}
954}
955
956func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +0900957 return android.AndroidMkData{
958 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
959 moduleNames := []string{}
960 for _, fi := range a.filesInfo {
961 if !android.InList(fi.moduleName, moduleNames) {
962 moduleNames = append(moduleNames, fi.moduleName)
963 }
964 }
965
966 for _, fi := range a.filesInfo {
967 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
968 continue
969 }
970 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
971 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
972 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
973 if a.flattened {
974 // /system/apex/<name>/{lib|framework|...}
975 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
976 a.installDir.RelPathString(), name, fi.installDir))
977 } else {
978 // /apex/<name>/{lib|framework|...}
979 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(PRODUCT_OUT)",
980 "apex", name, fi.installDir))
981 }
982 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
983 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
984 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
985 if fi.module != nil {
986 archStr := fi.module.Target().Arch.ArchType.String()
987 host := false
988 switch fi.module.Target().Os.Class {
989 case android.Host:
990 if archStr != "common" {
991 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
992 }
993 host = true
994 case android.HostCross:
995 if archStr != "common" {
996 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
997 }
998 host = true
999 case android.Device:
1000 if archStr != "common" {
1001 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1002 }
1003 }
1004 if host {
1005 makeOs := fi.module.Target().Os.String()
1006 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1007 makeOs = "linux"
1008 }
1009 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1010 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
Jiyong Park8fd61922018-11-08 02:50:25 +09001011 }
1012 }
Jiyong Park719b4462019-01-13 00:39:51 +09001013 if fi.class == javaSharedLib {
1014 javaModule := fi.module.(*java.Library)
1015 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1016 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1017 // we will have foo.jar.jar
1018 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1019 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1020 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1021 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1022 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1023 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1024 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1025 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1026 if cc, ok := fi.module.(*cc.Module); ok && cc.UnstrippedOutputFile() != nil {
1027 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1028 }
1029 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1030 } else {
1031 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1032 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1033 }
1034 }
1035 if a.flattened && apexType.image() {
1036 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001037 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1038 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1039 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1040 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1041 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001042 } else {
Alex Light5098a612018-11-29 17:12:15 -08001043 // zip-apex is the less common type so have the name refer to the image-apex
1044 // only and use {name}.zip if you want the zip-apex
1045 if apexType == zipApex && a.apexTypes == both {
1046 name = name + ".zip"
1047 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001048 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1049 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1050 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1051 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001052 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001053 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001054 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001055 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +09001056 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park719b4462019-01-13 00:39:51 +09001057 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
Jiyong Park8fd61922018-11-08 02:50:25 +09001058 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001059
Alex Light5098a612018-11-29 17:12:15 -08001060 if apexType == imageApex {
1061 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1062 }
Jiyong Park719b4462019-01-13 00:39:51 +09001063 }
1064 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001065}
1066
Alex Lightee250722018-12-06 14:00:02 -08001067func ApexBundleFactory() android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001068 module := &apexBundle{
1069 outputFiles: map[apexPackaging]android.WritablePath{},
1070 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001071 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001072 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001073 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001074 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1075 })
Alex Light5098a612018-11-29 17:12:15 -08001076 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001077 android.InitDefaultableModule(module)
1078 return module
1079}