blob: 4094094c3a82245eec63186d81b8a5fcd65facf0 [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
Jiyong Park99644e92020-11-17 22:21:02 +090077 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070078
79 Properties BaseProperties
80
81 hod android.HostOrDeviceSupported
82 multilib android.Multilib
83
84 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040085 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020086 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070087 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040088 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070089 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040090
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020091 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +090092
93 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070094}
95
Ivan Lozano43845682020-07-09 21:03:28 -040096func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
97 switch tag {
98 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070099 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400100 return mod.sourceProvider.Srcs(), nil
101 } else {
102 if mod.outputFile.Valid() {
103 return android.Paths{mod.outputFile.Path()}, nil
104 }
105 return android.Paths{}, nil
106 }
107 default:
108 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
109 }
110}
111
Colin Cross7228ecd2019-11-18 16:00:16 -0800112var _ android.ImageInterface = (*Module)(nil)
113
114func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
115
116func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
117 return true
118}
119
Yifan Hong1b3348d2020-01-21 15:53:22 -0800120func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
121 return mod.InRamdisk()
122}
123
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700124func (mod *Module) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool {
125 return mod.InVendorRamdisk()
126}
127
Colin Cross7228ecd2019-11-18 16:00:16 -0800128func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
129 return mod.InRecovery()
130}
131
132func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
133 return nil
134}
135
136func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
137}
138
Ivan Lozano52767be2019-10-18 14:49:46 -0700139func (mod *Module) SelectedStl() string {
140 return ""
141}
142
Ivan Lozano2b262972019-11-21 12:30:50 -0800143func (mod *Module) NonCcVariants() bool {
144 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400145 if _, ok := mod.compiler.(libraryInterface); ok {
146 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800147 }
148 }
149 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
150}
151
Ivan Lozano52767be2019-10-18 14:49:46 -0700152func (mod *Module) Static() bool {
153 if mod.compiler != nil {
154 if library, ok := mod.compiler.(libraryInterface); ok {
155 return library.static()
156 }
157 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400158 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700159}
160
161func (mod *Module) Shared() bool {
162 if mod.compiler != nil {
163 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400164 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700165 }
166 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400167 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700168}
169
170func (mod *Module) Toc() android.OptionalPath {
171 if mod.compiler != nil {
172 if _, ok := mod.compiler.(libraryInterface); ok {
173 return android.OptionalPath{}
174 }
175 }
176 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
177}
178
Yifan Hong1b3348d2020-01-21 15:53:22 -0800179func (mod *Module) OnlyInRamdisk() bool {
180 return false
181}
182
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700183func (mod *Module) OnlyInVendorRamdisk() bool {
184 return false
185}
186
Ivan Lozano52767be2019-10-18 14:49:46 -0700187func (mod *Module) OnlyInRecovery() bool {
188 return false
189}
190
Colin Crossc511bc52020-04-07 16:50:32 +0000191func (mod *Module) UseSdk() bool {
192 return false
193}
194
Ivan Lozano52767be2019-10-18 14:49:46 -0700195func (mod *Module) UseVndk() bool {
196 return false
197}
198
199func (mod *Module) MustUseVendorVariant() bool {
200 return false
201}
202
203func (mod *Module) IsVndk() bool {
204 return false
205}
206
207func (mod *Module) HasVendorVariant() bool {
208 return false
209}
210
Ivan Lozanof9e21722020-12-02 09:00:51 -0500211func (mod *Module) IsVndkExt() bool {
212 return false
213}
214
215func (c *Module) IsVndkPrivate(config android.Config) bool {
216 return false
217}
218
219func (mod *Module) InProduct() bool {
220 return false
221}
222
Ivan Lozano52767be2019-10-18 14:49:46 -0700223func (mod *Module) SdkVersion() string {
224 return ""
225}
226
Colin Crossc511bc52020-04-07 16:50:32 +0000227func (mod *Module) AlwaysSdk() bool {
228 return false
229}
230
Jiyong Park2286afd2020-06-16 21:58:53 +0900231func (mod *Module) IsSdkVariant() bool {
232 return false
233}
234
Colin Cross1348ce32020-10-01 13:37:16 -0700235func (mod *Module) SplitPerApiLevel() bool {
236 return false
237}
238
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239type Deps struct {
240 Dylibs []string
241 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700242 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400243 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700244 ProcMacros []string
245 SharedLibs []string
246 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800247 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700248
249 CrtBegin, CrtEnd string
250}
251
252type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400253 DyLibs RustLibraries
254 RLibs RustLibraries
255 SharedLibs android.Paths
256 StaticLibs android.Paths
257 ProcMacros RustLibraries
258 linkDirs []string
259 depFlags []string
260 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700262
Ivan Lozano45901ed2020-07-24 16:05:01 -0400263 // Used by bindgen modules which call clang
264 depClangFlags []string
265 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400266 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400267 depSystemIncludePaths android.Paths
268
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400269 coverageFiles android.Paths
270
Ivan Lozanof1c84332019-09-20 11:00:37 -0700271 CrtBegin android.OptionalPath
272 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700273
274 // Paths to generated source files
275 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276}
277
278type RustLibraries []RustLibrary
279
280type RustLibrary struct {
281 Path android.Path
282 CrateName string
283}
284
285type compiler interface {
286 compilerFlags(ctx ModuleContext, flags Flags) Flags
287 compilerProps() []interface{}
288 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
289 compilerDeps(ctx DepsContext, deps Deps) Deps
290 crateName() string
291
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800292 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200293 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400295
296 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400297
298 Disabled() bool
299 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400300
Ivan Lozanodd055472020-09-28 13:22:45 -0400301 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400302}
303
Matthew Maurerbb3add12020-06-25 09:34:12 -0700304type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700305 exportLinkDirs(...string)
306 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400307 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700308}
309
310type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400311 depFlags []string
312 linkDirs []string
313 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700314}
315
Matthew Maurerbb3add12020-06-25 09:34:12 -0700316func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
317 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
318}
319
320func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
321 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
322}
323
Ivan Lozano2093af22020-08-25 12:48:19 -0400324func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
325 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
326}
327
Colin Cross0de8a1e2020-09-18 14:15:30 -0700328func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
329 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
330 Flags: flagExporter.depFlags,
331 LinkDirs: flagExporter.linkDirs,
332 LinkObjects: flagExporter.linkObjects,
333 })
334}
335
Matthew Maurerbb3add12020-06-25 09:34:12 -0700336var _ exportedFlagsProducer = (*flagExporter)(nil)
337
338func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700339 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700340}
341
Colin Cross0de8a1e2020-09-18 14:15:30 -0700342type FlagExporterInfo struct {
343 Flags []string
344 LinkDirs []string // TODO: this should be android.Paths
345 LinkObjects []string // TODO: this should be android.Paths
346}
347
348var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
349
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400350func (mod *Module) isCoverageVariant() bool {
351 return mod.coverage.Properties.IsCoverageVariant
352}
353
354var _ cc.Coverage = (*Module)(nil)
355
356func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
357 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
358}
359
360func (mod *Module) PreventInstall() {
361 mod.Properties.PreventInstall = true
362}
363
364func (mod *Module) HideFromMake() {
365 mod.Properties.HideFromMake = true
366}
367
368func (mod *Module) MarkAsCoverageVariant(coverage bool) {
369 mod.coverage.Properties.IsCoverageVariant = coverage
370}
371
372func (mod *Module) EnableCoverageIfNeeded() {
373 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700374}
375
376func defaultsFactory() android.Module {
377 return DefaultsFactory()
378}
379
380type Defaults struct {
381 android.ModuleBase
382 android.DefaultsModuleBase
383}
384
385func DefaultsFactory(props ...interface{}) android.Module {
386 module := &Defaults{}
387
388 module.AddProperties(props...)
389 module.AddProperties(
390 &BaseProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400391 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700392 &BaseCompilerProperties{},
393 &BinaryCompilerProperties{},
394 &LibraryCompilerProperties{},
395 &ProcMacroCompilerProperties{},
396 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400397 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700398 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400399 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400400 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200401 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700402 )
403
404 android.InitDefaultsModule(module)
405 return module
406}
407
408func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700409 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410}
411
Ivan Lozano183a3212019-10-18 14:18:45 -0700412func (mod *Module) CcLibrary() bool {
413 if mod.compiler != nil {
414 if _, ok := mod.compiler.(*libraryDecorator); ok {
415 return true
416 }
417 }
418 return false
419}
420
421func (mod *Module) CcLibraryInterface() bool {
422 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400423 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
424 // VariantIs{Static,Shared} is set.
425 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700426 return true
427 }
428 }
429 return false
430}
431
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800432func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700433 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700434 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800435 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700436 }
437 }
438 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
439}
440
441func (mod *Module) SetStatic() {
442 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700443 if library, ok := mod.compiler.(libraryInterface); ok {
444 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700445 return
446 }
447 }
448 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
449}
450
451func (mod *Module) SetShared() {
452 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700453 if library, ok := mod.compiler.(libraryInterface); ok {
454 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700455 return
456 }
457 }
458 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
459}
460
Ivan Lozano183a3212019-10-18 14:18:45 -0700461func (mod *Module) BuildStaticVariant() bool {
462 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700463 if library, ok := mod.compiler.(libraryInterface); ok {
464 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700465 }
466 }
467 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
468}
469
470func (mod *Module) BuildSharedVariant() bool {
471 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700472 if library, ok := mod.compiler.(libraryInterface); ok {
473 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700474 }
475 }
476 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
477}
478
Ivan Lozano183a3212019-10-18 14:18:45 -0700479func (mod *Module) Module() android.Module {
480 return mod
481}
482
Ivan Lozano183a3212019-10-18 14:18:45 -0700483func (mod *Module) OutputFile() android.OptionalPath {
484 return mod.outputFile
485}
486
487func (mod *Module) InRecovery() bool {
488 // For now, Rust has no notion of the recovery image
489 return false
490}
Ivan Lozano183a3212019-10-18 14:18:45 -0700491
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400492func (mod *Module) CoverageFiles() android.Paths {
493 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700494 if !mod.compiler.nativeCoverage() {
495 return android.Paths{}
496 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400497 if library, ok := mod.compiler.(*libraryDecorator); ok {
498 if library.coverageFile != nil {
499 return android.Paths{library.coverageFile}
500 }
501 return android.Paths{}
502 }
503 }
504 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
505}
506
Ivan Lozano183a3212019-10-18 14:18:45 -0700507var _ cc.LinkableInterface = (*Module)(nil)
508
Ivan Lozanoffee3342019-08-27 12:03:00 -0700509func (mod *Module) Init() android.Module {
510 mod.AddProperties(&mod.Properties)
511
512 if mod.compiler != nil {
513 mod.AddProperties(mod.compiler.compilerProps()...)
514 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400515 if mod.coverage != nil {
516 mod.AddProperties(mod.coverage.props()...)
517 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200518 if mod.clippy != nil {
519 mod.AddProperties(mod.clippy.props()...)
520 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400521 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700522 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400523 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400524
Ivan Lozanoffee3342019-08-27 12:03:00 -0700525 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900526 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700527
528 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700529 return mod
530}
531
532func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
533 return &Module{
534 hod: hod,
535 multilib: multilib,
536 }
537}
538func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
539 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200541 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542 return module
543}
544
545type ModuleContext interface {
546 android.ModuleContext
547 ModuleContextIntf
548}
549
550type BaseModuleContext interface {
551 android.BaseModuleContext
552 ModuleContextIntf
553}
554
555type DepsContext interface {
556 android.BottomUpMutatorContext
557 ModuleContextIntf
558}
559
560type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200561 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700562 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563}
564
565type depsContext struct {
566 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567}
568
569type moduleContext struct {
570 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571}
572
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200573type baseModuleContext struct {
574 android.BaseModuleContext
575}
576
577func (ctx *moduleContext) RustModule() *Module {
578 return ctx.Module().(*Module)
579}
580
581func (ctx *moduleContext) toolchain() config.Toolchain {
582 return ctx.RustModule().toolchain(ctx)
583}
584
585func (ctx *depsContext) RustModule() *Module {
586 return ctx.Module().(*Module)
587}
588
589func (ctx *depsContext) toolchain() config.Toolchain {
590 return ctx.RustModule().toolchain(ctx)
591}
592
593func (ctx *baseModuleContext) RustModule() *Module {
594 return ctx.Module().(*Module)
595}
596
597func (ctx *baseModuleContext) toolchain() config.Toolchain {
598 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400599}
600
601func (mod *Module) nativeCoverage() bool {
602 return mod.compiler != nil && mod.compiler.nativeCoverage()
603}
604
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
606 if mod.cachedToolchain == nil {
607 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
608 }
609 return mod.cachedToolchain
610}
611
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200612func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
613 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
614}
615
Ivan Lozanoffee3342019-08-27 12:03:00 -0700616func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
617}
618
619func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
620 ctx := &moduleContext{
621 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700622 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700623
Jiyong Park99644e92020-11-17 22:21:02 +0900624 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
625 if !apexInfo.IsForPlatform() {
626 mod.hideApexVariantFromMake = true
627 }
628
Ivan Lozanoffee3342019-08-27 12:03:00 -0700629 toolchain := mod.toolchain(ctx)
630
631 if !toolchain.Supported() {
632 // This toolchain's unsupported, there's nothing to do for this mod.
633 return
634 }
635
636 deps := mod.depsToPaths(ctx)
637 flags := Flags{
638 Toolchain: toolchain,
639 }
640
641 if mod.compiler != nil {
642 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400643 }
644 if mod.coverage != nil {
645 flags, deps = mod.coverage.flags(ctx, flags, deps)
646 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200647 if mod.clippy != nil {
648 flags, deps = mod.clippy.flags(ctx, flags, deps)
649 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400650
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200651 // SourceProvider needs to call GenerateSource() before compiler calls
652 // compile() so it can provide the source. A SourceProvider has
653 // multiple variants (e.g. source, rlib, dylib). Only the "source"
654 // variant is responsible for effectively generating the source. The
655 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400656 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200657 if mod.compiler.(libraryInterface).source() {
658 mod.sourceProvider.GenerateSource(ctx, deps)
659 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
660 } else {
661 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
662 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700663 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200664 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400665 }
666
667 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700668 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400669
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400671 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200672 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400673 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674 }
675}
676
677func (mod *Module) deps(ctx DepsContext) Deps {
678 deps := Deps{}
679
680 if mod.compiler != nil {
681 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400682 }
683 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700684 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700685 }
686
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400687 if mod.coverage != nil {
688 deps = mod.coverage.deps(ctx, deps)
689 }
690
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
692 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700693 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700694 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
695 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
696 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
697
698 return deps
699
700}
701
Ivan Lozanoffee3342019-08-27 12:03:00 -0700702type dependencyTag struct {
703 blueprint.BaseDependencyTag
704 name string
705 library bool
706 proc_macro bool
707}
708
Jiyong Park65b62242020-11-25 12:44:59 +0900709// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
710// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
711func (d dependencyTag) InstallDepNeeded() bool {
712 return d.library || d.proc_macro
713}
714
715var _ android.InstallNeededDependencyTag = dependencyTag{}
716
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400718 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
719 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
720 dylibDepTag = dependencyTag{name: "dylib", library: true}
721 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
722 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200723 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724)
725
Jiyong Park99644e92020-11-17 22:21:02 +0900726func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
727 tag, ok := depTag.(dependencyTag)
728 return ok && tag == dylibDepTag
729}
730
Matthew Maurer0f003b12020-06-29 14:34:06 -0700731type autoDep struct {
732 variation string
733 depTag dependencyTag
734}
735
736var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200737 rlibVariation = "rlib"
738 dylibVariation = "dylib"
739 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
740 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700741)
742
743type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400744 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700745}
746
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400747func (mod *Module) begin(ctx BaseModuleContext) {
748 if mod.coverage != nil {
749 mod.coverage.begin(ctx)
750 }
751}
752
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
754 var depPaths PathDeps
755
756 directRlibDeps := []*Module{}
757 directDylibDeps := []*Module{}
758 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700759 directSharedLibDeps := [](cc.LinkableInterface){}
760 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400761 directSrcProvidersDeps := []*Module{}
762 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763
764 ctx.VisitDirectDeps(func(dep android.Module) {
765 depName := ctx.OtherModuleName(dep)
766 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400767 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700769
Ivan Lozanoffee3342019-08-27 12:03:00 -0700770 switch depTag {
771 case dylibDepTag:
772 dylib, ok := rustDep.compiler.(libraryInterface)
773 if !ok || !dylib.dylib() {
774 ctx.ModuleErrorf("mod %q not an dylib library", depName)
775 return
776 }
777 directDylibDeps = append(directDylibDeps, rustDep)
778 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
779 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400780
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781 rlib, ok := rustDep.compiler.(libraryInterface)
782 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400783 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784 return
785 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400786 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400788 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789 case procMacroDepTag:
790 directProcMacroDeps = append(directProcMacroDeps, rustDep)
791 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400792 case android.SourceDepTag:
793 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
794 // OS/Arch variant is used.
795 var helper string
796 if ctx.Host() {
797 helper = "missing 'host_supported'?"
798 } else {
799 helper = "device module defined?"
800 }
801
802 if dep.Target().Os != ctx.Os() {
803 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
804 return
805 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
806 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
807 return
808 }
809 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810 }
811
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400812 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700813 if depTag != procMacroDepTag {
814 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
815 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
816 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
817 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 }
819
Ivan Lozanoffee3342019-08-27 12:03:00 -0700820 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400821 linkFile := rustDep.outputFile
822 if !linkFile.Valid() {
823 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
824 depName, ctx.ModuleName())
825 return
826 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700828 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
829 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 }
831 }
832
Ivan Lozano89435d12020-07-31 11:01:18 -0400833 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700834 //Handle C dependencies
835 if _, ok := ccDep.(*Module); !ok {
836 if ccDep.Module().Target().Os != ctx.Os() {
837 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
838 return
839 }
840 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
841 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
842 return
843 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700844 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400845 linkObject := ccDep.OutputFile()
846 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500847
Ivan Lozano2093af22020-08-25 12:48:19 -0400848 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
850 }
851
852 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700853 switch {
854 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400856 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700857 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
858 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
859 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
860 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
861 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400862 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863 directStaticLibDeps = append(directStaticLibDeps, ccDep)
864 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700865 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400867 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700868 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
869 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
870 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
871 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
872 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 directSharedLibDeps = append(directSharedLibDeps, ccDep)
874 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
875 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800876 case cc.IsHeaderDepTag(depTag):
877 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
878 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
879 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
880 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700881 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400882 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700883 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400884 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885 }
886
887 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700888 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
889 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400890 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400893
894 if srcDep, ok := dep.(android.SourceFileProducer); ok {
895 switch depTag {
896 case android.SourceDepTag:
897 // These are usually genrules which don't have per-target variants.
898 directSrcDeps = append(directSrcDeps, srcDep)
899 }
900 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700901 })
902
903 var rlibDepFiles RustLibraries
904 for _, dep := range directRlibDeps {
905 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
906 }
907 var dylibDepFiles RustLibraries
908 for _, dep := range directDylibDeps {
909 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
910 }
911 var procMacroDepFiles RustLibraries
912 for _, dep := range directProcMacroDeps {
913 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
914 }
915
916 var staticLibDepFiles android.Paths
917 for _, dep := range directStaticLibDeps {
918 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
919 }
920
921 var sharedLibDepFiles android.Paths
922 for _, dep := range directSharedLibDeps {
923 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
924 }
925
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400926 var srcProviderDepFiles android.Paths
927 for _, dep := range directSrcProvidersDeps {
928 srcs, _ := dep.OutputFiles("")
929 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
930 }
931 for _, dep := range directSrcDeps {
932 srcs := dep.Srcs()
933 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
934 }
935
Ivan Lozanoffee3342019-08-27 12:03:00 -0700936 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
937 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
938 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
939 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
940 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400941 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942
943 // Dedup exported flags from dependencies
944 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
945 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400946 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
947 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
948 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949
950 return depPaths
951}
952
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800953func (mod *Module) InstallInData() bool {
954 if mod.compiler == nil {
955 return false
956 }
957 return mod.compiler.inData()
958}
959
Ivan Lozanoffee3342019-08-27 12:03:00 -0700960func linkPathFromFilePath(filepath android.Path) string {
961 return strings.Split(filepath.String(), filepath.Base())[0]
962}
Ivan Lozanod648c432020-02-06 12:05:10 -0500963
Ivan Lozanoffee3342019-08-27 12:03:00 -0700964func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
965 ctx := &depsContext{
966 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700968
969 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700970 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700971 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700972 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800973 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400975
Ivan Lozano2b081132020-09-08 12:46:52 -0400976 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400977 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400978 stdLinkage = "rlib-std"
979 }
980
981 rlibDepVariations := commonDepVariations
982 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
983 rlibDepVariations = append(rlibDepVariations,
984 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
985 }
986
Ivan Lozano52767be2019-10-18 14:49:46 -0700987 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400988 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200989 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700990 rlibDepTag, deps.Rlibs...)
991 actx.AddVariationDependencies(
992 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200993 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700994 dylibDepTag, deps.Dylibs...)
995
Ivan Lozano042504f2020-08-18 14:31:23 -0400996 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
997 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400998 if autoDep.depTag == rlibDepTag {
999 actx.AddVariationDependencies(
1000 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1001 autoDep.depTag, deps.Rustlibs...)
1002 } else {
1003 actx.AddVariationDependencies(
1004 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1005 autoDep.depTag, deps.Rustlibs...)
1006 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001007 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001008 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001009 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001010 actx.AddVariationDependencies(
1011 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1012 rlibDepTag, deps.Stdlibs...)
1013 } else {
1014 actx.AddVariationDependencies(
1015 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1016 dylibDepTag, deps.Stdlibs...)
1017 }
1018 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001019 actx.AddVariationDependencies(append(commonDepVariations,
1020 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001021 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001022 actx.AddVariationDependencies(append(commonDepVariations,
1023 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001024 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001025
Zach Johnson3df4e632020-11-06 11:56:27 -08001026 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1027
Colin Cross565cafd2020-09-25 18:47:38 -07001028 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001029 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001030 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001031 }
1032 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001033 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001034 }
1035
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001036 if mod.sourceProvider != nil {
1037 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1038 bindgen.Properties.Custom_bindgen != "" {
1039 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1040 bindgen.Properties.Custom_bindgen)
1041 }
1042 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001043 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001044 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001045}
1046
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001047func BeginMutator(ctx android.BottomUpMutatorContext) {
1048 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1049 mod.beginMutator(ctx)
1050 }
1051}
1052
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001053func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1054 ctx := &baseModuleContext{
1055 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001056 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001057
1058 mod.begin(ctx)
1059}
1060
Ivan Lozanoffee3342019-08-27 12:03:00 -07001061func (mod *Module) Name() string {
1062 name := mod.ModuleBase.Name()
1063 if p, ok := mod.compiler.(interface {
1064 Name(string) string
1065 }); ok {
1066 name = p.Name(name)
1067 }
1068 return name
1069}
1070
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001071func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001072 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001073 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001074 }
1075}
1076
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001077var _ android.HostToolProvider = (*Module)(nil)
1078
1079func (mod *Module) HostToolPath() android.OptionalPath {
1080 if !mod.Host() {
1081 return android.OptionalPath{}
1082 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001083 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1084 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001085 }
1086 return android.OptionalPath{}
1087}
1088
Jiyong Park99644e92020-11-17 22:21:02 +09001089var _ android.ApexModule = (*Module)(nil)
1090
1091func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
1092 return nil
1093}
1094
1095func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1096 depTag := ctx.OtherModuleDependencyTag(dep)
1097
1098 if ccm, ok := dep.(*cc.Module); ok {
1099 if ccm.HasStubsVariants() {
1100 if cc.IsSharedDepTag(depTag) {
1101 // dynamic dep to a stubs lib crosses APEX boundary
1102 return false
1103 }
1104 if cc.IsRuntimeDepTag(depTag) {
1105 // runtime dep to a stubs lib also crosses APEX boundary
1106 return false
1107 }
1108
1109 if cc.IsHeaderDepTag(depTag) {
1110 return false
1111 }
1112 }
1113 if mod.Static() && cc.IsSharedDepTag(depTag) {
1114 // shared_lib dependency from a static lib is considered as crossing
1115 // the APEX boundary because the dependency doesn't actually is
1116 // linked; the dependency is used only during the compilation phase.
1117 return false
1118 }
1119 }
1120
1121 if depTag == procMacroDepTag {
1122 return false
1123 }
1124
1125 return true
1126}
1127
1128// Overrides ApexModule.IsInstallabeToApex()
1129func (mod *Module) IsInstallableToApex() bool {
1130 if mod.compiler != nil {
1131 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1132 return true
1133 }
1134 if _, ok := mod.compiler.(*binaryDecorator); ok {
1135 return true
1136 }
1137 }
1138 return false
1139}
1140
Ivan Lozanoffee3342019-08-27 12:03:00 -07001141var Bool = proptools.Bool
1142var BoolDefault = proptools.BoolDefault
1143var String = proptools.String
1144var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001145
1146var _ android.OutputFileProducer = (*Module)(nil)