blob: 77d9a90d52afc312e181a855c5f28ff880846b20 [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
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700121func (mod *Module) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool {
122 return mod.InVendorRamdisk()
123}
124
Colin Cross7228ecd2019-11-18 16:00:16 -0800125func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
126 return mod.InRecovery()
127}
128
129func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
130 return nil
131}
132
133func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
134}
135
Ivan Lozano52767be2019-10-18 14:49:46 -0700136func (mod *Module) BuildStubs() bool {
137 return false
138}
139
140func (mod *Module) HasStubsVariants() bool {
141 return false
142}
143
144func (mod *Module) SelectedStl() string {
145 return ""
146}
147
Ivan Lozano2b262972019-11-21 12:30:50 -0800148func (mod *Module) NonCcVariants() bool {
149 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400150 if _, ok := mod.compiler.(libraryInterface); ok {
151 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800152 }
153 }
154 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
155}
156
Ivan Lozano52767be2019-10-18 14:49:46 -0700157func (mod *Module) ApiLevel() string {
158 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
159}
160
161func (mod *Module) Static() bool {
162 if mod.compiler != nil {
163 if library, ok := mod.compiler.(libraryInterface); ok {
164 return library.static()
165 }
166 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400167 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700168}
169
170func (mod *Module) Shared() bool {
171 if mod.compiler != nil {
172 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400173 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700174 }
175 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400176 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700177}
178
179func (mod *Module) Toc() android.OptionalPath {
180 if mod.compiler != nil {
181 if _, ok := mod.compiler.(libraryInterface); ok {
182 return android.OptionalPath{}
183 }
184 }
185 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
186}
187
Yifan Hong1b3348d2020-01-21 15:53:22 -0800188func (mod *Module) OnlyInRamdisk() bool {
189 return false
190}
191
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700192func (mod *Module) OnlyInVendorRamdisk() bool {
193 return false
194}
195
Ivan Lozano52767be2019-10-18 14:49:46 -0700196func (mod *Module) OnlyInRecovery() bool {
197 return false
198}
199
Colin Crossc511bc52020-04-07 16:50:32 +0000200func (mod *Module) UseSdk() bool {
201 return false
202}
203
Ivan Lozano52767be2019-10-18 14:49:46 -0700204func (mod *Module) UseVndk() bool {
205 return false
206}
207
208func (mod *Module) MustUseVendorVariant() bool {
209 return false
210}
211
212func (mod *Module) IsVndk() bool {
213 return false
214}
215
216func (mod *Module) HasVendorVariant() bool {
217 return false
218}
219
220func (mod *Module) SdkVersion() string {
221 return ""
222}
223
Colin Crossc511bc52020-04-07 16:50:32 +0000224func (mod *Module) AlwaysSdk() bool {
225 return false
226}
227
Jiyong Park2286afd2020-06-16 21:58:53 +0900228func (mod *Module) IsSdkVariant() bool {
229 return false
230}
231
Colin Cross1348ce32020-10-01 13:37:16 -0700232func (mod *Module) SplitPerApiLevel() bool {
233 return false
234}
235
Ivan Lozano52767be2019-10-18 14:49:46 -0700236func (mod *Module) ToolchainLibrary() bool {
237 return false
238}
239
240func (mod *Module) NdkPrebuiltStl() bool {
241 return false
242}
243
244func (mod *Module) StubDecorator() bool {
245 return false
246}
247
Ivan Lozanoffee3342019-08-27 12:03:00 -0700248type Deps struct {
249 Dylibs []string
250 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700251 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400252 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700253 ProcMacros []string
254 SharedLibs []string
255 StaticLibs []string
256
257 CrtBegin, CrtEnd string
258}
259
260type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400261 DyLibs RustLibraries
262 RLibs RustLibraries
263 SharedLibs android.Paths
264 StaticLibs android.Paths
265 ProcMacros RustLibraries
266 linkDirs []string
267 depFlags []string
268 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700269 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700270
Ivan Lozano45901ed2020-07-24 16:05:01 -0400271 // Used by bindgen modules which call clang
272 depClangFlags []string
273 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400274 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400275 depSystemIncludePaths android.Paths
276
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400277 coverageFiles android.Paths
278
Ivan Lozanof1c84332019-09-20 11:00:37 -0700279 CrtBegin android.OptionalPath
280 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700281
282 // Paths to generated source files
283 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700284}
285
286type RustLibraries []RustLibrary
287
288type RustLibrary struct {
289 Path android.Path
290 CrateName string
291}
292
293type compiler interface {
294 compilerFlags(ctx ModuleContext, flags Flags) Flags
295 compilerProps() []interface{}
296 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
297 compilerDeps(ctx DepsContext, deps Deps) Deps
298 crateName() string
299
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800300 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200301 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700302 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400303
304 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400305
306 Disabled() bool
307 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400308
Ivan Lozanodd055472020-09-28 13:22:45 -0400309 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400310}
311
Matthew Maurerbb3add12020-06-25 09:34:12 -0700312type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700313 exportLinkDirs(...string)
314 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400315 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700316}
317
318type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400319 depFlags []string
320 linkDirs []string
321 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700322}
323
Matthew Maurerbb3add12020-06-25 09:34:12 -0700324func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
325 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
326}
327
328func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
329 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
330}
331
Ivan Lozano2093af22020-08-25 12:48:19 -0400332func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
333 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
334}
335
Colin Cross0de8a1e2020-09-18 14:15:30 -0700336func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
337 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
338 Flags: flagExporter.depFlags,
339 LinkDirs: flagExporter.linkDirs,
340 LinkObjects: flagExporter.linkObjects,
341 })
342}
343
Matthew Maurerbb3add12020-06-25 09:34:12 -0700344var _ exportedFlagsProducer = (*flagExporter)(nil)
345
346func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700347 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700348}
349
Colin Cross0de8a1e2020-09-18 14:15:30 -0700350type FlagExporterInfo struct {
351 Flags []string
352 LinkDirs []string // TODO: this should be android.Paths
353 LinkObjects []string // TODO: this should be android.Paths
354}
355
356var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
357
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400358func (mod *Module) isCoverageVariant() bool {
359 return mod.coverage.Properties.IsCoverageVariant
360}
361
362var _ cc.Coverage = (*Module)(nil)
363
364func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
365 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
366}
367
368func (mod *Module) PreventInstall() {
369 mod.Properties.PreventInstall = true
370}
371
372func (mod *Module) HideFromMake() {
373 mod.Properties.HideFromMake = true
374}
375
376func (mod *Module) MarkAsCoverageVariant(coverage bool) {
377 mod.coverage.Properties.IsCoverageVariant = coverage
378}
379
380func (mod *Module) EnableCoverageIfNeeded() {
381 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382}
383
384func defaultsFactory() android.Module {
385 return DefaultsFactory()
386}
387
388type Defaults struct {
389 android.ModuleBase
390 android.DefaultsModuleBase
391}
392
393func DefaultsFactory(props ...interface{}) android.Module {
394 module := &Defaults{}
395
396 module.AddProperties(props...)
397 module.AddProperties(
398 &BaseProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400399 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400 &BaseCompilerProperties{},
401 &BinaryCompilerProperties{},
402 &LibraryCompilerProperties{},
403 &ProcMacroCompilerProperties{},
404 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400405 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700406 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400407 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400408 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200409 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410 )
411
412 android.InitDefaultsModule(module)
413 return module
414}
415
416func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700417 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700418}
419
Ivan Lozano183a3212019-10-18 14:18:45 -0700420func (mod *Module) CcLibrary() bool {
421 if mod.compiler != nil {
422 if _, ok := mod.compiler.(*libraryDecorator); ok {
423 return true
424 }
425 }
426 return false
427}
428
429func (mod *Module) CcLibraryInterface() bool {
430 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400431 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
432 // VariantIs{Static,Shared} is set.
433 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700434 return true
435 }
436 }
437 return false
438}
439
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800440func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700441 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700442 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800443 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700444 }
445 }
446 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
447}
448
449func (mod *Module) SetStatic() {
450 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700451 if library, ok := mod.compiler.(libraryInterface); ok {
452 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700453 return
454 }
455 }
456 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
457}
458
459func (mod *Module) SetShared() {
460 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700461 if library, ok := mod.compiler.(libraryInterface); ok {
462 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700463 return
464 }
465 }
466 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
467}
468
469func (mod *Module) SetBuildStubs() {
470 panic("SetBuildStubs not yet implemented for rust modules")
471}
472
Colin Crossd1f898e2020-08-18 18:35:15 -0700473func (mod *Module) SetStubsVersion(string) {
474 panic("SetStubsVersion not yet implemented for rust modules")
Ivan Lozano183a3212019-10-18 14:18:45 -0700475}
476
Jooyung Han03b51852020-02-26 22:45:42 +0900477func (mod *Module) StubsVersion() string {
Colin Crossd1f898e2020-08-18 18:35:15 -0700478 panic("StubsVersion not yet implemented for rust modules")
479}
480
481func (mod *Module) SetAllStubsVersions([]string) {
482 panic("SetAllStubsVersions not yet implemented for rust modules")
483}
484
485func (mod *Module) AllStubsVersions() []string {
486 return nil
Jooyung Han03b51852020-02-26 22:45:42 +0900487}
488
Ivan Lozano183a3212019-10-18 14:18:45 -0700489func (mod *Module) BuildStaticVariant() bool {
490 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700491 if library, ok := mod.compiler.(libraryInterface); ok {
492 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700493 }
494 }
495 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
496}
497
498func (mod *Module) BuildSharedVariant() bool {
499 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700500 if library, ok := mod.compiler.(libraryInterface); ok {
501 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700502 }
503 }
504 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
505}
506
Ivan Lozano183a3212019-10-18 14:18:45 -0700507func (mod *Module) Module() android.Module {
508 return mod
509}
510
Colin Cross3572cf72020-10-01 15:58:11 -0700511func (mod *Module) StubsVersions(ctx android.BaseMutatorContext) []string {
Ivan Lozano183a3212019-10-18 14:18:45 -0700512 // 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}
Ivan Lozano183a3212019-10-18 14:18:45 -0700529
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400530func (mod *Module) CoverageFiles() android.Paths {
531 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700532 if !mod.compiler.nativeCoverage() {
533 return android.Paths{}
534 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400535 if library, ok := mod.compiler.(*libraryDecorator); ok {
536 if library.coverageFile != nil {
537 return android.Paths{library.coverageFile}
538 }
539 return android.Paths{}
540 }
541 }
542 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
543}
544
Ivan Lozano183a3212019-10-18 14:18:45 -0700545var _ cc.LinkableInterface = (*Module)(nil)
546
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547func (mod *Module) Init() android.Module {
548 mod.AddProperties(&mod.Properties)
549
550 if mod.compiler != nil {
551 mod.AddProperties(mod.compiler.compilerProps()...)
552 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400553 if mod.coverage != nil {
554 mod.AddProperties(mod.coverage.props()...)
555 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200556 if mod.clippy != nil {
557 mod.AddProperties(mod.clippy.props()...)
558 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400559 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700560 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400561 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400562
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
564
565 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700566 return mod
567}
568
569func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
570 return &Module{
571 hod: hod,
572 multilib: multilib,
573 }
574}
575func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
576 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400577 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200578 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579 return module
580}
581
582type ModuleContext interface {
583 android.ModuleContext
584 ModuleContextIntf
585}
586
587type BaseModuleContext interface {
588 android.BaseModuleContext
589 ModuleContextIntf
590}
591
592type DepsContext interface {
593 android.BottomUpMutatorContext
594 ModuleContextIntf
595}
596
597type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200598 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600}
601
602type depsContext struct {
603 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604}
605
606type moduleContext struct {
607 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700608}
609
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200610type baseModuleContext struct {
611 android.BaseModuleContext
612}
613
614func (ctx *moduleContext) RustModule() *Module {
615 return ctx.Module().(*Module)
616}
617
618func (ctx *moduleContext) toolchain() config.Toolchain {
619 return ctx.RustModule().toolchain(ctx)
620}
621
622func (ctx *depsContext) RustModule() *Module {
623 return ctx.Module().(*Module)
624}
625
626func (ctx *depsContext) toolchain() config.Toolchain {
627 return ctx.RustModule().toolchain(ctx)
628}
629
630func (ctx *baseModuleContext) RustModule() *Module {
631 return ctx.Module().(*Module)
632}
633
634func (ctx *baseModuleContext) toolchain() config.Toolchain {
635 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400636}
637
638func (mod *Module) nativeCoverage() bool {
639 return mod.compiler != nil && mod.compiler.nativeCoverage()
640}
641
Ivan Lozanoffee3342019-08-27 12:03:00 -0700642func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
643 if mod.cachedToolchain == nil {
644 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
645 }
646 return mod.cachedToolchain
647}
648
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200649func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
650 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
651}
652
Ivan Lozanoffee3342019-08-27 12:03:00 -0700653func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
654}
655
656func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
657 ctx := &moduleContext{
658 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700660
661 toolchain := mod.toolchain(ctx)
662
663 if !toolchain.Supported() {
664 // This toolchain's unsupported, there's nothing to do for this mod.
665 return
666 }
667
668 deps := mod.depsToPaths(ctx)
669 flags := Flags{
670 Toolchain: toolchain,
671 }
672
673 if mod.compiler != nil {
674 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400675 }
676 if mod.coverage != nil {
677 flags, deps = mod.coverage.flags(ctx, flags, deps)
678 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200679 if mod.clippy != nil {
680 flags, deps = mod.clippy.flags(ctx, flags, deps)
681 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400682
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200683 // SourceProvider needs to call GenerateSource() before compiler calls
684 // compile() so it can provide the source. A SourceProvider has
685 // multiple variants (e.g. source, rlib, dylib). Only the "source"
686 // variant is responsible for effectively generating the source. The
687 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400688 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200689 if mod.compiler.(libraryInterface).source() {
690 mod.sourceProvider.GenerateSource(ctx, deps)
691 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
692 } else {
693 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
694 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700695 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200696 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400697 }
698
699 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400701
Ivan Lozanoffee3342019-08-27 12:03:00 -0700702 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400703 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200704 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400705 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706 }
707}
708
709func (mod *Module) deps(ctx DepsContext) Deps {
710 deps := Deps{}
711
712 if mod.compiler != nil {
713 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400714 }
715 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700716 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717 }
718
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400719 if mod.coverage != nil {
720 deps = mod.coverage.deps(ctx, deps)
721 }
722
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
724 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700725 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
727 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
728 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
729
730 return deps
731
732}
733
Ivan Lozanoffee3342019-08-27 12:03:00 -0700734type dependencyTag struct {
735 blueprint.BaseDependencyTag
736 name string
737 library bool
738 proc_macro bool
739}
740
741var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400742 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
743 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
744 dylibDepTag = dependencyTag{name: "dylib", library: true}
745 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
746 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200747 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748)
749
Matthew Maurer0f003b12020-06-29 14:34:06 -0700750type autoDep struct {
751 variation string
752 depTag dependencyTag
753}
754
755var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200756 rlibVariation = "rlib"
757 dylibVariation = "dylib"
758 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
759 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700760)
761
762type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400763 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700764}
765
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766func (mod *Module) begin(ctx BaseModuleContext) {
767 if mod.coverage != nil {
768 mod.coverage.begin(ctx)
769 }
770}
771
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
773 var depPaths PathDeps
774
775 directRlibDeps := []*Module{}
776 directDylibDeps := []*Module{}
777 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700778 directSharedLibDeps := [](cc.LinkableInterface){}
779 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400780 directSrcProvidersDeps := []*Module{}
781 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782
783 ctx.VisitDirectDeps(func(dep android.Module) {
784 depName := ctx.OtherModuleName(dep)
785 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400786 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700788
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789 switch depTag {
790 case dylibDepTag:
791 dylib, ok := rustDep.compiler.(libraryInterface)
792 if !ok || !dylib.dylib() {
793 ctx.ModuleErrorf("mod %q not an dylib library", depName)
794 return
795 }
796 directDylibDeps = append(directDylibDeps, rustDep)
797 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
798 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400799
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800 rlib, ok := rustDep.compiler.(libraryInterface)
801 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400802 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803 return
804 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400805 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400807 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808 case procMacroDepTag:
809 directProcMacroDeps = append(directProcMacroDeps, rustDep)
810 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400811 case android.SourceDepTag:
812 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
813 // OS/Arch variant is used.
814 var helper string
815 if ctx.Host() {
816 helper = "missing 'host_supported'?"
817 } else {
818 helper = "device module defined?"
819 }
820
821 if dep.Target().Os != ctx.Os() {
822 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
823 return
824 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
825 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
826 return
827 }
828 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 }
830
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400831 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700832 if depTag != procMacroDepTag {
833 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
834 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
835 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
836 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700837 }
838
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400840 linkFile := rustDep.outputFile
841 if !linkFile.Valid() {
842 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
843 depName, ctx.ModuleName())
844 return
845 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700847 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
848 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 }
850 }
851
Ivan Lozano89435d12020-07-31 11:01:18 -0400852 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700853 //Handle C dependencies
854 if _, ok := ccDep.(*Module); !ok {
855 if ccDep.Module().Target().Os != ctx.Os() {
856 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
857 return
858 }
859 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
860 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
861 return
862 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700863 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400864 linkObject := ccDep.OutputFile()
865 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500866
Ivan Lozano2093af22020-08-25 12:48:19 -0400867 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700868 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
869 }
870
871 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700872 switch {
873 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400875 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700876 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
877 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
878 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
879 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
880 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400881 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 directStaticLibDeps = append(directStaticLibDeps, ccDep)
883 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700884 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400886 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700887 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
888 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
889 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
890 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
891 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892 directSharedLibDeps = append(directSharedLibDeps, ccDep)
893 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
894 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700895 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400896 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700897 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400898 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899 }
900
901 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700902 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
903 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400904 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700905 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400907
908 if srcDep, ok := dep.(android.SourceFileProducer); ok {
909 switch depTag {
910 case android.SourceDepTag:
911 // These are usually genrules which don't have per-target variants.
912 directSrcDeps = append(directSrcDeps, srcDep)
913 }
914 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915 })
916
917 var rlibDepFiles RustLibraries
918 for _, dep := range directRlibDeps {
919 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
920 }
921 var dylibDepFiles RustLibraries
922 for _, dep := range directDylibDeps {
923 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
924 }
925 var procMacroDepFiles RustLibraries
926 for _, dep := range directProcMacroDeps {
927 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
928 }
929
930 var staticLibDepFiles android.Paths
931 for _, dep := range directStaticLibDeps {
932 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
933 }
934
935 var sharedLibDepFiles android.Paths
936 for _, dep := range directSharedLibDeps {
937 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
938 }
939
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400940 var srcProviderDepFiles android.Paths
941 for _, dep := range directSrcProvidersDeps {
942 srcs, _ := dep.OutputFiles("")
943 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
944 }
945 for _, dep := range directSrcDeps {
946 srcs := dep.Srcs()
947 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
948 }
949
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
951 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
952 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
953 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
954 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400955 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956
957 // Dedup exported flags from dependencies
958 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
959 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400960 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
961 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
962 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963
964 return depPaths
965}
966
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800967func (mod *Module) InstallInData() bool {
968 if mod.compiler == nil {
969 return false
970 }
971 return mod.compiler.inData()
972}
973
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974func linkPathFromFilePath(filepath android.Path) string {
975 return strings.Split(filepath.String(), filepath.Base())[0]
976}
Ivan Lozanod648c432020-02-06 12:05:10 -0500977
Ivan Lozanoffee3342019-08-27 12:03:00 -0700978func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
979 ctx := &depsContext{
980 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700981 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982
983 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700984 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700985 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700986 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800987 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700988 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400989
Ivan Lozano2b081132020-09-08 12:46:52 -0400990 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400991 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400992 stdLinkage = "rlib-std"
993 }
994
995 rlibDepVariations := commonDepVariations
996 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
997 rlibDepVariations = append(rlibDepVariations,
998 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
999 }
1000
Ivan Lozano52767be2019-10-18 14:49:46 -07001001 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001002 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001003 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001004 rlibDepTag, deps.Rlibs...)
1005 actx.AddVariationDependencies(
1006 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001007 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001008 dylibDepTag, deps.Dylibs...)
1009
Ivan Lozano042504f2020-08-18 14:31:23 -04001010 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1011 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001012 if autoDep.depTag == rlibDepTag {
1013 actx.AddVariationDependencies(
1014 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1015 autoDep.depTag, deps.Rustlibs...)
1016 } else {
1017 actx.AddVariationDependencies(
1018 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1019 autoDep.depTag, deps.Rustlibs...)
1020 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001021 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001022 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001023 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001024 actx.AddVariationDependencies(
1025 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1026 rlibDepTag, deps.Stdlibs...)
1027 } else {
1028 actx.AddVariationDependencies(
1029 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1030 dylibDepTag, deps.Stdlibs...)
1031 }
1032 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001033 actx.AddVariationDependencies(append(commonDepVariations,
1034 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001035 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001036 actx.AddVariationDependencies(append(commonDepVariations,
1037 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001038 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001039
Colin Cross565cafd2020-09-25 18:47:38 -07001040 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001041 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001042 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001043 }
1044 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001045 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001046 }
1047
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001048 if mod.sourceProvider != nil {
1049 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1050 bindgen.Properties.Custom_bindgen != "" {
1051 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1052 bindgen.Properties.Custom_bindgen)
1053 }
1054 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001055 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001056 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001057}
1058
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001059func BeginMutator(ctx android.BottomUpMutatorContext) {
1060 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1061 mod.beginMutator(ctx)
1062 }
1063}
1064
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001065func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1066 ctx := &baseModuleContext{
1067 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001068 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001069
1070 mod.begin(ctx)
1071}
1072
Ivan Lozanoffee3342019-08-27 12:03:00 -07001073func (mod *Module) Name() string {
1074 name := mod.ModuleBase.Name()
1075 if p, ok := mod.compiler.(interface {
1076 Name(string) string
1077 }); ok {
1078 name = p.Name(name)
1079 }
1080 return name
1081}
1082
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001083func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001084 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001085 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001086 }
1087}
1088
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001089var _ android.HostToolProvider = (*Module)(nil)
1090
1091func (mod *Module) HostToolPath() android.OptionalPath {
1092 if !mod.Host() {
1093 return android.OptionalPath{}
1094 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001095 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1096 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001097 }
1098 return android.OptionalPath{}
1099}
1100
Ivan Lozanoffee3342019-08-27 12:03:00 -07001101var Bool = proptools.Bool
1102var BoolDefault = proptools.BoolDefault
1103var String = proptools.String
1104var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001105
1106var _ android.OutputFileProducer = (*Module)(nil)