blob: bd81c1754f5432372f32ce825e50c70200b41631 [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
Jiyong Park65b62242020-11-25 12:44:59 +0900688// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
689// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
690func (d dependencyTag) InstallDepNeeded() bool {
691 return d.library || d.proc_macro
692}
693
694var _ android.InstallNeededDependencyTag = dependencyTag{}
695
Ivan Lozanoffee3342019-08-27 12:03:00 -0700696var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400697 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
698 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
699 dylibDepTag = dependencyTag{name: "dylib", library: true}
700 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
701 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200702 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700703)
704
Matthew Maurer0f003b12020-06-29 14:34:06 -0700705type autoDep struct {
706 variation string
707 depTag dependencyTag
708}
709
710var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200711 rlibVariation = "rlib"
712 dylibVariation = "dylib"
713 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
714 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700715)
716
717type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400718 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700719}
720
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400721func (mod *Module) begin(ctx BaseModuleContext) {
722 if mod.coverage != nil {
723 mod.coverage.begin(ctx)
724 }
725}
726
Ivan Lozanoffee3342019-08-27 12:03:00 -0700727func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
728 var depPaths PathDeps
729
730 directRlibDeps := []*Module{}
731 directDylibDeps := []*Module{}
732 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700733 directSharedLibDeps := [](cc.LinkableInterface){}
734 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400735 directSrcProvidersDeps := []*Module{}
736 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700737
738 ctx.VisitDirectDeps(func(dep android.Module) {
739 depName := ctx.OtherModuleName(dep)
740 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400741 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700743
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744 switch depTag {
745 case dylibDepTag:
746 dylib, ok := rustDep.compiler.(libraryInterface)
747 if !ok || !dylib.dylib() {
748 ctx.ModuleErrorf("mod %q not an dylib library", depName)
749 return
750 }
751 directDylibDeps = append(directDylibDeps, rustDep)
752 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
753 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400754
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 rlib, ok := rustDep.compiler.(libraryInterface)
756 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400757 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758 return
759 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400760 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400762 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763 case procMacroDepTag:
764 directProcMacroDeps = append(directProcMacroDeps, rustDep)
765 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400766 case android.SourceDepTag:
767 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
768 // OS/Arch variant is used.
769 var helper string
770 if ctx.Host() {
771 helper = "missing 'host_supported'?"
772 } else {
773 helper = "device module defined?"
774 }
775
776 if dep.Target().Os != ctx.Os() {
777 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
778 return
779 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
780 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
781 return
782 }
783 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784 }
785
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400786 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700787 if depTag != procMacroDepTag {
788 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
789 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
790 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
791 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792 }
793
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400795 linkFile := rustDep.outputFile
796 if !linkFile.Valid() {
797 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
798 depName, ctx.ModuleName())
799 return
800 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700801 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700802 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
803 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 }
805 }
806
Ivan Lozano89435d12020-07-31 11:01:18 -0400807 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700808 //Handle C dependencies
809 if _, ok := ccDep.(*Module); !ok {
810 if ccDep.Module().Target().Os != ctx.Os() {
811 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
812 return
813 }
814 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
815 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
816 return
817 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700818 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400819 linkObject := ccDep.OutputFile()
820 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500821
Ivan Lozano2093af22020-08-25 12:48:19 -0400822 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
824 }
825
826 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700827 switch {
828 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400830 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700831 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
832 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
833 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
834 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
835 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400836 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700837 directStaticLibDeps = append(directStaticLibDeps, ccDep)
838 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700839 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400841 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700842 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
843 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
844 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
845 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
846 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700847 directSharedLibDeps = append(directSharedLibDeps, ccDep)
848 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
849 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800850 case cc.IsHeaderDepTag(depTag):
851 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
852 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
853 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
854 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700855 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400856 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700857 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400858 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859 }
860
861 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700862 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
863 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400864 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400867
868 if srcDep, ok := dep.(android.SourceFileProducer); ok {
869 switch depTag {
870 case android.SourceDepTag:
871 // These are usually genrules which don't have per-target variants.
872 directSrcDeps = append(directSrcDeps, srcDep)
873 }
874 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700875 })
876
877 var rlibDepFiles RustLibraries
878 for _, dep := range directRlibDeps {
879 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
880 }
881 var dylibDepFiles RustLibraries
882 for _, dep := range directDylibDeps {
883 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
884 }
885 var procMacroDepFiles RustLibraries
886 for _, dep := range directProcMacroDeps {
887 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
888 }
889
890 var staticLibDepFiles android.Paths
891 for _, dep := range directStaticLibDeps {
892 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
893 }
894
895 var sharedLibDepFiles android.Paths
896 for _, dep := range directSharedLibDeps {
897 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
898 }
899
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400900 var srcProviderDepFiles android.Paths
901 for _, dep := range directSrcProvidersDeps {
902 srcs, _ := dep.OutputFiles("")
903 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
904 }
905 for _, dep := range directSrcDeps {
906 srcs := dep.Srcs()
907 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
908 }
909
Ivan Lozanoffee3342019-08-27 12:03:00 -0700910 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
911 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
912 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
913 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
914 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400915 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700916
917 // Dedup exported flags from dependencies
918 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
919 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400920 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
921 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
922 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700923
924 return depPaths
925}
926
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800927func (mod *Module) InstallInData() bool {
928 if mod.compiler == nil {
929 return false
930 }
931 return mod.compiler.inData()
932}
933
Ivan Lozanoffee3342019-08-27 12:03:00 -0700934func linkPathFromFilePath(filepath android.Path) string {
935 return strings.Split(filepath.String(), filepath.Base())[0]
936}
Ivan Lozanod648c432020-02-06 12:05:10 -0500937
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
939 ctx := &depsContext{
940 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700941 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942
943 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700944 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700946 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800947 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400949
Ivan Lozano2b081132020-09-08 12:46:52 -0400950 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400951 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400952 stdLinkage = "rlib-std"
953 }
954
955 rlibDepVariations := commonDepVariations
956 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
957 rlibDepVariations = append(rlibDepVariations,
958 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
959 }
960
Ivan Lozano52767be2019-10-18 14:49:46 -0700961 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400962 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200963 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700964 rlibDepTag, deps.Rlibs...)
965 actx.AddVariationDependencies(
966 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200967 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700968 dylibDepTag, deps.Dylibs...)
969
Ivan Lozano042504f2020-08-18 14:31:23 -0400970 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
971 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400972 if autoDep.depTag == rlibDepTag {
973 actx.AddVariationDependencies(
974 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
975 autoDep.depTag, deps.Rustlibs...)
976 } else {
977 actx.AddVariationDependencies(
978 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
979 autoDep.depTag, deps.Rustlibs...)
980 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700981 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400982 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400983 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400984 actx.AddVariationDependencies(
985 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
986 rlibDepTag, deps.Stdlibs...)
987 } else {
988 actx.AddVariationDependencies(
989 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
990 dylibDepTag, deps.Stdlibs...)
991 }
992 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700993 actx.AddVariationDependencies(append(commonDepVariations,
994 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700995 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700996 actx.AddVariationDependencies(append(commonDepVariations,
997 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700998 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700999
Zach Johnson3df4e632020-11-06 11:56:27 -08001000 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1001
Colin Cross565cafd2020-09-25 18:47:38 -07001002 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001003 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001004 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001005 }
1006 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001007 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001008 }
1009
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001010 if mod.sourceProvider != nil {
1011 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1012 bindgen.Properties.Custom_bindgen != "" {
1013 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1014 bindgen.Properties.Custom_bindgen)
1015 }
1016 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001017 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001018 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001019}
1020
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001021func BeginMutator(ctx android.BottomUpMutatorContext) {
1022 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1023 mod.beginMutator(ctx)
1024 }
1025}
1026
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001027func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1028 ctx := &baseModuleContext{
1029 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001030 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001031
1032 mod.begin(ctx)
1033}
1034
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035func (mod *Module) Name() string {
1036 name := mod.ModuleBase.Name()
1037 if p, ok := mod.compiler.(interface {
1038 Name(string) string
1039 }); ok {
1040 name = p.Name(name)
1041 }
1042 return name
1043}
1044
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001045func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001046 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001047 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001048 }
1049}
1050
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001051var _ android.HostToolProvider = (*Module)(nil)
1052
1053func (mod *Module) HostToolPath() android.OptionalPath {
1054 if !mod.Host() {
1055 return android.OptionalPath{}
1056 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001057 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1058 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001059 }
1060 return android.OptionalPath{}
1061}
1062
Ivan Lozanoffee3342019-08-27 12:03:00 -07001063var Bool = proptools.Bool
1064var BoolDefault = proptools.BoolDefault
1065var String = proptools.String
1066var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001067
1068var _ android.OutputFileProducer = (*Module)(nil)