blob: 68dc5a08b21bdf1f22f0eb39991ba69c159f1deb [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 {
Ivan Lozano2093af22020-08-25 12:48:19 -0400247 DyLibs RustLibraries
248 RLibs RustLibraries
249 SharedLibs android.Paths
250 StaticLibs android.Paths
251 ProcMacros RustLibraries
252 linkDirs []string
253 depFlags []string
254 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700256
Ivan Lozano45901ed2020-07-24 16:05:01 -0400257 // Used by bindgen modules which call clang
258 depClangFlags []string
259 depIncludePaths android.Paths
260 depSystemIncludePaths android.Paths
261
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400262 coverageFiles android.Paths
263
Ivan Lozanof1c84332019-09-20 11:00:37 -0700264 CrtBegin android.OptionalPath
265 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700266
267 // Paths to generated source files
268 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700269}
270
271type RustLibraries []RustLibrary
272
273type RustLibrary struct {
274 Path android.Path
275 CrateName string
276}
277
278type compiler interface {
279 compilerFlags(ctx ModuleContext, flags Flags) Flags
280 compilerProps() []interface{}
281 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
282 compilerDeps(ctx DepsContext, deps Deps) Deps
283 crateName() string
284
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800285 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286 install(ctx ModuleContext, path android.Path)
287 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400288
289 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400290
291 Disabled() bool
292 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400293
294 static() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400295}
296
Matthew Maurerbb3add12020-06-25 09:34:12 -0700297type exportedFlagsProducer interface {
298 exportedLinkDirs() []string
299 exportedDepFlags() []string
Ivan Lozano2093af22020-08-25 12:48:19 -0400300 exportedLinkObjects() []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700301 exportLinkDirs(...string)
302 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400303 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700304}
305
306type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400307 depFlags []string
308 linkDirs []string
309 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700310}
311
312func (flagExporter *flagExporter) exportedLinkDirs() []string {
313 return flagExporter.linkDirs
314}
315
316func (flagExporter *flagExporter) exportedDepFlags() []string {
317 return flagExporter.depFlags
318}
319
Ivan Lozano2093af22020-08-25 12:48:19 -0400320func (flagExporter *flagExporter) exportedLinkObjects() []string {
321 return flagExporter.linkObjects
322}
323
Matthew Maurerbb3add12020-06-25 09:34:12 -0700324func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
325 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
326}
327
328func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
329 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
330}
331
Ivan Lozano2093af22020-08-25 12:48:19 -0400332func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
333 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
334}
335
Matthew Maurerbb3add12020-06-25 09:34:12 -0700336var _ exportedFlagsProducer = (*flagExporter)(nil)
337
338func NewFlagExporter() *flagExporter {
339 return &flagExporter{
Ivan Lozano2093af22020-08-25 12:48:19 -0400340 depFlags: []string{},
341 linkDirs: []string{},
342 linkObjects: []string{},
Matthew Maurerbb3add12020-06-25 09:34:12 -0700343 }
344}
345
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400346func (mod *Module) isCoverageVariant() bool {
347 return mod.coverage.Properties.IsCoverageVariant
348}
349
350var _ cc.Coverage = (*Module)(nil)
351
352func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
353 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
354}
355
356func (mod *Module) PreventInstall() {
357 mod.Properties.PreventInstall = true
358}
359
360func (mod *Module) HideFromMake() {
361 mod.Properties.HideFromMake = true
362}
363
364func (mod *Module) MarkAsCoverageVariant(coverage bool) {
365 mod.coverage.Properties.IsCoverageVariant = coverage
366}
367
368func (mod *Module) EnableCoverageIfNeeded() {
369 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700370}
371
372func defaultsFactory() android.Module {
373 return DefaultsFactory()
374}
375
376type Defaults struct {
377 android.ModuleBase
378 android.DefaultsModuleBase
379}
380
381func DefaultsFactory(props ...interface{}) android.Module {
382 module := &Defaults{}
383
384 module.AddProperties(props...)
385 module.AddProperties(
386 &BaseProperties{},
387 &BaseCompilerProperties{},
388 &BinaryCompilerProperties{},
389 &LibraryCompilerProperties{},
390 &ProcMacroCompilerProperties{},
391 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400392 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700393 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400394 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200395 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700396 )
397
398 android.InitDefaultsModule(module)
399 return module
400}
401
402func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700403 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404}
405
Ivan Lozano183a3212019-10-18 14:18:45 -0700406func (mod *Module) CcLibrary() bool {
407 if mod.compiler != nil {
408 if _, ok := mod.compiler.(*libraryDecorator); ok {
409 return true
410 }
411 }
412 return false
413}
414
415func (mod *Module) CcLibraryInterface() bool {
416 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400417 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
418 // VariantIs{Static,Shared} is set.
419 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700420 return true
421 }
422 }
423 return false
424}
425
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800426func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700427 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700428 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800429 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700430 }
431 }
432 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
433}
434
435func (mod *Module) SetStatic() {
436 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700437 if library, ok := mod.compiler.(libraryInterface); ok {
438 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700439 return
440 }
441 }
442 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
443}
444
445func (mod *Module) SetShared() {
446 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700447 if library, ok := mod.compiler.(libraryInterface); ok {
448 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700449 return
450 }
451 }
452 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
453}
454
455func (mod *Module) SetBuildStubs() {
456 panic("SetBuildStubs not yet implemented for rust modules")
457}
458
459func (mod *Module) SetStubsVersions(string) {
460 panic("SetStubsVersions not yet implemented for rust modules")
461}
462
Jooyung Han03b51852020-02-26 22:45:42 +0900463func (mod *Module) StubsVersion() string {
464 panic("SetStubsVersions not yet implemented for rust modules")
465}
466
Ivan Lozano183a3212019-10-18 14:18:45 -0700467func (mod *Module) BuildStaticVariant() bool {
468 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700469 if library, ok := mod.compiler.(libraryInterface); ok {
470 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700471 }
472 }
473 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
474}
475
476func (mod *Module) BuildSharedVariant() bool {
477 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700478 if library, ok := mod.compiler.(libraryInterface); ok {
479 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700480 }
481 }
482 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
483}
484
485// Rust module deps don't have a link order (?)
486func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
487
488func (mod *Module) GetDepsInLinkOrder() []android.Path {
489 return []android.Path{}
490}
491
492func (mod *Module) GetStaticVariant() cc.LinkableInterface {
493 return nil
494}
495
496func (mod *Module) Module() android.Module {
497 return mod
498}
499
500func (mod *Module) StubsVersions() []string {
501 // For now, Rust has no stubs versions.
502 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700503 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700504 return []string{}
505 }
506 }
507 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
508}
509
510func (mod *Module) OutputFile() android.OptionalPath {
511 return mod.outputFile
512}
513
514func (mod *Module) InRecovery() bool {
515 // For now, Rust has no notion of the recovery image
516 return false
517}
518func (mod *Module) HasStaticVariant() bool {
519 if mod.GetStaticVariant() != nil {
520 return true
521 }
522 return false
523}
524
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400525func (mod *Module) CoverageFiles() android.Paths {
526 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700527 if !mod.compiler.nativeCoverage() {
528 return android.Paths{}
529 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400530 if library, ok := mod.compiler.(*libraryDecorator); ok {
531 if library.coverageFile != nil {
532 return android.Paths{library.coverageFile}
533 }
534 return android.Paths{}
535 }
536 }
537 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
538}
539
Ivan Lozano183a3212019-10-18 14:18:45 -0700540var _ cc.LinkableInterface = (*Module)(nil)
541
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542func (mod *Module) Init() android.Module {
543 mod.AddProperties(&mod.Properties)
544
545 if mod.compiler != nil {
546 mod.AddProperties(mod.compiler.compilerProps()...)
547 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400548 if mod.coverage != nil {
549 mod.AddProperties(mod.coverage.props()...)
550 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200551 if mod.clippy != nil {
552 mod.AddProperties(mod.clippy.props()...)
553 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400554 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700555 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400556 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400557
Ivan Lozanoffee3342019-08-27 12:03:00 -0700558 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
559
560 android.InitDefaultableModule(mod)
561
Ivan Lozanode252912019-09-06 15:29:52 -0700562 // Explicitly disable unsupported targets.
563 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
564 disableTargets := struct {
565 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700566 Linux_bionic struct {
567 Enabled *bool
568 }
569 }
570 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700571 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
572
573 ctx.AppendProperties(&disableTargets)
574 })
575
Ivan Lozanoffee3342019-08-27 12:03:00 -0700576 return mod
577}
578
579func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
580 return &Module{
581 hod: hod,
582 multilib: multilib,
583 }
584}
585func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
586 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400587 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200588 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589 return module
590}
591
592type ModuleContext interface {
593 android.ModuleContext
594 ModuleContextIntf
595}
596
597type BaseModuleContext interface {
598 android.BaseModuleContext
599 ModuleContextIntf
600}
601
602type DepsContext interface {
603 android.BottomUpMutatorContext
604 ModuleContextIntf
605}
606
607type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200608 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700609 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610}
611
612type depsContext struct {
613 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700614}
615
616type moduleContext struct {
617 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700618}
619
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200620type baseModuleContext struct {
621 android.BaseModuleContext
622}
623
624func (ctx *moduleContext) RustModule() *Module {
625 return ctx.Module().(*Module)
626}
627
628func (ctx *moduleContext) toolchain() config.Toolchain {
629 return ctx.RustModule().toolchain(ctx)
630}
631
632func (ctx *depsContext) RustModule() *Module {
633 return ctx.Module().(*Module)
634}
635
636func (ctx *depsContext) toolchain() config.Toolchain {
637 return ctx.RustModule().toolchain(ctx)
638}
639
640func (ctx *baseModuleContext) RustModule() *Module {
641 return ctx.Module().(*Module)
642}
643
644func (ctx *baseModuleContext) toolchain() config.Toolchain {
645 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400646}
647
648func (mod *Module) nativeCoverage() bool {
649 return mod.compiler != nil && mod.compiler.nativeCoverage()
650}
651
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
653 if mod.cachedToolchain == nil {
654 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
655 }
656 return mod.cachedToolchain
657}
658
659func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
660}
661
662func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
663 ctx := &moduleContext{
664 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700666
667 toolchain := mod.toolchain(ctx)
668
669 if !toolchain.Supported() {
670 // This toolchain's unsupported, there's nothing to do for this mod.
671 return
672 }
673
674 deps := mod.depsToPaths(ctx)
675 flags := Flags{
676 Toolchain: toolchain,
677 }
678
679 if mod.compiler != nil {
680 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400681 }
682 if mod.coverage != nil {
683 flags, deps = mod.coverage.flags(ctx, flags, deps)
684 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200685 if mod.clippy != nil {
686 flags, deps = mod.clippy.flags(ctx, flags, deps)
687 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400688
Andrei Homescuc7767922020-08-05 06:36:19 -0700689 // SourceProvider needs to call GenerateSource() before compiler calls compile() so it can provide the source.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400690 // TODO(b/162588681) This shouldn't have to run for every variant.
691 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700692 generatedFile := mod.sourceProvider.GenerateSource(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400693 mod.generatedFile = android.OptionalPathForPath(generatedFile)
694 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
695 }
696
697 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700698 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400699
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400701 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400702 mod.compiler.install(ctx, mod.outputFile.Path())
703 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704 }
705}
706
707func (mod *Module) deps(ctx DepsContext) Deps {
708 deps := Deps{}
709
710 if mod.compiler != nil {
711 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400712 }
713 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700714 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715 }
716
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400717 if mod.coverage != nil {
718 deps = mod.coverage.deps(ctx, deps)
719 }
720
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
722 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700723 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
725 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
726 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
727
728 return deps
729
730}
731
Ivan Lozanoffee3342019-08-27 12:03:00 -0700732type dependencyTag struct {
733 blueprint.BaseDependencyTag
734 name string
735 library bool
736 proc_macro bool
737}
738
739var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400740 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
741 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
742 dylibDepTag = dependencyTag{name: "dylib", library: true}
743 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
744 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700745)
746
Matthew Maurer0f003b12020-06-29 14:34:06 -0700747type autoDep struct {
748 variation string
749 depTag dependencyTag
750}
751
752var (
753 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
754 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
755)
756
757type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400758 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700759}
760
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400761func (mod *Module) begin(ctx BaseModuleContext) {
762 if mod.coverage != nil {
763 mod.coverage.begin(ctx)
764 }
765}
766
Ivan Lozanoffee3342019-08-27 12:03:00 -0700767func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
768 var depPaths PathDeps
769
770 directRlibDeps := []*Module{}
771 directDylibDeps := []*Module{}
772 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700773 directSharedLibDeps := [](cc.LinkableInterface){}
774 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400775 directSrcProvidersDeps := []*Module{}
776 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777
778 ctx.VisitDirectDeps(func(dep android.Module) {
779 depName := ctx.OtherModuleName(dep)
780 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400781 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700783
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784 switch depTag {
785 case dylibDepTag:
786 dylib, ok := rustDep.compiler.(libraryInterface)
787 if !ok || !dylib.dylib() {
788 ctx.ModuleErrorf("mod %q not an dylib library", depName)
789 return
790 }
791 directDylibDeps = append(directDylibDeps, rustDep)
792 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
793 case rlibDepTag:
794 rlib, ok := rustDep.compiler.(libraryInterface)
795 if !ok || !rlib.rlib() {
796 ctx.ModuleErrorf("mod %q not an rlib library", depName)
797 return
798 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400799 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800 directRlibDeps = append(directRlibDeps, rustDep)
801 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
802 case procMacroDepTag:
803 directProcMacroDeps = append(directProcMacroDeps, rustDep)
804 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400805 case android.SourceDepTag:
806 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
807 // OS/Arch variant is used.
808 var helper string
809 if ctx.Host() {
810 helper = "missing 'host_supported'?"
811 } else {
812 helper = "device module defined?"
813 }
814
815 if dep.Target().Os != ctx.Os() {
816 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
817 return
818 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
819 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
820 return
821 }
822 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 }
824
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400825 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
826 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700827 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400829 depPaths.linkObjects = append(depPaths.linkObjects, lib.exportedLinkObjects()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 }
831
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400833 linkFile := rustDep.outputFile
834 if !linkFile.Valid() {
835 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
836 depName, ctx.ModuleName())
837 return
838 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700840 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
841 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842 }
843 }
844
Ivan Lozano89435d12020-07-31 11:01:18 -0400845 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700846 //Handle C dependencies
847 if _, ok := ccDep.(*Module); !ok {
848 if ccDep.Module().Target().Os != ctx.Os() {
849 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
850 return
851 }
852 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
853 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
854 return
855 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700856 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400857 linkObject := ccDep.OutputFile()
858 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500859
Ivan Lozano2093af22020-08-25 12:48:19 -0400860 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
862 }
863
864 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700865 switch {
866 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700867 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400868 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400869 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
870 if mod, ok := ccDep.(*cc.Module); ok {
871 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
872 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
873 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400874 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700875 directStaticLibDeps = append(directStaticLibDeps, ccDep)
876 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700877 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400879 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400880 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
881 if mod, ok := ccDep.(*cc.Module); ok {
882 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
883 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
884 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885 directSharedLibDeps = append(directSharedLibDeps, ccDep)
886 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
887 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700888 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400889 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700890 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400891 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892 }
893
894 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700895 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
896 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400897 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400900
901 if srcDep, ok := dep.(android.SourceFileProducer); ok {
902 switch depTag {
903 case android.SourceDepTag:
904 // These are usually genrules which don't have per-target variants.
905 directSrcDeps = append(directSrcDeps, srcDep)
906 }
907 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700908 })
909
910 var rlibDepFiles RustLibraries
911 for _, dep := range directRlibDeps {
912 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
913 }
914 var dylibDepFiles RustLibraries
915 for _, dep := range directDylibDeps {
916 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
917 }
918 var procMacroDepFiles RustLibraries
919 for _, dep := range directProcMacroDeps {
920 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
921 }
922
923 var staticLibDepFiles android.Paths
924 for _, dep := range directStaticLibDeps {
925 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
926 }
927
928 var sharedLibDepFiles android.Paths
929 for _, dep := range directSharedLibDeps {
930 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
931 }
932
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400933 var srcProviderDepFiles android.Paths
934 for _, dep := range directSrcProvidersDeps {
935 srcs, _ := dep.OutputFiles("")
936 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
937 }
938 for _, dep := range directSrcDeps {
939 srcs := dep.Srcs()
940 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
941 }
942
Ivan Lozanoffee3342019-08-27 12:03:00 -0700943 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
944 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
945 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
946 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
947 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400948 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949
950 // Dedup exported flags from dependencies
951 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
952 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400953 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
954 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
955 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956
957 return depPaths
958}
959
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800960func (mod *Module) InstallInData() bool {
961 if mod.compiler == nil {
962 return false
963 }
964 return mod.compiler.inData()
965}
966
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967func linkPathFromFilePath(filepath android.Path) string {
968 return strings.Split(filepath.String(), filepath.Base())[0]
969}
Ivan Lozanod648c432020-02-06 12:05:10 -0500970
Ivan Lozanoffee3342019-08-27 12:03:00 -0700971func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
972 ctx := &depsContext{
973 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700975
976 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700977 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900978 if cc.VersionVariantAvailable(mod) {
979 commonDepVariations = append(commonDepVariations,
980 blueprint.Variation{Mutator: "version", Variation: ""})
981 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700983 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800984 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700985 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700986 actx.AddVariationDependencies(
987 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400988 {Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700989 rlibDepTag, deps.Rlibs...)
990 actx.AddVariationDependencies(
991 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400992 {Mutator: "rust_libraries", Variation: "dylib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700993 dylibDepTag, deps.Dylibs...)
994
Ivan Lozano042504f2020-08-18 14:31:23 -0400995 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
996 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700997 actx.AddVariationDependencies(
998 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400999 {Mutator: "rust_libraries", Variation: autoDep.variation}}...),
Matthew Maurer0f003b12020-06-29 14:34:06 -07001000 autoDep.depTag, deps.Rustlibs...)
1001 }
1002
Ivan Lozano52767be2019-10-18 14:49:46 -07001003 actx.AddVariationDependencies(append(commonDepVariations,
1004 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001005 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001006 actx.AddVariationDependencies(append(commonDepVariations,
1007 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001008 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001009
Dan Albert92fe7402020-07-15 13:33:30 -07001010 crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001011 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001012 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001013 }
1014 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001015 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001016 }
1017
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001018 if mod.sourceProvider != nil {
1019 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1020 bindgen.Properties.Custom_bindgen != "" {
1021 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1022 bindgen.Properties.Custom_bindgen)
1023 }
1024 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001025 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001026 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027}
1028
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001029func BeginMutator(ctx android.BottomUpMutatorContext) {
1030 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1031 mod.beginMutator(ctx)
1032 }
1033}
1034
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001035func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1036 ctx := &baseModuleContext{
1037 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001038 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001039
1040 mod.begin(ctx)
1041}
1042
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043func (mod *Module) Name() string {
1044 name := mod.ModuleBase.Name()
1045 if p, ok := mod.compiler.(interface {
1046 Name(string) string
1047 }); ok {
1048 name = p.Name(name)
1049 }
1050 return name
1051}
1052
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001053func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001054 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001055 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001056 }
1057}
1058
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001059var _ android.HostToolProvider = (*Module)(nil)
1060
1061func (mod *Module) HostToolPath() android.OptionalPath {
1062 if !mod.Host() {
1063 return android.OptionalPath{}
1064 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001065 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1066 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001067 }
1068 return android.OptionalPath{}
1069}
1070
Ivan Lozanoffee3342019-08-27 12:03:00 -07001071var Bool = proptools.Bool
1072var BoolDefault = proptools.BoolDefault
1073var String = proptools.String
1074var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001075
1076var _ android.OutputFileProducer = (*Module)(nil)