blob: 4cba6d6b1c20cbfa320832d19d806fcb2e2f4aa5 [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")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020046 pctx.ImportAs("cc_config", "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
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400261 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400262 depSystemIncludePaths android.Paths
263
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400264 coverageFiles android.Paths
265
Ivan Lozanof1c84332019-09-20 11:00:37 -0700266 CrtBegin android.OptionalPath
267 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700268
269 // Paths to generated source files
270 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700271}
272
273type RustLibraries []RustLibrary
274
275type RustLibrary struct {
276 Path android.Path
277 CrateName string
278}
279
280type compiler interface {
281 compilerFlags(ctx ModuleContext, flags Flags) Flags
282 compilerProps() []interface{}
283 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
284 compilerDeps(ctx DepsContext, deps Deps) Deps
285 crateName() string
286
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800287 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200288 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700289 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400290
291 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400292
293 Disabled() bool
294 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400295
296 static() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400297}
298
Matthew Maurerbb3add12020-06-25 09:34:12 -0700299type exportedFlagsProducer interface {
300 exportedLinkDirs() []string
301 exportedDepFlags() []string
Ivan Lozano2093af22020-08-25 12:48:19 -0400302 exportedLinkObjects() []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700303 exportLinkDirs(...string)
304 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400305 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700306}
307
308type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400309 depFlags []string
310 linkDirs []string
311 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700312}
313
314func (flagExporter *flagExporter) exportedLinkDirs() []string {
315 return flagExporter.linkDirs
316}
317
318func (flagExporter *flagExporter) exportedDepFlags() []string {
319 return flagExporter.depFlags
320}
321
Ivan Lozano2093af22020-08-25 12:48:19 -0400322func (flagExporter *flagExporter) exportedLinkObjects() []string {
323 return flagExporter.linkObjects
324}
325
Matthew Maurerbb3add12020-06-25 09:34:12 -0700326func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
327 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
328}
329
330func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
331 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
332}
333
Ivan Lozano2093af22020-08-25 12:48:19 -0400334func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
335 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
336}
337
Matthew Maurerbb3add12020-06-25 09:34:12 -0700338var _ exportedFlagsProducer = (*flagExporter)(nil)
339
340func NewFlagExporter() *flagExporter {
341 return &flagExporter{
Ivan Lozano2093af22020-08-25 12:48:19 -0400342 depFlags: []string{},
343 linkDirs: []string{},
344 linkObjects: []string{},
Matthew Maurerbb3add12020-06-25 09:34:12 -0700345 }
346}
347
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400348func (mod *Module) isCoverageVariant() bool {
349 return mod.coverage.Properties.IsCoverageVariant
350}
351
352var _ cc.Coverage = (*Module)(nil)
353
354func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
355 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
356}
357
358func (mod *Module) PreventInstall() {
359 mod.Properties.PreventInstall = true
360}
361
362func (mod *Module) HideFromMake() {
363 mod.Properties.HideFromMake = true
364}
365
366func (mod *Module) MarkAsCoverageVariant(coverage bool) {
367 mod.coverage.Properties.IsCoverageVariant = coverage
368}
369
370func (mod *Module) EnableCoverageIfNeeded() {
371 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700372}
373
374func defaultsFactory() android.Module {
375 return DefaultsFactory()
376}
377
378type Defaults struct {
379 android.ModuleBase
380 android.DefaultsModuleBase
381}
382
383func DefaultsFactory(props ...interface{}) android.Module {
384 module := &Defaults{}
385
386 module.AddProperties(props...)
387 module.AddProperties(
388 &BaseProperties{},
389 &BaseCompilerProperties{},
390 &BinaryCompilerProperties{},
391 &LibraryCompilerProperties{},
392 &ProcMacroCompilerProperties{},
393 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400394 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700395 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400396 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200397 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700398 )
399
400 android.InitDefaultsModule(module)
401 return module
402}
403
404func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700405 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700406}
407
Ivan Lozano183a3212019-10-18 14:18:45 -0700408func (mod *Module) CcLibrary() bool {
409 if mod.compiler != nil {
410 if _, ok := mod.compiler.(*libraryDecorator); ok {
411 return true
412 }
413 }
414 return false
415}
416
417func (mod *Module) CcLibraryInterface() bool {
418 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400419 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
420 // VariantIs{Static,Shared} is set.
421 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700422 return true
423 }
424 }
425 return false
426}
427
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800428func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700429 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700430 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800431 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700432 }
433 }
434 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
435}
436
437func (mod *Module) SetStatic() {
438 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700439 if library, ok := mod.compiler.(libraryInterface); ok {
440 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700441 return
442 }
443 }
444 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
445}
446
447func (mod *Module) SetShared() {
448 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700449 if library, ok := mod.compiler.(libraryInterface); ok {
450 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700451 return
452 }
453 }
454 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
455}
456
457func (mod *Module) SetBuildStubs() {
458 panic("SetBuildStubs not yet implemented for rust modules")
459}
460
461func (mod *Module) SetStubsVersions(string) {
462 panic("SetStubsVersions not yet implemented for rust modules")
463}
464
Jooyung Han03b51852020-02-26 22:45:42 +0900465func (mod *Module) StubsVersion() string {
466 panic("SetStubsVersions not yet implemented for rust modules")
467}
468
Ivan Lozano183a3212019-10-18 14:18:45 -0700469func (mod *Module) BuildStaticVariant() bool {
470 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700471 if library, ok := mod.compiler.(libraryInterface); ok {
472 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700473 }
474 }
475 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
476}
477
478func (mod *Module) BuildSharedVariant() bool {
479 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700480 if library, ok := mod.compiler.(libraryInterface); ok {
481 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700482 }
483 }
484 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
485}
486
487// Rust module deps don't have a link order (?)
488func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
489
490func (mod *Module) GetDepsInLinkOrder() []android.Path {
491 return []android.Path{}
492}
493
494func (mod *Module) GetStaticVariant() cc.LinkableInterface {
495 return nil
496}
497
498func (mod *Module) Module() android.Module {
499 return mod
500}
501
502func (mod *Module) StubsVersions() []string {
503 // For now, Rust has no stubs versions.
504 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700505 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700506 return []string{}
507 }
508 }
509 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
510}
511
512func (mod *Module) OutputFile() android.OptionalPath {
513 return mod.outputFile
514}
515
516func (mod *Module) InRecovery() bool {
517 // For now, Rust has no notion of the recovery image
518 return false
519}
520func (mod *Module) HasStaticVariant() bool {
521 if mod.GetStaticVariant() != nil {
522 return true
523 }
524 return false
525}
526
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400527func (mod *Module) CoverageFiles() android.Paths {
528 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700529 if !mod.compiler.nativeCoverage() {
530 return android.Paths{}
531 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400532 if library, ok := mod.compiler.(*libraryDecorator); ok {
533 if library.coverageFile != nil {
534 return android.Paths{library.coverageFile}
535 }
536 return android.Paths{}
537 }
538 }
539 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
540}
541
Ivan Lozano183a3212019-10-18 14:18:45 -0700542var _ cc.LinkableInterface = (*Module)(nil)
543
Ivan Lozanoffee3342019-08-27 12:03:00 -0700544func (mod *Module) Init() android.Module {
545 mod.AddProperties(&mod.Properties)
546
547 if mod.compiler != nil {
548 mod.AddProperties(mod.compiler.compilerProps()...)
549 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400550 if mod.coverage != nil {
551 mod.AddProperties(mod.coverage.props()...)
552 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200553 if mod.clippy != nil {
554 mod.AddProperties(mod.clippy.props()...)
555 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400556 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700557 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400558 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400559
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
561
562 android.InitDefaultableModule(mod)
563
Ivan Lozanode252912019-09-06 15:29:52 -0700564 // Explicitly disable unsupported targets.
565 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
566 disableTargets := struct {
567 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700568 Linux_bionic struct {
569 Enabled *bool
570 }
571 }
572 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700573 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
574
575 ctx.AppendProperties(&disableTargets)
576 })
577
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578 return mod
579}
580
581func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
582 return &Module{
583 hod: hod,
584 multilib: multilib,
585 }
586}
587func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
588 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400589 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200590 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700591 return module
592}
593
594type ModuleContext interface {
595 android.ModuleContext
596 ModuleContextIntf
597}
598
599type BaseModuleContext interface {
600 android.BaseModuleContext
601 ModuleContextIntf
602}
603
604type DepsContext interface {
605 android.BottomUpMutatorContext
606 ModuleContextIntf
607}
608
609type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200610 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700612}
613
614type depsContext struct {
615 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700616}
617
618type moduleContext struct {
619 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700620}
621
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200622type baseModuleContext struct {
623 android.BaseModuleContext
624}
625
626func (ctx *moduleContext) RustModule() *Module {
627 return ctx.Module().(*Module)
628}
629
630func (ctx *moduleContext) toolchain() config.Toolchain {
631 return ctx.RustModule().toolchain(ctx)
632}
633
634func (ctx *depsContext) RustModule() *Module {
635 return ctx.Module().(*Module)
636}
637
638func (ctx *depsContext) toolchain() config.Toolchain {
639 return ctx.RustModule().toolchain(ctx)
640}
641
642func (ctx *baseModuleContext) RustModule() *Module {
643 return ctx.Module().(*Module)
644}
645
646func (ctx *baseModuleContext) toolchain() config.Toolchain {
647 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400648}
649
650func (mod *Module) nativeCoverage() bool {
651 return mod.compiler != nil && mod.compiler.nativeCoverage()
652}
653
Ivan Lozanoffee3342019-08-27 12:03:00 -0700654func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
655 if mod.cachedToolchain == nil {
656 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
657 }
658 return mod.cachedToolchain
659}
660
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200661func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
662 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
663}
664
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
666}
667
668func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
669 ctx := &moduleContext{
670 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700671 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700672
673 toolchain := mod.toolchain(ctx)
674
675 if !toolchain.Supported() {
676 // This toolchain's unsupported, there's nothing to do for this mod.
677 return
678 }
679
680 deps := mod.depsToPaths(ctx)
681 flags := Flags{
682 Toolchain: toolchain,
683 }
684
685 if mod.compiler != nil {
686 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400687 }
688 if mod.coverage != nil {
689 flags, deps = mod.coverage.flags(ctx, flags, deps)
690 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200691 if mod.clippy != nil {
692 flags, deps = mod.clippy.flags(ctx, flags, deps)
693 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400694
Andrei Homescuc7767922020-08-05 06:36:19 -0700695 // SourceProvider needs to call GenerateSource() before compiler calls compile() so it can provide the source.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400696 // TODO(b/162588681) This shouldn't have to run for every variant.
697 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700698 generatedFile := mod.sourceProvider.GenerateSource(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400699 mod.generatedFile = android.OptionalPathForPath(generatedFile)
700 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
701 }
702
703 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400705
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400707 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200708 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400709 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700710 }
711}
712
713func (mod *Module) deps(ctx DepsContext) Deps {
714 deps := Deps{}
715
716 if mod.compiler != nil {
717 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400718 }
719 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700720 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721 }
722
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400723 if mod.coverage != nil {
724 deps = mod.coverage.deps(ctx, deps)
725 }
726
Ivan Lozanoffee3342019-08-27 12:03:00 -0700727 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
728 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700729 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
731 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
732 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
733
734 return deps
735
736}
737
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738type dependencyTag struct {
739 blueprint.BaseDependencyTag
740 name string
741 library bool
742 proc_macro bool
743}
744
745var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400746 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
747 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
748 dylibDepTag = dependencyTag{name: "dylib", library: true}
749 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
750 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751)
752
Matthew Maurer0f003b12020-06-29 14:34:06 -0700753type autoDep struct {
754 variation string
755 depTag dependencyTag
756}
757
758var (
759 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
760 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
761)
762
763type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400764 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700765}
766
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400767func (mod *Module) begin(ctx BaseModuleContext) {
768 if mod.coverage != nil {
769 mod.coverage.begin(ctx)
770 }
771}
772
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
774 var depPaths PathDeps
775
776 directRlibDeps := []*Module{}
777 directDylibDeps := []*Module{}
778 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700779 directSharedLibDeps := [](cc.LinkableInterface){}
780 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400781 directSrcProvidersDeps := []*Module{}
782 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783
784 ctx.VisitDirectDeps(func(dep android.Module) {
785 depName := ctx.OtherModuleName(dep)
786 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400787 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700789
Ivan Lozanoffee3342019-08-27 12:03:00 -0700790 switch depTag {
791 case dylibDepTag:
792 dylib, ok := rustDep.compiler.(libraryInterface)
793 if !ok || !dylib.dylib() {
794 ctx.ModuleErrorf("mod %q not an dylib library", depName)
795 return
796 }
797 directDylibDeps = append(directDylibDeps, rustDep)
798 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
799 case rlibDepTag:
800 rlib, ok := rustDep.compiler.(libraryInterface)
801 if !ok || !rlib.rlib() {
802 ctx.ModuleErrorf("mod %q not an rlib library", depName)
803 return
804 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400805 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806 directRlibDeps = append(directRlibDeps, rustDep)
807 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
808 case procMacroDepTag:
809 directProcMacroDeps = append(directProcMacroDeps, rustDep)
810 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400811 case android.SourceDepTag:
812 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
813 // OS/Arch variant is used.
814 var helper string
815 if ctx.Host() {
816 helper = "missing 'host_supported'?"
817 } else {
818 helper = "device module defined?"
819 }
820
821 if dep.Target().Os != ctx.Os() {
822 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
823 return
824 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
825 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
826 return
827 }
828 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 }
830
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400831 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
832 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700833 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400835 depPaths.linkObjects = append(depPaths.linkObjects, lib.exportedLinkObjects()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836 }
837
Ivan Lozanoffee3342019-08-27 12:03:00 -0700838 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400839 linkFile := rustDep.outputFile
840 if !linkFile.Valid() {
841 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
842 depName, ctx.ModuleName())
843 return
844 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700846 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
847 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700848 }
849 }
850
Ivan Lozano89435d12020-07-31 11:01:18 -0400851 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700852 //Handle C dependencies
853 if _, ok := ccDep.(*Module); !ok {
854 if ccDep.Module().Target().Os != ctx.Os() {
855 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
856 return
857 }
858 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
859 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
860 return
861 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700862 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400863 linkObject := ccDep.OutputFile()
864 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500865
Ivan Lozano2093af22020-08-25 12:48:19 -0400866 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700867 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
868 }
869
870 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700871 switch {
872 case cc.IsStaticDepTag(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 Lozanoa0cd8f92020-04-09 09:56:02 -0400881 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 directStaticLibDeps = append(directStaticLibDeps, ccDep)
883 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700884 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400886 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400887 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
888 if mod, ok := ccDep.(*cc.Module); ok {
889 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
890 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400891 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400892 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893 directSharedLibDeps = append(directSharedLibDeps, ccDep)
894 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
895 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700896 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400897 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700898 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400899 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900 }
901
902 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700903 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
904 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400905 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400908
909 if srcDep, ok := dep.(android.SourceFileProducer); ok {
910 switch depTag {
911 case android.SourceDepTag:
912 // These are usually genrules which don't have per-target variants.
913 directSrcDeps = append(directSrcDeps, srcDep)
914 }
915 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700916 })
917
918 var rlibDepFiles RustLibraries
919 for _, dep := range directRlibDeps {
920 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
921 }
922 var dylibDepFiles RustLibraries
923 for _, dep := range directDylibDeps {
924 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
925 }
926 var procMacroDepFiles RustLibraries
927 for _, dep := range directProcMacroDeps {
928 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
929 }
930
931 var staticLibDepFiles android.Paths
932 for _, dep := range directStaticLibDeps {
933 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
934 }
935
936 var sharedLibDepFiles android.Paths
937 for _, dep := range directSharedLibDeps {
938 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
939 }
940
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400941 var srcProviderDepFiles android.Paths
942 for _, dep := range directSrcProvidersDeps {
943 srcs, _ := dep.OutputFiles("")
944 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
945 }
946 for _, dep := range directSrcDeps {
947 srcs := dep.Srcs()
948 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
949 }
950
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
952 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
953 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
954 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
955 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400956 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957
958 // Dedup exported flags from dependencies
959 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
960 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400961 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
962 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
963 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700964
965 return depPaths
966}
967
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800968func (mod *Module) InstallInData() bool {
969 if mod.compiler == nil {
970 return false
971 }
972 return mod.compiler.inData()
973}
974
Ivan Lozanoffee3342019-08-27 12:03:00 -0700975func linkPathFromFilePath(filepath android.Path) string {
976 return strings.Split(filepath.String(), filepath.Base())[0]
977}
Ivan Lozanod648c432020-02-06 12:05:10 -0500978
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
980 ctx := &depsContext{
981 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700983
984 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700985 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900986 if cc.VersionVariantAvailable(mod) {
987 commonDepVariations = append(commonDepVariations,
988 blueprint.Variation{Mutator: "version", Variation: ""})
989 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700991 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800992 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700993 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700994 actx.AddVariationDependencies(
995 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -0400996 {Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700997 rlibDepTag, deps.Rlibs...)
998 actx.AddVariationDependencies(
999 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -04001000 {Mutator: "rust_libraries", Variation: "dylib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001001 dylibDepTag, deps.Dylibs...)
1002
Ivan Lozano042504f2020-08-18 14:31:23 -04001003 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1004 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001005 actx.AddVariationDependencies(
1006 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -04001007 {Mutator: "rust_libraries", Variation: autoDep.variation}}...),
Matthew Maurer0f003b12020-06-29 14:34:06 -07001008 autoDep.depTag, deps.Rustlibs...)
1009 }
1010
Ivan Lozano52767be2019-10-18 14:49:46 -07001011 actx.AddVariationDependencies(append(commonDepVariations,
1012 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001013 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001014 actx.AddVariationDependencies(append(commonDepVariations,
1015 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001016 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001017
Dan Albert92fe7402020-07-15 13:33:30 -07001018 crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001019 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001020 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001021 }
1022 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001023 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001024 }
1025
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001026 if mod.sourceProvider != nil {
1027 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1028 bindgen.Properties.Custom_bindgen != "" {
1029 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1030 bindgen.Properties.Custom_bindgen)
1031 }
1032 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001033 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001034 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035}
1036
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001037func BeginMutator(ctx android.BottomUpMutatorContext) {
1038 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1039 mod.beginMutator(ctx)
1040 }
1041}
1042
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001043func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1044 ctx := &baseModuleContext{
1045 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001046 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001047
1048 mod.begin(ctx)
1049}
1050
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051func (mod *Module) Name() string {
1052 name := mod.ModuleBase.Name()
1053 if p, ok := mod.compiler.(interface {
1054 Name(string) string
1055 }); ok {
1056 name = p.Name(name)
1057 }
1058 return name
1059}
1060
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001061func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001062 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001063 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001064 }
1065}
1066
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001067var _ android.HostToolProvider = (*Module)(nil)
1068
1069func (mod *Module) HostToolPath() android.OptionalPath {
1070 if !mod.Host() {
1071 return android.OptionalPath{}
1072 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001073 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1074 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001075 }
1076 return android.OptionalPath{}
1077}
1078
Ivan Lozanoffee3342019-08-27 12:03:00 -07001079var Bool = proptools.Bool
1080var BoolDefault = proptools.BoolDefault
1081var String = proptools.String
1082var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001083
1084var _ android.OutputFileProducer = (*Module)(nil)