blob: b82628ccb61a3e9b0de49bcb69c70b3b5a3d6dea [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 {
198 // directed recovery snapshot is not implemented yet
199 return false
200}
201
Colin Crosse0edaf92021-01-11 17:31:17 -0800202func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
203 return android.RecoveryVariation
204}
205
206func (recoverySnapshotImage) moduleNameSuffix() string {
207 return recoverySuffix
208}
209
Inseob Kimde5744a2020-12-02 13:14:28 +0900210var vendorSnapshotImageSingleton vendorSnapshotImage
211var recoverySnapshotImageSingleton recoverySnapshotImage
212
213func init() {
Colin Crosse0edaf92021-01-11 17:31:17 -0800214 vendorSnapshotImageSingleton.init(android.InitRegistrationContext)
215 recoverySnapshotImageSingleton.init(android.InitRegistrationContext)
Inseob Kimde5744a2020-12-02 13:14:28 +0900216}
217
218const (
Colin Crosse0edaf92021-01-11 17:31:17 -0800219 snapshotHeaderSuffix = "_header."
220 snapshotSharedSuffix = "_shared."
221 snapshotStaticSuffix = "_static."
222 snapshotBinarySuffix = "_binary."
223 snapshotObjectSuffix = "_object."
Inseob Kimde5744a2020-12-02 13:14:28 +0900224)
225
Colin Crosse0edaf92021-01-11 17:31:17 -0800226type SnapshotProperties struct {
227 Header_libs []string `android:"arch_variant"`
228 Static_libs []string `android:"arch_variant"`
229 Shared_libs []string `android:"arch_variant"`
230 Vndk_libs []string `android:"arch_variant"`
231 Binaries []string `android:"arch_variant"`
232 Objects []string `android:"arch_variant"`
233}
234
235type snapshot struct {
236 android.ModuleBase
237
238 properties SnapshotProperties
239
240 baseSnapshot baseSnapshotDecorator
241
242 image snapshotImage
243}
244
245func (s *snapshot) ImageMutatorBegin(ctx android.BaseModuleContext) {
246 cfg := ctx.DeviceConfig()
247 if !s.image.isUsingSnapshot(cfg) || s.image.targetSnapshotVersion(cfg) != s.baseSnapshot.version() {
248 s.Disable()
249 }
250}
251
252func (s *snapshot) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
253 return false
254}
255
256func (s *snapshot) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
257 return false
258}
259
260func (s *snapshot) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
261 return false
262}
263
264func (s *snapshot) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
265 return false
266}
267
268func (s *snapshot) ExtraImageVariations(ctx android.BaseModuleContext) []string {
269 return []string{s.image.imageVariantName(ctx.DeviceConfig())}
270}
271
272func (s *snapshot) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
273}
274
275func (s *snapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
276 // Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
277}
278
279func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
280 collectSnapshotMap := func(variations []blueprint.Variation, depTag blueprint.DependencyTag,
281 names []string, snapshotSuffix, moduleSuffix string) map[string]string {
282
283 decoratedNames := make([]string, 0, len(names))
284 for _, name := range names {
285 decoratedNames = append(decoratedNames, name+
286 snapshotSuffix+moduleSuffix+
287 s.baseSnapshot.version()+
288 "."+ctx.Arch().ArchType.Name)
289 }
290
291 deps := ctx.AddVariationDependencies(variations, depTag, decoratedNames...)
292 snapshotMap := make(map[string]string)
293 for _, dep := range deps {
294 if dep == nil {
295 continue
296 }
297
298 snapshotMap[dep.(*Module).BaseModuleName()] = ctx.OtherModuleName(dep)
299 }
300 return snapshotMap
301 }
302
303 snapshotSuffix := s.image.moduleNameSuffix()
304 headers := collectSnapshotMap(nil, HeaderDepTag(), s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
305 binaries := collectSnapshotMap(nil, nil, s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
306 objects := collectSnapshotMap(nil, nil, s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
307
308 staticLibs := collectSnapshotMap([]blueprint.Variation{
309 {Mutator: "link", Variation: "static"},
310 }, StaticDepTag(), s.properties.Static_libs, snapshotSuffix, snapshotStaticSuffix)
311
312 sharedLibs := collectSnapshotMap([]blueprint.Variation{
313 {Mutator: "link", Variation: "shared"},
314 }, SharedDepTag(), s.properties.Shared_libs, snapshotSuffix, snapshotSharedSuffix)
315
316 vndkLibs := collectSnapshotMap([]blueprint.Variation{
317 {Mutator: "link", Variation: "shared"},
318 }, SharedDepTag(), s.properties.Vndk_libs, "", vndkSuffix)
319
320 for k, v := range vndkLibs {
321 sharedLibs[k] = v
322 }
323 ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
324 HeaderLibs: headers,
325 Binaries: binaries,
326 Objects: objects,
327 StaticLibs: staticLibs,
328 SharedLibs: sharedLibs,
329 })
330}
331
332type SnapshotInfo struct {
333 HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs map[string]string
334}
335
336var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
337
338var _ android.ImageInterface = (*snapshot)(nil)
339
340func vendorSnapshotFactory() android.Module {
341 return snapshotFactory(vendorSnapshotImageSingleton)
342}
343
344func recoverySnapshotFactory() android.Module {
345 return snapshotFactory(recoverySnapshotImageSingleton)
346}
347
348func snapshotFactory(image snapshotImage) android.Module {
349 snapshot := &snapshot{}
350 snapshot.image = image
351 snapshot.AddProperties(
352 &snapshot.properties,
353 &snapshot.baseSnapshot.baseProperties)
354 android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
355 return snapshot
356}
357
Inseob Kimde5744a2020-12-02 13:14:28 +0900358type baseSnapshotDecoratorProperties struct {
359 // snapshot version.
360 Version string
361
362 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
363 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800364
Colin Crossa8890802021-01-22 14:06:33 -0800365 // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
366 Androidmk_suffix string
367
Jose Galmes6f843bc2020-12-11 13:36:29 -0800368 // Suffix to be added to the module name, e.g., vendor_shared,
369 // recovery_shared, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800370 ModuleSuffix string `blueprint:"mutated"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900371}
372
373// baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
374// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
375// collide with source modules. e.g. the following example module,
376//
377// vendor_snapshot_static {
378// name: "libbase",
379// arch: "arm64",
380// version: 30,
381// ...
382// }
383//
384// will be seen as "libbase.vendor_static.30.arm64" by Soong.
385type baseSnapshotDecorator struct {
386 baseProperties baseSnapshotDecoratorProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900387}
388
389func (p *baseSnapshotDecorator) Name(name string) string {
390 return name + p.NameSuffix()
391}
392
393func (p *baseSnapshotDecorator) NameSuffix() string {
394 versionSuffix := p.version()
395 if p.arch() != "" {
396 versionSuffix += "." + p.arch()
397 }
398
Colin Crosse0edaf92021-01-11 17:31:17 -0800399 return p.baseProperties.ModuleSuffix + versionSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900400}
401
402func (p *baseSnapshotDecorator) version() string {
403 return p.baseProperties.Version
404}
405
406func (p *baseSnapshotDecorator) arch() string {
407 return p.baseProperties.Target_arch
408}
409
Colin Crosse0edaf92021-01-11 17:31:17 -0800410func (p *baseSnapshotDecorator) moduleSuffix() string {
411 return p.baseProperties.ModuleSuffix
Jose Galmes6f843bc2020-12-11 13:36:29 -0800412}
413
Inseob Kimde5744a2020-12-02 13:14:28 +0900414func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
415 return true
416}
417
Colin Crossa8890802021-01-22 14:06:33 -0800418func (p *baseSnapshotDecorator) snapshotAndroidMkSuffix() string {
419 return p.baseProperties.Androidmk_suffix
420}
421
Inseob Kimde5744a2020-12-02 13:14:28 +0900422// Call this with a module suffix after creating a snapshot module, such as
423// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800424func (p *baseSnapshotDecorator) init(m *Module, snapshotSuffix, moduleSuffix string) {
425 p.baseProperties.ModuleSuffix = snapshotSuffix + moduleSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900426 m.AddProperties(&p.baseProperties)
427 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
428 vendorSnapshotLoadHook(ctx, p)
429 })
430}
431
432// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
433// As vendor snapshot is only for vendor, such modules won't be used at all.
434func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) {
435 if p.version() != ctx.DeviceConfig().VndkVersion() {
436 ctx.Module().Disable()
437 return
438 }
439}
440
441//
442// Module definitions for snapshots of libraries (shared, static, header).
443//
444// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
445// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
446// which can be installed or linked against. Also they export flags needed when linked, such as
447// include directories, c flags, sanitize dependency information, etc.
448//
449// These modules are auto-generated by development/vendor_snapshot/update.py.
450type snapshotLibraryProperties struct {
451 // Prebuilt file for each arch.
452 Src *string `android:"arch_variant"`
453
454 // list of directories that will be added to the include path (using -I).
455 Export_include_dirs []string `android:"arch_variant"`
456
457 // list of directories that will be added to the system path (using -isystem).
458 Export_system_include_dirs []string `android:"arch_variant"`
459
460 // list of flags that will be used for any module that links against this module.
461 Export_flags []string `android:"arch_variant"`
462
463 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
464 Sanitize_ubsan_dep *bool `android:"arch_variant"`
465
466 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
467 Sanitize_minimal_dep *bool `android:"arch_variant"`
468}
469
470type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500471 isSanitizerEnabled(t SanitizerType) bool
472 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900473}
474
475type snapshotLibraryDecorator struct {
476 baseSnapshotDecorator
477 *libraryDecorator
478 properties snapshotLibraryProperties
479 sanitizerProperties struct {
480 CfiEnabled bool `blueprint:"mutated"`
481
482 // Library flags for cfi variant.
483 Cfi snapshotLibraryProperties `android:"arch_variant"`
484 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900485}
486
487func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
488 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
489 return p.libraryDecorator.linkerFlags(ctx, flags)
490}
491
492func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
493 arches := config.Arches()
494 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
495 return false
496 }
497 if !p.header() && p.properties.Src == nil {
498 return false
499 }
500 return true
501}
502
503// cc modules' link functions are to link compiled objects into final binaries.
504// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
505// done by normal library decorator, e.g. exporting flags.
506func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Inseob Kimde5744a2020-12-02 13:14:28 +0900507 if p.header() {
508 return p.libraryDecorator.link(ctx, flags, deps, objs)
509 }
510
511 if p.sanitizerProperties.CfiEnabled {
512 p.properties = p.sanitizerProperties.Cfi
513 }
514
515 if !p.matchesWithDevice(ctx.DeviceConfig()) {
516 return nil
517 }
518
519 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
520 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
521 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
522
523 in := android.PathForModuleSrc(ctx, *p.properties.Src)
524 p.unstrippedOutputFile = in
525
526 if p.shared() {
527 libName := in.Base()
528 builderFlags := flagsToBuilderFlags(flags)
529
530 // Optimize out relinking against shared libraries whose interface hasn't changed by
531 // depending on a table of contents file instead of the library itself.
532 tocFile := android.PathForModuleOut(ctx, libName+".toc")
533 p.tocFile = android.OptionalPathForPath(tocFile)
534 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
535
536 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
537 SharedLibrary: in,
538 UnstrippedSharedLibrary: p.unstrippedOutputFile,
539
540 TableOfContents: p.tocFile,
541 })
542 }
543
544 if p.static() {
545 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
546 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
547 StaticLibrary: in,
548
549 TransitiveStaticLibrariesForOrdering: depSet,
550 })
551 }
552
553 p.libraryDecorator.flagExporter.setProvider(ctx)
554
555 return in
556}
557
558func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
559 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
560 p.baseInstaller.install(ctx, file)
561 }
562}
563
564func (p *snapshotLibraryDecorator) nativeCoverage() bool {
565 return false
566}
567
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500568func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900569 switch t {
570 case cfi:
571 return p.sanitizerProperties.Cfi.Src != nil
572 default:
573 return false
574 }
575}
576
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500577func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900578 if !enabled {
579 return
580 }
581 switch t {
582 case cfi:
583 p.sanitizerProperties.CfiEnabled = true
584 default:
585 return
586 }
587}
588
Colin Crosse0edaf92021-01-11 17:31:17 -0800589func snapshotLibraryFactory(snapshotSuffix, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900590 module, library := NewLibrary(android.DeviceSupported)
591
592 module.stl = nil
593 module.sanitize = nil
594 library.disableStripping()
595
596 prebuilt := &snapshotLibraryDecorator{
597 libraryDecorator: library,
598 }
599
600 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
601 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
602
603 // Prevent default system libs (libc, libm, and libdl) from being linked
604 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
605 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
606 }
607
608 module.compiler = nil
609 module.linker = prebuilt
610 module.installer = prebuilt
611
Colin Crosse0edaf92021-01-11 17:31:17 -0800612 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900613 module.AddProperties(
614 &prebuilt.properties,
615 &prebuilt.sanitizerProperties,
616 )
617
618 return module, prebuilt
619}
620
621// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
622// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
623// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
624// is set.
625func VendorSnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800626 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900627 prebuilt.libraryDecorator.BuildOnlyShared()
628 return module.Init()
629}
630
631// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
632// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
633// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
634// is set.
635func RecoverySnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800636 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900637 prebuilt.libraryDecorator.BuildOnlyShared()
638 return module.Init()
639}
640
641// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
642// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
643// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
644// is set.
645func VendorSnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800646 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900647 prebuilt.libraryDecorator.BuildOnlyStatic()
648 return module.Init()
649}
650
651// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
652// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
653// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
654// is set.
655func RecoverySnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800656 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900657 prebuilt.libraryDecorator.BuildOnlyStatic()
658 return module.Init()
659}
660
661// vendor_snapshot_header is a special header library which is auto-generated by
662// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
663// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
664// is set.
665func VendorSnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800666 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900667 prebuilt.libraryDecorator.HeaderOnly()
668 return module.Init()
669}
670
671// recovery_snapshot_header is a special header library which is auto-generated by
672// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
673// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
674// is set.
675func RecoverySnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800676 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900677 prebuilt.libraryDecorator.HeaderOnly()
678 return module.Init()
679}
680
681var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
682
683//
684// Module definitions for snapshots of executable binaries.
685//
686// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
687// binaries (e.g. toybox, sh) as their src, which can be installed.
688//
689// These modules are auto-generated by development/vendor_snapshot/update.py.
690type snapshotBinaryProperties struct {
691 // Prebuilt file for each arch.
692 Src *string `android:"arch_variant"`
693}
694
695type snapshotBinaryDecorator struct {
696 baseSnapshotDecorator
697 *binaryDecorator
Colin Crossa8890802021-01-22 14:06:33 -0800698 properties snapshotBinaryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900699}
700
701func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
702 if config.DeviceArch() != p.arch() {
703 return false
704 }
705 if p.properties.Src == nil {
706 return false
707 }
708 return true
709}
710
711// cc modules' link functions are to link compiled objects into final binaries.
712// As snapshots are prebuilts, this just returns the prebuilt binary
713func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
714 if !p.matchesWithDevice(ctx.DeviceConfig()) {
715 return nil
716 }
717
718 in := android.PathForModuleSrc(ctx, *p.properties.Src)
719 p.unstrippedOutputFile = in
720 binName := in.Base()
721
Inseob Kimde5744a2020-12-02 13:14:28 +0900722 // use cpExecutable to make it executable
723 outputFile := android.PathForModuleOut(ctx, binName)
724 ctx.Build(pctx, android.BuildParams{
725 Rule: android.CpExecutable,
726 Description: "prebuilt",
727 Output: outputFile,
728 Input: in,
729 })
730
731 return outputFile
732}
733
734func (p *snapshotBinaryDecorator) nativeCoverage() bool {
735 return false
736}
737
738// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
739// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
740// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
741func VendorSnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800742 return snapshotBinaryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900743}
744
745// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
746// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
747// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
748func RecoverySnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800749 return snapshotBinaryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900750}
751
Colin Crosse0edaf92021-01-11 17:31:17 -0800752func snapshotBinaryFactory(snapshotSuffix, moduleSuffix string) android.Module {
Inseob Kimde5744a2020-12-02 13:14:28 +0900753 module, binary := NewBinary(android.DeviceSupported)
754 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
755 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
756
757 // Prevent default system libs (libc, libm, and libdl) from being linked
758 if binary.baseLinker.Properties.System_shared_libs == nil {
759 binary.baseLinker.Properties.System_shared_libs = []string{}
760 }
761
762 prebuilt := &snapshotBinaryDecorator{
763 binaryDecorator: binary,
764 }
765
766 module.compiler = nil
767 module.sanitize = nil
768 module.stl = nil
769 module.linker = prebuilt
770
Colin Crosse0edaf92021-01-11 17:31:17 -0800771 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900772 module.AddProperties(&prebuilt.properties)
773 return module.Init()
774}
775
776//
777// Module definitions for snapshots of object files (*.o).
778//
779// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
780// files (*.o) as their src.
781//
782// These modules are auto-generated by development/vendor_snapshot/update.py.
783type vendorSnapshotObjectProperties struct {
784 // Prebuilt file for each arch.
785 Src *string `android:"arch_variant"`
786}
787
788type snapshotObjectLinker struct {
789 baseSnapshotDecorator
790 objectLinker
Colin Crossa8890802021-01-22 14:06:33 -0800791 properties vendorSnapshotObjectProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900792}
793
794func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
795 if config.DeviceArch() != p.arch() {
796 return false
797 }
798 if p.properties.Src == nil {
799 return false
800 }
801 return true
802}
803
804// cc modules' link functions are to link compiled objects into final binaries.
805// As snapshots are prebuilts, this just returns the prebuilt binary
806func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
807 if !p.matchesWithDevice(ctx.DeviceConfig()) {
808 return nil
809 }
810
Inseob Kimde5744a2020-12-02 13:14:28 +0900811 return android.PathForModuleSrc(ctx, *p.properties.Src)
812}
813
814func (p *snapshotObjectLinker) nativeCoverage() bool {
815 return false
816}
817
818// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
819// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
820// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
821func VendorSnapshotObjectFactory() android.Module {
822 module := newObject()
823
824 prebuilt := &snapshotObjectLinker{
825 objectLinker: objectLinker{
826 baseLinker: NewBaseLinker(nil),
827 },
828 }
829 module.linker = prebuilt
830
Colin Crosse0edaf92021-01-11 17:31:17 -0800831 prebuilt.init(module, vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900832 module.AddProperties(&prebuilt.properties)
833 return module.Init()
834}
835
836// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
837// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
838// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
839func RecoverySnapshotObjectFactory() android.Module {
840 module := newObject()
841
842 prebuilt := &snapshotObjectLinker{
843 objectLinker: objectLinker{
844 baseLinker: NewBaseLinker(nil),
845 },
846 }
847 module.linker = prebuilt
848
Colin Crosse0edaf92021-01-11 17:31:17 -0800849 prebuilt.init(module, recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900850 module.AddProperties(&prebuilt.properties)
851 return module.Init()
852}
853
854type snapshotInterface interface {
855 matchesWithDevice(config android.DeviceConfig) bool
Colin Crossa8890802021-01-22 14:06:33 -0800856 isSnapshotPrebuilt() bool
857 version() string
858 snapshotAndroidMkSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +0900859}
860
861var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
862var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
863var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
864var _ snapshotInterface = (*snapshotObjectLinker)(nil)