blob: 020dcd77fed44ca04a0b0a8422f850ac7e42effe [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 (
21 "strings"
22 "sync"
23
24 "android/soong/android"
Jose Galmes6f843bc2020-12-11 13:36:29 -080025
26 "github.com/google/blueprint/proptools"
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 {
32 // Used to register callbacks with the build system.
33 init()
34
Jose Galmes6f843bc2020-12-11 13:36:29 -080035 // Returns true if a snapshot should be generated for this image.
36 shouldGenerateSnapshot(ctx android.SingletonContext) bool
37
Inseob Kimde5744a2020-12-02 13:14:28 +090038 // Function that returns true if the module is included in this image.
39 // Using a function return instead of a value to prevent early
40 // evalution of a function that may be not be defined.
41 inImage(m *Module) func() bool
42
Justin Yune09ac172021-01-20 19:49:01 +090043 // Returns true if the module is private and must not be included in the
44 // snapshot. For example VNDK-private modules must return true for the
45 // vendor snapshots. But false for the recovery snapshots.
46 private(m *Module) bool
Inseob Kimde5744a2020-12-02 13:14:28 +090047
48 // Returns true if a dir under source tree is an SoC-owned proprietary
49 // directory, such as device/, vendor/, etc.
50 //
51 // For a given snapshot (e.g., vendor, recovery, etc.) if
52 // isProprietaryPath(dir) returns true, then the module in dir will be
53 // built from sources.
54 isProprietaryPath(dir string) bool
55
56 // Whether to include VNDK in the snapshot for this image.
57 includeVndk() bool
58
59 // Whether a given module has been explicitly excluded from the
60 // snapshot, e.g., using the exclude_from_vendor_snapshot or
61 // exclude_from_recovery_snapshot properties.
62 excludeFromSnapshot(m *Module) bool
Jose Galmes6f843bc2020-12-11 13:36:29 -080063
64 // Returns the snapshotMap to be used for a given module and config, or nil if the
65 // module is not included in this image.
66 getSnapshotMap(m *Module, cfg android.Config) *snapshotMap
67
68 // Returns mutex used for mutual exclusion when updating the snapshot maps.
69 getMutex() *sync.Mutex
70
71 // For a given arch, a maps of which modules are included in this image.
72 suffixModules(config android.Config) map[string]bool
73
74 // Whether to add a given module to the suffix map.
75 shouldBeAddedToSuffixModules(m *Module) bool
76
77 // Returns true if the build is using a snapshot for this image.
78 isUsingSnapshot(cfg android.DeviceConfig) bool
79
80 // Whether to skip the module mutator for a module in a given context.
81 skipModuleMutator(ctx android.BottomUpMutatorContext) bool
82
83 // Whether to skip the source mutator for a given module.
84 skipSourceMutator(ctx android.BottomUpMutatorContext) bool
Inseob Kim7cf14652021-01-06 23:06:52 +090085
86 // Whether to exclude a given module from the directed snapshot or not.
87 // If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on,
88 // and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured.
89 excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool
Inseob Kimde5744a2020-12-02 13:14:28 +090090}
91
92type vendorSnapshotImage struct{}
93type recoverySnapshotImage struct{}
94
95func (vendorSnapshotImage) init() {
96 android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
97 android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
98 android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
99 android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
100 android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
101 android.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
Inseob Kime9aec6a2021-01-05 20:03:22 +0900102
103 android.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
Inseob Kimde5744a2020-12-02 13:14:28 +0900104}
105
Jose Galmes6f843bc2020-12-11 13:36:29 -0800106func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
107 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot.
108 return ctx.DeviceConfig().VndkVersion() == "current"
109}
110
Inseob Kimde5744a2020-12-02 13:14:28 +0900111func (vendorSnapshotImage) inImage(m *Module) func() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500112 return m.InVendor
Inseob Kimde5744a2020-12-02 13:14:28 +0900113}
114
Justin Yune09ac172021-01-20 19:49:01 +0900115func (vendorSnapshotImage) private(m *Module) bool {
116 return m.IsVndkPrivate()
Inseob Kimde5744a2020-12-02 13:14:28 +0900117}
118
119func (vendorSnapshotImage) isProprietaryPath(dir string) bool {
120 return isVendorProprietaryPath(dir)
121}
122
123// vendor snapshot includes static/header libraries with vndk: {enabled: true}.
124func (vendorSnapshotImage) includeVndk() bool {
125 return true
126}
127
128func (vendorSnapshotImage) excludeFromSnapshot(m *Module) bool {
129 return m.ExcludeFromVendorSnapshot()
130}
131
Jose Galmes6f843bc2020-12-11 13:36:29 -0800132func (vendorSnapshotImage) getSnapshotMap(m *Module, cfg android.Config) *snapshotMap {
133 if lib, ok := m.linker.(libraryInterface); ok {
134 if lib.static() {
135 return vendorSnapshotStaticLibs(cfg)
136 } else if lib.shared() {
137 return vendorSnapshotSharedLibs(cfg)
138 } else {
139 // header
140 return vendorSnapshotHeaderLibs(cfg)
141 }
142 } else if m.binary() {
143 return vendorSnapshotBinaries(cfg)
144 } else if m.object() {
145 return vendorSnapshotObjects(cfg)
146 } else {
147 return nil
148 }
149}
150
151func (vendorSnapshotImage) getMutex() *sync.Mutex {
152 return &vendorSnapshotsLock
153}
154
155func (vendorSnapshotImage) suffixModules(config android.Config) map[string]bool {
156 return vendorSuffixModules(config)
157}
158
159func (vendorSnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool {
160 // vendor suffix should be added to snapshots if the source module isn't vendor: true.
161 if module.SocSpecific() {
162 return false
163 }
164
165 // But we can't just check SocSpecific() since we already passed the image mutator.
166 // Check ramdisk and recovery to see if we are real "vendor: true" module.
167 ramdiskAvailable := module.InRamdisk() && !module.OnlyInRamdisk()
168 vendorRamdiskAvailable := module.InVendorRamdisk() && !module.OnlyInVendorRamdisk()
169 recoveryAvailable := module.InRecovery() && !module.OnlyInRecovery()
170
171 return !ramdiskAvailable && !recoveryAvailable && !vendorRamdiskAvailable
172}
173
174func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
175 vndkVersion := cfg.VndkVersion()
176 return vndkVersion != "current" && vndkVersion != ""
177}
178
179func (vendorSnapshotImage) skipModuleMutator(ctx android.BottomUpMutatorContext) bool {
180 vndkVersion := ctx.DeviceConfig().VndkVersion()
181 module, ok := ctx.Module().(*Module)
182 return !ok || module.VndkVersion() != vndkVersion
183}
184
185func (vendorSnapshotImage) skipSourceMutator(ctx android.BottomUpMutatorContext) bool {
186 vndkVersion := ctx.DeviceConfig().VndkVersion()
187 module, ok := ctx.Module().(*Module)
188 if !ok {
189 return true
190 }
191 if module.VndkVersion() != vndkVersion {
192 return true
193 }
194 // .. and also filter out llndk library
195 if module.IsLlndk() {
196 return true
197 }
198 return false
199}
200
Inseob Kim7cf14652021-01-06 23:06:52 +0900201// returns true iff a given module SHOULD BE EXCLUDED, false if included
202func (vendorSnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
203 // If we're using full snapshot, not directed snapshot, capture every module
204 if !cfg.DirectedVendorSnapshot() {
205 return false
206 }
207 // Else, checks if name is in VENDOR_SNAPSHOT_MODULES.
208 return !cfg.VendorSnapshotModules()[name]
209}
210
Inseob Kimde5744a2020-12-02 13:14:28 +0900211func (recoverySnapshotImage) init() {
212 android.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
213 android.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
214 android.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
215 android.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
216 android.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
217 android.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
218}
219
Jose Galmes6f843bc2020-12-11 13:36:29 -0800220func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
221 // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a
222 // snapshot.
223 return ctx.DeviceConfig().RecoverySnapshotVersion() == "current"
224}
225
Inseob Kimde5744a2020-12-02 13:14:28 +0900226func (recoverySnapshotImage) inImage(m *Module) func() bool {
227 return m.InRecovery
228}
229
Justin Yune09ac172021-01-20 19:49:01 +0900230// recovery snapshot does not have private libraries.
231func (recoverySnapshotImage) private(m *Module) bool {
232 return false
Inseob Kimde5744a2020-12-02 13:14:28 +0900233}
234
235func (recoverySnapshotImage) isProprietaryPath(dir string) bool {
236 return isRecoveryProprietaryPath(dir)
237}
238
239// recovery snapshot does NOT treat vndk specially.
240func (recoverySnapshotImage) includeVndk() bool {
241 return false
242}
243
244func (recoverySnapshotImage) excludeFromSnapshot(m *Module) bool {
245 return m.ExcludeFromRecoverySnapshot()
246}
247
Jose Galmes6f843bc2020-12-11 13:36:29 -0800248func (recoverySnapshotImage) getSnapshotMap(m *Module, cfg android.Config) *snapshotMap {
249 if lib, ok := m.linker.(libraryInterface); ok {
250 if lib.static() {
251 return recoverySnapshotStaticLibs(cfg)
252 } else if lib.shared() {
253 return recoverySnapshotSharedLibs(cfg)
254 } else {
255 // header
256 return recoverySnapshotHeaderLibs(cfg)
257 }
258 } else if m.binary() {
259 return recoverySnapshotBinaries(cfg)
260 } else if m.object() {
261 return recoverySnapshotObjects(cfg)
262 } else {
263 return nil
264 }
265}
266
267func (recoverySnapshotImage) getMutex() *sync.Mutex {
268 return &recoverySnapshotsLock
269}
270
271func (recoverySnapshotImage) suffixModules(config android.Config) map[string]bool {
272 return recoverySuffixModules(config)
273}
274
275func (recoverySnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool {
276 return proptools.BoolDefault(module.Properties.Recovery_available, false)
277}
278
279func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
280 recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
281 return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
282}
283
284func (recoverySnapshotImage) skipModuleMutator(ctx android.BottomUpMutatorContext) bool {
285 module, ok := ctx.Module().(*Module)
286 return !ok || !module.InRecovery()
287}
288
289func (recoverySnapshotImage) skipSourceMutator(ctx android.BottomUpMutatorContext) bool {
290 module, ok := ctx.Module().(*Module)
291 return !ok || !module.InRecovery()
292}
293
Inseob Kim7cf14652021-01-06 23:06:52 +0900294func (recoverySnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
295 // directed recovery snapshot is not implemented yet
296 return false
297}
298
Inseob Kimde5744a2020-12-02 13:14:28 +0900299var vendorSnapshotImageSingleton vendorSnapshotImage
300var recoverySnapshotImageSingleton recoverySnapshotImage
301
302func init() {
303 vendorSnapshotImageSingleton.init()
304 recoverySnapshotImageSingleton.init()
305}
306
307const (
308 vendorSnapshotHeaderSuffix = ".vendor_header."
309 vendorSnapshotSharedSuffix = ".vendor_shared."
310 vendorSnapshotStaticSuffix = ".vendor_static."
311 vendorSnapshotBinarySuffix = ".vendor_binary."
312 vendorSnapshotObjectSuffix = ".vendor_object."
313)
314
315const (
316 recoverySnapshotHeaderSuffix = ".recovery_header."
317 recoverySnapshotSharedSuffix = ".recovery_shared."
318 recoverySnapshotStaticSuffix = ".recovery_static."
319 recoverySnapshotBinarySuffix = ".recovery_binary."
320 recoverySnapshotObjectSuffix = ".recovery_object."
321)
322
323var (
324 vendorSnapshotsLock sync.Mutex
325 vendorSuffixModulesKey = android.NewOnceKey("vendorSuffixModules")
326 vendorSnapshotHeaderLibsKey = android.NewOnceKey("vendorSnapshotHeaderLibs")
327 vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs")
328 vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs")
329 vendorSnapshotBinariesKey = android.NewOnceKey("vendorSnapshotBinaries")
330 vendorSnapshotObjectsKey = android.NewOnceKey("vendorSnapshotObjects")
331)
332
Jose Galmes6f843bc2020-12-11 13:36:29 -0800333var (
334 recoverySnapshotsLock sync.Mutex
335 recoverySuffixModulesKey = android.NewOnceKey("recoverySuffixModules")
336 recoverySnapshotHeaderLibsKey = android.NewOnceKey("recoverySnapshotHeaderLibs")
337 recoverySnapshotStaticLibsKey = android.NewOnceKey("recoverySnapshotStaticLibs")
338 recoverySnapshotSharedLibsKey = android.NewOnceKey("recoverySnapshotSharedLibs")
339 recoverySnapshotBinariesKey = android.NewOnceKey("recoverySnapshotBinaries")
340 recoverySnapshotObjectsKey = android.NewOnceKey("recoverySnapshotObjects")
341)
342
Inseob Kimde5744a2020-12-02 13:14:28 +0900343// vendorSuffixModules holds names of modules whose vendor variants should have the vendor suffix.
344// This is determined by source modules, and then this will be used when exporting snapshot modules
345// to Makefile.
346//
347// For example, if libbase has "vendor_available: true", the name of core variant will be "libbase"
348// while the name of vendor variant will be "libbase.vendor". In such cases, the vendor snapshot of
349// "libbase" should be exported with the name "libbase.vendor".
350//
351// Refer to VendorSnapshotSourceMutator and makeLibName which use this.
352func vendorSuffixModules(config android.Config) map[string]bool {
353 return config.Once(vendorSuffixModulesKey, func() interface{} {
354 return make(map[string]bool)
355 }).(map[string]bool)
356}
357
358// these are vendor snapshot maps holding names of vendor snapshot modules
359func vendorSnapshotHeaderLibs(config android.Config) *snapshotMap {
360 return config.Once(vendorSnapshotHeaderLibsKey, func() interface{} {
361 return newSnapshotMap()
362 }).(*snapshotMap)
363}
364
365func vendorSnapshotSharedLibs(config android.Config) *snapshotMap {
366 return config.Once(vendorSnapshotSharedLibsKey, func() interface{} {
367 return newSnapshotMap()
368 }).(*snapshotMap)
369}
370
371func vendorSnapshotStaticLibs(config android.Config) *snapshotMap {
372 return config.Once(vendorSnapshotStaticLibsKey, func() interface{} {
373 return newSnapshotMap()
374 }).(*snapshotMap)
375}
376
377func vendorSnapshotBinaries(config android.Config) *snapshotMap {
378 return config.Once(vendorSnapshotBinariesKey, func() interface{} {
379 return newSnapshotMap()
380 }).(*snapshotMap)
381}
382
383func vendorSnapshotObjects(config android.Config) *snapshotMap {
384 return config.Once(vendorSnapshotObjectsKey, func() interface{} {
385 return newSnapshotMap()
386 }).(*snapshotMap)
387}
388
Jose Galmes6f843bc2020-12-11 13:36:29 -0800389func recoverySuffixModules(config android.Config) map[string]bool {
390 return config.Once(recoverySuffixModulesKey, func() interface{} {
391 return make(map[string]bool)
392 }).(map[string]bool)
393}
394
395func recoverySnapshotHeaderLibs(config android.Config) *snapshotMap {
396 return config.Once(recoverySnapshotHeaderLibsKey, func() interface{} {
397 return newSnapshotMap()
398 }).(*snapshotMap)
399}
400
401func recoverySnapshotSharedLibs(config android.Config) *snapshotMap {
402 return config.Once(recoverySnapshotSharedLibsKey, func() interface{} {
403 return newSnapshotMap()
404 }).(*snapshotMap)
405}
406
407func recoverySnapshotStaticLibs(config android.Config) *snapshotMap {
408 return config.Once(recoverySnapshotStaticLibsKey, func() interface{} {
409 return newSnapshotMap()
410 }).(*snapshotMap)
411}
412
413func recoverySnapshotBinaries(config android.Config) *snapshotMap {
414 return config.Once(recoverySnapshotBinariesKey, func() interface{} {
415 return newSnapshotMap()
416 }).(*snapshotMap)
417}
418
419func recoverySnapshotObjects(config android.Config) *snapshotMap {
420 return config.Once(recoverySnapshotObjectsKey, func() interface{} {
421 return newSnapshotMap()
422 }).(*snapshotMap)
423}
424
Inseob Kimde5744a2020-12-02 13:14:28 +0900425type baseSnapshotDecoratorProperties struct {
426 // snapshot version.
427 Version string
428
429 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
430 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800431
432 // Suffix to be added to the module name, e.g., vendor_shared,
433 // recovery_shared, etc.
434 Module_suffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900435}
436
437// baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
438// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
439// collide with source modules. e.g. the following example module,
440//
441// vendor_snapshot_static {
442// name: "libbase",
443// arch: "arm64",
444// version: 30,
445// ...
446// }
447//
448// will be seen as "libbase.vendor_static.30.arm64" by Soong.
449type baseSnapshotDecorator struct {
450 baseProperties baseSnapshotDecoratorProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900451}
452
453func (p *baseSnapshotDecorator) Name(name string) string {
454 return name + p.NameSuffix()
455}
456
457func (p *baseSnapshotDecorator) NameSuffix() string {
458 versionSuffix := p.version()
459 if p.arch() != "" {
460 versionSuffix += "." + p.arch()
461 }
462
Jose Galmes6f843bc2020-12-11 13:36:29 -0800463 return p.baseProperties.Module_suffix + versionSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900464}
465
466func (p *baseSnapshotDecorator) version() string {
467 return p.baseProperties.Version
468}
469
470func (p *baseSnapshotDecorator) arch() string {
471 return p.baseProperties.Target_arch
472}
473
Jose Galmes6f843bc2020-12-11 13:36:29 -0800474func (p *baseSnapshotDecorator) module_suffix() string {
475 return p.baseProperties.Module_suffix
476}
477
Inseob Kimde5744a2020-12-02 13:14:28 +0900478func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
479 return true
480}
481
482// Call this with a module suffix after creating a snapshot module, such as
483// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
484func (p *baseSnapshotDecorator) init(m *Module, suffix string) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800485 p.baseProperties.Module_suffix = suffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900486 m.AddProperties(&p.baseProperties)
487 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
488 vendorSnapshotLoadHook(ctx, p)
489 })
490}
491
492// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
493// As vendor snapshot is only for vendor, such modules won't be used at all.
494func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) {
495 if p.version() != ctx.DeviceConfig().VndkVersion() {
496 ctx.Module().Disable()
497 return
498 }
499}
500
501//
502// Module definitions for snapshots of libraries (shared, static, header).
503//
504// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
505// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
506// which can be installed or linked against. Also they export flags needed when linked, such as
507// include directories, c flags, sanitize dependency information, etc.
508//
509// These modules are auto-generated by development/vendor_snapshot/update.py.
510type snapshotLibraryProperties struct {
511 // Prebuilt file for each arch.
512 Src *string `android:"arch_variant"`
513
514 // list of directories that will be added to the include path (using -I).
515 Export_include_dirs []string `android:"arch_variant"`
516
517 // list of directories that will be added to the system path (using -isystem).
518 Export_system_include_dirs []string `android:"arch_variant"`
519
520 // list of flags that will be used for any module that links against this module.
521 Export_flags []string `android:"arch_variant"`
522
523 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
524 Sanitize_ubsan_dep *bool `android:"arch_variant"`
525
526 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
527 Sanitize_minimal_dep *bool `android:"arch_variant"`
528}
529
530type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500531 isSanitizerEnabled(t SanitizerType) bool
532 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900533}
534
535type snapshotLibraryDecorator struct {
536 baseSnapshotDecorator
537 *libraryDecorator
538 properties snapshotLibraryProperties
539 sanitizerProperties struct {
540 CfiEnabled bool `blueprint:"mutated"`
541
542 // Library flags for cfi variant.
543 Cfi snapshotLibraryProperties `android:"arch_variant"`
544 }
Jose Galmes6f843bc2020-12-11 13:36:29 -0800545 androidMkSuffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900546}
547
548func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
549 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
550 return p.libraryDecorator.linkerFlags(ctx, flags)
551}
552
553func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
554 arches := config.Arches()
555 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
556 return false
557 }
558 if !p.header() && p.properties.Src == nil {
559 return false
560 }
561 return true
562}
563
564// cc modules' link functions are to link compiled objects into final binaries.
565// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
566// done by normal library decorator, e.g. exporting flags.
567func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
568 m := ctx.Module().(*Module)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800569
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500570 if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800571 p.androidMkSuffix = vendorSuffix
572 } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
573 p.androidMkSuffix = recoverySuffix
574 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900575
576 if p.header() {
577 return p.libraryDecorator.link(ctx, flags, deps, objs)
578 }
579
580 if p.sanitizerProperties.CfiEnabled {
581 p.properties = p.sanitizerProperties.Cfi
582 }
583
584 if !p.matchesWithDevice(ctx.DeviceConfig()) {
585 return nil
586 }
587
588 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
589 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
590 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
591
592 in := android.PathForModuleSrc(ctx, *p.properties.Src)
593 p.unstrippedOutputFile = in
594
595 if p.shared() {
596 libName := in.Base()
597 builderFlags := flagsToBuilderFlags(flags)
598
599 // Optimize out relinking against shared libraries whose interface hasn't changed by
600 // depending on a table of contents file instead of the library itself.
601 tocFile := android.PathForModuleOut(ctx, libName+".toc")
602 p.tocFile = android.OptionalPathForPath(tocFile)
603 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
604
605 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
606 SharedLibrary: in,
607 UnstrippedSharedLibrary: p.unstrippedOutputFile,
Colin Cross2df81772021-01-26 11:00:18 -0800608 Target: ctx.Target(),
Inseob Kimde5744a2020-12-02 13:14:28 +0900609
610 TableOfContents: p.tocFile,
611 })
612 }
613
614 if p.static() {
615 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
616 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
617 StaticLibrary: in,
618
619 TransitiveStaticLibrariesForOrdering: depSet,
620 })
621 }
622
623 p.libraryDecorator.flagExporter.setProvider(ctx)
624
625 return in
626}
627
628func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
629 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
630 p.baseInstaller.install(ctx, file)
631 }
632}
633
634func (p *snapshotLibraryDecorator) nativeCoverage() bool {
635 return false
636}
637
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500638func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900639 switch t {
640 case cfi:
641 return p.sanitizerProperties.Cfi.Src != nil
642 default:
643 return false
644 }
645}
646
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500647func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900648 if !enabled {
649 return
650 }
651 switch t {
652 case cfi:
653 p.sanitizerProperties.CfiEnabled = true
654 default:
655 return
656 }
657}
658
659func snapshotLibraryFactory(suffix string) (*Module, *snapshotLibraryDecorator) {
660 module, library := NewLibrary(android.DeviceSupported)
661
662 module.stl = nil
663 module.sanitize = nil
664 library.disableStripping()
665
666 prebuilt := &snapshotLibraryDecorator{
667 libraryDecorator: library,
668 }
669
670 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
671 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
672
673 // Prevent default system libs (libc, libm, and libdl) from being linked
674 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
675 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
676 }
677
678 module.compiler = nil
679 module.linker = prebuilt
680 module.installer = prebuilt
681
682 prebuilt.init(module, suffix)
683 module.AddProperties(
684 &prebuilt.properties,
685 &prebuilt.sanitizerProperties,
686 )
687
688 return module, prebuilt
689}
690
691// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
692// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
693// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
694// is set.
695func VendorSnapshotSharedFactory() android.Module {
696 module, prebuilt := snapshotLibraryFactory(vendorSnapshotSharedSuffix)
697 prebuilt.libraryDecorator.BuildOnlyShared()
698 return module.Init()
699}
700
701// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
702// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
703// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
704// is set.
705func RecoverySnapshotSharedFactory() android.Module {
706 module, prebuilt := snapshotLibraryFactory(recoverySnapshotSharedSuffix)
707 prebuilt.libraryDecorator.BuildOnlyShared()
708 return module.Init()
709}
710
711// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
712// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
713// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
714// is set.
715func VendorSnapshotStaticFactory() android.Module {
716 module, prebuilt := snapshotLibraryFactory(vendorSnapshotStaticSuffix)
717 prebuilt.libraryDecorator.BuildOnlyStatic()
718 return module.Init()
719}
720
721// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
722// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
723// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
724// is set.
725func RecoverySnapshotStaticFactory() android.Module {
726 module, prebuilt := snapshotLibraryFactory(recoverySnapshotStaticSuffix)
727 prebuilt.libraryDecorator.BuildOnlyStatic()
728 return module.Init()
729}
730
731// vendor_snapshot_header is a special header library which is auto-generated by
732// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
733// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
734// is set.
735func VendorSnapshotHeaderFactory() android.Module {
736 module, prebuilt := snapshotLibraryFactory(vendorSnapshotHeaderSuffix)
737 prebuilt.libraryDecorator.HeaderOnly()
738 return module.Init()
739}
740
741// recovery_snapshot_header is a special header library which is auto-generated by
742// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
743// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
744// is set.
745func RecoverySnapshotHeaderFactory() android.Module {
746 module, prebuilt := snapshotLibraryFactory(recoverySnapshotHeaderSuffix)
747 prebuilt.libraryDecorator.HeaderOnly()
748 return module.Init()
749}
750
751var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
752
753//
754// Module definitions for snapshots of executable binaries.
755//
756// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
757// binaries (e.g. toybox, sh) as their src, which can be installed.
758//
759// These modules are auto-generated by development/vendor_snapshot/update.py.
760type snapshotBinaryProperties struct {
761 // Prebuilt file for each arch.
762 Src *string `android:"arch_variant"`
763}
764
765type snapshotBinaryDecorator struct {
766 baseSnapshotDecorator
767 *binaryDecorator
Jose Galmes6f843bc2020-12-11 13:36:29 -0800768 properties snapshotBinaryProperties
769 androidMkSuffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900770}
771
772func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
773 if config.DeviceArch() != p.arch() {
774 return false
775 }
776 if p.properties.Src == nil {
777 return false
778 }
779 return true
780}
781
782// cc modules' link functions are to link compiled objects into final binaries.
783// As snapshots are prebuilts, this just returns the prebuilt binary
784func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
785 if !p.matchesWithDevice(ctx.DeviceConfig()) {
786 return nil
787 }
788
789 in := android.PathForModuleSrc(ctx, *p.properties.Src)
790 p.unstrippedOutputFile = in
791 binName := in.Base()
792
793 m := ctx.Module().(*Module)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500794 if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800795 p.androidMkSuffix = vendorSuffix
796 } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
797 p.androidMkSuffix = recoverySuffix
798
799 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900800
801 // use cpExecutable to make it executable
802 outputFile := android.PathForModuleOut(ctx, binName)
803 ctx.Build(pctx, android.BuildParams{
804 Rule: android.CpExecutable,
805 Description: "prebuilt",
806 Output: outputFile,
807 Input: in,
808 })
809
810 return outputFile
811}
812
813func (p *snapshotBinaryDecorator) nativeCoverage() bool {
814 return false
815}
816
817// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
818// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
819// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
820func VendorSnapshotBinaryFactory() android.Module {
821 return snapshotBinaryFactory(vendorSnapshotBinarySuffix)
822}
823
824// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
825// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
826// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
827func RecoverySnapshotBinaryFactory() android.Module {
828 return snapshotBinaryFactory(recoverySnapshotBinarySuffix)
829}
830
831func snapshotBinaryFactory(suffix string) android.Module {
832 module, binary := NewBinary(android.DeviceSupported)
833 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
834 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
835
836 // Prevent default system libs (libc, libm, and libdl) from being linked
837 if binary.baseLinker.Properties.System_shared_libs == nil {
838 binary.baseLinker.Properties.System_shared_libs = []string{}
839 }
840
841 prebuilt := &snapshotBinaryDecorator{
842 binaryDecorator: binary,
843 }
844
845 module.compiler = nil
846 module.sanitize = nil
847 module.stl = nil
848 module.linker = prebuilt
849
850 prebuilt.init(module, suffix)
851 module.AddProperties(&prebuilt.properties)
852 return module.Init()
853}
854
855//
856// Module definitions for snapshots of object files (*.o).
857//
858// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
859// files (*.o) as their src.
860//
861// These modules are auto-generated by development/vendor_snapshot/update.py.
862type vendorSnapshotObjectProperties struct {
863 // Prebuilt file for each arch.
864 Src *string `android:"arch_variant"`
865}
866
867type snapshotObjectLinker struct {
868 baseSnapshotDecorator
869 objectLinker
Jose Galmes6f843bc2020-12-11 13:36:29 -0800870 properties vendorSnapshotObjectProperties
871 androidMkSuffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900872}
873
874func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
875 if config.DeviceArch() != p.arch() {
876 return false
877 }
878 if p.properties.Src == nil {
879 return false
880 }
881 return true
882}
883
884// cc modules' link functions are to link compiled objects into final binaries.
885// As snapshots are prebuilts, this just returns the prebuilt binary
886func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
887 if !p.matchesWithDevice(ctx.DeviceConfig()) {
888 return nil
889 }
890
891 m := ctx.Module().(*Module)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800892
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500893 if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800894 p.androidMkSuffix = vendorSuffix
895 } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
896 p.androidMkSuffix = recoverySuffix
897 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900898
899 return android.PathForModuleSrc(ctx, *p.properties.Src)
900}
901
902func (p *snapshotObjectLinker) nativeCoverage() bool {
903 return false
904}
905
906// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
907// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
908// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
909func VendorSnapshotObjectFactory() android.Module {
910 module := newObject()
911
912 prebuilt := &snapshotObjectLinker{
913 objectLinker: objectLinker{
914 baseLinker: NewBaseLinker(nil),
915 },
916 }
917 module.linker = prebuilt
918
919 prebuilt.init(module, vendorSnapshotObjectSuffix)
920 module.AddProperties(&prebuilt.properties)
921 return module.Init()
922}
923
924// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
925// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
926// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
927func RecoverySnapshotObjectFactory() android.Module {
928 module := newObject()
929
930 prebuilt := &snapshotObjectLinker{
931 objectLinker: objectLinker{
932 baseLinker: NewBaseLinker(nil),
933 },
934 }
935 module.linker = prebuilt
936
937 prebuilt.init(module, recoverySnapshotObjectSuffix)
938 module.AddProperties(&prebuilt.properties)
939 return module.Init()
940}
941
942type snapshotInterface interface {
943 matchesWithDevice(config android.DeviceConfig) bool
944}
945
946var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
947var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
948var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
949var _ snapshotInterface = (*snapshotObjectLinker)(nil)
950
951//
952// Mutators that helps vendor snapshot modules override source modules.
953//
954
955// VendorSnapshotMutator gathers all snapshots for vendor, and disable all snapshots which don't
956// match with device, e.g.
957// - snapshot version is different with BOARD_VNDK_VERSION
958// - snapshot arch is different with device's arch (e.g. arm vs x86)
959//
960// This also handles vndk_prebuilt_shared, except for they won't be disabled in any cases, given
961// that any versions of VNDK might be packed into vndk APEX.
962//
963// TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules
964func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800965 snapshotMutator(ctx, vendorSnapshotImageSingleton)
966}
967
968func RecoverySnapshotMutator(ctx android.BottomUpMutatorContext) {
969 snapshotMutator(ctx, recoverySnapshotImageSingleton)
970}
971
972func snapshotMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
973 if !image.isUsingSnapshot(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900974 return
975 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900976 module, ok := ctx.Module().(*Module)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800977 if !ok || !module.Enabled() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900978 return
979 }
Jose Galmes6f843bc2020-12-11 13:36:29 -0800980 if image.skipModuleMutator(ctx) {
981 return
982 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900983 if !module.isSnapshotPrebuilt() {
984 return
985 }
986
987 // isSnapshotPrebuilt ensures snapshotInterface
988 if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) {
989 // Disable unnecessary snapshot module, but do not disable
990 // vndk_prebuilt_shared because they might be packed into vndk APEX
991 if !module.IsVndk() {
992 module.Disable()
993 }
994 return
995 }
996
Jose Galmes6f843bc2020-12-11 13:36:29 -0800997 var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config())
998 if snapshotMap == nil {
Inseob Kimde5744a2020-12-02 13:14:28 +0900999 return
1000 }
1001
Jose Galmes6f843bc2020-12-11 13:36:29 -08001002 mutex := image.getMutex()
1003 mutex.Lock()
1004 defer mutex.Unlock()
Inseob Kimde5744a2020-12-02 13:14:28 +09001005 snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName())
1006}
1007
1008// VendorSnapshotSourceMutator disables source modules which have corresponding snapshots.
1009func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
Jose Galmes6f843bc2020-12-11 13:36:29 -08001010 snapshotSourceMutator(ctx, vendorSnapshotImageSingleton)
1011}
1012
1013func RecoverySnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
1014 snapshotSourceMutator(ctx, recoverySnapshotImageSingleton)
1015}
1016
1017func snapshotSourceMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
Inseob Kimde5744a2020-12-02 13:14:28 +09001018 if !ctx.Device() {
1019 return
1020 }
Jose Galmes6f843bc2020-12-11 13:36:29 -08001021 if !image.isUsingSnapshot(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +09001022 return
1023 }
1024
1025 module, ok := ctx.Module().(*Module)
1026 if !ok {
1027 return
1028 }
1029
Jose Galmes6f843bc2020-12-11 13:36:29 -08001030 if image.shouldBeAddedToSuffixModules(module) {
1031 mutex := image.getMutex()
1032 mutex.Lock()
1033 defer mutex.Unlock()
Inseob Kimde5744a2020-12-02 13:14:28 +09001034
Jose Galmes6f843bc2020-12-11 13:36:29 -08001035 image.suffixModules(ctx.Config())[ctx.ModuleName()] = true
Inseob Kimde5744a2020-12-02 13:14:28 +09001036 }
1037
Jose Galmes6f843bc2020-12-11 13:36:29 -08001038 if module.isSnapshotPrebuilt() {
1039 return
1040 }
1041 if image.skipSourceMutator(ctx) {
Inseob Kimde5744a2020-12-02 13:14:28 +09001042 return
1043 }
1044
Jose Galmes6f843bc2020-12-11 13:36:29 -08001045 var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config())
1046 if snapshotMap == nil {
Inseob Kimde5744a2020-12-02 13:14:28 +09001047 return
1048 }
1049
1050 if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok {
1051 // Corresponding snapshot doesn't exist
1052 return
1053 }
1054
1055 // Disables source modules if corresponding snapshot exists.
1056 if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() {
1057 // But do not disable because the shared variant depends on the static variant.
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001058 module.HideFromMake()
Inseob Kimde5744a2020-12-02 13:14:28 +09001059 module.Properties.HideFromMake = true
1060 } else {
1061 module.Disable()
1062 }
1063}