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