blob: 096f7b68451041b93e5ef1fb0a953e7df1c6b310 [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
Ivan Lozano2b262972019-11-21 12:30:50 -080092func (mod *Module) NonCcVariants() bool {
93 if mod.compiler != nil {
94 if library, ok := mod.compiler.(libraryInterface); ok {
95 if library.buildRlib() || library.buildDylib() {
96 return true
97 } else {
98 return false
99 }
100 }
101 }
102 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
103}
104
Ivan Lozano52767be2019-10-18 14:49:46 -0700105func (mod *Module) ApiLevel() string {
106 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
107}
108
109func (mod *Module) Static() bool {
110 if mod.compiler != nil {
111 if library, ok := mod.compiler.(libraryInterface); ok {
112 return library.static()
113 }
114 }
115 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
116}
117
118func (mod *Module) Shared() bool {
119 if mod.compiler != nil {
120 if library, ok := mod.compiler.(libraryInterface); ok {
121 return library.static()
122 }
123 }
124 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
125}
126
127func (mod *Module) Toc() android.OptionalPath {
128 if mod.compiler != nil {
129 if _, ok := mod.compiler.(libraryInterface); ok {
130 return android.OptionalPath{}
131 }
132 }
133 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
134}
135
136func (mod *Module) OnlyInRecovery() bool {
137 return false
138}
139
140func (mod *Module) UseVndk() bool {
141 return false
142}
143
144func (mod *Module) MustUseVendorVariant() bool {
145 return false
146}
147
148func (mod *Module) IsVndk() bool {
149 return false
150}
151
152func (mod *Module) HasVendorVariant() bool {
153 return false
154}
155
156func (mod *Module) SdkVersion() string {
157 return ""
158}
159
160func (mod *Module) ToolchainLibrary() bool {
161 return false
162}
163
164func (mod *Module) NdkPrebuiltStl() bool {
165 return false
166}
167
168func (mod *Module) StubDecorator() bool {
169 return false
170}
171
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172type Deps struct {
173 Dylibs []string
174 Rlibs []string
175 ProcMacros []string
176 SharedLibs []string
177 StaticLibs []string
178
179 CrtBegin, CrtEnd string
180}
181
182type PathDeps struct {
183 DyLibs RustLibraries
184 RLibs RustLibraries
185 SharedLibs android.Paths
186 StaticLibs android.Paths
187 ProcMacros RustLibraries
188 linkDirs []string
189 depFlags []string
190 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700191
192 CrtBegin android.OptionalPath
193 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700194}
195
196type RustLibraries []RustLibrary
197
198type RustLibrary struct {
199 Path android.Path
200 CrateName string
201}
202
203type compiler interface {
204 compilerFlags(ctx ModuleContext, flags Flags) Flags
205 compilerProps() []interface{}
206 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
207 compilerDeps(ctx DepsContext, deps Deps) Deps
208 crateName() string
209
210 install(ctx ModuleContext, path android.Path)
211 relativeInstallPath() string
212}
213
214func defaultsFactory() android.Module {
215 return DefaultsFactory()
216}
217
218type Defaults struct {
219 android.ModuleBase
220 android.DefaultsModuleBase
221}
222
223func DefaultsFactory(props ...interface{}) android.Module {
224 module := &Defaults{}
225
226 module.AddProperties(props...)
227 module.AddProperties(
228 &BaseProperties{},
229 &BaseCompilerProperties{},
230 &BinaryCompilerProperties{},
231 &LibraryCompilerProperties{},
232 &ProcMacroCompilerProperties{},
233 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700234 &TestProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235 )
236
237 android.InitDefaultsModule(module)
238 return module
239}
240
241func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700242 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243}
244
Ivan Lozano183a3212019-10-18 14:18:45 -0700245func (mod *Module) CcLibrary() bool {
246 if mod.compiler != nil {
247 if _, ok := mod.compiler.(*libraryDecorator); ok {
248 return true
249 }
250 }
251 return false
252}
253
254func (mod *Module) CcLibraryInterface() bool {
255 if mod.compiler != nil {
256 if _, ok := mod.compiler.(libraryInterface); ok {
257 return true
258 }
259 }
260 return false
261}
262
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800263func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700264 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700265 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800266 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700267 }
268 }
269 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
270}
271
272func (mod *Module) SetStatic() {
273 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700274 if library, ok := mod.compiler.(libraryInterface); ok {
275 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700276 return
277 }
278 }
279 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
280}
281
282func (mod *Module) SetShared() {
283 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700284 if library, ok := mod.compiler.(libraryInterface); ok {
285 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700286 return
287 }
288 }
289 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
290}
291
292func (mod *Module) SetBuildStubs() {
293 panic("SetBuildStubs not yet implemented for rust modules")
294}
295
296func (mod *Module) SetStubsVersions(string) {
297 panic("SetStubsVersions not yet implemented for rust modules")
298}
299
300func (mod *Module) BuildStaticVariant() bool {
301 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700302 if library, ok := mod.compiler.(libraryInterface); ok {
303 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700304 }
305 }
306 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
307}
308
309func (mod *Module) BuildSharedVariant() bool {
310 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700311 if library, ok := mod.compiler.(libraryInterface); ok {
312 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700313 }
314 }
315 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
316}
317
318// Rust module deps don't have a link order (?)
319func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
320
321func (mod *Module) GetDepsInLinkOrder() []android.Path {
322 return []android.Path{}
323}
324
325func (mod *Module) GetStaticVariant() cc.LinkableInterface {
326 return nil
327}
328
329func (mod *Module) Module() android.Module {
330 return mod
331}
332
333func (mod *Module) StubsVersions() []string {
334 // For now, Rust has no stubs versions.
335 if mod.compiler != nil {
336 if _, ok := mod.compiler.(*libraryDecorator); ok {
337 return []string{}
338 }
339 }
340 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
341}
342
343func (mod *Module) OutputFile() android.OptionalPath {
344 return mod.outputFile
345}
346
347func (mod *Module) InRecovery() bool {
348 // For now, Rust has no notion of the recovery image
349 return false
350}
351func (mod *Module) HasStaticVariant() bool {
352 if mod.GetStaticVariant() != nil {
353 return true
354 }
355 return false
356}
357
358var _ cc.LinkableInterface = (*Module)(nil)
359
Ivan Lozanoffee3342019-08-27 12:03:00 -0700360func (mod *Module) Init() android.Module {
361 mod.AddProperties(&mod.Properties)
362
363 if mod.compiler != nil {
364 mod.AddProperties(mod.compiler.compilerProps()...)
365 }
366 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
367
368 android.InitDefaultableModule(mod)
369
Ivan Lozanode252912019-09-06 15:29:52 -0700370 // Explicitly disable unsupported targets.
371 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
372 disableTargets := struct {
373 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700374 Linux_bionic struct {
375 Enabled *bool
376 }
377 }
378 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700379 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
380
381 ctx.AppendProperties(&disableTargets)
382 })
383
Ivan Lozanoffee3342019-08-27 12:03:00 -0700384 return mod
385}
386
387func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
388 return &Module{
389 hod: hod,
390 multilib: multilib,
391 }
392}
393func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
394 module := newBaseModule(hod, multilib)
395 return module
396}
397
398type ModuleContext interface {
399 android.ModuleContext
400 ModuleContextIntf
401}
402
403type BaseModuleContext interface {
404 android.BaseModuleContext
405 ModuleContextIntf
406}
407
408type DepsContext interface {
409 android.BottomUpMutatorContext
410 ModuleContextIntf
411}
412
413type ModuleContextIntf interface {
414 toolchain() config.Toolchain
415 baseModuleName() string
416 CrateName() string
417}
418
419type depsContext struct {
420 android.BottomUpMutatorContext
421 moduleContextImpl
422}
423
424type moduleContext struct {
425 android.ModuleContext
426 moduleContextImpl
427}
428
429type moduleContextImpl struct {
430 mod *Module
431 ctx BaseModuleContext
432}
433
434func (ctx *moduleContextImpl) toolchain() config.Toolchain {
435 return ctx.mod.toolchain(ctx.ctx)
436}
437
438func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
439 if mod.cachedToolchain == nil {
440 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
441 }
442 return mod.cachedToolchain
443}
444
445func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
446}
447
448func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
449 ctx := &moduleContext{
450 ModuleContext: actx,
451 moduleContextImpl: moduleContextImpl{
452 mod: mod,
453 },
454 }
455 ctx.ctx = ctx
456
457 toolchain := mod.toolchain(ctx)
458
459 if !toolchain.Supported() {
460 // This toolchain's unsupported, there's nothing to do for this mod.
461 return
462 }
463
464 deps := mod.depsToPaths(ctx)
465 flags := Flags{
466 Toolchain: toolchain,
467 }
468
469 if mod.compiler != nil {
470 flags = mod.compiler.compilerFlags(ctx, flags)
471 outputFile := mod.compiler.compile(ctx, flags, deps)
472 mod.outputFile = android.OptionalPathForPath(outputFile)
473 mod.compiler.install(ctx, mod.outputFile.Path())
474 }
475}
476
477func (mod *Module) deps(ctx DepsContext) Deps {
478 deps := Deps{}
479
480 if mod.compiler != nil {
481 deps = mod.compiler.compilerDeps(ctx, deps)
482 }
483
484 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
485 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
486 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
487 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
488 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
489
490 return deps
491
492}
493
494func (ctx *moduleContextImpl) baseModuleName() string {
495 return ctx.mod.ModuleBase.BaseModuleName()
496}
497
498func (ctx *moduleContextImpl) CrateName() string {
499 return ctx.mod.CrateName()
500}
501
502type dependencyTag struct {
503 blueprint.BaseDependencyTag
504 name string
505 library bool
506 proc_macro bool
507}
508
509var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700510 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
511 dylibDepTag = dependencyTag{name: "dylib", library: true}
512 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
513 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700514)
515
516func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
517 var depPaths PathDeps
518
519 directRlibDeps := []*Module{}
520 directDylibDeps := []*Module{}
521 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700522 directSharedLibDeps := [](cc.LinkableInterface){}
523 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700524
525 ctx.VisitDirectDeps(func(dep android.Module) {
526 depName := ctx.OtherModuleName(dep)
527 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700528 if rustDep, ok := dep.(*Module); ok {
529 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700530
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531 linkFile := rustDep.outputFile
532 if !linkFile.Valid() {
533 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
534 }
535
536 switch depTag {
537 case dylibDepTag:
538 dylib, ok := rustDep.compiler.(libraryInterface)
539 if !ok || !dylib.dylib() {
540 ctx.ModuleErrorf("mod %q not an dylib library", depName)
541 return
542 }
543 directDylibDeps = append(directDylibDeps, rustDep)
544 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
545 case rlibDepTag:
546 rlib, ok := rustDep.compiler.(libraryInterface)
547 if !ok || !rlib.rlib() {
548 ctx.ModuleErrorf("mod %q not an rlib library", depName)
549 return
550 }
551 directRlibDeps = append(directRlibDeps, rustDep)
552 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
553 case procMacroDepTag:
554 directProcMacroDeps = append(directProcMacroDeps, rustDep)
555 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
556 }
557
558 //Append the dependencies exportedDirs
559 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
560 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
561 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700562 }
563
564 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
565 // This can be probably be refactored by defining a common exporter interface similar to cc's
566 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
567 linkDir := linkPathFromFilePath(linkFile.Path())
568 if lib, ok := mod.compiler.(*libraryDecorator); ok {
569 lib.linkDirs = append(lib.linkDirs, linkDir)
570 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
571 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
572 }
573 }
574
Ivan Lozano52767be2019-10-18 14:49:46 -0700575 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700576
Ivan Lozano52767be2019-10-18 14:49:46 -0700577 if ccDep, ok := dep.(cc.LinkableInterface); ok {
578 //Handle C dependencies
579 if _, ok := ccDep.(*Module); !ok {
580 if ccDep.Module().Target().Os != ctx.Os() {
581 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
582 return
583 }
584 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
585 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
586 return
587 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700588 }
589
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590 linkFile := ccDep.OutputFile()
591 linkPath := linkPathFromFilePath(linkFile.Path())
592 libName := libNameFromFilePath(linkFile.Path())
593 if !linkFile.Valid() {
594 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
595 }
596
597 exportDep := false
598
599 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700600 case cc.StaticDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700601 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
602 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
603 directStaticLibDeps = append(directStaticLibDeps, ccDep)
604 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700605 case cc.SharedDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
607 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
608 directSharedLibDeps = append(directSharedLibDeps, ccDep)
609 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
610 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700611 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700612 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700613 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700614 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615 }
616
617 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700618 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700619 lib.linkDirs = append(lib.linkDirs, linkPath)
620 lib.depFlags = append(lib.depFlags, "-l"+libName)
621 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
622 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
623 procMacro.depFlags = append(procMacro.depFlags, "-l"+libName)
624 }
625
626 }
627 })
628
629 var rlibDepFiles RustLibraries
630 for _, dep := range directRlibDeps {
631 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
632 }
633 var dylibDepFiles RustLibraries
634 for _, dep := range directDylibDeps {
635 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
636 }
637 var procMacroDepFiles RustLibraries
638 for _, dep := range directProcMacroDeps {
639 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
640 }
641
642 var staticLibDepFiles android.Paths
643 for _, dep := range directStaticLibDeps {
644 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
645 }
646
647 var sharedLibDepFiles android.Paths
648 for _, dep := range directSharedLibDeps {
649 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
650 }
651
652 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
653 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
654 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
655 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
656 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
657
658 // Dedup exported flags from dependencies
659 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
660 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
661
662 return depPaths
663}
664
665func linkPathFromFilePath(filepath android.Path) string {
666 return strings.Split(filepath.String(), filepath.Base())[0]
667}
668func libNameFromFilePath(filepath android.Path) string {
669 libName := strings.Split(filepath.Base(), filepath.Ext())[0]
Ivan Lozano52767be2019-10-18 14:49:46 -0700670 if strings.HasPrefix(libName, "lib") {
671 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700672 }
673 return libName
674}
675func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
676 ctx := &depsContext{
677 BottomUpMutatorContext: actx,
678 moduleContextImpl: moduleContextImpl{
679 mod: mod,
680 },
681 }
682 ctx.ctx = ctx
683
684 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700685 commonDepVariations := []blueprint.Variation{}
686 commonDepVariations = append(commonDepVariations,
687 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700688 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700689 commonDepVariations = append(commonDepVariations,
690 blueprint.Variation{Mutator: "image", Variation: "core"})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700692
693 actx.AddVariationDependencies(
694 append(commonDepVariations, []blueprint.Variation{
695 {Mutator: "rust_libraries", Variation: "rlib"},
696 {Mutator: "link", Variation: ""}}...),
697 rlibDepTag, deps.Rlibs...)
698 actx.AddVariationDependencies(
699 append(commonDepVariations, []blueprint.Variation{
700 {Mutator: "rust_libraries", Variation: "dylib"},
701 {Mutator: "link", Variation: ""}}...),
702 dylibDepTag, deps.Dylibs...)
703
704 actx.AddVariationDependencies(append(commonDepVariations,
705 blueprint.Variation{Mutator: "link", Variation: "shared"}),
706 cc.SharedDepTag, deps.SharedLibs...)
707 actx.AddVariationDependencies(append(commonDepVariations,
708 blueprint.Variation{Mutator: "link", Variation: "static"}),
709 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700710
Ivan Lozanof1c84332019-09-20 11:00:37 -0700711 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700712 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700713 }
714 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700715 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700716 }
717
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700718 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700719 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700720}
721
722func (mod *Module) Name() string {
723 name := mod.ModuleBase.Name()
724 if p, ok := mod.compiler.(interface {
725 Name(string) string
726 }); ok {
727 name = p.Name(name)
728 }
729 return name
730}
731
732var Bool = proptools.Bool
733var BoolDefault = proptools.BoolDefault
734var String = proptools.String
735var StringPtr = proptools.StringPtr