blob: c80ede2da9ed8d1f5874c80bfbb73d07ac411b40 [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 {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700443 if !mod.compiler.nativeCoverage() {
444 return android.Paths{}
445 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400446 if library, ok := mod.compiler.(*libraryDecorator); ok {
447 if library.coverageFile != nil {
448 return android.Paths{library.coverageFile}
449 }
450 return android.Paths{}
451 }
452 }
453 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
454}
455
Ivan Lozano183a3212019-10-18 14:18:45 -0700456var _ cc.LinkableInterface = (*Module)(nil)
457
Ivan Lozanoffee3342019-08-27 12:03:00 -0700458func (mod *Module) Init() android.Module {
459 mod.AddProperties(&mod.Properties)
460
461 if mod.compiler != nil {
462 mod.AddProperties(mod.compiler.compilerProps()...)
463 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400464 if mod.coverage != nil {
465 mod.AddProperties(mod.coverage.props()...)
466 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200467 if mod.clippy != nil {
468 mod.AddProperties(mod.clippy.props()...)
469 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400470
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
472
473 android.InitDefaultableModule(mod)
474
Ivan Lozanode252912019-09-06 15:29:52 -0700475 // Explicitly disable unsupported targets.
476 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
477 disableTargets := struct {
478 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700479 Linux_bionic struct {
480 Enabled *bool
481 }
482 }
483 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700484 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
485
486 ctx.AppendProperties(&disableTargets)
487 })
488
Ivan Lozanoffee3342019-08-27 12:03:00 -0700489 return mod
490}
491
492func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
493 return &Module{
494 hod: hod,
495 multilib: multilib,
496 }
497}
498func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
499 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400500 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200501 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700502 return module
503}
504
505type ModuleContext interface {
506 android.ModuleContext
507 ModuleContextIntf
508}
509
510type BaseModuleContext interface {
511 android.BaseModuleContext
512 ModuleContextIntf
513}
514
515type DepsContext interface {
516 android.BottomUpMutatorContext
517 ModuleContextIntf
518}
519
520type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200521 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700522 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700523}
524
525type depsContext struct {
526 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700527}
528
529type moduleContext struct {
530 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531}
532
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200533type baseModuleContext struct {
534 android.BaseModuleContext
535}
536
537func (ctx *moduleContext) RustModule() *Module {
538 return ctx.Module().(*Module)
539}
540
541func (ctx *moduleContext) toolchain() config.Toolchain {
542 return ctx.RustModule().toolchain(ctx)
543}
544
545func (ctx *depsContext) RustModule() *Module {
546 return ctx.Module().(*Module)
547}
548
549func (ctx *depsContext) toolchain() config.Toolchain {
550 return ctx.RustModule().toolchain(ctx)
551}
552
553func (ctx *baseModuleContext) RustModule() *Module {
554 return ctx.Module().(*Module)
555}
556
557func (ctx *baseModuleContext) toolchain() config.Toolchain {
558 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400559}
560
561func (mod *Module) nativeCoverage() bool {
562 return mod.compiler != nil && mod.compiler.nativeCoverage()
563}
564
Ivan Lozanoffee3342019-08-27 12:03:00 -0700565func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
566 if mod.cachedToolchain == nil {
567 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
568 }
569 return mod.cachedToolchain
570}
571
572func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
573}
574
575func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
576 ctx := &moduleContext{
577 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579
580 toolchain := mod.toolchain(ctx)
581
582 if !toolchain.Supported() {
583 // This toolchain's unsupported, there's nothing to do for this mod.
584 return
585 }
586
587 deps := mod.depsToPaths(ctx)
588 flags := Flags{
589 Toolchain: toolchain,
590 }
591
592 if mod.compiler != nil {
593 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400594 }
595 if mod.coverage != nil {
596 flags, deps = mod.coverage.flags(ctx, flags, deps)
597 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200598 if mod.clippy != nil {
599 flags, deps = mod.clippy.flags(ctx, flags, deps)
600 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400601
602 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700603 outputFile := mod.compiler.compile(ctx, flags, deps)
604 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400605 if !mod.Properties.PreventInstall {
606 mod.compiler.install(ctx, mod.outputFile.Path())
607 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700608 }
609}
610
611func (mod *Module) deps(ctx DepsContext) Deps {
612 deps := Deps{}
613
614 if mod.compiler != nil {
615 deps = mod.compiler.compilerDeps(ctx, deps)
616 }
617
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400618 if mod.coverage != nil {
619 deps = mod.coverage.deps(ctx, deps)
620 }
621
Ivan Lozanoffee3342019-08-27 12:03:00 -0700622 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
623 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700624 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700625 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
626 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
627 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
628
629 return deps
630
631}
632
Ivan Lozanoffee3342019-08-27 12:03:00 -0700633type dependencyTag struct {
634 blueprint.BaseDependencyTag
635 name string
636 library bool
637 proc_macro bool
638}
639
640var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700641 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
642 dylibDepTag = dependencyTag{name: "dylib", library: true}
643 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
644 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645)
646
Matthew Maurer0f003b12020-06-29 14:34:06 -0700647type autoDep struct {
648 variation string
649 depTag dependencyTag
650}
651
652var (
653 rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
654 dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
655)
656
657type autoDeppable interface {
658 autoDep() autoDep
659}
660
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400661func (mod *Module) begin(ctx BaseModuleContext) {
662 if mod.coverage != nil {
663 mod.coverage.begin(ctx)
664 }
665}
666
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
668 var depPaths PathDeps
669
670 directRlibDeps := []*Module{}
671 directDylibDeps := []*Module{}
672 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700673 directSharedLibDeps := [](cc.LinkableInterface){}
674 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675
676 ctx.VisitDirectDeps(func(dep android.Module) {
677 depName := ctx.OtherModuleName(dep)
678 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700679 if rustDep, ok := dep.(*Module); ok {
680 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700681
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682 linkFile := rustDep.outputFile
683 if !linkFile.Valid() {
684 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
685 }
686
687 switch depTag {
688 case dylibDepTag:
689 dylib, ok := rustDep.compiler.(libraryInterface)
690 if !ok || !dylib.dylib() {
691 ctx.ModuleErrorf("mod %q not an dylib library", depName)
692 return
693 }
694 directDylibDeps = append(directDylibDeps, rustDep)
695 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
696 case rlibDepTag:
697 rlib, ok := rustDep.compiler.(libraryInterface)
698 if !ok || !rlib.rlib() {
699 ctx.ModuleErrorf("mod %q not an rlib library", depName)
700 return
701 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400702 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700703 directRlibDeps = append(directRlibDeps, rustDep)
704 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
705 case procMacroDepTag:
706 directProcMacroDeps = append(directProcMacroDeps, rustDep)
707 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
708 }
709
710 //Append the dependencies exportedDirs
711 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
712 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
713 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700714 }
715
716 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
717 // This can be probably be refactored by defining a common exporter interface similar to cc's
718 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
719 linkDir := linkPathFromFilePath(linkFile.Path())
720 if lib, ok := mod.compiler.(*libraryDecorator); ok {
721 lib.linkDirs = append(lib.linkDirs, linkDir)
722 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
723 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
724 }
725 }
726
Ivan Lozano52767be2019-10-18 14:49:46 -0700727 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700728
Ivan Lozano52767be2019-10-18 14:49:46 -0700729 if ccDep, ok := dep.(cc.LinkableInterface); ok {
730 //Handle C dependencies
731 if _, ok := ccDep.(*Module); !ok {
732 if ccDep.Module().Target().Os != ctx.Os() {
733 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
734 return
735 }
736 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
737 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
738 return
739 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700740 }
741
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742 linkFile := ccDep.OutputFile()
743 linkPath := linkPathFromFilePath(linkFile.Path())
744 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500745 depFlag := "-l" + libName
746
Ivan Lozanoffee3342019-08-27 12:03:00 -0700747 if !linkFile.Valid() {
748 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
749 }
750
751 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700753 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500754 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500756 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400757 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758 directStaticLibDeps = append(directStaticLibDeps, ccDep)
759 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700760 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500761 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500763 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764 directSharedLibDeps = append(directSharedLibDeps, ccDep)
765 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
766 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700767 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700768 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700769 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700770 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700771 }
772
773 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700774 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500776 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
778 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500779 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700780 }
781
782 }
783 })
784
785 var rlibDepFiles RustLibraries
786 for _, dep := range directRlibDeps {
787 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
788 }
789 var dylibDepFiles RustLibraries
790 for _, dep := range directDylibDeps {
791 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
792 }
793 var procMacroDepFiles RustLibraries
794 for _, dep := range directProcMacroDeps {
795 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
796 }
797
798 var staticLibDepFiles android.Paths
799 for _, dep := range directStaticLibDeps {
800 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
801 }
802
803 var sharedLibDepFiles android.Paths
804 for _, dep := range directSharedLibDeps {
805 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
806 }
807
808 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
809 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
810 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
811 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
812 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
813
814 // Dedup exported flags from dependencies
815 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
816 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
817
818 return depPaths
819}
820
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800821func (mod *Module) InstallInData() bool {
822 if mod.compiler == nil {
823 return false
824 }
825 return mod.compiler.inData()
826}
827
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828func linkPathFromFilePath(filepath android.Path) string {
829 return strings.Split(filepath.String(), filepath.Base())[0]
830}
Ivan Lozanod648c432020-02-06 12:05:10 -0500831
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500833 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700834 if strings.HasPrefix(libName, "lib") {
835 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836 }
837 return libName
838}
Ivan Lozanod648c432020-02-06 12:05:10 -0500839
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
841 ctx := &depsContext{
842 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700843 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844
845 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700846 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900847 if cc.VersionVariantAvailable(mod) {
848 commonDepVariations = append(commonDepVariations,
849 blueprint.Variation{Mutator: "version", Variation: ""})
850 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700852 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800853 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700855 actx.AddVariationDependencies(
856 append(commonDepVariations, []blueprint.Variation{
857 {Mutator: "rust_libraries", Variation: "rlib"},
858 {Mutator: "link", Variation: ""}}...),
859 rlibDepTag, deps.Rlibs...)
860 actx.AddVariationDependencies(
861 append(commonDepVariations, []blueprint.Variation{
862 {Mutator: "rust_libraries", Variation: "dylib"},
863 {Mutator: "link", Variation: ""}}...),
864 dylibDepTag, deps.Dylibs...)
865
Matthew Maurer0f003b12020-06-29 14:34:06 -0700866 if deps.Rustlibs != nil {
867 autoDep := mod.compiler.(autoDeppable).autoDep()
868 actx.AddVariationDependencies(
869 append(commonDepVariations, []blueprint.Variation{
870 {Mutator: "rust_libraries", Variation: autoDep.variation},
871 {Mutator: "link", Variation: ""}}...),
872 autoDep.depTag, deps.Rustlibs...)
873 }
874
Ivan Lozano52767be2019-10-18 14:49:46 -0700875 actx.AddVariationDependencies(append(commonDepVariations,
876 blueprint.Variation{Mutator: "link", Variation: "shared"}),
877 cc.SharedDepTag, deps.SharedLibs...)
878 actx.AddVariationDependencies(append(commonDepVariations,
879 blueprint.Variation{Mutator: "link", Variation: "static"}),
880 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700881
Ivan Lozanof1c84332019-09-20 11:00:37 -0700882 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700883 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700884 }
885 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700886 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700887 }
888
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700889 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700890 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891}
892
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400893func BeginMutator(ctx android.BottomUpMutatorContext) {
894 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
895 mod.beginMutator(ctx)
896 }
897}
898
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400899func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
900 ctx := &baseModuleContext{
901 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400902 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400903
904 mod.begin(ctx)
905}
906
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907func (mod *Module) Name() string {
908 name := mod.ModuleBase.Name()
909 if p, ok := mod.compiler.(interface {
910 Name(string) string
911 }); ok {
912 name = p.Name(name)
913 }
914 return name
915}
916
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -0700917var _ android.HostToolProvider = (*Module)(nil)
918
919func (mod *Module) HostToolPath() android.OptionalPath {
920 if !mod.Host() {
921 return android.OptionalPath{}
922 }
923 if _, ok := mod.compiler.(*binaryDecorator); ok {
924 return mod.outputFile
925 }
926 return android.OptionalPath{}
927}
928
Ivan Lozanoffee3342019-08-27 12:03:00 -0700929var Bool = proptools.Bool
930var BoolDefault = proptools.BoolDefault
931var String = proptools.String
932var StringPtr = proptools.StringPtr