blob: 1f8b904d37eb6eace7d21a8f7dbf90e7024f5de3 [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
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 Lozano2b081132020-09-08 12:46:52 -0400240 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241 ProcMacros []string
242 SharedLibs []string
243 StaticLibs []string
244
245 CrtBegin, CrtEnd string
246}
247
248type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400249 DyLibs RustLibraries
250 RLibs RustLibraries
251 SharedLibs android.Paths
252 StaticLibs android.Paths
253 ProcMacros RustLibraries
254 linkDirs []string
255 depFlags []string
256 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700257 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700258
Ivan Lozano45901ed2020-07-24 16:05:01 -0400259 // Used by bindgen modules which call clang
260 depClangFlags []string
261 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400262 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400263 depSystemIncludePaths android.Paths
264
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400265 coverageFiles android.Paths
266
Ivan Lozanof1c84332019-09-20 11:00:37 -0700267 CrtBegin android.OptionalPath
268 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700269
270 // Paths to generated source files
271 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700272}
273
274type RustLibraries []RustLibrary
275
276type RustLibrary struct {
277 Path android.Path
278 CrateName string
279}
280
281type compiler interface {
282 compilerFlags(ctx ModuleContext, flags Flags) Flags
283 compilerProps() []interface{}
284 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
285 compilerDeps(ctx DepsContext, deps Deps) Deps
286 crateName() string
287
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800288 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200289 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400291
292 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400293
294 Disabled() bool
295 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400296
Ivan Lozano2b081132020-09-08 12:46:52 -0400297 staticStd(ctx *depsContext) bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400298}
299
Matthew Maurerbb3add12020-06-25 09:34:12 -0700300type exportedFlagsProducer interface {
301 exportedLinkDirs() []string
302 exportedDepFlags() []string
Ivan Lozano2093af22020-08-25 12:48:19 -0400303 exportedLinkObjects() []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700304 exportLinkDirs(...string)
305 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400306 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700307}
308
309type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400310 depFlags []string
311 linkDirs []string
312 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700313}
314
315func (flagExporter *flagExporter) exportedLinkDirs() []string {
316 return flagExporter.linkDirs
317}
318
319func (flagExporter *flagExporter) exportedDepFlags() []string {
320 return flagExporter.depFlags
321}
322
Ivan Lozano2093af22020-08-25 12:48:19 -0400323func (flagExporter *flagExporter) exportedLinkObjects() []string {
324 return flagExporter.linkObjects
325}
326
Matthew Maurerbb3add12020-06-25 09:34:12 -0700327func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
328 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
329}
330
331func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
332 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
333}
334
Ivan Lozano2093af22020-08-25 12:48:19 -0400335func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
336 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
337}
338
Matthew Maurerbb3add12020-06-25 09:34:12 -0700339var _ exportedFlagsProducer = (*flagExporter)(nil)
340
341func NewFlagExporter() *flagExporter {
342 return &flagExporter{
Ivan Lozano2093af22020-08-25 12:48:19 -0400343 depFlags: []string{},
344 linkDirs: []string{},
345 linkObjects: []string{},
Matthew Maurerbb3add12020-06-25 09:34:12 -0700346 }
347}
348
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400349func (mod *Module) isCoverageVariant() bool {
350 return mod.coverage.Properties.IsCoverageVariant
351}
352
353var _ cc.Coverage = (*Module)(nil)
354
355func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
356 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
357}
358
359func (mod *Module) PreventInstall() {
360 mod.Properties.PreventInstall = true
361}
362
363func (mod *Module) HideFromMake() {
364 mod.Properties.HideFromMake = true
365}
366
367func (mod *Module) MarkAsCoverageVariant(coverage bool) {
368 mod.coverage.Properties.IsCoverageVariant = coverage
369}
370
371func (mod *Module) EnableCoverageIfNeeded() {
372 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373}
374
375func defaultsFactory() android.Module {
376 return DefaultsFactory()
377}
378
379type Defaults struct {
380 android.ModuleBase
381 android.DefaultsModuleBase
382}
383
384func DefaultsFactory(props ...interface{}) android.Module {
385 module := &Defaults{}
386
387 module.AddProperties(props...)
388 module.AddProperties(
389 &BaseProperties{},
390 &BaseCompilerProperties{},
391 &BinaryCompilerProperties{},
392 &LibraryCompilerProperties{},
393 &ProcMacroCompilerProperties{},
394 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400395 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700396 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400397 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200398 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399 )
400
401 android.InitDefaultsModule(module)
402 return module
403}
404
405func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700406 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407}
408
Ivan Lozano183a3212019-10-18 14:18:45 -0700409func (mod *Module) CcLibrary() bool {
410 if mod.compiler != nil {
411 if _, ok := mod.compiler.(*libraryDecorator); ok {
412 return true
413 }
414 }
415 return false
416}
417
418func (mod *Module) CcLibraryInterface() bool {
419 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400420 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
421 // VariantIs{Static,Shared} is set.
422 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700423 return true
424 }
425 }
426 return false
427}
428
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800429func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700430 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700431 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800432 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700433 }
434 }
435 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
436}
437
438func (mod *Module) SetStatic() {
439 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700440 if library, ok := mod.compiler.(libraryInterface); ok {
441 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700442 return
443 }
444 }
445 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
446}
447
448func (mod *Module) SetShared() {
449 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700450 if library, ok := mod.compiler.(libraryInterface); ok {
451 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700452 return
453 }
454 }
455 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
456}
457
458func (mod *Module) SetBuildStubs() {
459 panic("SetBuildStubs not yet implemented for rust modules")
460}
461
Colin Crossd1f898e2020-08-18 18:35:15 -0700462func (mod *Module) SetStubsVersion(string) {
463 panic("SetStubsVersion not yet implemented for rust modules")
Ivan Lozano183a3212019-10-18 14:18:45 -0700464}
465
Jooyung Han03b51852020-02-26 22:45:42 +0900466func (mod *Module) StubsVersion() string {
Colin Crossd1f898e2020-08-18 18:35:15 -0700467 panic("StubsVersion not yet implemented for rust modules")
468}
469
470func (mod *Module) SetAllStubsVersions([]string) {
471 panic("SetAllStubsVersions not yet implemented for rust modules")
472}
473
474func (mod *Module) AllStubsVersions() []string {
475 return nil
Jooyung Han03b51852020-02-26 22:45:42 +0900476}
477
Ivan Lozano183a3212019-10-18 14:18:45 -0700478func (mod *Module) BuildStaticVariant() bool {
479 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700480 if library, ok := mod.compiler.(libraryInterface); ok {
481 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700482 }
483 }
484 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
485}
486
487func (mod *Module) BuildSharedVariant() bool {
488 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700489 if library, ok := mod.compiler.(libraryInterface); ok {
490 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700491 }
492 }
493 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
494}
495
496// Rust module deps don't have a link order (?)
497func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
498
499func (mod *Module) GetDepsInLinkOrder() []android.Path {
500 return []android.Path{}
501}
502
503func (mod *Module) GetStaticVariant() cc.LinkableInterface {
504 return nil
505}
506
507func (mod *Module) Module() android.Module {
508 return mod
509}
510
511func (mod *Module) StubsVersions() []string {
512 // For now, Rust has no stubs versions.
513 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700514 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700515 return []string{}
516 }
517 }
518 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
519}
520
521func (mod *Module) OutputFile() android.OptionalPath {
522 return mod.outputFile
523}
524
525func (mod *Module) InRecovery() bool {
526 // For now, Rust has no notion of the recovery image
527 return false
528}
529func (mod *Module) HasStaticVariant() bool {
530 if mod.GetStaticVariant() != nil {
531 return true
532 }
533 return false
534}
535
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400536func (mod *Module) CoverageFiles() android.Paths {
537 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700538 if !mod.compiler.nativeCoverage() {
539 return android.Paths{}
540 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400541 if library, ok := mod.compiler.(*libraryDecorator); ok {
542 if library.coverageFile != nil {
543 return android.Paths{library.coverageFile}
544 }
545 return android.Paths{}
546 }
547 }
548 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
549}
550
Ivan Lozano183a3212019-10-18 14:18:45 -0700551var _ cc.LinkableInterface = (*Module)(nil)
552
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553func (mod *Module) Init() android.Module {
554 mod.AddProperties(&mod.Properties)
555
556 if mod.compiler != nil {
557 mod.AddProperties(mod.compiler.compilerProps()...)
558 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400559 if mod.coverage != nil {
560 mod.AddProperties(mod.coverage.props()...)
561 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200562 if mod.clippy != nil {
563 mod.AddProperties(mod.clippy.props()...)
564 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400565 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700566 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400567 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400568
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
570
571 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572 return mod
573}
574
575func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
576 return &Module{
577 hod: hod,
578 multilib: multilib,
579 }
580}
581func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
582 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200584 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700585 return module
586}
587
588type ModuleContext interface {
589 android.ModuleContext
590 ModuleContextIntf
591}
592
593type BaseModuleContext interface {
594 android.BaseModuleContext
595 ModuleContextIntf
596}
597
598type DepsContext interface {
599 android.BottomUpMutatorContext
600 ModuleContextIntf
601}
602
603type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200604 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606}
607
608type depsContext struct {
609 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610}
611
612type moduleContext struct {
613 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700614}
615
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200616type baseModuleContext struct {
617 android.BaseModuleContext
618}
619
620func (ctx *moduleContext) RustModule() *Module {
621 return ctx.Module().(*Module)
622}
623
624func (ctx *moduleContext) toolchain() config.Toolchain {
625 return ctx.RustModule().toolchain(ctx)
626}
627
628func (ctx *depsContext) RustModule() *Module {
629 return ctx.Module().(*Module)
630}
631
632func (ctx *depsContext) toolchain() config.Toolchain {
633 return ctx.RustModule().toolchain(ctx)
634}
635
636func (ctx *baseModuleContext) RustModule() *Module {
637 return ctx.Module().(*Module)
638}
639
640func (ctx *baseModuleContext) toolchain() config.Toolchain {
641 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400642}
643
644func (mod *Module) nativeCoverage() bool {
645 return mod.compiler != nil && mod.compiler.nativeCoverage()
646}
647
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
649 if mod.cachedToolchain == nil {
650 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
651 }
652 return mod.cachedToolchain
653}
654
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200655func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
656 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
657}
658
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
660}
661
662func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
663 ctx := &moduleContext{
664 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700666
667 toolchain := mod.toolchain(ctx)
668
669 if !toolchain.Supported() {
670 // This toolchain's unsupported, there's nothing to do for this mod.
671 return
672 }
673
674 deps := mod.depsToPaths(ctx)
675 flags := Flags{
676 Toolchain: toolchain,
677 }
678
679 if mod.compiler != nil {
680 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400681 }
682 if mod.coverage != nil {
683 flags, deps = mod.coverage.flags(ctx, flags, deps)
684 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200685 if mod.clippy != nil {
686 flags, deps = mod.clippy.flags(ctx, flags, deps)
687 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400688
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200689 // SourceProvider needs to call GenerateSource() before compiler calls
690 // compile() so it can provide the source. A SourceProvider has
691 // multiple variants (e.g. source, rlib, dylib). Only the "source"
692 // variant is responsible for effectively generating the source. The
693 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400694 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200695 if mod.compiler.(libraryInterface).source() {
696 mod.sourceProvider.GenerateSource(ctx, deps)
697 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
Colin Crossd3e05ca2020-09-23 16:49:23 -0700698 if lib, ok := mod.compiler.(*libraryDecorator); ok {
699 lib.flagExporter.linkDirs = nil
700 lib.flagExporter.linkObjects = nil
701 lib.flagExporter.depFlags = nil
702 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200703 } else {
704 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
705 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
706 mod.sourceProvider.setOutputFile(sourceLib.sourceProvider.Srcs()[0])
707 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400708 }
709
710 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700711 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400712
Ivan Lozanoffee3342019-08-27 12:03:00 -0700713 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400714 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200715 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400716 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717 }
718}
719
720func (mod *Module) deps(ctx DepsContext) Deps {
721 deps := Deps{}
722
723 if mod.compiler != nil {
724 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400725 }
726 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700727 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700728 }
729
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400730 if mod.coverage != nil {
731 deps = mod.coverage.deps(ctx, deps)
732 }
733
Ivan Lozanoffee3342019-08-27 12:03:00 -0700734 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
735 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700736 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700737 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
738 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
739 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
740
741 return deps
742
743}
744
Ivan Lozanoffee3342019-08-27 12:03:00 -0700745type dependencyTag struct {
746 blueprint.BaseDependencyTag
747 name string
748 library bool
749 proc_macro bool
750}
751
752var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400753 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
754 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
755 dylibDepTag = dependencyTag{name: "dylib", library: true}
756 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
757 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200758 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759)
760
Matthew Maurer0f003b12020-06-29 14:34:06 -0700761type autoDep struct {
762 variation string
763 depTag dependencyTag
764}
765
766var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200767 rlibVariation = "rlib"
768 dylibVariation = "dylib"
769 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
770 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700771)
772
773type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400774 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700775}
776
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400777func (mod *Module) begin(ctx BaseModuleContext) {
778 if mod.coverage != nil {
779 mod.coverage.begin(ctx)
780 }
781}
782
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
784 var depPaths PathDeps
785
786 directRlibDeps := []*Module{}
787 directDylibDeps := []*Module{}
788 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700789 directSharedLibDeps := [](cc.LinkableInterface){}
790 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400791 directSrcProvidersDeps := []*Module{}
792 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700793
794 ctx.VisitDirectDeps(func(dep android.Module) {
795 depName := ctx.OtherModuleName(dep)
796 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400797 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700799
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800 switch depTag {
801 case dylibDepTag:
802 dylib, ok := rustDep.compiler.(libraryInterface)
803 if !ok || !dylib.dylib() {
804 ctx.ModuleErrorf("mod %q not an dylib library", depName)
805 return
806 }
807 directDylibDeps = append(directDylibDeps, rustDep)
808 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
809 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400810
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811 rlib, ok := rustDep.compiler.(libraryInterface)
812 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400813 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814 return
815 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400816 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400818 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 case procMacroDepTag:
820 directProcMacroDeps = append(directProcMacroDeps, rustDep)
821 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400822 case android.SourceDepTag:
823 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
824 // OS/Arch variant is used.
825 var helper string
826 if ctx.Host() {
827 helper = "missing 'host_supported'?"
828 } else {
829 helper = "device module defined?"
830 }
831
832 if dep.Target().Os != ctx.Os() {
833 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
834 return
835 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
836 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
837 return
838 }
839 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 }
841
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400842 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
843 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700844 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400846 depPaths.linkObjects = append(depPaths.linkObjects, lib.exportedLinkObjects()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700847 }
848
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400850 linkFile := rustDep.outputFile
851 if !linkFile.Valid() {
852 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
853 depName, ctx.ModuleName())
854 return
855 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700856 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700857 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
858 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859 }
860 }
861
Ivan Lozano89435d12020-07-31 11:01:18 -0400862 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700863 //Handle C dependencies
864 if _, ok := ccDep.(*Module); !ok {
865 if ccDep.Module().Target().Os != ctx.Os() {
866 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
867 return
868 }
869 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
870 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
871 return
872 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700873 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400874 linkObject := ccDep.OutputFile()
875 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500876
Ivan Lozano2093af22020-08-25 12:48:19 -0400877 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
879 }
880
881 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700882 switch {
883 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400885 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400886 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
887 if mod, ok := ccDep.(*cc.Module); ok {
888 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
889 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400890 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400891 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400892 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893 directStaticLibDeps = append(directStaticLibDeps, ccDep)
894 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700895 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700896 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400897 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400898 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
899 if mod, ok := ccDep.(*cc.Module); ok {
900 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
901 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400902 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400903 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904 directSharedLibDeps = append(directSharedLibDeps, ccDep)
905 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
906 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700907 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400908 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700909 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400910 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 }
912
913 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700914 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
915 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400916 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700917 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700918 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400919
920 if srcDep, ok := dep.(android.SourceFileProducer); ok {
921 switch depTag {
922 case android.SourceDepTag:
923 // These are usually genrules which don't have per-target variants.
924 directSrcDeps = append(directSrcDeps, srcDep)
925 }
926 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700927 })
928
929 var rlibDepFiles RustLibraries
930 for _, dep := range directRlibDeps {
931 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
932 }
933 var dylibDepFiles RustLibraries
934 for _, dep := range directDylibDeps {
935 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
936 }
937 var procMacroDepFiles RustLibraries
938 for _, dep := range directProcMacroDeps {
939 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
940 }
941
942 var staticLibDepFiles android.Paths
943 for _, dep := range directStaticLibDeps {
944 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
945 }
946
947 var sharedLibDepFiles android.Paths
948 for _, dep := range directSharedLibDeps {
949 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
950 }
951
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400952 var srcProviderDepFiles android.Paths
953 for _, dep := range directSrcProvidersDeps {
954 srcs, _ := dep.OutputFiles("")
955 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
956 }
957 for _, dep := range directSrcDeps {
958 srcs := dep.Srcs()
959 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
960 }
961
Ivan Lozanoffee3342019-08-27 12:03:00 -0700962 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
963 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
964 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
965 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
966 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400967 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700968
969 // Dedup exported flags from dependencies
970 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
971 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400972 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
973 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
974 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700975
976 return depPaths
977}
978
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800979func (mod *Module) InstallInData() bool {
980 if mod.compiler == nil {
981 return false
982 }
983 return mod.compiler.inData()
984}
985
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986func linkPathFromFilePath(filepath android.Path) string {
987 return strings.Split(filepath.String(), filepath.Base())[0]
988}
Ivan Lozanod648c432020-02-06 12:05:10 -0500989
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
991 ctx := &depsContext{
992 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700993 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994
995 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700996 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900997 if cc.VersionVariantAvailable(mod) {
998 commonDepVariations = append(commonDepVariations,
999 blueprint.Variation{Mutator: "version", Variation: ""})
1000 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001001 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001002 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -08001003 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001004 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001005 stdLinkage := "dylib-std"
1006 if mod.compiler.staticStd(ctx) {
1007 stdLinkage = "rlib-std"
1008 }
1009
1010 rlibDepVariations := commonDepVariations
1011 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1012 rlibDepVariations = append(rlibDepVariations,
1013 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1014 }
1015
Ivan Lozano52767be2019-10-18 14:49:46 -07001016 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001017 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001018 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001019 rlibDepTag, deps.Rlibs...)
1020 actx.AddVariationDependencies(
1021 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001022 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001023 dylibDepTag, deps.Dylibs...)
1024
Ivan Lozano042504f2020-08-18 14:31:23 -04001025 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1026 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001027 if autoDep.depTag == rlibDepTag {
1028 actx.AddVariationDependencies(
1029 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1030 autoDep.depTag, deps.Rustlibs...)
1031 } else {
1032 actx.AddVariationDependencies(
1033 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1034 autoDep.depTag, deps.Rustlibs...)
1035 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001036 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001037 if deps.Stdlibs != nil {
1038 if mod.compiler.staticStd(ctx) {
1039 actx.AddVariationDependencies(
1040 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1041 rlibDepTag, deps.Stdlibs...)
1042 } else {
1043 actx.AddVariationDependencies(
1044 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1045 dylibDepTag, deps.Stdlibs...)
1046 }
1047 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001048 actx.AddVariationDependencies(append(commonDepVariations,
1049 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001050 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001051 actx.AddVariationDependencies(append(commonDepVariations,
1052 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001053 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001054
Dan Albert92fe7402020-07-15 13:33:30 -07001055 crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001056 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001057 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001058 }
1059 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001060 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001061 }
1062
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001063 if mod.sourceProvider != nil {
1064 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1065 bindgen.Properties.Custom_bindgen != "" {
1066 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1067 bindgen.Properties.Custom_bindgen)
1068 }
1069 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001070 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001071 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001072}
1073
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001074func BeginMutator(ctx android.BottomUpMutatorContext) {
1075 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1076 mod.beginMutator(ctx)
1077 }
1078}
1079
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001080func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1081 ctx := &baseModuleContext{
1082 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001083 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001084
1085 mod.begin(ctx)
1086}
1087
Ivan Lozanoffee3342019-08-27 12:03:00 -07001088func (mod *Module) Name() string {
1089 name := mod.ModuleBase.Name()
1090 if p, ok := mod.compiler.(interface {
1091 Name(string) string
1092 }); ok {
1093 name = p.Name(name)
1094 }
1095 return name
1096}
1097
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001098func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001099 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001100 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001101 }
1102}
1103
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001104var _ android.HostToolProvider = (*Module)(nil)
1105
1106func (mod *Module) HostToolPath() android.OptionalPath {
1107 if !mod.Host() {
1108 return android.OptionalPath{}
1109 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001110 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1111 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001112 }
1113 return android.OptionalPath{}
1114}
1115
Ivan Lozanoffee3342019-08-27 12:03:00 -07001116var Bool = proptools.Bool
1117var BoolDefault = proptools.BoolDefault
1118var String = proptools.String
1119var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001120
1121var _ android.OutputFileProducer = (*Module)(nil)