blob: 51eaf6838b5342246fb1adbcf6d800036dac440a [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
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070065 SubName string `blueprint:"mutated"`
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040066 PreventInstall bool
67 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070068}
69
70type Module struct {
71 android.ModuleBase
72 android.DefaultableModuleBase
73
74 Properties BaseProperties
75
76 hod android.HostOrDeviceSupported
77 multilib android.Multilib
78
79 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040080 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020081 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070082 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040083 sourceProvider SourceProvider
Ivan Lozanoffee3342019-08-27 12:03:00 -070084 subAndroidMkOnce map[subAndroidMkProvider]bool
85 outputFile android.OptionalPath
Ivan Lozano4fef93c2020-07-08 08:39:44 -040086
87 subName string
Ivan Lozanoffee3342019-08-27 12:03:00 -070088}
89
Colin Cross7228ecd2019-11-18 16:00:16 -080090var _ android.ImageInterface = (*Module)(nil)
91
92func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
93
94func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
95 return true
96}
97
Yifan Hong1b3348d2020-01-21 15:53:22 -080098func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
99 return mod.InRamdisk()
100}
101
Colin Cross7228ecd2019-11-18 16:00:16 -0800102func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
103 return mod.InRecovery()
104}
105
106func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
107 return nil
108}
109
110func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
111}
112
Ivan Lozano52767be2019-10-18 14:49:46 -0700113func (mod *Module) BuildStubs() bool {
114 return false
115}
116
117func (mod *Module) HasStubsVariants() bool {
118 return false
119}
120
121func (mod *Module) SelectedStl() string {
122 return ""
123}
124
Ivan Lozano2b262972019-11-21 12:30:50 -0800125func (mod *Module) NonCcVariants() bool {
126 if mod.compiler != nil {
127 if library, ok := mod.compiler.(libraryInterface); ok {
128 if library.buildRlib() || library.buildDylib() {
129 return true
130 } else {
131 return false
132 }
133 }
134 }
135 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
136}
137
Ivan Lozano52767be2019-10-18 14:49:46 -0700138func (mod *Module) ApiLevel() string {
139 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
140}
141
142func (mod *Module) Static() bool {
143 if mod.compiler != nil {
144 if library, ok := mod.compiler.(libraryInterface); ok {
145 return library.static()
146 }
147 }
148 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
149}
150
151func (mod *Module) Shared() bool {
152 if mod.compiler != nil {
153 if library, ok := mod.compiler.(libraryInterface); ok {
154 return library.static()
155 }
156 }
157 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
158}
159
160func (mod *Module) Toc() android.OptionalPath {
161 if mod.compiler != nil {
162 if _, ok := mod.compiler.(libraryInterface); ok {
163 return android.OptionalPath{}
164 }
165 }
166 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
167}
168
Yifan Hong1b3348d2020-01-21 15:53:22 -0800169func (mod *Module) OnlyInRamdisk() bool {
170 return false
171}
172
Ivan Lozano52767be2019-10-18 14:49:46 -0700173func (mod *Module) OnlyInRecovery() bool {
174 return false
175}
176
Colin Crossc511bc52020-04-07 16:50:32 +0000177func (mod *Module) UseSdk() bool {
178 return false
179}
180
Ivan Lozano52767be2019-10-18 14:49:46 -0700181func (mod *Module) UseVndk() bool {
182 return false
183}
184
185func (mod *Module) MustUseVendorVariant() bool {
186 return false
187}
188
189func (mod *Module) IsVndk() bool {
190 return false
191}
192
193func (mod *Module) HasVendorVariant() bool {
194 return false
195}
196
197func (mod *Module) SdkVersion() string {
198 return ""
199}
200
Colin Crossc511bc52020-04-07 16:50:32 +0000201func (mod *Module) AlwaysSdk() bool {
202 return false
203}
204
Jiyong Park2286afd2020-06-16 21:58:53 +0900205func (mod *Module) IsSdkVariant() bool {
206 return false
207}
208
Ivan Lozano52767be2019-10-18 14:49:46 -0700209func (mod *Module) ToolchainLibrary() bool {
210 return false
211}
212
213func (mod *Module) NdkPrebuiltStl() bool {
214 return false
215}
216
217func (mod *Module) StubDecorator() bool {
218 return false
219}
220
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221type Deps struct {
222 Dylibs []string
223 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700224 Rustlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700225 ProcMacros []string
226 SharedLibs []string
227 StaticLibs []string
228
229 CrtBegin, CrtEnd string
230}
231
232type PathDeps struct {
233 DyLibs RustLibraries
234 RLibs RustLibraries
235 SharedLibs android.Paths
236 StaticLibs android.Paths
237 ProcMacros RustLibraries
238 linkDirs []string
239 depFlags []string
240 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700241
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400242 coverageFiles android.Paths
243
Ivan Lozanof1c84332019-09-20 11:00:37 -0700244 CrtBegin android.OptionalPath
245 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700246
247 // Paths to generated source files
248 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700249}
250
251type RustLibraries []RustLibrary
252
253type RustLibrary struct {
254 Path android.Path
255 CrateName string
256}
257
258type compiler interface {
259 compilerFlags(ctx ModuleContext, flags Flags) Flags
260 compilerProps() []interface{}
261 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
262 compilerDeps(ctx DepsContext, deps Deps) Deps
263 crateName() string
264
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800265 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266 install(ctx ModuleContext, path android.Path)
267 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400268
269 nativeCoverage() bool
270}
271
Matthew Maurerbb3add12020-06-25 09:34:12 -0700272type exportedFlagsProducer interface {
273 exportedLinkDirs() []string
274 exportedDepFlags() []string
275 exportLinkDirs(...string)
276 exportDepFlags(...string)
277}
278
279type flagExporter struct {
280 depFlags []string
281 linkDirs []string
282}
283
284func (flagExporter *flagExporter) exportedLinkDirs() []string {
285 return flagExporter.linkDirs
286}
287
288func (flagExporter *flagExporter) exportedDepFlags() []string {
289 return flagExporter.depFlags
290}
291
292func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
293 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
294}
295
296func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
297 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
298}
299
300var _ exportedFlagsProducer = (*flagExporter)(nil)
301
302func NewFlagExporter() *flagExporter {
303 return &flagExporter{
304 depFlags: []string{},
305 linkDirs: []string{},
306 }
307}
308
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400309func (mod *Module) isCoverageVariant() bool {
310 return mod.coverage.Properties.IsCoverageVariant
311}
312
313var _ cc.Coverage = (*Module)(nil)
314
315func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
316 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
317}
318
319func (mod *Module) PreventInstall() {
320 mod.Properties.PreventInstall = true
321}
322
323func (mod *Module) HideFromMake() {
324 mod.Properties.HideFromMake = true
325}
326
327func (mod *Module) MarkAsCoverageVariant(coverage bool) {
328 mod.coverage.Properties.IsCoverageVariant = coverage
329}
330
331func (mod *Module) EnableCoverageIfNeeded() {
332 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700333}
334
335func defaultsFactory() android.Module {
336 return DefaultsFactory()
337}
338
339type Defaults struct {
340 android.ModuleBase
341 android.DefaultsModuleBase
342}
343
344func DefaultsFactory(props ...interface{}) android.Module {
345 module := &Defaults{}
346
347 module.AddProperties(props...)
348 module.AddProperties(
349 &BaseProperties{},
350 &BaseCompilerProperties{},
351 &BinaryCompilerProperties{},
352 &LibraryCompilerProperties{},
353 &ProcMacroCompilerProperties{},
354 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400355 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700356 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400357 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200358 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700359 )
360
361 android.InitDefaultsModule(module)
362 return module
363}
364
365func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700366 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367}
368
Ivan Lozano183a3212019-10-18 14:18:45 -0700369func (mod *Module) CcLibrary() bool {
370 if mod.compiler != nil {
371 if _, ok := mod.compiler.(*libraryDecorator); ok {
372 return true
373 }
374 }
375 return false
376}
377
378func (mod *Module) CcLibraryInterface() bool {
379 if mod.compiler != nil {
380 if _, ok := mod.compiler.(libraryInterface); ok {
381 return true
382 }
383 }
384 return false
385}
386
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800387func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700388 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700389 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800390 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700391 }
392 }
393 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
394}
395
396func (mod *Module) SetStatic() {
397 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700398 if library, ok := mod.compiler.(libraryInterface); ok {
399 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700400 return
401 }
402 }
403 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
404}
405
406func (mod *Module) SetShared() {
407 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700408 if library, ok := mod.compiler.(libraryInterface); ok {
409 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700410 return
411 }
412 }
413 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
414}
415
416func (mod *Module) SetBuildStubs() {
417 panic("SetBuildStubs not yet implemented for rust modules")
418}
419
420func (mod *Module) SetStubsVersions(string) {
421 panic("SetStubsVersions not yet implemented for rust modules")
422}
423
Jooyung Han03b51852020-02-26 22:45:42 +0900424func (mod *Module) StubsVersion() string {
425 panic("SetStubsVersions not yet implemented for rust modules")
426}
427
Ivan Lozano183a3212019-10-18 14:18:45 -0700428func (mod *Module) BuildStaticVariant() bool {
429 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700430 if library, ok := mod.compiler.(libraryInterface); ok {
431 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700432 }
433 }
434 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
435}
436
437func (mod *Module) BuildSharedVariant() bool {
438 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700439 if library, ok := mod.compiler.(libraryInterface); ok {
440 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700441 }
442 }
443 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
444}
445
446// Rust module deps don't have a link order (?)
447func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
448
449func (mod *Module) GetDepsInLinkOrder() []android.Path {
450 return []android.Path{}
451}
452
453func (mod *Module) GetStaticVariant() cc.LinkableInterface {
454 return nil
455}
456
457func (mod *Module) Module() android.Module {
458 return mod
459}
460
461func (mod *Module) StubsVersions() []string {
462 // For now, Rust has no stubs versions.
463 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700464 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700465 return []string{}
466 }
467 }
468 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
469}
470
471func (mod *Module) OutputFile() android.OptionalPath {
472 return mod.outputFile
473}
474
475func (mod *Module) InRecovery() bool {
476 // For now, Rust has no notion of the recovery image
477 return false
478}
479func (mod *Module) HasStaticVariant() bool {
480 if mod.GetStaticVariant() != nil {
481 return true
482 }
483 return false
484}
485
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400486func (mod *Module) CoverageFiles() android.Paths {
487 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700488 if !mod.compiler.nativeCoverage() {
489 return android.Paths{}
490 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400491 if library, ok := mod.compiler.(*libraryDecorator); ok {
492 if library.coverageFile != nil {
493 return android.Paths{library.coverageFile}
494 }
495 return android.Paths{}
496 }
497 }
498 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
499}
500
Ivan Lozano183a3212019-10-18 14:18:45 -0700501var _ cc.LinkableInterface = (*Module)(nil)
502
Ivan Lozanoffee3342019-08-27 12:03:00 -0700503func (mod *Module) Init() android.Module {
504 mod.AddProperties(&mod.Properties)
505
506 if mod.compiler != nil {
507 mod.AddProperties(mod.compiler.compilerProps()...)
508 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400509 if mod.coverage != nil {
510 mod.AddProperties(mod.coverage.props()...)
511 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200512 if mod.clippy != nil {
513 mod.AddProperties(mod.clippy.props()...)
514 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400515 if mod.sourceProvider != nil {
516 mod.AddProperties(mod.sourceProvider.sourceProviderProps()...)
517 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400518
Ivan Lozanoffee3342019-08-27 12:03:00 -0700519 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
520
521 android.InitDefaultableModule(mod)
522
Ivan Lozanode252912019-09-06 15:29:52 -0700523 // Explicitly disable unsupported targets.
524 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
525 disableTargets := struct {
526 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700527 Linux_bionic struct {
528 Enabled *bool
529 }
530 }
531 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700532 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
533
534 ctx.AppendProperties(&disableTargets)
535 })
536
Ivan Lozanoffee3342019-08-27 12:03:00 -0700537 return mod
538}
539
540func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
541 return &Module{
542 hod: hod,
543 multilib: multilib,
544 }
545}
546func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
547 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400548 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200549 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700550 return module
551}
552
553type ModuleContext interface {
554 android.ModuleContext
555 ModuleContextIntf
556}
557
558type BaseModuleContext interface {
559 android.BaseModuleContext
560 ModuleContextIntf
561}
562
563type DepsContext interface {
564 android.BottomUpMutatorContext
565 ModuleContextIntf
566}
567
568type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200569 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700570 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571}
572
573type depsContext struct {
574 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700575}
576
577type moduleContext struct {
578 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579}
580
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200581type baseModuleContext struct {
582 android.BaseModuleContext
583}
584
585func (ctx *moduleContext) RustModule() *Module {
586 return ctx.Module().(*Module)
587}
588
589func (ctx *moduleContext) toolchain() config.Toolchain {
590 return ctx.RustModule().toolchain(ctx)
591}
592
593func (ctx *depsContext) RustModule() *Module {
594 return ctx.Module().(*Module)
595}
596
597func (ctx *depsContext) toolchain() config.Toolchain {
598 return ctx.RustModule().toolchain(ctx)
599}
600
601func (ctx *baseModuleContext) RustModule() *Module {
602 return ctx.Module().(*Module)
603}
604
605func (ctx *baseModuleContext) toolchain() config.Toolchain {
606 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400607}
608
609func (mod *Module) nativeCoverage() bool {
610 return mod.compiler != nil && mod.compiler.nativeCoverage()
611}
612
Ivan Lozanoffee3342019-08-27 12:03:00 -0700613func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
614 if mod.cachedToolchain == nil {
615 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
616 }
617 return mod.cachedToolchain
618}
619
620func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
621}
622
623func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
624 ctx := &moduleContext{
625 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700626 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700627
628 toolchain := mod.toolchain(ctx)
629
630 if !toolchain.Supported() {
631 // This toolchain's unsupported, there's nothing to do for this mod.
632 return
633 }
634
635 deps := mod.depsToPaths(ctx)
636 flags := Flags{
637 Toolchain: toolchain,
638 }
639
640 if mod.compiler != nil {
641 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400642 }
643 if mod.coverage != nil {
644 flags, deps = mod.coverage.flags(ctx, flags, deps)
645 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200646 if mod.clippy != nil {
647 flags, deps = mod.clippy.flags(ctx, flags, deps)
648 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400649
650 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651 outputFile := mod.compiler.compile(ctx, flags, deps)
652 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400653 if !mod.Properties.PreventInstall {
654 mod.compiler.install(ctx, mod.outputFile.Path())
655 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400656 } else if mod.sourceProvider != nil {
657 outputFile := mod.sourceProvider.generateSource(ctx)
658 mod.outputFile = android.OptionalPathForPath(outputFile)
659 mod.subName = ctx.ModuleSubDir()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700660 }
661}
662
663func (mod *Module) deps(ctx DepsContext) Deps {
664 deps := Deps{}
665
666 if mod.compiler != nil {
667 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400668 } else if mod.sourceProvider != nil {
669 deps = mod.sourceProvider.sourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670 }
671
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400672 if mod.coverage != nil {
673 deps = mod.coverage.deps(ctx, deps)
674 }
675
Ivan Lozanoffee3342019-08-27 12:03:00 -0700676 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
677 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700678 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700679 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
680 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
681 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
682
683 return deps
684
685}
686
Ivan Lozanoffee3342019-08-27 12:03:00 -0700687type dependencyTag struct {
688 blueprint.BaseDependencyTag
689 name string
690 library bool
691 proc_macro bool
692}
693
694var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700695 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
696 dylibDepTag = dependencyTag{name: "dylib", library: true}
697 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
698 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700699)
700
Matthew Maurer0f003b12020-06-29 14:34:06 -0700701type autoDep struct {
702 variation string
703 depTag dependencyTag
704}
705
706var (
707 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
708 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
709)
710
711type autoDeppable interface {
712 autoDep() autoDep
713}
714
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400715func (mod *Module) begin(ctx BaseModuleContext) {
716 if mod.coverage != nil {
717 mod.coverage.begin(ctx)
718 }
719}
720
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
722 var depPaths PathDeps
723
724 directRlibDeps := []*Module{}
725 directDylibDeps := []*Module{}
726 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700727 directSharedLibDeps := [](cc.LinkableInterface){}
728 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729
730 ctx.VisitDirectDeps(func(dep android.Module) {
731 depName := ctx.OtherModuleName(dep)
732 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 if rustDep, ok := dep.(*Module); ok {
734 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700735
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736 linkFile := rustDep.outputFile
737 if !linkFile.Valid() {
738 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
739 }
740
741 switch depTag {
742 case dylibDepTag:
743 dylib, ok := rustDep.compiler.(libraryInterface)
744 if !ok || !dylib.dylib() {
745 ctx.ModuleErrorf("mod %q not an dylib library", depName)
746 return
747 }
748 directDylibDeps = append(directDylibDeps, rustDep)
749 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
750 case rlibDepTag:
751 rlib, ok := rustDep.compiler.(libraryInterface)
752 if !ok || !rlib.rlib() {
753 ctx.ModuleErrorf("mod %q not an rlib library", depName)
754 return
755 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400756 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 directRlibDeps = append(directRlibDeps, rustDep)
758 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
759 case procMacroDepTag:
760 directProcMacroDeps = append(directProcMacroDeps, rustDep)
761 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
762 }
763
764 //Append the dependencies exportedDirs
Matthew Maurerbb3add12020-06-25 09:34:12 -0700765 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok {
766 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700767 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768 }
769
Ivan Lozanoffee3342019-08-27 12:03:00 -0700770 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
771 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700772 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
773 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700774 }
775 }
776
Ivan Lozano52767be2019-10-18 14:49:46 -0700777 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700778
Ivan Lozano52767be2019-10-18 14:49:46 -0700779 if ccDep, ok := dep.(cc.LinkableInterface); ok {
780 //Handle C dependencies
781 if _, ok := ccDep.(*Module); !ok {
782 if ccDep.Module().Target().Os != ctx.Os() {
783 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
784 return
785 }
786 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
787 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
788 return
789 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700790 }
791
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792 linkFile := ccDep.OutputFile()
793 linkPath := linkPathFromFilePath(linkFile.Path())
794 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500795 depFlag := "-l" + libName
796
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797 if !linkFile.Valid() {
798 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
799 }
800
801 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700803 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500804 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500806 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400807 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808 directStaticLibDeps = append(directStaticLibDeps, ccDep)
809 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700810 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500811 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500813 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814 directSharedLibDeps = append(directSharedLibDeps, ccDep)
815 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
816 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700817 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700818 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700819 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700820 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821 }
822
823 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700824 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
825 lib.exportLinkDirs(linkPath)
826 lib.exportDepFlags(depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828 }
829 })
830
831 var rlibDepFiles RustLibraries
832 for _, dep := range directRlibDeps {
833 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
834 }
835 var dylibDepFiles RustLibraries
836 for _, dep := range directDylibDeps {
837 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
838 }
839 var procMacroDepFiles RustLibraries
840 for _, dep := range directProcMacroDeps {
841 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
842 }
843
844 var staticLibDepFiles android.Paths
845 for _, dep := range directStaticLibDeps {
846 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
847 }
848
849 var sharedLibDepFiles android.Paths
850 for _, dep := range directSharedLibDeps {
851 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
852 }
853
854 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
855 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
856 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
857 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
858 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
859
860 // Dedup exported flags from dependencies
861 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
862 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700863 depPaths.SrcDeps = android.FirstUniquePaths(depPaths.SrcDeps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700864
865 return depPaths
866}
867
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800868func (mod *Module) InstallInData() bool {
869 if mod.compiler == nil {
870 return false
871 }
872 return mod.compiler.inData()
873}
874
Ivan Lozanoffee3342019-08-27 12:03:00 -0700875func linkPathFromFilePath(filepath android.Path) string {
876 return strings.Split(filepath.String(), filepath.Base())[0]
877}
Ivan Lozanod648c432020-02-06 12:05:10 -0500878
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500880 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700881 if strings.HasPrefix(libName, "lib") {
882 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 }
884 return libName
885}
Ivan Lozanod648c432020-02-06 12:05:10 -0500886
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
888 ctx := &depsContext{
889 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891
892 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700893 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900894 if cc.VersionVariantAvailable(mod) {
895 commonDepVariations = append(commonDepVariations,
896 blueprint.Variation{Mutator: "version", Variation: ""})
897 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700899 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800900 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700901 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700902 actx.AddVariationDependencies(
903 append(commonDepVariations, []blueprint.Variation{
904 {Mutator: "rust_libraries", Variation: "rlib"},
905 {Mutator: "link", Variation: ""}}...),
906 rlibDepTag, deps.Rlibs...)
907 actx.AddVariationDependencies(
908 append(commonDepVariations, []blueprint.Variation{
909 {Mutator: "rust_libraries", Variation: "dylib"},
910 {Mutator: "link", Variation: ""}}...),
911 dylibDepTag, deps.Dylibs...)
912
Matthew Maurer0f003b12020-06-29 14:34:06 -0700913 if deps.Rustlibs != nil {
914 autoDep := mod.compiler.(autoDeppable).autoDep()
915 actx.AddVariationDependencies(
916 append(commonDepVariations, []blueprint.Variation{
917 {Mutator: "rust_libraries", Variation: autoDep.variation},
918 {Mutator: "link", Variation: ""}}...),
919 autoDep.depTag, deps.Rustlibs...)
920 }
921
Ivan Lozano52767be2019-10-18 14:49:46 -0700922 actx.AddVariationDependencies(append(commonDepVariations,
923 blueprint.Variation{Mutator: "link", Variation: "shared"}),
924 cc.SharedDepTag, deps.SharedLibs...)
925 actx.AddVariationDependencies(append(commonDepVariations,
926 blueprint.Variation{Mutator: "link", Variation: "static"}),
927 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700928
Ivan Lozanof1c84332019-09-20 11:00:37 -0700929 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700930 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700931 }
932 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700933 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700934 }
935
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700936 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700937 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938}
939
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400940func BeginMutator(ctx android.BottomUpMutatorContext) {
941 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
942 mod.beginMutator(ctx)
943 }
944}
945
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400946func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
947 ctx := &baseModuleContext{
948 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400949 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400950
951 mod.begin(ctx)
952}
953
Ivan Lozanoffee3342019-08-27 12:03:00 -0700954func (mod *Module) Name() string {
955 name := mod.ModuleBase.Name()
956 if p, ok := mod.compiler.(interface {
957 Name(string) string
958 }); ok {
959 name = p.Name(name)
960 }
961 return name
962}
963
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -0700964var _ android.HostToolProvider = (*Module)(nil)
965
966func (mod *Module) HostToolPath() android.OptionalPath {
967 if !mod.Host() {
968 return android.OptionalPath{}
969 }
970 if _, ok := mod.compiler.(*binaryDecorator); ok {
971 return mod.outputFile
972 }
973 return android.OptionalPath{}
974}
975
Ivan Lozanoffee3342019-08-27 12:03:00 -0700976var Bool = proptools.Bool
977var BoolDefault = proptools.BoolDefault
978var String = proptools.String
979var StringPtr = proptools.StringPtr