blob: 26401d11acf21f87a5b126eae8a37c13e0f89dd4 [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
Yifan Hong1b3348d2020-01-21 15:53:22 -080088func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
89 return mod.InRamdisk()
90}
91
Colin Cross7228ecd2019-11-18 16:00:16 -080092func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
93 return mod.InRecovery()
94}
95
96func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
97 return nil
98}
99
100func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
101}
102
Ivan Lozano52767be2019-10-18 14:49:46 -0700103func (mod *Module) BuildStubs() bool {
104 return false
105}
106
107func (mod *Module) HasStubsVariants() bool {
108 return false
109}
110
111func (mod *Module) SelectedStl() string {
112 return ""
113}
114
Ivan Lozano2b262972019-11-21 12:30:50 -0800115func (mod *Module) NonCcVariants() bool {
116 if mod.compiler != nil {
117 if library, ok := mod.compiler.(libraryInterface); ok {
118 if library.buildRlib() || library.buildDylib() {
119 return true
120 } else {
121 return false
122 }
123 }
124 }
125 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
126}
127
Ivan Lozano52767be2019-10-18 14:49:46 -0700128func (mod *Module) ApiLevel() string {
129 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
130}
131
132func (mod *Module) Static() bool {
133 if mod.compiler != nil {
134 if library, ok := mod.compiler.(libraryInterface); ok {
135 return library.static()
136 }
137 }
138 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
139}
140
141func (mod *Module) Shared() bool {
142 if mod.compiler != nil {
143 if library, ok := mod.compiler.(libraryInterface); ok {
144 return library.static()
145 }
146 }
147 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
148}
149
150func (mod *Module) Toc() android.OptionalPath {
151 if mod.compiler != nil {
152 if _, ok := mod.compiler.(libraryInterface); ok {
153 return android.OptionalPath{}
154 }
155 }
156 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
157}
158
Yifan Hong1b3348d2020-01-21 15:53:22 -0800159func (mod *Module) OnlyInRamdisk() bool {
160 return false
161}
162
Ivan Lozano52767be2019-10-18 14:49:46 -0700163func (mod *Module) OnlyInRecovery() bool {
164 return false
165}
166
167func (mod *Module) UseVndk() bool {
168 return false
169}
170
171func (mod *Module) MustUseVendorVariant() bool {
172 return false
173}
174
175func (mod *Module) IsVndk() bool {
176 return false
177}
178
179func (mod *Module) HasVendorVariant() bool {
180 return false
181}
182
183func (mod *Module) SdkVersion() string {
184 return ""
185}
186
187func (mod *Module) ToolchainLibrary() bool {
188 return false
189}
190
191func (mod *Module) NdkPrebuiltStl() bool {
192 return false
193}
194
195func (mod *Module) StubDecorator() bool {
196 return false
197}
198
Ivan Lozanoffee3342019-08-27 12:03:00 -0700199type Deps struct {
200 Dylibs []string
201 Rlibs []string
202 ProcMacros []string
203 SharedLibs []string
204 StaticLibs []string
205
206 CrtBegin, CrtEnd string
207}
208
209type PathDeps struct {
210 DyLibs RustLibraries
211 RLibs RustLibraries
212 SharedLibs android.Paths
213 StaticLibs android.Paths
214 ProcMacros RustLibraries
215 linkDirs []string
216 depFlags []string
217 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700218
219 CrtBegin android.OptionalPath
220 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221}
222
223type RustLibraries []RustLibrary
224
225type RustLibrary struct {
226 Path android.Path
227 CrateName string
228}
229
230type compiler interface {
231 compilerFlags(ctx ModuleContext, flags Flags) Flags
232 compilerProps() []interface{}
233 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
234 compilerDeps(ctx DepsContext, deps Deps) Deps
235 crateName() string
236
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800237 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700238 install(ctx ModuleContext, path android.Path)
239 relativeInstallPath() string
240}
241
242func defaultsFactory() android.Module {
243 return DefaultsFactory()
244}
245
246type Defaults struct {
247 android.ModuleBase
248 android.DefaultsModuleBase
249}
250
251func DefaultsFactory(props ...interface{}) android.Module {
252 module := &Defaults{}
253
254 module.AddProperties(props...)
255 module.AddProperties(
256 &BaseProperties{},
257 &BaseCompilerProperties{},
258 &BinaryCompilerProperties{},
259 &LibraryCompilerProperties{},
260 &ProcMacroCompilerProperties{},
261 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700262 &TestProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700263 )
264
265 android.InitDefaultsModule(module)
266 return module
267}
268
269func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700270 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700271}
272
Ivan Lozano183a3212019-10-18 14:18:45 -0700273func (mod *Module) CcLibrary() bool {
274 if mod.compiler != nil {
275 if _, ok := mod.compiler.(*libraryDecorator); ok {
276 return true
277 }
278 }
279 return false
280}
281
282func (mod *Module) CcLibraryInterface() bool {
283 if mod.compiler != nil {
284 if _, ok := mod.compiler.(libraryInterface); ok {
285 return true
286 }
287 }
288 return false
289}
290
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800291func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700292 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700293 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800294 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700295 }
296 }
297 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
298}
299
300func (mod *Module) SetStatic() {
301 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700302 if library, ok := mod.compiler.(libraryInterface); ok {
303 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700304 return
305 }
306 }
307 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
308}
309
310func (mod *Module) SetShared() {
311 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700312 if library, ok := mod.compiler.(libraryInterface); ok {
313 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700314 return
315 }
316 }
317 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
318}
319
320func (mod *Module) SetBuildStubs() {
321 panic("SetBuildStubs not yet implemented for rust modules")
322}
323
324func (mod *Module) SetStubsVersions(string) {
325 panic("SetStubsVersions not yet implemented for rust modules")
326}
327
328func (mod *Module) BuildStaticVariant() bool {
329 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700330 if library, ok := mod.compiler.(libraryInterface); ok {
331 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700332 }
333 }
334 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
335}
336
337func (mod *Module) BuildSharedVariant() bool {
338 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700339 if library, ok := mod.compiler.(libraryInterface); ok {
340 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700341 }
342 }
343 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
344}
345
346// Rust module deps don't have a link order (?)
347func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
348
349func (mod *Module) GetDepsInLinkOrder() []android.Path {
350 return []android.Path{}
351}
352
353func (mod *Module) GetStaticVariant() cc.LinkableInterface {
354 return nil
355}
356
Jiyong Park83dc74b2020-01-14 18:38:44 +0900357func (mod *Module) AllStaticDeps() []string {
358 // TODO(jiyong): do this for rust?
359 return nil
360}
361
Ivan Lozano183a3212019-10-18 14:18:45 -0700362func (mod *Module) Module() android.Module {
363 return mod
364}
365
366func (mod *Module) StubsVersions() []string {
367 // For now, Rust has no stubs versions.
368 if mod.compiler != nil {
369 if _, ok := mod.compiler.(*libraryDecorator); ok {
370 return []string{}
371 }
372 }
373 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
374}
375
376func (mod *Module) OutputFile() android.OptionalPath {
377 return mod.outputFile
378}
379
380func (mod *Module) InRecovery() bool {
381 // For now, Rust has no notion of the recovery image
382 return false
383}
384func (mod *Module) HasStaticVariant() bool {
385 if mod.GetStaticVariant() != nil {
386 return true
387 }
388 return false
389}
390
391var _ cc.LinkableInterface = (*Module)(nil)
392
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393func (mod *Module) Init() android.Module {
394 mod.AddProperties(&mod.Properties)
395
396 if mod.compiler != nil {
397 mod.AddProperties(mod.compiler.compilerProps()...)
398 }
399 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
400
401 android.InitDefaultableModule(mod)
402
Ivan Lozanode252912019-09-06 15:29:52 -0700403 // Explicitly disable unsupported targets.
404 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
405 disableTargets := struct {
406 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700407 Linux_bionic struct {
408 Enabled *bool
409 }
410 }
411 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700412 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
413
414 ctx.AppendProperties(&disableTargets)
415 })
416
Ivan Lozanoffee3342019-08-27 12:03:00 -0700417 return mod
418}
419
420func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
421 return &Module{
422 hod: hod,
423 multilib: multilib,
424 }
425}
426func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
427 module := newBaseModule(hod, multilib)
428 return module
429}
430
431type ModuleContext interface {
432 android.ModuleContext
433 ModuleContextIntf
434}
435
436type BaseModuleContext interface {
437 android.BaseModuleContext
438 ModuleContextIntf
439}
440
441type DepsContext interface {
442 android.BottomUpMutatorContext
443 ModuleContextIntf
444}
445
446type ModuleContextIntf interface {
447 toolchain() config.Toolchain
448 baseModuleName() string
449 CrateName() string
450}
451
452type depsContext struct {
453 android.BottomUpMutatorContext
454 moduleContextImpl
455}
456
457type moduleContext struct {
458 android.ModuleContext
459 moduleContextImpl
460}
461
462type moduleContextImpl struct {
463 mod *Module
464 ctx BaseModuleContext
465}
466
467func (ctx *moduleContextImpl) toolchain() config.Toolchain {
468 return ctx.mod.toolchain(ctx.ctx)
469}
470
471func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
472 if mod.cachedToolchain == nil {
473 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
474 }
475 return mod.cachedToolchain
476}
477
478func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
479}
480
481func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
482 ctx := &moduleContext{
483 ModuleContext: actx,
484 moduleContextImpl: moduleContextImpl{
485 mod: mod,
486 },
487 }
488 ctx.ctx = ctx
489
490 toolchain := mod.toolchain(ctx)
491
492 if !toolchain.Supported() {
493 // This toolchain's unsupported, there's nothing to do for this mod.
494 return
495 }
496
497 deps := mod.depsToPaths(ctx)
498 flags := Flags{
499 Toolchain: toolchain,
500 }
501
502 if mod.compiler != nil {
503 flags = mod.compiler.compilerFlags(ctx, flags)
504 outputFile := mod.compiler.compile(ctx, flags, deps)
505 mod.outputFile = android.OptionalPathForPath(outputFile)
506 mod.compiler.install(ctx, mod.outputFile.Path())
507 }
508}
509
510func (mod *Module) deps(ctx DepsContext) Deps {
511 deps := Deps{}
512
513 if mod.compiler != nil {
514 deps = mod.compiler.compilerDeps(ctx, deps)
515 }
516
517 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
518 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
519 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
520 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
521 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
522
523 return deps
524
525}
526
527func (ctx *moduleContextImpl) baseModuleName() string {
528 return ctx.mod.ModuleBase.BaseModuleName()
529}
530
531func (ctx *moduleContextImpl) CrateName() string {
532 return ctx.mod.CrateName()
533}
534
535type dependencyTag struct {
536 blueprint.BaseDependencyTag
537 name string
538 library bool
539 proc_macro bool
540}
541
542var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700543 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
544 dylibDepTag = dependencyTag{name: "dylib", library: true}
545 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
546 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547)
548
549func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
550 var depPaths PathDeps
551
552 directRlibDeps := []*Module{}
553 directDylibDeps := []*Module{}
554 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700555 directSharedLibDeps := [](cc.LinkableInterface){}
556 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700557
558 ctx.VisitDirectDeps(func(dep android.Module) {
559 depName := ctx.OtherModuleName(dep)
560 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700561 if rustDep, ok := dep.(*Module); ok {
562 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700563
Ivan Lozanoffee3342019-08-27 12:03:00 -0700564 linkFile := rustDep.outputFile
565 if !linkFile.Valid() {
566 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
567 }
568
569 switch depTag {
570 case dylibDepTag:
571 dylib, ok := rustDep.compiler.(libraryInterface)
572 if !ok || !dylib.dylib() {
573 ctx.ModuleErrorf("mod %q not an dylib library", depName)
574 return
575 }
576 directDylibDeps = append(directDylibDeps, rustDep)
577 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
578 case rlibDepTag:
579 rlib, ok := rustDep.compiler.(libraryInterface)
580 if !ok || !rlib.rlib() {
581 ctx.ModuleErrorf("mod %q not an rlib library", depName)
582 return
583 }
584 directRlibDeps = append(directRlibDeps, rustDep)
585 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
586 case procMacroDepTag:
587 directProcMacroDeps = append(directProcMacroDeps, rustDep)
588 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
589 }
590
591 //Append the dependencies exportedDirs
592 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
593 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
594 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700595 }
596
597 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
598 // This can be probably be refactored by defining a common exporter interface similar to cc's
599 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
600 linkDir := linkPathFromFilePath(linkFile.Path())
601 if lib, ok := mod.compiler.(*libraryDecorator); ok {
602 lib.linkDirs = append(lib.linkDirs, linkDir)
603 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
604 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
605 }
606 }
607
Ivan Lozano52767be2019-10-18 14:49:46 -0700608 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700609
Ivan Lozano52767be2019-10-18 14:49:46 -0700610 if ccDep, ok := dep.(cc.LinkableInterface); ok {
611 //Handle C dependencies
612 if _, ok := ccDep.(*Module); !ok {
613 if ccDep.Module().Target().Os != ctx.Os() {
614 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
615 return
616 }
617 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
618 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
619 return
620 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700621 }
622
Ivan Lozanoffee3342019-08-27 12:03:00 -0700623 linkFile := ccDep.OutputFile()
624 linkPath := linkPathFromFilePath(linkFile.Path())
625 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500626 depFlag := "-l" + libName
627
Ivan Lozanoffee3342019-08-27 12:03:00 -0700628 if !linkFile.Valid() {
629 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
630 }
631
632 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700633 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700634 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500635 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700636 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500637 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700638 directStaticLibDeps = append(directStaticLibDeps, ccDep)
639 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700640 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500641 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700642 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500643 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644 directSharedLibDeps = append(directSharedLibDeps, ccDep)
645 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
646 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700647 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700648 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700649 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700650 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651 }
652
653 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700654 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500656 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
658 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500659 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700660 }
661
662 }
663 })
664
665 var rlibDepFiles RustLibraries
666 for _, dep := range directRlibDeps {
667 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
668 }
669 var dylibDepFiles RustLibraries
670 for _, dep := range directDylibDeps {
671 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
672 }
673 var procMacroDepFiles RustLibraries
674 for _, dep := range directProcMacroDeps {
675 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
676 }
677
678 var staticLibDepFiles android.Paths
679 for _, dep := range directStaticLibDeps {
680 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
681 }
682
683 var sharedLibDepFiles android.Paths
684 for _, dep := range directSharedLibDeps {
685 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
686 }
687
688 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
689 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
690 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
691 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
692 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
693
694 // Dedup exported flags from dependencies
695 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
696 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
697
698 return depPaths
699}
700
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800701func (mod *Module) InstallInData() bool {
702 if mod.compiler == nil {
703 return false
704 }
705 return mod.compiler.inData()
706}
707
Ivan Lozanoffee3342019-08-27 12:03:00 -0700708func linkPathFromFilePath(filepath android.Path) string {
709 return strings.Split(filepath.String(), filepath.Base())[0]
710}
711func libNameFromFilePath(filepath android.Path) string {
712 libName := strings.Split(filepath.Base(), filepath.Ext())[0]
Ivan Lozano52767be2019-10-18 14:49:46 -0700713 if strings.HasPrefix(libName, "lib") {
714 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715 }
716 return libName
717}
718func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
719 ctx := &depsContext{
720 BottomUpMutatorContext: actx,
721 moduleContextImpl: moduleContextImpl{
722 mod: mod,
723 },
724 }
725 ctx.ctx = ctx
726
727 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700728 commonDepVariations := []blueprint.Variation{}
729 commonDepVariations = append(commonDepVariations,
730 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700731 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700732 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800733 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700734 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700735
736 actx.AddVariationDependencies(
737 append(commonDepVariations, []blueprint.Variation{
738 {Mutator: "rust_libraries", Variation: "rlib"},
739 {Mutator: "link", Variation: ""}}...),
740 rlibDepTag, deps.Rlibs...)
741 actx.AddVariationDependencies(
742 append(commonDepVariations, []blueprint.Variation{
743 {Mutator: "rust_libraries", Variation: "dylib"},
744 {Mutator: "link", Variation: ""}}...),
745 dylibDepTag, deps.Dylibs...)
746
747 actx.AddVariationDependencies(append(commonDepVariations,
748 blueprint.Variation{Mutator: "link", Variation: "shared"}),
749 cc.SharedDepTag, deps.SharedLibs...)
750 actx.AddVariationDependencies(append(commonDepVariations,
751 blueprint.Variation{Mutator: "link", Variation: "static"}),
752 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700753
Ivan Lozanof1c84332019-09-20 11:00:37 -0700754 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700755 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700756 }
757 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700758 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700759 }
760
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700761 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700762 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763}
764
765func (mod *Module) Name() string {
766 name := mod.ModuleBase.Name()
767 if p, ok := mod.compiler.(interface {
768 Name(string) string
769 }); ok {
770 name = p.Name(name)
771 }
772 return name
773}
774
775var Bool = proptools.Bool
776var BoolDefault = proptools.BoolDefault
777var String = proptools.String
778var StringPtr = proptools.StringPtr