blob: 89798468c22ad476258526d5cc2f9b598e0d8377 [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
43 // Returns the value of the "available" property for a given module for
44 // and snapshot, e.g., "vendor_available", "recovery_available", etc.
45 // or nil if the property is not defined.
46 available(m *Module) *bool
47
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 Kimde5744a2020-12-02 13:14:28 +090085}
86
87type vendorSnapshotImage struct{}
88type recoverySnapshotImage struct{}
89
90func (vendorSnapshotImage) init() {
91 android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
92 android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
93 android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
94 android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
95 android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
96 android.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
Inseob Kime9aec6a2021-01-05 20:03:22 +090097
98 android.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
Inseob Kimde5744a2020-12-02 13:14:28 +090099}
100
Jose Galmes6f843bc2020-12-11 13:36:29 -0800101func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
102 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot.
103 return ctx.DeviceConfig().VndkVersion() == "current"
104}
105
Inseob Kimde5744a2020-12-02 13:14:28 +0900106func (vendorSnapshotImage) inImage(m *Module) func() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500107 return m.InVendor
Inseob Kimde5744a2020-12-02 13:14:28 +0900108}
109
110func (vendorSnapshotImage) available(m *Module) *bool {
111 return m.VendorProperties.Vendor_available
112}
113
114func (vendorSnapshotImage) isProprietaryPath(dir string) bool {
115 return isVendorProprietaryPath(dir)
116}
117
118// vendor snapshot includes static/header libraries with vndk: {enabled: true}.
119func (vendorSnapshotImage) includeVndk() bool {
120 return true
121}
122
123func (vendorSnapshotImage) excludeFromSnapshot(m *Module) bool {
124 return m.ExcludeFromVendorSnapshot()
125}
126
Jose Galmes6f843bc2020-12-11 13:36:29 -0800127func (vendorSnapshotImage) getSnapshotMap(m *Module, cfg android.Config) *snapshotMap {
128 if lib, ok := m.linker.(libraryInterface); ok {
129 if lib.static() {
130 return vendorSnapshotStaticLibs(cfg)
131 } else if lib.shared() {
132 return vendorSnapshotSharedLibs(cfg)
133 } else {
134 // header
135 return vendorSnapshotHeaderLibs(cfg)
136 }
137 } else if m.binary() {
138 return vendorSnapshotBinaries(cfg)
139 } else if m.object() {
140 return vendorSnapshotObjects(cfg)
141 } else {
142 return nil
143 }
144}
145
146func (vendorSnapshotImage) getMutex() *sync.Mutex {
147 return &vendorSnapshotsLock
148}
149
150func (vendorSnapshotImage) suffixModules(config android.Config) map[string]bool {
151 return vendorSuffixModules(config)
152}
153
154func (vendorSnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool {
155 // vendor suffix should be added to snapshots if the source module isn't vendor: true.
156 if module.SocSpecific() {
157 return false
158 }
159
160 // But we can't just check SocSpecific() since we already passed the image mutator.
161 // Check ramdisk and recovery to see if we are real "vendor: true" module.
162 ramdiskAvailable := module.InRamdisk() && !module.OnlyInRamdisk()
163 vendorRamdiskAvailable := module.InVendorRamdisk() && !module.OnlyInVendorRamdisk()
164 recoveryAvailable := module.InRecovery() && !module.OnlyInRecovery()
165
166 return !ramdiskAvailable && !recoveryAvailable && !vendorRamdiskAvailable
167}
168
169func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
170 vndkVersion := cfg.VndkVersion()
171 return vndkVersion != "current" && vndkVersion != ""
172}
173
174func (vendorSnapshotImage) skipModuleMutator(ctx android.BottomUpMutatorContext) bool {
175 vndkVersion := ctx.DeviceConfig().VndkVersion()
176 module, ok := ctx.Module().(*Module)
177 return !ok || module.VndkVersion() != vndkVersion
178}
179
180func (vendorSnapshotImage) skipSourceMutator(ctx android.BottomUpMutatorContext) bool {
181 vndkVersion := ctx.DeviceConfig().VndkVersion()
182 module, ok := ctx.Module().(*Module)
183 if !ok {
184 return true
185 }
186 if module.VndkVersion() != vndkVersion {
187 return true
188 }
189 // .. and also filter out llndk library
190 if module.IsLlndk() {
191 return true
192 }
193 return false
194}
195
Inseob Kimde5744a2020-12-02 13:14:28 +0900196func (recoverySnapshotImage) init() {
197 android.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
198 android.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
199 android.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
200 android.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
201 android.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
202 android.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
203}
204
Jose Galmes6f843bc2020-12-11 13:36:29 -0800205func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
206 // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a
207 // snapshot.
208 return ctx.DeviceConfig().RecoverySnapshotVersion() == "current"
209}
210
Inseob Kimde5744a2020-12-02 13:14:28 +0900211func (recoverySnapshotImage) inImage(m *Module) func() bool {
212 return m.InRecovery
213}
214
215func (recoverySnapshotImage) available(m *Module) *bool {
216 return m.Properties.Recovery_available
217}
218
219func (recoverySnapshotImage) isProprietaryPath(dir string) bool {
220 return isRecoveryProprietaryPath(dir)
221}
222
223// recovery snapshot does NOT treat vndk specially.
224func (recoverySnapshotImage) includeVndk() bool {
225 return false
226}
227
228func (recoverySnapshotImage) excludeFromSnapshot(m *Module) bool {
229 return m.ExcludeFromRecoverySnapshot()
230}
231
Jose Galmes6f843bc2020-12-11 13:36:29 -0800232func (recoverySnapshotImage) getSnapshotMap(m *Module, cfg android.Config) *snapshotMap {
233 if lib, ok := m.linker.(libraryInterface); ok {
234 if lib.static() {
235 return recoverySnapshotStaticLibs(cfg)
236 } else if lib.shared() {
237 return recoverySnapshotSharedLibs(cfg)
238 } else {
239 // header
240 return recoverySnapshotHeaderLibs(cfg)
241 }
242 } else if m.binary() {
243 return recoverySnapshotBinaries(cfg)
244 } else if m.object() {
245 return recoverySnapshotObjects(cfg)
246 } else {
247 return nil
248 }
249}
250
251func (recoverySnapshotImage) getMutex() *sync.Mutex {
252 return &recoverySnapshotsLock
253}
254
255func (recoverySnapshotImage) suffixModules(config android.Config) map[string]bool {
256 return recoverySuffixModules(config)
257}
258
259func (recoverySnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool {
260 return proptools.BoolDefault(module.Properties.Recovery_available, false)
261}
262
263func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
264 recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
265 return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
266}
267
268func (recoverySnapshotImage) skipModuleMutator(ctx android.BottomUpMutatorContext) bool {
269 module, ok := ctx.Module().(*Module)
270 return !ok || !module.InRecovery()
271}
272
273func (recoverySnapshotImage) skipSourceMutator(ctx android.BottomUpMutatorContext) bool {
274 module, ok := ctx.Module().(*Module)
275 return !ok || !module.InRecovery()
276}
277
Inseob Kimde5744a2020-12-02 13:14:28 +0900278var vendorSnapshotImageSingleton vendorSnapshotImage
279var recoverySnapshotImageSingleton recoverySnapshotImage
280
281func init() {
282 vendorSnapshotImageSingleton.init()
283 recoverySnapshotImageSingleton.init()
284}
285
286const (
287 vendorSnapshotHeaderSuffix = ".vendor_header."
288 vendorSnapshotSharedSuffix = ".vendor_shared."
289 vendorSnapshotStaticSuffix = ".vendor_static."
290 vendorSnapshotBinarySuffix = ".vendor_binary."
291 vendorSnapshotObjectSuffix = ".vendor_object."
292)
293
294const (
295 recoverySnapshotHeaderSuffix = ".recovery_header."
296 recoverySnapshotSharedSuffix = ".recovery_shared."
297 recoverySnapshotStaticSuffix = ".recovery_static."
298 recoverySnapshotBinarySuffix = ".recovery_binary."
299 recoverySnapshotObjectSuffix = ".recovery_object."
300)
301
302var (
303 vendorSnapshotsLock sync.Mutex
304 vendorSuffixModulesKey = android.NewOnceKey("vendorSuffixModules")
305 vendorSnapshotHeaderLibsKey = android.NewOnceKey("vendorSnapshotHeaderLibs")
306 vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs")
307 vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs")
308 vendorSnapshotBinariesKey = android.NewOnceKey("vendorSnapshotBinaries")
309 vendorSnapshotObjectsKey = android.NewOnceKey("vendorSnapshotObjects")
310)
311
Jose Galmes6f843bc2020-12-11 13:36:29 -0800312var (
313 recoverySnapshotsLock sync.Mutex
314 recoverySuffixModulesKey = android.NewOnceKey("recoverySuffixModules")
315 recoverySnapshotHeaderLibsKey = android.NewOnceKey("recoverySnapshotHeaderLibs")
316 recoverySnapshotStaticLibsKey = android.NewOnceKey("recoverySnapshotStaticLibs")
317 recoverySnapshotSharedLibsKey = android.NewOnceKey("recoverySnapshotSharedLibs")
318 recoverySnapshotBinariesKey = android.NewOnceKey("recoverySnapshotBinaries")
319 recoverySnapshotObjectsKey = android.NewOnceKey("recoverySnapshotObjects")
320)
321
Inseob Kimde5744a2020-12-02 13:14:28 +0900322// vendorSuffixModules holds names of modules whose vendor variants should have the vendor suffix.
323// This is determined by source modules, and then this will be used when exporting snapshot modules
324// to Makefile.
325//
326// For example, if libbase has "vendor_available: true", the name of core variant will be "libbase"
327// while the name of vendor variant will be "libbase.vendor". In such cases, the vendor snapshot of
328// "libbase" should be exported with the name "libbase.vendor".
329//
330// Refer to VendorSnapshotSourceMutator and makeLibName which use this.
331func vendorSuffixModules(config android.Config) map[string]bool {
332 return config.Once(vendorSuffixModulesKey, func() interface{} {
333 return make(map[string]bool)
334 }).(map[string]bool)
335}
336
337// these are vendor snapshot maps holding names of vendor snapshot modules
338func vendorSnapshotHeaderLibs(config android.Config) *snapshotMap {
339 return config.Once(vendorSnapshotHeaderLibsKey, func() interface{} {
340 return newSnapshotMap()
341 }).(*snapshotMap)
342}
343
344func vendorSnapshotSharedLibs(config android.Config) *snapshotMap {
345 return config.Once(vendorSnapshotSharedLibsKey, func() interface{} {
346 return newSnapshotMap()
347 }).(*snapshotMap)
348}
349
350func vendorSnapshotStaticLibs(config android.Config) *snapshotMap {
351 return config.Once(vendorSnapshotStaticLibsKey, func() interface{} {
352 return newSnapshotMap()
353 }).(*snapshotMap)
354}
355
356func vendorSnapshotBinaries(config android.Config) *snapshotMap {
357 return config.Once(vendorSnapshotBinariesKey, func() interface{} {
358 return newSnapshotMap()
359 }).(*snapshotMap)
360}
361
362func vendorSnapshotObjects(config android.Config) *snapshotMap {
363 return config.Once(vendorSnapshotObjectsKey, func() interface{} {
364 return newSnapshotMap()
365 }).(*snapshotMap)
366}
367
Jose Galmes6f843bc2020-12-11 13:36:29 -0800368func recoverySuffixModules(config android.Config) map[string]bool {
369 return config.Once(recoverySuffixModulesKey, func() interface{} {
370 return make(map[string]bool)
371 }).(map[string]bool)
372}
373
374func recoverySnapshotHeaderLibs(config android.Config) *snapshotMap {
375 return config.Once(recoverySnapshotHeaderLibsKey, func() interface{} {
376 return newSnapshotMap()
377 }).(*snapshotMap)
378}
379
380func recoverySnapshotSharedLibs(config android.Config) *snapshotMap {
381 return config.Once(recoverySnapshotSharedLibsKey, func() interface{} {
382 return newSnapshotMap()
383 }).(*snapshotMap)
384}
385
386func recoverySnapshotStaticLibs(config android.Config) *snapshotMap {
387 return config.Once(recoverySnapshotStaticLibsKey, func() interface{} {
388 return newSnapshotMap()
389 }).(*snapshotMap)
390}
391
392func recoverySnapshotBinaries(config android.Config) *snapshotMap {
393 return config.Once(recoverySnapshotBinariesKey, func() interface{} {
394 return newSnapshotMap()
395 }).(*snapshotMap)
396}
397
398func recoverySnapshotObjects(config android.Config) *snapshotMap {
399 return config.Once(recoverySnapshotObjectsKey, func() interface{} {
400 return newSnapshotMap()
401 }).(*snapshotMap)
402}
403
Inseob Kimde5744a2020-12-02 13:14:28 +0900404type baseSnapshotDecoratorProperties struct {
405 // snapshot version.
406 Version string
407
408 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
409 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800410
411 // Suffix to be added to the module name, e.g., vendor_shared,
412 // recovery_shared, etc.
413 Module_suffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900414}
415
416// baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
417// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
418// collide with source modules. e.g. the following example module,
419//
420// vendor_snapshot_static {
421// name: "libbase",
422// arch: "arm64",
423// version: 30,
424// ...
425// }
426//
427// will be seen as "libbase.vendor_static.30.arm64" by Soong.
428type baseSnapshotDecorator struct {
429 baseProperties baseSnapshotDecoratorProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900430}
431
432func (p *baseSnapshotDecorator) Name(name string) string {
433 return name + p.NameSuffix()
434}
435
436func (p *baseSnapshotDecorator) NameSuffix() string {
437 versionSuffix := p.version()
438 if p.arch() != "" {
439 versionSuffix += "." + p.arch()
440 }
441
Jose Galmes6f843bc2020-12-11 13:36:29 -0800442 return p.baseProperties.Module_suffix + versionSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900443}
444
445func (p *baseSnapshotDecorator) version() string {
446 return p.baseProperties.Version
447}
448
449func (p *baseSnapshotDecorator) arch() string {
450 return p.baseProperties.Target_arch
451}
452
Jose Galmes6f843bc2020-12-11 13:36:29 -0800453func (p *baseSnapshotDecorator) module_suffix() string {
454 return p.baseProperties.Module_suffix
455}
456
Inseob Kimde5744a2020-12-02 13:14:28 +0900457func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
458 return true
459}
460
461// Call this with a module suffix after creating a snapshot module, such as
462// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
463func (p *baseSnapshotDecorator) init(m *Module, suffix string) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800464 p.baseProperties.Module_suffix = suffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900465 m.AddProperties(&p.baseProperties)
466 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
467 vendorSnapshotLoadHook(ctx, p)
468 })
469}
470
471// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
472// As vendor snapshot is only for vendor, such modules won't be used at all.
473func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) {
474 if p.version() != ctx.DeviceConfig().VndkVersion() {
475 ctx.Module().Disable()
476 return
477 }
478}
479
480//
481// Module definitions for snapshots of libraries (shared, static, header).
482//
483// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
484// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
485// which can be installed or linked against. Also they export flags needed when linked, such as
486// include directories, c flags, sanitize dependency information, etc.
487//
488// These modules are auto-generated by development/vendor_snapshot/update.py.
489type snapshotLibraryProperties struct {
490 // Prebuilt file for each arch.
491 Src *string `android:"arch_variant"`
492
493 // list of directories that will be added to the include path (using -I).
494 Export_include_dirs []string `android:"arch_variant"`
495
496 // list of directories that will be added to the system path (using -isystem).
497 Export_system_include_dirs []string `android:"arch_variant"`
498
499 // list of flags that will be used for any module that links against this module.
500 Export_flags []string `android:"arch_variant"`
501
502 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
503 Sanitize_ubsan_dep *bool `android:"arch_variant"`
504
505 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
506 Sanitize_minimal_dep *bool `android:"arch_variant"`
507}
508
509type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500510 isSanitizerEnabled(t SanitizerType) bool
511 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900512}
513
514type snapshotLibraryDecorator struct {
515 baseSnapshotDecorator
516 *libraryDecorator
517 properties snapshotLibraryProperties
518 sanitizerProperties struct {
519 CfiEnabled bool `blueprint:"mutated"`
520
521 // Library flags for cfi variant.
522 Cfi snapshotLibraryProperties `android:"arch_variant"`
523 }
Jose Galmes6f843bc2020-12-11 13:36:29 -0800524 androidMkSuffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900525}
526
527func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
528 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
529 return p.libraryDecorator.linkerFlags(ctx, flags)
530}
531
532func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
533 arches := config.Arches()
534 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
535 return false
536 }
537 if !p.header() && p.properties.Src == nil {
538 return false
539 }
540 return true
541}
542
543// cc modules' link functions are to link compiled objects into final binaries.
544// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
545// done by normal library decorator, e.g. exporting flags.
546func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
547 m := ctx.Module().(*Module)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800548
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500549 if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800550 p.androidMkSuffix = vendorSuffix
551 } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
552 p.androidMkSuffix = recoverySuffix
553 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900554
555 if p.header() {
556 return p.libraryDecorator.link(ctx, flags, deps, objs)
557 }
558
559 if p.sanitizerProperties.CfiEnabled {
560 p.properties = p.sanitizerProperties.Cfi
561 }
562
563 if !p.matchesWithDevice(ctx.DeviceConfig()) {
564 return nil
565 }
566
567 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
568 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
569 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
570
571 in := android.PathForModuleSrc(ctx, *p.properties.Src)
572 p.unstrippedOutputFile = in
573
574 if p.shared() {
575 libName := in.Base()
576 builderFlags := flagsToBuilderFlags(flags)
577
578 // Optimize out relinking against shared libraries whose interface hasn't changed by
579 // depending on a table of contents file instead of the library itself.
580 tocFile := android.PathForModuleOut(ctx, libName+".toc")
581 p.tocFile = android.OptionalPathForPath(tocFile)
582 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
583
584 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
585 SharedLibrary: in,
586 UnstrippedSharedLibrary: p.unstrippedOutputFile,
587
588 TableOfContents: p.tocFile,
589 })
590 }
591
592 if p.static() {
593 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
594 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
595 StaticLibrary: in,
596
597 TransitiveStaticLibrariesForOrdering: depSet,
598 })
599 }
600
601 p.libraryDecorator.flagExporter.setProvider(ctx)
602
603 return in
604}
605
606func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
607 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
608 p.baseInstaller.install(ctx, file)
609 }
610}
611
612func (p *snapshotLibraryDecorator) nativeCoverage() bool {
613 return false
614}
615
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500616func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900617 switch t {
618 case cfi:
619 return p.sanitizerProperties.Cfi.Src != nil
620 default:
621 return false
622 }
623}
624
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500625func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900626 if !enabled {
627 return
628 }
629 switch t {
630 case cfi:
631 p.sanitizerProperties.CfiEnabled = true
632 default:
633 return
634 }
635}
636
637func snapshotLibraryFactory(suffix string) (*Module, *snapshotLibraryDecorator) {
638 module, library := NewLibrary(android.DeviceSupported)
639
640 module.stl = nil
641 module.sanitize = nil
642 library.disableStripping()
643
644 prebuilt := &snapshotLibraryDecorator{
645 libraryDecorator: library,
646 }
647
648 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
649 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
650
651 // Prevent default system libs (libc, libm, and libdl) from being linked
652 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
653 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
654 }
655
656 module.compiler = nil
657 module.linker = prebuilt
658 module.installer = prebuilt
659
660 prebuilt.init(module, suffix)
661 module.AddProperties(
662 &prebuilt.properties,
663 &prebuilt.sanitizerProperties,
664 )
665
666 return module, prebuilt
667}
668
669// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
670// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
671// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
672// is set.
673func VendorSnapshotSharedFactory() android.Module {
674 module, prebuilt := snapshotLibraryFactory(vendorSnapshotSharedSuffix)
675 prebuilt.libraryDecorator.BuildOnlyShared()
676 return module.Init()
677}
678
679// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
680// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
681// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
682// is set.
683func RecoverySnapshotSharedFactory() android.Module {
684 module, prebuilt := snapshotLibraryFactory(recoverySnapshotSharedSuffix)
685 prebuilt.libraryDecorator.BuildOnlyShared()
686 return module.Init()
687}
688
689// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
690// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
691// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
692// is set.
693func VendorSnapshotStaticFactory() android.Module {
694 module, prebuilt := snapshotLibraryFactory(vendorSnapshotStaticSuffix)
695 prebuilt.libraryDecorator.BuildOnlyStatic()
696 return module.Init()
697}
698
699// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
700// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
701// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
702// is set.
703func RecoverySnapshotStaticFactory() android.Module {
704 module, prebuilt := snapshotLibraryFactory(recoverySnapshotStaticSuffix)
705 prebuilt.libraryDecorator.BuildOnlyStatic()
706 return module.Init()
707}
708
709// vendor_snapshot_header is a special header library which is auto-generated by
710// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
711// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
712// is set.
713func VendorSnapshotHeaderFactory() android.Module {
714 module, prebuilt := snapshotLibraryFactory(vendorSnapshotHeaderSuffix)
715 prebuilt.libraryDecorator.HeaderOnly()
716 return module.Init()
717}
718
719// recovery_snapshot_header is a special header library which is auto-generated by
720// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
721// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
722// is set.
723func RecoverySnapshotHeaderFactory() android.Module {
724 module, prebuilt := snapshotLibraryFactory(recoverySnapshotHeaderSuffix)
725 prebuilt.libraryDecorator.HeaderOnly()
726 return module.Init()
727}
728
729var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
730
731//
732// Module definitions for snapshots of executable binaries.
733//
734// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
735// binaries (e.g. toybox, sh) as their src, which can be installed.
736//
737// These modules are auto-generated by development/vendor_snapshot/update.py.
738type snapshotBinaryProperties struct {
739 // Prebuilt file for each arch.
740 Src *string `android:"arch_variant"`
741}
742
743type snapshotBinaryDecorator struct {
744 baseSnapshotDecorator
745 *binaryDecorator
Jose Galmes6f843bc2020-12-11 13:36:29 -0800746 properties snapshotBinaryProperties
747 androidMkSuffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900748}
749
750func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
751 if config.DeviceArch() != p.arch() {
752 return false
753 }
754 if p.properties.Src == nil {
755 return false
756 }
757 return true
758}
759
760// cc modules' link functions are to link compiled objects into final binaries.
761// As snapshots are prebuilts, this just returns the prebuilt binary
762func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
763 if !p.matchesWithDevice(ctx.DeviceConfig()) {
764 return nil
765 }
766
767 in := android.PathForModuleSrc(ctx, *p.properties.Src)
768 p.unstrippedOutputFile = in
769 binName := in.Base()
770
771 m := ctx.Module().(*Module)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500772 if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800773 p.androidMkSuffix = vendorSuffix
774 } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
775 p.androidMkSuffix = recoverySuffix
776
777 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900778
779 // use cpExecutable to make it executable
780 outputFile := android.PathForModuleOut(ctx, binName)
781 ctx.Build(pctx, android.BuildParams{
782 Rule: android.CpExecutable,
783 Description: "prebuilt",
784 Output: outputFile,
785 Input: in,
786 })
787
788 return outputFile
789}
790
791func (p *snapshotBinaryDecorator) nativeCoverage() bool {
792 return false
793}
794
795// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
796// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
797// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
798func VendorSnapshotBinaryFactory() android.Module {
799 return snapshotBinaryFactory(vendorSnapshotBinarySuffix)
800}
801
802// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
803// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
804// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
805func RecoverySnapshotBinaryFactory() android.Module {
806 return snapshotBinaryFactory(recoverySnapshotBinarySuffix)
807}
808
809func snapshotBinaryFactory(suffix string) android.Module {
810 module, binary := NewBinary(android.DeviceSupported)
811 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
812 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
813
814 // Prevent default system libs (libc, libm, and libdl) from being linked
815 if binary.baseLinker.Properties.System_shared_libs == nil {
816 binary.baseLinker.Properties.System_shared_libs = []string{}
817 }
818
819 prebuilt := &snapshotBinaryDecorator{
820 binaryDecorator: binary,
821 }
822
823 module.compiler = nil
824 module.sanitize = nil
825 module.stl = nil
826 module.linker = prebuilt
827
828 prebuilt.init(module, suffix)
829 module.AddProperties(&prebuilt.properties)
830 return module.Init()
831}
832
833//
834// Module definitions for snapshots of object files (*.o).
835//
836// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
837// files (*.o) as their src.
838//
839// These modules are auto-generated by development/vendor_snapshot/update.py.
840type vendorSnapshotObjectProperties struct {
841 // Prebuilt file for each arch.
842 Src *string `android:"arch_variant"`
843}
844
845type snapshotObjectLinker struct {
846 baseSnapshotDecorator
847 objectLinker
Jose Galmes6f843bc2020-12-11 13:36:29 -0800848 properties vendorSnapshotObjectProperties
849 androidMkSuffix string
Inseob Kimde5744a2020-12-02 13:14:28 +0900850}
851
852func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
853 if config.DeviceArch() != p.arch() {
854 return false
855 }
856 if p.properties.Src == nil {
857 return false
858 }
859 return true
860}
861
862// cc modules' link functions are to link compiled objects into final binaries.
863// As snapshots are prebuilts, this just returns the prebuilt binary
864func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
865 if !p.matchesWithDevice(ctx.DeviceConfig()) {
866 return nil
867 }
868
869 m := ctx.Module().(*Module)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800870
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500871 if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800872 p.androidMkSuffix = vendorSuffix
873 } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
874 p.androidMkSuffix = recoverySuffix
875 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900876
877 return android.PathForModuleSrc(ctx, *p.properties.Src)
878}
879
880func (p *snapshotObjectLinker) nativeCoverage() bool {
881 return false
882}
883
884// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
885// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
886// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
887func VendorSnapshotObjectFactory() android.Module {
888 module := newObject()
889
890 prebuilt := &snapshotObjectLinker{
891 objectLinker: objectLinker{
892 baseLinker: NewBaseLinker(nil),
893 },
894 }
895 module.linker = prebuilt
896
897 prebuilt.init(module, vendorSnapshotObjectSuffix)
898 module.AddProperties(&prebuilt.properties)
899 return module.Init()
900}
901
902// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
903// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
904// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
905func RecoverySnapshotObjectFactory() android.Module {
906 module := newObject()
907
908 prebuilt := &snapshotObjectLinker{
909 objectLinker: objectLinker{
910 baseLinker: NewBaseLinker(nil),
911 },
912 }
913 module.linker = prebuilt
914
915 prebuilt.init(module, recoverySnapshotObjectSuffix)
916 module.AddProperties(&prebuilt.properties)
917 return module.Init()
918}
919
920type snapshotInterface interface {
921 matchesWithDevice(config android.DeviceConfig) bool
922}
923
924var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
925var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
926var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
927var _ snapshotInterface = (*snapshotObjectLinker)(nil)
928
929//
930// Mutators that helps vendor snapshot modules override source modules.
931//
932
933// VendorSnapshotMutator gathers all snapshots for vendor, and disable all snapshots which don't
934// match with device, e.g.
935// - snapshot version is different with BOARD_VNDK_VERSION
936// - snapshot arch is different with device's arch (e.g. arm vs x86)
937//
938// This also handles vndk_prebuilt_shared, except for they won't be disabled in any cases, given
939// that any versions of VNDK might be packed into vndk APEX.
940//
941// TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules
942func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800943 snapshotMutator(ctx, vendorSnapshotImageSingleton)
944}
945
946func RecoverySnapshotMutator(ctx android.BottomUpMutatorContext) {
947 snapshotMutator(ctx, recoverySnapshotImageSingleton)
948}
949
950func snapshotMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
951 if !image.isUsingSnapshot(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900952 return
953 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900954 module, ok := ctx.Module().(*Module)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800955 if !ok || !module.Enabled() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900956 return
957 }
Jose Galmes6f843bc2020-12-11 13:36:29 -0800958 if image.skipModuleMutator(ctx) {
959 return
960 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900961 if !module.isSnapshotPrebuilt() {
962 return
963 }
964
965 // isSnapshotPrebuilt ensures snapshotInterface
966 if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) {
967 // Disable unnecessary snapshot module, but do not disable
968 // vndk_prebuilt_shared because they might be packed into vndk APEX
969 if !module.IsVndk() {
970 module.Disable()
971 }
972 return
973 }
974
Jose Galmes6f843bc2020-12-11 13:36:29 -0800975 var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config())
976 if snapshotMap == nil {
Inseob Kimde5744a2020-12-02 13:14:28 +0900977 return
978 }
979
Jose Galmes6f843bc2020-12-11 13:36:29 -0800980 mutex := image.getMutex()
981 mutex.Lock()
982 defer mutex.Unlock()
Inseob Kimde5744a2020-12-02 13:14:28 +0900983 snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName())
984}
985
986// VendorSnapshotSourceMutator disables source modules which have corresponding snapshots.
987func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800988 snapshotSourceMutator(ctx, vendorSnapshotImageSingleton)
989}
990
991func RecoverySnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
992 snapshotSourceMutator(ctx, recoverySnapshotImageSingleton)
993}
994
995func snapshotSourceMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900996 if !ctx.Device() {
997 return
998 }
Jose Galmes6f843bc2020-12-11 13:36:29 -0800999 if !image.isUsingSnapshot(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +09001000 return
1001 }
1002
1003 module, ok := ctx.Module().(*Module)
1004 if !ok {
1005 return
1006 }
1007
Jose Galmes6f843bc2020-12-11 13:36:29 -08001008 if image.shouldBeAddedToSuffixModules(module) {
1009 mutex := image.getMutex()
1010 mutex.Lock()
1011 defer mutex.Unlock()
Inseob Kimde5744a2020-12-02 13:14:28 +09001012
Jose Galmes6f843bc2020-12-11 13:36:29 -08001013 image.suffixModules(ctx.Config())[ctx.ModuleName()] = true
Inseob Kimde5744a2020-12-02 13:14:28 +09001014 }
1015
Jose Galmes6f843bc2020-12-11 13:36:29 -08001016 if module.isSnapshotPrebuilt() {
1017 return
1018 }
1019 if image.skipSourceMutator(ctx) {
Inseob Kimde5744a2020-12-02 13:14:28 +09001020 return
1021 }
1022
Jose Galmes6f843bc2020-12-11 13:36:29 -08001023 var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config())
1024 if snapshotMap == nil {
Inseob Kimde5744a2020-12-02 13:14:28 +09001025 return
1026 }
1027
1028 if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok {
1029 // Corresponding snapshot doesn't exist
1030 return
1031 }
1032
1033 // Disables source modules if corresponding snapshot exists.
1034 if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() {
1035 // But do not disable because the shared variant depends on the static variant.
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001036 module.HideFromMake()
Inseob Kimde5744a2020-12-02 13:14:28 +09001037 module.Properties.HideFromMake = true
1038 } else {
1039 module.Disable()
1040 }
1041}