blob: 6cbcd7dc02ccceac44c4824dc5504b6903291890 [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"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020026 cc_config "android/soong/cc/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027 "android/soong/rust/config"
28)
29
30var pctx = android.NewPackageContext("android/soong/rust")
31
32func init() {
33 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070034
35 android.AddNeverAllowRules(
36 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070037 NotIn(config.RustAllowedPaths...).
38 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070039
40 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040051 GlobalRustFlags []string // Flags that apply globally to rust
52 GlobalLinkFlags []string // Flags that apply globally to linker
53 RustFlags []string // Flags that apply to rust
54 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070056 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040057 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61type BaseProperties struct {
62 AndroidMkRlibs []string
63 AndroidMkDylibs []string
64 AndroidMkProcMacroLibs []string
65 AndroidMkSharedLibs []string
66 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040067
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040068 SubName string `blueprint:"mutated"`
69
Ivan Lozano43845682020-07-09 21:03:28 -040070 PreventInstall bool
71 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070072}
73
74type Module struct {
75 android.ModuleBase
76 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090077 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070078
79 Properties BaseProperties
80
81 hod android.HostOrDeviceSupported
82 multilib android.Multilib
83
84 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040085 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020086 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070087 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040088 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070089 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040090
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020091 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +090092
93 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070094}
95
Ivan Lozano43845682020-07-09 21:03:28 -040096func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
97 switch tag {
98 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -070099 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400100 return mod.sourceProvider.Srcs(), nil
101 } else {
102 if mod.outputFile.Valid() {
103 return android.Paths{mod.outputFile.Path()}, nil
104 }
105 return android.Paths{}, nil
106 }
107 default:
108 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
109 }
110}
111
Colin Cross7228ecd2019-11-18 16:00:16 -0800112var _ android.ImageInterface = (*Module)(nil)
113
114func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
115
116func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
117 return true
118}
119
Yifan Hong1b3348d2020-01-21 15:53:22 -0800120func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
121 return mod.InRamdisk()
122}
123
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700124func (mod *Module) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool {
125 return mod.InVendorRamdisk()
126}
127
Colin Cross7228ecd2019-11-18 16:00:16 -0800128func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
129 return mod.InRecovery()
130}
131
132func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
133 return nil
134}
135
136func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
137}
138
Ivan Lozano52767be2019-10-18 14:49:46 -0700139func (mod *Module) SelectedStl() string {
140 return ""
141}
142
Ivan Lozano2b262972019-11-21 12:30:50 -0800143func (mod *Module) NonCcVariants() bool {
144 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400145 if _, ok := mod.compiler.(libraryInterface); ok {
146 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800147 }
148 }
149 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
150}
151
Ivan Lozano52767be2019-10-18 14:49:46 -0700152func (mod *Module) Static() bool {
153 if mod.compiler != nil {
154 if library, ok := mod.compiler.(libraryInterface); ok {
155 return library.static()
156 }
157 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400158 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700159}
160
161func (mod *Module) Shared() bool {
162 if mod.compiler != nil {
163 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400164 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700165 }
166 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400167 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700168}
169
170func (mod *Module) Toc() android.OptionalPath {
171 if mod.compiler != nil {
172 if _, ok := mod.compiler.(libraryInterface); ok {
173 return android.OptionalPath{}
174 }
175 }
176 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
177}
178
Yifan Hong1b3348d2020-01-21 15:53:22 -0800179func (mod *Module) OnlyInRamdisk() bool {
180 return false
181}
182
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700183func (mod *Module) OnlyInVendorRamdisk() bool {
184 return false
185}
186
Ivan Lozano52767be2019-10-18 14:49:46 -0700187func (mod *Module) OnlyInRecovery() bool {
188 return false
189}
190
Colin Crossc511bc52020-04-07 16:50:32 +0000191func (mod *Module) UseSdk() bool {
192 return false
193}
194
Ivan Lozano52767be2019-10-18 14:49:46 -0700195func (mod *Module) UseVndk() bool {
196 return false
197}
198
199func (mod *Module) MustUseVendorVariant() bool {
200 return false
201}
202
203func (mod *Module) IsVndk() bool {
204 return false
205}
206
207func (mod *Module) HasVendorVariant() bool {
208 return false
209}
210
211func (mod *Module) SdkVersion() string {
212 return ""
213}
214
Colin Crossc511bc52020-04-07 16:50:32 +0000215func (mod *Module) AlwaysSdk() bool {
216 return false
217}
218
Jiyong Park2286afd2020-06-16 21:58:53 +0900219func (mod *Module) IsSdkVariant() bool {
220 return false
221}
222
Colin Cross1348ce32020-10-01 13:37:16 -0700223func (mod *Module) SplitPerApiLevel() bool {
224 return false
225}
226
Ivan Lozanoffee3342019-08-27 12:03:00 -0700227type Deps struct {
228 Dylibs []string
229 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700230 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400231 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700232 ProcMacros []string
233 SharedLibs []string
234 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800235 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700236
237 CrtBegin, CrtEnd string
238}
239
240type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400241 DyLibs RustLibraries
242 RLibs RustLibraries
243 SharedLibs android.Paths
244 StaticLibs android.Paths
245 ProcMacros RustLibraries
246 linkDirs []string
247 depFlags []string
248 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700249 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700250
Ivan Lozano45901ed2020-07-24 16:05:01 -0400251 // Used by bindgen modules which call clang
252 depClangFlags []string
253 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400254 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400255 depSystemIncludePaths android.Paths
256
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400257 coverageFiles android.Paths
258
Ivan Lozanof1c84332019-09-20 11:00:37 -0700259 CrtBegin android.OptionalPath
260 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700261
262 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500263 SrcDeps android.Paths
264 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265}
266
267type RustLibraries []RustLibrary
268
269type RustLibrary struct {
270 Path android.Path
271 CrateName string
272}
273
274type compiler interface {
275 compilerFlags(ctx ModuleContext, flags Flags) Flags
276 compilerProps() []interface{}
277 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
278 compilerDeps(ctx DepsContext, deps Deps) Deps
279 crateName() string
280
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800281 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200282 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400284
285 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400286
287 Disabled() bool
288 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400289
Ivan Lozanodd055472020-09-28 13:22:45 -0400290 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400291}
292
Matthew Maurerbb3add12020-06-25 09:34:12 -0700293type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700294 exportLinkDirs(...string)
295 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400296 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700297}
298
299type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400300 depFlags []string
301 linkDirs []string
302 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700303}
304
Matthew Maurerbb3add12020-06-25 09:34:12 -0700305func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
306 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
307}
308
309func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
310 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
311}
312
Ivan Lozano2093af22020-08-25 12:48:19 -0400313func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
314 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
315}
316
Colin Cross0de8a1e2020-09-18 14:15:30 -0700317func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
318 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
319 Flags: flagExporter.depFlags,
320 LinkDirs: flagExporter.linkDirs,
321 LinkObjects: flagExporter.linkObjects,
322 })
323}
324
Matthew Maurerbb3add12020-06-25 09:34:12 -0700325var _ exportedFlagsProducer = (*flagExporter)(nil)
326
327func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700328 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700329}
330
Colin Cross0de8a1e2020-09-18 14:15:30 -0700331type FlagExporterInfo struct {
332 Flags []string
333 LinkDirs []string // TODO: this should be android.Paths
334 LinkObjects []string // TODO: this should be android.Paths
335}
336
337var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
338
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400339func (mod *Module) isCoverageVariant() bool {
340 return mod.coverage.Properties.IsCoverageVariant
341}
342
343var _ cc.Coverage = (*Module)(nil)
344
345func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
346 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
347}
348
349func (mod *Module) PreventInstall() {
350 mod.Properties.PreventInstall = true
351}
352
353func (mod *Module) HideFromMake() {
354 mod.Properties.HideFromMake = true
355}
356
357func (mod *Module) MarkAsCoverageVariant(coverage bool) {
358 mod.coverage.Properties.IsCoverageVariant = coverage
359}
360
361func (mod *Module) EnableCoverageIfNeeded() {
362 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700363}
364
365func defaultsFactory() android.Module {
366 return DefaultsFactory()
367}
368
369type Defaults struct {
370 android.ModuleBase
371 android.DefaultsModuleBase
372}
373
374func DefaultsFactory(props ...interface{}) android.Module {
375 module := &Defaults{}
376
377 module.AddProperties(props...)
378 module.AddProperties(
379 &BaseProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400380 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700381 &BaseCompilerProperties{},
382 &BinaryCompilerProperties{},
383 &LibraryCompilerProperties{},
384 &ProcMacroCompilerProperties{},
385 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400386 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700387 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400388 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400389 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200390 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700391 )
392
393 android.InitDefaultsModule(module)
394 return module
395}
396
397func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700398 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399}
400
Ivan Lozano183a3212019-10-18 14:18:45 -0700401func (mod *Module) CcLibrary() bool {
402 if mod.compiler != nil {
403 if _, ok := mod.compiler.(*libraryDecorator); ok {
404 return true
405 }
406 }
407 return false
408}
409
410func (mod *Module) CcLibraryInterface() bool {
411 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400412 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
413 // VariantIs{Static,Shared} is set.
414 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700415 return true
416 }
417 }
418 return false
419}
420
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800421func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700422 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700423 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800424 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700425 }
426 }
427 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
428}
429
430func (mod *Module) SetStatic() {
431 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700432 if library, ok := mod.compiler.(libraryInterface); ok {
433 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700434 return
435 }
436 }
437 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
438}
439
440func (mod *Module) SetShared() {
441 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700442 if library, ok := mod.compiler.(libraryInterface); ok {
443 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700444 return
445 }
446 }
447 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
448}
449
Ivan Lozano183a3212019-10-18 14:18:45 -0700450func (mod *Module) BuildStaticVariant() bool {
451 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700452 if library, ok := mod.compiler.(libraryInterface); ok {
453 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700454 }
455 }
456 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
457}
458
459func (mod *Module) BuildSharedVariant() bool {
460 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700461 if library, ok := mod.compiler.(libraryInterface); ok {
462 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700463 }
464 }
465 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
466}
467
Ivan Lozano183a3212019-10-18 14:18:45 -0700468func (mod *Module) Module() android.Module {
469 return mod
470}
471
Ivan Lozano183a3212019-10-18 14:18:45 -0700472func (mod *Module) OutputFile() android.OptionalPath {
473 return mod.outputFile
474}
475
476func (mod *Module) InRecovery() bool {
477 // For now, Rust has no notion of the recovery image
478 return false
479}
Ivan Lozano183a3212019-10-18 14:18:45 -0700480
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400481func (mod *Module) CoverageFiles() android.Paths {
482 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700483 if !mod.compiler.nativeCoverage() {
484 return android.Paths{}
485 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400486 if library, ok := mod.compiler.(*libraryDecorator); ok {
487 if library.coverageFile != nil {
488 return android.Paths{library.coverageFile}
489 }
490 return android.Paths{}
491 }
492 }
493 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
494}
495
Ivan Lozano183a3212019-10-18 14:18:45 -0700496var _ cc.LinkableInterface = (*Module)(nil)
497
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498func (mod *Module) Init() android.Module {
499 mod.AddProperties(&mod.Properties)
500
501 if mod.compiler != nil {
502 mod.AddProperties(mod.compiler.compilerProps()...)
503 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400504 if mod.coverage != nil {
505 mod.AddProperties(mod.coverage.props()...)
506 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200507 if mod.clippy != nil {
508 mod.AddProperties(mod.clippy.props()...)
509 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400510 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700511 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400512 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513
Ivan Lozanoffee3342019-08-27 12:03:00 -0700514 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900515 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700516
517 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518 return mod
519}
520
521func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
522 return &Module{
523 hod: hod,
524 multilib: multilib,
525 }
526}
527func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
528 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400529 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200530 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531 return module
532}
533
534type ModuleContext interface {
535 android.ModuleContext
536 ModuleContextIntf
537}
538
539type BaseModuleContext interface {
540 android.BaseModuleContext
541 ModuleContextIntf
542}
543
544type DepsContext interface {
545 android.BottomUpMutatorContext
546 ModuleContextIntf
547}
548
549type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200550 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700552}
553
554type depsContext struct {
555 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556}
557
558type moduleContext struct {
559 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560}
561
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200562type baseModuleContext struct {
563 android.BaseModuleContext
564}
565
566func (ctx *moduleContext) RustModule() *Module {
567 return ctx.Module().(*Module)
568}
569
570func (ctx *moduleContext) toolchain() config.Toolchain {
571 return ctx.RustModule().toolchain(ctx)
572}
573
574func (ctx *depsContext) RustModule() *Module {
575 return ctx.Module().(*Module)
576}
577
578func (ctx *depsContext) toolchain() config.Toolchain {
579 return ctx.RustModule().toolchain(ctx)
580}
581
582func (ctx *baseModuleContext) RustModule() *Module {
583 return ctx.Module().(*Module)
584}
585
586func (ctx *baseModuleContext) toolchain() config.Toolchain {
587 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400588}
589
590func (mod *Module) nativeCoverage() bool {
591 return mod.compiler != nil && mod.compiler.nativeCoverage()
592}
593
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
595 if mod.cachedToolchain == nil {
596 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
597 }
598 return mod.cachedToolchain
599}
600
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200601func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
602 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
603}
604
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
606}
607
608func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
609 ctx := &moduleContext{
610 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700612
Jiyong Park99644e92020-11-17 22:21:02 +0900613 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
614 if !apexInfo.IsForPlatform() {
615 mod.hideApexVariantFromMake = true
616 }
617
Ivan Lozanoffee3342019-08-27 12:03:00 -0700618 toolchain := mod.toolchain(ctx)
619
620 if !toolchain.Supported() {
621 // This toolchain's unsupported, there's nothing to do for this mod.
622 return
623 }
624
625 deps := mod.depsToPaths(ctx)
626 flags := Flags{
627 Toolchain: toolchain,
628 }
629
630 if mod.compiler != nil {
631 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400632 }
633 if mod.coverage != nil {
634 flags, deps = mod.coverage.flags(ctx, flags, deps)
635 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200636 if mod.clippy != nil {
637 flags, deps = mod.clippy.flags(ctx, flags, deps)
638 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400639
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200640 // SourceProvider needs to call GenerateSource() before compiler calls
641 // compile() so it can provide the source. A SourceProvider has
642 // multiple variants (e.g. source, rlib, dylib). Only the "source"
643 // variant is responsible for effectively generating the source. The
644 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400645 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200646 if mod.compiler.(libraryInterface).source() {
647 mod.sourceProvider.GenerateSource(ctx, deps)
648 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
649 } else {
650 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
651 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700652 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200653 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400654 }
655
656 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400658
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400660 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200661 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400662 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663 }
664}
665
666func (mod *Module) deps(ctx DepsContext) Deps {
667 deps := Deps{}
668
669 if mod.compiler != nil {
670 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400671 }
672 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700673 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674 }
675
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400676 if mod.coverage != nil {
677 deps = mod.coverage.deps(ctx, deps)
678 }
679
Ivan Lozanoffee3342019-08-27 12:03:00 -0700680 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
681 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700682 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
684 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
685 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
686
687 return deps
688
689}
690
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691type dependencyTag struct {
692 blueprint.BaseDependencyTag
693 name string
694 library bool
695 proc_macro bool
696}
697
Jiyong Park65b62242020-11-25 12:44:59 +0900698// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
699// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
700func (d dependencyTag) InstallDepNeeded() bool {
701 return d.library || d.proc_macro
702}
703
704var _ android.InstallNeededDependencyTag = dependencyTag{}
705
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400707 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
708 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
709 dylibDepTag = dependencyTag{name: "dylib", library: true}
710 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
711 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200712 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700713)
714
Jiyong Park99644e92020-11-17 22:21:02 +0900715func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
716 tag, ok := depTag.(dependencyTag)
717 return ok && tag == dylibDepTag
718}
719
Matthew Maurer0f003b12020-06-29 14:34:06 -0700720type autoDep struct {
721 variation string
722 depTag dependencyTag
723}
724
725var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200726 rlibVariation = "rlib"
727 dylibVariation = "dylib"
728 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
729 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700730)
731
732type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400733 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700734}
735
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400736func (mod *Module) begin(ctx BaseModuleContext) {
737 if mod.coverage != nil {
738 mod.coverage.begin(ctx)
739 }
740}
741
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
743 var depPaths PathDeps
744
745 directRlibDeps := []*Module{}
746 directDylibDeps := []*Module{}
747 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700748 directSharedLibDeps := [](cc.LinkableInterface){}
749 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400750 directSrcProvidersDeps := []*Module{}
751 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752
753 ctx.VisitDirectDeps(func(dep android.Module) {
754 depName := ctx.OtherModuleName(dep)
755 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400756 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700758
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759 switch depTag {
760 case dylibDepTag:
761 dylib, ok := rustDep.compiler.(libraryInterface)
762 if !ok || !dylib.dylib() {
763 ctx.ModuleErrorf("mod %q not an dylib library", depName)
764 return
765 }
766 directDylibDeps = append(directDylibDeps, rustDep)
767 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
768 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400769
Ivan Lozanoffee3342019-08-27 12:03:00 -0700770 rlib, ok := rustDep.compiler.(libraryInterface)
771 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400772 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773 return
774 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400775 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700776 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400777 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 case procMacroDepTag:
779 directProcMacroDeps = append(directProcMacroDeps, rustDep)
780 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400781 case android.SourceDepTag:
782 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
783 // OS/Arch variant is used.
784 var helper string
785 if ctx.Host() {
786 helper = "missing 'host_supported'?"
787 } else {
788 helper = "device module defined?"
789 }
790
791 if dep.Target().Os != ctx.Os() {
792 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
793 return
794 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
795 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
796 return
797 }
798 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 }
800
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400801 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700802 if depTag != procMacroDepTag {
803 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
804 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
805 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
806 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 }
808
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400810 linkFile := rustDep.outputFile
811 if !linkFile.Valid() {
812 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
813 depName, ctx.ModuleName())
814 return
815 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700817 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
818 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 }
820 }
821
Ivan Lozano89435d12020-07-31 11:01:18 -0400822 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700823 //Handle C dependencies
824 if _, ok := ccDep.(*Module); !ok {
825 if ccDep.Module().Target().Os != ctx.Os() {
826 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
827 return
828 }
829 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
830 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
831 return
832 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700833 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400834 linkObject := ccDep.OutputFile()
835 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500836
Ivan Lozano2093af22020-08-25 12:48:19 -0400837 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700838 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
839 }
840
841 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700842 switch {
843 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400845 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700846 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
847 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
848 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
849 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
850 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400851 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852 directStaticLibDeps = append(directStaticLibDeps, ccDep)
853 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700854 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400856 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700857 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
858 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
859 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
860 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
861 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 directSharedLibDeps = append(directSharedLibDeps, ccDep)
863 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
864 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800865 case cc.IsHeaderDepTag(depTag):
866 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
867 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
868 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
869 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700870 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400871 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700872 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400873 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 }
875
876 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700877 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
878 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400879 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400882
883 if srcDep, ok := dep.(android.SourceFileProducer); ok {
884 switch depTag {
885 case android.SourceDepTag:
886 // These are usually genrules which don't have per-target variants.
887 directSrcDeps = append(directSrcDeps, srcDep)
888 }
889 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 })
891
892 var rlibDepFiles RustLibraries
893 for _, dep := range directRlibDeps {
894 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
895 }
896 var dylibDepFiles RustLibraries
897 for _, dep := range directDylibDeps {
898 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
899 }
900 var procMacroDepFiles RustLibraries
901 for _, dep := range directProcMacroDeps {
902 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
903 }
904
905 var staticLibDepFiles android.Paths
906 for _, dep := range directStaticLibDeps {
907 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
908 }
909
910 var sharedLibDepFiles android.Paths
911 for _, dep := range directSharedLibDeps {
912 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
913 }
914
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400915 var srcProviderDepFiles android.Paths
916 for _, dep := range directSrcProvidersDeps {
917 srcs, _ := dep.OutputFiles("")
918 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
919 }
920 for _, dep := range directSrcDeps {
921 srcs := dep.Srcs()
922 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
923 }
924
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
926 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
927 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
928 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
929 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400930 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931
932 // Dedup exported flags from dependencies
933 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
934 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400935 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
936 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
937 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938
939 return depPaths
940}
941
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800942func (mod *Module) InstallInData() bool {
943 if mod.compiler == nil {
944 return false
945 }
946 return mod.compiler.inData()
947}
948
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949func linkPathFromFilePath(filepath android.Path) string {
950 return strings.Split(filepath.String(), filepath.Base())[0]
951}
Ivan Lozanod648c432020-02-06 12:05:10 -0500952
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
954 ctx := &depsContext{
955 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957
958 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700959 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700960 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700961 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800962 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400964
Ivan Lozano2b081132020-09-08 12:46:52 -0400965 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400966 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400967 stdLinkage = "rlib-std"
968 }
969
970 rlibDepVariations := commonDepVariations
971 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
972 rlibDepVariations = append(rlibDepVariations,
973 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
974 }
975
Ivan Lozano52767be2019-10-18 14:49:46 -0700976 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400977 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200978 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700979 rlibDepTag, deps.Rlibs...)
980 actx.AddVariationDependencies(
981 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200982 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700983 dylibDepTag, deps.Dylibs...)
984
Ivan Lozano042504f2020-08-18 14:31:23 -0400985 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
986 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400987 if autoDep.depTag == rlibDepTag {
988 actx.AddVariationDependencies(
989 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
990 autoDep.depTag, deps.Rustlibs...)
991 } else {
992 actx.AddVariationDependencies(
993 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
994 autoDep.depTag, deps.Rustlibs...)
995 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700996 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400997 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400998 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400999 actx.AddVariationDependencies(
1000 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1001 rlibDepTag, deps.Stdlibs...)
1002 } else {
1003 actx.AddVariationDependencies(
1004 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1005 dylibDepTag, deps.Stdlibs...)
1006 }
1007 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001008 actx.AddVariationDependencies(append(commonDepVariations,
1009 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001010 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001011 actx.AddVariationDependencies(append(commonDepVariations,
1012 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001013 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001014
Zach Johnson3df4e632020-11-06 11:56:27 -08001015 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1016
Colin Cross565cafd2020-09-25 18:47:38 -07001017 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001018 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001019 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001020 }
1021 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001022 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001023 }
1024
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001025 if mod.sourceProvider != nil {
1026 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1027 bindgen.Properties.Custom_bindgen != "" {
1028 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1029 bindgen.Properties.Custom_bindgen)
1030 }
1031 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001032 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001033 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034}
1035
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001036func BeginMutator(ctx android.BottomUpMutatorContext) {
1037 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1038 mod.beginMutator(ctx)
1039 }
1040}
1041
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001042func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1043 ctx := &baseModuleContext{
1044 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001045 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001046
1047 mod.begin(ctx)
1048}
1049
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050func (mod *Module) Name() string {
1051 name := mod.ModuleBase.Name()
1052 if p, ok := mod.compiler.(interface {
1053 Name(string) string
1054 }); ok {
1055 name = p.Name(name)
1056 }
1057 return name
1058}
1059
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001060func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001061 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001062 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001063 }
1064}
1065
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001066var _ android.HostToolProvider = (*Module)(nil)
1067
1068func (mod *Module) HostToolPath() android.OptionalPath {
1069 if !mod.Host() {
1070 return android.OptionalPath{}
1071 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001072 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1073 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001074 }
1075 return android.OptionalPath{}
1076}
1077
Jiyong Park99644e92020-11-17 22:21:02 +09001078var _ android.ApexModule = (*Module)(nil)
1079
1080func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
1081 return nil
1082}
1083
1084func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1085 depTag := ctx.OtherModuleDependencyTag(dep)
1086
1087 if ccm, ok := dep.(*cc.Module); ok {
1088 if ccm.HasStubsVariants() {
1089 if cc.IsSharedDepTag(depTag) {
1090 // dynamic dep to a stubs lib crosses APEX boundary
1091 return false
1092 }
1093 if cc.IsRuntimeDepTag(depTag) {
1094 // runtime dep to a stubs lib also crosses APEX boundary
1095 return false
1096 }
1097
1098 if cc.IsHeaderDepTag(depTag) {
1099 return false
1100 }
1101 }
1102 if mod.Static() && cc.IsSharedDepTag(depTag) {
1103 // shared_lib dependency from a static lib is considered as crossing
1104 // the APEX boundary because the dependency doesn't actually is
1105 // linked; the dependency is used only during the compilation phase.
1106 return false
1107 }
1108 }
1109
1110 if depTag == procMacroDepTag {
1111 return false
1112 }
1113
1114 return true
1115}
1116
1117// Overrides ApexModule.IsInstallabeToApex()
1118func (mod *Module) IsInstallableToApex() bool {
1119 if mod.compiler != nil {
1120 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1121 return true
1122 }
1123 if _, ok := mod.compiler.(*binaryDecorator); ok {
1124 return true
1125 }
1126 }
1127 return false
1128}
1129
Ivan Lozanoffee3342019-08-27 12:03:00 -07001130var Bool = proptools.Bool
1131var BoolDefault = proptools.BoolDefault
1132var String = proptools.String
1133var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001134
1135var _ android.OutputFileProducer = (*Module)(nil)