blob: 4f031ff96809dbd23329bf9855b82e6f0172bb81 [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.
Ivan Lozanod1dec542021-05-26 15:33:11 -040031type 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.
Ivan Lozanod7586b62021-04-01 09:49:36 -040038 inImage(m LinkableInterface) func() bool
Inseob Kimde5744a2020-12-02 13:14:28 +090039
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.
Ivan Lozanod7586b62021-04-01 09:49:36 -040043 private(m LinkableInterface) 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.
Ivan Lozanod7586b62021-04-01 09:49:36 -040059 excludeFromSnapshot(m LinkableInterface) 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
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400111func (vendorSnapshotImage) Init(ctx android.RegistrationContext) {
Colin Crosse0edaf92021-01-11 17:31:17 -0800112 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
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400123func (vendorSnapshotImage) RegisterAdditionalModule(ctx android.RegistrationContext, name string, factory android.ModuleFactory) {
124 ctx.RegisterModuleType(name, factory)
125}
126
Jose Galmes6f843bc2020-12-11 13:36:29 -0800127func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
128 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot.
129 return ctx.DeviceConfig().VndkVersion() == "current"
130}
131
Ivan Lozanod7586b62021-04-01 09:49:36 -0400132func (vendorSnapshotImage) inImage(m LinkableInterface) func() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500133 return m.InVendor
Inseob Kimde5744a2020-12-02 13:14:28 +0900134}
135
Ivan Lozanod7586b62021-04-01 09:49:36 -0400136func (vendorSnapshotImage) private(m LinkableInterface) bool {
Justin Yune09ac172021-01-20 19:49:01 +0900137 return m.IsVndkPrivate()
Inseob Kimde5744a2020-12-02 13:14:28 +0900138}
139
Justin DeMartino383bfb32021-02-24 10:49:43 -0800140func isDirectoryExcluded(dir string, excludedMap directoryMap, includedMap directoryMap) bool {
141 if dir == "." || dir == "/" {
142 return false
143 }
144 if includedMap[dir] {
145 return false
146 } else if excludedMap[dir] {
147 return true
148 } else if defaultDirectoryIncludedMap[dir] {
149 return false
150 } else if defaultDirectoryExcludedMap[dir] {
151 return true
152 } else {
153 return isDirectoryExcluded(filepath.Dir(dir), excludedMap, includedMap)
154 }
155}
156
157func (vendorSnapshotImage) isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
158 return isDirectoryExcluded(dir, deviceConfig.VendorSnapshotDirsExcludedMap(), deviceConfig.VendorSnapshotDirsIncludedMap())
Inseob Kimde5744a2020-12-02 13:14:28 +0900159}
160
161// vendor snapshot includes static/header libraries with vndk: {enabled: true}.
162func (vendorSnapshotImage) includeVndk() bool {
163 return true
164}
165
Ivan Lozanod7586b62021-04-01 09:49:36 -0400166func (vendorSnapshotImage) excludeFromSnapshot(m LinkableInterface) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900167 return m.ExcludeFromVendorSnapshot()
168}
169
Jose Galmes6f843bc2020-12-11 13:36:29 -0800170func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
171 vndkVersion := cfg.VndkVersion()
172 return vndkVersion != "current" && vndkVersion != ""
173}
174
Colin Crosse0edaf92021-01-11 17:31:17 -0800175func (vendorSnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
176 return cfg.VndkVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800177}
178
Inseob Kim7cf14652021-01-06 23:06:52 +0900179// returns true iff a given module SHOULD BE EXCLUDED, false if included
180func (vendorSnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
181 // If we're using full snapshot, not directed snapshot, capture every module
182 if !cfg.DirectedVendorSnapshot() {
183 return false
184 }
185 // Else, checks if name is in VENDOR_SNAPSHOT_MODULES.
186 return !cfg.VendorSnapshotModules()[name]
187}
188
Colin Crosse0edaf92021-01-11 17:31:17 -0800189func (vendorSnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
190 return VendorVariationPrefix + cfg.VndkVersion()
191}
192
193func (vendorSnapshotImage) moduleNameSuffix() string {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500194 return VendorSuffix
Colin Crosse0edaf92021-01-11 17:31:17 -0800195}
196
197func (recoverySnapshotImage) init(ctx android.RegistrationContext) {
198 ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
199 ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
200 ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
201 ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
202 ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
203 ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
204 ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
Inseob Kimde5744a2020-12-02 13:14:28 +0900205}
206
Jose Galmes6f843bc2020-12-11 13:36:29 -0800207func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
208 // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a
209 // snapshot.
210 return ctx.DeviceConfig().RecoverySnapshotVersion() == "current"
211}
212
Ivan Lozanod7586b62021-04-01 09:49:36 -0400213func (recoverySnapshotImage) inImage(m LinkableInterface) func() bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900214 return m.InRecovery
215}
216
Justin Yune09ac172021-01-20 19:49:01 +0900217// recovery snapshot does not have private libraries.
Ivan Lozanod7586b62021-04-01 09:49:36 -0400218func (recoverySnapshotImage) private(m LinkableInterface) bool {
Justin Yune09ac172021-01-20 19:49:01 +0900219 return false
Inseob Kimde5744a2020-12-02 13:14:28 +0900220}
221
Justin DeMartino383bfb32021-02-24 10:49:43 -0800222func (recoverySnapshotImage) isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
223 return isDirectoryExcluded(dir, deviceConfig.RecoverySnapshotDirsExcludedMap(), deviceConfig.RecoverySnapshotDirsIncludedMap())
Inseob Kimde5744a2020-12-02 13:14:28 +0900224}
225
226// recovery snapshot does NOT treat vndk specially.
227func (recoverySnapshotImage) includeVndk() bool {
228 return false
229}
230
Ivan Lozanod7586b62021-04-01 09:49:36 -0400231func (recoverySnapshotImage) excludeFromSnapshot(m LinkableInterface) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900232 return m.ExcludeFromRecoverySnapshot()
233}
234
Jose Galmes6f843bc2020-12-11 13:36:29 -0800235func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
236 recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
237 return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
238}
239
Colin Crosse0edaf92021-01-11 17:31:17 -0800240func (recoverySnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
241 return cfg.RecoverySnapshotVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800242}
243
Inseob Kim7cf14652021-01-06 23:06:52 +0900244func (recoverySnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
Jose Galmes4c6895e2021-02-09 07:44:30 -0800245 // If we're using full snapshot, not directed snapshot, capture every module
246 if !cfg.DirectedRecoverySnapshot() {
247 return false
248 }
249 // Else, checks if name is in RECOVERY_SNAPSHOT_MODULES.
250 return !cfg.RecoverySnapshotModules()[name]
Inseob Kim7cf14652021-01-06 23:06:52 +0900251}
252
Colin Crosse0edaf92021-01-11 17:31:17 -0800253func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
254 return android.RecoveryVariation
255}
256
257func (recoverySnapshotImage) moduleNameSuffix() string {
258 return recoverySuffix
259}
260
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400261var VendorSnapshotImageSingleton vendorSnapshotImage
Inseob Kimde5744a2020-12-02 13:14:28 +0900262var recoverySnapshotImageSingleton recoverySnapshotImage
263
264func init() {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400265 VendorSnapshotImageSingleton.Init(android.InitRegistrationContext)
Colin Crosse0edaf92021-01-11 17:31:17 -0800266 recoverySnapshotImageSingleton.init(android.InitRegistrationContext)
Justin Yund9e05752021-07-13 11:36:24 +0900267 android.RegisterMakeVarsProvider(pctx, snapshotMakeVarsProvider)
Inseob Kimde5744a2020-12-02 13:14:28 +0900268}
269
270const (
Colin Crosse0edaf92021-01-11 17:31:17 -0800271 snapshotHeaderSuffix = "_header."
Ivan Lozanod1dec542021-05-26 15:33:11 -0400272 SnapshotSharedSuffix = "_shared."
273 SnapshotStaticSuffix = "_static."
Colin Crosse0edaf92021-01-11 17:31:17 -0800274 snapshotBinarySuffix = "_binary."
275 snapshotObjectSuffix = "_object."
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400276 SnapshotRlibSuffix = "_rlib."
Inseob Kimde5744a2020-12-02 13:14:28 +0900277)
278
Colin Crosse0edaf92021-01-11 17:31:17 -0800279type SnapshotProperties struct {
280 Header_libs []string `android:"arch_variant"`
281 Static_libs []string `android:"arch_variant"`
282 Shared_libs []string `android:"arch_variant"`
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400283 Rlibs []string `android:"arch_variant"`
Colin Crosse0edaf92021-01-11 17:31:17 -0800284 Vndk_libs []string `android:"arch_variant"`
285 Binaries []string `android:"arch_variant"`
286 Objects []string `android:"arch_variant"`
287}
288
289type snapshot struct {
290 android.ModuleBase
291
292 properties SnapshotProperties
293
Ivan Lozanod1dec542021-05-26 15:33:11 -0400294 baseSnapshot BaseSnapshotDecorator
Colin Crosse0edaf92021-01-11 17:31:17 -0800295
Ivan Lozanod1dec542021-05-26 15:33:11 -0400296 image SnapshotImage
Colin Crosse0edaf92021-01-11 17:31:17 -0800297}
298
299func (s *snapshot) ImageMutatorBegin(ctx android.BaseModuleContext) {
300 cfg := ctx.DeviceConfig()
Ivan Lozanod1dec542021-05-26 15:33:11 -0400301 if !s.image.isUsingSnapshot(cfg) || s.image.targetSnapshotVersion(cfg) != s.baseSnapshot.Version() {
Colin Crosse0edaf92021-01-11 17:31:17 -0800302 s.Disable()
303 }
304}
305
306func (s *snapshot) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
307 return false
308}
309
310func (s *snapshot) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
311 return false
312}
313
314func (s *snapshot) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
315 return false
316}
317
Inseob Kim08758f02021-04-08 21:13:22 +0900318func (s *snapshot) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
319 return false
320}
321
Colin Crosse0edaf92021-01-11 17:31:17 -0800322func (s *snapshot) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
323 return false
324}
325
326func (s *snapshot) ExtraImageVariations(ctx android.BaseModuleContext) []string {
327 return []string{s.image.imageVariantName(ctx.DeviceConfig())}
328}
329
330func (s *snapshot) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
331}
332
333func (s *snapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
334 // Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
335}
336
Justin Yun07b9f862021-02-26 14:00:03 +0900337func getSnapshotNameSuffix(moduleSuffix, version, arch string) string {
338 versionSuffix := version
339 if arch != "" {
340 versionSuffix += "." + arch
341 }
342 return moduleSuffix + versionSuffix
343}
Colin Crosse0edaf92021-01-11 17:31:17 -0800344
Justin Yun07b9f862021-02-26 14:00:03 +0900345func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
346 collectSnapshotMap := func(names []string, snapshotSuffix, moduleSuffix string) map[string]string {
Colin Crosse0edaf92021-01-11 17:31:17 -0800347 snapshotMap := make(map[string]string)
Justin Yun48138672021-02-25 18:21:27 +0900348 for _, name := range names {
349 snapshotMap[name] = name +
Justin Yun07b9f862021-02-26 14:00:03 +0900350 getSnapshotNameSuffix(snapshotSuffix+moduleSuffix,
Ivan Lozanod1dec542021-05-26 15:33:11 -0400351 s.baseSnapshot.Version(),
Jose Galmesf9523ed2021-04-06 19:48:10 -0700352 ctx.DeviceConfig().Arches()[0].ArchType.String())
Colin Crosse0edaf92021-01-11 17:31:17 -0800353 }
354 return snapshotMap
355 }
356
357 snapshotSuffix := s.image.moduleNameSuffix()
Justin Yun07b9f862021-02-26 14:00:03 +0900358 headers := collectSnapshotMap(s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
359 binaries := collectSnapshotMap(s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
360 objects := collectSnapshotMap(s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
Ivan Lozanod1dec542021-05-26 15:33:11 -0400361 staticLibs := collectSnapshotMap(s.properties.Static_libs, snapshotSuffix, SnapshotStaticSuffix)
362 sharedLibs := collectSnapshotMap(s.properties.Shared_libs, snapshotSuffix, SnapshotSharedSuffix)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400363 rlibs := collectSnapshotMap(s.properties.Rlibs, snapshotSuffix, SnapshotRlibSuffix)
Justin Yun07b9f862021-02-26 14:00:03 +0900364 vndkLibs := collectSnapshotMap(s.properties.Vndk_libs, "", vndkSuffix)
Colin Crosse0edaf92021-01-11 17:31:17 -0800365 for k, v := range vndkLibs {
366 sharedLibs[k] = v
367 }
Justin Yun07b9f862021-02-26 14:00:03 +0900368
Colin Crosse0edaf92021-01-11 17:31:17 -0800369 ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
370 HeaderLibs: headers,
371 Binaries: binaries,
372 Objects: objects,
373 StaticLibs: staticLibs,
374 SharedLibs: sharedLibs,
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400375 Rlibs: rlibs,
Colin Crosse0edaf92021-01-11 17:31:17 -0800376 })
377}
378
379type SnapshotInfo struct {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400380 HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs, Rlibs map[string]string
Colin Crosse0edaf92021-01-11 17:31:17 -0800381}
382
383var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
384
385var _ android.ImageInterface = (*snapshot)(nil)
386
Justin Yund9e05752021-07-13 11:36:24 +0900387func snapshotMakeVarsProvider(ctx android.MakeVarsContext) {
388 snapshotSet := map[string]struct{}{}
389 ctx.VisitAllModules(func(m android.Module) {
390 if s, ok := m.(*snapshot); ok {
391 if _, ok := snapshotSet[s.Name()]; ok {
392 // arch variant generates duplicated modules
393 // skip this as we only need to know the path of the module.
394 return
395 }
396 snapshotSet[s.Name()] = struct{}{}
397 imageNameVersion := strings.Split(s.image.imageVariantName(ctx.DeviceConfig()), ".")
398 ctx.Strict(
399 strings.Join([]string{strings.ToUpper(imageNameVersion[0]), s.baseSnapshot.Version(), "SNAPSHOT_DIR"}, "_"),
400 ctx.ModuleDir(s))
401 }
402 })
403}
404
Colin Crosse0edaf92021-01-11 17:31:17 -0800405func vendorSnapshotFactory() android.Module {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400406 return snapshotFactory(VendorSnapshotImageSingleton)
Colin Crosse0edaf92021-01-11 17:31:17 -0800407}
408
409func recoverySnapshotFactory() android.Module {
410 return snapshotFactory(recoverySnapshotImageSingleton)
411}
412
Ivan Lozanod1dec542021-05-26 15:33:11 -0400413func snapshotFactory(image SnapshotImage) android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800414 snapshot := &snapshot{}
415 snapshot.image = image
416 snapshot.AddProperties(
417 &snapshot.properties,
418 &snapshot.baseSnapshot.baseProperties)
419 android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
420 return snapshot
421}
422
Ivan Lozanod1dec542021-05-26 15:33:11 -0400423type BaseSnapshotDecoratorProperties struct {
Inseob Kimde5744a2020-12-02 13:14:28 +0900424 // snapshot version.
425 Version string
426
427 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
428 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800429
Colin Crossa8890802021-01-22 14:06:33 -0800430 // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
Inseob Kim1b6fb872021-04-05 13:37:02 +0900431 Androidmk_suffix string `blueprint:"mutated"`
Colin Crossa8890802021-01-22 14:06:33 -0800432
Jose Galmes6f843bc2020-12-11 13:36:29 -0800433 // Suffix to be added to the module name, e.g., vendor_shared,
434 // recovery_shared, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800435 ModuleSuffix string `blueprint:"mutated"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900436}
437
Ivan Lozanod1dec542021-05-26 15:33:11 -0400438// BaseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
Inseob Kimde5744a2020-12-02 13:14:28 +0900439// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
440// collide with source modules. e.g. the following example module,
441//
442// vendor_snapshot_static {
443// name: "libbase",
444// arch: "arm64",
445// version: 30,
446// ...
447// }
448//
449// will be seen as "libbase.vendor_static.30.arm64" by Soong.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400450type BaseSnapshotDecorator struct {
451 baseProperties BaseSnapshotDecoratorProperties
452 image SnapshotImage
Inseob Kimde5744a2020-12-02 13:14:28 +0900453}
454
Ivan Lozanod1dec542021-05-26 15:33:11 -0400455func (p *BaseSnapshotDecorator) Name(name string) string {
Inseob Kimde5744a2020-12-02 13:14:28 +0900456 return name + p.NameSuffix()
457}
458
Ivan Lozanod1dec542021-05-26 15:33:11 -0400459func (p *BaseSnapshotDecorator) NameSuffix() string {
460 return getSnapshotNameSuffix(p.moduleSuffix(), p.Version(), p.Arch())
Inseob Kimde5744a2020-12-02 13:14:28 +0900461}
462
Ivan Lozanod1dec542021-05-26 15:33:11 -0400463func (p *BaseSnapshotDecorator) Version() string {
Inseob Kimde5744a2020-12-02 13:14:28 +0900464 return p.baseProperties.Version
465}
466
Ivan Lozanod1dec542021-05-26 15:33:11 -0400467func (p *BaseSnapshotDecorator) Arch() string {
Inseob Kimde5744a2020-12-02 13:14:28 +0900468 return p.baseProperties.Target_arch
469}
470
Ivan Lozanod1dec542021-05-26 15:33:11 -0400471func (p *BaseSnapshotDecorator) moduleSuffix() string {
Colin Crosse0edaf92021-01-11 17:31:17 -0800472 return p.baseProperties.ModuleSuffix
Jose Galmes6f843bc2020-12-11 13:36:29 -0800473}
474
Ivan Lozanod1dec542021-05-26 15:33:11 -0400475func (p *BaseSnapshotDecorator) IsSnapshotPrebuilt() bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900476 return true
477}
478
Ivan Lozanod1dec542021-05-26 15:33:11 -0400479func (p *BaseSnapshotDecorator) SnapshotAndroidMkSuffix() string {
Colin Crossa8890802021-01-22 14:06:33 -0800480 return p.baseProperties.Androidmk_suffix
481}
482
Ivan Lozanod1dec542021-05-26 15:33:11 -0400483func (p *BaseSnapshotDecorator) SetSnapshotAndroidMkSuffix(ctx android.ModuleContext, variant string) {
Jose Galmes7fdc3362021-05-26 21:16:52 -0700484 // If there are any 2 or more variations among {core, product, vendor, recovery}
485 // we have to add the androidmk suffix to avoid duplicate modules with the same
486 // name.
487 variations := append(ctx.Target().Variations(), blueprint.Variation{
Bill Peckham4016d7b2021-05-20 11:54:21 -0700488 Mutator: "image",
489 Variation: android.CoreVariation})
490
Ivan Lozanod1dec542021-05-26 15:33:11 -0400491 if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900492 p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
Bill Peckham4016d7b2021-05-20 11:54:21 -0700493 return
Inseob Kim1b6fb872021-04-05 13:37:02 +0900494 }
Bill Peckham4016d7b2021-05-20 11:54:21 -0700495
Jose Galmes7fdc3362021-05-26 21:16:52 -0700496 variations = append(ctx.Target().Variations(), blueprint.Variation{
Bill Peckham4016d7b2021-05-20 11:54:21 -0700497 Mutator: "image",
498 Variation: ProductVariationPrefix + ctx.DeviceConfig().PlatformVndkVersion()})
499
Ivan Lozanod1dec542021-05-26 15:33:11 -0400500 if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
Bill Peckham4016d7b2021-05-20 11:54:21 -0700501 p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
502 return
503 }
504
Ivan Lozanod1dec542021-05-26 15:33:11 -0400505 images := []SnapshotImage{VendorSnapshotImageSingleton, recoverySnapshotImageSingleton}
Jose Galmes7fdc3362021-05-26 21:16:52 -0700506
507 for _, image := range images {
508 if p.image == image {
509 continue
510 }
511 variations = append(ctx.Target().Variations(), blueprint.Variation{
512 Mutator: "image",
513 Variation: image.imageVariantName(ctx.DeviceConfig())})
514
515 if ctx.OtherModuleFarDependencyVariantExists(variations,
Ivan Lozanod1dec542021-05-26 15:33:11 -0400516 ctx.Module().(LinkableInterface).BaseModuleName()+
Jose Galmes7fdc3362021-05-26 21:16:52 -0700517 getSnapshotNameSuffix(
518 image.moduleNameSuffix()+variant,
Ivan Lozanod1dec542021-05-26 15:33:11 -0400519 p.Version(),
Jose Galmes7fdc3362021-05-26 21:16:52 -0700520 ctx.DeviceConfig().Arches()[0].ArchType.String())) {
521 p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
522 return
523 }
524 }
525
Bill Peckham4016d7b2021-05-20 11:54:21 -0700526 p.baseProperties.Androidmk_suffix = ""
Inseob Kim1b6fb872021-04-05 13:37:02 +0900527}
528
Inseob Kimde5744a2020-12-02 13:14:28 +0900529// Call this with a module suffix after creating a snapshot module, such as
530// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400531func (p *BaseSnapshotDecorator) Init(m LinkableInterface, image SnapshotImage, moduleSuffix string) {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900532 p.image = image
533 p.baseProperties.ModuleSuffix = image.moduleNameSuffix() + moduleSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900534 m.AddProperties(&p.baseProperties)
535 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
536 vendorSnapshotLoadHook(ctx, p)
537 })
538}
539
540// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
541// As vendor snapshot is only for vendor, such modules won't be used at all.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400542func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *BaseSnapshotDecorator) {
543 if p.Version() != ctx.DeviceConfig().VndkVersion() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900544 ctx.Module().Disable()
545 return
546 }
547}
548
549//
550// Module definitions for snapshots of libraries (shared, static, header).
551//
552// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
553// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
554// which can be installed or linked against. Also they export flags needed when linked, such as
555// include directories, c flags, sanitize dependency information, etc.
556//
557// These modules are auto-generated by development/vendor_snapshot/update.py.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400558type SnapshotLibraryProperties struct {
Inseob Kimde5744a2020-12-02 13:14:28 +0900559 // Prebuilt file for each arch.
560 Src *string `android:"arch_variant"`
561
562 // list of directories that will be added to the include path (using -I).
563 Export_include_dirs []string `android:"arch_variant"`
564
565 // list of directories that will be added to the system path (using -isystem).
566 Export_system_include_dirs []string `android:"arch_variant"`
567
568 // list of flags that will be used for any module that links against this module.
569 Export_flags []string `android:"arch_variant"`
570
571 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
572 Sanitize_ubsan_dep *bool `android:"arch_variant"`
573
574 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
575 Sanitize_minimal_dep *bool `android:"arch_variant"`
576}
577
578type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500579 isSanitizerEnabled(t SanitizerType) bool
580 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900581}
582
583type snapshotLibraryDecorator struct {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400584 BaseSnapshotDecorator
Inseob Kimde5744a2020-12-02 13:14:28 +0900585 *libraryDecorator
Ivan Lozanod1dec542021-05-26 15:33:11 -0400586 properties SnapshotLibraryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900587 sanitizerProperties struct {
588 CfiEnabled bool `blueprint:"mutated"`
589
590 // Library flags for cfi variant.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400591 Cfi SnapshotLibraryProperties `android:"arch_variant"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900592 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900593}
594
595func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
596 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
597 return p.libraryDecorator.linkerFlags(ctx, flags)
598}
599
Ivan Lozanod1dec542021-05-26 15:33:11 -0400600func (p *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900601 arches := config.Arches()
Ivan Lozanod1dec542021-05-26 15:33:11 -0400602 if len(arches) == 0 || arches[0].ArchType.String() != p.Arch() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900603 return false
604 }
605 if !p.header() && p.properties.Src == nil {
606 return false
607 }
608 return true
609}
610
611// cc modules' link functions are to link compiled objects into final binaries.
612// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
613// done by normal library decorator, e.g. exporting flags.
614func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Jose Galmes7fdc3362021-05-26 21:16:52 -0700615 var variant string
616 if p.shared() {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400617 variant = SnapshotSharedSuffix
Jose Galmes7fdc3362021-05-26 21:16:52 -0700618 } else if p.static() {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400619 variant = SnapshotStaticSuffix
Jose Galmes7fdc3362021-05-26 21:16:52 -0700620 } else {
621 variant = snapshotHeaderSuffix
622 }
623
Ivan Lozanod1dec542021-05-26 15:33:11 -0400624 p.SetSnapshotAndroidMkSuffix(ctx, variant)
Inseob Kim1b6fb872021-04-05 13:37:02 +0900625
Inseob Kimde5744a2020-12-02 13:14:28 +0900626 if p.header() {
627 return p.libraryDecorator.link(ctx, flags, deps, objs)
628 }
629
630 if p.sanitizerProperties.CfiEnabled {
631 p.properties = p.sanitizerProperties.Cfi
632 }
633
Ivan Lozanod1dec542021-05-26 15:33:11 -0400634 if !p.MatchesWithDevice(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900635 return nil
636 }
637
Inseob Kimdd0295d2021-04-12 21:09:59 +0900638 // Flags specified directly to this module.
Inseob Kimde5744a2020-12-02 13:14:28 +0900639 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
640 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
641 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
642
Inseob Kimdd0295d2021-04-12 21:09:59 +0900643 // Flags reexported from dependencies. (e.g. vndk_prebuilt_shared)
644 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
645 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
646 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
647 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
648 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
649
Inseob Kimde5744a2020-12-02 13:14:28 +0900650 in := android.PathForModuleSrc(ctx, *p.properties.Src)
651 p.unstrippedOutputFile = in
652
653 if p.shared() {
654 libName := in.Base()
655 builderFlags := flagsToBuilderFlags(flags)
656
657 // Optimize out relinking against shared libraries whose interface hasn't changed by
658 // depending on a table of contents file instead of the library itself.
659 tocFile := android.PathForModuleOut(ctx, libName+".toc")
660 p.tocFile = android.OptionalPathForPath(tocFile)
661 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
662
663 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400664 SharedLibrary: in,
665 Target: ctx.Target(),
Inseob Kimde5744a2020-12-02 13:14:28 +0900666
667 TableOfContents: p.tocFile,
668 })
669 }
670
671 if p.static() {
672 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
673 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
674 StaticLibrary: in,
675
676 TransitiveStaticLibrariesForOrdering: depSet,
677 })
678 }
679
680 p.libraryDecorator.flagExporter.setProvider(ctx)
681
682 return in
683}
684
685func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400686 if p.MatchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900687 p.baseInstaller.install(ctx, file)
688 }
689}
690
691func (p *snapshotLibraryDecorator) nativeCoverage() bool {
692 return false
693}
694
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500695func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900696 switch t {
697 case cfi:
698 return p.sanitizerProperties.Cfi.Src != nil
699 default:
700 return false
701 }
702}
703
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500704func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900705 if !enabled {
706 return
707 }
708 switch t {
709 case cfi:
710 p.sanitizerProperties.CfiEnabled = true
711 default:
712 return
713 }
714}
715
Ivan Lozanod1dec542021-05-26 15:33:11 -0400716func snapshotLibraryFactory(image SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900717 module, library := NewLibrary(android.DeviceSupported)
718
719 module.stl = nil
720 module.sanitize = nil
721 library.disableStripping()
722
723 prebuilt := &snapshotLibraryDecorator{
724 libraryDecorator: library,
725 }
726
727 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
728 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
729
730 // Prevent default system libs (libc, libm, and libdl) from being linked
731 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
732 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
733 }
734
735 module.compiler = nil
736 module.linker = prebuilt
737 module.installer = prebuilt
738
Ivan Lozanod1dec542021-05-26 15:33:11 -0400739 prebuilt.Init(module, image, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900740 module.AddProperties(
741 &prebuilt.properties,
742 &prebuilt.sanitizerProperties,
743 )
744
745 return module, prebuilt
746}
747
748// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
749// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
750// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
751// is set.
752func VendorSnapshotSharedFactory() android.Module {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400753 module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, SnapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900754 prebuilt.libraryDecorator.BuildOnlyShared()
755 return module.Init()
756}
757
758// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
759// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
760// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
761// is set.
762func RecoverySnapshotSharedFactory() android.Module {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400763 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, SnapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900764 prebuilt.libraryDecorator.BuildOnlyShared()
765 return module.Init()
766}
767
768// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
769// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
770// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
771// is set.
772func VendorSnapshotStaticFactory() android.Module {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400773 module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, SnapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900774 prebuilt.libraryDecorator.BuildOnlyStatic()
775 return module.Init()
776}
777
778// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
779// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
780// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
781// is set.
782func RecoverySnapshotStaticFactory() android.Module {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400783 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, SnapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900784 prebuilt.libraryDecorator.BuildOnlyStatic()
785 return module.Init()
786}
787
788// vendor_snapshot_header is a special header library which is auto-generated by
789// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
790// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
791// is set.
792func VendorSnapshotHeaderFactory() android.Module {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400793 module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900794 prebuilt.libraryDecorator.HeaderOnly()
795 return module.Init()
796}
797
798// recovery_snapshot_header is a special header library which is auto-generated by
799// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
800// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
801// is set.
802func RecoverySnapshotHeaderFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900803 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900804 prebuilt.libraryDecorator.HeaderOnly()
805 return module.Init()
806}
807
808var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
809
810//
811// Module definitions for snapshots of executable binaries.
812//
813// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
814// binaries (e.g. toybox, sh) as their src, which can be installed.
815//
816// These modules are auto-generated by development/vendor_snapshot/update.py.
817type snapshotBinaryProperties struct {
818 // Prebuilt file for each arch.
819 Src *string `android:"arch_variant"`
820}
821
822type snapshotBinaryDecorator struct {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400823 BaseSnapshotDecorator
Inseob Kimde5744a2020-12-02 13:14:28 +0900824 *binaryDecorator
Colin Crossa8890802021-01-22 14:06:33 -0800825 properties snapshotBinaryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900826}
827
Ivan Lozanod1dec542021-05-26 15:33:11 -0400828func (p *snapshotBinaryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
829 if config.DeviceArch() != p.Arch() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900830 return false
831 }
832 if p.properties.Src == nil {
833 return false
834 }
835 return true
836}
837
838// cc modules' link functions are to link compiled objects into final binaries.
839// As snapshots are prebuilts, this just returns the prebuilt binary
840func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400841 p.SetSnapshotAndroidMkSuffix(ctx, snapshotBinarySuffix)
Inseob Kim1b6fb872021-04-05 13:37:02 +0900842
Ivan Lozanod1dec542021-05-26 15:33:11 -0400843 if !p.MatchesWithDevice(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900844 return nil
845 }
846
847 in := android.PathForModuleSrc(ctx, *p.properties.Src)
848 p.unstrippedOutputFile = in
849 binName := in.Base()
850
Inseob Kimde5744a2020-12-02 13:14:28 +0900851 // use cpExecutable to make it executable
852 outputFile := android.PathForModuleOut(ctx, binName)
853 ctx.Build(pctx, android.BuildParams{
854 Rule: android.CpExecutable,
855 Description: "prebuilt",
856 Output: outputFile,
857 Input: in,
858 })
859
860 return outputFile
861}
862
863func (p *snapshotBinaryDecorator) nativeCoverage() bool {
864 return false
865}
866
867// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
868// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
869// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
870func VendorSnapshotBinaryFactory() android.Module {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400871 return snapshotBinaryFactory(VendorSnapshotImageSingleton, snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900872}
873
874// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
875// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
876// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
877func RecoverySnapshotBinaryFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900878 return snapshotBinaryFactory(recoverySnapshotImageSingleton, snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900879}
880
Ivan Lozanod1dec542021-05-26 15:33:11 -0400881func snapshotBinaryFactory(image SnapshotImage, moduleSuffix string) android.Module {
Inseob Kimde5744a2020-12-02 13:14:28 +0900882 module, binary := NewBinary(android.DeviceSupported)
883 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
884 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
885
886 // Prevent default system libs (libc, libm, and libdl) from being linked
887 if binary.baseLinker.Properties.System_shared_libs == nil {
888 binary.baseLinker.Properties.System_shared_libs = []string{}
889 }
890
891 prebuilt := &snapshotBinaryDecorator{
892 binaryDecorator: binary,
893 }
894
895 module.compiler = nil
896 module.sanitize = nil
897 module.stl = nil
898 module.linker = prebuilt
899
Ivan Lozanod1dec542021-05-26 15:33:11 -0400900 prebuilt.Init(module, image, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900901 module.AddProperties(&prebuilt.properties)
902 return module.Init()
903}
904
905//
906// Module definitions for snapshots of object files (*.o).
907//
908// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
909// files (*.o) as their src.
910//
911// These modules are auto-generated by development/vendor_snapshot/update.py.
912type vendorSnapshotObjectProperties struct {
913 // Prebuilt file for each arch.
914 Src *string `android:"arch_variant"`
915}
916
917type snapshotObjectLinker struct {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400918 BaseSnapshotDecorator
Inseob Kimde5744a2020-12-02 13:14:28 +0900919 objectLinker
Colin Crossa8890802021-01-22 14:06:33 -0800920 properties vendorSnapshotObjectProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900921}
922
Ivan Lozanod1dec542021-05-26 15:33:11 -0400923func (p *snapshotObjectLinker) MatchesWithDevice(config android.DeviceConfig) bool {
924 if config.DeviceArch() != p.Arch() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900925 return false
926 }
927 if p.properties.Src == nil {
928 return false
929 }
930 return true
931}
932
933// cc modules' link functions are to link compiled objects into final binaries.
934// As snapshots are prebuilts, this just returns the prebuilt binary
935func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400936 p.SetSnapshotAndroidMkSuffix(ctx, snapshotObjectSuffix)
Inseob Kim1b6fb872021-04-05 13:37:02 +0900937
Ivan Lozanod1dec542021-05-26 15:33:11 -0400938 if !p.MatchesWithDevice(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900939 return nil
940 }
941
Inseob Kimde5744a2020-12-02 13:14:28 +0900942 return android.PathForModuleSrc(ctx, *p.properties.Src)
943}
944
945func (p *snapshotObjectLinker) nativeCoverage() bool {
946 return false
947}
948
949// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
950// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
951// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
952func VendorSnapshotObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700953 module := newObject(android.DeviceSupported)
Inseob Kimde5744a2020-12-02 13:14:28 +0900954
955 prebuilt := &snapshotObjectLinker{
956 objectLinker: objectLinker{
957 baseLinker: NewBaseLinker(nil),
958 },
959 }
960 module.linker = prebuilt
961
Ivan Lozanod1dec542021-05-26 15:33:11 -0400962 prebuilt.Init(module, VendorSnapshotImageSingleton, snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900963 module.AddProperties(&prebuilt.properties)
964 return module.Init()
965}
966
967// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
968// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
969// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
970func RecoverySnapshotObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700971 module := newObject(android.DeviceSupported)
Inseob Kimde5744a2020-12-02 13:14:28 +0900972
973 prebuilt := &snapshotObjectLinker{
974 objectLinker: objectLinker{
975 baseLinker: NewBaseLinker(nil),
976 },
977 }
978 module.linker = prebuilt
979
Ivan Lozanod1dec542021-05-26 15:33:11 -0400980 prebuilt.Init(module, recoverySnapshotImageSingleton, snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900981 module.AddProperties(&prebuilt.properties)
982 return module.Init()
983}
984
Ivan Lozanod1dec542021-05-26 15:33:11 -0400985type SnapshotInterface interface {
986 MatchesWithDevice(config android.DeviceConfig) bool
987 IsSnapshotPrebuilt() bool
988 Version() string
989 SnapshotAndroidMkSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +0900990}
991
Ivan Lozanod1dec542021-05-26 15:33:11 -0400992var _ SnapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
993var _ SnapshotInterface = (*snapshotLibraryDecorator)(nil)
994var _ SnapshotInterface = (*snapshotBinaryDecorator)(nil)
995var _ SnapshotInterface = (*snapshotObjectLinker)(nil)