blob: 5b9404504ce675fdcd258059bc942f5019ad6843 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Ivan Lozano183a3212019-10-18 14:18:45 -070018 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020026 cc_config "android/soong/cc/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027 "android/soong/rust/config"
28)
29
30var pctx = android.NewPackageContext("android/soong/rust")
31
32func init() {
33 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070034
35 android.AddNeverAllowRules(
36 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070037 NotIn(config.RustAllowedPaths...).
38 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070039
40 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040051 GlobalRustFlags []string // Flags that apply globally to rust
52 GlobalLinkFlags []string // Flags that apply globally to linker
53 RustFlags []string // Flags that apply to rust
54 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070056 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040057 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61type BaseProperties struct {
62 AndroidMkRlibs []string
63 AndroidMkDylibs []string
64 AndroidMkProcMacroLibs []string
65 AndroidMkSharedLibs []string
66 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040067
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040068 SubName string `blueprint:"mutated"`
69
Ivan Lozano43845682020-07-09 21:03:28 -040070 PreventInstall bool
71 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070072}
73
74type Module struct {
75 android.ModuleBase
76 android.DefaultableModuleBase
77
78 Properties BaseProperties
79
80 hod android.HostOrDeviceSupported
81 multilib android.Multilib
82
83 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040084 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020085 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070086 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040087 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070088 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040089
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020090 outputFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
Ivan Lozano43845682020-07-09 21:03:28 -040093func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
94 switch tag {
95 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070096 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -040097 return mod.sourceProvider.Srcs(), nil
98 } else {
99 if mod.outputFile.Valid() {
100 return android.Paths{mod.outputFile.Path()}, nil
101 }
102 return android.Paths{}, nil
103 }
104 default:
105 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
106 }
107}
108
Colin Cross7228ecd2019-11-18 16:00:16 -0800109var _ android.ImageInterface = (*Module)(nil)
110
111func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
112
113func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
114 return true
115}
116
Yifan Hong1b3348d2020-01-21 15:53:22 -0800117func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
118 return mod.InRamdisk()
119}
120
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700121func (mod *Module) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool {
122 return mod.InVendorRamdisk()
123}
124
Colin Cross7228ecd2019-11-18 16:00:16 -0800125func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
126 return mod.InRecovery()
127}
128
129func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
130 return nil
131}
132
133func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
134}
135
Ivan Lozano52767be2019-10-18 14:49:46 -0700136func (mod *Module) SelectedStl() string {
137 return ""
138}
139
Ivan Lozano2b262972019-11-21 12:30:50 -0800140func (mod *Module) NonCcVariants() bool {
141 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400142 if _, ok := mod.compiler.(libraryInterface); ok {
143 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800144 }
145 }
146 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
147}
148
Ivan Lozano52767be2019-10-18 14:49:46 -0700149func (mod *Module) Static() bool {
150 if mod.compiler != nil {
151 if library, ok := mod.compiler.(libraryInterface); ok {
152 return library.static()
153 }
154 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400155 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700156}
157
158func (mod *Module) Shared() bool {
159 if mod.compiler != nil {
160 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400161 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700162 }
163 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400164 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700165}
166
167func (mod *Module) Toc() android.OptionalPath {
168 if mod.compiler != nil {
169 if _, ok := mod.compiler.(libraryInterface); ok {
170 return android.OptionalPath{}
171 }
172 }
173 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
174}
175
Yifan Hong1b3348d2020-01-21 15:53:22 -0800176func (mod *Module) OnlyInRamdisk() bool {
177 return false
178}
179
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700180func (mod *Module) OnlyInVendorRamdisk() bool {
181 return false
182}
183
Ivan Lozano52767be2019-10-18 14:49:46 -0700184func (mod *Module) OnlyInRecovery() bool {
185 return false
186}
187
Colin Crossc511bc52020-04-07 16:50:32 +0000188func (mod *Module) UseSdk() bool {
189 return false
190}
191
Ivan Lozano52767be2019-10-18 14:49:46 -0700192func (mod *Module) UseVndk() bool {
193 return false
194}
195
196func (mod *Module) MustUseVendorVariant() bool {
197 return false
198}
199
200func (mod *Module) IsVndk() bool {
201 return false
202}
203
204func (mod *Module) HasVendorVariant() bool {
205 return false
206}
207
208func (mod *Module) SdkVersion() string {
209 return ""
210}
211
Colin Crossc511bc52020-04-07 16:50:32 +0000212func (mod *Module) AlwaysSdk() bool {
213 return false
214}
215
Jiyong Park2286afd2020-06-16 21:58:53 +0900216func (mod *Module) IsSdkVariant() bool {
217 return false
218}
219
Colin Cross1348ce32020-10-01 13:37:16 -0700220func (mod *Module) SplitPerApiLevel() bool {
221 return false
222}
223
Ivan Lozanoffee3342019-08-27 12:03:00 -0700224type Deps struct {
225 Dylibs []string
226 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700227 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400228 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700229 ProcMacros []string
230 SharedLibs []string
231 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800232 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700233
234 CrtBegin, CrtEnd string
235}
236
237type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400238 DyLibs RustLibraries
239 RLibs RustLibraries
240 SharedLibs android.Paths
241 StaticLibs android.Paths
242 ProcMacros RustLibraries
243 linkDirs []string
244 depFlags []string
245 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700247
Ivan Lozano45901ed2020-07-24 16:05:01 -0400248 // Used by bindgen modules which call clang
249 depClangFlags []string
250 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400251 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400252 depSystemIncludePaths android.Paths
253
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400254 coverageFiles android.Paths
255
Ivan Lozanof1c84332019-09-20 11:00:37 -0700256 CrtBegin android.OptionalPath
257 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700258
259 // Paths to generated source files
260 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261}
262
263type RustLibraries []RustLibrary
264
265type RustLibrary struct {
266 Path android.Path
267 CrateName string
268}
269
270type compiler interface {
271 compilerFlags(ctx ModuleContext, flags Flags) Flags
272 compilerProps() []interface{}
273 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
274 compilerDeps(ctx DepsContext, deps Deps) Deps
275 crateName() string
276
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800277 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200278 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700279 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400280
281 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400282
283 Disabled() bool
284 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400285
Ivan Lozanodd055472020-09-28 13:22:45 -0400286 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400287}
288
Matthew Maurerbb3add12020-06-25 09:34:12 -0700289type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700290 exportLinkDirs(...string)
291 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400292 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700293}
294
295type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400296 depFlags []string
297 linkDirs []string
298 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700299}
300
Matthew Maurerbb3add12020-06-25 09:34:12 -0700301func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
302 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
303}
304
305func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
306 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
307}
308
Ivan Lozano2093af22020-08-25 12:48:19 -0400309func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
310 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
311}
312
Colin Cross0de8a1e2020-09-18 14:15:30 -0700313func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
314 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
315 Flags: flagExporter.depFlags,
316 LinkDirs: flagExporter.linkDirs,
317 LinkObjects: flagExporter.linkObjects,
318 })
319}
320
Matthew Maurerbb3add12020-06-25 09:34:12 -0700321var _ exportedFlagsProducer = (*flagExporter)(nil)
322
323func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700324 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700325}
326
Colin Cross0de8a1e2020-09-18 14:15:30 -0700327type FlagExporterInfo struct {
328 Flags []string
329 LinkDirs []string // TODO: this should be android.Paths
330 LinkObjects []string // TODO: this should be android.Paths
331}
332
333var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
334
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400335func (mod *Module) isCoverageVariant() bool {
336 return mod.coverage.Properties.IsCoverageVariant
337}
338
339var _ cc.Coverage = (*Module)(nil)
340
341func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
342 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
343}
344
345func (mod *Module) PreventInstall() {
346 mod.Properties.PreventInstall = true
347}
348
349func (mod *Module) HideFromMake() {
350 mod.Properties.HideFromMake = true
351}
352
353func (mod *Module) MarkAsCoverageVariant(coverage bool) {
354 mod.coverage.Properties.IsCoverageVariant = coverage
355}
356
357func (mod *Module) EnableCoverageIfNeeded() {
358 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700359}
360
361func defaultsFactory() android.Module {
362 return DefaultsFactory()
363}
364
365type Defaults struct {
366 android.ModuleBase
367 android.DefaultsModuleBase
368}
369
370func DefaultsFactory(props ...interface{}) android.Module {
371 module := &Defaults{}
372
373 module.AddProperties(props...)
374 module.AddProperties(
375 &BaseProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400376 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700377 &BaseCompilerProperties{},
378 &BinaryCompilerProperties{},
379 &LibraryCompilerProperties{},
380 &ProcMacroCompilerProperties{},
381 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400382 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700383 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400384 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400385 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200386 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700387 )
388
389 android.InitDefaultsModule(module)
390 return module
391}
392
393func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700394 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700395}
396
Ivan Lozano183a3212019-10-18 14:18:45 -0700397func (mod *Module) CcLibrary() bool {
398 if mod.compiler != nil {
399 if _, ok := mod.compiler.(*libraryDecorator); ok {
400 return true
401 }
402 }
403 return false
404}
405
406func (mod *Module) CcLibraryInterface() bool {
407 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400408 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
409 // VariantIs{Static,Shared} is set.
410 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700411 return true
412 }
413 }
414 return false
415}
416
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800417func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700418 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700419 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800420 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700421 }
422 }
423 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
424}
425
426func (mod *Module) SetStatic() {
427 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700428 if library, ok := mod.compiler.(libraryInterface); ok {
429 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700430 return
431 }
432 }
433 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
434}
435
436func (mod *Module) SetShared() {
437 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700438 if library, ok := mod.compiler.(libraryInterface); ok {
439 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700440 return
441 }
442 }
443 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
444}
445
Ivan Lozano183a3212019-10-18 14:18:45 -0700446func (mod *Module) BuildStaticVariant() bool {
447 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700448 if library, ok := mod.compiler.(libraryInterface); ok {
449 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700450 }
451 }
452 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
453}
454
455func (mod *Module) BuildSharedVariant() bool {
456 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700457 if library, ok := mod.compiler.(libraryInterface); ok {
458 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700459 }
460 }
461 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
462}
463
Ivan Lozano183a3212019-10-18 14:18:45 -0700464func (mod *Module) Module() android.Module {
465 return mod
466}
467
Ivan Lozano183a3212019-10-18 14:18:45 -0700468func (mod *Module) OutputFile() android.OptionalPath {
469 return mod.outputFile
470}
471
472func (mod *Module) InRecovery() bool {
473 // For now, Rust has no notion of the recovery image
474 return false
475}
Ivan Lozano183a3212019-10-18 14:18:45 -0700476
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400477func (mod *Module) CoverageFiles() android.Paths {
478 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700479 if !mod.compiler.nativeCoverage() {
480 return android.Paths{}
481 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400482 if library, ok := mod.compiler.(*libraryDecorator); ok {
483 if library.coverageFile != nil {
484 return android.Paths{library.coverageFile}
485 }
486 return android.Paths{}
487 }
488 }
489 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
490}
491
Ivan Lozano183a3212019-10-18 14:18:45 -0700492var _ cc.LinkableInterface = (*Module)(nil)
493
Ivan Lozanoffee3342019-08-27 12:03:00 -0700494func (mod *Module) Init() android.Module {
495 mod.AddProperties(&mod.Properties)
496
497 if mod.compiler != nil {
498 mod.AddProperties(mod.compiler.compilerProps()...)
499 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400500 if mod.coverage != nil {
501 mod.AddProperties(mod.coverage.props()...)
502 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200503 if mod.clippy != nil {
504 mod.AddProperties(mod.clippy.props()...)
505 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400506 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700507 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400508 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400509
Ivan Lozanoffee3342019-08-27 12:03:00 -0700510 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
511
512 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700513 return mod
514}
515
516func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
517 return &Module{
518 hod: hod,
519 multilib: multilib,
520 }
521}
522func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
523 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400524 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200525 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700526 return module
527}
528
529type ModuleContext interface {
530 android.ModuleContext
531 ModuleContextIntf
532}
533
534type BaseModuleContext interface {
535 android.BaseModuleContext
536 ModuleContextIntf
537}
538
539type DepsContext interface {
540 android.BottomUpMutatorContext
541 ModuleContextIntf
542}
543
544type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200545 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547}
548
549type depsContext struct {
550 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551}
552
553type moduleContext struct {
554 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555}
556
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200557type baseModuleContext struct {
558 android.BaseModuleContext
559}
560
561func (ctx *moduleContext) RustModule() *Module {
562 return ctx.Module().(*Module)
563}
564
565func (ctx *moduleContext) toolchain() config.Toolchain {
566 return ctx.RustModule().toolchain(ctx)
567}
568
569func (ctx *depsContext) RustModule() *Module {
570 return ctx.Module().(*Module)
571}
572
573func (ctx *depsContext) toolchain() config.Toolchain {
574 return ctx.RustModule().toolchain(ctx)
575}
576
577func (ctx *baseModuleContext) RustModule() *Module {
578 return ctx.Module().(*Module)
579}
580
581func (ctx *baseModuleContext) toolchain() config.Toolchain {
582 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583}
584
585func (mod *Module) nativeCoverage() bool {
586 return mod.compiler != nil && mod.compiler.nativeCoverage()
587}
588
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
590 if mod.cachedToolchain == nil {
591 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
592 }
593 return mod.cachedToolchain
594}
595
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200596func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
597 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
598}
599
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
601}
602
603func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
604 ctx := &moduleContext{
605 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607
608 toolchain := mod.toolchain(ctx)
609
610 if !toolchain.Supported() {
611 // This toolchain's unsupported, there's nothing to do for this mod.
612 return
613 }
614
615 deps := mod.depsToPaths(ctx)
616 flags := Flags{
617 Toolchain: toolchain,
618 }
619
620 if mod.compiler != nil {
621 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400622 }
623 if mod.coverage != nil {
624 flags, deps = mod.coverage.flags(ctx, flags, deps)
625 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200626 if mod.clippy != nil {
627 flags, deps = mod.clippy.flags(ctx, flags, deps)
628 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400629
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200630 // SourceProvider needs to call GenerateSource() before compiler calls
631 // compile() so it can provide the source. A SourceProvider has
632 // multiple variants (e.g. source, rlib, dylib). Only the "source"
633 // variant is responsible for effectively generating the source. The
634 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400635 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200636 if mod.compiler.(libraryInterface).source() {
637 mod.sourceProvider.GenerateSource(ctx, deps)
638 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
639 } else {
640 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
641 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700642 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200643 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400644 }
645
646 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700647 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400648
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400650 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200651 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400652 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700653 }
654}
655
656func (mod *Module) deps(ctx DepsContext) Deps {
657 deps := Deps{}
658
659 if mod.compiler != nil {
660 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400661 }
662 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700663 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664 }
665
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400666 if mod.coverage != nil {
667 deps = mod.coverage.deps(ctx, deps)
668 }
669
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
671 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700672 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
674 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
675 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
676
677 return deps
678
679}
680
Ivan Lozanoffee3342019-08-27 12:03:00 -0700681type dependencyTag struct {
682 blueprint.BaseDependencyTag
683 name string
684 library bool
685 proc_macro bool
686}
687
688var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400689 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
690 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
691 dylibDepTag = dependencyTag{name: "dylib", library: true}
692 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
693 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200694 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700695)
696
Matthew Maurer0f003b12020-06-29 14:34:06 -0700697type autoDep struct {
698 variation string
699 depTag dependencyTag
700}
701
702var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200703 rlibVariation = "rlib"
704 dylibVariation = "dylib"
705 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
706 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700707)
708
709type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400710 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700711}
712
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400713func (mod *Module) begin(ctx BaseModuleContext) {
714 if mod.coverage != nil {
715 mod.coverage.begin(ctx)
716 }
717}
718
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
720 var depPaths PathDeps
721
722 directRlibDeps := []*Module{}
723 directDylibDeps := []*Module{}
724 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700725 directSharedLibDeps := [](cc.LinkableInterface){}
726 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400727 directSrcProvidersDeps := []*Module{}
728 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729
730 ctx.VisitDirectDeps(func(dep android.Module) {
731 depName := ctx.OtherModuleName(dep)
732 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400733 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700734 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700735
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736 switch depTag {
737 case dylibDepTag:
738 dylib, ok := rustDep.compiler.(libraryInterface)
739 if !ok || !dylib.dylib() {
740 ctx.ModuleErrorf("mod %q not an dylib library", depName)
741 return
742 }
743 directDylibDeps = append(directDylibDeps, rustDep)
744 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
745 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400746
Ivan Lozanoffee3342019-08-27 12:03:00 -0700747 rlib, ok := rustDep.compiler.(libraryInterface)
748 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400749 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700750 return
751 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400752 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400754 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 case procMacroDepTag:
756 directProcMacroDeps = append(directProcMacroDeps, rustDep)
757 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400758 case android.SourceDepTag:
759 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
760 // OS/Arch variant is used.
761 var helper string
762 if ctx.Host() {
763 helper = "missing 'host_supported'?"
764 } else {
765 helper = "device module defined?"
766 }
767
768 if dep.Target().Os != ctx.Os() {
769 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
770 return
771 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
772 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
773 return
774 }
775 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700776 }
777
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400778 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700779 if depTag != procMacroDepTag {
780 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
781 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
782 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
783 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784 }
785
Ivan Lozanoffee3342019-08-27 12:03:00 -0700786 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400787 linkFile := rustDep.outputFile
788 if !linkFile.Valid() {
789 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
790 depName, ctx.ModuleName())
791 return
792 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700793 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700794 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
795 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796 }
797 }
798
Ivan Lozano89435d12020-07-31 11:01:18 -0400799 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700800 //Handle C dependencies
801 if _, ok := ccDep.(*Module); !ok {
802 if ccDep.Module().Target().Os != ctx.Os() {
803 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
804 return
805 }
806 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
807 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
808 return
809 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700810 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400811 linkObject := ccDep.OutputFile()
812 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500813
Ivan Lozano2093af22020-08-25 12:48:19 -0400814 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
816 }
817
818 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700819 switch {
820 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400822 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700823 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
824 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
825 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
826 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
827 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400828 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 directStaticLibDeps = append(directStaticLibDeps, ccDep)
830 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700831 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400833 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700834 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
835 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
836 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
837 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
838 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839 directSharedLibDeps = append(directSharedLibDeps, ccDep)
840 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
841 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800842 case cc.IsHeaderDepTag(depTag):
843 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
844 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
845 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
846 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700847 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400848 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700849 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400850 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851 }
852
853 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700854 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
855 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400856 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700858 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400859
860 if srcDep, ok := dep.(android.SourceFileProducer); ok {
861 switch depTag {
862 case android.SourceDepTag:
863 // These are usually genrules which don't have per-target variants.
864 directSrcDeps = append(directSrcDeps, srcDep)
865 }
866 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700867 })
868
869 var rlibDepFiles RustLibraries
870 for _, dep := range directRlibDeps {
871 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
872 }
873 var dylibDepFiles RustLibraries
874 for _, dep := range directDylibDeps {
875 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
876 }
877 var procMacroDepFiles RustLibraries
878 for _, dep := range directProcMacroDeps {
879 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
880 }
881
882 var staticLibDepFiles android.Paths
883 for _, dep := range directStaticLibDeps {
884 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
885 }
886
887 var sharedLibDepFiles android.Paths
888 for _, dep := range directSharedLibDeps {
889 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
890 }
891
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400892 var srcProviderDepFiles android.Paths
893 for _, dep := range directSrcProvidersDeps {
894 srcs, _ := dep.OutputFiles("")
895 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
896 }
897 for _, dep := range directSrcDeps {
898 srcs := dep.Srcs()
899 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
900 }
901
Ivan Lozanoffee3342019-08-27 12:03:00 -0700902 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
903 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
904 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
905 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
906 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400907 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700908
909 // Dedup exported flags from dependencies
910 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
911 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400912 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
913 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
914 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915
916 return depPaths
917}
918
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800919func (mod *Module) InstallInData() bool {
920 if mod.compiler == nil {
921 return false
922 }
923 return mod.compiler.inData()
924}
925
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926func linkPathFromFilePath(filepath android.Path) string {
927 return strings.Split(filepath.String(), filepath.Base())[0]
928}
Ivan Lozanod648c432020-02-06 12:05:10 -0500929
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
931 ctx := &depsContext{
932 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700933 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700934
935 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700936 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700938 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800939 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400941
Ivan Lozano2b081132020-09-08 12:46:52 -0400942 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400943 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400944 stdLinkage = "rlib-std"
945 }
946
947 rlibDepVariations := commonDepVariations
948 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
949 rlibDepVariations = append(rlibDepVariations,
950 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
951 }
952
Ivan Lozano52767be2019-10-18 14:49:46 -0700953 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400954 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200955 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700956 rlibDepTag, deps.Rlibs...)
957 actx.AddVariationDependencies(
958 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200959 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700960 dylibDepTag, deps.Dylibs...)
961
Ivan Lozano042504f2020-08-18 14:31:23 -0400962 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
963 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400964 if autoDep.depTag == rlibDepTag {
965 actx.AddVariationDependencies(
966 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
967 autoDep.depTag, deps.Rustlibs...)
968 } else {
969 actx.AddVariationDependencies(
970 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
971 autoDep.depTag, deps.Rustlibs...)
972 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700973 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400974 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400975 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400976 actx.AddVariationDependencies(
977 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
978 rlibDepTag, deps.Stdlibs...)
979 } else {
980 actx.AddVariationDependencies(
981 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
982 dylibDepTag, deps.Stdlibs...)
983 }
984 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700985 actx.AddVariationDependencies(append(commonDepVariations,
986 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700987 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700988 actx.AddVariationDependencies(append(commonDepVariations,
989 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700990 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700991
Zach Johnson3df4e632020-11-06 11:56:27 -0800992 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
993
Colin Cross565cafd2020-09-25 18:47:38 -0700994 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700995 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -0700996 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700997 }
998 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -0700999 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001000 }
1001
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001002 if mod.sourceProvider != nil {
1003 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1004 bindgen.Properties.Custom_bindgen != "" {
1005 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1006 bindgen.Properties.Custom_bindgen)
1007 }
1008 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001009 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001010 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001011}
1012
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001013func BeginMutator(ctx android.BottomUpMutatorContext) {
1014 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1015 mod.beginMutator(ctx)
1016 }
1017}
1018
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001019func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1020 ctx := &baseModuleContext{
1021 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001022 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001023
1024 mod.begin(ctx)
1025}
1026
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027func (mod *Module) Name() string {
1028 name := mod.ModuleBase.Name()
1029 if p, ok := mod.compiler.(interface {
1030 Name(string) string
1031 }); ok {
1032 name = p.Name(name)
1033 }
1034 return name
1035}
1036
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001037func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001038 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001039 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001040 }
1041}
1042
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001043var _ android.HostToolProvider = (*Module)(nil)
1044
1045func (mod *Module) HostToolPath() android.OptionalPath {
1046 if !mod.Host() {
1047 return android.OptionalPath{}
1048 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001049 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1050 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001051 }
1052 return android.OptionalPath{}
1053}
1054
Ivan Lozanoffee3342019-08-27 12:03:00 -07001055var Bool = proptools.Bool
1056var BoolDefault = proptools.BoolDefault
1057var String = proptools.String
1058var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001059
1060var _ android.OutputFileProducer = (*Module)(nil)