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