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