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