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