blob: 6004c68253238ce41126aef43a15b142d8cf63a5 [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
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020090 outputFile 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
Colin Cross1348ce32020-10-01 13:37:16 -0700224func (mod *Module) SplitPerApiLevel() bool {
225 return false
226}
227
Ivan Lozano52767be2019-10-18 14:49:46 -0700228func (mod *Module) ToolchainLibrary() bool {
229 return false
230}
231
232func (mod *Module) NdkPrebuiltStl() bool {
233 return false
234}
235
236func (mod *Module) StubDecorator() bool {
237 return false
238}
239
Ivan Lozanoffee3342019-08-27 12:03:00 -0700240type Deps struct {
241 Dylibs []string
242 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700243 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400244 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245 ProcMacros []string
246 SharedLibs []string
247 StaticLibs []string
248
249 CrtBegin, CrtEnd string
250}
251
252type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400253 DyLibs RustLibraries
254 RLibs RustLibraries
255 SharedLibs android.Paths
256 StaticLibs android.Paths
257 ProcMacros RustLibraries
258 linkDirs []string
259 depFlags []string
260 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700262
Ivan Lozano45901ed2020-07-24 16:05:01 -0400263 // Used by bindgen modules which call clang
264 depClangFlags []string
265 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400266 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400267 depSystemIncludePaths android.Paths
268
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400269 coverageFiles android.Paths
270
Ivan Lozanof1c84332019-09-20 11:00:37 -0700271 CrtBegin android.OptionalPath
272 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700273
274 // Paths to generated source files
275 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276}
277
278type RustLibraries []RustLibrary
279
280type RustLibrary struct {
281 Path android.Path
282 CrateName string
283}
284
285type compiler interface {
286 compilerFlags(ctx ModuleContext, flags Flags) Flags
287 compilerProps() []interface{}
288 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
289 compilerDeps(ctx DepsContext, deps Deps) Deps
290 crateName() string
291
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800292 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200293 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400295
296 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400297
298 Disabled() bool
299 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400300
Ivan Lozanodd055472020-09-28 13:22:45 -0400301 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400302}
303
Matthew Maurerbb3add12020-06-25 09:34:12 -0700304type exportedFlagsProducer interface {
305 exportedLinkDirs() []string
306 exportedDepFlags() []string
Ivan Lozano2093af22020-08-25 12:48:19 -0400307 exportedLinkObjects() []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700308 exportLinkDirs(...string)
309 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400310 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700311}
312
313type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400314 depFlags []string
315 linkDirs []string
316 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700317}
318
319func (flagExporter *flagExporter) exportedLinkDirs() []string {
320 return flagExporter.linkDirs
321}
322
323func (flagExporter *flagExporter) exportedDepFlags() []string {
324 return flagExporter.depFlags
325}
326
Ivan Lozano2093af22020-08-25 12:48:19 -0400327func (flagExporter *flagExporter) exportedLinkObjects() []string {
328 return flagExporter.linkObjects
329}
330
Matthew Maurerbb3add12020-06-25 09:34:12 -0700331func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
332 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
333}
334
335func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
336 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
337}
338
Ivan Lozano2093af22020-08-25 12:48:19 -0400339func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
340 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
341}
342
Matthew Maurerbb3add12020-06-25 09:34:12 -0700343var _ exportedFlagsProducer = (*flagExporter)(nil)
344
345func NewFlagExporter() *flagExporter {
346 return &flagExporter{
Ivan Lozano2093af22020-08-25 12:48:19 -0400347 depFlags: []string{},
348 linkDirs: []string{},
349 linkObjects: []string{},
Matthew Maurerbb3add12020-06-25 09:34:12 -0700350 }
351}
352
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400353func (mod *Module) isCoverageVariant() bool {
354 return mod.coverage.Properties.IsCoverageVariant
355}
356
357var _ cc.Coverage = (*Module)(nil)
358
359func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
360 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
361}
362
363func (mod *Module) PreventInstall() {
364 mod.Properties.PreventInstall = true
365}
366
367func (mod *Module) HideFromMake() {
368 mod.Properties.HideFromMake = true
369}
370
371func (mod *Module) MarkAsCoverageVariant(coverage bool) {
372 mod.coverage.Properties.IsCoverageVariant = coverage
373}
374
375func (mod *Module) EnableCoverageIfNeeded() {
376 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700377}
378
379func defaultsFactory() android.Module {
380 return DefaultsFactory()
381}
382
383type Defaults struct {
384 android.ModuleBase
385 android.DefaultsModuleBase
386}
387
388func DefaultsFactory(props ...interface{}) android.Module {
389 module := &Defaults{}
390
391 module.AddProperties(props...)
392 module.AddProperties(
393 &BaseProperties{},
394 &BaseCompilerProperties{},
395 &BinaryCompilerProperties{},
396 &LibraryCompilerProperties{},
397 &ProcMacroCompilerProperties{},
398 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400399 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700400 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400401 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200402 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700403 )
404
405 android.InitDefaultsModule(module)
406 return module
407}
408
409func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700410 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700411}
412
Ivan Lozano183a3212019-10-18 14:18:45 -0700413func (mod *Module) CcLibrary() bool {
414 if mod.compiler != nil {
415 if _, ok := mod.compiler.(*libraryDecorator); ok {
416 return true
417 }
418 }
419 return false
420}
421
422func (mod *Module) CcLibraryInterface() bool {
423 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400424 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
425 // VariantIs{Static,Shared} is set.
426 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700427 return true
428 }
429 }
430 return false
431}
432
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800433func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700434 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700435 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800436 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700437 }
438 }
439 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
440}
441
442func (mod *Module) SetStatic() {
443 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700444 if library, ok := mod.compiler.(libraryInterface); ok {
445 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700446 return
447 }
448 }
449 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
450}
451
452func (mod *Module) SetShared() {
453 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700454 if library, ok := mod.compiler.(libraryInterface); ok {
455 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700456 return
457 }
458 }
459 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
460}
461
462func (mod *Module) SetBuildStubs() {
463 panic("SetBuildStubs not yet implemented for rust modules")
464}
465
Colin Crossd1f898e2020-08-18 18:35:15 -0700466func (mod *Module) SetStubsVersion(string) {
467 panic("SetStubsVersion not yet implemented for rust modules")
Ivan Lozano183a3212019-10-18 14:18:45 -0700468}
469
Jooyung Han03b51852020-02-26 22:45:42 +0900470func (mod *Module) StubsVersion() string {
Colin Crossd1f898e2020-08-18 18:35:15 -0700471 panic("StubsVersion not yet implemented for rust modules")
472}
473
474func (mod *Module) SetAllStubsVersions([]string) {
475 panic("SetAllStubsVersions not yet implemented for rust modules")
476}
477
478func (mod *Module) AllStubsVersions() []string {
479 return nil
Jooyung Han03b51852020-02-26 22:45:42 +0900480}
481
Ivan Lozano183a3212019-10-18 14:18:45 -0700482func (mod *Module) BuildStaticVariant() bool {
483 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700484 if library, ok := mod.compiler.(libraryInterface); ok {
485 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700486 }
487 }
488 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
489}
490
491func (mod *Module) BuildSharedVariant() bool {
492 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700493 if library, ok := mod.compiler.(libraryInterface); ok {
494 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700495 }
496 }
497 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
498}
499
500// Rust module deps don't have a link order (?)
501func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
502
503func (mod *Module) GetDepsInLinkOrder() []android.Path {
504 return []android.Path{}
505}
506
507func (mod *Module) GetStaticVariant() cc.LinkableInterface {
508 return nil
509}
510
511func (mod *Module) Module() android.Module {
512 return mod
513}
514
515func (mod *Module) StubsVersions() []string {
516 // For now, Rust has no stubs versions.
517 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700518 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700519 return []string{}
520 }
521 }
522 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
523}
524
525func (mod *Module) OutputFile() android.OptionalPath {
526 return mod.outputFile
527}
528
529func (mod *Module) InRecovery() bool {
530 // For now, Rust has no notion of the recovery image
531 return false
532}
533func (mod *Module) HasStaticVariant() bool {
534 if mod.GetStaticVariant() != nil {
535 return true
536 }
537 return false
538}
539
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540func (mod *Module) CoverageFiles() android.Paths {
541 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700542 if !mod.compiler.nativeCoverage() {
543 return android.Paths{}
544 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400545 if library, ok := mod.compiler.(*libraryDecorator); ok {
546 if library.coverageFile != nil {
547 return android.Paths{library.coverageFile}
548 }
549 return android.Paths{}
550 }
551 }
552 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
553}
554
Ivan Lozano183a3212019-10-18 14:18:45 -0700555var _ cc.LinkableInterface = (*Module)(nil)
556
Ivan Lozanoffee3342019-08-27 12:03:00 -0700557func (mod *Module) Init() android.Module {
558 mod.AddProperties(&mod.Properties)
559
560 if mod.compiler != nil {
561 mod.AddProperties(mod.compiler.compilerProps()...)
562 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400563 if mod.coverage != nil {
564 mod.AddProperties(mod.coverage.props()...)
565 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200566 if mod.clippy != nil {
567 mod.AddProperties(mod.clippy.props()...)
568 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400569 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700570 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400571 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400572
Ivan Lozanoffee3342019-08-27 12:03:00 -0700573 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
574
575 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700576 return mod
577}
578
579func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
580 return &Module{
581 hod: hod,
582 multilib: multilib,
583 }
584}
585func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
586 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400587 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200588 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589 return module
590}
591
592type ModuleContext interface {
593 android.ModuleContext
594 ModuleContextIntf
595}
596
597type BaseModuleContext interface {
598 android.BaseModuleContext
599 ModuleContextIntf
600}
601
602type DepsContext interface {
603 android.BottomUpMutatorContext
604 ModuleContextIntf
605}
606
607type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200608 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700609 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610}
611
612type depsContext struct {
613 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700614}
615
616type moduleContext struct {
617 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700618}
619
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200620type baseModuleContext struct {
621 android.BaseModuleContext
622}
623
624func (ctx *moduleContext) RustModule() *Module {
625 return ctx.Module().(*Module)
626}
627
628func (ctx *moduleContext) toolchain() config.Toolchain {
629 return ctx.RustModule().toolchain(ctx)
630}
631
632func (ctx *depsContext) RustModule() *Module {
633 return ctx.Module().(*Module)
634}
635
636func (ctx *depsContext) toolchain() config.Toolchain {
637 return ctx.RustModule().toolchain(ctx)
638}
639
640func (ctx *baseModuleContext) RustModule() *Module {
641 return ctx.Module().(*Module)
642}
643
644func (ctx *baseModuleContext) toolchain() config.Toolchain {
645 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400646}
647
648func (mod *Module) nativeCoverage() bool {
649 return mod.compiler != nil && mod.compiler.nativeCoverage()
650}
651
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
653 if mod.cachedToolchain == nil {
654 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
655 }
656 return mod.cachedToolchain
657}
658
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200659func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
660 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
661}
662
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
664}
665
666func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
667 ctx := &moduleContext{
668 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700669 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670
671 toolchain := mod.toolchain(ctx)
672
673 if !toolchain.Supported() {
674 // This toolchain's unsupported, there's nothing to do for this mod.
675 return
676 }
677
678 deps := mod.depsToPaths(ctx)
679 flags := Flags{
680 Toolchain: toolchain,
681 }
682
683 if mod.compiler != nil {
684 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400685 }
686 if mod.coverage != nil {
687 flags, deps = mod.coverage.flags(ctx, flags, deps)
688 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200689 if mod.clippy != nil {
690 flags, deps = mod.clippy.flags(ctx, flags, deps)
691 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400692
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200693 // SourceProvider needs to call GenerateSource() before compiler calls
694 // compile() so it can provide the source. A SourceProvider has
695 // multiple variants (e.g. source, rlib, dylib). Only the "source"
696 // variant is responsible for effectively generating the source. The
697 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400698 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200699 if mod.compiler.(libraryInterface).source() {
700 mod.sourceProvider.GenerateSource(ctx, deps)
701 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
Colin Crossd3e05ca2020-09-23 16:49:23 -0700702 if lib, ok := mod.compiler.(*libraryDecorator); ok {
703 lib.flagExporter.linkDirs = nil
704 lib.flagExporter.linkObjects = nil
705 lib.flagExporter.depFlags = nil
706 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200707 } else {
708 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
709 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
710 mod.sourceProvider.setOutputFile(sourceLib.sourceProvider.Srcs()[0])
711 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400712 }
713
714 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400716
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400718 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200719 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400720 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721 }
722}
723
724func (mod *Module) deps(ctx DepsContext) Deps {
725 deps := Deps{}
726
727 if mod.compiler != nil {
728 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400729 }
730 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700731 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700732 }
733
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400734 if mod.coverage != nil {
735 deps = mod.coverage.deps(ctx, deps)
736 }
737
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
739 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700740 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700741 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
742 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
743 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
744
745 return deps
746
747}
748
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749type dependencyTag struct {
750 blueprint.BaseDependencyTag
751 name string
752 library bool
753 proc_macro bool
754}
755
756var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400757 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
758 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
759 dylibDepTag = dependencyTag{name: "dylib", library: true}
760 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
761 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200762 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763)
764
Matthew Maurer0f003b12020-06-29 14:34:06 -0700765type autoDep struct {
766 variation string
767 depTag dependencyTag
768}
769
770var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200771 rlibVariation = "rlib"
772 dylibVariation = "dylib"
773 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
774 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700775)
776
777type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400778 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700779}
780
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400781func (mod *Module) begin(ctx BaseModuleContext) {
782 if mod.coverage != nil {
783 mod.coverage.begin(ctx)
784 }
785}
786
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
788 var depPaths PathDeps
789
790 directRlibDeps := []*Module{}
791 directDylibDeps := []*Module{}
792 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700793 directSharedLibDeps := [](cc.LinkableInterface){}
794 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400795 directSrcProvidersDeps := []*Module{}
796 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797
798 ctx.VisitDirectDeps(func(dep android.Module) {
799 depName := ctx.OtherModuleName(dep)
800 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400801 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700803
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 switch depTag {
805 case dylibDepTag:
806 dylib, ok := rustDep.compiler.(libraryInterface)
807 if !ok || !dylib.dylib() {
808 ctx.ModuleErrorf("mod %q not an dylib library", depName)
809 return
810 }
811 directDylibDeps = append(directDylibDeps, rustDep)
812 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
813 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400814
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 rlib, ok := rustDep.compiler.(libraryInterface)
816 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400817 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 return
819 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400820 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400822 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 case procMacroDepTag:
824 directProcMacroDeps = append(directProcMacroDeps, rustDep)
825 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400826 case android.SourceDepTag:
827 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
828 // OS/Arch variant is used.
829 var helper string
830 if ctx.Host() {
831 helper = "missing 'host_supported'?"
832 } else {
833 helper = "device module defined?"
834 }
835
836 if dep.Target().Os != ctx.Os() {
837 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
838 return
839 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
840 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
841 return
842 }
843 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 }
845
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400846 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
847 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700848 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400850 depPaths.linkObjects = append(depPaths.linkObjects, lib.exportedLinkObjects()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851 }
852
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400854 linkFile := rustDep.outputFile
855 if !linkFile.Valid() {
856 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
857 depName, ctx.ModuleName())
858 return
859 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700860 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700861 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
862 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863 }
864 }
865
Ivan Lozano89435d12020-07-31 11:01:18 -0400866 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700867 //Handle C dependencies
868 if _, ok := ccDep.(*Module); !ok {
869 if ccDep.Module().Target().Os != ctx.Os() {
870 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
871 return
872 }
873 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
874 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
875 return
876 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700877 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400878 linkObject := ccDep.OutputFile()
879 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500880
Ivan Lozano2093af22020-08-25 12:48:19 -0400881 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
883 }
884
885 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700886 switch {
887 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700888 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400889 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400890 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
891 if mod, ok := ccDep.(*cc.Module); ok {
892 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
893 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400894 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400895 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400896 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897 directStaticLibDeps = append(directStaticLibDeps, ccDep)
898 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700899 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400901 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400902 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
903 if mod, ok := ccDep.(*cc.Module); ok {
904 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
905 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400906 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400907 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700908 directSharedLibDeps = append(directSharedLibDeps, ccDep)
909 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
910 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700911 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400912 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700913 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400914 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915 }
916
917 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700918 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
919 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400920 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700922 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400923
924 if srcDep, ok := dep.(android.SourceFileProducer); ok {
925 switch depTag {
926 case android.SourceDepTag:
927 // These are usually genrules which don't have per-target variants.
928 directSrcDeps = append(directSrcDeps, srcDep)
929 }
930 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931 })
932
933 var rlibDepFiles RustLibraries
934 for _, dep := range directRlibDeps {
935 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
936 }
937 var dylibDepFiles RustLibraries
938 for _, dep := range directDylibDeps {
939 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
940 }
941 var procMacroDepFiles RustLibraries
942 for _, dep := range directProcMacroDeps {
943 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
944 }
945
946 var staticLibDepFiles android.Paths
947 for _, dep := range directStaticLibDeps {
948 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
949 }
950
951 var sharedLibDepFiles android.Paths
952 for _, dep := range directSharedLibDeps {
953 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
954 }
955
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400956 var srcProviderDepFiles android.Paths
957 for _, dep := range directSrcProvidersDeps {
958 srcs, _ := dep.OutputFiles("")
959 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
960 }
961 for _, dep := range directSrcDeps {
962 srcs := dep.Srcs()
963 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
964 }
965
Ivan Lozanoffee3342019-08-27 12:03:00 -0700966 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
967 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
968 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
969 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
970 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400971 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700972
973 // Dedup exported flags from dependencies
974 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
975 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400976 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
977 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
978 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979
980 return depPaths
981}
982
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800983func (mod *Module) InstallInData() bool {
984 if mod.compiler == nil {
985 return false
986 }
987 return mod.compiler.inData()
988}
989
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990func linkPathFromFilePath(filepath android.Path) string {
991 return strings.Split(filepath.String(), filepath.Base())[0]
992}
Ivan Lozanod648c432020-02-06 12:05:10 -0500993
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
995 ctx := &depsContext{
996 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700997 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998
999 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -07001000 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +09001001 if cc.VersionVariantAvailable(mod) {
1002 commonDepVariations = append(commonDepVariations,
1003 blueprint.Variation{Mutator: "version", Variation: ""})
1004 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001005 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001006 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -08001007 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001008 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001009
Ivan Lozano2b081132020-09-08 12:46:52 -04001010 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001011 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001012 stdLinkage = "rlib-std"
1013 }
1014
1015 rlibDepVariations := commonDepVariations
1016 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1017 rlibDepVariations = append(rlibDepVariations,
1018 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1019 }
1020
Ivan Lozano52767be2019-10-18 14:49:46 -07001021 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001022 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001023 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001024 rlibDepTag, deps.Rlibs...)
1025 actx.AddVariationDependencies(
1026 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001027 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001028 dylibDepTag, deps.Dylibs...)
1029
Ivan Lozano042504f2020-08-18 14:31:23 -04001030 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1031 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001032 if autoDep.depTag == rlibDepTag {
1033 actx.AddVariationDependencies(
1034 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1035 autoDep.depTag, deps.Rustlibs...)
1036 } else {
1037 actx.AddVariationDependencies(
1038 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1039 autoDep.depTag, deps.Rustlibs...)
1040 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001041 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001042 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001043 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001044 actx.AddVariationDependencies(
1045 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1046 rlibDepTag, deps.Stdlibs...)
1047 } else {
1048 actx.AddVariationDependencies(
1049 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1050 dylibDepTag, deps.Stdlibs...)
1051 }
1052 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001053 actx.AddVariationDependencies(append(commonDepVariations,
1054 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001055 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001056 actx.AddVariationDependencies(append(commonDepVariations,
1057 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001058 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001059
Dan Albert92fe7402020-07-15 13:33:30 -07001060 crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001061 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001062 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001063 }
1064 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001065 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001066 }
1067
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001068 if mod.sourceProvider != nil {
1069 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1070 bindgen.Properties.Custom_bindgen != "" {
1071 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1072 bindgen.Properties.Custom_bindgen)
1073 }
1074 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001075 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001076 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001077}
1078
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001079func BeginMutator(ctx android.BottomUpMutatorContext) {
1080 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1081 mod.beginMutator(ctx)
1082 }
1083}
1084
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001085func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1086 ctx := &baseModuleContext{
1087 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001088 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001089
1090 mod.begin(ctx)
1091}
1092
Ivan Lozanoffee3342019-08-27 12:03:00 -07001093func (mod *Module) Name() string {
1094 name := mod.ModuleBase.Name()
1095 if p, ok := mod.compiler.(interface {
1096 Name(string) string
1097 }); ok {
1098 name = p.Name(name)
1099 }
1100 return name
1101}
1102
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001103func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001104 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001105 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001106 }
1107}
1108
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001109var _ android.HostToolProvider = (*Module)(nil)
1110
1111func (mod *Module) HostToolPath() android.OptionalPath {
1112 if !mod.Host() {
1113 return android.OptionalPath{}
1114 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001115 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1116 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001117 }
1118 return android.OptionalPath{}
1119}
1120
Ivan Lozanoffee3342019-08-27 12:03:00 -07001121var Bool = proptools.Bool
1122var BoolDefault = proptools.BoolDefault
1123var String = proptools.String
1124var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001125
1126var _ android.OutputFileProducer = (*Module)(nil)