blob: 14513fbf9e4ba2ccd1b3ef78380ade52f55e108e [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
88func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
89 return mod.InRecovery()
90}
91
92func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
93 return nil
94}
95
96func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
97}
98
Ivan Lozano52767be2019-10-18 14:49:46 -070099func (mod *Module) BuildStubs() bool {
100 return false
101}
102
103func (mod *Module) HasStubsVariants() bool {
104 return false
105}
106
107func (mod *Module) SelectedStl() string {
108 return ""
109}
110
Ivan Lozano2b262972019-11-21 12:30:50 -0800111func (mod *Module) NonCcVariants() bool {
112 if mod.compiler != nil {
113 if library, ok := mod.compiler.(libraryInterface); ok {
114 if library.buildRlib() || library.buildDylib() {
115 return true
116 } else {
117 return false
118 }
119 }
120 }
121 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
122}
123
Ivan Lozano52767be2019-10-18 14:49:46 -0700124func (mod *Module) ApiLevel() string {
125 panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
126}
127
128func (mod *Module) Static() bool {
129 if mod.compiler != nil {
130 if library, ok := mod.compiler.(libraryInterface); ok {
131 return library.static()
132 }
133 }
134 panic(fmt.Errorf("Static called on non-library module: %q", mod.BaseModuleName()))
135}
136
137func (mod *Module) Shared() bool {
138 if mod.compiler != nil {
139 if library, ok := mod.compiler.(libraryInterface); ok {
140 return library.static()
141 }
142 }
143 panic(fmt.Errorf("Shared called on non-library module: %q", mod.BaseModuleName()))
144}
145
146func (mod *Module) Toc() android.OptionalPath {
147 if mod.compiler != nil {
148 if _, ok := mod.compiler.(libraryInterface); ok {
149 return android.OptionalPath{}
150 }
151 }
152 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
153}
154
155func (mod *Module) OnlyInRecovery() bool {
156 return false
157}
158
159func (mod *Module) UseVndk() bool {
160 return false
161}
162
163func (mod *Module) MustUseVendorVariant() bool {
164 return false
165}
166
167func (mod *Module) IsVndk() bool {
168 return false
169}
170
171func (mod *Module) HasVendorVariant() bool {
172 return false
173}
174
175func (mod *Module) SdkVersion() string {
176 return ""
177}
178
179func (mod *Module) ToolchainLibrary() bool {
180 return false
181}
182
183func (mod *Module) NdkPrebuiltStl() bool {
184 return false
185}
186
187func (mod *Module) StubDecorator() bool {
188 return false
189}
190
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191type Deps struct {
192 Dylibs []string
193 Rlibs []string
194 ProcMacros []string
195 SharedLibs []string
196 StaticLibs []string
197
198 CrtBegin, CrtEnd string
199}
200
201type PathDeps struct {
202 DyLibs RustLibraries
203 RLibs RustLibraries
204 SharedLibs android.Paths
205 StaticLibs android.Paths
206 ProcMacros RustLibraries
207 linkDirs []string
208 depFlags []string
209 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700210
211 CrtBegin android.OptionalPath
212 CrtEnd android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700213}
214
215type RustLibraries []RustLibrary
216
217type RustLibrary struct {
218 Path android.Path
219 CrateName string
220}
221
222type compiler interface {
223 compilerFlags(ctx ModuleContext, flags Flags) Flags
224 compilerProps() []interface{}
225 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
226 compilerDeps(ctx DepsContext, deps Deps) Deps
227 crateName() string
228
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800229 inData() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700230 install(ctx ModuleContext, path android.Path)
231 relativeInstallPath() string
232}
233
234func defaultsFactory() android.Module {
235 return DefaultsFactory()
236}
237
238type Defaults struct {
239 android.ModuleBase
240 android.DefaultsModuleBase
241}
242
243func DefaultsFactory(props ...interface{}) android.Module {
244 module := &Defaults{}
245
246 module.AddProperties(props...)
247 module.AddProperties(
248 &BaseProperties{},
249 &BaseCompilerProperties{},
250 &BinaryCompilerProperties{},
251 &LibraryCompilerProperties{},
252 &ProcMacroCompilerProperties{},
253 &PrebuiltProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700254 &TestProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255 )
256
257 android.InitDefaultsModule(module)
258 return module
259}
260
261func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700262 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700263}
264
Ivan Lozano183a3212019-10-18 14:18:45 -0700265func (mod *Module) CcLibrary() bool {
266 if mod.compiler != nil {
267 if _, ok := mod.compiler.(*libraryDecorator); ok {
268 return true
269 }
270 }
271 return false
272}
273
274func (mod *Module) CcLibraryInterface() bool {
275 if mod.compiler != nil {
276 if _, ok := mod.compiler.(libraryInterface); ok {
277 return true
278 }
279 }
280 return false
281}
282
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800283func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700284 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700285 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800286 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700287 }
288 }
289 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
290}
291
292func (mod *Module) SetStatic() {
293 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700294 if library, ok := mod.compiler.(libraryInterface); ok {
295 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700296 return
297 }
298 }
299 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
300}
301
302func (mod *Module) SetShared() {
303 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700304 if library, ok := mod.compiler.(libraryInterface); ok {
305 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700306 return
307 }
308 }
309 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
310}
311
312func (mod *Module) SetBuildStubs() {
313 panic("SetBuildStubs not yet implemented for rust modules")
314}
315
316func (mod *Module) SetStubsVersions(string) {
317 panic("SetStubsVersions not yet implemented for rust modules")
318}
319
320func (mod *Module) BuildStaticVariant() bool {
321 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 if library, ok := mod.compiler.(libraryInterface); ok {
323 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700324 }
325 }
326 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
327}
328
329func (mod *Module) BuildSharedVariant() bool {
330 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700331 if library, ok := mod.compiler.(libraryInterface); ok {
332 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700333 }
334 }
335 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
336}
337
338// Rust module deps don't have a link order (?)
339func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
340
341func (mod *Module) GetDepsInLinkOrder() []android.Path {
342 return []android.Path{}
343}
344
345func (mod *Module) GetStaticVariant() cc.LinkableInterface {
346 return nil
347}
348
Jiyong Park83dc74b2020-01-14 18:38:44 +0900349func (mod *Module) AllStaticDeps() []string {
350 // TODO(jiyong): do this for rust?
351 return nil
352}
353
Ivan Lozano183a3212019-10-18 14:18:45 -0700354func (mod *Module) Module() android.Module {
355 return mod
356}
357
358func (mod *Module) StubsVersions() []string {
359 // For now, Rust has no stubs versions.
360 if mod.compiler != nil {
361 if _, ok := mod.compiler.(*libraryDecorator); ok {
362 return []string{}
363 }
364 }
365 panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
366}
367
368func (mod *Module) OutputFile() android.OptionalPath {
369 return mod.outputFile
370}
371
372func (mod *Module) InRecovery() bool {
373 // For now, Rust has no notion of the recovery image
374 return false
375}
376func (mod *Module) HasStaticVariant() bool {
377 if mod.GetStaticVariant() != nil {
378 return true
379 }
380 return false
381}
382
383var _ cc.LinkableInterface = (*Module)(nil)
384
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385func (mod *Module) Init() android.Module {
386 mod.AddProperties(&mod.Properties)
387
388 if mod.compiler != nil {
389 mod.AddProperties(mod.compiler.compilerProps()...)
390 }
391 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
392
393 android.InitDefaultableModule(mod)
394
Ivan Lozanode252912019-09-06 15:29:52 -0700395 // Explicitly disable unsupported targets.
396 android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
397 disableTargets := struct {
398 Target struct {
Ivan Lozanode252912019-09-06 15:29:52 -0700399 Linux_bionic struct {
400 Enabled *bool
401 }
402 }
403 }{}
Ivan Lozanode252912019-09-06 15:29:52 -0700404 disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
405
406 ctx.AppendProperties(&disableTargets)
407 })
408
Ivan Lozanoffee3342019-08-27 12:03:00 -0700409 return mod
410}
411
412func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
413 return &Module{
414 hod: hod,
415 multilib: multilib,
416 }
417}
418func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
419 module := newBaseModule(hod, multilib)
420 return module
421}
422
423type ModuleContext interface {
424 android.ModuleContext
425 ModuleContextIntf
426}
427
428type BaseModuleContext interface {
429 android.BaseModuleContext
430 ModuleContextIntf
431}
432
433type DepsContext interface {
434 android.BottomUpMutatorContext
435 ModuleContextIntf
436}
437
438type ModuleContextIntf interface {
439 toolchain() config.Toolchain
440 baseModuleName() string
441 CrateName() string
442}
443
444type depsContext struct {
445 android.BottomUpMutatorContext
446 moduleContextImpl
447}
448
449type moduleContext struct {
450 android.ModuleContext
451 moduleContextImpl
452}
453
454type moduleContextImpl struct {
455 mod *Module
456 ctx BaseModuleContext
457}
458
459func (ctx *moduleContextImpl) toolchain() config.Toolchain {
460 return ctx.mod.toolchain(ctx.ctx)
461}
462
463func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
464 if mod.cachedToolchain == nil {
465 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
466 }
467 return mod.cachedToolchain
468}
469
470func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
471}
472
473func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
474 ctx := &moduleContext{
475 ModuleContext: actx,
476 moduleContextImpl: moduleContextImpl{
477 mod: mod,
478 },
479 }
480 ctx.ctx = ctx
481
482 toolchain := mod.toolchain(ctx)
483
484 if !toolchain.Supported() {
485 // This toolchain's unsupported, there's nothing to do for this mod.
486 return
487 }
488
489 deps := mod.depsToPaths(ctx)
490 flags := Flags{
491 Toolchain: toolchain,
492 }
493
494 if mod.compiler != nil {
495 flags = mod.compiler.compilerFlags(ctx, flags)
496 outputFile := mod.compiler.compile(ctx, flags, deps)
497 mod.outputFile = android.OptionalPathForPath(outputFile)
498 mod.compiler.install(ctx, mod.outputFile.Path())
499 }
500}
501
502func (mod *Module) deps(ctx DepsContext) Deps {
503 deps := Deps{}
504
505 if mod.compiler != nil {
506 deps = mod.compiler.compilerDeps(ctx, deps)
507 }
508
509 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
510 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
511 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
512 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
513 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
514
515 return deps
516
517}
518
519func (ctx *moduleContextImpl) baseModuleName() string {
520 return ctx.mod.ModuleBase.BaseModuleName()
521}
522
523func (ctx *moduleContextImpl) CrateName() string {
524 return ctx.mod.CrateName()
525}
526
527type dependencyTag struct {
528 blueprint.BaseDependencyTag
529 name string
530 library bool
531 proc_macro bool
532}
533
534var (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700535 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
536 dylibDepTag = dependencyTag{name: "dylib", library: true}
537 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
538 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539)
540
541func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
542 var depPaths PathDeps
543
544 directRlibDeps := []*Module{}
545 directDylibDeps := []*Module{}
546 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700547 directSharedLibDeps := [](cc.LinkableInterface){}
548 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700549
550 ctx.VisitDirectDeps(func(dep android.Module) {
551 depName := ctx.OtherModuleName(dep)
552 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553 if rustDep, ok := dep.(*Module); ok {
554 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700555
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556 linkFile := rustDep.outputFile
557 if !linkFile.Valid() {
558 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
559 }
560
561 switch depTag {
562 case dylibDepTag:
563 dylib, ok := rustDep.compiler.(libraryInterface)
564 if !ok || !dylib.dylib() {
565 ctx.ModuleErrorf("mod %q not an dylib library", depName)
566 return
567 }
568 directDylibDeps = append(directDylibDeps, rustDep)
569 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
570 case rlibDepTag:
571 rlib, ok := rustDep.compiler.(libraryInterface)
572 if !ok || !rlib.rlib() {
573 ctx.ModuleErrorf("mod %q not an rlib library", depName)
574 return
575 }
576 directRlibDeps = append(directRlibDeps, rustDep)
577 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
578 case procMacroDepTag:
579 directProcMacroDeps = append(directProcMacroDeps, rustDep)
580 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
581 }
582
583 //Append the dependencies exportedDirs
584 if lib, ok := rustDep.compiler.(*libraryDecorator); ok {
585 depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedDirs()...)
586 depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587 }
588
589 // Append this dependencies output to this mod's linkDirs so they can be exported to dependencies
590 // This can be probably be refactored by defining a common exporter interface similar to cc's
591 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
592 linkDir := linkPathFromFilePath(linkFile.Path())
593 if lib, ok := mod.compiler.(*libraryDecorator); ok {
594 lib.linkDirs = append(lib.linkDirs, linkDir)
595 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok {
596 procMacro.linkDirs = append(procMacro.linkDirs, linkDir)
597 }
598 }
599
Ivan Lozano52767be2019-10-18 14:49:46 -0700600 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700601
Ivan Lozano52767be2019-10-18 14:49:46 -0700602 if ccDep, ok := dep.(cc.LinkableInterface); ok {
603 //Handle C dependencies
604 if _, ok := ccDep.(*Module); !ok {
605 if ccDep.Module().Target().Os != ctx.Os() {
606 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
607 return
608 }
609 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
610 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
611 return
612 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700613 }
614
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615 linkFile := ccDep.OutputFile()
616 linkPath := linkPathFromFilePath(linkFile.Path())
617 libName := libNameFromFilePath(linkFile.Path())
618 if !linkFile.Valid() {
619 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
620 }
621
622 exportDep := false
623
624 switch depTag {
Ivan Lozano183a3212019-10-18 14:18:45 -0700625 case cc.StaticDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700626 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
627 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
628 directStaticLibDeps = append(directStaticLibDeps, ccDep)
629 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Ivan Lozano183a3212019-10-18 14:18:45 -0700630 case cc.SharedDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700631 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
632 depPaths.depFlags = append(depPaths.depFlags, "-l"+libName)
633 directSharedLibDeps = append(directSharedLibDeps, ccDep)
634 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
635 exportDep = true
Ivan Lozano183a3212019-10-18 14:18:45 -0700636 case cc.CrtBeginDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700637 depPaths.CrtBegin = linkFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700638 case cc.CrtEndDepTag:
Ivan Lozanof1c84332019-09-20 11:00:37 -0700639 depPaths.CrtEnd = linkFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700640 }
641
642 // Make sure these dependencies are propagated
Ivan Lozano52767be2019-10-18 14:49:46 -0700643 if lib, ok := mod.compiler.(*libraryDecorator); ok && exportDep {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644 lib.linkDirs = append(lib.linkDirs, linkPath)
645 lib.depFlags = append(lib.depFlags, "-l"+libName)
646 } else if procMacro, ok := mod.compiler.(*procMacroDecorator); ok && exportDep {
647 procMacro.linkDirs = append(procMacro.linkDirs, linkPath)
648 procMacro.depFlags = append(procMacro.depFlags, "-l"+libName)
649 }
650
651 }
652 })
653
654 var rlibDepFiles RustLibraries
655 for _, dep := range directRlibDeps {
656 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
657 }
658 var dylibDepFiles RustLibraries
659 for _, dep := range directDylibDeps {
660 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
661 }
662 var procMacroDepFiles RustLibraries
663 for _, dep := range directProcMacroDeps {
664 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
665 }
666
667 var staticLibDepFiles android.Paths
668 for _, dep := range directStaticLibDeps {
669 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
670 }
671
672 var sharedLibDepFiles android.Paths
673 for _, dep := range directSharedLibDeps {
674 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
675 }
676
677 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
678 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
679 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
680 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
681 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
682
683 // Dedup exported flags from dependencies
684 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
685 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
686
687 return depPaths
688}
689
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800690func (mod *Module) InstallInData() bool {
691 if mod.compiler == nil {
692 return false
693 }
694 return mod.compiler.inData()
695}
696
Ivan Lozanoffee3342019-08-27 12:03:00 -0700697func linkPathFromFilePath(filepath android.Path) string {
698 return strings.Split(filepath.String(), filepath.Base())[0]
699}
700func libNameFromFilePath(filepath android.Path) string {
701 libName := strings.Split(filepath.Base(), filepath.Ext())[0]
Ivan Lozano52767be2019-10-18 14:49:46 -0700702 if strings.HasPrefix(libName, "lib") {
703 libName = libName[3:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704 }
705 return libName
706}
707func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
708 ctx := &depsContext{
709 BottomUpMutatorContext: actx,
710 moduleContextImpl: moduleContextImpl{
711 mod: mod,
712 },
713 }
714 ctx.ctx = ctx
715
716 deps := mod.deps(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700717 commonDepVariations := []blueprint.Variation{}
718 commonDepVariations = append(commonDepVariations,
719 blueprint.Variation{Mutator: "version", Variation: ""})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700720 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700721 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800722 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700724
725 actx.AddVariationDependencies(
726 append(commonDepVariations, []blueprint.Variation{
727 {Mutator: "rust_libraries", Variation: "rlib"},
728 {Mutator: "link", Variation: ""}}...),
729 rlibDepTag, deps.Rlibs...)
730 actx.AddVariationDependencies(
731 append(commonDepVariations, []blueprint.Variation{
732 {Mutator: "rust_libraries", Variation: "dylib"},
733 {Mutator: "link", Variation: ""}}...),
734 dylibDepTag, deps.Dylibs...)
735
736 actx.AddVariationDependencies(append(commonDepVariations,
737 blueprint.Variation{Mutator: "link", Variation: "shared"}),
738 cc.SharedDepTag, deps.SharedLibs...)
739 actx.AddVariationDependencies(append(commonDepVariations,
740 blueprint.Variation{Mutator: "link", Variation: "static"}),
741 cc.StaticDepTag, deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700742
Ivan Lozanof1c84332019-09-20 11:00:37 -0700743 if deps.CrtBegin != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700744 actx.AddVariationDependencies(commonDepVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700745 }
746 if deps.CrtEnd != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -0700747 actx.AddVariationDependencies(commonDepVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700748 }
749
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700750 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700751 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752}
753
754func (mod *Module) Name() string {
755 name := mod.ModuleBase.Name()
756 if p, ok := mod.compiler.(interface {
757 Name(string) string
758 }); ok {
759 name = p.Name(name)
760 }
761 return name
762}
763
764var Bool = proptools.Bool
765var BoolDefault = proptools.BoolDefault
766var String = proptools.String
767var StringPtr = proptools.StringPtr