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