blob: 3a382a18f30f1e1a30a2264a5a1fa752ea2210ec [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)
Inseob Kimde5744a2020-12-02 13:14:28 +0900267}
268
269const (
Colin Crosse0edaf92021-01-11 17:31:17 -0800270 snapshotHeaderSuffix = "_header."
Ivan Lozanod1dec542021-05-26 15:33:11 -0400271 SnapshotSharedSuffix = "_shared."
272 SnapshotStaticSuffix = "_static."
Colin Crosse0edaf92021-01-11 17:31:17 -0800273 snapshotBinarySuffix = "_binary."
274 snapshotObjectSuffix = "_object."
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400275 SnapshotRlibSuffix = "_rlib."
Inseob Kimde5744a2020-12-02 13:14:28 +0900276)
277
Colin Crosse0edaf92021-01-11 17:31:17 -0800278type SnapshotProperties struct {
279 Header_libs []string `android:"arch_variant"`
280 Static_libs []string `android:"arch_variant"`
281 Shared_libs []string `android:"arch_variant"`
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400282 Rlibs []string `android:"arch_variant"`
Colin Crosse0edaf92021-01-11 17:31:17 -0800283 Vndk_libs []string `android:"arch_variant"`
284 Binaries []string `android:"arch_variant"`
285 Objects []string `android:"arch_variant"`
286}
287
288type snapshot struct {
289 android.ModuleBase
290
291 properties SnapshotProperties
292
Ivan Lozanod1dec542021-05-26 15:33:11 -0400293 baseSnapshot BaseSnapshotDecorator
Colin Crosse0edaf92021-01-11 17:31:17 -0800294
Ivan Lozanod1dec542021-05-26 15:33:11 -0400295 image SnapshotImage
Colin Crosse0edaf92021-01-11 17:31:17 -0800296}
297
298func (s *snapshot) ImageMutatorBegin(ctx android.BaseModuleContext) {
299 cfg := ctx.DeviceConfig()
Ivan Lozanod1dec542021-05-26 15:33:11 -0400300 if !s.image.isUsingSnapshot(cfg) || s.image.targetSnapshotVersion(cfg) != s.baseSnapshot.Version() {
Colin Crosse0edaf92021-01-11 17:31:17 -0800301 s.Disable()
302 }
303}
304
305func (s *snapshot) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
306 return false
307}
308
309func (s *snapshot) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
310 return false
311}
312
313func (s *snapshot) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
314 return false
315}
316
Inseob Kim08758f02021-04-08 21:13:22 +0900317func (s *snapshot) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
318 return false
319}
320
Colin Crosse0edaf92021-01-11 17:31:17 -0800321func (s *snapshot) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
322 return false
323}
324
325func (s *snapshot) ExtraImageVariations(ctx android.BaseModuleContext) []string {
326 return []string{s.image.imageVariantName(ctx.DeviceConfig())}
327}
328
329func (s *snapshot) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
330}
331
332func (s *snapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
333 // Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
334}
335
Justin Yun07b9f862021-02-26 14:00:03 +0900336func getSnapshotNameSuffix(moduleSuffix, version, arch string) string {
337 versionSuffix := version
338 if arch != "" {
339 versionSuffix += "." + arch
340 }
341 return moduleSuffix + versionSuffix
342}
Colin Crosse0edaf92021-01-11 17:31:17 -0800343
Justin Yun07b9f862021-02-26 14:00:03 +0900344func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
345 collectSnapshotMap := func(names []string, snapshotSuffix, moduleSuffix string) map[string]string {
Colin Crosse0edaf92021-01-11 17:31:17 -0800346 snapshotMap := make(map[string]string)
Justin Yun48138672021-02-25 18:21:27 +0900347 for _, name := range names {
348 snapshotMap[name] = name +
Justin Yun07b9f862021-02-26 14:00:03 +0900349 getSnapshotNameSuffix(snapshotSuffix+moduleSuffix,
Ivan Lozanod1dec542021-05-26 15:33:11 -0400350 s.baseSnapshot.Version(),
Jose Galmesf9523ed2021-04-06 19:48:10 -0700351 ctx.DeviceConfig().Arches()[0].ArchType.String())
Colin Crosse0edaf92021-01-11 17:31:17 -0800352 }
353 return snapshotMap
354 }
355
356 snapshotSuffix := s.image.moduleNameSuffix()
Justin Yun07b9f862021-02-26 14:00:03 +0900357 headers := collectSnapshotMap(s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
358 binaries := collectSnapshotMap(s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
359 objects := collectSnapshotMap(s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
Ivan Lozanod1dec542021-05-26 15:33:11 -0400360 staticLibs := collectSnapshotMap(s.properties.Static_libs, snapshotSuffix, SnapshotStaticSuffix)
361 sharedLibs := collectSnapshotMap(s.properties.Shared_libs, snapshotSuffix, SnapshotSharedSuffix)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400362 rlibs := collectSnapshotMap(s.properties.Rlibs, snapshotSuffix, SnapshotRlibSuffix)
Justin Yun07b9f862021-02-26 14:00:03 +0900363 vndkLibs := collectSnapshotMap(s.properties.Vndk_libs, "", vndkSuffix)
Colin Crosse0edaf92021-01-11 17:31:17 -0800364 for k, v := range vndkLibs {
365 sharedLibs[k] = v
366 }
Justin Yun07b9f862021-02-26 14:00:03 +0900367
Colin Crosse0edaf92021-01-11 17:31:17 -0800368 ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
369 HeaderLibs: headers,
370 Binaries: binaries,
371 Objects: objects,
372 StaticLibs: staticLibs,
373 SharedLibs: sharedLibs,
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400374 Rlibs: rlibs,
Colin Crosse0edaf92021-01-11 17:31:17 -0800375 })
376}
377
378type SnapshotInfo struct {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400379 HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs, Rlibs map[string]string
Colin Crosse0edaf92021-01-11 17:31:17 -0800380}
381
382var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
383
384var _ android.ImageInterface = (*snapshot)(nil)
385
386func vendorSnapshotFactory() android.Module {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400387 return snapshotFactory(VendorSnapshotImageSingleton)
Colin Crosse0edaf92021-01-11 17:31:17 -0800388}
389
390func recoverySnapshotFactory() android.Module {
391 return snapshotFactory(recoverySnapshotImageSingleton)
392}
393
Ivan Lozanod1dec542021-05-26 15:33:11 -0400394func snapshotFactory(image SnapshotImage) android.Module {
Colin Crosse0edaf92021-01-11 17:31:17 -0800395 snapshot := &snapshot{}
396 snapshot.image = image
397 snapshot.AddProperties(
398 &snapshot.properties,
399 &snapshot.baseSnapshot.baseProperties)
400 android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
401 return snapshot
402}
403
Ivan Lozanod1dec542021-05-26 15:33:11 -0400404type BaseSnapshotDecoratorProperties struct {
Inseob Kimde5744a2020-12-02 13:14:28 +0900405 // snapshot version.
406 Version string
407
408 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
409 Target_arch string
Jose Galmes6f843bc2020-12-11 13:36:29 -0800410
Colin Crossa8890802021-01-22 14:06:33 -0800411 // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
Inseob Kim1b6fb872021-04-05 13:37:02 +0900412 Androidmk_suffix string `blueprint:"mutated"`
Colin Crossa8890802021-01-22 14:06:33 -0800413
Jose Galmes6f843bc2020-12-11 13:36:29 -0800414 // Suffix to be added to the module name, e.g., vendor_shared,
415 // recovery_shared, etc.
Colin Crosse0edaf92021-01-11 17:31:17 -0800416 ModuleSuffix string `blueprint:"mutated"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900417}
418
Ivan Lozanod1dec542021-05-26 15:33:11 -0400419// BaseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
Inseob Kimde5744a2020-12-02 13:14:28 +0900420// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
421// collide with source modules. e.g. the following example module,
422//
423// vendor_snapshot_static {
424// name: "libbase",
425// arch: "arm64",
426// version: 30,
427// ...
428// }
429//
430// will be seen as "libbase.vendor_static.30.arm64" by Soong.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400431type BaseSnapshotDecorator struct {
432 baseProperties BaseSnapshotDecoratorProperties
433 image SnapshotImage
Inseob Kimde5744a2020-12-02 13:14:28 +0900434}
435
Ivan Lozanod1dec542021-05-26 15:33:11 -0400436func (p *BaseSnapshotDecorator) Name(name string) string {
Inseob Kimde5744a2020-12-02 13:14:28 +0900437 return name + p.NameSuffix()
438}
439
Ivan Lozanod1dec542021-05-26 15:33:11 -0400440func (p *BaseSnapshotDecorator) NameSuffix() string {
441 return getSnapshotNameSuffix(p.moduleSuffix(), p.Version(), p.Arch())
Inseob Kimde5744a2020-12-02 13:14:28 +0900442}
443
Ivan Lozanod1dec542021-05-26 15:33:11 -0400444func (p *BaseSnapshotDecorator) Version() string {
Inseob Kimde5744a2020-12-02 13:14:28 +0900445 return p.baseProperties.Version
446}
447
Ivan Lozanod1dec542021-05-26 15:33:11 -0400448func (p *BaseSnapshotDecorator) Arch() string {
Inseob Kimde5744a2020-12-02 13:14:28 +0900449 return p.baseProperties.Target_arch
450}
451
Ivan Lozanod1dec542021-05-26 15:33:11 -0400452func (p *BaseSnapshotDecorator) moduleSuffix() string {
Colin Crosse0edaf92021-01-11 17:31:17 -0800453 return p.baseProperties.ModuleSuffix
Jose Galmes6f843bc2020-12-11 13:36:29 -0800454}
455
Ivan Lozanod1dec542021-05-26 15:33:11 -0400456func (p *BaseSnapshotDecorator) IsSnapshotPrebuilt() bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900457 return true
458}
459
Ivan Lozanod1dec542021-05-26 15:33:11 -0400460func (p *BaseSnapshotDecorator) SnapshotAndroidMkSuffix() string {
Colin Crossa8890802021-01-22 14:06:33 -0800461 return p.baseProperties.Androidmk_suffix
462}
463
Ivan Lozanod1dec542021-05-26 15:33:11 -0400464func (p *BaseSnapshotDecorator) SetSnapshotAndroidMkSuffix(ctx android.ModuleContext, variant string) {
Jose Galmes7fdc3362021-05-26 21:16:52 -0700465 // If there are any 2 or more variations among {core, product, vendor, recovery}
466 // we have to add the androidmk suffix to avoid duplicate modules with the same
467 // name.
468 variations := append(ctx.Target().Variations(), blueprint.Variation{
Bill Peckham4016d7b2021-05-20 11:54:21 -0700469 Mutator: "image",
470 Variation: android.CoreVariation})
471
Ivan Lozanod1dec542021-05-26 15:33:11 -0400472 if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900473 p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
Bill Peckham4016d7b2021-05-20 11:54:21 -0700474 return
Inseob Kim1b6fb872021-04-05 13:37:02 +0900475 }
Bill Peckham4016d7b2021-05-20 11:54:21 -0700476
Jose Galmes7fdc3362021-05-26 21:16:52 -0700477 variations = append(ctx.Target().Variations(), blueprint.Variation{
Bill Peckham4016d7b2021-05-20 11:54:21 -0700478 Mutator: "image",
479 Variation: ProductVariationPrefix + ctx.DeviceConfig().PlatformVndkVersion()})
480
Ivan Lozanod1dec542021-05-26 15:33:11 -0400481 if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
Bill Peckham4016d7b2021-05-20 11:54:21 -0700482 p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
483 return
484 }
485
Ivan Lozanod1dec542021-05-26 15:33:11 -0400486 images := []SnapshotImage{VendorSnapshotImageSingleton, recoverySnapshotImageSingleton}
Jose Galmes7fdc3362021-05-26 21:16:52 -0700487
488 for _, image := range images {
489 if p.image == image {
490 continue
491 }
492 variations = append(ctx.Target().Variations(), blueprint.Variation{
493 Mutator: "image",
494 Variation: image.imageVariantName(ctx.DeviceConfig())})
495
496 if ctx.OtherModuleFarDependencyVariantExists(variations,
Ivan Lozanod1dec542021-05-26 15:33:11 -0400497 ctx.Module().(LinkableInterface).BaseModuleName()+
Jose Galmes7fdc3362021-05-26 21:16:52 -0700498 getSnapshotNameSuffix(
499 image.moduleNameSuffix()+variant,
Ivan Lozanod1dec542021-05-26 15:33:11 -0400500 p.Version(),
Jose Galmes7fdc3362021-05-26 21:16:52 -0700501 ctx.DeviceConfig().Arches()[0].ArchType.String())) {
502 p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
503 return
504 }
505 }
506
Bill Peckham4016d7b2021-05-20 11:54:21 -0700507 p.baseProperties.Androidmk_suffix = ""
Inseob Kim1b6fb872021-04-05 13:37:02 +0900508}
509
Inseob Kimde5744a2020-12-02 13:14:28 +0900510// Call this with a module suffix after creating a snapshot module, such as
511// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400512func (p *BaseSnapshotDecorator) Init(m LinkableInterface, image SnapshotImage, moduleSuffix string) {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900513 p.image = image
514 p.baseProperties.ModuleSuffix = image.moduleNameSuffix() + moduleSuffix
Inseob Kimde5744a2020-12-02 13:14:28 +0900515 m.AddProperties(&p.baseProperties)
516 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
517 vendorSnapshotLoadHook(ctx, p)
518 })
519}
520
521// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
522// As vendor snapshot is only for vendor, such modules won't be used at all.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400523func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *BaseSnapshotDecorator) {
524 if p.Version() != ctx.DeviceConfig().VndkVersion() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900525 ctx.Module().Disable()
526 return
527 }
528}
529
530//
531// Module definitions for snapshots of libraries (shared, static, header).
532//
533// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
534// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
535// which can be installed or linked against. Also they export flags needed when linked, such as
536// include directories, c flags, sanitize dependency information, etc.
537//
538// These modules are auto-generated by development/vendor_snapshot/update.py.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400539type SnapshotLibraryProperties struct {
Inseob Kimde5744a2020-12-02 13:14:28 +0900540 // Prebuilt file for each arch.
541 Src *string `android:"arch_variant"`
542
543 // list of directories that will be added to the include path (using -I).
544 Export_include_dirs []string `android:"arch_variant"`
545
546 // list of directories that will be added to the system path (using -isystem).
547 Export_system_include_dirs []string `android:"arch_variant"`
548
549 // list of flags that will be used for any module that links against this module.
550 Export_flags []string `android:"arch_variant"`
551
552 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
553 Sanitize_ubsan_dep *bool `android:"arch_variant"`
554
555 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
556 Sanitize_minimal_dep *bool `android:"arch_variant"`
557}
558
559type snapshotSanitizer interface {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500560 isSanitizerEnabled(t SanitizerType) bool
561 setSanitizerVariation(t SanitizerType, enabled bool)
Inseob Kimde5744a2020-12-02 13:14:28 +0900562}
563
564type snapshotLibraryDecorator struct {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400565 BaseSnapshotDecorator
Inseob Kimde5744a2020-12-02 13:14:28 +0900566 *libraryDecorator
Ivan Lozanod1dec542021-05-26 15:33:11 -0400567 properties SnapshotLibraryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900568 sanitizerProperties struct {
569 CfiEnabled bool `blueprint:"mutated"`
570
571 // Library flags for cfi variant.
Ivan Lozanod1dec542021-05-26 15:33:11 -0400572 Cfi SnapshotLibraryProperties `android:"arch_variant"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900573 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900574}
575
576func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
577 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
578 return p.libraryDecorator.linkerFlags(ctx, flags)
579}
580
Ivan Lozanod1dec542021-05-26 15:33:11 -0400581func (p *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900582 arches := config.Arches()
Ivan Lozanod1dec542021-05-26 15:33:11 -0400583 if len(arches) == 0 || arches[0].ArchType.String() != p.Arch() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900584 return false
585 }
586 if !p.header() && p.properties.Src == nil {
587 return false
588 }
589 return true
590}
591
592// cc modules' link functions are to link compiled objects into final binaries.
593// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
594// done by normal library decorator, e.g. exporting flags.
595func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Jose Galmes7fdc3362021-05-26 21:16:52 -0700596 var variant string
597 if p.shared() {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400598 variant = SnapshotSharedSuffix
Jose Galmes7fdc3362021-05-26 21:16:52 -0700599 } else if p.static() {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400600 variant = SnapshotStaticSuffix
Jose Galmes7fdc3362021-05-26 21:16:52 -0700601 } else {
602 variant = snapshotHeaderSuffix
603 }
604
Ivan Lozanod1dec542021-05-26 15:33:11 -0400605 p.SetSnapshotAndroidMkSuffix(ctx, variant)
Inseob Kim1b6fb872021-04-05 13:37:02 +0900606
Inseob Kimde5744a2020-12-02 13:14:28 +0900607 if p.header() {
608 return p.libraryDecorator.link(ctx, flags, deps, objs)
609 }
610
611 if p.sanitizerProperties.CfiEnabled {
612 p.properties = p.sanitizerProperties.Cfi
613 }
614
Ivan Lozanod1dec542021-05-26 15:33:11 -0400615 if !p.MatchesWithDevice(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900616 return nil
617 }
618
Inseob Kimdd0295d2021-04-12 21:09:59 +0900619 // Flags specified directly to this module.
Inseob Kimde5744a2020-12-02 13:14:28 +0900620 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
621 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
622 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
623
Inseob Kimdd0295d2021-04-12 21:09:59 +0900624 // Flags reexported from dependencies. (e.g. vndk_prebuilt_shared)
625 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
626 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
627 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
628 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
629 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
630
Inseob Kimde5744a2020-12-02 13:14:28 +0900631 in := android.PathForModuleSrc(ctx, *p.properties.Src)
632 p.unstrippedOutputFile = in
633
634 if p.shared() {
635 libName := in.Base()
636 builderFlags := flagsToBuilderFlags(flags)
637
638 // Optimize out relinking against shared libraries whose interface hasn't changed by
639 // depending on a table of contents file instead of the library itself.
640 tocFile := android.PathForModuleOut(ctx, libName+".toc")
641 p.tocFile = android.OptionalPathForPath(tocFile)
642 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
643
644 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400645 SharedLibrary: in,
646 Target: ctx.Target(),
Inseob Kimde5744a2020-12-02 13:14:28 +0900647
648 TableOfContents: p.tocFile,
649 })
650 }
651
652 if p.static() {
653 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
654 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
655 StaticLibrary: in,
656
657 TransitiveStaticLibrariesForOrdering: depSet,
658 })
659 }
660
661 p.libraryDecorator.flagExporter.setProvider(ctx)
662
663 return in
664}
665
666func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400667 if p.MatchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900668 p.baseInstaller.install(ctx, file)
669 }
670}
671
672func (p *snapshotLibraryDecorator) nativeCoverage() bool {
673 return false
674}
675
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500676func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
Inseob Kimde5744a2020-12-02 13:14:28 +0900677 switch t {
678 case cfi:
679 return p.sanitizerProperties.Cfi.Src != nil
680 default:
681 return false
682 }
683}
684
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500685func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900686 if !enabled {
687 return
688 }
689 switch t {
690 case cfi:
691 p.sanitizerProperties.CfiEnabled = true
692 default:
693 return
694 }
695}
696
Ivan Lozanod1dec542021-05-26 15:33:11 -0400697func snapshotLibraryFactory(image SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900698 module, library := NewLibrary(android.DeviceSupported)
699
700 module.stl = nil
701 module.sanitize = nil
702 library.disableStripping()
703
704 prebuilt := &snapshotLibraryDecorator{
705 libraryDecorator: library,
706 }
707
708 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
709 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
710
711 // Prevent default system libs (libc, libm, and libdl) from being linked
712 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
713 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
714 }
715
716 module.compiler = nil
717 module.linker = prebuilt
718 module.installer = prebuilt
719
Ivan Lozanod1dec542021-05-26 15:33:11 -0400720 prebuilt.Init(module, image, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900721 module.AddProperties(
722 &prebuilt.properties,
723 &prebuilt.sanitizerProperties,
724 )
725
726 return module, prebuilt
727}
728
729// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
730// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
731// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
732// is set.
733func VendorSnapshotSharedFactory() android.Module {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400734 module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, SnapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900735 prebuilt.libraryDecorator.BuildOnlyShared()
736 return module.Init()
737}
738
739// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
740// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
741// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
742// is set.
743func RecoverySnapshotSharedFactory() android.Module {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400744 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, SnapshotSharedSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900745 prebuilt.libraryDecorator.BuildOnlyShared()
746 return module.Init()
747}
748
749// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
750// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
751// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
752// is set.
753func VendorSnapshotStaticFactory() android.Module {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400754 module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, SnapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900755 prebuilt.libraryDecorator.BuildOnlyStatic()
756 return module.Init()
757}
758
759// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
760// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
761// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
762// is set.
763func RecoverySnapshotStaticFactory() android.Module {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400764 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, SnapshotStaticSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900765 prebuilt.libraryDecorator.BuildOnlyStatic()
766 return module.Init()
767}
768
769// vendor_snapshot_header is a special header library which is auto-generated by
770// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
771// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
772// is set.
773func VendorSnapshotHeaderFactory() android.Module {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400774 module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900775 prebuilt.libraryDecorator.HeaderOnly()
776 return module.Init()
777}
778
779// recovery_snapshot_header is a special header library which is auto-generated by
780// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
781// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
782// is set.
783func RecoverySnapshotHeaderFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900784 module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton, snapshotHeaderSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900785 prebuilt.libraryDecorator.HeaderOnly()
786 return module.Init()
787}
788
789var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
790
791//
792// Module definitions for snapshots of executable binaries.
793//
794// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
795// binaries (e.g. toybox, sh) as their src, which can be installed.
796//
797// These modules are auto-generated by development/vendor_snapshot/update.py.
798type snapshotBinaryProperties struct {
799 // Prebuilt file for each arch.
800 Src *string `android:"arch_variant"`
801}
802
803type snapshotBinaryDecorator struct {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400804 BaseSnapshotDecorator
Inseob Kimde5744a2020-12-02 13:14:28 +0900805 *binaryDecorator
Colin Crossa8890802021-01-22 14:06:33 -0800806 properties snapshotBinaryProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900807}
808
Ivan Lozanod1dec542021-05-26 15:33:11 -0400809func (p *snapshotBinaryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
810 if config.DeviceArch() != p.Arch() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900811 return false
812 }
813 if p.properties.Src == nil {
814 return false
815 }
816 return true
817}
818
819// cc modules' link functions are to link compiled objects into final binaries.
820// As snapshots are prebuilts, this just returns the prebuilt binary
821func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400822 p.SetSnapshotAndroidMkSuffix(ctx, snapshotBinarySuffix)
Inseob Kim1b6fb872021-04-05 13:37:02 +0900823
Ivan Lozanod1dec542021-05-26 15:33:11 -0400824 if !p.MatchesWithDevice(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900825 return nil
826 }
827
828 in := android.PathForModuleSrc(ctx, *p.properties.Src)
829 p.unstrippedOutputFile = in
830 binName := in.Base()
831
Inseob Kimde5744a2020-12-02 13:14:28 +0900832 // use cpExecutable to make it executable
833 outputFile := android.PathForModuleOut(ctx, binName)
834 ctx.Build(pctx, android.BuildParams{
835 Rule: android.CpExecutable,
836 Description: "prebuilt",
837 Output: outputFile,
838 Input: in,
839 })
840
841 return outputFile
842}
843
844func (p *snapshotBinaryDecorator) nativeCoverage() bool {
845 return false
846}
847
848// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
849// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
850// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
851func VendorSnapshotBinaryFactory() android.Module {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400852 return snapshotBinaryFactory(VendorSnapshotImageSingleton, snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900853}
854
855// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
856// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
857// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
858func RecoverySnapshotBinaryFactory() android.Module {
Inseob Kim1b6fb872021-04-05 13:37:02 +0900859 return snapshotBinaryFactory(recoverySnapshotImageSingleton, snapshotBinarySuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900860}
861
Ivan Lozanod1dec542021-05-26 15:33:11 -0400862func snapshotBinaryFactory(image SnapshotImage, moduleSuffix string) android.Module {
Inseob Kimde5744a2020-12-02 13:14:28 +0900863 module, binary := NewBinary(android.DeviceSupported)
864 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
865 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
866
867 // Prevent default system libs (libc, libm, and libdl) from being linked
868 if binary.baseLinker.Properties.System_shared_libs == nil {
869 binary.baseLinker.Properties.System_shared_libs = []string{}
870 }
871
872 prebuilt := &snapshotBinaryDecorator{
873 binaryDecorator: binary,
874 }
875
876 module.compiler = nil
877 module.sanitize = nil
878 module.stl = nil
879 module.linker = prebuilt
880
Ivan Lozanod1dec542021-05-26 15:33:11 -0400881 prebuilt.Init(module, image, moduleSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900882 module.AddProperties(&prebuilt.properties)
883 return module.Init()
884}
885
886//
887// Module definitions for snapshots of object files (*.o).
888//
889// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
890// files (*.o) as their src.
891//
892// These modules are auto-generated by development/vendor_snapshot/update.py.
893type vendorSnapshotObjectProperties struct {
894 // Prebuilt file for each arch.
895 Src *string `android:"arch_variant"`
896}
897
898type snapshotObjectLinker struct {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400899 BaseSnapshotDecorator
Inseob Kimde5744a2020-12-02 13:14:28 +0900900 objectLinker
Colin Crossa8890802021-01-22 14:06:33 -0800901 properties vendorSnapshotObjectProperties
Inseob Kimde5744a2020-12-02 13:14:28 +0900902}
903
Ivan Lozanod1dec542021-05-26 15:33:11 -0400904func (p *snapshotObjectLinker) MatchesWithDevice(config android.DeviceConfig) bool {
905 if config.DeviceArch() != p.Arch() {
Inseob Kimde5744a2020-12-02 13:14:28 +0900906 return false
907 }
908 if p.properties.Src == nil {
909 return false
910 }
911 return true
912}
913
914// cc modules' link functions are to link compiled objects into final binaries.
915// As snapshots are prebuilts, this just returns the prebuilt binary
916func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400917 p.SetSnapshotAndroidMkSuffix(ctx, snapshotObjectSuffix)
Inseob Kim1b6fb872021-04-05 13:37:02 +0900918
Ivan Lozanod1dec542021-05-26 15:33:11 -0400919 if !p.MatchesWithDevice(ctx.DeviceConfig()) {
Inseob Kimde5744a2020-12-02 13:14:28 +0900920 return nil
921 }
922
Inseob Kimde5744a2020-12-02 13:14:28 +0900923 return android.PathForModuleSrc(ctx, *p.properties.Src)
924}
925
926func (p *snapshotObjectLinker) nativeCoverage() bool {
927 return false
928}
929
930// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
931// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
932// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
933func VendorSnapshotObjectFactory() android.Module {
934 module := newObject()
935
936 prebuilt := &snapshotObjectLinker{
937 objectLinker: objectLinker{
938 baseLinker: NewBaseLinker(nil),
939 },
940 }
941 module.linker = prebuilt
942
Ivan Lozanod1dec542021-05-26 15:33:11 -0400943 prebuilt.Init(module, VendorSnapshotImageSingleton, snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900944 module.AddProperties(&prebuilt.properties)
945 return module.Init()
946}
947
948// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
949// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
950// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
951func RecoverySnapshotObjectFactory() android.Module {
952 module := newObject()
953
954 prebuilt := &snapshotObjectLinker{
955 objectLinker: objectLinker{
956 baseLinker: NewBaseLinker(nil),
957 },
958 }
959 module.linker = prebuilt
960
Ivan Lozanod1dec542021-05-26 15:33:11 -0400961 prebuilt.Init(module, recoverySnapshotImageSingleton, snapshotObjectSuffix)
Inseob Kimde5744a2020-12-02 13:14:28 +0900962 module.AddProperties(&prebuilt.properties)
963 return module.Init()
964}
965
Ivan Lozanod1dec542021-05-26 15:33:11 -0400966type SnapshotInterface interface {
967 MatchesWithDevice(config android.DeviceConfig) bool
968 IsSnapshotPrebuilt() bool
969 Version() string
970 SnapshotAndroidMkSuffix() string
Inseob Kimde5744a2020-12-02 13:14:28 +0900971}
972
Ivan Lozanod1dec542021-05-26 15:33:11 -0400973var _ SnapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
974var _ SnapshotInterface = (*snapshotLibraryDecorator)(nil)
975var _ SnapshotInterface = (*snapshotBinaryDecorator)(nil)
976var _ SnapshotInterface = (*snapshotObjectLinker)(nil)