blob: 29606fafa6b83ce0121df42fdec834e48d341cad [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Ivan Lozano183a3212019-10-18 14:18:45 -070018 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020026 cc_config "android/soong/cc/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027 "android/soong/rust/config"
28)
29
30var pctx = android.NewPackageContext("android/soong/rust")
31
32func init() {
33 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070034
35 android.AddNeverAllowRules(
36 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070037 NotIn(config.RustAllowedPaths...).
38 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070039
40 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040051 GlobalRustFlags []string // Flags that apply globally to rust
52 GlobalLinkFlags []string // Flags that apply globally to linker
53 RustFlags []string // Flags that apply to rust
54 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070056 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040057 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61type BaseProperties struct {
62 AndroidMkRlibs []string
63 AndroidMkDylibs []string
64 AndroidMkProcMacroLibs []string
65 AndroidMkSharedLibs []string
66 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040067
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040068 SubName string `blueprint:"mutated"`
69
Ivan Lozano43845682020-07-09 21:03:28 -040070 PreventInstall bool
71 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070072}
73
74type Module struct {
75 android.ModuleBase
76 android.DefaultableModuleBase
77
78 Properties BaseProperties
79
80 hod android.HostOrDeviceSupported
81 multilib android.Multilib
82
83 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040084 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020085 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070086 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040087 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070088 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040089
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020090 outputFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
Ivan Lozano43845682020-07-09 21:03:28 -040093func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
94 switch tag {
95 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070096 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -040097 return mod.sourceProvider.Srcs(), nil
98 } else {
99 if mod.outputFile.Valid() {
100 return android.Paths{mod.outputFile.Path()}, nil
101 }
102 return android.Paths{}, nil
103 }
104 default:
105 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
106 }
107}
108
Colin Cross7228ecd2019-11-18 16:00:16 -0800109var _ android.ImageInterface = (*Module)(nil)
110
111func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
112
113func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
114 return true
115}
116
Yifan Hong1b3348d2020-01-21 15:53:22 -0800117func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
118 return mod.InRamdisk()
119}
120
Colin Cross7228ecd2019-11-18 16:00:16 -0800121func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
122 return mod.InRecovery()
123}
124
125func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
126 return nil
127}
128
129func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
130}
131
Ivan Lozano52767be2019-10-18 14:49:46 -0700132func (mod *Module) BuildStubs() bool {
133 return false
134}
135
136func (mod *Module) HasStubsVariants() bool {
137 return false
138}
139
140func (mod *Module) SelectedStl() string {
141 return ""
142}
143
Ivan Lozano2b262972019-11-21 12:30:50 -0800144func (mod *Module) NonCcVariants() bool {
145 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400146 if _, ok := mod.compiler.(libraryInterface); ok {
147 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800148 }
149 }
150 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
151}
152
Ivan Lozano52767be2019-10-18 14:49:46 -0700153func (mod *Module) ApiLevel() string {
154 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
155}
156
157func (mod *Module) Static() bool {
158 if mod.compiler != nil {
159 if library, ok := mod.compiler.(libraryInterface); ok {
160 return library.static()
161 }
162 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400163 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700164}
165
166func (mod *Module) Shared() bool {
167 if mod.compiler != nil {
168 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400169 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700170 }
171 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400172 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700173}
174
175func (mod *Module) Toc() android.OptionalPath {
176 if mod.compiler != nil {
177 if _, ok := mod.compiler.(libraryInterface); ok {
178 return android.OptionalPath{}
179 }
180 }
181 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
182}
183
Yifan Hong1b3348d2020-01-21 15:53:22 -0800184func (mod *Module) OnlyInRamdisk() bool {
185 return false
186}
187
Ivan Lozano52767be2019-10-18 14:49:46 -0700188func (mod *Module) OnlyInRecovery() bool {
189 return false
190}
191
Colin Crossc511bc52020-04-07 16:50:32 +0000192func (mod *Module) UseSdk() bool {
193 return false
194}
195
Ivan Lozano52767be2019-10-18 14:49:46 -0700196func (mod *Module) UseVndk() bool {
197 return false
198}
199
200func (mod *Module) MustUseVendorVariant() bool {
201 return false
202}
203
204func (mod *Module) IsVndk() bool {
205 return false
206}
207
208func (mod *Module) HasVendorVariant() bool {
209 return false
210}
211
212func (mod *Module) SdkVersion() string {
213 return ""
214}
215
Colin Crossc511bc52020-04-07 16:50:32 +0000216func (mod *Module) AlwaysSdk() bool {
217 return false
218}
219
Jiyong Park2286afd2020-06-16 21:58:53 +0900220func (mod *Module) IsSdkVariant() bool {
221 return false
222}
223
Colin Cross1348ce32020-10-01 13:37:16 -0700224func (mod *Module) SplitPerApiLevel() bool {
225 return false
226}
227
Ivan Lozano52767be2019-10-18 14:49:46 -0700228func (mod *Module) ToolchainLibrary() bool {
229 return false
230}
231
232func (mod *Module) NdkPrebuiltStl() bool {
233 return false
234}
235
236func (mod *Module) StubDecorator() bool {
237 return false
238}
239
Ivan Lozanoffee3342019-08-27 12:03:00 -0700240type Deps struct {
241 Dylibs []string
242 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700243 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400244 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245 ProcMacros []string
246 SharedLibs []string
247 StaticLibs []string
248
249 CrtBegin, CrtEnd string
250}
251
252type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400253 DyLibs RustLibraries
254 RLibs RustLibraries
255 SharedLibs android.Paths
256 StaticLibs android.Paths
257 ProcMacros RustLibraries
258 linkDirs []string
259 depFlags []string
260 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700262
Ivan Lozano45901ed2020-07-24 16:05:01 -0400263 // Used by bindgen modules which call clang
264 depClangFlags []string
265 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400266 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400267 depSystemIncludePaths android.Paths
268
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400269 coverageFiles android.Paths
270
Ivan Lozanof1c84332019-09-20 11:00:37 -0700271 CrtBegin android.OptionalPath
272 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700273
274 // Paths to generated source files
275 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276}
277
278type RustLibraries []RustLibrary
279
280type RustLibrary struct {
281 Path android.Path
282 CrateName string
283}
284
285type compiler interface {
286 compilerFlags(ctx ModuleContext, flags Flags) Flags
287 compilerProps() []interface{}
288 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
289 compilerDeps(ctx DepsContext, deps Deps) Deps
290 crateName() string
291
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800292 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200293 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400295
296 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400297
298 Disabled() bool
299 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400300
Ivan Lozanodd055472020-09-28 13:22:45 -0400301 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400302}
303
Matthew Maurerbb3add12020-06-25 09:34:12 -0700304type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700305 exportLinkDirs(...string)
306 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400307 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700308}
309
310type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400311 depFlags []string
312 linkDirs []string
313 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700314}
315
Matthew Maurerbb3add12020-06-25 09:34:12 -0700316func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
317 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
318}
319
320func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
321 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
322}
323
Ivan Lozano2093af22020-08-25 12:48:19 -0400324func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
325 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
326}
327
Colin Cross0de8a1e2020-09-18 14:15:30 -0700328func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
329 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
330 Flags: flagExporter.depFlags,
331 LinkDirs: flagExporter.linkDirs,
332 LinkObjects: flagExporter.linkObjects,
333 })
334}
335
Matthew Maurerbb3add12020-06-25 09:34:12 -0700336var _ exportedFlagsProducer = (*flagExporter)(nil)
337
338func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700339 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700340}
341
Colin Cross0de8a1e2020-09-18 14:15:30 -0700342type FlagExporterInfo struct {
343 Flags []string
344 LinkDirs []string // TODO: this should be android.Paths
345 LinkObjects []string // TODO: this should be android.Paths
346}
347
348var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
349
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400350func (mod *Module) isCoverageVariant() bool {
351 return mod.coverage.Properties.IsCoverageVariant
352}
353
354var _ cc.Coverage = (*Module)(nil)
355
356func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
357 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
358}
359
360func (mod *Module) PreventInstall() {
361 mod.Properties.PreventInstall = true
362}
363
364func (mod *Module) HideFromMake() {
365 mod.Properties.HideFromMake = true
366}
367
368func (mod *Module) MarkAsCoverageVariant(coverage bool) {
369 mod.coverage.Properties.IsCoverageVariant = coverage
370}
371
372func (mod *Module) EnableCoverageIfNeeded() {
373 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700374}
375
376func defaultsFactory() android.Module {
377 return DefaultsFactory()
378}
379
380type Defaults struct {
381 android.ModuleBase
382 android.DefaultsModuleBase
383}
384
385func DefaultsFactory(props ...interface{}) android.Module {
386 module := &Defaults{}
387
388 module.AddProperties(props...)
389 module.AddProperties(
390 &BaseProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400391 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700392 &BaseCompilerProperties{},
393 &BinaryCompilerProperties{},
394 &LibraryCompilerProperties{},
395 &ProcMacroCompilerProperties{},
396 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400397 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700398 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400399 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400400 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200401 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700402 )
403
404 android.InitDefaultsModule(module)
405 return module
406}
407
408func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700409 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410}
411
Ivan Lozano183a3212019-10-18 14:18:45 -0700412func (mod *Module) CcLibrary() bool {
413 if mod.compiler != nil {
414 if _, ok := mod.compiler.(*libraryDecorator); ok {
415 return true
416 }
417 }
418 return false
419}
420
421func (mod *Module) CcLibraryInterface() bool {
422 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400423 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
424 // VariantIs{Static,Shared} is set.
425 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700426 return true
427 }
428 }
429 return false
430}
431
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800432func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700433 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700434 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800435 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700436 }
437 }
438 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
439}
440
441func (mod *Module) SetStatic() {
442 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700443 if library, ok := mod.compiler.(libraryInterface); ok {
444 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700445 return
446 }
447 }
448 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
449}
450
451func (mod *Module) SetShared() {
452 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700453 if library, ok := mod.compiler.(libraryInterface); ok {
454 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700455 return
456 }
457 }
458 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
459}
460
461func (mod *Module) SetBuildStubs() {
462 panic("SetBuildStubs not yet implemented for rust modules")
463}
464
Colin Crossd1f898e2020-08-18 18:35:15 -0700465func (mod *Module) SetStubsVersion(string) {
466 panic("SetStubsVersion not yet implemented for rust modules")
Ivan Lozano183a3212019-10-18 14:18:45 -0700467}
468
Jooyung Han03b51852020-02-26 22:45:42 +0900469func (mod *Module) StubsVersion() string {
Colin Crossd1f898e2020-08-18 18:35:15 -0700470 panic("StubsVersion not yet implemented for rust modules")
471}
472
473func (mod *Module) SetAllStubsVersions([]string) {
474 panic("SetAllStubsVersions not yet implemented for rust modules")
475}
476
477func (mod *Module) AllStubsVersions() []string {
478 return nil
Jooyung Han03b51852020-02-26 22:45:42 +0900479}
480
Ivan Lozano183a3212019-10-18 14:18:45 -0700481func (mod *Module) BuildStaticVariant() bool {
482 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700483 if library, ok := mod.compiler.(libraryInterface); ok {
484 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700485 }
486 }
487 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
488}
489
490func (mod *Module) BuildSharedVariant() bool {
491 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700492 if library, ok := mod.compiler.(libraryInterface); ok {
493 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700494 }
495 }
496 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
497}
498
Ivan Lozano183a3212019-10-18 14:18:45 -0700499func (mod *Module) Module() android.Module {
500 return mod
501}
502
503func (mod *Module) StubsVersions() []string {
504 // For now, Rust has no stubs versions.
505 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700506 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700507 return []string{}
508 }
509 }
510 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
511}
512
513func (mod *Module) OutputFile() android.OptionalPath {
514 return mod.outputFile
515}
516
517func (mod *Module) InRecovery() bool {
518 // For now, Rust has no notion of the recovery image
519 return false
520}
Ivan Lozano183a3212019-10-18 14:18:45 -0700521
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400522func (mod *Module) CoverageFiles() android.Paths {
523 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700524 if !mod.compiler.nativeCoverage() {
525 return android.Paths{}
526 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400527 if library, ok := mod.compiler.(*libraryDecorator); ok {
528 if library.coverageFile != nil {
529 return android.Paths{library.coverageFile}
530 }
531 return android.Paths{}
532 }
533 }
534 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
535}
536
Ivan Lozano183a3212019-10-18 14:18:45 -0700537var _ cc.LinkableInterface = (*Module)(nil)
538
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539func (mod *Module) Init() android.Module {
540 mod.AddProperties(&mod.Properties)
541
542 if mod.compiler != nil {
543 mod.AddProperties(mod.compiler.compilerProps()...)
544 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400545 if mod.coverage != nil {
546 mod.AddProperties(mod.coverage.props()...)
547 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200548 if mod.clippy != nil {
549 mod.AddProperties(mod.clippy.props()...)
550 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400551 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700552 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400553 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400554
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
556
557 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700558 return mod
559}
560
561func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
562 return &Module{
563 hod: hod,
564 multilib: multilib,
565 }
566}
567func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
568 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400569 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200570 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571 return module
572}
573
574type ModuleContext interface {
575 android.ModuleContext
576 ModuleContextIntf
577}
578
579type BaseModuleContext interface {
580 android.BaseModuleContext
581 ModuleContextIntf
582}
583
584type DepsContext interface {
585 android.BottomUpMutatorContext
586 ModuleContextIntf
587}
588
589type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200590 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700591 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700592}
593
594type depsContext struct {
595 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596}
597
598type moduleContext struct {
599 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600}
601
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200602type baseModuleContext struct {
603 android.BaseModuleContext
604}
605
606func (ctx *moduleContext) RustModule() *Module {
607 return ctx.Module().(*Module)
608}
609
610func (ctx *moduleContext) toolchain() config.Toolchain {
611 return ctx.RustModule().toolchain(ctx)
612}
613
614func (ctx *depsContext) RustModule() *Module {
615 return ctx.Module().(*Module)
616}
617
618func (ctx *depsContext) toolchain() config.Toolchain {
619 return ctx.RustModule().toolchain(ctx)
620}
621
622func (ctx *baseModuleContext) RustModule() *Module {
623 return ctx.Module().(*Module)
624}
625
626func (ctx *baseModuleContext) toolchain() config.Toolchain {
627 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400628}
629
630func (mod *Module) nativeCoverage() bool {
631 return mod.compiler != nil && mod.compiler.nativeCoverage()
632}
633
Ivan Lozanoffee3342019-08-27 12:03:00 -0700634func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
635 if mod.cachedToolchain == nil {
636 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
637 }
638 return mod.cachedToolchain
639}
640
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200641func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
642 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
643}
644
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
646}
647
648func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
649 ctx := &moduleContext{
650 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652
653 toolchain := mod.toolchain(ctx)
654
655 if !toolchain.Supported() {
656 // This toolchain's unsupported, there's nothing to do for this mod.
657 return
658 }
659
660 deps := mod.depsToPaths(ctx)
661 flags := Flags{
662 Toolchain: toolchain,
663 }
664
665 if mod.compiler != nil {
666 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400667 }
668 if mod.coverage != nil {
669 flags, deps = mod.coverage.flags(ctx, flags, deps)
670 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200671 if mod.clippy != nil {
672 flags, deps = mod.clippy.flags(ctx, flags, deps)
673 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400674
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200675 // SourceProvider needs to call GenerateSource() before compiler calls
676 // compile() so it can provide the source. A SourceProvider has
677 // multiple variants (e.g. source, rlib, dylib). Only the "source"
678 // variant is responsible for effectively generating the source. The
679 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400680 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200681 if mod.compiler.(libraryInterface).source() {
682 mod.sourceProvider.GenerateSource(ctx, deps)
683 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
684 } else {
685 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
686 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
687 mod.sourceProvider.setOutputFile(sourceLib.sourceProvider.Srcs()[0])
688 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400689 }
690
691 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700692 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400693
Ivan Lozanoffee3342019-08-27 12:03:00 -0700694 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400695 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200696 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400697 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700698 }
699}
700
701func (mod *Module) deps(ctx DepsContext) Deps {
702 deps := Deps{}
703
704 if mod.compiler != nil {
705 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400706 }
707 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700708 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709 }
710
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400711 if mod.coverage != nil {
712 deps = mod.coverage.deps(ctx, deps)
713 }
714
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
716 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700717 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
719 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
720 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
721
722 return deps
723
724}
725
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726type dependencyTag struct {
727 blueprint.BaseDependencyTag
728 name string
729 library bool
730 proc_macro bool
731}
732
733var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400734 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
735 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
736 dylibDepTag = dependencyTag{name: "dylib", library: true}
737 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
738 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200739 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740)
741
Matthew Maurer0f003b12020-06-29 14:34:06 -0700742type autoDep struct {
743 variation string
744 depTag dependencyTag
745}
746
747var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200748 rlibVariation = "rlib"
749 dylibVariation = "dylib"
750 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
751 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700752)
753
754type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400755 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700756}
757
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400758func (mod *Module) begin(ctx BaseModuleContext) {
759 if mod.coverage != nil {
760 mod.coverage.begin(ctx)
761 }
762}
763
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
765 var depPaths PathDeps
766
767 directRlibDeps := []*Module{}
768 directDylibDeps := []*Module{}
769 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700770 directSharedLibDeps := [](cc.LinkableInterface){}
771 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400772 directSrcProvidersDeps := []*Module{}
773 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700774
775 ctx.VisitDirectDeps(func(dep android.Module) {
776 depName := ctx.OtherModuleName(dep)
777 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400778 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700780
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781 switch depTag {
782 case dylibDepTag:
783 dylib, ok := rustDep.compiler.(libraryInterface)
784 if !ok || !dylib.dylib() {
785 ctx.ModuleErrorf("mod %q not an dylib library", depName)
786 return
787 }
788 directDylibDeps = append(directDylibDeps, rustDep)
789 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
790 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400791
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792 rlib, ok := rustDep.compiler.(libraryInterface)
793 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400794 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 return
796 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400797 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400799 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800 case procMacroDepTag:
801 directProcMacroDeps = append(directProcMacroDeps, rustDep)
802 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400803 case android.SourceDepTag:
804 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
805 // OS/Arch variant is used.
806 var helper string
807 if ctx.Host() {
808 helper = "missing 'host_supported'?"
809 } else {
810 helper = "device module defined?"
811 }
812
813 if dep.Target().Os != ctx.Os() {
814 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
815 return
816 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
817 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
818 return
819 }
820 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821 }
822
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400823 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700824 if depTag != procMacroDepTag {
825 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
826 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
827 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
828 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 }
830
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400832 linkFile := rustDep.outputFile
833 if !linkFile.Valid() {
834 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
835 depName, ctx.ModuleName())
836 return
837 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700838 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700839 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
840 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841 }
842 }
843
Ivan Lozano89435d12020-07-31 11:01:18 -0400844 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700845 //Handle C dependencies
846 if _, ok := ccDep.(*Module); !ok {
847 if ccDep.Module().Target().Os != ctx.Os() {
848 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
849 return
850 }
851 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
852 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
853 return
854 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700855 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400856 linkObject := ccDep.OutputFile()
857 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500858
Ivan Lozano2093af22020-08-25 12:48:19 -0400859 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700860 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
861 }
862
863 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700864 switch {
865 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400867 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700868 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
869 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
870 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
871 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
872 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400873 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 directStaticLibDeps = append(directStaticLibDeps, ccDep)
875 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700876 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700877 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400878 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700879 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
880 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
881 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
882 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
883 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 directSharedLibDeps = append(directSharedLibDeps, ccDep)
885 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
886 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700887 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400888 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700889 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400890 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891 }
892
893 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700894 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
895 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400896 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400899
900 if srcDep, ok := dep.(android.SourceFileProducer); ok {
901 switch depTag {
902 case android.SourceDepTag:
903 // These are usually genrules which don't have per-target variants.
904 directSrcDeps = append(directSrcDeps, srcDep)
905 }
906 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907 })
908
909 var rlibDepFiles RustLibraries
910 for _, dep := range directRlibDeps {
911 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
912 }
913 var dylibDepFiles RustLibraries
914 for _, dep := range directDylibDeps {
915 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
916 }
917 var procMacroDepFiles RustLibraries
918 for _, dep := range directProcMacroDeps {
919 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
920 }
921
922 var staticLibDepFiles android.Paths
923 for _, dep := range directStaticLibDeps {
924 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
925 }
926
927 var sharedLibDepFiles android.Paths
928 for _, dep := range directSharedLibDeps {
929 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
930 }
931
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400932 var srcProviderDepFiles android.Paths
933 for _, dep := range directSrcProvidersDeps {
934 srcs, _ := dep.OutputFiles("")
935 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
936 }
937 for _, dep := range directSrcDeps {
938 srcs := dep.Srcs()
939 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
940 }
941
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
943 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
944 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
945 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
946 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400947 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948
949 // Dedup exported flags from dependencies
950 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
951 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400952 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
953 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
954 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955
956 return depPaths
957}
958
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800959func (mod *Module) InstallInData() bool {
960 if mod.compiler == nil {
961 return false
962 }
963 return mod.compiler.inData()
964}
965
Ivan Lozanoffee3342019-08-27 12:03:00 -0700966func linkPathFromFilePath(filepath android.Path) string {
967 return strings.Split(filepath.String(), filepath.Base())[0]
968}
Ivan Lozanod648c432020-02-06 12:05:10 -0500969
Ivan Lozanoffee3342019-08-27 12:03:00 -0700970func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
971 ctx := &depsContext{
972 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700973 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974
975 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700976 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700977 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700978 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800979 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700980 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400981
Ivan Lozano2b081132020-09-08 12:46:52 -0400982 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400983 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400984 stdLinkage = "rlib-std"
985 }
986
987 rlibDepVariations := commonDepVariations
988 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
989 rlibDepVariations = append(rlibDepVariations,
990 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
991 }
992
Ivan Lozano52767be2019-10-18 14:49:46 -0700993 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400994 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200995 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700996 rlibDepTag, deps.Rlibs...)
997 actx.AddVariationDependencies(
998 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200999 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001000 dylibDepTag, deps.Dylibs...)
1001
Ivan Lozano042504f2020-08-18 14:31:23 -04001002 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1003 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001004 if autoDep.depTag == rlibDepTag {
1005 actx.AddVariationDependencies(
1006 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1007 autoDep.depTag, deps.Rustlibs...)
1008 } else {
1009 actx.AddVariationDependencies(
1010 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1011 autoDep.depTag, deps.Rustlibs...)
1012 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001013 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001014 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001015 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001016 actx.AddVariationDependencies(
1017 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1018 rlibDepTag, deps.Stdlibs...)
1019 } else {
1020 actx.AddVariationDependencies(
1021 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1022 dylibDepTag, deps.Stdlibs...)
1023 }
1024 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001025 actx.AddVariationDependencies(append(commonDepVariations,
1026 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001027 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001028 actx.AddVariationDependencies(append(commonDepVariations,
1029 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001030 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001031
Colin Cross565cafd2020-09-25 18:47:38 -07001032 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001033 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001034 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001035 }
1036 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001037 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001038 }
1039
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001040 if mod.sourceProvider != nil {
1041 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1042 bindgen.Properties.Custom_bindgen != "" {
1043 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1044 bindgen.Properties.Custom_bindgen)
1045 }
1046 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001047 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001048 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001049}
1050
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001051func BeginMutator(ctx android.BottomUpMutatorContext) {
1052 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1053 mod.beginMutator(ctx)
1054 }
1055}
1056
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001057func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1058 ctx := &baseModuleContext{
1059 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001060 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001061
1062 mod.begin(ctx)
1063}
1064
Ivan Lozanoffee3342019-08-27 12:03:00 -07001065func (mod *Module) Name() string {
1066 name := mod.ModuleBase.Name()
1067 if p, ok := mod.compiler.(interface {
1068 Name(string) string
1069 }); ok {
1070 name = p.Name(name)
1071 }
1072 return name
1073}
1074
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001075func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001076 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001077 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001078 }
1079}
1080
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001081var _ android.HostToolProvider = (*Module)(nil)
1082
1083func (mod *Module) HostToolPath() android.OptionalPath {
1084 if !mod.Host() {
1085 return android.OptionalPath{}
1086 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001087 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1088 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001089 }
1090 return android.OptionalPath{}
1091}
1092
Ivan Lozanoffee3342019-08-27 12:03:00 -07001093var Bool = proptools.Bool
1094var BoolDefault = proptools.BoolDefault
1095var String = proptools.String
1096var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001097
1098var _ android.OutputFileProducer = (*Module)(nil)