blob: 8cf2e6df17da4c72db28f3f90e1fe69a18fe68fa [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()
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070042 ctx.BottomUp("rust_unit_tests", TestPerSrcMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040043 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070044 })
45 pctx.Import("android/soong/rust/config")
46}
47
48type Flags struct {
Ivan Lozanof1c84332019-09-20 11:00:37 -070049 GlobalRustFlags []string // Flags that apply globally to rust
50 GlobalLinkFlags []string // Flags that apply globally to linker
51 RustFlags []string // Flags that apply to rust
52 LinkFlags []string // Flags that apply to linker
53 RustFlagsDeps android.Paths // Files depended on by compiler flags
54 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040055 Coverage 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
Ivan Lozanoffee3342019-08-27 12:03:00 -070080 cachedToolchain config.Toolchain
81 subAndroidMkOnce map[subAndroidMkProvider]bool
82 outputFile android.OptionalPath
83}
84
Colin Cross7228ecd2019-11-18 16:00:16 -080085var _ android.ImageInterface = (*Module)(nil)
86
87func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
88
89func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
90 return true
91}
92
Yifan Hong1b3348d2020-01-21 15:53:22 -080093func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
94 return mod.InRamdisk()
95}
96
Colin Cross7228ecd2019-11-18 16:00:16 -080097func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
98 return mod.InRecovery()
99}
100
101func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
102 return nil
103}
104
105func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
106}
107
Ivan Lozano52767be2019-10-18 14:49:46 -0700108func (mod *Module) BuildStubs() bool {
109 return false
110}
111
112func (mod *Module) HasStubsVariants() bool {
113 return false
114}
115
116func (mod *Module) SelectedStl() string {
117 return ""
118}
119
Ivan Lozano2b262972019-11-21 12:30:50 -0800120func (mod *Module) NonCcVariants() bool {
121 if mod.compiler != nil {
122 if library, ok := mod.compiler.(libraryInterface); ok {
123 if library.buildRlib() || library.buildDylib() {
124 return true
125 } else {
126 return false
127 }
128 }
129 }
130 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
131}
132
Ivan Lozano52767be2019-10-18 14:49:46 -0700133func (mod *Module) ApiLevel() string {
134 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
135}
136
137func (mod *Module) Static() bool {
138 if mod.compiler != nil {
139 if library, ok := mod.compiler.(libraryInterface); ok {
140 return library.static()
141 }
142 }
143 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
144}
145
146func (mod *Module) Shared() bool {
147 if mod.compiler != nil {
148 if library, ok := mod.compiler.(libraryInterface); ok {
149 return library.static()
150 }
151 }
152 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
153}
154
155func (mod *Module) Toc() android.OptionalPath {
156 if mod.compiler != nil {
157 if _, ok := mod.compiler.(libraryInterface); ok {
158 return android.OptionalPath{}
159 }
160 }
161 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
162}
163
Yifan Hong1b3348d2020-01-21 15:53:22 -0800164func (mod *Module) OnlyInRamdisk() bool {
165 return false
166}
167
Ivan Lozano52767be2019-10-18 14:49:46 -0700168func (mod *Module) OnlyInRecovery() bool {
169 return false
170}
171
Colin Crossc511bc52020-04-07 16:50:32 +0000172func (mod *Module) UseSdk() bool {
173 return false
174}
175
Ivan Lozano52767be2019-10-18 14:49:46 -0700176func (mod *Module) UseVndk() bool {
177 return false
178}
179
180func (mod *Module) MustUseVendorVariant() bool {
181 return false
182}
183
184func (mod *Module) IsVndk() bool {
185 return false
186}
187
188func (mod *Module) HasVendorVariant() bool {
189 return false
190}
191
192func (mod *Module) SdkVersion() string {
193 return ""
194}
195
Colin Crossc511bc52020-04-07 16:50:32 +0000196func (mod *Module) AlwaysSdk() bool {
197 return false
198}
199
Ivan Lozano52767be2019-10-18 14:49:46 -0700200func (mod *Module) ToolchainLibrary() bool {
201 return false
202}
203
204func (mod *Module) NdkPrebuiltStl() bool {
205 return false
206}
207
208func (mod *Module) StubDecorator() bool {
209 return false
210}
211
Ivan Lozanoffee3342019-08-27 12:03:00 -0700212type Deps struct {
213 Dylibs []string
214 Rlibs []string
215 ProcMacros []string
216 SharedLibs []string
217 StaticLibs []string
218
219 CrtBegin, CrtEnd string
220}
221
222type PathDeps struct {
223 DyLibs RustLibraries
224 RLibs RustLibraries
225 SharedLibs android.Paths
226 StaticLibs android.Paths
227 ProcMacros RustLibraries
228 linkDirs []string
229 depFlags []string
230 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700231
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400232 coverageFiles android.Paths
233
Ivan Lozanof1c84332019-09-20 11:00:37 -0700234 CrtBegin android.OptionalPath
235 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700236}
237
238type RustLibraries []RustLibrary
239
240type RustLibrary struct {
241 Path android.Path
242 CrateName string
243}
244
245type compiler interface {
246 compilerFlags(ctx ModuleContext, flags Flags) Flags
247 compilerProps() []interface{}
248 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
249 compilerDeps(ctx DepsContext, deps Deps) Deps
250 crateName() string
251
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800252 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700253 install(ctx ModuleContext, path android.Path)
254 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400255
256 nativeCoverage() bool
257}
258
259func (mod *Module) isCoverageVariant() bool {
260 return mod.coverage.Properties.IsCoverageVariant
261}
262
263var _ cc.Coverage = (*Module)(nil)
264
265func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
266 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
267}
268
269func (mod *Module) PreventInstall() {
270 mod.Properties.PreventInstall = true
271}
272
273func (mod *Module) HideFromMake() {
274 mod.Properties.HideFromMake = true
275}
276
277func (mod *Module) MarkAsCoverageVariant(coverage bool) {
278 mod.coverage.Properties.IsCoverageVariant = coverage
279}
280
281func (mod *Module) EnableCoverageIfNeeded() {
282 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283}
284
285func defaultsFactory() android.Module {
286 return DefaultsFactory()
287}
288
289type Defaults struct {
290 android.ModuleBase
291 android.DefaultsModuleBase
292}
293
294func DefaultsFactory(props ...interface{}) android.Module {
295 module := &Defaults{}
296
297 module.AddProperties(props...)
298 module.AddProperties(
299 &BaseProperties{},
300 &BaseCompilerProperties{},
301 &BinaryCompilerProperties{},
302 &LibraryCompilerProperties{},
303 &ProcMacroCompilerProperties{},
304 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700305 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400306 &cc.CoverageProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700307 )
308
309 android.InitDefaultsModule(module)
310 return module
311}
312
313func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700314 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700315}
316
Ivan Lozano183a3212019-10-18 14:18:45 -0700317func (mod *Module) CcLibrary() bool {
318 if mod.compiler != nil {
319 if _, ok := mod.compiler.(*libraryDecorator); ok {
320 return true
321 }
322 }
323 return false
324}
325
326func (mod *Module) CcLibraryInterface() bool {
327 if mod.compiler != nil {
328 if _, ok := mod.compiler.(libraryInterface); ok {
329 return true
330 }
331 }
332 return false
333}
334
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800335func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700336 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700337 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800338 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700339 }
340 }
341 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
342}
343
344func (mod *Module) SetStatic() {
345 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700346 if library, ok := mod.compiler.(libraryInterface); ok {
347 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700348 return
349 }
350 }
351 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
352}
353
354func (mod *Module) SetShared() {
355 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700356 if library, ok := mod.compiler.(libraryInterface); ok {
357 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700358 return
359 }
360 }
361 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
362}
363
364func (mod *Module) SetBuildStubs() {
365 panic("SetBuildStubs not yet implemented for rust modules")
366}
367
368func (mod *Module) SetStubsVersions(string) {
369 panic("SetStubsVersions not yet implemented for rust modules")
370}
371
Jooyung Han03b51852020-02-26 22:45:42 +0900372func (mod *Module) StubsVersion() string {
373 panic("SetStubsVersions not yet implemented for rust modules")
374}
375
Ivan Lozano183a3212019-10-18 14:18:45 -0700376func (mod *Module) BuildStaticVariant() bool {
377 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700378 if library, ok := mod.compiler.(libraryInterface); ok {
379 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700380 }
381 }
382 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
383}
384
385func (mod *Module) BuildSharedVariant() bool {
386 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700387 if library, ok := mod.compiler.(libraryInterface); ok {
388 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700389 }
390 }
391 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
392}
393
394// Rust module deps don't have a link order (?)
395func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
396
397func (mod *Module) GetDepsInLinkOrder() []android.Path {
398 return []android.Path{}
399}
400
401func (mod *Module) GetStaticVariant() cc.LinkableInterface {
402 return nil
403}
404
405func (mod *Module) Module() android.Module {
406 return mod
407}
408
409func (mod *Module) StubsVersions() []string {
410 // For now, Rust has no stubs versions.
411 if mod.compiler != nil {
412 if _, ok := mod.compiler.(*libraryDecorator); ok {
413 return []string{}
414 }
415 }
416 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
417}
418
419func (mod *Module) OutputFile() android.OptionalPath {
420 return mod.outputFile
421}
422
423func (mod *Module) InRecovery() bool {
424 // For now, Rust has no notion of the recovery image
425 return false
426}
427func (mod *Module) HasStaticVariant() bool {
428 if mod.GetStaticVariant() != nil {
429 return true
430 }
431 return false
432}
433
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400434func (mod *Module) CoverageFiles() android.Paths {
435 if mod.compiler != nil {
436 if library, ok := mod.compiler.(*libraryDecorator); ok {
437 if library.coverageFile != nil {
438 return android.Paths{library.coverageFile}
439 }
440 return android.Paths{}
441 }
442 }
443 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
444}
445
Ivan Lozano183a3212019-10-18 14:18:45 -0700446var _ cc.LinkableInterface = (*Module)(nil)
447
Ivan Lozanoffee3342019-08-27 12:03:00 -0700448func (mod *Module) Init() android.Module {
449 mod.AddProperties(&mod.Properties)
450
451 if mod.compiler != nil {
452 mod.AddProperties(mod.compiler.compilerProps()...)
453 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400454 if mod.coverage != nil {
455 mod.AddProperties(mod.coverage.props()...)
456 }
457
Ivan Lozanoffee3342019-08-27 12:03:00 -0700458 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
459
460 android.InitDefaultableModule(mod)
461
Ivan Lozanode252912019-09-06 15:29:52 -0700462 // Explicitly disable unsupported targets.
463 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
464 disableTargets := struct {
465 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700466 Linux_bionic struct {
467 Enabled *bool
468 }
469 }
470 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700471 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
472
473 ctx.AppendProperties(&disableTargets)
474 })
475
Ivan Lozanoffee3342019-08-27 12:03:00 -0700476 return mod
477}
478
479func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
480 return &Module{
481 hod: hod,
482 multilib: multilib,
483 }
484}
485func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
486 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400487 module.coverage = &coverage{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700488 return module
489}
490
491type ModuleContext interface {
492 android.ModuleContext
493 ModuleContextIntf
494}
495
496type BaseModuleContext interface {
497 android.BaseModuleContext
498 ModuleContextIntf
499}
500
501type DepsContext interface {
502 android.BottomUpMutatorContext
503 ModuleContextIntf
504}
505
506type ModuleContextIntf interface {
507 toolchain() config.Toolchain
508 baseModuleName() string
509 CrateName() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400510 nativeCoverage() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700511}
512
513type depsContext struct {
514 android.BottomUpMutatorContext
515 moduleContextImpl
516}
517
518type moduleContext struct {
519 android.ModuleContext
520 moduleContextImpl
521}
522
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400523func (ctx *moduleContextImpl) nativeCoverage() bool {
524 return ctx.mod.nativeCoverage()
525}
526
527func (mod *Module) nativeCoverage() bool {
528 return mod.compiler != nil && mod.compiler.nativeCoverage()
529}
530
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531type moduleContextImpl struct {
532 mod *Module
533 ctx BaseModuleContext
534}
535
536func (ctx *moduleContextImpl) toolchain() config.Toolchain {
537 return ctx.mod.toolchain(ctx.ctx)
538}
539
540func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
541 if mod.cachedToolchain == nil {
542 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
543 }
544 return mod.cachedToolchain
545}
546
547func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
548}
549
550func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
551 ctx := &moduleContext{
552 ModuleContext: actx,
553 moduleContextImpl: moduleContextImpl{
554 mod: mod,
555 },
556 }
557 ctx.ctx = ctx
558
559 toolchain := mod.toolchain(ctx)
560
561 if !toolchain.Supported() {
562 // This toolchain's unsupported, there's nothing to do for this mod.
563 return
564 }
565
566 deps := mod.depsToPaths(ctx)
567 flags := Flags{
568 Toolchain: toolchain,
569 }
570
571 if mod.compiler != nil {
572 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400573 }
574 if mod.coverage != nil {
575 flags, deps = mod.coverage.flags(ctx, flags, deps)
576 }
577
578 if mod.compiler != nil {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579 outputFile := mod.compiler.compile(ctx, flags, deps)
580 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400581 if !mod.Properties.PreventInstall {
582 mod.compiler.install(ctx, mod.outputFile.Path())
583 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700584 }
585}
586
587func (mod *Module) deps(ctx DepsContext) Deps {
588 deps := Deps{}
589
590 if mod.compiler != nil {
591 deps = mod.compiler.compilerDeps(ctx, deps)
592 }
593
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400594 if mod.coverage != nil {
595 deps = mod.coverage.deps(ctx, deps)
596 }
597
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
599 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
600 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
601 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
602 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
603
604 return deps
605
606}
607
608func (ctx *moduleContextImpl) baseModuleName() string {
609 return ctx.mod.ModuleBase.BaseModuleName()
610}
611
612func (ctx *moduleContextImpl) CrateName() string {
613 return ctx.mod.CrateName()
614}
615
616type dependencyTag struct {
617 blueprint.BaseDependencyTag
618 name string
619 library bool
620 proc_macro bool
621}
622
623var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700624 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
625 dylibDepTag = dependencyTag{name: "dylib", library: true}
626 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
627 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700628)
629
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400630func (mod *Module) begin(ctx BaseModuleContext) {
631 if mod.coverage != nil {
632 mod.coverage.begin(ctx)
633 }
634}
635
Ivan Lozanoffee3342019-08-27 12:03:00 -0700636func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
637 var depPaths PathDeps
638
639 directRlibDeps := []*Module{}
640 directDylibDeps := []*Module{}
641 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700642 directSharedLibDeps := [](cc.LinkableInterface){}
643 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644
645 ctx.VisitDirectDeps(func(dep android.Module) {
646 depName := ctx.OtherModuleName(dep)
647 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648 if rustDep, ok := dep.(*Module); ok {
649 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700650
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651 linkFile := rustDep.outputFile
652 if !linkFile.Valid() {
653 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
654 }
655
656 switch depTag {
657 case dylibDepTag:
658 dylib, ok := rustDep.compiler.(libraryInterface)
659 if !ok || !dylib.dylib() {
660 ctx.ModuleErrorf("mod %q not an dylib library", depName)
661 return
662 }
663 directDylibDeps = append(directDylibDeps, rustDep)
664 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
665 case rlibDepTag:
666 rlib, ok := rustDep.compiler.(libraryInterface)
667 if !ok || !rlib.rlib() {
668 ctx.ModuleErrorf("mod %q not an rlib library", depName)
669 return
670 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400671 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700672 directRlibDeps = append(directRlibDeps, rustDep)
673 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
674 case procMacroDepTag:
675 directProcMacroDeps = append(directProcMacroDeps, rustDep)
676 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
677 }
678
679 //Append the dependencies exportedDirs
680 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
681 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
682 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683 }
684
685 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
686 // This can be probably be refactored by defining a common exporter interface similar to cc's
687 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
688 linkDir := linkPathFromFilePath(linkFile.Path())
689 if lib, ok := mod.compiler.(*libraryDecorator); ok {
690 lib.linkDirs = append(lib.linkDirs, linkDir)
691 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
692 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
693 }
694 }
695
Ivan Lozano52767be2019-10-18 14:49:46 -0700696 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700697
Ivan Lozano52767be2019-10-18 14:49:46 -0700698 if ccDep, ok := dep.(cc.LinkableInterface); ok {
699 //Handle C dependencies
700 if _, ok := ccDep.(*Module); !ok {
701 if ccDep.Module().Target().Os != ctx.Os() {
702 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
703 return
704 }
705 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
706 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
707 return
708 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700709 }
710
Ivan Lozanoffee3342019-08-27 12:03:00 -0700711 linkFile := ccDep.OutputFile()
712 linkPath := linkPathFromFilePath(linkFile.Path())
713 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500714 depFlag := "-l" + libName
715
Ivan Lozanoffee3342019-08-27 12:03:00 -0700716 if !linkFile.Valid() {
717 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
718 }
719
720 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700722 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500723 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500725 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400726 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700727 directStaticLibDeps = append(directStaticLibDeps, ccDep)
728 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700729 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500730 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700731 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500732 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 directSharedLibDeps = append(directSharedLibDeps, ccDep)
734 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
735 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700736 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700737 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700738 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700739 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740 }
741
742 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700743 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500745 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
747 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500748 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749 }
750
751 }
752 })
753
754 var rlibDepFiles RustLibraries
755 for _, dep := range directRlibDeps {
756 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
757 }
758 var dylibDepFiles RustLibraries
759 for _, dep := range directDylibDeps {
760 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
761 }
762 var procMacroDepFiles RustLibraries
763 for _, dep := range directProcMacroDeps {
764 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
765 }
766
767 var staticLibDepFiles android.Paths
768 for _, dep := range directStaticLibDeps {
769 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
770 }
771
772 var sharedLibDepFiles android.Paths
773 for _, dep := range directSharedLibDeps {
774 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
775 }
776
777 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
778 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
779 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
780 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
781 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
782
783 // Dedup exported flags from dependencies
784 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
785 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
786
787 return depPaths
788}
789
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800790func (mod *Module) InstallInData() bool {
791 if mod.compiler == nil {
792 return false
793 }
794 return mod.compiler.inData()
795}
796
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797func linkPathFromFilePath(filepath android.Path) string {
798 return strings.Split(filepath.String(), filepath.Base())[0]
799}
Ivan Lozanod648c432020-02-06 12:05:10 -0500800
Ivan Lozanoffee3342019-08-27 12:03:00 -0700801func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500802 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700803 if strings.HasPrefix(libName, "lib") {
804 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 }
806 return libName
807}
Ivan Lozanod648c432020-02-06 12:05:10 -0500808
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
810 ctx := &depsContext{
811 BottomUpMutatorContext: actx,
812 moduleContextImpl: moduleContextImpl{
813 mod: mod,
814 },
815 }
816 ctx.ctx = ctx
817
818 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700819 commonDepVariations := []blueprint.Variation{}
Jooyung Han624d35c2020-04-10 12:57:24 +0900820 if cc.VersionVariantAvailable(mod) {
821 commonDepVariations = append(commonDepVariations,
822 blueprint.Variation{Mutator: "version", Variation: ""})
823 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700825 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800826 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700828 actx.AddVariationDependencies(
829 append(commonDepVariations, []blueprint.Variation{
830 {Mutator: "rust_libraries", Variation: "rlib"},
831 {Mutator: "link", Variation: ""}}...),
832 rlibDepTag, deps.Rlibs...)
833 actx.AddVariationDependencies(
834 append(commonDepVariations, []blueprint.Variation{
835 {Mutator: "rust_libraries", Variation: "dylib"},
836 {Mutator: "link", Variation: ""}}...),
837 dylibDepTag, deps.Dylibs...)
838
839 actx.AddVariationDependencies(append(commonDepVariations,
840 blueprint.Variation{Mutator: "link", Variation: "shared"}),
841 cc.SharedDepTag, deps.SharedLibs...)
842 actx.AddVariationDependencies(append(commonDepVariations,
843 blueprint.Variation{Mutator: "link", Variation: "static"}),
844 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700845
Ivan Lozanof1c84332019-09-20 11:00:37 -0700846 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700847 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700848 }
849 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700850 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700851 }
852
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700853 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700854 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855}
856
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400857func BeginMutator(ctx android.BottomUpMutatorContext) {
858 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
859 mod.beginMutator(ctx)
860 }
861}
862
863type baseModuleContext struct {
864 android.BaseModuleContext
865 moduleContextImpl
866}
867
868func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
869 ctx := &baseModuleContext{
870 BaseModuleContext: actx,
871 moduleContextImpl: moduleContextImpl{
872 mod: mod,
873 },
874 }
875 ctx.ctx = ctx
876
877 mod.begin(ctx)
878}
879
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880func (mod *Module) Name() string {
881 name := mod.ModuleBase.Name()
882 if p, ok := mod.compiler.(interface {
883 Name(string) string
884 }); ok {
885 name = p.Name(name)
886 }
887 return name
888}
889
890var Bool = proptools.Bool
891var BoolDefault = proptools.BoolDefault
892var String = proptools.String
893var StringPtr = proptools.StringPtr