blob: af05102724aa490df17d6639a2df0a6c58b6753d [file] [log] [blame]
Inseob Kimde5744a2020-12-02 13:14:28 +09001// Copyright 2020 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.
14package cc
15
16// This file defines snapshot prebuilt modules, e.g. vendor snapshot and recovery snapshot. Such
17// snapshot modules will override original source modules with setting BOARD_VNDK_VERSION, with
18// snapshot mutators and snapshot information maps which are also defined in this file.
19
20import (
Justin DeMartino383bfb32021-02-24 10:49:43 -080021 "path/filepath"
Inseob Kimde5744a2020-12-02 13:14:28 +090022 "strings"
Inseob Kimde5744a2020-12-02 13:14:28 +090023
24 "android/soong/android"
Jose Galmes6f843bc2020-12-11 13:36:29 -080025
Colin Crosse0edaf92021-01-11 17:31:17 -080026 "github.com/google/blueprint"
Inseob Kimde5744a2020-12-02 13:14:28 +090027)
28
29// Defines the specifics of different images to which the snapshot process is applicable, e.g.,
30// vendor, recovery, ramdisk.
31type snapshotImage interface {
Jose Galmes6f843bc2020-12-11 13:36:29 -080032 // Returns true if a snapshot should be generated for this image.
33 shouldGenerateSnapshot(ctx android.SingletonContext) bool
34
Inseob Kimde5744a2020-12-02 13:14:28 +090035 // Function that returns true if the module is included in this image.
36 // Using a function return instead of a value to prevent early
37 // evalution of a function that may be not be defined.
38 inImage(m *Module) func() bool
39
Justin Yune09ac172021-01-20 19:49:01 +090040 // Returns true if the module is private and must not be included in the
41 // snapshot. For example VNDK-private modules must return true for the
42 // vendor snapshots. But false for the recovery snapshots.
43 private(m *Module) bool
Inseob Kimde5744a2020-12-02 13:14:28 +090044
45 // Returns true if a dir under source tree is an SoC-owned proprietary
46 // directory, such as device/, vendor/, etc.
47 //
48 // For a given snapshot (e.g., vendor, recovery, etc.) if
Justin DeMartino383bfb32021-02-24 10:49:43 -080049 // isProprietaryPath(dir, deviceConfig) returns true, then the module in dir
50 // will be built from sources.
51 isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool
Inseob Kimde5744a2020-12-02 13:14:28 +090052
53 // Whether to include VNDK in the snapshot for this image.
54 includeVndk() bool
55
56 // Whether a given module has been explicitly excluded from the
57 // snapshot, e.g., using the exclude_from_vendor_snapshot or
58 // exclude_from_recovery_snapshot properties.
59 excludeFromSnapshot(m *Module) bool
Jose Galmes6f843bc2020-12-11 13:36:29 -080060
Jose Galmes6f843bc2020-12-11 13:36:29 -080061 // Returns true if the build is using a snapshot for this image.
62 isUsingSnapshot(cfg android.DeviceConfig) bool
63
Colin Crosse0edaf92021-01-11 17:31:17 -080064 // Returns a version of which the snapshot should be used in this target.
65 // This will only be meaningful when isUsingSnapshot is true.
66 targetSnapshotVersion(cfg android.DeviceConfig) string
Inseob Kim7cf14652021-01-06 23:06:52 +090067
68 // Whether to exclude a given module from the directed snapshot or not.
69 // If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on,
70 // and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured.
71 excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool
Colin Crosse0edaf92021-01-11 17:31:17 -080072
73 // The image variant name for this snapshot image.
74 // For example, recovery snapshot image will return "recovery", and vendor snapshot image will
75 // return "vendor." + version.
76 imageVariantName(cfg android.DeviceConfig) string
77
78 // The variant suffix for snapshot modules. For example, vendor snapshot modules will have
79 // ".vendor" as their suffix.
80 moduleNameSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +090081}
82
83type vendorSnapshotImage struct{}
84type recoverySnapshotImage struct{}
85
Justin DeMartino383bfb32021-02-24 10:49:43 -080086type directoryMap map[string]bool
87
88var (
89 // Modules under following directories are ignored. They are OEM's and vendor's
90 // proprietary modules(device/, kernel/, vendor/, and hardware/).
91 defaultDirectoryExcludedMap = directoryMap{
92 "device": true,
93 "hardware": true,
94 "kernel": true,
95 "vendor": true,
96 }
97
98 // Modules under following directories are included as they are in AOSP,
99 // although hardware/ and kernel/ are normally for vendor's own.
100 defaultDirectoryIncludedMap = directoryMap{
101 "kernel/configs": true,
102 "kernel/prebuilts": true,
103 "kernel/tests": true,
104 "hardware/interfaces": true,
105 "hardware/libhardware": true,
106 "hardware/libhardware_legacy": true,
107 "hardware/ril": true,
108 }
109)
110
Colin Crosse0edaf92021-01-11 17:31:17 -0800111func (vendorSnapshotImage) init(ctx android.RegistrationContext) {
112 ctx.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
113 ctx.RegisterModuleType("vendor_snapshot", vendorSnapshotFactory)
114 ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
115 ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
116 ctx.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
117 ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
118 ctx.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
Inseob Kime9aec6a2021-01-05 20:03:22 +0900119
Colin Crosse0edaf92021-01-11 17:31:17 -0800120 ctx.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
Inseob Kimde5744a2020-12-02 13:14:28 +0900121}
122
Jose Galmes6f843bc2020-12-11 13:36:29 -0800123func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
124 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot.
125 return ctx.DeviceConfig().VndkVersion() == "current"
126}
127
Inseob Kimde5744a2020-12-02 13:14:28 +0900128func (vendorSnapshotImage) inImage(m *Module) func() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500129 return m.InVendor
Inseob Kimde5744a2020-12-02 13:14:28 +0900130}
131
Justin Yune09ac172021-01-20 19:49:01 +0900132func (vendorSnapshotImage) private(m *Module) bool {
133 return m.IsVndkPrivate()
Inseob Kimde5744a2020-12-02 13:14:28 +0900134}
135
Justin DeMartino383bfb32021-02-24 10:49:43 -0800136func isDirectoryExcluded(dir string, excludedMap directoryMap, includedMap directoryMap) bool {
137 if dir == "." || dir == "/" {
138 return false
139 }
140 if includedMap[dir] {
141 return false
142 } else if excludedMap[dir] {
143 return true
144 } else if defaultDirectoryIncludedMap[dir] {
145 return false
146 } else if defaultDirectoryExcludedMap[dir] {
147 return true
148 } else {
149 return isDirectoryExcluded(filepath.Dir(dir), excludedMap, includedMap)
150 }
151}
152
153func (vendorSnapshotImage) isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
154 return isDirectoryExcluded(dir, deviceConfig.VendorSnapshotDirsExcludedMap(), deviceConfig.VendorSnapshotDirsIncludedMap())
Inseob Kimde5744a2020-12-02 13:14:28 +0900155}
156
157// vendor snapshot includes static/header libraries with vndk: {enabled: true}.
158func (vendorSnapshotImage) includeVndk() bool {
159 return true
160}
161
162func (vendorSnapshotImage) excludeFromSnapshot(m *Module) bool {
163 return m.ExcludeFromVendorSnapshot()
164}
165
Jose Galmes6f843bc2020-12-11 13:36:29 -0800166func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
167 vndkVersion := cfg.VndkVersion()
168 return vndkVersion != "current" && vndkVersion != ""
169}
170
Colin Crosse0edaf92021-01-11 17:31:17 -0800171func (vendorSnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
172 return cfg.VndkVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800173}
174
Inseob Kim7cf14652021-01-06 23:06:52 +0900175// returns true iff a given module SHOULD BE EXCLUDED, false if included
176func (vendorSnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
177 // If we're using full snapshot, not directed snapshot, capture every module
178 if !cfg.DirectedVendorSnapshot() {
179 return false
180 }
181 // Else, checks if name is in VENDOR_SNAPSHOT_MODULES.
182 return !cfg.VendorSnapshotModules()[name]
183}
184
Colin Crosse0edaf92021-01-11 17:31:17 -0800185func (vendorSnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
186 return VendorVariationPrefix + cfg.VndkVersion()
187}
188
189func (vendorSnapshotImage) moduleNameSuffix() string {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500190 return VendorSuffix
Colin Crosse0edaf92021-01-11 17:31:17 -0800191}
192
193func (recoverySnapshotImage) init(ctx android.RegistrationContext) {
194 ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
195 ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
196 ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
197 ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
198 ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
199 ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
200 ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
Inseob Kimde5744a2020-12-02 13:14:28 +0900201}
202
Jose Galmes6f843bc2020-12-11 13:36:29 -0800203func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
204 // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a
205 // snapshot.
206 return ctx.DeviceConfig().RecoverySnapshotVersion() == "current"
207}
208
Inseob Kimde5744a2020-12-02 13:14:28 +0900209func (recoverySnapshotImage) inImage(m *Module) func() bool {
210 return m.InRecovery
211}
212
Justin Yune09ac172021-01-20 19:49:01 +0900213// recovery snapshot does not have private libraries.
214func (recoverySnapshotImage) private(m *Module) bool {
215 return false
Inseob Kimde5744a2020-12-02 13:14:28 +0900216}
217
Justin DeMartino383bfb32021-02-24 10:49:43 -0800218func (recoverySnapshotImage) isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
219 return isDirectoryExcluded(dir, deviceConfig.RecoverySnapshotDirsExcludedMap(), deviceConfig.RecoverySnapshotDirsIncludedMap())
Inseob Kimde5744a2020-12-02 13:14:28 +0900220}
221
222// recovery snapshot does NOT treat vndk specially.
223func (recoverySnapshotImage) includeVndk() bool {
224 return false
225}
226
227func (recoverySnapshotImage) excludeFromSnapshot(m *Module) bool {
228 return m.ExcludeFromRecoverySnapshot()
229}
230
Jose Galmes6f843bc2020-12-11 13:36:29 -0800231func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
232 recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
233 return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
234}
235
Colin Crosse0edaf92021-01-11 17:31:17 -0800236func (recoverySnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
237 return cfg.RecoverySnapshotVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800238}
239
Inseob Kim7cf14652021-01-06 23:06:52 +0900240func (recoverySnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
Jose Galmes4c6895e2021-02-09 07:44:30 -0800241 // If we're using full snapshot, not directed snapshot, capture every module
242 if !cfg.DirectedRecoverySnapshot() {
243 return false
244 }
245 // Else, checks if name is in RECOVERY_SNAPSHOT_MODULES.
246 return !cfg.RecoverySnapshotModules()[name]
Inseob Kim7cf14652021-01-06 23:06:52 +0900247}
248
Colin Crosse0edaf92021-01-11 17:31:17 -0800249func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
250 return android.RecoveryVariation
251}
252
253func (recoverySnapshotImage) moduleNameSuffix() string {
254 return recoverySuffix
255}
256
Inseob Kimde5744a2020-12-02 13:14:28 +0900257var vendorSnapshotImageSingleton vendorSnapshotImage
258var recoverySnapshotImageSingleton recoverySnapshotImage
259
260func init() {
Colin Crosse0edaf92021-01-11 17:31:17 -0800261 vendorSnapshotImageSingleton.init(android.InitRegistrationContext)
262 recoverySnapshotImageSingleton.init(android.InitRegistrationContext)
Inseob Kimde5744a2020-12-02 13:14:28 +0900263}
264
265const (
Colin Crosse0edaf92021-01-11 17:31:17 -0800266 snapshotHeaderSuffix = "_header."
267 snapshotSharedSuffix = "_shared."
268 snapshotStaticSuffix = "_static."
269 snapshotBinarySuffix = "_binary."
270 snapshotObjectSuffix = "_object."
Inseob Kimde5744a2020-12-02 13:14:28 +0900271)
272
Colin Crosse0edaf92021-01-11 17:31:17 -0800273type SnapshotProperties struct {
274 Header_libs []string `android:"arch_variant"`
275 Static_libs []string `android:"arch_variant"`
276 Shared_libs []string `android:"arch_variant"`
277 Vndk_libs []string `android:"arch_variant"`
278 Binaries []string `android:"arch_variant"`
279 Objects []string `android:"arch_variant"`
280}
281
282type snapshot struct {
283 android.ModuleBase
284
285 properties SnapshotProperties
286
287 baseSnapshot baseSnapshotDecorator
288
289 image snapshotImage
290}
291
292func (s *snapshot) ImageMutatorBegin(ctx android.BaseModuleContext) {
293 cfg := ctx.DeviceConfig()
294 if !s.image.isUsingSnapshot(cfg) || s.image.targetSnapshotVersion(cfg) != s.baseSnapshot.version() {
295 s.Disable()
296 }
297}
298
299func (s *snapshot) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
300 return false
301}
302
303func (s *snapshot) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
304 return false
305}
306
307func (s *snapshot) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
308 return false
309}
310
311func (s *snapshot) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
312 return false
313}
314
315func (s *snapshot) ExtraImageVariations(ctx android.BaseModuleContext) []string {
316 return []string{s.image.imageVariantName(ctx.DeviceConfig())}
317}
318
319func (s *snapshot) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
320}
321
322func (s *snapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
323 // Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
324}
325
Justin Yun07b9f862021-02-26 14:00:03 +0900326func getSnapshotNameSuffix(moduleSuffix, version, arch string) string {
327 versionSuffix := version
328 if arch != "" {
329 versionSuffix += "." + arch
330 }
331 return moduleSuffix + versionSuffix
332}
Colin Crosse0edaf92021-01-11 17:31:17 -0800333
Justin Yun07b9f862021-02-26 14:00:03 +0900334func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
335 collectSnapshotMap := func(names []string, snapshotSuffix, moduleSuffix string) map[string]string {
Colin Crosse0edaf92021-01-11 17:31:17 -0800336 snapshotMap := make(map[string]string)
Justin Yun48138672021-02-25 18:21:27 +0900337 for _, name := range names {
338 snapshotMap[name] = name +
Justin Yun07b9f862021-02-26 14:00:03 +0900339 getSnapshotNameSuffix(snapshotSuffix+moduleSuffix,
Jose Galmesf9523ed2021-04-06 19:48:10 -0700340 s.baseSnapshot.version(),
341 ctx.DeviceConfig().Arches()[0].ArchType.String())
Colin Crosse0edaf92021-01-11 17:31:17 -0800342 }
343 return snapshotMap
344 }
345
346 snapshotSuffix := s.image.moduleNameSuffix()
Justin Yun07b9f862021-02-26 14:00:03 +0900347 headers := collectSnapshotMap(s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
348 binaries := collectSnapshotMap(s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
349 objects := collectSnapshotMap(s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
350 staticLibs := collectSnapshotMap(s.properties.Static_libs, snapshotSuffix, snapshotStaticSuffix)
351 sharedLibs := collectSnapshotMap(s.properties.Shared_libs, snapshotSuffix, snapshotSharedSuffix)
352 vndkLibs := collectSnapshotMap(s.properties.Vndk_libs, "", vndkSuffix)
Colin Crosse0edaf92021-01-11 17:31:17 -0800353 for k, v := range vndkLibs {
354 sharedLibs[k] = v
355 }
Justin Yun07b9f862021-02-26 14:00:03 +0900356
Colin Crosse0edaf92021-01-11 17:31:17 -0800357 ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
358 HeaderLibs: headers,
359 Binaries: binaries,
360 Objects: objects,
361 StaticLibs: staticLibs,
362 SharedLibs: sharedLibs,
363 })
364}
365
366type SnapshotInfo struct {
367 HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs map[string]string
368}
369
370var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
371
372var _ android.ImageInterface = (*snapshot)(nil)
373
374func vendorSnapshotFactory() android.Module {
375 return snapshotFactory(vendorSnapshotImageSingleton)
376}
377
378func recoverySnapshotFactory() android.Module {
379 return snapshotFactory(recoverySnapshotImageSingleton)
380}
381
382func snapshotFactory(image snapshotImage) android.Module {
383 snapshot := &snapshot{}
384 snapshot.image = image
385 snapshot.AddProperties(
386 &snapshot.properties,
387 &snapshot.baseSnapshot.baseProperties)
388 android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
389 return snapshot
390}
391
Inseob Kimde5744a2020-12-02 13:14:28 +0900392type baseSnapshotDecoratorProperties struct {
393 // snapshot version.
394 Version string
395
396 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
397 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800398
Colin Crossa8890802021-01-22 14:06:33 -0800399 // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
Inseob Kim1b6fb872021-04-05 13:37:02 +0900400 Androidmk_suffix string `blueprint:"mutated"`
Colin Crossa8890802021-01-22 14:06:33 -0800401
Jose Galmes6f843bc2020-12-11 13:36:29 -0800402 // Suffix to be added to the module name, e.g., vendor_shared,
403 // recovery_shared, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800404 ModuleSuffix string `blueprint:"mutated"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900405}
406
407// baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
408// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
409// collide with source modules. e.g. the following example module,
410//
411// vendor_snapshot_static {
412// name: "libbase",
413// arch: "arm64",
414// version: 30,
415// ...
416// }
417//
418// will be seen as "libbase.vendor_static.30.arm64" by Soong.
419type baseSnapshotDecorator struct {
420 baseProperties baseSnapshotDecoratorProperties
Inseob Kim1b6fb872021-04-05 13:37:02 +0900421 image snapshotImage
Inseob Kimde5744a2020-12-02 13:14:28 +0900422}
423
424func (p *baseSnapshotDecorator) Name(name string) string {
425 return name + p.NameSuffix()
426}
427
428func (p *baseSnapshotDecorator) NameSuffix() string {
Justin Yun07b9f862021-02-26 14:00:03 +0900429 return getSnapshotNameSuffix(p.moduleSuffix(), p.version(), p.arch())
Inseob Kimde5744a2020-12-02 13:14:28 +0900430}
431
432func (p *baseSnapshotDecorator) version() string {
433 return p.baseProperties.Version
434}
435
436func (p *baseSnapshotDecorator) arch() string {
437 return p.baseProperties.Target_arch
438}
439
Colin Crosse0edaf92021-01-11 17:31:17 -0800440func (p *baseSnapshotDecorator) moduleSuffix() string {
441 return p.baseProperties.ModuleSuffix
Jose Galmes6f843bc2020-12-11 13:36:29 -0800442}
443
Inseob Kimde5744a2020-12-02 13:14:28 +0900444func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
445 return true
446}
447
Colin Crossa8890802021-01-22 14:06:33 -0800448func (p *baseSnapshotDecorator) snapshotAndroidMkSuffix() string {
449 return p.baseProperties.Androidmk_suffix
450}
451
Inseob Kim1b6fb872021-04-05 13:37:02 +0900452func (p *baseSnapshotDecorator) setSnapshotAndroidMkSuffix(ctx android.ModuleContext) {
453 if ctx.OtherModuleDependencyVariantExists([]blueprint.Variation{
454 {Mutator: "image", Variation: android.CoreVariation},
455 }, ctx.Module().(*Module).BaseModuleName()) {
456 p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
457 } else {
458 p.baseProperties.Androidmk_suffix = ""
459 }
460}
461
Inseob Kimde5744a2020-12-02 13:14:28 +0900462// Call this with a module suffix after creating a snapshot module, such as
463// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
Inseob Kim1b6fb872021-04-05 13:37:02 +0900464func (p *baseSnapshotDecorator) init(m *Module, image snapshotImage, moduleSuffix string) {
465 p.image = image
466 p.baseProperties.ModuleSuffix = image.moduleNameSuffix() + moduleSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900467 m.AddProperties(&p.baseProperties)
468 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
469 vendorSnapshotLoadHook(ctx, p)
470 })
471}
472
473// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
474// As vendor snapshot is only for vendor, such modules won't be used at all.
475func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) {
476 if p.version() != ctx.DeviceConfig().VndkVersion() {
477 ctx.Module().Disable()
478 return
479 }
480}
481
482//
483// Module definitions for snapshots of libraries (shared, static, header).
484//
485// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
486// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
487// which can be installed or linked against. Also they export flags needed when linked, such as
488// include directories, c flags, sanitize dependency information, etc.
489//
490// These modules are auto-generated by development/vendor_snapshot/update.py.
491type snapshotLibraryProperties struct {
492 // Prebuilt file for each arch.
493 Src *string `android:"arch_variant"`
494
495 // list of directories that will be added to the include path (using -I).
496 Export_include_dirs []string `android:"arch_variant"`
497
498 // list of directories that will be added to the system path (using -isystem).
499 Export_system_include_dirs []string `android:"arch_variant"`
500
501 // list of flags that will be used for any module that links against this module.
502 Export_flags []string `android:"arch_variant"`
503
504 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
505 Sanitize_ubsan_dep *bool `android:"arch_variant"`
506
507 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
508 Sanitize_minimal_dep *bool `android:"arch_variant"`
509}
510
511type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500512 isSanitizerEnabled(t SanitizerType) bool
513 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900514}
515
516type snapshotLibraryDecorator struct {
517 baseSnapshotDecorator
518 *libraryDecorator
519 properties snapshotLibraryProperties
520 sanitizerProperties struct {
521 CfiEnabled bool `blueprint:"mutated"`
522
523 // Library flags for cfi variant.
524 Cfi snapshotLibraryProperties `android:"arch_variant"`
525 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900526}
527
528func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
529 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
530 return p.libraryDecorator.linkerFlags(ctx, flags)
531}
532
533func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
534 arches := config.Arches()
535 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
536 return false
537 }
538 if !p.header() && p.properties.Src == nil {
539 return false
540 }
541 return true
542}
543
544// cc modules' link functions are to link compiled objects into final binaries.
545// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
546// done by normal library decorator, e.g. exporting flags.
547func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900548 p.setSnapshotAndroidMkSuffix(ctx)
549
Inseob Kimde5744a2020-12-02 13:14:28 +0900550 if p.header() {
551 return p.libraryDecorator.link(ctx, flags, deps, objs)
552 }
553
554 if p.sanitizerProperties.CfiEnabled {
555 p.properties = p.sanitizerProperties.Cfi
556 }
557
558 if !p.matchesWithDevice(ctx.DeviceConfig()) {
559 return nil
560 }
561
562 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
563 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
564 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
565
566 in := android.PathForModuleSrc(ctx, *p.properties.Src)
567 p.unstrippedOutputFile = in
568
569 if p.shared() {
570 libName := in.Base()
571 builderFlags := flagsToBuilderFlags(flags)
572
573 // Optimize out relinking against shared libraries whose interface hasn't changed by
574 // depending on a table of contents file instead of the library itself.
575 tocFile := android.PathForModuleOut(ctx, libName+".toc")
576 p.tocFile = android.OptionalPathForPath(tocFile)
577 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
578
579 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
580 SharedLibrary: in,
581 UnstrippedSharedLibrary: p.unstrippedOutputFile,
582
583 TableOfContents: p.tocFile,
584 })
585 }
586
587 if p.static() {
588 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
589 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
590 StaticLibrary: in,
591
592 TransitiveStaticLibrariesForOrdering: depSet,
593 })
594 }
595
596 p.libraryDecorator.flagExporter.setProvider(ctx)
597
598 return in
599}
600
601func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
602 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
603 p.baseInstaller.install(ctx, file)
604 }
605}
606
607func (p *snapshotLibraryDecorator) nativeCoverage() bool {
608 return false
609}
610
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500611func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900612 switch t {
613 case cfi:
614 return p.sanitizerProperties.Cfi.Src != nil
615 default:
616 return false
617 }
618}
619
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500620func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900621 if !enabled {
622 return
623 }
624 switch t {
625 case cfi:
626 p.sanitizerProperties.CfiEnabled = true
627 default:
628 return
629 }
630}
631
Inseob Kim1b6fb872021-04-05 13:37:02 +0900632func snapshotLibraryFactory(image snapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900633 module, library := NewLibrary(android.DeviceSupported)
634
635 module.stl = nil
636 module.sanitize = nil
637 library.disableStripping()
638
639 prebuilt := &snapshotLibraryDecorator{
640 libraryDecorator: library,
641 }
642
643 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
644 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
645
646 // Prevent default system libs (libc, libm, and libdl) from being linked
647 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
648 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
649 }
650
651 module.compiler = nil
652 module.linker = prebuilt
653 module.installer = prebuilt
654
Inseob Kim1b6fb872021-04-05 13:37:02 +0900655 prebuilt.init(module, image, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900656 module.AddProperties(
657 &prebuilt.properties,
658 &prebuilt.sanitizerProperties,
659 )
660
661 return module, prebuilt
662}
663
664// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
665// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
666// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
667// is set.
668func VendorSnapshotSharedFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900669 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton, snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900670 prebuilt.libraryDecorator.BuildOnlyShared()
671 return module.Init()
672}
673
674// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
675// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
676// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
677// is set.
678func RecoverySnapshotSharedFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900679 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900680 prebuilt.libraryDecorator.BuildOnlyShared()
681 return module.Init()
682}
683
684// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
685// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
686// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
687// is set.
688func VendorSnapshotStaticFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900689 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton, snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900690 prebuilt.libraryDecorator.BuildOnlyStatic()
691 return module.Init()
692}
693
694// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
695// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
696// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
697// is set.
698func RecoverySnapshotStaticFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900699 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900700 prebuilt.libraryDecorator.BuildOnlyStatic()
701 return module.Init()
702}
703
704// vendor_snapshot_header is a special header library which is auto-generated by
705// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
706// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
707// is set.
708func VendorSnapshotHeaderFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900709 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton, snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900710 prebuilt.libraryDecorator.HeaderOnly()
711 return module.Init()
712}
713
714// recovery_snapshot_header is a special header library which is auto-generated by
715// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
716// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
717// is set.
718func RecoverySnapshotHeaderFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900719 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900720 prebuilt.libraryDecorator.HeaderOnly()
721 return module.Init()
722}
723
724var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
725
726//
727// Module definitions for snapshots of executable binaries.
728//
729// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
730// binaries (e.g. toybox, sh) as their src, which can be installed.
731//
732// These modules are auto-generated by development/vendor_snapshot/update.py.
733type snapshotBinaryProperties struct {
734 // Prebuilt file for each arch.
735 Src *string `android:"arch_variant"`
736}
737
738type snapshotBinaryDecorator struct {
739 baseSnapshotDecorator
740 *binaryDecorator
Colin Crossa8890802021-01-22 14:06:33 -0800741 properties snapshotBinaryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900742}
743
744func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
745 if config.DeviceArch() != p.arch() {
746 return false
747 }
748 if p.properties.Src == nil {
749 return false
750 }
751 return true
752}
753
754// cc modules' link functions are to link compiled objects into final binaries.
755// As snapshots are prebuilts, this just returns the prebuilt binary
756func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900757 p.setSnapshotAndroidMkSuffix(ctx)
758
Inseob Kimde5744a2020-12-02 13:14:28 +0900759 if !p.matchesWithDevice(ctx.DeviceConfig()) {
760 return nil
761 }
762
763 in := android.PathForModuleSrc(ctx, *p.properties.Src)
764 p.unstrippedOutputFile = in
765 binName := in.Base()
766
Inseob Kimde5744a2020-12-02 13:14:28 +0900767 // use cpExecutable to make it executable
768 outputFile := android.PathForModuleOut(ctx, binName)
769 ctx.Build(pctx, android.BuildParams{
770 Rule: android.CpExecutable,
771 Description: "prebuilt",
772 Output: outputFile,
773 Input: in,
774 })
775
776 return outputFile
777}
778
779func (p *snapshotBinaryDecorator) nativeCoverage() bool {
780 return false
781}
782
783// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
784// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
785// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
786func VendorSnapshotBinaryFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900787 return snapshotBinaryFactory(vendorSnapshotImageSingleton, snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900788}
789
790// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
791// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
792// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
793func RecoverySnapshotBinaryFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900794 return snapshotBinaryFactory(recoverySnapshotImageSingleton, snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900795}
796
Inseob Kim1b6fb872021-04-05 13:37:02 +0900797func snapshotBinaryFactory(image snapshotImage, moduleSuffix string) android.Module {
Inseob Kimde5744a2020-12-02 13:14:28 +0900798 module, binary := NewBinary(android.DeviceSupported)
799 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
800 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
801
802 // Prevent default system libs (libc, libm, and libdl) from being linked
803 if binary.baseLinker.Properties.System_shared_libs == nil {
804 binary.baseLinker.Properties.System_shared_libs = []string{}
805 }
806
807 prebuilt := &snapshotBinaryDecorator{
808 binaryDecorator: binary,
809 }
810
811 module.compiler = nil
812 module.sanitize = nil
813 module.stl = nil
814 module.linker = prebuilt
815
Inseob Kim1b6fb872021-04-05 13:37:02 +0900816 prebuilt.init(module, image, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900817 module.AddProperties(&prebuilt.properties)
818 return module.Init()
819}
820
821//
822// Module definitions for snapshots of object files (*.o).
823//
824// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
825// files (*.o) as their src.
826//
827// These modules are auto-generated by development/vendor_snapshot/update.py.
828type vendorSnapshotObjectProperties struct {
829 // Prebuilt file for each arch.
830 Src *string `android:"arch_variant"`
831}
832
833type snapshotObjectLinker struct {
834 baseSnapshotDecorator
835 objectLinker
Colin Crossa8890802021-01-22 14:06:33 -0800836 properties vendorSnapshotObjectProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900837}
838
839func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
840 if config.DeviceArch() != p.arch() {
841 return false
842 }
843 if p.properties.Src == nil {
844 return false
845 }
846 return true
847}
848
849// cc modules' link functions are to link compiled objects into final binaries.
850// As snapshots are prebuilts, this just returns the prebuilt binary
851func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900852 p.setSnapshotAndroidMkSuffix(ctx)
853
Inseob Kimde5744a2020-12-02 13:14:28 +0900854 if !p.matchesWithDevice(ctx.DeviceConfig()) {
855 return nil
856 }
857
Inseob Kimde5744a2020-12-02 13:14:28 +0900858 return android.PathForModuleSrc(ctx, *p.properties.Src)
859}
860
861func (p *snapshotObjectLinker) nativeCoverage() bool {
862 return false
863}
864
865// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
866// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
867// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
868func VendorSnapshotObjectFactory() android.Module {
869 module := newObject()
870
871 prebuilt := &snapshotObjectLinker{
872 objectLinker: objectLinker{
873 baseLinker: NewBaseLinker(nil),
874 },
875 }
876 module.linker = prebuilt
877
Inseob Kim1b6fb872021-04-05 13:37:02 +0900878 prebuilt.init(module, vendorSnapshotImageSingleton, snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900879 module.AddProperties(&prebuilt.properties)
880 return module.Init()
881}
882
883// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
884// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
885// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
886func RecoverySnapshotObjectFactory() android.Module {
887 module := newObject()
888
889 prebuilt := &snapshotObjectLinker{
890 objectLinker: objectLinker{
891 baseLinker: NewBaseLinker(nil),
892 },
893 }
894 module.linker = prebuilt
895
Inseob Kim1b6fb872021-04-05 13:37:02 +0900896 prebuilt.init(module, recoverySnapshotImageSingleton, snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900897 module.AddProperties(&prebuilt.properties)
898 return module.Init()
899}
900
901type snapshotInterface interface {
902 matchesWithDevice(config android.DeviceConfig) bool
Colin Crossa8890802021-01-22 14:06:33 -0800903 isSnapshotPrebuilt() bool
904 version() string
905 snapshotAndroidMkSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +0900906}
907
908var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
909var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
910var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
911var _ snapshotInterface = (*snapshotObjectLinker)(nil)