blob: 6671dd34a677d2ff56d93a02abd7e2ffdee19e9c [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// 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
15package rust
16
17import (
Ivan Lozano183a3212019-10-18 14:18:45 -070018 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "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
29var pctx = android.NewPackageContext("android/soong/rust")
30
31func init() {
32 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070033
34 android.AddNeverAllowRules(
35 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070036 NotIn(config.RustAllowedPaths...).
37 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070038
39 android.RegisterModuleType("rust_defaults", defaultsFactory)
40 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
41 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040042 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070043 })
44 pctx.Import("android/soong/rust/config")
45}
46
47type Flags struct {
Ivan Lozanof1c84332019-09-20 11:00:37 -070048 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 Lozanoa0cd8f92020-04-09 09:56:02 -040054 Coverage bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070055}
56
57type BaseProperties struct {
58 AndroidMkRlibs []string
59 AndroidMkDylibs []string
60 AndroidMkProcMacroLibs []string
61 AndroidMkSharedLibs []string
62 AndroidMkStaticLibs []string
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070063 SubName string `blueprint:"mutated"`
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040064 PreventInstall bool
65 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070066}
67
68type Module struct {
69 android.ModuleBase
70 android.DefaultableModuleBase
71
72 Properties BaseProperties
73
74 hod android.HostOrDeviceSupported
75 multilib android.Multilib
76
77 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040078 coverage *coverage
Ivan Lozanoffee3342019-08-27 12:03:00 -070079 cachedToolchain config.Toolchain
80 subAndroidMkOnce map[subAndroidMkProvider]bool
81 outputFile android.OptionalPath
82}
83
Colin Cross7228ecd2019-11-18 16:00:16 -080084var _ android.ImageInterface = (*Module)(nil)
85
86func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
87
88func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
89 return true
90}
91
Yifan Hong1b3348d2020-01-21 15:53:22 -080092func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
93 return mod.InRamdisk()
94}
95
Colin Cross7228ecd2019-11-18 16:00:16 -080096func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
97 return mod.InRecovery()
98}
99
100func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
101 return nil
102}
103
104func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
105}
106
Ivan Lozano52767be2019-10-18 14:49:46 -0700107func (mod *Module) BuildStubs() bool {
108 return false
109}
110
111func (mod *Module) HasStubsVariants() bool {
112 return false
113}
114
115func (mod *Module) SelectedStl() string {
116 return ""
117}
118
Ivan Lozano2b262972019-11-21 12:30:50 -0800119func (mod *Module) NonCcVariants() bool {
120 if mod.compiler != nil {
121 if library, ok := mod.compiler.(libraryInterface); ok {
122 if library.buildRlib() || library.buildDylib() {
123 return true
124 } else {
125 return false
126 }
127 }
128 }
129 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
130}
131
Ivan Lozano52767be2019-10-18 14:49:46 -0700132func (mod *Module) ApiLevel() string {
133 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
134}
135
136func (mod *Module) Static() bool {
137 if mod.compiler != nil {
138 if library, ok := mod.compiler.(libraryInterface); ok {
139 return library.static()
140 }
141 }
142 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
143}
144
145func (mod *Module) Shared() bool {
146 if mod.compiler != nil {
147 if library, ok := mod.compiler.(libraryInterface); ok {
148 return library.static()
149 }
150 }
151 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
152}
153
154func (mod *Module) Toc() android.OptionalPath {
155 if mod.compiler != nil {
156 if _, ok := mod.compiler.(libraryInterface); ok {
157 return android.OptionalPath{}
158 }
159 }
160 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
161}
162
Yifan Hong1b3348d2020-01-21 15:53:22 -0800163func (mod *Module) OnlyInRamdisk() bool {
164 return false
165}
166
Ivan Lozano52767be2019-10-18 14:49:46 -0700167func (mod *Module) OnlyInRecovery() bool {
168 return false
169}
170
Colin Crossc511bc52020-04-07 16:50:32 +0000171func (mod *Module) UseSdk() bool {
172 return false
173}
174
Ivan Lozano52767be2019-10-18 14:49:46 -0700175func (mod *Module) UseVndk() bool {
176 return false
177}
178
179func (mod *Module) MustUseVendorVariant() bool {
180 return false
181}
182
183func (mod *Module) IsVndk() bool {
184 return false
185}
186
187func (mod *Module) HasVendorVariant() bool {
188 return false
189}
190
191func (mod *Module) SdkVersion() string {
192 return ""
193}
194
Colin Crossc511bc52020-04-07 16:50:32 +0000195func (mod *Module) AlwaysSdk() bool {
196 return false
197}
198
Ivan Lozano52767be2019-10-18 14:49:46 -0700199func (mod *Module) ToolchainLibrary() bool {
200 return false
201}
202
203func (mod *Module) NdkPrebuiltStl() bool {
204 return false
205}
206
207func (mod *Module) StubDecorator() bool {
208 return false
209}
210
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211type Deps struct {
212 Dylibs []string
213 Rlibs []string
214 ProcMacros []string
215 SharedLibs []string
216 StaticLibs []string
217
218 CrtBegin, CrtEnd string
219}
220
221type PathDeps struct {
222 DyLibs RustLibraries
223 RLibs RustLibraries
224 SharedLibs android.Paths
225 StaticLibs android.Paths
226 ProcMacros RustLibraries
227 linkDirs []string
228 depFlags []string
229 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700230
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400231 coverageFiles android.Paths
232
Ivan Lozanof1c84332019-09-20 11:00:37 -0700233 CrtBegin android.OptionalPath
234 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235}
236
237type RustLibraries []RustLibrary
238
239type RustLibrary struct {
240 Path android.Path
241 CrateName string
242}
243
244type compiler interface {
245 compilerFlags(ctx ModuleContext, flags Flags) Flags
246 compilerProps() []interface{}
247 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
248 compilerDeps(ctx DepsContext, deps Deps) Deps
249 crateName() string
250
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800251 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700252 install(ctx ModuleContext, path android.Path)
253 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400254
255 nativeCoverage() bool
256}
257
258func (mod *Module) isCoverageVariant() bool {
259 return mod.coverage.Properties.IsCoverageVariant
260}
261
262var _ cc.Coverage = (*Module)(nil)
263
264func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
265 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
266}
267
268func (mod *Module) PreventInstall() {
269 mod.Properties.PreventInstall = true
270}
271
272func (mod *Module) HideFromMake() {
273 mod.Properties.HideFromMake = true
274}
275
276func (mod *Module) MarkAsCoverageVariant(coverage bool) {
277 mod.coverage.Properties.IsCoverageVariant = coverage
278}
279
280func (mod *Module) EnableCoverageIfNeeded() {
281 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700282}
283
284func defaultsFactory() android.Module {
285 return DefaultsFactory()
286}
287
288type Defaults struct {
289 android.ModuleBase
290 android.DefaultsModuleBase
291}
292
293func DefaultsFactory(props ...interface{}) android.Module {
294 module := &Defaults{}
295
296 module.AddProperties(props...)
297 module.AddProperties(
298 &BaseProperties{},
299 &BaseCompilerProperties{},
300 &BinaryCompilerProperties{},
301 &LibraryCompilerProperties{},
302 &ProcMacroCompilerProperties{},
303 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700304 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400305 &cc.CoverageProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700306 )
307
308 android.InitDefaultsModule(module)
309 return module
310}
311
312func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700313 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700314}
315
Ivan Lozano183a3212019-10-18 14:18:45 -0700316func (mod *Module) CcLibrary() bool {
317 if mod.compiler != nil {
318 if _, ok := mod.compiler.(*libraryDecorator); ok {
319 return true
320 }
321 }
322 return false
323}
324
325func (mod *Module) CcLibraryInterface() bool {
326 if mod.compiler != nil {
327 if _, ok := mod.compiler.(libraryInterface); ok {
328 return true
329 }
330 }
331 return false
332}
333
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800334func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700335 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700336 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800337 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700338 }
339 }
340 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
341}
342
343func (mod *Module) SetStatic() {
344 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700345 if library, ok := mod.compiler.(libraryInterface); ok {
346 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700347 return
348 }
349 }
350 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
351}
352
353func (mod *Module) SetShared() {
354 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700355 if library, ok := mod.compiler.(libraryInterface); ok {
356 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700357 return
358 }
359 }
360 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
361}
362
363func (mod *Module) SetBuildStubs() {
364 panic("SetBuildStubs not yet implemented for rust modules")
365}
366
367func (mod *Module) SetStubsVersions(string) {
368 panic("SetStubsVersions not yet implemented for rust modules")
369}
370
Jooyung Han03b51852020-02-26 22:45:42 +0900371func (mod *Module) StubsVersion() string {
372 panic("SetStubsVersions not yet implemented for rust modules")
373}
374
Ivan Lozano183a3212019-10-18 14:18:45 -0700375func (mod *Module) BuildStaticVariant() bool {
376 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700377 if library, ok := mod.compiler.(libraryInterface); ok {
378 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700379 }
380 }
381 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
382}
383
384func (mod *Module) BuildSharedVariant() bool {
385 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700386 if library, ok := mod.compiler.(libraryInterface); ok {
387 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700388 }
389 }
390 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
391}
392
393// Rust module deps don't have a link order (?)
394func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
395
396func (mod *Module) GetDepsInLinkOrder() []android.Path {
397 return []android.Path{}
398}
399
400func (mod *Module) GetStaticVariant() cc.LinkableInterface {
401 return nil
402}
403
404func (mod *Module) Module() android.Module {
405 return mod
406}
407
408func (mod *Module) StubsVersions() []string {
409 // For now, Rust has no stubs versions.
410 if mod.compiler != nil {
411 if _, ok := mod.compiler.(*libraryDecorator); ok {
412 return []string{}
413 }
414 }
415 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
416}
417
418func (mod *Module) OutputFile() android.OptionalPath {
419 return mod.outputFile
420}
421
422func (mod *Module) InRecovery() bool {
423 // For now, Rust has no notion of the recovery image
424 return false
425}
426func (mod *Module) HasStaticVariant() bool {
427 if mod.GetStaticVariant() != nil {
428 return true
429 }
430 return false
431}
432
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400433func (mod *Module) CoverageFiles() android.Paths {
434 if mod.compiler != nil {
435 if library, ok := mod.compiler.(*libraryDecorator); ok {
436 if library.coverageFile != nil {
437 return android.Paths{library.coverageFile}
438 }
439 return android.Paths{}
440 }
441 }
442 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
443}
444
Ivan Lozano183a3212019-10-18 14:18:45 -0700445var _ cc.LinkableInterface = (*Module)(nil)
446
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447func (mod *Module) Init() android.Module {
448 mod.AddProperties(&mod.Properties)
449
450 if mod.compiler != nil {
451 mod.AddProperties(mod.compiler.compilerProps()...)
452 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400453 if mod.coverage != nil {
454 mod.AddProperties(mod.coverage.props()...)
455 }
456
Ivan Lozanoffee3342019-08-27 12:03:00 -0700457 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
458
459 android.InitDefaultableModule(mod)
460
Ivan Lozanode252912019-09-06 15:29:52 -0700461 // Explicitly disable unsupported targets.
462 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
463 disableTargets := struct {
464 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700465 Linux_bionic struct {
466 Enabled *bool
467 }
468 }
469 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700470 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
471
472 ctx.AppendProperties(&disableTargets)
473 })
474
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475 return mod
476}
477
478func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
479 return &Module{
480 hod: hod,
481 multilib: multilib,
482 }
483}
484func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
485 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400486 module.coverage = &coverage{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700487 return module
488}
489
490type ModuleContext interface {
491 android.ModuleContext
492 ModuleContextIntf
493}
494
495type BaseModuleContext interface {
496 android.BaseModuleContext
497 ModuleContextIntf
498}
499
500type DepsContext interface {
501 android.BottomUpMutatorContext
502 ModuleContextIntf
503}
504
505type ModuleContextIntf interface {
506 toolchain() config.Toolchain
507 baseModuleName() string
508 CrateName() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400509 nativeCoverage() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700510}
511
512type depsContext struct {
513 android.BottomUpMutatorContext
514 moduleContextImpl
515}
516
517type moduleContext struct {
518 android.ModuleContext
519 moduleContextImpl
520}
521
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400522func (ctx *moduleContextImpl) nativeCoverage() bool {
523 return ctx.mod.nativeCoverage()
524}
525
526func (mod *Module) nativeCoverage() bool {
527 return mod.compiler != nil && mod.compiler.nativeCoverage()
528}
529
Ivan Lozanoffee3342019-08-27 12:03:00 -0700530type moduleContextImpl struct {
531 mod *Module
532 ctx BaseModuleContext
533}
534
535func (ctx *moduleContextImpl) toolchain() config.Toolchain {
536 return ctx.mod.toolchain(ctx.ctx)
537}
538
539func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
540 if mod.cachedToolchain == nil {
541 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
542 }
543 return mod.cachedToolchain
544}
545
546func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
547}
548
549func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
550 ctx := &moduleContext{
551 ModuleContext: actx,
552 moduleContextImpl: moduleContextImpl{
553 mod: mod,
554 },
555 }
556 ctx.ctx = ctx
557
558 toolchain := mod.toolchain(ctx)
559
560 if !toolchain.Supported() {
561 // This toolchain's unsupported, there's nothing to do for this mod.
562 return
563 }
564
565 deps := mod.depsToPaths(ctx)
566 flags := Flags{
567 Toolchain: toolchain,
568 }
569
570 if mod.compiler != nil {
571 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400572 }
573 if mod.coverage != nil {
574 flags, deps = mod.coverage.flags(ctx, flags, deps)
575 }
576
577 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578 outputFile := mod.compiler.compile(ctx, flags, deps)
579 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400580 if !mod.Properties.PreventInstall {
581 mod.compiler.install(ctx, mod.outputFile.Path())
582 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700583 }
584}
585
586func (mod *Module) deps(ctx DepsContext) Deps {
587 deps := Deps{}
588
589 if mod.compiler != nil {
590 deps = mod.compiler.compilerDeps(ctx, deps)
591 }
592
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400593 if mod.coverage != nil {
594 deps = mod.coverage.deps(ctx, deps)
595 }
596
Ivan Lozanoffee3342019-08-27 12:03:00 -0700597 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
598 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
599 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
600 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
601 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
602
603 return deps
604
605}
606
607func (ctx *moduleContextImpl) baseModuleName() string {
608 return ctx.mod.ModuleBase.BaseModuleName()
609}
610
611func (ctx *moduleContextImpl) CrateName() string {
612 return ctx.mod.CrateName()
613}
614
615type dependencyTag struct {
616 blueprint.BaseDependencyTag
617 name string
618 library bool
619 proc_macro bool
620}
621
622var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700623 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
624 dylibDepTag = dependencyTag{name: "dylib", library: true}
625 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
626 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700627)
628
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400629func (mod *Module) begin(ctx BaseModuleContext) {
630 if mod.coverage != nil {
631 mod.coverage.begin(ctx)
632 }
633}
634
Ivan Lozanoffee3342019-08-27 12:03:00 -0700635func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
636 var depPaths PathDeps
637
638 directRlibDeps := []*Module{}
639 directDylibDeps := []*Module{}
640 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700641 directSharedLibDeps := [](cc.LinkableInterface){}
642 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643
644 ctx.VisitDirectDeps(func(dep android.Module) {
645 depName := ctx.OtherModuleName(dep)
646 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700647 if rustDep, ok := dep.(*Module); ok {
648 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700649
Ivan Lozanoffee3342019-08-27 12:03:00 -0700650 linkFile := rustDep.outputFile
651 if !linkFile.Valid() {
652 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
653 }
654
655 switch depTag {
656 case dylibDepTag:
657 dylib, ok := rustDep.compiler.(libraryInterface)
658 if !ok || !dylib.dylib() {
659 ctx.ModuleErrorf("mod %q not an dylib library", depName)
660 return
661 }
662 directDylibDeps = append(directDylibDeps, rustDep)
663 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
664 case rlibDepTag:
665 rlib, ok := rustDep.compiler.(libraryInterface)
666 if !ok || !rlib.rlib() {
667 ctx.ModuleErrorf("mod %q not an rlib library", depName)
668 return
669 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400670 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700671 directRlibDeps = append(directRlibDeps, rustDep)
672 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
673 case procMacroDepTag:
674 directProcMacroDeps = append(directProcMacroDeps, rustDep)
675 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
676 }
677
678 //Append the dependencies exportedDirs
679 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
680 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
681 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682 }
683
684 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
685 // This can be probably be refactored by defining a common exporter interface similar to cc's
686 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
687 linkDir := linkPathFromFilePath(linkFile.Path())
688 if lib, ok := mod.compiler.(*libraryDecorator); ok {
689 lib.linkDirs = append(lib.linkDirs, linkDir)
690 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
691 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
692 }
693 }
694
Ivan Lozano52767be2019-10-18 14:49:46 -0700695 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700696
Ivan Lozano52767be2019-10-18 14:49:46 -0700697 if ccDep, ok := dep.(cc.LinkableInterface); ok {
698 //Handle C dependencies
699 if _, ok := ccDep.(*Module); !ok {
700 if ccDep.Module().Target().Os != ctx.Os() {
701 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
702 return
703 }
704 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
705 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
706 return
707 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700708 }
709
Ivan Lozanoffee3342019-08-27 12:03:00 -0700710 linkFile := ccDep.OutputFile()
711 linkPath := linkPathFromFilePath(linkFile.Path())
712 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500713 depFlag := "-l" + libName
714
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715 if !linkFile.Valid() {
716 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
717 }
718
719 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700720 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700721 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500722 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500724 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400725 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726 directStaticLibDeps = append(directStaticLibDeps, ccDep)
727 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700728 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500729 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500731 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700732 directSharedLibDeps = append(directSharedLibDeps, ccDep)
733 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
734 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700735 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700736 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700737 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700738 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739 }
740
741 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700742 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500744 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700745 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
746 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500747 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748 }
749
750 }
751 })
752
753 var rlibDepFiles RustLibraries
754 for _, dep := range directRlibDeps {
755 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
756 }
757 var dylibDepFiles RustLibraries
758 for _, dep := range directDylibDeps {
759 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
760 }
761 var procMacroDepFiles RustLibraries
762 for _, dep := range directProcMacroDeps {
763 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
764 }
765
766 var staticLibDepFiles android.Paths
767 for _, dep := range directStaticLibDeps {
768 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
769 }
770
771 var sharedLibDepFiles android.Paths
772 for _, dep := range directSharedLibDeps {
773 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
774 }
775
776 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
777 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
778 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
779 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
780 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
781
782 // Dedup exported flags from dependencies
783 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
784 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
785
786 return depPaths
787}
788
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800789func (mod *Module) InstallInData() bool {
790 if mod.compiler == nil {
791 return false
792 }
793 return mod.compiler.inData()
794}
795
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796func linkPathFromFilePath(filepath android.Path) string {
797 return strings.Split(filepath.String(), filepath.Base())[0]
798}
Ivan Lozanod648c432020-02-06 12:05:10 -0500799
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500801 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700802 if strings.HasPrefix(libName, "lib") {
803 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 }
805 return libName
806}
Ivan Lozanod648c432020-02-06 12:05:10 -0500807
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
809 ctx := &depsContext{
810 BottomUpMutatorContext: actx,
811 moduleContextImpl: moduleContextImpl{
812 mod: mod,
813 },
814 }
815 ctx.ctx = ctx
816
817 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700818 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900819 if cc.VersionVariantAvailable(mod) {
820 commonDepVariations = append(commonDepVariations,
821 blueprint.Variation{Mutator: "version", Variation: ""})
822 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700824 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800825 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700827 actx.AddVariationDependencies(
828 append(commonDepVariations, []blueprint.Variation{
829 {Mutator: "rust_libraries", Variation: "rlib"},
830 {Mutator: "link", Variation: ""}}...),
831 rlibDepTag, deps.Rlibs...)
832 actx.AddVariationDependencies(
833 append(commonDepVariations, []blueprint.Variation{
834 {Mutator: "rust_libraries", Variation: "dylib"},
835 {Mutator: "link", Variation: ""}}...),
836 dylibDepTag, deps.Dylibs...)
837
838 actx.AddVariationDependencies(append(commonDepVariations,
839 blueprint.Variation{Mutator: "link", Variation: "shared"}),
840 cc.SharedDepTag, deps.SharedLibs...)
841 actx.AddVariationDependencies(append(commonDepVariations,
842 blueprint.Variation{Mutator: "link", Variation: "static"}),
843 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700844
Ivan Lozanof1c84332019-09-20 11:00:37 -0700845 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700846 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700847 }
848 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700849 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700850 }
851
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700852 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700853 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854}
855
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400856func BeginMutator(ctx android.BottomUpMutatorContext) {
857 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
858 mod.beginMutator(ctx)
859 }
860}
861
862type baseModuleContext struct {
863 android.BaseModuleContext
864 moduleContextImpl
865}
866
867func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
868 ctx := &baseModuleContext{
869 BaseModuleContext: actx,
870 moduleContextImpl: moduleContextImpl{
871 mod: mod,
872 },
873 }
874 ctx.ctx = ctx
875
876 mod.begin(ctx)
877}
878
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879func (mod *Module) Name() string {
880 name := mod.ModuleBase.Name()
881 if p, ok := mod.compiler.(interface {
882 Name(string) string
883 }); ok {
884 name = p.Name(name)
885 }
886 return name
887}
888
889var Bool = proptools.Bool
890var BoolDefault = proptools.BoolDefault
891var String = proptools.String
892var StringPtr = proptools.StringPtr