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 ( |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 18 | "regexp" |
| 19 | "strings" |
| 20 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 24 | var ( |
| 25 | DylibStdlibSuffix = ".dylib-std" |
| 26 | RlibStdlibSuffix = ".rlib-std" |
| 27 | ) |
| 28 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 29 | func init() { |
| 30 | android.RegisterModuleType("rust_library", RustLibraryFactory) |
| 31 | android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory) |
| 32 | android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory) |
| 33 | android.RegisterModuleType("rust_library_host", RustLibraryHostFactory) |
| 34 | android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory) |
| 35 | android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory) |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 36 | android.RegisterModuleType("rust_ffi", RustFFIFactory) |
| 37 | android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory) |
| 38 | android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory) |
| 39 | android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory) |
| 40 | android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory) |
| 41 | android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | type VariantLibraryProperties struct { |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 45 | Enabled *bool `android:"arch_variant"` |
| 46 | Srcs []string `android:"path,arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | type LibraryCompilerProperties struct { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 50 | Rlib VariantLibraryProperties `android:"arch_variant"` |
| 51 | Dylib VariantLibraryProperties `android:"arch_variant"` |
| 52 | Shared VariantLibraryProperties `android:"arch_variant"` |
| 53 | Static VariantLibraryProperties `android:"arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 54 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 55 | // path to include directories to pass to cc_* modules, only relevant for static/shared variants. |
| 56 | Include_dirs []string `android:"path,arch_variant"` |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 57 | |
| 58 | // Whether this library is part of the Rust toolchain sysroot. |
| 59 | Sysroot *bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type LibraryMutatedProperties struct { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 63 | // Build a dylib variant |
| 64 | BuildDylib bool `blueprint:"mutated"` |
| 65 | // Build an rlib variant |
| 66 | BuildRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 67 | // Build a shared library variant |
| 68 | BuildShared bool `blueprint:"mutated"` |
| 69 | // Build a static library variant |
| 70 | BuildStatic bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 71 | |
| 72 | // This variant is a dylib |
| 73 | VariantIsDylib bool `blueprint:"mutated"` |
| 74 | // This variant is an rlib |
| 75 | VariantIsRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 76 | // This variant is a shared library |
| 77 | VariantIsShared bool `blueprint:"mutated"` |
| 78 | // This variant is a static library |
| 79 | VariantIsStatic bool `blueprint:"mutated"` |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 80 | |
| 81 | // This variant is disabled and should not be compiled |
| 82 | // (used for SourceProvider variants that produce only source) |
| 83 | VariantIsDisabled bool `blueprint:"mutated"` |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 84 | |
| 85 | // Whether this library variant should be link libstd via rlibs |
| 86 | VariantIsStaticStd bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | type libraryDecorator struct { |
| 90 | *baseCompiler |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 91 | *flagExporter |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 92 | stripper Stripper |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 93 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 94 | Properties LibraryCompilerProperties |
| 95 | MutatedProperties LibraryMutatedProperties |
| 96 | includeDirs android.Paths |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 97 | sourceProvider SourceProvider |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | type libraryInterface interface { |
| 101 | rlib() bool |
| 102 | dylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 103 | static() bool |
| 104 | shared() bool |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 105 | sysroot() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 106 | |
| 107 | // Returns true if the build options for the module have selected a particular build type |
| 108 | buildRlib() bool |
| 109 | buildDylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 110 | buildShared() bool |
| 111 | buildStatic() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 112 | |
| 113 | // Sets a particular variant type |
| 114 | setRlib() |
| 115 | setDylib() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 116 | setShared() |
| 117 | setStatic() |
| 118 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 119 | // Set libstd linkage |
| 120 | setRlibStd() |
| 121 | setDylibStd() |
| 122 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 123 | // Build a specific library variant |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 124 | BuildOnlyFFI() |
| 125 | BuildOnlyRust() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 126 | BuildOnlyRlib() |
| 127 | BuildOnlyDylib() |
| 128 | BuildOnlyStatic() |
| 129 | BuildOnlyShared() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 132 | func (library *libraryDecorator) nativeCoverage() bool { |
| 133 | return true |
| 134 | } |
| 135 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 136 | func (library *libraryDecorator) rlib() bool { |
| 137 | return library.MutatedProperties.VariantIsRlib |
| 138 | } |
| 139 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 140 | func (library *libraryDecorator) sysroot() bool { |
| 141 | return Bool(library.Properties.Sysroot) |
| 142 | } |
| 143 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 144 | func (library *libraryDecorator) dylib() bool { |
| 145 | return library.MutatedProperties.VariantIsDylib |
| 146 | } |
| 147 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 148 | func (library *libraryDecorator) shared() bool { |
| 149 | return library.MutatedProperties.VariantIsShared |
| 150 | } |
| 151 | |
| 152 | func (library *libraryDecorator) static() bool { |
| 153 | return library.MutatedProperties.VariantIsStatic |
| 154 | } |
| 155 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 156 | func (library *libraryDecorator) staticStd(ctx *depsContext) bool { |
| 157 | // libraries should only request the staticStd when building a static FFI or when variant is staticStd |
| 158 | return library.static() || library.MutatedProperties.VariantIsStaticStd |
| 159 | } |
| 160 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 161 | func (library *libraryDecorator) buildRlib() bool { |
| 162 | return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true) |
| 163 | } |
| 164 | |
| 165 | func (library *libraryDecorator) buildDylib() bool { |
| 166 | return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true) |
| 167 | } |
| 168 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 169 | func (library *libraryDecorator) buildShared() bool { |
| 170 | return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true) |
| 171 | } |
| 172 | |
| 173 | func (library *libraryDecorator) buildStatic() bool { |
| 174 | return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true) |
| 175 | } |
| 176 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 177 | func (library *libraryDecorator) setRlib() { |
| 178 | library.MutatedProperties.VariantIsRlib = true |
| 179 | library.MutatedProperties.VariantIsDylib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 180 | library.MutatedProperties.VariantIsStatic = false |
| 181 | library.MutatedProperties.VariantIsShared = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | func (library *libraryDecorator) setDylib() { |
| 185 | library.MutatedProperties.VariantIsRlib = false |
| 186 | library.MutatedProperties.VariantIsDylib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 187 | library.MutatedProperties.VariantIsStatic = false |
| 188 | library.MutatedProperties.VariantIsShared = false |
| 189 | } |
| 190 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 191 | func (library *libraryDecorator) setRlibStd() { |
| 192 | library.MutatedProperties.VariantIsStaticStd = true |
| 193 | } |
| 194 | |
| 195 | func (library *libraryDecorator) setDylibStd() { |
| 196 | library.MutatedProperties.VariantIsStaticStd = false |
| 197 | } |
| 198 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 199 | func (library *libraryDecorator) setShared() { |
| 200 | library.MutatedProperties.VariantIsStatic = false |
| 201 | library.MutatedProperties.VariantIsShared = true |
| 202 | library.MutatedProperties.VariantIsRlib = false |
| 203 | library.MutatedProperties.VariantIsDylib = false |
| 204 | } |
| 205 | |
| 206 | func (library *libraryDecorator) setStatic() { |
| 207 | library.MutatedProperties.VariantIsStatic = true |
| 208 | library.MutatedProperties.VariantIsShared = false |
| 209 | library.MutatedProperties.VariantIsRlib = false |
| 210 | library.MutatedProperties.VariantIsDylib = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 213 | func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 214 | if library.rlib() || library.static() { |
| 215 | return rlibAutoDep |
| 216 | } else if library.dylib() || library.shared() { |
| 217 | return dylibAutoDep |
| 218 | } else { |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 219 | panic("autoDep called on library" + ctx.ModuleName() + "that has no enabled variants.") |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 223 | var _ compiler = (*libraryDecorator)(nil) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 224 | var _ libraryInterface = (*libraryDecorator)(nil) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 225 | var _ exportedFlagsProducer = (*libraryDecorator)(nil) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 226 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 227 | // rust_library produces all rust variants. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 228 | func RustLibraryFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 229 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 230 | library.BuildOnlyRust() |
| 231 | return module.Init() |
| 232 | } |
| 233 | |
| 234 | // rust_ffi produces all ffi variants. |
| 235 | func RustFFIFactory() android.Module { |
| 236 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 237 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 238 | return module.Init() |
| 239 | } |
| 240 | |
| 241 | // rust_library_dylib produces a dylib. |
| 242 | func RustLibraryDylibFactory() android.Module { |
| 243 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 244 | library.BuildOnlyDylib() |
| 245 | return module.Init() |
| 246 | } |
| 247 | |
| 248 | // rust_library_rlib produces an rlib. |
| 249 | func RustLibraryRlibFactory() android.Module { |
| 250 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 251 | library.BuildOnlyRlib() |
| 252 | return module.Init() |
| 253 | } |
| 254 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 255 | // rust_ffi_shared produces a shared library. |
| 256 | func RustFFISharedFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 257 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 258 | library.BuildOnlyShared() |
| 259 | return module.Init() |
| 260 | } |
| 261 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 262 | // rust_ffi_static produces a static library. |
| 263 | func RustFFIStaticFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 264 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 265 | library.BuildOnlyStatic() |
| 266 | return module.Init() |
| 267 | } |
| 268 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 269 | // rust_library_host produces all rust variants. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 270 | func RustLibraryHostFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 271 | module, library := NewRustLibrary(android.HostSupported) |
| 272 | library.BuildOnlyRust() |
| 273 | return module.Init() |
| 274 | } |
| 275 | |
| 276 | // rust_ffi_host produces all FFI variants. |
| 277 | func RustFFIHostFactory() android.Module { |
| 278 | module, library := NewRustLibrary(android.HostSupported) |
| 279 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 280 | return module.Init() |
| 281 | } |
| 282 | |
| 283 | // rust_library_dylib_host produces a dylib. |
| 284 | func RustLibraryDylibHostFactory() android.Module { |
| 285 | module, library := NewRustLibrary(android.HostSupported) |
| 286 | library.BuildOnlyDylib() |
| 287 | return module.Init() |
| 288 | } |
| 289 | |
| 290 | // rust_library_rlib_host produces an rlib. |
| 291 | func RustLibraryRlibHostFactory() android.Module { |
| 292 | module, library := NewRustLibrary(android.HostSupported) |
| 293 | library.BuildOnlyRlib() |
| 294 | return module.Init() |
| 295 | } |
| 296 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 297 | // rust_ffi_static_host produces a static library. |
| 298 | func RustFFIStaticHostFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 299 | module, library := NewRustLibrary(android.HostSupported) |
| 300 | library.BuildOnlyStatic() |
| 301 | return module.Init() |
| 302 | } |
| 303 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 304 | // rust_ffi_shared_host produces an shared library. |
| 305 | func RustFFISharedHostFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 306 | module, library := NewRustLibrary(android.HostSupported) |
| 307 | library.BuildOnlyShared() |
| 308 | return module.Init() |
| 309 | } |
| 310 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 311 | func (library *libraryDecorator) BuildOnlyFFI() { |
| 312 | library.MutatedProperties.BuildDylib = false |
| 313 | library.MutatedProperties.BuildRlib = false |
| 314 | library.MutatedProperties.BuildShared = true |
| 315 | library.MutatedProperties.BuildStatic = true |
| 316 | } |
| 317 | |
| 318 | func (library *libraryDecorator) BuildOnlyRust() { |
| 319 | library.MutatedProperties.BuildDylib = true |
| 320 | library.MutatedProperties.BuildRlib = true |
| 321 | library.MutatedProperties.BuildShared = false |
| 322 | library.MutatedProperties.BuildStatic = false |
| 323 | } |
| 324 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 325 | func (library *libraryDecorator) BuildOnlyDylib() { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 326 | library.MutatedProperties.BuildDylib = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 327 | library.MutatedProperties.BuildRlib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 328 | library.MutatedProperties.BuildShared = false |
| 329 | library.MutatedProperties.BuildStatic = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | func (library *libraryDecorator) BuildOnlyRlib() { |
| 333 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 334 | library.MutatedProperties.BuildRlib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 335 | library.MutatedProperties.BuildShared = false |
| 336 | library.MutatedProperties.BuildStatic = false |
| 337 | } |
| 338 | |
| 339 | func (library *libraryDecorator) BuildOnlyStatic() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 340 | library.MutatedProperties.BuildRlib = false |
| 341 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 342 | library.MutatedProperties.BuildShared = false |
| 343 | library.MutatedProperties.BuildStatic = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | func (library *libraryDecorator) BuildOnlyShared() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 347 | library.MutatedProperties.BuildRlib = false |
| 348 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 349 | library.MutatedProperties.BuildStatic = false |
| 350 | library.MutatedProperties.BuildShared = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 354 | module := newModule(hod, android.MultilibBoth) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 355 | |
| 356 | library := &libraryDecorator{ |
| 357 | MutatedProperties: LibraryMutatedProperties{ |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 358 | BuildDylib: false, |
| 359 | BuildRlib: false, |
| 360 | BuildShared: false, |
| 361 | BuildStatic: false, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 362 | }, |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 363 | baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem), |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 364 | flagExporter: NewFlagExporter(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | module.compiler = library |
| 368 | |
| 369 | return module, library |
| 370 | } |
| 371 | |
| 372 | func (library *libraryDecorator) compilerProps() []interface{} { |
| 373 | return append(library.baseCompiler.compilerProps(), |
| 374 | &library.Properties, |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 375 | &library.MutatedProperties, |
| 376 | &library.stripper.StripProperties) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 379 | func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 380 | deps = library.baseCompiler.compilerDeps(ctx, deps) |
| 381 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 382 | if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) { |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 383 | deps = bionicDeps(deps) |
Ivan Lozano | 12ee9ca | 2020-04-07 13:19:44 -0400 | [diff] [blame] | 384 | deps.CrtBegin = "crtbegin_so" |
| 385 | deps.CrtEnd = "crtend_so" |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | return deps |
| 389 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 390 | |
| 391 | func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string { |
| 392 | return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix() |
| 393 | } |
| 394 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 395 | func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 396 | flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName()) |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 397 | flags = library.baseCompiler.compilerFlags(ctx, flags) |
| 398 | if library.shared() || library.static() { |
| 399 | library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...) |
| 400 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 401 | if library.shared() { |
| 402 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx)) |
| 403 | } |
| 404 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 405 | return flags |
| 406 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 407 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 408 | func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 409 | var outputFile android.ModuleOutPath |
| 410 | var fileName string |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 411 | var srcPath android.Path |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 412 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 413 | if library.sourceProvider != nil { |
| 414 | srcPath = library.sourceProvider.Srcs()[0] |
| 415 | } else { |
| 416 | srcPath, _ = srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs) |
| 417 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 418 | |
| 419 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame] | 420 | flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 421 | |
Matthew Maurer | 46c46cc | 2020-01-13 16:34:34 -0800 | [diff] [blame] | 422 | if library.dylib() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 423 | // We need prefer-dynamic for now to avoid linking in the static stdlib. See: |
| 424 | // https://github.com/rust-lang/rust/issues/19680 |
| 425 | // https://github.com/rust-lang/rust/issues/34909 |
| 426 | flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic") |
| 427 | } |
| 428 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 429 | if library.rlib() { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 430 | fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 431 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 432 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 433 | outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 434 | library.coverageFile = outputs.coverageFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 435 | } else if library.dylib() { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 436 | fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 437 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 438 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 439 | outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 440 | library.coverageFile = outputs.coverageFile |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 441 | } else if library.static() { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 442 | fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 443 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 444 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 445 | outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 446 | library.coverageFile = outputs.coverageFile |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 447 | } else if library.shared() { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 448 | fileName = library.sharedLibFilename(ctx) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 449 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 450 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 451 | outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 452 | library.coverageFile = outputs.coverageFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 455 | if !library.rlib() && library.stripper.NeedsStrip(ctx) { |
| 456 | strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName) |
| 457 | library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile) |
| 458 | library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile) |
| 459 | } |
| 460 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 461 | var coverageFiles android.Paths |
| 462 | if library.coverageFile != nil { |
| 463 | coverageFiles = append(coverageFiles, library.coverageFile) |
| 464 | } |
| 465 | if len(deps.coverageFiles) > 0 { |
| 466 | coverageFiles = append(coverageFiles, deps.coverageFiles...) |
| 467 | } |
| 468 | library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx)) |
| 469 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 470 | if library.rlib() || library.dylib() { |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 471 | library.exportLinkDirs(deps.linkDirs...) |
| 472 | library.exportDepFlags(deps.depFlags...) |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame] | 473 | library.exportLinkObjects(deps.linkObjects...) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 474 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 475 | |
| 476 | return outputFile |
| 477 | } |
| 478 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 479 | func (library *libraryDecorator) getStem(ctx ModuleContext) string { |
| 480 | stem := library.baseCompiler.getStemWithoutSuffix(ctx) |
| 481 | validateLibraryStem(ctx, stem, library.crateName()) |
| 482 | |
| 483 | return stem + String(library.baseCompiler.Properties.Suffix) |
| 484 | } |
| 485 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 486 | func (library *libraryDecorator) install(ctx ModuleContext) { |
| 487 | // Only shared and dylib variants make sense to install. |
| 488 | if library.shared() || library.dylib() { |
| 489 | library.baseCompiler.install(ctx) |
| 490 | } |
| 491 | } |
| 492 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 493 | func (library *libraryDecorator) Disabled() bool { |
| 494 | return library.MutatedProperties.VariantIsDisabled |
| 495 | } |
| 496 | |
| 497 | func (library *libraryDecorator) SetDisabled() { |
| 498 | library.MutatedProperties.VariantIsDisabled = true |
| 499 | } |
| 500 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 501 | var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+") |
| 502 | |
| 503 | func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) { |
| 504 | if crate_name == "" { |
| 505 | ctx.PropertyErrorf("crate_name", "crate_name must be defined.") |
| 506 | } |
| 507 | |
| 508 | // crate_names are used for the library output file, and rustc expects these |
| 509 | // to be alphanumeric with underscores allowed. |
| 510 | if validCrateName.MatchString(crate_name) { |
| 511 | ctx.PropertyErrorf("crate_name", |
| 512 | "library crate_names must be alphanumeric with underscores allowed") |
| 513 | } |
| 514 | |
| 515 | // Libraries are expected to begin with "lib" followed by the crate_name |
| 516 | if !strings.HasPrefix(filename, "lib"+crate_name) { |
| 517 | ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>") |
| 518 | } |
| 519 | } |
| 520 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 521 | func LibraryMutator(mctx android.BottomUpMutatorContext) { |
| 522 | if m, ok := mctx.Module().(*Module); ok && m.compiler != nil { |
| 523 | switch library := m.compiler.(type) { |
| 524 | case libraryInterface: |
Ivan Lozano | 89435d1 | 2020-07-31 11:01:18 -0400 | [diff] [blame] | 525 | if library.buildRlib() && library.buildDylib() { |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 526 | variants := []string{"rlib", "dylib"} |
| 527 | if m.sourceProvider != nil { |
| 528 | variants = append(variants, "") |
| 529 | } |
| 530 | modules := mctx.CreateLocalVariations(variants...) |
| 531 | |
Ivan Lozano | 89435d1 | 2020-07-31 11:01:18 -0400 | [diff] [blame] | 532 | rlib := modules[0].(*Module) |
| 533 | dylib := modules[1].(*Module) |
Ivan Lozano | 89435d1 | 2020-07-31 11:01:18 -0400 | [diff] [blame] | 534 | rlib.compiler.(libraryInterface).setRlib() |
| 535 | dylib.compiler.(libraryInterface).setDylib() |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 536 | if m.sourceProvider != nil { |
| 537 | // This library is SourceProvider generated, so the non-library-producing |
| 538 | // variant needs to disable it's compiler and skip installation. |
| 539 | sourceProvider := modules[2].(*Module) |
| 540 | sourceProvider.compiler.SetDisabled() |
| 541 | } |
Ivan Lozano | 89435d1 | 2020-07-31 11:01:18 -0400 | [diff] [blame] | 542 | } else if library.buildRlib() { |
| 543 | modules := mctx.CreateLocalVariations("rlib") |
| 544 | modules[0].(*Module).compiler.(libraryInterface).setRlib() |
| 545 | } else if library.buildDylib() { |
| 546 | modules := mctx.CreateLocalVariations("dylib") |
| 547 | modules[0].(*Module).compiler.(libraryInterface).setDylib() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 548 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 549 | |
| 550 | if m.sourceProvider != nil { |
| 551 | // Alias the non-library variant to the empty-string variant. |
| 552 | mctx.AliasVariation("") |
| 553 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame^] | 557 | |
| 558 | func LibstdMutator(mctx android.BottomUpMutatorContext) { |
| 559 | if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() { |
| 560 | switch library := m.compiler.(type) { |
| 561 | case libraryInterface: |
| 562 | // Only create a variant if a library is actually being built. |
| 563 | if library.rlib() && !library.sysroot() { |
| 564 | variants := []string{"rlib-std", "dylib-std"} |
| 565 | modules := mctx.CreateLocalVariations(variants...) |
| 566 | |
| 567 | rlib := modules[0].(*Module) |
| 568 | dylib := modules[1].(*Module) |
| 569 | rlib.compiler.(libraryInterface).setRlibStd() |
| 570 | dylib.compiler.(libraryInterface).setDylibStd() |
| 571 | rlib.Properties.SubName += RlibStdlibSuffix |
| 572 | dylib.Properties.SubName += DylibStdlibSuffix |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |