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() |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 42 | ctx.BottomUp("rust_begin", BeginMutator).Parallel() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 43 | }) |
| 44 | pctx.Import("android/soong/rust/config") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 45 | pctx.ImportAs("ccConfig", "android/soong/cc/config") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | type Flags struct { |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 49 | GlobalRustFlags []string // Flags that apply globally to rust |
| 50 | GlobalLinkFlags []string // Flags that apply globally to linker |
| 51 | RustFlags []string // Flags that apply to rust |
| 52 | LinkFlags []string // Flags that apply to linker |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 53 | ClippyFlags []string // Flags that apply to clippy-driver, during the linting |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 54 | Toolchain config.Toolchain |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 55 | Coverage bool |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 56 | Clippy bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | type BaseProperties struct { |
| 60 | AndroidMkRlibs []string |
| 61 | AndroidMkDylibs []string |
| 62 | AndroidMkProcMacroLibs []string |
| 63 | AndroidMkSharedLibs []string |
| 64 | AndroidMkStaticLibs []string |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 65 | |
| 66 | SubName string `blueprint:"mutated"` |
| 67 | PreventInstall bool |
| 68 | HideFromMake bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | type Module struct { |
| 72 | android.ModuleBase |
| 73 | android.DefaultableModuleBase |
| 74 | |
| 75 | Properties BaseProperties |
| 76 | |
| 77 | hod android.HostOrDeviceSupported |
| 78 | multilib android.Multilib |
| 79 | |
| 80 | compiler compiler |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 81 | coverage *coverage |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 82 | clippy *clippy |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 83 | cachedToolchain config.Toolchain |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 84 | sourceProvider SourceProvider |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 85 | subAndroidMkOnce map[subAndroidMkProvider]bool |
| 86 | outputFile android.OptionalPath |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 87 | |
| 88 | subName string |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 91 | func (mod *Module) OutputFiles(tag string) (android.Paths, error) { |
| 92 | switch tag { |
| 93 | case "": |
| 94 | if mod.sourceProvider != nil { |
| 95 | return mod.sourceProvider.Srcs(), nil |
| 96 | } else { |
| 97 | if mod.outputFile.Valid() { |
| 98 | return android.Paths{mod.outputFile.Path()}, nil |
| 99 | } |
| 100 | return android.Paths{}, nil |
| 101 | } |
| 102 | default: |
| 103 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 104 | } |
| 105 | } |
| 106 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 107 | var _ android.ImageInterface = (*Module)(nil) |
| 108 | |
| 109 | func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {} |
| 110 | |
| 111 | func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 112 | return true |
| 113 | } |
| 114 | |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 115 | func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool { |
| 116 | return mod.InRamdisk() |
| 117 | } |
| 118 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 119 | func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool { |
| 120 | return mod.InRecovery() |
| 121 | } |
| 122 | |
| 123 | func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string { |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) { |
| 128 | } |
| 129 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 130 | func (mod *Module) BuildStubs() bool { |
| 131 | return false |
| 132 | } |
| 133 | |
| 134 | func (mod *Module) HasStubsVariants() bool { |
| 135 | return false |
| 136 | } |
| 137 | |
| 138 | func (mod *Module) SelectedStl() string { |
| 139 | return "" |
| 140 | } |
| 141 | |
Ivan Lozano | 2b26297 | 2019-11-21 12:30:50 -0800 | [diff] [blame] | 142 | func (mod *Module) NonCcVariants() bool { |
| 143 | if mod.compiler != nil { |
| 144 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 145 | if library.buildRlib() || library.buildDylib() { |
| 146 | return true |
| 147 | } else { |
| 148 | return false |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName())) |
| 153 | } |
| 154 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 155 | func (mod *Module) ApiLevel() string { |
| 156 | panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName())) |
| 157 | } |
| 158 | |
| 159 | func (mod *Module) Static() bool { |
| 160 | if mod.compiler != nil { |
| 161 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 162 | return library.static() |
| 163 | } |
| 164 | } |
| 165 | panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName())) |
| 166 | } |
| 167 | |
| 168 | func (mod *Module) Shared() bool { |
| 169 | if mod.compiler != nil { |
| 170 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 171 | return library.static() |
| 172 | } |
| 173 | } |
| 174 | panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName())) |
| 175 | } |
| 176 | |
| 177 | func (mod *Module) Toc() android.OptionalPath { |
| 178 | if mod.compiler != nil { |
| 179 | if _, ok := mod.compiler.(libraryInterface); ok { |
| 180 | return android.OptionalPath{} |
| 181 | } |
| 182 | } |
| 183 | panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName())) |
| 184 | } |
| 185 | |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 186 | func (mod *Module) OnlyInRamdisk() bool { |
| 187 | return false |
| 188 | } |
| 189 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 190 | func (mod *Module) OnlyInRecovery() bool { |
| 191 | return false |
| 192 | } |
| 193 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 194 | func (mod *Module) UseSdk() bool { |
| 195 | return false |
| 196 | } |
| 197 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 198 | func (mod *Module) UseVndk() bool { |
| 199 | return false |
| 200 | } |
| 201 | |
| 202 | func (mod *Module) MustUseVendorVariant() bool { |
| 203 | return false |
| 204 | } |
| 205 | |
| 206 | func (mod *Module) IsVndk() bool { |
| 207 | return false |
| 208 | } |
| 209 | |
| 210 | func (mod *Module) HasVendorVariant() bool { |
| 211 | return false |
| 212 | } |
| 213 | |
| 214 | func (mod *Module) SdkVersion() string { |
| 215 | return "" |
| 216 | } |
| 217 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 218 | func (mod *Module) AlwaysSdk() bool { |
| 219 | return false |
| 220 | } |
| 221 | |
Jiyong Park | 2286afd | 2020-06-16 21:58:53 +0900 | [diff] [blame] | 222 | func (mod *Module) IsSdkVariant() bool { |
| 223 | return false |
| 224 | } |
| 225 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 226 | func (mod *Module) ToolchainLibrary() bool { |
| 227 | return false |
| 228 | } |
| 229 | |
| 230 | func (mod *Module) NdkPrebuiltStl() bool { |
| 231 | return false |
| 232 | } |
| 233 | |
| 234 | func (mod *Module) StubDecorator() bool { |
| 235 | return false |
| 236 | } |
| 237 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 238 | type Deps struct { |
| 239 | Dylibs []string |
| 240 | Rlibs []string |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 241 | Rustlibs []string |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 242 | ProcMacros []string |
| 243 | SharedLibs []string |
| 244 | StaticLibs []string |
| 245 | |
| 246 | CrtBegin, CrtEnd string |
| 247 | } |
| 248 | |
| 249 | type PathDeps struct { |
| 250 | DyLibs RustLibraries |
| 251 | RLibs RustLibraries |
| 252 | SharedLibs android.Paths |
| 253 | StaticLibs android.Paths |
| 254 | ProcMacros RustLibraries |
| 255 | linkDirs []string |
| 256 | depFlags []string |
| 257 | //ReexportedDeps android.Paths |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 258 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 259 | coverageFiles android.Paths |
| 260 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 261 | CrtBegin android.OptionalPath |
| 262 | CrtEnd android.OptionalPath |
Chih-Hung Hsieh | bbd25ae | 2020-05-15 17:36:30 -0700 | [diff] [blame] | 263 | |
| 264 | // Paths to generated source files |
| 265 | SrcDeps android.Paths |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | type RustLibraries []RustLibrary |
| 269 | |
| 270 | type RustLibrary struct { |
| 271 | Path android.Path |
| 272 | CrateName string |
| 273 | } |
| 274 | |
| 275 | type compiler interface { |
| 276 | compilerFlags(ctx ModuleContext, flags Flags) Flags |
| 277 | compilerProps() []interface{} |
| 278 | compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path |
| 279 | compilerDeps(ctx DepsContext, deps Deps) Deps |
| 280 | crateName() string |
| 281 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 282 | inData() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 283 | install(ctx ModuleContext, path android.Path) |
| 284 | relativeInstallPath() string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 285 | |
| 286 | nativeCoverage() bool |
| 287 | } |
| 288 | |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 289 | type exportedFlagsProducer interface { |
| 290 | exportedLinkDirs() []string |
| 291 | exportedDepFlags() []string |
| 292 | exportLinkDirs(...string) |
| 293 | exportDepFlags(...string) |
| 294 | } |
| 295 | |
| 296 | type flagExporter struct { |
| 297 | depFlags []string |
| 298 | linkDirs []string |
| 299 | } |
| 300 | |
| 301 | func (flagExporter *flagExporter) exportedLinkDirs() []string { |
| 302 | return flagExporter.linkDirs |
| 303 | } |
| 304 | |
| 305 | func (flagExporter *flagExporter) exportedDepFlags() []string { |
| 306 | return flagExporter.depFlags |
| 307 | } |
| 308 | |
| 309 | func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) { |
| 310 | flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...)) |
| 311 | } |
| 312 | |
| 313 | func (flagExporter *flagExporter) exportDepFlags(flags ...string) { |
| 314 | flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...)) |
| 315 | } |
| 316 | |
| 317 | var _ exportedFlagsProducer = (*flagExporter)(nil) |
| 318 | |
| 319 | func NewFlagExporter() *flagExporter { |
| 320 | return &flagExporter{ |
| 321 | depFlags: []string{}, |
| 322 | linkDirs: []string{}, |
| 323 | } |
| 324 | } |
| 325 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 326 | func (mod *Module) isCoverageVariant() bool { |
| 327 | return mod.coverage.Properties.IsCoverageVariant |
| 328 | } |
| 329 | |
| 330 | var _ cc.Coverage = (*Module)(nil) |
| 331 | |
| 332 | func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool { |
| 333 | return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant |
| 334 | } |
| 335 | |
| 336 | func (mod *Module) PreventInstall() { |
| 337 | mod.Properties.PreventInstall = true |
| 338 | } |
| 339 | |
| 340 | func (mod *Module) HideFromMake() { |
| 341 | mod.Properties.HideFromMake = true |
| 342 | } |
| 343 | |
| 344 | func (mod *Module) MarkAsCoverageVariant(coverage bool) { |
| 345 | mod.coverage.Properties.IsCoverageVariant = coverage |
| 346 | } |
| 347 | |
| 348 | func (mod *Module) EnableCoverageIfNeeded() { |
| 349 | mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | func defaultsFactory() android.Module { |
| 353 | return DefaultsFactory() |
| 354 | } |
| 355 | |
| 356 | type Defaults struct { |
| 357 | android.ModuleBase |
| 358 | android.DefaultsModuleBase |
| 359 | } |
| 360 | |
| 361 | func DefaultsFactory(props ...interface{}) android.Module { |
| 362 | module := &Defaults{} |
| 363 | |
| 364 | module.AddProperties(props...) |
| 365 | module.AddProperties( |
| 366 | &BaseProperties{}, |
| 367 | &BaseCompilerProperties{}, |
| 368 | &BinaryCompilerProperties{}, |
| 369 | &LibraryCompilerProperties{}, |
| 370 | &ProcMacroCompilerProperties{}, |
| 371 | &PrebuiltProperties{}, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 372 | &SourceProviderProperties{}, |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 373 | &TestProperties{}, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 374 | &cc.CoverageProperties{}, |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 375 | &ClippyProperties{}, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 376 | ) |
| 377 | |
| 378 | android.InitDefaultsModule(module) |
| 379 | return module |
| 380 | } |
| 381 | |
| 382 | func (mod *Module) CrateName() string { |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 383 | return mod.compiler.crateName() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 386 | func (mod *Module) CcLibrary() bool { |
| 387 | if mod.compiler != nil { |
| 388 | if _, ok := mod.compiler.(*libraryDecorator); ok { |
| 389 | return true |
| 390 | } |
| 391 | } |
| 392 | return false |
| 393 | } |
| 394 | |
| 395 | func (mod *Module) CcLibraryInterface() bool { |
| 396 | if mod.compiler != nil { |
| 397 | if _, ok := mod.compiler.(libraryInterface); ok { |
| 398 | return true |
| 399 | } |
| 400 | } |
| 401 | return false |
| 402 | } |
| 403 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 404 | func (mod *Module) IncludeDirs() android.Paths { |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 405 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 406 | if library, ok := mod.compiler.(*libraryDecorator); ok { |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 407 | return library.includeDirs |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName())) |
| 411 | } |
| 412 | |
| 413 | func (mod *Module) SetStatic() { |
| 414 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 415 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 416 | library.setStatic() |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 417 | return |
| 418 | } |
| 419 | } |
| 420 | panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName())) |
| 421 | } |
| 422 | |
| 423 | func (mod *Module) SetShared() { |
| 424 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 425 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 426 | library.setShared() |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 427 | return |
| 428 | } |
| 429 | } |
| 430 | panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName())) |
| 431 | } |
| 432 | |
| 433 | func (mod *Module) SetBuildStubs() { |
| 434 | panic("SetBuildStubs not yet implemented for rust modules") |
| 435 | } |
| 436 | |
| 437 | func (mod *Module) SetStubsVersions(string) { |
| 438 | panic("SetStubsVersions not yet implemented for rust modules") |
| 439 | } |
| 440 | |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 441 | func (mod *Module) StubsVersion() string { |
| 442 | panic("SetStubsVersions not yet implemented for rust modules") |
| 443 | } |
| 444 | |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 445 | func (mod *Module) BuildStaticVariant() bool { |
| 446 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 447 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 448 | return library.buildStatic() |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName())) |
| 452 | } |
| 453 | |
| 454 | func (mod *Module) BuildSharedVariant() bool { |
| 455 | if mod.compiler != nil { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 456 | if library, ok := mod.compiler.(libraryInterface); ok { |
| 457 | return library.buildShared() |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName())) |
| 461 | } |
| 462 | |
| 463 | // Rust module deps don't have a link order (?) |
| 464 | func (mod *Module) SetDepsInLinkOrder([]android.Path) {} |
| 465 | |
| 466 | func (mod *Module) GetDepsInLinkOrder() []android.Path { |
| 467 | return []android.Path{} |
| 468 | } |
| 469 | |
| 470 | func (mod *Module) GetStaticVariant() cc.LinkableInterface { |
| 471 | return nil |
| 472 | } |
| 473 | |
| 474 | func (mod *Module) Module() android.Module { |
| 475 | return mod |
| 476 | } |
| 477 | |
| 478 | func (mod *Module) StubsVersions() []string { |
| 479 | // For now, Rust has no stubs versions. |
| 480 | if mod.compiler != nil { |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 481 | if _, ok := mod.compiler.(libraryInterface); ok { |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 482 | return []string{} |
| 483 | } |
| 484 | } |
| 485 | panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName())) |
| 486 | } |
| 487 | |
| 488 | func (mod *Module) OutputFile() android.OptionalPath { |
| 489 | return mod.outputFile |
| 490 | } |
| 491 | |
| 492 | func (mod *Module) InRecovery() bool { |
| 493 | // For now, Rust has no notion of the recovery image |
| 494 | return false |
| 495 | } |
| 496 | func (mod *Module) HasStaticVariant() bool { |
| 497 | if mod.GetStaticVariant() != nil { |
| 498 | return true |
| 499 | } |
| 500 | return false |
| 501 | } |
| 502 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 503 | func (mod *Module) CoverageFiles() android.Paths { |
| 504 | if mod.compiler != nil { |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 505 | if !mod.compiler.nativeCoverage() { |
| 506 | return android.Paths{} |
| 507 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 508 | if library, ok := mod.compiler.(*libraryDecorator); ok { |
| 509 | if library.coverageFile != nil { |
| 510 | return android.Paths{library.coverageFile} |
| 511 | } |
| 512 | return android.Paths{} |
| 513 | } |
| 514 | } |
| 515 | panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName())) |
| 516 | } |
| 517 | |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 518 | var _ cc.LinkableInterface = (*Module)(nil) |
| 519 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 520 | func (mod *Module) Init() android.Module { |
| 521 | mod.AddProperties(&mod.Properties) |
| 522 | |
| 523 | if mod.compiler != nil { |
| 524 | mod.AddProperties(mod.compiler.compilerProps()...) |
| 525 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 526 | if mod.coverage != nil { |
| 527 | mod.AddProperties(mod.coverage.props()...) |
| 528 | } |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 529 | if mod.clippy != nil { |
| 530 | mod.AddProperties(mod.clippy.props()...) |
| 531 | } |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 532 | if mod.sourceProvider != nil { |
| 533 | mod.AddProperties(mod.sourceProvider.sourceProviderProps()...) |
| 534 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 535 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 536 | android.InitAndroidArchModule(mod, mod.hod, mod.multilib) |
| 537 | |
| 538 | android.InitDefaultableModule(mod) |
| 539 | |
Ivan Lozano | de25291 | 2019-09-06 15:29:52 -0700 | [diff] [blame] | 540 | // Explicitly disable unsupported targets. |
| 541 | android.AddLoadHook(mod, func(ctx android.LoadHookContext) { |
| 542 | disableTargets := struct { |
| 543 | Target struct { |
Ivan Lozano | de25291 | 2019-09-06 15:29:52 -0700 | [diff] [blame] | 544 | Linux_bionic struct { |
| 545 | Enabled *bool |
| 546 | } |
| 547 | } |
| 548 | }{} |
Ivan Lozano | de25291 | 2019-09-06 15:29:52 -0700 | [diff] [blame] | 549 | disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false) |
| 550 | |
| 551 | ctx.AppendProperties(&disableTargets) |
| 552 | }) |
| 553 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 554 | return mod |
| 555 | } |
| 556 | |
| 557 | func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module { |
| 558 | return &Module{ |
| 559 | hod: hod, |
| 560 | multilib: multilib, |
| 561 | } |
| 562 | } |
| 563 | func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module { |
| 564 | module := newBaseModule(hod, multilib) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 565 | module.coverage = &coverage{} |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 566 | module.clippy = &clippy{} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 567 | return module |
| 568 | } |
| 569 | |
| 570 | type ModuleContext interface { |
| 571 | android.ModuleContext |
| 572 | ModuleContextIntf |
| 573 | } |
| 574 | |
| 575 | type BaseModuleContext interface { |
| 576 | android.BaseModuleContext |
| 577 | ModuleContextIntf |
| 578 | } |
| 579 | |
| 580 | type DepsContext interface { |
| 581 | android.BottomUpMutatorContext |
| 582 | ModuleContextIntf |
| 583 | } |
| 584 | |
| 585 | type ModuleContextIntf interface { |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 586 | RustModule() *Module |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 587 | toolchain() config.Toolchain |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | type depsContext struct { |
| 591 | android.BottomUpMutatorContext |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | type moduleContext struct { |
| 595 | android.ModuleContext |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 598 | type baseModuleContext struct { |
| 599 | android.BaseModuleContext |
| 600 | } |
| 601 | |
| 602 | func (ctx *moduleContext) RustModule() *Module { |
| 603 | return ctx.Module().(*Module) |
| 604 | } |
| 605 | |
| 606 | func (ctx *moduleContext) toolchain() config.Toolchain { |
| 607 | return ctx.RustModule().toolchain(ctx) |
| 608 | } |
| 609 | |
| 610 | func (ctx *depsContext) RustModule() *Module { |
| 611 | return ctx.Module().(*Module) |
| 612 | } |
| 613 | |
| 614 | func (ctx *depsContext) toolchain() config.Toolchain { |
| 615 | return ctx.RustModule().toolchain(ctx) |
| 616 | } |
| 617 | |
| 618 | func (ctx *baseModuleContext) RustModule() *Module { |
| 619 | return ctx.Module().(*Module) |
| 620 | } |
| 621 | |
| 622 | func (ctx *baseModuleContext) toolchain() config.Toolchain { |
| 623 | return ctx.RustModule().toolchain(ctx) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | func (mod *Module) nativeCoverage() bool { |
| 627 | return mod.compiler != nil && mod.compiler.nativeCoverage() |
| 628 | } |
| 629 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 630 | func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain { |
| 631 | if mod.cachedToolchain == nil { |
| 632 | mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch()) |
| 633 | } |
| 634 | return mod.cachedToolchain |
| 635 | } |
| 636 | |
| 637 | func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 638 | } |
| 639 | |
| 640 | func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) { |
| 641 | ctx := &moduleContext{ |
| 642 | ModuleContext: actx, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 643 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 644 | |
| 645 | toolchain := mod.toolchain(ctx) |
| 646 | |
| 647 | if !toolchain.Supported() { |
| 648 | // This toolchain's unsupported, there's nothing to do for this mod. |
| 649 | return |
| 650 | } |
| 651 | |
| 652 | deps := mod.depsToPaths(ctx) |
| 653 | flags := Flags{ |
| 654 | Toolchain: toolchain, |
| 655 | } |
| 656 | |
| 657 | if mod.compiler != nil { |
| 658 | flags = mod.compiler.compilerFlags(ctx, flags) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 659 | } |
| 660 | if mod.coverage != nil { |
| 661 | flags, deps = mod.coverage.flags(ctx, flags, deps) |
| 662 | } |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 663 | if mod.clippy != nil { |
| 664 | flags, deps = mod.clippy.flags(ctx, flags, deps) |
| 665 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 666 | |
| 667 | if mod.compiler != nil { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 668 | outputFile := mod.compiler.compile(ctx, flags, deps) |
| 669 | mod.outputFile = android.OptionalPathForPath(outputFile) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 670 | if !mod.Properties.PreventInstall { |
| 671 | mod.compiler.install(ctx, mod.outputFile.Path()) |
| 672 | } |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 673 | } else if mod.sourceProvider != nil { |
| 674 | outputFile := mod.sourceProvider.generateSource(ctx) |
| 675 | mod.outputFile = android.OptionalPathForPath(outputFile) |
| 676 | mod.subName = ctx.ModuleSubDir() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
| 680 | func (mod *Module) deps(ctx DepsContext) Deps { |
| 681 | deps := Deps{} |
| 682 | |
| 683 | if mod.compiler != nil { |
| 684 | deps = mod.compiler.compilerDeps(ctx, deps) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 685 | } else if mod.sourceProvider != nil { |
| 686 | deps = mod.sourceProvider.sourceProviderDeps(ctx, deps) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 689 | if mod.coverage != nil { |
| 690 | deps = mod.coverage.deps(ctx, deps) |
| 691 | } |
| 692 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 693 | deps.Rlibs = android.LastUniqueStrings(deps.Rlibs) |
| 694 | deps.Dylibs = android.LastUniqueStrings(deps.Dylibs) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 695 | deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 696 | deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros) |
| 697 | deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs) |
| 698 | deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs) |
| 699 | |
| 700 | return deps |
| 701 | |
| 702 | } |
| 703 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 704 | type dependencyTag struct { |
| 705 | blueprint.BaseDependencyTag |
| 706 | name string |
| 707 | library bool |
| 708 | proc_macro bool |
| 709 | } |
| 710 | |
| 711 | var ( |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 712 | rlibDepTag = dependencyTag{name: "rlibTag", library: true} |
| 713 | dylibDepTag = dependencyTag{name: "dylib", library: true} |
| 714 | procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true} |
| 715 | testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 716 | ) |
| 717 | |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 718 | type autoDep struct { |
| 719 | variation string |
| 720 | depTag dependencyTag |
| 721 | } |
| 722 | |
| 723 | var ( |
| 724 | rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag} |
| 725 | dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag} |
| 726 | ) |
| 727 | |
| 728 | type autoDeppable interface { |
| 729 | autoDep() autoDep |
| 730 | } |
| 731 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 732 | func (mod *Module) begin(ctx BaseModuleContext) { |
| 733 | if mod.coverage != nil { |
| 734 | mod.coverage.begin(ctx) |
| 735 | } |
| 736 | } |
| 737 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 738 | func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { |
| 739 | var depPaths PathDeps |
| 740 | |
| 741 | directRlibDeps := []*Module{} |
| 742 | directDylibDeps := []*Module{} |
| 743 | directProcMacroDeps := []*Module{} |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 744 | directSharedLibDeps := [](cc.LinkableInterface){} |
| 745 | directStaticLibDeps := [](cc.LinkableInterface){} |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame^] | 746 | directSrcProvidersDeps := []*Module{} |
| 747 | directSrcDeps := [](android.SourceFileProducer){} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 748 | |
| 749 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 750 | depName := ctx.OtherModuleName(dep) |
| 751 | depTag := ctx.OtherModuleDependencyTag(dep) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 752 | if rustDep, ok := dep.(*Module); ok { |
| 753 | //Handle Rust Modules |
Ivan Lozano | 70e0a07 | 2019-09-13 14:23:15 -0700 | [diff] [blame] | 754 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 755 | linkFile := rustDep.outputFile |
| 756 | if !linkFile.Valid() { |
| 757 | ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName()) |
| 758 | } |
| 759 | |
| 760 | switch depTag { |
| 761 | case dylibDepTag: |
| 762 | dylib, ok := rustDep.compiler.(libraryInterface) |
| 763 | if !ok || !dylib.dylib() { |
| 764 | ctx.ModuleErrorf("mod %q not an dylib library", depName) |
| 765 | return |
| 766 | } |
| 767 | directDylibDeps = append(directDylibDeps, rustDep) |
| 768 | mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName) |
| 769 | case rlibDepTag: |
| 770 | rlib, ok := rustDep.compiler.(libraryInterface) |
| 771 | if !ok || !rlib.rlib() { |
| 772 | ctx.ModuleErrorf("mod %q not an rlib library", depName) |
| 773 | return |
| 774 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 775 | depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 776 | directRlibDeps = append(directRlibDeps, rustDep) |
| 777 | mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName) |
| 778 | case procMacroDepTag: |
| 779 | directProcMacroDeps = append(directProcMacroDeps, rustDep) |
| 780 | mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName) |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame^] | 781 | case android.SourceDepTag: |
| 782 | // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct |
| 783 | // OS/Arch variant is used. |
| 784 | var helper string |
| 785 | if ctx.Host() { |
| 786 | helper = "missing 'host_supported'?" |
| 787 | } else { |
| 788 | helper = "device module defined?" |
| 789 | } |
| 790 | |
| 791 | if dep.Target().Os != ctx.Os() { |
| 792 | ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper) |
| 793 | return |
| 794 | } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType { |
| 795 | ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper) |
| 796 | return |
| 797 | } |
| 798 | directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | //Append the dependencies exportedDirs |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 802 | if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok { |
| 803 | depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 804 | depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 807 | if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag { |
| 808 | linkDir := linkPathFromFilePath(linkFile.Path()) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 809 | if lib, ok := mod.compiler.(exportedFlagsProducer); ok { |
| 810 | lib.exportLinkDirs(linkDir) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 814 | } |
Ivan Lozano | 70e0a07 | 2019-09-13 14:23:15 -0700 | [diff] [blame] | 815 | |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame^] | 816 | if srcDep, ok := dep.(android.SourceFileProducer); ok { |
| 817 | switch depTag { |
| 818 | case android.SourceDepTag: |
| 819 | // These are usually genrules which don't have per-target variants. |
| 820 | directSrcDeps = append(directSrcDeps, srcDep) |
| 821 | } |
| 822 | } |
| 823 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 824 | if ccDep, ok := dep.(cc.LinkableInterface); ok { |
| 825 | //Handle C dependencies |
| 826 | if _, ok := ccDep.(*Module); !ok { |
| 827 | if ccDep.Module().Target().Os != ctx.Os() { |
| 828 | ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName) |
| 829 | return |
| 830 | } |
| 831 | if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType { |
| 832 | ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName) |
| 833 | return |
| 834 | } |
Ivan Lozano | 70e0a07 | 2019-09-13 14:23:15 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 837 | linkFile := ccDep.OutputFile() |
| 838 | linkPath := linkPathFromFilePath(linkFile.Path()) |
| 839 | libName := libNameFromFilePath(linkFile.Path()) |
Ivan Lozano | 6aa6602 | 2020-02-06 13:22:43 -0500 | [diff] [blame] | 840 | depFlag := "-l" + libName |
| 841 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 842 | if !linkFile.Valid() { |
| 843 | ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName()) |
| 844 | } |
| 845 | |
| 846 | exportDep := false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 847 | switch depTag { |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 848 | case cc.StaticDepTag: |
Ivan Lozano | 6aa6602 | 2020-02-06 13:22:43 -0500 | [diff] [blame] | 849 | depFlag = "-lstatic=" + libName |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 850 | depPaths.linkDirs = append(depPaths.linkDirs, linkPath) |
Ivan Lozano | 6aa6602 | 2020-02-06 13:22:43 -0500 | [diff] [blame] | 851 | depPaths.depFlags = append(depPaths.depFlags, depFlag) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 852 | depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 853 | directStaticLibDeps = append(directStaticLibDeps, ccDep) |
| 854 | mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName) |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 855 | case cc.SharedDepTag: |
Ivan Lozano | 6aa6602 | 2020-02-06 13:22:43 -0500 | [diff] [blame] | 856 | depFlag = "-ldylib=" + libName |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 857 | depPaths.linkDirs = append(depPaths.linkDirs, linkPath) |
Ivan Lozano | 6aa6602 | 2020-02-06 13:22:43 -0500 | [diff] [blame] | 858 | depPaths.depFlags = append(depPaths.depFlags, depFlag) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 859 | directSharedLibDeps = append(directSharedLibDeps, ccDep) |
| 860 | mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName) |
| 861 | exportDep = true |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 862 | case cc.CrtBeginDepTag: |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 863 | depPaths.CrtBegin = linkFile |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 864 | case cc.CrtEndDepTag: |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 865 | depPaths.CrtEnd = linkFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // Make sure these dependencies are propagated |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 869 | if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep { |
| 870 | lib.exportLinkDirs(linkPath) |
| 871 | lib.exportDepFlags(depFlag) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 872 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 873 | } |
| 874 | }) |
| 875 | |
| 876 | var rlibDepFiles RustLibraries |
| 877 | for _, dep := range directRlibDeps { |
| 878 | rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()}) |
| 879 | } |
| 880 | var dylibDepFiles RustLibraries |
| 881 | for _, dep := range directDylibDeps { |
| 882 | dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()}) |
| 883 | } |
| 884 | var procMacroDepFiles RustLibraries |
| 885 | for _, dep := range directProcMacroDeps { |
| 886 | procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()}) |
| 887 | } |
| 888 | |
| 889 | var staticLibDepFiles android.Paths |
| 890 | for _, dep := range directStaticLibDeps { |
| 891 | staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path()) |
| 892 | } |
| 893 | |
| 894 | var sharedLibDepFiles android.Paths |
| 895 | for _, dep := range directSharedLibDeps { |
| 896 | sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path()) |
| 897 | } |
| 898 | |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame^] | 899 | var srcProviderDepFiles android.Paths |
| 900 | for _, dep := range directSrcProvidersDeps { |
| 901 | srcs, _ := dep.OutputFiles("") |
| 902 | srcProviderDepFiles = append(srcProviderDepFiles, srcs...) |
| 903 | } |
| 904 | for _, dep := range directSrcDeps { |
| 905 | srcs := dep.Srcs() |
| 906 | srcProviderDepFiles = append(srcProviderDepFiles, srcs...) |
| 907 | } |
| 908 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 909 | depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...) |
| 910 | depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...) |
| 911 | depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...) |
| 912 | depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...) |
| 913 | depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...) |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame^] | 914 | depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 915 | |
| 916 | // Dedup exported flags from dependencies |
| 917 | depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs) |
| 918 | depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags) |
| 919 | |
| 920 | return depPaths |
| 921 | } |
| 922 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 923 | func (mod *Module) InstallInData() bool { |
| 924 | if mod.compiler == nil { |
| 925 | return false |
| 926 | } |
| 927 | return mod.compiler.inData() |
| 928 | } |
| 929 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 930 | func linkPathFromFilePath(filepath android.Path) string { |
| 931 | return strings.Split(filepath.String(), filepath.Base())[0] |
| 932 | } |
Ivan Lozano | d648c43 | 2020-02-06 12:05:10 -0500 | [diff] [blame] | 933 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 934 | func libNameFromFilePath(filepath android.Path) string { |
Ivan Lozano | d648c43 | 2020-02-06 12:05:10 -0500 | [diff] [blame] | 935 | libName := strings.TrimSuffix(filepath.Base(), filepath.Ext()) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 936 | if strings.HasPrefix(libName, "lib") { |
| 937 | libName = libName[3:] |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 938 | } |
| 939 | return libName |
| 940 | } |
Ivan Lozano | d648c43 | 2020-02-06 12:05:10 -0500 | [diff] [blame] | 941 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 942 | func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) { |
| 943 | ctx := &depsContext{ |
| 944 | BottomUpMutatorContext: actx, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 945 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 946 | |
| 947 | deps := mod.deps(ctx) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 948 | commonDepVariations := []blueprint.Variation{} |
Jooyung Han | 624d35c | 2020-04-10 12:57:24 +0900 | [diff] [blame] | 949 | if cc.VersionVariantAvailable(mod) { |
| 950 | commonDepVariations = append(commonDepVariations, |
| 951 | blueprint.Variation{Mutator: "version", Variation: ""}) |
| 952 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 953 | if !mod.Host() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 954 | commonDepVariations = append(commonDepVariations, |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 955 | blueprint.Variation{Mutator: "image", Variation: android.CoreVariation}) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 956 | } |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 957 | actx.AddVariationDependencies( |
| 958 | append(commonDepVariations, []blueprint.Variation{ |
| 959 | {Mutator: "rust_libraries", Variation: "rlib"}, |
| 960 | {Mutator: "link", Variation: ""}}...), |
| 961 | rlibDepTag, deps.Rlibs...) |
| 962 | actx.AddVariationDependencies( |
| 963 | append(commonDepVariations, []blueprint.Variation{ |
| 964 | {Mutator: "rust_libraries", Variation: "dylib"}, |
| 965 | {Mutator: "link", Variation: ""}}...), |
| 966 | dylibDepTag, deps.Dylibs...) |
| 967 | |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 968 | if deps.Rustlibs != nil { |
| 969 | autoDep := mod.compiler.(autoDeppable).autoDep() |
| 970 | actx.AddVariationDependencies( |
| 971 | append(commonDepVariations, []blueprint.Variation{ |
| 972 | {Mutator: "rust_libraries", Variation: autoDep.variation}, |
| 973 | {Mutator: "link", Variation: ""}}...), |
| 974 | autoDep.depTag, deps.Rustlibs...) |
| 975 | } |
| 976 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 977 | actx.AddVariationDependencies(append(commonDepVariations, |
| 978 | blueprint.Variation{Mutator: "link", Variation: "shared"}), |
| 979 | cc.SharedDepTag, deps.SharedLibs...) |
| 980 | actx.AddVariationDependencies(append(commonDepVariations, |
| 981 | blueprint.Variation{Mutator: "link", Variation: "static"}), |
| 982 | cc.StaticDepTag, deps.StaticLibs...) |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame] | 983 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 984 | if deps.CrtBegin != "" { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 985 | actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 986 | } |
| 987 | if deps.CrtEnd != "" { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 988 | actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame] | 991 | // 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] | 992 | actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 995 | func BeginMutator(ctx android.BottomUpMutatorContext) { |
| 996 | if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() { |
| 997 | mod.beginMutator(ctx) |
| 998 | } |
| 999 | } |
| 1000 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 1001 | func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) { |
| 1002 | ctx := &baseModuleContext{ |
| 1003 | BaseModuleContext: actx, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 1004 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 1005 | |
| 1006 | mod.begin(ctx) |
| 1007 | } |
| 1008 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1009 | func (mod *Module) Name() string { |
| 1010 | name := mod.ModuleBase.Name() |
| 1011 | if p, ok := mod.compiler.(interface { |
| 1012 | Name(string) string |
| 1013 | }); ok { |
| 1014 | name = p.Name(name) |
| 1015 | } |
| 1016 | return name |
| 1017 | } |
| 1018 | |
Chih-Hung Hsieh | 5c4e489 | 2020-05-15 17:36:30 -0700 | [diff] [blame] | 1019 | var _ android.HostToolProvider = (*Module)(nil) |
| 1020 | |
| 1021 | func (mod *Module) HostToolPath() android.OptionalPath { |
| 1022 | if !mod.Host() { |
| 1023 | return android.OptionalPath{} |
| 1024 | } |
| 1025 | if _, ok := mod.compiler.(*binaryDecorator); ok { |
| 1026 | return mod.outputFile |
| 1027 | } |
| 1028 | return android.OptionalPath{} |
| 1029 | } |
| 1030 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1031 | var Bool = proptools.Bool |
| 1032 | var BoolDefault = proptools.BoolDefault |
| 1033 | var String = proptools.String |
| 1034 | var StringPtr = proptools.StringPtr |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 1035 | |
| 1036 | var _ android.OutputFileProducer = (*Module)(nil) |