Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 18 | "fmt" |
| 19 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 20 | "android/soong/android" |
| 21 | "android/soong/cc" |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 22 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 26 | type snapshotLibraryDecorator struct { |
| 27 | cc.BaseSnapshotDecorator |
| 28 | *libraryDecorator |
| 29 | properties cc.SnapshotLibraryProperties |
| 30 | sanitizerProperties struct { |
Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 31 | SanitizerVariation cc.SanitizerType `blueprint:"mutated"` |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 32 | |
Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 33 | //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 Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 41 | var _ cc.SnapshotSanitizer = (*snapshotLibraryDecorator)(nil) |
| 42 | |
| 43 | func (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 | |
| 53 | func (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 | |
| 63 | func (library *snapshotLibraryDecorator) IsSanitizerEnabled(t cc.SanitizerType) bool { |
| 64 | return library.sanitizerProperties.SanitizerVariation == t |
| 65 | } |
| 66 | |
| 67 | func (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 Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 72 | func init() { |
| 73 | registerRustSnapshotModules(android.InitRegistrationContext) |
| 74 | } |
| 75 | |
Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 76 | func (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 | |
| 83 | func (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 | |
| 91 | func (mod *Module) IsSnapshotUnsanitizedVariant() bool { |
| 92 | if ss, ok := mod.compiler.(cc.SnapshotSanitizer); ok { |
| 93 | return ss.IsUnsanitizedVariant() |
| 94 | } |
| 95 | return false |
| 96 | } |
| 97 | |
| 98 | func (mod *Module) IsSnapshotSanitizer() bool { |
| 99 | if _, ok := mod.compiler.(cc.SnapshotSanitizer); ok { |
| 100 | return true |
| 101 | } |
| 102 | return false |
| 103 | } |
| 104 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 105 | func registerRustSnapshotModules(ctx android.RegistrationContext) { |
| 106 | cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx, |
| 107 | "vendor_snapshot_rlib", VendorSnapshotRlibFactory) |
Ivan Lozano | add122a | 2023-07-13 11:01:41 -0400 | [diff] [blame] | 108 | cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx, |
| 109 | "vendor_snapshot_dylib", VendorSnapshotDylibFactory) |
Jose Galmes | d7d99be | 2021-11-05 14:04:54 -0700 | [diff] [blame] | 110 | cc.RecoverySnapshotImageSingleton.RegisterAdditionalModule(ctx, |
| 111 | "recovery_snapshot_rlib", RecoverySnapshotRlibFactory) |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | func 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 Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 135 | func (library *snapshotLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 136 | 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 Lozano | add122a | 2023-07-13 11:01:41 -0400 | [diff] [blame] | 143 | } else if library.dylib() { |
| 144 | variant = cc.SnapshotDylibSuffix |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Ivan Lozano | add122a | 2023-07-13 11:01:41 -0400 | [diff] [blame] | 147 | library.SetSnapshotAndroidMkSuffix(ctx, variant) |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 148 | |
Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 149 | if library.IsSanitizerEnabled(cc.Hwasan) { |
| 150 | library.properties = library.sanitizerProperties.Hwasan |
| 151 | } |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 152 | if !library.MatchesWithDevice(ctx.DeviceConfig()) { |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 153 | return buildOutput{} |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 154 | } |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 155 | outputFile := android.PathForModuleSrc(ctx, *library.properties.Src) |
| 156 | library.unstrippedOutputFile = outputFile |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 157 | return buildOutput{outputFile: outputFile} |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | func (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. |
| 168 | func 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 Lozano | add122a | 2023-07-13 11:01:41 -0400 | [diff] [blame] | 175 | // 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. |
| 179 | func 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 Galmes | d7d99be | 2021-11-05 14:04:54 -0700 | [diff] [blame] | 186 | func 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 Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 193 | func (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 | |
| 204 | func (library *snapshotLibraryDecorator) IsSnapshotPrebuilt() bool { |
| 205 | return true |
| 206 | } |
| 207 | |
| 208 | var _ cc.SnapshotInterface = (*snapshotLibraryDecorator)(nil) |