blob: 7f8287394eed79798a19bd33208fc8fc10f6640e [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
220 ProcMacros []string
221 SharedLibs []string
222 StaticLibs []string
223
224 CrtBegin, CrtEnd string
225}
226
227type PathDeps struct {
228 DyLibs RustLibraries
229 RLibs RustLibraries
230 SharedLibs android.Paths
231 StaticLibs android.Paths
232 ProcMacros RustLibraries
233 linkDirs []string
234 depFlags []string
235 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700236
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400237 coverageFiles android.Paths
238
Ivan Lozanof1c84332019-09-20 11:00:37 -0700239 CrtBegin android.OptionalPath
240 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241}
242
243type RustLibraries []RustLibrary
244
245type RustLibrary struct {
246 Path android.Path
247 CrateName string
248}
249
250type compiler interface {
251 compilerFlags(ctx ModuleContext, flags Flags) Flags
252 compilerProps() []interface{}
253 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
254 compilerDeps(ctx DepsContext, deps Deps) Deps
255 crateName() string
256
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800257 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258 install(ctx ModuleContext, path android.Path)
259 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400260
261 nativeCoverage() bool
262}
263
264func (mod *Module) isCoverageVariant() bool {
265 return mod.coverage.Properties.IsCoverageVariant
266}
267
268var _ cc.Coverage = (*Module)(nil)
269
270func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
271 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
272}
273
274func (mod *Module) PreventInstall() {
275 mod.Properties.PreventInstall = true
276}
277
278func (mod *Module) HideFromMake() {
279 mod.Properties.HideFromMake = true
280}
281
282func (mod *Module) MarkAsCoverageVariant(coverage bool) {
283 mod.coverage.Properties.IsCoverageVariant = coverage
284}
285
286func (mod *Module) EnableCoverageIfNeeded() {
287 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700288}
289
290func defaultsFactory() android.Module {
291 return DefaultsFactory()
292}
293
294type Defaults struct {
295 android.ModuleBase
296 android.DefaultsModuleBase
297}
298
299func DefaultsFactory(props ...interface{}) android.Module {
300 module := &Defaults{}
301
302 module.AddProperties(props...)
303 module.AddProperties(
304 &BaseProperties{},
305 &BaseCompilerProperties{},
306 &BinaryCompilerProperties{},
307 &LibraryCompilerProperties{},
308 &ProcMacroCompilerProperties{},
309 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700310 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400311 &cc.CoverageProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200312 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700313 )
314
315 android.InitDefaultsModule(module)
316 return module
317}
318
319func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700320 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700321}
322
Ivan Lozano183a3212019-10-18 14:18:45 -0700323func (mod *Module) CcLibrary() bool {
324 if mod.compiler != nil {
325 if _, ok := mod.compiler.(*libraryDecorator); ok {
326 return true
327 }
328 }
329 return false
330}
331
332func (mod *Module) CcLibraryInterface() bool {
333 if mod.compiler != nil {
334 if _, ok := mod.compiler.(libraryInterface); ok {
335 return true
336 }
337 }
338 return false
339}
340
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800341func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700342 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700343 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800344 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700345 }
346 }
347 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
348}
349
350func (mod *Module) SetStatic() {
351 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700352 if library, ok := mod.compiler.(libraryInterface); ok {
353 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700354 return
355 }
356 }
357 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
358}
359
360func (mod *Module) SetShared() {
361 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700362 if library, ok := mod.compiler.(libraryInterface); ok {
363 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700364 return
365 }
366 }
367 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
368}
369
370func (mod *Module) SetBuildStubs() {
371 panic("SetBuildStubs not yet implemented for rust modules")
372}
373
374func (mod *Module) SetStubsVersions(string) {
375 panic("SetStubsVersions not yet implemented for rust modules")
376}
377
Jooyung Han03b51852020-02-26 22:45:42 +0900378func (mod *Module) StubsVersion() string {
379 panic("SetStubsVersions not yet implemented for rust modules")
380}
381
Ivan Lozano183a3212019-10-18 14:18:45 -0700382func (mod *Module) BuildStaticVariant() bool {
383 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700384 if library, ok := mod.compiler.(libraryInterface); ok {
385 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700386 }
387 }
388 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
389}
390
391func (mod *Module) BuildSharedVariant() bool {
392 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700393 if library, ok := mod.compiler.(libraryInterface); ok {
394 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700395 }
396 }
397 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
398}
399
400// Rust module deps don't have a link order (?)
401func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
402
403func (mod *Module) GetDepsInLinkOrder() []android.Path {
404 return []android.Path{}
405}
406
407func (mod *Module) GetStaticVariant() cc.LinkableInterface {
408 return nil
409}
410
411func (mod *Module) Module() android.Module {
412 return mod
413}
414
415func (mod *Module) StubsVersions() []string {
416 // For now, Rust has no stubs versions.
417 if mod.compiler != nil {
418 if _, ok := mod.compiler.(*libraryDecorator); ok {
419 return []string{}
420 }
421 }
422 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
423}
424
425func (mod *Module) OutputFile() android.OptionalPath {
426 return mod.outputFile
427}
428
429func (mod *Module) InRecovery() bool {
430 // For now, Rust has no notion of the recovery image
431 return false
432}
433func (mod *Module) HasStaticVariant() bool {
434 if mod.GetStaticVariant() != nil {
435 return true
436 }
437 return false
438}
439
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400440func (mod *Module) CoverageFiles() android.Paths {
441 if mod.compiler != nil {
442 if library, ok := mod.compiler.(*libraryDecorator); ok {
443 if library.coverageFile != nil {
444 return android.Paths{library.coverageFile}
445 }
446 return android.Paths{}
447 }
448 }
449 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
450}
451
Ivan Lozano183a3212019-10-18 14:18:45 -0700452var _ cc.LinkableInterface = (*Module)(nil)
453
Ivan Lozanoffee3342019-08-27 12:03:00 -0700454func (mod *Module) Init() android.Module {
455 mod.AddProperties(&mod.Properties)
456
457 if mod.compiler != nil {
458 mod.AddProperties(mod.compiler.compilerProps()...)
459 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400460 if mod.coverage != nil {
461 mod.AddProperties(mod.coverage.props()...)
462 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200463 if mod.clippy != nil {
464 mod.AddProperties(mod.clippy.props()...)
465 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400466
Ivan Lozanoffee3342019-08-27 12:03:00 -0700467 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
468
469 android.InitDefaultableModule(mod)
470
Ivan Lozanode252912019-09-06 15:29:52 -0700471 // Explicitly disable unsupported targets.
472 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
473 disableTargets := struct {
474 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700475 Linux_bionic struct {
476 Enabled *bool
477 }
478 }
479 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700480 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
481
482 ctx.AppendProperties(&disableTargets)
483 })
484
Ivan Lozanoffee3342019-08-27 12:03:00 -0700485 return mod
486}
487
488func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
489 return &Module{
490 hod: hod,
491 multilib: multilib,
492 }
493}
494func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
495 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400496 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200497 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498 return module
499}
500
501type ModuleContext interface {
502 android.ModuleContext
503 ModuleContextIntf
504}
505
506type BaseModuleContext interface {
507 android.BaseModuleContext
508 ModuleContextIntf
509}
510
511type DepsContext interface {
512 android.BottomUpMutatorContext
513 ModuleContextIntf
514}
515
516type ModuleContextIntf interface {
517 toolchain() config.Toolchain
518 baseModuleName() string
519 CrateName() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400520 nativeCoverage() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700521}
522
523type depsContext struct {
524 android.BottomUpMutatorContext
525 moduleContextImpl
526}
527
528type moduleContext struct {
529 android.ModuleContext
530 moduleContextImpl
531}
532
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533func (ctx *moduleContextImpl) nativeCoverage() bool {
534 return ctx.mod.nativeCoverage()
535}
536
537func (mod *Module) nativeCoverage() bool {
538 return mod.compiler != nil && mod.compiler.nativeCoverage()
539}
540
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541type moduleContextImpl struct {
542 mod *Module
543 ctx BaseModuleContext
544}
545
546func (ctx *moduleContextImpl) toolchain() config.Toolchain {
547 return ctx.mod.toolchain(ctx.ctx)
548}
549
550func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
551 if mod.cachedToolchain == nil {
552 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
553 }
554 return mod.cachedToolchain
555}
556
557func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
558}
559
560func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
561 ctx := &moduleContext{
562 ModuleContext: actx,
563 moduleContextImpl: moduleContextImpl{
564 mod: mod,
565 },
566 }
567 ctx.ctx = ctx
568
569 toolchain := mod.toolchain(ctx)
570
571 if !toolchain.Supported() {
572 // This toolchain's unsupported, there's nothing to do for this mod.
573 return
574 }
575
576 deps := mod.depsToPaths(ctx)
577 flags := Flags{
578 Toolchain: toolchain,
579 }
580
581 if mod.compiler != nil {
582 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583 }
584 if mod.coverage != nil {
585 flags, deps = mod.coverage.flags(ctx, flags, deps)
586 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200587 if mod.clippy != nil {
588 flags, deps = mod.clippy.flags(ctx, flags, deps)
589 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400590
591 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700592 outputFile := mod.compiler.compile(ctx, flags, deps)
593 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400594 if !mod.Properties.PreventInstall {
595 mod.compiler.install(ctx, mod.outputFile.Path())
596 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700597 }
598}
599
600func (mod *Module) deps(ctx DepsContext) Deps {
601 deps := Deps{}
602
603 if mod.compiler != nil {
604 deps = mod.compiler.compilerDeps(ctx, deps)
605 }
606
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400607 if mod.coverage != nil {
608 deps = mod.coverage.deps(ctx, deps)
609 }
610
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
612 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
613 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
614 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
615 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
616
617 return deps
618
619}
620
621func (ctx *moduleContextImpl) baseModuleName() string {
622 return ctx.mod.ModuleBase.BaseModuleName()
623}
624
625func (ctx *moduleContextImpl) CrateName() string {
626 return ctx.mod.CrateName()
627}
628
629type dependencyTag struct {
630 blueprint.BaseDependencyTag
631 name string
632 library bool
633 proc_macro bool
634}
635
636var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700637 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
638 dylibDepTag = dependencyTag{name: "dylib", library: true}
639 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
640 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700641)
642
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400643func (mod *Module) begin(ctx BaseModuleContext) {
644 if mod.coverage != nil {
645 mod.coverage.begin(ctx)
646 }
647}
648
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
650 var depPaths PathDeps
651
652 directRlibDeps := []*Module{}
653 directDylibDeps := []*Module{}
654 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700655 directSharedLibDeps := [](cc.LinkableInterface){}
656 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657
658 ctx.VisitDirectDeps(func(dep android.Module) {
659 depName := ctx.OtherModuleName(dep)
660 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700661 if rustDep, ok := dep.(*Module); ok {
662 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700663
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664 linkFile := rustDep.outputFile
665 if !linkFile.Valid() {
666 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
667 }
668
669 switch depTag {
670 case dylibDepTag:
671 dylib, ok := rustDep.compiler.(libraryInterface)
672 if !ok || !dylib.dylib() {
673 ctx.ModuleErrorf("mod %q not an dylib library", depName)
674 return
675 }
676 directDylibDeps = append(directDylibDeps, rustDep)
677 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
678 case rlibDepTag:
679 rlib, ok := rustDep.compiler.(libraryInterface)
680 if !ok || !rlib.rlib() {
681 ctx.ModuleErrorf("mod %q not an rlib library", depName)
682 return
683 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400684 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700685 directRlibDeps = append(directRlibDeps, rustDep)
686 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
687 case procMacroDepTag:
688 directProcMacroDeps = append(directProcMacroDeps, rustDep)
689 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
690 }
691
692 //Append the dependencies exportedDirs
693 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
694 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
695 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700696 }
697
698 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
699 // This can be probably be refactored by defining a common exporter interface similar to cc's
700 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
701 linkDir := linkPathFromFilePath(linkFile.Path())
702 if lib, ok := mod.compiler.(*libraryDecorator); ok {
703 lib.linkDirs = append(lib.linkDirs, linkDir)
704 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
705 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
706 }
707 }
708
Ivan Lozano52767be2019-10-18 14:49:46 -0700709 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700710
Ivan Lozano52767be2019-10-18 14:49:46 -0700711 if ccDep, ok := dep.(cc.LinkableInterface); ok {
712 //Handle C dependencies
713 if _, ok := ccDep.(*Module); !ok {
714 if ccDep.Module().Target().Os != ctx.Os() {
715 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
716 return
717 }
718 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
719 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
720 return
721 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700722 }
723
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724 linkFile := ccDep.OutputFile()
725 linkPath := linkPathFromFilePath(linkFile.Path())
726 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500727 depFlag := "-l" + libName
728
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729 if !linkFile.Valid() {
730 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
731 }
732
733 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700734 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700735 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500736 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700737 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500738 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400739 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740 directStaticLibDeps = append(directStaticLibDeps, ccDep)
741 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700742 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500743 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500745 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 directSharedLibDeps = append(directSharedLibDeps, ccDep)
747 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
748 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700749 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700750 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700751 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700752 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753 }
754
755 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700756 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500758 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
760 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500761 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 }
763
764 }
765 })
766
767 var rlibDepFiles RustLibraries
768 for _, dep := range directRlibDeps {
769 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
770 }
771 var dylibDepFiles RustLibraries
772 for _, dep := range directDylibDeps {
773 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
774 }
775 var procMacroDepFiles RustLibraries
776 for _, dep := range directProcMacroDeps {
777 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
778 }
779
780 var staticLibDepFiles android.Paths
781 for _, dep := range directStaticLibDeps {
782 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
783 }
784
785 var sharedLibDepFiles android.Paths
786 for _, dep := range directSharedLibDeps {
787 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
788 }
789
790 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
791 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
792 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
793 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
794 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
795
796 // Dedup exported flags from dependencies
797 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
798 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
799
800 return depPaths
801}
802
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800803func (mod *Module) InstallInData() bool {
804 if mod.compiler == nil {
805 return false
806 }
807 return mod.compiler.inData()
808}
809
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810func linkPathFromFilePath(filepath android.Path) string {
811 return strings.Split(filepath.String(), filepath.Base())[0]
812}
Ivan Lozanod648c432020-02-06 12:05:10 -0500813
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500815 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700816 if strings.HasPrefix(libName, "lib") {
817 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 }
819 return libName
820}
Ivan Lozanod648c432020-02-06 12:05:10 -0500821
Ivan Lozanoffee3342019-08-27 12:03:00 -0700822func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
823 ctx := &depsContext{
824 BottomUpMutatorContext: actx,
825 moduleContextImpl: moduleContextImpl{
826 mod: mod,
827 },
828 }
829 ctx.ctx = ctx
830
831 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700832 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900833 if cc.VersionVariantAvailable(mod) {
834 commonDepVariations = append(commonDepVariations,
835 blueprint.Variation{Mutator: "version", Variation: ""})
836 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700837 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700838 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800839 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700841 actx.AddVariationDependencies(
842 append(commonDepVariations, []blueprint.Variation{
843 {Mutator: "rust_libraries", Variation: "rlib"},
844 {Mutator: "link", Variation: ""}}...),
845 rlibDepTag, deps.Rlibs...)
846 actx.AddVariationDependencies(
847 append(commonDepVariations, []blueprint.Variation{
848 {Mutator: "rust_libraries", Variation: "dylib"},
849 {Mutator: "link", Variation: ""}}...),
850 dylibDepTag, deps.Dylibs...)
851
852 actx.AddVariationDependencies(append(commonDepVariations,
853 blueprint.Variation{Mutator: "link", Variation: "shared"}),
854 cc.SharedDepTag, deps.SharedLibs...)
855 actx.AddVariationDependencies(append(commonDepVariations,
856 blueprint.Variation{Mutator: "link", Variation: "static"}),
857 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700858
Ivan Lozanof1c84332019-09-20 11:00:37 -0700859 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700860 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700861 }
862 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700863 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700864 }
865
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700866 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700867 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700868}
869
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400870func BeginMutator(ctx android.BottomUpMutatorContext) {
871 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
872 mod.beginMutator(ctx)
873 }
874}
875
876type baseModuleContext struct {
877 android.BaseModuleContext
878 moduleContextImpl
879}
880
881func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
882 ctx := &baseModuleContext{
883 BaseModuleContext: actx,
884 moduleContextImpl: moduleContextImpl{
885 mod: mod,
886 },
887 }
888 ctx.ctx = ctx
889
890 mod.begin(ctx)
891}
892
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893func (mod *Module) Name() string {
894 name := mod.ModuleBase.Name()
895 if p, ok := mod.compiler.(interface {
896 Name(string) string
897 }); ok {
898 name = p.Name(name)
899 }
900 return name
901}
902
903var Bool = proptools.Bool
904var BoolDefault = proptools.BoolDefault
905var String = proptools.String
906var StringPtr = proptools.StringPtr