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