blob: f446ef039024222a477e5f46b05f24007eff54ae [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
Jooyung Han03b51852020-02-26 22:45:42 +0900328func (mod *Module) StubsVersion() string {
329 panic("SetStubsVersions not yet implemented for rust modules")
330}
331
Ivan Lozano183a3212019-10-18 14:18:45 -0700332func (mod *Module) BuildStaticVariant() bool {
333 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700334 if library, ok := mod.compiler.(libraryInterface); ok {
335 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700336 }
337 }
338 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
339}
340
341func (mod *Module) BuildSharedVariant() bool {
342 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700343 if library, ok := mod.compiler.(libraryInterface); ok {
344 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700345 }
346 }
347 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
348}
349
350// Rust module deps don't have a link order (?)
351func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
352
353func (mod *Module) GetDepsInLinkOrder() []android.Path {
354 return []android.Path{}
355}
356
357func (mod *Module) GetStaticVariant() cc.LinkableInterface {
358 return nil
359}
360
361func (mod *Module) Module() android.Module {
362 return mod
363}
364
365func (mod *Module) StubsVersions() []string {
366 // For now, Rust has no stubs versions.
367 if mod.compiler != nil {
368 if _, ok := mod.compiler.(*libraryDecorator); ok {
369 return []string{}
370 }
371 }
372 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
373}
374
375func (mod *Module) OutputFile() android.OptionalPath {
376 return mod.outputFile
377}
378
379func (mod *Module) InRecovery() bool {
380 // For now, Rust has no notion of the recovery image
381 return false
382}
383func (mod *Module) HasStaticVariant() bool {
384 if mod.GetStaticVariant() != nil {
385 return true
386 }
387 return false
388}
389
390var _ cc.LinkableInterface = (*Module)(nil)
391
Ivan Lozanoffee3342019-08-27 12:03:00 -0700392func (mod *Module) Init() android.Module {
393 mod.AddProperties(&mod.Properties)
394
395 if mod.compiler != nil {
396 mod.AddProperties(mod.compiler.compilerProps()...)
397 }
398 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
399
400 android.InitDefaultableModule(mod)
401
Ivan Lozanode252912019-09-06 15:29:52 -0700402 // Explicitly disable unsupported targets.
403 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
404 disableTargets := struct {
405 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700406 Linux_bionic struct {
407 Enabled *bool
408 }
409 }
410 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700411 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
412
413 ctx.AppendProperties(&disableTargets)
414 })
415
Ivan Lozanoffee3342019-08-27 12:03:00 -0700416 return mod
417}
418
419func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
420 return &Module{
421 hod: hod,
422 multilib: multilib,
423 }
424}
425func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
426 module := newBaseModule(hod, multilib)
427 return module
428}
429
430type ModuleContext interface {
431 android.ModuleContext
432 ModuleContextIntf
433}
434
435type BaseModuleContext interface {
436 android.BaseModuleContext
437 ModuleContextIntf
438}
439
440type DepsContext interface {
441 android.BottomUpMutatorContext
442 ModuleContextIntf
443}
444
445type ModuleContextIntf interface {
446 toolchain() config.Toolchain
447 baseModuleName() string
448 CrateName() string
449}
450
451type depsContext struct {
452 android.BottomUpMutatorContext
453 moduleContextImpl
454}
455
456type moduleContext struct {
457 android.ModuleContext
458 moduleContextImpl
459}
460
461type moduleContextImpl struct {
462 mod *Module
463 ctx BaseModuleContext
464}
465
466func (ctx *moduleContextImpl) toolchain() config.Toolchain {
467 return ctx.mod.toolchain(ctx.ctx)
468}
469
470func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
471 if mod.cachedToolchain == nil {
472 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
473 }
474 return mod.cachedToolchain
475}
476
477func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
478}
479
480func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
481 ctx := &moduleContext{
482 ModuleContext: actx,
483 moduleContextImpl: moduleContextImpl{
484 mod: mod,
485 },
486 }
487 ctx.ctx = ctx
488
489 toolchain := mod.toolchain(ctx)
490
491 if !toolchain.Supported() {
492 // This toolchain's unsupported, there's nothing to do for this mod.
493 return
494 }
495
496 deps := mod.depsToPaths(ctx)
497 flags := Flags{
498 Toolchain: toolchain,
499 }
500
501 if mod.compiler != nil {
502 flags = mod.compiler.compilerFlags(ctx, flags)
503 outputFile := mod.compiler.compile(ctx, flags, deps)
504 mod.outputFile = android.OptionalPathForPath(outputFile)
505 mod.compiler.install(ctx, mod.outputFile.Path())
506 }
507}
508
509func (mod *Module) deps(ctx DepsContext) Deps {
510 deps := Deps{}
511
512 if mod.compiler != nil {
513 deps = mod.compiler.compilerDeps(ctx, deps)
514 }
515
516 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
517 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
518 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
519 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
520 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
521
522 return deps
523
524}
525
526func (ctx *moduleContextImpl) baseModuleName() string {
527 return ctx.mod.ModuleBase.BaseModuleName()
528}
529
530func (ctx *moduleContextImpl) CrateName() string {
531 return ctx.mod.CrateName()
532}
533
534type dependencyTag struct {
535 blueprint.BaseDependencyTag
536 name string
537 library bool
538 proc_macro bool
539}
540
541var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700542 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
543 dylibDepTag = dependencyTag{name: "dylib", library: true}
544 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
545 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546)
547
548func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
549 var depPaths PathDeps
550
551 directRlibDeps := []*Module{}
552 directDylibDeps := []*Module{}
553 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700554 directSharedLibDeps := [](cc.LinkableInterface){}
555 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556
557 ctx.VisitDirectDeps(func(dep android.Module) {
558 depName := ctx.OtherModuleName(dep)
559 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560 if rustDep, ok := dep.(*Module); ok {
561 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700562
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563 linkFile := rustDep.outputFile
564 if !linkFile.Valid() {
565 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
566 }
567
568 switch depTag {
569 case dylibDepTag:
570 dylib, ok := rustDep.compiler.(libraryInterface)
571 if !ok || !dylib.dylib() {
572 ctx.ModuleErrorf("mod %q not an dylib library", depName)
573 return
574 }
575 directDylibDeps = append(directDylibDeps, rustDep)
576 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
577 case rlibDepTag:
578 rlib, ok := rustDep.compiler.(libraryInterface)
579 if !ok || !rlib.rlib() {
580 ctx.ModuleErrorf("mod %q not an rlib library", depName)
581 return
582 }
583 directRlibDeps = append(directRlibDeps, rustDep)
584 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
585 case procMacroDepTag:
586 directProcMacroDeps = append(directProcMacroDeps, rustDep)
587 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
588 }
589
590 //Append the dependencies exportedDirs
591 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
592 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
593 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594 }
595
596 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
597 // This can be probably be refactored by defining a common exporter interface similar to cc's
598 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
599 linkDir := linkPathFromFilePath(linkFile.Path())
600 if lib, ok := mod.compiler.(*libraryDecorator); ok {
601 lib.linkDirs = append(lib.linkDirs, linkDir)
602 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
603 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
604 }
605 }
606
Ivan Lozano52767be2019-10-18 14:49:46 -0700607 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700608
Ivan Lozano52767be2019-10-18 14:49:46 -0700609 if ccDep, ok := dep.(cc.LinkableInterface); ok {
610 //Handle C dependencies
611 if _, ok := ccDep.(*Module); !ok {
612 if ccDep.Module().Target().Os != ctx.Os() {
613 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
614 return
615 }
616 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
617 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
618 return
619 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700620 }
621
Ivan Lozanoffee3342019-08-27 12:03:00 -0700622 linkFile := ccDep.OutputFile()
623 linkPath := linkPathFromFilePath(linkFile.Path())
624 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500625 depFlag := "-l" + libName
626
Ivan Lozanoffee3342019-08-27 12:03:00 -0700627 if !linkFile.Valid() {
628 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
629 }
630
631 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700632 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700633 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500634 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700635 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500636 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700637 directStaticLibDeps = append(directStaticLibDeps, ccDep)
638 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700639 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500640 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700641 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500642 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643 directSharedLibDeps = append(directSharedLibDeps, ccDep)
644 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
645 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700646 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700647 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700648 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700649 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700650 }
651
652 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700653 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700654 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500655 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700656 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
657 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500658 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659 }
660
661 }
662 })
663
664 var rlibDepFiles RustLibraries
665 for _, dep := range directRlibDeps {
666 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
667 }
668 var dylibDepFiles RustLibraries
669 for _, dep := range directDylibDeps {
670 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
671 }
672 var procMacroDepFiles RustLibraries
673 for _, dep := range directProcMacroDeps {
674 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
675 }
676
677 var staticLibDepFiles android.Paths
678 for _, dep := range directStaticLibDeps {
679 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
680 }
681
682 var sharedLibDepFiles android.Paths
683 for _, dep := range directSharedLibDeps {
684 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
685 }
686
687 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
688 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
689 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
690 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
691 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
692
693 // Dedup exported flags from dependencies
694 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
695 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
696
697 return depPaths
698}
699
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800700func (mod *Module) InstallInData() bool {
701 if mod.compiler == nil {
702 return false
703 }
704 return mod.compiler.inData()
705}
706
Ivan Lozanoffee3342019-08-27 12:03:00 -0700707func linkPathFromFilePath(filepath android.Path) string {
708 return strings.Split(filepath.String(), filepath.Base())[0]
709}
Ivan Lozanod648c432020-02-06 12:05:10 -0500710
Ivan Lozanoffee3342019-08-27 12:03:00 -0700711func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500712 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
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}
Ivan Lozanod648c432020-02-06 12:05:10 -0500718
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
720 ctx := &depsContext{
721 BottomUpMutatorContext: actx,
722 moduleContextImpl: moduleContextImpl{
723 mod: mod,
724 },
725 }
726 ctx.ctx = ctx
727
728 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700729 commonDepVariations := []blueprint.Variation{}
730 commonDepVariations = append(commonDepVariations,
731 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700732 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700733 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800734 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700735 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700736
737 actx.AddVariationDependencies(
738 append(commonDepVariations, []blueprint.Variation{
739 {Mutator: "rust_libraries", Variation: "rlib"},
740 {Mutator: "link", Variation: ""}}...),
741 rlibDepTag, deps.Rlibs...)
742 actx.AddVariationDependencies(
743 append(commonDepVariations, []blueprint.Variation{
744 {Mutator: "rust_libraries", Variation: "dylib"},
745 {Mutator: "link", Variation: ""}}...),
746 dylibDepTag, deps.Dylibs...)
747
748 actx.AddVariationDependencies(append(commonDepVariations,
749 blueprint.Variation{Mutator: "link", Variation: "shared"}),
750 cc.SharedDepTag, deps.SharedLibs...)
751 actx.AddVariationDependencies(append(commonDepVariations,
752 blueprint.Variation{Mutator: "link", Variation: "static"}),
753 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700754
Ivan Lozanof1c84332019-09-20 11:00:37 -0700755 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700756 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700757 }
758 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700759 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700760 }
761
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700762 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700763 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764}
765
766func (mod *Module) Name() string {
767 name := mod.ModuleBase.Name()
768 if p, ok := mod.compiler.(interface {
769 Name(string) string
770 }); ok {
771 name = p.Name(name)
772 }
773 return name
774}
775
776var Bool = proptools.Bool
777var BoolDefault = proptools.BoolDefault
778var String = proptools.String
779var StringPtr = proptools.StringPtr