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