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