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