blob: 62daafde475a70ad306e7db6bee164c93ae736a4 [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
283func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
284 collectSnapshotMap := func(variations []blueprint.Variation, depTag blueprint.DependencyTag,
285 names []string, snapshotSuffix, moduleSuffix string) map[string]string {
286
287 decoratedNames := make([]string, 0, len(names))
288 for _, name := range names {
289 decoratedNames = append(decoratedNames, name+
290 snapshotSuffix+moduleSuffix+
291 s.baseSnapshot.version()+
292 "."+ctx.Arch().ArchType.Name)
293 }
294
295 deps := ctx.AddVariationDependencies(variations, depTag, decoratedNames...)
296 snapshotMap := make(map[string]string)
297 for _, dep := range deps {
298 if dep == nil {
299 continue
300 }
301
302 snapshotMap[dep.(*Module).BaseModuleName()] = ctx.OtherModuleName(dep)
303 }
304 return snapshotMap
305 }
306
307 snapshotSuffix := s.image.moduleNameSuffix()
308 headers := collectSnapshotMap(nil, HeaderDepTag(), s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
309 binaries := collectSnapshotMap(nil, nil, s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
310 objects := collectSnapshotMap(nil, nil, s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
311
312 staticLibs := collectSnapshotMap([]blueprint.Variation{
313 {Mutator: "link", Variation: "static"},
314 }, StaticDepTag(), s.properties.Static_libs, snapshotSuffix, snapshotStaticSuffix)
315
316 sharedLibs := collectSnapshotMap([]blueprint.Variation{
317 {Mutator: "link", Variation: "shared"},
318 }, SharedDepTag(), s.properties.Shared_libs, snapshotSuffix, snapshotSharedSuffix)
319
320 vndkLibs := collectSnapshotMap([]blueprint.Variation{
321 {Mutator: "link", Variation: "shared"},
322 }, SharedDepTag(), s.properties.Vndk_libs, "", vndkSuffix)
323
324 for k, v := range vndkLibs {
325 sharedLibs[k] = v
326 }
327 ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
328 HeaderLibs: headers,
329 Binaries: binaries,
330 Objects: objects,
331 StaticLibs: staticLibs,
332 SharedLibs: sharedLibs,
333 })
334}
335
336type SnapshotInfo struct {
337 HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs map[string]string
338}
339
340var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
341
342var _ android.ImageInterface = (*snapshot)(nil)
343
344func vendorSnapshotFactory() android.Module {
345 return snapshotFactory(vendorSnapshotImageSingleton)
346}
347
348func recoverySnapshotFactory() android.Module {
349 return snapshotFactory(recoverySnapshotImageSingleton)
350}
351
352func snapshotFactory(image snapshotImage) android.Module {
353 snapshot := &snapshot{}
354 snapshot.image = image
355 snapshot.AddProperties(
356 &snapshot.properties,
357 &snapshot.baseSnapshot.baseProperties)
358 android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
359 return snapshot
360}
361
Inseob Kimde5744a2020-12-02 13:14:28 +0900362type baseSnapshotDecoratorProperties struct {
363 // snapshot version.
364 Version string
365
366 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
367 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800368
Colin Crossa8890802021-01-22 14:06:33 -0800369 // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
370 Androidmk_suffix string
371
Jose Galmes6f843bc2020-12-11 13:36:29 -0800372 // Suffix to be added to the module name, e.g., vendor_shared,
373 // recovery_shared, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800374 ModuleSuffix string `blueprint:"mutated"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900375}
376
377// baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
378// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
379// collide with source modules. e.g. the following example module,
380//
381// vendor_snapshot_static {
382// name: "libbase",
383// arch: "arm64",
384// version: 30,
385// ...
386// }
387//
388// will be seen as "libbase.vendor_static.30.arm64" by Soong.
389type baseSnapshotDecorator struct {
390 baseProperties baseSnapshotDecoratorProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900391}
392
393func (p *baseSnapshotDecorator) Name(name string) string {
394 return name + p.NameSuffix()
395}
396
397func (p *baseSnapshotDecorator) NameSuffix() string {
398 versionSuffix := p.version()
399 if p.arch() != "" {
400 versionSuffix += "." + p.arch()
401 }
402
Colin Crosse0edaf92021-01-11 17:31:17 -0800403 return p.baseProperties.ModuleSuffix + versionSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900404}
405
406func (p *baseSnapshotDecorator) version() string {
407 return p.baseProperties.Version
408}
409
410func (p *baseSnapshotDecorator) arch() string {
411 return p.baseProperties.Target_arch
412}
413
Colin Crosse0edaf92021-01-11 17:31:17 -0800414func (p *baseSnapshotDecorator) moduleSuffix() string {
415 return p.baseProperties.ModuleSuffix
Jose Galmes6f843bc2020-12-11 13:36:29 -0800416}
417
Inseob Kimde5744a2020-12-02 13:14:28 +0900418func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
419 return true
420}
421
Colin Crossa8890802021-01-22 14:06:33 -0800422func (p *baseSnapshotDecorator) snapshotAndroidMkSuffix() string {
423 return p.baseProperties.Androidmk_suffix
424}
425
Inseob Kimde5744a2020-12-02 13:14:28 +0900426// Call this with a module suffix after creating a snapshot module, such as
427// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800428func (p *baseSnapshotDecorator) init(m *Module, snapshotSuffix, moduleSuffix string) {
429 p.baseProperties.ModuleSuffix = snapshotSuffix + moduleSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900430 m.AddProperties(&p.baseProperties)
431 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
432 vendorSnapshotLoadHook(ctx, p)
433 })
434}
435
436// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
437// As vendor snapshot is only for vendor, such modules won't be used at all.
438func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) {
439 if p.version() != ctx.DeviceConfig().VndkVersion() {
440 ctx.Module().Disable()
441 return
442 }
443}
444
445//
446// Module definitions for snapshots of libraries (shared, static, header).
447//
448// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
449// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
450// which can be installed or linked against. Also they export flags needed when linked, such as
451// include directories, c flags, sanitize dependency information, etc.
452//
453// These modules are auto-generated by development/vendor_snapshot/update.py.
454type snapshotLibraryProperties struct {
455 // Prebuilt file for each arch.
456 Src *string `android:"arch_variant"`
457
458 // list of directories that will be added to the include path (using -I).
459 Export_include_dirs []string `android:"arch_variant"`
460
461 // list of directories that will be added to the system path (using -isystem).
462 Export_system_include_dirs []string `android:"arch_variant"`
463
464 // list of flags that will be used for any module that links against this module.
465 Export_flags []string `android:"arch_variant"`
466
467 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
468 Sanitize_ubsan_dep *bool `android:"arch_variant"`
469
470 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
471 Sanitize_minimal_dep *bool `android:"arch_variant"`
472}
473
474type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500475 isSanitizerEnabled(t SanitizerType) bool
476 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900477}
478
479type snapshotLibraryDecorator struct {
480 baseSnapshotDecorator
481 *libraryDecorator
482 properties snapshotLibraryProperties
483 sanitizerProperties struct {
484 CfiEnabled bool `blueprint:"mutated"`
485
486 // Library flags for cfi variant.
487 Cfi snapshotLibraryProperties `android:"arch_variant"`
488 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900489}
490
491func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
492 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
493 return p.libraryDecorator.linkerFlags(ctx, flags)
494}
495
496func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
497 arches := config.Arches()
498 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
499 return false
500 }
501 if !p.header() && p.properties.Src == nil {
502 return false
503 }
504 return true
505}
506
507// cc modules' link functions are to link compiled objects into final binaries.
508// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
509// done by normal library decorator, e.g. exporting flags.
510func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Inseob Kimde5744a2020-12-02 13:14:28 +0900511 if p.header() {
512 return p.libraryDecorator.link(ctx, flags, deps, objs)
513 }
514
515 if p.sanitizerProperties.CfiEnabled {
516 p.properties = p.sanitizerProperties.Cfi
517 }
518
519 if !p.matchesWithDevice(ctx.DeviceConfig()) {
520 return nil
521 }
522
523 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
524 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
525 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
526
527 in := android.PathForModuleSrc(ctx, *p.properties.Src)
528 p.unstrippedOutputFile = in
529
530 if p.shared() {
531 libName := in.Base()
532 builderFlags := flagsToBuilderFlags(flags)
533
534 // Optimize out relinking against shared libraries whose interface hasn't changed by
535 // depending on a table of contents file instead of the library itself.
536 tocFile := android.PathForModuleOut(ctx, libName+".toc")
537 p.tocFile = android.OptionalPathForPath(tocFile)
538 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
539
540 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
541 SharedLibrary: in,
542 UnstrippedSharedLibrary: p.unstrippedOutputFile,
543
544 TableOfContents: p.tocFile,
545 })
546 }
547
548 if p.static() {
549 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
550 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
551 StaticLibrary: in,
552
553 TransitiveStaticLibrariesForOrdering: depSet,
554 })
555 }
556
557 p.libraryDecorator.flagExporter.setProvider(ctx)
558
559 return in
560}
561
562func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
563 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
564 p.baseInstaller.install(ctx, file)
565 }
566}
567
568func (p *snapshotLibraryDecorator) nativeCoverage() bool {
569 return false
570}
571
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500572func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900573 switch t {
574 case cfi:
575 return p.sanitizerProperties.Cfi.Src != nil
576 default:
577 return false
578 }
579}
580
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500581func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900582 if !enabled {
583 return
584 }
585 switch t {
586 case cfi:
587 p.sanitizerProperties.CfiEnabled = true
588 default:
589 return
590 }
591}
592
Colin Crosse0edaf92021-01-11 17:31:17 -0800593func snapshotLibraryFactory(snapshotSuffix, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900594 module, library := NewLibrary(android.DeviceSupported)
595
596 module.stl = nil
597 module.sanitize = nil
598 library.disableStripping()
599
600 prebuilt := &snapshotLibraryDecorator{
601 libraryDecorator: library,
602 }
603
604 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
605 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
606
607 // Prevent default system libs (libc, libm, and libdl) from being linked
608 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
609 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
610 }
611
612 module.compiler = nil
613 module.linker = prebuilt
614 module.installer = prebuilt
615
Colin Crosse0edaf92021-01-11 17:31:17 -0800616 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900617 module.AddProperties(
618 &prebuilt.properties,
619 &prebuilt.sanitizerProperties,
620 )
621
622 return module, prebuilt
623}
624
625// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
626// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
627// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
628// is set.
629func VendorSnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800630 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900631 prebuilt.libraryDecorator.BuildOnlyShared()
632 return module.Init()
633}
634
635// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
636// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
637// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
638// is set.
639func RecoverySnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800640 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900641 prebuilt.libraryDecorator.BuildOnlyShared()
642 return module.Init()
643}
644
645// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
646// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
647// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
648// is set.
649func VendorSnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800650 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900651 prebuilt.libraryDecorator.BuildOnlyStatic()
652 return module.Init()
653}
654
655// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
656// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
657// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
658// is set.
659func RecoverySnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800660 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900661 prebuilt.libraryDecorator.BuildOnlyStatic()
662 return module.Init()
663}
664
665// vendor_snapshot_header is a special header library which is auto-generated by
666// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
667// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
668// is set.
669func VendorSnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800670 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900671 prebuilt.libraryDecorator.HeaderOnly()
672 return module.Init()
673}
674
675// recovery_snapshot_header is a special header library which is auto-generated by
676// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
677// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
678// is set.
679func RecoverySnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800680 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900681 prebuilt.libraryDecorator.HeaderOnly()
682 return module.Init()
683}
684
685var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
686
687//
688// Module definitions for snapshots of executable binaries.
689//
690// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
691// binaries (e.g. toybox, sh) as their src, which can be installed.
692//
693// These modules are auto-generated by development/vendor_snapshot/update.py.
694type snapshotBinaryProperties struct {
695 // Prebuilt file for each arch.
696 Src *string `android:"arch_variant"`
697}
698
699type snapshotBinaryDecorator struct {
700 baseSnapshotDecorator
701 *binaryDecorator
Colin Crossa8890802021-01-22 14:06:33 -0800702 properties snapshotBinaryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900703}
704
705func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
706 if config.DeviceArch() != p.arch() {
707 return false
708 }
709 if p.properties.Src == nil {
710 return false
711 }
712 return true
713}
714
715// cc modules' link functions are to link compiled objects into final binaries.
716// As snapshots are prebuilts, this just returns the prebuilt binary
717func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
718 if !p.matchesWithDevice(ctx.DeviceConfig()) {
719 return nil
720 }
721
722 in := android.PathForModuleSrc(ctx, *p.properties.Src)
723 p.unstrippedOutputFile = in
724 binName := in.Base()
725
Inseob Kimde5744a2020-12-02 13:14:28 +0900726 // use cpExecutable to make it executable
727 outputFile := android.PathForModuleOut(ctx, binName)
728 ctx.Build(pctx, android.BuildParams{
729 Rule: android.CpExecutable,
730 Description: "prebuilt",
731 Output: outputFile,
732 Input: in,
733 })
734
735 return outputFile
736}
737
738func (p *snapshotBinaryDecorator) nativeCoverage() bool {
739 return false
740}
741
742// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
743// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
744// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
745func VendorSnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800746 return snapshotBinaryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900747}
748
749// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
750// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
751// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
752func RecoverySnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800753 return snapshotBinaryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900754}
755
Colin Crosse0edaf92021-01-11 17:31:17 -0800756func snapshotBinaryFactory(snapshotSuffix, moduleSuffix string) android.Module {
Inseob Kimde5744a2020-12-02 13:14:28 +0900757 module, binary := NewBinary(android.DeviceSupported)
758 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
759 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
760
761 // Prevent default system libs (libc, libm, and libdl) from being linked
762 if binary.baseLinker.Properties.System_shared_libs == nil {
763 binary.baseLinker.Properties.System_shared_libs = []string{}
764 }
765
766 prebuilt := &snapshotBinaryDecorator{
767 binaryDecorator: binary,
768 }
769
770 module.compiler = nil
771 module.sanitize = nil
772 module.stl = nil
773 module.linker = prebuilt
774
Colin Crosse0edaf92021-01-11 17:31:17 -0800775 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900776 module.AddProperties(&prebuilt.properties)
777 return module.Init()
778}
779
780//
781// Module definitions for snapshots of object files (*.o).
782//
783// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
784// files (*.o) as their src.
785//
786// These modules are auto-generated by development/vendor_snapshot/update.py.
787type vendorSnapshotObjectProperties struct {
788 // Prebuilt file for each arch.
789 Src *string `android:"arch_variant"`
790}
791
792type snapshotObjectLinker struct {
793 baseSnapshotDecorator
794 objectLinker
Colin Crossa8890802021-01-22 14:06:33 -0800795 properties vendorSnapshotObjectProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900796}
797
798func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
799 if config.DeviceArch() != p.arch() {
800 return false
801 }
802 if p.properties.Src == nil {
803 return false
804 }
805 return true
806}
807
808// cc modules' link functions are to link compiled objects into final binaries.
809// As snapshots are prebuilts, this just returns the prebuilt binary
810func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
811 if !p.matchesWithDevice(ctx.DeviceConfig()) {
812 return nil
813 }
814
Inseob Kimde5744a2020-12-02 13:14:28 +0900815 return android.PathForModuleSrc(ctx, *p.properties.Src)
816}
817
818func (p *snapshotObjectLinker) nativeCoverage() bool {
819 return false
820}
821
822// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
823// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
824// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
825func VendorSnapshotObjectFactory() android.Module {
826 module := newObject()
827
828 prebuilt := &snapshotObjectLinker{
829 objectLinker: objectLinker{
830 baseLinker: NewBaseLinker(nil),
831 },
832 }
833 module.linker = prebuilt
834
Colin Crosse0edaf92021-01-11 17:31:17 -0800835 prebuilt.init(module, vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900836 module.AddProperties(&prebuilt.properties)
837 return module.Init()
838}
839
840// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
841// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
842// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
843func RecoverySnapshotObjectFactory() android.Module {
844 module := newObject()
845
846 prebuilt := &snapshotObjectLinker{
847 objectLinker: objectLinker{
848 baseLinker: NewBaseLinker(nil),
849 },
850 }
851 module.linker = prebuilt
852
Colin Crosse0edaf92021-01-11 17:31:17 -0800853 prebuilt.init(module, recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900854 module.AddProperties(&prebuilt.properties)
855 return module.Init()
856}
857
858type snapshotInterface interface {
859 matchesWithDevice(config android.DeviceConfig) bool
Colin Crossa8890802021-01-22 14:06:33 -0800860 isSnapshotPrebuilt() bool
861 version() string
862 snapshotAndroidMkSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +0900863}
864
865var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
866var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
867var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
868var _ snapshotInterface = (*snapshotObjectLinker)(nil)