blob: cc70a7ad8ac61f354c1d49519cdec5a57a4ba1b1 [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
Jiyong Park459feca2020-12-15 11:02:21 +0900477func (mod *Module) installable(apexInfo android.ApexInfo) bool {
478 // The apex variant is not installable because it is included in the APEX and won't appear
479 // in the system partition as a standalone file.
480 if !apexInfo.IsForPlatform() {
481 return false
482 }
483
484 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
485}
486
Ivan Lozano183a3212019-10-18 14:18:45 -0700487var _ cc.LinkableInterface = (*Module)(nil)
488
Ivan Lozanoffee3342019-08-27 12:03:00 -0700489func (mod *Module) Init() android.Module {
490 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500491 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700492
493 if mod.compiler != nil {
494 mod.AddProperties(mod.compiler.compilerProps()...)
495 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400496 if mod.coverage != nil {
497 mod.AddProperties(mod.coverage.props()...)
498 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200499 if mod.clippy != nil {
500 mod.AddProperties(mod.clippy.props()...)
501 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400502 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700503 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400504 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400505
Ivan Lozanoffee3342019-08-27 12:03:00 -0700506 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900507 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700508
509 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700510 return mod
511}
512
513func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
514 return &Module{
515 hod: hod,
516 multilib: multilib,
517 }
518}
519func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
520 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400521 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200522 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700523 return module
524}
525
526type ModuleContext interface {
527 android.ModuleContext
528 ModuleContextIntf
529}
530
531type BaseModuleContext interface {
532 android.BaseModuleContext
533 ModuleContextIntf
534}
535
536type DepsContext interface {
537 android.BottomUpMutatorContext
538 ModuleContextIntf
539}
540
541type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200542 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700544}
545
546type depsContext struct {
547 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700548}
549
550type moduleContext struct {
551 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700552}
553
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200554type baseModuleContext struct {
555 android.BaseModuleContext
556}
557
558func (ctx *moduleContext) RustModule() *Module {
559 return ctx.Module().(*Module)
560}
561
562func (ctx *moduleContext) toolchain() config.Toolchain {
563 return ctx.RustModule().toolchain(ctx)
564}
565
566func (ctx *depsContext) RustModule() *Module {
567 return ctx.Module().(*Module)
568}
569
570func (ctx *depsContext) toolchain() config.Toolchain {
571 return ctx.RustModule().toolchain(ctx)
572}
573
574func (ctx *baseModuleContext) RustModule() *Module {
575 return ctx.Module().(*Module)
576}
577
578func (ctx *baseModuleContext) toolchain() config.Toolchain {
579 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400580}
581
582func (mod *Module) nativeCoverage() bool {
583 return mod.compiler != nil && mod.compiler.nativeCoverage()
584}
585
Ivan Lozanoffee3342019-08-27 12:03:00 -0700586func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
587 if mod.cachedToolchain == nil {
588 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
589 }
590 return mod.cachedToolchain
591}
592
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200593func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
594 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
595}
596
Ivan Lozanoffee3342019-08-27 12:03:00 -0700597func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
598}
599
600func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
601 ctx := &moduleContext{
602 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700603 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604
Jiyong Park99644e92020-11-17 22:21:02 +0900605 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
606 if !apexInfo.IsForPlatform() {
607 mod.hideApexVariantFromMake = true
608 }
609
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500611 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
612
613 // Differentiate static libraries that are vendor available
614 if mod.UseVndk() {
615 mod.Properties.SubName += ".vendor"
616 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700617
618 if !toolchain.Supported() {
619 // This toolchain's unsupported, there's nothing to do for this mod.
620 return
621 }
622
623 deps := mod.depsToPaths(ctx)
624 flags := Flags{
625 Toolchain: toolchain,
626 }
627
628 if mod.compiler != nil {
629 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400630 }
631 if mod.coverage != nil {
632 flags, deps = mod.coverage.flags(ctx, flags, deps)
633 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200634 if mod.clippy != nil {
635 flags, deps = mod.clippy.flags(ctx, flags, deps)
636 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400637
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200638 // SourceProvider needs to call GenerateSource() before compiler calls
639 // compile() so it can provide the source. A SourceProvider has
640 // multiple variants (e.g. source, rlib, dylib). Only the "source"
641 // variant is responsible for effectively generating the source. The
642 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400643 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200644 if mod.compiler.(libraryInterface).source() {
645 mod.sourceProvider.GenerateSource(ctx, deps)
646 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
647 } else {
648 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
649 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700650 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200651 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400652 }
653
654 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400656
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900658
659 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
660 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200661 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400662 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663 }
664}
665
666func (mod *Module) deps(ctx DepsContext) Deps {
667 deps := Deps{}
668
669 if mod.compiler != nil {
670 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400671 }
672 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700673 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674 }
675
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400676 if mod.coverage != nil {
677 deps = mod.coverage.deps(ctx, deps)
678 }
679
Ivan Lozanoffee3342019-08-27 12:03:00 -0700680 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
681 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700682 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
684 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
685 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
686
687 return deps
688
689}
690
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691type dependencyTag struct {
692 blueprint.BaseDependencyTag
693 name string
694 library bool
695 proc_macro bool
696}
697
Jiyong Park65b62242020-11-25 12:44:59 +0900698// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
699// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
700func (d dependencyTag) InstallDepNeeded() bool {
701 return d.library || d.proc_macro
702}
703
704var _ android.InstallNeededDependencyTag = dependencyTag{}
705
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400707 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
708 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
709 dylibDepTag = dependencyTag{name: "dylib", library: true}
710 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
711 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200712 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700713)
714
Jiyong Park99644e92020-11-17 22:21:02 +0900715func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
716 tag, ok := depTag.(dependencyTag)
717 return ok && tag == dylibDepTag
718}
719
Matthew Maurer0f003b12020-06-29 14:34:06 -0700720type autoDep struct {
721 variation string
722 depTag dependencyTag
723}
724
725var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200726 rlibVariation = "rlib"
727 dylibVariation = "dylib"
728 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
729 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700730)
731
732type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400733 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700734}
735
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400736func (mod *Module) begin(ctx BaseModuleContext) {
737 if mod.coverage != nil {
738 mod.coverage.begin(ctx)
739 }
740}
741
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
743 var depPaths PathDeps
744
745 directRlibDeps := []*Module{}
746 directDylibDeps := []*Module{}
747 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700748 directSharedLibDeps := [](cc.LinkableInterface){}
749 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400750 directSrcProvidersDeps := []*Module{}
751 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752
753 ctx.VisitDirectDeps(func(dep android.Module) {
754 depName := ctx.OtherModuleName(dep)
755 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400756 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700758
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759 switch depTag {
760 case dylibDepTag:
761 dylib, ok := rustDep.compiler.(libraryInterface)
762 if !ok || !dylib.dylib() {
763 ctx.ModuleErrorf("mod %q not an dylib library", depName)
764 return
765 }
766 directDylibDeps = append(directDylibDeps, rustDep)
767 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
768 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400769
Ivan Lozanoffee3342019-08-27 12:03:00 -0700770 rlib, ok := rustDep.compiler.(libraryInterface)
771 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400772 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773 return
774 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400775 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700776 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400777 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 case procMacroDepTag:
779 directProcMacroDeps = append(directProcMacroDeps, rustDep)
780 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400781 case android.SourceDepTag:
782 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
783 // OS/Arch variant is used.
784 var helper string
785 if ctx.Host() {
786 helper = "missing 'host_supported'?"
787 } else {
788 helper = "device module defined?"
789 }
790
791 if dep.Target().Os != ctx.Os() {
792 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
793 return
794 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
795 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
796 return
797 }
798 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 }
800
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400801 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700802 if depTag != procMacroDepTag {
803 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
804 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
805 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
806 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 }
808
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400810 linkFile := rustDep.outputFile
811 if !linkFile.Valid() {
812 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
813 depName, ctx.ModuleName())
814 return
815 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700817 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
818 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 }
820 }
821
Ivan Lozano89435d12020-07-31 11:01:18 -0400822 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700823 //Handle C dependencies
824 if _, ok := ccDep.(*Module); !ok {
825 if ccDep.Module().Target().Os != ctx.Os() {
826 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
827 return
828 }
829 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
830 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
831 return
832 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700833 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400834 linkObject := ccDep.OutputFile()
835 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500836
Ivan Lozano2093af22020-08-25 12:48:19 -0400837 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700838 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
839 }
840
841 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700842 switch {
843 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400845 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700846 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
847 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
848 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
849 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
850 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400851 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852 directStaticLibDeps = append(directStaticLibDeps, ccDep)
853 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700854 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400856 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700857 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
858 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
859 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
860 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
861 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 directSharedLibDeps = append(directSharedLibDeps, ccDep)
863 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
864 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800865 case cc.IsHeaderDepTag(depTag):
866 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
867 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
868 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
869 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700870 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400871 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700872 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400873 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 }
875
876 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700877 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
878 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400879 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400882
883 if srcDep, ok := dep.(android.SourceFileProducer); ok {
884 switch depTag {
885 case android.SourceDepTag:
886 // These are usually genrules which don't have per-target variants.
887 directSrcDeps = append(directSrcDeps, srcDep)
888 }
889 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 })
891
892 var rlibDepFiles RustLibraries
893 for _, dep := range directRlibDeps {
894 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
895 }
896 var dylibDepFiles RustLibraries
897 for _, dep := range directDylibDeps {
898 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
899 }
900 var procMacroDepFiles RustLibraries
901 for _, dep := range directProcMacroDeps {
902 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
903 }
904
905 var staticLibDepFiles android.Paths
906 for _, dep := range directStaticLibDeps {
907 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
908 }
909
910 var sharedLibDepFiles android.Paths
911 for _, dep := range directSharedLibDeps {
912 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
913 }
914
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400915 var srcProviderDepFiles android.Paths
916 for _, dep := range directSrcProvidersDeps {
917 srcs, _ := dep.OutputFiles("")
918 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
919 }
920 for _, dep := range directSrcDeps {
921 srcs := dep.Srcs()
922 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
923 }
924
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
926 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
927 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
928 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
929 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400930 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931
932 // Dedup exported flags from dependencies
933 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
934 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400935 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
936 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
937 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938
939 return depPaths
940}
941
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800942func (mod *Module) InstallInData() bool {
943 if mod.compiler == nil {
944 return false
945 }
946 return mod.compiler.inData()
947}
948
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949func linkPathFromFilePath(filepath android.Path) string {
950 return strings.Split(filepath.String(), filepath.Base())[0]
951}
Ivan Lozanod648c432020-02-06 12:05:10 -0500952
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
954 ctx := &depsContext{
955 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957
958 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700959 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -0400960
Ivan Lozano2b081132020-09-08 12:46:52 -0400961 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400962 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400963 stdLinkage = "rlib-std"
964 }
965
966 rlibDepVariations := commonDepVariations
967 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
968 rlibDepVariations = append(rlibDepVariations,
969 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
970 }
971
Ivan Lozano52767be2019-10-18 14:49:46 -0700972 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400973 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200974 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700975 rlibDepTag, deps.Rlibs...)
976 actx.AddVariationDependencies(
977 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200978 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700979 dylibDepTag, deps.Dylibs...)
980
Ivan Lozano042504f2020-08-18 14:31:23 -0400981 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
982 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400983 if autoDep.depTag == rlibDepTag {
984 actx.AddVariationDependencies(
985 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
986 autoDep.depTag, deps.Rustlibs...)
987 } else {
988 actx.AddVariationDependencies(
989 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
990 autoDep.depTag, deps.Rustlibs...)
991 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700992 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400993 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400994 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400995 actx.AddVariationDependencies(
996 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
997 rlibDepTag, deps.Stdlibs...)
998 } else {
999 actx.AddVariationDependencies(
1000 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1001 dylibDepTag, deps.Stdlibs...)
1002 }
1003 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001004 actx.AddVariationDependencies(append(commonDepVariations,
1005 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001006 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001007 actx.AddVariationDependencies(append(commonDepVariations,
1008 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001009 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001010
Zach Johnson3df4e632020-11-06 11:56:27 -08001011 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1012
Colin Cross565cafd2020-09-25 18:47:38 -07001013 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001014 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001015 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001016 }
1017 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001018 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001019 }
1020
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001021 if mod.sourceProvider != nil {
1022 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1023 bindgen.Properties.Custom_bindgen != "" {
1024 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1025 bindgen.Properties.Custom_bindgen)
1026 }
1027 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001028 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001029 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030}
1031
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001032func BeginMutator(ctx android.BottomUpMutatorContext) {
1033 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1034 mod.beginMutator(ctx)
1035 }
1036}
1037
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001038func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1039 ctx := &baseModuleContext{
1040 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001041 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001042
1043 mod.begin(ctx)
1044}
1045
Ivan Lozanoffee3342019-08-27 12:03:00 -07001046func (mod *Module) Name() string {
1047 name := mod.ModuleBase.Name()
1048 if p, ok := mod.compiler.(interface {
1049 Name(string) string
1050 }); ok {
1051 name = p.Name(name)
1052 }
1053 return name
1054}
1055
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001056func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001057 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001058 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001059 }
1060}
1061
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001062var _ android.HostToolProvider = (*Module)(nil)
1063
1064func (mod *Module) HostToolPath() android.OptionalPath {
1065 if !mod.Host() {
1066 return android.OptionalPath{}
1067 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001068 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1069 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001070 }
1071 return android.OptionalPath{}
1072}
1073
Jiyong Park99644e92020-11-17 22:21:02 +09001074var _ android.ApexModule = (*Module)(nil)
1075
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001076func (mod *Module) minSdkVersion() string {
1077 return String(mod.Properties.Min_sdk_version)
1078}
1079
Jiyong Park99644e92020-11-17 22:21:02 +09001080func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001081 minSdkVersion := mod.minSdkVersion()
1082 if minSdkVersion == "apex_inherit" {
1083 return nil
1084 }
1085 if minSdkVersion == "" {
1086 return fmt.Errorf("min_sdk_version is not specificed")
1087 }
1088
1089 // Not using nativeApiLevelFromUser because the context here is not
1090 // necessarily a native context.
1091 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1092 if err != nil {
1093 return err
1094 }
1095
1096 if ver.GreaterThan(sdkVersion) {
1097 return fmt.Errorf("newer SDK(%v)", ver)
1098 }
Jiyong Park99644e92020-11-17 22:21:02 +09001099 return nil
1100}
1101
1102func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1103 depTag := ctx.OtherModuleDependencyTag(dep)
1104
1105 if ccm, ok := dep.(*cc.Module); ok {
1106 if ccm.HasStubsVariants() {
1107 if cc.IsSharedDepTag(depTag) {
1108 // dynamic dep to a stubs lib crosses APEX boundary
1109 return false
1110 }
1111 if cc.IsRuntimeDepTag(depTag) {
1112 // runtime dep to a stubs lib also crosses APEX boundary
1113 return false
1114 }
1115
1116 if cc.IsHeaderDepTag(depTag) {
1117 return false
1118 }
1119 }
1120 if mod.Static() && cc.IsSharedDepTag(depTag) {
1121 // shared_lib dependency from a static lib is considered as crossing
1122 // the APEX boundary because the dependency doesn't actually is
1123 // linked; the dependency is used only during the compilation phase.
1124 return false
1125 }
1126 }
1127
1128 if depTag == procMacroDepTag {
1129 return false
1130 }
1131
1132 return true
1133}
1134
1135// Overrides ApexModule.IsInstallabeToApex()
1136func (mod *Module) IsInstallableToApex() bool {
1137 if mod.compiler != nil {
1138 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1139 return true
1140 }
1141 if _, ok := mod.compiler.(*binaryDecorator); ok {
1142 return true
1143 }
1144 }
1145 return false
1146}
1147
Ivan Lozanoffee3342019-08-27 12:03:00 -07001148var Bool = proptools.Bool
1149var BoolDefault = proptools.BoolDefault
1150var String = proptools.String
1151var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001152
1153var _ android.OutputFileProducer = (*Module)(nil)