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