blob: ad1b57c28a57164ed5ac9f33b373812f9c0a0149 [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 Park23c52b02019-02-02 13:13:47 +0900487 if String(a.properties.Key) == "" {
488 ctx.ModuleErrorf("key is missing")
489 return
490 }
491 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900492
Jiyong Park23c52b02019-02-02 13:13:47 +0900493 cert := android.SrcIsModule(String(a.properties.Certificate))
494 if cert != "" {
495 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900496 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900497}
498
Jiyong Park74e240b2018-11-27 21:27:08 +0900499func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900500 if file, ok := a.outputFiles[imageApex]; ok {
501 return android.Paths{file}
502 } else {
503 return nil
504 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900505}
506
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900507func (a *apexBundle) installable() bool {
508 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
509}
510
Jiyong Park7c1dc612019-01-05 11:15:24 +0900511func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
512 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900513 return "vendor"
514 } else {
515 return "core"
516 }
517}
518
Jiyong Park388ef3f2019-01-28 19:47:32 +0900519func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
520 globalSanitizerNames := []string{}
521 if a.Host() {
522 globalSanitizerNames = ctx.Config().SanitizeHost()
523 } else {
524 arches := ctx.Config().SanitizeDeviceArch()
525 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
526 globalSanitizerNames = ctx.Config().SanitizeDevice()
527 }
528 }
529 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900530}
531
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800532func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900533 // Decide the APEX-local directory by the multilib of the library
534 // In the future, we may query this to the module.
535 switch cc.Arch().ArchType.Multilib {
536 case "lib32":
537 dirInApex = "lib"
538 case "lib64":
539 dirInApex = "lib64"
540 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900541 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900542 if !cc.Arch().Native {
543 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
544 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800545 if handleSpecialLibs {
546 switch cc.Name() {
547 case "libc", "libm", "libdl":
548 // Special case for bionic libs. This is to prevent the bionic libs
549 // from being included in the search path /apex/com.android.apex/lib.
550 // This exclusion is required because bionic libs in the runtime APEX
551 // are available via the legacy paths /system/lib/libc.so, etc. By the
552 // init process, the bionic libs in the APEX are bind-mounted to the
553 // legacy paths and thus will be loaded into the default linker namespace.
554 // If the bionic libs are directly in /apex/com.android.apex/lib then
555 // the same libs will be again loaded to the runtime linker namespace,
556 // which will result double loading of bionic libs that isn't supported.
557 dirInApex = filepath.Join(dirInApex, "bionic")
558 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900559 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900560
561 fileToCopy = cc.OutputFile().Path()
562 return
563}
564
565func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900566 // TODO(b/123721777) respect relative_install_path also for binaries
567 // dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900568 dirInApex = "bin"
569 fileToCopy = cc.OutputFile().Path()
570 return
571}
572
573func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
574 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900575 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900576 return
577}
578
579func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
580 dirInApex = filepath.Join("etc", prebuilt.SubDir())
581 fileToCopy = prebuilt.OutputFile()
582 return
583}
584
585func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900586 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900587
Jiyong Parkff1458f2018-10-12 21:49:38 +0900588 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900589 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900590 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900591
Alex Light5098a612018-11-29 17:12:15 -0800592 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
593 a.apexTypes = imageApex
594 } else if *a.properties.Payload_type == "zip" {
595 a.apexTypes = zipApex
596 } else if *a.properties.Payload_type == "both" {
597 a.apexTypes = both
598 } else {
599 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
600 return
601 }
602
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800603 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
604
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900605 ctx.WalkDeps(func(child, parent android.Module) bool {
606 if _, ok := parent.(*apexBundle); ok {
607 // direct dependencies
608 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900609 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900610 switch depTag {
611 case sharedLibTag:
612 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800613 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900614 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900615 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900616 } else {
617 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900618 }
619 case executableTag:
620 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800621 if !cc.Arch().Native {
622 // There is only one 'bin' directory so we shouldn't bother copying in
623 // native-bridge'd binaries and only use main ones.
624 return true
625 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900627 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900628 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900629 } else {
630 ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900631 }
632 case javaLibTag:
633 if java, ok := child.(*java.Library); ok {
634 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900635 if fileToCopy == nil {
636 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
637 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900638 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900639 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900640 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900641 } else {
642 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900643 }
644 case prebuiltTag:
645 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
646 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900647 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900648 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900649 } else {
650 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
651 }
652 case keyTag:
653 if key, ok := child.(*apexKey); ok {
654 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900655 if !key.installable() && ctx.Config().Debuggable() {
656 // If the key is not installed, bundled it with the APEX.
657 // Note: this bundled key is valid only for non-production builds
658 // (eng/userdebug).
659 pubKeyFile = key.public_key_file
660 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900661 return false
662 } else {
663 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900664 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900665 case certificateTag:
666 if dep, ok := child.(*java.AndroidAppCertificate); ok {
667 certificate = dep.Certificate
668 return false
669 } else {
670 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
671 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900672 }
673 } else {
674 // indirect dependencies
675 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
676 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677 if cc.IsStubs() || cc.HasStubsVariants() {
678 return false
679 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900680 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800681 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900682 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900683 return true
684 }
685 }
686 }
687 return false
688 })
689
Jiyong Park9335a262018-12-24 11:31:58 +0900690 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park23c52b02019-02-02 13:13:47 +0900691 if keyFile == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900692 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
693 return
694 }
695
Jiyong Park8fd61922018-11-08 02:50:25 +0900696 // remove duplicates in filesInfo
697 removeDup := func(filesInfo []apexFile) []apexFile {
698 encountered := make(map[android.Path]bool)
699 result := []apexFile{}
700 for _, f := range filesInfo {
701 if !encountered[f.builtFile] {
702 encountered[f.builtFile] = true
703 result = append(result, f)
704 }
705 }
706 return result
707 }
708 filesInfo = removeDup(filesInfo)
709
710 // to have consistent build rules
711 sort.Slice(filesInfo, func(i, j int) bool {
712 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
713 })
714
715 // prepend the name of this APEX to the module names. These names will be the names of
716 // modules that will be defined if the APEX is flattened.
717 for i := range filesInfo {
718 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
719 }
720
Jiyong Park8fd61922018-11-08 02:50:25 +0900721 a.installDir = android.PathForModuleInstall(ctx, "apex")
722 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800723
724 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900725 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800726 }
727 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900728 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
729 // is true. This is to support referencing APEX via ":<module_name" syntax
730 // in other modules. It is in AndroidMk where the selection of flattened
731 // or unflattened APEX is made.
732 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
733 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900734 }
735}
736
Jiyong Park835d82b2018-12-27 16:04:18 +0900737func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
738 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900739 cert := String(a.properties.Certificate)
740 if cert != "" && android.SrcIsModule(cert) == "" {
741 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
742 certificate = java.Certificate{
743 defaultDir.Join(ctx, cert+".x509.pem"),
744 defaultDir.Join(ctx, cert+".pk8"),
745 }
746 } else if cert == "" {
747 pem, key := ctx.Config().DefaultAppCertificate(ctx)
748 certificate = java.Certificate{pem, key}
749 }
750
Dario Freni4abb1dc2018-11-20 18:04:58 +0000751 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900752
Alex Light5098a612018-11-29 17:12:15 -0800753 var abis []string
754 for _, target := range ctx.MultiTargets() {
755 if len(target.Arch.Abi) > 0 {
756 abis = append(abis, target.Arch.Abi[0])
757 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900758 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900759
Alex Light5098a612018-11-29 17:12:15 -0800760 abis = android.FirstUniqueStrings(abis)
761
762 suffix := apexType.suffix()
763 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900764
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900765 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900766 for _, f := range a.filesInfo {
767 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900768 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900769
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900770 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900771 for i, src := range filesToCopy {
772 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800773 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900774 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
775 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800776 for _, sym := range a.filesInfo[i].symlinks {
777 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
778 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
779 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900780 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900781 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800782 implicitInputs = append(implicitInputs, manifest)
783
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900784 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
785 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900786
Alex Light5098a612018-11-29 17:12:15 -0800787 if apexType.image() {
788 // files and dirs that will be created in APEX
789 var readOnlyPaths []string
790 var executablePaths []string // this also includes dirs
791 for _, f := range a.filesInfo {
792 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
793 if f.installDir == "bin" {
794 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800795 for _, s := range f.symlinks {
796 executablePaths = append(executablePaths, filepath.Join("bin", s))
797 }
Alex Light5098a612018-11-29 17:12:15 -0800798 } else {
799 readOnlyPaths = append(readOnlyPaths, pathInApex)
800 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900801 dir := f.installDir
802 for !android.InList(dir, executablePaths) && dir != "" {
803 executablePaths = append(executablePaths, dir)
804 dir, _ = filepath.Split(dir) // move up to the parent
805 if len(dir) > 0 {
806 // remove trailing slash
807 dir = dir[:len(dir)-1]
808 }
Alex Light5098a612018-11-29 17:12:15 -0800809 }
810 }
811 sort.Strings(readOnlyPaths)
812 sort.Strings(executablePaths)
813 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
814 ctx.Build(pctx, android.BuildParams{
815 Rule: generateFsConfig,
816 Output: cannedFsConfig,
817 Description: "generate fs config",
818 Args: map[string]string{
819 "ro_paths": strings.Join(readOnlyPaths, " "),
820 "exec_paths": strings.Join(executablePaths, " "),
821 },
822 })
823
824 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
825 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
826 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
827 if !fileContextsOptionalPath.Valid() {
828 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
829 return
830 }
831 fileContexts := fileContextsOptionalPath.Path()
832
Jiyong Park835d82b2018-12-27 16:04:18 +0900833 optFlags := []string{}
834
Alex Light5098a612018-11-29 17:12:15 -0800835 // Additional implicit inputs.
836 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900837 if pubKeyFile != nil {
838 implicitInputs = append(implicitInputs, pubKeyFile)
839 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
840 }
Alex Light5098a612018-11-29 17:12:15 -0800841
Jiyong Park7f67f482019-01-05 12:57:48 +0900842 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
843 if overridden {
844 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
845 }
846
Alex Light5098a612018-11-29 17:12:15 -0800847 ctx.Build(pctx, android.BuildParams{
848 Rule: apexRule,
849 Implicits: implicitInputs,
850 Output: unsignedOutputFile,
851 Description: "apex (" + apexType.name() + ")",
852 Args: map[string]string{
853 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
854 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
855 "copy_commands": strings.Join(copyCommands, " && "),
856 "manifest": manifest.String(),
857 "file_contexts": fileContexts.String(),
858 "canned_fs_config": cannedFsConfig.String(),
859 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900860 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800861 },
862 })
863
864 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
865 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
866 a.bundleModuleFile = bundleModuleFile
867
868 ctx.Build(pctx, android.BuildParams{
869 Rule: apexProtoConvertRule,
870 Input: unsignedOutputFile,
871 Output: apexProtoFile,
872 Description: "apex proto convert",
873 })
874
875 ctx.Build(pctx, android.BuildParams{
876 Rule: apexBundleRule,
877 Input: apexProtoFile,
878 Output: a.bundleModuleFile,
879 Description: "apex bundle module",
880 Args: map[string]string{
881 "abi": strings.Join(abis, "."),
882 },
883 })
884 } else {
885 ctx.Build(pctx, android.BuildParams{
886 Rule: zipApexRule,
887 Implicits: implicitInputs,
888 Output: unsignedOutputFile,
889 Description: "apex (" + apexType.name() + ")",
890 Args: map[string]string{
891 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
892 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
893 "copy_commands": strings.Join(copyCommands, " && "),
894 "manifest": manifest.String(),
895 },
896 })
Colin Crossa4925902018-11-16 11:36:28 -0800897 }
Colin Crossa4925902018-11-16 11:36:28 -0800898
Alex Light5098a612018-11-29 17:12:15 -0800899 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900900 ctx.Build(pctx, android.BuildParams{
901 Rule: java.Signapk,
902 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800903 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900904 Input: unsignedOutputFile,
905 Args: map[string]string{
906 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900907 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900908 },
909 })
Alex Light5098a612018-11-29 17:12:15 -0800910
911 // Install to $OUT/soong/{target,host}/.../apex
Jiyong Park23c52b02019-02-02 13:13:47 +0900912 if a.installable() && !ctx.Config().FlattenApex() {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900913 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
914 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900915}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900916
Jiyong Park8fd61922018-11-08 02:50:25 +0900917func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900918 if a.installable() {
919 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
920 // with other ordinary files.
921 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +0900922
923 // rename to apex_manifest.json
924 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
925 ctx.Build(pctx, android.BuildParams{
926 Rule: android.Cp,
927 Input: manifest,
928 Output: copiedManifest,
929 })
Jiyong Park719b4462019-01-13 00:39:51 +0900930 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900931
Jiyong Park23c52b02019-02-02 13:13:47 +0900932 if ctx.Config().FlattenApex() {
933 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 Park92c0f9c2018-12-13 23:14:57 +0900937 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900938 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900939}
940
941func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -0800942 writers := []android.AndroidMkData{}
943 if a.apexTypes.image() {
944 writers = append(writers, a.androidMkForType(imageApex))
945 }
946 if a.apexTypes.zip() {
947 writers = append(writers, a.androidMkForType(zipApex))
948 }
949 return android.AndroidMkData{
950 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
951 for _, data := range writers {
952 data.Custom(w, name, prefix, moduleDir, data)
953 }
954 }}
955}
956
Jiyong Park94427262019-02-05 23:18:47 +0900957func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string) []string {
958 moduleNames := []string{}
959
960 for _, fi := range a.filesInfo {
961 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
962 continue
963 }
964 if !android.InList(fi.moduleName, moduleNames) {
965 moduleNames = append(moduleNames, fi.moduleName)
966 }
967 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
968 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
969 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
970 if a.flattened {
971 // /system/apex/<name>/{lib|framework|...}
972 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
973 a.installDir.RelPathString(), name, fi.installDir))
974 } else {
975 // /apex/<name>/{lib|framework|...}
976 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(PRODUCT_OUT)",
977 "apex", name, fi.installDir))
978 }
979 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
980 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
981 if fi.module != nil {
982 archStr := fi.module.Target().Arch.ArchType.String()
983 host := false
984 switch fi.module.Target().Os.Class {
985 case android.Host:
986 if archStr != "common" {
987 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
988 }
989 host = true
990 case android.HostCross:
991 if archStr != "common" {
992 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
993 }
994 host = true
995 case android.Device:
996 if archStr != "common" {
997 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
998 }
999 }
1000 if host {
1001 makeOs := fi.module.Target().Os.String()
1002 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1003 makeOs = "linux"
1004 }
1005 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1006 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1007 }
1008 }
1009 if fi.class == javaSharedLib {
1010 javaModule := fi.module.(*java.Library)
1011 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1012 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1013 // we will have foo.jar.jar
1014 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1015 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1016 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1017 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1018 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1019 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1020 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1021 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1022 if cc, ok := fi.module.(*cc.Module); ok && cc.UnstrippedOutputFile() != nil {
1023 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1024 }
1025 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1026 } else {
1027 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1028 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1029 }
1030 }
1031 return moduleNames
1032}
1033
Alex Light5098a612018-11-29 17:12:15 -08001034func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001035 return android.AndroidMkData{
1036 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1037 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001038 if a.installable() {
1039 a.androidMkForFiles(w, name, moduleDir)
Jiyong Park719b4462019-01-13 00:39:51 +09001040 }
1041
Jiyong Park719b4462019-01-13 00:39:51 +09001042 if a.flattened && apexType.image() {
1043 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001044 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1045 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1046 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001047 if len(moduleNames) > 0 {
1048 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1049 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001050 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001051 } else {
Alex Light5098a612018-11-29 17:12:15 -08001052 // zip-apex is the less common type so have the name refer to the image-apex
1053 // only and use {name}.zip if you want the zip-apex
1054 if apexType == zipApex && a.apexTypes == both {
1055 name = name + ".zip"
1056 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001057 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1058 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1059 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1060 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001061 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001062 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001063 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001064 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +09001065 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park94427262019-02-05 23:18:47 +09001066 if len(moduleNames) > 0 {
1067 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1068 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001069 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001070
Alex Light5098a612018-11-29 17:12:15 -08001071 if apexType == imageApex {
1072 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1073 }
Jiyong Park719b4462019-01-13 00:39:51 +09001074 }
1075 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001076}
1077
Alex Lightee250722018-12-06 14:00:02 -08001078func ApexBundleFactory() android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001079 module := &apexBundle{
1080 outputFiles: map[apexPackaging]android.WritablePath{},
1081 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001082 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001083 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001084 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001085 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1086 })
Alex Light5098a612018-11-29 17:12:15 -08001087 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001088 android.InitDefaultableModule(module)
1089 return module
1090}