blob: 56f94cf6d07f94c847819c0a010175a8ec85b383 [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 Lozanoffee3342019-08-27 12:03:00 -070043 })
44 pctx.Import("android/soong/rust/config")
45}
46
47type Flags struct {
Ivan Lozanof1c84332019-09-20 11:00:37 -070048 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
52 RustFlagsDeps android.Paths // Files depended on by compiler flags
53 Toolchain config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -070054}
55
56type BaseProperties struct {
57 AndroidMkRlibs []string
58 AndroidMkDylibs []string
59 AndroidMkProcMacroLibs []string
60 AndroidMkSharedLibs []string
61 AndroidMkStaticLibs []string
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070062 SubName string `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type Module struct {
66 android.ModuleBase
67 android.DefaultableModuleBase
68
69 Properties BaseProperties
70
71 hod android.HostOrDeviceSupported
72 multilib android.Multilib
73
74 compiler compiler
75 cachedToolchain config.Toolchain
76 subAndroidMkOnce map[subAndroidMkProvider]bool
77 outputFile android.OptionalPath
78}
79
Colin Cross7228ecd2019-11-18 16:00:16 -080080var _ android.ImageInterface = (*Module)(nil)
81
82func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
83
84func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
85 return true
86}
87
88func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
89 return mod.InRecovery()
90}
91
92func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
93 return nil
94}
95
96func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
97}
98
Ivan Lozano52767be2019-10-18 14:49:46 -070099func (mod *Module) BuildStubs() bool {
100 return false
101}
102
103func (mod *Module) HasStubsVariants() bool {
104 return false
105}
106
107func (mod *Module) SelectedStl() string {
108 return ""
109}
110
111func (mod *Module) ApiLevel() string {
112 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
113}
114
115func (mod *Module) Static() bool {
116 if mod.compiler != nil {
117 if library, ok := mod.compiler.(libraryInterface); ok {
118 return library.static()
119 }
120 }
121 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
122}
123
124func (mod *Module) Shared() bool {
125 if mod.compiler != nil {
126 if library, ok := mod.compiler.(libraryInterface); ok {
127 return library.static()
128 }
129 }
130 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
131}
132
133func (mod *Module) Toc() android.OptionalPath {
134 if mod.compiler != nil {
135 if _, ok := mod.compiler.(libraryInterface); ok {
136 return android.OptionalPath{}
137 }
138 }
139 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
140}
141
142func (mod *Module) OnlyInRecovery() bool {
143 return false
144}
145
146func (mod *Module) UseVndk() bool {
147 return false
148}
149
150func (mod *Module) MustUseVendorVariant() bool {
151 return false
152}
153
154func (mod *Module) IsVndk() bool {
155 return false
156}
157
158func (mod *Module) HasVendorVariant() bool {
159 return false
160}
161
162func (mod *Module) SdkVersion() string {
163 return ""
164}
165
166func (mod *Module) ToolchainLibrary() bool {
167 return false
168}
169
170func (mod *Module) NdkPrebuiltStl() bool {
171 return false
172}
173
174func (mod *Module) StubDecorator() bool {
175 return false
176}
177
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178type Deps struct {
179 Dylibs []string
180 Rlibs []string
181 ProcMacros []string
182 SharedLibs []string
183 StaticLibs []string
184
185 CrtBegin, CrtEnd string
186}
187
188type PathDeps struct {
189 DyLibs RustLibraries
190 RLibs RustLibraries
191 SharedLibs android.Paths
192 StaticLibs android.Paths
193 ProcMacros RustLibraries
194 linkDirs []string
195 depFlags []string
196 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700197
198 CrtBegin android.OptionalPath
199 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700200}
201
202type RustLibraries []RustLibrary
203
204type RustLibrary struct {
205 Path android.Path
206 CrateName string
207}
208
209type compiler interface {
210 compilerFlags(ctx ModuleContext, flags Flags) Flags
211 compilerProps() []interface{}
212 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
213 compilerDeps(ctx DepsContext, deps Deps) Deps
214 crateName() string
215
216 install(ctx ModuleContext, path android.Path)
217 relativeInstallPath() string
218}
219
220func defaultsFactory() android.Module {
221 return DefaultsFactory()
222}
223
224type Defaults struct {
225 android.ModuleBase
226 android.DefaultsModuleBase
227}
228
229func DefaultsFactory(props ...interface{}) android.Module {
230 module := &Defaults{}
231
232 module.AddProperties(props...)
233 module.AddProperties(
234 &BaseProperties{},
235 &BaseCompilerProperties{},
236 &BinaryCompilerProperties{},
237 &LibraryCompilerProperties{},
238 &ProcMacroCompilerProperties{},
239 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700240 &TestProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241 )
242
243 android.InitDefaultsModule(module)
244 return module
245}
246
247func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700248 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700249}
250
Ivan Lozano183a3212019-10-18 14:18:45 -0700251func (mod *Module) CcLibrary() bool {
252 if mod.compiler != nil {
253 if _, ok := mod.compiler.(*libraryDecorator); ok {
254 return true
255 }
256 }
257 return false
258}
259
260func (mod *Module) CcLibraryInterface() bool {
261 if mod.compiler != nil {
262 if _, ok := mod.compiler.(libraryInterface); ok {
263 return true
264 }
265 }
266 return false
267}
268
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800269func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700270 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700271 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800272 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700273 }
274 }
275 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
276}
277
278func (mod *Module) SetStatic() {
279 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700280 if library, ok := mod.compiler.(libraryInterface); ok {
281 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700282 return
283 }
284 }
285 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
286}
287
288func (mod *Module) SetShared() {
289 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700290 if library, ok := mod.compiler.(libraryInterface); ok {
291 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700292 return
293 }
294 }
295 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
296}
297
298func (mod *Module) SetBuildStubs() {
299 panic("SetBuildStubs not yet implemented for rust modules")
300}
301
302func (mod *Module) SetStubsVersions(string) {
303 panic("SetStubsVersions not yet implemented for rust modules")
304}
305
306func (mod *Module) BuildStaticVariant() bool {
307 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700308 if library, ok := mod.compiler.(libraryInterface); ok {
309 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700310 }
311 }
312 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
313}
314
315func (mod *Module) BuildSharedVariant() bool {
316 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700317 if library, ok := mod.compiler.(libraryInterface); ok {
318 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700319 }
320 }
321 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
322}
323
324// Rust module deps don't have a link order (?)
325func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
326
327func (mod *Module) GetDepsInLinkOrder() []android.Path {
328 return []android.Path{}
329}
330
331func (mod *Module) GetStaticVariant() cc.LinkableInterface {
332 return nil
333}
334
335func (mod *Module) Module() android.Module {
336 return mod
337}
338
339func (mod *Module) StubsVersions() []string {
340 // For now, Rust has no stubs versions.
341 if mod.compiler != nil {
342 if _, ok := mod.compiler.(*libraryDecorator); ok {
343 return []string{}
344 }
345 }
346 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
347}
348
349func (mod *Module) OutputFile() android.OptionalPath {
350 return mod.outputFile
351}
352
353func (mod *Module) InRecovery() bool {
354 // For now, Rust has no notion of the recovery image
355 return false
356}
357func (mod *Module) HasStaticVariant() bool {
358 if mod.GetStaticVariant() != nil {
359 return true
360 }
361 return false
362}
363
364var _ cc.LinkableInterface = (*Module)(nil)
365
Ivan Lozanoffee3342019-08-27 12:03:00 -0700366func (mod *Module) Init() android.Module {
367 mod.AddProperties(&mod.Properties)
368
369 if mod.compiler != nil {
370 mod.AddProperties(mod.compiler.compilerProps()...)
371 }
372 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
373
374 android.InitDefaultableModule(mod)
375
Ivan Lozanode252912019-09-06 15:29:52 -0700376 // Explicitly disable unsupported targets.
377 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
378 disableTargets := struct {
379 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700380 Linux_bionic struct {
381 Enabled *bool
382 }
383 }
384 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700385 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
386
387 ctx.AppendProperties(&disableTargets)
388 })
389
Ivan Lozanoffee3342019-08-27 12:03:00 -0700390 return mod
391}
392
393func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
394 return &Module{
395 hod: hod,
396 multilib: multilib,
397 }
398}
399func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
400 module := newBaseModule(hod, multilib)
401 return module
402}
403
404type ModuleContext interface {
405 android.ModuleContext
406 ModuleContextIntf
407}
408
409type BaseModuleContext interface {
410 android.BaseModuleContext
411 ModuleContextIntf
412}
413
414type DepsContext interface {
415 android.BottomUpMutatorContext
416 ModuleContextIntf
417}
418
419type ModuleContextIntf interface {
420 toolchain() config.Toolchain
421 baseModuleName() string
422 CrateName() string
423}
424
425type depsContext struct {
426 android.BottomUpMutatorContext
427 moduleContextImpl
428}
429
430type moduleContext struct {
431 android.ModuleContext
432 moduleContextImpl
433}
434
435type moduleContextImpl struct {
436 mod *Module
437 ctx BaseModuleContext
438}
439
440func (ctx *moduleContextImpl) toolchain() config.Toolchain {
441 return ctx.mod.toolchain(ctx.ctx)
442}
443
444func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
445 if mod.cachedToolchain == nil {
446 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
447 }
448 return mod.cachedToolchain
449}
450
451func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
452}
453
454func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
455 ctx := &moduleContext{
456 ModuleContext: actx,
457 moduleContextImpl: moduleContextImpl{
458 mod: mod,
459 },
460 }
461 ctx.ctx = ctx
462
463 toolchain := mod.toolchain(ctx)
464
465 if !toolchain.Supported() {
466 // This toolchain's unsupported, there's nothing to do for this mod.
467 return
468 }
469
470 deps := mod.depsToPaths(ctx)
471 flags := Flags{
472 Toolchain: toolchain,
473 }
474
475 if mod.compiler != nil {
476 flags = mod.compiler.compilerFlags(ctx, flags)
477 outputFile := mod.compiler.compile(ctx, flags, deps)
478 mod.outputFile = android.OptionalPathForPath(outputFile)
479 mod.compiler.install(ctx, mod.outputFile.Path())
480 }
481}
482
483func (mod *Module) deps(ctx DepsContext) Deps {
484 deps := Deps{}
485
486 if mod.compiler != nil {
487 deps = mod.compiler.compilerDeps(ctx, deps)
488 }
489
490 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
491 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
492 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
493 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
494 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
495
496 return deps
497
498}
499
500func (ctx *moduleContextImpl) baseModuleName() string {
501 return ctx.mod.ModuleBase.BaseModuleName()
502}
503
504func (ctx *moduleContextImpl) CrateName() string {
505 return ctx.mod.CrateName()
506}
507
508type dependencyTag struct {
509 blueprint.BaseDependencyTag
510 name string
511 library bool
512 proc_macro bool
513}
514
515var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700516 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
517 dylibDepTag = dependencyTag{name: "dylib", library: true}
518 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
519 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700520)
521
522func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
523 var depPaths PathDeps
524
525 directRlibDeps := []*Module{}
526 directDylibDeps := []*Module{}
527 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700528 directSharedLibDeps := [](cc.LinkableInterface){}
529 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700530
531 ctx.VisitDirectDeps(func(dep android.Module) {
532 depName := ctx.OtherModuleName(dep)
533 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534 if rustDep, ok := dep.(*Module); ok {
535 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700536
Ivan Lozanoffee3342019-08-27 12:03:00 -0700537 linkFile := rustDep.outputFile
538 if !linkFile.Valid() {
539 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
540 }
541
542 switch depTag {
543 case dylibDepTag:
544 dylib, ok := rustDep.compiler.(libraryInterface)
545 if !ok || !dylib.dylib() {
546 ctx.ModuleErrorf("mod %q not an dylib library", depName)
547 return
548 }
549 directDylibDeps = append(directDylibDeps, rustDep)
550 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
551 case rlibDepTag:
552 rlib, ok := rustDep.compiler.(libraryInterface)
553 if !ok || !rlib.rlib() {
554 ctx.ModuleErrorf("mod %q not an rlib library", depName)
555 return
556 }
557 directRlibDeps = append(directRlibDeps, rustDep)
558 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
559 case procMacroDepTag:
560 directProcMacroDeps = append(directProcMacroDeps, rustDep)
561 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
562 }
563
564 //Append the dependencies exportedDirs
565 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
566 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
567 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568 }
569
570 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
571 // This can be probably be refactored by defining a common exporter interface similar to cc's
572 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
573 linkDir := linkPathFromFilePath(linkFile.Path())
574 if lib, ok := mod.compiler.(*libraryDecorator); ok {
575 lib.linkDirs = append(lib.linkDirs, linkDir)
576 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
577 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
578 }
579 }
580
Ivan Lozano52767be2019-10-18 14:49:46 -0700581 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700582
Ivan Lozano52767be2019-10-18 14:49:46 -0700583 if ccDep, ok := dep.(cc.LinkableInterface); ok {
584 //Handle C dependencies
585 if _, ok := ccDep.(*Module); !ok {
586 if ccDep.Module().Target().Os != ctx.Os() {
587 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
588 return
589 }
590 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
591 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
592 return
593 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700594 }
595
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596 linkFile := ccDep.OutputFile()
597 linkPath := linkPathFromFilePath(linkFile.Path())
598 libName := libNameFromFilePath(linkFile.Path())
599 if !linkFile.Valid() {
600 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
601 }
602
603 exportDep := false
604
605 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700606 case cc.StaticDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
608 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
609 directStaticLibDeps = append(directStaticLibDeps, ccDep)
610 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700611 case cc.SharedDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700612 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
613 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
614 directSharedLibDeps = append(directSharedLibDeps, ccDep)
615 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
616 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700617 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700618 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700619 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700620 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700621 }
622
623 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700624 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700625 lib.linkDirs = append(lib.linkDirs, linkPath)
626 lib.depFlags = append(lib.depFlags, "-l"+libName)
627 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
628 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
629 procMacro.depFlags = append(procMacro.depFlags, "-l"+libName)
630 }
631
632 }
633 })
634
635 var rlibDepFiles RustLibraries
636 for _, dep := range directRlibDeps {
637 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
638 }
639 var dylibDepFiles RustLibraries
640 for _, dep := range directDylibDeps {
641 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
642 }
643 var procMacroDepFiles RustLibraries
644 for _, dep := range directProcMacroDeps {
645 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
646 }
647
648 var staticLibDepFiles android.Paths
649 for _, dep := range directStaticLibDeps {
650 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
651 }
652
653 var sharedLibDepFiles android.Paths
654 for _, dep := range directSharedLibDeps {
655 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
656 }
657
658 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
659 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
660 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
661 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
662 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
663
664 // Dedup exported flags from dependencies
665 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
666 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
667
668 return depPaths
669}
670
671func linkPathFromFilePath(filepath android.Path) string {
672 return strings.Split(filepath.String(), filepath.Base())[0]
673}
674func libNameFromFilePath(filepath android.Path) string {
675 libName := strings.Split(filepath.Base(), filepath.Ext())[0]
Ivan Lozano52767be2019-10-18 14:49:46 -0700676 if strings.HasPrefix(libName, "lib") {
677 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678 }
679 return libName
680}
681func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
682 ctx := &depsContext{
683 BottomUpMutatorContext: actx,
684 moduleContextImpl: moduleContextImpl{
685 mod: mod,
686 },
687 }
688 ctx.ctx = ctx
689
690 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700691 commonDepVariations := []blueprint.Variation{}
692 commonDepVariations = append(commonDepVariations,
693 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700694 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700695 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800696 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700697 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700698
699 actx.AddVariationDependencies(
700 append(commonDepVariations, []blueprint.Variation{
701 {Mutator: "rust_libraries", Variation: "rlib"},
702 {Mutator: "link", Variation: ""}}...),
703 rlibDepTag, deps.Rlibs...)
704 actx.AddVariationDependencies(
705 append(commonDepVariations, []blueprint.Variation{
706 {Mutator: "rust_libraries", Variation: "dylib"},
707 {Mutator: "link", Variation: ""}}...),
708 dylibDepTag, deps.Dylibs...)
709
710 actx.AddVariationDependencies(append(commonDepVariations,
711 blueprint.Variation{Mutator: "link", Variation: "shared"}),
712 cc.SharedDepTag, deps.SharedLibs...)
713 actx.AddVariationDependencies(append(commonDepVariations,
714 blueprint.Variation{Mutator: "link", Variation: "static"}),
715 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700716
Ivan Lozanof1c84332019-09-20 11:00:37 -0700717 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700718 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700719 }
720 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700721 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700722 }
723
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700724 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700725 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726}
727
728func (mod *Module) Name() string {
729 name := mod.ModuleBase.Name()
730 if p, ok := mod.compiler.(interface {
731 Name(string) string
732 }); ok {
733 name = p.Name(name)
734 }
735 return name
736}
737
738var Bool = proptools.Bool
739var BoolDefault = proptools.BoolDefault
740var String = proptools.String
741var StringPtr = proptools.StringPtr