blob: 531d961918e2c5a75838ab9a6d520637578b4a95 [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
248 SrcDeps android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700249}
250
251type RustLibraries []RustLibrary
252
253type RustLibrary struct {
254 Path android.Path
255 CrateName string
256}
257
258type compiler interface {
259 compilerFlags(ctx ModuleContext, flags Flags) Flags
260 compilerProps() []interface{}
261 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
262 compilerDeps(ctx DepsContext, deps Deps) Deps
263 crateName() string
264
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800265 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200266 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700267 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400268
269 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400270
271 Disabled() bool
272 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400273
Ivan Lozanodd055472020-09-28 13:22:45 -0400274 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400275}
276
Matthew Maurerbb3add12020-06-25 09:34:12 -0700277type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700278 exportLinkDirs(...string)
279 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400280 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700281}
282
283type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400284 depFlags []string
285 linkDirs []string
286 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700287}
288
Matthew Maurerbb3add12020-06-25 09:34:12 -0700289func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
290 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
291}
292
293func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
294 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
295}
296
Ivan Lozano2093af22020-08-25 12:48:19 -0400297func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
298 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
299}
300
Colin Cross0de8a1e2020-09-18 14:15:30 -0700301func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
302 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
303 Flags: flagExporter.depFlags,
304 LinkDirs: flagExporter.linkDirs,
305 LinkObjects: flagExporter.linkObjects,
306 })
307}
308
Matthew Maurerbb3add12020-06-25 09:34:12 -0700309var _ exportedFlagsProducer = (*flagExporter)(nil)
310
311func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700312 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700313}
314
Colin Cross0de8a1e2020-09-18 14:15:30 -0700315type FlagExporterInfo struct {
316 Flags []string
317 LinkDirs []string // TODO: this should be android.Paths
318 LinkObjects []string // TODO: this should be android.Paths
319}
320
321var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
322
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400323func (mod *Module) isCoverageVariant() bool {
324 return mod.coverage.Properties.IsCoverageVariant
325}
326
327var _ cc.Coverage = (*Module)(nil)
328
329func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
330 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
331}
332
333func (mod *Module) PreventInstall() {
334 mod.Properties.PreventInstall = true
335}
336
337func (mod *Module) HideFromMake() {
338 mod.Properties.HideFromMake = true
339}
340
341func (mod *Module) MarkAsCoverageVariant(coverage bool) {
342 mod.coverage.Properties.IsCoverageVariant = coverage
343}
344
345func (mod *Module) EnableCoverageIfNeeded() {
346 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700347}
348
349func defaultsFactory() android.Module {
350 return DefaultsFactory()
351}
352
353type Defaults struct {
354 android.ModuleBase
355 android.DefaultsModuleBase
356}
357
358func DefaultsFactory(props ...interface{}) android.Module {
359 module := &Defaults{}
360
361 module.AddProperties(props...)
362 module.AddProperties(
363 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500364 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400365 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700366 &BaseCompilerProperties{},
367 &BinaryCompilerProperties{},
368 &LibraryCompilerProperties{},
369 &ProcMacroCompilerProperties{},
370 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400371 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700372 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400373 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400374 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200375 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376 )
377
378 android.InitDefaultsModule(module)
379 return module
380}
381
382func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700383 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700384}
385
Ivan Lozano183a3212019-10-18 14:18:45 -0700386func (mod *Module) CcLibrary() bool {
387 if mod.compiler != nil {
388 if _, ok := mod.compiler.(*libraryDecorator); ok {
389 return true
390 }
391 }
392 return false
393}
394
395func (mod *Module) CcLibraryInterface() bool {
396 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400397 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
398 // VariantIs{Static,Shared} is set.
399 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700400 return true
401 }
402 }
403 return false
404}
405
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800406func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700407 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700408 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800409 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700410 }
411 }
412 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
413}
414
415func (mod *Module) SetStatic() {
416 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700417 if library, ok := mod.compiler.(libraryInterface); ok {
418 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700419 return
420 }
421 }
422 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
423}
424
425func (mod *Module) SetShared() {
426 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700427 if library, ok := mod.compiler.(libraryInterface); ok {
428 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700429 return
430 }
431 }
432 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
433}
434
Ivan Lozano183a3212019-10-18 14:18:45 -0700435func (mod *Module) BuildStaticVariant() bool {
436 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700437 if library, ok := mod.compiler.(libraryInterface); ok {
438 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700439 }
440 }
441 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
442}
443
444func (mod *Module) BuildSharedVariant() bool {
445 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700446 if library, ok := mod.compiler.(libraryInterface); ok {
447 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700448 }
449 }
450 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
451}
452
Ivan Lozano183a3212019-10-18 14:18:45 -0700453func (mod *Module) Module() android.Module {
454 return mod
455}
456
Ivan Lozano183a3212019-10-18 14:18:45 -0700457func (mod *Module) OutputFile() android.OptionalPath {
458 return mod.outputFile
459}
460
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400461func (mod *Module) CoverageFiles() android.Paths {
462 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700463 if !mod.compiler.nativeCoverage() {
464 return android.Paths{}
465 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400466 if library, ok := mod.compiler.(*libraryDecorator); ok {
467 if library.coverageFile != nil {
468 return android.Paths{library.coverageFile}
469 }
470 return android.Paths{}
471 }
472 }
473 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
474}
475
Ivan Lozano183a3212019-10-18 14:18:45 -0700476var _ cc.LinkableInterface = (*Module)(nil)
477
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478func (mod *Module) Init() android.Module {
479 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500480 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481
482 if mod.compiler != nil {
483 mod.AddProperties(mod.compiler.compilerProps()...)
484 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400485 if mod.coverage != nil {
486 mod.AddProperties(mod.coverage.props()...)
487 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200488 if mod.clippy != nil {
489 mod.AddProperties(mod.clippy.props()...)
490 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400491 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700492 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400493 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400494
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900496 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700497
498 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700499 return mod
500}
501
502func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
503 return &Module{
504 hod: hod,
505 multilib: multilib,
506 }
507}
508func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
509 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400510 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200511 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700512 return module
513}
514
515type ModuleContext interface {
516 android.ModuleContext
517 ModuleContextIntf
518}
519
520type BaseModuleContext interface {
521 android.BaseModuleContext
522 ModuleContextIntf
523}
524
525type DepsContext interface {
526 android.BottomUpMutatorContext
527 ModuleContextIntf
528}
529
530type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200531 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700532 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533}
534
535type depsContext struct {
536 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700537}
538
539type moduleContext struct {
540 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541}
542
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200543type baseModuleContext struct {
544 android.BaseModuleContext
545}
546
547func (ctx *moduleContext) RustModule() *Module {
548 return ctx.Module().(*Module)
549}
550
551func (ctx *moduleContext) toolchain() config.Toolchain {
552 return ctx.RustModule().toolchain(ctx)
553}
554
555func (ctx *depsContext) RustModule() *Module {
556 return ctx.Module().(*Module)
557}
558
559func (ctx *depsContext) toolchain() config.Toolchain {
560 return ctx.RustModule().toolchain(ctx)
561}
562
563func (ctx *baseModuleContext) RustModule() *Module {
564 return ctx.Module().(*Module)
565}
566
567func (ctx *baseModuleContext) toolchain() config.Toolchain {
568 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400569}
570
571func (mod *Module) nativeCoverage() bool {
572 return mod.compiler != nil && mod.compiler.nativeCoverage()
573}
574
Ivan Lozanoffee3342019-08-27 12:03:00 -0700575func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
576 if mod.cachedToolchain == nil {
577 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
578 }
579 return mod.cachedToolchain
580}
581
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200582func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
583 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
584}
585
Ivan Lozanoffee3342019-08-27 12:03:00 -0700586func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
587}
588
589func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
590 ctx := &moduleContext{
591 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700592 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593
Jiyong Park99644e92020-11-17 22:21:02 +0900594 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
595 if !apexInfo.IsForPlatform() {
596 mod.hideApexVariantFromMake = true
597 }
598
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500600 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
601
602 // Differentiate static libraries that are vendor available
603 if mod.UseVndk() {
604 mod.Properties.SubName += ".vendor"
605 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606
607 if !toolchain.Supported() {
608 // This toolchain's unsupported, there's nothing to do for this mod.
609 return
610 }
611
612 deps := mod.depsToPaths(ctx)
613 flags := Flags{
614 Toolchain: toolchain,
615 }
616
617 if mod.compiler != nil {
618 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400619 }
620 if mod.coverage != nil {
621 flags, deps = mod.coverage.flags(ctx, flags, deps)
622 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200623 if mod.clippy != nil {
624 flags, deps = mod.clippy.flags(ctx, flags, deps)
625 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400626
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200627 // SourceProvider needs to call GenerateSource() before compiler calls
628 // compile() so it can provide the source. A SourceProvider has
629 // multiple variants (e.g. source, rlib, dylib). Only the "source"
630 // variant is responsible for effectively generating the source. The
631 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400632 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200633 if mod.compiler.(libraryInterface).source() {
634 mod.sourceProvider.GenerateSource(ctx, deps)
635 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
636 } else {
637 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
638 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700639 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200640 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400641 }
642
643 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400645
Ivan Lozanoffee3342019-08-27 12:03:00 -0700646 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400647 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200648 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400649 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700650 }
651}
652
653func (mod *Module) deps(ctx DepsContext) Deps {
654 deps := Deps{}
655
656 if mod.compiler != nil {
657 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400658 }
659 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700660 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700661 }
662
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400663 if mod.coverage != nil {
664 deps = mod.coverage.deps(ctx, deps)
665 }
666
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
668 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700669 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
671 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
672 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
673
674 return deps
675
676}
677
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678type dependencyTag struct {
679 blueprint.BaseDependencyTag
680 name string
681 library bool
682 proc_macro bool
683}
684
Jiyong Park65b62242020-11-25 12:44:59 +0900685// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
686// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
687func (d dependencyTag) InstallDepNeeded() bool {
688 return d.library || d.proc_macro
689}
690
691var _ android.InstallNeededDependencyTag = dependencyTag{}
692
Ivan Lozanoffee3342019-08-27 12:03:00 -0700693var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400694 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
695 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
696 dylibDepTag = dependencyTag{name: "dylib", library: true}
697 procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
698 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200699 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700)
701
Jiyong Park99644e92020-11-17 22:21:02 +0900702func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
703 tag, ok := depTag.(dependencyTag)
704 return ok && tag == dylibDepTag
705}
706
Matthew Maurer0f003b12020-06-29 14:34:06 -0700707type autoDep struct {
708 variation string
709 depTag dependencyTag
710}
711
712var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200713 rlibVariation = "rlib"
714 dylibVariation = "dylib"
715 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
716 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700717)
718
719type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400720 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700721}
722
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400723func (mod *Module) begin(ctx BaseModuleContext) {
724 if mod.coverage != nil {
725 mod.coverage.begin(ctx)
726 }
727}
728
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
730 var depPaths PathDeps
731
732 directRlibDeps := []*Module{}
733 directDylibDeps := []*Module{}
734 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700735 directSharedLibDeps := [](cc.LinkableInterface){}
736 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400737 directSrcProvidersDeps := []*Module{}
738 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739
740 ctx.VisitDirectDeps(func(dep android.Module) {
741 depName := ctx.OtherModuleName(dep)
742 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400743 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700745
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 switch depTag {
747 case dylibDepTag:
748 dylib, ok := rustDep.compiler.(libraryInterface)
749 if !ok || !dylib.dylib() {
750 ctx.ModuleErrorf("mod %q not an dylib library", depName)
751 return
752 }
753 directDylibDeps = append(directDylibDeps, rustDep)
754 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
755 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400756
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 rlib, ok := rustDep.compiler.(libraryInterface)
758 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400759 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760 return
761 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400762 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400764 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700765 case procMacroDepTag:
766 directProcMacroDeps = append(directProcMacroDeps, rustDep)
767 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400768 case android.SourceDepTag:
769 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
770 // OS/Arch variant is used.
771 var helper string
772 if ctx.Host() {
773 helper = "missing 'host_supported'?"
774 } else {
775 helper = "device module defined?"
776 }
777
778 if dep.Target().Os != ctx.Os() {
779 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
780 return
781 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
782 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
783 return
784 }
785 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700786 }
787
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400788 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700789 if depTag != procMacroDepTag {
790 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
791 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
792 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
793 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794 }
795
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400797 linkFile := rustDep.outputFile
798 if !linkFile.Valid() {
799 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
800 depName, ctx.ModuleName())
801 return
802 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700804 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
805 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806 }
807 }
808
Ivan Lozano89435d12020-07-31 11:01:18 -0400809 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700810 //Handle C dependencies
811 if _, ok := ccDep.(*Module); !ok {
812 if ccDep.Module().Target().Os != ctx.Os() {
813 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
814 return
815 }
816 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
817 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
818 return
819 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700820 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400821 linkObject := ccDep.OutputFile()
822 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500823
Ivan Lozano2093af22020-08-25 12:48:19 -0400824 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
826 }
827
828 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700829 switch {
830 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400832 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700833 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
834 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
835 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
836 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
837 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400838 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839 directStaticLibDeps = append(directStaticLibDeps, ccDep)
840 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700841 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400843 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700844 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
845 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
846 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
847 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
848 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 directSharedLibDeps = append(directSharedLibDeps, ccDep)
850 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
851 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800852 case cc.IsHeaderDepTag(depTag):
853 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
854 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
855 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
856 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700857 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400858 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700859 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400860 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861 }
862
863 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700864 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
865 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400866 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700867 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700868 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400869
870 if srcDep, ok := dep.(android.SourceFileProducer); ok {
871 switch depTag {
872 case android.SourceDepTag:
873 // These are usually genrules which don't have per-target variants.
874 directSrcDeps = append(directSrcDeps, srcDep)
875 }
876 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700877 })
878
879 var rlibDepFiles RustLibraries
880 for _, dep := range directRlibDeps {
881 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
882 }
883 var dylibDepFiles RustLibraries
884 for _, dep := range directDylibDeps {
885 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
886 }
887 var procMacroDepFiles RustLibraries
888 for _, dep := range directProcMacroDeps {
889 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
890 }
891
892 var staticLibDepFiles android.Paths
893 for _, dep := range directStaticLibDeps {
894 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
895 }
896
897 var sharedLibDepFiles android.Paths
898 for _, dep := range directSharedLibDeps {
899 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
900 }
901
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400902 var srcProviderDepFiles android.Paths
903 for _, dep := range directSrcProvidersDeps {
904 srcs, _ := dep.OutputFiles("")
905 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
906 }
907 for _, dep := range directSrcDeps {
908 srcs := dep.Srcs()
909 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
910 }
911
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
913 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
914 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
915 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
916 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400917 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700918
919 // Dedup exported flags from dependencies
920 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
921 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400922 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
923 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
924 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925
926 return depPaths
927}
928
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800929func (mod *Module) InstallInData() bool {
930 if mod.compiler == nil {
931 return false
932 }
933 return mod.compiler.inData()
934}
935
Ivan Lozanoffee3342019-08-27 12:03:00 -0700936func linkPathFromFilePath(filepath android.Path) string {
937 return strings.Split(filepath.String(), filepath.Base())[0]
938}
Ivan Lozanod648c432020-02-06 12:05:10 -0500939
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
941 ctx := &depsContext{
942 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700943 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944
945 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700946 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -0400947
Ivan Lozano2b081132020-09-08 12:46:52 -0400948 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400949 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400950 stdLinkage = "rlib-std"
951 }
952
953 rlibDepVariations := commonDepVariations
954 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
955 rlibDepVariations = append(rlibDepVariations,
956 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
957 }
958
Ivan Lozano52767be2019-10-18 14:49:46 -0700959 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400960 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200961 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700962 rlibDepTag, deps.Rlibs...)
963 actx.AddVariationDependencies(
964 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200965 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700966 dylibDepTag, deps.Dylibs...)
967
Ivan Lozano042504f2020-08-18 14:31:23 -0400968 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
969 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400970 if autoDep.depTag == rlibDepTag {
971 actx.AddVariationDependencies(
972 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
973 autoDep.depTag, deps.Rustlibs...)
974 } else {
975 actx.AddVariationDependencies(
976 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
977 autoDep.depTag, deps.Rustlibs...)
978 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700979 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400980 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400981 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400982 actx.AddVariationDependencies(
983 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
984 rlibDepTag, deps.Stdlibs...)
985 } else {
986 actx.AddVariationDependencies(
987 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
988 dylibDepTag, deps.Stdlibs...)
989 }
990 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700991 actx.AddVariationDependencies(append(commonDepVariations,
992 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700993 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700994 actx.AddVariationDependencies(append(commonDepVariations,
995 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -0700996 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700997
Zach Johnson3df4e632020-11-06 11:56:27 -0800998 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
999
Colin Cross565cafd2020-09-25 18:47:38 -07001000 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001001 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001002 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001003 }
1004 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001005 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001006 }
1007
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001008 if mod.sourceProvider != nil {
1009 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1010 bindgen.Properties.Custom_bindgen != "" {
1011 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1012 bindgen.Properties.Custom_bindgen)
1013 }
1014 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001015 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001016 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001017}
1018
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001019func BeginMutator(ctx android.BottomUpMutatorContext) {
1020 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1021 mod.beginMutator(ctx)
1022 }
1023}
1024
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001025func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1026 ctx := &baseModuleContext{
1027 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001028 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001029
1030 mod.begin(ctx)
1031}
1032
Ivan Lozanoffee3342019-08-27 12:03:00 -07001033func (mod *Module) Name() string {
1034 name := mod.ModuleBase.Name()
1035 if p, ok := mod.compiler.(interface {
1036 Name(string) string
1037 }); ok {
1038 name = p.Name(name)
1039 }
1040 return name
1041}
1042
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001043func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001044 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001045 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001046 }
1047}
1048
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001049var _ android.HostToolProvider = (*Module)(nil)
1050
1051func (mod *Module) HostToolPath() android.OptionalPath {
1052 if !mod.Host() {
1053 return android.OptionalPath{}
1054 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001055 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1056 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001057 }
1058 return android.OptionalPath{}
1059}
1060
Jiyong Park99644e92020-11-17 22:21:02 +09001061var _ android.ApexModule = (*Module)(nil)
1062
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001063func (mod *Module) minSdkVersion() string {
1064 return String(mod.Properties.Min_sdk_version)
1065}
1066
Jiyong Park99644e92020-11-17 22:21:02 +09001067func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001068 minSdkVersion := mod.minSdkVersion()
1069 if minSdkVersion == "apex_inherit" {
1070 return nil
1071 }
1072 if minSdkVersion == "" {
1073 return fmt.Errorf("min_sdk_version is not specificed")
1074 }
1075
1076 // Not using nativeApiLevelFromUser because the context here is not
1077 // necessarily a native context.
1078 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1079 if err != nil {
1080 return err
1081 }
1082
1083 if ver.GreaterThan(sdkVersion) {
1084 return fmt.Errorf("newer SDK(%v)", ver)
1085 }
Jiyong Park99644e92020-11-17 22:21:02 +09001086 return nil
1087}
1088
1089func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1090 depTag := ctx.OtherModuleDependencyTag(dep)
1091
1092 if ccm, ok := dep.(*cc.Module); ok {
1093 if ccm.HasStubsVariants() {
1094 if cc.IsSharedDepTag(depTag) {
1095 // dynamic dep to a stubs lib crosses APEX boundary
1096 return false
1097 }
1098 if cc.IsRuntimeDepTag(depTag) {
1099 // runtime dep to a stubs lib also crosses APEX boundary
1100 return false
1101 }
1102
1103 if cc.IsHeaderDepTag(depTag) {
1104 return false
1105 }
1106 }
1107 if mod.Static() && cc.IsSharedDepTag(depTag) {
1108 // shared_lib dependency from a static lib is considered as crossing
1109 // the APEX boundary because the dependency doesn't actually is
1110 // linked; the dependency is used only during the compilation phase.
1111 return false
1112 }
1113 }
1114
1115 if depTag == procMacroDepTag {
1116 return false
1117 }
1118
1119 return true
1120}
1121
1122// Overrides ApexModule.IsInstallabeToApex()
1123func (mod *Module) IsInstallableToApex() bool {
1124 if mod.compiler != nil {
1125 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1126 return true
1127 }
1128 if _, ok := mod.compiler.(*binaryDecorator); ok {
1129 return true
1130 }
1131 }
1132 return false
1133}
1134
Ivan Lozanoffee3342019-08-27 12:03:00 -07001135var Bool = proptools.Bool
1136var BoolDefault = proptools.BoolDefault
1137var String = proptools.String
1138var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001139
1140var _ android.OutputFileProducer = (*Module)(nil)