• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//	http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package cc
15
16// This file defines snapshot prebuilt modules, e.g. vendor snapshot and recovery snapshot. Such
17// snapshot modules will override original source modules with setting BOARD_VNDK_VERSION, with
18// snapshot mutators and snapshot information maps which are also defined in this file.
19
20import (
21	"fmt"
22	"strings"
23
24	"android/soong/android"
25	"android/soong/snapshot"
26
27	"github.com/google/blueprint"
28)
29
30// This interface overrides snapshot.SnapshotImage to implement cc module specific functions
31type SnapshotImage interface {
32	snapshot.SnapshotImage
33
34	// The image variant name for this snapshot image.
35	// For example, recovery snapshot image will return "recovery", and vendor snapshot image will
36	// return "vendor." + version.
37	imageVariantName(cfg android.DeviceConfig) string
38
39	// The variant suffix for snapshot modules. For example, vendor snapshot modules will have
40	// ".vendor" as their suffix.
41	moduleNameSuffix() string
42}
43
44type vendorSnapshotImage struct {
45	*snapshot.VendorSnapshotImage
46}
47
48type recoverySnapshotImage struct {
49	*snapshot.RecoverySnapshotImage
50}
51
52func (vendorSnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
53	return VendorVariationPrefix + cfg.VndkVersion()
54}
55
56func (vendorSnapshotImage) moduleNameSuffix() string {
57	return VendorSuffix
58}
59
60func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
61	return android.RecoveryVariation
62}
63
64func (recoverySnapshotImage) moduleNameSuffix() string {
65	return RecoverySuffix
66}
67
68// Override existing vendor and recovery snapshot for cc module specific extra functions
69var VendorSnapshotImageSingleton vendorSnapshotImage = vendorSnapshotImage{&snapshot.VendorSnapshotImageSingleton}
70var RecoverySnapshotImageSingleton recoverySnapshotImage = recoverySnapshotImage{&snapshot.RecoverySnapshotImageSingleton}
71
72func RegisterVendorSnapshotModules(ctx android.RegistrationContext) {
73	ctx.RegisterModuleType("vendor_snapshot", vendorSnapshotFactory)
74	ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
75	ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
76	ctx.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
77	ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
78	ctx.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
79}
80
81func RegisterRecoverySnapshotModules(ctx android.RegistrationContext) {
82	ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
83	ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
84	ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
85	ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
86	ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
87	ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
88}
89
90func init() {
91	RegisterVendorSnapshotModules(android.InitRegistrationContext)
92	RegisterRecoverySnapshotModules(android.InitRegistrationContext)
93	android.RegisterMakeVarsProvider(pctx, snapshotMakeVarsProvider)
94}
95
96const (
97	snapshotHeaderSuffix = "_header."
98	SnapshotSharedSuffix = "_shared."
99	SnapshotStaticSuffix = "_static."
100	snapshotBinarySuffix = "_binary."
101	snapshotObjectSuffix = "_object."
102	SnapshotRlibSuffix   = "_rlib."
103)
104
105type SnapshotProperties struct {
106	Header_libs []string `android:"arch_variant"`
107	Static_libs []string `android:"arch_variant"`
108	Shared_libs []string `android:"arch_variant"`
109	Rlibs       []string `android:"arch_variant"`
110	Vndk_libs   []string `android:"arch_variant"`
111	Binaries    []string `android:"arch_variant"`
112	Objects     []string `android:"arch_variant"`
113}
114type snapshotModule struct {
115	android.ModuleBase
116
117	properties SnapshotProperties
118
119	baseSnapshot BaseSnapshotDecorator
120
121	image SnapshotImage
122}
123
124func (s *snapshotModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
125	cfg := ctx.DeviceConfig()
126	if !s.image.IsUsingSnapshot(cfg) || s.image.TargetSnapshotVersion(cfg) != s.baseSnapshot.Version() {
127		s.Disable()
128	}
129}
130
131func (s *snapshotModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
132	return false
133}
134
135func (s *snapshotModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
136	return false
137}
138
139func (s *snapshotModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
140	return false
141}
142
143func (s *snapshotModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
144	return false
145}
146
147func (s *snapshotModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
148	return false
149}
150
151func (s *snapshotModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
152	return []string{s.image.imageVariantName(ctx.DeviceConfig())}
153}
154
155func (s *snapshotModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
156}
157
158func (s *snapshotModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
159	// Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
160}
161
162func getSnapshotNameSuffix(moduleSuffix, version, arch string) string {
163	versionSuffix := version
164	if arch != "" {
165		versionSuffix += "." + arch
166	}
167	return moduleSuffix + versionSuffix
168}
169
170func (s *snapshotModule) DepsMutator(ctx android.BottomUpMutatorContext) {
171	collectSnapshotMap := func(names []string, snapshotSuffix, moduleSuffix string) map[string]string {
172		snapshotMap := make(map[string]string)
173		for _, name := range names {
174			snapshotMap[name] = name +
175				getSnapshotNameSuffix(snapshotSuffix+moduleSuffix,
176					s.baseSnapshot.Version(),
177					ctx.DeviceConfig().Arches()[0].ArchType.String())
178		}
179		return snapshotMap
180	}
181
182	snapshotSuffix := s.image.moduleNameSuffix()
183	headers := collectSnapshotMap(s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
184	binaries := collectSnapshotMap(s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
185	objects := collectSnapshotMap(s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
186	staticLibs := collectSnapshotMap(s.properties.Static_libs, snapshotSuffix, SnapshotStaticSuffix)
187	sharedLibs := collectSnapshotMap(s.properties.Shared_libs, snapshotSuffix, SnapshotSharedSuffix)
188	rlibs := collectSnapshotMap(s.properties.Rlibs, snapshotSuffix, SnapshotRlibSuffix)
189	vndkLibs := collectSnapshotMap(s.properties.Vndk_libs, "", vndkSuffix)
190	for k, v := range vndkLibs {
191		sharedLibs[k] = v
192	}
193
194	ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
195		HeaderLibs: headers,
196		Binaries:   binaries,
197		Objects:    objects,
198		StaticLibs: staticLibs,
199		SharedLibs: sharedLibs,
200		Rlibs:      rlibs,
201	})
202}
203
204type SnapshotInfo struct {
205	HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs, Rlibs map[string]string
206}
207
208var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
209
210var _ android.ImageInterface = (*snapshotModule)(nil)
211
212func snapshotMakeVarsProvider(ctx android.MakeVarsContext) {
213	snapshotSet := map[string]struct{}{}
214	ctx.VisitAllModules(func(m android.Module) {
215		if s, ok := m.(*snapshotModule); ok {
216			if _, ok := snapshotSet[s.Name()]; ok {
217				// arch variant generates duplicated modules
218				// skip this as we only need to know the path of the module.
219				return
220			}
221			snapshotSet[s.Name()] = struct{}{}
222			imageNameVersion := strings.Split(s.image.imageVariantName(ctx.DeviceConfig()), ".")
223			ctx.Strict(
224				strings.Join([]string{strings.ToUpper(imageNameVersion[0]), s.baseSnapshot.Version(), "SNAPSHOT_DIR"}, "_"),
225				ctx.ModuleDir(s))
226		}
227	})
228}
229
230func vendorSnapshotFactory() android.Module {
231	return snapshotFactory(VendorSnapshotImageSingleton)
232}
233
234func recoverySnapshotFactory() android.Module {
235	return snapshotFactory(RecoverySnapshotImageSingleton)
236}
237
238func snapshotFactory(image SnapshotImage) android.Module {
239	snapshotModule := &snapshotModule{}
240	snapshotModule.image = image
241	snapshotModule.AddProperties(
242		&snapshotModule.properties,
243		&snapshotModule.baseSnapshot.baseProperties)
244	android.InitAndroidArchModule(snapshotModule, android.DeviceSupported, android.MultilibBoth)
245	return snapshotModule
246}
247
248type BaseSnapshotDecoratorProperties struct {
249	// snapshot version.
250	Version string
251
252	// Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
253	Target_arch string
254
255	// Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
256	Androidmk_suffix string `blueprint:"mutated"`
257
258	// Suffix to be added to the module name, e.g., vendor_shared,
259	// recovery_shared, etc.
260	ModuleSuffix string `blueprint:"mutated"`
261}
262
263// BaseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
264// version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
265// collide with source modules. e.g. the following example module,
266//
267//	vendor_snapshot_static {
268//	    name: "libbase",
269//	    arch: "arm64",
270//	    version: 30,
271//	    ...
272//	}
273//
274// will be seen as "libbase.vendor_static.30.arm64" by Soong.
275type BaseSnapshotDecorator struct {
276	baseProperties BaseSnapshotDecoratorProperties
277	Image          SnapshotImage
278}
279
280func (p *BaseSnapshotDecorator) Name(name string) string {
281	return name + p.NameSuffix()
282}
283
284func (p *BaseSnapshotDecorator) NameSuffix() string {
285	return getSnapshotNameSuffix(p.moduleSuffix(), p.Version(), p.Arch())
286}
287
288func (p *BaseSnapshotDecorator) Version() string {
289	return p.baseProperties.Version
290}
291
292func (p *BaseSnapshotDecorator) Arch() string {
293	return p.baseProperties.Target_arch
294}
295
296func (p *BaseSnapshotDecorator) moduleSuffix() string {
297	return p.baseProperties.ModuleSuffix
298}
299
300func (p *BaseSnapshotDecorator) IsSnapshotPrebuilt() bool {
301	return true
302}
303
304func (p *BaseSnapshotDecorator) SnapshotAndroidMkSuffix() string {
305	return p.baseProperties.Androidmk_suffix
306}
307
308func (p *BaseSnapshotDecorator) SetSnapshotAndroidMkSuffix(ctx android.ModuleContext, variant string) {
309	// If there are any 2 or more variations among {core, product, vendor, recovery}
310	// we have to add the androidmk suffix to avoid duplicate modules with the same
311	// name.
312	variations := append(ctx.Target().Variations(), blueprint.Variation{
313		Mutator:   "image",
314		Variation: android.CoreVariation})
315
316	if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
317		p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
318		return
319	}
320
321	variations = append(ctx.Target().Variations(), blueprint.Variation{
322		Mutator:   "image",
323		Variation: ProductVariationPrefix + ctx.DeviceConfig().PlatformVndkVersion()})
324
325	if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
326		p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
327		return
328	}
329
330	images := []SnapshotImage{VendorSnapshotImageSingleton, RecoverySnapshotImageSingleton}
331
332	for _, image := range images {
333		if p.Image == image {
334			continue
335		}
336		variations = append(ctx.Target().Variations(), blueprint.Variation{
337			Mutator:   "image",
338			Variation: image.imageVariantName(ctx.DeviceConfig())})
339
340		if ctx.OtherModuleFarDependencyVariantExists(variations,
341			ctx.Module().(LinkableInterface).BaseModuleName()+
342				getSnapshotNameSuffix(
343					image.moduleNameSuffix()+variant,
344					p.Version(),
345					ctx.DeviceConfig().Arches()[0].ArchType.String())) {
346			p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
347			return
348		}
349	}
350
351	p.baseProperties.Androidmk_suffix = ""
352}
353
354// Call this with a module suffix after creating a snapshot module, such as
355// vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
356func (p *BaseSnapshotDecorator) Init(m LinkableInterface, image SnapshotImage, moduleSuffix string) {
357	p.Image = image
358	p.baseProperties.ModuleSuffix = image.moduleNameSuffix() + moduleSuffix
359	m.AddProperties(&p.baseProperties)
360	android.AddLoadHook(m, func(ctx android.LoadHookContext) {
361		vendorSnapshotLoadHook(ctx, p)
362	})
363}
364
365// vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
366// As vendor snapshot is only for vendor, such modules won't be used at all.
367func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *BaseSnapshotDecorator) {
368	if p.Version() != ctx.DeviceConfig().VndkVersion() {
369		ctx.Module().Disable()
370		return
371	}
372}
373
374// Module definitions for snapshots of libraries (shared, static, header).
375//
376// Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
377// static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
378// which can be installed or linked against. Also they export flags needed when linked, such as
379// include directories, c flags, sanitize dependency information, etc.
380//
381// These modules are auto-generated by development/vendor_snapshot/update.py.
382type SnapshotLibraryProperties struct {
383	// Prebuilt file for each arch.
384	Src *string `android:"arch_variant"`
385
386	// list of directories that will be added to the include path (using -I).
387	Export_include_dirs []string `android:"arch_variant"`
388
389	// list of directories that will be added to the system path (using -isystem).
390	Export_system_include_dirs []string `android:"arch_variant"`
391
392	// list of flags that will be used for any module that links against this module.
393	Export_flags []string `android:"arch_variant"`
394
395	// Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
396	Sanitize_ubsan_dep *bool `android:"arch_variant"`
397
398	// Whether this prebuilt needs to depend on sanitize minimal runtime or not.
399	Sanitize_minimal_dep *bool `android:"arch_variant"`
400}
401
402type snapshotSanitizer interface {
403	isSanitizerAvailable(t SanitizerType) bool
404	setSanitizerVariation(t SanitizerType, enabled bool)
405	isSanitizerEnabled(t SanitizerType) bool
406	isUnsanitizedVariant() bool
407}
408
409type snapshotLibraryDecorator struct {
410	BaseSnapshotDecorator
411	*libraryDecorator
412	properties          SnapshotLibraryProperties
413	sanitizerProperties struct {
414		SanitizerVariation SanitizerType `blueprint:"mutated"`
415
416		// Library flags for cfi variant.
417		Cfi SnapshotLibraryProperties `android:"arch_variant"`
418
419		// Library flags for hwasan variant.
420		Hwasan SnapshotLibraryProperties `android:"arch_variant"`
421	}
422}
423
424func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
425	p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
426	return p.libraryDecorator.linkerFlags(ctx, flags)
427}
428
429func (p *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
430	arches := config.Arches()
431	if len(arches) == 0 || arches[0].ArchType.String() != p.Arch() {
432		return false
433	}
434	if !p.header() && p.properties.Src == nil {
435		return false
436	}
437	return true
438}
439
440// cc modules' link functions are to link compiled objects into final binaries.
441// As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
442// done by normal library decorator, e.g. exporting flags.
443func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
444	var variant string
445	if p.shared() {
446		variant = SnapshotSharedSuffix
447	} else if p.static() {
448		variant = SnapshotStaticSuffix
449	} else {
450		variant = snapshotHeaderSuffix
451	}
452
453	p.SetSnapshotAndroidMkSuffix(ctx, variant)
454
455	if p.header() {
456		return p.libraryDecorator.link(ctx, flags, deps, objs)
457	}
458
459	if p.isSanitizerEnabled(cfi) {
460		p.properties = p.sanitizerProperties.Cfi
461	} else if p.isSanitizerEnabled(Hwasan) {
462		p.properties = p.sanitizerProperties.Hwasan
463	}
464
465	if !p.MatchesWithDevice(ctx.DeviceConfig()) {
466		return nil
467	}
468
469	// Flags specified directly to this module.
470	p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
471	p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
472	p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
473
474	// Flags reexported from dependencies. (e.g. vndk_prebuilt_shared)
475	p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
476	p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
477	p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
478	p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
479	p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
480
481	in := android.PathForModuleSrc(ctx, *p.properties.Src)
482	p.unstrippedOutputFile = in
483
484	if p.shared() {
485		libName := in.Base()
486
487		// Optimize out relinking against shared libraries whose interface hasn't changed by
488		// depending on a table of contents file instead of the library itself.
489		tocFile := android.PathForModuleOut(ctx, libName+".toc")
490		p.tocFile = android.OptionalPathForPath(tocFile)
491		TransformSharedObjectToToc(ctx, in, tocFile)
492
493		ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
494			SharedLibrary: in,
495			Target:        ctx.Target(),
496
497			TableOfContents: p.tocFile,
498		})
499	}
500
501	if p.static() {
502		depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
503		ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
504			StaticLibrary: in,
505
506			TransitiveStaticLibrariesForOrdering: depSet,
507		})
508	}
509
510	p.libraryDecorator.flagExporter.setProvider(ctx)
511
512	return in
513}
514
515func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
516	if p.MatchesWithDevice(ctx.DeviceConfig()) && p.shared() {
517		p.baseInstaller.install(ctx, file)
518	}
519}
520
521func (p *snapshotLibraryDecorator) nativeCoverage() bool {
522	return false
523}
524
525var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
526
527func (p *snapshotLibraryDecorator) isSanitizerAvailable(t SanitizerType) bool {
528	switch t {
529	case cfi:
530		return p.sanitizerProperties.Cfi.Src != nil
531	case Hwasan:
532		return p.sanitizerProperties.Hwasan.Src != nil
533	default:
534		return false
535	}
536}
537
538func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
539	if !enabled || p.isSanitizerEnabled(t) {
540		return
541	}
542	if !p.isUnsanitizedVariant() {
543		panic(fmt.Errorf("snapshot Sanitizer must be one of Cfi or Hwasan but not both"))
544	}
545	p.sanitizerProperties.SanitizerVariation = t
546}
547
548func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
549	return p.sanitizerProperties.SanitizerVariation == t
550}
551
552func (p *snapshotLibraryDecorator) isUnsanitizedVariant() bool {
553	return !p.isSanitizerEnabled(Asan) &&
554		!p.isSanitizerEnabled(Hwasan)
555}
556
557func snapshotLibraryFactory(image SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
558	module, library := NewLibrary(android.DeviceSupported)
559
560	module.stl = nil
561	module.sanitize = nil
562	library.disableStripping()
563
564	prebuilt := &snapshotLibraryDecorator{
565		libraryDecorator: library,
566	}
567
568	prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
569	prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
570
571	// Prevent default system libs (libc, libm, and libdl) from being linked
572	if prebuilt.baseLinker.Properties.System_shared_libs == nil {
573		prebuilt.baseLinker.Properties.System_shared_libs = []string{}
574	}
575
576	module.compiler = nil
577	module.linker = prebuilt
578	module.installer = prebuilt
579
580	prebuilt.Init(module, image, moduleSuffix)
581	module.AddProperties(
582		&prebuilt.properties,
583		&prebuilt.sanitizerProperties,
584	)
585
586	return module, prebuilt
587}
588
589// vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
590// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
591// overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
592// is set.
593func VendorSnapshotSharedFactory() android.Module {
594	module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, SnapshotSharedSuffix)
595	prebuilt.libraryDecorator.BuildOnlyShared()
596	return module.Init()
597}
598
599// recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
600// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
601// overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
602// is set.
603func RecoverySnapshotSharedFactory() android.Module {
604	module, prebuilt := snapshotLibraryFactory(RecoverySnapshotImageSingleton, SnapshotSharedSuffix)
605	prebuilt.libraryDecorator.BuildOnlyShared()
606	return module.Init()
607}
608
609// vendor_snapshot_static is a special prebuilt static library which is auto-generated by
610// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
611// overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
612// is set.
613func VendorSnapshotStaticFactory() android.Module {
614	module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, SnapshotStaticSuffix)
615	prebuilt.libraryDecorator.BuildOnlyStatic()
616	return module.Init()
617}
618
619// recovery_snapshot_static is a special prebuilt static library which is auto-generated by
620// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
621// overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
622// is set.
623func RecoverySnapshotStaticFactory() android.Module {
624	module, prebuilt := snapshotLibraryFactory(RecoverySnapshotImageSingleton, SnapshotStaticSuffix)
625	prebuilt.libraryDecorator.BuildOnlyStatic()
626	return module.Init()
627}
628
629// vendor_snapshot_header is a special header library which is auto-generated by
630// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
631// overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
632// is set.
633func VendorSnapshotHeaderFactory() android.Module {
634	module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, snapshotHeaderSuffix)
635	prebuilt.libraryDecorator.HeaderOnly()
636	return module.Init()
637}
638
639// recovery_snapshot_header is a special header library which is auto-generated by
640// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
641// overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
642// is set.
643func RecoverySnapshotHeaderFactory() android.Module {
644	module, prebuilt := snapshotLibraryFactory(RecoverySnapshotImageSingleton, snapshotHeaderSuffix)
645	prebuilt.libraryDecorator.HeaderOnly()
646	return module.Init()
647}
648
649// Module definitions for snapshots of executable binaries.
650//
651// Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
652// binaries (e.g. toybox, sh) as their src, which can be installed.
653//
654// These modules are auto-generated by development/vendor_snapshot/update.py.
655type snapshotBinaryProperties struct {
656	// Prebuilt file for each arch.
657	Src *string `android:"arch_variant"`
658}
659
660type snapshotBinaryDecorator struct {
661	BaseSnapshotDecorator
662	*binaryDecorator
663	properties snapshotBinaryProperties
664}
665
666func (p *snapshotBinaryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
667	if config.DeviceArch() != p.Arch() {
668		return false
669	}
670	if p.properties.Src == nil {
671		return false
672	}
673	return true
674}
675
676// cc modules' link functions are to link compiled objects into final binaries.
677// As snapshots are prebuilts, this just returns the prebuilt binary
678func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
679	p.SetSnapshotAndroidMkSuffix(ctx, snapshotBinarySuffix)
680
681	if !p.MatchesWithDevice(ctx.DeviceConfig()) {
682		return nil
683	}
684
685	in := android.PathForModuleSrc(ctx, *p.properties.Src)
686	p.unstrippedOutputFile = in
687	binName := in.Base()
688
689	// use cpExecutable to make it executable
690	outputFile := android.PathForModuleOut(ctx, binName)
691	ctx.Build(pctx, android.BuildParams{
692		Rule:        android.CpExecutable,
693		Description: "prebuilt",
694		Output:      outputFile,
695		Input:       in,
696	})
697
698	// binary snapshots need symlinking
699	p.setSymlinkList(ctx)
700
701	return outputFile
702}
703
704func (p *snapshotBinaryDecorator) nativeCoverage() bool {
705	return false
706}
707
708// vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
709// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
710// overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
711func VendorSnapshotBinaryFactory() android.Module {
712	return snapshotBinaryFactory(VendorSnapshotImageSingleton, snapshotBinarySuffix)
713}
714
715// recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
716// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
717// overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
718func RecoverySnapshotBinaryFactory() android.Module {
719	return snapshotBinaryFactory(RecoverySnapshotImageSingleton, snapshotBinarySuffix)
720}
721
722func snapshotBinaryFactory(image SnapshotImage, moduleSuffix string) android.Module {
723	module, binary := NewBinary(android.DeviceSupported)
724	binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
725	binary.baseLinker.Properties.Nocrt = BoolPtr(true)
726
727	// Prevent default system libs (libc, libm, and libdl) from being linked
728	if binary.baseLinker.Properties.System_shared_libs == nil {
729		binary.baseLinker.Properties.System_shared_libs = []string{}
730	}
731
732	prebuilt := &snapshotBinaryDecorator{
733		binaryDecorator: binary,
734	}
735
736	module.compiler = nil
737	module.sanitize = nil
738	module.stl = nil
739	module.linker = prebuilt
740
741	prebuilt.Init(module, image, moduleSuffix)
742	module.AddProperties(&prebuilt.properties)
743	return module.Init()
744}
745
746// Module definitions for snapshots of object files (*.o).
747//
748// Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
749// files (*.o) as their src.
750//
751// These modules are auto-generated by development/vendor_snapshot/update.py.
752type vendorSnapshotObjectProperties struct {
753	// Prebuilt file for each arch.
754	Src *string `android:"arch_variant"`
755}
756
757type snapshotObjectLinker struct {
758	BaseSnapshotDecorator
759	objectLinker
760	properties vendorSnapshotObjectProperties
761}
762
763func (p *snapshotObjectLinker) MatchesWithDevice(config android.DeviceConfig) bool {
764	if config.DeviceArch() != p.Arch() {
765		return false
766	}
767	if p.properties.Src == nil {
768		return false
769	}
770	return true
771}
772
773// cc modules' link functions are to link compiled objects into final binaries.
774// As snapshots are prebuilts, this just returns the prebuilt binary
775func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
776	p.SetSnapshotAndroidMkSuffix(ctx, snapshotObjectSuffix)
777
778	if !p.MatchesWithDevice(ctx.DeviceConfig()) {
779		return nil
780	}
781
782	return android.PathForModuleSrc(ctx, *p.properties.Src)
783}
784
785func (p *snapshotObjectLinker) nativeCoverage() bool {
786	return false
787}
788
789// vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
790// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
791// overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
792func VendorSnapshotObjectFactory() android.Module {
793	module := newObject(android.DeviceSupported)
794
795	prebuilt := &snapshotObjectLinker{
796		objectLinker: objectLinker{
797			baseLinker: NewBaseLinker(nil),
798		},
799	}
800	module.linker = prebuilt
801
802	prebuilt.Init(module, VendorSnapshotImageSingleton, snapshotObjectSuffix)
803	module.AddProperties(&prebuilt.properties)
804
805	// vendor_snapshot_object module does not provide sanitizer variants
806	module.sanitize.Properties.Sanitize.Never = BoolPtr(true)
807
808	return module.Init()
809}
810
811// recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
812// development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
813// overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
814func RecoverySnapshotObjectFactory() android.Module {
815	module := newObject(android.DeviceSupported)
816
817	prebuilt := &snapshotObjectLinker{
818		objectLinker: objectLinker{
819			baseLinker: NewBaseLinker(nil),
820		},
821	}
822	module.linker = prebuilt
823
824	prebuilt.Init(module, RecoverySnapshotImageSingleton, snapshotObjectSuffix)
825	module.AddProperties(&prebuilt.properties)
826	return module.Init()
827}
828
829type SnapshotInterface interface {
830	MatchesWithDevice(config android.DeviceConfig) bool
831	IsSnapshotPrebuilt() bool
832	Version() string
833	SnapshotAndroidMkSuffix() string
834}
835
836var _ SnapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
837var _ SnapshotInterface = (*snapshotLibraryDecorator)(nil)
838var _ SnapshotInterface = (*snapshotBinaryDecorator)(nil)
839var _ SnapshotInterface = (*snapshotObjectLinker)(nil)
840