blob: fa54b1b320ecc40e616cfa64bbbba13d7c4438a1 [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 {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400276 Dylibs []string
277 Rlibs []string
278 Rustlibs []string
279 Stdlibs []string
280 ProcMacros []string
281 SharedLibs []string
282 StaticLibs []string
283 WholeStaticLibs []string
284 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700285
286 CrtBegin, CrtEnd string
287}
288
289type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500290 DyLibs RustLibraries
291 RLibs RustLibraries
292 SharedLibs android.Paths
293 SharedLibDeps android.Paths
294 StaticLibs android.Paths
295 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500296
297 // depFlags and depLinkFlags are rustc and linker (clang) flags.
298 depFlags []string
299 depLinkFlags []string
300
301 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
302 // Both of these are exported and propagate to dependencies.
303 linkDirs []string
304 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700305
Ivan Lozano45901ed2020-07-24 16:05:01 -0400306 // Used by bindgen modules which call clang
307 depClangFlags []string
308 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400309 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400310 depSystemIncludePaths android.Paths
311
Ivan Lozanof1c84332019-09-20 11:00:37 -0700312 CrtBegin android.OptionalPath
313 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700314
315 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500316 SrcDeps android.Paths
317 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318}
319
320type RustLibraries []RustLibrary
321
322type RustLibrary struct {
323 Path android.Path
324 CrateName string
325}
326
327type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100328 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700329 compilerFlags(ctx ModuleContext, flags Flags) Flags
330 compilerProps() []interface{}
331 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
332 compilerDeps(ctx DepsContext, deps Deps) Deps
333 crateName() string
334
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100335 // Output directory in which source-generated code from dependencies is
336 // copied. This is equivalent to Cargo's OUT_DIR variable.
337 CargoOutDir() android.OptionalPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800338 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200339 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700340 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400341
342 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400343
344 Disabled() bool
345 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400346
Ivan Lozanodd055472020-09-28 13:22:45 -0400347 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500348 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400349}
350
Matthew Maurerbb3add12020-06-25 09:34:12 -0700351type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700352 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400353 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700354}
355
356type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400357 linkDirs []string
358 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700359}
360
Matthew Maurerbb3add12020-06-25 09:34:12 -0700361func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
362 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
363}
364
Ivan Lozano2093af22020-08-25 12:48:19 -0400365func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
366 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
367}
368
Colin Cross0de8a1e2020-09-18 14:15:30 -0700369func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
370 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700371 LinkDirs: flagExporter.linkDirs,
372 LinkObjects: flagExporter.linkObjects,
373 })
374}
375
Matthew Maurerbb3add12020-06-25 09:34:12 -0700376var _ exportedFlagsProducer = (*flagExporter)(nil)
377
378func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700379 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700380}
381
Colin Cross0de8a1e2020-09-18 14:15:30 -0700382type FlagExporterInfo struct {
383 Flags []string
384 LinkDirs []string // TODO: this should be android.Paths
385 LinkObjects []string // TODO: this should be android.Paths
386}
387
388var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
389
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400390func (mod *Module) isCoverageVariant() bool {
391 return mod.coverage.Properties.IsCoverageVariant
392}
393
394var _ cc.Coverage = (*Module)(nil)
395
396func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
397 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
398}
399
400func (mod *Module) PreventInstall() {
401 mod.Properties.PreventInstall = true
402}
403
404func (mod *Module) HideFromMake() {
405 mod.Properties.HideFromMake = true
406}
407
408func (mod *Module) MarkAsCoverageVariant(coverage bool) {
409 mod.coverage.Properties.IsCoverageVariant = coverage
410}
411
412func (mod *Module) EnableCoverageIfNeeded() {
413 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414}
415
416func defaultsFactory() android.Module {
417 return DefaultsFactory()
418}
419
420type Defaults struct {
421 android.ModuleBase
422 android.DefaultsModuleBase
423}
424
425func DefaultsFactory(props ...interface{}) android.Module {
426 module := &Defaults{}
427
428 module.AddProperties(props...)
429 module.AddProperties(
430 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500431 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100432 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400433 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700434 &BaseCompilerProperties{},
435 &BinaryCompilerProperties{},
436 &LibraryCompilerProperties{},
437 &ProcMacroCompilerProperties{},
438 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400439 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700440 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400441 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400442 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200443 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500444 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445 )
446
447 android.InitDefaultsModule(module)
448 return module
449}
450
451func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700452 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700453}
454
Ivan Lozano183a3212019-10-18 14:18:45 -0700455func (mod *Module) CcLibrary() bool {
456 if mod.compiler != nil {
457 if _, ok := mod.compiler.(*libraryDecorator); ok {
458 return true
459 }
460 }
461 return false
462}
463
464func (mod *Module) CcLibraryInterface() bool {
465 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400466 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
467 // VariantIs{Static,Shared} is set.
468 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700469 return true
470 }
471 }
472 return false
473}
474
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800475func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700476 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700477 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800478 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700479 }
480 }
481 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
482}
483
484func (mod *Module) SetStatic() {
485 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700486 if library, ok := mod.compiler.(libraryInterface); ok {
487 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700488 return
489 }
490 }
491 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
492}
493
494func (mod *Module) SetShared() {
495 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700496 if library, ok := mod.compiler.(libraryInterface); ok {
497 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700498 return
499 }
500 }
501 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
502}
503
Ivan Lozano183a3212019-10-18 14:18:45 -0700504func (mod *Module) BuildStaticVariant() bool {
505 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700506 if library, ok := mod.compiler.(libraryInterface); ok {
507 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700508 }
509 }
510 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
511}
512
513func (mod *Module) BuildSharedVariant() bool {
514 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700515 if library, ok := mod.compiler.(libraryInterface); ok {
516 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700517 }
518 }
519 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
520}
521
Ivan Lozano183a3212019-10-18 14:18:45 -0700522func (mod *Module) Module() android.Module {
523 return mod
524}
525
Ivan Lozano183a3212019-10-18 14:18:45 -0700526func (mod *Module) OutputFile() android.OptionalPath {
527 return mod.outputFile
528}
529
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400530func (mod *Module) CoverageFiles() android.Paths {
531 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800532 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533 }
534 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
535}
536
Jiyong Park459feca2020-12-15 11:02:21 +0900537func (mod *Module) installable(apexInfo android.ApexInfo) bool {
538 // The apex variant is not installable because it is included in the APEX and won't appear
539 // in the system partition as a standalone file.
540 if !apexInfo.IsForPlatform() {
541 return false
542 }
543
544 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
545}
546
Ivan Lozano183a3212019-10-18 14:18:45 -0700547var _ cc.LinkableInterface = (*Module)(nil)
548
Ivan Lozanoffee3342019-08-27 12:03:00 -0700549func (mod *Module) Init() android.Module {
550 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500551 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700552
553 if mod.compiler != nil {
554 mod.AddProperties(mod.compiler.compilerProps()...)
555 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400556 if mod.coverage != nil {
557 mod.AddProperties(mod.coverage.props()...)
558 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200559 if mod.clippy != nil {
560 mod.AddProperties(mod.clippy.props()...)
561 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400562 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700563 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400564 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500565 if mod.sanitize != nil {
566 mod.AddProperties(mod.sanitize.props()...)
567 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400568
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900570 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571
572 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700573 return mod
574}
575
576func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
577 return &Module{
578 hod: hod,
579 multilib: multilib,
580 }
581}
582func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
583 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400584 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200585 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500586 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587 return module
588}
589
590type ModuleContext interface {
591 android.ModuleContext
592 ModuleContextIntf
593}
594
595type BaseModuleContext interface {
596 android.BaseModuleContext
597 ModuleContextIntf
598}
599
600type DepsContext interface {
601 android.BottomUpMutatorContext
602 ModuleContextIntf
603}
604
605type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200606 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700608}
609
610type depsContext struct {
611 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700612}
613
614type moduleContext struct {
615 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700616}
617
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200618type baseModuleContext struct {
619 android.BaseModuleContext
620}
621
622func (ctx *moduleContext) RustModule() *Module {
623 return ctx.Module().(*Module)
624}
625
626func (ctx *moduleContext) toolchain() config.Toolchain {
627 return ctx.RustModule().toolchain(ctx)
628}
629
630func (ctx *depsContext) RustModule() *Module {
631 return ctx.Module().(*Module)
632}
633
634func (ctx *depsContext) toolchain() config.Toolchain {
635 return ctx.RustModule().toolchain(ctx)
636}
637
638func (ctx *baseModuleContext) RustModule() *Module {
639 return ctx.Module().(*Module)
640}
641
642func (ctx *baseModuleContext) toolchain() config.Toolchain {
643 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400644}
645
646func (mod *Module) nativeCoverage() bool {
647 return mod.compiler != nil && mod.compiler.nativeCoverage()
648}
649
Ivan Lozanoffee3342019-08-27 12:03:00 -0700650func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
651 if mod.cachedToolchain == nil {
652 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
653 }
654 return mod.cachedToolchain
655}
656
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200657func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
658 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
659}
660
Ivan Lozanoffee3342019-08-27 12:03:00 -0700661func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
662}
663
664func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
665 ctx := &moduleContext{
666 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700668
Jiyong Park99644e92020-11-17 22:21:02 +0900669 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
670 if !apexInfo.IsForPlatform() {
671 mod.hideApexVariantFromMake = true
672 }
673
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500675 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
676
677 // Differentiate static libraries that are vendor available
678 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500679 mod.Properties.SubName += cc.VendorSuffix
680 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
681 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500682 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683
684 if !toolchain.Supported() {
685 // This toolchain's unsupported, there's nothing to do for this mod.
686 return
687 }
688
689 deps := mod.depsToPaths(ctx)
690 flags := Flags{
691 Toolchain: toolchain,
692 }
693
694 if mod.compiler != nil {
695 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400696 }
697 if mod.coverage != nil {
698 flags, deps = mod.coverage.flags(ctx, flags, deps)
699 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200700 if mod.clippy != nil {
701 flags, deps = mod.clippy.flags(ctx, flags, deps)
702 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500703 if mod.sanitize != nil {
704 flags, deps = mod.sanitize.flags(ctx, flags, deps)
705 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400706
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200707 // SourceProvider needs to call GenerateSource() before compiler calls
708 // compile() so it can provide the source. A SourceProvider has
709 // multiple variants (e.g. source, rlib, dylib). Only the "source"
710 // variant is responsible for effectively generating the source. The
711 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400712 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200713 if mod.compiler.(libraryInterface).source() {
714 mod.sourceProvider.GenerateSource(ctx, deps)
715 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
716 } else {
717 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
718 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700719 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200720 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400721 }
722
723 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100724 mod.compiler.initialize(ctx)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400726
Ivan Lozanoffee3342019-08-27 12:03:00 -0700727 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900728
729 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
730 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200731 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400732 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 }
734}
735
736func (mod *Module) deps(ctx DepsContext) Deps {
737 deps := Deps{}
738
739 if mod.compiler != nil {
740 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400741 }
742 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700743 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744 }
745
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400746 if mod.coverage != nil {
747 deps = mod.coverage.deps(ctx, deps)
748 }
749
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500750 if mod.sanitize != nil {
751 deps = mod.sanitize.deps(ctx, deps)
752 }
753
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
755 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700756 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
758 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
759 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400760 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 return deps
762
763}
764
Ivan Lozanoffee3342019-08-27 12:03:00 -0700765type dependencyTag struct {
766 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800767 name string
768 library bool
769 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700770}
771
Jiyong Park65b62242020-11-25 12:44:59 +0900772// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
773// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
774func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800775 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900776}
777
778var _ android.InstallNeededDependencyTag = dependencyTag{}
779
Ivan Lozanoffee3342019-08-27 12:03:00 -0700780var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400781 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
782 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
783 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800784 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400785 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200786 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787)
788
Jiyong Park99644e92020-11-17 22:21:02 +0900789func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
790 tag, ok := depTag.(dependencyTag)
791 return ok && tag == dylibDepTag
792}
793
Matthew Maurer0f003b12020-06-29 14:34:06 -0700794type autoDep struct {
795 variation string
796 depTag dependencyTag
797}
798
799var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200800 rlibVariation = "rlib"
801 dylibVariation = "dylib"
802 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
803 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700804)
805
806type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500807 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700808}
809
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400810func (mod *Module) begin(ctx BaseModuleContext) {
811 if mod.coverage != nil {
812 mod.coverage.begin(ctx)
813 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500814 if mod.sanitize != nil {
815 mod.sanitize.begin(ctx)
816 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400817}
818
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
820 var depPaths PathDeps
821
822 directRlibDeps := []*Module{}
823 directDylibDeps := []*Module{}
824 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700825 directSharedLibDeps := [](cc.LinkableInterface){}
826 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400827 directSrcProvidersDeps := []*Module{}
828 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829
830 ctx.VisitDirectDeps(func(dep android.Module) {
831 depName := ctx.OtherModuleName(dep)
832 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400833 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700835
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836 switch depTag {
837 case dylibDepTag:
838 dylib, ok := rustDep.compiler.(libraryInterface)
839 if !ok || !dylib.dylib() {
840 ctx.ModuleErrorf("mod %q not an dylib library", depName)
841 return
842 }
843 directDylibDeps = append(directDylibDeps, rustDep)
844 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
845 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400846
Ivan Lozanoffee3342019-08-27 12:03:00 -0700847 rlib, ok := rustDep.compiler.(libraryInterface)
848 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400849 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850 return
851 }
852 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400853 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 case procMacroDepTag:
855 directProcMacroDeps = append(directProcMacroDeps, rustDep)
856 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400857 case android.SourceDepTag:
858 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
859 // OS/Arch variant is used.
860 var helper string
861 if ctx.Host() {
862 helper = "missing 'host_supported'?"
863 } else {
864 helper = "device module defined?"
865 }
866
867 if dep.Target().Os != ctx.Os() {
868 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
869 return
870 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
871 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
872 return
873 }
874 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700875 }
876
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400877 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700878 if depTag != procMacroDepTag {
879 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
880 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
881 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
882 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 }
884
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400886 linkFile := rustDep.outputFile
887 if !linkFile.Valid() {
888 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
889 depName, ctx.ModuleName())
890 return
891 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700893 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
894 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700895 }
896 }
897
Ivan Lozano89435d12020-07-31 11:01:18 -0400898 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700899 //Handle C dependencies
900 if _, ok := ccDep.(*Module); !ok {
901 if ccDep.Module().Target().Os != ctx.Os() {
902 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
903 return
904 }
905 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
906 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
907 return
908 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700909 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400910 linkObject := ccDep.OutputFile()
911 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500912
Ivan Lozano2093af22020-08-25 12:48:19 -0400913 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
915 }
916
917 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700918 switch {
919 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -0400920 if cc.IsWholeStaticLib(depTag) {
921 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
922 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500923 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
924 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400925 } else {
926 ctx.ModuleErrorf("'%q' cannot be listed as a whole_static_library in Rust modules unless the output is prefixed by 'lib'", depName, ctx.ModuleName())
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500927 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500928 }
929
930 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
931 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400932 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500933 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
934
Colin Cross0de8a1e2020-09-18 14:15:30 -0700935 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
936 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
937 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
938 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
939 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940 directStaticLibDeps = append(directStaticLibDeps, ccDep)
941 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700942 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700943 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400944 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700945 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
946 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
947 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
948 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
949 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950 directSharedLibDeps = append(directSharedLibDeps, ccDep)
951 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
952 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800953 case cc.IsHeaderDepTag(depTag):
954 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
955 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
956 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
957 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700958 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400959 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700960 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400961 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700962 }
963
964 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700965 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
966 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400967 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700968 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700969 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400970
971 if srcDep, ok := dep.(android.SourceFileProducer); ok {
972 switch depTag {
973 case android.SourceDepTag:
974 // These are usually genrules which don't have per-target variants.
975 directSrcDeps = append(directSrcDeps, srcDep)
976 }
977 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700978 })
979
980 var rlibDepFiles RustLibraries
981 for _, dep := range directRlibDeps {
982 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
983 }
984 var dylibDepFiles RustLibraries
985 for _, dep := range directDylibDeps {
986 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
987 }
988 var procMacroDepFiles RustLibraries
989 for _, dep := range directProcMacroDeps {
990 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
991 }
992
993 var staticLibDepFiles android.Paths
994 for _, dep := range directStaticLibDeps {
995 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
996 }
997
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500998 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700999 var sharedLibDepFiles android.Paths
1000 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001001 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1002 if dep.Toc().Valid() {
1003 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1004 } else {
1005 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1006 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001007 }
1008
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001009 var srcProviderDepFiles android.Paths
1010 for _, dep := range directSrcProvidersDeps {
1011 srcs, _ := dep.OutputFiles("")
1012 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1013 }
1014 for _, dep := range directSrcDeps {
1015 srcs := dep.Srcs()
1016 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1017 }
1018
Ivan Lozanoffee3342019-08-27 12:03:00 -07001019 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1020 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1021 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001022 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001023 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1024 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001025 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026
1027 // Dedup exported flags from dependencies
1028 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001029 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001031 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1032 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1033 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034
1035 return depPaths
1036}
1037
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001038func (mod *Module) InstallInData() bool {
1039 if mod.compiler == nil {
1040 return false
1041 }
1042 return mod.compiler.inData()
1043}
1044
Ivan Lozanoffee3342019-08-27 12:03:00 -07001045func linkPathFromFilePath(filepath android.Path) string {
1046 return strings.Split(filepath.String(), filepath.Base())[0]
1047}
Ivan Lozanod648c432020-02-06 12:05:10 -05001048
Ivan Lozanoffee3342019-08-27 12:03:00 -07001049func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1050 ctx := &depsContext{
1051 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001053
1054 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001055 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001056
Ivan Lozano2b081132020-09-08 12:46:52 -04001057 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001058 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001059 stdLinkage = "rlib-std"
1060 }
1061
1062 rlibDepVariations := commonDepVariations
1063 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1064 rlibDepVariations = append(rlibDepVariations,
1065 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1066 }
1067
Ivan Lozano52767be2019-10-18 14:49:46 -07001068 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001069 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001070 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001071 rlibDepTag, deps.Rlibs...)
1072 actx.AddVariationDependencies(
1073 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001074 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001075 dylibDepTag, deps.Dylibs...)
1076
Ivan Lozano042504f2020-08-18 14:31:23 -04001077 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1078 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001079 if autoDep.depTag == rlibDepTag {
1080 actx.AddVariationDependencies(
1081 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1082 autoDep.depTag, deps.Rustlibs...)
1083 } else {
1084 actx.AddVariationDependencies(
1085 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1086 autoDep.depTag, deps.Rustlibs...)
1087 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001088 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001089 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001090 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001091 actx.AddVariationDependencies(
1092 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1093 rlibDepTag, deps.Stdlibs...)
1094 } else {
1095 actx.AddVariationDependencies(
1096 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1097 dylibDepTag, deps.Stdlibs...)
1098 }
1099 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001100 actx.AddVariationDependencies(append(commonDepVariations,
1101 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001102 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001103 actx.AddVariationDependencies(append(commonDepVariations,
1104 blueprint.Variation{Mutator: "link", Variation: "static"}),
Ivan Lozano63bb7682021-03-23 15:53:44 -04001105 cc.StaticDepTag(false), deps.StaticLibs...)
1106 actx.AddVariationDependencies(append(commonDepVariations,
1107 blueprint.Variation{Mutator: "link", Variation: "static"}),
1108 cc.StaticDepTag(true), deps.WholeStaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001109
Zach Johnson3df4e632020-11-06 11:56:27 -08001110 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1111
Colin Cross565cafd2020-09-25 18:47:38 -07001112 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001113 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001114 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001115 }
1116 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001117 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001118 }
1119
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001120 if mod.sourceProvider != nil {
1121 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1122 bindgen.Properties.Custom_bindgen != "" {
1123 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1124 bindgen.Properties.Custom_bindgen)
1125 }
1126 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001127 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001128 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001129}
1130
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001131func BeginMutator(ctx android.BottomUpMutatorContext) {
1132 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1133 mod.beginMutator(ctx)
1134 }
1135}
1136
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001137func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1138 ctx := &baseModuleContext{
1139 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001140 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001141
1142 mod.begin(ctx)
1143}
1144
Ivan Lozanoffee3342019-08-27 12:03:00 -07001145func (mod *Module) Name() string {
1146 name := mod.ModuleBase.Name()
1147 if p, ok := mod.compiler.(interface {
1148 Name(string) string
1149 }); ok {
1150 name = p.Name(name)
1151 }
1152 return name
1153}
1154
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001155func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001156 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001157 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001158 }
1159}
1160
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001161var _ android.HostToolProvider = (*Module)(nil)
1162
1163func (mod *Module) HostToolPath() android.OptionalPath {
1164 if !mod.Host() {
1165 return android.OptionalPath{}
1166 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001167 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1168 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001169 }
1170 return android.OptionalPath{}
1171}
1172
Jiyong Park99644e92020-11-17 22:21:02 +09001173var _ android.ApexModule = (*Module)(nil)
1174
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001175func (mod *Module) minSdkVersion() string {
1176 return String(mod.Properties.Min_sdk_version)
1177}
1178
Jiyong Park45bf82e2020-12-15 22:29:02 +09001179var _ android.ApexModule = (*Module)(nil)
1180
1181// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001182func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001183 minSdkVersion := mod.minSdkVersion()
1184 if minSdkVersion == "apex_inherit" {
1185 return nil
1186 }
1187 if minSdkVersion == "" {
1188 return fmt.Errorf("min_sdk_version is not specificed")
1189 }
1190
1191 // Not using nativeApiLevelFromUser because the context here is not
1192 // necessarily a native context.
1193 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1194 if err != nil {
1195 return err
1196 }
1197
1198 if ver.GreaterThan(sdkVersion) {
1199 return fmt.Errorf("newer SDK(%v)", ver)
1200 }
Jiyong Park99644e92020-11-17 22:21:02 +09001201 return nil
1202}
1203
Jiyong Park45bf82e2020-12-15 22:29:02 +09001204// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001205func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1206 depTag := ctx.OtherModuleDependencyTag(dep)
1207
1208 if ccm, ok := dep.(*cc.Module); ok {
1209 if ccm.HasStubsVariants() {
1210 if cc.IsSharedDepTag(depTag) {
1211 // dynamic dep to a stubs lib crosses APEX boundary
1212 return false
1213 }
1214 if cc.IsRuntimeDepTag(depTag) {
1215 // runtime dep to a stubs lib also crosses APEX boundary
1216 return false
1217 }
1218
1219 if cc.IsHeaderDepTag(depTag) {
1220 return false
1221 }
1222 }
1223 if mod.Static() && cc.IsSharedDepTag(depTag) {
1224 // shared_lib dependency from a static lib is considered as crossing
1225 // the APEX boundary because the dependency doesn't actually is
1226 // linked; the dependency is used only during the compilation phase.
1227 return false
1228 }
1229 }
1230
1231 if depTag == procMacroDepTag {
1232 return false
1233 }
1234
1235 return true
1236}
1237
1238// Overrides ApexModule.IsInstallabeToApex()
1239func (mod *Module) IsInstallableToApex() bool {
1240 if mod.compiler != nil {
1241 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1242 return true
1243 }
1244 if _, ok := mod.compiler.(*binaryDecorator); ok {
1245 return true
1246 }
1247 }
1248 return false
1249}
1250
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001251// If a library file has a "lib" prefix, extract the library name without the prefix.
1252func libNameFromFilePath(filepath android.Path) (string, bool) {
1253 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1254 if strings.HasPrefix(libName, "lib") {
1255 libName = libName[3:]
1256 return libName, true
1257 }
1258 return "", false
1259}
1260
Ivan Lozanoffee3342019-08-27 12:03:00 -07001261var Bool = proptools.Bool
1262var BoolDefault = proptools.BoolDefault
1263var String = proptools.String
1264var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001265
1266var _ android.OutputFileProducer = (*Module)(nil)