blob: 69b99486145f94927bd9b3484b54741faa726c6c [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
Colin Crosse0edaf92021-01-11 17:31:17 -0800287 snapshotMap := make(map[string]string)
Justin Yun48138672021-02-25 18:21:27 +0900288 for _, name := range names {
289 snapshotMap[name] = name +
290 snapshotSuffix + moduleSuffix +
291 s.baseSnapshot.version() +
292 "." + ctx.Arch().ArchType.Name
Colin Crosse0edaf92021-01-11 17:31:17 -0800293 }
Justin Yun48138672021-02-25 18:21:27 +0900294
Colin Crosse0edaf92021-01-11 17:31:17 -0800295 return snapshotMap
296 }
297
298 snapshotSuffix := s.image.moduleNameSuffix()
299 headers := collectSnapshotMap(nil, HeaderDepTag(), s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
300 binaries := collectSnapshotMap(nil, nil, s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
301 objects := collectSnapshotMap(nil, nil, s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
302
303 staticLibs := collectSnapshotMap([]blueprint.Variation{
304 {Mutator: "link", Variation: "static"},
305 }, StaticDepTag(), s.properties.Static_libs, snapshotSuffix, snapshotStaticSuffix)
306
307 sharedLibs := collectSnapshotMap([]blueprint.Variation{
308 {Mutator: "link", Variation: "shared"},
309 }, SharedDepTag(), s.properties.Shared_libs, snapshotSuffix, snapshotSharedSuffix)
310
311 vndkLibs := collectSnapshotMap([]blueprint.Variation{
312 {Mutator: "link", Variation: "shared"},
313 }, SharedDepTag(), s.properties.Vndk_libs, "", vndkSuffix)
314
315 for k, v := range vndkLibs {
316 sharedLibs[k] = v
317 }
318 ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
319 HeaderLibs: headers,
320 Binaries: binaries,
321 Objects: objects,
322 StaticLibs: staticLibs,
323 SharedLibs: sharedLibs,
324 })
325}
326
327type SnapshotInfo struct {
328 HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs map[string]string
329}
330
331var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
332
333var _ android.ImageInterface = (*snapshot)(nil)
334
335func vendorSnapshotFactory() android.Module {
336 return snapshotFactory(vendorSnapshotImageSingleton)
337}
338
339func recoverySnapshotFactory() android.Module {
340 return snapshotFactory(recoverySnapshotImageSingleton)
341}
342
343func snapshotFactory(image snapshotImage) android.Module {
344 snapshot := &snapshot{}
345 snapshot.image = image
346 snapshot.AddProperties(
347 &snapshot.properties,
348 &snapshot.baseSnapshot.baseProperties)
349 android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
350 return snapshot
351}
352
Inseob Kimde5744a2020-12-02 13:14:28 +0900353type baseSnapshotDecoratorProperties struct {
354 // snapshot version.
355 Version string
356
357 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
358 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800359
Colin Crossa8890802021-01-22 14:06:33 -0800360 // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
361 Androidmk_suffix string
362
Jose Galmes6f843bc2020-12-11 13:36:29 -0800363 // Suffix to be added to the module name, e.g., vendor_shared,
364 // recovery_shared, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800365 ModuleSuffix string `blueprint:"mutated"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900366}
367
368// baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
369// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
370// collide with source modules. e.g. the following example module,
371//
372// vendor_snapshot_static {
373// name: "libbase",
374// arch: "arm64",
375// version: 30,
376// ...
377// }
378//
379// will be seen as "libbase.vendor_static.30.arm64" by Soong.
380type baseSnapshotDecorator struct {
381 baseProperties baseSnapshotDecoratorProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900382}
383
384func (p *baseSnapshotDecorator) Name(name string) string {
385 return name + p.NameSuffix()
386}
387
388func (p *baseSnapshotDecorator) NameSuffix() string {
389 versionSuffix := p.version()
390 if p.arch() != "" {
391 versionSuffix += "." + p.arch()
392 }
393
Colin Crosse0edaf92021-01-11 17:31:17 -0800394 return p.baseProperties.ModuleSuffix + versionSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900395}
396
397func (p *baseSnapshotDecorator) version() string {
398 return p.baseProperties.Version
399}
400
401func (p *baseSnapshotDecorator) arch() string {
402 return p.baseProperties.Target_arch
403}
404
Colin Crosse0edaf92021-01-11 17:31:17 -0800405func (p *baseSnapshotDecorator) moduleSuffix() string {
406 return p.baseProperties.ModuleSuffix
Jose Galmes6f843bc2020-12-11 13:36:29 -0800407}
408
Inseob Kimde5744a2020-12-02 13:14:28 +0900409func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
410 return true
411}
412
Colin Crossa8890802021-01-22 14:06:33 -0800413func (p *baseSnapshotDecorator) snapshotAndroidMkSuffix() string {
414 return p.baseProperties.Androidmk_suffix
415}
416
Inseob Kimde5744a2020-12-02 13:14:28 +0900417// Call this with a module suffix after creating a snapshot module, such as
418// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800419func (p *baseSnapshotDecorator) init(m *Module, snapshotSuffix, moduleSuffix string) {
420 p.baseProperties.ModuleSuffix = snapshotSuffix + moduleSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900421 m.AddProperties(&p.baseProperties)
422 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
423 vendorSnapshotLoadHook(ctx, p)
424 })
425}
426
427// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
428// As vendor snapshot is only for vendor, such modules won't be used at all.
429func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) {
430 if p.version() != ctx.DeviceConfig().VndkVersion() {
431 ctx.Module().Disable()
432 return
433 }
434}
435
436//
437// Module definitions for snapshots of libraries (shared, static, header).
438//
439// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
440// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
441// which can be installed or linked against. Also they export flags needed when linked, such as
442// include directories, c flags, sanitize dependency information, etc.
443//
444// These modules are auto-generated by development/vendor_snapshot/update.py.
445type snapshotLibraryProperties struct {
446 // Prebuilt file for each arch.
447 Src *string `android:"arch_variant"`
448
449 // list of directories that will be added to the include path (using -I).
450 Export_include_dirs []string `android:"arch_variant"`
451
452 // list of directories that will be added to the system path (using -isystem).
453 Export_system_include_dirs []string `android:"arch_variant"`
454
455 // list of flags that will be used for any module that links against this module.
456 Export_flags []string `android:"arch_variant"`
457
458 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
459 Sanitize_ubsan_dep *bool `android:"arch_variant"`
460
461 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
462 Sanitize_minimal_dep *bool `android:"arch_variant"`
463}
464
465type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500466 isSanitizerEnabled(t SanitizerType) bool
467 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900468}
469
470type snapshotLibraryDecorator struct {
471 baseSnapshotDecorator
472 *libraryDecorator
473 properties snapshotLibraryProperties
474 sanitizerProperties struct {
475 CfiEnabled bool `blueprint:"mutated"`
476
477 // Library flags for cfi variant.
478 Cfi snapshotLibraryProperties `android:"arch_variant"`
479 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900480}
481
482func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
483 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
484 return p.libraryDecorator.linkerFlags(ctx, flags)
485}
486
487func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
488 arches := config.Arches()
489 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
490 return false
491 }
492 if !p.header() && p.properties.Src == nil {
493 return false
494 }
495 return true
496}
497
498// cc modules' link functions are to link compiled objects into final binaries.
499// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
500// done by normal library decorator, e.g. exporting flags.
501func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Inseob Kimde5744a2020-12-02 13:14:28 +0900502 if p.header() {
503 return p.libraryDecorator.link(ctx, flags, deps, objs)
504 }
505
506 if p.sanitizerProperties.CfiEnabled {
507 p.properties = p.sanitizerProperties.Cfi
508 }
509
510 if !p.matchesWithDevice(ctx.DeviceConfig()) {
511 return nil
512 }
513
514 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
515 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
516 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
517
518 in := android.PathForModuleSrc(ctx, *p.properties.Src)
519 p.unstrippedOutputFile = in
520
521 if p.shared() {
522 libName := in.Base()
523 builderFlags := flagsToBuilderFlags(flags)
524
525 // Optimize out relinking against shared libraries whose interface hasn't changed by
526 // depending on a table of contents file instead of the library itself.
527 tocFile := android.PathForModuleOut(ctx, libName+".toc")
528 p.tocFile = android.OptionalPathForPath(tocFile)
529 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
530
531 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
532 SharedLibrary: in,
533 UnstrippedSharedLibrary: p.unstrippedOutputFile,
534
535 TableOfContents: p.tocFile,
536 })
537 }
538
539 if p.static() {
540 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
541 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
542 StaticLibrary: in,
543
544 TransitiveStaticLibrariesForOrdering: depSet,
545 })
546 }
547
548 p.libraryDecorator.flagExporter.setProvider(ctx)
549
550 return in
551}
552
553func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
554 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
555 p.baseInstaller.install(ctx, file)
556 }
557}
558
559func (p *snapshotLibraryDecorator) nativeCoverage() bool {
560 return false
561}
562
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500563func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900564 switch t {
565 case cfi:
566 return p.sanitizerProperties.Cfi.Src != nil
567 default:
568 return false
569 }
570}
571
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500572func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900573 if !enabled {
574 return
575 }
576 switch t {
577 case cfi:
578 p.sanitizerProperties.CfiEnabled = true
579 default:
580 return
581 }
582}
583
Colin Crosse0edaf92021-01-11 17:31:17 -0800584func snapshotLibraryFactory(snapshotSuffix, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900585 module, library := NewLibrary(android.DeviceSupported)
586
587 module.stl = nil
588 module.sanitize = nil
589 library.disableStripping()
590
591 prebuilt := &snapshotLibraryDecorator{
592 libraryDecorator: library,
593 }
594
595 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
596 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
597
598 // Prevent default system libs (libc, libm, and libdl) from being linked
599 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
600 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
601 }
602
603 module.compiler = nil
604 module.linker = prebuilt
605 module.installer = prebuilt
606
Colin Crosse0edaf92021-01-11 17:31:17 -0800607 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900608 module.AddProperties(
609 &prebuilt.properties,
610 &prebuilt.sanitizerProperties,
611 )
612
613 return module, prebuilt
614}
615
616// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
617// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
618// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
619// is set.
620func VendorSnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800621 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900622 prebuilt.libraryDecorator.BuildOnlyShared()
623 return module.Init()
624}
625
626// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
627// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
628// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
629// is set.
630func RecoverySnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800631 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900632 prebuilt.libraryDecorator.BuildOnlyShared()
633 return module.Init()
634}
635
636// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
637// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
638// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
639// is set.
640func VendorSnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800641 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900642 prebuilt.libraryDecorator.BuildOnlyStatic()
643 return module.Init()
644}
645
646// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
647// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
648// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
649// is set.
650func RecoverySnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800651 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900652 prebuilt.libraryDecorator.BuildOnlyStatic()
653 return module.Init()
654}
655
656// vendor_snapshot_header is a special header library which is auto-generated by
657// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
658// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
659// is set.
660func VendorSnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800661 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900662 prebuilt.libraryDecorator.HeaderOnly()
663 return module.Init()
664}
665
666// recovery_snapshot_header is a special header library which is auto-generated by
667// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
668// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
669// is set.
670func RecoverySnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800671 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900672 prebuilt.libraryDecorator.HeaderOnly()
673 return module.Init()
674}
675
676var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
677
678//
679// Module definitions for snapshots of executable binaries.
680//
681// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
682// binaries (e.g. toybox, sh) as their src, which can be installed.
683//
684// These modules are auto-generated by development/vendor_snapshot/update.py.
685type snapshotBinaryProperties struct {
686 // Prebuilt file for each arch.
687 Src *string `android:"arch_variant"`
688}
689
690type snapshotBinaryDecorator struct {
691 baseSnapshotDecorator
692 *binaryDecorator
Colin Crossa8890802021-01-22 14:06:33 -0800693 properties snapshotBinaryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900694}
695
696func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
697 if config.DeviceArch() != p.arch() {
698 return false
699 }
700 if p.properties.Src == nil {
701 return false
702 }
703 return true
704}
705
706// cc modules' link functions are to link compiled objects into final binaries.
707// As snapshots are prebuilts, this just returns the prebuilt binary
708func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
709 if !p.matchesWithDevice(ctx.DeviceConfig()) {
710 return nil
711 }
712
713 in := android.PathForModuleSrc(ctx, *p.properties.Src)
714 p.unstrippedOutputFile = in
715 binName := in.Base()
716
Inseob Kimde5744a2020-12-02 13:14:28 +0900717 // use cpExecutable to make it executable
718 outputFile := android.PathForModuleOut(ctx, binName)
719 ctx.Build(pctx, android.BuildParams{
720 Rule: android.CpExecutable,
721 Description: "prebuilt",
722 Output: outputFile,
723 Input: in,
724 })
725
726 return outputFile
727}
728
729func (p *snapshotBinaryDecorator) nativeCoverage() bool {
730 return false
731}
732
733// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
734// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
735// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
736func VendorSnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800737 return snapshotBinaryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900738}
739
740// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
741// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
742// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
743func RecoverySnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800744 return snapshotBinaryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900745}
746
Colin Crosse0edaf92021-01-11 17:31:17 -0800747func snapshotBinaryFactory(snapshotSuffix, moduleSuffix string) android.Module {
Inseob Kimde5744a2020-12-02 13:14:28 +0900748 module, binary := NewBinary(android.DeviceSupported)
749 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
750 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
751
752 // Prevent default system libs (libc, libm, and libdl) from being linked
753 if binary.baseLinker.Properties.System_shared_libs == nil {
754 binary.baseLinker.Properties.System_shared_libs = []string{}
755 }
756
757 prebuilt := &snapshotBinaryDecorator{
758 binaryDecorator: binary,
759 }
760
761 module.compiler = nil
762 module.sanitize = nil
763 module.stl = nil
764 module.linker = prebuilt
765
Colin Crosse0edaf92021-01-11 17:31:17 -0800766 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900767 module.AddProperties(&prebuilt.properties)
768 return module.Init()
769}
770
771//
772// Module definitions for snapshots of object files (*.o).
773//
774// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
775// files (*.o) as their src.
776//
777// These modules are auto-generated by development/vendor_snapshot/update.py.
778type vendorSnapshotObjectProperties struct {
779 // Prebuilt file for each arch.
780 Src *string `android:"arch_variant"`
781}
782
783type snapshotObjectLinker struct {
784 baseSnapshotDecorator
785 objectLinker
Colin Crossa8890802021-01-22 14:06:33 -0800786 properties vendorSnapshotObjectProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900787}
788
789func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
790 if config.DeviceArch() != p.arch() {
791 return false
792 }
793 if p.properties.Src == nil {
794 return false
795 }
796 return true
797}
798
799// cc modules' link functions are to link compiled objects into final binaries.
800// As snapshots are prebuilts, this just returns the prebuilt binary
801func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
802 if !p.matchesWithDevice(ctx.DeviceConfig()) {
803 return nil
804 }
805
Inseob Kimde5744a2020-12-02 13:14:28 +0900806 return android.PathForModuleSrc(ctx, *p.properties.Src)
807}
808
809func (p *snapshotObjectLinker) nativeCoverage() bool {
810 return false
811}
812
813// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
814// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
815// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
816func VendorSnapshotObjectFactory() android.Module {
817 module := newObject()
818
819 prebuilt := &snapshotObjectLinker{
820 objectLinker: objectLinker{
821 baseLinker: NewBaseLinker(nil),
822 },
823 }
824 module.linker = prebuilt
825
Colin Crosse0edaf92021-01-11 17:31:17 -0800826 prebuilt.init(module, vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900827 module.AddProperties(&prebuilt.properties)
828 return module.Init()
829}
830
831// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
832// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
833// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
834func RecoverySnapshotObjectFactory() android.Module {
835 module := newObject()
836
837 prebuilt := &snapshotObjectLinker{
838 objectLinker: objectLinker{
839 baseLinker: NewBaseLinker(nil),
840 },
841 }
842 module.linker = prebuilt
843
Colin Crosse0edaf92021-01-11 17:31:17 -0800844 prebuilt.init(module, recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900845 module.AddProperties(&prebuilt.properties)
846 return module.Init()
847}
848
849type snapshotInterface interface {
850 matchesWithDevice(config android.DeviceConfig) bool
Colin Crossa8890802021-01-22 14:06:33 -0800851 isSnapshotPrebuilt() bool
852 version() string
853 snapshotAndroidMkSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +0900854}
855
856var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
857var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
858var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
859var _ snapshotInterface = (*snapshotObjectLinker)(nil)