blob: 6fe887129cdebe4626f81f76c84e940122e3951b [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
Colin Crossc511bc52020-04-07 16:50:32 +0000167func (mod *Module) UseSdk() bool {
168 return false
169}
170
Ivan Lozano52767be2019-10-18 14:49:46 -0700171func (mod *Module) UseVndk() bool {
172 return false
173}
174
175func (mod *Module) MustUseVendorVariant() bool {
176 return false
177}
178
179func (mod *Module) IsVndk() bool {
180 return false
181}
182
183func (mod *Module) HasVendorVariant() bool {
184 return false
185}
186
187func (mod *Module) SdkVersion() string {
188 return ""
189}
190
Colin Crossc511bc52020-04-07 16:50:32 +0000191func (mod *Module) AlwaysSdk() bool {
192 return false
193}
194
Ivan Lozano52767be2019-10-18 14:49:46 -0700195func (mod *Module) ToolchainLibrary() bool {
196 return false
197}
198
199func (mod *Module) NdkPrebuiltStl() bool {
200 return false
201}
202
203func (mod *Module) StubDecorator() bool {
204 return false
205}
206
Ivan Lozanoffee3342019-08-27 12:03:00 -0700207type Deps struct {
208 Dylibs []string
209 Rlibs []string
210 ProcMacros []string
211 SharedLibs []string
212 StaticLibs []string
213
214 CrtBegin, CrtEnd string
215}
216
217type PathDeps struct {
218 DyLibs RustLibraries
219 RLibs RustLibraries
220 SharedLibs android.Paths
221 StaticLibs android.Paths
222 ProcMacros RustLibraries
223 linkDirs []string
224 depFlags []string
225 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700226
227 CrtBegin android.OptionalPath
228 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700229}
230
231type RustLibraries []RustLibrary
232
233type RustLibrary struct {
234 Path android.Path
235 CrateName string
236}
237
238type compiler interface {
239 compilerFlags(ctx ModuleContext, flags Flags) Flags
240 compilerProps() []interface{}
241 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
242 compilerDeps(ctx DepsContext, deps Deps) Deps
243 crateName() string
244
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800245 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246 install(ctx ModuleContext, path android.Path)
247 relativeInstallPath() string
248}
249
250func defaultsFactory() android.Module {
251 return DefaultsFactory()
252}
253
254type Defaults struct {
255 android.ModuleBase
256 android.DefaultsModuleBase
257}
258
259func DefaultsFactory(props ...interface{}) android.Module {
260 module := &Defaults{}
261
262 module.AddProperties(props...)
263 module.AddProperties(
264 &BaseProperties{},
265 &BaseCompilerProperties{},
266 &BinaryCompilerProperties{},
267 &LibraryCompilerProperties{},
268 &ProcMacroCompilerProperties{},
269 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700270 &TestProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700271 )
272
273 android.InitDefaultsModule(module)
274 return module
275}
276
277func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700278 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700279}
280
Ivan Lozano183a3212019-10-18 14:18:45 -0700281func (mod *Module) CcLibrary() bool {
282 if mod.compiler != nil {
283 if _, ok := mod.compiler.(*libraryDecorator); ok {
284 return true
285 }
286 }
287 return false
288}
289
290func (mod *Module) CcLibraryInterface() bool {
291 if mod.compiler != nil {
292 if _, ok := mod.compiler.(libraryInterface); ok {
293 return true
294 }
295 }
296 return false
297}
298
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800299func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700300 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800302 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700303 }
304 }
305 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
306}
307
308func (mod *Module) SetStatic() {
309 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700310 if library, ok := mod.compiler.(libraryInterface); ok {
311 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700312 return
313 }
314 }
315 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
316}
317
318func (mod *Module) SetShared() {
319 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700320 if library, ok := mod.compiler.(libraryInterface); ok {
321 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700322 return
323 }
324 }
325 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
326}
327
328func (mod *Module) SetBuildStubs() {
329 panic("SetBuildStubs not yet implemented for rust modules")
330}
331
332func (mod *Module) SetStubsVersions(string) {
333 panic("SetStubsVersions not yet implemented for rust modules")
334}
335
Jooyung Han03b51852020-02-26 22:45:42 +0900336func (mod *Module) StubsVersion() string {
337 panic("SetStubsVersions not yet implemented for rust modules")
338}
339
Ivan Lozano183a3212019-10-18 14:18:45 -0700340func (mod *Module) BuildStaticVariant() bool {
341 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700342 if library, ok := mod.compiler.(libraryInterface); ok {
343 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700344 }
345 }
346 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
347}
348
349func (mod *Module) BuildSharedVariant() bool {
350 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700351 if library, ok := mod.compiler.(libraryInterface); ok {
352 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700353 }
354 }
355 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
356}
357
358// Rust module deps don't have a link order (?)
359func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
360
361func (mod *Module) GetDepsInLinkOrder() []android.Path {
362 return []android.Path{}
363}
364
365func (mod *Module) GetStaticVariant() cc.LinkableInterface {
366 return nil
367}
368
369func (mod *Module) Module() android.Module {
370 return mod
371}
372
373func (mod *Module) StubsVersions() []string {
374 // For now, Rust has no stubs versions.
375 if mod.compiler != nil {
376 if _, ok := mod.compiler.(*libraryDecorator); ok {
377 return []string{}
378 }
379 }
380 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
381}
382
383func (mod *Module) OutputFile() android.OptionalPath {
384 return mod.outputFile
385}
386
387func (mod *Module) InRecovery() bool {
388 // For now, Rust has no notion of the recovery image
389 return false
390}
391func (mod *Module) HasStaticVariant() bool {
392 if mod.GetStaticVariant() != nil {
393 return true
394 }
395 return false
396}
397
398var _ cc.LinkableInterface = (*Module)(nil)
399
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400func (mod *Module) Init() android.Module {
401 mod.AddProperties(&mod.Properties)
402
403 if mod.compiler != nil {
404 mod.AddProperties(mod.compiler.compilerProps()...)
405 }
406 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
407
408 android.InitDefaultableModule(mod)
409
Ivan Lozanode252912019-09-06 15:29:52 -0700410 // Explicitly disable unsupported targets.
411 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
412 disableTargets := struct {
413 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700414 Linux_bionic struct {
415 Enabled *bool
416 }
417 }
418 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700419 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
420
421 ctx.AppendProperties(&disableTargets)
422 })
423
Ivan Lozanoffee3342019-08-27 12:03:00 -0700424 return mod
425}
426
427func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
428 return &Module{
429 hod: hod,
430 multilib: multilib,
431 }
432}
433func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
434 module := newBaseModule(hod, multilib)
435 return module
436}
437
438type ModuleContext interface {
439 android.ModuleContext
440 ModuleContextIntf
441}
442
443type BaseModuleContext interface {
444 android.BaseModuleContext
445 ModuleContextIntf
446}
447
448type DepsContext interface {
449 android.BottomUpMutatorContext
450 ModuleContextIntf
451}
452
453type ModuleContextIntf interface {
454 toolchain() config.Toolchain
455 baseModuleName() string
456 CrateName() string
457}
458
459type depsContext struct {
460 android.BottomUpMutatorContext
461 moduleContextImpl
462}
463
464type moduleContext struct {
465 android.ModuleContext
466 moduleContextImpl
467}
468
469type moduleContextImpl struct {
470 mod *Module
471 ctx BaseModuleContext
472}
473
474func (ctx *moduleContextImpl) toolchain() config.Toolchain {
475 return ctx.mod.toolchain(ctx.ctx)
476}
477
478func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
479 if mod.cachedToolchain == nil {
480 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
481 }
482 return mod.cachedToolchain
483}
484
485func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
486}
487
488func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
489 ctx := &moduleContext{
490 ModuleContext: actx,
491 moduleContextImpl: moduleContextImpl{
492 mod: mod,
493 },
494 }
495 ctx.ctx = ctx
496
497 toolchain := mod.toolchain(ctx)
498
499 if !toolchain.Supported() {
500 // This toolchain's unsupported, there's nothing to do for this mod.
501 return
502 }
503
504 deps := mod.depsToPaths(ctx)
505 flags := Flags{
506 Toolchain: toolchain,
507 }
508
509 if mod.compiler != nil {
510 flags = mod.compiler.compilerFlags(ctx, flags)
511 outputFile := mod.compiler.compile(ctx, flags, deps)
512 mod.outputFile = android.OptionalPathForPath(outputFile)
513 mod.compiler.install(ctx, mod.outputFile.Path())
514 }
515}
516
517func (mod *Module) deps(ctx DepsContext) Deps {
518 deps := Deps{}
519
520 if mod.compiler != nil {
521 deps = mod.compiler.compilerDeps(ctx, deps)
522 }
523
524 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
525 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
526 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
527 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
528 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
529
530 return deps
531
532}
533
534func (ctx *moduleContextImpl) baseModuleName() string {
535 return ctx.mod.ModuleBase.BaseModuleName()
536}
537
538func (ctx *moduleContextImpl) CrateName() string {
539 return ctx.mod.CrateName()
540}
541
542type dependencyTag struct {
543 blueprint.BaseDependencyTag
544 name string
545 library bool
546 proc_macro bool
547}
548
549var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700550 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
551 dylibDepTag = dependencyTag{name: "dylib", library: true}
552 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
553 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554)
555
556func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
557 var depPaths PathDeps
558
559 directRlibDeps := []*Module{}
560 directDylibDeps := []*Module{}
561 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700562 directSharedLibDeps := [](cc.LinkableInterface){}
563 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700564
565 ctx.VisitDirectDeps(func(dep android.Module) {
566 depName := ctx.OtherModuleName(dep)
567 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568 if rustDep, ok := dep.(*Module); ok {
569 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700570
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571 linkFile := rustDep.outputFile
572 if !linkFile.Valid() {
573 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
574 }
575
576 switch depTag {
577 case dylibDepTag:
578 dylib, ok := rustDep.compiler.(libraryInterface)
579 if !ok || !dylib.dylib() {
580 ctx.ModuleErrorf("mod %q not an dylib library", depName)
581 return
582 }
583 directDylibDeps = append(directDylibDeps, rustDep)
584 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
585 case rlibDepTag:
586 rlib, ok := rustDep.compiler.(libraryInterface)
587 if !ok || !rlib.rlib() {
588 ctx.ModuleErrorf("mod %q not an rlib library", depName)
589 return
590 }
591 directRlibDeps = append(directRlibDeps, rustDep)
592 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
593 case procMacroDepTag:
594 directProcMacroDeps = append(directProcMacroDeps, rustDep)
595 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
596 }
597
598 //Append the dependencies exportedDirs
599 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
600 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
601 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602 }
603
604 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
605 // This can be probably be refactored by defining a common exporter interface similar to cc's
606 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
607 linkDir := linkPathFromFilePath(linkFile.Path())
608 if lib, ok := mod.compiler.(*libraryDecorator); ok {
609 lib.linkDirs = append(lib.linkDirs, linkDir)
610 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
611 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
612 }
613 }
614
Ivan Lozano52767be2019-10-18 14:49:46 -0700615 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700616
Ivan Lozano52767be2019-10-18 14:49:46 -0700617 if ccDep, ok := dep.(cc.LinkableInterface); ok {
618 //Handle C dependencies
619 if _, ok := ccDep.(*Module); !ok {
620 if ccDep.Module().Target().Os != ctx.Os() {
621 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
622 return
623 }
624 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
625 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
626 return
627 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700628 }
629
Ivan Lozanoffee3342019-08-27 12:03:00 -0700630 linkFile := ccDep.OutputFile()
631 linkPath := linkPathFromFilePath(linkFile.Path())
632 libName := libNameFromFilePath(linkFile.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500633 depFlag := "-l" + libName
634
Ivan Lozanoffee3342019-08-27 12:03:00 -0700635 if !linkFile.Valid() {
636 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
637 }
638
639 exportDep := false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700640 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700641 case cc.StaticDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500642 depFlag = "-lstatic=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500644 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645 directStaticLibDeps = append(directStaticLibDeps, ccDep)
646 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700647 case cc.SharedDepTag:
Ivan Lozano6aa66022020-02-06 13:22:43 -0500648 depFlag = "-ldylib=" + libName
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500650 depPaths.depFlags = append(depPaths.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651 directSharedLibDeps = append(directSharedLibDeps, ccDep)
652 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
653 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700654 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700655 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700656 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700657 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700658 }
659
660 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700661 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662 lib.linkDirs = append(lib.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500663 lib.depFlags = append(lib.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
665 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
Ivan Lozano6aa66022020-02-06 13:22:43 -0500666 procMacro.depFlags = append(procMacro.depFlags, depFlag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667 }
668
669 }
670 })
671
672 var rlibDepFiles RustLibraries
673 for _, dep := range directRlibDeps {
674 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
675 }
676 var dylibDepFiles RustLibraries
677 for _, dep := range directDylibDeps {
678 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
679 }
680 var procMacroDepFiles RustLibraries
681 for _, dep := range directProcMacroDeps {
682 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
683 }
684
685 var staticLibDepFiles android.Paths
686 for _, dep := range directStaticLibDeps {
687 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
688 }
689
690 var sharedLibDepFiles android.Paths
691 for _, dep := range directSharedLibDeps {
692 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
693 }
694
695 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
696 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
697 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
698 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
699 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
700
701 // Dedup exported flags from dependencies
702 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
703 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
704
705 return depPaths
706}
707
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800708func (mod *Module) InstallInData() bool {
709 if mod.compiler == nil {
710 return false
711 }
712 return mod.compiler.inData()
713}
714
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715func linkPathFromFilePath(filepath android.Path) string {
716 return strings.Split(filepath.String(), filepath.Base())[0]
717}
Ivan Lozanod648c432020-02-06 12:05:10 -0500718
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719func libNameFromFilePath(filepath android.Path) string {
Ivan Lozanod648c432020-02-06 12:05:10 -0500720 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
Ivan Lozano52767be2019-10-18 14:49:46 -0700721 if strings.HasPrefix(libName, "lib") {
722 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 }
724 return libName
725}
Ivan Lozanod648c432020-02-06 12:05:10 -0500726
Ivan Lozanoffee3342019-08-27 12:03:00 -0700727func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
728 ctx := &depsContext{
729 BottomUpMutatorContext: actx,
730 moduleContextImpl: moduleContextImpl{
731 mod: mod,
732 },
733 }
734 ctx.ctx = ctx
735
736 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700737 commonDepVariations := []blueprint.Variation{}
738 commonDepVariations = append(commonDepVariations,
739 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700741 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800742 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700744
745 actx.AddVariationDependencies(
746 append(commonDepVariations, []blueprint.Variation{
747 {Mutator: "rust_libraries", Variation: "rlib"},
748 {Mutator: "link", Variation: ""}}...),
749 rlibDepTag, deps.Rlibs...)
750 actx.AddVariationDependencies(
751 append(commonDepVariations, []blueprint.Variation{
752 {Mutator: "rust_libraries", Variation: "dylib"},
753 {Mutator: "link", Variation: ""}}...),
754 dylibDepTag, deps.Dylibs...)
755
756 actx.AddVariationDependencies(append(commonDepVariations,
757 blueprint.Variation{Mutator: "link", Variation: "shared"}),
758 cc.SharedDepTag, deps.SharedLibs...)
759 actx.AddVariationDependencies(append(commonDepVariations,
760 blueprint.Variation{Mutator: "link", Variation: "static"}),
761 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700762
Ivan Lozanof1c84332019-09-20 11:00:37 -0700763 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700764 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700765 }
766 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700767 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700768 }
769
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700770 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700771 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772}
773
774func (mod *Module) Name() string {
775 name := mod.ModuleBase.Name()
776 if p, ok := mod.compiler.(interface {
777 Name(string) string
778 }); ok {
779 name = p.Name(name)
780 }
781 return name
782}
783
784var Bool = proptools.Bool
785var BoolDefault = proptools.BoolDefault
786var String = proptools.String
787var StringPtr = proptools.StringPtr