blob: 03efb75914f27aab0dc523cb65525cc652ae8ee6 [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
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020052 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070053 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040054 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070056}
57
58type BaseProperties struct {
59 AndroidMkRlibs []string
60 AndroidMkDylibs []string
61 AndroidMkProcMacroLibs []string
62 AndroidMkSharedLibs []string
63 AndroidMkStaticLibs []string
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070064 SubName string `blueprint:"mutated"`
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040065 PreventInstall bool
66 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070067}
68
69type Module struct {
70 android.ModuleBase
71 android.DefaultableModuleBase
72
73 Properties BaseProperties
74
75 hod android.HostOrDeviceSupported
76 multilib android.Multilib
77
78 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040079 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020080 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070081 cachedToolchain config.Toolchain
82 subAndroidMkOnce map[subAndroidMkProvider]bool
83 outputFile android.OptionalPath
84}
85
Colin Cross7228ecd2019-11-18 16:00:16 -080086var _ android.ImageInterface = (*Module)(nil)
87
88func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
89
90func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
91 return true
92}
93
Yifan Hong1b3348d2020-01-21 15:53:22 -080094func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
95 return mod.InRamdisk()
96}
97
Colin Cross7228ecd2019-11-18 16:00:16 -080098func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
99 return mod.InRecovery()
100}
101
102func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
103 return nil
104}
105
106func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
107}
108
Ivan Lozano52767be2019-10-18 14:49:46 -0700109func (mod *Module) BuildStubs() bool {
110 return false
111}
112
113func (mod *Module) HasStubsVariants() bool {
114 return false
115}
116
117func (mod *Module) SelectedStl() string {
118 return ""
119}
120
Ivan Lozano2b262972019-11-21 12:30:50 -0800121func (mod *Module) NonCcVariants() bool {
122 if mod.compiler != nil {
123 if library, ok := mod.compiler.(libraryInterface); ok {
124 if library.buildRlib() || library.buildDylib() {
125 return true
126 } else {
127 return false
128 }
129 }
130 }
131 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
132}
133
Ivan Lozano52767be2019-10-18 14:49:46 -0700134func (mod *Module) ApiLevel() string {
135 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
136}
137
138func (mod *Module) Static() bool {
139 if mod.compiler != nil {
140 if library, ok := mod.compiler.(libraryInterface); ok {
141 return library.static()
142 }
143 }
144 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
145}
146
147func (mod *Module) Shared() bool {
148 if mod.compiler != nil {
149 if library, ok := mod.compiler.(libraryInterface); ok {
150 return library.static()
151 }
152 }
153 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
154}
155
156func (mod *Module) Toc() android.OptionalPath {
157 if mod.compiler != nil {
158 if _, ok := mod.compiler.(libraryInterface); ok {
159 return android.OptionalPath{}
160 }
161 }
162 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
163}
164
Yifan Hong1b3348d2020-01-21 15:53:22 -0800165func (mod *Module) OnlyInRamdisk() bool {
166 return false
167}
168
Ivan Lozano52767be2019-10-18 14:49:46 -0700169func (mod *Module) OnlyInRecovery() bool {
170 return false
171}
172
Colin Crossc511bc52020-04-07 16:50:32 +0000173func (mod *Module) UseSdk() bool {
174 return false
175}
176
Ivan Lozano52767be2019-10-18 14:49:46 -0700177func (mod *Module) UseVndk() bool {
178 return false
179}
180
181func (mod *Module) MustUseVendorVariant() bool {
182 return false
183}
184
185func (mod *Module) IsVndk() bool {
186 return false
187}
188
189func (mod *Module) HasVendorVariant() bool {
190 return false
191}
192
193func (mod *Module) SdkVersion() string {
194 return ""
195}
196
Colin Crossc511bc52020-04-07 16:50:32 +0000197func (mod *Module) AlwaysSdk() bool {
198 return false
199}
200
Jiyong Park2286afd2020-06-16 21:58:53 +0900201func (mod *Module) IsSdkVariant() bool {
202 return false
203}
204
Ivan Lozano52767be2019-10-18 14:49:46 -0700205func (mod *Module) ToolchainLibrary() bool {
206 return false
207}
208
209func (mod *Module) NdkPrebuiltStl() bool {
210 return false
211}
212
213func (mod *Module) StubDecorator() bool {
214 return false
215}
216
Ivan Lozanoffee3342019-08-27 12:03:00 -0700217type Deps struct {
218 Dylibs []string
219 Rlibs []string
220 ProcMacros []string
221 SharedLibs []string
222 StaticLibs []string
223
224 CrtBegin, CrtEnd string
225}
226
227type PathDeps struct {
228 DyLibs RustLibraries
229 RLibs RustLibraries
230 SharedLibs android.Paths
231 StaticLibs android.Paths
232 ProcMacros RustLibraries
233 linkDirs []string
234 depFlags []string
235 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700236
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400237 coverageFiles android.Paths
238
Ivan Lozanof1c84332019-09-20 11:00:37 -0700239 CrtBegin android.OptionalPath
240 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241}
242
243type RustLibraries []RustLibrary
244
245type RustLibrary struct {
246 Path android.Path
247 CrateName string
248}
249
250type compiler interface {
251 compilerFlags(ctx ModuleContext, flags Flags) Flags
252 compilerProps() []interface{}
253 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
254 compilerDeps(ctx DepsContext, deps Deps) Deps
255 crateName() string
256
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800257 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258 install(ctx ModuleContext, path android.Path)
259 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400260
261 nativeCoverage() bool
262}
263
264func (mod *Module) isCoverageVariant() bool {
265 return mod.coverage.Properties.IsCoverageVariant
266}
267
268var _ cc.Coverage = (*Module)(nil)
269
270func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
271 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
272}
273
274func (mod *Module) PreventInstall() {
275 mod.Properties.PreventInstall = true
276}
277
278func (mod *Module) HideFromMake() {
279 mod.Properties.HideFromMake = true
280}
281
282func (mod *Module) MarkAsCoverageVariant(coverage bool) {
283 mod.coverage.Properties.IsCoverageVariant = coverage
284}
285
286func (mod *Module) EnableCoverageIfNeeded() {
287 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700288}
289
290func defaultsFactory() android.Module {
291 return DefaultsFactory()
292}
293
294type Defaults struct {
295 android.ModuleBase
296 android.DefaultsModuleBase
297}
298
299func DefaultsFactory(props ...interface{}) android.Module {
300 module := &Defaults{}
301
302 module.AddProperties(props...)
303 module.AddProperties(
304 &BaseProperties{},
305 &BaseCompilerProperties{},
306 &BinaryCompilerProperties{},
307 &LibraryCompilerProperties{},
308 &ProcMacroCompilerProperties{},
309 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700310 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400311 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200312 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700313 )
314
315 android.InitDefaultsModule(module)
316 return module
317}
318
319func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700320 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700321}
322
Ivan Lozano183a3212019-10-18 14:18:45 -0700323func (mod *Module) CcLibrary() bool {
324 if mod.compiler != nil {
325 if _, ok := mod.compiler.(*libraryDecorator); ok {
326 return true
327 }
328 }
329 return false
330}
331
332func (mod *Module) CcLibraryInterface() bool {
333 if mod.compiler != nil {
334 if _, ok := mod.compiler.(libraryInterface); ok {
335 return true
336 }
337 }
338 return false
339}
340
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800341func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700342 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700343 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800344 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700345 }
346 }
347 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
348}
349
350func (mod *Module) SetStatic() {
351 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700352 if library, ok := mod.compiler.(libraryInterface); ok {
353 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700354 return
355 }
356 }
357 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
358}
359
360func (mod *Module) SetShared() {
361 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700362 if library, ok := mod.compiler.(libraryInterface); ok {
363 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700364 return
365 }
366 }
367 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
368}
369
370func (mod *Module) SetBuildStubs() {
371 panic("SetBuildStubs not yet implemented for rust modules")
372}
373
374func (mod *Module) SetStubsVersions(string) {
375 panic("SetStubsVersions not yet implemented for rust modules")
376}
377
Jooyung Han03b51852020-02-26 22:45:42 +0900378func (mod *Module) StubsVersion() string {
379 panic("SetStubsVersions not yet implemented for rust modules")
380}
381
Ivan Lozano183a3212019-10-18 14:18:45 -0700382func (mod *Module) BuildStaticVariant() bool {
383 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700384 if library, ok := mod.compiler.(libraryInterface); ok {
385 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700386 }
387 }
388 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
389}
390
391func (mod *Module) BuildSharedVariant() bool {
392 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700393 if library, ok := mod.compiler.(libraryInterface); ok {
394 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700395 }
396 }
397 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
398}
399
400// Rust module deps don't have a link order (?)
401func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
402
403func (mod *Module) GetDepsInLinkOrder() []android.Path {
404 return []android.Path{}
405}
406
407func (mod *Module) GetStaticVariant() cc.LinkableInterface {
408 return nil
409}
410
411func (mod *Module) Module() android.Module {
412 return mod
413}
414
415func (mod *Module) StubsVersions() []string {
416 // For now, Rust has no stubs versions.
417 if mod.compiler != nil {
418 if _, ok := mod.compiler.(*libraryDecorator); ok {
419 return []string{}
420 }
421 }
422 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
423}
424
425func (mod *Module) OutputFile() android.OptionalPath {
426 return mod.outputFile
427}
428
429func (mod *Module) InRecovery() bool {
430 // For now, Rust has no notion of the recovery image
431 return false
432}
433func (mod *Module) HasStaticVariant() bool {
434 if mod.GetStaticVariant() != nil {
435 return true
436 }
437 return false
438}
439
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400440func (mod *Module) CoverageFiles() android.Paths {
441 if mod.compiler != nil {
442 if library, ok := mod.compiler.(*libraryDecorator); ok {
443 if library.coverageFile != nil {
444 return android.Paths{library.coverageFile}
445 }
446 return android.Paths{}
447 }
448 }
449 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
450}
451
Ivan Lozano183a3212019-10-18 14:18:45 -0700452var _ cc.LinkableInterface = (*Module)(nil)
453
Ivan Lozanoffee3342019-08-27 12:03:00 -0700454func (mod *Module) Init() android.Module {
455 mod.AddProperties(&mod.Properties)
456
457 if mod.compiler != nil {
458 mod.AddProperties(mod.compiler.compilerProps()...)
459 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400460 if mod.coverage != nil {
461 mod.AddProperties(mod.coverage.props()...)
462 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200463 if mod.clippy != nil {
464 mod.AddProperties(mod.clippy.props()...)
465 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400466
Ivan Lozanoffee3342019-08-27 12:03:00 -0700467 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
468
469 android.InitDefaultableModule(mod)
470
Ivan Lozanode252912019-09-06 15:29:52 -0700471 // Explicitly disable unsupported targets.
472 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
473 disableTargets := struct {
474 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700475 Linux_bionic struct {
476 Enabled *bool
477 }
478 }
479 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700480 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
481
482 ctx.AppendProperties(&disableTargets)
483 })
484
Ivan Lozanoffee3342019-08-27 12:03:00 -0700485 return mod
486}
487
488func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
489 return &Module{
490 hod: hod,
491 multilib: multilib,
492 }
493}
494func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
495 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400496 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200497 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498 return module
499}
500
501type ModuleContext interface {
502 android.ModuleContext
503 ModuleContextIntf
504}
505
506type BaseModuleContext interface {
507 android.BaseModuleContext
508 ModuleContextIntf
509}
510
511type DepsContext interface {
512 android.BottomUpMutatorContext
513 ModuleContextIntf
514}
515
516type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200517 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700519}
520
521type depsContext struct {
522 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700523}
524
525type moduleContext struct {
526 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700527}
528
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200529type baseModuleContext struct {
530 android.BaseModuleContext
531}
532
533func (ctx *moduleContext) RustModule() *Module {
534 return ctx.Module().(*Module)
535}
536
537func (ctx *moduleContext) toolchain() config.Toolchain {
538 return ctx.RustModule().toolchain(ctx)
539}
540
541func (ctx *depsContext) RustModule() *Module {
542 return ctx.Module().(*Module)
543}
544
545func (ctx *depsContext) toolchain() config.Toolchain {
546 return ctx.RustModule().toolchain(ctx)
547}
548
549func (ctx *baseModuleContext) RustModule() *Module {
550 return ctx.Module().(*Module)
551}
552
553func (ctx *baseModuleContext) toolchain() config.Toolchain {
554 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400555}
556
557func (mod *Module) nativeCoverage() bool {
558 return mod.compiler != nil && mod.compiler.nativeCoverage()
559}
560
Ivan Lozanoffee3342019-08-27 12:03:00 -0700561func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
562 if mod.cachedToolchain == nil {
563 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
564 }
565 return mod.cachedToolchain
566}
567
568func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
569}
570
571func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
572 ctx := &moduleContext{
573 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700574 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700575
576 toolchain := mod.toolchain(ctx)
577
578 if !toolchain.Supported() {
579 // This toolchain's unsupported, there's nothing to do for this mod.
580 return
581 }
582
583 deps := mod.depsToPaths(ctx)
584 flags := Flags{
585 Toolchain: toolchain,
586 }
587
588 if mod.compiler != nil {
589 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400590 }
591 if mod.coverage != nil {
592 flags, deps = mod.coverage.flags(ctx, flags, deps)
593 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200594 if mod.clippy != nil {
595 flags, deps = mod.clippy.flags(ctx, flags, deps)
596 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400597
598 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599 outputFile := mod.compiler.compile(ctx, flags, deps)
600 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400601 if !mod.Properties.PreventInstall {
602 mod.compiler.install(ctx, mod.outputFile.Path())
603 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604 }
605}
606
607func (mod *Module) deps(ctx DepsContext) Deps {
608 deps := Deps{}
609
610 if mod.compiler != nil {
611 deps = mod.compiler.compilerDeps(ctx, deps)
612 }
613
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400614 if mod.coverage != nil {
615 deps = mod.coverage.deps(ctx, deps)
616 }
617
Ivan Lozanoffee3342019-08-27 12:03:00 -0700618 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
619 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
620 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
621 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
622 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
623
624 return deps
625
626}
627
Ivan Lozanoffee3342019-08-27 12:03:00 -0700628type dependencyTag struct {
629 blueprint.BaseDependencyTag
630 name string
631 library bool
632 proc_macro bool
633}
634
635var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700636 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
637 dylibDepTag = dependencyTag{name: "dylib", library: true}
638 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
639 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700640)
641
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400642func (mod *Module) begin(ctx BaseModuleContext) {
643 if mod.coverage != nil {
644 mod.coverage.begin(ctx)
645 }
646}
647
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
649 var depPaths PathDeps
650
651 directRlibDeps := []*Module{}
652 directDylibDeps := []*Module{}
653 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700654 directSharedLibDeps := [](cc.LinkableInterface){}
655 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700656
657 ctx.VisitDirectDeps(func(dep android.Module) {
658 depName := ctx.OtherModuleName(dep)
659 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700660 if rustDep, ok := dep.(*Module); ok {
661 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700662
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663 linkFile := rustDep.outputFile
664 if !linkFile.Valid() {
665 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
666 }
667
668 switch depTag {
669 case dylibDepTag:
670 dylib, ok := rustDep.compiler.(libraryInterface)
671 if !ok || !dylib.dylib() {
672 ctx.ModuleErrorf("mod %q not an dylib library", depName)
673 return
674 }
675 directDylibDeps = append(directDylibDeps, rustDep)
676 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
677 case rlibDepTag:
678 rlib, ok := rustDep.compiler.(libraryInterface)
679 if !ok || !rlib.rlib() {
680 ctx.ModuleErrorf("mod %q not an rlib library", depName)
681 return
682 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400683 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700684 directRlibDeps = append(directRlibDeps, rustDep)
685 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
686 case procMacroDepTag:
687 directProcMacroDeps = append(directProcMacroDeps, rustDep)
688 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
689 }
690
691 //Append the dependencies exportedDirs
692 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
693 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
694 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700695 }
696
697 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
698 // This can be probably be refactored by defining a common exporter interface similar to cc's
699 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
700 linkDir := linkPathFromFilePath(linkFile.Path())
701 if lib, ok := mod.compiler.(*libraryDecorator); ok {
702 lib.linkDirs = append(lib.linkDirs, linkDir)
703 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
704 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
705 }
706 }
707
Ivan Lozano52767be2019-10-18 14:49:46 -0700708 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700709
Ivan Lozano52767be2019-10-18 14:49:46 -0700710 if ccDep, ok := dep.(cc.LinkableInterface); ok {
711 //Handle C dependencies
712 if _, ok := ccDep.(*Module); !ok {
713 if ccDep.Module().Target().Os != ctx.Os() {
714 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
715 return
716 }
717 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
718 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
719 return
720 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700721 }
722
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 linkFile := ccDep.OutputFile()
724 linkPath := linkPathFromFilePath(linkFile.Path())
725 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500726 depFlag := "-l" + libName
727
Ivan Lozanoffee3342019-08-27 12:03:00 -0700728 if !linkFile.Valid() {
729 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
730 }
731
732 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700734 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500735 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500737 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400738 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739 directStaticLibDeps = append(directStaticLibDeps, ccDep)
740 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700741 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500742 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500744 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700745 directSharedLibDeps = append(directSharedLibDeps, ccDep)
746 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
747 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700748 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700749 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700750 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700751 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 }
753
754 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700755 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500757 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
759 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500760 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 }
762
763 }
764 })
765
766 var rlibDepFiles RustLibraries
767 for _, dep := range directRlibDeps {
768 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
769 }
770 var dylibDepFiles RustLibraries
771 for _, dep := range directDylibDeps {
772 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
773 }
774 var procMacroDepFiles RustLibraries
775 for _, dep := range directProcMacroDeps {
776 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
777 }
778
779 var staticLibDepFiles android.Paths
780 for _, dep := range directStaticLibDeps {
781 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
782 }
783
784 var sharedLibDepFiles android.Paths
785 for _, dep := range directSharedLibDeps {
786 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
787 }
788
789 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
790 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
791 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
792 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
793 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
794
795 // Dedup exported flags from dependencies
796 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
797 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
798
799 return depPaths
800}
801
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800802func (mod *Module) InstallInData() bool {
803 if mod.compiler == nil {
804 return false
805 }
806 return mod.compiler.inData()
807}
808
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809func linkPathFromFilePath(filepath android.Path) string {
810 return strings.Split(filepath.String(), filepath.Base())[0]
811}
Ivan Lozanod648c432020-02-06 12:05:10 -0500812
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500814 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700815 if strings.HasPrefix(libName, "lib") {
816 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 }
818 return libName
819}
Ivan Lozanod648c432020-02-06 12:05:10 -0500820
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
822 ctx := &depsContext{
823 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825
826 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700827 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900828 if cc.VersionVariantAvailable(mod) {
829 commonDepVariations = append(commonDepVariations,
830 blueprint.Variation{Mutator: "version", Variation: ""})
831 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700833 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800834 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700835 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700836 actx.AddVariationDependencies(
837 append(commonDepVariations, []blueprint.Variation{
838 {Mutator: "rust_libraries", Variation: "rlib"},
839 {Mutator: "link", Variation: ""}}...),
840 rlibDepTag, deps.Rlibs...)
841 actx.AddVariationDependencies(
842 append(commonDepVariations, []blueprint.Variation{
843 {Mutator: "rust_libraries", Variation: "dylib"},
844 {Mutator: "link", Variation: ""}}...),
845 dylibDepTag, deps.Dylibs...)
846
847 actx.AddVariationDependencies(append(commonDepVariations,
848 blueprint.Variation{Mutator: "link", Variation: "shared"}),
849 cc.SharedDepTag, deps.SharedLibs...)
850 actx.AddVariationDependencies(append(commonDepVariations,
851 blueprint.Variation{Mutator: "link", Variation: "static"}),
852 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700853
Ivan Lozanof1c84332019-09-20 11:00:37 -0700854 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700855 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700856 }
857 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700858 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700859 }
860
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700861 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700862 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863}
864
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400865func BeginMutator(ctx android.BottomUpMutatorContext) {
866 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
867 mod.beginMutator(ctx)
868 }
869}
870
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400871func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
872 ctx := &baseModuleContext{
873 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400874 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400875
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