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