blob: 89b89e20c0dcb3b1019707f762ec196156253246 [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")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040045 pctx.ImportAs("ccConfig", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070046}
47
48type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040049 GlobalRustFlags []string // Flags that apply globally to rust
50 GlobalLinkFlags []string // Flags that apply globally to linker
51 RustFlags []string // Flags that apply to rust
52 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020053 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070054 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040055 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020056 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070057}
58
59type BaseProperties struct {
60 AndroidMkRlibs []string
61 AndroidMkDylibs []string
62 AndroidMkProcMacroLibs []string
63 AndroidMkSharedLibs []string
64 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040065
66 SubName string `blueprint:"mutated"`
67 PreventInstall bool
68 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070069}
70
71type Module struct {
72 android.ModuleBase
73 android.DefaultableModuleBase
74
75 Properties BaseProperties
76
77 hod android.HostOrDeviceSupported
78 multilib android.Multilib
79
80 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040081 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020082 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070083 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040084 sourceProvider SourceProvider
Ivan Lozanoffee3342019-08-27 12:03:00 -070085 subAndroidMkOnce map[subAndroidMkProvider]bool
86 outputFile android.OptionalPath
Ivan Lozano4fef93c2020-07-08 08:39:44 -040087
88 subName string
Ivan Lozanoffee3342019-08-27 12:03:00 -070089}
90
Ivan Lozano43845682020-07-09 21:03:28 -040091func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
92 switch tag {
93 case "":
94 if mod.sourceProvider != nil {
95 return mod.sourceProvider.Srcs(), nil
96 } else {
97 if mod.outputFile.Valid() {
98 return android.Paths{mod.outputFile.Path()}, nil
99 }
100 return android.Paths{}, nil
101 }
102 default:
103 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
104 }
105}
106
Colin Cross7228ecd2019-11-18 16:00:16 -0800107var _ android.ImageInterface = (*Module)(nil)
108
109func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
110
111func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
112 return true
113}
114
Yifan Hong1b3348d2020-01-21 15:53:22 -0800115func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
116 return mod.InRamdisk()
117}
118
Colin Cross7228ecd2019-11-18 16:00:16 -0800119func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
120 return mod.InRecovery()
121}
122
123func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
124 return nil
125}
126
127func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
128}
129
Ivan Lozano52767be2019-10-18 14:49:46 -0700130func (mod *Module) BuildStubs() bool {
131 return false
132}
133
134func (mod *Module) HasStubsVariants() bool {
135 return false
136}
137
138func (mod *Module) SelectedStl() string {
139 return ""
140}
141
Ivan Lozano2b262972019-11-21 12:30:50 -0800142func (mod *Module) NonCcVariants() bool {
143 if mod.compiler != nil {
144 if library, ok := mod.compiler.(libraryInterface); ok {
145 if library.buildRlib() || library.buildDylib() {
146 return true
147 } else {
148 return false
149 }
150 }
151 }
152 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
153}
154
Ivan Lozano52767be2019-10-18 14:49:46 -0700155func (mod *Module) ApiLevel() string {
156 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
157}
158
159func (mod *Module) Static() bool {
160 if mod.compiler != nil {
161 if library, ok := mod.compiler.(libraryInterface); ok {
162 return library.static()
163 }
164 }
165 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
166}
167
168func (mod *Module) Shared() bool {
169 if mod.compiler != nil {
170 if library, ok := mod.compiler.(libraryInterface); ok {
171 return library.static()
172 }
173 }
174 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
175}
176
177func (mod *Module) Toc() android.OptionalPath {
178 if mod.compiler != nil {
179 if _, ok := mod.compiler.(libraryInterface); ok {
180 return android.OptionalPath{}
181 }
182 }
183 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
184}
185
Yifan Hong1b3348d2020-01-21 15:53:22 -0800186func (mod *Module) OnlyInRamdisk() bool {
187 return false
188}
189
Ivan Lozano52767be2019-10-18 14:49:46 -0700190func (mod *Module) OnlyInRecovery() bool {
191 return false
192}
193
Colin Crossc511bc52020-04-07 16:50:32 +0000194func (mod *Module) UseSdk() bool {
195 return false
196}
197
Ivan Lozano52767be2019-10-18 14:49:46 -0700198func (mod *Module) UseVndk() bool {
199 return false
200}
201
202func (mod *Module) MustUseVendorVariant() bool {
203 return false
204}
205
206func (mod *Module) IsVndk() bool {
207 return false
208}
209
210func (mod *Module) HasVendorVariant() bool {
211 return false
212}
213
214func (mod *Module) SdkVersion() string {
215 return ""
216}
217
Colin Crossc511bc52020-04-07 16:50:32 +0000218func (mod *Module) AlwaysSdk() bool {
219 return false
220}
221
Jiyong Park2286afd2020-06-16 21:58:53 +0900222func (mod *Module) IsSdkVariant() bool {
223 return false
224}
225
Ivan Lozano52767be2019-10-18 14:49:46 -0700226func (mod *Module) ToolchainLibrary() bool {
227 return false
228}
229
230func (mod *Module) NdkPrebuiltStl() bool {
231 return false
232}
233
234func (mod *Module) StubDecorator() bool {
235 return false
236}
237
Ivan Lozanoffee3342019-08-27 12:03:00 -0700238type Deps struct {
239 Dylibs []string
240 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700241 Rustlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700242 ProcMacros []string
243 SharedLibs []string
244 StaticLibs []string
245
246 CrtBegin, CrtEnd string
247}
248
249type PathDeps struct {
250 DyLibs RustLibraries
251 RLibs RustLibraries
252 SharedLibs android.Paths
253 StaticLibs android.Paths
254 ProcMacros RustLibraries
255 linkDirs []string
256 depFlags []string
257 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700258
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400259 coverageFiles android.Paths
260
Ivan Lozanof1c84332019-09-20 11:00:37 -0700261 CrtBegin android.OptionalPath
262 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700263
264 // Paths to generated source files
265 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266}
267
268type RustLibraries []RustLibrary
269
270type RustLibrary struct {
271 Path android.Path
272 CrateName string
273}
274
275type compiler interface {
276 compilerFlags(ctx ModuleContext, flags Flags) Flags
277 compilerProps() []interface{}
278 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
279 compilerDeps(ctx DepsContext, deps Deps) Deps
280 crateName() string
281
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800282 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283 install(ctx ModuleContext, path android.Path)
284 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400285
286 nativeCoverage() bool
287}
288
Matthew Maurerbb3add12020-06-25 09:34:12 -0700289type exportedFlagsProducer interface {
290 exportedLinkDirs() []string
291 exportedDepFlags() []string
292 exportLinkDirs(...string)
293 exportDepFlags(...string)
294}
295
296type flagExporter struct {
297 depFlags []string
298 linkDirs []string
299}
300
301func (flagExporter *flagExporter) exportedLinkDirs() []string {
302 return flagExporter.linkDirs
303}
304
305func (flagExporter *flagExporter) exportedDepFlags() []string {
306 return flagExporter.depFlags
307}
308
309func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
310 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
311}
312
313func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
314 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
315}
316
317var _ exportedFlagsProducer = (*flagExporter)(nil)
318
319func NewFlagExporter() *flagExporter {
320 return &flagExporter{
321 depFlags: []string{},
322 linkDirs: []string{},
323 }
324}
325
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400326func (mod *Module) isCoverageVariant() bool {
327 return mod.coverage.Properties.IsCoverageVariant
328}
329
330var _ cc.Coverage = (*Module)(nil)
331
332func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
333 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
334}
335
336func (mod *Module) PreventInstall() {
337 mod.Properties.PreventInstall = true
338}
339
340func (mod *Module) HideFromMake() {
341 mod.Properties.HideFromMake = true
342}
343
344func (mod *Module) MarkAsCoverageVariant(coverage bool) {
345 mod.coverage.Properties.IsCoverageVariant = coverage
346}
347
348func (mod *Module) EnableCoverageIfNeeded() {
349 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700350}
351
352func defaultsFactory() android.Module {
353 return DefaultsFactory()
354}
355
356type Defaults struct {
357 android.ModuleBase
358 android.DefaultsModuleBase
359}
360
361func DefaultsFactory(props ...interface{}) android.Module {
362 module := &Defaults{}
363
364 module.AddProperties(props...)
365 module.AddProperties(
366 &BaseProperties{},
367 &BaseCompilerProperties{},
368 &BinaryCompilerProperties{},
369 &LibraryCompilerProperties{},
370 &ProcMacroCompilerProperties{},
371 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400372 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700373 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400374 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200375 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376 )
377
378 android.InitDefaultsModule(module)
379 return module
380}
381
382func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700383 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700384}
385
Ivan Lozano183a3212019-10-18 14:18:45 -0700386func (mod *Module) CcLibrary() bool {
387 if mod.compiler != nil {
388 if _, ok := mod.compiler.(*libraryDecorator); ok {
389 return true
390 }
391 }
392 return false
393}
394
395func (mod *Module) CcLibraryInterface() bool {
396 if mod.compiler != nil {
397 if _, ok := mod.compiler.(libraryInterface); ok {
398 return true
399 }
400 }
401 return false
402}
403
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800404func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700405 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700406 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800407 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700408 }
409 }
410 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
411}
412
413func (mod *Module) SetStatic() {
414 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700415 if library, ok := mod.compiler.(libraryInterface); ok {
416 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700417 return
418 }
419 }
420 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
421}
422
423func (mod *Module) SetShared() {
424 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700425 if library, ok := mod.compiler.(libraryInterface); ok {
426 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700427 return
428 }
429 }
430 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
431}
432
433func (mod *Module) SetBuildStubs() {
434 panic("SetBuildStubs not yet implemented for rust modules")
435}
436
437func (mod *Module) SetStubsVersions(string) {
438 panic("SetStubsVersions not yet implemented for rust modules")
439}
440
Jooyung Han03b51852020-02-26 22:45:42 +0900441func (mod *Module) StubsVersion() string {
442 panic("SetStubsVersions not yet implemented for rust modules")
443}
444
Ivan Lozano183a3212019-10-18 14:18:45 -0700445func (mod *Module) BuildStaticVariant() bool {
446 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700447 if library, ok := mod.compiler.(libraryInterface); ok {
448 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700449 }
450 }
451 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
452}
453
454func (mod *Module) BuildSharedVariant() bool {
455 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700456 if library, ok := mod.compiler.(libraryInterface); ok {
457 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700458 }
459 }
460 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
461}
462
463// Rust module deps don't have a link order (?)
464func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
465
466func (mod *Module) GetDepsInLinkOrder() []android.Path {
467 return []android.Path{}
468}
469
470func (mod *Module) GetStaticVariant() cc.LinkableInterface {
471 return nil
472}
473
474func (mod *Module) Module() android.Module {
475 return mod
476}
477
478func (mod *Module) StubsVersions() []string {
479 // For now, Rust has no stubs versions.
480 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700481 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700482 return []string{}
483 }
484 }
485 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
486}
487
488func (mod *Module) OutputFile() android.OptionalPath {
489 return mod.outputFile
490}
491
492func (mod *Module) InRecovery() bool {
493 // For now, Rust has no notion of the recovery image
494 return false
495}
496func (mod *Module) HasStaticVariant() bool {
497 if mod.GetStaticVariant() != nil {
498 return true
499 }
500 return false
501}
502
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400503func (mod *Module) CoverageFiles() android.Paths {
504 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700505 if !mod.compiler.nativeCoverage() {
506 return android.Paths{}
507 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400508 if library, ok := mod.compiler.(*libraryDecorator); ok {
509 if library.coverageFile != nil {
510 return android.Paths{library.coverageFile}
511 }
512 return android.Paths{}
513 }
514 }
515 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
516}
517
Ivan Lozano183a3212019-10-18 14:18:45 -0700518var _ cc.LinkableInterface = (*Module)(nil)
519
Ivan Lozanoffee3342019-08-27 12:03:00 -0700520func (mod *Module) Init() android.Module {
521 mod.AddProperties(&mod.Properties)
522
523 if mod.compiler != nil {
524 mod.AddProperties(mod.compiler.compilerProps()...)
525 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400526 if mod.coverage != nil {
527 mod.AddProperties(mod.coverage.props()...)
528 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200529 if mod.clippy != nil {
530 mod.AddProperties(mod.clippy.props()...)
531 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400532 if mod.sourceProvider != nil {
533 mod.AddProperties(mod.sourceProvider.sourceProviderProps()...)
534 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400535
Ivan Lozanoffee3342019-08-27 12:03:00 -0700536 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
537
538 android.InitDefaultableModule(mod)
539
Ivan Lozanode252912019-09-06 15:29:52 -0700540 // Explicitly disable unsupported targets.
541 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
542 disableTargets := struct {
543 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700544 Linux_bionic struct {
545 Enabled *bool
546 }
547 }
548 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700549 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
550
551 ctx.AppendProperties(&disableTargets)
552 })
553
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554 return mod
555}
556
557func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
558 return &Module{
559 hod: hod,
560 multilib: multilib,
561 }
562}
563func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
564 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400565 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200566 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567 return module
568}
569
570type ModuleContext interface {
571 android.ModuleContext
572 ModuleContextIntf
573}
574
575type BaseModuleContext interface {
576 android.BaseModuleContext
577 ModuleContextIntf
578}
579
580type DepsContext interface {
581 android.BottomUpMutatorContext
582 ModuleContextIntf
583}
584
585type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200586 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700588}
589
590type depsContext struct {
591 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700592}
593
594type moduleContext struct {
595 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596}
597
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200598type baseModuleContext struct {
599 android.BaseModuleContext
600}
601
602func (ctx *moduleContext) RustModule() *Module {
603 return ctx.Module().(*Module)
604}
605
606func (ctx *moduleContext) toolchain() config.Toolchain {
607 return ctx.RustModule().toolchain(ctx)
608}
609
610func (ctx *depsContext) RustModule() *Module {
611 return ctx.Module().(*Module)
612}
613
614func (ctx *depsContext) toolchain() config.Toolchain {
615 return ctx.RustModule().toolchain(ctx)
616}
617
618func (ctx *baseModuleContext) RustModule() *Module {
619 return ctx.Module().(*Module)
620}
621
622func (ctx *baseModuleContext) toolchain() config.Toolchain {
623 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400624}
625
626func (mod *Module) nativeCoverage() bool {
627 return mod.compiler != nil && mod.compiler.nativeCoverage()
628}
629
Ivan Lozanoffee3342019-08-27 12:03:00 -0700630func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
631 if mod.cachedToolchain == nil {
632 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
633 }
634 return mod.cachedToolchain
635}
636
637func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
638}
639
640func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
641 ctx := &moduleContext{
642 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644
645 toolchain := mod.toolchain(ctx)
646
647 if !toolchain.Supported() {
648 // This toolchain's unsupported, there's nothing to do for this mod.
649 return
650 }
651
652 deps := mod.depsToPaths(ctx)
653 flags := Flags{
654 Toolchain: toolchain,
655 }
656
657 if mod.compiler != nil {
658 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400659 }
660 if mod.coverage != nil {
661 flags, deps = mod.coverage.flags(ctx, flags, deps)
662 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200663 if mod.clippy != nil {
664 flags, deps = mod.clippy.flags(ctx, flags, deps)
665 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400666
667 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700668 outputFile := mod.compiler.compile(ctx, flags, deps)
669 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400670 if !mod.Properties.PreventInstall {
671 mod.compiler.install(ctx, mod.outputFile.Path())
672 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400673 } else if mod.sourceProvider != nil {
674 outputFile := mod.sourceProvider.generateSource(ctx)
675 mod.outputFile = android.OptionalPathForPath(outputFile)
676 mod.subName = ctx.ModuleSubDir()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700677 }
678}
679
680func (mod *Module) deps(ctx DepsContext) Deps {
681 deps := Deps{}
682
683 if mod.compiler != nil {
684 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400685 } else if mod.sourceProvider != nil {
686 deps = mod.sourceProvider.sourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700687 }
688
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400689 if mod.coverage != nil {
690 deps = mod.coverage.deps(ctx, deps)
691 }
692
Ivan Lozanoffee3342019-08-27 12:03:00 -0700693 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
694 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700695 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700696 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
697 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
698 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
699
700 return deps
701
702}
703
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704type dependencyTag struct {
705 blueprint.BaseDependencyTag
706 name string
707 library bool
708 proc_macro bool
709}
710
711var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700712 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
713 dylibDepTag = dependencyTag{name: "dylib", library: true}
714 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
715 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700716)
717
Matthew Maurer0f003b12020-06-29 14:34:06 -0700718type autoDep struct {
719 variation string
720 depTag dependencyTag
721}
722
723var (
724 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
725 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
726)
727
728type autoDeppable interface {
729 autoDep() autoDep
730}
731
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400732func (mod *Module) begin(ctx BaseModuleContext) {
733 if mod.coverage != nil {
734 mod.coverage.begin(ctx)
735 }
736}
737
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
739 var depPaths PathDeps
740
741 directRlibDeps := []*Module{}
742 directDylibDeps := []*Module{}
743 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700744 directSharedLibDeps := [](cc.LinkableInterface){}
745 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400746 directSrcProvidersDeps := []*Module{}
747 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748
749 ctx.VisitDirectDeps(func(dep android.Module) {
750 depName := ctx.OtherModuleName(dep)
751 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 if rustDep, ok := dep.(*Module); ok {
753 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700754
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 linkFile := rustDep.outputFile
756 if !linkFile.Valid() {
757 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
758 }
759
760 switch depTag {
761 case dylibDepTag:
762 dylib, ok := rustDep.compiler.(libraryInterface)
763 if !ok || !dylib.dylib() {
764 ctx.ModuleErrorf("mod %q not an dylib library", depName)
765 return
766 }
767 directDylibDeps = append(directDylibDeps, rustDep)
768 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
769 case rlibDepTag:
770 rlib, ok := rustDep.compiler.(libraryInterface)
771 if !ok || !rlib.rlib() {
772 ctx.ModuleErrorf("mod %q not an rlib library", depName)
773 return
774 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400775 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700776 directRlibDeps = append(directRlibDeps, rustDep)
777 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
778 case procMacroDepTag:
779 directProcMacroDeps = append(directProcMacroDeps, rustDep)
780 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400781 case android.SourceDepTag:
782 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
783 // OS/Arch variant is used.
784 var helper string
785 if ctx.Host() {
786 helper = "missing 'host_supported'?"
787 } else {
788 helper = "device module defined?"
789 }
790
791 if dep.Target().Os != ctx.Os() {
792 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
793 return
794 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
795 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
796 return
797 }
798 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 }
800
801 //Append the dependencies exportedDirs
Matthew Maurerbb3add12020-06-25 09:34:12 -0700802 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok {
803 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 }
806
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
808 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700809 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
810 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811 }
812 }
813
Ivan Lozano52767be2019-10-18 14:49:46 -0700814 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700815
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400816 if srcDep, ok := dep.(android.SourceFileProducer); ok {
817 switch depTag {
818 case android.SourceDepTag:
819 // These are usually genrules which don't have per-target variants.
820 directSrcDeps = append(directSrcDeps, srcDep)
821 }
822 }
823
Ivan Lozano52767be2019-10-18 14:49:46 -0700824 if ccDep, ok := dep.(cc.LinkableInterface); ok {
825 //Handle C dependencies
826 if _, ok := ccDep.(*Module); !ok {
827 if ccDep.Module().Target().Os != ctx.Os() {
828 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
829 return
830 }
831 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
832 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
833 return
834 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700835 }
836
Ivan Lozanoffee3342019-08-27 12:03:00 -0700837 linkFile := ccDep.OutputFile()
838 linkPath := linkPathFromFilePath(linkFile.Path())
839 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500840 depFlag := "-l" + libName
841
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842 if !linkFile.Valid() {
843 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
844 }
845
846 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700847 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700848 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500849 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500851 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400852 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 directStaticLibDeps = append(directStaticLibDeps, ccDep)
854 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700855 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500856 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500858 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859 directSharedLibDeps = append(directSharedLibDeps, ccDep)
860 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
861 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700862 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700863 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700864 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700865 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 }
867
868 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700869 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
870 lib.exportLinkDirs(linkPath)
871 lib.exportDepFlags(depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 }
874 })
875
876 var rlibDepFiles RustLibraries
877 for _, dep := range directRlibDeps {
878 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
879 }
880 var dylibDepFiles RustLibraries
881 for _, dep := range directDylibDeps {
882 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
883 }
884 var procMacroDepFiles RustLibraries
885 for _, dep := range directProcMacroDeps {
886 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
887 }
888
889 var staticLibDepFiles android.Paths
890 for _, dep := range directStaticLibDeps {
891 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
892 }
893
894 var sharedLibDepFiles android.Paths
895 for _, dep := range directSharedLibDeps {
896 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
897 }
898
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400899 var srcProviderDepFiles android.Paths
900 for _, dep := range directSrcProvidersDeps {
901 srcs, _ := dep.OutputFiles("")
902 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
903 }
904 for _, dep := range directSrcDeps {
905 srcs := dep.Srcs()
906 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
907 }
908
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
910 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
911 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
912 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
913 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400914 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915
916 // Dedup exported flags from dependencies
917 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
918 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
919
920 return depPaths
921}
922
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800923func (mod *Module) InstallInData() bool {
924 if mod.compiler == nil {
925 return false
926 }
927 return mod.compiler.inData()
928}
929
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930func linkPathFromFilePath(filepath android.Path) string {
931 return strings.Split(filepath.String(), filepath.Base())[0]
932}
Ivan Lozanod648c432020-02-06 12:05:10 -0500933
Ivan Lozanoffee3342019-08-27 12:03:00 -0700934func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500935 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700936 if strings.HasPrefix(libName, "lib") {
937 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938 }
939 return libName
940}
Ivan Lozanod648c432020-02-06 12:05:10 -0500941
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
943 ctx := &depsContext{
944 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700946
947 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700948 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900949 if cc.VersionVariantAvailable(mod) {
950 commonDepVariations = append(commonDepVariations,
951 blueprint.Variation{Mutator: "version", Variation: ""})
952 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700954 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800955 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700957 actx.AddVariationDependencies(
958 append(commonDepVariations, []blueprint.Variation{
959 {Mutator: "rust_libraries", Variation: "rlib"},
960 {Mutator: "link", Variation: ""}}...),
961 rlibDepTag, deps.Rlibs...)
962 actx.AddVariationDependencies(
963 append(commonDepVariations, []blueprint.Variation{
964 {Mutator: "rust_libraries", Variation: "dylib"},
965 {Mutator: "link", Variation: ""}}...),
966 dylibDepTag, deps.Dylibs...)
967
Matthew Maurer0f003b12020-06-29 14:34:06 -0700968 if deps.Rustlibs != nil {
969 autoDep := mod.compiler.(autoDeppable).autoDep()
970 actx.AddVariationDependencies(
971 append(commonDepVariations, []blueprint.Variation{
972 {Mutator: "rust_libraries", Variation: autoDep.variation},
973 {Mutator: "link", Variation: ""}}...),
974 autoDep.depTag, deps.Rustlibs...)
975 }
976
Ivan Lozano52767be2019-10-18 14:49:46 -0700977 actx.AddVariationDependencies(append(commonDepVariations,
978 blueprint.Variation{Mutator: "link", Variation: "shared"}),
979 cc.SharedDepTag, deps.SharedLibs...)
980 actx.AddVariationDependencies(append(commonDepVariations,
981 blueprint.Variation{Mutator: "link", Variation: "static"}),
982 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700983
Ivan Lozanof1c84332019-09-20 11:00:37 -0700984 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700985 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700986 }
987 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700988 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700989 }
990
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700991 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700992 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700993}
994
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400995func BeginMutator(ctx android.BottomUpMutatorContext) {
996 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
997 mod.beginMutator(ctx)
998 }
999}
1000
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001001func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1002 ctx := &baseModuleContext{
1003 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001004 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001005
1006 mod.begin(ctx)
1007}
1008
Ivan Lozanoffee3342019-08-27 12:03:00 -07001009func (mod *Module) Name() string {
1010 name := mod.ModuleBase.Name()
1011 if p, ok := mod.compiler.(interface {
1012 Name(string) string
1013 }); ok {
1014 name = p.Name(name)
1015 }
1016 return name
1017}
1018
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001019var _ android.HostToolProvider = (*Module)(nil)
1020
1021func (mod *Module) HostToolPath() android.OptionalPath {
1022 if !mod.Host() {
1023 return android.OptionalPath{}
1024 }
1025 if _, ok := mod.compiler.(*binaryDecorator); ok {
1026 return mod.outputFile
1027 }
1028 return android.OptionalPath{}
1029}
1030
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031var Bool = proptools.Bool
1032var BoolDefault = proptools.BoolDefault
1033var String = proptools.String
1034var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001035
1036var _ android.OutputFileProducer = (*Module)(nil)