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 | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 18 | "fmt" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | "android/soong/cc" |
| 26 | "android/soong/rust/config" |
| 27 | ) |
| 28 | |
| 29 | var pctx = android.NewPackageContext("android/soong/rust") |
| 30 | |
| 31 | func init() { |
| 32 | // Only allow rust modules to be defined for certain projects |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 33 | |
| 34 | android.AddNeverAllowRules( |
| 35 | android.NeverAllow(). |
Ivan Lozano | e169ad7 | 2019-09-18 08:42:54 -0700 | [diff] [blame] | 36 | NotIn(config.RustAllowedPaths...). |
| 37 | ModuleType(config.RustModuleTypes...)) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 38 | |
| 39 | android.RegisterModuleType("rust_defaults", defaultsFactory) |
| 40 | android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 41 | ctx.BottomUp("rust_libraries", LibraryMutator).Parallel() |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 42 | ctx.BottomUp("rust_unit_tests", TestPerSrcMutator).Parallel() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 43 | }) |
| 44 | pctx.Import("android/soong/rust/config") |
| 45 | } |
| 46 | |
| 47 | type Flags struct { |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 48 | GlobalRustFlags []string // Flags that apply globally to rust |
| 49 | GlobalLinkFlags []string // Flags that apply globally to linker |
| 50 | RustFlags []string // Flags that apply to rust |
| 51 | LinkFlags []string // Flags that apply to linker |
| 52 | RustFlagsDeps android.Paths // Files depended on by compiler flags |
| 53 | Toolchain config.Toolchain |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | type BaseProperties struct { |
| 57 | AndroidMkRlibs []string |
| 58 | AndroidMkDylibs []string |
| 59 | AndroidMkProcMacroLibs []string |
| 60 | AndroidMkSharedLibs []string |
| 61 | AndroidMkStaticLibs []string |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 62 | SubName string `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | type Module struct { |
| 66 | android.ModuleBase |
| 67 | android.DefaultableModuleBase |
| 68 | |
| 69 | Properties BaseProperties |
| 70 | |
| 71 | hod android.HostOrDeviceSupported |
| 72 | multilib android.Multilib |
| 73 | |
| 74 | compiler compiler |
| 75 | cachedToolchain config.Toolchain |
| 76 | subAndroidMkOnce map[subAndroidMkProvider]bool |
| 77 | outputFile android.OptionalPath |
| 78 | } |
| 79 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame^] | 80 | var _ android.ImageInterface = (*Module)(nil) |
| 81 | |
| 82 | func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {} |
| 83 | |
| 84 | func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 85 | return true |
| 86 | } |
| 87 | |
| 88 | func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool { |
| 89 | return mod.InRecovery() |
| 90 | } |
| 91 | |
| 92 | func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string { |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) { |
| 97 | } |
| 98 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 99 | func (mod *Module) BuildStubs() bool { |
| 100 | return false |
| 101 | } |
| 102 | |
| 103 | func (mod *Module) HasStubsVariants() bool { |
| 104 | return false |
| 105 | } |
| 106 | |
| 107 | func (mod *Module) SelectedStl() string { |
| 108 | return "" |
| 109 | } |
| 110 | |
| 111 | func (mod *Module) ApiLevel() string { |
| 112 | panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName())) |
| 113 | } |
| 114 | |
| 115 | func (mod *Module) Static() bool { |
| 116 | if mod.compiler != nil { |
| 117 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 118 | return library.static() |
| 119 | } |
| 120 | } |
| 121 | panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName())) |
| 122 | } |
| 123 | |
| 124 | func (mod *Module) Shared() bool { |
| 125 | if mod.compiler != nil { |
| 126 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 127 | return library.static() |
| 128 | } |
| 129 | } |
| 130 | panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName())) |
| 131 | } |
| 132 | |
| 133 | func (mod *Module) Toc() android.OptionalPath { |
| 134 | if mod.compiler != nil { |
| 135 | if _, ok := mod.compiler.(libraryInterface); ok { |
| 136 | return android.OptionalPath{} |
| 137 | } |
| 138 | } |
| 139 | panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName())) |
| 140 | } |
| 141 | |
| 142 | func (mod *Module) OnlyInRecovery() bool { |
| 143 | return false |
| 144 | } |
| 145 | |
| 146 | func (mod *Module) UseVndk() bool { |
| 147 | return false |
| 148 | } |
| 149 | |
| 150 | func (mod *Module) MustUseVendorVariant() bool { |
| 151 | return false |
| 152 | } |
| 153 | |
| 154 | func (mod *Module) IsVndk() bool { |
| 155 | return false |
| 156 | } |
| 157 | |
| 158 | func (mod *Module) HasVendorVariant() bool { |
| 159 | return false |
| 160 | } |
| 161 | |
| 162 | func (mod *Module) SdkVersion() string { |
| 163 | return "" |
| 164 | } |
| 165 | |
| 166 | func (mod *Module) ToolchainLibrary() bool { |
| 167 | return false |
| 168 | } |
| 169 | |
| 170 | func (mod *Module) NdkPrebuiltStl() bool { |
| 171 | return false |
| 172 | } |
| 173 | |
| 174 | func (mod *Module) StubDecorator() bool { |
| 175 | return false |
| 176 | } |
| 177 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 178 | type Deps struct { |
| 179 | Dylibs []string |
| 180 | Rlibs []string |
| 181 | ProcMacros []string |
| 182 | SharedLibs []string |
| 183 | StaticLibs []string |
| 184 | |
| 185 | CrtBegin, CrtEnd string |
| 186 | } |
| 187 | |
| 188 | type PathDeps struct { |
| 189 | DyLibs RustLibraries |
| 190 | RLibs RustLibraries |
| 191 | SharedLibs android.Paths |
| 192 | StaticLibs android.Paths |
| 193 | ProcMacros RustLibraries |
| 194 | linkDirs []string |
| 195 | depFlags []string |
| 196 | //ReexportedDeps android.Paths |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 197 | |
| 198 | CrtBegin android.OptionalPath |
| 199 | CrtEnd android.OptionalPath |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | type RustLibraries []RustLibrary |
| 203 | |
| 204 | type RustLibrary struct { |
| 205 | Path android.Path |
| 206 | CrateName string |
| 207 | } |
| 208 | |
| 209 | type compiler interface { |
| 210 | compilerFlags(ctx ModuleContext, flags Flags) Flags |
| 211 | compilerProps() []interface{} |
| 212 | compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path |
| 213 | compilerDeps(ctx DepsContext, deps Deps) Deps |
| 214 | crateName() string |
| 215 | |
| 216 | install(ctx ModuleContext, path android.Path) |
| 217 | relativeInstallPath() string |
| 218 | } |
| 219 | |
| 220 | func defaultsFactory() android.Module { |
| 221 | return DefaultsFactory() |
| 222 | } |
| 223 | |
| 224 | type Defaults struct { |
| 225 | android.ModuleBase |
| 226 | android.DefaultsModuleBase |
| 227 | } |
| 228 | |
| 229 | func DefaultsFactory(props ...interface{}) android.Module { |
| 230 | module := &Defaults{} |
| 231 | |
| 232 | module.AddProperties(props...) |
| 233 | module.AddProperties( |
| 234 | &BaseProperties{}, |
| 235 | &BaseCompilerProperties{}, |
| 236 | &BinaryCompilerProperties{}, |
| 237 | &LibraryCompilerProperties{}, |
| 238 | &ProcMacroCompilerProperties{}, |
| 239 | &PrebuiltProperties{}, |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 240 | &TestProperties{}, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 241 | ) |
| 242 | |
| 243 | android.InitDefaultsModule(module) |
| 244 | return module |
| 245 | } |
| 246 | |
| 247 | func (mod *Module) CrateName() string { |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 248 | return mod.compiler.crateName() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 251 | func (mod *Module) CcLibrary() bool { |
| 252 | if mod.compiler != nil { |
| 253 | if _, ok := mod.compiler.(*libraryDecorator); ok { |
| 254 | return true |
| 255 | } |
| 256 | } |
| 257 | return false |
| 258 | } |
| 259 | |
| 260 | func (mod *Module) CcLibraryInterface() bool { |
| 261 | if mod.compiler != nil { |
| 262 | if _, ok := mod.compiler.(libraryInterface); ok { |
| 263 | return true |
| 264 | } |
| 265 | } |
| 266 | return false |
| 267 | } |
| 268 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 269 | func (mod *Module) IncludeDirs() android.Paths { |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 270 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 271 | if library, ok := mod.compiler.(*libraryDecorator); ok { |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 272 | return library.includeDirs |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName())) |
| 276 | } |
| 277 | |
| 278 | func (mod *Module) SetStatic() { |
| 279 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 280 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 281 | library.setStatic() |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 282 | return |
| 283 | } |
| 284 | } |
| 285 | panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName())) |
| 286 | } |
| 287 | |
| 288 | func (mod *Module) SetShared() { |
| 289 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 290 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 291 | library.setShared() |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 292 | return |
| 293 | } |
| 294 | } |
| 295 | panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName())) |
| 296 | } |
| 297 | |
| 298 | func (mod *Module) SetBuildStubs() { |
| 299 | panic("SetBuildStubs not yet implemented for rust modules") |
| 300 | } |
| 301 | |
| 302 | func (mod *Module) SetStubsVersions(string) { |
| 303 | panic("SetStubsVersions not yet implemented for rust modules") |
| 304 | } |
| 305 | |
| 306 | func (mod *Module) BuildStaticVariant() bool { |
| 307 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 308 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 309 | return library.buildStatic() |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName())) |
| 313 | } |
| 314 | |
| 315 | func (mod *Module) BuildSharedVariant() bool { |
| 316 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 317 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 318 | return library.buildShared() |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName())) |
| 322 | } |
| 323 | |
| 324 | // Rust module deps don't have a link order (?) |
| 325 | func (mod *Module) SetDepsInLinkOrder([]android.Path) {} |
| 326 | |
| 327 | func (mod *Module) GetDepsInLinkOrder() []android.Path { |
| 328 | return []android.Path{} |
| 329 | } |
| 330 | |
| 331 | func (mod *Module) GetStaticVariant() cc.LinkableInterface { |
| 332 | return nil |
| 333 | } |
| 334 | |
| 335 | func (mod *Module) Module() android.Module { |
| 336 | return mod |
| 337 | } |
| 338 | |
| 339 | func (mod *Module) StubsVersions() []string { |
| 340 | // For now, Rust has no stubs versions. |
| 341 | if mod.compiler != nil { |
| 342 | if _, ok := mod.compiler.(*libraryDecorator); ok { |
| 343 | return []string{} |
| 344 | } |
| 345 | } |
| 346 | panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName())) |
| 347 | } |
| 348 | |
| 349 | func (mod *Module) OutputFile() android.OptionalPath { |
| 350 | return mod.outputFile |
| 351 | } |
| 352 | |
| 353 | func (mod *Module) InRecovery() bool { |
| 354 | // For now, Rust has no notion of the recovery image |
| 355 | return false |
| 356 | } |
| 357 | func (mod *Module) HasStaticVariant() bool { |
| 358 | if mod.GetStaticVariant() != nil { |
| 359 | return true |
| 360 | } |
| 361 | return false |
| 362 | } |
| 363 | |
| 364 | var _ cc.LinkableInterface = (*Module)(nil) |
| 365 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 366 | func (mod *Module) Init() android.Module { |
| 367 | mod.AddProperties(&mod.Properties) |
| 368 | |
| 369 | if mod.compiler != nil { |
| 370 | mod.AddProperties(mod.compiler.compilerProps()...) |
| 371 | } |
| 372 | android.InitAndroidArchModule(mod, mod.hod, mod.multilib) |
| 373 | |
| 374 | android.InitDefaultableModule(mod) |
| 375 | |
Ivan Lozano | de25291 | 2019-09-06 15:29:52 -0700 | [diff] [blame] | 376 | // Explicitly disable unsupported targets. |
| 377 | android.AddLoadHook(mod, func(ctx android.LoadHookContext) { |
| 378 | disableTargets := struct { |
| 379 | Target struct { |
Ivan Lozano | de25291 | 2019-09-06 15:29:52 -0700 | [diff] [blame] | 380 | Linux_bionic struct { |
| 381 | Enabled *bool |
| 382 | } |
| 383 | } |
| 384 | }{} |
Ivan Lozano | de25291 | 2019-09-06 15:29:52 -0700 | [diff] [blame] | 385 | disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false) |
| 386 | |
| 387 | ctx.AppendProperties(&disableTargets) |
| 388 | }) |
| 389 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 390 | return mod |
| 391 | } |
| 392 | |
| 393 | func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module { |
| 394 | return &Module{ |
| 395 | hod: hod, |
| 396 | multilib: multilib, |
| 397 | } |
| 398 | } |
| 399 | func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module { |
| 400 | module := newBaseModule(hod, multilib) |
| 401 | return module |
| 402 | } |
| 403 | |
| 404 | type ModuleContext interface { |
| 405 | android.ModuleContext |
| 406 | ModuleContextIntf |
| 407 | } |
| 408 | |
| 409 | type BaseModuleContext interface { |
| 410 | android.BaseModuleContext |
| 411 | ModuleContextIntf |
| 412 | } |
| 413 | |
| 414 | type DepsContext interface { |
| 415 | android.BottomUpMutatorContext |
| 416 | ModuleContextIntf |
| 417 | } |
| 418 | |
| 419 | type ModuleContextIntf interface { |
| 420 | toolchain() config.Toolchain |
| 421 | baseModuleName() string |
| 422 | CrateName() string |
| 423 | } |
| 424 | |
| 425 | type depsContext struct { |
| 426 | android.BottomUpMutatorContext |
| 427 | moduleContextImpl |
| 428 | } |
| 429 | |
| 430 | type moduleContext struct { |
| 431 | android.ModuleContext |
| 432 | moduleContextImpl |
| 433 | } |
| 434 | |
| 435 | type moduleContextImpl struct { |
| 436 | mod *Module |
| 437 | ctx BaseModuleContext |
| 438 | } |
| 439 | |
| 440 | func (ctx *moduleContextImpl) toolchain() config.Toolchain { |
| 441 | return ctx.mod.toolchain(ctx.ctx) |
| 442 | } |
| 443 | |
| 444 | func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain { |
| 445 | if mod.cachedToolchain == nil { |
| 446 | mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch()) |
| 447 | } |
| 448 | return mod.cachedToolchain |
| 449 | } |
| 450 | |
| 451 | func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 452 | } |
| 453 | |
| 454 | func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) { |
| 455 | ctx := &moduleContext{ |
| 456 | ModuleContext: actx, |
| 457 | moduleContextImpl: moduleContextImpl{ |
| 458 | mod: mod, |
| 459 | }, |
| 460 | } |
| 461 | ctx.ctx = ctx |
| 462 | |
| 463 | toolchain := mod.toolchain(ctx) |
| 464 | |
| 465 | if !toolchain.Supported() { |
| 466 | // This toolchain's unsupported, there's nothing to do for this mod. |
| 467 | return |
| 468 | } |
| 469 | |
| 470 | deps := mod.depsToPaths(ctx) |
| 471 | flags := Flags{ |
| 472 | Toolchain: toolchain, |
| 473 | } |
| 474 | |
| 475 | if mod.compiler != nil { |
| 476 | flags = mod.compiler.compilerFlags(ctx, flags) |
| 477 | outputFile := mod.compiler.compile(ctx, flags, deps) |
| 478 | mod.outputFile = android.OptionalPathForPath(outputFile) |
| 479 | mod.compiler.install(ctx, mod.outputFile.Path()) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | func (mod *Module) deps(ctx DepsContext) Deps { |
| 484 | deps := Deps{} |
| 485 | |
| 486 | if mod.compiler != nil { |
| 487 | deps = mod.compiler.compilerDeps(ctx, deps) |
| 488 | } |
| 489 | |
| 490 | deps.Rlibs = android.LastUniqueStrings(deps.Rlibs) |
| 491 | deps.Dylibs = android.LastUniqueStrings(deps.Dylibs) |
| 492 | deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros) |
| 493 | deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs) |
| 494 | deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs) |
| 495 | |
| 496 | return deps |
| 497 | |
| 498 | } |
| 499 | |
| 500 | func (ctx *moduleContextImpl) baseModuleName() string { |
| 501 | return ctx.mod.ModuleBase.BaseModuleName() |
| 502 | } |
| 503 | |
| 504 | func (ctx *moduleContextImpl) CrateName() string { |
| 505 | return ctx.mod.CrateName() |
| 506 | } |
| 507 | |
| 508 | type dependencyTag struct { |
| 509 | blueprint.BaseDependencyTag |
| 510 | name string |
| 511 | library bool |
| 512 | proc_macro bool |
| 513 | } |
| 514 | |
| 515 | var ( |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 516 | rlibDepTag = dependencyTag{name: "rlibTag", library: true} |
| 517 | dylibDepTag = dependencyTag{name: "dylib", library: true} |
| 518 | procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true} |
| 519 | testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 520 | ) |
| 521 | |
| 522 | func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { |
| 523 | var depPaths PathDeps |
| 524 | |
| 525 | directRlibDeps := []*Module{} |
| 526 | directDylibDeps := []*Module{} |
| 527 | directProcMacroDeps := []*Module{} |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 528 | directSharedLibDeps := [](cc.LinkableInterface){} |
| 529 | directStaticLibDeps := [](cc.LinkableInterface){} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 530 | |
| 531 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 532 | depName := ctx.OtherModuleName(dep) |
| 533 | depTag := ctx.OtherModuleDependencyTag(dep) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 534 | if rustDep, ok := dep.(*Module); ok { |
| 535 | //Handle Rust Modules |
Ivan Lozano | 70e0a07 | 2019-09-13 14:23:15 -0700 | [diff] [blame] | 536 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 537 | linkFile := rustDep.outputFile |
| 538 | if !linkFile.Valid() { |
| 539 | ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName()) |
| 540 | } |
| 541 | |
| 542 | switch depTag { |
| 543 | case dylibDepTag: |
| 544 | dylib, ok := rustDep.compiler.(libraryInterface) |
| 545 | if !ok || !dylib.dylib() { |
| 546 | ctx.ModuleErrorf("mod %q not an dylib library", depName) |
| 547 | return |
| 548 | } |
| 549 | directDylibDeps = append(directDylibDeps, rustDep) |
| 550 | mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName) |
| 551 | case rlibDepTag: |
| 552 | rlib, ok := rustDep.compiler.(libraryInterface) |
| 553 | if !ok || !rlib.rlib() { |
| 554 | ctx.ModuleErrorf("mod %q not an rlib library", depName) |
| 555 | return |
| 556 | } |
| 557 | directRlibDeps = append(directRlibDeps, rustDep) |
| 558 | mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName) |
| 559 | case procMacroDepTag: |
| 560 | directProcMacroDeps = append(directProcMacroDeps, rustDep) |
| 561 | mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName) |
| 562 | } |
| 563 | |
| 564 | //Append the dependencies exportedDirs |
| 565 | if lib, ok := rustDep.compiler.(*libraryDecorator); ok { |
| 566 | depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...) |
| 567 | depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies |
| 571 | // This can be probably be refactored by defining a common exporter interface similar to cc's |
| 572 | if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag { |
| 573 | linkDir := linkPathFromFilePath(linkFile.Path()) |
| 574 | if lib, ok := mod.compiler.(*libraryDecorator); ok { |
| 575 | lib.linkDirs = append(lib.linkDirs, linkDir) |
| 576 | } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok { |
| 577 | procMacro.linkDirs = append(procMacro.linkDirs, linkDir) |
| 578 | } |
| 579 | } |
| 580 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 581 | } |
Ivan Lozano | 70e0a07 | 2019-09-13 14:23:15 -0700 | [diff] [blame] | 582 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 583 | if ccDep, ok := dep.(cc.LinkableInterface); ok { |
| 584 | //Handle C dependencies |
| 585 | if _, ok := ccDep.(*Module); !ok { |
| 586 | if ccDep.Module().Target().Os != ctx.Os() { |
| 587 | ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName) |
| 588 | return |
| 589 | } |
| 590 | if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType { |
| 591 | ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName) |
| 592 | return |
| 593 | } |
Ivan Lozano | 70e0a07 | 2019-09-13 14:23:15 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 596 | linkFile := ccDep.OutputFile() |
| 597 | linkPath := linkPathFromFilePath(linkFile.Path()) |
| 598 | libName := libNameFromFilePath(linkFile.Path()) |
| 599 | if !linkFile.Valid() { |
| 600 | ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName()) |
| 601 | } |
| 602 | |
| 603 | exportDep := false |
| 604 | |
| 605 | switch depTag { |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 606 | case cc.StaticDepTag: |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 607 | depPaths.linkDirs = append(depPaths.linkDirs, linkPath) |
| 608 | depPaths.depFlags = append(depPaths.depFlags, "-l"+libName) |
| 609 | directStaticLibDeps = append(directStaticLibDeps, ccDep) |
| 610 | mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName) |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 611 | case cc.SharedDepTag: |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 612 | depPaths.linkDirs = append(depPaths.linkDirs, linkPath) |
| 613 | depPaths.depFlags = append(depPaths.depFlags, "-l"+libName) |
| 614 | directSharedLibDeps = append(directSharedLibDeps, ccDep) |
| 615 | mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName) |
| 616 | exportDep = true |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 617 | case cc.CrtBeginDepTag: |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 618 | depPaths.CrtBegin = linkFile |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 619 | case cc.CrtEndDepTag: |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 620 | depPaths.CrtEnd = linkFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // Make sure these dependencies are propagated |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 624 | if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 625 | lib.linkDirs = append(lib.linkDirs, linkPath) |
| 626 | lib.depFlags = append(lib.depFlags, "-l"+libName) |
| 627 | } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep { |
| 628 | procMacro.linkDirs = append(procMacro.linkDirs, linkPath) |
| 629 | procMacro.depFlags = append(procMacro.depFlags, "-l"+libName) |
| 630 | } |
| 631 | |
| 632 | } |
| 633 | }) |
| 634 | |
| 635 | var rlibDepFiles RustLibraries |
| 636 | for _, dep := range directRlibDeps { |
| 637 | rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()}) |
| 638 | } |
| 639 | var dylibDepFiles RustLibraries |
| 640 | for _, dep := range directDylibDeps { |
| 641 | dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()}) |
| 642 | } |
| 643 | var procMacroDepFiles RustLibraries |
| 644 | for _, dep := range directProcMacroDeps { |
| 645 | procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()}) |
| 646 | } |
| 647 | |
| 648 | var staticLibDepFiles android.Paths |
| 649 | for _, dep := range directStaticLibDeps { |
| 650 | staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path()) |
| 651 | } |
| 652 | |
| 653 | var sharedLibDepFiles android.Paths |
| 654 | for _, dep := range directSharedLibDeps { |
| 655 | sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path()) |
| 656 | } |
| 657 | |
| 658 | depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...) |
| 659 | depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...) |
| 660 | depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...) |
| 661 | depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...) |
| 662 | depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...) |
| 663 | |
| 664 | // Dedup exported flags from dependencies |
| 665 | depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs) |
| 666 | depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags) |
| 667 | |
| 668 | return depPaths |
| 669 | } |
| 670 | |
| 671 | func linkPathFromFilePath(filepath android.Path) string { |
| 672 | return strings.Split(filepath.String(), filepath.Base())[0] |
| 673 | } |
| 674 | func libNameFromFilePath(filepath android.Path) string { |
| 675 | libName := strings.Split(filepath.Base(), filepath.Ext())[0] |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 676 | if strings.HasPrefix(libName, "lib") { |
| 677 | libName = libName[3:] |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 678 | } |
| 679 | return libName |
| 680 | } |
| 681 | func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) { |
| 682 | ctx := &depsContext{ |
| 683 | BottomUpMutatorContext: actx, |
| 684 | moduleContextImpl: moduleContextImpl{ |
| 685 | mod: mod, |
| 686 | }, |
| 687 | } |
| 688 | ctx.ctx = ctx |
| 689 | |
| 690 | deps := mod.deps(ctx) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 691 | commonDepVariations := []blueprint.Variation{} |
| 692 | commonDepVariations = append(commonDepVariations, |
| 693 | blueprint.Variation{Mutator: "version", Variation: ""}) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 694 | if !mod.Host() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 695 | commonDepVariations = append(commonDepVariations, |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame^] | 696 | blueprint.Variation{Mutator: "image", Variation: android.CoreVariation}) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 697 | } |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 698 | |
| 699 | actx.AddVariationDependencies( |
| 700 | append(commonDepVariations, []blueprint.Variation{ |
| 701 | {Mutator: "rust_libraries", Variation: "rlib"}, |
| 702 | {Mutator: "link", Variation: ""}}...), |
| 703 | rlibDepTag, deps.Rlibs...) |
| 704 | actx.AddVariationDependencies( |
| 705 | append(commonDepVariations, []blueprint.Variation{ |
| 706 | {Mutator: "rust_libraries", Variation: "dylib"}, |
| 707 | {Mutator: "link", Variation: ""}}...), |
| 708 | dylibDepTag, deps.Dylibs...) |
| 709 | |
| 710 | actx.AddVariationDependencies(append(commonDepVariations, |
| 711 | blueprint.Variation{Mutator: "link", Variation: "shared"}), |
| 712 | cc.SharedDepTag, deps.SharedLibs...) |
| 713 | actx.AddVariationDependencies(append(commonDepVariations, |
| 714 | blueprint.Variation{Mutator: "link", Variation: "static"}), |
| 715 | cc.StaticDepTag, deps.StaticLibs...) |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame] | 716 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 717 | if deps.CrtBegin != "" { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 718 | actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 719 | } |
| 720 | if deps.CrtEnd != "" { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 721 | actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame] | 724 | // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 725 | actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | func (mod *Module) Name() string { |
| 729 | name := mod.ModuleBase.Name() |
| 730 | if p, ok := mod.compiler.(interface { |
| 731 | Name(string) string |
| 732 | }); ok { |
| 733 | name = p.Name(name) |
| 734 | } |
| 735 | return name |
| 736 | } |
| 737 | |
| 738 | var Bool = proptools.Bool |
| 739 | var BoolDefault = proptools.BoolDefault |
| 740 | var String = proptools.String |
| 741 | var StringPtr = proptools.StringPtr |