blob: df323e3689e95a9ed3eee1184981d7ebaec3bc1e [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"
26 "android/soong/rust/config"
27)
28
29var pctx = android.NewPackageContext("android/soong/rust")
30
31func init() {
32 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070033
34 android.AddNeverAllowRules(
35 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070036 NotIn(config.RustAllowedPaths...).
37 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070038
39 android.RegisterModuleType("rust_defaults", defaultsFactory)
40 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
41 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040042 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070043 })
44 pctx.Import("android/soong/rust/config")
45}
46
47type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040048 GlobalRustFlags []string // Flags that apply globally to rust
49 GlobalLinkFlags []string // Flags that apply globally to linker
50 RustFlags []string // Flags that apply to rust
51 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020052 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070053 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040054 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070056}
57
58type BaseProperties struct {
59 AndroidMkRlibs []string
60 AndroidMkDylibs []string
61 AndroidMkProcMacroLibs []string
62 AndroidMkSharedLibs []string
63 AndroidMkStaticLibs []string
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070064 SubName string `blueprint:"mutated"`
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040065 PreventInstall bool
66 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070067}
68
69type Module struct {
70 android.ModuleBase
71 android.DefaultableModuleBase
72
73 Properties BaseProperties
74
75 hod android.HostOrDeviceSupported
76 multilib android.Multilib
77
78 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040079 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020080 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070081 cachedToolchain config.Toolchain
82 subAndroidMkOnce map[subAndroidMkProvider]bool
83 outputFile android.OptionalPath
84}
85
Colin Cross7228ecd2019-11-18 16:00:16 -080086var _ android.ImageInterface = (*Module)(nil)
87
88func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
89
90func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
91 return true
92}
93
Yifan Hong1b3348d2020-01-21 15:53:22 -080094func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
95 return mod.InRamdisk()
96}
97
Colin Cross7228ecd2019-11-18 16:00:16 -080098func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
99 return mod.InRecovery()
100}
101
102func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
103 return nil
104}
105
106func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
107}
108
Ivan Lozano52767be2019-10-18 14:49:46 -0700109func (mod *Module) BuildStubs() bool {
110 return false
111}
112
113func (mod *Module) HasStubsVariants() bool {
114 return false
115}
116
117func (mod *Module) SelectedStl() string {
118 return ""
119}
120
Ivan Lozano2b262972019-11-21 12:30:50 -0800121func (mod *Module) NonCcVariants() bool {
122 if mod.compiler != nil {
123 if library, ok := mod.compiler.(libraryInterface); ok {
124 if library.buildRlib() || library.buildDylib() {
125 return true
126 } else {
127 return false
128 }
129 }
130 }
131 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
132}
133
Ivan Lozano52767be2019-10-18 14:49:46 -0700134func (mod *Module) ApiLevel() string {
135 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
136}
137
138func (mod *Module) Static() bool {
139 if mod.compiler != nil {
140 if library, ok := mod.compiler.(libraryInterface); ok {
141 return library.static()
142 }
143 }
144 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
145}
146
147func (mod *Module) Shared() bool {
148 if mod.compiler != nil {
149 if library, ok := mod.compiler.(libraryInterface); ok {
150 return library.static()
151 }
152 }
153 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
154}
155
156func (mod *Module) Toc() android.OptionalPath {
157 if mod.compiler != nil {
158 if _, ok := mod.compiler.(libraryInterface); ok {
159 return android.OptionalPath{}
160 }
161 }
162 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
163}
164
Yifan Hong1b3348d2020-01-21 15:53:22 -0800165func (mod *Module) OnlyInRamdisk() bool {
166 return false
167}
168
Ivan Lozano52767be2019-10-18 14:49:46 -0700169func (mod *Module) OnlyInRecovery() bool {
170 return false
171}
172
Colin Crossc511bc52020-04-07 16:50:32 +0000173func (mod *Module) UseSdk() bool {
174 return false
175}
176
Ivan Lozano52767be2019-10-18 14:49:46 -0700177func (mod *Module) UseVndk() bool {
178 return false
179}
180
181func (mod *Module) MustUseVendorVariant() bool {
182 return false
183}
184
185func (mod *Module) IsVndk() bool {
186 return false
187}
188
189func (mod *Module) HasVendorVariant() bool {
190 return false
191}
192
193func (mod *Module) SdkVersion() string {
194 return ""
195}
196
Colin Crossc511bc52020-04-07 16:50:32 +0000197func (mod *Module) AlwaysSdk() bool {
198 return false
199}
200
Jiyong Park2286afd2020-06-16 21:58:53 +0900201func (mod *Module) IsSdkVariant() bool {
202 return false
203}
204
Ivan Lozano52767be2019-10-18 14:49:46 -0700205func (mod *Module) ToolchainLibrary() bool {
206 return false
207}
208
209func (mod *Module) NdkPrebuiltStl() bool {
210 return false
211}
212
213func (mod *Module) StubDecorator() bool {
214 return false
215}
216
Ivan Lozanoffee3342019-08-27 12:03:00 -0700217type Deps struct {
218 Dylibs []string
219 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700220 Rustlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221 ProcMacros []string
222 SharedLibs []string
223 StaticLibs []string
224
225 CrtBegin, CrtEnd string
226}
227
228type PathDeps struct {
229 DyLibs RustLibraries
230 RLibs RustLibraries
231 SharedLibs android.Paths
232 StaticLibs android.Paths
233 ProcMacros RustLibraries
234 linkDirs []string
235 depFlags []string
236 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700237
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400238 coverageFiles android.Paths
239
Ivan Lozanof1c84332019-09-20 11:00:37 -0700240 CrtBegin android.OptionalPath
241 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700242}
243
244type RustLibraries []RustLibrary
245
246type RustLibrary struct {
247 Path android.Path
248 CrateName string
249}
250
251type compiler interface {
252 compilerFlags(ctx ModuleContext, flags Flags) Flags
253 compilerProps() []interface{}
254 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
255 compilerDeps(ctx DepsContext, deps Deps) Deps
256 crateName() string
257
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800258 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700259 install(ctx ModuleContext, path android.Path)
260 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400261
262 nativeCoverage() bool
263}
264
265func (mod *Module) isCoverageVariant() bool {
266 return mod.coverage.Properties.IsCoverageVariant
267}
268
269var _ cc.Coverage = (*Module)(nil)
270
271func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
272 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
273}
274
275func (mod *Module) PreventInstall() {
276 mod.Properties.PreventInstall = true
277}
278
279func (mod *Module) HideFromMake() {
280 mod.Properties.HideFromMake = true
281}
282
283func (mod *Module) MarkAsCoverageVariant(coverage bool) {
284 mod.coverage.Properties.IsCoverageVariant = coverage
285}
286
287func (mod *Module) EnableCoverageIfNeeded() {
288 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700289}
290
291func defaultsFactory() android.Module {
292 return DefaultsFactory()
293}
294
295type Defaults struct {
296 android.ModuleBase
297 android.DefaultsModuleBase
298}
299
300func DefaultsFactory(props ...interface{}) android.Module {
301 module := &Defaults{}
302
303 module.AddProperties(props...)
304 module.AddProperties(
305 &BaseProperties{},
306 &BaseCompilerProperties{},
307 &BinaryCompilerProperties{},
308 &LibraryCompilerProperties{},
309 &ProcMacroCompilerProperties{},
310 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700311 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400312 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200313 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700314 )
315
316 android.InitDefaultsModule(module)
317 return module
318}
319
320func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700321 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700322}
323
Ivan Lozano183a3212019-10-18 14:18:45 -0700324func (mod *Module) CcLibrary() bool {
325 if mod.compiler != nil {
326 if _, ok := mod.compiler.(*libraryDecorator); ok {
327 return true
328 }
329 }
330 return false
331}
332
333func (mod *Module) CcLibraryInterface() bool {
334 if mod.compiler != nil {
335 if _, ok := mod.compiler.(libraryInterface); ok {
336 return true
337 }
338 }
339 return false
340}
341
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800342func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700343 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700344 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800345 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700346 }
347 }
348 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
349}
350
351func (mod *Module) SetStatic() {
352 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700353 if library, ok := mod.compiler.(libraryInterface); ok {
354 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700355 return
356 }
357 }
358 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
359}
360
361func (mod *Module) SetShared() {
362 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700363 if library, ok := mod.compiler.(libraryInterface); ok {
364 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700365 return
366 }
367 }
368 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
369}
370
371func (mod *Module) SetBuildStubs() {
372 panic("SetBuildStubs not yet implemented for rust modules")
373}
374
375func (mod *Module) SetStubsVersions(string) {
376 panic("SetStubsVersions not yet implemented for rust modules")
377}
378
Jooyung Han03b51852020-02-26 22:45:42 +0900379func (mod *Module) StubsVersion() string {
380 panic("SetStubsVersions not yet implemented for rust modules")
381}
382
Ivan Lozano183a3212019-10-18 14:18:45 -0700383func (mod *Module) BuildStaticVariant() bool {
384 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700385 if library, ok := mod.compiler.(libraryInterface); ok {
386 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700387 }
388 }
389 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
390}
391
392func (mod *Module) BuildSharedVariant() bool {
393 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700394 if library, ok := mod.compiler.(libraryInterface); ok {
395 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700396 }
397 }
398 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
399}
400
401// Rust module deps don't have a link order (?)
402func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
403
404func (mod *Module) GetDepsInLinkOrder() []android.Path {
405 return []android.Path{}
406}
407
408func (mod *Module) GetStaticVariant() cc.LinkableInterface {
409 return nil
410}
411
412func (mod *Module) Module() android.Module {
413 return mod
414}
415
416func (mod *Module) StubsVersions() []string {
417 // For now, Rust has no stubs versions.
418 if mod.compiler != nil {
419 if _, ok := mod.compiler.(*libraryDecorator); ok {
420 return []string{}
421 }
422 }
423 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
424}
425
426func (mod *Module) OutputFile() android.OptionalPath {
427 return mod.outputFile
428}
429
430func (mod *Module) InRecovery() bool {
431 // For now, Rust has no notion of the recovery image
432 return false
433}
434func (mod *Module) HasStaticVariant() bool {
435 if mod.GetStaticVariant() != nil {
436 return true
437 }
438 return false
439}
440
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400441func (mod *Module) CoverageFiles() android.Paths {
442 if mod.compiler != nil {
443 if library, ok := mod.compiler.(*libraryDecorator); ok {
444 if library.coverageFile != nil {
445 return android.Paths{library.coverageFile}
446 }
447 return android.Paths{}
448 }
449 }
450 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
451}
452
Ivan Lozano183a3212019-10-18 14:18:45 -0700453var _ cc.LinkableInterface = (*Module)(nil)
454
Ivan Lozanoffee3342019-08-27 12:03:00 -0700455func (mod *Module) Init() android.Module {
456 mod.AddProperties(&mod.Properties)
457
458 if mod.compiler != nil {
459 mod.AddProperties(mod.compiler.compilerProps()...)
460 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400461 if mod.coverage != nil {
462 mod.AddProperties(mod.coverage.props()...)
463 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200464 if mod.clippy != nil {
465 mod.AddProperties(mod.clippy.props()...)
466 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400467
Ivan Lozanoffee3342019-08-27 12:03:00 -0700468 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
469
470 android.InitDefaultableModule(mod)
471
Ivan Lozanode252912019-09-06 15:29:52 -0700472 // Explicitly disable unsupported targets.
473 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
474 disableTargets := struct {
475 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700476 Linux_bionic struct {
477 Enabled *bool
478 }
479 }
480 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700481 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
482
483 ctx.AppendProperties(&disableTargets)
484 })
485
Ivan Lozanoffee3342019-08-27 12:03:00 -0700486 return mod
487}
488
489func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
490 return &Module{
491 hod: hod,
492 multilib: multilib,
493 }
494}
495func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
496 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400497 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200498 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700499 return module
500}
501
502type ModuleContext interface {
503 android.ModuleContext
504 ModuleContextIntf
505}
506
507type BaseModuleContext interface {
508 android.BaseModuleContext
509 ModuleContextIntf
510}
511
512type DepsContext interface {
513 android.BottomUpMutatorContext
514 ModuleContextIntf
515}
516
517type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200518 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700519 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700520}
521
522type depsContext struct {
523 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700524}
525
526type moduleContext struct {
527 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700528}
529
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200530type baseModuleContext struct {
531 android.BaseModuleContext
532}
533
534func (ctx *moduleContext) RustModule() *Module {
535 return ctx.Module().(*Module)
536}
537
538func (ctx *moduleContext) toolchain() config.Toolchain {
539 return ctx.RustModule().toolchain(ctx)
540}
541
542func (ctx *depsContext) RustModule() *Module {
543 return ctx.Module().(*Module)
544}
545
546func (ctx *depsContext) toolchain() config.Toolchain {
547 return ctx.RustModule().toolchain(ctx)
548}
549
550func (ctx *baseModuleContext) RustModule() *Module {
551 return ctx.Module().(*Module)
552}
553
554func (ctx *baseModuleContext) toolchain() config.Toolchain {
555 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400556}
557
558func (mod *Module) nativeCoverage() bool {
559 return mod.compiler != nil && mod.compiler.nativeCoverage()
560}
561
Ivan Lozanoffee3342019-08-27 12:03:00 -0700562func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
563 if mod.cachedToolchain == nil {
564 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
565 }
566 return mod.cachedToolchain
567}
568
569func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
570}
571
572func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
573 ctx := &moduleContext{
574 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700575 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700576
577 toolchain := mod.toolchain(ctx)
578
579 if !toolchain.Supported() {
580 // This toolchain's unsupported, there's nothing to do for this mod.
581 return
582 }
583
584 deps := mod.depsToPaths(ctx)
585 flags := Flags{
586 Toolchain: toolchain,
587 }
588
589 if mod.compiler != nil {
590 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400591 }
592 if mod.coverage != nil {
593 flags, deps = mod.coverage.flags(ctx, flags, deps)
594 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200595 if mod.clippy != nil {
596 flags, deps = mod.clippy.flags(ctx, flags, deps)
597 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400598
599 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600 outputFile := mod.compiler.compile(ctx, flags, deps)
601 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400602 if !mod.Properties.PreventInstall {
603 mod.compiler.install(ctx, mod.outputFile.Path())
604 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 }
606}
607
608func (mod *Module) deps(ctx DepsContext) Deps {
609 deps := Deps{}
610
611 if mod.compiler != nil {
612 deps = mod.compiler.compilerDeps(ctx, deps)
613 }
614
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400615 if mod.coverage != nil {
616 deps = mod.coverage.deps(ctx, deps)
617 }
618
Ivan Lozanoffee3342019-08-27 12:03:00 -0700619 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
620 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700621 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700622 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
623 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
624 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
625
626 return deps
627
628}
629
Ivan Lozanoffee3342019-08-27 12:03:00 -0700630type dependencyTag struct {
631 blueprint.BaseDependencyTag
632 name string
633 library bool
634 proc_macro bool
635}
636
637var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700638 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
639 dylibDepTag = dependencyTag{name: "dylib", library: true}
640 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
641 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700642)
643
Matthew Maurer0f003b12020-06-29 14:34:06 -0700644type autoDep struct {
645 variation string
646 depTag dependencyTag
647}
648
649var (
650 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
651 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
652)
653
654type autoDeppable interface {
655 autoDep() autoDep
656}
657
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400658func (mod *Module) begin(ctx BaseModuleContext) {
659 if mod.coverage != nil {
660 mod.coverage.begin(ctx)
661 }
662}
663
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
665 var depPaths PathDeps
666
667 directRlibDeps := []*Module{}
668 directDylibDeps := []*Module{}
669 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700670 directSharedLibDeps := [](cc.LinkableInterface){}
671 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700672
673 ctx.VisitDirectDeps(func(dep android.Module) {
674 depName := ctx.OtherModuleName(dep)
675 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700676 if rustDep, ok := dep.(*Module); ok {
677 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700678
Ivan Lozanoffee3342019-08-27 12:03:00 -0700679 linkFile := rustDep.outputFile
680 if !linkFile.Valid() {
681 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
682 }
683
684 switch depTag {
685 case dylibDepTag:
686 dylib, ok := rustDep.compiler.(libraryInterface)
687 if !ok || !dylib.dylib() {
688 ctx.ModuleErrorf("mod %q not an dylib library", depName)
689 return
690 }
691 directDylibDeps = append(directDylibDeps, rustDep)
692 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
693 case rlibDepTag:
694 rlib, ok := rustDep.compiler.(libraryInterface)
695 if !ok || !rlib.rlib() {
696 ctx.ModuleErrorf("mod %q not an rlib library", depName)
697 return
698 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400699 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700 directRlibDeps = append(directRlibDeps, rustDep)
701 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
702 case procMacroDepTag:
703 directProcMacroDeps = append(directProcMacroDeps, rustDep)
704 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
705 }
706
707 //Append the dependencies exportedDirs
708 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
709 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
710 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700711 }
712
713 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
714 // This can be probably be refactored by defining a common exporter interface similar to cc's
715 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
716 linkDir := linkPathFromFilePath(linkFile.Path())
717 if lib, ok := mod.compiler.(*libraryDecorator); ok {
718 lib.linkDirs = append(lib.linkDirs, linkDir)
719 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
720 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
721 }
722 }
723
Ivan Lozano52767be2019-10-18 14:49:46 -0700724 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700725
Ivan Lozano52767be2019-10-18 14:49:46 -0700726 if ccDep, ok := dep.(cc.LinkableInterface); ok {
727 //Handle C dependencies
728 if _, ok := ccDep.(*Module); !ok {
729 if ccDep.Module().Target().Os != ctx.Os() {
730 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
731 return
732 }
733 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
734 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
735 return
736 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700737 }
738
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739 linkFile := ccDep.OutputFile()
740 linkPath := linkPathFromFilePath(linkFile.Path())
741 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500742 depFlag := "-l" + libName
743
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744 if !linkFile.Valid() {
745 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
746 }
747
748 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700750 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500751 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500753 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400754 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 directStaticLibDeps = append(directStaticLibDeps, ccDep)
756 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700757 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500758 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500760 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 directSharedLibDeps = append(directSharedLibDeps, ccDep)
762 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
763 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700764 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700765 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700766 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700767 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768 }
769
770 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700771 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500773 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700774 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
775 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500776 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777 }
778
779 }
780 })
781
782 var rlibDepFiles RustLibraries
783 for _, dep := range directRlibDeps {
784 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
785 }
786 var dylibDepFiles RustLibraries
787 for _, dep := range directDylibDeps {
788 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
789 }
790 var procMacroDepFiles RustLibraries
791 for _, dep := range directProcMacroDeps {
792 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
793 }
794
795 var staticLibDepFiles android.Paths
796 for _, dep := range directStaticLibDeps {
797 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
798 }
799
800 var sharedLibDepFiles android.Paths
801 for _, dep := range directSharedLibDeps {
802 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
803 }
804
805 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
806 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
807 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
808 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
809 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
810
811 // Dedup exported flags from dependencies
812 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
813 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
814
815 return depPaths
816}
817
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800818func (mod *Module) InstallInData() bool {
819 if mod.compiler == nil {
820 return false
821 }
822 return mod.compiler.inData()
823}
824
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825func linkPathFromFilePath(filepath android.Path) string {
826 return strings.Split(filepath.String(), filepath.Base())[0]
827}
Ivan Lozanod648c432020-02-06 12:05:10 -0500828
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500830 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700831 if strings.HasPrefix(libName, "lib") {
832 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 }
834 return libName
835}
Ivan Lozanod648c432020-02-06 12:05:10 -0500836
Ivan Lozanoffee3342019-08-27 12:03:00 -0700837func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
838 ctx := &depsContext{
839 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841
842 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700843 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900844 if cc.VersionVariantAvailable(mod) {
845 commonDepVariations = append(commonDepVariations,
846 blueprint.Variation{Mutator: "version", Variation: ""})
847 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700848 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700849 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800850 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700852 actx.AddVariationDependencies(
853 append(commonDepVariations, []blueprint.Variation{
854 {Mutator: "rust_libraries", Variation: "rlib"},
855 {Mutator: "link", Variation: ""}}...),
856 rlibDepTag, deps.Rlibs...)
857 actx.AddVariationDependencies(
858 append(commonDepVariations, []blueprint.Variation{
859 {Mutator: "rust_libraries", Variation: "dylib"},
860 {Mutator: "link", Variation: ""}}...),
861 dylibDepTag, deps.Dylibs...)
862
Matthew Maurer0f003b12020-06-29 14:34:06 -0700863 if deps.Rustlibs != nil {
864 autoDep := mod.compiler.(autoDeppable).autoDep()
865 actx.AddVariationDependencies(
866 append(commonDepVariations, []blueprint.Variation{
867 {Mutator: "rust_libraries", Variation: autoDep.variation},
868 {Mutator: "link", Variation: ""}}...),
869 autoDep.depTag, deps.Rustlibs...)
870 }
871
Ivan Lozano52767be2019-10-18 14:49:46 -0700872 actx.AddVariationDependencies(append(commonDepVariations,
873 blueprint.Variation{Mutator: "link", Variation: "shared"}),
874 cc.SharedDepTag, deps.SharedLibs...)
875 actx.AddVariationDependencies(append(commonDepVariations,
876 blueprint.Variation{Mutator: "link", Variation: "static"}),
877 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700878
Ivan Lozanof1c84332019-09-20 11:00:37 -0700879 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700880 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700881 }
882 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700883 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700884 }
885
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700886 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700887 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700888}
889
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400890func BeginMutator(ctx android.BottomUpMutatorContext) {
891 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
892 mod.beginMutator(ctx)
893 }
894}
895
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400896func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
897 ctx := &baseModuleContext{
898 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400899 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400900
901 mod.begin(ctx)
902}
903
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904func (mod *Module) Name() string {
905 name := mod.ModuleBase.Name()
906 if p, ok := mod.compiler.(interface {
907 Name(string) string
908 }); ok {
909 name = p.Name(name)
910 }
911 return name
912}
913
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -0700914var _ android.HostToolProvider = (*Module)(nil)
915
916func (mod *Module) HostToolPath() android.OptionalPath {
917 if !mod.Host() {
918 return android.OptionalPath{}
919 }
920 if _, ok := mod.compiler.(*binaryDecorator); ok {
921 return mod.outputFile
922 }
923 return android.OptionalPath{}
924}
925
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926var Bool = proptools.Bool
927var BoolDefault = proptools.BoolDefault
928var String = proptools.String
929var StringPtr = proptools.StringPtr