• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 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.
14
15package rust
16
17import (
18	"android/soong/android"
19	"android/soong/cc"
20
21	"github.com/google/blueprint/proptools"
22)
23
24const (
25	snapshotRlibSuffix = "_rlib."
26)
27
28type snapshotLibraryDecorator struct {
29	cc.BaseSnapshotDecorator
30	*libraryDecorator
31	properties          cc.SnapshotLibraryProperties
32	sanitizerProperties struct {
33		CfiEnabled bool `blueprint:"mutated"`
34
35		// Library flags for cfi variant.
36		Cfi cc.SnapshotLibraryProperties `android:"arch_variant"`
37	}
38}
39
40func init() {
41	registerRustSnapshotModules(android.InitRegistrationContext)
42}
43
44func registerRustSnapshotModules(ctx android.RegistrationContext) {
45	cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx,
46		"vendor_snapshot_rlib", VendorSnapshotRlibFactory)
47	cc.RecoverySnapshotImageSingleton.RegisterAdditionalModule(ctx,
48		"recovery_snapshot_rlib", RecoverySnapshotRlibFactory)
49}
50
51func snapshotLibraryFactory(image cc.SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
52	module, library := NewRustLibrary(android.DeviceSupported)
53
54	module.sanitize = nil
55	library.stripper.StripProperties.Strip.None = proptools.BoolPtr(true)
56
57	prebuilt := &snapshotLibraryDecorator{
58		libraryDecorator: library,
59	}
60
61	module.compiler = prebuilt
62
63	prebuilt.Init(module, image, moduleSuffix)
64	module.AddProperties(
65		&prebuilt.properties,
66		&prebuilt.sanitizerProperties,
67	)
68
69	return module, prebuilt
70}
71
72func (library *snapshotLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
73	var variant string
74	if library.static() {
75		variant = cc.SnapshotStaticSuffix
76	} else if library.shared() {
77		variant = cc.SnapshotSharedSuffix
78	} else if library.rlib() {
79		variant = cc.SnapshotRlibSuffix
80	}
81
82	if !library.dylib() {
83		// TODO(184042776): Remove this check when dylibs are supported in snapshots.
84		library.SetSnapshotAndroidMkSuffix(ctx, variant)
85	}
86
87	if !library.MatchesWithDevice(ctx.DeviceConfig()) {
88		return buildOutput{}
89	}
90	outputFile := android.PathForModuleSrc(ctx, *library.properties.Src)
91	library.unstrippedOutputFile = outputFile
92	return buildOutput{outputFile: outputFile}
93}
94
95func (library *snapshotLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath {
96	return android.OptionalPath{}
97}
98
99// vendor_snapshot_rlib is a special prebuilt rlib library which is auto-generated by
100// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_rlib
101// overrides the vendor variant of the rust rlib library with the same name, if BOARD_VNDK_VERSION
102// is set.
103func VendorSnapshotRlibFactory() android.Module {
104	module, prebuilt := snapshotLibraryFactory(cc.VendorSnapshotImageSingleton, cc.SnapshotRlibSuffix)
105	prebuilt.libraryDecorator.BuildOnlyRlib()
106	prebuilt.libraryDecorator.setNoStdlibs()
107	return module.Init()
108}
109
110func RecoverySnapshotRlibFactory() android.Module {
111	module, prebuilt := snapshotLibraryFactory(cc.RecoverySnapshotImageSingleton, cc.SnapshotRlibSuffix)
112	prebuilt.libraryDecorator.BuildOnlyRlib()
113	prebuilt.libraryDecorator.setNoStdlibs()
114	return module.Init()
115}
116
117func (library *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
118	arches := config.Arches()
119	if len(arches) == 0 || arches[0].ArchType.String() != library.Arch() {
120		return false
121	}
122	if library.properties.Src == nil {
123		return false
124	}
125	return true
126}
127
128func (library *snapshotLibraryDecorator) IsSnapshotPrebuilt() bool {
129	return true
130}
131
132var _ cc.SnapshotInterface = (*snapshotLibraryDecorator)(nil)
133