blob: b277afce54cbe9a4d61a4faf1ae45c887d8c421f [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
263 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264}
265
266type RustLibraries []RustLibrary
267
268type RustLibrary struct {
269 Path android.Path
270 CrateName string
271}
272
273type compiler interface {
274 compilerFlags(ctx ModuleContext, flags Flags) Flags
275 compilerProps() []interface{}
276 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
277 compilerDeps(ctx DepsContext, deps Deps) Deps
278 crateName() string
279
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800280 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200281 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700282 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400283
284 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400285
286 Disabled() bool
287 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400288
Ivan Lozanodd055472020-09-28 13:22:45 -0400289 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400290}
291
Matthew Maurerbb3add12020-06-25 09:34:12 -0700292type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700293 exportLinkDirs(...string)
294 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400295 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700296}
297
298type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400299 depFlags []string
300 linkDirs []string
301 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700302}
303
Matthew Maurerbb3add12020-06-25 09:34:12 -0700304func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
305 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
306}
307
308func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
309 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
310}
311
Ivan Lozano2093af22020-08-25 12:48:19 -0400312func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
313 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
314}
315
Colin Cross0de8a1e2020-09-18 14:15:30 -0700316func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
317 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
318 Flags: flagExporter.depFlags,
319 LinkDirs: flagExporter.linkDirs,
320 LinkObjects: flagExporter.linkObjects,
321 })
322}
323
Matthew Maurerbb3add12020-06-25 09:34:12 -0700324var _ exportedFlagsProducer = (*flagExporter)(nil)
325
326func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700327 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700328}
329
Colin Cross0de8a1e2020-09-18 14:15:30 -0700330type FlagExporterInfo struct {
331 Flags []string
332 LinkDirs []string // TODO: this should be android.Paths
333 LinkObjects []string // TODO: this should be android.Paths
334}
335
336var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
337
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400338func (mod *Module) isCoverageVariant() bool {
339 return mod.coverage.Properties.IsCoverageVariant
340}
341
342var _ cc.Coverage = (*Module)(nil)
343
344func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
345 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
346}
347
348func (mod *Module) PreventInstall() {
349 mod.Properties.PreventInstall = true
350}
351
352func (mod *Module) HideFromMake() {
353 mod.Properties.HideFromMake = true
354}
355
356func (mod *Module) MarkAsCoverageVariant(coverage bool) {
357 mod.coverage.Properties.IsCoverageVariant = coverage
358}
359
360func (mod *Module) EnableCoverageIfNeeded() {
361 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700362}
363
364func defaultsFactory() android.Module {
365 return DefaultsFactory()
366}
367
368type Defaults struct {
369 android.ModuleBase
370 android.DefaultsModuleBase
371}
372
373func DefaultsFactory(props ...interface{}) android.Module {
374 module := &Defaults{}
375
376 module.AddProperties(props...)
377 module.AddProperties(
378 &BaseProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400379 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700380 &BaseCompilerProperties{},
381 &BinaryCompilerProperties{},
382 &LibraryCompilerProperties{},
383 &ProcMacroCompilerProperties{},
384 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400385 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700386 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400387 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400388 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200389 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700390 )
391
392 android.InitDefaultsModule(module)
393 return module
394}
395
396func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700397 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700398}
399
Ivan Lozano183a3212019-10-18 14:18:45 -0700400func (mod *Module) CcLibrary() bool {
401 if mod.compiler != nil {
402 if _, ok := mod.compiler.(*libraryDecorator); ok {
403 return true
404 }
405 }
406 return false
407}
408
409func (mod *Module) CcLibraryInterface() bool {
410 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400411 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
412 // VariantIs{Static,Shared} is set.
413 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700414 return true
415 }
416 }
417 return false
418}
419
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800420func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700421 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700422 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800423 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700424 }
425 }
426 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
427}
428
429func (mod *Module) SetStatic() {
430 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700431 if library, ok := mod.compiler.(libraryInterface); ok {
432 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700433 return
434 }
435 }
436 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
437}
438
439func (mod *Module) SetShared() {
440 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700441 if library, ok := mod.compiler.(libraryInterface); ok {
442 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700443 return
444 }
445 }
446 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
447}
448
Ivan Lozano183a3212019-10-18 14:18:45 -0700449func (mod *Module) BuildStaticVariant() bool {
450 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700451 if library, ok := mod.compiler.(libraryInterface); ok {
452 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700453 }
454 }
455 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
456}
457
458func (mod *Module) BuildSharedVariant() bool {
459 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700460 if library, ok := mod.compiler.(libraryInterface); ok {
461 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700462 }
463 }
464 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
465}
466
Ivan Lozano183a3212019-10-18 14:18:45 -0700467func (mod *Module) Module() android.Module {
468 return mod
469}
470
Ivan Lozano183a3212019-10-18 14:18:45 -0700471func (mod *Module) OutputFile() android.OptionalPath {
472 return mod.outputFile
473}
474
475func (mod *Module) InRecovery() bool {
476 // For now, Rust has no notion of the recovery image
477 return false
478}
Ivan Lozano183a3212019-10-18 14:18:45 -0700479
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400480func (mod *Module) CoverageFiles() android.Paths {
481 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700482 if !mod.compiler.nativeCoverage() {
483 return android.Paths{}
484 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400485 if library, ok := mod.compiler.(*libraryDecorator); ok {
486 if library.coverageFile != nil {
487 return android.Paths{library.coverageFile}
488 }
489 return android.Paths{}
490 }
491 }
492 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
493}
494
Ivan Lozano183a3212019-10-18 14:18:45 -0700495var _ cc.LinkableInterface = (*Module)(nil)
496
Ivan Lozanoffee3342019-08-27 12:03:00 -0700497func (mod *Module) Init() android.Module {
498 mod.AddProperties(&mod.Properties)
499
500 if mod.compiler != nil {
501 mod.AddProperties(mod.compiler.compilerProps()...)
502 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400503 if mod.coverage != nil {
504 mod.AddProperties(mod.coverage.props()...)
505 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200506 if mod.clippy != nil {
507 mod.AddProperties(mod.clippy.props()...)
508 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400509 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700510 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400511 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400512
Ivan Lozanoffee3342019-08-27 12:03:00 -0700513 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900514 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700515
516 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700517 return mod
518}
519
520func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
521 return &Module{
522 hod: hod,
523 multilib: multilib,
524 }
525}
526func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
527 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400528 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200529 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700530 return module
531}
532
533type ModuleContext interface {
534 android.ModuleContext
535 ModuleContextIntf
536}
537
538type BaseModuleContext interface {
539 android.BaseModuleContext
540 ModuleContextIntf
541}
542
543type DepsContext interface {
544 android.BottomUpMutatorContext
545 ModuleContextIntf
546}
547
548type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200549 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700550 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551}
552
553type depsContext struct {
554 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555}
556
557type moduleContext struct {
558 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700559}
560
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200561type baseModuleContext struct {
562 android.BaseModuleContext
563}
564
565func (ctx *moduleContext) RustModule() *Module {
566 return ctx.Module().(*Module)
567}
568
569func (ctx *moduleContext) toolchain() config.Toolchain {
570 return ctx.RustModule().toolchain(ctx)
571}
572
573func (ctx *depsContext) RustModule() *Module {
574 return ctx.Module().(*Module)
575}
576
577func (ctx *depsContext) toolchain() config.Toolchain {
578 return ctx.RustModule().toolchain(ctx)
579}
580
581func (ctx *baseModuleContext) RustModule() *Module {
582 return ctx.Module().(*Module)
583}
584
585func (ctx *baseModuleContext) toolchain() config.Toolchain {
586 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400587}
588
589func (mod *Module) nativeCoverage() bool {
590 return mod.compiler != nil && mod.compiler.nativeCoverage()
591}
592
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
594 if mod.cachedToolchain == nil {
595 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
596 }
597 return mod.cachedToolchain
598}
599
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200600func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
601 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
602}
603
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
605}
606
607func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
608 ctx := &moduleContext{
609 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611
Jiyong Park99644e92020-11-17 22:21:02 +0900612 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
613 if !apexInfo.IsForPlatform() {
614 mod.hideApexVariantFromMake = true
615 }
616
Ivan Lozanoffee3342019-08-27 12:03:00 -0700617 toolchain := mod.toolchain(ctx)
618
619 if !toolchain.Supported() {
620 // This toolchain's unsupported, there's nothing to do for this mod.
621 return
622 }
623
624 deps := mod.depsToPaths(ctx)
625 flags := Flags{
626 Toolchain: toolchain,
627 }
628
629 if mod.compiler != nil {
630 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400631 }
632 if mod.coverage != nil {
633 flags, deps = mod.coverage.flags(ctx, flags, deps)
634 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200635 if mod.clippy != nil {
636 flags, deps = mod.clippy.flags(ctx, flags, deps)
637 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400638
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200639 // SourceProvider needs to call GenerateSource() before compiler calls
640 // compile() so it can provide the source. A SourceProvider has
641 // multiple variants (e.g. source, rlib, dylib). Only the "source"
642 // variant is responsible for effectively generating the source. The
643 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400644 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200645 if mod.compiler.(libraryInterface).source() {
646 mod.sourceProvider.GenerateSource(ctx, deps)
647 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
648 } else {
649 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
650 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700651 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200652 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400653 }
654
655 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700656 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400657
Ivan Lozanoffee3342019-08-27 12:03:00 -0700658 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400659 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200660 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400661 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662 }
663}
664
665func (mod *Module) deps(ctx DepsContext) Deps {
666 deps := Deps{}
667
668 if mod.compiler != nil {
669 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400670 }
671 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700672 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673 }
674
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400675 if mod.coverage != nil {
676 deps = mod.coverage.deps(ctx, deps)
677 }
678
Ivan Lozanoffee3342019-08-27 12:03:00 -0700679 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
680 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700681 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
683 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
684 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
685
686 return deps
687
688}
689
Ivan Lozanoffee3342019-08-27 12:03:00 -0700690type dependencyTag struct {
691 blueprint.BaseDependencyTag
692 name string
693 library bool
694 proc_macro bool
695}
696
697var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400698 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
699 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
700 dylibDepTag = dependencyTag{name: "dylib", library: true}
701 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
702 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200703 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704)
705
Jiyong Park99644e92020-11-17 22:21:02 +0900706func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
707 tag, ok := depTag.(dependencyTag)
708 return ok && tag == dylibDepTag
709}
710
Matthew Maurer0f003b12020-06-29 14:34:06 -0700711type autoDep struct {
712 variation string
713 depTag dependencyTag
714}
715
716var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200717 rlibVariation = "rlib"
718 dylibVariation = "dylib"
719 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
720 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700721)
722
723type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400724 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700725}
726
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400727func (mod *Module) begin(ctx BaseModuleContext) {
728 if mod.coverage != nil {
729 mod.coverage.begin(ctx)
730 }
731}
732
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
734 var depPaths PathDeps
735
736 directRlibDeps := []*Module{}
737 directDylibDeps := []*Module{}
738 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700739 directSharedLibDeps := [](cc.LinkableInterface){}
740 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400741 directSrcProvidersDeps := []*Module{}
742 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743
744 ctx.VisitDirectDeps(func(dep android.Module) {
745 depName := ctx.OtherModuleName(dep)
746 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400747 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700749
Ivan Lozanoffee3342019-08-27 12:03:00 -0700750 switch depTag {
751 case dylibDepTag:
752 dylib, ok := rustDep.compiler.(libraryInterface)
753 if !ok || !dylib.dylib() {
754 ctx.ModuleErrorf("mod %q not an dylib library", depName)
755 return
756 }
757 directDylibDeps = append(directDylibDeps, rustDep)
758 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
759 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400760
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 rlib, ok := rustDep.compiler.(libraryInterface)
762 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400763 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764 return
765 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700767 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400768 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700769 case procMacroDepTag:
770 directProcMacroDeps = append(directProcMacroDeps, rustDep)
771 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400772 case android.SourceDepTag:
773 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
774 // OS/Arch variant is used.
775 var helper string
776 if ctx.Host() {
777 helper = "missing 'host_supported'?"
778 } else {
779 helper = "device module defined?"
780 }
781
782 if dep.Target().Os != ctx.Os() {
783 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
784 return
785 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
786 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
787 return
788 }
789 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700790 }
791
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400792 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700793 if depTag != procMacroDepTag {
794 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
795 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
796 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
797 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 }
799
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400801 linkFile := rustDep.outputFile
802 if !linkFile.Valid() {
803 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
804 depName, ctx.ModuleName())
805 return
806 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700808 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
809 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810 }
811 }
812
Ivan Lozano89435d12020-07-31 11:01:18 -0400813 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700814 //Handle C dependencies
815 if _, ok := ccDep.(*Module); !ok {
816 if ccDep.Module().Target().Os != ctx.Os() {
817 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
818 return
819 }
820 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
821 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
822 return
823 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700824 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400825 linkObject := ccDep.OutputFile()
826 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500827
Ivan Lozano2093af22020-08-25 12:48:19 -0400828 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
830 }
831
832 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700833 switch {
834 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700835 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400836 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700837 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
838 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
839 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
840 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
841 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400842 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700843 directStaticLibDeps = append(directStaticLibDeps, ccDep)
844 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700845 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400847 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700848 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
849 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
850 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
851 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
852 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 directSharedLibDeps = append(directSharedLibDeps, ccDep)
854 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
855 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800856 case cc.IsHeaderDepTag(depTag):
857 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.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700861 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400862 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700863 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400864 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865 }
866
867 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700868 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
869 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400870 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700871 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400873
874 if srcDep, ok := dep.(android.SourceFileProducer); ok {
875 switch depTag {
876 case android.SourceDepTag:
877 // These are usually genrules which don't have per-target variants.
878 directSrcDeps = append(directSrcDeps, srcDep)
879 }
880 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 })
882
883 var rlibDepFiles RustLibraries
884 for _, dep := range directRlibDeps {
885 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
886 }
887 var dylibDepFiles RustLibraries
888 for _, dep := range directDylibDeps {
889 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
890 }
891 var procMacroDepFiles RustLibraries
892 for _, dep := range directProcMacroDeps {
893 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
894 }
895
896 var staticLibDepFiles android.Paths
897 for _, dep := range directStaticLibDeps {
898 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
899 }
900
901 var sharedLibDepFiles android.Paths
902 for _, dep := range directSharedLibDeps {
903 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
904 }
905
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400906 var srcProviderDepFiles android.Paths
907 for _, dep := range directSrcProvidersDeps {
908 srcs, _ := dep.OutputFiles("")
909 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
910 }
911 for _, dep := range directSrcDeps {
912 srcs := dep.Srcs()
913 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
914 }
915
Ivan Lozanoffee3342019-08-27 12:03:00 -0700916 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
917 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
918 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
919 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
920 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400921 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700922
923 // Dedup exported flags from dependencies
924 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
925 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400926 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
927 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
928 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700929
930 return depPaths
931}
932
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800933func (mod *Module) InstallInData() bool {
934 if mod.compiler == nil {
935 return false
936 }
937 return mod.compiler.inData()
938}
939
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940func linkPathFromFilePath(filepath android.Path) string {
941 return strings.Split(filepath.String(), filepath.Base())[0]
942}
Ivan Lozanod648c432020-02-06 12:05:10 -0500943
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
945 ctx := &depsContext{
946 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700947 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948
949 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700950 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700952 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800953 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700954 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400955
Ivan Lozano2b081132020-09-08 12:46:52 -0400956 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400957 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400958 stdLinkage = "rlib-std"
959 }
960
961 rlibDepVariations := commonDepVariations
962 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
963 rlibDepVariations = append(rlibDepVariations,
964 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
965 }
966
Ivan Lozano52767be2019-10-18 14:49:46 -0700967 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400968 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200969 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700970 rlibDepTag, deps.Rlibs...)
971 actx.AddVariationDependencies(
972 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200973 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700974 dylibDepTag, deps.Dylibs...)
975
Ivan Lozano042504f2020-08-18 14:31:23 -0400976 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
977 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400978 if autoDep.depTag == rlibDepTag {
979 actx.AddVariationDependencies(
980 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
981 autoDep.depTag, deps.Rustlibs...)
982 } else {
983 actx.AddVariationDependencies(
984 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
985 autoDep.depTag, deps.Rustlibs...)
986 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700987 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400988 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400989 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400990 actx.AddVariationDependencies(
991 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
992 rlibDepTag, deps.Stdlibs...)
993 } else {
994 actx.AddVariationDependencies(
995 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
996 dylibDepTag, deps.Stdlibs...)
997 }
998 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700999 actx.AddVariationDependencies(append(commonDepVariations,
1000 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001001 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001002 actx.AddVariationDependencies(append(commonDepVariations,
1003 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001004 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001005
Zach Johnson3df4e632020-11-06 11:56:27 -08001006 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1007
Colin Cross565cafd2020-09-25 18:47:38 -07001008 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001009 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001010 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001011 }
1012 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001013 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001014 }
1015
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001016 if mod.sourceProvider != nil {
1017 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1018 bindgen.Properties.Custom_bindgen != "" {
1019 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1020 bindgen.Properties.Custom_bindgen)
1021 }
1022 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001023 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001024 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025}
1026
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001027func BeginMutator(ctx android.BottomUpMutatorContext) {
1028 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1029 mod.beginMutator(ctx)
1030 }
1031}
1032
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001033func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1034 ctx := &baseModuleContext{
1035 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001036 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001037
1038 mod.begin(ctx)
1039}
1040
Ivan Lozanoffee3342019-08-27 12:03:00 -07001041func (mod *Module) Name() string {
1042 name := mod.ModuleBase.Name()
1043 if p, ok := mod.compiler.(interface {
1044 Name(string) string
1045 }); ok {
1046 name = p.Name(name)
1047 }
1048 return name
1049}
1050
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001051func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001052 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001053 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001054 }
1055}
1056
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001057var _ android.HostToolProvider = (*Module)(nil)
1058
1059func (mod *Module) HostToolPath() android.OptionalPath {
1060 if !mod.Host() {
1061 return android.OptionalPath{}
1062 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001063 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1064 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001065 }
1066 return android.OptionalPath{}
1067}
1068
Jiyong Park99644e92020-11-17 22:21:02 +09001069var _ android.ApexModule = (*Module)(nil)
1070
1071func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
1072 return nil
1073}
1074
1075func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1076 depTag := ctx.OtherModuleDependencyTag(dep)
1077
1078 if ccm, ok := dep.(*cc.Module); ok {
1079 if ccm.HasStubsVariants() {
1080 if cc.IsSharedDepTag(depTag) {
1081 // dynamic dep to a stubs lib crosses APEX boundary
1082 return false
1083 }
1084 if cc.IsRuntimeDepTag(depTag) {
1085 // runtime dep to a stubs lib also crosses APEX boundary
1086 return false
1087 }
1088
1089 if cc.IsHeaderDepTag(depTag) {
1090 return false
1091 }
1092 }
1093 if mod.Static() && cc.IsSharedDepTag(depTag) {
1094 // shared_lib dependency from a static lib is considered as crossing
1095 // the APEX boundary because the dependency doesn't actually is
1096 // linked; the dependency is used only during the compilation phase.
1097 return false
1098 }
1099 }
1100
1101 if depTag == procMacroDepTag {
1102 return false
1103 }
1104
1105 return true
1106}
1107
1108// Overrides ApexModule.IsInstallabeToApex()
1109func (mod *Module) IsInstallableToApex() bool {
1110 if mod.compiler != nil {
1111 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1112 return true
1113 }
1114 if _, ok := mod.compiler.(*binaryDecorator); ok {
1115 return true
1116 }
1117 }
1118 return false
1119}
1120
Ivan Lozanoffee3342019-08-27 12:03:00 -07001121var Bool = proptools.Bool
1122var BoolDefault = proptools.BoolDefault
1123var String = proptools.String
1124var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001125
1126var _ android.OutputFileProducer = (*Module)(nil)