blob: 0eab8d222c9e3be6a11d2a9442188634bfe288ab [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
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800229 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700230 install(ctx ModuleContext, path android.Path)
231 relativeInstallPath() string
232}
233
234func defaultsFactory() android.Module {
235 return DefaultsFactory()
236}
237
238type Defaults struct {
239 android.ModuleBase
240 android.DefaultsModuleBase
241}
242
243func DefaultsFactory(props ...interface{}) android.Module {
244 module := &Defaults{}
245
246 module.AddProperties(props...)
247 module.AddProperties(
248 &BaseProperties{},
249 &BaseCompilerProperties{},
250 &BinaryCompilerProperties{},
251 &LibraryCompilerProperties{},
252 &ProcMacroCompilerProperties{},
253 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700254 &TestProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255 )
256
257 android.InitDefaultsModule(module)
258 return module
259}
260
261func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700262 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700263}
264
Ivan Lozano183a3212019-10-18 14:18:45 -0700265func (mod *Module) CcLibrary() bool {
266 if mod.compiler != nil {
267 if _, ok := mod.compiler.(*libraryDecorator); ok {
268 return true
269 }
270 }
271 return false
272}
273
274func (mod *Module) CcLibraryInterface() bool {
275 if mod.compiler != nil {
276 if _, ok := mod.compiler.(libraryInterface); ok {
277 return true
278 }
279 }
280 return false
281}
282
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800283func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700284 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700285 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800286 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700287 }
288 }
289 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
290}
291
292func (mod *Module) SetStatic() {
293 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700294 if library, ok := mod.compiler.(libraryInterface); ok {
295 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700296 return
297 }
298 }
299 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
300}
301
302func (mod *Module) SetShared() {
303 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700304 if library, ok := mod.compiler.(libraryInterface); ok {
305 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700306 return
307 }
308 }
309 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
310}
311
312func (mod *Module) SetBuildStubs() {
313 panic("SetBuildStubs not yet implemented for rust modules")
314}
315
316func (mod *Module) SetStubsVersions(string) {
317 panic("SetStubsVersions not yet implemented for rust modules")
318}
319
320func (mod *Module) BuildStaticVariant() bool {
321 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 if library, ok := mod.compiler.(libraryInterface); ok {
323 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700324 }
325 }
326 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
327}
328
329func (mod *Module) BuildSharedVariant() bool {
330 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700331 if library, ok := mod.compiler.(libraryInterface); ok {
332 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700333 }
334 }
335 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
336}
337
338// Rust module deps don't have a link order (?)
339func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
340
341func (mod *Module) GetDepsInLinkOrder() []android.Path {
342 return []android.Path{}
343}
344
345func (mod *Module) GetStaticVariant() cc.LinkableInterface {
346 return nil
347}
348
349func (mod *Module) Module() android.Module {
350 return mod
351}
352
353func (mod *Module) StubsVersions() []string {
354 // For now, Rust has no stubs versions.
355 if mod.compiler != nil {
356 if _, ok := mod.compiler.(*libraryDecorator); ok {
357 return []string{}
358 }
359 }
360 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
361}
362
363func (mod *Module) OutputFile() android.OptionalPath {
364 return mod.outputFile
365}
366
367func (mod *Module) InRecovery() bool {
368 // For now, Rust has no notion of the recovery image
369 return false
370}
371func (mod *Module) HasStaticVariant() bool {
372 if mod.GetStaticVariant() != nil {
373 return true
374 }
375 return false
376}
377
378var _ cc.LinkableInterface = (*Module)(nil)
379
Ivan Lozanoffee3342019-08-27 12:03:00 -0700380func (mod *Module) Init() android.Module {
381 mod.AddProperties(&mod.Properties)
382
383 if mod.compiler != nil {
384 mod.AddProperties(mod.compiler.compilerProps()...)
385 }
386 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
387
388 android.InitDefaultableModule(mod)
389
Ivan Lozanode252912019-09-06 15:29:52 -0700390 // Explicitly disable unsupported targets.
391 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
392 disableTargets := struct {
393 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700394 Linux_bionic struct {
395 Enabled *bool
396 }
397 }
398 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700399 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
400
401 ctx.AppendProperties(&disableTargets)
402 })
403
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404 return mod
405}
406
407func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
408 return &Module{
409 hod: hod,
410 multilib: multilib,
411 }
412}
413func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
414 module := newBaseModule(hod, multilib)
415 return module
416}
417
418type ModuleContext interface {
419 android.ModuleContext
420 ModuleContextIntf
421}
422
423type BaseModuleContext interface {
424 android.BaseModuleContext
425 ModuleContextIntf
426}
427
428type DepsContext interface {
429 android.BottomUpMutatorContext
430 ModuleContextIntf
431}
432
433type ModuleContextIntf interface {
434 toolchain() config.Toolchain
435 baseModuleName() string
436 CrateName() string
437}
438
439type depsContext struct {
440 android.BottomUpMutatorContext
441 moduleContextImpl
442}
443
444type moduleContext struct {
445 android.ModuleContext
446 moduleContextImpl
447}
448
449type moduleContextImpl struct {
450 mod *Module
451 ctx BaseModuleContext
452}
453
454func (ctx *moduleContextImpl) toolchain() config.Toolchain {
455 return ctx.mod.toolchain(ctx.ctx)
456}
457
458func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
459 if mod.cachedToolchain == nil {
460 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
461 }
462 return mod.cachedToolchain
463}
464
465func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
466}
467
468func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
469 ctx := &moduleContext{
470 ModuleContext: actx,
471 moduleContextImpl: moduleContextImpl{
472 mod: mod,
473 },
474 }
475 ctx.ctx = ctx
476
477 toolchain := mod.toolchain(ctx)
478
479 if !toolchain.Supported() {
480 // This toolchain's unsupported, there's nothing to do for this mod.
481 return
482 }
483
484 deps := mod.depsToPaths(ctx)
485 flags := Flags{
486 Toolchain: toolchain,
487 }
488
489 if mod.compiler != nil {
490 flags = mod.compiler.compilerFlags(ctx, flags)
491 outputFile := mod.compiler.compile(ctx, flags, deps)
492 mod.outputFile = android.OptionalPathForPath(outputFile)
493 mod.compiler.install(ctx, mod.outputFile.Path())
494 }
495}
496
497func (mod *Module) deps(ctx DepsContext) Deps {
498 deps := Deps{}
499
500 if mod.compiler != nil {
501 deps = mod.compiler.compilerDeps(ctx, deps)
502 }
503
504 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
505 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
506 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
507 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
508 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
509
510 return deps
511
512}
513
514func (ctx *moduleContextImpl) baseModuleName() string {
515 return ctx.mod.ModuleBase.BaseModuleName()
516}
517
518func (ctx *moduleContextImpl) CrateName() string {
519 return ctx.mod.CrateName()
520}
521
522type dependencyTag struct {
523 blueprint.BaseDependencyTag
524 name string
525 library bool
526 proc_macro bool
527}
528
529var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700530 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
531 dylibDepTag = dependencyTag{name: "dylib", library: true}
532 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
533 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534)
535
536func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
537 var depPaths PathDeps
538
539 directRlibDeps := []*Module{}
540 directDylibDeps := []*Module{}
541 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700542 directSharedLibDeps := [](cc.LinkableInterface){}
543 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700544
545 ctx.VisitDirectDeps(func(dep android.Module) {
546 depName := ctx.OtherModuleName(dep)
547 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700548 if rustDep, ok := dep.(*Module); ok {
549 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700550
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551 linkFile := rustDep.outputFile
552 if !linkFile.Valid() {
553 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
554 }
555
556 switch depTag {
557 case dylibDepTag:
558 dylib, ok := rustDep.compiler.(libraryInterface)
559 if !ok || !dylib.dylib() {
560 ctx.ModuleErrorf("mod %q not an dylib library", depName)
561 return
562 }
563 directDylibDeps = append(directDylibDeps, rustDep)
564 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
565 case rlibDepTag:
566 rlib, ok := rustDep.compiler.(libraryInterface)
567 if !ok || !rlib.rlib() {
568 ctx.ModuleErrorf("mod %q not an rlib library", depName)
569 return
570 }
571 directRlibDeps = append(directRlibDeps, rustDep)
572 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
573 case procMacroDepTag:
574 directProcMacroDeps = append(directProcMacroDeps, rustDep)
575 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
576 }
577
578 //Append the dependencies exportedDirs
579 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
580 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
581 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700582 }
583
584 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
585 // This can be probably be refactored by defining a common exporter interface similar to cc's
586 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
587 linkDir := linkPathFromFilePath(linkFile.Path())
588 if lib, ok := mod.compiler.(*libraryDecorator); ok {
589 lib.linkDirs = append(lib.linkDirs, linkDir)
590 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
591 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
592 }
593 }
594
Ivan Lozano52767be2019-10-18 14:49:46 -0700595 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700596
Ivan Lozano52767be2019-10-18 14:49:46 -0700597 if ccDep, ok := dep.(cc.LinkableInterface); ok {
598 //Handle C dependencies
599 if _, ok := ccDep.(*Module); !ok {
600 if ccDep.Module().Target().Os != ctx.Os() {
601 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
602 return
603 }
604 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
605 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
606 return
607 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700608 }
609
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610 linkFile := ccDep.OutputFile()
611 linkPath := linkPathFromFilePath(linkFile.Path())
612 libName := libNameFromFilePath(linkFile.Path())
613 if !linkFile.Valid() {
614 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
615 }
616
617 exportDep := false
618
619 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700620 case cc.StaticDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700621 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
622 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
623 directStaticLibDeps = append(directStaticLibDeps, ccDep)
624 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700625 case cc.SharedDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700626 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
627 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
628 directSharedLibDeps = append(directSharedLibDeps, ccDep)
629 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
630 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700631 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700632 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700633 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700634 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700635 }
636
637 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700638 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700639 lib.linkDirs = append(lib.linkDirs, linkPath)
640 lib.depFlags = append(lib.depFlags, "-l"+libName)
641 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
642 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
643 procMacro.depFlags = append(procMacro.depFlags, "-l"+libName)
644 }
645
646 }
647 })
648
649 var rlibDepFiles RustLibraries
650 for _, dep := range directRlibDeps {
651 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
652 }
653 var dylibDepFiles RustLibraries
654 for _, dep := range directDylibDeps {
655 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
656 }
657 var procMacroDepFiles RustLibraries
658 for _, dep := range directProcMacroDeps {
659 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
660 }
661
662 var staticLibDepFiles android.Paths
663 for _, dep := range directStaticLibDeps {
664 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
665 }
666
667 var sharedLibDepFiles android.Paths
668 for _, dep := range directSharedLibDeps {
669 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
670 }
671
672 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
673 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
674 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
675 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
676 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
677
678 // Dedup exported flags from dependencies
679 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
680 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
681
682 return depPaths
683}
684
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800685func (mod *Module) InstallInData() bool {
686 if mod.compiler == nil {
687 return false
688 }
689 return mod.compiler.inData()
690}
691
Ivan Lozanoffee3342019-08-27 12:03:00 -0700692func linkPathFromFilePath(filepath android.Path) string {
693 return strings.Split(filepath.String(), filepath.Base())[0]
694}
695func libNameFromFilePath(filepath android.Path) string {
696 libName := strings.Split(filepath.Base(), filepath.Ext())[0]
Ivan Lozano52767be2019-10-18 14:49:46 -0700697 if strings.HasPrefix(libName, "lib") {
698 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700699 }
700 return libName
701}
702func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
703 ctx := &depsContext{
704 BottomUpMutatorContext: actx,
705 moduleContextImpl: moduleContextImpl{
706 mod: mod,
707 },
708 }
709 ctx.ctx = ctx
710
711 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700712 commonDepVariations := []blueprint.Variation{}
713 commonDepVariations = append(commonDepVariations,
714 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700716 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800717 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700719
720 actx.AddVariationDependencies(
721 append(commonDepVariations, []blueprint.Variation{
722 {Mutator: "rust_libraries", Variation: "rlib"},
723 {Mutator: "link", Variation: ""}}...),
724 rlibDepTag, deps.Rlibs...)
725 actx.AddVariationDependencies(
726 append(commonDepVariations, []blueprint.Variation{
727 {Mutator: "rust_libraries", Variation: "dylib"},
728 {Mutator: "link", Variation: ""}}...),
729 dylibDepTag, deps.Dylibs...)
730
731 actx.AddVariationDependencies(append(commonDepVariations,
732 blueprint.Variation{Mutator: "link", Variation: "shared"}),
733 cc.SharedDepTag, deps.SharedLibs...)
734 actx.AddVariationDependencies(append(commonDepVariations,
735 blueprint.Variation{Mutator: "link", Variation: "static"}),
736 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700737
Ivan Lozanof1c84332019-09-20 11:00:37 -0700738 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700739 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700740 }
741 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700742 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700743 }
744
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700745 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700746 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700747}
748
749func (mod *Module) Name() string {
750 name := mod.ModuleBase.Name()
751 if p, ok := mod.compiler.(interface {
752 Name(string) string
753 }); ok {
754 name = p.Name(name)
755 }
756 return name
757}
758
759var Bool = proptools.Bool
760var BoolDefault = proptools.BoolDefault
761var String = proptools.String
762var StringPtr = proptools.StringPtr