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