blob: 3d701210f8527172205476e12d3be1bff2d3f49f [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 Lozano3e9f9e42020-12-04 15:05:43 -050076 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
77 Min_sdk_version *string
78
Ivan Lozano43845682020-07-09 21:03:28 -040079 PreventInstall bool
80 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070081}
82
83type Module struct {
84 android.ModuleBase
85 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090086 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070087
Ivan Lozano6a884432020-12-02 09:15:16 -050088 VendorProperties cc.VendorProperties
89
Ivan Lozanoffee3342019-08-27 12:03:00 -070090 Properties BaseProperties
91
92 hod android.HostOrDeviceSupported
93 multilib android.Multilib
94
Ivan Lozano6a884432020-12-02 09:15:16 -050095 makeLinkType string
96
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040098 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020099 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400101 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700102 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400103
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200104 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900105
106 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107}
108
Ivan Lozano43845682020-07-09 21:03:28 -0400109func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
110 switch tag {
111 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700112 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400113 return mod.sourceProvider.Srcs(), nil
114 } else {
115 if mod.outputFile.Valid() {
116 return android.Paths{mod.outputFile.Path()}, nil
117 }
118 return android.Paths{}, nil
119 }
120 default:
121 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
122 }
123}
124
Ivan Lozano52767be2019-10-18 14:49:46 -0700125func (mod *Module) SelectedStl() string {
126 return ""
127}
128
Ivan Lozano2b262972019-11-21 12:30:50 -0800129func (mod *Module) NonCcVariants() bool {
130 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400131 if _, ok := mod.compiler.(libraryInterface); ok {
132 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800133 }
134 }
135 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
136}
137
Ivan Lozano52767be2019-10-18 14:49:46 -0700138func (mod *Module) Static() bool {
139 if mod.compiler != nil {
140 if library, ok := mod.compiler.(libraryInterface); ok {
141 return library.static()
142 }
143 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400144 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700145}
146
147func (mod *Module) Shared() bool {
148 if mod.compiler != nil {
149 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400150 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700151 }
152 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400153 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700154}
155
156func (mod *Module) Toc() android.OptionalPath {
157 if mod.compiler != nil {
158 if _, ok := mod.compiler.(libraryInterface); ok {
159 return android.OptionalPath{}
160 }
161 }
162 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
163}
164
Colin Crossc511bc52020-04-07 16:50:32 +0000165func (mod *Module) UseSdk() bool {
166 return false
167}
168
Ivan Lozano6a884432020-12-02 09:15:16 -0500169// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
170// "product" and "vendor" variant modules return true for this function.
171// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
172// "soc_specific: true" and more vendor installed modules are included here.
173// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
174// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700175func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500176 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700177}
178
179func (mod *Module) MustUseVendorVariant() bool {
180 return false
181}
182
183func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500184 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700185 return false
186}
187
Ivan Lozanof9e21722020-12-02 09:00:51 -0500188func (mod *Module) IsVndkExt() bool {
189 return false
190}
191
192func (c *Module) IsVndkPrivate(config android.Config) bool {
193 return false
194}
195
Ivan Lozano52767be2019-10-18 14:49:46 -0700196func (mod *Module) SdkVersion() string {
197 return ""
198}
199
Colin Crossc511bc52020-04-07 16:50:32 +0000200func (mod *Module) AlwaysSdk() bool {
201 return false
202}
203
Jiyong Park2286afd2020-06-16 21:58:53 +0900204func (mod *Module) IsSdkVariant() bool {
205 return false
206}
207
Colin Cross1348ce32020-10-01 13:37:16 -0700208func (mod *Module) SplitPerApiLevel() bool {
209 return false
210}
211
Ivan Lozanoffee3342019-08-27 12:03:00 -0700212type Deps struct {
213 Dylibs []string
214 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700215 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400216 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700217 ProcMacros []string
218 SharedLibs []string
219 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800220 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221
222 CrtBegin, CrtEnd string
223}
224
225type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400226 DyLibs RustLibraries
227 RLibs RustLibraries
228 SharedLibs android.Paths
229 StaticLibs android.Paths
230 ProcMacros RustLibraries
231 linkDirs []string
232 depFlags []string
233 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700234 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700235
Ivan Lozano45901ed2020-07-24 16:05:01 -0400236 // Used by bindgen modules which call clang
237 depClangFlags []string
238 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400239 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400240 depSystemIncludePaths android.Paths
241
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400242 coverageFiles android.Paths
243
Ivan Lozanof1c84332019-09-20 11:00:37 -0700244 CrtBegin android.OptionalPath
245 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700246
247 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500248 SrcDeps android.Paths
249 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700250}
251
252type RustLibraries []RustLibrary
253
254type RustLibrary struct {
255 Path android.Path
256 CrateName string
257}
258
259type compiler interface {
260 compilerFlags(ctx ModuleContext, flags Flags) Flags
261 compilerProps() []interface{}
262 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
263 compilerDeps(ctx DepsContext, deps Deps) Deps
264 crateName() string
265
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800266 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200267 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400269
270 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400271
272 Disabled() bool
273 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400274
Ivan Lozanodd055472020-09-28 13:22:45 -0400275 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400276}
277
Matthew Maurerbb3add12020-06-25 09:34:12 -0700278type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700279 exportLinkDirs(...string)
280 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400281 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700282}
283
284type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400285 depFlags []string
286 linkDirs []string
287 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700288}
289
Matthew Maurerbb3add12020-06-25 09:34:12 -0700290func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
291 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
292}
293
294func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
295 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
296}
297
Ivan Lozano2093af22020-08-25 12:48:19 -0400298func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
299 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
300}
301
Colin Cross0de8a1e2020-09-18 14:15:30 -0700302func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
303 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
304 Flags: flagExporter.depFlags,
305 LinkDirs: flagExporter.linkDirs,
306 LinkObjects: flagExporter.linkObjects,
307 })
308}
309
Matthew Maurerbb3add12020-06-25 09:34:12 -0700310var _ exportedFlagsProducer = (*flagExporter)(nil)
311
312func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700313 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700314}
315
Colin Cross0de8a1e2020-09-18 14:15:30 -0700316type FlagExporterInfo struct {
317 Flags []string
318 LinkDirs []string // TODO: this should be android.Paths
319 LinkObjects []string // TODO: this should be android.Paths
320}
321
322var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
323
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400324func (mod *Module) isCoverageVariant() bool {
325 return mod.coverage.Properties.IsCoverageVariant
326}
327
328var _ cc.Coverage = (*Module)(nil)
329
330func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
331 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
332}
333
334func (mod *Module) PreventInstall() {
335 mod.Properties.PreventInstall = true
336}
337
338func (mod *Module) HideFromMake() {
339 mod.Properties.HideFromMake = true
340}
341
342func (mod *Module) MarkAsCoverageVariant(coverage bool) {
343 mod.coverage.Properties.IsCoverageVariant = coverage
344}
345
346func (mod *Module) EnableCoverageIfNeeded() {
347 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700348}
349
350func defaultsFactory() android.Module {
351 return DefaultsFactory()
352}
353
354type Defaults struct {
355 android.ModuleBase
356 android.DefaultsModuleBase
357}
358
359func DefaultsFactory(props ...interface{}) android.Module {
360 module := &Defaults{}
361
362 module.AddProperties(props...)
363 module.AddProperties(
364 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500365 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400366 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367 &BaseCompilerProperties{},
368 &BinaryCompilerProperties{},
369 &LibraryCompilerProperties{},
370 &ProcMacroCompilerProperties{},
371 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400372 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700373 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400374 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400375 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200376 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700377 )
378
379 android.InitDefaultsModule(module)
380 return module
381}
382
383func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700384 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385}
386
Ivan Lozano183a3212019-10-18 14:18:45 -0700387func (mod *Module) CcLibrary() bool {
388 if mod.compiler != nil {
389 if _, ok := mod.compiler.(*libraryDecorator); ok {
390 return true
391 }
392 }
393 return false
394}
395
396func (mod *Module) CcLibraryInterface() bool {
397 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400398 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
399 // VariantIs{Static,Shared} is set.
400 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700401 return true
402 }
403 }
404 return false
405}
406
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800407func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700408 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700409 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800410 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700411 }
412 }
413 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
414}
415
416func (mod *Module) SetStatic() {
417 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700418 if library, ok := mod.compiler.(libraryInterface); ok {
419 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700420 return
421 }
422 }
423 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
424}
425
426func (mod *Module) SetShared() {
427 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700428 if library, ok := mod.compiler.(libraryInterface); ok {
429 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700430 return
431 }
432 }
433 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
434}
435
Ivan Lozano183a3212019-10-18 14:18:45 -0700436func (mod *Module) BuildStaticVariant() bool {
437 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700438 if library, ok := mod.compiler.(libraryInterface); ok {
439 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700440 }
441 }
442 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
443}
444
445func (mod *Module) BuildSharedVariant() bool {
446 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700447 if library, ok := mod.compiler.(libraryInterface); ok {
448 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700449 }
450 }
451 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
452}
453
Ivan Lozano183a3212019-10-18 14:18:45 -0700454func (mod *Module) Module() android.Module {
455 return mod
456}
457
Ivan Lozano183a3212019-10-18 14:18:45 -0700458func (mod *Module) OutputFile() android.OptionalPath {
459 return mod.outputFile
460}
461
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400462func (mod *Module) CoverageFiles() android.Paths {
463 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700464 if !mod.compiler.nativeCoverage() {
465 return android.Paths{}
466 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400467 if library, ok := mod.compiler.(*libraryDecorator); ok {
468 if library.coverageFile != nil {
469 return android.Paths{library.coverageFile}
470 }
471 return android.Paths{}
472 }
473 }
474 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
475}
476
Ivan Lozano183a3212019-10-18 14:18:45 -0700477var _ cc.LinkableInterface = (*Module)(nil)
478
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479func (mod *Module) Init() android.Module {
480 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500481 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700482
483 if mod.compiler != nil {
484 mod.AddProperties(mod.compiler.compilerProps()...)
485 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400486 if mod.coverage != nil {
487 mod.AddProperties(mod.coverage.props()...)
488 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200489 if mod.clippy != nil {
490 mod.AddProperties(mod.clippy.props()...)
491 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400492 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700493 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400494 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400495
Ivan Lozanoffee3342019-08-27 12:03:00 -0700496 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900497 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498
499 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700500 return mod
501}
502
503func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
504 return &Module{
505 hod: hod,
506 multilib: multilib,
507 }
508}
509func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
510 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400511 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200512 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700513 return module
514}
515
516type ModuleContext interface {
517 android.ModuleContext
518 ModuleContextIntf
519}
520
521type BaseModuleContext interface {
522 android.BaseModuleContext
523 ModuleContextIntf
524}
525
526type DepsContext interface {
527 android.BottomUpMutatorContext
528 ModuleContextIntf
529}
530
531type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200532 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534}
535
536type depsContext struct {
537 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700538}
539
540type moduleContext struct {
541 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542}
543
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200544type baseModuleContext struct {
545 android.BaseModuleContext
546}
547
548func (ctx *moduleContext) RustModule() *Module {
549 return ctx.Module().(*Module)
550}
551
552func (ctx *moduleContext) toolchain() config.Toolchain {
553 return ctx.RustModule().toolchain(ctx)
554}
555
556func (ctx *depsContext) RustModule() *Module {
557 return ctx.Module().(*Module)
558}
559
560func (ctx *depsContext) toolchain() config.Toolchain {
561 return ctx.RustModule().toolchain(ctx)
562}
563
564func (ctx *baseModuleContext) RustModule() *Module {
565 return ctx.Module().(*Module)
566}
567
568func (ctx *baseModuleContext) toolchain() config.Toolchain {
569 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400570}
571
572func (mod *Module) nativeCoverage() bool {
573 return mod.compiler != nil && mod.compiler.nativeCoverage()
574}
575
Ivan Lozanoffee3342019-08-27 12:03:00 -0700576func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
577 if mod.cachedToolchain == nil {
578 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
579 }
580 return mod.cachedToolchain
581}
582
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200583func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
584 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
585}
586
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
588}
589
590func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
591 ctx := &moduleContext{
592 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594
Jiyong Park99644e92020-11-17 22:21:02 +0900595 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
596 if !apexInfo.IsForPlatform() {
597 mod.hideApexVariantFromMake = true
598 }
599
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500601 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
602
603 // Differentiate static libraries that are vendor available
604 if mod.UseVndk() {
605 mod.Properties.SubName += ".vendor"
606 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607
608 if !toolchain.Supported() {
609 // This toolchain's unsupported, there's nothing to do for this mod.
610 return
611 }
612
613 deps := mod.depsToPaths(ctx)
614 flags := Flags{
615 Toolchain: toolchain,
616 }
617
618 if mod.compiler != nil {
619 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400620 }
621 if mod.coverage != nil {
622 flags, deps = mod.coverage.flags(ctx, flags, deps)
623 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200624 if mod.clippy != nil {
625 flags, deps = mod.clippy.flags(ctx, flags, deps)
626 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400627
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200628 // SourceProvider needs to call GenerateSource() before compiler calls
629 // compile() so it can provide the source. A SourceProvider has
630 // multiple variants (e.g. source, rlib, dylib). Only the "source"
631 // variant is responsible for effectively generating the source. The
632 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400633 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200634 if mod.compiler.(libraryInterface).source() {
635 mod.sourceProvider.GenerateSource(ctx, deps)
636 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
637 } else {
638 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
639 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700640 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200641 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400642 }
643
644 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400646
Ivan Lozanoffee3342019-08-27 12:03:00 -0700647 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400648 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200649 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400650 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651 }
652}
653
654func (mod *Module) deps(ctx DepsContext) Deps {
655 deps := Deps{}
656
657 if mod.compiler != nil {
658 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400659 }
660 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700661 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662 }
663
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400664 if mod.coverage != nil {
665 deps = mod.coverage.deps(ctx, deps)
666 }
667
Ivan Lozanoffee3342019-08-27 12:03:00 -0700668 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
669 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700670 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700671 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
672 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
673 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
674
675 return deps
676
677}
678
Ivan Lozanoffee3342019-08-27 12:03:00 -0700679type dependencyTag struct {
680 blueprint.BaseDependencyTag
681 name string
682 library bool
683 proc_macro bool
684}
685
Jiyong Park65b62242020-11-25 12:44:59 +0900686// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
687// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
688func (d dependencyTag) InstallDepNeeded() bool {
689 return d.library || d.proc_macro
690}
691
692var _ android.InstallNeededDependencyTag = dependencyTag{}
693
Ivan Lozanoffee3342019-08-27 12:03:00 -0700694var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400695 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
696 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
697 dylibDepTag = dependencyTag{name: "dylib", library: true}
698 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
699 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200700 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700701)
702
Jiyong Park99644e92020-11-17 22:21:02 +0900703func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
704 tag, ok := depTag.(dependencyTag)
705 return ok && tag == dylibDepTag
706}
707
Matthew Maurer0f003b12020-06-29 14:34:06 -0700708type autoDep struct {
709 variation string
710 depTag dependencyTag
711}
712
713var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200714 rlibVariation = "rlib"
715 dylibVariation = "dylib"
716 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
717 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700718)
719
720type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400721 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700722}
723
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400724func (mod *Module) begin(ctx BaseModuleContext) {
725 if mod.coverage != nil {
726 mod.coverage.begin(ctx)
727 }
728}
729
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
731 var depPaths PathDeps
732
733 directRlibDeps := []*Module{}
734 directDylibDeps := []*Module{}
735 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700736 directSharedLibDeps := [](cc.LinkableInterface){}
737 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400738 directSrcProvidersDeps := []*Module{}
739 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740
741 ctx.VisitDirectDeps(func(dep android.Module) {
742 depName := ctx.OtherModuleName(dep)
743 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400744 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700745 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700746
Ivan Lozanoffee3342019-08-27 12:03:00 -0700747 switch depTag {
748 case dylibDepTag:
749 dylib, ok := rustDep.compiler.(libraryInterface)
750 if !ok || !dylib.dylib() {
751 ctx.ModuleErrorf("mod %q not an dylib library", depName)
752 return
753 }
754 directDylibDeps = append(directDylibDeps, rustDep)
755 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
756 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400757
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758 rlib, ok := rustDep.compiler.(libraryInterface)
759 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400760 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 return
762 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400763 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400765 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700766 case procMacroDepTag:
767 directProcMacroDeps = append(directProcMacroDeps, rustDep)
768 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400769 case android.SourceDepTag:
770 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
771 // OS/Arch variant is used.
772 var helper string
773 if ctx.Host() {
774 helper = "missing 'host_supported'?"
775 } else {
776 helper = "device module defined?"
777 }
778
779 if dep.Target().Os != ctx.Os() {
780 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
781 return
782 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
783 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
784 return
785 }
786 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787 }
788
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400789 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700790 if depTag != procMacroDepTag {
791 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
792 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
793 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
794 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 }
796
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400798 linkFile := rustDep.outputFile
799 if !linkFile.Valid() {
800 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
801 depName, ctx.ModuleName())
802 return
803 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700805 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
806 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 }
808 }
809
Ivan Lozano89435d12020-07-31 11:01:18 -0400810 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700811 //Handle C dependencies
812 if _, ok := ccDep.(*Module); !ok {
813 if ccDep.Module().Target().Os != ctx.Os() {
814 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
815 return
816 }
817 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
818 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
819 return
820 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700821 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400822 linkObject := ccDep.OutputFile()
823 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500824
Ivan Lozano2093af22020-08-25 12:48:19 -0400825 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
827 }
828
829 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700830 switch {
831 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400833 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700834 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
835 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
836 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
837 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
838 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400839 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 directStaticLibDeps = append(directStaticLibDeps, ccDep)
841 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700842 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700843 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400844 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700845 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
846 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
847 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
848 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
849 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850 directSharedLibDeps = append(directSharedLibDeps, ccDep)
851 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
852 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800853 case cc.IsHeaderDepTag(depTag):
854 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
855 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
856 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
857 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700858 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400859 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700860 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400861 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 }
863
864 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700865 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
866 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400867 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700868 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400870
871 if srcDep, ok := dep.(android.SourceFileProducer); ok {
872 switch depTag {
873 case android.SourceDepTag:
874 // These are usually genrules which don't have per-target variants.
875 directSrcDeps = append(directSrcDeps, srcDep)
876 }
877 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 })
879
880 var rlibDepFiles RustLibraries
881 for _, dep := range directRlibDeps {
882 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
883 }
884 var dylibDepFiles RustLibraries
885 for _, dep := range directDylibDeps {
886 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
887 }
888 var procMacroDepFiles RustLibraries
889 for _, dep := range directProcMacroDeps {
890 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
891 }
892
893 var staticLibDepFiles android.Paths
894 for _, dep := range directStaticLibDeps {
895 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
896 }
897
898 var sharedLibDepFiles android.Paths
899 for _, dep := range directSharedLibDeps {
900 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
901 }
902
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400903 var srcProviderDepFiles android.Paths
904 for _, dep := range directSrcProvidersDeps {
905 srcs, _ := dep.OutputFiles("")
906 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
907 }
908 for _, dep := range directSrcDeps {
909 srcs := dep.Srcs()
910 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
911 }
912
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
914 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
915 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
916 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
917 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400918 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919
920 // Dedup exported flags from dependencies
921 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
922 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400923 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
924 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
925 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926
927 return depPaths
928}
929
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800930func (mod *Module) InstallInData() bool {
931 if mod.compiler == nil {
932 return false
933 }
934 return mod.compiler.inData()
935}
936
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937func linkPathFromFilePath(filepath android.Path) string {
938 return strings.Split(filepath.String(), filepath.Base())[0]
939}
Ivan Lozanod648c432020-02-06 12:05:10 -0500940
Ivan Lozanoffee3342019-08-27 12:03:00 -0700941func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
942 ctx := &depsContext{
943 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945
946 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700947 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -0400948
Ivan Lozano2b081132020-09-08 12:46:52 -0400949 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400950 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400951 stdLinkage = "rlib-std"
952 }
953
954 rlibDepVariations := commonDepVariations
955 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
956 rlibDepVariations = append(rlibDepVariations,
957 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
958 }
959
Ivan Lozano52767be2019-10-18 14:49:46 -0700960 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400961 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200962 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700963 rlibDepTag, deps.Rlibs...)
964 actx.AddVariationDependencies(
965 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200966 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700967 dylibDepTag, deps.Dylibs...)
968
Ivan Lozano042504f2020-08-18 14:31:23 -0400969 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
970 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400971 if autoDep.depTag == rlibDepTag {
972 actx.AddVariationDependencies(
973 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
974 autoDep.depTag, deps.Rustlibs...)
975 } else {
976 actx.AddVariationDependencies(
977 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
978 autoDep.depTag, deps.Rustlibs...)
979 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700980 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400981 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400982 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400983 actx.AddVariationDependencies(
984 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
985 rlibDepTag, deps.Stdlibs...)
986 } else {
987 actx.AddVariationDependencies(
988 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
989 dylibDepTag, deps.Stdlibs...)
990 }
991 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700992 actx.AddVariationDependencies(append(commonDepVariations,
993 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700994 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700995 actx.AddVariationDependencies(append(commonDepVariations,
996 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700997 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700998
Zach Johnson3df4e632020-11-06 11:56:27 -0800999 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1000
Colin Cross565cafd2020-09-25 18:47:38 -07001001 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001002 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001003 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001004 }
1005 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001006 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001007 }
1008
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001009 if mod.sourceProvider != nil {
1010 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1011 bindgen.Properties.Custom_bindgen != "" {
1012 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1013 bindgen.Properties.Custom_bindgen)
1014 }
1015 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001016 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001017 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018}
1019
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001020func BeginMutator(ctx android.BottomUpMutatorContext) {
1021 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1022 mod.beginMutator(ctx)
1023 }
1024}
1025
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001026func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1027 ctx := &baseModuleContext{
1028 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001029 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001030
1031 mod.begin(ctx)
1032}
1033
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034func (mod *Module) Name() string {
1035 name := mod.ModuleBase.Name()
1036 if p, ok := mod.compiler.(interface {
1037 Name(string) string
1038 }); ok {
1039 name = p.Name(name)
1040 }
1041 return name
1042}
1043
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001044func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001045 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001046 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001047 }
1048}
1049
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001050var _ android.HostToolProvider = (*Module)(nil)
1051
1052func (mod *Module) HostToolPath() android.OptionalPath {
1053 if !mod.Host() {
1054 return android.OptionalPath{}
1055 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001056 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1057 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001058 }
1059 return android.OptionalPath{}
1060}
1061
Jiyong Park99644e92020-11-17 22:21:02 +09001062var _ android.ApexModule = (*Module)(nil)
1063
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001064func (mod *Module) minSdkVersion() string {
1065 return String(mod.Properties.Min_sdk_version)
1066}
1067
Jiyong Park99644e92020-11-17 22:21:02 +09001068func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001069 minSdkVersion := mod.minSdkVersion()
1070 if minSdkVersion == "apex_inherit" {
1071 return nil
1072 }
1073 if minSdkVersion == "" {
1074 return fmt.Errorf("min_sdk_version is not specificed")
1075 }
1076
1077 // Not using nativeApiLevelFromUser because the context here is not
1078 // necessarily a native context.
1079 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1080 if err != nil {
1081 return err
1082 }
1083
1084 if ver.GreaterThan(sdkVersion) {
1085 return fmt.Errorf("newer SDK(%v)", ver)
1086 }
Jiyong Park99644e92020-11-17 22:21:02 +09001087 return nil
1088}
1089
1090func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1091 depTag := ctx.OtherModuleDependencyTag(dep)
1092
1093 if ccm, ok := dep.(*cc.Module); ok {
1094 if ccm.HasStubsVariants() {
1095 if cc.IsSharedDepTag(depTag) {
1096 // dynamic dep to a stubs lib crosses APEX boundary
1097 return false
1098 }
1099 if cc.IsRuntimeDepTag(depTag) {
1100 // runtime dep to a stubs lib also crosses APEX boundary
1101 return false
1102 }
1103
1104 if cc.IsHeaderDepTag(depTag) {
1105 return false
1106 }
1107 }
1108 if mod.Static() && cc.IsSharedDepTag(depTag) {
1109 // shared_lib dependency from a static lib is considered as crossing
1110 // the APEX boundary because the dependency doesn't actually is
1111 // linked; the dependency is used only during the compilation phase.
1112 return false
1113 }
1114 }
1115
1116 if depTag == procMacroDepTag {
1117 return false
1118 }
1119
1120 return true
1121}
1122
1123// Overrides ApexModule.IsInstallabeToApex()
1124func (mod *Module) IsInstallableToApex() bool {
1125 if mod.compiler != nil {
1126 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1127 return true
1128 }
1129 if _, ok := mod.compiler.(*binaryDecorator); ok {
1130 return true
1131 }
1132 }
1133 return false
1134}
1135
Ivan Lozanoffee3342019-08-27 12:03:00 -07001136var Bool = proptools.Bool
1137var BoolDefault = proptools.BoolDefault
1138var String = proptools.String
1139var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001140
1141var _ android.OutputFileProducer = (*Module)(nil)