blob: 7a98c6468a630876e394c940ab02db571d551e4e [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
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700242
243 // Paths to generated source files
244 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245}
246
247type RustLibraries []RustLibrary
248
249type RustLibrary struct {
250 Path android.Path
251 CrateName string
252}
253
254type compiler interface {
255 compilerFlags(ctx ModuleContext, flags Flags) Flags
256 compilerProps() []interface{}
257 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
258 compilerDeps(ctx DepsContext, deps Deps) Deps
259 crateName() string
260
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800261 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700262 install(ctx ModuleContext, path android.Path)
263 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400264
265 nativeCoverage() bool
266}
267
Matthew Maurerbb3add12020-06-25 09:34:12 -0700268type exportedFlagsProducer interface {
269 exportedLinkDirs() []string
270 exportedDepFlags() []string
271 exportLinkDirs(...string)
272 exportDepFlags(...string)
273}
274
275type flagExporter struct {
276 depFlags []string
277 linkDirs []string
278}
279
280func (flagExporter *flagExporter) exportedLinkDirs() []string {
281 return flagExporter.linkDirs
282}
283
284func (flagExporter *flagExporter) exportedDepFlags() []string {
285 return flagExporter.depFlags
286}
287
288func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
289 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
290}
291
292func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
293 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
294}
295
296var _ exportedFlagsProducer = (*flagExporter)(nil)
297
298func NewFlagExporter() *flagExporter {
299 return &flagExporter{
300 depFlags: []string{},
301 linkDirs: []string{},
302 }
303}
304
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400305func (mod *Module) isCoverageVariant() bool {
306 return mod.coverage.Properties.IsCoverageVariant
307}
308
309var _ cc.Coverage = (*Module)(nil)
310
311func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
312 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
313}
314
315func (mod *Module) PreventInstall() {
316 mod.Properties.PreventInstall = true
317}
318
319func (mod *Module) HideFromMake() {
320 mod.Properties.HideFromMake = true
321}
322
323func (mod *Module) MarkAsCoverageVariant(coverage bool) {
324 mod.coverage.Properties.IsCoverageVariant = coverage
325}
326
327func (mod *Module) EnableCoverageIfNeeded() {
328 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700329}
330
331func defaultsFactory() android.Module {
332 return DefaultsFactory()
333}
334
335type Defaults struct {
336 android.ModuleBase
337 android.DefaultsModuleBase
338}
339
340func DefaultsFactory(props ...interface{}) android.Module {
341 module := &Defaults{}
342
343 module.AddProperties(props...)
344 module.AddProperties(
345 &BaseProperties{},
346 &BaseCompilerProperties{},
347 &BinaryCompilerProperties{},
348 &LibraryCompilerProperties{},
349 &ProcMacroCompilerProperties{},
350 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700351 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400352 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200353 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700354 )
355
356 android.InitDefaultsModule(module)
357 return module
358}
359
360func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700361 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700362}
363
Ivan Lozano183a3212019-10-18 14:18:45 -0700364func (mod *Module) CcLibrary() bool {
365 if mod.compiler != nil {
366 if _, ok := mod.compiler.(*libraryDecorator); ok {
367 return true
368 }
369 }
370 return false
371}
372
373func (mod *Module) CcLibraryInterface() bool {
374 if mod.compiler != nil {
375 if _, ok := mod.compiler.(libraryInterface); ok {
376 return true
377 }
378 }
379 return false
380}
381
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800382func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700383 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700384 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800385 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700386 }
387 }
388 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
389}
390
391func (mod *Module) SetStatic() {
392 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700393 if library, ok := mod.compiler.(libraryInterface); ok {
394 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700395 return
396 }
397 }
398 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
399}
400
401func (mod *Module) SetShared() {
402 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700403 if library, ok := mod.compiler.(libraryInterface); ok {
404 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700405 return
406 }
407 }
408 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
409}
410
411func (mod *Module) SetBuildStubs() {
412 panic("SetBuildStubs not yet implemented for rust modules")
413}
414
415func (mod *Module) SetStubsVersions(string) {
416 panic("SetStubsVersions not yet implemented for rust modules")
417}
418
Jooyung Han03b51852020-02-26 22:45:42 +0900419func (mod *Module) StubsVersion() string {
420 panic("SetStubsVersions not yet implemented for rust modules")
421}
422
Ivan Lozano183a3212019-10-18 14:18:45 -0700423func (mod *Module) BuildStaticVariant() bool {
424 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700425 if library, ok := mod.compiler.(libraryInterface); ok {
426 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700427 }
428 }
429 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
430}
431
432func (mod *Module) BuildSharedVariant() bool {
433 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700434 if library, ok := mod.compiler.(libraryInterface); ok {
435 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700436 }
437 }
438 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
439}
440
441// Rust module deps don't have a link order (?)
442func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
443
444func (mod *Module) GetDepsInLinkOrder() []android.Path {
445 return []android.Path{}
446}
447
448func (mod *Module) GetStaticVariant() cc.LinkableInterface {
449 return nil
450}
451
452func (mod *Module) Module() android.Module {
453 return mod
454}
455
456func (mod *Module) StubsVersions() []string {
457 // For now, Rust has no stubs versions.
458 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700459 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700460 return []string{}
461 }
462 }
463 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
464}
465
466func (mod *Module) OutputFile() android.OptionalPath {
467 return mod.outputFile
468}
469
470func (mod *Module) InRecovery() bool {
471 // For now, Rust has no notion of the recovery image
472 return false
473}
474func (mod *Module) HasStaticVariant() bool {
475 if mod.GetStaticVariant() != nil {
476 return true
477 }
478 return false
479}
480
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400481func (mod *Module) CoverageFiles() android.Paths {
482 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700483 if !mod.compiler.nativeCoverage() {
484 return android.Paths{}
485 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400486 if library, ok := mod.compiler.(*libraryDecorator); ok {
487 if library.coverageFile != nil {
488 return android.Paths{library.coverageFile}
489 }
490 return android.Paths{}
491 }
492 }
493 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
494}
495
Ivan Lozano183a3212019-10-18 14:18:45 -0700496var _ cc.LinkableInterface = (*Module)(nil)
497
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498func (mod *Module) Init() android.Module {
499 mod.AddProperties(&mod.Properties)
500
501 if mod.compiler != nil {
502 mod.AddProperties(mod.compiler.compilerProps()...)
503 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400504 if mod.coverage != nil {
505 mod.AddProperties(mod.coverage.props()...)
506 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200507 if mod.clippy != nil {
508 mod.AddProperties(mod.clippy.props()...)
509 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400510
Ivan Lozanoffee3342019-08-27 12:03:00 -0700511 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
512
513 android.InitDefaultableModule(mod)
514
Ivan Lozanode252912019-09-06 15:29:52 -0700515 // Explicitly disable unsupported targets.
516 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
517 disableTargets := struct {
518 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700519 Linux_bionic struct {
520 Enabled *bool
521 }
522 }
523 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700524 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
525
526 ctx.AppendProperties(&disableTargets)
527 })
528
Ivan Lozanoffee3342019-08-27 12:03:00 -0700529 return mod
530}
531
532func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
533 return &Module{
534 hod: hod,
535 multilib: multilib,
536 }
537}
538func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
539 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200541 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542 return module
543}
544
545type ModuleContext interface {
546 android.ModuleContext
547 ModuleContextIntf
548}
549
550type BaseModuleContext interface {
551 android.BaseModuleContext
552 ModuleContextIntf
553}
554
555type DepsContext interface {
556 android.BottomUpMutatorContext
557 ModuleContextIntf
558}
559
560type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200561 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700562 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563}
564
565type depsContext struct {
566 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567}
568
569type moduleContext struct {
570 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571}
572
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200573type baseModuleContext struct {
574 android.BaseModuleContext
575}
576
577func (ctx *moduleContext) RustModule() *Module {
578 return ctx.Module().(*Module)
579}
580
581func (ctx *moduleContext) toolchain() config.Toolchain {
582 return ctx.RustModule().toolchain(ctx)
583}
584
585func (ctx *depsContext) RustModule() *Module {
586 return ctx.Module().(*Module)
587}
588
589func (ctx *depsContext) toolchain() config.Toolchain {
590 return ctx.RustModule().toolchain(ctx)
591}
592
593func (ctx *baseModuleContext) RustModule() *Module {
594 return ctx.Module().(*Module)
595}
596
597func (ctx *baseModuleContext) toolchain() config.Toolchain {
598 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400599}
600
601func (mod *Module) nativeCoverage() bool {
602 return mod.compiler != nil && mod.compiler.nativeCoverage()
603}
604
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
606 if mod.cachedToolchain == nil {
607 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
608 }
609 return mod.cachedToolchain
610}
611
612func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
613}
614
615func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
616 ctx := &moduleContext{
617 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700618 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700619
620 toolchain := mod.toolchain(ctx)
621
622 if !toolchain.Supported() {
623 // This toolchain's unsupported, there's nothing to do for this mod.
624 return
625 }
626
627 deps := mod.depsToPaths(ctx)
628 flags := Flags{
629 Toolchain: toolchain,
630 }
631
632 if mod.compiler != nil {
633 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400634 }
635 if mod.coverage != nil {
636 flags, deps = mod.coverage.flags(ctx, flags, deps)
637 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200638 if mod.clippy != nil {
639 flags, deps = mod.clippy.flags(ctx, flags, deps)
640 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400641
642 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643 outputFile := mod.compiler.compile(ctx, flags, deps)
644 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400645 if !mod.Properties.PreventInstall {
646 mod.compiler.install(ctx, mod.outputFile.Path())
647 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648 }
649}
650
651func (mod *Module) deps(ctx DepsContext) Deps {
652 deps := Deps{}
653
654 if mod.compiler != nil {
655 deps = mod.compiler.compilerDeps(ctx, deps)
656 }
657
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400658 if mod.coverage != nil {
659 deps = mod.coverage.deps(ctx, deps)
660 }
661
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
663 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700664 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
666 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
667 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
668
669 return deps
670
671}
672
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673type dependencyTag struct {
674 blueprint.BaseDependencyTag
675 name string
676 library bool
677 proc_macro bool
678}
679
680var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700681 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
682 dylibDepTag = dependencyTag{name: "dylib", library: true}
683 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
684 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700685)
686
Matthew Maurer0f003b12020-06-29 14:34:06 -0700687type autoDep struct {
688 variation string
689 depTag dependencyTag
690}
691
692var (
693 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
694 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
695)
696
697type autoDeppable interface {
698 autoDep() autoDep
699}
700
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400701func (mod *Module) begin(ctx BaseModuleContext) {
702 if mod.coverage != nil {
703 mod.coverage.begin(ctx)
704 }
705}
706
Ivan Lozanoffee3342019-08-27 12:03:00 -0700707func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
708 var depPaths PathDeps
709
710 directRlibDeps := []*Module{}
711 directDylibDeps := []*Module{}
712 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700713 directSharedLibDeps := [](cc.LinkableInterface){}
714 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715
716 ctx.VisitDirectDeps(func(dep android.Module) {
717 depName := ctx.OtherModuleName(dep)
718 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719 if rustDep, ok := dep.(*Module); ok {
720 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700721
Ivan Lozanoffee3342019-08-27 12:03:00 -0700722 linkFile := rustDep.outputFile
723 if !linkFile.Valid() {
724 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
725 }
726
727 switch depTag {
728 case dylibDepTag:
729 dylib, ok := rustDep.compiler.(libraryInterface)
730 if !ok || !dylib.dylib() {
731 ctx.ModuleErrorf("mod %q not an dylib library", depName)
732 return
733 }
734 directDylibDeps = append(directDylibDeps, rustDep)
735 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
736 case rlibDepTag:
737 rlib, ok := rustDep.compiler.(libraryInterface)
738 if !ok || !rlib.rlib() {
739 ctx.ModuleErrorf("mod %q not an rlib library", depName)
740 return
741 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400742 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 directRlibDeps = append(directRlibDeps, rustDep)
744 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
745 case procMacroDepTag:
746 directProcMacroDeps = append(directProcMacroDeps, rustDep)
747 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
748 }
749
750 //Append the dependencies exportedDirs
Matthew Maurerbb3add12020-06-25 09:34:12 -0700751 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok {
752 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754 }
755
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
757 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700758 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
759 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760 }
761 }
762
Ivan Lozano52767be2019-10-18 14:49:46 -0700763 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700764
Ivan Lozano52767be2019-10-18 14:49:46 -0700765 if ccDep, ok := dep.(cc.LinkableInterface); ok {
766 //Handle C dependencies
767 if _, ok := ccDep.(*Module); !ok {
768 if ccDep.Module().Target().Os != ctx.Os() {
769 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
770 return
771 }
772 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
773 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
774 return
775 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700776 }
777
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 linkFile := ccDep.OutputFile()
779 linkPath := linkPathFromFilePath(linkFile.Path())
780 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500781 depFlag := "-l" + libName
782
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 if !linkFile.Valid() {
784 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
785 }
786
787 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700789 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500790 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700791 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500792 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400793 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794 directStaticLibDeps = append(directStaticLibDeps, ccDep)
795 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700796 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500797 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500799 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800 directSharedLibDeps = append(directSharedLibDeps, ccDep)
801 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
802 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700803 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700804 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700805 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700806 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 }
808
809 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700810 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
811 lib.exportLinkDirs(linkPath)
812 lib.exportDepFlags(depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814 }
815 })
816
817 var rlibDepFiles RustLibraries
818 for _, dep := range directRlibDeps {
819 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
820 }
821 var dylibDepFiles RustLibraries
822 for _, dep := range directDylibDeps {
823 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
824 }
825 var procMacroDepFiles RustLibraries
826 for _, dep := range directProcMacroDeps {
827 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
828 }
829
830 var staticLibDepFiles android.Paths
831 for _, dep := range directStaticLibDeps {
832 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
833 }
834
835 var sharedLibDepFiles android.Paths
836 for _, dep := range directSharedLibDeps {
837 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
838 }
839
840 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
841 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
842 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
843 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
844 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
845
846 // Dedup exported flags from dependencies
847 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
848 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700849 depPaths.SrcDeps = android.FirstUniquePaths(depPaths.SrcDeps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850
851 return depPaths
852}
853
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800854func (mod *Module) InstallInData() bool {
855 if mod.compiler == nil {
856 return false
857 }
858 return mod.compiler.inData()
859}
860
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861func linkPathFromFilePath(filepath android.Path) string {
862 return strings.Split(filepath.String(), filepath.Base())[0]
863}
Ivan Lozanod648c432020-02-06 12:05:10 -0500864
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500866 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700867 if strings.HasPrefix(libName, "lib") {
868 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869 }
870 return libName
871}
Ivan Lozanod648c432020-02-06 12:05:10 -0500872
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
874 ctx := &depsContext{
875 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700877
878 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700879 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900880 if cc.VersionVariantAvailable(mod) {
881 commonDepVariations = append(commonDepVariations,
882 blueprint.Variation{Mutator: "version", Variation: ""})
883 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700885 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800886 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700888 actx.AddVariationDependencies(
889 append(commonDepVariations, []blueprint.Variation{
890 {Mutator: "rust_libraries", Variation: "rlib"},
891 {Mutator: "link", Variation: ""}}...),
892 rlibDepTag, deps.Rlibs...)
893 actx.AddVariationDependencies(
894 append(commonDepVariations, []blueprint.Variation{
895 {Mutator: "rust_libraries", Variation: "dylib"},
896 {Mutator: "link", Variation: ""}}...),
897 dylibDepTag, deps.Dylibs...)
898
Matthew Maurer0f003b12020-06-29 14:34:06 -0700899 if deps.Rustlibs != nil {
900 autoDep := mod.compiler.(autoDeppable).autoDep()
901 actx.AddVariationDependencies(
902 append(commonDepVariations, []blueprint.Variation{
903 {Mutator: "rust_libraries", Variation: autoDep.variation},
904 {Mutator: "link", Variation: ""}}...),
905 autoDep.depTag, deps.Rustlibs...)
906 }
907
Ivan Lozano52767be2019-10-18 14:49:46 -0700908 actx.AddVariationDependencies(append(commonDepVariations,
909 blueprint.Variation{Mutator: "link", Variation: "shared"}),
910 cc.SharedDepTag, deps.SharedLibs...)
911 actx.AddVariationDependencies(append(commonDepVariations,
912 blueprint.Variation{Mutator: "link", Variation: "static"}),
913 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700914
Ivan Lozanof1c84332019-09-20 11:00:37 -0700915 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700916 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700917 }
918 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700919 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700920 }
921
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700922 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700923 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924}
925
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400926func BeginMutator(ctx android.BottomUpMutatorContext) {
927 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
928 mod.beginMutator(ctx)
929 }
930}
931
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400932func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
933 ctx := &baseModuleContext{
934 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400935 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400936
937 mod.begin(ctx)
938}
939
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940func (mod *Module) Name() string {
941 name := mod.ModuleBase.Name()
942 if p, ok := mod.compiler.(interface {
943 Name(string) string
944 }); ok {
945 name = p.Name(name)
946 }
947 return name
948}
949
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -0700950var _ android.HostToolProvider = (*Module)(nil)
951
952func (mod *Module) HostToolPath() android.OptionalPath {
953 if !mod.Host() {
954 return android.OptionalPath{}
955 }
956 if _, ok := mod.compiler.(*binaryDecorator); ok {
957 return mod.outputFile
958 }
959 return android.OptionalPath{}
960}
961
Ivan Lozanoffee3342019-08-27 12:03:00 -0700962var Bool = proptools.Bool
963var BoolDefault = proptools.BoolDefault
964var String = proptools.String
965var StringPtr = proptools.StringPtr