blob: 17ff6252f6fba2180966f27435e081e91da07a73 [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
Ivan Lozanoe6d30982021-02-05 10:57:43 -050077 CoreVariantNeeded bool `blueprint:"mutated"`
78 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
79 ExtraVariants []string `blueprint:"mutated"`
80
81 // Make this module available when building for vendor ramdisk.
82 // On device without a dedicated recovery partition, the module is only
83 // available after switching root into
84 // /first_stage_ramdisk. To expose the module before switching root, install
85 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
86 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040087
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050088 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
89 Min_sdk_version *string
90
Ivan Lozano43845682020-07-09 21:03:28 -040091 PreventInstall bool
92 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070093}
94
95type Module struct {
96 android.ModuleBase
97 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090098 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070099
Ivan Lozano6a884432020-12-02 09:15:16 -0500100 VendorProperties cc.VendorProperties
101
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102 Properties BaseProperties
103
104 hod android.HostOrDeviceSupported
105 multilib android.Multilib
106
Ivan Lozano6a884432020-12-02 09:15:16 -0500107 makeLinkType string
108
Ivan Lozanoffee3342019-08-27 12:03:00 -0700109 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400110 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200111 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500112 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700113 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400114 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700115 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400116
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200117 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900118
119 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700120}
121
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500122func (mod *Module) Header() bool {
123 //TODO: If Rust libraries provide header variants, this needs to be updated.
124 return false
125}
126
127func (mod *Module) SetPreventInstall() {
128 mod.Properties.PreventInstall = true
129}
130
131// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
132func (mod *Module) InVendor() bool {
133 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
134}
135
136func (mod *Module) SetHideFromMake() {
137 mod.Properties.HideFromMake = true
138}
139
140func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500141 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
142 // nil since we need compiler to actually sanitize.
143 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500144}
145
146func (mod *Module) IsDependencyRoot() bool {
147 if mod.compiler != nil {
148 return mod.compiler.isDependencyRoot()
149 }
150 panic("IsDependencyRoot called on a non-compiler Rust module")
151}
152
153func (mod *Module) IsPrebuilt() bool {
154 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
155 return true
156 }
157 return false
158}
159
Ivan Lozano43845682020-07-09 21:03:28 -0400160func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
161 switch tag {
162 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700163 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400164 return mod.sourceProvider.Srcs(), nil
165 } else {
166 if mod.outputFile.Valid() {
167 return android.Paths{mod.outputFile.Path()}, nil
168 }
169 return android.Paths{}, nil
170 }
171 default:
172 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
173 }
174}
175
Ivan Lozano52767be2019-10-18 14:49:46 -0700176func (mod *Module) SelectedStl() string {
177 return ""
178}
179
Ivan Lozano2b262972019-11-21 12:30:50 -0800180func (mod *Module) NonCcVariants() bool {
181 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400182 if _, ok := mod.compiler.(libraryInterface); ok {
183 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800184 }
185 }
186 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
187}
188
Ivan Lozano52767be2019-10-18 14:49:46 -0700189func (mod *Module) Static() bool {
190 if mod.compiler != nil {
191 if library, ok := mod.compiler.(libraryInterface); ok {
192 return library.static()
193 }
194 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400195 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700196}
197
198func (mod *Module) Shared() bool {
199 if mod.compiler != nil {
200 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400201 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700202 }
203 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400204 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700205}
206
207func (mod *Module) Toc() android.OptionalPath {
208 if mod.compiler != nil {
209 if _, ok := mod.compiler.(libraryInterface); ok {
210 return android.OptionalPath{}
211 }
212 }
213 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
214}
215
Colin Crossc511bc52020-04-07 16:50:32 +0000216func (mod *Module) UseSdk() bool {
217 return false
218}
219
Ivan Lozano6a884432020-12-02 09:15:16 -0500220// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
221// "product" and "vendor" variant modules return true for this function.
222// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
223// "soc_specific: true" and more vendor installed modules are included here.
224// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
225// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700226func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500227 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700228}
229
230func (mod *Module) MustUseVendorVariant() bool {
231 return false
232}
233
234func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500235 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700236 return false
237}
238
Ivan Lozanof9e21722020-12-02 09:00:51 -0500239func (mod *Module) IsVndkExt() bool {
240 return false
241}
242
Colin Cross127bb8b2020-12-16 16:46:01 -0800243func (c *Module) IsVndkPrivate() bool {
244 return false
245}
246
247func (c *Module) IsLlndk() bool {
248 return false
249}
250
251func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500252 return false
253}
254
Ivan Lozano52767be2019-10-18 14:49:46 -0700255func (mod *Module) SdkVersion() string {
256 return ""
257}
258
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900259func (mod *Module) MinSdkVersion() string {
260 return ""
261}
262
Colin Crossc511bc52020-04-07 16:50:32 +0000263func (mod *Module) AlwaysSdk() bool {
264 return false
265}
266
Jiyong Park2286afd2020-06-16 21:58:53 +0900267func (mod *Module) IsSdkVariant() bool {
268 return false
269}
270
Colin Cross1348ce32020-10-01 13:37:16 -0700271func (mod *Module) SplitPerApiLevel() bool {
272 return false
273}
274
Ivan Lozanoffee3342019-08-27 12:03:00 -0700275type Deps struct {
276 Dylibs []string
277 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700278 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400279 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700280 ProcMacros []string
281 SharedLibs []string
282 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800283 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700284
285 CrtBegin, CrtEnd string
286}
287
288type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500289 DyLibs RustLibraries
290 RLibs RustLibraries
291 SharedLibs android.Paths
292 SharedLibDeps android.Paths
293 StaticLibs android.Paths
294 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500295
296 // depFlags and depLinkFlags are rustc and linker (clang) flags.
297 depFlags []string
298 depLinkFlags []string
299
300 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
301 // Both of these are exported and propagate to dependencies.
302 linkDirs []string
303 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700304
Ivan Lozano45901ed2020-07-24 16:05:01 -0400305 // Used by bindgen modules which call clang
306 depClangFlags []string
307 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400308 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400309 depSystemIncludePaths android.Paths
310
Ivan Lozanof1c84332019-09-20 11:00:37 -0700311 CrtBegin android.OptionalPath
312 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700313
314 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500315 SrcDeps android.Paths
316 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700317}
318
319type RustLibraries []RustLibrary
320
321type RustLibrary struct {
322 Path android.Path
323 CrateName string
324}
325
326type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100327 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700328 compilerFlags(ctx ModuleContext, flags Flags) Flags
329 compilerProps() []interface{}
330 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
331 compilerDeps(ctx DepsContext, deps Deps) Deps
332 crateName() string
333
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100334 // Output directory in which source-generated code from dependencies is
335 // copied. This is equivalent to Cargo's OUT_DIR variable.
336 CargoOutDir() android.OptionalPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800337 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200338 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700339 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400340
341 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400342
343 Disabled() bool
344 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400345
Ivan Lozanodd055472020-09-28 13:22:45 -0400346 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500347 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400348}
349
Matthew Maurerbb3add12020-06-25 09:34:12 -0700350type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700351 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400352 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700353}
354
355type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400356 linkDirs []string
357 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700358}
359
Matthew Maurerbb3add12020-06-25 09:34:12 -0700360func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
361 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
362}
363
Ivan Lozano2093af22020-08-25 12:48:19 -0400364func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
365 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
366}
367
Colin Cross0de8a1e2020-09-18 14:15:30 -0700368func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
369 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700370 LinkDirs: flagExporter.linkDirs,
371 LinkObjects: flagExporter.linkObjects,
372 })
373}
374
Matthew Maurerbb3add12020-06-25 09:34:12 -0700375var _ exportedFlagsProducer = (*flagExporter)(nil)
376
377func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700378 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700379}
380
Colin Cross0de8a1e2020-09-18 14:15:30 -0700381type FlagExporterInfo struct {
382 Flags []string
383 LinkDirs []string // TODO: this should be android.Paths
384 LinkObjects []string // TODO: this should be android.Paths
385}
386
387var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
388
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400389func (mod *Module) isCoverageVariant() bool {
390 return mod.coverage.Properties.IsCoverageVariant
391}
392
393var _ cc.Coverage = (*Module)(nil)
394
395func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
396 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
397}
398
399func (mod *Module) PreventInstall() {
400 mod.Properties.PreventInstall = true
401}
402
403func (mod *Module) HideFromMake() {
404 mod.Properties.HideFromMake = true
405}
406
407func (mod *Module) MarkAsCoverageVariant(coverage bool) {
408 mod.coverage.Properties.IsCoverageVariant = coverage
409}
410
411func (mod *Module) EnableCoverageIfNeeded() {
412 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700413}
414
415func defaultsFactory() android.Module {
416 return DefaultsFactory()
417}
418
419type Defaults struct {
420 android.ModuleBase
421 android.DefaultsModuleBase
422}
423
424func DefaultsFactory(props ...interface{}) android.Module {
425 module := &Defaults{}
426
427 module.AddProperties(props...)
428 module.AddProperties(
429 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500430 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400431 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432 &BaseCompilerProperties{},
433 &BinaryCompilerProperties{},
434 &LibraryCompilerProperties{},
435 &ProcMacroCompilerProperties{},
436 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400437 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700438 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400439 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400440 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200441 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500442 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443 )
444
445 android.InitDefaultsModule(module)
446 return module
447}
448
449func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700450 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700451}
452
Ivan Lozano183a3212019-10-18 14:18:45 -0700453func (mod *Module) CcLibrary() bool {
454 if mod.compiler != nil {
455 if _, ok := mod.compiler.(*libraryDecorator); ok {
456 return true
457 }
458 }
459 return false
460}
461
462func (mod *Module) CcLibraryInterface() bool {
463 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400464 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
465 // VariantIs{Static,Shared} is set.
466 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700467 return true
468 }
469 }
470 return false
471}
472
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800473func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700474 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700475 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800476 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700477 }
478 }
479 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
480}
481
482func (mod *Module) SetStatic() {
483 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700484 if library, ok := mod.compiler.(libraryInterface); ok {
485 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700486 return
487 }
488 }
489 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
490}
491
492func (mod *Module) SetShared() {
493 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700494 if library, ok := mod.compiler.(libraryInterface); ok {
495 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700496 return
497 }
498 }
499 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
500}
501
Ivan Lozano183a3212019-10-18 14:18:45 -0700502func (mod *Module) BuildStaticVariant() bool {
503 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700504 if library, ok := mod.compiler.(libraryInterface); ok {
505 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700506 }
507 }
508 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
509}
510
511func (mod *Module) BuildSharedVariant() bool {
512 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700513 if library, ok := mod.compiler.(libraryInterface); ok {
514 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700515 }
516 }
517 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
518}
519
Ivan Lozano183a3212019-10-18 14:18:45 -0700520func (mod *Module) Module() android.Module {
521 return mod
522}
523
Ivan Lozano183a3212019-10-18 14:18:45 -0700524func (mod *Module) OutputFile() android.OptionalPath {
525 return mod.outputFile
526}
527
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400528func (mod *Module) CoverageFiles() android.Paths {
529 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800530 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400531 }
532 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
533}
534
Jiyong Park459feca2020-12-15 11:02:21 +0900535func (mod *Module) installable(apexInfo android.ApexInfo) bool {
536 // The apex variant is not installable because it is included in the APEX and won't appear
537 // in the system partition as a standalone file.
538 if !apexInfo.IsForPlatform() {
539 return false
540 }
541
542 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
543}
544
Ivan Lozano183a3212019-10-18 14:18:45 -0700545var _ cc.LinkableInterface = (*Module)(nil)
546
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547func (mod *Module) Init() android.Module {
548 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500549 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700550
551 if mod.compiler != nil {
552 mod.AddProperties(mod.compiler.compilerProps()...)
553 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400554 if mod.coverage != nil {
555 mod.AddProperties(mod.coverage.props()...)
556 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200557 if mod.clippy != nil {
558 mod.AddProperties(mod.clippy.props()...)
559 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400560 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700561 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400562 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500563 if mod.sanitize != nil {
564 mod.AddProperties(mod.sanitize.props()...)
565 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400566
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900568 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569
570 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571 return mod
572}
573
574func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
575 return &Module{
576 hod: hod,
577 multilib: multilib,
578 }
579}
580func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
581 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400582 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200583 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500584 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700585 return module
586}
587
588type ModuleContext interface {
589 android.ModuleContext
590 ModuleContextIntf
591}
592
593type BaseModuleContext interface {
594 android.BaseModuleContext
595 ModuleContextIntf
596}
597
598type DepsContext interface {
599 android.BottomUpMutatorContext
600 ModuleContextIntf
601}
602
603type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200604 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606}
607
608type depsContext struct {
609 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610}
611
612type moduleContext struct {
613 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700614}
615
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200616type baseModuleContext struct {
617 android.BaseModuleContext
618}
619
620func (ctx *moduleContext) RustModule() *Module {
621 return ctx.Module().(*Module)
622}
623
624func (ctx *moduleContext) toolchain() config.Toolchain {
625 return ctx.RustModule().toolchain(ctx)
626}
627
628func (ctx *depsContext) RustModule() *Module {
629 return ctx.Module().(*Module)
630}
631
632func (ctx *depsContext) toolchain() config.Toolchain {
633 return ctx.RustModule().toolchain(ctx)
634}
635
636func (ctx *baseModuleContext) RustModule() *Module {
637 return ctx.Module().(*Module)
638}
639
640func (ctx *baseModuleContext) toolchain() config.Toolchain {
641 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400642}
643
644func (mod *Module) nativeCoverage() bool {
645 return mod.compiler != nil && mod.compiler.nativeCoverage()
646}
647
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
649 if mod.cachedToolchain == nil {
650 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
651 }
652 return mod.cachedToolchain
653}
654
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200655func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
656 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
657}
658
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
660}
661
662func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
663 ctx := &moduleContext{
664 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700666
Jiyong Park99644e92020-11-17 22:21:02 +0900667 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
668 if !apexInfo.IsForPlatform() {
669 mod.hideApexVariantFromMake = true
670 }
671
Ivan Lozanoffee3342019-08-27 12:03:00 -0700672 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500673 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
674
675 // Differentiate static libraries that are vendor available
676 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500677 mod.Properties.SubName += cc.VendorSuffix
678 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
679 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500680 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700681
682 if !toolchain.Supported() {
683 // This toolchain's unsupported, there's nothing to do for this mod.
684 return
685 }
686
687 deps := mod.depsToPaths(ctx)
688 flags := Flags{
689 Toolchain: toolchain,
690 }
691
692 if mod.compiler != nil {
693 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400694 }
695 if mod.coverage != nil {
696 flags, deps = mod.coverage.flags(ctx, flags, deps)
697 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200698 if mod.clippy != nil {
699 flags, deps = mod.clippy.flags(ctx, flags, deps)
700 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500701 if mod.sanitize != nil {
702 flags, deps = mod.sanitize.flags(ctx, flags, deps)
703 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400704
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200705 // SourceProvider needs to call GenerateSource() before compiler calls
706 // compile() so it can provide the source. A SourceProvider has
707 // multiple variants (e.g. source, rlib, dylib). Only the "source"
708 // variant is responsible for effectively generating the source. The
709 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400710 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200711 if mod.compiler.(libraryInterface).source() {
712 mod.sourceProvider.GenerateSource(ctx, deps)
713 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
714 } else {
715 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
716 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700717 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200718 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400719 }
720
721 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100722 mod.compiler.initialize(ctx)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400724
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900726
727 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
728 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200729 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400730 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700731 }
732}
733
734func (mod *Module) deps(ctx DepsContext) Deps {
735 deps := Deps{}
736
737 if mod.compiler != nil {
738 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400739 }
740 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700741 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742 }
743
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400744 if mod.coverage != nil {
745 deps = mod.coverage.deps(ctx, deps)
746 }
747
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500748 if mod.sanitize != nil {
749 deps = mod.sanitize.deps(ctx, deps)
750 }
751
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
753 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700754 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
756 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
757 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
758
759 return deps
760
761}
762
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763type dependencyTag struct {
764 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800765 name string
766 library bool
767 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768}
769
Jiyong Park65b62242020-11-25 12:44:59 +0900770// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
771// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
772func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800773 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900774}
775
776var _ android.InstallNeededDependencyTag = dependencyTag{}
777
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400779 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
780 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
781 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800782 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400783 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200784 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785)
786
Jiyong Park99644e92020-11-17 22:21:02 +0900787func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
788 tag, ok := depTag.(dependencyTag)
789 return ok && tag == dylibDepTag
790}
791
Matthew Maurer0f003b12020-06-29 14:34:06 -0700792type autoDep struct {
793 variation string
794 depTag dependencyTag
795}
796
797var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200798 rlibVariation = "rlib"
799 dylibVariation = "dylib"
800 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
801 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700802)
803
804type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500805 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700806}
807
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400808func (mod *Module) begin(ctx BaseModuleContext) {
809 if mod.coverage != nil {
810 mod.coverage.begin(ctx)
811 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500812 if mod.sanitize != nil {
813 mod.sanitize.begin(ctx)
814 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400815}
816
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
818 var depPaths PathDeps
819
820 directRlibDeps := []*Module{}
821 directDylibDeps := []*Module{}
822 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700823 directSharedLibDeps := [](cc.LinkableInterface){}
824 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400825 directSrcProvidersDeps := []*Module{}
826 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827
828 ctx.VisitDirectDeps(func(dep android.Module) {
829 depName := ctx.OtherModuleName(dep)
830 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400831 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700833
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834 switch depTag {
835 case dylibDepTag:
836 dylib, ok := rustDep.compiler.(libraryInterface)
837 if !ok || !dylib.dylib() {
838 ctx.ModuleErrorf("mod %q not an dylib library", depName)
839 return
840 }
841 directDylibDeps = append(directDylibDeps, rustDep)
842 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
843 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400844
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845 rlib, ok := rustDep.compiler.(libraryInterface)
846 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400847 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700848 return
849 }
850 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400851 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852 case procMacroDepTag:
853 directProcMacroDeps = append(directProcMacroDeps, rustDep)
854 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400855 case android.SourceDepTag:
856 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
857 // OS/Arch variant is used.
858 var helper string
859 if ctx.Host() {
860 helper = "missing 'host_supported'?"
861 } else {
862 helper = "device module defined?"
863 }
864
865 if dep.Target().Os != ctx.Os() {
866 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
867 return
868 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
869 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
870 return
871 }
872 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 }
874
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400875 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700876 if depTag != procMacroDepTag {
877 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
878 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
879 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
880 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 }
882
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400884 linkFile := rustDep.outputFile
885 if !linkFile.Valid() {
886 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
887 depName, ctx.ModuleName())
888 return
889 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700891 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
892 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893 }
894 }
895
Ivan Lozano89435d12020-07-31 11:01:18 -0400896 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700897 //Handle C dependencies
898 if _, ok := ccDep.(*Module); !ok {
899 if ccDep.Module().Target().Os != ctx.Os() {
900 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
901 return
902 }
903 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
904 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
905 return
906 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700907 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400908 linkObject := ccDep.OutputFile()
909 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500910
Ivan Lozano2093af22020-08-25 12:48:19 -0400911 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
913 }
914
915 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700916 switch {
917 case cc.IsStaticDepTag(depTag):
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500918 // Only pass -lstatic for rlibs as it results in dylib bloat.
919 if lib, ok := ctx.Module().(*Module).compiler.(libraryInterface); ok && lib.rlib() {
920 // Link cc static libraries using "-lstatic" so rustc can reason about how to handle these
921 // (for example, bundling them into rlibs).
922 //
923 // rustc does not support linking libraries with the "-l" flag unless they are prefixed by "lib".
924 // If we need to link a library that isn't prefixed by "lib", we'll just link to it directly through
925 // linkObjects; such a library may need to be redeclared by static dependents.
926 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
927 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
928 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500929 }
930
931 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
932 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400933 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500934 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
935
Colin Cross0de8a1e2020-09-18 14:15:30 -0700936 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
937 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
938 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
939 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
940 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700941 directStaticLibDeps = append(directStaticLibDeps, ccDep)
942 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700943 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400945 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700946 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
947 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
948 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
949 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
950 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951 directSharedLibDeps = append(directSharedLibDeps, ccDep)
952 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
953 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800954 case cc.IsHeaderDepTag(depTag):
955 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
956 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
957 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
958 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700959 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400960 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700961 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400962 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963 }
964
965 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700966 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
967 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400968 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700969 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700970 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400971
972 if srcDep, ok := dep.(android.SourceFileProducer); ok {
973 switch depTag {
974 case android.SourceDepTag:
975 // These are usually genrules which don't have per-target variants.
976 directSrcDeps = append(directSrcDeps, srcDep)
977 }
978 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979 })
980
981 var rlibDepFiles RustLibraries
982 for _, dep := range directRlibDeps {
983 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
984 }
985 var dylibDepFiles RustLibraries
986 for _, dep := range directDylibDeps {
987 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
988 }
989 var procMacroDepFiles RustLibraries
990 for _, dep := range directProcMacroDeps {
991 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
992 }
993
994 var staticLibDepFiles android.Paths
995 for _, dep := range directStaticLibDeps {
996 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
997 }
998
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500999 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001000 var sharedLibDepFiles android.Paths
1001 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001002 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1003 if dep.Toc().Valid() {
1004 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1005 } else {
1006 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1007 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001008 }
1009
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001010 var srcProviderDepFiles android.Paths
1011 for _, dep := range directSrcProvidersDeps {
1012 srcs, _ := dep.OutputFiles("")
1013 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1014 }
1015 for _, dep := range directSrcDeps {
1016 srcs := dep.Srcs()
1017 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1018 }
1019
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1021 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1022 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001023 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001024 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1025 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001026 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027
1028 // Dedup exported flags from dependencies
1029 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001030 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001032 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1033 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1034 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035
1036 return depPaths
1037}
1038
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001039func (mod *Module) InstallInData() bool {
1040 if mod.compiler == nil {
1041 return false
1042 }
1043 return mod.compiler.inData()
1044}
1045
Ivan Lozanoffee3342019-08-27 12:03:00 -07001046func linkPathFromFilePath(filepath android.Path) string {
1047 return strings.Split(filepath.String(), filepath.Base())[0]
1048}
Ivan Lozanod648c432020-02-06 12:05:10 -05001049
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1051 ctx := &depsContext{
1052 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001053 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054
1055 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001056 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001057
Ivan Lozano2b081132020-09-08 12:46:52 -04001058 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001059 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001060 stdLinkage = "rlib-std"
1061 }
1062
1063 rlibDepVariations := commonDepVariations
1064 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1065 rlibDepVariations = append(rlibDepVariations,
1066 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1067 }
1068
Ivan Lozano52767be2019-10-18 14:49:46 -07001069 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001070 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001071 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001072 rlibDepTag, deps.Rlibs...)
1073 actx.AddVariationDependencies(
1074 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001075 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001076 dylibDepTag, deps.Dylibs...)
1077
Ivan Lozano042504f2020-08-18 14:31:23 -04001078 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1079 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001080 if autoDep.depTag == rlibDepTag {
1081 actx.AddVariationDependencies(
1082 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1083 autoDep.depTag, deps.Rustlibs...)
1084 } else {
1085 actx.AddVariationDependencies(
1086 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1087 autoDep.depTag, deps.Rustlibs...)
1088 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001089 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001090 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001091 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001092 actx.AddVariationDependencies(
1093 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1094 rlibDepTag, deps.Stdlibs...)
1095 } else {
1096 actx.AddVariationDependencies(
1097 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1098 dylibDepTag, deps.Stdlibs...)
1099 }
1100 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001101 actx.AddVariationDependencies(append(commonDepVariations,
1102 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001103 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001104 actx.AddVariationDependencies(append(commonDepVariations,
1105 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001106 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001107
Zach Johnson3df4e632020-11-06 11:56:27 -08001108 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1109
Colin Cross565cafd2020-09-25 18:47:38 -07001110 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001111 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001112 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001113 }
1114 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001115 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001116 }
1117
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001118 if mod.sourceProvider != nil {
1119 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1120 bindgen.Properties.Custom_bindgen != "" {
1121 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1122 bindgen.Properties.Custom_bindgen)
1123 }
1124 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001125 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001126 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001127}
1128
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001129func BeginMutator(ctx android.BottomUpMutatorContext) {
1130 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1131 mod.beginMutator(ctx)
1132 }
1133}
1134
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001135func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1136 ctx := &baseModuleContext{
1137 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001138 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001139
1140 mod.begin(ctx)
1141}
1142
Ivan Lozanoffee3342019-08-27 12:03:00 -07001143func (mod *Module) Name() string {
1144 name := mod.ModuleBase.Name()
1145 if p, ok := mod.compiler.(interface {
1146 Name(string) string
1147 }); ok {
1148 name = p.Name(name)
1149 }
1150 return name
1151}
1152
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001153func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001154 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001155 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001156 }
1157}
1158
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001159var _ android.HostToolProvider = (*Module)(nil)
1160
1161func (mod *Module) HostToolPath() android.OptionalPath {
1162 if !mod.Host() {
1163 return android.OptionalPath{}
1164 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001165 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1166 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001167 }
1168 return android.OptionalPath{}
1169}
1170
Jiyong Park99644e92020-11-17 22:21:02 +09001171var _ android.ApexModule = (*Module)(nil)
1172
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001173func (mod *Module) minSdkVersion() string {
1174 return String(mod.Properties.Min_sdk_version)
1175}
1176
Jiyong Park45bf82e2020-12-15 22:29:02 +09001177var _ android.ApexModule = (*Module)(nil)
1178
1179// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001180func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001181 minSdkVersion := mod.minSdkVersion()
1182 if minSdkVersion == "apex_inherit" {
1183 return nil
1184 }
1185 if minSdkVersion == "" {
1186 return fmt.Errorf("min_sdk_version is not specificed")
1187 }
1188
1189 // Not using nativeApiLevelFromUser because the context here is not
1190 // necessarily a native context.
1191 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1192 if err != nil {
1193 return err
1194 }
1195
1196 if ver.GreaterThan(sdkVersion) {
1197 return fmt.Errorf("newer SDK(%v)", ver)
1198 }
Jiyong Park99644e92020-11-17 22:21:02 +09001199 return nil
1200}
1201
Jiyong Park45bf82e2020-12-15 22:29:02 +09001202// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001203func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1204 depTag := ctx.OtherModuleDependencyTag(dep)
1205
1206 if ccm, ok := dep.(*cc.Module); ok {
1207 if ccm.HasStubsVariants() {
1208 if cc.IsSharedDepTag(depTag) {
1209 // dynamic dep to a stubs lib crosses APEX boundary
1210 return false
1211 }
1212 if cc.IsRuntimeDepTag(depTag) {
1213 // runtime dep to a stubs lib also crosses APEX boundary
1214 return false
1215 }
1216
1217 if cc.IsHeaderDepTag(depTag) {
1218 return false
1219 }
1220 }
1221 if mod.Static() && cc.IsSharedDepTag(depTag) {
1222 // shared_lib dependency from a static lib is considered as crossing
1223 // the APEX boundary because the dependency doesn't actually is
1224 // linked; the dependency is used only during the compilation phase.
1225 return false
1226 }
1227 }
1228
1229 if depTag == procMacroDepTag {
1230 return false
1231 }
1232
1233 return true
1234}
1235
1236// Overrides ApexModule.IsInstallabeToApex()
1237func (mod *Module) IsInstallableToApex() bool {
1238 if mod.compiler != nil {
1239 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1240 return true
1241 }
1242 if _, ok := mod.compiler.(*binaryDecorator); ok {
1243 return true
1244 }
1245 }
1246 return false
1247}
1248
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001249// If a library file has a "lib" prefix, extract the library name without the prefix.
1250func libNameFromFilePath(filepath android.Path) (string, bool) {
1251 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1252 if strings.HasPrefix(libName, "lib") {
1253 libName = libName[3:]
1254 return libName, true
1255 }
1256 return "", false
1257}
1258
Ivan Lozanoffee3342019-08-27 12:03:00 -07001259var Bool = proptools.Bool
1260var BoolDefault = proptools.BoolDefault
1261var String = proptools.String
1262var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001263
1264var _ android.OutputFileProducer = (*Module)(nil)