blob: d22acea4ada34e97d397058e84c26ffebf932e85 [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
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040090 outputFile android.OptionalPath
91 generatedFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -070092}
93
Ivan Lozano43845682020-07-09 21:03:28 -040094func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
95 switch tag {
96 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070097 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -040098 return mod.sourceProvider.Srcs(), nil
99 } else {
100 if mod.outputFile.Valid() {
101 return android.Paths{mod.outputFile.Path()}, nil
102 }
103 return android.Paths{}, nil
104 }
105 default:
106 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
107 }
108}
109
Colin Cross7228ecd2019-11-18 16:00:16 -0800110var _ android.ImageInterface = (*Module)(nil)
111
112func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
113
114func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
115 return true
116}
117
Yifan Hong1b3348d2020-01-21 15:53:22 -0800118func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
119 return mod.InRamdisk()
120}
121
Colin Cross7228ecd2019-11-18 16:00:16 -0800122func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
123 return mod.InRecovery()
124}
125
126func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
127 return nil
128}
129
130func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
131}
132
Ivan Lozano52767be2019-10-18 14:49:46 -0700133func (mod *Module) BuildStubs() bool {
134 return false
135}
136
137func (mod *Module) HasStubsVariants() bool {
138 return false
139}
140
141func (mod *Module) SelectedStl() string {
142 return ""
143}
144
Ivan Lozano2b262972019-11-21 12:30:50 -0800145func (mod *Module) NonCcVariants() bool {
146 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400147 if _, ok := mod.compiler.(libraryInterface); ok {
148 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800149 }
150 }
151 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
152}
153
Ivan Lozano52767be2019-10-18 14:49:46 -0700154func (mod *Module) ApiLevel() string {
155 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
156}
157
158func (mod *Module) Static() bool {
159 if mod.compiler != nil {
160 if library, ok := mod.compiler.(libraryInterface); ok {
161 return library.static()
162 }
163 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400164 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700165}
166
167func (mod *Module) Shared() bool {
168 if mod.compiler != nil {
169 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400170 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700171 }
172 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400173 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700174}
175
176func (mod *Module) Toc() android.OptionalPath {
177 if mod.compiler != nil {
178 if _, ok := mod.compiler.(libraryInterface); ok {
179 return android.OptionalPath{}
180 }
181 }
182 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
183}
184
Yifan Hong1b3348d2020-01-21 15:53:22 -0800185func (mod *Module) OnlyInRamdisk() bool {
186 return false
187}
188
Ivan Lozano52767be2019-10-18 14:49:46 -0700189func (mod *Module) OnlyInRecovery() bool {
190 return false
191}
192
Colin Crossc511bc52020-04-07 16:50:32 +0000193func (mod *Module) UseSdk() bool {
194 return false
195}
196
Ivan Lozano52767be2019-10-18 14:49:46 -0700197func (mod *Module) UseVndk() bool {
198 return false
199}
200
201func (mod *Module) MustUseVendorVariant() bool {
202 return false
203}
204
205func (mod *Module) IsVndk() bool {
206 return false
207}
208
209func (mod *Module) HasVendorVariant() bool {
210 return false
211}
212
213func (mod *Module) SdkVersion() string {
214 return ""
215}
216
Colin Crossc511bc52020-04-07 16:50:32 +0000217func (mod *Module) AlwaysSdk() bool {
218 return false
219}
220
Jiyong Park2286afd2020-06-16 21:58:53 +0900221func (mod *Module) IsSdkVariant() bool {
222 return false
223}
224
Ivan Lozano52767be2019-10-18 14:49:46 -0700225func (mod *Module) ToolchainLibrary() bool {
226 return false
227}
228
229func (mod *Module) NdkPrebuiltStl() bool {
230 return false
231}
232
233func (mod *Module) StubDecorator() bool {
234 return false
235}
236
Ivan Lozanoffee3342019-08-27 12:03:00 -0700237type Deps struct {
238 Dylibs []string
239 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700240 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400241 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700242 ProcMacros []string
243 SharedLibs []string
244 StaticLibs []string
245
246 CrtBegin, CrtEnd string
247}
248
249type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400250 DyLibs RustLibraries
251 RLibs RustLibraries
252 SharedLibs android.Paths
253 StaticLibs android.Paths
254 ProcMacros RustLibraries
255 linkDirs []string
256 depFlags []string
257 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700259
Ivan Lozano45901ed2020-07-24 16:05:01 -0400260 // Used by bindgen modules which call clang
261 depClangFlags []string
262 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400263 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400264 depSystemIncludePaths android.Paths
265
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400266 coverageFiles android.Paths
267
Ivan Lozanof1c84332019-09-20 11:00:37 -0700268 CrtBegin android.OptionalPath
269 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700270
271 // Paths to generated source files
272 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700273}
274
275type RustLibraries []RustLibrary
276
277type RustLibrary struct {
278 Path android.Path
279 CrateName string
280}
281
282type compiler interface {
283 compilerFlags(ctx ModuleContext, flags Flags) Flags
284 compilerProps() []interface{}
285 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
286 compilerDeps(ctx DepsContext, deps Deps) Deps
287 crateName() string
288
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800289 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200290 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700291 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400292
293 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400294
295 Disabled() bool
296 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400297
Ivan Lozano2b081132020-09-08 12:46:52 -0400298 staticStd(ctx *depsContext) bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400299}
300
Matthew Maurerbb3add12020-06-25 09:34:12 -0700301type exportedFlagsProducer interface {
302 exportedLinkDirs() []string
303 exportedDepFlags() []string
Ivan Lozano2093af22020-08-25 12:48:19 -0400304 exportedLinkObjects() []string
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
316func (flagExporter *flagExporter) exportedLinkDirs() []string {
317 return flagExporter.linkDirs
318}
319
320func (flagExporter *flagExporter) exportedDepFlags() []string {
321 return flagExporter.depFlags
322}
323
Ivan Lozano2093af22020-08-25 12:48:19 -0400324func (flagExporter *flagExporter) exportedLinkObjects() []string {
325 return flagExporter.linkObjects
326}
327
Matthew Maurerbb3add12020-06-25 09:34:12 -0700328func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
329 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
330}
331
332func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
333 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
334}
335
Ivan Lozano2093af22020-08-25 12:48:19 -0400336func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
337 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
338}
339
Matthew Maurerbb3add12020-06-25 09:34:12 -0700340var _ exportedFlagsProducer = (*flagExporter)(nil)
341
342func NewFlagExporter() *flagExporter {
343 return &flagExporter{
Ivan Lozano2093af22020-08-25 12:48:19 -0400344 depFlags: []string{},
345 linkDirs: []string{},
346 linkObjects: []string{},
Matthew Maurerbb3add12020-06-25 09:34:12 -0700347 }
348}
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{},
391 &BaseCompilerProperties{},
392 &BinaryCompilerProperties{},
393 &LibraryCompilerProperties{},
394 &ProcMacroCompilerProperties{},
395 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400396 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700397 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400398 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200399 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400 )
401
402 android.InitDefaultsModule(module)
403 return module
404}
405
406func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700407 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700408}
409
Ivan Lozano183a3212019-10-18 14:18:45 -0700410func (mod *Module) CcLibrary() bool {
411 if mod.compiler != nil {
412 if _, ok := mod.compiler.(*libraryDecorator); ok {
413 return true
414 }
415 }
416 return false
417}
418
419func (mod *Module) CcLibraryInterface() bool {
420 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400421 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
422 // VariantIs{Static,Shared} is set.
423 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700424 return true
425 }
426 }
427 return false
428}
429
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800430func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700431 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700432 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800433 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700434 }
435 }
436 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
437}
438
439func (mod *Module) SetStatic() {
440 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700441 if library, ok := mod.compiler.(libraryInterface); ok {
442 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700443 return
444 }
445 }
446 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
447}
448
449func (mod *Module) SetShared() {
450 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700451 if library, ok := mod.compiler.(libraryInterface); ok {
452 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700453 return
454 }
455 }
456 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
457}
458
459func (mod *Module) SetBuildStubs() {
460 panic("SetBuildStubs not yet implemented for rust modules")
461}
462
Colin Crossd1f898e2020-08-18 18:35:15 -0700463func (mod *Module) SetStubsVersion(string) {
464 panic("SetStubsVersion not yet implemented for rust modules")
Ivan Lozano183a3212019-10-18 14:18:45 -0700465}
466
Jooyung Han03b51852020-02-26 22:45:42 +0900467func (mod *Module) StubsVersion() string {
Colin Crossd1f898e2020-08-18 18:35:15 -0700468 panic("StubsVersion not yet implemented for rust modules")
469}
470
471func (mod *Module) SetAllStubsVersions([]string) {
472 panic("SetAllStubsVersions not yet implemented for rust modules")
473}
474
475func (mod *Module) AllStubsVersions() []string {
476 return nil
Jooyung Han03b51852020-02-26 22:45:42 +0900477}
478
Ivan Lozano183a3212019-10-18 14:18:45 -0700479func (mod *Module) BuildStaticVariant() bool {
480 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700481 if library, ok := mod.compiler.(libraryInterface); ok {
482 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700483 }
484 }
485 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
486}
487
488func (mod *Module) BuildSharedVariant() bool {
489 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700490 if library, ok := mod.compiler.(libraryInterface); ok {
491 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700492 }
493 }
494 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
495}
496
497// Rust module deps don't have a link order (?)
498func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
499
500func (mod *Module) GetDepsInLinkOrder() []android.Path {
501 return []android.Path{}
502}
503
504func (mod *Module) GetStaticVariant() cc.LinkableInterface {
505 return nil
506}
507
508func (mod *Module) Module() android.Module {
509 return mod
510}
511
512func (mod *Module) StubsVersions() []string {
513 // For now, Rust has no stubs versions.
514 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700515 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700516 return []string{}
517 }
518 }
519 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
520}
521
522func (mod *Module) OutputFile() android.OptionalPath {
523 return mod.outputFile
524}
525
526func (mod *Module) InRecovery() bool {
527 // For now, Rust has no notion of the recovery image
528 return false
529}
530func (mod *Module) HasStaticVariant() bool {
531 if mod.GetStaticVariant() != nil {
532 return true
533 }
534 return false
535}
536
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400537func (mod *Module) CoverageFiles() android.Paths {
538 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700539 if !mod.compiler.nativeCoverage() {
540 return android.Paths{}
541 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400542 if library, ok := mod.compiler.(*libraryDecorator); ok {
543 if library.coverageFile != nil {
544 return android.Paths{library.coverageFile}
545 }
546 return android.Paths{}
547 }
548 }
549 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
550}
551
Ivan Lozano183a3212019-10-18 14:18:45 -0700552var _ cc.LinkableInterface = (*Module)(nil)
553
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554func (mod *Module) Init() android.Module {
555 mod.AddProperties(&mod.Properties)
556
557 if mod.compiler != nil {
558 mod.AddProperties(mod.compiler.compilerProps()...)
559 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400560 if mod.coverage != nil {
561 mod.AddProperties(mod.coverage.props()...)
562 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200563 if mod.clippy != nil {
564 mod.AddProperties(mod.clippy.props()...)
565 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400566 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700567 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400568 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400569
Ivan Lozanoffee3342019-08-27 12:03:00 -0700570 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
571
572 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700573 return mod
574}
575
576func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
577 return &Module{
578 hod: hod,
579 multilib: multilib,
580 }
581}
582func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
583 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400584 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200585 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700586 return module
587}
588
589type ModuleContext interface {
590 android.ModuleContext
591 ModuleContextIntf
592}
593
594type BaseModuleContext interface {
595 android.BaseModuleContext
596 ModuleContextIntf
597}
598
599type DepsContext interface {
600 android.BottomUpMutatorContext
601 ModuleContextIntf
602}
603
604type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200605 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607}
608
609type depsContext struct {
610 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611}
612
613type moduleContext struct {
614 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615}
616
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200617type baseModuleContext struct {
618 android.BaseModuleContext
619}
620
621func (ctx *moduleContext) RustModule() *Module {
622 return ctx.Module().(*Module)
623}
624
625func (ctx *moduleContext) toolchain() config.Toolchain {
626 return ctx.RustModule().toolchain(ctx)
627}
628
629func (ctx *depsContext) RustModule() *Module {
630 return ctx.Module().(*Module)
631}
632
633func (ctx *depsContext) toolchain() config.Toolchain {
634 return ctx.RustModule().toolchain(ctx)
635}
636
637func (ctx *baseModuleContext) RustModule() *Module {
638 return ctx.Module().(*Module)
639}
640
641func (ctx *baseModuleContext) toolchain() config.Toolchain {
642 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400643}
644
645func (mod *Module) nativeCoverage() bool {
646 return mod.compiler != nil && mod.compiler.nativeCoverage()
647}
648
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
650 if mod.cachedToolchain == nil {
651 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
652 }
653 return mod.cachedToolchain
654}
655
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200656func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
657 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
658}
659
Ivan Lozanoffee3342019-08-27 12:03:00 -0700660func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
661}
662
663func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
664 ctx := &moduleContext{
665 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700666 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667
668 toolchain := mod.toolchain(ctx)
669
670 if !toolchain.Supported() {
671 // This toolchain's unsupported, there's nothing to do for this mod.
672 return
673 }
674
675 deps := mod.depsToPaths(ctx)
676 flags := Flags{
677 Toolchain: toolchain,
678 }
679
680 if mod.compiler != nil {
681 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400682 }
683 if mod.coverage != nil {
684 flags, deps = mod.coverage.flags(ctx, flags, deps)
685 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200686 if mod.clippy != nil {
687 flags, deps = mod.clippy.flags(ctx, flags, deps)
688 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400689
Andrei Homescuc7767922020-08-05 06:36:19 -0700690 // SourceProvider needs to call GenerateSource() before compiler calls compile() so it can provide the source.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400691 // TODO(b/162588681) This shouldn't have to run for every variant.
692 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700693 generatedFile := mod.sourceProvider.GenerateSource(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400694 mod.generatedFile = android.OptionalPathForPath(generatedFile)
695 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
696 }
697
698 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700699 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400700
Ivan Lozanoffee3342019-08-27 12:03:00 -0700701 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400702 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200703 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400704 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700705 }
706}
707
708func (mod *Module) deps(ctx DepsContext) Deps {
709 deps := Deps{}
710
711 if mod.compiler != nil {
712 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400713 }
714 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700715 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700716 }
717
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400718 if mod.coverage != nil {
719 deps = mod.coverage.deps(ctx, deps)
720 }
721
Ivan Lozanoffee3342019-08-27 12:03:00 -0700722 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
723 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700724 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
726 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
727 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
728
729 return deps
730
731}
732
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733type dependencyTag struct {
734 blueprint.BaseDependencyTag
735 name string
736 library bool
737 proc_macro bool
738}
739
740var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400741 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
742 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
743 dylibDepTag = dependencyTag{name: "dylib", library: true}
744 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
745 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746)
747
Matthew Maurer0f003b12020-06-29 14:34:06 -0700748type autoDep struct {
749 variation string
750 depTag dependencyTag
751}
752
753var (
754 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
755 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
756)
757
758type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400759 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700760}
761
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400762func (mod *Module) begin(ctx BaseModuleContext) {
763 if mod.coverage != nil {
764 mod.coverage.begin(ctx)
765 }
766}
767
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
769 var depPaths PathDeps
770
771 directRlibDeps := []*Module{}
772 directDylibDeps := []*Module{}
773 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700774 directSharedLibDeps := [](cc.LinkableInterface){}
775 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400776 directSrcProvidersDeps := []*Module{}
777 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778
779 ctx.VisitDirectDeps(func(dep android.Module) {
780 depName := ctx.OtherModuleName(dep)
781 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400782 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700784
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785 switch depTag {
786 case dylibDepTag:
787 dylib, ok := rustDep.compiler.(libraryInterface)
788 if !ok || !dylib.dylib() {
789 ctx.ModuleErrorf("mod %q not an dylib library", depName)
790 return
791 }
792 directDylibDeps = append(directDylibDeps, rustDep)
793 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
794 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400795
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796 rlib, ok := rustDep.compiler.(libraryInterface)
797 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400798 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 return
800 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400801 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400803 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 case procMacroDepTag:
805 directProcMacroDeps = append(directProcMacroDeps, rustDep)
806 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400807 case android.SourceDepTag:
808 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
809 // OS/Arch variant is used.
810 var helper string
811 if ctx.Host() {
812 helper = "missing 'host_supported'?"
813 } else {
814 helper = "device module defined?"
815 }
816
817 if dep.Target().Os != ctx.Os() {
818 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
819 return
820 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
821 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
822 return
823 }
824 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825 }
826
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400827 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
828 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700829 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400831 depPaths.linkObjects = append(depPaths.linkObjects, lib.exportedLinkObjects()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832 }
833
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400835 linkFile := rustDep.outputFile
836 if !linkFile.Valid() {
837 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
838 depName, ctx.ModuleName())
839 return
840 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700842 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
843 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 }
845 }
846
Ivan Lozano89435d12020-07-31 11:01:18 -0400847 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700848 //Handle C dependencies
849 if _, ok := ccDep.(*Module); !ok {
850 if ccDep.Module().Target().Os != ctx.Os() {
851 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
852 return
853 }
854 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
855 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
856 return
857 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700858 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400859 linkObject := ccDep.OutputFile()
860 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500861
Ivan Lozano2093af22020-08-25 12:48:19 -0400862 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
864 }
865
866 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700867 switch {
868 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400870 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400871 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
872 if mod, ok := ccDep.(*cc.Module); ok {
873 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
874 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400875 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400876 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400877 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 directStaticLibDeps = append(directStaticLibDeps, ccDep)
879 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700880 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400882 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400883 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
884 if mod, ok := ccDep.(*cc.Module); ok {
885 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
886 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400887 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400888 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 directSharedLibDeps = append(directSharedLibDeps, ccDep)
890 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
891 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700892 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400893 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700894 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400895 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700896 }
897
898 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700899 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
900 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400901 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700902 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700903 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400904
905 if srcDep, ok := dep.(android.SourceFileProducer); ok {
906 switch depTag {
907 case android.SourceDepTag:
908 // These are usually genrules which don't have per-target variants.
909 directSrcDeps = append(directSrcDeps, srcDep)
910 }
911 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 })
913
914 var rlibDepFiles RustLibraries
915 for _, dep := range directRlibDeps {
916 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
917 }
918 var dylibDepFiles RustLibraries
919 for _, dep := range directDylibDeps {
920 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
921 }
922 var procMacroDepFiles RustLibraries
923 for _, dep := range directProcMacroDeps {
924 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
925 }
926
927 var staticLibDepFiles android.Paths
928 for _, dep := range directStaticLibDeps {
929 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
930 }
931
932 var sharedLibDepFiles android.Paths
933 for _, dep := range directSharedLibDeps {
934 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
935 }
936
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400937 var srcProviderDepFiles android.Paths
938 for _, dep := range directSrcProvidersDeps {
939 srcs, _ := dep.OutputFiles("")
940 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
941 }
942 for _, dep := range directSrcDeps {
943 srcs := dep.Srcs()
944 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
945 }
946
Ivan Lozanoffee3342019-08-27 12:03:00 -0700947 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
948 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
949 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
950 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
951 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400952 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953
954 // Dedup exported flags from dependencies
955 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
956 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400957 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
958 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
959 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700960
961 return depPaths
962}
963
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800964func (mod *Module) InstallInData() bool {
965 if mod.compiler == nil {
966 return false
967 }
968 return mod.compiler.inData()
969}
970
Ivan Lozanoffee3342019-08-27 12:03:00 -0700971func linkPathFromFilePath(filepath android.Path) string {
972 return strings.Split(filepath.String(), filepath.Base())[0]
973}
Ivan Lozanod648c432020-02-06 12:05:10 -0500974
Ivan Lozanoffee3342019-08-27 12:03:00 -0700975func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
976 ctx := &depsContext{
977 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700978 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979
980 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700981 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900982 if cc.VersionVariantAvailable(mod) {
983 commonDepVariations = append(commonDepVariations,
984 blueprint.Variation{Mutator: "version", Variation: ""})
985 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700987 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800988 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700989 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400990 stdLinkage := "dylib-std"
991 if mod.compiler.staticStd(ctx) {
992 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{
Ivan Lozano89435d12020-07-31 11:01:18 -04001003 {Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001004 rlibDepTag, deps.Rlibs...)
1005 actx.AddVariationDependencies(
1006 append(commonDepVariations, []blueprint.Variation{
Ivan Lozano89435d12020-07-31 11:01:18 -04001007 {Mutator: "rust_libraries", Variation: "dylib"}}...),
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 {
1023 if mod.compiler.staticStd(ctx) {
1024 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
Dan Albert92fe7402020-07-15 13:33:30 -07001040 crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
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)