blob: f9aea0cfeb49ad2071b54e4d8c8d4be6626eff77 [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"
Inseob Kimde5744a2020-12-02 13:14:28 +090022
23 "android/soong/android"
Jose Galmes6f843bc2020-12-11 13:36:29 -080024
Colin Crosse0edaf92021-01-11 17:31:17 -080025 "github.com/google/blueprint"
Inseob Kimde5744a2020-12-02 13:14:28 +090026)
27
28// Defines the specifics of different images to which the snapshot process is applicable, e.g.,
29// vendor, recovery, ramdisk.
30type snapshotImage interface {
Jose Galmes6f843bc2020-12-11 13:36:29 -080031 // Returns true if a snapshot should be generated for this image.
32 shouldGenerateSnapshot(ctx android.SingletonContext) bool
33
Inseob Kimde5744a2020-12-02 13:14:28 +090034 // Function that returns true if the module is included in this image.
35 // Using a function return instead of a value to prevent early
36 // evalution of a function that may be not be defined.
37 inImage(m *Module) func() bool
38
Justin Yune09ac172021-01-20 19:49:01 +090039 // Returns true if the module is private and must not be included in the
40 // snapshot. For example VNDK-private modules must return true for the
41 // vendor snapshots. But false for the recovery snapshots.
42 private(m *Module) bool
Inseob Kimde5744a2020-12-02 13:14:28 +090043
44 // Returns true if a dir under source tree is an SoC-owned proprietary
45 // directory, such as device/, vendor/, etc.
46 //
47 // For a given snapshot (e.g., vendor, recovery, etc.) if
48 // isProprietaryPath(dir) returns true, then the module in dir will be
49 // built from sources.
50 isProprietaryPath(dir string) bool
51
52 // Whether to include VNDK in the snapshot for this image.
53 includeVndk() bool
54
55 // Whether a given module has been explicitly excluded from the
56 // snapshot, e.g., using the exclude_from_vendor_snapshot or
57 // exclude_from_recovery_snapshot properties.
58 excludeFromSnapshot(m *Module) bool
Jose Galmes6f843bc2020-12-11 13:36:29 -080059
Jose Galmes6f843bc2020-12-11 13:36:29 -080060 // Returns true if the build is using a snapshot for this image.
61 isUsingSnapshot(cfg android.DeviceConfig) bool
62
Colin Crosse0edaf92021-01-11 17:31:17 -080063 // Returns a version of which the snapshot should be used in this target.
64 // This will only be meaningful when isUsingSnapshot is true.
65 targetSnapshotVersion(cfg android.DeviceConfig) string
Inseob Kim7cf14652021-01-06 23:06:52 +090066
67 // Whether to exclude a given module from the directed snapshot or not.
68 // If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on,
69 // and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured.
70 excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool
Colin Crosse0edaf92021-01-11 17:31:17 -080071
72 // The image variant name for this snapshot image.
73 // For example, recovery snapshot image will return "recovery", and vendor snapshot image will
74 // return "vendor." + version.
75 imageVariantName(cfg android.DeviceConfig) string
76
77 // The variant suffix for snapshot modules. For example, vendor snapshot modules will have
78 // ".vendor" as their suffix.
79 moduleNameSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +090080}
81
82type vendorSnapshotImage struct{}
83type recoverySnapshotImage struct{}
84
Colin Crosse0edaf92021-01-11 17:31:17 -080085func (vendorSnapshotImage) init(ctx android.RegistrationContext) {
86 ctx.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
87 ctx.RegisterModuleType("vendor_snapshot", vendorSnapshotFactory)
88 ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
89 ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
90 ctx.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
91 ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
92 ctx.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
Inseob Kime9aec6a2021-01-05 20:03:22 +090093
Colin Crosse0edaf92021-01-11 17:31:17 -080094 ctx.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
Inseob Kimde5744a2020-12-02 13:14:28 +090095}
96
Jose Galmes6f843bc2020-12-11 13:36:29 -080097func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
98 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot.
99 return ctx.DeviceConfig().VndkVersion() == "current"
100}
101
Inseob Kimde5744a2020-12-02 13:14:28 +0900102func (vendorSnapshotImage) inImage(m *Module) func() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500103 return m.InVendor
Inseob Kimde5744a2020-12-02 13:14:28 +0900104}
105
Justin Yune09ac172021-01-20 19:49:01 +0900106func (vendorSnapshotImage) private(m *Module) bool {
107 return m.IsVndkPrivate()
Inseob Kimde5744a2020-12-02 13:14:28 +0900108}
109
110func (vendorSnapshotImage) isProprietaryPath(dir string) bool {
111 return isVendorProprietaryPath(dir)
112}
113
114// vendor snapshot includes static/header libraries with vndk: {enabled: true}.
115func (vendorSnapshotImage) includeVndk() bool {
116 return true
117}
118
119func (vendorSnapshotImage) excludeFromSnapshot(m *Module) bool {
120 return m.ExcludeFromVendorSnapshot()
121}
122
Jose Galmes6f843bc2020-12-11 13:36:29 -0800123func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
124 vndkVersion := cfg.VndkVersion()
125 return vndkVersion != "current" && vndkVersion != ""
126}
127
Colin Crosse0edaf92021-01-11 17:31:17 -0800128func (vendorSnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
129 return cfg.VndkVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800130}
131
Inseob Kim7cf14652021-01-06 23:06:52 +0900132// returns true iff a given module SHOULD BE EXCLUDED, false if included
133func (vendorSnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
134 // If we're using full snapshot, not directed snapshot, capture every module
135 if !cfg.DirectedVendorSnapshot() {
136 return false
137 }
138 // Else, checks if name is in VENDOR_SNAPSHOT_MODULES.
139 return !cfg.VendorSnapshotModules()[name]
140}
141
Colin Crosse0edaf92021-01-11 17:31:17 -0800142func (vendorSnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
143 return VendorVariationPrefix + cfg.VndkVersion()
144}
145
146func (vendorSnapshotImage) moduleNameSuffix() string {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500147 return VendorSuffix
Colin Crosse0edaf92021-01-11 17:31:17 -0800148}
149
150func (recoverySnapshotImage) init(ctx android.RegistrationContext) {
151 ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
152 ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
153 ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
154 ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
155 ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
156 ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
157 ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
Inseob Kimde5744a2020-12-02 13:14:28 +0900158}
159
Jose Galmes6f843bc2020-12-11 13:36:29 -0800160func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
161 // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a
162 // snapshot.
163 return ctx.DeviceConfig().RecoverySnapshotVersion() == "current"
164}
165
Inseob Kimde5744a2020-12-02 13:14:28 +0900166func (recoverySnapshotImage) inImage(m *Module) func() bool {
167 return m.InRecovery
168}
169
Justin Yune09ac172021-01-20 19:49:01 +0900170// recovery snapshot does not have private libraries.
171func (recoverySnapshotImage) private(m *Module) bool {
172 return false
Inseob Kimde5744a2020-12-02 13:14:28 +0900173}
174
175func (recoverySnapshotImage) isProprietaryPath(dir string) bool {
176 return isRecoveryProprietaryPath(dir)
177}
178
179// recovery snapshot does NOT treat vndk specially.
180func (recoverySnapshotImage) includeVndk() bool {
181 return false
182}
183
184func (recoverySnapshotImage) excludeFromSnapshot(m *Module) bool {
185 return m.ExcludeFromRecoverySnapshot()
186}
187
Jose Galmes6f843bc2020-12-11 13:36:29 -0800188func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
189 recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
190 return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
191}
192
Colin Crosse0edaf92021-01-11 17:31:17 -0800193func (recoverySnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
194 return cfg.RecoverySnapshotVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800195}
196
Inseob Kim7cf14652021-01-06 23:06:52 +0900197func (recoverySnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
Jose Galmes4c6895e2021-02-09 07:44:30 -0800198 // If we're using full snapshot, not directed snapshot, capture every module
199 if !cfg.DirectedRecoverySnapshot() {
200 return false
201 }
202 // Else, checks if name is in RECOVERY_SNAPSHOT_MODULES.
203 return !cfg.RecoverySnapshotModules()[name]
Inseob Kim7cf14652021-01-06 23:06:52 +0900204}
205
Colin Crosse0edaf92021-01-11 17:31:17 -0800206func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
207 return android.RecoveryVariation
208}
209
210func (recoverySnapshotImage) moduleNameSuffix() string {
211 return recoverySuffix
212}
213
Inseob Kimde5744a2020-12-02 13:14:28 +0900214var vendorSnapshotImageSingleton vendorSnapshotImage
215var recoverySnapshotImageSingleton recoverySnapshotImage
216
217func init() {
Colin Crosse0edaf92021-01-11 17:31:17 -0800218 vendorSnapshotImageSingleton.init(android.InitRegistrationContext)
219 recoverySnapshotImageSingleton.init(android.InitRegistrationContext)
Inseob Kimde5744a2020-12-02 13:14:28 +0900220}
221
222const (
Colin Crosse0edaf92021-01-11 17:31:17 -0800223 snapshotHeaderSuffix = "_header."
224 snapshotSharedSuffix = "_shared."
225 snapshotStaticSuffix = "_static."
226 snapshotBinarySuffix = "_binary."
227 snapshotObjectSuffix = "_object."
Inseob Kimde5744a2020-12-02 13:14:28 +0900228)
229
Colin Crosse0edaf92021-01-11 17:31:17 -0800230type SnapshotProperties struct {
231 Header_libs []string `android:"arch_variant"`
232 Static_libs []string `android:"arch_variant"`
233 Shared_libs []string `android:"arch_variant"`
234 Vndk_libs []string `android:"arch_variant"`
235 Binaries []string `android:"arch_variant"`
236 Objects []string `android:"arch_variant"`
237}
238
239type snapshot struct {
240 android.ModuleBase
241
242 properties SnapshotProperties
243
244 baseSnapshot baseSnapshotDecorator
245
246 image snapshotImage
247}
248
249func (s *snapshot) ImageMutatorBegin(ctx android.BaseModuleContext) {
250 cfg := ctx.DeviceConfig()
251 if !s.image.isUsingSnapshot(cfg) || s.image.targetSnapshotVersion(cfg) != s.baseSnapshot.version() {
252 s.Disable()
253 }
254}
255
256func (s *snapshot) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
257 return false
258}
259
260func (s *snapshot) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
261 return false
262}
263
264func (s *snapshot) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
265 return false
266}
267
268func (s *snapshot) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
269 return false
270}
271
272func (s *snapshot) ExtraImageVariations(ctx android.BaseModuleContext) []string {
273 return []string{s.image.imageVariantName(ctx.DeviceConfig())}
274}
275
276func (s *snapshot) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
277}
278
279func (s *snapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
280 // Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
281}
282
Justin Yun07b9f862021-02-26 14:00:03 +0900283func getSnapshotNameSuffix(moduleSuffix, version, arch string) string {
284 versionSuffix := version
285 if arch != "" {
286 versionSuffix += "." + arch
287 }
288 return moduleSuffix + versionSuffix
289}
Colin Crosse0edaf92021-01-11 17:31:17 -0800290
Justin Yun07b9f862021-02-26 14:00:03 +0900291func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
292 collectSnapshotMap := func(names []string, snapshotSuffix, moduleSuffix string) map[string]string {
Colin Crosse0edaf92021-01-11 17:31:17 -0800293 snapshotMap := make(map[string]string)
Justin Yun48138672021-02-25 18:21:27 +0900294 for _, name := range names {
295 snapshotMap[name] = name +
Justin Yun07b9f862021-02-26 14:00:03 +0900296 getSnapshotNameSuffix(snapshotSuffix+moduleSuffix,
297 s.baseSnapshot.version(), ctx.Arch().ArchType.Name)
Colin Crosse0edaf92021-01-11 17:31:17 -0800298 }
299 return snapshotMap
300 }
301
302 snapshotSuffix := s.image.moduleNameSuffix()
Justin Yun07b9f862021-02-26 14:00:03 +0900303 headers := collectSnapshotMap(s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
304 binaries := collectSnapshotMap(s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
305 objects := collectSnapshotMap(s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
306 staticLibs := collectSnapshotMap(s.properties.Static_libs, snapshotSuffix, snapshotStaticSuffix)
307 sharedLibs := collectSnapshotMap(s.properties.Shared_libs, snapshotSuffix, snapshotSharedSuffix)
308 vndkLibs := collectSnapshotMap(s.properties.Vndk_libs, "", vndkSuffix)
Colin Crosse0edaf92021-01-11 17:31:17 -0800309 for k, v := range vndkLibs {
310 sharedLibs[k] = v
311 }
Justin Yun07b9f862021-02-26 14:00:03 +0900312
Colin Crosse0edaf92021-01-11 17:31:17 -0800313 ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
314 HeaderLibs: headers,
315 Binaries: binaries,
316 Objects: objects,
317 StaticLibs: staticLibs,
318 SharedLibs: sharedLibs,
319 })
320}
321
322type SnapshotInfo struct {
323 HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs map[string]string
324}
325
326var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
327
328var _ android.ImageInterface = (*snapshot)(nil)
329
330func vendorSnapshotFactory() android.Module {
331 return snapshotFactory(vendorSnapshotImageSingleton)
332}
333
334func recoverySnapshotFactory() android.Module {
335 return snapshotFactory(recoverySnapshotImageSingleton)
336}
337
338func snapshotFactory(image snapshotImage) android.Module {
339 snapshot := &snapshot{}
340 snapshot.image = image
341 snapshot.AddProperties(
342 &snapshot.properties,
343 &snapshot.baseSnapshot.baseProperties)
344 android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
345 return snapshot
346}
347
Inseob Kimde5744a2020-12-02 13:14:28 +0900348type baseSnapshotDecoratorProperties struct {
349 // snapshot version.
350 Version string
351
352 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
353 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800354
Colin Crossa8890802021-01-22 14:06:33 -0800355 // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
356 Androidmk_suffix string
357
Jose Galmes6f843bc2020-12-11 13:36:29 -0800358 // Suffix to be added to the module name, e.g., vendor_shared,
359 // recovery_shared, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800360 ModuleSuffix string `blueprint:"mutated"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900361}
362
363// baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
364// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
365// collide with source modules. e.g. the following example module,
366//
367// vendor_snapshot_static {
368// name: "libbase",
369// arch: "arm64",
370// version: 30,
371// ...
372// }
373//
374// will be seen as "libbase.vendor_static.30.arm64" by Soong.
375type baseSnapshotDecorator struct {
376 baseProperties baseSnapshotDecoratorProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900377}
378
379func (p *baseSnapshotDecorator) Name(name string) string {
380 return name + p.NameSuffix()
381}
382
383func (p *baseSnapshotDecorator) NameSuffix() string {
Justin Yun07b9f862021-02-26 14:00:03 +0900384 return getSnapshotNameSuffix(p.moduleSuffix(), p.version(), p.arch())
Inseob Kimde5744a2020-12-02 13:14:28 +0900385}
386
387func (p *baseSnapshotDecorator) version() string {
388 return p.baseProperties.Version
389}
390
391func (p *baseSnapshotDecorator) arch() string {
392 return p.baseProperties.Target_arch
393}
394
Colin Crosse0edaf92021-01-11 17:31:17 -0800395func (p *baseSnapshotDecorator) moduleSuffix() string {
396 return p.baseProperties.ModuleSuffix
Jose Galmes6f843bc2020-12-11 13:36:29 -0800397}
398
Inseob Kimde5744a2020-12-02 13:14:28 +0900399func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
400 return true
401}
402
Colin Crossa8890802021-01-22 14:06:33 -0800403func (p *baseSnapshotDecorator) snapshotAndroidMkSuffix() string {
404 return p.baseProperties.Androidmk_suffix
405}
406
Inseob Kimde5744a2020-12-02 13:14:28 +0900407// Call this with a module suffix after creating a snapshot module, such as
408// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800409func (p *baseSnapshotDecorator) init(m *Module, snapshotSuffix, moduleSuffix string) {
410 p.baseProperties.ModuleSuffix = snapshotSuffix + moduleSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900411 m.AddProperties(&p.baseProperties)
412 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
413 vendorSnapshotLoadHook(ctx, p)
414 })
415}
416
417// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
418// As vendor snapshot is only for vendor, such modules won't be used at all.
419func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) {
420 if p.version() != ctx.DeviceConfig().VndkVersion() {
421 ctx.Module().Disable()
422 return
423 }
424}
425
426//
427// Module definitions for snapshots of libraries (shared, static, header).
428//
429// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
430// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
431// which can be installed or linked against. Also they export flags needed when linked, such as
432// include directories, c flags, sanitize dependency information, etc.
433//
434// These modules are auto-generated by development/vendor_snapshot/update.py.
435type snapshotLibraryProperties struct {
436 // Prebuilt file for each arch.
437 Src *string `android:"arch_variant"`
438
439 // list of directories that will be added to the include path (using -I).
440 Export_include_dirs []string `android:"arch_variant"`
441
442 // list of directories that will be added to the system path (using -isystem).
443 Export_system_include_dirs []string `android:"arch_variant"`
444
445 // list of flags that will be used for any module that links against this module.
446 Export_flags []string `android:"arch_variant"`
447
448 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
449 Sanitize_ubsan_dep *bool `android:"arch_variant"`
450
451 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
452 Sanitize_minimal_dep *bool `android:"arch_variant"`
453}
454
455type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500456 isSanitizerEnabled(t SanitizerType) bool
457 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900458}
459
460type snapshotLibraryDecorator struct {
461 baseSnapshotDecorator
462 *libraryDecorator
463 properties snapshotLibraryProperties
464 sanitizerProperties struct {
465 CfiEnabled bool `blueprint:"mutated"`
466
467 // Library flags for cfi variant.
468 Cfi snapshotLibraryProperties `android:"arch_variant"`
469 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900470}
471
472func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
473 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
474 return p.libraryDecorator.linkerFlags(ctx, flags)
475}
476
477func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
478 arches := config.Arches()
479 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
480 return false
481 }
482 if !p.header() && p.properties.Src == nil {
483 return false
484 }
485 return true
486}
487
488// cc modules' link functions are to link compiled objects into final binaries.
489// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
490// done by normal library decorator, e.g. exporting flags.
491func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Inseob Kimde5744a2020-12-02 13:14:28 +0900492 if p.header() {
493 return p.libraryDecorator.link(ctx, flags, deps, objs)
494 }
495
496 if p.sanitizerProperties.CfiEnabled {
497 p.properties = p.sanitizerProperties.Cfi
498 }
499
500 if !p.matchesWithDevice(ctx.DeviceConfig()) {
501 return nil
502 }
503
504 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
505 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
506 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
507
508 in := android.PathForModuleSrc(ctx, *p.properties.Src)
509 p.unstrippedOutputFile = in
510
511 if p.shared() {
512 libName := in.Base()
513 builderFlags := flagsToBuilderFlags(flags)
514
515 // Optimize out relinking against shared libraries whose interface hasn't changed by
516 // depending on a table of contents file instead of the library itself.
517 tocFile := android.PathForModuleOut(ctx, libName+".toc")
518 p.tocFile = android.OptionalPathForPath(tocFile)
519 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
520
521 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
522 SharedLibrary: in,
523 UnstrippedSharedLibrary: p.unstrippedOutputFile,
524
525 TableOfContents: p.tocFile,
526 })
527 }
528
529 if p.static() {
530 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
531 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
532 StaticLibrary: in,
533
534 TransitiveStaticLibrariesForOrdering: depSet,
535 })
536 }
537
538 p.libraryDecorator.flagExporter.setProvider(ctx)
539
540 return in
541}
542
543func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
544 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
545 p.baseInstaller.install(ctx, file)
546 }
547}
548
549func (p *snapshotLibraryDecorator) nativeCoverage() bool {
550 return false
551}
552
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500553func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900554 switch t {
555 case cfi:
556 return p.sanitizerProperties.Cfi.Src != nil
557 default:
558 return false
559 }
560}
561
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500562func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900563 if !enabled {
564 return
565 }
566 switch t {
567 case cfi:
568 p.sanitizerProperties.CfiEnabled = true
569 default:
570 return
571 }
572}
573
Colin Crosse0edaf92021-01-11 17:31:17 -0800574func snapshotLibraryFactory(snapshotSuffix, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900575 module, library := NewLibrary(android.DeviceSupported)
576
577 module.stl = nil
578 module.sanitize = nil
579 library.disableStripping()
580
581 prebuilt := &snapshotLibraryDecorator{
582 libraryDecorator: library,
583 }
584
585 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
586 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
587
588 // Prevent default system libs (libc, libm, and libdl) from being linked
589 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
590 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
591 }
592
593 module.compiler = nil
594 module.linker = prebuilt
595 module.installer = prebuilt
596
Colin Crosse0edaf92021-01-11 17:31:17 -0800597 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900598 module.AddProperties(
599 &prebuilt.properties,
600 &prebuilt.sanitizerProperties,
601 )
602
603 return module, prebuilt
604}
605
606// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
607// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
608// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
609// is set.
610func VendorSnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800611 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900612 prebuilt.libraryDecorator.BuildOnlyShared()
613 return module.Init()
614}
615
616// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
617// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
618// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
619// is set.
620func RecoverySnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800621 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900622 prebuilt.libraryDecorator.BuildOnlyShared()
623 return module.Init()
624}
625
626// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
627// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
628// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
629// is set.
630func VendorSnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800631 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900632 prebuilt.libraryDecorator.BuildOnlyStatic()
633 return module.Init()
634}
635
636// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
637// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
638// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
639// is set.
640func RecoverySnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800641 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900642 prebuilt.libraryDecorator.BuildOnlyStatic()
643 return module.Init()
644}
645
646// vendor_snapshot_header is a special header library which is auto-generated by
647// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
648// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
649// is set.
650func VendorSnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800651 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900652 prebuilt.libraryDecorator.HeaderOnly()
653 return module.Init()
654}
655
656// recovery_snapshot_header is a special header library which is auto-generated by
657// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
658// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
659// is set.
660func RecoverySnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800661 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900662 prebuilt.libraryDecorator.HeaderOnly()
663 return module.Init()
664}
665
666var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
667
668//
669// Module definitions for snapshots of executable binaries.
670//
671// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
672// binaries (e.g. toybox, sh) as their src, which can be installed.
673//
674// These modules are auto-generated by development/vendor_snapshot/update.py.
675type snapshotBinaryProperties struct {
676 // Prebuilt file for each arch.
677 Src *string `android:"arch_variant"`
678}
679
680type snapshotBinaryDecorator struct {
681 baseSnapshotDecorator
682 *binaryDecorator
Colin Crossa8890802021-01-22 14:06:33 -0800683 properties snapshotBinaryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900684}
685
686func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
687 if config.DeviceArch() != p.arch() {
688 return false
689 }
690 if p.properties.Src == nil {
691 return false
692 }
693 return true
694}
695
696// cc modules' link functions are to link compiled objects into final binaries.
697// As snapshots are prebuilts, this just returns the prebuilt binary
698func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
699 if !p.matchesWithDevice(ctx.DeviceConfig()) {
700 return nil
701 }
702
703 in := android.PathForModuleSrc(ctx, *p.properties.Src)
704 p.unstrippedOutputFile = in
705 binName := in.Base()
706
Inseob Kimde5744a2020-12-02 13:14:28 +0900707 // use cpExecutable to make it executable
708 outputFile := android.PathForModuleOut(ctx, binName)
709 ctx.Build(pctx, android.BuildParams{
710 Rule: android.CpExecutable,
711 Description: "prebuilt",
712 Output: outputFile,
713 Input: in,
714 })
715
716 return outputFile
717}
718
719func (p *snapshotBinaryDecorator) nativeCoverage() bool {
720 return false
721}
722
723// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
724// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
725// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
726func VendorSnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800727 return snapshotBinaryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900728}
729
730// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
731// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
732// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
733func RecoverySnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800734 return snapshotBinaryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900735}
736
Colin Crosse0edaf92021-01-11 17:31:17 -0800737func snapshotBinaryFactory(snapshotSuffix, moduleSuffix string) android.Module {
Inseob Kimde5744a2020-12-02 13:14:28 +0900738 module, binary := NewBinary(android.DeviceSupported)
739 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
740 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
741
742 // Prevent default system libs (libc, libm, and libdl) from being linked
743 if binary.baseLinker.Properties.System_shared_libs == nil {
744 binary.baseLinker.Properties.System_shared_libs = []string{}
745 }
746
747 prebuilt := &snapshotBinaryDecorator{
748 binaryDecorator: binary,
749 }
750
751 module.compiler = nil
752 module.sanitize = nil
753 module.stl = nil
754 module.linker = prebuilt
755
Colin Crosse0edaf92021-01-11 17:31:17 -0800756 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900757 module.AddProperties(&prebuilt.properties)
758 return module.Init()
759}
760
761//
762// Module definitions for snapshots of object files (*.o).
763//
764// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
765// files (*.o) as their src.
766//
767// These modules are auto-generated by development/vendor_snapshot/update.py.
768type vendorSnapshotObjectProperties struct {
769 // Prebuilt file for each arch.
770 Src *string `android:"arch_variant"`
771}
772
773type snapshotObjectLinker struct {
774 baseSnapshotDecorator
775 objectLinker
Colin Crossa8890802021-01-22 14:06:33 -0800776 properties vendorSnapshotObjectProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900777}
778
779func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
780 if config.DeviceArch() != p.arch() {
781 return false
782 }
783 if p.properties.Src == nil {
784 return false
785 }
786 return true
787}
788
789// cc modules' link functions are to link compiled objects into final binaries.
790// As snapshots are prebuilts, this just returns the prebuilt binary
791func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
792 if !p.matchesWithDevice(ctx.DeviceConfig()) {
793 return nil
794 }
795
Inseob Kimde5744a2020-12-02 13:14:28 +0900796 return android.PathForModuleSrc(ctx, *p.properties.Src)
797}
798
799func (p *snapshotObjectLinker) nativeCoverage() bool {
800 return false
801}
802
803// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
804// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
805// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
806func VendorSnapshotObjectFactory() android.Module {
807 module := newObject()
808
809 prebuilt := &snapshotObjectLinker{
810 objectLinker: objectLinker{
811 baseLinker: NewBaseLinker(nil),
812 },
813 }
814 module.linker = prebuilt
815
Colin Crosse0edaf92021-01-11 17:31:17 -0800816 prebuilt.init(module, vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900817 module.AddProperties(&prebuilt.properties)
818 return module.Init()
819}
820
821// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
822// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
823// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
824func RecoverySnapshotObjectFactory() android.Module {
825 module := newObject()
826
827 prebuilt := &snapshotObjectLinker{
828 objectLinker: objectLinker{
829 baseLinker: NewBaseLinker(nil),
830 },
831 }
832 module.linker = prebuilt
833
Colin Crosse0edaf92021-01-11 17:31:17 -0800834 prebuilt.init(module, recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900835 module.AddProperties(&prebuilt.properties)
836 return module.Init()
837}
838
839type snapshotInterface interface {
840 matchesWithDevice(config android.DeviceConfig) bool
Colin Crossa8890802021-01-22 14:06:33 -0800841 isSnapshotPrebuilt() bool
842 version() string
843 snapshotAndroidMkSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +0900844}
845
846var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
847var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
848var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
849var _ snapshotInterface = (*snapshotObjectLinker)(nil)