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