blob: 42e3cef38a234b480c76c558f9aa331e52e7b08f [file] [log] [blame]
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001// 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 (
Ivan Lozano5467a392023-08-23 14:20:25 -040018 "fmt"
19
Ivan Lozano3149e6e2021-06-01 15:09:53 -040020 "android/soong/android"
21 "android/soong/cc"
Kiyoung Kim48f37782021-07-07 12:42:39 +090022
Ivan Lozano3149e6e2021-06-01 15:09:53 -040023 "github.com/google/blueprint/proptools"
24)
25
Ivan Lozano3149e6e2021-06-01 15:09:53 -040026type snapshotLibraryDecorator struct {
27 cc.BaseSnapshotDecorator
28 *libraryDecorator
29 properties cc.SnapshotLibraryProperties
30 sanitizerProperties struct {
Ivan Lozano5467a392023-08-23 14:20:25 -040031 SanitizerVariation cc.SanitizerType `blueprint:"mutated"`
Ivan Lozano3149e6e2021-06-01 15:09:53 -040032
Ivan Lozano5467a392023-08-23 14:20:25 -040033 //TODO: Library flags for cfi variant when CFI is supported.
34 //Cfi cc.SnapshotLibraryProperties `android:"arch_variant"`
35
36 // Library flags for hwasan variant.
37 Hwasan cc.SnapshotLibraryProperties `android:"arch_variant"`
Ivan Lozano3149e6e2021-06-01 15:09:53 -040038 }
39}
40
Ivan Lozano5467a392023-08-23 14:20:25 -040041var _ cc.SnapshotSanitizer = (*snapshotLibraryDecorator)(nil)
42
43func (library *snapshotLibraryDecorator) IsSanitizerAvailable(t cc.SanitizerType) bool {
44 switch t {
45 //TODO: When CFI is supported, add a check here as well
46 case cc.Hwasan:
47 return library.sanitizerProperties.Hwasan.Src != nil
48 default:
49 return false
50 }
51}
52
53func (library *snapshotLibraryDecorator) SetSanitizerVariation(t cc.SanitizerType, enabled bool) {
54 if !enabled || library.IsSanitizerEnabled(t) {
55 return
56 }
57 if !library.IsUnsanitizedVariant() {
58 panic(fmt.Errorf("snapshot Sanitizer must be one of Cfi or Hwasan but not both"))
59 }
60 library.sanitizerProperties.SanitizerVariation = t
61}
62
63func (library *snapshotLibraryDecorator) IsSanitizerEnabled(t cc.SanitizerType) bool {
64 return library.sanitizerProperties.SanitizerVariation == t
65}
66
67func (library *snapshotLibraryDecorator) IsUnsanitizedVariant() bool {
68 //TODO: When CFI is supported, add a check here as well
69 return !library.IsSanitizerEnabled(cc.Hwasan)
70}
71
Ivan Lozano3149e6e2021-06-01 15:09:53 -040072func init() {
73 registerRustSnapshotModules(android.InitRegistrationContext)
74}
75
Ivan Lozano5467a392023-08-23 14:20:25 -040076func (mod *Module) IsSnapshotSanitizerAvailable(t cc.SanitizerType) bool {
77 if ss, ok := mod.compiler.(cc.SnapshotSanitizer); ok {
78 return ss.IsSanitizerAvailable(t)
79 }
80 return false
81}
82
83func (mod *Module) SetSnapshotSanitizerVariation(t cc.SanitizerType, enabled bool) {
84 if ss, ok := mod.compiler.(cc.SnapshotSanitizer); ok {
85 ss.SetSanitizerVariation(t, enabled)
86 } else {
87 panic(fmt.Errorf("Calling SetSnapshotSanitizerVariation on a non-snapshotLibraryDecorator: %s", mod.Name()))
88 }
89}
90
91func (mod *Module) IsSnapshotUnsanitizedVariant() bool {
92 if ss, ok := mod.compiler.(cc.SnapshotSanitizer); ok {
93 return ss.IsUnsanitizedVariant()
94 }
95 return false
96}
97
98func (mod *Module) IsSnapshotSanitizer() bool {
99 if _, ok := mod.compiler.(cc.SnapshotSanitizer); ok {
100 return true
101 }
102 return false
103}
104
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400105func registerRustSnapshotModules(ctx android.RegistrationContext) {
106 cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx,
107 "vendor_snapshot_rlib", VendorSnapshotRlibFactory)
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400108 cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx,
109 "vendor_snapshot_dylib", VendorSnapshotDylibFactory)
Jose Galmesd7d99be2021-11-05 14:04:54 -0700110 cc.RecoverySnapshotImageSingleton.RegisterAdditionalModule(ctx,
111 "recovery_snapshot_rlib", RecoverySnapshotRlibFactory)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400112}
113
114func snapshotLibraryFactory(image cc.SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
115 module, library := NewRustLibrary(android.DeviceSupported)
116
117 module.sanitize = nil
118 library.stripper.StripProperties.Strip.None = proptools.BoolPtr(true)
119
120 prebuilt := &snapshotLibraryDecorator{
121 libraryDecorator: library,
122 }
123
124 module.compiler = prebuilt
125
126 prebuilt.Init(module, image, moduleSuffix)
127 module.AddProperties(
128 &prebuilt.properties,
129 &prebuilt.sanitizerProperties,
130 )
131
132 return module, prebuilt
133}
134
Sasha Smundaka76acba2022-04-18 20:12:56 -0700135func (library *snapshotLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400136 var variant string
137 if library.static() {
138 variant = cc.SnapshotStaticSuffix
139 } else if library.shared() {
140 variant = cc.SnapshotSharedSuffix
141 } else if library.rlib() {
142 variant = cc.SnapshotRlibSuffix
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400143 } else if library.dylib() {
144 variant = cc.SnapshotDylibSuffix
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400145 }
146
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400147 library.SetSnapshotAndroidMkSuffix(ctx, variant)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400148
Ivan Lozano5467a392023-08-23 14:20:25 -0400149 if library.IsSanitizerEnabled(cc.Hwasan) {
150 library.properties = library.sanitizerProperties.Hwasan
151 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400152 if !library.MatchesWithDevice(ctx.DeviceConfig()) {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700153 return buildOutput{}
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400154 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400155 outputFile := android.PathForModuleSrc(ctx, *library.properties.Src)
156 library.unstrippedOutputFile = outputFile
Sasha Smundaka76acba2022-04-18 20:12:56 -0700157 return buildOutput{outputFile: outputFile}
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400158}
159
160func (library *snapshotLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath {
161 return android.OptionalPath{}
162}
163
164// vendor_snapshot_rlib is a special prebuilt rlib library which is auto-generated by
165// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_rlib
166// overrides the vendor variant of the rust rlib library with the same name, if BOARD_VNDK_VERSION
167// is set.
168func VendorSnapshotRlibFactory() android.Module {
169 module, prebuilt := snapshotLibraryFactory(cc.VendorSnapshotImageSingleton, cc.SnapshotRlibSuffix)
170 prebuilt.libraryDecorator.BuildOnlyRlib()
171 prebuilt.libraryDecorator.setNoStdlibs()
172 return module.Init()
173}
174
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400175// vendor_snapshot_dylib is a special prebuilt dylib library which is auto-generated by
176// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_dylib
177// overrides the vendor variant of the rust dylib library with the same name, if BOARD_VNDK_VERSION
178// is set.
179func VendorSnapshotDylibFactory() android.Module {
180 module, prebuilt := snapshotLibraryFactory(cc.VendorSnapshotImageSingleton, cc.SnapshotDylibSuffix)
181 prebuilt.libraryDecorator.BuildOnlyDylib()
182 prebuilt.libraryDecorator.setNoStdlibs()
183 return module.Init()
184}
185
Jose Galmesd7d99be2021-11-05 14:04:54 -0700186func RecoverySnapshotRlibFactory() android.Module {
187 module, prebuilt := snapshotLibraryFactory(cc.RecoverySnapshotImageSingleton, cc.SnapshotRlibSuffix)
188 prebuilt.libraryDecorator.BuildOnlyRlib()
189 prebuilt.libraryDecorator.setNoStdlibs()
190 return module.Init()
191}
192
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400193func (library *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
194 arches := config.Arches()
195 if len(arches) == 0 || arches[0].ArchType.String() != library.Arch() {
196 return false
197 }
198 if library.properties.Src == nil {
199 return false
200 }
201 return true
202}
203
204func (library *snapshotLibraryDecorator) IsSnapshotPrebuilt() bool {
205 return true
206}
207
208var _ cc.SnapshotInterface = (*snapshotLibraryDecorator)(nil)