blob: 3bb48619b151b3a0e8b693088dd32e94fd480556 [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 Lozano6a884432020-12-02 09:15:16 -050068 ImageVariationPrefix string `blueprint:"mutated"`
69 VndkVersion string `blueprint:"mutated"`
70 SubName string `blueprint:"mutated"`
71
72 // Set by imageMutator
73 CoreVariantNeeded bool `blueprint:"mutated"`
74 ExtraVariants []string `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040075
Ivan Lozano43845682020-07-09 21:03:28 -040076 PreventInstall bool
77 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070078}
79
80type Module struct {
81 android.ModuleBase
82 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090083 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070084
Ivan Lozano6a884432020-12-02 09:15:16 -050085 VendorProperties cc.VendorProperties
86
Ivan Lozanoffee3342019-08-27 12:03:00 -070087 Properties BaseProperties
88
89 hod android.HostOrDeviceSupported
90 multilib android.Multilib
91
Ivan Lozano6a884432020-12-02 09:15:16 -050092 makeLinkType string
93
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040095 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020096 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -040098 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -070099 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400100
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200101 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900102
103 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104}
105
Ivan Lozano43845682020-07-09 21:03:28 -0400106func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
107 switch tag {
108 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700109 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400110 return mod.sourceProvider.Srcs(), nil
111 } else {
112 if mod.outputFile.Valid() {
113 return android.Paths{mod.outputFile.Path()}, nil
114 }
115 return android.Paths{}, nil
116 }
117 default:
118 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
119 }
120}
121
Ivan Lozano52767be2019-10-18 14:49:46 -0700122func (mod *Module) SelectedStl() string {
123 return ""
124}
125
Ivan Lozano2b262972019-11-21 12:30:50 -0800126func (mod *Module) NonCcVariants() bool {
127 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400128 if _, ok := mod.compiler.(libraryInterface); ok {
129 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800130 }
131 }
132 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
133}
134
Ivan Lozano52767be2019-10-18 14:49:46 -0700135func (mod *Module) Static() bool {
136 if mod.compiler != nil {
137 if library, ok := mod.compiler.(libraryInterface); ok {
138 return library.static()
139 }
140 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400141 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700142}
143
144func (mod *Module) Shared() bool {
145 if mod.compiler != nil {
146 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400147 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700148 }
149 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400150 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700151}
152
153func (mod *Module) Toc() android.OptionalPath {
154 if mod.compiler != nil {
155 if _, ok := mod.compiler.(libraryInterface); ok {
156 return android.OptionalPath{}
157 }
158 }
159 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
160}
161
Colin Crossc511bc52020-04-07 16:50:32 +0000162func (mod *Module) UseSdk() bool {
163 return false
164}
165
Ivan Lozano6a884432020-12-02 09:15:16 -0500166// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
167// "product" and "vendor" variant modules return true for this function.
168// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
169// "soc_specific: true" and more vendor installed modules are included here.
170// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
171// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700172func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500173 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700174}
175
176func (mod *Module) MustUseVendorVariant() bool {
177 return false
178}
179
180func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500181 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700182 return false
183}
184
Ivan Lozanof9e21722020-12-02 09:00:51 -0500185func (mod *Module) IsVndkExt() bool {
186 return false
187}
188
189func (c *Module) IsVndkPrivate(config android.Config) bool {
190 return false
191}
192
Ivan Lozano52767be2019-10-18 14:49:46 -0700193func (mod *Module) SdkVersion() string {
194 return ""
195}
196
Colin Crossc511bc52020-04-07 16:50:32 +0000197func (mod *Module) AlwaysSdk() bool {
198 return false
199}
200
Jiyong Park2286afd2020-06-16 21:58:53 +0900201func (mod *Module) IsSdkVariant() bool {
202 return false
203}
204
Colin Cross1348ce32020-10-01 13:37:16 -0700205func (mod *Module) SplitPerApiLevel() bool {
206 return false
207}
208
Ivan Lozanoffee3342019-08-27 12:03:00 -0700209type Deps struct {
210 Dylibs []string
211 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700212 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400213 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700214 ProcMacros []string
215 SharedLibs []string
216 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800217 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700218
219 CrtBegin, CrtEnd string
220}
221
222type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400223 DyLibs RustLibraries
224 RLibs RustLibraries
225 SharedLibs android.Paths
226 StaticLibs android.Paths
227 ProcMacros RustLibraries
228 linkDirs []string
229 depFlags []string
230 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700231 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700232
Ivan Lozano45901ed2020-07-24 16:05:01 -0400233 // Used by bindgen modules which call clang
234 depClangFlags []string
235 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400236 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400237 depSystemIncludePaths android.Paths
238
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400239 coverageFiles android.Paths
240
Ivan Lozanof1c84332019-09-20 11:00:37 -0700241 CrtBegin android.OptionalPath
242 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700243
244 // Paths to generated source files
245 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246}
247
248type RustLibraries []RustLibrary
249
250type RustLibrary struct {
251 Path android.Path
252 CrateName string
253}
254
255type compiler interface {
256 compilerFlags(ctx ModuleContext, flags Flags) Flags
257 compilerProps() []interface{}
258 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
259 compilerDeps(ctx DepsContext, deps Deps) Deps
260 crateName() string
261
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800262 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200263 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400265
266 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400267
268 Disabled() bool
269 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400270
Ivan Lozanodd055472020-09-28 13:22:45 -0400271 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400272}
273
Matthew Maurerbb3add12020-06-25 09:34:12 -0700274type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700275 exportLinkDirs(...string)
276 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400277 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700278}
279
280type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400281 depFlags []string
282 linkDirs []string
283 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700284}
285
Matthew Maurerbb3add12020-06-25 09:34:12 -0700286func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
287 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
288}
289
290func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
291 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
292}
293
Ivan Lozano2093af22020-08-25 12:48:19 -0400294func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
295 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
296}
297
Colin Cross0de8a1e2020-09-18 14:15:30 -0700298func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
299 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
300 Flags: flagExporter.depFlags,
301 LinkDirs: flagExporter.linkDirs,
302 LinkObjects: flagExporter.linkObjects,
303 })
304}
305
Matthew Maurerbb3add12020-06-25 09:34:12 -0700306var _ exportedFlagsProducer = (*flagExporter)(nil)
307
308func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700309 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700310}
311
Colin Cross0de8a1e2020-09-18 14:15:30 -0700312type FlagExporterInfo struct {
313 Flags []string
314 LinkDirs []string // TODO: this should be android.Paths
315 LinkObjects []string // TODO: this should be android.Paths
316}
317
318var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
319
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400320func (mod *Module) isCoverageVariant() bool {
321 return mod.coverage.Properties.IsCoverageVariant
322}
323
324var _ cc.Coverage = (*Module)(nil)
325
326func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
327 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
328}
329
330func (mod *Module) PreventInstall() {
331 mod.Properties.PreventInstall = true
332}
333
334func (mod *Module) HideFromMake() {
335 mod.Properties.HideFromMake = true
336}
337
338func (mod *Module) MarkAsCoverageVariant(coverage bool) {
339 mod.coverage.Properties.IsCoverageVariant = coverage
340}
341
342func (mod *Module) EnableCoverageIfNeeded() {
343 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700344}
345
346func defaultsFactory() android.Module {
347 return DefaultsFactory()
348}
349
350type Defaults struct {
351 android.ModuleBase
352 android.DefaultsModuleBase
353}
354
355func DefaultsFactory(props ...interface{}) android.Module {
356 module := &Defaults{}
357
358 module.AddProperties(props...)
359 module.AddProperties(
360 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500361 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400362 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700363 &BaseCompilerProperties{},
364 &BinaryCompilerProperties{},
365 &LibraryCompilerProperties{},
366 &ProcMacroCompilerProperties{},
367 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400368 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700369 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400370 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400371 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200372 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373 )
374
375 android.InitDefaultsModule(module)
376 return module
377}
378
379func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700380 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700381}
382
Ivan Lozano183a3212019-10-18 14:18:45 -0700383func (mod *Module) CcLibrary() bool {
384 if mod.compiler != nil {
385 if _, ok := mod.compiler.(*libraryDecorator); ok {
386 return true
387 }
388 }
389 return false
390}
391
392func (mod *Module) CcLibraryInterface() bool {
393 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400394 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
395 // VariantIs{Static,Shared} is set.
396 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700397 return true
398 }
399 }
400 return false
401}
402
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800403func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700404 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700405 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800406 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700407 }
408 }
409 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
410}
411
412func (mod *Module) SetStatic() {
413 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700414 if library, ok := mod.compiler.(libraryInterface); ok {
415 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700416 return
417 }
418 }
419 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
420}
421
422func (mod *Module) SetShared() {
423 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700424 if library, ok := mod.compiler.(libraryInterface); ok {
425 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700426 return
427 }
428 }
429 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
430}
431
Ivan Lozano183a3212019-10-18 14:18:45 -0700432func (mod *Module) BuildStaticVariant() bool {
433 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700434 if library, ok := mod.compiler.(libraryInterface); ok {
435 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700436 }
437 }
438 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
439}
440
441func (mod *Module) BuildSharedVariant() bool {
442 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700443 if library, ok := mod.compiler.(libraryInterface); ok {
444 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700445 }
446 }
447 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
448}
449
Ivan Lozano183a3212019-10-18 14:18:45 -0700450func (mod *Module) Module() android.Module {
451 return mod
452}
453
Ivan Lozano183a3212019-10-18 14:18:45 -0700454func (mod *Module) OutputFile() android.OptionalPath {
455 return mod.outputFile
456}
457
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400458func (mod *Module) CoverageFiles() android.Paths {
459 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700460 if !mod.compiler.nativeCoverage() {
461 return android.Paths{}
462 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400463 if library, ok := mod.compiler.(*libraryDecorator); ok {
464 if library.coverageFile != nil {
465 return android.Paths{library.coverageFile}
466 }
467 return android.Paths{}
468 }
469 }
470 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
471}
472
Ivan Lozano183a3212019-10-18 14:18:45 -0700473var _ cc.LinkableInterface = (*Module)(nil)
474
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475func (mod *Module) Init() android.Module {
476 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500477 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478
479 if mod.compiler != nil {
480 mod.AddProperties(mod.compiler.compilerProps()...)
481 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400482 if mod.coverage != nil {
483 mod.AddProperties(mod.coverage.props()...)
484 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200485 if mod.clippy != nil {
486 mod.AddProperties(mod.clippy.props()...)
487 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400488 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700489 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400490 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400491
Ivan Lozanoffee3342019-08-27 12:03:00 -0700492 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900493 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700494
495 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700496 return mod
497}
498
499func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
500 return &Module{
501 hod: hod,
502 multilib: multilib,
503 }
504}
505func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
506 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400507 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200508 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700509 return module
510}
511
512type ModuleContext interface {
513 android.ModuleContext
514 ModuleContextIntf
515}
516
517type BaseModuleContext interface {
518 android.BaseModuleContext
519 ModuleContextIntf
520}
521
522type DepsContext interface {
523 android.BottomUpMutatorContext
524 ModuleContextIntf
525}
526
527type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200528 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700529 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700530}
531
532type depsContext struct {
533 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534}
535
536type moduleContext struct {
537 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700538}
539
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200540type baseModuleContext struct {
541 android.BaseModuleContext
542}
543
544func (ctx *moduleContext) RustModule() *Module {
545 return ctx.Module().(*Module)
546}
547
548func (ctx *moduleContext) toolchain() config.Toolchain {
549 return ctx.RustModule().toolchain(ctx)
550}
551
552func (ctx *depsContext) RustModule() *Module {
553 return ctx.Module().(*Module)
554}
555
556func (ctx *depsContext) toolchain() config.Toolchain {
557 return ctx.RustModule().toolchain(ctx)
558}
559
560func (ctx *baseModuleContext) RustModule() *Module {
561 return ctx.Module().(*Module)
562}
563
564func (ctx *baseModuleContext) toolchain() config.Toolchain {
565 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400566}
567
568func (mod *Module) nativeCoverage() bool {
569 return mod.compiler != nil && mod.compiler.nativeCoverage()
570}
571
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
573 if mod.cachedToolchain == nil {
574 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
575 }
576 return mod.cachedToolchain
577}
578
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200579func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
580 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
581}
582
Ivan Lozanoffee3342019-08-27 12:03:00 -0700583func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
584}
585
586func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
587 ctx := &moduleContext{
588 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590
Jiyong Park99644e92020-11-17 22:21:02 +0900591 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
592 if !apexInfo.IsForPlatform() {
593 mod.hideApexVariantFromMake = true
594 }
595
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500597 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
598
599 // Differentiate static libraries that are vendor available
600 if mod.UseVndk() {
601 mod.Properties.SubName += ".vendor"
602 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700603
604 if !toolchain.Supported() {
605 // This toolchain's unsupported, there's nothing to do for this mod.
606 return
607 }
608
609 deps := mod.depsToPaths(ctx)
610 flags := Flags{
611 Toolchain: toolchain,
612 }
613
614 if mod.compiler != nil {
615 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400616 }
617 if mod.coverage != nil {
618 flags, deps = mod.coverage.flags(ctx, flags, deps)
619 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200620 if mod.clippy != nil {
621 flags, deps = mod.clippy.flags(ctx, flags, deps)
622 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400623
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200624 // SourceProvider needs to call GenerateSource() before compiler calls
625 // compile() so it can provide the source. A SourceProvider has
626 // multiple variants (e.g. source, rlib, dylib). Only the "source"
627 // variant is responsible for effectively generating the source. The
628 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400629 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200630 if mod.compiler.(libraryInterface).source() {
631 mod.sourceProvider.GenerateSource(ctx, deps)
632 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
633 } else {
634 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
635 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700636 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200637 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400638 }
639
640 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700641 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400642
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400644 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200645 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400646 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700647 }
648}
649
650func (mod *Module) deps(ctx DepsContext) Deps {
651 deps := Deps{}
652
653 if mod.compiler != nil {
654 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400655 }
656 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700657 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700658 }
659
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400660 if mod.coverage != nil {
661 deps = mod.coverage.deps(ctx, deps)
662 }
663
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
665 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700666 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
668 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
669 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
670
671 return deps
672
673}
674
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675type dependencyTag struct {
676 blueprint.BaseDependencyTag
677 name string
678 library bool
679 proc_macro bool
680}
681
Jiyong Park65b62242020-11-25 12:44:59 +0900682// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
683// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
684func (d dependencyTag) InstallDepNeeded() bool {
685 return d.library || d.proc_macro
686}
687
688var _ android.InstallNeededDependencyTag = dependencyTag{}
689
Ivan Lozanoffee3342019-08-27 12:03:00 -0700690var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400691 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
692 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
693 dylibDepTag = dependencyTag{name: "dylib", library: true}
694 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
695 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200696 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700697)
698
Jiyong Park99644e92020-11-17 22:21:02 +0900699func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
700 tag, ok := depTag.(dependencyTag)
701 return ok && tag == dylibDepTag
702}
703
Matthew Maurer0f003b12020-06-29 14:34:06 -0700704type autoDep struct {
705 variation string
706 depTag dependencyTag
707}
708
709var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200710 rlibVariation = "rlib"
711 dylibVariation = "dylib"
712 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
713 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700714)
715
716type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400717 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700718}
719
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400720func (mod *Module) begin(ctx BaseModuleContext) {
721 if mod.coverage != nil {
722 mod.coverage.begin(ctx)
723 }
724}
725
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
727 var depPaths PathDeps
728
729 directRlibDeps := []*Module{}
730 directDylibDeps := []*Module{}
731 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700732 directSharedLibDeps := [](cc.LinkableInterface){}
733 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400734 directSrcProvidersDeps := []*Module{}
735 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736
737 ctx.VisitDirectDeps(func(dep android.Module) {
738 depName := ctx.OtherModuleName(dep)
739 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400740 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700741 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700742
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 switch depTag {
744 case dylibDepTag:
745 dylib, ok := rustDep.compiler.(libraryInterface)
746 if !ok || !dylib.dylib() {
747 ctx.ModuleErrorf("mod %q not an dylib library", depName)
748 return
749 }
750 directDylibDeps = append(directDylibDeps, rustDep)
751 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
752 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400753
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754 rlib, ok := rustDep.compiler.(libraryInterface)
755 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400756 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 return
758 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400759 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400761 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 case procMacroDepTag:
763 directProcMacroDeps = append(directProcMacroDeps, rustDep)
764 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400765 case android.SourceDepTag:
766 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
767 // OS/Arch variant is used.
768 var helper string
769 if ctx.Host() {
770 helper = "missing 'host_supported'?"
771 } else {
772 helper = "device module defined?"
773 }
774
775 if dep.Target().Os != ctx.Os() {
776 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
777 return
778 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
779 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
780 return
781 }
782 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 }
784
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400785 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700786 if depTag != procMacroDepTag {
787 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
788 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
789 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
790 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700791 }
792
Ivan Lozanoffee3342019-08-27 12:03:00 -0700793 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400794 linkFile := rustDep.outputFile
795 if !linkFile.Valid() {
796 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
797 depName, ctx.ModuleName())
798 return
799 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700801 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
802 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803 }
804 }
805
Ivan Lozano89435d12020-07-31 11:01:18 -0400806 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700807 //Handle C dependencies
808 if _, ok := ccDep.(*Module); !ok {
809 if ccDep.Module().Target().Os != ctx.Os() {
810 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
811 return
812 }
813 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
814 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
815 return
816 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700817 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400818 linkObject := ccDep.OutputFile()
819 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500820
Ivan Lozano2093af22020-08-25 12:48:19 -0400821 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700822 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
823 }
824
825 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700826 switch {
827 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400829 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700830 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
831 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
832 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
833 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
834 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400835 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836 directStaticLibDeps = append(directStaticLibDeps, ccDep)
837 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700838 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400840 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700841 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
842 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
843 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
844 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
845 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 directSharedLibDeps = append(directSharedLibDeps, ccDep)
847 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
848 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800849 case cc.IsHeaderDepTag(depTag):
850 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
851 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
852 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
853 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700854 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400855 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700856 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400857 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700858 }
859
860 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700861 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
862 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400863 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700864 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400866
867 if srcDep, ok := dep.(android.SourceFileProducer); ok {
868 switch depTag {
869 case android.SourceDepTag:
870 // These are usually genrules which don't have per-target variants.
871 directSrcDeps = append(directSrcDeps, srcDep)
872 }
873 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 })
875
876 var rlibDepFiles RustLibraries
877 for _, dep := range directRlibDeps {
878 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
879 }
880 var dylibDepFiles RustLibraries
881 for _, dep := range directDylibDeps {
882 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
883 }
884 var procMacroDepFiles RustLibraries
885 for _, dep := range directProcMacroDeps {
886 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
887 }
888
889 var staticLibDepFiles android.Paths
890 for _, dep := range directStaticLibDeps {
891 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
892 }
893
894 var sharedLibDepFiles android.Paths
895 for _, dep := range directSharedLibDeps {
896 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
897 }
898
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400899 var srcProviderDepFiles android.Paths
900 for _, dep := range directSrcProvidersDeps {
901 srcs, _ := dep.OutputFiles("")
902 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
903 }
904 for _, dep := range directSrcDeps {
905 srcs := dep.Srcs()
906 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
907 }
908
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
910 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
911 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
912 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
913 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400914 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915
916 // Dedup exported flags from dependencies
917 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
918 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400919 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
920 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
921 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700922
923 return depPaths
924}
925
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800926func (mod *Module) InstallInData() bool {
927 if mod.compiler == nil {
928 return false
929 }
930 return mod.compiler.inData()
931}
932
Ivan Lozanoffee3342019-08-27 12:03:00 -0700933func linkPathFromFilePath(filepath android.Path) string {
934 return strings.Split(filepath.String(), filepath.Base())[0]
935}
Ivan Lozanod648c432020-02-06 12:05:10 -0500936
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
938 ctx := &depsContext{
939 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700941
942 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700943 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -0400944
Ivan Lozano2b081132020-09-08 12:46:52 -0400945 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400946 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400947 stdLinkage = "rlib-std"
948 }
949
950 rlibDepVariations := commonDepVariations
951 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
952 rlibDepVariations = append(rlibDepVariations,
953 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
954 }
955
Ivan Lozano52767be2019-10-18 14:49:46 -0700956 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400957 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200958 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700959 rlibDepTag, deps.Rlibs...)
960 actx.AddVariationDependencies(
961 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200962 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700963 dylibDepTag, deps.Dylibs...)
964
Ivan Lozano042504f2020-08-18 14:31:23 -0400965 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
966 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400967 if autoDep.depTag == rlibDepTag {
968 actx.AddVariationDependencies(
969 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
970 autoDep.depTag, deps.Rustlibs...)
971 } else {
972 actx.AddVariationDependencies(
973 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
974 autoDep.depTag, deps.Rustlibs...)
975 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700976 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400977 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400978 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400979 actx.AddVariationDependencies(
980 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
981 rlibDepTag, deps.Stdlibs...)
982 } else {
983 actx.AddVariationDependencies(
984 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
985 dylibDepTag, deps.Stdlibs...)
986 }
987 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700988 actx.AddVariationDependencies(append(commonDepVariations,
989 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700990 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700991 actx.AddVariationDependencies(append(commonDepVariations,
992 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700993 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700994
Zach Johnson3df4e632020-11-06 11:56:27 -0800995 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
996
Colin Cross565cafd2020-09-25 18:47:38 -0700997 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700998 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -0700999 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001000 }
1001 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001002 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001003 }
1004
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001005 if mod.sourceProvider != nil {
1006 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1007 bindgen.Properties.Custom_bindgen != "" {
1008 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1009 bindgen.Properties.Custom_bindgen)
1010 }
1011 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001012 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001013 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014}
1015
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001016func BeginMutator(ctx android.BottomUpMutatorContext) {
1017 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1018 mod.beginMutator(ctx)
1019 }
1020}
1021
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001022func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1023 ctx := &baseModuleContext{
1024 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001025 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001026
1027 mod.begin(ctx)
1028}
1029
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030func (mod *Module) Name() string {
1031 name := mod.ModuleBase.Name()
1032 if p, ok := mod.compiler.(interface {
1033 Name(string) string
1034 }); ok {
1035 name = p.Name(name)
1036 }
1037 return name
1038}
1039
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001040func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001041 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001042 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001043 }
1044}
1045
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001046var _ android.HostToolProvider = (*Module)(nil)
1047
1048func (mod *Module) HostToolPath() android.OptionalPath {
1049 if !mod.Host() {
1050 return android.OptionalPath{}
1051 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001052 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1053 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001054 }
1055 return android.OptionalPath{}
1056}
1057
Jiyong Park99644e92020-11-17 22:21:02 +09001058var _ android.ApexModule = (*Module)(nil)
1059
1060func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
1061 return nil
1062}
1063
1064func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1065 depTag := ctx.OtherModuleDependencyTag(dep)
1066
1067 if ccm, ok := dep.(*cc.Module); ok {
1068 if ccm.HasStubsVariants() {
1069 if cc.IsSharedDepTag(depTag) {
1070 // dynamic dep to a stubs lib crosses APEX boundary
1071 return false
1072 }
1073 if cc.IsRuntimeDepTag(depTag) {
1074 // runtime dep to a stubs lib also crosses APEX boundary
1075 return false
1076 }
1077
1078 if cc.IsHeaderDepTag(depTag) {
1079 return false
1080 }
1081 }
1082 if mod.Static() && cc.IsSharedDepTag(depTag) {
1083 // shared_lib dependency from a static lib is considered as crossing
1084 // the APEX boundary because the dependency doesn't actually is
1085 // linked; the dependency is used only during the compilation phase.
1086 return false
1087 }
1088 }
1089
1090 if depTag == procMacroDepTag {
1091 return false
1092 }
1093
1094 return true
1095}
1096
1097// Overrides ApexModule.IsInstallabeToApex()
1098func (mod *Module) IsInstallableToApex() bool {
1099 if mod.compiler != nil {
1100 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1101 return true
1102 }
1103 if _, ok := mod.compiler.(*binaryDecorator); ok {
1104 return true
1105 }
1106 }
1107 return false
1108}
1109
Ivan Lozanoffee3342019-08-27 12:03:00 -07001110var Bool = proptools.Bool
1111var BoolDefault = proptools.BoolDefault
1112var String = proptools.String
1113var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001114
1115var _ android.OutputFileProducer = (*Module)(nil)