blob: 72301a718ef934b65ea44ff2acdbe7f871e7a699 [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
Matthew Maurer0f003b12020-06-29 14:34:06 -0700220 Rustlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221 ProcMacros []string
222 SharedLibs []string
223 StaticLibs []string
224
225 CrtBegin, CrtEnd string
226}
227
228type PathDeps struct {
229 DyLibs RustLibraries
230 RLibs RustLibraries
231 SharedLibs android.Paths
232 StaticLibs android.Paths
233 ProcMacros RustLibraries
234 linkDirs []string
235 depFlags []string
236 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700237
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400238 coverageFiles android.Paths
239
Ivan Lozanof1c84332019-09-20 11:00:37 -0700240 CrtBegin android.OptionalPath
241 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700242}
243
244type RustLibraries []RustLibrary
245
246type RustLibrary struct {
247 Path android.Path
248 CrateName string
249}
250
251type compiler interface {
252 compilerFlags(ctx ModuleContext, flags Flags) Flags
253 compilerProps() []interface{}
254 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
255 compilerDeps(ctx DepsContext, deps Deps) Deps
256 crateName() string
257
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800258 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700259 install(ctx ModuleContext, path android.Path)
260 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400261
262 nativeCoverage() bool
263}
264
Matthew Maurerbb3add12020-06-25 09:34:12 -0700265type exportedFlagsProducer interface {
266 exportedLinkDirs() []string
267 exportedDepFlags() []string
268 exportLinkDirs(...string)
269 exportDepFlags(...string)
270}
271
272type flagExporter struct {
273 depFlags []string
274 linkDirs []string
275}
276
277func (flagExporter *flagExporter) exportedLinkDirs() []string {
278 return flagExporter.linkDirs
279}
280
281func (flagExporter *flagExporter) exportedDepFlags() []string {
282 return flagExporter.depFlags
283}
284
285func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
286 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
287}
288
289func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
290 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
291}
292
293var _ exportedFlagsProducer = (*flagExporter)(nil)
294
295func NewFlagExporter() *flagExporter {
296 return &flagExporter{
297 depFlags: []string{},
298 linkDirs: []string{},
299 }
300}
301
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400302func (mod *Module) isCoverageVariant() bool {
303 return mod.coverage.Properties.IsCoverageVariant
304}
305
306var _ cc.Coverage = (*Module)(nil)
307
308func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
309 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
310}
311
312func (mod *Module) PreventInstall() {
313 mod.Properties.PreventInstall = true
314}
315
316func (mod *Module) HideFromMake() {
317 mod.Properties.HideFromMake = true
318}
319
320func (mod *Module) MarkAsCoverageVariant(coverage bool) {
321 mod.coverage.Properties.IsCoverageVariant = coverage
322}
323
324func (mod *Module) EnableCoverageIfNeeded() {
325 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700326}
327
328func defaultsFactory() android.Module {
329 return DefaultsFactory()
330}
331
332type Defaults struct {
333 android.ModuleBase
334 android.DefaultsModuleBase
335}
336
337func DefaultsFactory(props ...interface{}) android.Module {
338 module := &Defaults{}
339
340 module.AddProperties(props...)
341 module.AddProperties(
342 &BaseProperties{},
343 &BaseCompilerProperties{},
344 &BinaryCompilerProperties{},
345 &LibraryCompilerProperties{},
346 &ProcMacroCompilerProperties{},
347 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700348 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400349 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200350 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700351 )
352
353 android.InitDefaultsModule(module)
354 return module
355}
356
357func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700358 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700359}
360
Ivan Lozano183a3212019-10-18 14:18:45 -0700361func (mod *Module) CcLibrary() bool {
362 if mod.compiler != nil {
363 if _, ok := mod.compiler.(*libraryDecorator); ok {
364 return true
365 }
366 }
367 return false
368}
369
370func (mod *Module) CcLibraryInterface() bool {
371 if mod.compiler != nil {
372 if _, ok := mod.compiler.(libraryInterface); ok {
373 return true
374 }
375 }
376 return false
377}
378
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800379func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700380 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700381 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800382 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700383 }
384 }
385 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
386}
387
388func (mod *Module) SetStatic() {
389 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700390 if library, ok := mod.compiler.(libraryInterface); ok {
391 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700392 return
393 }
394 }
395 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
396}
397
398func (mod *Module) SetShared() {
399 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700400 if library, ok := mod.compiler.(libraryInterface); ok {
401 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700402 return
403 }
404 }
405 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
406}
407
408func (mod *Module) SetBuildStubs() {
409 panic("SetBuildStubs not yet implemented for rust modules")
410}
411
412func (mod *Module) SetStubsVersions(string) {
413 panic("SetStubsVersions not yet implemented for rust modules")
414}
415
Jooyung Han03b51852020-02-26 22:45:42 +0900416func (mod *Module) StubsVersion() string {
417 panic("SetStubsVersions not yet implemented for rust modules")
418}
419
Ivan Lozano183a3212019-10-18 14:18:45 -0700420func (mod *Module) BuildStaticVariant() bool {
421 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700422 if library, ok := mod.compiler.(libraryInterface); ok {
423 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700424 }
425 }
426 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
427}
428
429func (mod *Module) BuildSharedVariant() bool {
430 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700431 if library, ok := mod.compiler.(libraryInterface); ok {
432 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700433 }
434 }
435 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
436}
437
438// Rust module deps don't have a link order (?)
439func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
440
441func (mod *Module) GetDepsInLinkOrder() []android.Path {
442 return []android.Path{}
443}
444
445func (mod *Module) GetStaticVariant() cc.LinkableInterface {
446 return nil
447}
448
449func (mod *Module) Module() android.Module {
450 return mod
451}
452
453func (mod *Module) StubsVersions() []string {
454 // For now, Rust has no stubs versions.
455 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700456 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700457 return []string{}
458 }
459 }
460 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
461}
462
463func (mod *Module) OutputFile() android.OptionalPath {
464 return mod.outputFile
465}
466
467func (mod *Module) InRecovery() bool {
468 // For now, Rust has no notion of the recovery image
469 return false
470}
471func (mod *Module) HasStaticVariant() bool {
472 if mod.GetStaticVariant() != nil {
473 return true
474 }
475 return false
476}
477
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400478func (mod *Module) CoverageFiles() android.Paths {
479 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700480 if !mod.compiler.nativeCoverage() {
481 return android.Paths{}
482 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400483 if library, ok := mod.compiler.(*libraryDecorator); ok {
484 if library.coverageFile != nil {
485 return android.Paths{library.coverageFile}
486 }
487 return android.Paths{}
488 }
489 }
490 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
491}
492
Ivan Lozano183a3212019-10-18 14:18:45 -0700493var _ cc.LinkableInterface = (*Module)(nil)
494
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495func (mod *Module) Init() android.Module {
496 mod.AddProperties(&mod.Properties)
497
498 if mod.compiler != nil {
499 mod.AddProperties(mod.compiler.compilerProps()...)
500 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400501 if mod.coverage != nil {
502 mod.AddProperties(mod.coverage.props()...)
503 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200504 if mod.clippy != nil {
505 mod.AddProperties(mod.clippy.props()...)
506 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400507
Ivan Lozanoffee3342019-08-27 12:03:00 -0700508 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
509
510 android.InitDefaultableModule(mod)
511
Ivan Lozanode252912019-09-06 15:29:52 -0700512 // Explicitly disable unsupported targets.
513 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
514 disableTargets := struct {
515 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700516 Linux_bionic struct {
517 Enabled *bool
518 }
519 }
520 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700521 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
522
523 ctx.AppendProperties(&disableTargets)
524 })
525
Ivan Lozanoffee3342019-08-27 12:03:00 -0700526 return mod
527}
528
529func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
530 return &Module{
531 hod: hod,
532 multilib: multilib,
533 }
534}
535func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
536 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400537 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200538 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539 return module
540}
541
542type ModuleContext interface {
543 android.ModuleContext
544 ModuleContextIntf
545}
546
547type BaseModuleContext interface {
548 android.BaseModuleContext
549 ModuleContextIntf
550}
551
552type DepsContext interface {
553 android.BottomUpMutatorContext
554 ModuleContextIntf
555}
556
557type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200558 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700559 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560}
561
562type depsContext struct {
563 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700564}
565
566type moduleContext struct {
567 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568}
569
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200570type baseModuleContext struct {
571 android.BaseModuleContext
572}
573
574func (ctx *moduleContext) RustModule() *Module {
575 return ctx.Module().(*Module)
576}
577
578func (ctx *moduleContext) toolchain() config.Toolchain {
579 return ctx.RustModule().toolchain(ctx)
580}
581
582func (ctx *depsContext) RustModule() *Module {
583 return ctx.Module().(*Module)
584}
585
586func (ctx *depsContext) toolchain() config.Toolchain {
587 return ctx.RustModule().toolchain(ctx)
588}
589
590func (ctx *baseModuleContext) RustModule() *Module {
591 return ctx.Module().(*Module)
592}
593
594func (ctx *baseModuleContext) toolchain() config.Toolchain {
595 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400596}
597
598func (mod *Module) nativeCoverage() bool {
599 return mod.compiler != nil && mod.compiler.nativeCoverage()
600}
601
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
603 if mod.cachedToolchain == nil {
604 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
605 }
606 return mod.cachedToolchain
607}
608
609func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
610}
611
612func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
613 ctx := &moduleContext{
614 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700616
617 toolchain := mod.toolchain(ctx)
618
619 if !toolchain.Supported() {
620 // This toolchain's unsupported, there's nothing to do for this mod.
621 return
622 }
623
624 deps := mod.depsToPaths(ctx)
625 flags := Flags{
626 Toolchain: toolchain,
627 }
628
629 if mod.compiler != nil {
630 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400631 }
632 if mod.coverage != nil {
633 flags, deps = mod.coverage.flags(ctx, flags, deps)
634 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200635 if mod.clippy != nil {
636 flags, deps = mod.clippy.flags(ctx, flags, deps)
637 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400638
639 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700640 outputFile := mod.compiler.compile(ctx, flags, deps)
641 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400642 if !mod.Properties.PreventInstall {
643 mod.compiler.install(ctx, mod.outputFile.Path())
644 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645 }
646}
647
648func (mod *Module) deps(ctx DepsContext) Deps {
649 deps := Deps{}
650
651 if mod.compiler != nil {
652 deps = mod.compiler.compilerDeps(ctx, deps)
653 }
654
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400655 if mod.coverage != nil {
656 deps = mod.coverage.deps(ctx, deps)
657 }
658
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
660 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700661 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
663 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
664 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
665
666 return deps
667
668}
669
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670type dependencyTag struct {
671 blueprint.BaseDependencyTag
672 name string
673 library bool
674 proc_macro bool
675}
676
677var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700678 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
679 dylibDepTag = dependencyTag{name: "dylib", library: true}
680 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
681 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682)
683
Matthew Maurer0f003b12020-06-29 14:34:06 -0700684type autoDep struct {
685 variation string
686 depTag dependencyTag
687}
688
689var (
690 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
691 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
692)
693
694type autoDeppable interface {
695 autoDep() autoDep
696}
697
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400698func (mod *Module) begin(ctx BaseModuleContext) {
699 if mod.coverage != nil {
700 mod.coverage.begin(ctx)
701 }
702}
703
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
705 var depPaths PathDeps
706
707 directRlibDeps := []*Module{}
708 directDylibDeps := []*Module{}
709 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700710 directSharedLibDeps := [](cc.LinkableInterface){}
711 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700712
713 ctx.VisitDirectDeps(func(dep android.Module) {
714 depName := ctx.OtherModuleName(dep)
715 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700716 if rustDep, ok := dep.(*Module); ok {
717 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700718
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719 linkFile := rustDep.outputFile
720 if !linkFile.Valid() {
721 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
722 }
723
724 switch depTag {
725 case dylibDepTag:
726 dylib, ok := rustDep.compiler.(libraryInterface)
727 if !ok || !dylib.dylib() {
728 ctx.ModuleErrorf("mod %q not an dylib library", depName)
729 return
730 }
731 directDylibDeps = append(directDylibDeps, rustDep)
732 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
733 case rlibDepTag:
734 rlib, ok := rustDep.compiler.(libraryInterface)
735 if !ok || !rlib.rlib() {
736 ctx.ModuleErrorf("mod %q not an rlib library", depName)
737 return
738 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400739 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740 directRlibDeps = append(directRlibDeps, rustDep)
741 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
742 case procMacroDepTag:
743 directProcMacroDeps = append(directProcMacroDeps, rustDep)
744 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
745 }
746
747 //Append the dependencies exportedDirs
Matthew Maurerbb3add12020-06-25 09:34:12 -0700748 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok {
749 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700750 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751 }
752
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
754 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700755 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
756 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 }
758 }
759
Ivan Lozano52767be2019-10-18 14:49:46 -0700760 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700761
Ivan Lozano52767be2019-10-18 14:49:46 -0700762 if ccDep, ok := dep.(cc.LinkableInterface); ok {
763 //Handle C dependencies
764 if _, ok := ccDep.(*Module); !ok {
765 if ccDep.Module().Target().Os != ctx.Os() {
766 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
767 return
768 }
769 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
770 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
771 return
772 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700773 }
774
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775 linkFile := ccDep.OutputFile()
776 linkPath := linkPathFromFilePath(linkFile.Path())
777 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500778 depFlag := "-l" + libName
779
Ivan Lozanoffee3342019-08-27 12:03:00 -0700780 if !linkFile.Valid() {
781 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
782 }
783
784 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700786 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500787 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500789 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400790 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700791 directStaticLibDeps = append(directStaticLibDeps, ccDep)
792 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700793 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500794 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500796 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797 directSharedLibDeps = append(directSharedLibDeps, ccDep)
798 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
799 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700800 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700801 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700802 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700803 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 }
805
806 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700807 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
808 lib.exportLinkDirs(linkPath)
809 lib.exportDepFlags(depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811 }
812 })
813
814 var rlibDepFiles RustLibraries
815 for _, dep := range directRlibDeps {
816 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
817 }
818 var dylibDepFiles RustLibraries
819 for _, dep := range directDylibDeps {
820 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
821 }
822 var procMacroDepFiles RustLibraries
823 for _, dep := range directProcMacroDeps {
824 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
825 }
826
827 var staticLibDepFiles android.Paths
828 for _, dep := range directStaticLibDeps {
829 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
830 }
831
832 var sharedLibDepFiles android.Paths
833 for _, dep := range directSharedLibDeps {
834 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
835 }
836
837 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
838 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
839 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
840 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
841 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
842
843 // Dedup exported flags from dependencies
844 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
845 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
846
847 return depPaths
848}
849
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800850func (mod *Module) InstallInData() bool {
851 if mod.compiler == nil {
852 return false
853 }
854 return mod.compiler.inData()
855}
856
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857func linkPathFromFilePath(filepath android.Path) string {
858 return strings.Split(filepath.String(), filepath.Base())[0]
859}
Ivan Lozanod648c432020-02-06 12:05:10 -0500860
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500862 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700863 if strings.HasPrefix(libName, "lib") {
864 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865 }
866 return libName
867}
Ivan Lozanod648c432020-02-06 12:05:10 -0500868
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
870 ctx := &depsContext{
871 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873
874 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700875 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900876 if cc.VersionVariantAvailable(mod) {
877 commonDepVariations = append(commonDepVariations,
878 blueprint.Variation{Mutator: "version", Variation: ""})
879 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700881 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800882 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700884 actx.AddVariationDependencies(
885 append(commonDepVariations, []blueprint.Variation{
886 {Mutator: "rust_libraries", Variation: "rlib"},
887 {Mutator: "link", Variation: ""}}...),
888 rlibDepTag, deps.Rlibs...)
889 actx.AddVariationDependencies(
890 append(commonDepVariations, []blueprint.Variation{
891 {Mutator: "rust_libraries", Variation: "dylib"},
892 {Mutator: "link", Variation: ""}}...),
893 dylibDepTag, deps.Dylibs...)
894
Matthew Maurer0f003b12020-06-29 14:34:06 -0700895 if deps.Rustlibs != nil {
896 autoDep := mod.compiler.(autoDeppable).autoDep()
897 actx.AddVariationDependencies(
898 append(commonDepVariations, []blueprint.Variation{
899 {Mutator: "rust_libraries", Variation: autoDep.variation},
900 {Mutator: "link", Variation: ""}}...),
901 autoDep.depTag, deps.Rustlibs...)
902 }
903
Ivan Lozano52767be2019-10-18 14:49:46 -0700904 actx.AddVariationDependencies(append(commonDepVariations,
905 blueprint.Variation{Mutator: "link", Variation: "shared"}),
906 cc.SharedDepTag, deps.SharedLibs...)
907 actx.AddVariationDependencies(append(commonDepVariations,
908 blueprint.Variation{Mutator: "link", Variation: "static"}),
909 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700910
Ivan Lozanof1c84332019-09-20 11:00:37 -0700911 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700912 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700913 }
914 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700915 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700916 }
917
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700918 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700919 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920}
921
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400922func BeginMutator(ctx android.BottomUpMutatorContext) {
923 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
924 mod.beginMutator(ctx)
925 }
926}
927
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400928func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
929 ctx := &baseModuleContext{
930 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400931 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400932
933 mod.begin(ctx)
934}
935
Ivan Lozanoffee3342019-08-27 12:03:00 -0700936func (mod *Module) Name() string {
937 name := mod.ModuleBase.Name()
938 if p, ok := mod.compiler.(interface {
939 Name(string) string
940 }); ok {
941 name = p.Name(name)
942 }
943 return name
944}
945
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -0700946var _ android.HostToolProvider = (*Module)(nil)
947
948func (mod *Module) HostToolPath() android.OptionalPath {
949 if !mod.Host() {
950 return android.OptionalPath{}
951 }
952 if _, ok := mod.compiler.(*binaryDecorator); ok {
953 return mod.outputFile
954 }
955 return android.OptionalPath{}
956}
957
Ivan Lozanoffee3342019-08-27 12:03:00 -0700958var Bool = proptools.Bool
959var BoolDefault = proptools.BoolDefault
960var String = proptools.String
961var StringPtr = proptools.StringPtr