Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 ( |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 18 | "errors" |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 19 | "fmt" |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 20 | "regexp" |
| 21 | "strings" |
| 22 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 23 | "android/soong/android" |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 24 | "android/soong/cc" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 27 | var ( |
Ivan Lozano | 4df0257 | 2023-06-15 14:21:09 -0400 | [diff] [blame] | 28 | RlibStdlibSuffix = ".rlib-std" |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 29 | ) |
| 30 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 31 | func init() { |
| 32 | android.RegisterModuleType("rust_library", RustLibraryFactory) |
| 33 | android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory) |
| 34 | android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory) |
| 35 | android.RegisterModuleType("rust_library_host", RustLibraryHostFactory) |
| 36 | android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory) |
| 37 | android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory) |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 38 | android.RegisterModuleType("rust_ffi", RustFFIFactory) |
| 39 | android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory) |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 40 | android.RegisterModuleType("rust_ffi_rlib", RustFFIRlibFactory) |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 41 | android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory) |
| 42 | android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory) |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 43 | android.RegisterModuleType("rust_ffi_host_rlib", RustFFIRlibHostFactory) |
| 44 | |
| 45 | // TODO: Remove when all instances of rust_ffi_static have been switched to rust_ffi_rlib |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 46 | // Alias rust_ffi_static to the rust_ffi_rlib factory |
| 47 | android.RegisterModuleType("rust_ffi_static", RustFFIRlibFactory) |
| 48 | android.RegisterModuleType("rust_ffi_host_static", RustFFIRlibHostFactory) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | type VariantLibraryProperties struct { |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 52 | Enabled *bool `android:"arch_variant"` |
| 53 | Srcs []string `android:"path,arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | type LibraryCompilerProperties struct { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 57 | Rlib VariantLibraryProperties `android:"arch_variant"` |
| 58 | Dylib VariantLibraryProperties `android:"arch_variant"` |
| 59 | Shared VariantLibraryProperties `android:"arch_variant"` |
| 60 | Static VariantLibraryProperties `android:"arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 61 | |
Ivan Lozano | f033ca6 | 2024-03-21 13:43:14 -0400 | [diff] [blame] | 62 | // TODO: Remove this when all instances of Include_dirs have been removed from rust_ffi modules. |
| 63 | // path to include directories to pass to cc_* modules, only relevant for static/shared variants (deprecated, use export_include_dirs instead). |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 64 | Include_dirs []string `android:"path,arch_variant"` |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 65 | |
Ivan Lozano | f033ca6 | 2024-03-21 13:43:14 -0400 | [diff] [blame] | 66 | // path to include directories to export to cc_* modules, only relevant for static/shared variants. |
| 67 | Export_include_dirs []string `android:"path,arch_variant"` |
| 68 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 69 | // Whether this library is part of the Rust toolchain sysroot. |
| 70 | Sysroot *bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | type LibraryMutatedProperties struct { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 74 | // Build a dylib variant |
| 75 | BuildDylib bool `blueprint:"mutated"` |
| 76 | // Build an rlib variant |
| 77 | BuildRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 78 | // Build a shared library variant |
| 79 | BuildShared bool `blueprint:"mutated"` |
| 80 | // Build a static library variant |
| 81 | BuildStatic bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 82 | |
| 83 | // This variant is a dylib |
| 84 | VariantIsDylib bool `blueprint:"mutated"` |
| 85 | // This variant is an rlib |
| 86 | VariantIsRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 87 | // This variant is a shared library |
| 88 | VariantIsShared bool `blueprint:"mutated"` |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 89 | // This variant is a source provider |
| 90 | VariantIsSource bool `blueprint:"mutated"` |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 91 | |
| 92 | // This variant is disabled and should not be compiled |
| 93 | // (used for SourceProvider variants that produce only source) |
| 94 | VariantIsDisabled bool `blueprint:"mutated"` |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 95 | |
| 96 | // Whether this library variant should be link libstd via rlibs |
| 97 | VariantIsStaticStd bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | type libraryDecorator struct { |
| 101 | *baseCompiler |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 102 | *flagExporter |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 103 | stripper Stripper |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 104 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 105 | Properties LibraryCompilerProperties |
| 106 | MutatedProperties LibraryMutatedProperties |
| 107 | includeDirs android.Paths |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 108 | sourceProvider SourceProvider |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 109 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 110 | isFFI bool |
| 111 | |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 112 | // table-of-contents file for cdylib crates to optimize out relinking when possible |
| 113 | tocFile android.OptionalPath |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | type libraryInterface interface { |
| 117 | rlib() bool |
| 118 | dylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 119 | static() bool |
| 120 | shared() bool |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 121 | sysroot() bool |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 122 | source() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 123 | |
| 124 | // Returns true if the build options for the module have selected a particular build type |
| 125 | buildRlib() bool |
| 126 | buildDylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 127 | buildShared() bool |
| 128 | buildStatic() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 129 | |
| 130 | // Sets a particular variant type |
| 131 | setRlib() |
| 132 | setDylib() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 133 | setShared() |
| 134 | setStatic() |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 135 | setSource() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 136 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 137 | // libstd linkage functions |
| 138 | rlibStd() bool |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 139 | setRlibStd() |
| 140 | setDylibStd() |
| 141 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 142 | // Build a specific library variant |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 143 | BuildOnlyFFI() |
| 144 | BuildOnlyRust() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 145 | BuildOnlyRlib() |
| 146 | BuildOnlyDylib() |
| 147 | BuildOnlyStatic() |
| 148 | BuildOnlyShared() |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 149 | |
| 150 | toc() android.OptionalPath |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 151 | |
| 152 | isFFILibrary() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 155 | func (library *libraryDecorator) nativeCoverage() bool { |
| 156 | return true |
| 157 | } |
| 158 | |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 159 | func (library *libraryDecorator) toc() android.OptionalPath { |
| 160 | return library.tocFile |
| 161 | } |
| 162 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 163 | func (library *libraryDecorator) rlib() bool { |
| 164 | return library.MutatedProperties.VariantIsRlib |
| 165 | } |
| 166 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 167 | func (library *libraryDecorator) sysroot() bool { |
| 168 | return Bool(library.Properties.Sysroot) |
| 169 | } |
| 170 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 171 | func (library *libraryDecorator) dylib() bool { |
| 172 | return library.MutatedProperties.VariantIsDylib |
| 173 | } |
| 174 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 175 | func (library *libraryDecorator) shared() bool { |
| 176 | return library.MutatedProperties.VariantIsShared |
| 177 | } |
| 178 | |
| 179 | func (library *libraryDecorator) static() bool { |
Colin Cross | 17f9dc5 | 2024-07-01 20:05:54 -0700 | [diff] [blame^] | 180 | return false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 183 | func (library *libraryDecorator) source() bool { |
| 184 | return library.MutatedProperties.VariantIsSource |
| 185 | } |
| 186 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 187 | func (library *libraryDecorator) buildRlib() bool { |
| 188 | return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true) |
| 189 | } |
| 190 | |
| 191 | func (library *libraryDecorator) buildDylib() bool { |
| 192 | return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true) |
| 193 | } |
| 194 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 195 | func (library *libraryDecorator) buildShared() bool { |
| 196 | return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true) |
| 197 | } |
| 198 | |
| 199 | func (library *libraryDecorator) buildStatic() bool { |
| 200 | return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true) |
| 201 | } |
| 202 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 203 | func (library *libraryDecorator) setRlib() { |
| 204 | library.MutatedProperties.VariantIsRlib = true |
| 205 | library.MutatedProperties.VariantIsDylib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 206 | library.MutatedProperties.VariantIsShared = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | func (library *libraryDecorator) setDylib() { |
| 210 | library.MutatedProperties.VariantIsRlib = false |
| 211 | library.MutatedProperties.VariantIsDylib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 212 | library.MutatedProperties.VariantIsShared = false |
| 213 | } |
| 214 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 215 | func (library *libraryDecorator) rlibStd() bool { |
| 216 | return library.MutatedProperties.VariantIsStaticStd |
| 217 | } |
| 218 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 219 | func (library *libraryDecorator) setRlibStd() { |
| 220 | library.MutatedProperties.VariantIsStaticStd = true |
| 221 | } |
| 222 | |
| 223 | func (library *libraryDecorator) setDylibStd() { |
| 224 | library.MutatedProperties.VariantIsStaticStd = false |
| 225 | } |
| 226 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 227 | func (library *libraryDecorator) setShared() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 228 | library.MutatedProperties.VariantIsShared = true |
| 229 | library.MutatedProperties.VariantIsRlib = false |
| 230 | library.MutatedProperties.VariantIsDylib = false |
| 231 | } |
| 232 | |
| 233 | func (library *libraryDecorator) setStatic() { |
Colin Cross | 17f9dc5 | 2024-07-01 20:05:54 -0700 | [diff] [blame^] | 234 | panic(fmt.Errorf("static variant is not supported for rust modules, use the rlib variant instead")) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 237 | func (library *libraryDecorator) setSource() { |
| 238 | library.MutatedProperties.VariantIsSource = true |
| 239 | } |
| 240 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 241 | func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Ivan Lozano | add122a | 2023-07-13 11:01:41 -0400 | [diff] [blame] | 242 | if library.preferRlib() { |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 243 | return rlibAutoDep |
| 244 | } else if library.rlib() || library.static() { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 245 | return rlibAutoDep |
| 246 | } else if library.dylib() || library.shared() { |
| 247 | return dylibAutoDep |
| 248 | } else { |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 249 | panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName())) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 253 | func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage { |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 254 | if library.static() || library.MutatedProperties.VariantIsStaticStd || (library.rlib() && library.isFFILibrary()) { |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 255 | return RlibLinkage |
| 256 | } else if library.baseCompiler.preferRlib() { |
| 257 | return RlibLinkage |
| 258 | } |
| 259 | return DefaultLinkage |
| 260 | } |
| 261 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 262 | var _ compiler = (*libraryDecorator)(nil) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 263 | var _ libraryInterface = (*libraryDecorator)(nil) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 264 | var _ exportedFlagsProducer = (*libraryDecorator)(nil) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 265 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 266 | // rust_library produces all Rust variants (rust_library_dylib and |
| 267 | // rust_library_rlib). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 268 | func RustLibraryFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 269 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 270 | library.BuildOnlyRust() |
| 271 | return module.Init() |
| 272 | } |
| 273 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 274 | // rust_ffi produces all FFI variants (rust_ffi_shared, rust_ffi_static, and |
| 275 | // rust_ffi_rlib). |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 276 | func RustFFIFactory() android.Module { |
| 277 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 278 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 279 | return module.Init() |
| 280 | } |
| 281 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 282 | // rust_library_dylib produces a Rust dylib (Rust crate type "dylib"). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 283 | func RustLibraryDylibFactory() android.Module { |
| 284 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 285 | library.BuildOnlyDylib() |
| 286 | return module.Init() |
| 287 | } |
| 288 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 289 | // rust_library_rlib produces an rlib (Rust crate type "rlib"). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 290 | func RustLibraryRlibFactory() android.Module { |
| 291 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 292 | library.BuildOnlyRlib() |
| 293 | return module.Init() |
| 294 | } |
| 295 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 296 | // rust_ffi_shared produces a shared library (Rust crate type |
| 297 | // "cdylib"). |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 298 | func RustFFISharedFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 299 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 300 | library.BuildOnlyShared() |
| 301 | return module.Init() |
| 302 | } |
| 303 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 304 | // rust_library_host produces all Rust variants for the host |
| 305 | // (rust_library_dylib_host and rust_library_rlib_host). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 306 | func RustLibraryHostFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 307 | module, library := NewRustLibrary(android.HostSupported) |
| 308 | library.BuildOnlyRust() |
| 309 | return module.Init() |
| 310 | } |
| 311 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 312 | // rust_ffi_host produces all FFI variants for the host |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 313 | // (rust_ffi_rlib_host, rust_ffi_static_host, and rust_ffi_shared_host). |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 314 | func RustFFIHostFactory() android.Module { |
| 315 | module, library := NewRustLibrary(android.HostSupported) |
| 316 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 317 | return module.Init() |
| 318 | } |
| 319 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 320 | // rust_library_dylib_host produces a dylib for the host (Rust crate |
| 321 | // type "dylib"). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 322 | func RustLibraryDylibHostFactory() android.Module { |
| 323 | module, library := NewRustLibrary(android.HostSupported) |
| 324 | library.BuildOnlyDylib() |
| 325 | return module.Init() |
| 326 | } |
| 327 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 328 | // rust_library_rlib_host produces an rlib for the host (Rust crate |
| 329 | // type "rlib"). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 330 | func RustLibraryRlibHostFactory() android.Module { |
| 331 | module, library := NewRustLibrary(android.HostSupported) |
| 332 | library.BuildOnlyRlib() |
| 333 | return module.Init() |
| 334 | } |
| 335 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 336 | // rust_ffi_shared_host produces an shared library for the host (Rust |
| 337 | // crate type "cdylib"). |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 338 | func RustFFISharedHostFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 339 | module, library := NewRustLibrary(android.HostSupported) |
| 340 | library.BuildOnlyShared() |
| 341 | return module.Init() |
| 342 | } |
| 343 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 344 | // rust_ffi_rlib_host produces an rlib for the host (Rust crate |
| 345 | // type "rlib"). |
| 346 | func RustFFIRlibHostFactory() android.Module { |
| 347 | module, library := NewRustLibrary(android.HostSupported) |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 348 | library.BuildOnlyRlib() |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 349 | |
| 350 | library.isFFI = true |
| 351 | return module.Init() |
| 352 | } |
| 353 | |
| 354 | // rust_ffi_rlib produces an rlib (Rust crate type "rlib"). |
| 355 | func RustFFIRlibFactory() android.Module { |
| 356 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 357 | library.BuildOnlyRlib() |
| 358 | |
| 359 | library.isFFI = true |
| 360 | return module.Init() |
| 361 | } |
| 362 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 363 | func (library *libraryDecorator) BuildOnlyFFI() { |
| 364 | library.MutatedProperties.BuildDylib = false |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 365 | // we build rlibs for later static ffi linkage. |
| 366 | library.MutatedProperties.BuildRlib = true |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 367 | library.MutatedProperties.BuildShared = true |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 368 | library.MutatedProperties.BuildStatic = false |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 369 | |
| 370 | library.isFFI = true |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | func (library *libraryDecorator) BuildOnlyRust() { |
| 374 | library.MutatedProperties.BuildDylib = true |
| 375 | library.MutatedProperties.BuildRlib = true |
| 376 | library.MutatedProperties.BuildShared = false |
| 377 | library.MutatedProperties.BuildStatic = false |
| 378 | } |
| 379 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 380 | func (library *libraryDecorator) BuildOnlyDylib() { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 381 | library.MutatedProperties.BuildDylib = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 382 | library.MutatedProperties.BuildRlib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 383 | library.MutatedProperties.BuildShared = false |
| 384 | library.MutatedProperties.BuildStatic = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | func (library *libraryDecorator) BuildOnlyRlib() { |
| 388 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 389 | library.MutatedProperties.BuildRlib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 390 | library.MutatedProperties.BuildShared = false |
| 391 | library.MutatedProperties.BuildStatic = false |
| 392 | } |
| 393 | |
| 394 | func (library *libraryDecorator) BuildOnlyStatic() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 395 | library.MutatedProperties.BuildRlib = false |
| 396 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 397 | library.MutatedProperties.BuildShared = false |
| 398 | library.MutatedProperties.BuildStatic = true |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 399 | |
| 400 | library.isFFI = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | func (library *libraryDecorator) BuildOnlyShared() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 404 | library.MutatedProperties.BuildRlib = false |
| 405 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 406 | library.MutatedProperties.BuildStatic = false |
| 407 | library.MutatedProperties.BuildShared = true |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 408 | |
| 409 | library.isFFI = true |
| 410 | } |
| 411 | |
| 412 | func (library *libraryDecorator) isFFILibrary() bool { |
| 413 | return library.isFFI |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 417 | module := newModule(hod, android.MultilibBoth) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 418 | |
| 419 | library := &libraryDecorator{ |
| 420 | MutatedProperties: LibraryMutatedProperties{ |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 421 | BuildDylib: false, |
| 422 | BuildRlib: false, |
| 423 | BuildShared: false, |
| 424 | BuildStatic: false, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 425 | }, |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 426 | baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem), |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 427 | flagExporter: NewFlagExporter(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | module.compiler = library |
| 431 | |
| 432 | return module, library |
| 433 | } |
| 434 | |
| 435 | func (library *libraryDecorator) compilerProps() []interface{} { |
| 436 | return append(library.baseCompiler.compilerProps(), |
| 437 | &library.Properties, |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 438 | &library.MutatedProperties, |
| 439 | &library.stripper.StripProperties) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 442 | func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 443 | deps = library.baseCompiler.compilerDeps(ctx, deps) |
| 444 | |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 445 | if library.dylib() || library.shared() { |
| 446 | if ctx.toolchain().Bionic() { |
| 447 | deps = bionicDeps(ctx, deps, false) |
| 448 | deps.CrtBegin = []string{"crtbegin_so"} |
| 449 | deps.CrtEnd = []string{"crtend_so"} |
| 450 | } else if ctx.Os() == android.LinuxMusl { |
| 451 | deps = muslDeps(ctx, deps, false) |
| 452 | deps.CrtBegin = []string{"libc_musl_crtbegin_so"} |
| 453 | deps.CrtEnd = []string{"libc_musl_crtend_so"} |
| 454 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | return deps |
| 458 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 459 | |
| 460 | func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string { |
| 461 | return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix() |
| 462 | } |
| 463 | |
Ivan Lozano | 28ed8f4 | 2024-05-06 21:46:39 -0400 | [diff] [blame] | 464 | // Library cfg flags common to all variants |
| 465 | func CommonLibraryCfgFlags(ctx android.ModuleContext, flags Flags) Flags { |
| 466 | return flags |
| 467 | } |
| 468 | |
Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 469 | func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags { |
| 470 | flags = library.baseCompiler.cfgFlags(ctx, flags) |
Ivan Lozano | 28ed8f4 | 2024-05-06 21:46:39 -0400 | [diff] [blame] | 471 | flags = CommonLibraryCfgFlags(ctx, flags) |
| 472 | |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 473 | cfgs := library.baseCompiler.Properties.Cfgs.GetOrDefault(ctx, nil) |
| 474 | |
Stephen Crane | 0dbfc56 | 2021-07-07 19:05:02 -0700 | [diff] [blame] | 475 | if library.dylib() { |
| 476 | // We need to add a dependency on std in order to link crates as dylibs. |
| 477 | // The hack to add this dependency is guarded by the following cfg so |
| 478 | // that we don't force a dependency when it isn't needed. |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 479 | cfgs = append(cfgs, "android_dylib") |
Stephen Crane | 0dbfc56 | 2021-07-07 19:05:02 -0700 | [diff] [blame] | 480 | } |
Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 481 | |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 482 | cfgFlags := cfgsToFlags(cfgs) |
| 483 | |
| 484 | flags.RustFlags = append(flags.RustFlags, cfgFlags...) |
| 485 | flags.RustdocFlags = append(flags.RustdocFlags, cfgFlags...) |
Ivan Lozano | 28ed8f4 | 2024-05-06 21:46:39 -0400 | [diff] [blame] | 486 | |
| 487 | return flags |
| 488 | } |
| 489 | |
| 490 | // Common flags applied to all libraries irrespective of properties or variant should be included here |
| 491 | func CommonLibraryCompilerFlags(ctx android.ModuleContext, flags Flags) Flags { |
| 492 | flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName()) |
Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 493 | |
| 494 | return flags |
| 495 | } |
| 496 | |
| 497 | func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 498 | flags = library.baseCompiler.compilerFlags(ctx, flags) |
Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 499 | |
Ivan Lozano | 28ed8f4 | 2024-05-06 21:46:39 -0400 | [diff] [blame] | 500 | flags = CommonLibraryCompilerFlags(ctx, flags) |
| 501 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 502 | if library.isFFI { |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 503 | library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...) |
Ivan Lozano | f033ca6 | 2024-03-21 13:43:14 -0400 | [diff] [blame] | 504 | library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)...) |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 505 | } |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 506 | |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 507 | if library.shared() { |
A. Cody Schuffelen | c183e3a | 2023-08-14 21:09:47 -0700 | [diff] [blame] | 508 | if ctx.Darwin() { |
| 509 | flags.LinkFlags = append( |
| 510 | flags.LinkFlags, |
| 511 | "-dynamic_lib", |
| 512 | "-install_name @rpath/"+library.sharedLibFilename(ctx), |
| 513 | ) |
| 514 | } else { |
| 515 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx)) |
| 516 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 519 | return flags |
| 520 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 521 | |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 522 | func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { |
| 523 | var outputFile android.ModuleOutPath |
| 524 | var ret buildOutput |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 525 | var fileName string |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 526 | crateRootPath := crateRootPath(ctx, library) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 527 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 528 | if library.sourceProvider != nil { |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 529 | deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...) |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 530 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 531 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 532 | // Ensure link dirs are not duplicated |
| 533 | deps.linkDirs = android.FirstUniqueStrings(deps.linkDirs) |
| 534 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 535 | // Calculate output filename |
| 536 | if library.rlib() { |
| 537 | fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix() |
| 538 | outputFile = android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 539 | ret.outputFile = outputFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 540 | } else if library.dylib() { |
| 541 | fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix() |
| 542 | outputFile = android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 543 | ret.outputFile = outputFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 544 | } else if library.static() { |
| 545 | fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix() |
| 546 | outputFile = android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 547 | ret.outputFile = outputFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 548 | } else if library.shared() { |
| 549 | fileName = library.sharedLibFilename(ctx) |
| 550 | outputFile = android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 551 | ret.outputFile = outputFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) { |
| 555 | strippedOutputFile := outputFile |
| 556 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
| 557 | library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile) |
| 558 | |
| 559 | library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile) |
| 560 | } |
| 561 | library.baseCompiler.unstrippedOutputFile = outputFile |
| 562 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 563 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) |
Ivan Lozano | 3dfa12d | 2021-02-04 11:29:41 -0500 | [diff] [blame] | 564 | flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...) |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 565 | flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 566 | |
Matthew Maurer | 46c46cc | 2020-01-13 16:34:34 -0800 | [diff] [blame] | 567 | if library.dylib() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 568 | // We need prefer-dynamic for now to avoid linking in the static stdlib. See: |
| 569 | // https://github.com/rust-lang/rust/issues/19680 |
| 570 | // https://github.com/rust-lang/rust/issues/34909 |
| 571 | flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic") |
| 572 | } |
| 573 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 574 | // Call the appropriate builder for this library type |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 575 | if library.rlib() { |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 576 | ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 577 | } else if library.dylib() { |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 578 | ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 579 | } else if library.static() { |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 580 | ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 581 | } else if library.shared() { |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 582 | ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 585 | if library.rlib() || library.dylib() { |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 586 | library.flagExporter.exportLinkDirs(deps.linkDirs...) |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 587 | library.flagExporter.exportLinkObjects(deps.linkObjects...) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 588 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 589 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 590 | // Since we have FFI rlibs, we need to collect their includes as well |
| 591 | if library.static() || library.shared() || library.rlib() { |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 592 | android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{ |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 593 | IncludeDirs: android.FirstUniquePaths(library.includeDirs), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 594 | }) |
| 595 | } |
| 596 | |
| 597 | if library.shared() { |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 598 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 599 | // depending on a table of contents file instead of the library itself. |
| 600 | tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc") |
| 601 | library.tocFile = android.OptionalPathForPath(tocFile) |
| 602 | cc.TransformSharedObjectToToc(ctx, outputFile, tocFile) |
| 603 | |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 604 | android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{ |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 605 | TableOfContents: android.OptionalPathForPath(tocFile), |
| 606 | SharedLibrary: outputFile, |
| 607 | Target: ctx.Target(), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 608 | }) |
| 609 | } |
| 610 | |
| 611 | if library.static() { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 612 | depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputFile).Build() |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 613 | android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{ |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 614 | StaticLibrary: outputFile, |
| 615 | |
| 616 | TransitiveStaticLibrariesForOrdering: depSet, |
| 617 | }) |
| 618 | } |
| 619 | |
| 620 | library.flagExporter.setProvider(ctx) |
| 621 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 622 | return ret |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 625 | func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 626 | if library.sourceProvider != nil { |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 627 | srcs := library.sourceProvider.Srcs() |
| 628 | if len(srcs) == 0 { |
| 629 | return nil, errors.New("Source provider generated 0 sources") |
| 630 | } |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 631 | // Assume the first source from the source provider is the library entry point. |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 632 | return srcs[0], nil |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 633 | } else { |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 634 | return library.baseCompiler.checkedCrateRootPath() |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags, |
| 639 | deps PathDeps) android.OptionalPath { |
| 640 | // rustdoc has builtin support for documenting config specific information |
| 641 | // regardless of the actual config it was given |
| 642 | // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information), |
| 643 | // so we generate the rustdoc for only the primary module so that we have a |
| 644 | // single set of docs to refer to. |
| 645 | if ctx.Module() != ctx.PrimaryModule() { |
| 646 | return android.OptionalPath{} |
| 647 | } |
| 648 | |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 649 | return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library), |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 650 | deps, flags)) |
| 651 | } |
| 652 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 653 | func (library *libraryDecorator) getStem(ctx ModuleContext) string { |
| 654 | stem := library.baseCompiler.getStemWithoutSuffix(ctx) |
| 655 | validateLibraryStem(ctx, stem, library.crateName()) |
| 656 | |
| 657 | return stem + String(library.baseCompiler.Properties.Suffix) |
| 658 | } |
| 659 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 660 | func (library *libraryDecorator) install(ctx ModuleContext) { |
| 661 | // Only shared and dylib variants make sense to install. |
| 662 | if library.shared() || library.dylib() { |
| 663 | library.baseCompiler.install(ctx) |
| 664 | } |
| 665 | } |
| 666 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 667 | func (library *libraryDecorator) Disabled() bool { |
| 668 | return library.MutatedProperties.VariantIsDisabled |
| 669 | } |
| 670 | |
| 671 | func (library *libraryDecorator) SetDisabled() { |
| 672 | library.MutatedProperties.VariantIsDisabled = true |
| 673 | } |
| 674 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 675 | var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+") |
| 676 | |
| 677 | func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) { |
| 678 | if crate_name == "" { |
| 679 | ctx.PropertyErrorf("crate_name", "crate_name must be defined.") |
| 680 | } |
| 681 | |
| 682 | // crate_names are used for the library output file, and rustc expects these |
| 683 | // to be alphanumeric with underscores allowed. |
| 684 | if validCrateName.MatchString(crate_name) { |
| 685 | ctx.PropertyErrorf("crate_name", |
| 686 | "library crate_names must be alphanumeric with underscores allowed") |
| 687 | } |
| 688 | |
| 689 | // Libraries are expected to begin with "lib" followed by the crate_name |
| 690 | if !strings.HasPrefix(filename, "lib"+crate_name) { |
| 691 | ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>") |
| 692 | } |
| 693 | } |
| 694 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 695 | // LibraryMutator mutates the libraries into variants according to the |
| 696 | // build{Rlib,Dylib} attributes. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 697 | func LibraryMutator(mctx android.BottomUpMutatorContext) { |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 698 | // Only mutate on Rust libraries. |
| 699 | m, ok := mctx.Module().(*Module) |
| 700 | if !ok || m.compiler == nil { |
| 701 | return |
| 702 | } |
| 703 | library, ok := m.compiler.(libraryInterface) |
| 704 | if !ok { |
| 705 | return |
| 706 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 707 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 708 | // Don't produce rlib/dylib/source variants for shared or static variants |
| 709 | if library.shared() || library.static() { |
| 710 | return |
| 711 | } |
| 712 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 713 | var variants []string |
| 714 | // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib) |
| 715 | // depend on this variant. It must be the first variant to be declared. |
| 716 | sourceVariant := false |
| 717 | if m.sourceProvider != nil { |
| 718 | variants = append(variants, "source") |
| 719 | sourceVariant = true |
| 720 | } |
| 721 | if library.buildRlib() { |
| 722 | variants = append(variants, rlibVariation) |
| 723 | } |
| 724 | if library.buildDylib() { |
| 725 | variants = append(variants, dylibVariation) |
| 726 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 727 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 728 | if len(variants) == 0 { |
| 729 | return |
| 730 | } |
| 731 | modules := mctx.CreateLocalVariations(variants...) |
| 732 | |
| 733 | // The order of the variations (modules) matches the variant names provided. Iterate |
| 734 | // through the new variation modules and set their mutated properties. |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 735 | var emptyVariant = false |
| 736 | var rlibVariant = false |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 737 | for i, v := range modules { |
| 738 | switch variants[i] { |
| 739 | case rlibVariation: |
| 740 | v.(*Module).compiler.(libraryInterface).setRlib() |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 741 | rlibVariant = true |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 742 | case dylibVariation: |
| 743 | v.(*Module).compiler.(libraryInterface).setDylib() |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 744 | if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 745 | // TODO(b/165791368) |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 746 | // Disable dylib Vendor Ramdisk variations until we support these. |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 747 | v.(*Module).Disable() |
| 748 | } |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 749 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 750 | case "source": |
| 751 | v.(*Module).compiler.(libraryInterface).setSource() |
| 752 | // The source variant does not produce any library. |
| 753 | // Disable the compilation steps. |
| 754 | v.(*Module).compiler.SetDisabled() |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 755 | case "": |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 756 | emptyVariant = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 759 | |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 760 | if rlibVariant && library.isFFILibrary() { |
| 761 | // If an rlib variant is set and this is an FFI library, make it the |
| 762 | // default variant so CC can link against it appropriately. |
| 763 | mctx.AliasVariation(rlibVariation) |
| 764 | } else if emptyVariant { |
| 765 | // If there's an empty variant, alias it so it is the default variant |
| 766 | mctx.AliasVariation("") |
| 767 | } |
| 768 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 769 | // If a source variant is created, add an inter-variant dependency |
| 770 | // between the other variants and the source variant. |
| 771 | if sourceVariant { |
| 772 | sv := modules[0] |
| 773 | for _, v := range modules[1:] { |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 774 | if !v.Enabled(mctx) { |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 775 | continue |
| 776 | } |
| 777 | mctx.AddInterVariantDependency(sourceDepTag, v, sv) |
| 778 | } |
| 779 | // Alias the source variation so it can be named directly in "srcs" properties. |
| 780 | mctx.AliasVariation("source") |
| 781 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 782 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 783 | |
| 784 | func LibstdMutator(mctx android.BottomUpMutatorContext) { |
| 785 | if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() { |
| 786 | switch library := m.compiler.(type) { |
| 787 | case libraryInterface: |
| 788 | // Only create a variant if a library is actually being built. |
| 789 | if library.rlib() && !library.sysroot() { |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 790 | // If this is a rust_ffi variant it only needs rlib-std |
| 791 | if library.isFFILibrary() { |
| 792 | variants := []string{"rlib-std"} |
| 793 | modules := mctx.CreateLocalVariations(variants...) |
| 794 | rlib := modules[0].(*Module) |
| 795 | rlib.compiler.(libraryInterface).setRlibStd() |
| 796 | rlib.Properties.RustSubName += RlibStdlibSuffix |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 797 | mctx.AliasVariation("rlib-std") |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 798 | } else { |
| 799 | variants := []string{"rlib-std", "dylib-std"} |
| 800 | modules := mctx.CreateLocalVariations(variants...) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 801 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 802 | rlib := modules[0].(*Module) |
| 803 | dylib := modules[1].(*Module) |
| 804 | rlib.compiler.(libraryInterface).setRlibStd() |
| 805 | dylib.compiler.(libraryInterface).setDylibStd() |
| 806 | if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation { |
| 807 | // TODO(b/165791368) |
| 808 | // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib |
| 809 | // variants are properly supported. |
| 810 | dylib.Disable() |
| 811 | } |
| 812 | rlib.Properties.RustSubName += RlibStdlibSuffix |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 813 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | } |
| 817 | } |