blob: 8ebd39dccf2d133d47f56b043e772c1c3913c6e3 [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 Lozano45901ed2020-07-24 16:05:01 -0400259 // Used by bindgen modules which call clang
260 depClangFlags []string
261 depIncludePaths android.Paths
262 depSystemIncludePaths android.Paths
263
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400264 coverageFiles android.Paths
265
Ivan Lozanof1c84332019-09-20 11:00:37 -0700266 CrtBegin android.OptionalPath
267 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700268
269 // Paths to generated source files
270 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700271}
272
273type RustLibraries []RustLibrary
274
275type RustLibrary struct {
276 Path android.Path
277 CrateName string
278}
279
280type compiler interface {
281 compilerFlags(ctx ModuleContext, flags Flags) Flags
282 compilerProps() []interface{}
283 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
284 compilerDeps(ctx DepsContext, deps Deps) Deps
285 crateName() string
286
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800287 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700288 install(ctx ModuleContext, path android.Path)
289 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400290
291 nativeCoverage() bool
292}
293
Matthew Maurerbb3add12020-06-25 09:34:12 -0700294type exportedFlagsProducer interface {
295 exportedLinkDirs() []string
296 exportedDepFlags() []string
297 exportLinkDirs(...string)
298 exportDepFlags(...string)
299}
300
301type flagExporter struct {
302 depFlags []string
303 linkDirs []string
304}
305
306func (flagExporter *flagExporter) exportedLinkDirs() []string {
307 return flagExporter.linkDirs
308}
309
310func (flagExporter *flagExporter) exportedDepFlags() []string {
311 return flagExporter.depFlags
312}
313
314func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
315 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
316}
317
318func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
319 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
320}
321
322var _ exportedFlagsProducer = (*flagExporter)(nil)
323
324func NewFlagExporter() *flagExporter {
325 return &flagExporter{
326 depFlags: []string{},
327 linkDirs: []string{},
328 }
329}
330
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400331func (mod *Module) isCoverageVariant() bool {
332 return mod.coverage.Properties.IsCoverageVariant
333}
334
335var _ cc.Coverage = (*Module)(nil)
336
337func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
338 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
339}
340
341func (mod *Module) PreventInstall() {
342 mod.Properties.PreventInstall = true
343}
344
345func (mod *Module) HideFromMake() {
346 mod.Properties.HideFromMake = true
347}
348
349func (mod *Module) MarkAsCoverageVariant(coverage bool) {
350 mod.coverage.Properties.IsCoverageVariant = coverage
351}
352
353func (mod *Module) EnableCoverageIfNeeded() {
354 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700355}
356
357func defaultsFactory() android.Module {
358 return DefaultsFactory()
359}
360
361type Defaults struct {
362 android.ModuleBase
363 android.DefaultsModuleBase
364}
365
366func DefaultsFactory(props ...interface{}) android.Module {
367 module := &Defaults{}
368
369 module.AddProperties(props...)
370 module.AddProperties(
371 &BaseProperties{},
372 &BaseCompilerProperties{},
373 &BinaryCompilerProperties{},
374 &LibraryCompilerProperties{},
375 &ProcMacroCompilerProperties{},
376 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400377 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700378 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400379 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200380 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700381 )
382
383 android.InitDefaultsModule(module)
384 return module
385}
386
387func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700388 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700389}
390
Ivan Lozano183a3212019-10-18 14:18:45 -0700391func (mod *Module) CcLibrary() bool {
392 if mod.compiler != nil {
393 if _, ok := mod.compiler.(*libraryDecorator); ok {
394 return true
395 }
396 }
397 return false
398}
399
400func (mod *Module) CcLibraryInterface() bool {
401 if mod.compiler != nil {
402 if _, ok := mod.compiler.(libraryInterface); ok {
403 return true
404 }
405 }
406 return false
407}
408
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800409func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700410 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700411 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800412 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700413 }
414 }
415 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
416}
417
418func (mod *Module) SetStatic() {
419 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700420 if library, ok := mod.compiler.(libraryInterface); ok {
421 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700422 return
423 }
424 }
425 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
426}
427
428func (mod *Module) SetShared() {
429 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700430 if library, ok := mod.compiler.(libraryInterface); ok {
431 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700432 return
433 }
434 }
435 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
436}
437
438func (mod *Module) SetBuildStubs() {
439 panic("SetBuildStubs not yet implemented for rust modules")
440}
441
442func (mod *Module) SetStubsVersions(string) {
443 panic("SetStubsVersions not yet implemented for rust modules")
444}
445
Jooyung Han03b51852020-02-26 22:45:42 +0900446func (mod *Module) StubsVersion() string {
447 panic("SetStubsVersions not yet implemented for rust modules")
448}
449
Ivan Lozano183a3212019-10-18 14:18:45 -0700450func (mod *Module) BuildStaticVariant() bool {
451 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700452 if library, ok := mod.compiler.(libraryInterface); ok {
453 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700454 }
455 }
456 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
457}
458
459func (mod *Module) BuildSharedVariant() bool {
460 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700461 if library, ok := mod.compiler.(libraryInterface); ok {
462 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700463 }
464 }
465 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
466}
467
468// Rust module deps don't have a link order (?)
469func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
470
471func (mod *Module) GetDepsInLinkOrder() []android.Path {
472 return []android.Path{}
473}
474
475func (mod *Module) GetStaticVariant() cc.LinkableInterface {
476 return nil
477}
478
479func (mod *Module) Module() android.Module {
480 return mod
481}
482
483func (mod *Module) StubsVersions() []string {
484 // For now, Rust has no stubs versions.
485 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700486 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700487 return []string{}
488 }
489 }
490 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
491}
492
493func (mod *Module) OutputFile() android.OptionalPath {
494 return mod.outputFile
495}
496
497func (mod *Module) InRecovery() bool {
498 // For now, Rust has no notion of the recovery image
499 return false
500}
501func (mod *Module) HasStaticVariant() bool {
502 if mod.GetStaticVariant() != nil {
503 return true
504 }
505 return false
506}
507
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400508func (mod *Module) CoverageFiles() android.Paths {
509 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700510 if !mod.compiler.nativeCoverage() {
511 return android.Paths{}
512 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513 if library, ok := mod.compiler.(*libraryDecorator); ok {
514 if library.coverageFile != nil {
515 return android.Paths{library.coverageFile}
516 }
517 return android.Paths{}
518 }
519 }
520 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
521}
522
Ivan Lozano183a3212019-10-18 14:18:45 -0700523var _ cc.LinkableInterface = (*Module)(nil)
524
Ivan Lozanoffee3342019-08-27 12:03:00 -0700525func (mod *Module) Init() android.Module {
526 mod.AddProperties(&mod.Properties)
527
528 if mod.compiler != nil {
529 mod.AddProperties(mod.compiler.compilerProps()...)
530 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400531 if mod.coverage != nil {
532 mod.AddProperties(mod.coverage.props()...)
533 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200534 if mod.clippy != nil {
535 mod.AddProperties(mod.clippy.props()...)
536 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400537 if mod.sourceProvider != nil {
538 mod.AddProperties(mod.sourceProvider.sourceProviderProps()...)
539 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
542
543 android.InitDefaultableModule(mod)
544
Ivan Lozanode252912019-09-06 15:29:52 -0700545 // Explicitly disable unsupported targets.
546 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
547 disableTargets := struct {
548 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700549 Linux_bionic struct {
550 Enabled *bool
551 }
552 }
553 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700554 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
555
556 ctx.AppendProperties(&disableTargets)
557 })
558
Ivan Lozanoffee3342019-08-27 12:03:00 -0700559 return mod
560}
561
562func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
563 return &Module{
564 hod: hod,
565 multilib: multilib,
566 }
567}
568func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
569 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400570 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200571 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572 return module
573}
574
575type ModuleContext interface {
576 android.ModuleContext
577 ModuleContextIntf
578}
579
580type BaseModuleContext interface {
581 android.BaseModuleContext
582 ModuleContextIntf
583}
584
585type DepsContext interface {
586 android.BottomUpMutatorContext
587 ModuleContextIntf
588}
589
590type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200591 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700592 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593}
594
595type depsContext struct {
596 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700597}
598
599type moduleContext struct {
600 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700601}
602
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200603type baseModuleContext struct {
604 android.BaseModuleContext
605}
606
607func (ctx *moduleContext) RustModule() *Module {
608 return ctx.Module().(*Module)
609}
610
611func (ctx *moduleContext) toolchain() config.Toolchain {
612 return ctx.RustModule().toolchain(ctx)
613}
614
615func (ctx *depsContext) RustModule() *Module {
616 return ctx.Module().(*Module)
617}
618
619func (ctx *depsContext) toolchain() config.Toolchain {
620 return ctx.RustModule().toolchain(ctx)
621}
622
623func (ctx *baseModuleContext) RustModule() *Module {
624 return ctx.Module().(*Module)
625}
626
627func (ctx *baseModuleContext) toolchain() config.Toolchain {
628 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400629}
630
631func (mod *Module) nativeCoverage() bool {
632 return mod.compiler != nil && mod.compiler.nativeCoverage()
633}
634
Ivan Lozanoffee3342019-08-27 12:03:00 -0700635func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
636 if mod.cachedToolchain == nil {
637 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
638 }
639 return mod.cachedToolchain
640}
641
642func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
643}
644
645func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
646 ctx := &moduleContext{
647 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649
650 toolchain := mod.toolchain(ctx)
651
652 if !toolchain.Supported() {
653 // This toolchain's unsupported, there's nothing to do for this mod.
654 return
655 }
656
657 deps := mod.depsToPaths(ctx)
658 flags := Flags{
659 Toolchain: toolchain,
660 }
661
662 if mod.compiler != nil {
663 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400664 }
665 if mod.coverage != nil {
666 flags, deps = mod.coverage.flags(ctx, flags, deps)
667 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200668 if mod.clippy != nil {
669 flags, deps = mod.clippy.flags(ctx, flags, deps)
670 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400671
672 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673 outputFile := mod.compiler.compile(ctx, flags, deps)
674 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400675 if !mod.Properties.PreventInstall {
676 mod.compiler.install(ctx, mod.outputFile.Path())
677 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400678 } else if mod.sourceProvider != nil {
Ivan Lozano45901ed2020-07-24 16:05:01 -0400679 outputFile := mod.sourceProvider.generateSource(ctx, deps)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400680 mod.outputFile = android.OptionalPathForPath(outputFile)
681 mod.subName = ctx.ModuleSubDir()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682 }
683}
684
685func (mod *Module) deps(ctx DepsContext) Deps {
686 deps := Deps{}
687
688 if mod.compiler != nil {
689 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400690 } else if mod.sourceProvider != nil {
691 deps = mod.sourceProvider.sourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700692 }
693
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400694 if mod.coverage != nil {
695 deps = mod.coverage.deps(ctx, deps)
696 }
697
Ivan Lozanoffee3342019-08-27 12:03:00 -0700698 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
699 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700700 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700701 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
702 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
703 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
704
705 return deps
706
707}
708
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709type dependencyTag struct {
710 blueprint.BaseDependencyTag
711 name string
712 library bool
713 proc_macro bool
714}
715
716var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700717 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
718 dylibDepTag = dependencyTag{name: "dylib", library: true}
719 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
720 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721)
722
Matthew Maurer0f003b12020-06-29 14:34:06 -0700723type autoDep struct {
724 variation string
725 depTag dependencyTag
726}
727
728var (
729 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
730 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
731)
732
733type autoDeppable interface {
734 autoDep() autoDep
735}
736
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400737func (mod *Module) begin(ctx BaseModuleContext) {
738 if mod.coverage != nil {
739 mod.coverage.begin(ctx)
740 }
741}
742
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
744 var depPaths PathDeps
745
746 directRlibDeps := []*Module{}
747 directDylibDeps := []*Module{}
748 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700749 directSharedLibDeps := [](cc.LinkableInterface){}
750 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751
752 ctx.VisitDirectDeps(func(dep android.Module) {
753 depName := ctx.OtherModuleName(dep)
754 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 if rustDep, ok := dep.(*Module); ok {
756 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700757
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758 linkFile := rustDep.outputFile
759 if !linkFile.Valid() {
760 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
761 }
762
763 switch depTag {
764 case dylibDepTag:
765 dylib, ok := rustDep.compiler.(libraryInterface)
766 if !ok || !dylib.dylib() {
767 ctx.ModuleErrorf("mod %q not an dylib library", depName)
768 return
769 }
770 directDylibDeps = append(directDylibDeps, rustDep)
771 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
772 case rlibDepTag:
773 rlib, ok := rustDep.compiler.(libraryInterface)
774 if !ok || !rlib.rlib() {
775 ctx.ModuleErrorf("mod %q not an rlib library", depName)
776 return
777 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400778 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779 directRlibDeps = append(directRlibDeps, rustDep)
780 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
781 case procMacroDepTag:
782 directProcMacroDeps = append(directProcMacroDeps, rustDep)
783 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
784 }
785
786 //Append the dependencies exportedDirs
Matthew Maurerbb3add12020-06-25 09:34:12 -0700787 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok {
788 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700790 }
791
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
793 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700794 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
795 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796 }
797 }
798
Ivan Lozano52767be2019-10-18 14:49:46 -0700799 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700800
Ivan Lozano52767be2019-10-18 14:49:46 -0700801 if ccDep, ok := dep.(cc.LinkableInterface); ok {
802 //Handle C dependencies
803 if _, ok := ccDep.(*Module); !ok {
804 if ccDep.Module().Target().Os != ctx.Os() {
805 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
806 return
807 }
808 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
809 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
810 return
811 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700812 }
813
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814 linkFile := ccDep.OutputFile()
815 linkPath := linkPathFromFilePath(linkFile.Path())
816 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500817 depFlag := "-l" + libName
818
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 if !linkFile.Valid() {
820 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
821 }
822
823 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700825 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500826 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500828 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400829 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
830 if mod, ok := ccDep.(*cc.Module); ok {
831 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
832 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
833 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400834 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700835 directStaticLibDeps = append(directStaticLibDeps, ccDep)
836 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700837 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500838 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500840 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400841 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
842 if mod, ok := ccDep.(*cc.Module); ok {
843 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
844 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
845 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 directSharedLibDeps = append(directSharedLibDeps, ccDep)
847 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
848 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700849 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700850 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700851 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700852 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 }
854
855 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700856 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
857 lib.exportLinkDirs(linkPath)
858 lib.exportDepFlags(depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700860 }
861 })
862
863 var rlibDepFiles RustLibraries
864 for _, dep := range directRlibDeps {
865 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
866 }
867 var dylibDepFiles RustLibraries
868 for _, dep := range directDylibDeps {
869 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
870 }
871 var procMacroDepFiles RustLibraries
872 for _, dep := range directProcMacroDeps {
873 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
874 }
875
876 var staticLibDepFiles android.Paths
877 for _, dep := range directStaticLibDeps {
878 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
879 }
880
881 var sharedLibDepFiles android.Paths
882 for _, dep := range directSharedLibDeps {
883 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
884 }
885
886 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
887 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
888 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
889 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
890 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
891
892 // Dedup exported flags from dependencies
893 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
894 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400895 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
896 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
897 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898
899 return depPaths
900}
901
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800902func (mod *Module) InstallInData() bool {
903 if mod.compiler == nil {
904 return false
905 }
906 return mod.compiler.inData()
907}
908
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909func linkPathFromFilePath(filepath android.Path) string {
910 return strings.Split(filepath.String(), filepath.Base())[0]
911}
Ivan Lozanod648c432020-02-06 12:05:10 -0500912
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500914 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700915 if strings.HasPrefix(libName, "lib") {
916 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700917 }
918 return libName
919}
Ivan Lozanod648c432020-02-06 12:05:10 -0500920
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
922 ctx := &depsContext{
923 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925
926 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700927 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900928 if cc.VersionVariantAvailable(mod) {
929 commonDepVariations = append(commonDepVariations,
930 blueprint.Variation{Mutator: "version", Variation: ""})
931 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700932 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700933 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800934 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700936 actx.AddVariationDependencies(
937 append(commonDepVariations, []blueprint.Variation{
938 {Mutator: "rust_libraries", Variation: "rlib"},
939 {Mutator: "link", Variation: ""}}...),
940 rlibDepTag, deps.Rlibs...)
941 actx.AddVariationDependencies(
942 append(commonDepVariations, []blueprint.Variation{
943 {Mutator: "rust_libraries", Variation: "dylib"},
944 {Mutator: "link", Variation: ""}}...),
945 dylibDepTag, deps.Dylibs...)
946
Matthew Maurer0f003b12020-06-29 14:34:06 -0700947 if deps.Rustlibs != nil {
948 autoDep := mod.compiler.(autoDeppable).autoDep()
949 actx.AddVariationDependencies(
950 append(commonDepVariations, []blueprint.Variation{
951 {Mutator: "rust_libraries", Variation: autoDep.variation},
952 {Mutator: "link", Variation: ""}}...),
953 autoDep.depTag, deps.Rustlibs...)
954 }
955
Ivan Lozano52767be2019-10-18 14:49:46 -0700956 actx.AddVariationDependencies(append(commonDepVariations,
957 blueprint.Variation{Mutator: "link", Variation: "shared"}),
958 cc.SharedDepTag, deps.SharedLibs...)
959 actx.AddVariationDependencies(append(commonDepVariations,
960 blueprint.Variation{Mutator: "link", Variation: "static"}),
961 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700962
Ivan Lozanof1c84332019-09-20 11:00:37 -0700963 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700964 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700965 }
966 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700967 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700968 }
969
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700970 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700971 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700972}
973
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400974func BeginMutator(ctx android.BottomUpMutatorContext) {
975 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
976 mod.beginMutator(ctx)
977 }
978}
979
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400980func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
981 ctx := &baseModuleContext{
982 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400983 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400984
985 mod.begin(ctx)
986}
987
Ivan Lozanoffee3342019-08-27 12:03:00 -0700988func (mod *Module) Name() string {
989 name := mod.ModuleBase.Name()
990 if p, ok := mod.compiler.(interface {
991 Name(string) string
992 }); ok {
993 name = p.Name(name)
994 }
995 return name
996}
997
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -0700998var _ android.HostToolProvider = (*Module)(nil)
999
1000func (mod *Module) HostToolPath() android.OptionalPath {
1001 if !mod.Host() {
1002 return android.OptionalPath{}
1003 }
1004 if _, ok := mod.compiler.(*binaryDecorator); ok {
1005 return mod.outputFile
1006 }
1007 return android.OptionalPath{}
1008}
1009
Ivan Lozanoffee3342019-08-27 12:03:00 -07001010var Bool = proptools.Bool
1011var BoolDefault = proptools.BoolDefault
1012var String = proptools.String
1013var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001014
1015var _ android.OutputFileProducer = (*Module)(nil)