blob: 7b82b1f691f25bd4f75679e9c466307de5c2bc68 [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 Lozano8a23fa42020-06-16 10:26:57 -040048 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
Ivan Lozanof1c84332019-09-20 11:00:37 -070052 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040053 Coverage bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070054}
55
56type BaseProperties struct {
57 AndroidMkRlibs []string
58 AndroidMkDylibs []string
59 AndroidMkProcMacroLibs []string
60 AndroidMkSharedLibs []string
61 AndroidMkStaticLibs []string
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070062 SubName string `blueprint:"mutated"`
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063 PreventInstall bool
64 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67type Module struct {
68 android.ModuleBase
69 android.DefaultableModuleBase
70
71 Properties BaseProperties
72
73 hod android.HostOrDeviceSupported
74 multilib android.Multilib
75
76 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040077 coverage *coverage
Ivan Lozanoffee3342019-08-27 12:03:00 -070078 cachedToolchain config.Toolchain
79 subAndroidMkOnce map[subAndroidMkProvider]bool
80 outputFile android.OptionalPath
81}
82
Colin Cross7228ecd2019-11-18 16:00:16 -080083var _ android.ImageInterface = (*Module)(nil)
84
85func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
86
87func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
88 return true
89}
90
Yifan Hong1b3348d2020-01-21 15:53:22 -080091func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
92 return mod.InRamdisk()
93}
94
Colin Cross7228ecd2019-11-18 16:00:16 -080095func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
96 return mod.InRecovery()
97}
98
99func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
100 return nil
101}
102
103func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
104}
105
Ivan Lozano52767be2019-10-18 14:49:46 -0700106func (mod *Module) BuildStubs() bool {
107 return false
108}
109
110func (mod *Module) HasStubsVariants() bool {
111 return false
112}
113
114func (mod *Module) SelectedStl() string {
115 return ""
116}
117
Ivan Lozano2b262972019-11-21 12:30:50 -0800118func (mod *Module) NonCcVariants() bool {
119 if mod.compiler != nil {
120 if library, ok := mod.compiler.(libraryInterface); ok {
121 if library.buildRlib() || library.buildDylib() {
122 return true
123 } else {
124 return false
125 }
126 }
127 }
128 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
129}
130
Ivan Lozano52767be2019-10-18 14:49:46 -0700131func (mod *Module) ApiLevel() string {
132 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
133}
134
135func (mod *Module) Static() bool {
136 if mod.compiler != nil {
137 if library, ok := mod.compiler.(libraryInterface); ok {
138 return library.static()
139 }
140 }
141 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
142}
143
144func (mod *Module) Shared() bool {
145 if mod.compiler != nil {
146 if library, ok := mod.compiler.(libraryInterface); ok {
147 return library.static()
148 }
149 }
150 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
151}
152
153func (mod *Module) Toc() android.OptionalPath {
154 if mod.compiler != nil {
155 if _, ok := mod.compiler.(libraryInterface); ok {
156 return android.OptionalPath{}
157 }
158 }
159 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
160}
161
Yifan Hong1b3348d2020-01-21 15:53:22 -0800162func (mod *Module) OnlyInRamdisk() bool {
163 return false
164}
165
Ivan Lozano52767be2019-10-18 14:49:46 -0700166func (mod *Module) OnlyInRecovery() bool {
167 return false
168}
169
Colin Crossc511bc52020-04-07 16:50:32 +0000170func (mod *Module) UseSdk() bool {
171 return false
172}
173
Ivan Lozano52767be2019-10-18 14:49:46 -0700174func (mod *Module) UseVndk() bool {
175 return false
176}
177
178func (mod *Module) MustUseVendorVariant() bool {
179 return false
180}
181
182func (mod *Module) IsVndk() bool {
183 return false
184}
185
186func (mod *Module) HasVendorVariant() bool {
187 return false
188}
189
190func (mod *Module) SdkVersion() string {
191 return ""
192}
193
Colin Crossc511bc52020-04-07 16:50:32 +0000194func (mod *Module) AlwaysSdk() bool {
195 return false
196}
197
Jiyong Park2286afd2020-06-16 21:58:53 +0900198func (mod *Module) IsSdkVariant() bool {
199 return false
200}
201
Ivan Lozano52767be2019-10-18 14:49:46 -0700202func (mod *Module) ToolchainLibrary() bool {
203 return false
204}
205
206func (mod *Module) NdkPrebuiltStl() bool {
207 return false
208}
209
210func (mod *Module) StubDecorator() bool {
211 return false
212}
213
Ivan Lozanoffee3342019-08-27 12:03:00 -0700214type Deps struct {
215 Dylibs []string
216 Rlibs []string
217 ProcMacros []string
218 SharedLibs []string
219 StaticLibs []string
220
221 CrtBegin, CrtEnd string
222}
223
224type PathDeps struct {
225 DyLibs RustLibraries
226 RLibs RustLibraries
227 SharedLibs android.Paths
228 StaticLibs android.Paths
229 ProcMacros RustLibraries
230 linkDirs []string
231 depFlags []string
232 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700233
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400234 coverageFiles android.Paths
235
Ivan Lozanof1c84332019-09-20 11:00:37 -0700236 CrtBegin android.OptionalPath
237 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700238}
239
240type RustLibraries []RustLibrary
241
242type RustLibrary struct {
243 Path android.Path
244 CrateName string
245}
246
247type compiler interface {
248 compilerFlags(ctx ModuleContext, flags Flags) Flags
249 compilerProps() []interface{}
250 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
251 compilerDeps(ctx DepsContext, deps Deps) Deps
252 crateName() string
253
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800254 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255 install(ctx ModuleContext, path android.Path)
256 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400257
258 nativeCoverage() bool
259}
260
261func (mod *Module) isCoverageVariant() bool {
262 return mod.coverage.Properties.IsCoverageVariant
263}
264
265var _ cc.Coverage = (*Module)(nil)
266
267func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
268 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
269}
270
271func (mod *Module) PreventInstall() {
272 mod.Properties.PreventInstall = true
273}
274
275func (mod *Module) HideFromMake() {
276 mod.Properties.HideFromMake = true
277}
278
279func (mod *Module) MarkAsCoverageVariant(coverage bool) {
280 mod.coverage.Properties.IsCoverageVariant = coverage
281}
282
283func (mod *Module) EnableCoverageIfNeeded() {
284 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700285}
286
287func defaultsFactory() android.Module {
288 return DefaultsFactory()
289}
290
291type Defaults struct {
292 android.ModuleBase
293 android.DefaultsModuleBase
294}
295
296func DefaultsFactory(props ...interface{}) android.Module {
297 module := &Defaults{}
298
299 module.AddProperties(props...)
300 module.AddProperties(
301 &BaseProperties{},
302 &BaseCompilerProperties{},
303 &BinaryCompilerProperties{},
304 &LibraryCompilerProperties{},
305 &ProcMacroCompilerProperties{},
306 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700307 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400308 &cc.CoverageProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700309 )
310
311 android.InitDefaultsModule(module)
312 return module
313}
314
315func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700316 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700317}
318
Ivan Lozano183a3212019-10-18 14:18:45 -0700319func (mod *Module) CcLibrary() bool {
320 if mod.compiler != nil {
321 if _, ok := mod.compiler.(*libraryDecorator); ok {
322 return true
323 }
324 }
325 return false
326}
327
328func (mod *Module) CcLibraryInterface() bool {
329 if mod.compiler != nil {
330 if _, ok := mod.compiler.(libraryInterface); ok {
331 return true
332 }
333 }
334 return false
335}
336
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800337func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700338 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700339 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800340 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700341 }
342 }
343 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
344}
345
346func (mod *Module) SetStatic() {
347 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700348 if library, ok := mod.compiler.(libraryInterface); ok {
349 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700350 return
351 }
352 }
353 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
354}
355
356func (mod *Module) SetShared() {
357 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700358 if library, ok := mod.compiler.(libraryInterface); ok {
359 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700360 return
361 }
362 }
363 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
364}
365
366func (mod *Module) SetBuildStubs() {
367 panic("SetBuildStubs not yet implemented for rust modules")
368}
369
370func (mod *Module) SetStubsVersions(string) {
371 panic("SetStubsVersions not yet implemented for rust modules")
372}
373
Jooyung Han03b51852020-02-26 22:45:42 +0900374func (mod *Module) StubsVersion() string {
375 panic("SetStubsVersions not yet implemented for rust modules")
376}
377
Ivan Lozano183a3212019-10-18 14:18:45 -0700378func (mod *Module) BuildStaticVariant() bool {
379 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700380 if library, ok := mod.compiler.(libraryInterface); ok {
381 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700382 }
383 }
384 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
385}
386
387func (mod *Module) BuildSharedVariant() bool {
388 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700389 if library, ok := mod.compiler.(libraryInterface); ok {
390 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700391 }
392 }
393 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
394}
395
396// Rust module deps don't have a link order (?)
397func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
398
399func (mod *Module) GetDepsInLinkOrder() []android.Path {
400 return []android.Path{}
401}
402
403func (mod *Module) GetStaticVariant() cc.LinkableInterface {
404 return nil
405}
406
407func (mod *Module) Module() android.Module {
408 return mod
409}
410
411func (mod *Module) StubsVersions() []string {
412 // For now, Rust has no stubs versions.
413 if mod.compiler != nil {
414 if _, ok := mod.compiler.(*libraryDecorator); ok {
415 return []string{}
416 }
417 }
418 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
419}
420
421func (mod *Module) OutputFile() android.OptionalPath {
422 return mod.outputFile
423}
424
425func (mod *Module) InRecovery() bool {
426 // For now, Rust has no notion of the recovery image
427 return false
428}
429func (mod *Module) HasStaticVariant() bool {
430 if mod.GetStaticVariant() != nil {
431 return true
432 }
433 return false
434}
435
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400436func (mod *Module) CoverageFiles() android.Paths {
437 if mod.compiler != nil {
438 if library, ok := mod.compiler.(*libraryDecorator); ok {
439 if library.coverageFile != nil {
440 return android.Paths{library.coverageFile}
441 }
442 return android.Paths{}
443 }
444 }
445 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
446}
447
Ivan Lozano183a3212019-10-18 14:18:45 -0700448var _ cc.LinkableInterface = (*Module)(nil)
449
Ivan Lozanoffee3342019-08-27 12:03:00 -0700450func (mod *Module) Init() android.Module {
451 mod.AddProperties(&mod.Properties)
452
453 if mod.compiler != nil {
454 mod.AddProperties(mod.compiler.compilerProps()...)
455 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400456 if mod.coverage != nil {
457 mod.AddProperties(mod.coverage.props()...)
458 }
459
Ivan Lozanoffee3342019-08-27 12:03:00 -0700460 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
461
462 android.InitDefaultableModule(mod)
463
Ivan Lozanode252912019-09-06 15:29:52 -0700464 // Explicitly disable unsupported targets.
465 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
466 disableTargets := struct {
467 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700468 Linux_bionic struct {
469 Enabled *bool
470 }
471 }
472 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700473 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
474
475 ctx.AppendProperties(&disableTargets)
476 })
477
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478 return mod
479}
480
481func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
482 return &Module{
483 hod: hod,
484 multilib: multilib,
485 }
486}
487func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
488 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400489 module.coverage = &coverage{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700490 return module
491}
492
493type ModuleContext interface {
494 android.ModuleContext
495 ModuleContextIntf
496}
497
498type BaseModuleContext interface {
499 android.BaseModuleContext
500 ModuleContextIntf
501}
502
503type DepsContext interface {
504 android.BottomUpMutatorContext
505 ModuleContextIntf
506}
507
508type ModuleContextIntf interface {
509 toolchain() config.Toolchain
510 baseModuleName() string
511 CrateName() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400512 nativeCoverage() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700513}
514
515type depsContext struct {
516 android.BottomUpMutatorContext
517 moduleContextImpl
518}
519
520type moduleContext struct {
521 android.ModuleContext
522 moduleContextImpl
523}
524
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400525func (ctx *moduleContextImpl) nativeCoverage() bool {
526 return ctx.mod.nativeCoverage()
527}
528
529func (mod *Module) nativeCoverage() bool {
530 return mod.compiler != nil && mod.compiler.nativeCoverage()
531}
532
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533type moduleContextImpl struct {
534 mod *Module
535 ctx BaseModuleContext
536}
537
538func (ctx *moduleContextImpl) toolchain() config.Toolchain {
539 return ctx.mod.toolchain(ctx.ctx)
540}
541
542func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
543 if mod.cachedToolchain == nil {
544 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
545 }
546 return mod.cachedToolchain
547}
548
549func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
550}
551
552func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
553 ctx := &moduleContext{
554 ModuleContext: actx,
555 moduleContextImpl: moduleContextImpl{
556 mod: mod,
557 },
558 }
559 ctx.ctx = ctx
560
561 toolchain := mod.toolchain(ctx)
562
563 if !toolchain.Supported() {
564 // This toolchain's unsupported, there's nothing to do for this mod.
565 return
566 }
567
568 deps := mod.depsToPaths(ctx)
569 flags := Flags{
570 Toolchain: toolchain,
571 }
572
573 if mod.compiler != nil {
574 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400575 }
576 if mod.coverage != nil {
577 flags, deps = mod.coverage.flags(ctx, flags, deps)
578 }
579
580 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581 outputFile := mod.compiler.compile(ctx, flags, deps)
582 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583 if !mod.Properties.PreventInstall {
584 mod.compiler.install(ctx, mod.outputFile.Path())
585 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700586 }
587}
588
589func (mod *Module) deps(ctx DepsContext) Deps {
590 deps := Deps{}
591
592 if mod.compiler != nil {
593 deps = mod.compiler.compilerDeps(ctx, deps)
594 }
595
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400596 if mod.coverage != nil {
597 deps = mod.coverage.deps(ctx, deps)
598 }
599
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
601 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
602 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
603 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
604 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
605
606 return deps
607
608}
609
610func (ctx *moduleContextImpl) baseModuleName() string {
611 return ctx.mod.ModuleBase.BaseModuleName()
612}
613
614func (ctx *moduleContextImpl) CrateName() string {
615 return ctx.mod.CrateName()
616}
617
618type dependencyTag struct {
619 blueprint.BaseDependencyTag
620 name string
621 library bool
622 proc_macro bool
623}
624
625var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700626 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
627 dylibDepTag = dependencyTag{name: "dylib", library: true}
628 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
629 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700630)
631
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400632func (mod *Module) begin(ctx BaseModuleContext) {
633 if mod.coverage != nil {
634 mod.coverage.begin(ctx)
635 }
636}
637
Ivan Lozanoffee3342019-08-27 12:03:00 -0700638func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
639 var depPaths PathDeps
640
641 directRlibDeps := []*Module{}
642 directDylibDeps := []*Module{}
643 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700644 directSharedLibDeps := [](cc.LinkableInterface){}
645 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700646
647 ctx.VisitDirectDeps(func(dep android.Module) {
648 depName := ctx.OtherModuleName(dep)
649 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700650 if rustDep, ok := dep.(*Module); ok {
651 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700652
Ivan Lozanoffee3342019-08-27 12:03:00 -0700653 linkFile := rustDep.outputFile
654 if !linkFile.Valid() {
655 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
656 }
657
658 switch depTag {
659 case dylibDepTag:
660 dylib, ok := rustDep.compiler.(libraryInterface)
661 if !ok || !dylib.dylib() {
662 ctx.ModuleErrorf("mod %q not an dylib library", depName)
663 return
664 }
665 directDylibDeps = append(directDylibDeps, rustDep)
666 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
667 case rlibDepTag:
668 rlib, ok := rustDep.compiler.(libraryInterface)
669 if !ok || !rlib.rlib() {
670 ctx.ModuleErrorf("mod %q not an rlib library", depName)
671 return
672 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400673 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674 directRlibDeps = append(directRlibDeps, rustDep)
675 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
676 case procMacroDepTag:
677 directProcMacroDeps = append(directProcMacroDeps, rustDep)
678 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
679 }
680
681 //Append the dependencies exportedDirs
682 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
683 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
684 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700685 }
686
687 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
688 // This can be probably be refactored by defining a common exporter interface similar to cc's
689 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
690 linkDir := linkPathFromFilePath(linkFile.Path())
691 if lib, ok := mod.compiler.(*libraryDecorator); ok {
692 lib.linkDirs = append(lib.linkDirs, linkDir)
693 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
694 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
695 }
696 }
697
Ivan Lozano52767be2019-10-18 14:49:46 -0700698 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700699
Ivan Lozano52767be2019-10-18 14:49:46 -0700700 if ccDep, ok := dep.(cc.LinkableInterface); ok {
701 //Handle C dependencies
702 if _, ok := ccDep.(*Module); !ok {
703 if ccDep.Module().Target().Os != ctx.Os() {
704 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
705 return
706 }
707 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
708 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
709 return
710 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700711 }
712
Ivan Lozanoffee3342019-08-27 12:03:00 -0700713 linkFile := ccDep.OutputFile()
714 linkPath := linkPathFromFilePath(linkFile.Path())
715 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500716 depFlag := "-l" + libName
717
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718 if !linkFile.Valid() {
719 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
720 }
721
722 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700724 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500725 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500727 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400728 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729 directStaticLibDeps = append(directStaticLibDeps, ccDep)
730 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700731 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500732 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500734 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700735 directSharedLibDeps = append(directSharedLibDeps, ccDep)
736 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
737 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700738 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700739 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700740 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700741 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742 }
743
744 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700745 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500747 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
749 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500750 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751 }
752
753 }
754 })
755
756 var rlibDepFiles RustLibraries
757 for _, dep := range directRlibDeps {
758 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
759 }
760 var dylibDepFiles RustLibraries
761 for _, dep := range directDylibDeps {
762 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
763 }
764 var procMacroDepFiles RustLibraries
765 for _, dep := range directProcMacroDeps {
766 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
767 }
768
769 var staticLibDepFiles android.Paths
770 for _, dep := range directStaticLibDeps {
771 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
772 }
773
774 var sharedLibDepFiles android.Paths
775 for _, dep := range directSharedLibDeps {
776 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
777 }
778
779 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
780 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
781 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
782 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
783 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
784
785 // Dedup exported flags from dependencies
786 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
787 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
788
789 return depPaths
790}
791
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800792func (mod *Module) InstallInData() bool {
793 if mod.compiler == nil {
794 return false
795 }
796 return mod.compiler.inData()
797}
798
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799func linkPathFromFilePath(filepath android.Path) string {
800 return strings.Split(filepath.String(), filepath.Base())[0]
801}
Ivan Lozanod648c432020-02-06 12:05:10 -0500802
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500804 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700805 if strings.HasPrefix(libName, "lib") {
806 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 }
808 return libName
809}
Ivan Lozanod648c432020-02-06 12:05:10 -0500810
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
812 ctx := &depsContext{
813 BottomUpMutatorContext: actx,
814 moduleContextImpl: moduleContextImpl{
815 mod: mod,
816 },
817 }
818 ctx.ctx = ctx
819
820 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700821 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900822 if cc.VersionVariantAvailable(mod) {
823 commonDepVariations = append(commonDepVariations,
824 blueprint.Variation{Mutator: "version", Variation: ""})
825 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700827 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800828 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700830 actx.AddVariationDependencies(
831 append(commonDepVariations, []blueprint.Variation{
832 {Mutator: "rust_libraries", Variation: "rlib"},
833 {Mutator: "link", Variation: ""}}...),
834 rlibDepTag, deps.Rlibs...)
835 actx.AddVariationDependencies(
836 append(commonDepVariations, []blueprint.Variation{
837 {Mutator: "rust_libraries", Variation: "dylib"},
838 {Mutator: "link", Variation: ""}}...),
839 dylibDepTag, deps.Dylibs...)
840
841 actx.AddVariationDependencies(append(commonDepVariations,
842 blueprint.Variation{Mutator: "link", Variation: "shared"}),
843 cc.SharedDepTag, deps.SharedLibs...)
844 actx.AddVariationDependencies(append(commonDepVariations,
845 blueprint.Variation{Mutator: "link", Variation: "static"}),
846 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700847
Ivan Lozanof1c84332019-09-20 11:00:37 -0700848 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700849 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700850 }
851 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700852 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700853 }
854
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700855 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700856 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857}
858
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400859func BeginMutator(ctx android.BottomUpMutatorContext) {
860 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
861 mod.beginMutator(ctx)
862 }
863}
864
865type baseModuleContext struct {
866 android.BaseModuleContext
867 moduleContextImpl
868}
869
870func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
871 ctx := &baseModuleContext{
872 BaseModuleContext: actx,
873 moduleContextImpl: moduleContextImpl{
874 mod: mod,
875 },
876 }
877 ctx.ctx = ctx
878
879 mod.begin(ctx)
880}
881
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882func (mod *Module) Name() string {
883 name := mod.ModuleBase.Name()
884 if p, ok := mod.compiler.(interface {
885 Name(string) string
886 }); ok {
887 name = p.Name(name)
888 }
889 return name
890}
891
892var Bool = proptools.Bool
893var BoolDefault = proptools.BoolDefault
894var String = proptools.String
895var StringPtr = proptools.StringPtr