blob: e4f85f0f44372b22552f992575615e6451cd1dd0 [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())
626 if !linkFile.Valid() {
627 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
628 }
629
630 exportDep := false
631
632 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700633 case cc.StaticDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700634 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
635 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
636 directStaticLibDeps = append(directStaticLibDeps, ccDep)
637 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700638 case cc.SharedDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700639 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
640 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
641 directSharedLibDeps = append(directSharedLibDeps, ccDep)
642 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
643 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700644 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700645 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700646 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700647 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648 }
649
650 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700651 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652 lib.linkDirs = append(lib.linkDirs, linkPath)
653 lib.depFlags = append(lib.depFlags, "-l"+libName)
654 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
655 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
656 procMacro.depFlags = append(procMacro.depFlags, "-l"+libName)
657 }
658
659 }
660 })
661
662 var rlibDepFiles RustLibraries
663 for _, dep := range directRlibDeps {
664 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
665 }
666 var dylibDepFiles RustLibraries
667 for _, dep := range directDylibDeps {
668 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
669 }
670 var procMacroDepFiles RustLibraries
671 for _, dep := range directProcMacroDeps {
672 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
673 }
674
675 var staticLibDepFiles android.Paths
676 for _, dep := range directStaticLibDeps {
677 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
678 }
679
680 var sharedLibDepFiles android.Paths
681 for _, dep := range directSharedLibDeps {
682 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
683 }
684
685 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
686 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
687 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
688 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
689 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
690
691 // Dedup exported flags from dependencies
692 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
693 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
694
695 return depPaths
696}
697
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800698func (mod *Module) InstallInData() bool {
699 if mod.compiler == nil {
700 return false
701 }
702 return mod.compiler.inData()
703}
704
Ivan Lozanoffee3342019-08-27 12:03:00 -0700705func linkPathFromFilePath(filepath android.Path) string {
706 return strings.Split(filepath.String(), filepath.Base())[0]
707}
Ivan Lozanod648c432020-02-06 12:05:10 -0500708
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500710 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700711 if strings.HasPrefix(libName, "lib") {
712 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700713 }
714 return libName
715}
Ivan Lozanod648c432020-02-06 12:05:10 -0500716
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
718 ctx := &depsContext{
719 BottomUpMutatorContext: actx,
720 moduleContextImpl: moduleContextImpl{
721 mod: mod,
722 },
723 }
724 ctx.ctx = ctx
725
726 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700727 commonDepVariations := []blueprint.Variation{}
728 commonDepVariations = append(commonDepVariations,
729 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700731 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800732 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700734
735 actx.AddVariationDependencies(
736 append(commonDepVariations, []blueprint.Variation{
737 {Mutator: "rust_libraries", Variation: "rlib"},
738 {Mutator: "link", Variation: ""}}...),
739 rlibDepTag, deps.Rlibs...)
740 actx.AddVariationDependencies(
741 append(commonDepVariations, []blueprint.Variation{
742 {Mutator: "rust_libraries", Variation: "dylib"},
743 {Mutator: "link", Variation: ""}}...),
744 dylibDepTag, deps.Dylibs...)
745
746 actx.AddVariationDependencies(append(commonDepVariations,
747 blueprint.Variation{Mutator: "link", Variation: "shared"}),
748 cc.SharedDepTag, deps.SharedLibs...)
749 actx.AddVariationDependencies(append(commonDepVariations,
750 blueprint.Variation{Mutator: "link", Variation: "static"}),
751 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700752
Ivan Lozanof1c84332019-09-20 11:00:37 -0700753 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700754 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700755 }
756 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700757 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700758 }
759
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700760 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700761 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762}
763
764func (mod *Module) Name() string {
765 name := mod.ModuleBase.Name()
766 if p, ok := mod.compiler.(interface {
767 Name(string) string
768 }); ok {
769 name = p.Name(name)
770 }
771 return name
772}
773
774var Bool = proptools.Bool
775var BoolDefault = proptools.BoolDefault
776var String = proptools.String
777var StringPtr = proptools.StringPtr