blob: a3266f7a8c4beb7379a8d08994c610f2088f2e0b [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
Ivan Lozano2b262972019-11-21 12:30:50 -0800111func (mod *Module) NonCcVariants() bool {
112 if mod.compiler != nil {
113 if library, ok := mod.compiler.(libraryInterface); ok {
114 if library.buildRlib() || library.buildDylib() {
115 return true
116 } else {
117 return false
118 }
119 }
120 }
121 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
122}
123
Ivan Lozano52767be2019-10-18 14:49:46 -0700124func (mod *Module) ApiLevel() string {
125 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
126}
127
128func (mod *Module) Static() bool {
129 if mod.compiler != nil {
130 if library, ok := mod.compiler.(libraryInterface); ok {
131 return library.static()
132 }
133 }
134 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
135}
136
137func (mod *Module) Shared() bool {
138 if mod.compiler != nil {
139 if library, ok := mod.compiler.(libraryInterface); ok {
140 return library.static()
141 }
142 }
143 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
144}
145
146func (mod *Module) Toc() android.OptionalPath {
147 if mod.compiler != nil {
148 if _, ok := mod.compiler.(libraryInterface); ok {
149 return android.OptionalPath{}
150 }
151 }
152 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
153}
154
155func (mod *Module) OnlyInRecovery() bool {
156 return false
157}
158
159func (mod *Module) UseVndk() bool {
160 return false
161}
162
163func (mod *Module) MustUseVendorVariant() bool {
164 return false
165}
166
167func (mod *Module) IsVndk() bool {
168 return false
169}
170
171func (mod *Module) HasVendorVariant() bool {
172 return false
173}
174
175func (mod *Module) SdkVersion() string {
176 return ""
177}
178
179func (mod *Module) ToolchainLibrary() bool {
180 return false
181}
182
183func (mod *Module) NdkPrebuiltStl() bool {
184 return false
185}
186
187func (mod *Module) StubDecorator() bool {
188 return false
189}
190
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191type Deps struct {
192 Dylibs []string
193 Rlibs []string
194 ProcMacros []string
195 SharedLibs []string
196 StaticLibs []string
197
198 CrtBegin, CrtEnd string
199}
200
201type PathDeps struct {
202 DyLibs RustLibraries
203 RLibs RustLibraries
204 SharedLibs android.Paths
205 StaticLibs android.Paths
206 ProcMacros RustLibraries
207 linkDirs []string
208 depFlags []string
209 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700210
211 CrtBegin android.OptionalPath
212 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700213}
214
215type RustLibraries []RustLibrary
216
217type RustLibrary struct {
218 Path android.Path
219 CrateName string
220}
221
222type compiler interface {
223 compilerFlags(ctx ModuleContext, flags Flags) Flags
224 compilerProps() []interface{}
225 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
226 compilerDeps(ctx DepsContext, deps Deps) Deps
227 crateName() string
228
229 install(ctx ModuleContext, path android.Path)
230 relativeInstallPath() string
231}
232
233func defaultsFactory() android.Module {
234 return DefaultsFactory()
235}
236
237type Defaults struct {
238 android.ModuleBase
239 android.DefaultsModuleBase
240}
241
242func DefaultsFactory(props ...interface{}) android.Module {
243 module := &Defaults{}
244
245 module.AddProperties(props...)
246 module.AddProperties(
247 &BaseProperties{},
248 &BaseCompilerProperties{},
249 &BinaryCompilerProperties{},
250 &LibraryCompilerProperties{},
251 &ProcMacroCompilerProperties{},
252 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700253 &TestProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700254 )
255
256 android.InitDefaultsModule(module)
257 return module
258}
259
260func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700261 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700262}
263
Ivan Lozano183a3212019-10-18 14:18:45 -0700264func (mod *Module) CcLibrary() bool {
265 if mod.compiler != nil {
266 if _, ok := mod.compiler.(*libraryDecorator); ok {
267 return true
268 }
269 }
270 return false
271}
272
273func (mod *Module) CcLibraryInterface() bool {
274 if mod.compiler != nil {
275 if _, ok := mod.compiler.(libraryInterface); ok {
276 return true
277 }
278 }
279 return false
280}
281
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800282func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700283 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700284 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800285 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700286 }
287 }
288 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
289}
290
291func (mod *Module) SetStatic() {
292 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700293 if library, ok := mod.compiler.(libraryInterface); ok {
294 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700295 return
296 }
297 }
298 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
299}
300
301func (mod *Module) SetShared() {
302 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700303 if library, ok := mod.compiler.(libraryInterface); ok {
304 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700305 return
306 }
307 }
308 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
309}
310
311func (mod *Module) SetBuildStubs() {
312 panic("SetBuildStubs not yet implemented for rust modules")
313}
314
315func (mod *Module) SetStubsVersions(string) {
316 panic("SetStubsVersions not yet implemented for rust modules")
317}
318
319func (mod *Module) BuildStaticVariant() bool {
320 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700321 if library, ok := mod.compiler.(libraryInterface); ok {
322 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700323 }
324 }
325 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
326}
327
328func (mod *Module) BuildSharedVariant() bool {
329 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700330 if library, ok := mod.compiler.(libraryInterface); ok {
331 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700332 }
333 }
334 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
335}
336
337// Rust module deps don't have a link order (?)
338func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
339
340func (mod *Module) GetDepsInLinkOrder() []android.Path {
341 return []android.Path{}
342}
343
344func (mod *Module) GetStaticVariant() cc.LinkableInterface {
345 return nil
346}
347
348func (mod *Module) Module() android.Module {
349 return mod
350}
351
352func (mod *Module) StubsVersions() []string {
353 // For now, Rust has no stubs versions.
354 if mod.compiler != nil {
355 if _, ok := mod.compiler.(*libraryDecorator); ok {
356 return []string{}
357 }
358 }
359 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
360}
361
362func (mod *Module) OutputFile() android.OptionalPath {
363 return mod.outputFile
364}
365
366func (mod *Module) InRecovery() bool {
367 // For now, Rust has no notion of the recovery image
368 return false
369}
370func (mod *Module) HasStaticVariant() bool {
371 if mod.GetStaticVariant() != nil {
372 return true
373 }
374 return false
375}
376
377var _ cc.LinkableInterface = (*Module)(nil)
378
Ivan Lozanoffee3342019-08-27 12:03:00 -0700379func (mod *Module) Init() android.Module {
380 mod.AddProperties(&mod.Properties)
381
382 if mod.compiler != nil {
383 mod.AddProperties(mod.compiler.compilerProps()...)
384 }
385 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
386
387 android.InitDefaultableModule(mod)
388
Ivan Lozanode252912019-09-06 15:29:52 -0700389 // Explicitly disable unsupported targets.
390 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
391 disableTargets := struct {
392 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700393 Linux_bionic struct {
394 Enabled *bool
395 }
396 }
397 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700398 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
399
400 ctx.AppendProperties(&disableTargets)
401 })
402
Ivan Lozanoffee3342019-08-27 12:03:00 -0700403 return mod
404}
405
406func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
407 return &Module{
408 hod: hod,
409 multilib: multilib,
410 }
411}
412func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
413 module := newBaseModule(hod, multilib)
414 return module
415}
416
417type ModuleContext interface {
418 android.ModuleContext
419 ModuleContextIntf
420}
421
422type BaseModuleContext interface {
423 android.BaseModuleContext
424 ModuleContextIntf
425}
426
427type DepsContext interface {
428 android.BottomUpMutatorContext
429 ModuleContextIntf
430}
431
432type ModuleContextIntf interface {
433 toolchain() config.Toolchain
434 baseModuleName() string
435 CrateName() string
436}
437
438type depsContext struct {
439 android.BottomUpMutatorContext
440 moduleContextImpl
441}
442
443type moduleContext struct {
444 android.ModuleContext
445 moduleContextImpl
446}
447
448type moduleContextImpl struct {
449 mod *Module
450 ctx BaseModuleContext
451}
452
453func (ctx *moduleContextImpl) toolchain() config.Toolchain {
454 return ctx.mod.toolchain(ctx.ctx)
455}
456
457func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
458 if mod.cachedToolchain == nil {
459 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
460 }
461 return mod.cachedToolchain
462}
463
464func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
465}
466
467func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
468 ctx := &moduleContext{
469 ModuleContext: actx,
470 moduleContextImpl: moduleContextImpl{
471 mod: mod,
472 },
473 }
474 ctx.ctx = ctx
475
476 toolchain := mod.toolchain(ctx)
477
478 if !toolchain.Supported() {
479 // This toolchain's unsupported, there's nothing to do for this mod.
480 return
481 }
482
483 deps := mod.depsToPaths(ctx)
484 flags := Flags{
485 Toolchain: toolchain,
486 }
487
488 if mod.compiler != nil {
489 flags = mod.compiler.compilerFlags(ctx, flags)
490 outputFile := mod.compiler.compile(ctx, flags, deps)
491 mod.outputFile = android.OptionalPathForPath(outputFile)
492 mod.compiler.install(ctx, mod.outputFile.Path())
493 }
494}
495
496func (mod *Module) deps(ctx DepsContext) Deps {
497 deps := Deps{}
498
499 if mod.compiler != nil {
500 deps = mod.compiler.compilerDeps(ctx, deps)
501 }
502
503 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
504 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
505 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
506 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
507 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
508
509 return deps
510
511}
512
513func (ctx *moduleContextImpl) baseModuleName() string {
514 return ctx.mod.ModuleBase.BaseModuleName()
515}
516
517func (ctx *moduleContextImpl) CrateName() string {
518 return ctx.mod.CrateName()
519}
520
521type dependencyTag struct {
522 blueprint.BaseDependencyTag
523 name string
524 library bool
525 proc_macro bool
526}
527
528var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700529 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
530 dylibDepTag = dependencyTag{name: "dylib", library: true}
531 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
532 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533)
534
535func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
536 var depPaths PathDeps
537
538 directRlibDeps := []*Module{}
539 directDylibDeps := []*Module{}
540 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700541 directSharedLibDeps := [](cc.LinkableInterface){}
542 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543
544 ctx.VisitDirectDeps(func(dep android.Module) {
545 depName := ctx.OtherModuleName(dep)
546 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547 if rustDep, ok := dep.(*Module); ok {
548 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700549
Ivan Lozanoffee3342019-08-27 12:03:00 -0700550 linkFile := rustDep.outputFile
551 if !linkFile.Valid() {
552 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
553 }
554
555 switch depTag {
556 case dylibDepTag:
557 dylib, ok := rustDep.compiler.(libraryInterface)
558 if !ok || !dylib.dylib() {
559 ctx.ModuleErrorf("mod %q not an dylib library", depName)
560 return
561 }
562 directDylibDeps = append(directDylibDeps, rustDep)
563 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
564 case rlibDepTag:
565 rlib, ok := rustDep.compiler.(libraryInterface)
566 if !ok || !rlib.rlib() {
567 ctx.ModuleErrorf("mod %q not an rlib library", depName)
568 return
569 }
570 directRlibDeps = append(directRlibDeps, rustDep)
571 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
572 case procMacroDepTag:
573 directProcMacroDeps = append(directProcMacroDeps, rustDep)
574 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
575 }
576
577 //Append the dependencies exportedDirs
578 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
579 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
580 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581 }
582
583 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
584 // This can be probably be refactored by defining a common exporter interface similar to cc's
585 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
586 linkDir := linkPathFromFilePath(linkFile.Path())
587 if lib, ok := mod.compiler.(*libraryDecorator); ok {
588 lib.linkDirs = append(lib.linkDirs, linkDir)
589 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
590 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
591 }
592 }
593
Ivan Lozano52767be2019-10-18 14:49:46 -0700594 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700595
Ivan Lozano52767be2019-10-18 14:49:46 -0700596 if ccDep, ok := dep.(cc.LinkableInterface); ok {
597 //Handle C dependencies
598 if _, ok := ccDep.(*Module); !ok {
599 if ccDep.Module().Target().Os != ctx.Os() {
600 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
601 return
602 }
603 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
604 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
605 return
606 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700607 }
608
Ivan Lozanoffee3342019-08-27 12:03:00 -0700609 linkFile := ccDep.OutputFile()
610 linkPath := linkPathFromFilePath(linkFile.Path())
611 libName := libNameFromFilePath(linkFile.Path())
612 if !linkFile.Valid() {
613 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
614 }
615
616 exportDep := false
617
618 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700619 case cc.StaticDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700620 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
621 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
622 directStaticLibDeps = append(directStaticLibDeps, ccDep)
623 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700624 case cc.SharedDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700625 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
626 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
627 directSharedLibDeps = append(directSharedLibDeps, ccDep)
628 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
629 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700630 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700631 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700632 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700633 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700634 }
635
636 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700637 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700638 lib.linkDirs = append(lib.linkDirs, linkPath)
639 lib.depFlags = append(lib.depFlags, "-l"+libName)
640 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
641 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
642 procMacro.depFlags = append(procMacro.depFlags, "-l"+libName)
643 }
644
645 }
646 })
647
648 var rlibDepFiles RustLibraries
649 for _, dep := range directRlibDeps {
650 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
651 }
652 var dylibDepFiles RustLibraries
653 for _, dep := range directDylibDeps {
654 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
655 }
656 var procMacroDepFiles RustLibraries
657 for _, dep := range directProcMacroDeps {
658 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
659 }
660
661 var staticLibDepFiles android.Paths
662 for _, dep := range directStaticLibDeps {
663 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
664 }
665
666 var sharedLibDepFiles android.Paths
667 for _, dep := range directSharedLibDeps {
668 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
669 }
670
671 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
672 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
673 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
674 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
675 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
676
677 // Dedup exported flags from dependencies
678 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
679 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
680
681 return depPaths
682}
683
684func linkPathFromFilePath(filepath android.Path) string {
685 return strings.Split(filepath.String(), filepath.Base())[0]
686}
687func libNameFromFilePath(filepath android.Path) string {
688 libName := strings.Split(filepath.Base(), filepath.Ext())[0]
Ivan Lozano52767be2019-10-18 14:49:46 -0700689 if strings.HasPrefix(libName, "lib") {
690 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691 }
692 return libName
693}
694func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
695 ctx := &depsContext{
696 BottomUpMutatorContext: actx,
697 moduleContextImpl: moduleContextImpl{
698 mod: mod,
699 },
700 }
701 ctx.ctx = ctx
702
703 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700704 commonDepVariations := []blueprint.Variation{}
705 commonDepVariations = append(commonDepVariations,
706 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700707 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700708 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800709 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700710 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700711
712 actx.AddVariationDependencies(
713 append(commonDepVariations, []blueprint.Variation{
714 {Mutator: "rust_libraries", Variation: "rlib"},
715 {Mutator: "link", Variation: ""}}...),
716 rlibDepTag, deps.Rlibs...)
717 actx.AddVariationDependencies(
718 append(commonDepVariations, []blueprint.Variation{
719 {Mutator: "rust_libraries", Variation: "dylib"},
720 {Mutator: "link", Variation: ""}}...),
721 dylibDepTag, deps.Dylibs...)
722
723 actx.AddVariationDependencies(append(commonDepVariations,
724 blueprint.Variation{Mutator: "link", Variation: "shared"}),
725 cc.SharedDepTag, deps.SharedLibs...)
726 actx.AddVariationDependencies(append(commonDepVariations,
727 blueprint.Variation{Mutator: "link", Variation: "static"}),
728 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700729
Ivan Lozanof1c84332019-09-20 11:00:37 -0700730 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700731 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700732 }
733 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700734 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700735 }
736
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700737 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700738 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739}
740
741func (mod *Module) Name() string {
742 name := mod.ModuleBase.Name()
743 if p, ok := mod.compiler.(interface {
744 Name(string) string
745 }); ok {
746 name = p.Name(name)
747 }
748 return name
749}
750
751var Bool = proptools.Bool
752var BoolDefault = proptools.BoolDefault
753var String = proptools.String
754var StringPtr = proptools.StringPtr