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