blob: 1ceedf3973ba560bb9566f4d648bd0e771c2013f [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 Lozano3e9f9e42020-12-04 15:05:43 -050070 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
71 Min_sdk_version *string
72
Ivan Lozano43845682020-07-09 21:03:28 -040073 PreventInstall bool
74 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070075}
76
77type Module struct {
78 android.ModuleBase
79 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090080 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070081
82 Properties BaseProperties
83
84 hod android.HostOrDeviceSupported
85 multilib android.Multilib
86
87 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040088 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020089 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070090 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040091 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070092 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -040093
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020094 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +090095
96 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070097}
98
Ivan Lozano43845682020-07-09 21:03:28 -040099func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
100 switch tag {
101 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700102 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400103 return mod.sourceProvider.Srcs(), nil
104 } else {
105 if mod.outputFile.Valid() {
106 return android.Paths{mod.outputFile.Path()}, nil
107 }
108 return android.Paths{}, nil
109 }
110 default:
111 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
112 }
113}
114
Colin Cross7228ecd2019-11-18 16:00:16 -0800115var _ android.ImageInterface = (*Module)(nil)
116
117func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
118
119func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
120 return true
121}
122
Yifan Hong1b3348d2020-01-21 15:53:22 -0800123func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
124 return mod.InRamdisk()
125}
126
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700127func (mod *Module) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool {
128 return mod.InVendorRamdisk()
129}
130
Colin Cross7228ecd2019-11-18 16:00:16 -0800131func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
132 return mod.InRecovery()
133}
134
135func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
136 return nil
137}
138
139func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
140}
141
Ivan Lozano52767be2019-10-18 14:49:46 -0700142func (mod *Module) SelectedStl() string {
143 return ""
144}
145
Ivan Lozano2b262972019-11-21 12:30:50 -0800146func (mod *Module) NonCcVariants() bool {
147 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400148 if _, ok := mod.compiler.(libraryInterface); ok {
149 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800150 }
151 }
152 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
153}
154
Ivan Lozano52767be2019-10-18 14:49:46 -0700155func (mod *Module) Static() bool {
156 if mod.compiler != nil {
157 if library, ok := mod.compiler.(libraryInterface); ok {
158 return library.static()
159 }
160 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400161 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700162}
163
164func (mod *Module) Shared() bool {
165 if mod.compiler != nil {
166 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400167 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700168 }
169 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400170 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700171}
172
173func (mod *Module) Toc() android.OptionalPath {
174 if mod.compiler != nil {
175 if _, ok := mod.compiler.(libraryInterface); ok {
176 return android.OptionalPath{}
177 }
178 }
179 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
180}
181
Yifan Hong1b3348d2020-01-21 15:53:22 -0800182func (mod *Module) OnlyInRamdisk() bool {
183 return false
184}
185
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700186func (mod *Module) OnlyInVendorRamdisk() bool {
187 return false
188}
189
Ivan Lozano52767be2019-10-18 14:49:46 -0700190func (mod *Module) OnlyInRecovery() bool {
191 return false
192}
193
Colin Crossc511bc52020-04-07 16:50:32 +0000194func (mod *Module) UseSdk() bool {
195 return false
196}
197
Ivan Lozano52767be2019-10-18 14:49:46 -0700198func (mod *Module) UseVndk() bool {
199 return false
200}
201
202func (mod *Module) MustUseVendorVariant() bool {
203 return false
204}
205
206func (mod *Module) IsVndk() bool {
207 return false
208}
209
210func (mod *Module) HasVendorVariant() bool {
211 return false
212}
213
214func (mod *Module) SdkVersion() string {
215 return ""
216}
217
Colin Crossc511bc52020-04-07 16:50:32 +0000218func (mod *Module) AlwaysSdk() bool {
219 return false
220}
221
Jiyong Park2286afd2020-06-16 21:58:53 +0900222func (mod *Module) IsSdkVariant() bool {
223 return false
224}
225
Colin Cross1348ce32020-10-01 13:37:16 -0700226func (mod *Module) SplitPerApiLevel() bool {
227 return false
228}
229
Ivan Lozanoffee3342019-08-27 12:03:00 -0700230type Deps struct {
231 Dylibs []string
232 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700233 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400234 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235 ProcMacros []string
236 SharedLibs []string
237 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800238 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239
240 CrtBegin, CrtEnd string
241}
242
243type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400244 DyLibs RustLibraries
245 RLibs RustLibraries
246 SharedLibs android.Paths
247 StaticLibs android.Paths
248 ProcMacros RustLibraries
249 linkDirs []string
250 depFlags []string
251 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700252 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700253
Ivan Lozano45901ed2020-07-24 16:05:01 -0400254 // Used by bindgen modules which call clang
255 depClangFlags []string
256 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400257 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400258 depSystemIncludePaths android.Paths
259
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400260 coverageFiles android.Paths
261
Ivan Lozanof1c84332019-09-20 11:00:37 -0700262 CrtBegin android.OptionalPath
263 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700264
265 // Paths to generated source files
266 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700267}
268
269type RustLibraries []RustLibrary
270
271type RustLibrary struct {
272 Path android.Path
273 CrateName string
274}
275
276type compiler interface {
277 compilerFlags(ctx ModuleContext, flags Flags) Flags
278 compilerProps() []interface{}
279 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
280 compilerDeps(ctx DepsContext, deps Deps) Deps
281 crateName() string
282
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800283 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200284 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700285 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400286
287 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400288
289 Disabled() bool
290 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400291
Ivan Lozanodd055472020-09-28 13:22:45 -0400292 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400293}
294
Matthew Maurerbb3add12020-06-25 09:34:12 -0700295type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700296 exportLinkDirs(...string)
297 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400298 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700299}
300
301type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400302 depFlags []string
303 linkDirs []string
304 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700305}
306
Matthew Maurerbb3add12020-06-25 09:34:12 -0700307func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
308 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
309}
310
311func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
312 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
313}
314
Ivan Lozano2093af22020-08-25 12:48:19 -0400315func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
316 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
317}
318
Colin Cross0de8a1e2020-09-18 14:15:30 -0700319func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
320 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
321 Flags: flagExporter.depFlags,
322 LinkDirs: flagExporter.linkDirs,
323 LinkObjects: flagExporter.linkObjects,
324 })
325}
326
Matthew Maurerbb3add12020-06-25 09:34:12 -0700327var _ exportedFlagsProducer = (*flagExporter)(nil)
328
329func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700330 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700331}
332
Colin Cross0de8a1e2020-09-18 14:15:30 -0700333type FlagExporterInfo struct {
334 Flags []string
335 LinkDirs []string // TODO: this should be android.Paths
336 LinkObjects []string // TODO: this should be android.Paths
337}
338
339var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
340
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400341func (mod *Module) isCoverageVariant() bool {
342 return mod.coverage.Properties.IsCoverageVariant
343}
344
345var _ cc.Coverage = (*Module)(nil)
346
347func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
348 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
349}
350
351func (mod *Module) PreventInstall() {
352 mod.Properties.PreventInstall = true
353}
354
355func (mod *Module) HideFromMake() {
356 mod.Properties.HideFromMake = true
357}
358
359func (mod *Module) MarkAsCoverageVariant(coverage bool) {
360 mod.coverage.Properties.IsCoverageVariant = coverage
361}
362
363func (mod *Module) EnableCoverageIfNeeded() {
364 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700365}
366
367func defaultsFactory() android.Module {
368 return DefaultsFactory()
369}
370
371type Defaults struct {
372 android.ModuleBase
373 android.DefaultsModuleBase
374}
375
376func DefaultsFactory(props ...interface{}) android.Module {
377 module := &Defaults{}
378
379 module.AddProperties(props...)
380 module.AddProperties(
381 &BaseProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400382 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700383 &BaseCompilerProperties{},
384 &BinaryCompilerProperties{},
385 &LibraryCompilerProperties{},
386 &ProcMacroCompilerProperties{},
387 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400388 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700389 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400390 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400391 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200392 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393 )
394
395 android.InitDefaultsModule(module)
396 return module
397}
398
399func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700400 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700401}
402
Ivan Lozano183a3212019-10-18 14:18:45 -0700403func (mod *Module) CcLibrary() bool {
404 if mod.compiler != nil {
405 if _, ok := mod.compiler.(*libraryDecorator); ok {
406 return true
407 }
408 }
409 return false
410}
411
412func (mod *Module) CcLibraryInterface() bool {
413 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400414 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
415 // VariantIs{Static,Shared} is set.
416 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700417 return true
418 }
419 }
420 return false
421}
422
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800423func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700424 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700425 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800426 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700427 }
428 }
429 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
430}
431
432func (mod *Module) SetStatic() {
433 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700434 if library, ok := mod.compiler.(libraryInterface); ok {
435 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700436 return
437 }
438 }
439 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
440}
441
442func (mod *Module) SetShared() {
443 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700444 if library, ok := mod.compiler.(libraryInterface); ok {
445 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700446 return
447 }
448 }
449 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
450}
451
Ivan Lozano183a3212019-10-18 14:18:45 -0700452func (mod *Module) BuildStaticVariant() bool {
453 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700454 if library, ok := mod.compiler.(libraryInterface); ok {
455 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700456 }
457 }
458 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
459}
460
461func (mod *Module) BuildSharedVariant() bool {
462 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700463 if library, ok := mod.compiler.(libraryInterface); ok {
464 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700465 }
466 }
467 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
468}
469
Ivan Lozano183a3212019-10-18 14:18:45 -0700470func (mod *Module) Module() android.Module {
471 return mod
472}
473
Ivan Lozano183a3212019-10-18 14:18:45 -0700474func (mod *Module) OutputFile() android.OptionalPath {
475 return mod.outputFile
476}
477
478func (mod *Module) InRecovery() bool {
479 // For now, Rust has no notion of the recovery image
480 return false
481}
Ivan Lozano183a3212019-10-18 14:18:45 -0700482
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400483func (mod *Module) CoverageFiles() android.Paths {
484 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700485 if !mod.compiler.nativeCoverage() {
486 return android.Paths{}
487 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400488 if library, ok := mod.compiler.(*libraryDecorator); ok {
489 if library.coverageFile != nil {
490 return android.Paths{library.coverageFile}
491 }
492 return android.Paths{}
493 }
494 }
495 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
496}
497
Ivan Lozano183a3212019-10-18 14:18:45 -0700498var _ cc.LinkableInterface = (*Module)(nil)
499
Ivan Lozanoffee3342019-08-27 12:03:00 -0700500func (mod *Module) Init() android.Module {
501 mod.AddProperties(&mod.Properties)
502
503 if mod.compiler != nil {
504 mod.AddProperties(mod.compiler.compilerProps()...)
505 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400506 if mod.coverage != nil {
507 mod.AddProperties(mod.coverage.props()...)
508 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200509 if mod.clippy != nil {
510 mod.AddProperties(mod.clippy.props()...)
511 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400512 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700513 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400514 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400515
Ivan Lozanoffee3342019-08-27 12:03:00 -0700516 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900517 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518
519 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700520 return mod
521}
522
523func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
524 return &Module{
525 hod: hod,
526 multilib: multilib,
527 }
528}
529func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
530 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400531 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200532 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533 return module
534}
535
536type ModuleContext interface {
537 android.ModuleContext
538 ModuleContextIntf
539}
540
541type BaseModuleContext interface {
542 android.BaseModuleContext
543 ModuleContextIntf
544}
545
546type DepsContext interface {
547 android.BottomUpMutatorContext
548 ModuleContextIntf
549}
550
551type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200552 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554}
555
556type depsContext struct {
557 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700558}
559
560type moduleContext struct {
561 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700562}
563
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200564type baseModuleContext struct {
565 android.BaseModuleContext
566}
567
568func (ctx *moduleContext) RustModule() *Module {
569 return ctx.Module().(*Module)
570}
571
572func (ctx *moduleContext) toolchain() config.Toolchain {
573 return ctx.RustModule().toolchain(ctx)
574}
575
576func (ctx *depsContext) RustModule() *Module {
577 return ctx.Module().(*Module)
578}
579
580func (ctx *depsContext) toolchain() config.Toolchain {
581 return ctx.RustModule().toolchain(ctx)
582}
583
584func (ctx *baseModuleContext) RustModule() *Module {
585 return ctx.Module().(*Module)
586}
587
588func (ctx *baseModuleContext) toolchain() config.Toolchain {
589 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400590}
591
592func (mod *Module) nativeCoverage() bool {
593 return mod.compiler != nil && mod.compiler.nativeCoverage()
594}
595
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
597 if mod.cachedToolchain == nil {
598 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
599 }
600 return mod.cachedToolchain
601}
602
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200603func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
604 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
605}
606
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
608}
609
610func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
611 ctx := &moduleContext{
612 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700613 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700614
Jiyong Park99644e92020-11-17 22:21:02 +0900615 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
616 if !apexInfo.IsForPlatform() {
617 mod.hideApexVariantFromMake = true
618 }
619
Ivan Lozanoffee3342019-08-27 12:03:00 -0700620 toolchain := mod.toolchain(ctx)
621
622 if !toolchain.Supported() {
623 // This toolchain's unsupported, there's nothing to do for this mod.
624 return
625 }
626
627 deps := mod.depsToPaths(ctx)
628 flags := Flags{
629 Toolchain: toolchain,
630 }
631
632 if mod.compiler != nil {
633 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400634 }
635 if mod.coverage != nil {
636 flags, deps = mod.coverage.flags(ctx, flags, deps)
637 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200638 if mod.clippy != nil {
639 flags, deps = mod.clippy.flags(ctx, flags, deps)
640 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400641
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200642 // SourceProvider needs to call GenerateSource() before compiler calls
643 // compile() so it can provide the source. A SourceProvider has
644 // multiple variants (e.g. source, rlib, dylib). Only the "source"
645 // variant is responsible for effectively generating the source. The
646 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400647 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200648 if mod.compiler.(libraryInterface).source() {
649 mod.sourceProvider.GenerateSource(ctx, deps)
650 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
651 } else {
652 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
653 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700654 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200655 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400656 }
657
658 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400660
Ivan Lozanoffee3342019-08-27 12:03:00 -0700661 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400662 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200663 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400664 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665 }
666}
667
668func (mod *Module) deps(ctx DepsContext) Deps {
669 deps := Deps{}
670
671 if mod.compiler != nil {
672 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400673 }
674 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700675 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700676 }
677
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400678 if mod.coverage != nil {
679 deps = mod.coverage.deps(ctx, deps)
680 }
681
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
683 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700684 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700685 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
686 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
687 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
688
689 return deps
690
691}
692
Ivan Lozanoffee3342019-08-27 12:03:00 -0700693type dependencyTag struct {
694 blueprint.BaseDependencyTag
695 name string
696 library bool
697 proc_macro bool
698}
699
Jiyong Park65b62242020-11-25 12:44:59 +0900700// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
701// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
702func (d dependencyTag) InstallDepNeeded() bool {
703 return d.library || d.proc_macro
704}
705
706var _ android.InstallNeededDependencyTag = dependencyTag{}
707
Ivan Lozanoffee3342019-08-27 12:03:00 -0700708var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400709 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
710 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
711 dylibDepTag = dependencyTag{name: "dylib", library: true}
712 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
713 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200714 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715)
716
Jiyong Park99644e92020-11-17 22:21:02 +0900717func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
718 tag, ok := depTag.(dependencyTag)
719 return ok && tag == dylibDepTag
720}
721
Matthew Maurer0f003b12020-06-29 14:34:06 -0700722type autoDep struct {
723 variation string
724 depTag dependencyTag
725}
726
727var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200728 rlibVariation = "rlib"
729 dylibVariation = "dylib"
730 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
731 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700732)
733
734type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400735 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700736}
737
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400738func (mod *Module) begin(ctx BaseModuleContext) {
739 if mod.coverage != nil {
740 mod.coverage.begin(ctx)
741 }
742}
743
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
745 var depPaths PathDeps
746
747 directRlibDeps := []*Module{}
748 directDylibDeps := []*Module{}
749 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700750 directSharedLibDeps := [](cc.LinkableInterface){}
751 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400752 directSrcProvidersDeps := []*Module{}
753 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754
755 ctx.VisitDirectDeps(func(dep android.Module) {
756 depName := ctx.OtherModuleName(dep)
757 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400758 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700760
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 switch depTag {
762 case dylibDepTag:
763 dylib, ok := rustDep.compiler.(libraryInterface)
764 if !ok || !dylib.dylib() {
765 ctx.ModuleErrorf("mod %q not an dylib library", depName)
766 return
767 }
768 directDylibDeps = append(directDylibDeps, rustDep)
769 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
770 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400771
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772 rlib, ok := rustDep.compiler.(libraryInterface)
773 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400774 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775 return
776 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400777 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400779 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700780 case procMacroDepTag:
781 directProcMacroDeps = append(directProcMacroDeps, rustDep)
782 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400783 case android.SourceDepTag:
784 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
785 // OS/Arch variant is used.
786 var helper string
787 if ctx.Host() {
788 helper = "missing 'host_supported'?"
789 } else {
790 helper = "device module defined?"
791 }
792
793 if dep.Target().Os != ctx.Os() {
794 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
795 return
796 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
797 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
798 return
799 }
800 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700801 }
802
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400803 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700804 if depTag != procMacroDepTag {
805 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
806 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
807 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
808 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809 }
810
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400812 linkFile := rustDep.outputFile
813 if !linkFile.Valid() {
814 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
815 depName, ctx.ModuleName())
816 return
817 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700819 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
820 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821 }
822 }
823
Ivan Lozano89435d12020-07-31 11:01:18 -0400824 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700825 //Handle C dependencies
826 if _, ok := ccDep.(*Module); !ok {
827 if ccDep.Module().Target().Os != ctx.Os() {
828 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
829 return
830 }
831 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
832 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
833 return
834 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700835 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400836 linkObject := ccDep.OutputFile()
837 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500838
Ivan Lozano2093af22020-08-25 12:48:19 -0400839 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
841 }
842
843 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700844 switch {
845 case cc.IsStaticDepTag(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 Lozanoa0cd8f92020-04-09 09:56:02 -0400853 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 directStaticLibDeps = append(directStaticLibDeps, ccDep)
855 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700856 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400858 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700859 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
860 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
861 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
862 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
863 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700864 directSharedLibDeps = append(directSharedLibDeps, ccDep)
865 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
866 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800867 case cc.IsHeaderDepTag(depTag):
868 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
869 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
870 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
871 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700872 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400873 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700874 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400875 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876 }
877
878 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700879 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
880 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400881 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400884
885 if srcDep, ok := dep.(android.SourceFileProducer); ok {
886 switch depTag {
887 case android.SourceDepTag:
888 // These are usually genrules which don't have per-target variants.
889 directSrcDeps = append(directSrcDeps, srcDep)
890 }
891 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892 })
893
894 var rlibDepFiles RustLibraries
895 for _, dep := range directRlibDeps {
896 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
897 }
898 var dylibDepFiles RustLibraries
899 for _, dep := range directDylibDeps {
900 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
901 }
902 var procMacroDepFiles RustLibraries
903 for _, dep := range directProcMacroDeps {
904 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
905 }
906
907 var staticLibDepFiles android.Paths
908 for _, dep := range directStaticLibDeps {
909 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
910 }
911
912 var sharedLibDepFiles android.Paths
913 for _, dep := range directSharedLibDeps {
914 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
915 }
916
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400917 var srcProviderDepFiles android.Paths
918 for _, dep := range directSrcProvidersDeps {
919 srcs, _ := dep.OutputFiles("")
920 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
921 }
922 for _, dep := range directSrcDeps {
923 srcs := dep.Srcs()
924 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
925 }
926
Ivan Lozanoffee3342019-08-27 12:03:00 -0700927 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
928 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
929 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
930 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
931 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400932 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700933
934 // Dedup exported flags from dependencies
935 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
936 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400937 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
938 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
939 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940
941 return depPaths
942}
943
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800944func (mod *Module) InstallInData() bool {
945 if mod.compiler == nil {
946 return false
947 }
948 return mod.compiler.inData()
949}
950
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951func linkPathFromFilePath(filepath android.Path) string {
952 return strings.Split(filepath.String(), filepath.Base())[0]
953}
Ivan Lozanod648c432020-02-06 12:05:10 -0500954
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
956 ctx := &depsContext{
957 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700958 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700959
960 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700961 var commonDepVariations []blueprint.Variation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700962 if !mod.Host() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700963 commonDepVariations = append(commonDepVariations,
Colin Cross7228ecd2019-11-18 16:00:16 -0800964 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700965 }
Ivan Lozanodd055472020-09-28 13:22:45 -0400966
Ivan Lozano2b081132020-09-08 12:46:52 -0400967 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400968 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400969 stdLinkage = "rlib-std"
970 }
971
972 rlibDepVariations := commonDepVariations
973 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
974 rlibDepVariations = append(rlibDepVariations,
975 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
976 }
977
Ivan Lozano52767be2019-10-18 14:49:46 -0700978 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400979 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200980 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700981 rlibDepTag, deps.Rlibs...)
982 actx.AddVariationDependencies(
983 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200984 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700985 dylibDepTag, deps.Dylibs...)
986
Ivan Lozano042504f2020-08-18 14:31:23 -0400987 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
988 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400989 if autoDep.depTag == rlibDepTag {
990 actx.AddVariationDependencies(
991 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
992 autoDep.depTag, deps.Rustlibs...)
993 } else {
994 actx.AddVariationDependencies(
995 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
996 autoDep.depTag, deps.Rustlibs...)
997 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700998 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400999 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001000 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001001 actx.AddVariationDependencies(
1002 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1003 rlibDepTag, deps.Stdlibs...)
1004 } else {
1005 actx.AddVariationDependencies(
1006 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1007 dylibDepTag, deps.Stdlibs...)
1008 }
1009 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001010 actx.AddVariationDependencies(append(commonDepVariations,
1011 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001012 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001013 actx.AddVariationDependencies(append(commonDepVariations,
1014 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001015 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001016
Zach Johnson3df4e632020-11-06 11:56:27 -08001017 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1018
Colin Cross565cafd2020-09-25 18:47:38 -07001019 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001020 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001021 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001022 }
1023 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001024 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001025 }
1026
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001027 if mod.sourceProvider != nil {
1028 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1029 bindgen.Properties.Custom_bindgen != "" {
1030 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1031 bindgen.Properties.Custom_bindgen)
1032 }
1033 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001034 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001035 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001036}
1037
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001038func BeginMutator(ctx android.BottomUpMutatorContext) {
1039 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1040 mod.beginMutator(ctx)
1041 }
1042}
1043
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001044func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1045 ctx := &baseModuleContext{
1046 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001047 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001048
1049 mod.begin(ctx)
1050}
1051
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052func (mod *Module) Name() string {
1053 name := mod.ModuleBase.Name()
1054 if p, ok := mod.compiler.(interface {
1055 Name(string) string
1056 }); ok {
1057 name = p.Name(name)
1058 }
1059 return name
1060}
1061
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001062func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001063 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001064 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001065 }
1066}
1067
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001068var _ android.HostToolProvider = (*Module)(nil)
1069
1070func (mod *Module) HostToolPath() android.OptionalPath {
1071 if !mod.Host() {
1072 return android.OptionalPath{}
1073 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001074 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1075 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001076 }
1077 return android.OptionalPath{}
1078}
1079
Jiyong Park99644e92020-11-17 22:21:02 +09001080var _ android.ApexModule = (*Module)(nil)
1081
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001082func (mod *Module) minSdkVersion() string {
1083 return String(mod.Properties.Min_sdk_version)
1084}
1085
Jiyong Park99644e92020-11-17 22:21:02 +09001086func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001087 minSdkVersion := mod.minSdkVersion()
1088 if minSdkVersion == "apex_inherit" {
1089 return nil
1090 }
1091 if minSdkVersion == "" {
1092 return fmt.Errorf("min_sdk_version is not specificed")
1093 }
1094
1095 // Not using nativeApiLevelFromUser because the context here is not
1096 // necessarily a native context.
1097 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1098 if err != nil {
1099 return err
1100 }
1101
1102 if ver.GreaterThan(sdkVersion) {
1103 return fmt.Errorf("newer SDK(%v)", ver)
1104 }
Jiyong Park99644e92020-11-17 22:21:02 +09001105 return nil
1106}
1107
1108func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1109 depTag := ctx.OtherModuleDependencyTag(dep)
1110
1111 if ccm, ok := dep.(*cc.Module); ok {
1112 if ccm.HasStubsVariants() {
1113 if cc.IsSharedDepTag(depTag) {
1114 // dynamic dep to a stubs lib crosses APEX boundary
1115 return false
1116 }
1117 if cc.IsRuntimeDepTag(depTag) {
1118 // runtime dep to a stubs lib also crosses APEX boundary
1119 return false
1120 }
1121
1122 if cc.IsHeaderDepTag(depTag) {
1123 return false
1124 }
1125 }
1126 if mod.Static() && cc.IsSharedDepTag(depTag) {
1127 // shared_lib dependency from a static lib is considered as crossing
1128 // the APEX boundary because the dependency doesn't actually is
1129 // linked; the dependency is used only during the compilation phase.
1130 return false
1131 }
1132 }
1133
1134 if depTag == procMacroDepTag {
1135 return false
1136 }
1137
1138 return true
1139}
1140
1141// Overrides ApexModule.IsInstallabeToApex()
1142func (mod *Module) IsInstallableToApex() bool {
1143 if mod.compiler != nil {
1144 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1145 return true
1146 }
1147 if _, ok := mod.compiler.(*binaryDecorator); ok {
1148 return true
1149 }
1150 }
1151 return false
1152}
1153
Ivan Lozanoffee3342019-08-27 12:03:00 -07001154var Bool = proptools.Bool
1155var BoolDefault = proptools.BoolDefault
1156var String = proptools.String
1157var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001158
1159var _ android.OutputFileProducer = (*Module)(nil)