blob: f82dd5477de045ebf661ddfa45da559b28375393 [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 Lozano6cd99e62020-02-11 08:24:25 -050045
46 })
47 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
48 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 })
50 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020051 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040055 GlobalRustFlags []string // Flags that apply globally to rust
56 GlobalLinkFlags []string // Flags that apply globally to linker
57 RustFlags []string // Flags that apply to rust
58 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020059 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070060 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040061 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type BaseProperties struct {
66 AndroidMkRlibs []string
67 AndroidMkDylibs []string
68 AndroidMkProcMacroLibs []string
69 AndroidMkSharedLibs []string
70 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040071
Ivan Lozano6a884432020-12-02 09:15:16 -050072 ImageVariationPrefix string `blueprint:"mutated"`
73 VndkVersion string `blueprint:"mutated"`
74 SubName string `blueprint:"mutated"`
75
76 // Set by imageMutator
77 CoreVariantNeeded bool `blueprint:"mutated"`
78 ExtraVariants []string `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040079
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050080 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
81 Min_sdk_version *string
82
Ivan Lozano43845682020-07-09 21:03:28 -040083 PreventInstall bool
84 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070085}
86
87type Module struct {
88 android.ModuleBase
89 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090090 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070091
Ivan Lozano6a884432020-12-02 09:15:16 -050092 VendorProperties cc.VendorProperties
93
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 Properties BaseProperties
95
96 hod android.HostOrDeviceSupported
97 multilib android.Multilib
98
Ivan Lozano6a884432020-12-02 09:15:16 -050099 makeLinkType string
100
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400102 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200103 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500104 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400106 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700107 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400108
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200109 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900110
111 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112}
113
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500114func (mod *Module) Header() bool {
115 //TODO: If Rust libraries provide header variants, this needs to be updated.
116 return false
117}
118
119func (mod *Module) SetPreventInstall() {
120 mod.Properties.PreventInstall = true
121}
122
123// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
124func (mod *Module) InVendor() bool {
125 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
126}
127
128func (mod *Module) SetHideFromMake() {
129 mod.Properties.HideFromMake = true
130}
131
132func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500133 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
134 // nil since we need compiler to actually sanitize.
135 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500136}
137
138func (mod *Module) IsDependencyRoot() bool {
139 if mod.compiler != nil {
140 return mod.compiler.isDependencyRoot()
141 }
142 panic("IsDependencyRoot called on a non-compiler Rust module")
143}
144
145func (mod *Module) IsPrebuilt() bool {
146 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
147 return true
148 }
149 return false
150}
151
Ivan Lozano43845682020-07-09 21:03:28 -0400152func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
153 switch tag {
154 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700155 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400156 return mod.sourceProvider.Srcs(), nil
157 } else {
158 if mod.outputFile.Valid() {
159 return android.Paths{mod.outputFile.Path()}, nil
160 }
161 return android.Paths{}, nil
162 }
163 default:
164 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
165 }
166}
167
Ivan Lozano52767be2019-10-18 14:49:46 -0700168func (mod *Module) SelectedStl() string {
169 return ""
170}
171
Ivan Lozano2b262972019-11-21 12:30:50 -0800172func (mod *Module) NonCcVariants() bool {
173 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400174 if _, ok := mod.compiler.(libraryInterface); ok {
175 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800176 }
177 }
178 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
179}
180
Ivan Lozano52767be2019-10-18 14:49:46 -0700181func (mod *Module) Static() bool {
182 if mod.compiler != nil {
183 if library, ok := mod.compiler.(libraryInterface); ok {
184 return library.static()
185 }
186 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400187 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700188}
189
190func (mod *Module) Shared() bool {
191 if mod.compiler != nil {
192 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400193 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700194 }
195 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400196 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700197}
198
199func (mod *Module) Toc() android.OptionalPath {
200 if mod.compiler != nil {
201 if _, ok := mod.compiler.(libraryInterface); ok {
202 return android.OptionalPath{}
203 }
204 }
205 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
206}
207
Colin Crossc511bc52020-04-07 16:50:32 +0000208func (mod *Module) UseSdk() bool {
209 return false
210}
211
Ivan Lozano6a884432020-12-02 09:15:16 -0500212// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
213// "product" and "vendor" variant modules return true for this function.
214// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
215// "soc_specific: true" and more vendor installed modules are included here.
216// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
217// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700218func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500219 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700220}
221
222func (mod *Module) MustUseVendorVariant() bool {
223 return false
224}
225
226func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500227 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700228 return false
229}
230
Ivan Lozanof9e21722020-12-02 09:00:51 -0500231func (mod *Module) IsVndkExt() bool {
232 return false
233}
234
Colin Cross127bb8b2020-12-16 16:46:01 -0800235func (c *Module) IsVndkPrivate() bool {
236 return false
237}
238
239func (c *Module) IsLlndk() bool {
240 return false
241}
242
243func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500244 return false
245}
246
Ivan Lozano52767be2019-10-18 14:49:46 -0700247func (mod *Module) SdkVersion() string {
248 return ""
249}
250
Colin Crossc511bc52020-04-07 16:50:32 +0000251func (mod *Module) AlwaysSdk() bool {
252 return false
253}
254
Jiyong Park2286afd2020-06-16 21:58:53 +0900255func (mod *Module) IsSdkVariant() bool {
256 return false
257}
258
Colin Cross1348ce32020-10-01 13:37:16 -0700259func (mod *Module) SplitPerApiLevel() bool {
260 return false
261}
262
Ivan Lozanoffee3342019-08-27 12:03:00 -0700263type Deps struct {
264 Dylibs []string
265 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700266 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400267 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268 ProcMacros []string
269 SharedLibs []string
270 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800271 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700272
273 CrtBegin, CrtEnd string
274}
275
276type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500277 DyLibs RustLibraries
278 RLibs RustLibraries
279 SharedLibs android.Paths
280 SharedLibDeps android.Paths
281 StaticLibs android.Paths
282 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500283
284 // depFlags and depLinkFlags are rustc and linker (clang) flags.
285 depFlags []string
286 depLinkFlags []string
287
288 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
289 // Both of these are exported and propagate to dependencies.
290 linkDirs []string
291 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700292
Ivan Lozano45901ed2020-07-24 16:05:01 -0400293 // Used by bindgen modules which call clang
294 depClangFlags []string
295 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400296 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400297 depSystemIncludePaths android.Paths
298
Ivan Lozanof1c84332019-09-20 11:00:37 -0700299 CrtBegin android.OptionalPath
300 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700301
302 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500303 SrcDeps android.Paths
304 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700305}
306
307type RustLibraries []RustLibrary
308
309type RustLibrary struct {
310 Path android.Path
311 CrateName string
312}
313
314type compiler interface {
315 compilerFlags(ctx ModuleContext, flags Flags) Flags
316 compilerProps() []interface{}
317 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
318 compilerDeps(ctx DepsContext, deps Deps) Deps
319 crateName() string
320
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800321 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200322 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700323 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400324
325 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400326
327 Disabled() bool
328 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400329
Ivan Lozanodd055472020-09-28 13:22:45 -0400330 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500331 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400332}
333
Matthew Maurerbb3add12020-06-25 09:34:12 -0700334type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700335 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400336 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700337}
338
339type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400340 linkDirs []string
341 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700342}
343
Matthew Maurerbb3add12020-06-25 09:34:12 -0700344func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
345 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
346}
347
Ivan Lozano2093af22020-08-25 12:48:19 -0400348func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
349 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
350}
351
Colin Cross0de8a1e2020-09-18 14:15:30 -0700352func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
353 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700354 LinkDirs: flagExporter.linkDirs,
355 LinkObjects: flagExporter.linkObjects,
356 })
357}
358
Matthew Maurerbb3add12020-06-25 09:34:12 -0700359var _ exportedFlagsProducer = (*flagExporter)(nil)
360
361func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700362 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700363}
364
Colin Cross0de8a1e2020-09-18 14:15:30 -0700365type FlagExporterInfo struct {
366 Flags []string
367 LinkDirs []string // TODO: this should be android.Paths
368 LinkObjects []string // TODO: this should be android.Paths
369}
370
371var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
372
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400373func (mod *Module) isCoverageVariant() bool {
374 return mod.coverage.Properties.IsCoverageVariant
375}
376
377var _ cc.Coverage = (*Module)(nil)
378
379func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
380 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
381}
382
383func (mod *Module) PreventInstall() {
384 mod.Properties.PreventInstall = true
385}
386
387func (mod *Module) HideFromMake() {
388 mod.Properties.HideFromMake = true
389}
390
391func (mod *Module) MarkAsCoverageVariant(coverage bool) {
392 mod.coverage.Properties.IsCoverageVariant = coverage
393}
394
395func (mod *Module) EnableCoverageIfNeeded() {
396 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397}
398
399func defaultsFactory() android.Module {
400 return DefaultsFactory()
401}
402
403type Defaults struct {
404 android.ModuleBase
405 android.DefaultsModuleBase
406}
407
408func DefaultsFactory(props ...interface{}) android.Module {
409 module := &Defaults{}
410
411 module.AddProperties(props...)
412 module.AddProperties(
413 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500414 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400415 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700416 &BaseCompilerProperties{},
417 &BinaryCompilerProperties{},
418 &LibraryCompilerProperties{},
419 &ProcMacroCompilerProperties{},
420 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400421 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700422 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400423 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400424 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200425 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500426 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700427 )
428
429 android.InitDefaultsModule(module)
430 return module
431}
432
433func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700434 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700435}
436
Ivan Lozano183a3212019-10-18 14:18:45 -0700437func (mod *Module) CcLibrary() bool {
438 if mod.compiler != nil {
439 if _, ok := mod.compiler.(*libraryDecorator); ok {
440 return true
441 }
442 }
443 return false
444}
445
446func (mod *Module) CcLibraryInterface() bool {
447 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400448 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
449 // VariantIs{Static,Shared} is set.
450 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700451 return true
452 }
453 }
454 return false
455}
456
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800457func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700458 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700459 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800460 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700461 }
462 }
463 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
464}
465
466func (mod *Module) SetStatic() {
467 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700468 if library, ok := mod.compiler.(libraryInterface); ok {
469 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700470 return
471 }
472 }
473 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
474}
475
476func (mod *Module) SetShared() {
477 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700478 if library, ok := mod.compiler.(libraryInterface); ok {
479 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700480 return
481 }
482 }
483 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
484}
485
Ivan Lozano183a3212019-10-18 14:18:45 -0700486func (mod *Module) BuildStaticVariant() bool {
487 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700488 if library, ok := mod.compiler.(libraryInterface); ok {
489 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700490 }
491 }
492 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
493}
494
495func (mod *Module) BuildSharedVariant() bool {
496 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700497 if library, ok := mod.compiler.(libraryInterface); ok {
498 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700499 }
500 }
501 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
502}
503
Ivan Lozano183a3212019-10-18 14:18:45 -0700504func (mod *Module) Module() android.Module {
505 return mod
506}
507
Ivan Lozano183a3212019-10-18 14:18:45 -0700508func (mod *Module) OutputFile() android.OptionalPath {
509 return mod.outputFile
510}
511
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400512func (mod *Module) CoverageFiles() android.Paths {
513 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800514 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400515 }
516 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
517}
518
Jiyong Park459feca2020-12-15 11:02:21 +0900519func (mod *Module) installable(apexInfo android.ApexInfo) bool {
520 // The apex variant is not installable because it is included in the APEX and won't appear
521 // in the system partition as a standalone file.
522 if !apexInfo.IsForPlatform() {
523 return false
524 }
525
526 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
527}
528
Ivan Lozano183a3212019-10-18 14:18:45 -0700529var _ cc.LinkableInterface = (*Module)(nil)
530
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531func (mod *Module) Init() android.Module {
532 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500533 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534
535 if mod.compiler != nil {
536 mod.AddProperties(mod.compiler.compilerProps()...)
537 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400538 if mod.coverage != nil {
539 mod.AddProperties(mod.coverage.props()...)
540 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200541 if mod.clippy != nil {
542 mod.AddProperties(mod.clippy.props()...)
543 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400544 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700545 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400546 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500547 if mod.sanitize != nil {
548 mod.AddProperties(mod.sanitize.props()...)
549 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400550
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900552 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553
554 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555 return mod
556}
557
558func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
559 return &Module{
560 hod: hod,
561 multilib: multilib,
562 }
563}
564func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
565 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400566 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200567 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500568 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569 return module
570}
571
572type ModuleContext interface {
573 android.ModuleContext
574 ModuleContextIntf
575}
576
577type BaseModuleContext interface {
578 android.BaseModuleContext
579 ModuleContextIntf
580}
581
582type DepsContext interface {
583 android.BottomUpMutatorContext
584 ModuleContextIntf
585}
586
587type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200588 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590}
591
592type depsContext struct {
593 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594}
595
596type moduleContext struct {
597 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598}
599
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200600type baseModuleContext struct {
601 android.BaseModuleContext
602}
603
604func (ctx *moduleContext) RustModule() *Module {
605 return ctx.Module().(*Module)
606}
607
608func (ctx *moduleContext) toolchain() config.Toolchain {
609 return ctx.RustModule().toolchain(ctx)
610}
611
612func (ctx *depsContext) RustModule() *Module {
613 return ctx.Module().(*Module)
614}
615
616func (ctx *depsContext) toolchain() config.Toolchain {
617 return ctx.RustModule().toolchain(ctx)
618}
619
620func (ctx *baseModuleContext) RustModule() *Module {
621 return ctx.Module().(*Module)
622}
623
624func (ctx *baseModuleContext) toolchain() config.Toolchain {
625 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400626}
627
628func (mod *Module) nativeCoverage() bool {
629 return mod.compiler != nil && mod.compiler.nativeCoverage()
630}
631
Ivan Lozanoffee3342019-08-27 12:03:00 -0700632func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
633 if mod.cachedToolchain == nil {
634 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
635 }
636 return mod.cachedToolchain
637}
638
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200639func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
640 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
641}
642
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
644}
645
646func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
647 ctx := &moduleContext{
648 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700650
Jiyong Park99644e92020-11-17 22:21:02 +0900651 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
652 if !apexInfo.IsForPlatform() {
653 mod.hideApexVariantFromMake = true
654 }
655
Ivan Lozanoffee3342019-08-27 12:03:00 -0700656 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500657 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
658
659 // Differentiate static libraries that are vendor available
660 if mod.UseVndk() {
661 mod.Properties.SubName += ".vendor"
662 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663
664 if !toolchain.Supported() {
665 // This toolchain's unsupported, there's nothing to do for this mod.
666 return
667 }
668
669 deps := mod.depsToPaths(ctx)
670 flags := Flags{
671 Toolchain: toolchain,
672 }
673
674 if mod.compiler != nil {
675 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400676 }
677 if mod.coverage != nil {
678 flags, deps = mod.coverage.flags(ctx, flags, deps)
679 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200680 if mod.clippy != nil {
681 flags, deps = mod.clippy.flags(ctx, flags, deps)
682 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500683 if mod.sanitize != nil {
684 flags, deps = mod.sanitize.flags(ctx, flags, deps)
685 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400686
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200687 // SourceProvider needs to call GenerateSource() before compiler calls
688 // compile() so it can provide the source. A SourceProvider has
689 // multiple variants (e.g. source, rlib, dylib). Only the "source"
690 // variant is responsible for effectively generating the source. The
691 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400692 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200693 if mod.compiler.(libraryInterface).source() {
694 mod.sourceProvider.GenerateSource(ctx, deps)
695 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
696 } else {
697 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
698 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700699 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200700 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400701 }
702
703 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400705
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900707
708 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
709 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200710 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400711 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700712 }
713}
714
715func (mod *Module) deps(ctx DepsContext) Deps {
716 deps := Deps{}
717
718 if mod.compiler != nil {
719 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400720 }
721 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700722 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 }
724
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400725 if mod.coverage != nil {
726 deps = mod.coverage.deps(ctx, deps)
727 }
728
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500729 if mod.sanitize != nil {
730 deps = mod.sanitize.deps(ctx, deps)
731 }
732
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
734 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700735 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
737 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
738 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
739
740 return deps
741
742}
743
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744type dependencyTag struct {
745 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800746 name string
747 library bool
748 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749}
750
Jiyong Park65b62242020-11-25 12:44:59 +0900751// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
752// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
753func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800754 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900755}
756
757var _ android.InstallNeededDependencyTag = dependencyTag{}
758
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400760 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
761 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
762 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800763 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400764 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200765 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700766)
767
Jiyong Park99644e92020-11-17 22:21:02 +0900768func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
769 tag, ok := depTag.(dependencyTag)
770 return ok && tag == dylibDepTag
771}
772
Matthew Maurer0f003b12020-06-29 14:34:06 -0700773type autoDep struct {
774 variation string
775 depTag dependencyTag
776}
777
778var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200779 rlibVariation = "rlib"
780 dylibVariation = "dylib"
781 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
782 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700783)
784
785type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400786 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700787}
788
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400789func (mod *Module) begin(ctx BaseModuleContext) {
790 if mod.coverage != nil {
791 mod.coverage.begin(ctx)
792 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500793 if mod.sanitize != nil {
794 mod.sanitize.begin(ctx)
795 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400796}
797
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
799 var depPaths PathDeps
800
801 directRlibDeps := []*Module{}
802 directDylibDeps := []*Module{}
803 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700804 directSharedLibDeps := [](cc.LinkableInterface){}
805 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400806 directSrcProvidersDeps := []*Module{}
807 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808
809 ctx.VisitDirectDeps(func(dep android.Module) {
810 depName := ctx.OtherModuleName(dep)
811 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400812 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700814
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 switch depTag {
816 case dylibDepTag:
817 dylib, ok := rustDep.compiler.(libraryInterface)
818 if !ok || !dylib.dylib() {
819 ctx.ModuleErrorf("mod %q not an dylib library", depName)
820 return
821 }
822 directDylibDeps = append(directDylibDeps, rustDep)
823 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
824 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400825
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 rlib, ok := rustDep.compiler.(libraryInterface)
827 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400828 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 return
830 }
831 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400832 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 case procMacroDepTag:
834 directProcMacroDeps = append(directProcMacroDeps, rustDep)
835 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400836 case android.SourceDepTag:
837 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
838 // OS/Arch variant is used.
839 var helper string
840 if ctx.Host() {
841 helper = "missing 'host_supported'?"
842 } else {
843 helper = "device module defined?"
844 }
845
846 if dep.Target().Os != ctx.Os() {
847 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
848 return
849 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
850 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
851 return
852 }
853 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 }
855
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400856 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700857 if depTag != procMacroDepTag {
858 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
859 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
860 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
861 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 }
863
Ivan Lozanoffee3342019-08-27 12:03:00 -0700864 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400865 linkFile := rustDep.outputFile
866 if !linkFile.Valid() {
867 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
868 depName, ctx.ModuleName())
869 return
870 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700871 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700872 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
873 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 }
875 }
876
Ivan Lozano89435d12020-07-31 11:01:18 -0400877 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700878 //Handle C dependencies
879 if _, ok := ccDep.(*Module); !ok {
880 if ccDep.Module().Target().Os != ctx.Os() {
881 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
882 return
883 }
884 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
885 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
886 return
887 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700888 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400889 linkObject := ccDep.OutputFile()
890 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500891
Ivan Lozano2093af22020-08-25 12:48:19 -0400892 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
894 }
895
896 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700897 switch {
898 case cc.IsStaticDepTag(depTag):
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500899 // Link cc static libraries using "-lstatic" so rustc can reason about how to handle these
900 // (for example, bundling them into rlibs).
901 //
902 // rustc does not support linking libraries with the "-l" flag unless they are prefixed by "lib".
903 // If we need to link a library that isn't prefixed by "lib", we'll just link to it directly through
904 // linkObjects; such a library may need to be redeclared by static dependents.
905 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
906 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
907 }
908
909 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
910 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400911 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500912 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
913
Colin Cross0de8a1e2020-09-18 14:15:30 -0700914 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
915 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
916 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
917 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
918 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919 directStaticLibDeps = append(directStaticLibDeps, ccDep)
920 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700921 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700922 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400923 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700924 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
925 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
926 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
927 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
928 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700929 directSharedLibDeps = append(directSharedLibDeps, ccDep)
930 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
931 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800932 case cc.IsHeaderDepTag(depTag):
933 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
934 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
935 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
936 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700937 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400938 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700939 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400940 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700941 }
942
943 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700944 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
945 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400946 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700947 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400949
950 if srcDep, ok := dep.(android.SourceFileProducer); ok {
951 switch depTag {
952 case android.SourceDepTag:
953 // These are usually genrules which don't have per-target variants.
954 directSrcDeps = append(directSrcDeps, srcDep)
955 }
956 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957 })
958
959 var rlibDepFiles RustLibraries
960 for _, dep := range directRlibDeps {
961 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
962 }
963 var dylibDepFiles RustLibraries
964 for _, dep := range directDylibDeps {
965 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
966 }
967 var procMacroDepFiles RustLibraries
968 for _, dep := range directProcMacroDeps {
969 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
970 }
971
972 var staticLibDepFiles android.Paths
973 for _, dep := range directStaticLibDeps {
974 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
975 }
976
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500977 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700978 var sharedLibDepFiles android.Paths
979 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500980 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
981 if dep.Toc().Valid() {
982 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
983 } else {
984 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
985 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986 }
987
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400988 var srcProviderDepFiles android.Paths
989 for _, dep := range directSrcProvidersDeps {
990 srcs, _ := dep.OutputFiles("")
991 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
992 }
993 for _, dep := range directSrcDeps {
994 srcs := dep.Srcs()
995 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
996 }
997
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
999 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1000 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001001 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1003 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001004 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001005
1006 // Dedup exported flags from dependencies
1007 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001008 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001009 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001010 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1011 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1012 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001013
1014 return depPaths
1015}
1016
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001017func (mod *Module) InstallInData() bool {
1018 if mod.compiler == nil {
1019 return false
1020 }
1021 return mod.compiler.inData()
1022}
1023
Ivan Lozanoffee3342019-08-27 12:03:00 -07001024func linkPathFromFilePath(filepath android.Path) string {
1025 return strings.Split(filepath.String(), filepath.Base())[0]
1026}
Ivan Lozanod648c432020-02-06 12:05:10 -05001027
Ivan Lozanoffee3342019-08-27 12:03:00 -07001028func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1029 ctx := &depsContext{
1030 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032
1033 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001034 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001035
Ivan Lozano2b081132020-09-08 12:46:52 -04001036 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001037 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001038 stdLinkage = "rlib-std"
1039 }
1040
1041 rlibDepVariations := commonDepVariations
1042 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1043 rlibDepVariations = append(rlibDepVariations,
1044 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1045 }
1046
Ivan Lozano52767be2019-10-18 14:49:46 -07001047 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001048 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001049 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001050 rlibDepTag, deps.Rlibs...)
1051 actx.AddVariationDependencies(
1052 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001053 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001054 dylibDepTag, deps.Dylibs...)
1055
Ivan Lozano042504f2020-08-18 14:31:23 -04001056 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1057 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001058 if autoDep.depTag == rlibDepTag {
1059 actx.AddVariationDependencies(
1060 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1061 autoDep.depTag, deps.Rustlibs...)
1062 } else {
1063 actx.AddVariationDependencies(
1064 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1065 autoDep.depTag, deps.Rustlibs...)
1066 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001067 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001068 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001069 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001070 actx.AddVariationDependencies(
1071 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1072 rlibDepTag, deps.Stdlibs...)
1073 } else {
1074 actx.AddVariationDependencies(
1075 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1076 dylibDepTag, deps.Stdlibs...)
1077 }
1078 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001079 actx.AddVariationDependencies(append(commonDepVariations,
1080 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001081 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001082 actx.AddVariationDependencies(append(commonDepVariations,
1083 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001084 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001085
Zach Johnson3df4e632020-11-06 11:56:27 -08001086 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1087
Colin Cross565cafd2020-09-25 18:47:38 -07001088 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001089 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001090 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001091 }
1092 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001093 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001094 }
1095
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001096 if mod.sourceProvider != nil {
1097 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1098 bindgen.Properties.Custom_bindgen != "" {
1099 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1100 bindgen.Properties.Custom_bindgen)
1101 }
1102 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001103 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001104 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001105}
1106
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001107func BeginMutator(ctx android.BottomUpMutatorContext) {
1108 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1109 mod.beginMutator(ctx)
1110 }
1111}
1112
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001113func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1114 ctx := &baseModuleContext{
1115 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001116 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001117
1118 mod.begin(ctx)
1119}
1120
Ivan Lozanoffee3342019-08-27 12:03:00 -07001121func (mod *Module) Name() string {
1122 name := mod.ModuleBase.Name()
1123 if p, ok := mod.compiler.(interface {
1124 Name(string) string
1125 }); ok {
1126 name = p.Name(name)
1127 }
1128 return name
1129}
1130
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001131func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001132 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001133 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001134 }
1135}
1136
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001137var _ android.HostToolProvider = (*Module)(nil)
1138
1139func (mod *Module) HostToolPath() android.OptionalPath {
1140 if !mod.Host() {
1141 return android.OptionalPath{}
1142 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001143 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1144 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001145 }
1146 return android.OptionalPath{}
1147}
1148
Jiyong Park99644e92020-11-17 22:21:02 +09001149var _ android.ApexModule = (*Module)(nil)
1150
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001151func (mod *Module) minSdkVersion() string {
1152 return String(mod.Properties.Min_sdk_version)
1153}
1154
Jiyong Park45bf82e2020-12-15 22:29:02 +09001155var _ android.ApexModule = (*Module)(nil)
1156
1157// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001158func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001159 minSdkVersion := mod.minSdkVersion()
1160 if minSdkVersion == "apex_inherit" {
1161 return nil
1162 }
1163 if minSdkVersion == "" {
1164 return fmt.Errorf("min_sdk_version is not specificed")
1165 }
1166
1167 // Not using nativeApiLevelFromUser because the context here is not
1168 // necessarily a native context.
1169 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1170 if err != nil {
1171 return err
1172 }
1173
1174 if ver.GreaterThan(sdkVersion) {
1175 return fmt.Errorf("newer SDK(%v)", ver)
1176 }
Jiyong Park99644e92020-11-17 22:21:02 +09001177 return nil
1178}
1179
Jiyong Park45bf82e2020-12-15 22:29:02 +09001180// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001181func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1182 depTag := ctx.OtherModuleDependencyTag(dep)
1183
1184 if ccm, ok := dep.(*cc.Module); ok {
1185 if ccm.HasStubsVariants() {
1186 if cc.IsSharedDepTag(depTag) {
1187 // dynamic dep to a stubs lib crosses APEX boundary
1188 return false
1189 }
1190 if cc.IsRuntimeDepTag(depTag) {
1191 // runtime dep to a stubs lib also crosses APEX boundary
1192 return false
1193 }
1194
1195 if cc.IsHeaderDepTag(depTag) {
1196 return false
1197 }
1198 }
1199 if mod.Static() && cc.IsSharedDepTag(depTag) {
1200 // shared_lib dependency from a static lib is considered as crossing
1201 // the APEX boundary because the dependency doesn't actually is
1202 // linked; the dependency is used only during the compilation phase.
1203 return false
1204 }
1205 }
1206
1207 if depTag == procMacroDepTag {
1208 return false
1209 }
1210
1211 return true
1212}
1213
1214// Overrides ApexModule.IsInstallabeToApex()
1215func (mod *Module) IsInstallableToApex() bool {
1216 if mod.compiler != nil {
1217 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1218 return true
1219 }
1220 if _, ok := mod.compiler.(*binaryDecorator); ok {
1221 return true
1222 }
1223 }
1224 return false
1225}
1226
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001227// If a library file has a "lib" prefix, extract the library name without the prefix.
1228func libNameFromFilePath(filepath android.Path) (string, bool) {
1229 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1230 if strings.HasPrefix(libName, "lib") {
1231 libName = libName[3:]
1232 return libName, true
1233 }
1234 return "", false
1235}
1236
Ivan Lozanoffee3342019-08-27 12:03:00 -07001237var Bool = proptools.Bool
1238var BoolDefault = proptools.BoolDefault
1239var String = proptools.String
1240var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001241
1242var _ android.OutputFileProducer = (*Module)(nil)