blob: e65b90e8aee98ea6ac3799f25c9b00b099fb2c9a [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
232
233 CrtBegin, CrtEnd string
234}
235
236type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400237 DyLibs RustLibraries
238 RLibs RustLibraries
239 SharedLibs android.Paths
240 StaticLibs android.Paths
241 ProcMacros RustLibraries
242 linkDirs []string
243 depFlags []string
244 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700246
Ivan Lozano45901ed2020-07-24 16:05:01 -0400247 // Used by bindgen modules which call clang
248 depClangFlags []string
249 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400250 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400251 depSystemIncludePaths android.Paths
252
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400253 coverageFiles android.Paths
254
Ivan Lozanof1c84332019-09-20 11:00:37 -0700255 CrtBegin android.OptionalPath
256 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700257
258 // Paths to generated source files
259 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700260}
261
262type RustLibraries []RustLibrary
263
264type RustLibrary struct {
265 Path android.Path
266 CrateName string
267}
268
269type compiler interface {
270 compilerFlags(ctx ModuleContext, flags Flags) Flags
271 compilerProps() []interface{}
272 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
273 compilerDeps(ctx DepsContext, deps Deps) Deps
274 crateName() string
275
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800276 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200277 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700278 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400279
280 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400281
282 Disabled() bool
283 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400284
Ivan Lozanodd055472020-09-28 13:22:45 -0400285 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400286}
287
Matthew Maurerbb3add12020-06-25 09:34:12 -0700288type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700289 exportLinkDirs(...string)
290 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400291 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700292}
293
294type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400295 depFlags []string
296 linkDirs []string
297 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700298}
299
Matthew Maurerbb3add12020-06-25 09:34:12 -0700300func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
301 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
302}
303
304func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
305 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
306}
307
Ivan Lozano2093af22020-08-25 12:48:19 -0400308func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
309 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
310}
311
Colin Cross0de8a1e2020-09-18 14:15:30 -0700312func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
313 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
314 Flags: flagExporter.depFlags,
315 LinkDirs: flagExporter.linkDirs,
316 LinkObjects: flagExporter.linkObjects,
317 })
318}
319
Matthew Maurerbb3add12020-06-25 09:34:12 -0700320var _ exportedFlagsProducer = (*flagExporter)(nil)
321
322func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700323 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700324}
325
Colin Cross0de8a1e2020-09-18 14:15:30 -0700326type FlagExporterInfo struct {
327 Flags []string
328 LinkDirs []string // TODO: this should be android.Paths
329 LinkObjects []string // TODO: this should be android.Paths
330}
331
332var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
333
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400334func (mod *Module) isCoverageVariant() bool {
335 return mod.coverage.Properties.IsCoverageVariant
336}
337
338var _ cc.Coverage = (*Module)(nil)
339
340func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
341 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
342}
343
344func (mod *Module) PreventInstall() {
345 mod.Properties.PreventInstall = true
346}
347
348func (mod *Module) HideFromMake() {
349 mod.Properties.HideFromMake = true
350}
351
352func (mod *Module) MarkAsCoverageVariant(coverage bool) {
353 mod.coverage.Properties.IsCoverageVariant = coverage
354}
355
356func (mod *Module) EnableCoverageIfNeeded() {
357 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700358}
359
360func defaultsFactory() android.Module {
361 return DefaultsFactory()
362}
363
364type Defaults struct {
365 android.ModuleBase
366 android.DefaultsModuleBase
367}
368
369func DefaultsFactory(props ...interface{}) android.Module {
370 module := &Defaults{}
371
372 module.AddProperties(props...)
373 module.AddProperties(
374 &BaseProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400375 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376 &BaseCompilerProperties{},
377 &BinaryCompilerProperties{},
378 &LibraryCompilerProperties{},
379 &ProcMacroCompilerProperties{},
380 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400381 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700382 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400383 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400384 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200385 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700386 )
387
388 android.InitDefaultsModule(module)
389 return module
390}
391
392func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700393 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394}
395
Ivan Lozano183a3212019-10-18 14:18:45 -0700396func (mod *Module) CcLibrary() bool {
397 if mod.compiler != nil {
398 if _, ok := mod.compiler.(*libraryDecorator); ok {
399 return true
400 }
401 }
402 return false
403}
404
405func (mod *Module) CcLibraryInterface() bool {
406 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400407 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
408 // VariantIs{Static,Shared} is set.
409 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700410 return true
411 }
412 }
413 return false
414}
415
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800416func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700417 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700418 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800419 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700420 }
421 }
422 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
423}
424
425func (mod *Module) SetStatic() {
426 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700427 if library, ok := mod.compiler.(libraryInterface); ok {
428 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700429 return
430 }
431 }
432 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
433}
434
435func (mod *Module) SetShared() {
436 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700437 if library, ok := mod.compiler.(libraryInterface); ok {
438 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700439 return
440 }
441 }
442 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
443}
444
Ivan Lozano183a3212019-10-18 14:18:45 -0700445func (mod *Module) BuildStaticVariant() bool {
446 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700447 if library, ok := mod.compiler.(libraryInterface); ok {
448 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700449 }
450 }
451 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
452}
453
454func (mod *Module) BuildSharedVariant() bool {
455 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700456 if library, ok := mod.compiler.(libraryInterface); ok {
457 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700458 }
459 }
460 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
461}
462
Ivan Lozano183a3212019-10-18 14:18:45 -0700463func (mod *Module) Module() android.Module {
464 return mod
465}
466
Ivan Lozano183a3212019-10-18 14:18:45 -0700467func (mod *Module) OutputFile() android.OptionalPath {
468 return mod.outputFile
469}
470
471func (mod *Module) InRecovery() bool {
472 // For now, Rust has no notion of the recovery image
473 return false
474}
Ivan Lozano183a3212019-10-18 14:18:45 -0700475
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400476func (mod *Module) CoverageFiles() android.Paths {
477 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700478 if !mod.compiler.nativeCoverage() {
479 return android.Paths{}
480 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400481 if library, ok := mod.compiler.(*libraryDecorator); ok {
482 if library.coverageFile != nil {
483 return android.Paths{library.coverageFile}
484 }
485 return android.Paths{}
486 }
487 }
488 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
489}
490
Ivan Lozano183a3212019-10-18 14:18:45 -0700491var _ cc.LinkableInterface = (*Module)(nil)
492
Ivan Lozanoffee3342019-08-27 12:03:00 -0700493func (mod *Module) Init() android.Module {
494 mod.AddProperties(&mod.Properties)
495
496 if mod.compiler != nil {
497 mod.AddProperties(mod.compiler.compilerProps()...)
498 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400499 if mod.coverage != nil {
500 mod.AddProperties(mod.coverage.props()...)
501 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200502 if mod.clippy != nil {
503 mod.AddProperties(mod.clippy.props()...)
504 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400505 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700506 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400507 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400508
Ivan Lozanoffee3342019-08-27 12:03:00 -0700509 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
510
511 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700512 return mod
513}
514
515func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
516 return &Module{
517 hod: hod,
518 multilib: multilib,
519 }
520}
521func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
522 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400523 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200524 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700525 return module
526}
527
528type ModuleContext interface {
529 android.ModuleContext
530 ModuleContextIntf
531}
532
533type BaseModuleContext interface {
534 android.BaseModuleContext
535 ModuleContextIntf
536}
537
538type DepsContext interface {
539 android.BottomUpMutatorContext
540 ModuleContextIntf
541}
542
543type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200544 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700545 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546}
547
548type depsContext struct {
549 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700550}
551
552type moduleContext struct {
553 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554}
555
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200556type baseModuleContext struct {
557 android.BaseModuleContext
558}
559
560func (ctx *moduleContext) RustModule() *Module {
561 return ctx.Module().(*Module)
562}
563
564func (ctx *moduleContext) toolchain() config.Toolchain {
565 return ctx.RustModule().toolchain(ctx)
566}
567
568func (ctx *depsContext) RustModule() *Module {
569 return ctx.Module().(*Module)
570}
571
572func (ctx *depsContext) toolchain() config.Toolchain {
573 return ctx.RustModule().toolchain(ctx)
574}
575
576func (ctx *baseModuleContext) RustModule() *Module {
577 return ctx.Module().(*Module)
578}
579
580func (ctx *baseModuleContext) toolchain() config.Toolchain {
581 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400582}
583
584func (mod *Module) nativeCoverage() bool {
585 return mod.compiler != nil && mod.compiler.nativeCoverage()
586}
587
Ivan Lozanoffee3342019-08-27 12:03:00 -0700588func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
589 if mod.cachedToolchain == nil {
590 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
591 }
592 return mod.cachedToolchain
593}
594
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200595func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
596 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
597}
598
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
600}
601
602func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
603 ctx := &moduleContext{
604 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606
607 toolchain := mod.toolchain(ctx)
608
609 if !toolchain.Supported() {
610 // This toolchain's unsupported, there's nothing to do for this mod.
611 return
612 }
613
614 deps := mod.depsToPaths(ctx)
615 flags := Flags{
616 Toolchain: toolchain,
617 }
618
619 if mod.compiler != nil {
620 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400621 }
622 if mod.coverage != nil {
623 flags, deps = mod.coverage.flags(ctx, flags, deps)
624 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200625 if mod.clippy != nil {
626 flags, deps = mod.clippy.flags(ctx, flags, deps)
627 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400628
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200629 // SourceProvider needs to call GenerateSource() before compiler calls
630 // compile() so it can provide the source. A SourceProvider has
631 // multiple variants (e.g. source, rlib, dylib). Only the "source"
632 // variant is responsible for effectively generating the source. The
633 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400634 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200635 if mod.compiler.(libraryInterface).source() {
636 mod.sourceProvider.GenerateSource(ctx, deps)
637 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
638 } else {
639 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
640 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700641 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200642 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400643 }
644
645 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700646 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400647
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400649 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200650 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400651 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652 }
653}
654
655func (mod *Module) deps(ctx DepsContext) Deps {
656 deps := Deps{}
657
658 if mod.compiler != nil {
659 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400660 }
661 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700662 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663 }
664
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400665 if mod.coverage != nil {
666 deps = mod.coverage.deps(ctx, deps)
667 }
668
Ivan Lozanoffee3342019-08-27 12:03:00 -0700669 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
670 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700671 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700672 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
673 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
674 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
675
676 return deps
677
678}
679
Ivan Lozanoffee3342019-08-27 12:03:00 -0700680type dependencyTag struct {
681 blueprint.BaseDependencyTag
682 name string
683 library bool
684 proc_macro bool
685}
686
687var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400688 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
689 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
690 dylibDepTag = dependencyTag{name: "dylib", library: true}
691 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
692 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200693 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700694)
695
Matthew Maurer0f003b12020-06-29 14:34:06 -0700696type autoDep struct {
697 variation string
698 depTag dependencyTag
699}
700
701var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200702 rlibVariation = "rlib"
703 dylibVariation = "dylib"
704 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
705 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700706)
707
708type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400709 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700710}
711
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400712func (mod *Module) begin(ctx BaseModuleContext) {
713 if mod.coverage != nil {
714 mod.coverage.begin(ctx)
715 }
716}
717
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
719 var depPaths PathDeps
720
721 directRlibDeps := []*Module{}
722 directDylibDeps := []*Module{}
723 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700724 directSharedLibDeps := [](cc.LinkableInterface){}
725 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400726 directSrcProvidersDeps := []*Module{}
727 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700728
729 ctx.VisitDirectDeps(func(dep android.Module) {
730 depName := ctx.OtherModuleName(dep)
731 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400732 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700734
Ivan Lozanoffee3342019-08-27 12:03:00 -0700735 switch depTag {
736 case dylibDepTag:
737 dylib, ok := rustDep.compiler.(libraryInterface)
738 if !ok || !dylib.dylib() {
739 ctx.ModuleErrorf("mod %q not an dylib library", depName)
740 return
741 }
742 directDylibDeps = append(directDylibDeps, rustDep)
743 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
744 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400745
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 rlib, ok := rustDep.compiler.(libraryInterface)
747 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400748 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749 return
750 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400751 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400753 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754 case procMacroDepTag:
755 directProcMacroDeps = append(directProcMacroDeps, rustDep)
756 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400757 case android.SourceDepTag:
758 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
759 // OS/Arch variant is used.
760 var helper string
761 if ctx.Host() {
762 helper = "missing 'host_supported'?"
763 } else {
764 helper = "device module defined?"
765 }
766
767 if dep.Target().Os != ctx.Os() {
768 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
769 return
770 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
771 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
772 return
773 }
774 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775 }
776
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400777 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700778 if depTag != procMacroDepTag {
779 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
780 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
781 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
782 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 }
784
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400786 linkFile := rustDep.outputFile
787 if !linkFile.Valid() {
788 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
789 depName, ctx.ModuleName())
790 return
791 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700793 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
794 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 }
796 }
797
Ivan Lozano89435d12020-07-31 11:01:18 -0400798 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700799 //Handle C dependencies
800 if _, ok := ccDep.(*Module); !ok {
801 if ccDep.Module().Target().Os != ctx.Os() {
802 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
803 return
804 }
805 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
806 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
807 return
808 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700809 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400810 linkObject := ccDep.OutputFile()
811 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500812
Ivan Lozano2093af22020-08-25 12:48:19 -0400813 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
815 }
816
817 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700818 switch {
819 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700820 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400821 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700822 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
823 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
824 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
825 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
826 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400827 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828 directStaticLibDeps = append(directStaticLibDeps, ccDep)
829 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700830 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400832 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700833 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
834 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
835 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
836 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
837 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700838 directSharedLibDeps = append(directSharedLibDeps, ccDep)
839 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
840 exportDep = true
Colin Cross6e511a92020-07-27 21:26:48 -0700841 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400842 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700843 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400844 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845 }
846
847 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700848 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
849 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400850 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400853
854 if srcDep, ok := dep.(android.SourceFileProducer); ok {
855 switch depTag {
856 case android.SourceDepTag:
857 // These are usually genrules which don't have per-target variants.
858 directSrcDeps = append(directSrcDeps, srcDep)
859 }
860 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861 })
862
863 var rlibDepFiles RustLibraries
864 for _, dep := range directRlibDeps {
865 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
866 }
867 var dylibDepFiles RustLibraries
868 for _, dep := range directDylibDeps {
869 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
870 }
871 var procMacroDepFiles RustLibraries
872 for _, dep := range directProcMacroDeps {
873 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
874 }
875
876 var staticLibDepFiles android.Paths
877 for _, dep := range directStaticLibDeps {
878 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
879 }
880
881 var sharedLibDepFiles android.Paths
882 for _, dep := range directSharedLibDeps {
883 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
884 }
885
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400886 var srcProviderDepFiles android.Paths
887 for _, dep := range directSrcProvidersDeps {
888 srcs, _ := dep.OutputFiles("")
889 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
890 }
891 for _, dep := range directSrcDeps {
892 srcs := dep.Srcs()
893 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
894 }
895
Ivan Lozanoffee3342019-08-27 12:03:00 -0700896 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
897 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
898 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
899 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
900 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400901 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700902
903 // Dedup exported flags from dependencies
904 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
905 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400906 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
907 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
908 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909
910 return depPaths
911}
912
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800913func (mod *Module) InstallInData() bool {
914 if mod.compiler == nil {
915 return false
916 }
917 return mod.compiler.inData()
918}
919
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920func linkPathFromFilePath(filepath android.Path) string {
921 return strings.Split(filepath.String(), filepath.Base())[0]
922}
Ivan Lozanod648c432020-02-06 12:05:10 -0500923
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
925 ctx := &depsContext{
926 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700927 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700928
929 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700930 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700932 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800933 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700934 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400935
Ivan Lozano2b081132020-09-08 12:46:52 -0400936 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400937 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400938 stdLinkage = "rlib-std"
939 }
940
941 rlibDepVariations := commonDepVariations
942 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
943 rlibDepVariations = append(rlibDepVariations,
944 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
945 }
946
Ivan Lozano52767be2019-10-18 14:49:46 -0700947 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400948 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200949 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700950 rlibDepTag, deps.Rlibs...)
951 actx.AddVariationDependencies(
952 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200953 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700954 dylibDepTag, deps.Dylibs...)
955
Ivan Lozano042504f2020-08-18 14:31:23 -0400956 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
957 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400958 if autoDep.depTag == rlibDepTag {
959 actx.AddVariationDependencies(
960 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
961 autoDep.depTag, deps.Rustlibs...)
962 } else {
963 actx.AddVariationDependencies(
964 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
965 autoDep.depTag, deps.Rustlibs...)
966 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700967 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400968 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400969 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400970 actx.AddVariationDependencies(
971 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
972 rlibDepTag, deps.Stdlibs...)
973 } else {
974 actx.AddVariationDependencies(
975 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
976 dylibDepTag, deps.Stdlibs...)
977 }
978 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700979 actx.AddVariationDependencies(append(commonDepVariations,
980 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700981 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700982 actx.AddVariationDependencies(append(commonDepVariations,
983 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700984 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700985
Colin Cross565cafd2020-09-25 18:47:38 -0700986 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700987 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -0700988 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700989 }
990 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -0700991 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700992 }
993
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400994 if mod.sourceProvider != nil {
995 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
996 bindgen.Properties.Custom_bindgen != "" {
997 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
998 bindgen.Properties.Custom_bindgen)
999 }
1000 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001001 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001002 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001003}
1004
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001005func BeginMutator(ctx android.BottomUpMutatorContext) {
1006 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1007 mod.beginMutator(ctx)
1008 }
1009}
1010
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001011func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1012 ctx := &baseModuleContext{
1013 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001014 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001015
1016 mod.begin(ctx)
1017}
1018
Ivan Lozanoffee3342019-08-27 12:03:00 -07001019func (mod *Module) Name() string {
1020 name := mod.ModuleBase.Name()
1021 if p, ok := mod.compiler.(interface {
1022 Name(string) string
1023 }); ok {
1024 name = p.Name(name)
1025 }
1026 return name
1027}
1028
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001029func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001030 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001031 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001032 }
1033}
1034
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001035var _ android.HostToolProvider = (*Module)(nil)
1036
1037func (mod *Module) HostToolPath() android.OptionalPath {
1038 if !mod.Host() {
1039 return android.OptionalPath{}
1040 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001041 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1042 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001043 }
1044 return android.OptionalPath{}
1045}
1046
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047var Bool = proptools.Bool
1048var BoolDefault = proptools.BoolDefault
1049var String = proptools.String
1050var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001051
1052var _ android.OutputFileProducer = (*Module)(nil)