blob: ec3b59086f5ff2dfab1f559fc80fbd3851125e46 [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
Ivan Lozano52767be2019-10-18 14:49:46 -070080func (mod *Module) BuildStubs() bool {
81 return false
82}
83
84func (mod *Module) HasStubsVariants() bool {
85 return false
86}
87
88func (mod *Module) SelectedStl() string {
89 return ""
90}
91
92func (mod *Module) ApiLevel() string {
93 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
94}
95
96func (mod *Module) Static() bool {
97 if mod.compiler != nil {
98 if library, ok := mod.compiler.(libraryInterface); ok {
99 return library.static()
100 }
101 }
102 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
103}
104
105func (mod *Module) Shared() bool {
106 if mod.compiler != nil {
107 if library, ok := mod.compiler.(libraryInterface); ok {
108 return library.static()
109 }
110 }
111 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
112}
113
114func (mod *Module) Toc() android.OptionalPath {
115 if mod.compiler != nil {
116 if _, ok := mod.compiler.(libraryInterface); ok {
117 return android.OptionalPath{}
118 }
119 }
120 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
121}
122
123func (mod *Module) OnlyInRecovery() bool {
124 return false
125}
126
127func (mod *Module) UseVndk() bool {
128 return false
129}
130
131func (mod *Module) MustUseVendorVariant() bool {
132 return false
133}
134
135func (mod *Module) IsVndk() bool {
136 return false
137}
138
139func (mod *Module) HasVendorVariant() bool {
140 return false
141}
142
143func (mod *Module) SdkVersion() string {
144 return ""
145}
146
147func (mod *Module) ToolchainLibrary() bool {
148 return false
149}
150
151func (mod *Module) NdkPrebuiltStl() bool {
152 return false
153}
154
155func (mod *Module) StubDecorator() bool {
156 return false
157}
158
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159type Deps struct {
160 Dylibs []string
161 Rlibs []string
162 ProcMacros []string
163 SharedLibs []string
164 StaticLibs []string
165
166 CrtBegin, CrtEnd string
167}
168
169type PathDeps struct {
170 DyLibs RustLibraries
171 RLibs RustLibraries
172 SharedLibs android.Paths
173 StaticLibs android.Paths
174 ProcMacros RustLibraries
175 linkDirs []string
176 depFlags []string
177 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700178
179 CrtBegin android.OptionalPath
180 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700181}
182
183type RustLibraries []RustLibrary
184
185type RustLibrary struct {
186 Path android.Path
187 CrateName string
188}
189
190type compiler interface {
191 compilerFlags(ctx ModuleContext, flags Flags) Flags
192 compilerProps() []interface{}
193 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
194 compilerDeps(ctx DepsContext, deps Deps) Deps
195 crateName() string
196
197 install(ctx ModuleContext, path android.Path)
198 relativeInstallPath() string
199}
200
201func defaultsFactory() android.Module {
202 return DefaultsFactory()
203}
204
205type Defaults struct {
206 android.ModuleBase
207 android.DefaultsModuleBase
208}
209
210func DefaultsFactory(props ...interface{}) android.Module {
211 module := &Defaults{}
212
213 module.AddProperties(props...)
214 module.AddProperties(
215 &BaseProperties{},
216 &BaseCompilerProperties{},
217 &BinaryCompilerProperties{},
218 &LibraryCompilerProperties{},
219 &ProcMacroCompilerProperties{},
220 &PrebuiltProperties{},
221 )
222
223 android.InitDefaultsModule(module)
224 return module
225}
226
227func (mod *Module) CrateName() string {
228 if mod.compiler != nil && mod.compiler.crateName() != "" {
229 return mod.compiler.crateName()
230 }
231 // Default crate names replace '-' in the name to '_'
232 return strings.Replace(mod.BaseModuleName(), "-", "_", -1)
233}
234
Ivan Lozano183a3212019-10-18 14:18:45 -0700235func (mod *Module) CcLibrary() bool {
236 if mod.compiler != nil {
237 if _, ok := mod.compiler.(*libraryDecorator); ok {
238 return true
239 }
240 }
241 return false
242}
243
244func (mod *Module) CcLibraryInterface() bool {
245 if mod.compiler != nil {
246 if _, ok := mod.compiler.(libraryInterface); ok {
247 return true
248 }
249 }
250 return false
251}
252
Ivan Lozano52767be2019-10-18 14:49:46 -0700253func (mod *Module) IncludeDirs(ctx android.BaseModuleContext) android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700254 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700255 if library, ok := mod.compiler.(*libraryDecorator); ok {
256 return android.PathsForSource(ctx, library.Properties.Include_dirs)
Ivan Lozano183a3212019-10-18 14:18:45 -0700257 }
258 }
259 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
260}
261
262func (mod *Module) SetStatic() {
263 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700264 if library, ok := mod.compiler.(libraryInterface); ok {
265 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700266 return
267 }
268 }
269 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
270}
271
272func (mod *Module) SetShared() {
273 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700274 if library, ok := mod.compiler.(libraryInterface); ok {
275 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700276 return
277 }
278 }
279 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
280}
281
282func (mod *Module) SetBuildStubs() {
283 panic("SetBuildStubs not yet implemented for rust modules")
284}
285
286func (mod *Module) SetStubsVersions(string) {
287 panic("SetStubsVersions not yet implemented for rust modules")
288}
289
290func (mod *Module) BuildStaticVariant() bool {
291 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700292 if library, ok := mod.compiler.(libraryInterface); ok {
293 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700294 }
295 }
296 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
297}
298
299func (mod *Module) BuildSharedVariant() bool {
300 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 if library, ok := mod.compiler.(libraryInterface); ok {
302 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700303 }
304 }
305 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
306}
307
308// Rust module deps don't have a link order (?)
309func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
310
311func (mod *Module) GetDepsInLinkOrder() []android.Path {
312 return []android.Path{}
313}
314
315func (mod *Module) GetStaticVariant() cc.LinkableInterface {
316 return nil
317}
318
319func (mod *Module) Module() android.Module {
320 return mod
321}
322
323func (mod *Module) StubsVersions() []string {
324 // For now, Rust has no stubs versions.
325 if mod.compiler != nil {
326 if _, ok := mod.compiler.(*libraryDecorator); ok {
327 return []string{}
328 }
329 }
330 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
331}
332
333func (mod *Module) OutputFile() android.OptionalPath {
334 return mod.outputFile
335}
336
337func (mod *Module) InRecovery() bool {
338 // For now, Rust has no notion of the recovery image
339 return false
340}
341func (mod *Module) HasStaticVariant() bool {
342 if mod.GetStaticVariant() != nil {
343 return true
344 }
345 return false
346}
347
348var _ cc.LinkableInterface = (*Module)(nil)
349
Ivan Lozanoffee3342019-08-27 12:03:00 -0700350func (mod *Module) Init() android.Module {
351 mod.AddProperties(&mod.Properties)
352
353 if mod.compiler != nil {
354 mod.AddProperties(mod.compiler.compilerProps()...)
355 }
356 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
357
358 android.InitDefaultableModule(mod)
359
Ivan Lozanode252912019-09-06 15:29:52 -0700360 // Explicitly disable unsupported targets.
361 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
362 disableTargets := struct {
363 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700364 Linux_bionic struct {
365 Enabled *bool
366 }
367 }
368 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700369 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
370
371 ctx.AppendProperties(&disableTargets)
372 })
373
Ivan Lozanoffee3342019-08-27 12:03:00 -0700374 return mod
375}
376
377func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
378 return &Module{
379 hod: hod,
380 multilib: multilib,
381 }
382}
383func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
384 module := newBaseModule(hod, multilib)
385 return module
386}
387
388type ModuleContext interface {
389 android.ModuleContext
390 ModuleContextIntf
391}
392
393type BaseModuleContext interface {
394 android.BaseModuleContext
395 ModuleContextIntf
396}
397
398type DepsContext interface {
399 android.BottomUpMutatorContext
400 ModuleContextIntf
401}
402
403type ModuleContextIntf interface {
404 toolchain() config.Toolchain
405 baseModuleName() string
406 CrateName() string
407}
408
409type depsContext struct {
410 android.BottomUpMutatorContext
411 moduleContextImpl
412}
413
414type moduleContext struct {
415 android.ModuleContext
416 moduleContextImpl
417}
418
419type moduleContextImpl struct {
420 mod *Module
421 ctx BaseModuleContext
422}
423
424func (ctx *moduleContextImpl) toolchain() config.Toolchain {
425 return ctx.mod.toolchain(ctx.ctx)
426}
427
428func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
429 if mod.cachedToolchain == nil {
430 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
431 }
432 return mod.cachedToolchain
433}
434
435func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
436}
437
438func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
439 ctx := &moduleContext{
440 ModuleContext: actx,
441 moduleContextImpl: moduleContextImpl{
442 mod: mod,
443 },
444 }
445 ctx.ctx = ctx
446
447 toolchain := mod.toolchain(ctx)
448
449 if !toolchain.Supported() {
450 // This toolchain's unsupported, there's nothing to do for this mod.
451 return
452 }
453
454 deps := mod.depsToPaths(ctx)
455 flags := Flags{
456 Toolchain: toolchain,
457 }
458
459 if mod.compiler != nil {
460 flags = mod.compiler.compilerFlags(ctx, flags)
461 outputFile := mod.compiler.compile(ctx, flags, deps)
462 mod.outputFile = android.OptionalPathForPath(outputFile)
463 mod.compiler.install(ctx, mod.outputFile.Path())
464 }
465}
466
467func (mod *Module) deps(ctx DepsContext) Deps {
468 deps := Deps{}
469
470 if mod.compiler != nil {
471 deps = mod.compiler.compilerDeps(ctx, deps)
472 }
473
474 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
475 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
476 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
477 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
478 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
479
480 return deps
481
482}
483
484func (ctx *moduleContextImpl) baseModuleName() string {
485 return ctx.mod.ModuleBase.BaseModuleName()
486}
487
488func (ctx *moduleContextImpl) CrateName() string {
489 return ctx.mod.CrateName()
490}
491
492type dependencyTag struct {
493 blueprint.BaseDependencyTag
494 name string
495 library bool
496 proc_macro bool
497}
498
499var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700500 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
501 dylibDepTag = dependencyTag{name: "dylib", library: true}
502 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
503 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700504)
505
506func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
507 var depPaths PathDeps
508
509 directRlibDeps := []*Module{}
510 directDylibDeps := []*Module{}
511 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700512 directSharedLibDeps := [](cc.LinkableInterface){}
513 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700514
515 ctx.VisitDirectDeps(func(dep android.Module) {
516 depName := ctx.OtherModuleName(dep)
517 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518 if rustDep, ok := dep.(*Module); ok {
519 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700520
Ivan Lozanoffee3342019-08-27 12:03:00 -0700521 linkFile := rustDep.outputFile
522 if !linkFile.Valid() {
523 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
524 }
525
526 switch depTag {
527 case dylibDepTag:
528 dylib, ok := rustDep.compiler.(libraryInterface)
529 if !ok || !dylib.dylib() {
530 ctx.ModuleErrorf("mod %q not an dylib library", depName)
531 return
532 }
533 directDylibDeps = append(directDylibDeps, rustDep)
534 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
535 case rlibDepTag:
536 rlib, ok := rustDep.compiler.(libraryInterface)
537 if !ok || !rlib.rlib() {
538 ctx.ModuleErrorf("mod %q not an rlib library", depName)
539 return
540 }
541 directRlibDeps = append(directRlibDeps, rustDep)
542 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
543 case procMacroDepTag:
544 directProcMacroDeps = append(directProcMacroDeps, rustDep)
545 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
546 }
547
548 //Append the dependencies exportedDirs
549 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
550 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
551 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700552 }
553
554 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
555 // This can be probably be refactored by defining a common exporter interface similar to cc's
556 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
557 linkDir := linkPathFromFilePath(linkFile.Path())
558 if lib, ok := mod.compiler.(*libraryDecorator); ok {
559 lib.linkDirs = append(lib.linkDirs, linkDir)
560 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
561 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
562 }
563 }
564
Ivan Lozano52767be2019-10-18 14:49:46 -0700565 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700566
Ivan Lozano52767be2019-10-18 14:49:46 -0700567 if ccDep, ok := dep.(cc.LinkableInterface); ok {
568 //Handle C dependencies
569 if _, ok := ccDep.(*Module); !ok {
570 if ccDep.Module().Target().Os != ctx.Os() {
571 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
572 return
573 }
574 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
575 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
576 return
577 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700578 }
579
Ivan Lozanoffee3342019-08-27 12:03:00 -0700580 linkFile := ccDep.OutputFile()
581 linkPath := linkPathFromFilePath(linkFile.Path())
582 libName := libNameFromFilePath(linkFile.Path())
583 if !linkFile.Valid() {
584 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
585 }
586
587 exportDep := false
588
589 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700590 case cc.StaticDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700591 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
592 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
593 directStaticLibDeps = append(directStaticLibDeps, ccDep)
594 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700595 case cc.SharedDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
597 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
598 directSharedLibDeps = append(directSharedLibDeps, ccDep)
599 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
600 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700601 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700602 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700603 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700604 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 }
606
607 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700608 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700609 lib.linkDirs = append(lib.linkDirs, linkPath)
610 lib.depFlags = append(lib.depFlags, "-l"+libName)
611 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
612 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
613 procMacro.depFlags = append(procMacro.depFlags, "-l"+libName)
614 }
615
616 }
617 })
618
619 var rlibDepFiles RustLibraries
620 for _, dep := range directRlibDeps {
621 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
622 }
623 var dylibDepFiles RustLibraries
624 for _, dep := range directDylibDeps {
625 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
626 }
627 var procMacroDepFiles RustLibraries
628 for _, dep := range directProcMacroDeps {
629 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
630 }
631
632 var staticLibDepFiles android.Paths
633 for _, dep := range directStaticLibDeps {
634 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
635 }
636
637 var sharedLibDepFiles android.Paths
638 for _, dep := range directSharedLibDeps {
639 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
640 }
641
642 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
643 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
644 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
645 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
646 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
647
648 // Dedup exported flags from dependencies
649 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
650 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
651
652 return depPaths
653}
654
655func linkPathFromFilePath(filepath android.Path) string {
656 return strings.Split(filepath.String(), filepath.Base())[0]
657}
658func libNameFromFilePath(filepath android.Path) string {
659 libName := strings.Split(filepath.Base(), filepath.Ext())[0]
Ivan Lozano52767be2019-10-18 14:49:46 -0700660 if strings.HasPrefix(libName, "lib") {
661 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662 }
663 return libName
664}
665func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
666 ctx := &depsContext{
667 BottomUpMutatorContext: actx,
668 moduleContextImpl: moduleContextImpl{
669 mod: mod,
670 },
671 }
672 ctx.ctx = ctx
673
674 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700675 commonDepVariations := []blueprint.Variation{}
676 commonDepVariations = append(commonDepVariations,
677 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700679 commonDepVariations = append(commonDepVariations,
680 blueprint.Variation{Mutator: "image", Variation: "core"})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700681 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700682
683 actx.AddVariationDependencies(
684 append(commonDepVariations, []blueprint.Variation{
685 {Mutator: "rust_libraries", Variation: "rlib"},
686 {Mutator: "link", Variation: ""}}...),
687 rlibDepTag, deps.Rlibs...)
688 actx.AddVariationDependencies(
689 append(commonDepVariations, []blueprint.Variation{
690 {Mutator: "rust_libraries", Variation: "dylib"},
691 {Mutator: "link", Variation: ""}}...),
692 dylibDepTag, deps.Dylibs...)
693
694 actx.AddVariationDependencies(append(commonDepVariations,
695 blueprint.Variation{Mutator: "link", Variation: "shared"}),
696 cc.SharedDepTag, deps.SharedLibs...)
697 actx.AddVariationDependencies(append(commonDepVariations,
698 blueprint.Variation{Mutator: "link", Variation: "static"}),
699 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700700
Ivan Lozanof1c84332019-09-20 11:00:37 -0700701 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700702 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700703 }
704 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700705 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700706 }
707
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700708 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700709 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700710}
711
712func (mod *Module) Name() string {
713 name := mod.ModuleBase.Name()
714 if p, ok := mod.compiler.(interface {
715 Name(string) string
716 }); ok {
717 name = p.Name(name)
718 }
719 return name
720}
721
722var Bool = proptools.Bool
723var BoolDefault = proptools.BoolDefault
724var String = proptools.String
725var StringPtr = proptools.StringPtr