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