blob: 84728df88f7c54caa24ca49e568c9a06723a90f2 [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"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020026 cc_config "android/soong/cc/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027 "android/soong/rust/config"
28)
29
30var pctx = android.NewPackageContext("android/soong/rust")
31
32func init() {
33 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070034
35 android.AddNeverAllowRules(
36 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070037 NotIn(config.RustAllowedPaths...).
38 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070039
40 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040043 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070044 })
45 pctx.Import("android/soong/rust/config")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040046 pctx.ImportAs("ccConfig", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070047}
48
49type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040050 GlobalRustFlags []string // Flags that apply globally to rust
51 GlobalLinkFlags []string // Flags that apply globally to linker
52 RustFlags []string // Flags that apply to rust
53 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020054 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070055 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040056 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020057 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070058}
59
60type BaseProperties struct {
61 AndroidMkRlibs []string
62 AndroidMkDylibs []string
63 AndroidMkProcMacroLibs []string
64 AndroidMkSharedLibs []string
65 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040066
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040067 SubName string `blueprint:"mutated"`
68
Ivan Lozano43845682020-07-09 21:03:28 -040069 PreventInstall bool
70 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070071}
72
73type Module struct {
74 android.ModuleBase
75 android.DefaultableModuleBase
76
77 Properties BaseProperties
78
79 hod android.HostOrDeviceSupported
80 multilib android.Multilib
81
82 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040083 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020084 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070085 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040086 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070087 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040088
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040089 outputFile android.OptionalPath
90 generatedFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
Ivan Lozano43845682020-07-09 21:03:28 -040093func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
94 switch tag {
95 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070096 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -040097 return mod.sourceProvider.Srcs(), nil
98 } else {
99 if mod.outputFile.Valid() {
100 return android.Paths{mod.outputFile.Path()}, nil
101 }
102 return android.Paths{}, nil
103 }
104 default:
105 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
106 }
107}
108
Colin Cross7228ecd2019-11-18 16:00:16 -0800109var _ android.ImageInterface = (*Module)(nil)
110
111func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
112
113func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
114 return true
115}
116
Yifan Hong1b3348d2020-01-21 15:53:22 -0800117func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
118 return mod.InRamdisk()
119}
120
Colin Cross7228ecd2019-11-18 16:00:16 -0800121func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
122 return mod.InRecovery()
123}
124
125func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
126 return nil
127}
128
129func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
130}
131
Ivan Lozano52767be2019-10-18 14:49:46 -0700132func (mod *Module) BuildStubs() bool {
133 return false
134}
135
136func (mod *Module) HasStubsVariants() bool {
137 return false
138}
139
140func (mod *Module) SelectedStl() string {
141 return ""
142}
143
Ivan Lozano2b262972019-11-21 12:30:50 -0800144func (mod *Module) NonCcVariants() bool {
145 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400146 if _, ok := mod.compiler.(libraryInterface); ok {
147 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800148 }
149 }
150 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
151}
152
Ivan Lozano52767be2019-10-18 14:49:46 -0700153func (mod *Module) ApiLevel() string {
154 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
155}
156
157func (mod *Module) Static() bool {
158 if mod.compiler != nil {
159 if library, ok := mod.compiler.(libraryInterface); ok {
160 return library.static()
161 }
162 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400163 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700164}
165
166func (mod *Module) Shared() bool {
167 if mod.compiler != nil {
168 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400169 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700170 }
171 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400172 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700173}
174
175func (mod *Module) Toc() android.OptionalPath {
176 if mod.compiler != nil {
177 if _, ok := mod.compiler.(libraryInterface); ok {
178 return android.OptionalPath{}
179 }
180 }
181 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
182}
183
Yifan Hong1b3348d2020-01-21 15:53:22 -0800184func (mod *Module) OnlyInRamdisk() bool {
185 return false
186}
187
Ivan Lozano52767be2019-10-18 14:49:46 -0700188func (mod *Module) OnlyInRecovery() bool {
189 return false
190}
191
Colin Crossc511bc52020-04-07 16:50:32 +0000192func (mod *Module) UseSdk() bool {
193 return false
194}
195
Ivan Lozano52767be2019-10-18 14:49:46 -0700196func (mod *Module) UseVndk() bool {
197 return false
198}
199
200func (mod *Module) MustUseVendorVariant() bool {
201 return false
202}
203
204func (mod *Module) IsVndk() bool {
205 return false
206}
207
208func (mod *Module) HasVendorVariant() bool {
209 return false
210}
211
212func (mod *Module) SdkVersion() string {
213 return ""
214}
215
Colin Crossc511bc52020-04-07 16:50:32 +0000216func (mod *Module) AlwaysSdk() bool {
217 return false
218}
219
Jiyong Park2286afd2020-06-16 21:58:53 +0900220func (mod *Module) IsSdkVariant() bool {
221 return false
222}
223
Ivan Lozano52767be2019-10-18 14:49:46 -0700224func (mod *Module) ToolchainLibrary() bool {
225 return false
226}
227
228func (mod *Module) NdkPrebuiltStl() bool {
229 return false
230}
231
232func (mod *Module) StubDecorator() bool {
233 return false
234}
235
Ivan Lozanoffee3342019-08-27 12:03:00 -0700236type Deps struct {
237 Dylibs []string
238 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700239 Rustlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700240 ProcMacros []string
241 SharedLibs []string
242 StaticLibs []string
243
244 CrtBegin, CrtEnd string
245}
246
247type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400248 DyLibs RustLibraries
249 RLibs RustLibraries
250 SharedLibs android.Paths
251 StaticLibs android.Paths
252 ProcMacros RustLibraries
253 linkDirs []string
254 depFlags []string
255 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700257
Ivan Lozano45901ed2020-07-24 16:05:01 -0400258 // Used by bindgen modules which call clang
259 depClangFlags []string
260 depIncludePaths android.Paths
261 depSystemIncludePaths android.Paths
262
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400263 coverageFiles android.Paths
264
Ivan Lozanof1c84332019-09-20 11:00:37 -0700265 CrtBegin android.OptionalPath
266 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700267
268 // Paths to generated source files
269 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700270}
271
272type RustLibraries []RustLibrary
273
274type RustLibrary struct {
275 Path android.Path
276 CrateName string
277}
278
279type compiler interface {
280 compilerFlags(ctx ModuleContext, flags Flags) Flags
281 compilerProps() []interface{}
282 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
283 compilerDeps(ctx DepsContext, deps Deps) Deps
284 crateName() string
285
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800286 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200287 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700288 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400289
290 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400291
292 Disabled() bool
293 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400294
295 static() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400296}
297
Matthew Maurerbb3add12020-06-25 09:34:12 -0700298type exportedFlagsProducer interface {
299 exportedLinkDirs() []string
300 exportedDepFlags() []string
Ivan Lozano2093af22020-08-25 12:48:19 -0400301 exportedLinkObjects() []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700302 exportLinkDirs(...string)
303 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400304 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700305}
306
307type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400308 depFlags []string
309 linkDirs []string
310 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700311}
312
313func (flagExporter *flagExporter) exportedLinkDirs() []string {
314 return flagExporter.linkDirs
315}
316
317func (flagExporter *flagExporter) exportedDepFlags() []string {
318 return flagExporter.depFlags
319}
320
Ivan Lozano2093af22020-08-25 12:48:19 -0400321func (flagExporter *flagExporter) exportedLinkObjects() []string {
322 return flagExporter.linkObjects
323}
324
Matthew Maurerbb3add12020-06-25 09:34:12 -0700325func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
326 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
327}
328
329func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
330 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
331}
332
Ivan Lozano2093af22020-08-25 12:48:19 -0400333func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
334 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
335}
336
Matthew Maurerbb3add12020-06-25 09:34:12 -0700337var _ exportedFlagsProducer = (*flagExporter)(nil)
338
339func NewFlagExporter() *flagExporter {
340 return &flagExporter{
Ivan Lozano2093af22020-08-25 12:48:19 -0400341 depFlags: []string{},
342 linkDirs: []string{},
343 linkObjects: []string{},
Matthew Maurerbb3add12020-06-25 09:34:12 -0700344 }
345}
346
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400347func (mod *Module) isCoverageVariant() bool {
348 return mod.coverage.Properties.IsCoverageVariant
349}
350
351var _ cc.Coverage = (*Module)(nil)
352
353func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
354 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
355}
356
357func (mod *Module) PreventInstall() {
358 mod.Properties.PreventInstall = true
359}
360
361func (mod *Module) HideFromMake() {
362 mod.Properties.HideFromMake = true
363}
364
365func (mod *Module) MarkAsCoverageVariant(coverage bool) {
366 mod.coverage.Properties.IsCoverageVariant = coverage
367}
368
369func (mod *Module) EnableCoverageIfNeeded() {
370 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371}
372
373func defaultsFactory() android.Module {
374 return DefaultsFactory()
375}
376
377type Defaults struct {
378 android.ModuleBase
379 android.DefaultsModuleBase
380}
381
382func DefaultsFactory(props ...interface{}) android.Module {
383 module := &Defaults{}
384
385 module.AddProperties(props...)
386 module.AddProperties(
387 &BaseProperties{},
388 &BaseCompilerProperties{},
389 &BinaryCompilerProperties{},
390 &LibraryCompilerProperties{},
391 &ProcMacroCompilerProperties{},
392 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400393 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700394 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400395 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200396 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397 )
398
399 android.InitDefaultsModule(module)
400 return module
401}
402
403func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700404 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700405}
406
Ivan Lozano183a3212019-10-18 14:18:45 -0700407func (mod *Module) CcLibrary() bool {
408 if mod.compiler != nil {
409 if _, ok := mod.compiler.(*libraryDecorator); ok {
410 return true
411 }
412 }
413 return false
414}
415
416func (mod *Module) CcLibraryInterface() bool {
417 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400418 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
419 // VariantIs{Static,Shared} is set.
420 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700421 return true
422 }
423 }
424 return false
425}
426
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800427func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700428 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700429 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800430 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700431 }
432 }
433 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
434}
435
436func (mod *Module) SetStatic() {
437 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700438 if library, ok := mod.compiler.(libraryInterface); ok {
439 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700440 return
441 }
442 }
443 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
444}
445
446func (mod *Module) SetShared() {
447 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700448 if library, ok := mod.compiler.(libraryInterface); ok {
449 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700450 return
451 }
452 }
453 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
454}
455
456func (mod *Module) SetBuildStubs() {
457 panic("SetBuildStubs not yet implemented for rust modules")
458}
459
460func (mod *Module) SetStubsVersions(string) {
461 panic("SetStubsVersions not yet implemented for rust modules")
462}
463
Jooyung Han03b51852020-02-26 22:45:42 +0900464func (mod *Module) StubsVersion() string {
465 panic("SetStubsVersions not yet implemented for rust modules")
466}
467
Ivan Lozano183a3212019-10-18 14:18:45 -0700468func (mod *Module) BuildStaticVariant() bool {
469 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700470 if library, ok := mod.compiler.(libraryInterface); ok {
471 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700472 }
473 }
474 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
475}
476
477func (mod *Module) BuildSharedVariant() bool {
478 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700479 if library, ok := mod.compiler.(libraryInterface); ok {
480 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700481 }
482 }
483 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
484}
485
486// Rust module deps don't have a link order (?)
487func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
488
489func (mod *Module) GetDepsInLinkOrder() []android.Path {
490 return []android.Path{}
491}
492
493func (mod *Module) GetStaticVariant() cc.LinkableInterface {
494 return nil
495}
496
497func (mod *Module) Module() android.Module {
498 return mod
499}
500
501func (mod *Module) StubsVersions() []string {
502 // For now, Rust has no stubs versions.
503 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700504 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700505 return []string{}
506 }
507 }
508 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
509}
510
511func (mod *Module) OutputFile() android.OptionalPath {
512 return mod.outputFile
513}
514
515func (mod *Module) InRecovery() bool {
516 // For now, Rust has no notion of the recovery image
517 return false
518}
519func (mod *Module) HasStaticVariant() bool {
520 if mod.GetStaticVariant() != nil {
521 return true
522 }
523 return false
524}
525
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400526func (mod *Module) CoverageFiles() android.Paths {
527 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700528 if !mod.compiler.nativeCoverage() {
529 return android.Paths{}
530 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400531 if library, ok := mod.compiler.(*libraryDecorator); ok {
532 if library.coverageFile != nil {
533 return android.Paths{library.coverageFile}
534 }
535 return android.Paths{}
536 }
537 }
538 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
539}
540
Ivan Lozano183a3212019-10-18 14:18:45 -0700541var _ cc.LinkableInterface = (*Module)(nil)
542
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543func (mod *Module) Init() android.Module {
544 mod.AddProperties(&mod.Properties)
545
546 if mod.compiler != nil {
547 mod.AddProperties(mod.compiler.compilerProps()...)
548 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400549 if mod.coverage != nil {
550 mod.AddProperties(mod.coverage.props()...)
551 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200552 if mod.clippy != nil {
553 mod.AddProperties(mod.clippy.props()...)
554 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400555 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700556 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400557 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400558
Ivan Lozanoffee3342019-08-27 12:03:00 -0700559 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
560
561 android.InitDefaultableModule(mod)
562
Ivan Lozanode252912019-09-06 15:29:52 -0700563 // Explicitly disable unsupported targets.
564 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
565 disableTargets := struct {
566 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700567 Linux_bionic struct {
568 Enabled *bool
569 }
570 }
571 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700572 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
573
574 ctx.AppendProperties(&disableTargets)
575 })
576
Ivan Lozanoffee3342019-08-27 12:03:00 -0700577 return mod
578}
579
580func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
581 return &Module{
582 hod: hod,
583 multilib: multilib,
584 }
585}
586func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
587 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400588 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200589 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590 return module
591}
592
593type ModuleContext interface {
594 android.ModuleContext
595 ModuleContextIntf
596}
597
598type BaseModuleContext interface {
599 android.BaseModuleContext
600 ModuleContextIntf
601}
602
603type DepsContext interface {
604 android.BottomUpMutatorContext
605 ModuleContextIntf
606}
607
608type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200609 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611}
612
613type depsContext struct {
614 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615}
616
617type moduleContext struct {
618 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700619}
620
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200621type baseModuleContext struct {
622 android.BaseModuleContext
623}
624
625func (ctx *moduleContext) RustModule() *Module {
626 return ctx.Module().(*Module)
627}
628
629func (ctx *moduleContext) toolchain() config.Toolchain {
630 return ctx.RustModule().toolchain(ctx)
631}
632
633func (ctx *depsContext) RustModule() *Module {
634 return ctx.Module().(*Module)
635}
636
637func (ctx *depsContext) toolchain() config.Toolchain {
638 return ctx.RustModule().toolchain(ctx)
639}
640
641func (ctx *baseModuleContext) RustModule() *Module {
642 return ctx.Module().(*Module)
643}
644
645func (ctx *baseModuleContext) toolchain() config.Toolchain {
646 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400647}
648
649func (mod *Module) nativeCoverage() bool {
650 return mod.compiler != nil && mod.compiler.nativeCoverage()
651}
652
Ivan Lozanoffee3342019-08-27 12:03:00 -0700653func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
654 if mod.cachedToolchain == nil {
655 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
656 }
657 return mod.cachedToolchain
658}
659
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200660func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
661 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
662}
663
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
665}
666
667func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
668 ctx := &moduleContext{
669 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700671
672 toolchain := mod.toolchain(ctx)
673
674 if !toolchain.Supported() {
675 // This toolchain's unsupported, there's nothing to do for this mod.
676 return
677 }
678
679 deps := mod.depsToPaths(ctx)
680 flags := Flags{
681 Toolchain: toolchain,
682 }
683
684 if mod.compiler != nil {
685 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400686 }
687 if mod.coverage != nil {
688 flags, deps = mod.coverage.flags(ctx, flags, deps)
689 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200690 if mod.clippy != nil {
691 flags, deps = mod.clippy.flags(ctx, flags, deps)
692 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400693
Andrei Homescuc7767922020-08-05 06:36:19 -0700694 // SourceProvider needs to call GenerateSource() before compiler calls compile() so it can provide the source.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400695 // TODO(b/162588681) This shouldn't have to run for every variant.
696 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700697 generatedFile := mod.sourceProvider.GenerateSource(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400698 mod.generatedFile = android.OptionalPathForPath(generatedFile)
699 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
700 }
701
702 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700703 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400704
Ivan Lozanoffee3342019-08-27 12:03:00 -0700705 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400706 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200707 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400708 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709 }
710}
711
712func (mod *Module) deps(ctx DepsContext) Deps {
713 deps := Deps{}
714
715 if mod.compiler != nil {
716 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400717 }
718 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700719 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700720 }
721
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400722 if mod.coverage != nil {
723 deps = mod.coverage.deps(ctx, deps)
724 }
725
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
727 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700728 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
730 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
731 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
732
733 return deps
734
735}
736
Ivan Lozanoffee3342019-08-27 12:03:00 -0700737type dependencyTag struct {
738 blueprint.BaseDependencyTag
739 name string
740 library bool
741 proc_macro bool
742}
743
744var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400745 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
746 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
747 dylibDepTag = dependencyTag{name: "dylib", library: true}
748 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
749 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700750)
751
Matthew Maurer0f003b12020-06-29 14:34:06 -0700752type autoDep struct {
753 variation string
754 depTag dependencyTag
755}
756
757var (
758 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
759 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
760)
761
762type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400763 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700764}
765
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766func (mod *Module) begin(ctx BaseModuleContext) {
767 if mod.coverage != nil {
768 mod.coverage.begin(ctx)
769 }
770}
771
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
773 var depPaths PathDeps
774
775 directRlibDeps := []*Module{}
776 directDylibDeps := []*Module{}
777 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700778 directSharedLibDeps := [](cc.LinkableInterface){}
779 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400780 directSrcProvidersDeps := []*Module{}
781 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782
783 ctx.VisitDirectDeps(func(dep android.Module) {
784 depName := ctx.OtherModuleName(dep)
785 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400786 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700788
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789 switch depTag {
790 case dylibDepTag:
791 dylib, ok := rustDep.compiler.(libraryInterface)
792 if !ok || !dylib.dylib() {
793 ctx.ModuleErrorf("mod %q not an dylib library", depName)
794 return
795 }
796 directDylibDeps = append(directDylibDeps, rustDep)
797 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
798 case rlibDepTag:
799 rlib, ok := rustDep.compiler.(libraryInterface)
800 if !ok || !rlib.rlib() {
801 ctx.ModuleErrorf("mod %q not an rlib library", depName)
802 return
803 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400804 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 directRlibDeps = append(directRlibDeps, rustDep)
806 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
807 case procMacroDepTag:
808 directProcMacroDeps = append(directProcMacroDeps, rustDep)
809 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400810 case android.SourceDepTag:
811 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
812 // OS/Arch variant is used.
813 var helper string
814 if ctx.Host() {
815 helper = "missing 'host_supported'?"
816 } else {
817 helper = "device module defined?"
818 }
819
820 if dep.Target().Os != ctx.Os() {
821 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
822 return
823 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
824 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
825 return
826 }
827 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828 }
829
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400830 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
831 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700832 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400834 depPaths.linkObjects = append(depPaths.linkObjects, lib.exportedLinkObjects()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700835 }
836
Ivan Lozanoffee3342019-08-27 12:03:00 -0700837 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400838 linkFile := rustDep.outputFile
839 if !linkFile.Valid() {
840 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
841 depName, ctx.ModuleName())
842 return
843 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700845 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
846 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700847 }
848 }
849
Ivan Lozano89435d12020-07-31 11:01:18 -0400850 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700851 //Handle C dependencies
852 if _, ok := ccDep.(*Module); !ok {
853 if ccDep.Module().Target().Os != ctx.Os() {
854 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
855 return
856 }
857 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
858 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
859 return
860 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700861 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400862 linkObject := ccDep.OutputFile()
863 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500864
Ivan Lozano2093af22020-08-25 12:48:19 -0400865 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
867 }
868
869 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700870 switch {
871 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400873 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400874 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
875 if mod, ok := ccDep.(*cc.Module); ok {
876 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
877 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
878 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400879 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880 directStaticLibDeps = append(directStaticLibDeps, ccDep)
881 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700882 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400884 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400885 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
886 if mod, ok := ccDep.(*cc.Module); ok {
887 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
888 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
889 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 directSharedLibDeps = append(directSharedLibDeps, ccDep)
891 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
892 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700893 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400894 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700895 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400896 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897 }
898
899 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700900 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
901 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400902 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700903 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400905
906 if srcDep, ok := dep.(android.SourceFileProducer); ok {
907 switch depTag {
908 case android.SourceDepTag:
909 // These are usually genrules which don't have per-target variants.
910 directSrcDeps = append(directSrcDeps, srcDep)
911 }
912 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913 })
914
915 var rlibDepFiles RustLibraries
916 for _, dep := range directRlibDeps {
917 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
918 }
919 var dylibDepFiles RustLibraries
920 for _, dep := range directDylibDeps {
921 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
922 }
923 var procMacroDepFiles RustLibraries
924 for _, dep := range directProcMacroDeps {
925 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
926 }
927
928 var staticLibDepFiles android.Paths
929 for _, dep := range directStaticLibDeps {
930 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
931 }
932
933 var sharedLibDepFiles android.Paths
934 for _, dep := range directSharedLibDeps {
935 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
936 }
937
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400938 var srcProviderDepFiles android.Paths
939 for _, dep := range directSrcProvidersDeps {
940 srcs, _ := dep.OutputFiles("")
941 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
942 }
943 for _, dep := range directSrcDeps {
944 srcs := dep.Srcs()
945 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
946 }
947
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
949 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
950 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
951 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
952 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400953 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700954
955 // Dedup exported flags from dependencies
956 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
957 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400958 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
959 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
960 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961
962 return depPaths
963}
964
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800965func (mod *Module) InstallInData() bool {
966 if mod.compiler == nil {
967 return false
968 }
969 return mod.compiler.inData()
970}
971
Ivan Lozanoffee3342019-08-27 12:03:00 -0700972func linkPathFromFilePath(filepath android.Path) string {
973 return strings.Split(filepath.String(), filepath.Base())[0]
974}
Ivan Lozanod648c432020-02-06 12:05:10 -0500975
Ivan Lozanoffee3342019-08-27 12:03:00 -0700976func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
977 ctx := &depsContext{
978 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700980
981 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700982 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900983 if cc.VersionVariantAvailable(mod) {
984 commonDepVariations = append(commonDepVariations,
985 blueprint.Variation{Mutator: "version", Variation: ""})
986 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700987 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700988 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800989 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700991 actx.AddVariationDependencies(
992 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400993 {Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700994 rlibDepTag, deps.Rlibs...)
995 actx.AddVariationDependencies(
996 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400997 {Mutator: "rust_libraries", Variation: "dylib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700998 dylibDepTag, deps.Dylibs...)
999
Ivan Lozano042504f2020-08-18 14:31:23 -04001000 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1001 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001002 actx.AddVariationDependencies(
1003 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -04001004 {Mutator: "rust_libraries", Variation: autoDep.variation}}...),
Matthew Maurer0f003b12020-06-29 14:34:06 -07001005 autoDep.depTag, deps.Rustlibs...)
1006 }
1007
Ivan Lozano52767be2019-10-18 14:49:46 -07001008 actx.AddVariationDependencies(append(commonDepVariations,
1009 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001010 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001011 actx.AddVariationDependencies(append(commonDepVariations,
1012 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001013 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001014
Dan Albert92fe7402020-07-15 13:33:30 -07001015 crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001016 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001017 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001018 }
1019 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001020 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001021 }
1022
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001023 if mod.sourceProvider != nil {
1024 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1025 bindgen.Properties.Custom_bindgen != "" {
1026 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1027 bindgen.Properties.Custom_bindgen)
1028 }
1029 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001030 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001031 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032}
1033
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001034func BeginMutator(ctx android.BottomUpMutatorContext) {
1035 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1036 mod.beginMutator(ctx)
1037 }
1038}
1039
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001040func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1041 ctx := &baseModuleContext{
1042 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001043 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001044
1045 mod.begin(ctx)
1046}
1047
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048func (mod *Module) Name() string {
1049 name := mod.ModuleBase.Name()
1050 if p, ok := mod.compiler.(interface {
1051 Name(string) string
1052 }); ok {
1053 name = p.Name(name)
1054 }
1055 return name
1056}
1057
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001058func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001059 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001060 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001061 }
1062}
1063
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001064var _ android.HostToolProvider = (*Module)(nil)
1065
1066func (mod *Module) HostToolPath() android.OptionalPath {
1067 if !mod.Host() {
1068 return android.OptionalPath{}
1069 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001070 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1071 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001072 }
1073 return android.OptionalPath{}
1074}
1075
Ivan Lozanoffee3342019-08-27 12:03:00 -07001076var Bool = proptools.Bool
1077var BoolDefault = proptools.BoolDefault
1078var String = proptools.String
1079var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001080
1081var _ android.OutputFileProducer = (*Module)(nil)