blob: 2003e03ff4e472d9571b2c9c65e02f3a6610a67b [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,
608
609 TableOfContents: p.tocFile,
610 })
611 }
612
613 if p.static() {
614 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
615 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
616 StaticLibrary: in,
617
618 TransitiveStaticLibrariesForOrdering: depSet,
619 })
620 }
621
622 p.libraryDecorator.flagExporter.setProvider(ctx)
623
624 return in
625}
626
627func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
628 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
629 p.baseInstaller.install(ctx, file)
630 }
631}
632
633func (p *snapshotLibraryDecorator) nativeCoverage() bool {
634 return false
635}
636
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500637func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900638 switch t {
639 case cfi:
640 return p.sanitizerProperties.Cfi.Src != nil
641 default:
642 return false
643 }
644}
645
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500646func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900647 if !enabled {
648 return
649 }
650 switch t {
651 case cfi:
652 p.sanitizerProperties.CfiEnabled = true
653 default:
654 return
655 }
656}
657
658func snapshotLibraryFactory(suffix string) (*Module, *snapshotLibraryDecorator) {
659 module, library := NewLibrary(android.DeviceSupported)
660
661 module.stl = nil
662 module.sanitize = nil
663 library.disableStripping()
664
665 prebuilt := &snapshotLibraryDecorator{
666 libraryDecorator: library,
667 }
668
669 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
670 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
671
672 // Prevent default system libs (libc, libm, and libdl) from being linked
673 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
674 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
675 }
676
677 module.compiler = nil
678 module.linker = prebuilt
679 module.installer = prebuilt
680
681 prebuilt.init(module, suffix)
682 module.AddProperties(
683 &prebuilt.properties,
684 &prebuilt.sanitizerProperties,
685 )
686
687 return module, prebuilt
688}
689
690// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
691// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
692// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
693// is set.
694func VendorSnapshotSharedFactory() android.Module {
695 module, prebuilt := snapshotLibraryFactory(vendorSnapshotSharedSuffix)
696 prebuilt.libraryDecorator.BuildOnlyShared()
697 return module.Init()
698}
699
700// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
701// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
702// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
703// is set.
704func RecoverySnapshotSharedFactory() android.Module {
705 module, prebuilt := snapshotLibraryFactory(recoverySnapshotSharedSuffix)
706 prebuilt.libraryDecorator.BuildOnlyShared()
707 return module.Init()
708}
709
710// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
711// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
712// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
713// is set.
714func VendorSnapshotStaticFactory() android.Module {
715 module, prebuilt := snapshotLibraryFactory(vendorSnapshotStaticSuffix)
716 prebuilt.libraryDecorator.BuildOnlyStatic()
717 return module.Init()
718}
719
720// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
721// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
722// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
723// is set.
724func RecoverySnapshotStaticFactory() android.Module {
725 module, prebuilt := snapshotLibraryFactory(recoverySnapshotStaticSuffix)
726 prebuilt.libraryDecorator.BuildOnlyStatic()
727 return module.Init()
728}
729
730// vendor_snapshot_header is a special header library which is auto-generated by
731// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
732// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
733// is set.
734func VendorSnapshotHeaderFactory() android.Module {
735 module, prebuilt := snapshotLibraryFactory(vendorSnapshotHeaderSuffix)
736 prebuilt.libraryDecorator.HeaderOnly()
737 return module.Init()
738}
739
740// recovery_snapshot_header is a special header library which is auto-generated by
741// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
742// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
743// is set.
744func RecoverySnapshotHeaderFactory() android.Module {
745 module, prebuilt := snapshotLibraryFactory(recoverySnapshotHeaderSuffix)
746 prebuilt.libraryDecorator.HeaderOnly()
747 return module.Init()
748}
749
750var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
751
752//
753// Module definitions for snapshots of executable binaries.
754//
755// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
756// binaries (e.g. toybox, sh) as their src, which can be installed.
757//
758// These modules are auto-generated by development/vendor_snapshot/update.py.
759type snapshotBinaryProperties struct {
760 // Prebuilt file for each arch.
761 Src *string `android:"arch_variant"`
762}
763
764type snapshotBinaryDecorator struct {
765 baseSnapshotDecorator
766 *binaryDecorator
Jose Galmes6f843bc2020-12-11 13:36:29 -0800767 properties snapshotBinaryProperties
768 androidMkSuffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900769}
770
771func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
772 if config.DeviceArch() != p.arch() {
773 return false
774 }
775 if p.properties.Src == nil {
776 return false
777 }
778 return true
779}
780
781// cc modules' link functions are to link compiled objects into final binaries.
782// As snapshots are prebuilts, this just returns the prebuilt binary
783func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
784 if !p.matchesWithDevice(ctx.DeviceConfig()) {
785 return nil
786 }
787
788 in := android.PathForModuleSrc(ctx, *p.properties.Src)
789 p.unstrippedOutputFile = in
790 binName := in.Base()
791
792 m := ctx.Module().(*Module)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500793 if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800794 p.androidMkSuffix = vendorSuffix
795 } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
796 p.androidMkSuffix = recoverySuffix
797
798 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900799
800 // use cpExecutable to make it executable
801 outputFile := android.PathForModuleOut(ctx, binName)
802 ctx.Build(pctx, android.BuildParams{
803 Rule: android.CpExecutable,
804 Description: "prebuilt",
805 Output: outputFile,
806 Input: in,
807 })
808
809 return outputFile
810}
811
812func (p *snapshotBinaryDecorator) nativeCoverage() bool {
813 return false
814}
815
816// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
817// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
818// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
819func VendorSnapshotBinaryFactory() android.Module {
820 return snapshotBinaryFactory(vendorSnapshotBinarySuffix)
821}
822
823// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
824// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
825// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
826func RecoverySnapshotBinaryFactory() android.Module {
827 return snapshotBinaryFactory(recoverySnapshotBinarySuffix)
828}
829
830func snapshotBinaryFactory(suffix string) android.Module {
831 module, binary := NewBinary(android.DeviceSupported)
832 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
833 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
834
835 // Prevent default system libs (libc, libm, and libdl) from being linked
836 if binary.baseLinker.Properties.System_shared_libs == nil {
837 binary.baseLinker.Properties.System_shared_libs = []string{}
838 }
839
840 prebuilt := &snapshotBinaryDecorator{
841 binaryDecorator: binary,
842 }
843
844 module.compiler = nil
845 module.sanitize = nil
846 module.stl = nil
847 module.linker = prebuilt
848
849 prebuilt.init(module, suffix)
850 module.AddProperties(&prebuilt.properties)
851 return module.Init()
852}
853
854//
855// Module definitions for snapshots of object files (*.o).
856//
857// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
858// files (*.o) as their src.
859//
860// These modules are auto-generated by development/vendor_snapshot/update.py.
861type vendorSnapshotObjectProperties struct {
862 // Prebuilt file for each arch.
863 Src *string `android:"arch_variant"`
864}
865
866type snapshotObjectLinker struct {
867 baseSnapshotDecorator
868 objectLinker
Jose Galmes6f843bc2020-12-11 13:36:29 -0800869 properties vendorSnapshotObjectProperties
870 androidMkSuffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900871}
872
873func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
874 if config.DeviceArch() != p.arch() {
875 return false
876 }
877 if p.properties.Src == nil {
878 return false
879 }
880 return true
881}
882
883// cc modules' link functions are to link compiled objects into final binaries.
884// As snapshots are prebuilts, this just returns the prebuilt binary
885func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
886 if !p.matchesWithDevice(ctx.DeviceConfig()) {
887 return nil
888 }
889
890 m := ctx.Module().(*Module)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800891
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500892 if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800893 p.androidMkSuffix = vendorSuffix
894 } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
895 p.androidMkSuffix = recoverySuffix
896 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900897
898 return android.PathForModuleSrc(ctx, *p.properties.Src)
899}
900
901func (p *snapshotObjectLinker) nativeCoverage() bool {
902 return false
903}
904
905// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
906// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
907// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
908func VendorSnapshotObjectFactory() android.Module {
909 module := newObject()
910
911 prebuilt := &snapshotObjectLinker{
912 objectLinker: objectLinker{
913 baseLinker: NewBaseLinker(nil),
914 },
915 }
916 module.linker = prebuilt
917
918 prebuilt.init(module, vendorSnapshotObjectSuffix)
919 module.AddProperties(&prebuilt.properties)
920 return module.Init()
921}
922
923// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
924// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
925// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
926func RecoverySnapshotObjectFactory() android.Module {
927 module := newObject()
928
929 prebuilt := &snapshotObjectLinker{
930 objectLinker: objectLinker{
931 baseLinker: NewBaseLinker(nil),
932 },
933 }
934 module.linker = prebuilt
935
936 prebuilt.init(module, recoverySnapshotObjectSuffix)
937 module.AddProperties(&prebuilt.properties)
938 return module.Init()
939}
940
941type snapshotInterface interface {
942 matchesWithDevice(config android.DeviceConfig) bool
943}
944
945var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
946var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
947var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
948var _ snapshotInterface = (*snapshotObjectLinker)(nil)
949
950//
951// Mutators that helps vendor snapshot modules override source modules.
952//
953
954// VendorSnapshotMutator gathers all snapshots for vendor, and disable all snapshots which don't
955// match with device, e.g.
956// - snapshot version is different with BOARD_VNDK_VERSION
957// - snapshot arch is different with device's arch (e.g. arm vs x86)
958//
959// This also handles vndk_prebuilt_shared, except for they won't be disabled in any cases, given
960// that any versions of VNDK might be packed into vndk APEX.
961//
962// TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules
963func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800964 snapshotMutator(ctx, vendorSnapshotImageSingleton)
965}
966
967func RecoverySnapshotMutator(ctx android.BottomUpMutatorContext) {
968 snapshotMutator(ctx, recoverySnapshotImageSingleton)
969}
970
971func snapshotMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
972 if !image.isUsingSnapshot(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900973 return
974 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900975 module, ok := ctx.Module().(*Module)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800976 if !ok || !module.Enabled() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900977 return
978 }
Jose Galmes6f843bc2020-12-11 13:36:29 -0800979 if image.skipModuleMutator(ctx) {
980 return
981 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900982 if !module.isSnapshotPrebuilt() {
983 return
984 }
985
986 // isSnapshotPrebuilt ensures snapshotInterface
987 if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) {
988 // Disable unnecessary snapshot module, but do not disable
989 // vndk_prebuilt_shared because they might be packed into vndk APEX
990 if !module.IsVndk() {
991 module.Disable()
992 }
993 return
994 }
995
Jose Galmes6f843bc2020-12-11 13:36:29 -0800996 var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config())
997 if snapshotMap == nil {
Inseob Kimde5744a2020-12-02 13:14:28 +0900998 return
999 }
1000
Jose Galmes6f843bc2020-12-11 13:36:29 -08001001 mutex := image.getMutex()
1002 mutex.Lock()
1003 defer mutex.Unlock()
Inseob Kimde5744a2020-12-02 13:14:28 +09001004 snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName())
1005}
1006
1007// VendorSnapshotSourceMutator disables source modules which have corresponding snapshots.
1008func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
Jose Galmes6f843bc2020-12-11 13:36:29 -08001009 snapshotSourceMutator(ctx, vendorSnapshotImageSingleton)
1010}
1011
1012func RecoverySnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
1013 snapshotSourceMutator(ctx, recoverySnapshotImageSingleton)
1014}
1015
1016func snapshotSourceMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
Inseob Kimde5744a2020-12-02 13:14:28 +09001017 if !ctx.Device() {
1018 return
1019 }
Jose Galmes6f843bc2020-12-11 13:36:29 -08001020 if !image.isUsingSnapshot(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +09001021 return
1022 }
1023
1024 module, ok := ctx.Module().(*Module)
1025 if !ok {
1026 return
1027 }
1028
Jose Galmes6f843bc2020-12-11 13:36:29 -08001029 if image.shouldBeAddedToSuffixModules(module) {
1030 mutex := image.getMutex()
1031 mutex.Lock()
1032 defer mutex.Unlock()
Inseob Kimde5744a2020-12-02 13:14:28 +09001033
Jose Galmes6f843bc2020-12-11 13:36:29 -08001034 image.suffixModules(ctx.Config())[ctx.ModuleName()] = true
Inseob Kimde5744a2020-12-02 13:14:28 +09001035 }
1036
Jose Galmes6f843bc2020-12-11 13:36:29 -08001037 if module.isSnapshotPrebuilt() {
1038 return
1039 }
1040 if image.skipSourceMutator(ctx) {
Inseob Kimde5744a2020-12-02 13:14:28 +09001041 return
1042 }
1043
Jose Galmes6f843bc2020-12-11 13:36:29 -08001044 var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config())
1045 if snapshotMap == nil {
Inseob Kimde5744a2020-12-02 13:14:28 +09001046 return
1047 }
1048
1049 if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok {
1050 // Corresponding snapshot doesn't exist
1051 return
1052 }
1053
1054 // Disables source modules if corresponding snapshot exists.
1055 if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() {
1056 // But do not disable because the shared variant depends on the static variant.
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001057 module.HideFromMake()
Inseob Kimde5744a2020-12-02 13:14:28 +09001058 module.Properties.HideFromMake = true
1059 } else {
1060 module.Disable()
1061 }
1062}