blob: 1272e1d8fea60af2aeb707240f6db7843ed45af6 [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
Jiyong Park2286afd2020-06-16 21:58:53 +0900199func (mod *Module) IsSdkVariant() bool {
200 return false
201}
202
Ivan Lozano52767be2019-10-18 14:49:46 -0700203func (mod *Module) ToolchainLibrary() bool {
204 return false
205}
206
207func (mod *Module) NdkPrebuiltStl() bool {
208 return false
209}
210
211func (mod *Module) StubDecorator() bool {
212 return false
213}
214
Ivan Lozanoffee3342019-08-27 12:03:00 -0700215type Deps struct {
216 Dylibs []string
217 Rlibs []string
218 ProcMacros []string
219 SharedLibs []string
220 StaticLibs []string
221
222 CrtBegin, CrtEnd string
223}
224
225type PathDeps struct {
226 DyLibs RustLibraries
227 RLibs RustLibraries
228 SharedLibs android.Paths
229 StaticLibs android.Paths
230 ProcMacros RustLibraries
231 linkDirs []string
232 depFlags []string
233 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700234
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400235 coverageFiles android.Paths
236
Ivan Lozanof1c84332019-09-20 11:00:37 -0700237 CrtBegin android.OptionalPath
238 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239}
240
241type RustLibraries []RustLibrary
242
243type RustLibrary struct {
244 Path android.Path
245 CrateName string
246}
247
248type compiler interface {
249 compilerFlags(ctx ModuleContext, flags Flags) Flags
250 compilerProps() []interface{}
251 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
252 compilerDeps(ctx DepsContext, deps Deps) Deps
253 crateName() string
254
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800255 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256 install(ctx ModuleContext, path android.Path)
257 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400258
259 nativeCoverage() bool
260}
261
262func (mod *Module) isCoverageVariant() bool {
263 return mod.coverage.Properties.IsCoverageVariant
264}
265
266var _ cc.Coverage = (*Module)(nil)
267
268func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
269 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
270}
271
272func (mod *Module) PreventInstall() {
273 mod.Properties.PreventInstall = true
274}
275
276func (mod *Module) HideFromMake() {
277 mod.Properties.HideFromMake = true
278}
279
280func (mod *Module) MarkAsCoverageVariant(coverage bool) {
281 mod.coverage.Properties.IsCoverageVariant = coverage
282}
283
284func (mod *Module) EnableCoverageIfNeeded() {
285 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286}
287
288func defaultsFactory() android.Module {
289 return DefaultsFactory()
290}
291
292type Defaults struct {
293 android.ModuleBase
294 android.DefaultsModuleBase
295}
296
297func DefaultsFactory(props ...interface{}) android.Module {
298 module := &Defaults{}
299
300 module.AddProperties(props...)
301 module.AddProperties(
302 &BaseProperties{},
303 &BaseCompilerProperties{},
304 &BinaryCompilerProperties{},
305 &LibraryCompilerProperties{},
306 &ProcMacroCompilerProperties{},
307 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700308 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400309 &cc.CoverageProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700310 )
311
312 android.InitDefaultsModule(module)
313 return module
314}
315
316func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700317 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318}
319
Ivan Lozano183a3212019-10-18 14:18:45 -0700320func (mod *Module) CcLibrary() bool {
321 if mod.compiler != nil {
322 if _, ok := mod.compiler.(*libraryDecorator); ok {
323 return true
324 }
325 }
326 return false
327}
328
329func (mod *Module) CcLibraryInterface() bool {
330 if mod.compiler != nil {
331 if _, ok := mod.compiler.(libraryInterface); ok {
332 return true
333 }
334 }
335 return false
336}
337
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800338func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700339 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700340 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800341 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700342 }
343 }
344 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
345}
346
347func (mod *Module) SetStatic() {
348 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700349 if library, ok := mod.compiler.(libraryInterface); ok {
350 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700351 return
352 }
353 }
354 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
355}
356
357func (mod *Module) SetShared() {
358 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700359 if library, ok := mod.compiler.(libraryInterface); ok {
360 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700361 return
362 }
363 }
364 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
365}
366
367func (mod *Module) SetBuildStubs() {
368 panic("SetBuildStubs not yet implemented for rust modules")
369}
370
371func (mod *Module) SetStubsVersions(string) {
372 panic("SetStubsVersions not yet implemented for rust modules")
373}
374
Jooyung Han03b51852020-02-26 22:45:42 +0900375func (mod *Module) StubsVersion() string {
376 panic("SetStubsVersions not yet implemented for rust modules")
377}
378
Ivan Lozano183a3212019-10-18 14:18:45 -0700379func (mod *Module) BuildStaticVariant() bool {
380 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700381 if library, ok := mod.compiler.(libraryInterface); ok {
382 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700383 }
384 }
385 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
386}
387
388func (mod *Module) BuildSharedVariant() bool {
389 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700390 if library, ok := mod.compiler.(libraryInterface); ok {
391 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700392 }
393 }
394 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
395}
396
397// Rust module deps don't have a link order (?)
398func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
399
400func (mod *Module) GetDepsInLinkOrder() []android.Path {
401 return []android.Path{}
402}
403
404func (mod *Module) GetStaticVariant() cc.LinkableInterface {
405 return nil
406}
407
408func (mod *Module) Module() android.Module {
409 return mod
410}
411
412func (mod *Module) StubsVersions() []string {
413 // For now, Rust has no stubs versions.
414 if mod.compiler != nil {
415 if _, ok := mod.compiler.(*libraryDecorator); ok {
416 return []string{}
417 }
418 }
419 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
420}
421
422func (mod *Module) OutputFile() android.OptionalPath {
423 return mod.outputFile
424}
425
426func (mod *Module) InRecovery() bool {
427 // For now, Rust has no notion of the recovery image
428 return false
429}
430func (mod *Module) HasStaticVariant() bool {
431 if mod.GetStaticVariant() != nil {
432 return true
433 }
434 return false
435}
436
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400437func (mod *Module) CoverageFiles() android.Paths {
438 if mod.compiler != nil {
439 if library, ok := mod.compiler.(*libraryDecorator); ok {
440 if library.coverageFile != nil {
441 return android.Paths{library.coverageFile}
442 }
443 return android.Paths{}
444 }
445 }
446 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
447}
448
Ivan Lozano183a3212019-10-18 14:18:45 -0700449var _ cc.LinkableInterface = (*Module)(nil)
450
Ivan Lozanoffee3342019-08-27 12:03:00 -0700451func (mod *Module) Init() android.Module {
452 mod.AddProperties(&mod.Properties)
453
454 if mod.compiler != nil {
455 mod.AddProperties(mod.compiler.compilerProps()...)
456 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400457 if mod.coverage != nil {
458 mod.AddProperties(mod.coverage.props()...)
459 }
460
Ivan Lozanoffee3342019-08-27 12:03:00 -0700461 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
462
463 android.InitDefaultableModule(mod)
464
Ivan Lozanode252912019-09-06 15:29:52 -0700465 // Explicitly disable unsupported targets.
466 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
467 disableTargets := struct {
468 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700469 Linux_bionic struct {
470 Enabled *bool
471 }
472 }
473 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700474 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
475
476 ctx.AppendProperties(&disableTargets)
477 })
478
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479 return mod
480}
481
482func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
483 return &Module{
484 hod: hod,
485 multilib: multilib,
486 }
487}
488func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
489 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400490 module.coverage = &coverage{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700491 return module
492}
493
494type ModuleContext interface {
495 android.ModuleContext
496 ModuleContextIntf
497}
498
499type BaseModuleContext interface {
500 android.BaseModuleContext
501 ModuleContextIntf
502}
503
504type DepsContext interface {
505 android.BottomUpMutatorContext
506 ModuleContextIntf
507}
508
509type ModuleContextIntf interface {
510 toolchain() config.Toolchain
511 baseModuleName() string
512 CrateName() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513 nativeCoverage() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700514}
515
516type depsContext struct {
517 android.BottomUpMutatorContext
518 moduleContextImpl
519}
520
521type moduleContext struct {
522 android.ModuleContext
523 moduleContextImpl
524}
525
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400526func (ctx *moduleContextImpl) nativeCoverage() bool {
527 return ctx.mod.nativeCoverage()
528}
529
530func (mod *Module) nativeCoverage() bool {
531 return mod.compiler != nil && mod.compiler.nativeCoverage()
532}
533
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534type moduleContextImpl struct {
535 mod *Module
536 ctx BaseModuleContext
537}
538
539func (ctx *moduleContextImpl) toolchain() config.Toolchain {
540 return ctx.mod.toolchain(ctx.ctx)
541}
542
543func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
544 if mod.cachedToolchain == nil {
545 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
546 }
547 return mod.cachedToolchain
548}
549
550func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
551}
552
553func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
554 ctx := &moduleContext{
555 ModuleContext: actx,
556 moduleContextImpl: moduleContextImpl{
557 mod: mod,
558 },
559 }
560 ctx.ctx = ctx
561
562 toolchain := mod.toolchain(ctx)
563
564 if !toolchain.Supported() {
565 // This toolchain's unsupported, there's nothing to do for this mod.
566 return
567 }
568
569 deps := mod.depsToPaths(ctx)
570 flags := Flags{
571 Toolchain: toolchain,
572 }
573
574 if mod.compiler != nil {
575 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400576 }
577 if mod.coverage != nil {
578 flags, deps = mod.coverage.flags(ctx, flags, deps)
579 }
580
581 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700582 outputFile := mod.compiler.compile(ctx, flags, deps)
583 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400584 if !mod.Properties.PreventInstall {
585 mod.compiler.install(ctx, mod.outputFile.Path())
586 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587 }
588}
589
590func (mod *Module) deps(ctx DepsContext) Deps {
591 deps := Deps{}
592
593 if mod.compiler != nil {
594 deps = mod.compiler.compilerDeps(ctx, deps)
595 }
596
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400597 if mod.coverage != nil {
598 deps = mod.coverage.deps(ctx, deps)
599 }
600
Ivan Lozanoffee3342019-08-27 12:03:00 -0700601 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
602 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
603 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
604 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
605 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
606
607 return deps
608
609}
610
611func (ctx *moduleContextImpl) baseModuleName() string {
612 return ctx.mod.ModuleBase.BaseModuleName()
613}
614
615func (ctx *moduleContextImpl) CrateName() string {
616 return ctx.mod.CrateName()
617}
618
619type dependencyTag struct {
620 blueprint.BaseDependencyTag
621 name string
622 library bool
623 proc_macro bool
624}
625
626var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700627 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
628 dylibDepTag = dependencyTag{name: "dylib", library: true}
629 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
630 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700631)
632
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400633func (mod *Module) begin(ctx BaseModuleContext) {
634 if mod.coverage != nil {
635 mod.coverage.begin(ctx)
636 }
637}
638
Ivan Lozanoffee3342019-08-27 12:03:00 -0700639func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
640 var depPaths PathDeps
641
642 directRlibDeps := []*Module{}
643 directDylibDeps := []*Module{}
644 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700645 directSharedLibDeps := [](cc.LinkableInterface){}
646 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700647
648 ctx.VisitDirectDeps(func(dep android.Module) {
649 depName := ctx.OtherModuleName(dep)
650 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651 if rustDep, ok := dep.(*Module); ok {
652 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700653
Ivan Lozanoffee3342019-08-27 12:03:00 -0700654 linkFile := rustDep.outputFile
655 if !linkFile.Valid() {
656 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
657 }
658
659 switch depTag {
660 case dylibDepTag:
661 dylib, ok := rustDep.compiler.(libraryInterface)
662 if !ok || !dylib.dylib() {
663 ctx.ModuleErrorf("mod %q not an dylib library", depName)
664 return
665 }
666 directDylibDeps = append(directDylibDeps, rustDep)
667 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
668 case rlibDepTag:
669 rlib, ok := rustDep.compiler.(libraryInterface)
670 if !ok || !rlib.rlib() {
671 ctx.ModuleErrorf("mod %q not an rlib library", depName)
672 return
673 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400674 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675 directRlibDeps = append(directRlibDeps, rustDep)
676 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
677 case procMacroDepTag:
678 directProcMacroDeps = append(directProcMacroDeps, rustDep)
679 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
680 }
681
682 //Append the dependencies exportedDirs
683 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
684 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
685 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700686 }
687
688 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
689 // This can be probably be refactored by defining a common exporter interface similar to cc's
690 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
691 linkDir := linkPathFromFilePath(linkFile.Path())
692 if lib, ok := mod.compiler.(*libraryDecorator); ok {
693 lib.linkDirs = append(lib.linkDirs, linkDir)
694 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
695 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
696 }
697 }
698
Ivan Lozano52767be2019-10-18 14:49:46 -0700699 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700700
Ivan Lozano52767be2019-10-18 14:49:46 -0700701 if ccDep, ok := dep.(cc.LinkableInterface); ok {
702 //Handle C dependencies
703 if _, ok := ccDep.(*Module); !ok {
704 if ccDep.Module().Target().Os != ctx.Os() {
705 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
706 return
707 }
708 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
709 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
710 return
711 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700712 }
713
Ivan Lozanoffee3342019-08-27 12:03:00 -0700714 linkFile := ccDep.OutputFile()
715 linkPath := linkPathFromFilePath(linkFile.Path())
716 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500717 depFlag := "-l" + libName
718
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719 if !linkFile.Valid() {
720 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
721 }
722
723 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700725 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500726 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700727 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500728 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400729 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730 directStaticLibDeps = append(directStaticLibDeps, ccDep)
731 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700732 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500733 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700734 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500735 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736 directSharedLibDeps = append(directSharedLibDeps, ccDep)
737 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
738 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700739 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700740 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700741 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700742 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 }
744
745 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700746 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700747 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500748 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
750 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500751 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 }
753
754 }
755 })
756
757 var rlibDepFiles RustLibraries
758 for _, dep := range directRlibDeps {
759 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
760 }
761 var dylibDepFiles RustLibraries
762 for _, dep := range directDylibDeps {
763 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
764 }
765 var procMacroDepFiles RustLibraries
766 for _, dep := range directProcMacroDeps {
767 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
768 }
769
770 var staticLibDepFiles android.Paths
771 for _, dep := range directStaticLibDeps {
772 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
773 }
774
775 var sharedLibDepFiles android.Paths
776 for _, dep := range directSharedLibDeps {
777 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
778 }
779
780 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
781 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
782 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
783 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
784 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
785
786 // Dedup exported flags from dependencies
787 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
788 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
789
790 return depPaths
791}
792
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800793func (mod *Module) InstallInData() bool {
794 if mod.compiler == nil {
795 return false
796 }
797 return mod.compiler.inData()
798}
799
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800func linkPathFromFilePath(filepath android.Path) string {
801 return strings.Split(filepath.String(), filepath.Base())[0]
802}
Ivan Lozanod648c432020-02-06 12:05:10 -0500803
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500805 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700806 if strings.HasPrefix(libName, "lib") {
807 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808 }
809 return libName
810}
Ivan Lozanod648c432020-02-06 12:05:10 -0500811
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
813 ctx := &depsContext{
814 BottomUpMutatorContext: actx,
815 moduleContextImpl: moduleContextImpl{
816 mod: mod,
817 },
818 }
819 ctx.ctx = ctx
820
821 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700822 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900823 if cc.VersionVariantAvailable(mod) {
824 commonDepVariations = append(commonDepVariations,
825 blueprint.Variation{Mutator: "version", Variation: ""})
826 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700828 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800829 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700831 actx.AddVariationDependencies(
832 append(commonDepVariations, []blueprint.Variation{
833 {Mutator: "rust_libraries", Variation: "rlib"},
834 {Mutator: "link", Variation: ""}}...),
835 rlibDepTag, deps.Rlibs...)
836 actx.AddVariationDependencies(
837 append(commonDepVariations, []blueprint.Variation{
838 {Mutator: "rust_libraries", Variation: "dylib"},
839 {Mutator: "link", Variation: ""}}...),
840 dylibDepTag, deps.Dylibs...)
841
842 actx.AddVariationDependencies(append(commonDepVariations,
843 blueprint.Variation{Mutator: "link", Variation: "shared"}),
844 cc.SharedDepTag, deps.SharedLibs...)
845 actx.AddVariationDependencies(append(commonDepVariations,
846 blueprint.Variation{Mutator: "link", Variation: "static"}),
847 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700848
Ivan Lozanof1c84332019-09-20 11:00:37 -0700849 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700850 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700851 }
852 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700853 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700854 }
855
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700856 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700857 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700858}
859
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400860func BeginMutator(ctx android.BottomUpMutatorContext) {
861 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
862 mod.beginMutator(ctx)
863 }
864}
865
866type baseModuleContext struct {
867 android.BaseModuleContext
868 moduleContextImpl
869}
870
871func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
872 ctx := &baseModuleContext{
873 BaseModuleContext: actx,
874 moduleContextImpl: moduleContextImpl{
875 mod: mod,
876 },
877 }
878 ctx.ctx = ctx
879
880 mod.begin(ctx)
881}
882
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883func (mod *Module) Name() string {
884 name := mod.ModuleBase.Name()
885 if p, ok := mod.compiler.(interface {
886 Name(string) string
887 }); ok {
888 name = p.Name(name)
889 }
890 return name
891}
892
893var Bool = proptools.Bool
894var BoolDefault = proptools.BoolDefault
895var String = proptools.String
896var StringPtr = proptools.StringPtr