blob: f88b310da22aae774c7b3be53d9f1b53479760e6 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Ivan Lozano183a3212019-10-18 14:18:45 -070018 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020026 cc_config "android/soong/cc/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027 "android/soong/rust/config"
28)
29
30var pctx = android.NewPackageContext("android/soong/rust")
31
32func init() {
33 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070034
35 android.AddNeverAllowRules(
36 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070037 NotIn(config.RustAllowedPaths...).
38 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070039
40 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040051 GlobalRustFlags []string // Flags that apply globally to rust
52 GlobalLinkFlags []string // Flags that apply globally to linker
53 RustFlags []string // Flags that apply to rust
54 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070056 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040057 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61type BaseProperties struct {
62 AndroidMkRlibs []string
63 AndroidMkDylibs []string
64 AndroidMkProcMacroLibs []string
65 AndroidMkSharedLibs []string
66 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040067
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040068 SubName string `blueprint:"mutated"`
69
Ivan Lozano43845682020-07-09 21:03:28 -040070 PreventInstall bool
71 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070072}
73
74type Module struct {
75 android.ModuleBase
76 android.DefaultableModuleBase
77
78 Properties BaseProperties
79
80 hod android.HostOrDeviceSupported
81 multilib android.Multilib
82
83 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040084 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020085 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070086 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040087 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070088 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040089
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020090 outputFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
Ivan Lozano43845682020-07-09 21:03:28 -040093func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
94 switch tag {
95 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070096 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -040097 return mod.sourceProvider.Srcs(), nil
98 } else {
99 if mod.outputFile.Valid() {
100 return android.Paths{mod.outputFile.Path()}, nil
101 }
102 return android.Paths{}, nil
103 }
104 default:
105 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
106 }
107}
108
Colin Cross7228ecd2019-11-18 16:00:16 -0800109var _ android.ImageInterface = (*Module)(nil)
110
111func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
112
113func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
114 return true
115}
116
Yifan Hong1b3348d2020-01-21 15:53:22 -0800117func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
118 return mod.InRamdisk()
119}
120
Colin Cross7228ecd2019-11-18 16:00:16 -0800121func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
122 return mod.InRecovery()
123}
124
125func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
126 return nil
127}
128
129func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
130}
131
Ivan Lozano52767be2019-10-18 14:49:46 -0700132func (mod *Module) BuildStubs() bool {
133 return false
134}
135
136func (mod *Module) HasStubsVariants() bool {
137 return false
138}
139
140func (mod *Module) SelectedStl() string {
141 return ""
142}
143
Ivan Lozano2b262972019-11-21 12:30:50 -0800144func (mod *Module) NonCcVariants() bool {
145 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400146 if _, ok := mod.compiler.(libraryInterface); ok {
147 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800148 }
149 }
150 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
151}
152
Ivan Lozano52767be2019-10-18 14:49:46 -0700153func (mod *Module) ApiLevel() string {
154 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
155}
156
157func (mod *Module) Static() bool {
158 if mod.compiler != nil {
159 if library, ok := mod.compiler.(libraryInterface); ok {
160 return library.static()
161 }
162 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400163 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700164}
165
166func (mod *Module) Shared() bool {
167 if mod.compiler != nil {
168 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400169 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700170 }
171 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400172 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700173}
174
175func (mod *Module) Toc() android.OptionalPath {
176 if mod.compiler != nil {
177 if _, ok := mod.compiler.(libraryInterface); ok {
178 return android.OptionalPath{}
179 }
180 }
181 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
182}
183
Yifan Hong1b3348d2020-01-21 15:53:22 -0800184func (mod *Module) OnlyInRamdisk() bool {
185 return false
186}
187
Ivan Lozano52767be2019-10-18 14:49:46 -0700188func (mod *Module) OnlyInRecovery() bool {
189 return false
190}
191
Colin Crossc511bc52020-04-07 16:50:32 +0000192func (mod *Module) UseSdk() bool {
193 return false
194}
195
Ivan Lozano52767be2019-10-18 14:49:46 -0700196func (mod *Module) UseVndk() bool {
197 return false
198}
199
200func (mod *Module) MustUseVendorVariant() bool {
201 return false
202}
203
204func (mod *Module) IsVndk() bool {
205 return false
206}
207
208func (mod *Module) HasVendorVariant() bool {
209 return false
210}
211
212func (mod *Module) SdkVersion() string {
213 return ""
214}
215
Colin Crossc511bc52020-04-07 16:50:32 +0000216func (mod *Module) AlwaysSdk() bool {
217 return false
218}
219
Jiyong Park2286afd2020-06-16 21:58:53 +0900220func (mod *Module) IsSdkVariant() bool {
221 return false
222}
223
Ivan Lozano52767be2019-10-18 14:49:46 -0700224func (mod *Module) ToolchainLibrary() bool {
225 return false
226}
227
228func (mod *Module) NdkPrebuiltStl() bool {
229 return false
230}
231
232func (mod *Module) StubDecorator() bool {
233 return false
234}
235
Ivan Lozanoffee3342019-08-27 12:03:00 -0700236type Deps struct {
237 Dylibs []string
238 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700239 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400240 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241 ProcMacros []string
242 SharedLibs []string
243 StaticLibs []string
244
245 CrtBegin, CrtEnd string
246}
247
248type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400249 DyLibs RustLibraries
250 RLibs RustLibraries
251 SharedLibs android.Paths
252 StaticLibs android.Paths
253 ProcMacros RustLibraries
254 linkDirs []string
255 depFlags []string
256 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700257 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700258
Ivan Lozano45901ed2020-07-24 16:05:01 -0400259 // Used by bindgen modules which call clang
260 depClangFlags []string
261 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400262 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400263 depSystemIncludePaths android.Paths
264
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400265 coverageFiles android.Paths
266
Ivan Lozanof1c84332019-09-20 11:00:37 -0700267 CrtBegin android.OptionalPath
268 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700269
270 // Paths to generated source files
271 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700272}
273
274type RustLibraries []RustLibrary
275
276type RustLibrary struct {
277 Path android.Path
278 CrateName string
279}
280
281type compiler interface {
282 compilerFlags(ctx ModuleContext, flags Flags) Flags
283 compilerProps() []interface{}
284 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
285 compilerDeps(ctx DepsContext, deps Deps) Deps
286 crateName() string
287
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800288 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200289 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400291
292 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400293
294 Disabled() bool
295 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400296
Ivan Lozanodd055472020-09-28 13:22:45 -0400297 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400298}
299
Matthew Maurerbb3add12020-06-25 09:34:12 -0700300type exportedFlagsProducer interface {
301 exportedLinkDirs() []string
302 exportedDepFlags() []string
Ivan Lozano2093af22020-08-25 12:48:19 -0400303 exportedLinkObjects() []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700304 exportLinkDirs(...string)
305 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400306 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700307}
308
309type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400310 depFlags []string
311 linkDirs []string
312 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700313}
314
315func (flagExporter *flagExporter) exportedLinkDirs() []string {
316 return flagExporter.linkDirs
317}
318
319func (flagExporter *flagExporter) exportedDepFlags() []string {
320 return flagExporter.depFlags
321}
322
Ivan Lozano2093af22020-08-25 12:48:19 -0400323func (flagExporter *flagExporter) exportedLinkObjects() []string {
324 return flagExporter.linkObjects
325}
326
Matthew Maurerbb3add12020-06-25 09:34:12 -0700327func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
328 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
329}
330
331func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
332 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
333}
334
Ivan Lozano2093af22020-08-25 12:48:19 -0400335func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
336 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
337}
338
Matthew Maurerbb3add12020-06-25 09:34:12 -0700339var _ exportedFlagsProducer = (*flagExporter)(nil)
340
341func NewFlagExporter() *flagExporter {
342 return &flagExporter{
Ivan Lozano2093af22020-08-25 12:48:19 -0400343 depFlags: []string{},
344 linkDirs: []string{},
345 linkObjects: []string{},
Matthew Maurerbb3add12020-06-25 09:34:12 -0700346 }
347}
348
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400349func (mod *Module) isCoverageVariant() bool {
350 return mod.coverage.Properties.IsCoverageVariant
351}
352
353var _ cc.Coverage = (*Module)(nil)
354
355func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
356 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
357}
358
359func (mod *Module) PreventInstall() {
360 mod.Properties.PreventInstall = true
361}
362
363func (mod *Module) HideFromMake() {
364 mod.Properties.HideFromMake = true
365}
366
367func (mod *Module) MarkAsCoverageVariant(coverage bool) {
368 mod.coverage.Properties.IsCoverageVariant = coverage
369}
370
371func (mod *Module) EnableCoverageIfNeeded() {
372 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373}
374
375func defaultsFactory() android.Module {
376 return DefaultsFactory()
377}
378
379type Defaults struct {
380 android.ModuleBase
381 android.DefaultsModuleBase
382}
383
384func DefaultsFactory(props ...interface{}) android.Module {
385 module := &Defaults{}
386
387 module.AddProperties(props...)
388 module.AddProperties(
389 &BaseProperties{},
390 &BaseCompilerProperties{},
391 &BinaryCompilerProperties{},
392 &LibraryCompilerProperties{},
393 &ProcMacroCompilerProperties{},
394 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400395 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700396 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400397 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200398 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399 )
400
401 android.InitDefaultsModule(module)
402 return module
403}
404
405func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700406 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407}
408
Ivan Lozano183a3212019-10-18 14:18:45 -0700409func (mod *Module) CcLibrary() bool {
410 if mod.compiler != nil {
411 if _, ok := mod.compiler.(*libraryDecorator); ok {
412 return true
413 }
414 }
415 return false
416}
417
418func (mod *Module) CcLibraryInterface() bool {
419 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400420 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
421 // VariantIs{Static,Shared} is set.
422 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700423 return true
424 }
425 }
426 return false
427}
428
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800429func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700430 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700431 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800432 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700433 }
434 }
435 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
436}
437
438func (mod *Module) SetStatic() {
439 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700440 if library, ok := mod.compiler.(libraryInterface); ok {
441 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700442 return
443 }
444 }
445 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
446}
447
448func (mod *Module) SetShared() {
449 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700450 if library, ok := mod.compiler.(libraryInterface); ok {
451 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700452 return
453 }
454 }
455 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
456}
457
458func (mod *Module) SetBuildStubs() {
459 panic("SetBuildStubs not yet implemented for rust modules")
460}
461
Colin Crossd1f898e2020-08-18 18:35:15 -0700462func (mod *Module) SetStubsVersion(string) {
463 panic("SetStubsVersion not yet implemented for rust modules")
Ivan Lozano183a3212019-10-18 14:18:45 -0700464}
465
Jooyung Han03b51852020-02-26 22:45:42 +0900466func (mod *Module) StubsVersion() string {
Colin Crossd1f898e2020-08-18 18:35:15 -0700467 panic("StubsVersion not yet implemented for rust modules")
468}
469
470func (mod *Module) SetAllStubsVersions([]string) {
471 panic("SetAllStubsVersions not yet implemented for rust modules")
472}
473
474func (mod *Module) AllStubsVersions() []string {
475 return nil
Jooyung Han03b51852020-02-26 22:45:42 +0900476}
477
Ivan Lozano183a3212019-10-18 14:18:45 -0700478func (mod *Module) BuildStaticVariant() bool {
479 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700480 if library, ok := mod.compiler.(libraryInterface); ok {
481 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700482 }
483 }
484 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
485}
486
487func (mod *Module) BuildSharedVariant() bool {
488 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700489 if library, ok := mod.compiler.(libraryInterface); ok {
490 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700491 }
492 }
493 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
494}
495
496// Rust module deps don't have a link order (?)
497func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
498
499func (mod *Module) GetDepsInLinkOrder() []android.Path {
500 return []android.Path{}
501}
502
503func (mod *Module) GetStaticVariant() cc.LinkableInterface {
504 return nil
505}
506
507func (mod *Module) Module() android.Module {
508 return mod
509}
510
511func (mod *Module) StubsVersions() []string {
512 // For now, Rust has no stubs versions.
513 if mod.compiler != nil {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700514 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700515 return []string{}
516 }
517 }
518 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
519}
520
521func (mod *Module) OutputFile() android.OptionalPath {
522 return mod.outputFile
523}
524
525func (mod *Module) InRecovery() bool {
526 // For now, Rust has no notion of the recovery image
527 return false
528}
529func (mod *Module) HasStaticVariant() bool {
530 if mod.GetStaticVariant() != nil {
531 return true
532 }
533 return false
534}
535
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400536func (mod *Module) CoverageFiles() android.Paths {
537 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700538 if !mod.compiler.nativeCoverage() {
539 return android.Paths{}
540 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400541 if library, ok := mod.compiler.(*libraryDecorator); ok {
542 if library.coverageFile != nil {
543 return android.Paths{library.coverageFile}
544 }
545 return android.Paths{}
546 }
547 }
548 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
549}
550
Ivan Lozano183a3212019-10-18 14:18:45 -0700551var _ cc.LinkableInterface = (*Module)(nil)
552
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553func (mod *Module) Init() android.Module {
554 mod.AddProperties(&mod.Properties)
555
556 if mod.compiler != nil {
557 mod.AddProperties(mod.compiler.compilerProps()...)
558 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400559 if mod.coverage != nil {
560 mod.AddProperties(mod.coverage.props()...)
561 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200562 if mod.clippy != nil {
563 mod.AddProperties(mod.clippy.props()...)
564 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400565 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700566 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400567 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400568
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
570
571 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572 return mod
573}
574
575func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
576 return &Module{
577 hod: hod,
578 multilib: multilib,
579 }
580}
581func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
582 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200584 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700585 return module
586}
587
588type ModuleContext interface {
589 android.ModuleContext
590 ModuleContextIntf
591}
592
593type BaseModuleContext interface {
594 android.BaseModuleContext
595 ModuleContextIntf
596}
597
598type DepsContext interface {
599 android.BottomUpMutatorContext
600 ModuleContextIntf
601}
602
603type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200604 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606}
607
608type depsContext struct {
609 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610}
611
612type moduleContext struct {
613 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700614}
615
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200616type baseModuleContext struct {
617 android.BaseModuleContext
618}
619
620func (ctx *moduleContext) RustModule() *Module {
621 return ctx.Module().(*Module)
622}
623
624func (ctx *moduleContext) toolchain() config.Toolchain {
625 return ctx.RustModule().toolchain(ctx)
626}
627
628func (ctx *depsContext) RustModule() *Module {
629 return ctx.Module().(*Module)
630}
631
632func (ctx *depsContext) toolchain() config.Toolchain {
633 return ctx.RustModule().toolchain(ctx)
634}
635
636func (ctx *baseModuleContext) RustModule() *Module {
637 return ctx.Module().(*Module)
638}
639
640func (ctx *baseModuleContext) toolchain() config.Toolchain {
641 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400642}
643
644func (mod *Module) nativeCoverage() bool {
645 return mod.compiler != nil && mod.compiler.nativeCoverage()
646}
647
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
649 if mod.cachedToolchain == nil {
650 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
651 }
652 return mod.cachedToolchain
653}
654
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200655func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
656 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
657}
658
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
660}
661
662func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
663 ctx := &moduleContext{
664 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700666
667 toolchain := mod.toolchain(ctx)
668
669 if !toolchain.Supported() {
670 // This toolchain's unsupported, there's nothing to do for this mod.
671 return
672 }
673
674 deps := mod.depsToPaths(ctx)
675 flags := Flags{
676 Toolchain: toolchain,
677 }
678
679 if mod.compiler != nil {
680 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400681 }
682 if mod.coverage != nil {
683 flags, deps = mod.coverage.flags(ctx, flags, deps)
684 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200685 if mod.clippy != nil {
686 flags, deps = mod.clippy.flags(ctx, flags, deps)
687 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400688
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200689 // SourceProvider needs to call GenerateSource() before compiler calls
690 // compile() so it can provide the source. A SourceProvider has
691 // multiple variants (e.g. source, rlib, dylib). Only the "source"
692 // variant is responsible for effectively generating the source. The
693 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400694 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200695 if mod.compiler.(libraryInterface).source() {
696 mod.sourceProvider.GenerateSource(ctx, deps)
697 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
698 } else {
699 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
700 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
701 mod.sourceProvider.setOutputFile(sourceLib.sourceProvider.Srcs()[0])
702 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400703 }
704
705 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400707
Ivan Lozanoffee3342019-08-27 12:03:00 -0700708 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400709 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200710 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400711 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700712 }
713}
714
715func (mod *Module) deps(ctx DepsContext) Deps {
716 deps := Deps{}
717
718 if mod.compiler != nil {
719 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400720 }
721 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700722 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 }
724
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400725 if mod.coverage != nil {
726 deps = mod.coverage.deps(ctx, deps)
727 }
728
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
730 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700731 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700732 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
733 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
734 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
735
736 return deps
737
738}
739
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740type dependencyTag struct {
741 blueprint.BaseDependencyTag
742 name string
743 library bool
744 proc_macro bool
745}
746
747var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400748 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
749 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
750 dylibDepTag = dependencyTag{name: "dylib", library: true}
751 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
752 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200753 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754)
755
Matthew Maurer0f003b12020-06-29 14:34:06 -0700756type autoDep struct {
757 variation string
758 depTag dependencyTag
759}
760
761var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200762 rlibVariation = "rlib"
763 dylibVariation = "dylib"
764 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
765 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700766)
767
768type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400769 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700770}
771
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400772func (mod *Module) begin(ctx BaseModuleContext) {
773 if mod.coverage != nil {
774 mod.coverage.begin(ctx)
775 }
776}
777
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
779 var depPaths PathDeps
780
781 directRlibDeps := []*Module{}
782 directDylibDeps := []*Module{}
783 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700784 directSharedLibDeps := [](cc.LinkableInterface){}
785 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400786 directSrcProvidersDeps := []*Module{}
787 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788
789 ctx.VisitDirectDeps(func(dep android.Module) {
790 depName := ctx.OtherModuleName(dep)
791 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400792 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700793 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700794
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 switch depTag {
796 case dylibDepTag:
797 dylib, ok := rustDep.compiler.(libraryInterface)
798 if !ok || !dylib.dylib() {
799 ctx.ModuleErrorf("mod %q not an dylib library", depName)
800 return
801 }
802 directDylibDeps = append(directDylibDeps, rustDep)
803 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
804 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400805
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806 rlib, ok := rustDep.compiler.(libraryInterface)
807 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400808 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809 return
810 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400811 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400813 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814 case procMacroDepTag:
815 directProcMacroDeps = append(directProcMacroDeps, rustDep)
816 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400817 case android.SourceDepTag:
818 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
819 // OS/Arch variant is used.
820 var helper string
821 if ctx.Host() {
822 helper = "missing 'host_supported'?"
823 } else {
824 helper = "device module defined?"
825 }
826
827 if dep.Target().Os != ctx.Os() {
828 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
829 return
830 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
831 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
832 return
833 }
834 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700835 }
836
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400837 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
838 if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700839 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400841 depPaths.linkObjects = append(depPaths.linkObjects, lib.exportedLinkObjects()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842 }
843
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400845 linkFile := rustDep.outputFile
846 if !linkFile.Valid() {
847 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
848 depName, ctx.ModuleName())
849 return
850 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700852 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
853 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 }
855 }
856
Ivan Lozano89435d12020-07-31 11:01:18 -0400857 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700858 //Handle C dependencies
859 if _, ok := ccDep.(*Module); !ok {
860 if ccDep.Module().Target().Os != ctx.Os() {
861 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
862 return
863 }
864 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
865 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
866 return
867 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700868 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400869 linkObject := ccDep.OutputFile()
870 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500871
Ivan Lozano2093af22020-08-25 12:48:19 -0400872 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
874 }
875
876 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700877 switch {
878 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400880 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400881 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
882 if mod, ok := ccDep.(*cc.Module); ok {
883 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
884 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400885 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400886 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400887 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700888 directStaticLibDeps = append(directStaticLibDeps, ccDep)
889 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700890 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400892 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400893 depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
894 if mod, ok := ccDep.(*cc.Module); ok {
895 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
896 depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400897 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, mod.ExportedGeneratedHeaders()...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400898 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899 directSharedLibDeps = append(directSharedLibDeps, ccDep)
900 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
901 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700902 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400903 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700904 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400905 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 }
907
908 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700909 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
910 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400911 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400914
915 if srcDep, ok := dep.(android.SourceFileProducer); ok {
916 switch depTag {
917 case android.SourceDepTag:
918 // These are usually genrules which don't have per-target variants.
919 directSrcDeps = append(directSrcDeps, srcDep)
920 }
921 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700922 })
923
924 var rlibDepFiles RustLibraries
925 for _, dep := range directRlibDeps {
926 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
927 }
928 var dylibDepFiles RustLibraries
929 for _, dep := range directDylibDeps {
930 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
931 }
932 var procMacroDepFiles RustLibraries
933 for _, dep := range directProcMacroDeps {
934 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
935 }
936
937 var staticLibDepFiles android.Paths
938 for _, dep := range directStaticLibDeps {
939 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
940 }
941
942 var sharedLibDepFiles android.Paths
943 for _, dep := range directSharedLibDeps {
944 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
945 }
946
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400947 var srcProviderDepFiles android.Paths
948 for _, dep := range directSrcProvidersDeps {
949 srcs, _ := dep.OutputFiles("")
950 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
951 }
952 for _, dep := range directSrcDeps {
953 srcs := dep.Srcs()
954 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
955 }
956
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
958 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
959 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
960 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
961 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400962 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963
964 // Dedup exported flags from dependencies
965 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
966 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400967 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
968 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
969 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700970
971 return depPaths
972}
973
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800974func (mod *Module) InstallInData() bool {
975 if mod.compiler == nil {
976 return false
977 }
978 return mod.compiler.inData()
979}
980
Ivan Lozanoffee3342019-08-27 12:03:00 -0700981func linkPathFromFilePath(filepath android.Path) string {
982 return strings.Split(filepath.String(), filepath.Base())[0]
983}
Ivan Lozanod648c432020-02-06 12:05:10 -0500984
Ivan Lozanoffee3342019-08-27 12:03:00 -0700985func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
986 ctx := &depsContext{
987 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700988 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700989
990 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700991 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900992 if cc.VersionVariantAvailable(mod) {
993 commonDepVariations = append(commonDepVariations,
994 blueprint.Variation{Mutator: "version", Variation: ""})
995 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700996 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700997 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800998 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700999 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001000
Ivan Lozano2b081132020-09-08 12:46:52 -04001001 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001002 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001003 stdLinkage = "rlib-std"
1004 }
1005
1006 rlibDepVariations := commonDepVariations
1007 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1008 rlibDepVariations = append(rlibDepVariations,
1009 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1010 }
1011
Ivan Lozano52767be2019-10-18 14:49:46 -07001012 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001013 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001014 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001015 rlibDepTag, deps.Rlibs...)
1016 actx.AddVariationDependencies(
1017 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001018 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001019 dylibDepTag, deps.Dylibs...)
1020
Ivan Lozano042504f2020-08-18 14:31:23 -04001021 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1022 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001023 if autoDep.depTag == rlibDepTag {
1024 actx.AddVariationDependencies(
1025 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1026 autoDep.depTag, deps.Rustlibs...)
1027 } else {
1028 actx.AddVariationDependencies(
1029 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1030 autoDep.depTag, deps.Rustlibs...)
1031 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001032 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001033 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001034 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001035 actx.AddVariationDependencies(
1036 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1037 rlibDepTag, deps.Stdlibs...)
1038 } else {
1039 actx.AddVariationDependencies(
1040 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1041 dylibDepTag, deps.Stdlibs...)
1042 }
1043 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001044 actx.AddVariationDependencies(append(commonDepVariations,
1045 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001046 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001047 actx.AddVariationDependencies(append(commonDepVariations,
1048 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001049 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001050
Dan Albert92fe7402020-07-15 13:33:30 -07001051 crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001052 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001053 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001054 }
1055 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001056 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001057 }
1058
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001059 if mod.sourceProvider != nil {
1060 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1061 bindgen.Properties.Custom_bindgen != "" {
1062 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1063 bindgen.Properties.Custom_bindgen)
1064 }
1065 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001066 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001067 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001068}
1069
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001070func BeginMutator(ctx android.BottomUpMutatorContext) {
1071 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1072 mod.beginMutator(ctx)
1073 }
1074}
1075
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001076func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1077 ctx := &baseModuleContext{
1078 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001079 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001080
1081 mod.begin(ctx)
1082}
1083
Ivan Lozanoffee3342019-08-27 12:03:00 -07001084func (mod *Module) Name() string {
1085 name := mod.ModuleBase.Name()
1086 if p, ok := mod.compiler.(interface {
1087 Name(string) string
1088 }); ok {
1089 name = p.Name(name)
1090 }
1091 return name
1092}
1093
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001094func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001095 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001096 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001097 }
1098}
1099
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001100var _ android.HostToolProvider = (*Module)(nil)
1101
1102func (mod *Module) HostToolPath() android.OptionalPath {
1103 if !mod.Host() {
1104 return android.OptionalPath{}
1105 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001106 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1107 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001108 }
1109 return android.OptionalPath{}
1110}
1111
Ivan Lozanoffee3342019-08-27 12:03:00 -07001112var Bool = proptools.Bool
1113var BoolDefault = proptools.BoolDefault
1114var String = proptools.String
1115var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001116
1117var _ android.OutputFileProducer = (*Module)(nil)