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