blob: bbb889644882f224672a2228cb622dcea842d927 [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 (
Justin DeMartino383bfb32021-02-24 10:49:43 -080021 "path/filepath"
Inseob Kimde5744a2020-12-02 13:14:28 +090022 "strings"
Inseob Kimde5744a2020-12-02 13:14:28 +090023
24 "android/soong/android"
Jose Galmes6f843bc2020-12-11 13:36:29 -080025
Colin Crosse0edaf92021-01-11 17:31:17 -080026 "github.com/google/blueprint"
Inseob Kimde5744a2020-12-02 13:14:28 +090027)
28
29// Defines the specifics of different images to which the snapshot process is applicable, e.g.,
30// vendor, recovery, ramdisk.
31type snapshotImage interface {
Jose Galmes6f843bc2020-12-11 13:36:29 -080032 // Returns true if a snapshot should be generated for this image.
33 shouldGenerateSnapshot(ctx android.SingletonContext) bool
34
Inseob Kimde5744a2020-12-02 13:14:28 +090035 // Function that returns true if the module is included in this image.
36 // Using a function return instead of a value to prevent early
37 // evalution of a function that may be not be defined.
38 inImage(m *Module) func() bool
39
Justin Yune09ac172021-01-20 19:49:01 +090040 // Returns true if the module is private and must not be included in the
41 // snapshot. For example VNDK-private modules must return true for the
42 // vendor snapshots. But false for the recovery snapshots.
43 private(m *Module) bool
Inseob Kimde5744a2020-12-02 13:14:28 +090044
45 // Returns true if a dir under source tree is an SoC-owned proprietary
46 // directory, such as device/, vendor/, etc.
47 //
48 // For a given snapshot (e.g., vendor, recovery, etc.) if
Justin DeMartino383bfb32021-02-24 10:49:43 -080049 // isProprietaryPath(dir, deviceConfig) returns true, then the module in dir
50 // will be built from sources.
51 isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool
Inseob Kimde5744a2020-12-02 13:14:28 +090052
53 // Whether to include VNDK in the snapshot for this image.
54 includeVndk() bool
55
56 // Whether a given module has been explicitly excluded from the
57 // snapshot, e.g., using the exclude_from_vendor_snapshot or
58 // exclude_from_recovery_snapshot properties.
59 excludeFromSnapshot(m *Module) bool
Jose Galmes6f843bc2020-12-11 13:36:29 -080060
Jose Galmes6f843bc2020-12-11 13:36:29 -080061 // Returns true if the build is using a snapshot for this image.
62 isUsingSnapshot(cfg android.DeviceConfig) bool
63
Colin Crosse0edaf92021-01-11 17:31:17 -080064 // Returns a version of which the snapshot should be used in this target.
65 // This will only be meaningful when isUsingSnapshot is true.
66 targetSnapshotVersion(cfg android.DeviceConfig) string
Inseob Kim7cf14652021-01-06 23:06:52 +090067
68 // Whether to exclude a given module from the directed snapshot or not.
69 // If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on,
70 // and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured.
71 excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool
Colin Crosse0edaf92021-01-11 17:31:17 -080072
73 // The image variant name for this snapshot image.
74 // For example, recovery snapshot image will return "recovery", and vendor snapshot image will
75 // return "vendor." + version.
76 imageVariantName(cfg android.DeviceConfig) string
77
78 // The variant suffix for snapshot modules. For example, vendor snapshot modules will have
79 // ".vendor" as their suffix.
80 moduleNameSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +090081}
82
83type vendorSnapshotImage struct{}
84type recoverySnapshotImage struct{}
85
Justin DeMartino383bfb32021-02-24 10:49:43 -080086type directoryMap map[string]bool
87
88var (
89 // Modules under following directories are ignored. They are OEM's and vendor's
90 // proprietary modules(device/, kernel/, vendor/, and hardware/).
91 defaultDirectoryExcludedMap = directoryMap{
92 "device": true,
93 "hardware": true,
94 "kernel": true,
95 "vendor": true,
96 }
97
98 // Modules under following directories are included as they are in AOSP,
99 // although hardware/ and kernel/ are normally for vendor's own.
100 defaultDirectoryIncludedMap = directoryMap{
101 "kernel/configs": true,
102 "kernel/prebuilts": true,
103 "kernel/tests": true,
104 "hardware/interfaces": true,
105 "hardware/libhardware": true,
106 "hardware/libhardware_legacy": true,
107 "hardware/ril": true,
108 }
109)
110
Colin Crosse0edaf92021-01-11 17:31:17 -0800111func (vendorSnapshotImage) init(ctx android.RegistrationContext) {
112 ctx.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
113 ctx.RegisterModuleType("vendor_snapshot", vendorSnapshotFactory)
114 ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
115 ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
116 ctx.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
117 ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
118 ctx.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
Inseob Kime9aec6a2021-01-05 20:03:22 +0900119
Colin Crosse0edaf92021-01-11 17:31:17 -0800120 ctx.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
Inseob Kimde5744a2020-12-02 13:14:28 +0900121}
122
Jose Galmes6f843bc2020-12-11 13:36:29 -0800123func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
124 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot.
125 return ctx.DeviceConfig().VndkVersion() == "current"
126}
127
Inseob Kimde5744a2020-12-02 13:14:28 +0900128func (vendorSnapshotImage) inImage(m *Module) func() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500129 return m.InVendor
Inseob Kimde5744a2020-12-02 13:14:28 +0900130}
131
Justin Yune09ac172021-01-20 19:49:01 +0900132func (vendorSnapshotImage) private(m *Module) bool {
133 return m.IsVndkPrivate()
Inseob Kimde5744a2020-12-02 13:14:28 +0900134}
135
Justin DeMartino383bfb32021-02-24 10:49:43 -0800136func isDirectoryExcluded(dir string, excludedMap directoryMap, includedMap directoryMap) bool {
137 if dir == "." || dir == "/" {
138 return false
139 }
140 if includedMap[dir] {
141 return false
142 } else if excludedMap[dir] {
143 return true
144 } else if defaultDirectoryIncludedMap[dir] {
145 return false
146 } else if defaultDirectoryExcludedMap[dir] {
147 return true
148 } else {
149 return isDirectoryExcluded(filepath.Dir(dir), excludedMap, includedMap)
150 }
151}
152
153func (vendorSnapshotImage) isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
154 return isDirectoryExcluded(dir, deviceConfig.VendorSnapshotDirsExcludedMap(), deviceConfig.VendorSnapshotDirsIncludedMap())
Inseob Kimde5744a2020-12-02 13:14:28 +0900155}
156
157// vendor snapshot includes static/header libraries with vndk: {enabled: true}.
158func (vendorSnapshotImage) includeVndk() bool {
159 return true
160}
161
162func (vendorSnapshotImage) excludeFromSnapshot(m *Module) bool {
163 return m.ExcludeFromVendorSnapshot()
164}
165
Jose Galmes6f843bc2020-12-11 13:36:29 -0800166func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
167 vndkVersion := cfg.VndkVersion()
168 return vndkVersion != "current" && vndkVersion != ""
169}
170
Colin Crosse0edaf92021-01-11 17:31:17 -0800171func (vendorSnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
172 return cfg.VndkVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800173}
174
Inseob Kim7cf14652021-01-06 23:06:52 +0900175// returns true iff a given module SHOULD BE EXCLUDED, false if included
176func (vendorSnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
177 // If we're using full snapshot, not directed snapshot, capture every module
178 if !cfg.DirectedVendorSnapshot() {
179 return false
180 }
181 // Else, checks if name is in VENDOR_SNAPSHOT_MODULES.
182 return !cfg.VendorSnapshotModules()[name]
183}
184
Colin Crosse0edaf92021-01-11 17:31:17 -0800185func (vendorSnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
186 return VendorVariationPrefix + cfg.VndkVersion()
187}
188
189func (vendorSnapshotImage) moduleNameSuffix() string {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500190 return VendorSuffix
Colin Crosse0edaf92021-01-11 17:31:17 -0800191}
192
193func (recoverySnapshotImage) init(ctx android.RegistrationContext) {
194 ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
195 ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
196 ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
197 ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
198 ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
199 ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
200 ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
Inseob Kimde5744a2020-12-02 13:14:28 +0900201}
202
Jose Galmes6f843bc2020-12-11 13:36:29 -0800203func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
204 // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a
205 // snapshot.
206 return ctx.DeviceConfig().RecoverySnapshotVersion() == "current"
207}
208
Inseob Kimde5744a2020-12-02 13:14:28 +0900209func (recoverySnapshotImage) inImage(m *Module) func() bool {
210 return m.InRecovery
211}
212
Justin Yune09ac172021-01-20 19:49:01 +0900213// recovery snapshot does not have private libraries.
214func (recoverySnapshotImage) private(m *Module) bool {
215 return false
Inseob Kimde5744a2020-12-02 13:14:28 +0900216}
217
Justin DeMartino383bfb32021-02-24 10:49:43 -0800218func (recoverySnapshotImage) isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
219 return isDirectoryExcluded(dir, deviceConfig.RecoverySnapshotDirsExcludedMap(), deviceConfig.RecoverySnapshotDirsIncludedMap())
Inseob Kimde5744a2020-12-02 13:14:28 +0900220}
221
222// recovery snapshot does NOT treat vndk specially.
223func (recoverySnapshotImage) includeVndk() bool {
224 return false
225}
226
227func (recoverySnapshotImage) excludeFromSnapshot(m *Module) bool {
228 return m.ExcludeFromRecoverySnapshot()
229}
230
Jose Galmes6f843bc2020-12-11 13:36:29 -0800231func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
232 recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
233 return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
234}
235
Colin Crosse0edaf92021-01-11 17:31:17 -0800236func (recoverySnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
237 return cfg.RecoverySnapshotVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800238}
239
Inseob Kim7cf14652021-01-06 23:06:52 +0900240func (recoverySnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
Jose Galmes4c6895e2021-02-09 07:44:30 -0800241 // If we're using full snapshot, not directed snapshot, capture every module
242 if !cfg.DirectedRecoverySnapshot() {
243 return false
244 }
245 // Else, checks if name is in RECOVERY_SNAPSHOT_MODULES.
246 return !cfg.RecoverySnapshotModules()[name]
Inseob Kim7cf14652021-01-06 23:06:52 +0900247}
248
Colin Crosse0edaf92021-01-11 17:31:17 -0800249func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
250 return android.RecoveryVariation
251}
252
253func (recoverySnapshotImage) moduleNameSuffix() string {
254 return recoverySuffix
255}
256
Inseob Kimde5744a2020-12-02 13:14:28 +0900257var vendorSnapshotImageSingleton vendorSnapshotImage
258var recoverySnapshotImageSingleton recoverySnapshotImage
259
260func init() {
Colin Crosse0edaf92021-01-11 17:31:17 -0800261 vendorSnapshotImageSingleton.init(android.InitRegistrationContext)
262 recoverySnapshotImageSingleton.init(android.InitRegistrationContext)
Inseob Kimde5744a2020-12-02 13:14:28 +0900263}
264
265const (
Colin Crosse0edaf92021-01-11 17:31:17 -0800266 snapshotHeaderSuffix = "_header."
267 snapshotSharedSuffix = "_shared."
268 snapshotStaticSuffix = "_static."
269 snapshotBinarySuffix = "_binary."
270 snapshotObjectSuffix = "_object."
Inseob Kimde5744a2020-12-02 13:14:28 +0900271)
272
Colin Crosse0edaf92021-01-11 17:31:17 -0800273type SnapshotProperties struct {
274 Header_libs []string `android:"arch_variant"`
275 Static_libs []string `android:"arch_variant"`
276 Shared_libs []string `android:"arch_variant"`
277 Vndk_libs []string `android:"arch_variant"`
278 Binaries []string `android:"arch_variant"`
279 Objects []string `android:"arch_variant"`
280}
281
282type snapshot struct {
283 android.ModuleBase
284
285 properties SnapshotProperties
286
287 baseSnapshot baseSnapshotDecorator
288
289 image snapshotImage
290}
291
292func (s *snapshot) ImageMutatorBegin(ctx android.BaseModuleContext) {
293 cfg := ctx.DeviceConfig()
294 if !s.image.isUsingSnapshot(cfg) || s.image.targetSnapshotVersion(cfg) != s.baseSnapshot.version() {
295 s.Disable()
296 }
297}
298
299func (s *snapshot) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
300 return false
301}
302
303func (s *snapshot) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
304 return false
305}
306
307func (s *snapshot) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
308 return false
309}
310
311func (s *snapshot) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
312 return false
313}
314
315func (s *snapshot) ExtraImageVariations(ctx android.BaseModuleContext) []string {
316 return []string{s.image.imageVariantName(ctx.DeviceConfig())}
317}
318
319func (s *snapshot) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
320}
321
322func (s *snapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
323 // Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
324}
325
Justin Yun07b9f862021-02-26 14:00:03 +0900326func getSnapshotNameSuffix(moduleSuffix, version, arch string) string {
327 versionSuffix := version
328 if arch != "" {
329 versionSuffix += "." + arch
330 }
331 return moduleSuffix + versionSuffix
332}
Colin Crosse0edaf92021-01-11 17:31:17 -0800333
Justin Yun07b9f862021-02-26 14:00:03 +0900334func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
335 collectSnapshotMap := func(names []string, snapshotSuffix, moduleSuffix string) map[string]string {
Colin Crosse0edaf92021-01-11 17:31:17 -0800336 snapshotMap := make(map[string]string)
Justin Yun48138672021-02-25 18:21:27 +0900337 for _, name := range names {
338 snapshotMap[name] = name +
Justin Yun07b9f862021-02-26 14:00:03 +0900339 getSnapshotNameSuffix(snapshotSuffix+moduleSuffix,
340 s.baseSnapshot.version(), ctx.Arch().ArchType.Name)
Colin Crosse0edaf92021-01-11 17:31:17 -0800341 }
342 return snapshotMap
343 }
344
345 snapshotSuffix := s.image.moduleNameSuffix()
Justin Yun07b9f862021-02-26 14:00:03 +0900346 headers := collectSnapshotMap(s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
347 binaries := collectSnapshotMap(s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
348 objects := collectSnapshotMap(s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
349 staticLibs := collectSnapshotMap(s.properties.Static_libs, snapshotSuffix, snapshotStaticSuffix)
350 sharedLibs := collectSnapshotMap(s.properties.Shared_libs, snapshotSuffix, snapshotSharedSuffix)
351 vndkLibs := collectSnapshotMap(s.properties.Vndk_libs, "", vndkSuffix)
Colin Crosse0edaf92021-01-11 17:31:17 -0800352 for k, v := range vndkLibs {
353 sharedLibs[k] = v
354 }
Justin Yun07b9f862021-02-26 14:00:03 +0900355
Colin Crosse0edaf92021-01-11 17:31:17 -0800356 ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
357 HeaderLibs: headers,
358 Binaries: binaries,
359 Objects: objects,
360 StaticLibs: staticLibs,
361 SharedLibs: sharedLibs,
362 })
363}
364
365type SnapshotInfo struct {
366 HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs map[string]string
367}
368
369var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
370
371var _ android.ImageInterface = (*snapshot)(nil)
372
373func vendorSnapshotFactory() android.Module {
374 return snapshotFactory(vendorSnapshotImageSingleton)
375}
376
377func recoverySnapshotFactory() android.Module {
378 return snapshotFactory(recoverySnapshotImageSingleton)
379}
380
381func snapshotFactory(image snapshotImage) android.Module {
382 snapshot := &snapshot{}
383 snapshot.image = image
384 snapshot.AddProperties(
385 &snapshot.properties,
386 &snapshot.baseSnapshot.baseProperties)
387 android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
388 return snapshot
389}
390
Inseob Kimde5744a2020-12-02 13:14:28 +0900391type baseSnapshotDecoratorProperties struct {
392 // snapshot version.
393 Version string
394
395 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
396 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800397
Colin Crossa8890802021-01-22 14:06:33 -0800398 // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
399 Androidmk_suffix string
400
Jose Galmes6f843bc2020-12-11 13:36:29 -0800401 // Suffix to be added to the module name, e.g., vendor_shared,
402 // recovery_shared, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800403 ModuleSuffix string `blueprint:"mutated"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900404}
405
406// baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
407// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
408// collide with source modules. e.g. the following example module,
409//
410// vendor_snapshot_static {
411// name: "libbase",
412// arch: "arm64",
413// version: 30,
414// ...
415// }
416//
417// will be seen as "libbase.vendor_static.30.arm64" by Soong.
418type baseSnapshotDecorator struct {
419 baseProperties baseSnapshotDecoratorProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900420}
421
422func (p *baseSnapshotDecorator) Name(name string) string {
423 return name + p.NameSuffix()
424}
425
426func (p *baseSnapshotDecorator) NameSuffix() string {
Justin Yun07b9f862021-02-26 14:00:03 +0900427 return getSnapshotNameSuffix(p.moduleSuffix(), p.version(), p.arch())
Inseob Kimde5744a2020-12-02 13:14:28 +0900428}
429
430func (p *baseSnapshotDecorator) version() string {
431 return p.baseProperties.Version
432}
433
434func (p *baseSnapshotDecorator) arch() string {
435 return p.baseProperties.Target_arch
436}
437
Colin Crosse0edaf92021-01-11 17:31:17 -0800438func (p *baseSnapshotDecorator) moduleSuffix() string {
439 return p.baseProperties.ModuleSuffix
Jose Galmes6f843bc2020-12-11 13:36:29 -0800440}
441
Inseob Kimde5744a2020-12-02 13:14:28 +0900442func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
443 return true
444}
445
Colin Crossa8890802021-01-22 14:06:33 -0800446func (p *baseSnapshotDecorator) snapshotAndroidMkSuffix() string {
447 return p.baseProperties.Androidmk_suffix
448}
449
Inseob Kimde5744a2020-12-02 13:14:28 +0900450// Call this with a module suffix after creating a snapshot module, such as
451// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800452func (p *baseSnapshotDecorator) init(m *Module, snapshotSuffix, moduleSuffix string) {
453 p.baseProperties.ModuleSuffix = snapshotSuffix + moduleSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900454 m.AddProperties(&p.baseProperties)
455 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
456 vendorSnapshotLoadHook(ctx, p)
457 })
458}
459
460// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
461// As vendor snapshot is only for vendor, such modules won't be used at all.
462func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) {
463 if p.version() != ctx.DeviceConfig().VndkVersion() {
464 ctx.Module().Disable()
465 return
466 }
467}
468
469//
470// Module definitions for snapshots of libraries (shared, static, header).
471//
472// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
473// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
474// which can be installed or linked against. Also they export flags needed when linked, such as
475// include directories, c flags, sanitize dependency information, etc.
476//
477// These modules are auto-generated by development/vendor_snapshot/update.py.
478type snapshotLibraryProperties struct {
479 // Prebuilt file for each arch.
480 Src *string `android:"arch_variant"`
481
482 // list of directories that will be added to the include path (using -I).
483 Export_include_dirs []string `android:"arch_variant"`
484
485 // list of directories that will be added to the system path (using -isystem).
486 Export_system_include_dirs []string `android:"arch_variant"`
487
488 // list of flags that will be used for any module that links against this module.
489 Export_flags []string `android:"arch_variant"`
490
491 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
492 Sanitize_ubsan_dep *bool `android:"arch_variant"`
493
494 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
495 Sanitize_minimal_dep *bool `android:"arch_variant"`
496}
497
498type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500499 isSanitizerEnabled(t SanitizerType) bool
500 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900501}
502
503type snapshotLibraryDecorator struct {
504 baseSnapshotDecorator
505 *libraryDecorator
506 properties snapshotLibraryProperties
507 sanitizerProperties struct {
508 CfiEnabled bool `blueprint:"mutated"`
509
510 // Library flags for cfi variant.
511 Cfi snapshotLibraryProperties `android:"arch_variant"`
512 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900513}
514
515func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
516 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
517 return p.libraryDecorator.linkerFlags(ctx, flags)
518}
519
520func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
521 arches := config.Arches()
522 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
523 return false
524 }
525 if !p.header() && p.properties.Src == nil {
526 return false
527 }
528 return true
529}
530
531// cc modules' link functions are to link compiled objects into final binaries.
532// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
533// done by normal library decorator, e.g. exporting flags.
534func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Inseob Kimde5744a2020-12-02 13:14:28 +0900535 if p.header() {
536 return p.libraryDecorator.link(ctx, flags, deps, objs)
537 }
538
539 if p.sanitizerProperties.CfiEnabled {
540 p.properties = p.sanitizerProperties.Cfi
541 }
542
543 if !p.matchesWithDevice(ctx.DeviceConfig()) {
544 return nil
545 }
546
547 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
548 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
549 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
550
551 in := android.PathForModuleSrc(ctx, *p.properties.Src)
552 p.unstrippedOutputFile = in
553
554 if p.shared() {
555 libName := in.Base()
556 builderFlags := flagsToBuilderFlags(flags)
557
558 // Optimize out relinking against shared libraries whose interface hasn't changed by
559 // depending on a table of contents file instead of the library itself.
560 tocFile := android.PathForModuleOut(ctx, libName+".toc")
561 p.tocFile = android.OptionalPathForPath(tocFile)
562 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
563
564 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
565 SharedLibrary: in,
566 UnstrippedSharedLibrary: p.unstrippedOutputFile,
567
568 TableOfContents: p.tocFile,
569 })
570 }
571
572 if p.static() {
573 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
574 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
575 StaticLibrary: in,
576
577 TransitiveStaticLibrariesForOrdering: depSet,
578 })
579 }
580
581 p.libraryDecorator.flagExporter.setProvider(ctx)
582
583 return in
584}
585
586func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
587 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
588 p.baseInstaller.install(ctx, file)
589 }
590}
591
592func (p *snapshotLibraryDecorator) nativeCoverage() bool {
593 return false
594}
595
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500596func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900597 switch t {
598 case cfi:
599 return p.sanitizerProperties.Cfi.Src != nil
600 default:
601 return false
602 }
603}
604
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500605func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900606 if !enabled {
607 return
608 }
609 switch t {
610 case cfi:
611 p.sanitizerProperties.CfiEnabled = true
612 default:
613 return
614 }
615}
616
Colin Crosse0edaf92021-01-11 17:31:17 -0800617func snapshotLibraryFactory(snapshotSuffix, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900618 module, library := NewLibrary(android.DeviceSupported)
619
620 module.stl = nil
621 module.sanitize = nil
622 library.disableStripping()
623
624 prebuilt := &snapshotLibraryDecorator{
625 libraryDecorator: library,
626 }
627
628 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
629 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
630
631 // Prevent default system libs (libc, libm, and libdl) from being linked
632 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
633 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
634 }
635
636 module.compiler = nil
637 module.linker = prebuilt
638 module.installer = prebuilt
639
Colin Crosse0edaf92021-01-11 17:31:17 -0800640 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900641 module.AddProperties(
642 &prebuilt.properties,
643 &prebuilt.sanitizerProperties,
644 )
645
646 return module, prebuilt
647}
648
649// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
650// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
651// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
652// is set.
653func VendorSnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800654 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900655 prebuilt.libraryDecorator.BuildOnlyShared()
656 return module.Init()
657}
658
659// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
660// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
661// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
662// is set.
663func RecoverySnapshotSharedFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800664 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900665 prebuilt.libraryDecorator.BuildOnlyShared()
666 return module.Init()
667}
668
669// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
670// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
671// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
672// is set.
673func VendorSnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800674 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900675 prebuilt.libraryDecorator.BuildOnlyStatic()
676 return module.Init()
677}
678
679// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
680// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
681// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
682// is set.
683func RecoverySnapshotStaticFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800684 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900685 prebuilt.libraryDecorator.BuildOnlyStatic()
686 return module.Init()
687}
688
689// vendor_snapshot_header is a special header library which is auto-generated by
690// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
691// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
692// is set.
693func VendorSnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800694 module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900695 prebuilt.libraryDecorator.HeaderOnly()
696 return module.Init()
697}
698
699// recovery_snapshot_header is a special header library which is auto-generated by
700// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
701// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
702// is set.
703func RecoverySnapshotHeaderFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800704 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900705 prebuilt.libraryDecorator.HeaderOnly()
706 return module.Init()
707}
708
709var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
710
711//
712// Module definitions for snapshots of executable binaries.
713//
714// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
715// binaries (e.g. toybox, sh) as their src, which can be installed.
716//
717// These modules are auto-generated by development/vendor_snapshot/update.py.
718type snapshotBinaryProperties struct {
719 // Prebuilt file for each arch.
720 Src *string `android:"arch_variant"`
721}
722
723type snapshotBinaryDecorator struct {
724 baseSnapshotDecorator
725 *binaryDecorator
Colin Crossa8890802021-01-22 14:06:33 -0800726 properties snapshotBinaryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900727}
728
729func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
730 if config.DeviceArch() != p.arch() {
731 return false
732 }
733 if p.properties.Src == nil {
734 return false
735 }
736 return true
737}
738
739// cc modules' link functions are to link compiled objects into final binaries.
740// As snapshots are prebuilts, this just returns the prebuilt binary
741func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
742 if !p.matchesWithDevice(ctx.DeviceConfig()) {
743 return nil
744 }
745
746 in := android.PathForModuleSrc(ctx, *p.properties.Src)
747 p.unstrippedOutputFile = in
748 binName := in.Base()
749
Inseob Kimde5744a2020-12-02 13:14:28 +0900750 // use cpExecutable to make it executable
751 outputFile := android.PathForModuleOut(ctx, binName)
752 ctx.Build(pctx, android.BuildParams{
753 Rule: android.CpExecutable,
754 Description: "prebuilt",
755 Output: outputFile,
756 Input: in,
757 })
758
759 return outputFile
760}
761
762func (p *snapshotBinaryDecorator) nativeCoverage() bool {
763 return false
764}
765
766// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
767// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
768// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
769func VendorSnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800770 return snapshotBinaryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900771}
772
773// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
774// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
775// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
776func RecoverySnapshotBinaryFactory() android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800777 return snapshotBinaryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900778}
779
Colin Crosse0edaf92021-01-11 17:31:17 -0800780func snapshotBinaryFactory(snapshotSuffix, moduleSuffix string) android.Module {
Inseob Kimde5744a2020-12-02 13:14:28 +0900781 module, binary := NewBinary(android.DeviceSupported)
782 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
783 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
784
785 // Prevent default system libs (libc, libm, and libdl) from being linked
786 if binary.baseLinker.Properties.System_shared_libs == nil {
787 binary.baseLinker.Properties.System_shared_libs = []string{}
788 }
789
790 prebuilt := &snapshotBinaryDecorator{
791 binaryDecorator: binary,
792 }
793
794 module.compiler = nil
795 module.sanitize = nil
796 module.stl = nil
797 module.linker = prebuilt
798
Colin Crosse0edaf92021-01-11 17:31:17 -0800799 prebuilt.init(module, snapshotSuffix, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900800 module.AddProperties(&prebuilt.properties)
801 return module.Init()
802}
803
804//
805// Module definitions for snapshots of object files (*.o).
806//
807// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
808// files (*.o) as their src.
809//
810// These modules are auto-generated by development/vendor_snapshot/update.py.
811type vendorSnapshotObjectProperties struct {
812 // Prebuilt file for each arch.
813 Src *string `android:"arch_variant"`
814}
815
816type snapshotObjectLinker struct {
817 baseSnapshotDecorator
818 objectLinker
Colin Crossa8890802021-01-22 14:06:33 -0800819 properties vendorSnapshotObjectProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900820}
821
822func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
823 if config.DeviceArch() != p.arch() {
824 return false
825 }
826 if p.properties.Src == nil {
827 return false
828 }
829 return true
830}
831
832// cc modules' link functions are to link compiled objects into final binaries.
833// As snapshots are prebuilts, this just returns the prebuilt binary
834func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
835 if !p.matchesWithDevice(ctx.DeviceConfig()) {
836 return nil
837 }
838
Inseob Kimde5744a2020-12-02 13:14:28 +0900839 return android.PathForModuleSrc(ctx, *p.properties.Src)
840}
841
842func (p *snapshotObjectLinker) nativeCoverage() bool {
843 return false
844}
845
846// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
847// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
848// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
849func VendorSnapshotObjectFactory() android.Module {
850 module := newObject()
851
852 prebuilt := &snapshotObjectLinker{
853 objectLinker: objectLinker{
854 baseLinker: NewBaseLinker(nil),
855 },
856 }
857 module.linker = prebuilt
858
Colin Crosse0edaf92021-01-11 17:31:17 -0800859 prebuilt.init(module, vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900860 module.AddProperties(&prebuilt.properties)
861 return module.Init()
862}
863
864// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
865// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
866// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
867func RecoverySnapshotObjectFactory() android.Module {
868 module := newObject()
869
870 prebuilt := &snapshotObjectLinker{
871 objectLinker: objectLinker{
872 baseLinker: NewBaseLinker(nil),
873 },
874 }
875 module.linker = prebuilt
876
Colin Crosse0edaf92021-01-11 17:31:17 -0800877 prebuilt.init(module, recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900878 module.AddProperties(&prebuilt.properties)
879 return module.Init()
880}
881
882type snapshotInterface interface {
883 matchesWithDevice(config android.DeviceConfig) bool
Colin Crossa8890802021-01-22 14:06:33 -0800884 isSnapshotPrebuilt() bool
885 version() string
886 snapshotAndroidMkSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +0900887}
888
889var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
890var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
891var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
892var _ snapshotInterface = (*snapshotObjectLinker)(nil)