blob: aa11d80a90db40b5860be978d8b5573d0f252b7d [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{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700221 &TestProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700222 )
223
224 android.InitDefaultsModule(module)
225 return module
226}
227
228func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700229 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700230}
231
Ivan Lozano183a3212019-10-18 14:18:45 -0700232func (mod *Module) CcLibrary() bool {
233 if mod.compiler != nil {
234 if _, ok := mod.compiler.(*libraryDecorator); ok {
235 return true
236 }
237 }
238 return false
239}
240
241func (mod *Module) CcLibraryInterface() bool {
242 if mod.compiler != nil {
243 if _, ok := mod.compiler.(libraryInterface); ok {
244 return true
245 }
246 }
247 return false
248}
249
Ivan Lozano52767be2019-10-18 14:49:46 -0700250func (mod *Module) IncludeDirs(ctx android.BaseModuleContext) android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700251 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700252 if library, ok := mod.compiler.(*libraryDecorator); ok {
253 return android.PathsForSource(ctx, library.Properties.Include_dirs)
Ivan Lozano183a3212019-10-18 14:18:45 -0700254 }
255 }
256 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
257}
258
259func (mod *Module) SetStatic() {
260 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700261 if library, ok := mod.compiler.(libraryInterface); ok {
262 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700263 return
264 }
265 }
266 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
267}
268
269func (mod *Module) SetShared() {
270 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700271 if library, ok := mod.compiler.(libraryInterface); ok {
272 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700273 return
274 }
275 }
276 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
277}
278
279func (mod *Module) SetBuildStubs() {
280 panic("SetBuildStubs not yet implemented for rust modules")
281}
282
283func (mod *Module) SetStubsVersions(string) {
284 panic("SetStubsVersions not yet implemented for rust modules")
285}
286
287func (mod *Module) BuildStaticVariant() bool {
288 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700289 if library, ok := mod.compiler.(libraryInterface); ok {
290 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700291 }
292 }
293 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
294}
295
296func (mod *Module) BuildSharedVariant() bool {
297 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700298 if library, ok := mod.compiler.(libraryInterface); ok {
299 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700300 }
301 }
302 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
303}
304
305// Rust module deps don't have a link order (?)
306func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
307
308func (mod *Module) GetDepsInLinkOrder() []android.Path {
309 return []android.Path{}
310}
311
312func (mod *Module) GetStaticVariant() cc.LinkableInterface {
313 return nil
314}
315
316func (mod *Module) Module() android.Module {
317 return mod
318}
319
320func (mod *Module) StubsVersions() []string {
321 // For now, Rust has no stubs versions.
322 if mod.compiler != nil {
323 if _, ok := mod.compiler.(*libraryDecorator); ok {
324 return []string{}
325 }
326 }
327 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
328}
329
330func (mod *Module) OutputFile() android.OptionalPath {
331 return mod.outputFile
332}
333
334func (mod *Module) InRecovery() bool {
335 // For now, Rust has no notion of the recovery image
336 return false
337}
338func (mod *Module) HasStaticVariant() bool {
339 if mod.GetStaticVariant() != nil {
340 return true
341 }
342 return false
343}
344
345var _ cc.LinkableInterface = (*Module)(nil)
346
Ivan Lozanoffee3342019-08-27 12:03:00 -0700347func (mod *Module) Init() android.Module {
348 mod.AddProperties(&mod.Properties)
349
350 if mod.compiler != nil {
351 mod.AddProperties(mod.compiler.compilerProps()...)
352 }
353 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
354
355 android.InitDefaultableModule(mod)
356
Ivan Lozanode252912019-09-06 15:29:52 -0700357 // Explicitly disable unsupported targets.
358 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
359 disableTargets := struct {
360 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700361 Linux_bionic struct {
362 Enabled *bool
363 }
364 }
365 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700366 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
367
368 ctx.AppendProperties(&disableTargets)
369 })
370
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371 return mod
372}
373
374func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
375 return &Module{
376 hod: hod,
377 multilib: multilib,
378 }
379}
380func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
381 module := newBaseModule(hod, multilib)
382 return module
383}
384
385type ModuleContext interface {
386 android.ModuleContext
387 ModuleContextIntf
388}
389
390type BaseModuleContext interface {
391 android.BaseModuleContext
392 ModuleContextIntf
393}
394
395type DepsContext interface {
396 android.BottomUpMutatorContext
397 ModuleContextIntf
398}
399
400type ModuleContextIntf interface {
401 toolchain() config.Toolchain
402 baseModuleName() string
403 CrateName() string
404}
405
406type depsContext struct {
407 android.BottomUpMutatorContext
408 moduleContextImpl
409}
410
411type moduleContext struct {
412 android.ModuleContext
413 moduleContextImpl
414}
415
416type moduleContextImpl struct {
417 mod *Module
418 ctx BaseModuleContext
419}
420
421func (ctx *moduleContextImpl) toolchain() config.Toolchain {
422 return ctx.mod.toolchain(ctx.ctx)
423}
424
425func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
426 if mod.cachedToolchain == nil {
427 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
428 }
429 return mod.cachedToolchain
430}
431
432func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
433}
434
435func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
436 ctx := &moduleContext{
437 ModuleContext: actx,
438 moduleContextImpl: moduleContextImpl{
439 mod: mod,
440 },
441 }
442 ctx.ctx = ctx
443
444 toolchain := mod.toolchain(ctx)
445
446 if !toolchain.Supported() {
447 // This toolchain's unsupported, there's nothing to do for this mod.
448 return
449 }
450
451 deps := mod.depsToPaths(ctx)
452 flags := Flags{
453 Toolchain: toolchain,
454 }
455
456 if mod.compiler != nil {
457 flags = mod.compiler.compilerFlags(ctx, flags)
458 outputFile := mod.compiler.compile(ctx, flags, deps)
459 mod.outputFile = android.OptionalPathForPath(outputFile)
460 mod.compiler.install(ctx, mod.outputFile.Path())
461 }
462}
463
464func (mod *Module) deps(ctx DepsContext) Deps {
465 deps := Deps{}
466
467 if mod.compiler != nil {
468 deps = mod.compiler.compilerDeps(ctx, deps)
469 }
470
471 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
472 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
473 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
474 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
475 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
476
477 return deps
478
479}
480
481func (ctx *moduleContextImpl) baseModuleName() string {
482 return ctx.mod.ModuleBase.BaseModuleName()
483}
484
485func (ctx *moduleContextImpl) CrateName() string {
486 return ctx.mod.CrateName()
487}
488
489type dependencyTag struct {
490 blueprint.BaseDependencyTag
491 name string
492 library bool
493 proc_macro bool
494}
495
496var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700497 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
498 dylibDepTag = dependencyTag{name: "dylib", library: true}
499 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
500 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700501)
502
503func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
504 var depPaths PathDeps
505
506 directRlibDeps := []*Module{}
507 directDylibDeps := []*Module{}
508 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700509 directSharedLibDeps := [](cc.LinkableInterface){}
510 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700511
512 ctx.VisitDirectDeps(func(dep android.Module) {
513 depName := ctx.OtherModuleName(dep)
514 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700515 if rustDep, ok := dep.(*Module); ok {
516 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700517
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518 linkFile := rustDep.outputFile
519 if !linkFile.Valid() {
520 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
521 }
522
523 switch depTag {
524 case dylibDepTag:
525 dylib, ok := rustDep.compiler.(libraryInterface)
526 if !ok || !dylib.dylib() {
527 ctx.ModuleErrorf("mod %q not an dylib library", depName)
528 return
529 }
530 directDylibDeps = append(directDylibDeps, rustDep)
531 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
532 case rlibDepTag:
533 rlib, ok := rustDep.compiler.(libraryInterface)
534 if !ok || !rlib.rlib() {
535 ctx.ModuleErrorf("mod %q not an rlib library", depName)
536 return
537 }
538 directRlibDeps = append(directRlibDeps, rustDep)
539 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
540 case procMacroDepTag:
541 directProcMacroDeps = append(directProcMacroDeps, rustDep)
542 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
543 }
544
545 //Append the dependencies exportedDirs
546 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
547 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
548 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700549 }
550
551 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
552 // This can be probably be refactored by defining a common exporter interface similar to cc's
553 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
554 linkDir := linkPathFromFilePath(linkFile.Path())
555 if lib, ok := mod.compiler.(*libraryDecorator); ok {
556 lib.linkDirs = append(lib.linkDirs, linkDir)
557 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
558 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
559 }
560 }
561
Ivan Lozano52767be2019-10-18 14:49:46 -0700562 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700563
Ivan Lozano52767be2019-10-18 14:49:46 -0700564 if ccDep, ok := dep.(cc.LinkableInterface); ok {
565 //Handle C dependencies
566 if _, ok := ccDep.(*Module); !ok {
567 if ccDep.Module().Target().Os != ctx.Os() {
568 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
569 return
570 }
571 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
572 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
573 return
574 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700575 }
576
Ivan Lozanoffee3342019-08-27 12:03:00 -0700577 linkFile := ccDep.OutputFile()
578 linkPath := linkPathFromFilePath(linkFile.Path())
579 libName := libNameFromFilePath(linkFile.Path())
580 if !linkFile.Valid() {
581 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
582 }
583
584 exportDep := false
585
586 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700587 case cc.StaticDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700588 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
589 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
590 directStaticLibDeps = append(directStaticLibDeps, ccDep)
591 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700592 case cc.SharedDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
594 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
595 directSharedLibDeps = append(directSharedLibDeps, ccDep)
596 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
597 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700598 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700599 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700600 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700601 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602 }
603
604 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700605 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606 lib.linkDirs = append(lib.linkDirs, linkPath)
607 lib.depFlags = append(lib.depFlags, "-l"+libName)
608 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
609 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
610 procMacro.depFlags = append(procMacro.depFlags, "-l"+libName)
611 }
612
613 }
614 })
615
616 var rlibDepFiles RustLibraries
617 for _, dep := range directRlibDeps {
618 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
619 }
620 var dylibDepFiles RustLibraries
621 for _, dep := range directDylibDeps {
622 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
623 }
624 var procMacroDepFiles RustLibraries
625 for _, dep := range directProcMacroDeps {
626 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
627 }
628
629 var staticLibDepFiles android.Paths
630 for _, dep := range directStaticLibDeps {
631 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
632 }
633
634 var sharedLibDepFiles android.Paths
635 for _, dep := range directSharedLibDeps {
636 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
637 }
638
639 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
640 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
641 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
642 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
643 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
644
645 // Dedup exported flags from dependencies
646 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
647 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
648
649 return depPaths
650}
651
652func linkPathFromFilePath(filepath android.Path) string {
653 return strings.Split(filepath.String(), filepath.Base())[0]
654}
655func libNameFromFilePath(filepath android.Path) string {
656 libName := strings.Split(filepath.Base(), filepath.Ext())[0]
Ivan Lozano52767be2019-10-18 14:49:46 -0700657 if strings.HasPrefix(libName, "lib") {
658 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659 }
660 return libName
661}
662func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
663 ctx := &depsContext{
664 BottomUpMutatorContext: actx,
665 moduleContextImpl: moduleContextImpl{
666 mod: mod,
667 },
668 }
669 ctx.ctx = ctx
670
671 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700672 commonDepVariations := []blueprint.Variation{}
673 commonDepVariations = append(commonDepVariations,
674 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700676 commonDepVariations = append(commonDepVariations,
677 blueprint.Variation{Mutator: "image", Variation: "core"})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700679
680 actx.AddVariationDependencies(
681 append(commonDepVariations, []blueprint.Variation{
682 {Mutator: "rust_libraries", Variation: "rlib"},
683 {Mutator: "link", Variation: ""}}...),
684 rlibDepTag, deps.Rlibs...)
685 actx.AddVariationDependencies(
686 append(commonDepVariations, []blueprint.Variation{
687 {Mutator: "rust_libraries", Variation: "dylib"},
688 {Mutator: "link", Variation: ""}}...),
689 dylibDepTag, deps.Dylibs...)
690
691 actx.AddVariationDependencies(append(commonDepVariations,
692 blueprint.Variation{Mutator: "link", Variation: "shared"}),
693 cc.SharedDepTag, deps.SharedLibs...)
694 actx.AddVariationDependencies(append(commonDepVariations,
695 blueprint.Variation{Mutator: "link", Variation: "static"}),
696 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700697
Ivan Lozanof1c84332019-09-20 11:00:37 -0700698 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700699 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700700 }
701 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700702 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700703 }
704
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700705 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700706 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700707}
708
709func (mod *Module) Name() string {
710 name := mod.ModuleBase.Name()
711 if p, ok := mod.compiler.(interface {
712 Name(string) string
713 }); ok {
714 name = p.Name(name)
715 }
716 return name
717}
718
719var Bool = proptools.Bool
720var BoolDefault = proptools.BoolDefault
721var String = proptools.String
722var StringPtr = proptools.StringPtr