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