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