blob: e5614770bff8a017e7cc671360e07e31a76c4f14 [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 Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040051 GlobalRustFlags []string // Flags that apply globally to rust
52 GlobalLinkFlags []string // Flags that apply globally to linker
53 RustFlags []string // Flags that apply to rust
54 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070056 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040057 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61type BaseProperties struct {
62 AndroidMkRlibs []string
63 AndroidMkDylibs []string
64 AndroidMkProcMacroLibs []string
65 AndroidMkSharedLibs []string
66 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040067
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040068 SubName string `blueprint:"mutated"`
69
Ivan Lozano43845682020-07-09 21:03:28 -040070 PreventInstall bool
71 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070072}
73
74type Module struct {
75 android.ModuleBase
76 android.DefaultableModuleBase
77
78 Properties BaseProperties
79
80 hod android.HostOrDeviceSupported
81 multilib android.Multilib
82
83 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040084 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020085 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070086 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040087 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070088 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040089
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040090 outputFile android.OptionalPath
91 generatedFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -070092}
93
Ivan Lozano43845682020-07-09 21:03:28 -040094func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
95 switch tag {
96 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070097 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -040098 return mod.sourceProvider.Srcs(), nil
99 } else {
100 if mod.outputFile.Valid() {
101 return android.Paths{mod.outputFile.Path()}, nil
102 }
103 return android.Paths{}, nil
104 }
105 default:
106 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
107 }
108}
109
Colin Cross7228ecd2019-11-18 16:00:16 -0800110var _ android.ImageInterface = (*Module)(nil)
111
112func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
113
114func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
115 return true
116}
117
Yifan Hong1b3348d2020-01-21 15:53:22 -0800118func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
119 return mod.InRamdisk()
120}
121
Colin Cross7228ecd2019-11-18 16:00:16 -0800122func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
123 return mod.InRecovery()
124}
125
126func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
127 return nil
128}
129
130func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
131}
132
Ivan Lozano52767be2019-10-18 14:49:46 -0700133func (mod *Module) BuildStubs() bool {
134 return false
135}
136
137func (mod *Module) HasStubsVariants() bool {
138 return false
139}
140
141func (mod *Module) SelectedStl() string {
142 return ""
143}
144
Ivan Lozano2b262972019-11-21 12:30:50 -0800145func (mod *Module) NonCcVariants() bool {
146 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400147 if _, ok := mod.compiler.(libraryInterface); ok {
148 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800149 }
150 }
151 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
152}
153
Ivan Lozano52767be2019-10-18 14:49:46 -0700154func (mod *Module) ApiLevel() string {
155 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
156}
157
158func (mod *Module) Static() bool {
159 if mod.compiler != nil {
160 if library, ok := mod.compiler.(libraryInterface); ok {
161 return library.static()
162 }
163 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400164 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700165}
166
167func (mod *Module) Shared() bool {
168 if mod.compiler != nil {
169 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400170 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700171 }
172 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400173 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700174}
175
176func (mod *Module) Toc() android.OptionalPath {
177 if mod.compiler != nil {
178 if _, ok := mod.compiler.(libraryInterface); ok {
179 return android.OptionalPath{}
180 }
181 }
182 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
183}
184
Yifan Hong1b3348d2020-01-21 15:53:22 -0800185func (mod *Module) OnlyInRamdisk() bool {
186 return false
187}
188
Ivan Lozano52767be2019-10-18 14:49:46 -0700189func (mod *Module) OnlyInRecovery() bool {
190 return false
191}
192
Colin Crossc511bc52020-04-07 16:50:32 +0000193func (mod *Module) UseSdk() bool {
194 return false
195}
196
Ivan Lozano52767be2019-10-18 14:49:46 -0700197func (mod *Module) UseVndk() bool {
198 return false
199}
200
201func (mod *Module) MustUseVendorVariant() bool {
202 return false
203}
204
205func (mod *Module) IsVndk() bool {
206 return false
207}
208
209func (mod *Module) HasVendorVariant() bool {
210 return false
211}
212
213func (mod *Module) SdkVersion() string {
214 return ""
215}
216
Colin Crossc511bc52020-04-07 16:50:32 +0000217func (mod *Module) AlwaysSdk() bool {
218 return false
219}
220
Jiyong Park2286afd2020-06-16 21:58:53 +0900221func (mod *Module) IsSdkVariant() bool {
222 return false
223}
224
Ivan Lozano52767be2019-10-18 14:49:46 -0700225func (mod *Module) ToolchainLibrary() bool {
226 return false
227}
228
229func (mod *Module) NdkPrebuiltStl() bool {
230 return false
231}
232
233func (mod *Module) StubDecorator() bool {
234 return false
235}
236
Ivan Lozanoffee3342019-08-27 12:03:00 -0700237type Deps struct {
238 Dylibs []string
239 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700240 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400241 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700242 ProcMacros []string
243 SharedLibs []string
244 StaticLibs []string
245
246 CrtBegin, CrtEnd string
247}
248
249type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400250 DyLibs RustLibraries
251 RLibs RustLibraries
252 SharedLibs android.Paths
253 StaticLibs android.Paths
254 ProcMacros RustLibraries
255 linkDirs []string
256 depFlags []string
257 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700259
Ivan Lozano45901ed2020-07-24 16:05:01 -0400260 // Used by bindgen modules which call clang
261 depClangFlags []string
262 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400263 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400264 depSystemIncludePaths android.Paths
265
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400266 coverageFiles android.Paths
267
Ivan Lozanof1c84332019-09-20 11:00:37 -0700268 CrtBegin android.OptionalPath
269 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700270
271 // Paths to generated source files
272 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700273}
274
275type RustLibraries []RustLibrary
276
277type RustLibrary struct {
278 Path android.Path
279 CrateName string
280}
281
282type compiler interface {
283 compilerFlags(ctx ModuleContext, flags Flags) Flags
284 compilerProps() []interface{}
285 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
286 compilerDeps(ctx DepsContext, deps Deps) Deps
287 crateName() string
288
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800289 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200290 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700291 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400292
293 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400294
295 Disabled() bool
296 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400297
Ivan Lozano2b081132020-09-08 12:46:52 -0400298 staticStd(ctx *depsContext) bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400299}
300
Matthew Maurerbb3add12020-06-25 09:34:12 -0700301type exportedFlagsProducer interface {
302 exportedLinkDirs() []string
303 exportedDepFlags() []string
Ivan Lozano2093af22020-08-25 12:48:19 -0400304 exportedLinkObjects() []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700305 exportLinkDirs(...string)
306 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400307 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700308}
309
310type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400311 depFlags []string
312 linkDirs []string
313 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700314}
315
316func (flagExporter *flagExporter) exportedLinkDirs() []string {
317 return flagExporter.linkDirs
318}
319
320func (flagExporter *flagExporter) exportedDepFlags() []string {
321 return flagExporter.depFlags
322}
323
Ivan Lozano2093af22020-08-25 12:48:19 -0400324func (flagExporter *flagExporter) exportedLinkObjects() []string {
325 return flagExporter.linkObjects
326}
327
Matthew Maurerbb3add12020-06-25 09:34:12 -0700328func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
329 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
330}
331
332func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
333 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
334}
335
Ivan Lozano2093af22020-08-25 12:48:19 -0400336func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
337 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
338}
339
Matthew Maurerbb3add12020-06-25 09:34:12 -0700340var _ exportedFlagsProducer = (*flagExporter)(nil)
341
342func NewFlagExporter() *flagExporter {
343 return &flagExporter{
Ivan Lozano2093af22020-08-25 12:48:19 -0400344 depFlags: []string{},
345 linkDirs: []string{},
346 linkObjects: []string{},
Matthew Maurerbb3add12020-06-25 09:34:12 -0700347 }
348}
349
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400350func (mod *Module) isCoverageVariant() bool {
351 return mod.coverage.Properties.IsCoverageVariant
352}
353
354var _ cc.Coverage = (*Module)(nil)
355
356func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
357 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
358}
359
360func (mod *Module) PreventInstall() {
361 mod.Properties.PreventInstall = true
362}
363
364func (mod *Module) HideFromMake() {
365 mod.Properties.HideFromMake = true
366}
367
368func (mod *Module) MarkAsCoverageVariant(coverage bool) {
369 mod.coverage.Properties.IsCoverageVariant = coverage
370}
371
372func (mod *Module) EnableCoverageIfNeeded() {
373 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700374}
375
376func defaultsFactory() android.Module {
377 return DefaultsFactory()
378}
379
380type Defaults struct {
381 android.ModuleBase
382 android.DefaultsModuleBase
383}
384
385func DefaultsFactory(props ...interface{}) android.Module {
386 module := &Defaults{}
387
388 module.AddProperties(props...)
389 module.AddProperties(
390 &BaseProperties{},
391 &BaseCompilerProperties{},
392 &BinaryCompilerProperties{},
393 &LibraryCompilerProperties{},
394 &ProcMacroCompilerProperties{},
395 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400396 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700397 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400398 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200399 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400 )
401
402 android.InitDefaultsModule(module)
403 return module
404}
405
406func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700407 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700408}
409
Ivan Lozano183a3212019-10-18 14:18:45 -0700410func (mod *Module) CcLibrary() bool {
411 if mod.compiler != nil {
412 if _, ok := mod.compiler.(*libraryDecorator); ok {
413 return true
414 }
415 }
416 return false
417}
418
419func (mod *Module) CcLibraryInterface() bool {
420 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400421 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
422 // VariantIs{Static,Shared} is set.
423 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700424 return true
425 }
426 }
427 return false
428}
429
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800430func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700431 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700432 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800433 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700434 }
435 }
436 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
437}
438
439func (mod *Module) SetStatic() {
440 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700441 if library, ok := mod.compiler.(libraryInterface); ok {
442 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700443 return
444 }
445 }
446 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
447}
448
449func (mod *Module) SetShared() {
450 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700451 if library, ok := mod.compiler.(libraryInterface); ok {
452 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700453 return
454 }
455 }
456 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
457}
458
459func (mod *Module) SetBuildStubs() {
460 panic("SetBuildStubs not yet implemented for rust modules")
461}
462
463func (mod *Module) SetStubsVersions(string) {
464 panic("SetStubsVersions not yet implemented for rust modules")
465}
466
Jooyung Han03b51852020-02-26 22:45:42 +0900467func (mod *Module) StubsVersion() string {
468 panic("SetStubsVersions not yet implemented for rust modules")
469}
470
Ivan Lozano183a3212019-10-18 14:18:45 -0700471func (mod *Module) BuildStaticVariant() bool {
472 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700473 if library, ok := mod.compiler.(libraryInterface); ok {
474 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700475 }
476 }
477 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
478}
479
480func (mod *Module) BuildSharedVariant() bool {
481 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700482 if library, ok := mod.compiler.(libraryInterface); ok {
483 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700484 }
485 }
486 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
487}
488
489// Rust module deps don't have a link order (?)
490func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
491
492func (mod *Module) GetDepsInLinkOrder() []android.Path {
493 return []android.Path{}
494}
495
496func (mod *Module) GetStaticVariant() cc.LinkableInterface {
497 return nil
498}
499
500func (mod *Module) Module() android.Module {
501 return mod
502}
503
504func (mod *Module) StubsVersions() []string {
505 // For now, Rust has no stubs versions.
506 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700507 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700508 return []string{}
509 }
510 }
511 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
512}
513
514func (mod *Module) OutputFile() android.OptionalPath {
515 return mod.outputFile
516}
517
518func (mod *Module) InRecovery() bool {
519 // For now, Rust has no notion of the recovery image
520 return false
521}
522func (mod *Module) HasStaticVariant() bool {
523 if mod.GetStaticVariant() != nil {
524 return true
525 }
526 return false
527}
528
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400529func (mod *Module) CoverageFiles() android.Paths {
530 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700531 if !mod.compiler.nativeCoverage() {
532 return android.Paths{}
533 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400534 if library, ok := mod.compiler.(*libraryDecorator); ok {
535 if library.coverageFile != nil {
536 return android.Paths{library.coverageFile}
537 }
538 return android.Paths{}
539 }
540 }
541 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
542}
543
Ivan Lozano183a3212019-10-18 14:18:45 -0700544var _ cc.LinkableInterface = (*Module)(nil)
545
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546func (mod *Module) Init() android.Module {
547 mod.AddProperties(&mod.Properties)
548
549 if mod.compiler != nil {
550 mod.AddProperties(mod.compiler.compilerProps()...)
551 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400552 if mod.coverage != nil {
553 mod.AddProperties(mod.coverage.props()...)
554 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200555 if mod.clippy != nil {
556 mod.AddProperties(mod.clippy.props()...)
557 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400558 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700559 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400560 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400561
Ivan Lozanoffee3342019-08-27 12:03:00 -0700562 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
563
564 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700565 return mod
566}
567
568func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
569 return &Module{
570 hod: hod,
571 multilib: multilib,
572 }
573}
574func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
575 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400576 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200577 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578 return module
579}
580
581type ModuleContext interface {
582 android.ModuleContext
583 ModuleContextIntf
584}
585
586type BaseModuleContext interface {
587 android.BaseModuleContext
588 ModuleContextIntf
589}
590
591type DepsContext interface {
592 android.BottomUpMutatorContext
593 ModuleContextIntf
594}
595
596type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200597 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599}
600
601type depsContext struct {
602 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700603}
604
605type moduleContext struct {
606 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607}
608
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200609type baseModuleContext struct {
610 android.BaseModuleContext
611}
612
613func (ctx *moduleContext) RustModule() *Module {
614 return ctx.Module().(*Module)
615}
616
617func (ctx *moduleContext) toolchain() config.Toolchain {
618 return ctx.RustModule().toolchain(ctx)
619}
620
621func (ctx *depsContext) RustModule() *Module {
622 return ctx.Module().(*Module)
623}
624
625func (ctx *depsContext) toolchain() config.Toolchain {
626 return ctx.RustModule().toolchain(ctx)
627}
628
629func (ctx *baseModuleContext) RustModule() *Module {
630 return ctx.Module().(*Module)
631}
632
633func (ctx *baseModuleContext) toolchain() config.Toolchain {
634 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400635}
636
637func (mod *Module) nativeCoverage() bool {
638 return mod.compiler != nil && mod.compiler.nativeCoverage()
639}
640
Ivan Lozanoffee3342019-08-27 12:03:00 -0700641func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
642 if mod.cachedToolchain == nil {
643 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
644 }
645 return mod.cachedToolchain
646}
647
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200648func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
649 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
650}
651
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
653}
654
655func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
656 ctx := &moduleContext{
657 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700658 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659
660 toolchain := mod.toolchain(ctx)
661
662 if !toolchain.Supported() {
663 // This toolchain's unsupported, there's nothing to do for this mod.
664 return
665 }
666
667 deps := mod.depsToPaths(ctx)
668 flags := Flags{
669 Toolchain: toolchain,
670 }
671
672 if mod.compiler != nil {
673 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400674 }
675 if mod.coverage != nil {
676 flags, deps = mod.coverage.flags(ctx, flags, deps)
677 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200678 if mod.clippy != nil {
679 flags, deps = mod.clippy.flags(ctx, flags, deps)
680 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400681
Andrei Homescuc7767922020-08-05 06:36:19 -0700682 // SourceProvider needs to call GenerateSource() before compiler calls compile() so it can provide the source.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400683 // TODO(b/162588681) This shouldn't have to run for every variant.
684 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700685 generatedFile := mod.sourceProvider.GenerateSource(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400686 mod.generatedFile = android.OptionalPathForPath(generatedFile)
687 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
688 }
689
690 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400692
Ivan Lozanoffee3342019-08-27 12:03:00 -0700693 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400694 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200695 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400696 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700697 }
698}
699
700func (mod *Module) deps(ctx DepsContext) Deps {
701 deps := Deps{}
702
703 if mod.compiler != nil {
704 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400705 }
706 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700707 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700708 }
709
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400710 if mod.coverage != nil {
711 deps = mod.coverage.deps(ctx, deps)
712 }
713
Ivan Lozanoffee3342019-08-27 12:03:00 -0700714 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
715 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700716 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
718 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
719 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
720
721 return deps
722
723}
724
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725type dependencyTag struct {
726 blueprint.BaseDependencyTag
727 name string
728 library bool
729 proc_macro bool
730}
731
732var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400733 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
734 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
735 dylibDepTag = dependencyTag{name: "dylib", library: true}
736 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
737 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738)
739
Matthew Maurer0f003b12020-06-29 14:34:06 -0700740type autoDep struct {
741 variation string
742 depTag dependencyTag
743}
744
745var (
746 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
747 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
748)
749
750type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400751 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700752}
753
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400754func (mod *Module) begin(ctx BaseModuleContext) {
755 if mod.coverage != nil {
756 mod.coverage.begin(ctx)
757 }
758}
759
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
761 var depPaths PathDeps
762
763 directRlibDeps := []*Module{}
764 directDylibDeps := []*Module{}
765 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700766 directSharedLibDeps := [](cc.LinkableInterface){}
767 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400768 directSrcProvidersDeps := []*Module{}
769 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700770
771 ctx.VisitDirectDeps(func(dep android.Module) {
772 depName := ctx.OtherModuleName(dep)
773 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400774 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700776
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777 switch depTag {
778 case dylibDepTag:
779 dylib, ok := rustDep.compiler.(libraryInterface)
780 if !ok || !dylib.dylib() {
781 ctx.ModuleErrorf("mod %q not an dylib library", depName)
782 return
783 }
784 directDylibDeps = append(directDylibDeps, rustDep)
785 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
786 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400787
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788 rlib, ok := rustDep.compiler.(libraryInterface)
789 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400790 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700791 return
792 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400793 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400795 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796 case procMacroDepTag:
797 directProcMacroDeps = append(directProcMacroDeps, rustDep)
798 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400799 case android.SourceDepTag:
800 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
801 // OS/Arch variant is used.
802 var helper string
803 if ctx.Host() {
804 helper = "missing 'host_supported'?"
805 } else {
806 helper = "device module defined?"
807 }
808
809 if dep.Target().Os != ctx.Os() {
810 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
811 return
812 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
813 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
814 return
815 }
816 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 }
818
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400819 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
820 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700821 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700822 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400823 depPaths.linkObjects = append(depPaths.linkObjects, lib.exportedLinkObjects()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824 }
825
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400827 linkFile := rustDep.outputFile
828 if !linkFile.Valid() {
829 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
830 depName, ctx.ModuleName())
831 return
832 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700834 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
835 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836 }
837 }
838
Ivan Lozano89435d12020-07-31 11:01:18 -0400839 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700840 //Handle C dependencies
841 if _, ok := ccDep.(*Module); !ok {
842 if ccDep.Module().Target().Os != ctx.Os() {
843 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
844 return
845 }
846 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
847 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
848 return
849 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700850 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400851 linkObject := ccDep.OutputFile()
852 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500853
Ivan Lozano2093af22020-08-25 12:48:19 -0400854 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
856 }
857
858 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700859 switch {
860 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400862 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400863 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
864 if mod, ok := ccDep.(*cc.Module); ok {
865 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
866 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400867 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400868 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400869 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700870 directStaticLibDeps = append(directStaticLibDeps, ccDep)
871 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700872 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400874 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400875 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
876 if mod, ok := ccDep.(*cc.Module); ok {
877 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
878 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400879 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400880 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 directSharedLibDeps = append(directSharedLibDeps, ccDep)
882 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
883 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700884 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400885 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700886 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400887 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700888 }
889
890 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700891 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
892 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400893 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700895 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400896
897 if srcDep, ok := dep.(android.SourceFileProducer); ok {
898 switch depTag {
899 case android.SourceDepTag:
900 // These are usually genrules which don't have per-target variants.
901 directSrcDeps = append(directSrcDeps, srcDep)
902 }
903 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904 })
905
906 var rlibDepFiles RustLibraries
907 for _, dep := range directRlibDeps {
908 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
909 }
910 var dylibDepFiles RustLibraries
911 for _, dep := range directDylibDeps {
912 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
913 }
914 var procMacroDepFiles RustLibraries
915 for _, dep := range directProcMacroDeps {
916 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
917 }
918
919 var staticLibDepFiles android.Paths
920 for _, dep := range directStaticLibDeps {
921 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
922 }
923
924 var sharedLibDepFiles android.Paths
925 for _, dep := range directSharedLibDeps {
926 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
927 }
928
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400929 var srcProviderDepFiles android.Paths
930 for _, dep := range directSrcProvidersDeps {
931 srcs, _ := dep.OutputFiles("")
932 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
933 }
934 for _, dep := range directSrcDeps {
935 srcs := dep.Srcs()
936 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
937 }
938
Ivan Lozanoffee3342019-08-27 12:03:00 -0700939 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
940 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
941 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
942 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
943 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400944 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945
946 // Dedup exported flags from dependencies
947 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
948 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400949 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
950 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
951 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700952
953 return depPaths
954}
955
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800956func (mod *Module) InstallInData() bool {
957 if mod.compiler == nil {
958 return false
959 }
960 return mod.compiler.inData()
961}
962
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963func linkPathFromFilePath(filepath android.Path) string {
964 return strings.Split(filepath.String(), filepath.Base())[0]
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 Lozano2b081132020-09-08 12:46:52 -0400982 stdLinkage := "dylib-std"
983 if mod.compiler.staticStd(ctx) {
984 stdLinkage = "rlib-std"
985 }
986
987 rlibDepVariations := commonDepVariations
988 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
989 rlibDepVariations = append(rlibDepVariations,
990 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
991 }
992
Ivan Lozano52767be2019-10-18 14:49:46 -0700993 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400994 append(rlibDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400995 {Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700996 rlibDepTag, deps.Rlibs...)
997 actx.AddVariationDependencies(
998 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400999 {Mutator: "rust_libraries", Variation: "dylib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001000 dylibDepTag, deps.Dylibs...)
1001
Ivan Lozano042504f2020-08-18 14:31:23 -04001002 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1003 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001004 if autoDep.depTag == rlibDepTag {
1005 actx.AddVariationDependencies(
1006 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1007 autoDep.depTag, deps.Rustlibs...)
1008 } else {
1009 actx.AddVariationDependencies(
1010 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1011 autoDep.depTag, deps.Rustlibs...)
1012 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001013 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001014 if deps.Stdlibs != nil {
1015 if mod.compiler.staticStd(ctx) {
1016 actx.AddVariationDependencies(
1017 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1018 rlibDepTag, deps.Stdlibs...)
1019 } else {
1020 actx.AddVariationDependencies(
1021 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1022 dylibDepTag, deps.Stdlibs...)
1023 }
1024 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001025 actx.AddVariationDependencies(append(commonDepVariations,
1026 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001027 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001028 actx.AddVariationDependencies(append(commonDepVariations,
1029 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001030 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001031
Dan Albert92fe7402020-07-15 13:33:30 -07001032 crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001033 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001034 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001035 }
1036 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001037 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001038 }
1039
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001040 if mod.sourceProvider != nil {
1041 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1042 bindgen.Properties.Custom_bindgen != "" {
1043 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1044 bindgen.Properties.Custom_bindgen)
1045 }
1046 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001047 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001048 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001049}
1050
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001051func BeginMutator(ctx android.BottomUpMutatorContext) {
1052 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1053 mod.beginMutator(ctx)
1054 }
1055}
1056
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001057func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1058 ctx := &baseModuleContext{
1059 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001060 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001061
1062 mod.begin(ctx)
1063}
1064
Ivan Lozanoffee3342019-08-27 12:03:00 -07001065func (mod *Module) Name() string {
1066 name := mod.ModuleBase.Name()
1067 if p, ok := mod.compiler.(interface {
1068 Name(string) string
1069 }); ok {
1070 name = p.Name(name)
1071 }
1072 return name
1073}
1074
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001075func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001076 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001077 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001078 }
1079}
1080
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001081var _ android.HostToolProvider = (*Module)(nil)
1082
1083func (mod *Module) HostToolPath() android.OptionalPath {
1084 if !mod.Host() {
1085 return android.OptionalPath{}
1086 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001087 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1088 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001089 }
1090 return android.OptionalPath{}
1091}
1092
Ivan Lozanoffee3342019-08-27 12:03:00 -07001093var Bool = proptools.Bool
1094var BoolDefault = proptools.BoolDefault
1095var String = proptools.String
1096var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001097
1098var _ android.OutputFileProducer = (*Module)(nil)