blob: d302f27d7f2acba66f1fc7eee8de0c9f33598f58 [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
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040066 SubName string `blueprint:"mutated"`
67
Ivan Lozano43845682020-07-09 21:03:28 -040068 PreventInstall bool
69 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070070}
71
72type Module struct {
73 android.ModuleBase
74 android.DefaultableModuleBase
75
76 Properties BaseProperties
77
78 hod android.HostOrDeviceSupported
79 multilib android.Multilib
80
81 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040082 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020083 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070084 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040085 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070086 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040087
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040088 outputFile android.OptionalPath
89 generatedFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -070090}
91
Ivan Lozano43845682020-07-09 21:03:28 -040092func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
93 switch tag {
94 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070095 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -040096 return mod.sourceProvider.Srcs(), nil
97 } else {
98 if mod.outputFile.Valid() {
99 return android.Paths{mod.outputFile.Path()}, nil
100 }
101 return android.Paths{}, nil
102 }
103 default:
104 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
105 }
106}
107
Colin Cross7228ecd2019-11-18 16:00:16 -0800108var _ android.ImageInterface = (*Module)(nil)
109
110func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
111
112func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
113 return true
114}
115
Yifan Hong1b3348d2020-01-21 15:53:22 -0800116func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
117 return mod.InRamdisk()
118}
119
Colin Cross7228ecd2019-11-18 16:00:16 -0800120func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
121 return mod.InRecovery()
122}
123
124func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
125 return nil
126}
127
128func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
129}
130
Ivan Lozano52767be2019-10-18 14:49:46 -0700131func (mod *Module) BuildStubs() bool {
132 return false
133}
134
135func (mod *Module) HasStubsVariants() bool {
136 return false
137}
138
139func (mod *Module) SelectedStl() string {
140 return ""
141}
142
Ivan Lozano2b262972019-11-21 12:30:50 -0800143func (mod *Module) NonCcVariants() bool {
144 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400145 if _, ok := mod.compiler.(libraryInterface); ok {
146 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800147 }
148 }
149 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
150}
151
Ivan Lozano52767be2019-10-18 14:49:46 -0700152func (mod *Module) ApiLevel() string {
153 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
154}
155
156func (mod *Module) Static() bool {
157 if mod.compiler != nil {
158 if library, ok := mod.compiler.(libraryInterface); ok {
159 return library.static()
160 }
161 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400162 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700163}
164
165func (mod *Module) Shared() bool {
166 if mod.compiler != nil {
167 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400168 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700169 }
170 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400171 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700172}
173
174func (mod *Module) Toc() android.OptionalPath {
175 if mod.compiler != nil {
176 if _, ok := mod.compiler.(libraryInterface); ok {
177 return android.OptionalPath{}
178 }
179 }
180 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
181}
182
Yifan Hong1b3348d2020-01-21 15:53:22 -0800183func (mod *Module) OnlyInRamdisk() bool {
184 return false
185}
186
Ivan Lozano52767be2019-10-18 14:49:46 -0700187func (mod *Module) OnlyInRecovery() bool {
188 return false
189}
190
Colin Crossc511bc52020-04-07 16:50:32 +0000191func (mod *Module) UseSdk() bool {
192 return false
193}
194
Ivan Lozano52767be2019-10-18 14:49:46 -0700195func (mod *Module) UseVndk() bool {
196 return false
197}
198
199func (mod *Module) MustUseVendorVariant() bool {
200 return false
201}
202
203func (mod *Module) IsVndk() bool {
204 return false
205}
206
207func (mod *Module) HasVendorVariant() bool {
208 return false
209}
210
211func (mod *Module) SdkVersion() string {
212 return ""
213}
214
Colin Crossc511bc52020-04-07 16:50:32 +0000215func (mod *Module) AlwaysSdk() bool {
216 return false
217}
218
Jiyong Park2286afd2020-06-16 21:58:53 +0900219func (mod *Module) IsSdkVariant() bool {
220 return false
221}
222
Ivan Lozano52767be2019-10-18 14:49:46 -0700223func (mod *Module) ToolchainLibrary() bool {
224 return false
225}
226
227func (mod *Module) NdkPrebuiltStl() bool {
228 return false
229}
230
231func (mod *Module) StubDecorator() bool {
232 return false
233}
234
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235type Deps struct {
236 Dylibs []string
237 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700238 Rustlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239 ProcMacros []string
240 SharedLibs []string
241 StaticLibs []string
242
243 CrtBegin, CrtEnd string
244}
245
246type PathDeps struct {
247 DyLibs RustLibraries
248 RLibs RustLibraries
249 SharedLibs android.Paths
250 StaticLibs android.Paths
251 ProcMacros RustLibraries
252 linkDirs []string
253 depFlags []string
254 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700255
Ivan Lozano45901ed2020-07-24 16:05:01 -0400256 // Used by bindgen modules which call clang
257 depClangFlags []string
258 depIncludePaths android.Paths
259 depSystemIncludePaths android.Paths
260
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400261 coverageFiles android.Paths
262
Ivan Lozanof1c84332019-09-20 11:00:37 -0700263 CrtBegin android.OptionalPath
264 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700265
266 // Paths to generated source files
267 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268}
269
270type RustLibraries []RustLibrary
271
272type RustLibrary struct {
273 Path android.Path
274 CrateName string
275}
276
277type compiler interface {
278 compilerFlags(ctx ModuleContext, flags Flags) Flags
279 compilerProps() []interface{}
280 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
281 compilerDeps(ctx DepsContext, deps Deps) Deps
282 crateName() string
283
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800284 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700285 install(ctx ModuleContext, path android.Path)
286 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400287
288 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400289
290 Disabled() bool
291 SetDisabled()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400292}
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 {
Ivan Lozano89435d12020-07-31 11:01:18 -0400402 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
403 // VariantIs{Static,Shared} is set.
404 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700405 return true
406 }
407 }
408 return false
409}
410
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800411func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700412 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700413 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800414 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700415 }
416 }
417 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
418}
419
420func (mod *Module) SetStatic() {
421 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700422 if library, ok := mod.compiler.(libraryInterface); ok {
423 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700424 return
425 }
426 }
427 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
428}
429
430func (mod *Module) SetShared() {
431 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700432 if library, ok := mod.compiler.(libraryInterface); ok {
433 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700434 return
435 }
436 }
437 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
438}
439
440func (mod *Module) SetBuildStubs() {
441 panic("SetBuildStubs not yet implemented for rust modules")
442}
443
444func (mod *Module) SetStubsVersions(string) {
445 panic("SetStubsVersions not yet implemented for rust modules")
446}
447
Jooyung Han03b51852020-02-26 22:45:42 +0900448func (mod *Module) StubsVersion() string {
449 panic("SetStubsVersions not yet implemented for rust modules")
450}
451
Ivan Lozano183a3212019-10-18 14:18:45 -0700452func (mod *Module) BuildStaticVariant() bool {
453 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700454 if library, ok := mod.compiler.(libraryInterface); ok {
455 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700456 }
457 }
458 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
459}
460
461func (mod *Module) BuildSharedVariant() bool {
462 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700463 if library, ok := mod.compiler.(libraryInterface); ok {
464 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700465 }
466 }
467 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
468}
469
470// Rust module deps don't have a link order (?)
471func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
472
473func (mod *Module) GetDepsInLinkOrder() []android.Path {
474 return []android.Path{}
475}
476
477func (mod *Module) GetStaticVariant() cc.LinkableInterface {
478 return nil
479}
480
481func (mod *Module) Module() android.Module {
482 return mod
483}
484
485func (mod *Module) StubsVersions() []string {
486 // For now, Rust has no stubs versions.
487 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700488 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700489 return []string{}
490 }
491 }
492 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
493}
494
495func (mod *Module) OutputFile() android.OptionalPath {
496 return mod.outputFile
497}
498
499func (mod *Module) InRecovery() bool {
500 // For now, Rust has no notion of the recovery image
501 return false
502}
503func (mod *Module) HasStaticVariant() bool {
504 if mod.GetStaticVariant() != nil {
505 return true
506 }
507 return false
508}
509
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400510func (mod *Module) CoverageFiles() android.Paths {
511 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700512 if !mod.compiler.nativeCoverage() {
513 return android.Paths{}
514 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400515 if library, ok := mod.compiler.(*libraryDecorator); ok {
516 if library.coverageFile != nil {
517 return android.Paths{library.coverageFile}
518 }
519 return android.Paths{}
520 }
521 }
522 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
523}
524
Ivan Lozano183a3212019-10-18 14:18:45 -0700525var _ cc.LinkableInterface = (*Module)(nil)
526
Ivan Lozanoffee3342019-08-27 12:03:00 -0700527func (mod *Module) Init() android.Module {
528 mod.AddProperties(&mod.Properties)
529
530 if mod.compiler != nil {
531 mod.AddProperties(mod.compiler.compilerProps()...)
532 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533 if mod.coverage != nil {
534 mod.AddProperties(mod.coverage.props()...)
535 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200536 if mod.clippy != nil {
537 mod.AddProperties(mod.clippy.props()...)
538 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400539 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700540 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400541 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400542
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
544
545 android.InitDefaultableModule(mod)
546
Ivan Lozanode252912019-09-06 15:29:52 -0700547 // Explicitly disable unsupported targets.
548 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
549 disableTargets := struct {
550 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700551 Linux_bionic struct {
552 Enabled *bool
553 }
554 }
555 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700556 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
557
558 ctx.AppendProperties(&disableTargets)
559 })
560
Ivan Lozanoffee3342019-08-27 12:03:00 -0700561 return mod
562}
563
564func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
565 return &Module{
566 hod: hod,
567 multilib: multilib,
568 }
569}
570func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
571 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400572 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200573 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700574 return module
575}
576
577type ModuleContext interface {
578 android.ModuleContext
579 ModuleContextIntf
580}
581
582type BaseModuleContext interface {
583 android.BaseModuleContext
584 ModuleContextIntf
585}
586
587type DepsContext interface {
588 android.BottomUpMutatorContext
589 ModuleContextIntf
590}
591
592type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200593 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700595}
596
597type depsContext struct {
598 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599}
600
601type moduleContext struct {
602 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700603}
604
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200605type baseModuleContext struct {
606 android.BaseModuleContext
607}
608
609func (ctx *moduleContext) RustModule() *Module {
610 return ctx.Module().(*Module)
611}
612
613func (ctx *moduleContext) toolchain() config.Toolchain {
614 return ctx.RustModule().toolchain(ctx)
615}
616
617func (ctx *depsContext) RustModule() *Module {
618 return ctx.Module().(*Module)
619}
620
621func (ctx *depsContext) toolchain() config.Toolchain {
622 return ctx.RustModule().toolchain(ctx)
623}
624
625func (ctx *baseModuleContext) RustModule() *Module {
626 return ctx.Module().(*Module)
627}
628
629func (ctx *baseModuleContext) toolchain() config.Toolchain {
630 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400631}
632
633func (mod *Module) nativeCoverage() bool {
634 return mod.compiler != nil && mod.compiler.nativeCoverage()
635}
636
Ivan Lozanoffee3342019-08-27 12:03:00 -0700637func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
638 if mod.cachedToolchain == nil {
639 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
640 }
641 return mod.cachedToolchain
642}
643
644func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
645}
646
647func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
648 ctx := &moduleContext{
649 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700650 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651
652 toolchain := mod.toolchain(ctx)
653
654 if !toolchain.Supported() {
655 // This toolchain's unsupported, there's nothing to do for this mod.
656 return
657 }
658
659 deps := mod.depsToPaths(ctx)
660 flags := Flags{
661 Toolchain: toolchain,
662 }
663
664 if mod.compiler != nil {
665 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400666 }
667 if mod.coverage != nil {
668 flags, deps = mod.coverage.flags(ctx, flags, deps)
669 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200670 if mod.clippy != nil {
671 flags, deps = mod.clippy.flags(ctx, flags, deps)
672 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400673
Andrei Homescuc7767922020-08-05 06:36:19 -0700674 // SourceProvider needs to call GenerateSource() before compiler calls compile() so it can provide the source.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400675 // TODO(b/162588681) This shouldn't have to run for every variant.
676 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700677 generatedFile := mod.sourceProvider.GenerateSource(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400678 mod.generatedFile = android.OptionalPathForPath(generatedFile)
679 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
680 }
681
682 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400684
Ivan Lozanoffee3342019-08-27 12:03:00 -0700685 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400686 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400687 mod.compiler.install(ctx, mod.outputFile.Path())
688 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700689 }
690}
691
692func (mod *Module) deps(ctx DepsContext) Deps {
693 deps := Deps{}
694
695 if mod.compiler != nil {
696 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400697 }
698 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700699 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700 }
701
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400702 if mod.coverage != nil {
703 deps = mod.coverage.deps(ctx, deps)
704 }
705
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
707 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700708 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
710 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
711 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
712
713 return deps
714
715}
716
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717type dependencyTag struct {
718 blueprint.BaseDependencyTag
719 name string
720 library bool
721 proc_macro bool
722}
723
724var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400725 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
726 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
727 dylibDepTag = dependencyTag{name: "dylib", library: true}
728 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
729 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730)
731
Matthew Maurer0f003b12020-06-29 14:34:06 -0700732type autoDep struct {
733 variation string
734 depTag dependencyTag
735}
736
737var (
738 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
739 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
740)
741
742type autoDeppable interface {
743 autoDep() autoDep
744}
745
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400746func (mod *Module) begin(ctx BaseModuleContext) {
747 if mod.coverage != nil {
748 mod.coverage.begin(ctx)
749 }
750}
751
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
753 var depPaths PathDeps
754
755 directRlibDeps := []*Module{}
756 directDylibDeps := []*Module{}
757 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700758 directSharedLibDeps := [](cc.LinkableInterface){}
759 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400760 directSrcProvidersDeps := []*Module{}
761 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762
763 ctx.VisitDirectDeps(func(dep android.Module) {
764 depName := ctx.OtherModuleName(dep)
765 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400766 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700767 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700768
Ivan Lozanoffee3342019-08-27 12:03:00 -0700769 switch depTag {
770 case dylibDepTag:
771 dylib, ok := rustDep.compiler.(libraryInterface)
772 if !ok || !dylib.dylib() {
773 ctx.ModuleErrorf("mod %q not an dylib library", depName)
774 return
775 }
776 directDylibDeps = append(directDylibDeps, rustDep)
777 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
778 case rlibDepTag:
779 rlib, ok := rustDep.compiler.(libraryInterface)
780 if !ok || !rlib.rlib() {
781 ctx.ModuleErrorf("mod %q not an rlib library", depName)
782 return
783 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400784 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785 directRlibDeps = append(directRlibDeps, rustDep)
786 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
787 case procMacroDepTag:
788 directProcMacroDeps = append(directProcMacroDeps, rustDep)
789 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400790 case android.SourceDepTag:
791 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
792 // OS/Arch variant is used.
793 var helper string
794 if ctx.Host() {
795 helper = "missing 'host_supported'?"
796 } else {
797 helper = "device module defined?"
798 }
799
800 if dep.Target().Os != ctx.Os() {
801 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
802 return
803 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
804 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
805 return
806 }
807 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808 }
809
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400810 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
811 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700812 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814 }
815
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400817 linkFile := rustDep.outputFile
818 if !linkFile.Valid() {
819 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
820 depName, ctx.ModuleName())
821 return
822 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700824 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
825 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 }
827 }
828
Ivan Lozano89435d12020-07-31 11:01:18 -0400829 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700830 //Handle C dependencies
831 if _, ok := ccDep.(*Module); !ok {
832 if ccDep.Module().Target().Os != ctx.Os() {
833 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
834 return
835 }
836 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
837 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
838 return
839 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700840 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841 linkFile := ccDep.OutputFile()
842 linkPath := linkPathFromFilePath(linkFile.Path())
843 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500844 depFlag := "-l" + libName
845
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 if !linkFile.Valid() {
847 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
848 }
849
850 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700851 switch {
852 case cc.IsStaticDepTag(depTag):
Ivan Lozano6aa66022020-02-06 13:22:43 -0500853 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500855 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400856 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
857 if mod, ok := ccDep.(*cc.Module); ok {
858 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
859 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
860 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400861 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 directStaticLibDeps = append(directStaticLibDeps, ccDep)
863 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700864 case cc.IsSharedDepTag(depTag):
Ivan Lozano6aa66022020-02-06 13:22:43 -0500865 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500867 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400868 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
869 if mod, ok := ccDep.(*cc.Module); ok {
870 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
871 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
872 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 directSharedLibDeps = append(directSharedLibDeps, ccDep)
874 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
875 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700876 case depTag == cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700877 depPaths.CrtBegin = linkFile
Colin Cross6e511a92020-07-27 21:26:48 -0700878 case depTag == cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700879 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880 }
881
882 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700883 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
884 lib.exportLinkDirs(linkPath)
885 lib.exportDepFlags(depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700886 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400888
889 if srcDep, ok := dep.(android.SourceFileProducer); ok {
890 switch depTag {
891 case android.SourceDepTag:
892 // These are usually genrules which don't have per-target variants.
893 directSrcDeps = append(directSrcDeps, srcDep)
894 }
895 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700896 })
897
898 var rlibDepFiles RustLibraries
899 for _, dep := range directRlibDeps {
900 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
901 }
902 var dylibDepFiles RustLibraries
903 for _, dep := range directDylibDeps {
904 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
905 }
906 var procMacroDepFiles RustLibraries
907 for _, dep := range directProcMacroDeps {
908 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
909 }
910
911 var staticLibDepFiles android.Paths
912 for _, dep := range directStaticLibDeps {
913 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
914 }
915
916 var sharedLibDepFiles android.Paths
917 for _, dep := range directSharedLibDeps {
918 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
919 }
920
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400921 var srcProviderDepFiles android.Paths
922 for _, dep := range directSrcProvidersDeps {
923 srcs, _ := dep.OutputFiles("")
924 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
925 }
926 for _, dep := range directSrcDeps {
927 srcs := dep.Srcs()
928 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
929 }
930
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
932 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
933 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
934 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
935 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400936 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937
938 // Dedup exported flags from dependencies
939 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
940 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400941 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
942 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
943 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944
945 return depPaths
946}
947
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800948func (mod *Module) InstallInData() bool {
949 if mod.compiler == nil {
950 return false
951 }
952 return mod.compiler.inData()
953}
954
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955func linkPathFromFilePath(filepath android.Path) string {
956 return strings.Split(filepath.String(), filepath.Base())[0]
957}
Ivan Lozanod648c432020-02-06 12:05:10 -0500958
Ivan Lozanoffee3342019-08-27 12:03:00 -0700959func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500960 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700961 if strings.HasPrefix(libName, "lib") {
962 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963 }
964 return libName
965}
Ivan Lozanod648c432020-02-06 12:05:10 -0500966
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
968 ctx := &depsContext{
969 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700970 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700971
972 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700973 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900974 if cc.VersionVariantAvailable(mod) {
975 commonDepVariations = append(commonDepVariations,
976 blueprint.Variation{Mutator: "version", Variation: ""})
977 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700978 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700979 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800980 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700981 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700982 actx.AddVariationDependencies(
983 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400984 {Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700985 rlibDepTag, deps.Rlibs...)
986 actx.AddVariationDependencies(
987 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400988 {Mutator: "rust_libraries", Variation: "dylib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700989 dylibDepTag, deps.Dylibs...)
990
Matthew Maurer0f003b12020-06-29 14:34:06 -0700991 if deps.Rustlibs != nil {
992 autoDep := mod.compiler.(autoDeppable).autoDep()
993 actx.AddVariationDependencies(
994 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400995 {Mutator: "rust_libraries", Variation: autoDep.variation}}...),
Matthew Maurer0f003b12020-06-29 14:34:06 -0700996 autoDep.depTag, deps.Rustlibs...)
997 }
998
Ivan Lozano52767be2019-10-18 14:49:46 -0700999 actx.AddVariationDependencies(append(commonDepVariations,
1000 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001001 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001002 actx.AddVariationDependencies(append(commonDepVariations,
1003 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001004 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001005
Ivan Lozanof1c84332019-09-20 11:00:37 -07001006 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -07001007 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001008 }
1009 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -07001010 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001011 }
1012
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001013 if mod.sourceProvider != nil {
1014 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1015 bindgen.Properties.Custom_bindgen != "" {
1016 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1017 bindgen.Properties.Custom_bindgen)
1018 }
1019 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001020 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001021 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001022}
1023
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001024func BeginMutator(ctx android.BottomUpMutatorContext) {
1025 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1026 mod.beginMutator(ctx)
1027 }
1028}
1029
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001030func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1031 ctx := &baseModuleContext{
1032 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001033 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001034
1035 mod.begin(ctx)
1036}
1037
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038func (mod *Module) Name() string {
1039 name := mod.ModuleBase.Name()
1040 if p, ok := mod.compiler.(interface {
1041 Name(string) string
1042 }); ok {
1043 name = p.Name(name)
1044 }
1045 return name
1046}
1047
Ivan Lozano32267c82020-08-04 16:27:16 -04001048func (mod *Module) setClippy(clippy bool) {
1049 if mod.clippy != nil {
1050 mod.clippy.Properties.Clippy = proptools.BoolPtr(clippy)
1051 }
1052}
1053
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001054var _ android.HostToolProvider = (*Module)(nil)
1055
1056func (mod *Module) HostToolPath() android.OptionalPath {
1057 if !mod.Host() {
1058 return android.OptionalPath{}
1059 }
1060 if _, ok := mod.compiler.(*binaryDecorator); ok {
1061 return mod.outputFile
1062 }
1063 return android.OptionalPath{}
1064}
1065
Ivan Lozanoffee3342019-08-27 12:03:00 -07001066var Bool = proptools.Bool
1067var BoolDefault = proptools.BoolDefault
1068var String = proptools.String
1069var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001070
1071var _ android.OutputFileProducer = (*Module)(nil)