| 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" | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 23 | "android/soong/bazel" | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 24 | "android/soong/cc" | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 25 |  | 
|  | 26 | "github.com/google/blueprint/proptools" | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 27 | ) | 
|  | 28 |  | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 29 | var ( | 
| Ivan Lozano | 4df0257 | 2023-06-15 14:21:09 -0400 | [diff] [blame] | 30 | RlibStdlibSuffix = ".rlib-std" | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 31 | ) | 
|  | 32 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 33 | func init() { | 
|  | 34 | android.RegisterModuleType("rust_library", RustLibraryFactory) | 
|  | 35 | android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory) | 
|  | 36 | android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory) | 
|  | 37 | android.RegisterModuleType("rust_library_host", RustLibraryHostFactory) | 
|  | 38 | android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory) | 
|  | 39 | android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory) | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 40 | android.RegisterModuleType("rust_ffi", RustFFIFactory) | 
|  | 41 | android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory) | 
|  | 42 | android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory) | 
|  | 43 | android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory) | 
|  | 44 | android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory) | 
|  | 45 | android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
|  | 48 | type VariantLibraryProperties struct { | 
| Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 49 | Enabled *bool    `android:"arch_variant"` | 
|  | 50 | Srcs    []string `android:"path,arch_variant"` | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
|  | 53 | type LibraryCompilerProperties struct { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 54 | Rlib   VariantLibraryProperties `android:"arch_variant"` | 
|  | 55 | Dylib  VariantLibraryProperties `android:"arch_variant"` | 
|  | 56 | Shared VariantLibraryProperties `android:"arch_variant"` | 
|  | 57 | Static VariantLibraryProperties `android:"arch_variant"` | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 58 |  | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 59 | // path to include directories to pass to cc_* modules, only relevant for static/shared variants. | 
|  | 60 | Include_dirs []string `android:"path,arch_variant"` | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 61 |  | 
|  | 62 | // Whether this library is part of the Rust toolchain sysroot. | 
|  | 63 | Sysroot *bool | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | type LibraryMutatedProperties struct { | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 67 | // Build a dylib variant | 
|  | 68 | BuildDylib bool `blueprint:"mutated"` | 
|  | 69 | // Build an rlib variant | 
|  | 70 | BuildRlib bool `blueprint:"mutated"` | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 71 | // Build a shared library variant | 
|  | 72 | BuildShared bool `blueprint:"mutated"` | 
|  | 73 | // Build a static library variant | 
|  | 74 | BuildStatic bool `blueprint:"mutated"` | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 75 |  | 
|  | 76 | // This variant is a dylib | 
|  | 77 | VariantIsDylib bool `blueprint:"mutated"` | 
|  | 78 | // This variant is an rlib | 
|  | 79 | VariantIsRlib bool `blueprint:"mutated"` | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 80 | // This variant is a shared library | 
|  | 81 | VariantIsShared bool `blueprint:"mutated"` | 
|  | 82 | // This variant is a static library | 
|  | 83 | VariantIsStatic bool `blueprint:"mutated"` | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 84 | // This variant is a source provider | 
|  | 85 | VariantIsSource bool `blueprint:"mutated"` | 
| Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 86 |  | 
|  | 87 | // This variant is disabled and should not be compiled | 
|  | 88 | // (used for SourceProvider variants that produce only source) | 
|  | 89 | VariantIsDisabled bool `blueprint:"mutated"` | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 90 |  | 
|  | 91 | // Whether this library variant should be link libstd via rlibs | 
|  | 92 | VariantIsStaticStd bool `blueprint:"mutated"` | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | type libraryDecorator struct { | 
|  | 96 | *baseCompiler | 
| Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 97 | *flagExporter | 
| Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 98 | stripper Stripper | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 99 |  | 
| Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 100 | Properties        LibraryCompilerProperties | 
|  | 101 | MutatedProperties LibraryMutatedProperties | 
|  | 102 | includeDirs       android.Paths | 
| Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 103 | sourceProvider    SourceProvider | 
| Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 104 |  | 
|  | 105 | collectedSnapshotHeaders android.Paths | 
| Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 106 |  | 
|  | 107 | // table-of-contents file for cdylib crates to optimize out relinking when possible | 
|  | 108 | tocFile android.OptionalPath | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | type libraryInterface interface { | 
|  | 112 | rlib() bool | 
|  | 113 | dylib() bool | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 114 | static() bool | 
|  | 115 | shared() bool | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 116 | sysroot() bool | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 117 | source() bool | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | // Returns true if the build options for the module have selected a particular build type | 
|  | 120 | buildRlib() bool | 
|  | 121 | buildDylib() bool | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 122 | buildShared() bool | 
|  | 123 | buildStatic() bool | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 124 |  | 
|  | 125 | // Sets a particular variant type | 
|  | 126 | setRlib() | 
|  | 127 | setDylib() | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 128 | setShared() | 
|  | 129 | setStatic() | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 130 | setSource() | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 131 |  | 
| Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 132 | // libstd linkage functions | 
|  | 133 | rlibStd() bool | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 134 | setRlibStd() | 
|  | 135 | setDylibStd() | 
|  | 136 |  | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 137 | // Build a specific library variant | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 138 | BuildOnlyFFI() | 
|  | 139 | BuildOnlyRust() | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 140 | BuildOnlyRlib() | 
|  | 141 | BuildOnlyDylib() | 
|  | 142 | BuildOnlyStatic() | 
|  | 143 | BuildOnlyShared() | 
| Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 144 |  | 
|  | 145 | toc() android.OptionalPath | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 148 | func (library *libraryDecorator) nativeCoverage() bool { | 
|  | 149 | return true | 
|  | 150 | } | 
|  | 151 |  | 
| Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 152 | func (library *libraryDecorator) toc() android.OptionalPath { | 
|  | 153 | return library.tocFile | 
|  | 154 | } | 
|  | 155 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 156 | func (library *libraryDecorator) rlib() bool { | 
|  | 157 | return library.MutatedProperties.VariantIsRlib | 
|  | 158 | } | 
|  | 159 |  | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 160 | func (library *libraryDecorator) sysroot() bool { | 
|  | 161 | return Bool(library.Properties.Sysroot) | 
|  | 162 | } | 
|  | 163 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 164 | func (library *libraryDecorator) dylib() bool { | 
|  | 165 | return library.MutatedProperties.VariantIsDylib | 
|  | 166 | } | 
|  | 167 |  | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 168 | func (library *libraryDecorator) shared() bool { | 
|  | 169 | return library.MutatedProperties.VariantIsShared | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | func (library *libraryDecorator) static() bool { | 
|  | 173 | return library.MutatedProperties.VariantIsStatic | 
|  | 174 | } | 
|  | 175 |  | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 176 | func (library *libraryDecorator) source() bool { | 
|  | 177 | return library.MutatedProperties.VariantIsSource | 
|  | 178 | } | 
|  | 179 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 180 | func (library *libraryDecorator) buildRlib() bool { | 
|  | 181 | return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true) | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | func (library *libraryDecorator) buildDylib() bool { | 
|  | 185 | return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true) | 
|  | 186 | } | 
|  | 187 |  | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 188 | func (library *libraryDecorator) buildShared() bool { | 
|  | 189 | return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true) | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | func (library *libraryDecorator) buildStatic() bool { | 
|  | 193 | return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true) | 
|  | 194 | } | 
|  | 195 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 196 | func (library *libraryDecorator) setRlib() { | 
|  | 197 | library.MutatedProperties.VariantIsRlib = true | 
|  | 198 | library.MutatedProperties.VariantIsDylib = false | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 199 | library.MutatedProperties.VariantIsStatic = false | 
|  | 200 | library.MutatedProperties.VariantIsShared = false | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
|  | 203 | func (library *libraryDecorator) setDylib() { | 
|  | 204 | library.MutatedProperties.VariantIsRlib = false | 
|  | 205 | library.MutatedProperties.VariantIsDylib = true | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 206 | library.MutatedProperties.VariantIsStatic = false | 
|  | 207 | library.MutatedProperties.VariantIsShared = false | 
|  | 208 | } | 
|  | 209 |  | 
| Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 210 | func (library *libraryDecorator) rlibStd() bool { | 
|  | 211 | return library.MutatedProperties.VariantIsStaticStd | 
|  | 212 | } | 
|  | 213 |  | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 214 | func (library *libraryDecorator) setRlibStd() { | 
|  | 215 | library.MutatedProperties.VariantIsStaticStd = true | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | func (library *libraryDecorator) setDylibStd() { | 
|  | 219 | library.MutatedProperties.VariantIsStaticStd = false | 
|  | 220 | } | 
|  | 221 |  | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 222 | func (library *libraryDecorator) setShared() { | 
|  | 223 | library.MutatedProperties.VariantIsStatic = false | 
|  | 224 | library.MutatedProperties.VariantIsShared = true | 
|  | 225 | library.MutatedProperties.VariantIsRlib = false | 
|  | 226 | library.MutatedProperties.VariantIsDylib = false | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | func (library *libraryDecorator) setStatic() { | 
|  | 230 | library.MutatedProperties.VariantIsStatic = true | 
|  | 231 | library.MutatedProperties.VariantIsShared = false | 
|  | 232 | library.MutatedProperties.VariantIsRlib = false | 
|  | 233 | library.MutatedProperties.VariantIsDylib = false | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 234 | } | 
|  | 235 |  | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 236 | func (library *libraryDecorator) setSource() { | 
|  | 237 | library.MutatedProperties.VariantIsSource = true | 
|  | 238 | } | 
|  | 239 |  | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 240 | func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { | 
| Ivan Lozano | add122a | 2023-07-13 11:01:41 -0400 | [diff] [blame] | 241 | if library.preferRlib() { | 
| Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 242 | return rlibAutoDep | 
|  | 243 | } else if library.rlib() || library.static() { | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 244 | return rlibAutoDep | 
|  | 245 | } else if library.dylib() || library.shared() { | 
|  | 246 | return dylibAutoDep | 
|  | 247 | } else { | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 248 | 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] | 249 | } | 
|  | 250 | } | 
|  | 251 |  | 
| Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 252 | func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage { | 
| Ivan Lozano | add122a | 2023-07-13 11:01:41 -0400 | [diff] [blame] | 253 | if library.static() || library.MutatedProperties.VariantIsStaticStd { | 
| Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 254 | return RlibLinkage | 
|  | 255 | } else if library.baseCompiler.preferRlib() { | 
|  | 256 | return RlibLinkage | 
|  | 257 | } | 
|  | 258 | return DefaultLinkage | 
|  | 259 | } | 
|  | 260 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 261 | var _ compiler = (*libraryDecorator)(nil) | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 262 | var _ libraryInterface = (*libraryDecorator)(nil) | 
| Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 263 | var _ exportedFlagsProducer = (*libraryDecorator)(nil) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 264 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 265 | // rust_library produces all Rust variants (rust_library_dylib and | 
|  | 266 | // rust_library_rlib). | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 267 | func RustLibraryFactory() android.Module { | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 268 | module, library := NewRustLibrary(android.HostAndDeviceSupported) | 
|  | 269 | library.BuildOnlyRust() | 
|  | 270 | return module.Init() | 
|  | 271 | } | 
|  | 272 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 273 | // rust_ffi produces all FFI variants (rust_ffi_shared and | 
|  | 274 | // rust_ffi_static). | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 275 | func RustFFIFactory() android.Module { | 
|  | 276 | module, library := NewRustLibrary(android.HostAndDeviceSupported) | 
|  | 277 | library.BuildOnlyFFI() | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 278 | return module.Init() | 
|  | 279 | } | 
|  | 280 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 281 | // rust_library_dylib produces a Rust dylib (Rust crate type "dylib"). | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 282 | func RustLibraryDylibFactory() android.Module { | 
|  | 283 | module, library := NewRustLibrary(android.HostAndDeviceSupported) | 
|  | 284 | library.BuildOnlyDylib() | 
|  | 285 | return module.Init() | 
|  | 286 | } | 
|  | 287 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 288 | // rust_library_rlib produces an rlib (Rust crate type "rlib"). | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 289 | func RustLibraryRlibFactory() android.Module { | 
|  | 290 | module, library := NewRustLibrary(android.HostAndDeviceSupported) | 
|  | 291 | library.BuildOnlyRlib() | 
|  | 292 | return module.Init() | 
|  | 293 | } | 
|  | 294 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 295 | // rust_ffi_shared produces a shared library (Rust crate type | 
|  | 296 | // "cdylib"). | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 297 | func RustFFISharedFactory() android.Module { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 298 | module, library := NewRustLibrary(android.HostAndDeviceSupported) | 
|  | 299 | library.BuildOnlyShared() | 
|  | 300 | return module.Init() | 
|  | 301 | } | 
|  | 302 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 303 | // rust_ffi_static produces a static library (Rust crate type | 
|  | 304 | // "staticlib"). | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 305 | func RustFFIStaticFactory() android.Module { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 306 | module, library := NewRustLibrary(android.HostAndDeviceSupported) | 
|  | 307 | library.BuildOnlyStatic() | 
|  | 308 | return module.Init() | 
|  | 309 | } | 
|  | 310 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 311 | // rust_library_host produces all Rust variants for the host | 
|  | 312 | // (rust_library_dylib_host and rust_library_rlib_host). | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 313 | func RustLibraryHostFactory() android.Module { | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 314 | module, library := NewRustLibrary(android.HostSupported) | 
|  | 315 | library.BuildOnlyRust() | 
|  | 316 | return module.Init() | 
|  | 317 | } | 
|  | 318 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 319 | // rust_ffi_host produces all FFI variants for the host | 
|  | 320 | // (rust_ffi_static_host and rust_ffi_shared_host). | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 321 | func RustFFIHostFactory() android.Module { | 
|  | 322 | module, library := NewRustLibrary(android.HostSupported) | 
|  | 323 | library.BuildOnlyFFI() | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 324 | return module.Init() | 
|  | 325 | } | 
|  | 326 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 327 | // rust_library_dylib_host produces a dylib for the host (Rust crate | 
|  | 328 | // type "dylib"). | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 329 | func RustLibraryDylibHostFactory() android.Module { | 
|  | 330 | module, library := NewRustLibrary(android.HostSupported) | 
|  | 331 | library.BuildOnlyDylib() | 
|  | 332 | return module.Init() | 
|  | 333 | } | 
|  | 334 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 335 | // rust_library_rlib_host produces an rlib for the host (Rust crate | 
|  | 336 | // type "rlib"). | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 337 | func RustLibraryRlibHostFactory() android.Module { | 
|  | 338 | module, library := NewRustLibrary(android.HostSupported) | 
|  | 339 | library.BuildOnlyRlib() | 
|  | 340 | return module.Init() | 
|  | 341 | } | 
|  | 342 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 343 | // rust_ffi_static_host produces a static library for the host (Rust | 
|  | 344 | // crate type "staticlib"). | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 345 | func RustFFIStaticHostFactory() android.Module { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 346 | module, library := NewRustLibrary(android.HostSupported) | 
|  | 347 | library.BuildOnlyStatic() | 
|  | 348 | return module.Init() | 
|  | 349 | } | 
|  | 350 |  | 
| Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 351 | // rust_ffi_shared_host produces an shared library for the host (Rust | 
|  | 352 | // crate type "cdylib"). | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 353 | func RustFFISharedHostFactory() android.Module { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 354 | module, library := NewRustLibrary(android.HostSupported) | 
|  | 355 | library.BuildOnlyShared() | 
|  | 356 | return module.Init() | 
|  | 357 | } | 
|  | 358 |  | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 359 | func (library *libraryDecorator) BuildOnlyFFI() { | 
|  | 360 | library.MutatedProperties.BuildDylib = false | 
|  | 361 | library.MutatedProperties.BuildRlib = false | 
|  | 362 | library.MutatedProperties.BuildShared = true | 
|  | 363 | library.MutatedProperties.BuildStatic = true | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | func (library *libraryDecorator) BuildOnlyRust() { | 
|  | 367 | library.MutatedProperties.BuildDylib = true | 
|  | 368 | library.MutatedProperties.BuildRlib = true | 
|  | 369 | library.MutatedProperties.BuildShared = false | 
|  | 370 | library.MutatedProperties.BuildStatic = false | 
|  | 371 | } | 
|  | 372 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 373 | func (library *libraryDecorator) BuildOnlyDylib() { | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 374 | library.MutatedProperties.BuildDylib = true | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 375 | library.MutatedProperties.BuildRlib = false | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 376 | library.MutatedProperties.BuildShared = false | 
|  | 377 | library.MutatedProperties.BuildStatic = false | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 378 | } | 
|  | 379 |  | 
|  | 380 | func (library *libraryDecorator) BuildOnlyRlib() { | 
|  | 381 | library.MutatedProperties.BuildDylib = false | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 382 | library.MutatedProperties.BuildRlib = true | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 383 | library.MutatedProperties.BuildShared = false | 
|  | 384 | library.MutatedProperties.BuildStatic = false | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | func (library *libraryDecorator) BuildOnlyStatic() { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 388 | library.MutatedProperties.BuildRlib = false | 
|  | 389 | library.MutatedProperties.BuildDylib = false | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 390 | library.MutatedProperties.BuildShared = false | 
|  | 391 | library.MutatedProperties.BuildStatic = true | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
|  | 394 | func (library *libraryDecorator) BuildOnlyShared() { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 395 | library.MutatedProperties.BuildRlib = false | 
|  | 396 | library.MutatedProperties.BuildDylib = false | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 397 | library.MutatedProperties.BuildStatic = false | 
|  | 398 | library.MutatedProperties.BuildShared = true | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 399 | } | 
|  | 400 |  | 
|  | 401 | func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { | 
| Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 402 | module := newModule(hod, android.MultilibBoth) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 403 |  | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 404 | android.InitBazelModule(module) | 
|  | 405 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 406 | library := &libraryDecorator{ | 
|  | 407 | MutatedProperties: LibraryMutatedProperties{ | 
| Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 408 | BuildDylib:  false, | 
|  | 409 | BuildRlib:   false, | 
|  | 410 | BuildShared: false, | 
|  | 411 | BuildStatic: false, | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 412 | }, | 
| Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 413 | baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem), | 
| Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 414 | flagExporter: NewFlagExporter(), | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
|  | 417 | module.compiler = library | 
|  | 418 |  | 
|  | 419 | return module, library | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | func (library *libraryDecorator) compilerProps() []interface{} { | 
|  | 423 | return append(library.baseCompiler.compilerProps(), | 
|  | 424 | &library.Properties, | 
| Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 425 | &library.MutatedProperties, | 
|  | 426 | &library.stripper.StripProperties) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 427 | } | 
|  | 428 |  | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 429 | func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { | 
|  | 430 | deps = library.baseCompiler.compilerDeps(ctx, deps) | 
|  | 431 |  | 
| Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 432 | if library.dylib() || library.shared() { | 
|  | 433 | if ctx.toolchain().Bionic() { | 
|  | 434 | deps = bionicDeps(ctx, deps, false) | 
|  | 435 | deps.CrtBegin = []string{"crtbegin_so"} | 
|  | 436 | deps.CrtEnd = []string{"crtend_so"} | 
|  | 437 | } else if ctx.Os() == android.LinuxMusl { | 
|  | 438 | deps = muslDeps(ctx, deps, false) | 
|  | 439 | deps.CrtBegin = []string{"libc_musl_crtbegin_so"} | 
|  | 440 | deps.CrtEnd = []string{"libc_musl_crtend_so"} | 
|  | 441 | } | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
|  | 444 | return deps | 
|  | 445 | } | 
| Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 446 |  | 
|  | 447 | func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string { | 
|  | 448 | return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix() | 
|  | 449 | } | 
|  | 450 |  | 
| Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 451 | func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags { | 
|  | 452 | flags = library.baseCompiler.cfgFlags(ctx, flags) | 
| Stephen Crane | 0dbfc56 | 2021-07-07 19:05:02 -0700 | [diff] [blame] | 453 | if library.dylib() { | 
|  | 454 | // We need to add a dependency on std in order to link crates as dylibs. | 
|  | 455 | // The hack to add this dependency is guarded by the following cfg so | 
|  | 456 | // that we don't force a dependency when it isn't needed. | 
|  | 457 | library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib") | 
|  | 458 | } | 
| Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 459 |  | 
|  | 460 | flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...) | 
|  | 461 | flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...) | 
|  | 462 |  | 
|  | 463 | return flags | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { | 
| Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 467 | flags = library.baseCompiler.compilerFlags(ctx, flags) | 
| Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 468 |  | 
|  | 469 | flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName()) | 
| Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 470 | if library.shared() || library.static() { | 
|  | 471 | library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...) | 
|  | 472 | } | 
| Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 473 | if library.shared() { | 
| A. Cody Schuffelen | c183e3a | 2023-08-14 21:09:47 -0700 | [diff] [blame] | 474 | if ctx.Darwin() { | 
|  | 475 | flags.LinkFlags = append( | 
|  | 476 | flags.LinkFlags, | 
|  | 477 | "-dynamic_lib", | 
|  | 478 | "-install_name @rpath/"+library.sharedLibFilename(ctx), | 
|  | 479 | ) | 
|  | 480 | } else { | 
|  | 481 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx)) | 
|  | 482 | } | 
| Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 483 | } | 
|  | 484 |  | 
| Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 485 | return flags | 
|  | 486 | } | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 487 |  | 
| Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 488 | func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { | 
|  | 489 | var outputFile android.ModuleOutPath | 
|  | 490 | var ret buildOutput | 
| Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 491 | var fileName string | 
| Matthew Maurer | d221d31 | 2023-11-20 21:02:40 +0000 | [diff] [blame] | 492 | crateRootPath := library.crateRootPath(ctx) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 493 |  | 
| Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 494 | if library.sourceProvider != nil { | 
| Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 495 | deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...) | 
| Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 496 | } | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 497 |  | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 498 | // Calculate output filename | 
|  | 499 | if library.rlib() { | 
|  | 500 | fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix() | 
|  | 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.dylib() { | 
|  | 504 | fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix() | 
|  | 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.static() { | 
|  | 508 | fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix() | 
|  | 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 | } else if library.shared() { | 
|  | 512 | fileName = library.sharedLibFilename(ctx) | 
|  | 513 | outputFile = android.PathForModuleOut(ctx, fileName) | 
| Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 514 | ret.outputFile = outputFile | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 515 | } | 
|  | 516 |  | 
|  | 517 | if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) { | 
|  | 518 | strippedOutputFile := outputFile | 
|  | 519 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) | 
|  | 520 | library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile) | 
|  | 521 |  | 
|  | 522 | library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile) | 
|  | 523 | } | 
|  | 524 | library.baseCompiler.unstrippedOutputFile = outputFile | 
|  | 525 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 526 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) | 
| Ivan Lozano | 3dfa12d | 2021-02-04 11:29:41 -0500 | [diff] [blame] | 527 | flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...) | 
| Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 528 | flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 529 |  | 
| Matthew Maurer | 46c46cc | 2020-01-13 16:34:34 -0800 | [diff] [blame] | 530 | if library.dylib() { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 531 | // We need prefer-dynamic for now to avoid linking in the static stdlib. See: | 
|  | 532 | // https://github.com/rust-lang/rust/issues/19680 | 
|  | 533 | // https://github.com/rust-lang/rust/issues/34909 | 
|  | 534 | flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic") | 
|  | 535 | } | 
|  | 536 |  | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 537 | // Call the appropriate builder for this library type | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 538 | if library.rlib() { | 
| Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 539 | ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 540 | } else if library.dylib() { | 
| Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 541 | ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 542 | } else if library.static() { | 
| Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 543 | ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 544 | } else if library.shared() { | 
| Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 545 | ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 546 | } | 
|  | 547 |  | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 548 | if library.rlib() || library.dylib() { | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 549 | library.flagExporter.exportLinkDirs(deps.linkDirs...) | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 550 | library.flagExporter.exportLinkObjects(deps.linkObjects...) | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 551 | } | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 552 |  | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 553 | if library.static() || library.shared() { | 
|  | 554 | ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{ | 
|  | 555 | IncludeDirs: library.includeDirs, | 
|  | 556 | }) | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | if library.shared() { | 
| Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 560 | // Optimize out relinking against shared libraries whose interface hasn't changed by | 
|  | 561 | // depending on a table of contents file instead of the library itself. | 
|  | 562 | tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc") | 
|  | 563 | library.tocFile = android.OptionalPathForPath(tocFile) | 
|  | 564 | cc.TransformSharedObjectToToc(ctx, outputFile, tocFile) | 
|  | 565 |  | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 566 | ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{ | 
| Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 567 | TableOfContents: android.OptionalPathForPath(tocFile), | 
|  | 568 | SharedLibrary:   outputFile, | 
|  | 569 | Target:          ctx.Target(), | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 570 | }) | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | if library.static() { | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 574 | depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputFile).Build() | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 575 | ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{ | 
|  | 576 | StaticLibrary: outputFile, | 
|  | 577 |  | 
|  | 578 | TransitiveStaticLibrariesForOrdering: depSet, | 
|  | 579 | }) | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | library.flagExporter.setProvider(ctx) | 
|  | 583 |  | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 584 | return ret | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 585 | } | 
|  | 586 |  | 
| Matthew Maurer | d221d31 | 2023-11-20 21:02:40 +0000 | [diff] [blame] | 587 | func (library *libraryDecorator) crateRootPath(ctx ModuleContext) android.Path { | 
| Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 588 | if library.sourceProvider != nil { | 
|  | 589 | // Assume the first source from the source provider is the library entry point. | 
|  | 590 | return library.sourceProvider.Srcs()[0] | 
| Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 591 | } else { | 
| Matthew Maurer | d221d31 | 2023-11-20 21:02:40 +0000 | [diff] [blame] | 592 | return library.baseCompiler.crateRootPath(ctx) | 
| 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 | d221d31 | 2023-11-20 21:02:40 +0000 | [diff] [blame] | 607 | return android.OptionalPathForPath(Rustdoc(ctx, library.crateRootPath(ctx), | 
| 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 | } | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 799 |  | 
|  | 800 | type rustLibraryAttributes struct { | 
| Vinh Tran | 7515994 | 2023-10-17 16:28:04 -0400 | [diff] [blame] | 801 | commonLibraryAttrs | 
|  | 802 | } | 
|  | 803 |  | 
|  | 804 | type commonLibraryAttrs struct { | 
| Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 805 | Srcs            bazel.LabelListAttribute | 
|  | 806 | Compile_data    bazel.LabelListAttribute | 
|  | 807 | Crate_name      bazel.StringAttribute | 
|  | 808 | Edition         bazel.StringAttribute | 
|  | 809 | Crate_features  bazel.StringListAttribute | 
|  | 810 | Deps            bazel.LabelListAttribute | 
|  | 811 | Rustc_flags     bazel.StringListAttribute | 
|  | 812 | Proc_macro_deps bazel.LabelListAttribute | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 813 | } | 
|  | 814 |  | 
| Vinh Tran | 7515994 | 2023-10-17 16:28:04 -0400 | [diff] [blame] | 815 | func commonLibraryAttrsBp2build(ctx android.Bp2buildMutatorContext, m *Module) *commonLibraryAttrs { | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 816 | lib := m.compiler.(*libraryDecorator) | 
| Vinh Tran | 9b84678 | 2023-08-24 12:55:12 -0400 | [diff] [blame] | 817 |  | 
| Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 818 | srcs, compileData := srcsAndCompileDataAttrs(ctx, *lib.baseCompiler) | 
| Vinh Tran | 9b84678 | 2023-08-24 12:55:12 -0400 | [diff] [blame] | 819 |  | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 820 | deps := android.BazelLabelForModuleDeps(ctx, append( | 
|  | 821 | lib.baseCompiler.Properties.Rustlibs, | 
|  | 822 | lib.baseCompiler.Properties.Rlibs..., | 
|  | 823 | )) | 
| Vinh Tran | 9b84678 | 2023-08-24 12:55:12 -0400 | [diff] [blame] | 824 |  | 
|  | 825 | cargoBuildScript := cargoBuildScriptBp2build(ctx, m) | 
|  | 826 | if cargoBuildScript != nil { | 
|  | 827 | deps.Add(&bazel.Label{ | 
|  | 828 | Label: ":" + *cargoBuildScript, | 
|  | 829 | }) | 
|  | 830 | } | 
|  | 831 |  | 
| Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 832 | procMacroDeps := android.BazelLabelForModuleDeps(ctx, lib.baseCompiler.Properties.Proc_macros) | 
|  | 833 |  | 
|  | 834 | var rustcFLags []string | 
|  | 835 | for _, cfg := range lib.baseCompiler.Properties.Cfgs { | 
|  | 836 | rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg)) | 
|  | 837 | } | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 838 |  | 
| Vinh Tran | 7515994 | 2023-10-17 16:28:04 -0400 | [diff] [blame] | 839 | return &commonLibraryAttrs{ | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 840 | Srcs: bazel.MakeLabelListAttribute( | 
|  | 841 | srcs, | 
|  | 842 | ), | 
|  | 843 | Compile_data: bazel.MakeLabelListAttribute( | 
|  | 844 | compileData, | 
|  | 845 | ), | 
|  | 846 | Crate_name: bazel.StringAttribute{ | 
|  | 847 | Value: &lib.baseCompiler.Properties.Crate_name, | 
|  | 848 | }, | 
|  | 849 | Edition: bazel.StringAttribute{ | 
|  | 850 | Value: lib.baseCompiler.Properties.Edition, | 
|  | 851 | }, | 
|  | 852 | Crate_features: bazel.StringListAttribute{ | 
|  | 853 | Value: lib.baseCompiler.Properties.Features, | 
|  | 854 | }, | 
|  | 855 | Deps: bazel.MakeLabelListAttribute( | 
|  | 856 | deps, | 
|  | 857 | ), | 
| Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 858 | Proc_macro_deps: bazel.MakeLabelListAttribute( | 
|  | 859 | procMacroDeps, | 
|  | 860 | ), | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 861 | Rustc_flags: bazel.StringListAttribute{ | 
|  | 862 | Value: append( | 
|  | 863 | rustcFLags, | 
|  | 864 | lib.baseCompiler.Properties.Flags..., | 
|  | 865 | ), | 
|  | 866 | }, | 
|  | 867 | } | 
|  | 868 |  | 
| Vinh Tran | 7515994 | 2023-10-17 16:28:04 -0400 | [diff] [blame] | 869 | } | 
|  | 870 |  | 
|  | 871 | func libraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { | 
| Vinh Tran | fb8c5a5 | 2023-10-12 15:28:14 -0400 | [diff] [blame] | 872 | ctx.CreateBazelTargetModule( | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 873 | bazel.BazelTargetModuleProperties{ | 
|  | 874 | Rule_class:        "rust_library", | 
|  | 875 | Bzl_load_location: "@rules_rust//rust:defs.bzl", | 
|  | 876 | }, | 
|  | 877 | android.CommonAttributes{ | 
|  | 878 | Name: m.Name(), | 
|  | 879 | }, | 
| Vinh Tran | 7515994 | 2023-10-17 16:28:04 -0400 | [diff] [blame] | 880 | commonLibraryAttrsBp2build(ctx, m), | 
| Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 881 | ) | 
|  | 882 | } | 
| Vinh Tran | 9b84678 | 2023-08-24 12:55:12 -0400 | [diff] [blame] | 883 |  | 
|  | 884 | type cargoBuildScriptAttributes struct { | 
|  | 885 | Srcs    bazel.LabelListAttribute | 
|  | 886 | Edition bazel.StringAttribute | 
|  | 887 | Version bazel.StringAttribute | 
|  | 888 | } | 
|  | 889 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 890 | func cargoBuildScriptBp2build(ctx android.Bp2buildMutatorContext, m *Module) *string { | 
| Vinh Tran | 9b84678 | 2023-08-24 12:55:12 -0400 | [diff] [blame] | 891 | // Soong treats some crates like libprotobuf as special in that they have | 
|  | 892 | // cargo build script ran to produce an out folder and check it into AOSP | 
|  | 893 | // For example, https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/out/ | 
|  | 894 | // is produced by cargo build script https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/build.rs | 
|  | 895 | // The out folder is then fed into `rust_library` by a genrule | 
|  | 896 | // https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/Android.bp;l=22 | 
|  | 897 | // This allows Soong to decouple from cargo completely. | 
|  | 898 |  | 
|  | 899 | // Soong decouples from cargo so that it has control over cc compilation. | 
|  | 900 | // https://cs.android.com/android/platform/superproject/main/+/main:development/scripts/cargo2android.py;l=1033-1041;drc=8449944a50a0445a5ecaf9b7aed12608c81bf3f1 | 
|  | 901 | // generates a `cc_library_static` module to have custom cc flags. | 
|  | 902 | // Since bp2build will convert the cc modules to cc targets which include the cflags, | 
|  | 903 | // Bazel does not need to have this optimization. | 
|  | 904 |  | 
|  | 905 | // Performance-wise: rust_library -> cargo_build_script vs rust_library -> genrule (like Soong) | 
|  | 906 | // don't have any major difference in build time in Bazel. So using cargo_build_script does not slow | 
|  | 907 | // down the build. | 
|  | 908 |  | 
|  | 909 | // The benefit of using `cargo_build_script` here is that it would take care of setting correct | 
|  | 910 | // `OUT_DIR` for us - similar to what Soong does here | 
|  | 911 | // https://cs.android.com/android/platform/superproject/main/+/main:build/soong/rust/builder.go;l=202-218;drc=f29ca58e88c5846bbe8955e5192135e5ab4f14a1 | 
|  | 912 |  | 
|  | 913 | // TODO(b/297364081): cargo2android.py has logic for when generate/not cc_library_static and out directory | 
|  | 914 | // bp2build might be able use the same logic for when to use `cargo_build_script`. | 
|  | 915 | // For now, we're building libprotobuf_build_script as a one-off until we have a more principled solution | 
|  | 916 | if m.Name() != "libprotobuf" { | 
|  | 917 | return nil | 
|  | 918 | } | 
|  | 919 |  | 
|  | 920 | lib := m.compiler.(*libraryDecorator) | 
|  | 921 |  | 
|  | 922 | name := m.Name() + "_build_script" | 
|  | 923 | attrs := &cargoBuildScriptAttributes{ | 
|  | 924 | Srcs: bazel.MakeLabelListAttribute( | 
|  | 925 | android.BazelLabelForModuleSrc(ctx, []string{"build.rs"}), | 
|  | 926 | ), | 
|  | 927 | Edition: bazel.StringAttribute{ | 
|  | 928 | Value: lib.baseCompiler.Properties.Edition, | 
|  | 929 | }, | 
|  | 930 | Version: bazel.StringAttribute{ | 
|  | 931 | Value: lib.baseCompiler.Properties.Cargo_pkg_version, | 
|  | 932 | }, | 
|  | 933 | } | 
|  | 934 |  | 
|  | 935 | // TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented | 
|  | 936 | var restriction bazel.BoolAttribute | 
|  | 937 | restriction.SetSelectValue(bazel.OsConfigurationAxis, "android", proptools.BoolPtr(false)) | 
|  | 938 |  | 
|  | 939 | ctx.CreateBazelTargetModuleWithRestrictions( | 
|  | 940 | bazel.BazelTargetModuleProperties{ | 
|  | 941 | Rule_class:        "cargo_build_script", | 
|  | 942 | Bzl_load_location: "@rules_rust//cargo:cargo_build_script.bzl", | 
|  | 943 | }, | 
|  | 944 | android.CommonAttributes{ | 
|  | 945 | Name: name, | 
|  | 946 | }, | 
|  | 947 | attrs, | 
|  | 948 | restriction, | 
|  | 949 | ) | 
|  | 950 |  | 
|  | 951 | return &name | 
|  | 952 | } | 
| Vinh Tran | 7515994 | 2023-10-17 16:28:04 -0400 | [diff] [blame] | 953 |  | 
|  | 954 | type ffiStaticAttributes struct { | 
|  | 955 | commonLibraryAttrs | 
|  | 956 | Export_includes bazel.StringListAttribute | 
|  | 957 | } | 
|  | 958 |  | 
|  | 959 | func ffiStaticBp2build(ctx android.Bp2buildMutatorContext, m *Module) { | 
|  | 960 | lib := m.compiler.(*libraryDecorator) | 
|  | 961 |  | 
|  | 962 | attrs := &ffiStaticAttributes{ | 
|  | 963 | Export_includes: bazel.StringListAttribute{ | 
|  | 964 | Value: lib.Properties.Include_dirs, | 
|  | 965 | }, | 
|  | 966 | commonLibraryAttrs: *commonLibraryAttrsBp2build(ctx, m), | 
|  | 967 | } | 
|  | 968 |  | 
|  | 969 | ctx.CreateBazelTargetModule( | 
|  | 970 | bazel.BazelTargetModuleProperties{ | 
|  | 971 | Rule_class:        "rust_ffi_static", | 
|  | 972 | Bzl_load_location: "//build/bazel/rules/rust:rust_ffi_static.bzl", | 
|  | 973 | }, | 
|  | 974 | android.CommonAttributes{ | 
|  | 975 | Name: m.Name(), | 
|  | 976 | }, | 
|  | 977 | attrs, | 
|  | 978 | ) | 
|  | 979 | } |