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