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