blob: f0d0e361ca2dd43e8a6588706e96506f352170a0 [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{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400432 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700433 &BaseCompilerProperties{},
434 &BinaryCompilerProperties{},
435 &LibraryCompilerProperties{},
436 &ProcMacroCompilerProperties{},
437 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400438 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700439 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400440 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400441 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200442 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500443 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444 )
445
446 android.InitDefaultsModule(module)
447 return module
448}
449
450func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700451 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700452}
453
Ivan Lozano183a3212019-10-18 14:18:45 -0700454func (mod *Module) CcLibrary() bool {
455 if mod.compiler != nil {
456 if _, ok := mod.compiler.(*libraryDecorator); ok {
457 return true
458 }
459 }
460 return false
461}
462
463func (mod *Module) CcLibraryInterface() bool {
464 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400465 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
466 // VariantIs{Static,Shared} is set.
467 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700468 return true
469 }
470 }
471 return false
472}
473
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800474func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700475 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700476 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800477 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700478 }
479 }
480 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
481}
482
483func (mod *Module) SetStatic() {
484 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700485 if library, ok := mod.compiler.(libraryInterface); ok {
486 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700487 return
488 }
489 }
490 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
491}
492
493func (mod *Module) SetShared() {
494 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700495 if library, ok := mod.compiler.(libraryInterface); ok {
496 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700497 return
498 }
499 }
500 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
501}
502
Ivan Lozano183a3212019-10-18 14:18:45 -0700503func (mod *Module) BuildStaticVariant() bool {
504 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700505 if library, ok := mod.compiler.(libraryInterface); ok {
506 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700507 }
508 }
509 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
510}
511
512func (mod *Module) BuildSharedVariant() bool {
513 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700514 if library, ok := mod.compiler.(libraryInterface); ok {
515 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700516 }
517 }
518 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
519}
520
Ivan Lozano183a3212019-10-18 14:18:45 -0700521func (mod *Module) Module() android.Module {
522 return mod
523}
524
Ivan Lozano183a3212019-10-18 14:18:45 -0700525func (mod *Module) OutputFile() android.OptionalPath {
526 return mod.outputFile
527}
528
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400529func (mod *Module) CoverageFiles() android.Paths {
530 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800531 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400532 }
533 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
534}
535
Jiyong Park459feca2020-12-15 11:02:21 +0900536func (mod *Module) installable(apexInfo android.ApexInfo) bool {
537 // The apex variant is not installable because it is included in the APEX and won't appear
538 // in the system partition as a standalone file.
539 if !apexInfo.IsForPlatform() {
540 return false
541 }
542
543 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
544}
545
Ivan Lozano183a3212019-10-18 14:18:45 -0700546var _ cc.LinkableInterface = (*Module)(nil)
547
Ivan Lozanoffee3342019-08-27 12:03:00 -0700548func (mod *Module) Init() android.Module {
549 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500550 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551
552 if mod.compiler != nil {
553 mod.AddProperties(mod.compiler.compilerProps()...)
554 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400555 if mod.coverage != nil {
556 mod.AddProperties(mod.coverage.props()...)
557 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200558 if mod.clippy != nil {
559 mod.AddProperties(mod.clippy.props()...)
560 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400561 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700562 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400563 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500564 if mod.sanitize != nil {
565 mod.AddProperties(mod.sanitize.props()...)
566 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400567
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900569 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700570
571 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572 return mod
573}
574
575func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
576 return &Module{
577 hod: hod,
578 multilib: multilib,
579 }
580}
581func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
582 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200584 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500585 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700586 return module
587}
588
589type ModuleContext interface {
590 android.ModuleContext
591 ModuleContextIntf
592}
593
594type BaseModuleContext interface {
595 android.BaseModuleContext
596 ModuleContextIntf
597}
598
599type DepsContext interface {
600 android.BottomUpMutatorContext
601 ModuleContextIntf
602}
603
604type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200605 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607}
608
609type depsContext struct {
610 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611}
612
613type moduleContext struct {
614 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615}
616
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200617type baseModuleContext struct {
618 android.BaseModuleContext
619}
620
621func (ctx *moduleContext) RustModule() *Module {
622 return ctx.Module().(*Module)
623}
624
625func (ctx *moduleContext) toolchain() config.Toolchain {
626 return ctx.RustModule().toolchain(ctx)
627}
628
629func (ctx *depsContext) RustModule() *Module {
630 return ctx.Module().(*Module)
631}
632
633func (ctx *depsContext) toolchain() config.Toolchain {
634 return ctx.RustModule().toolchain(ctx)
635}
636
637func (ctx *baseModuleContext) RustModule() *Module {
638 return ctx.Module().(*Module)
639}
640
641func (ctx *baseModuleContext) toolchain() config.Toolchain {
642 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400643}
644
645func (mod *Module) nativeCoverage() bool {
646 return mod.compiler != nil && mod.compiler.nativeCoverage()
647}
648
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
650 if mod.cachedToolchain == nil {
651 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
652 }
653 return mod.cachedToolchain
654}
655
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200656func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
657 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
658}
659
Ivan Lozanoffee3342019-08-27 12:03:00 -0700660func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
661}
662
663func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
664 ctx := &moduleContext{
665 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700666 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667
Jiyong Park99644e92020-11-17 22:21:02 +0900668 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
669 if !apexInfo.IsForPlatform() {
670 mod.hideApexVariantFromMake = true
671 }
672
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500674 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
675
676 // Differentiate static libraries that are vendor available
677 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500678 mod.Properties.SubName += cc.VendorSuffix
679 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
680 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500681 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682
683 if !toolchain.Supported() {
684 // This toolchain's unsupported, there's nothing to do for this mod.
685 return
686 }
687
688 deps := mod.depsToPaths(ctx)
689 flags := Flags{
690 Toolchain: toolchain,
691 }
692
693 if mod.compiler != nil {
694 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400695 }
696 if mod.coverage != nil {
697 flags, deps = mod.coverage.flags(ctx, flags, deps)
698 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200699 if mod.clippy != nil {
700 flags, deps = mod.clippy.flags(ctx, flags, deps)
701 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500702 if mod.sanitize != nil {
703 flags, deps = mod.sanitize.flags(ctx, flags, deps)
704 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400705
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200706 // SourceProvider needs to call GenerateSource() before compiler calls
707 // compile() so it can provide the source. A SourceProvider has
708 // multiple variants (e.g. source, rlib, dylib). Only the "source"
709 // variant is responsible for effectively generating the source. The
710 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400711 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200712 if mod.compiler.(libraryInterface).source() {
713 mod.sourceProvider.GenerateSource(ctx, deps)
714 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
715 } else {
716 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
717 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700718 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200719 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400720 }
721
722 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100723 mod.compiler.initialize(ctx)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400725
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900727
728 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
729 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200730 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400731 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700732 }
733}
734
735func (mod *Module) deps(ctx DepsContext) Deps {
736 deps := Deps{}
737
738 if mod.compiler != nil {
739 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400740 }
741 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700742 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 }
744
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400745 if mod.coverage != nil {
746 deps = mod.coverage.deps(ctx, deps)
747 }
748
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500749 if mod.sanitize != nil {
750 deps = mod.sanitize.deps(ctx, deps)
751 }
752
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
754 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700755 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
757 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
758 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400759 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760 return deps
761
762}
763
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764type dependencyTag struct {
765 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800766 name string
767 library bool
768 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700769}
770
Jiyong Park65b62242020-11-25 12:44:59 +0900771// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
772// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
773func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800774 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900775}
776
777var _ android.InstallNeededDependencyTag = dependencyTag{}
778
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400780 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
781 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
782 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800783 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400784 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200785 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700786)
787
Jiyong Park99644e92020-11-17 22:21:02 +0900788func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
789 tag, ok := depTag.(dependencyTag)
790 return ok && tag == dylibDepTag
791}
792
Matthew Maurer0f003b12020-06-29 14:34:06 -0700793type autoDep struct {
794 variation string
795 depTag dependencyTag
796}
797
798var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200799 rlibVariation = "rlib"
800 dylibVariation = "dylib"
801 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
802 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700803)
804
805type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500806 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700807}
808
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400809func (mod *Module) begin(ctx BaseModuleContext) {
810 if mod.coverage != nil {
811 mod.coverage.begin(ctx)
812 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500813 if mod.sanitize != nil {
814 mod.sanitize.begin(ctx)
815 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400816}
817
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
819 var depPaths PathDeps
820
821 directRlibDeps := []*Module{}
822 directDylibDeps := []*Module{}
823 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700824 directSharedLibDeps := [](cc.LinkableInterface){}
825 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400826 directSrcProvidersDeps := []*Module{}
827 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828
829 ctx.VisitDirectDeps(func(dep android.Module) {
830 depName := ctx.OtherModuleName(dep)
831 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400832 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700834
Ivan Lozanoffee3342019-08-27 12:03:00 -0700835 switch depTag {
836 case dylibDepTag:
837 dylib, ok := rustDep.compiler.(libraryInterface)
838 if !ok || !dylib.dylib() {
839 ctx.ModuleErrorf("mod %q not an dylib library", depName)
840 return
841 }
842 directDylibDeps = append(directDylibDeps, rustDep)
843 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
844 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400845
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 rlib, ok := rustDep.compiler.(libraryInterface)
847 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400848 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 return
850 }
851 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400852 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 case procMacroDepTag:
854 directProcMacroDeps = append(directProcMacroDeps, rustDep)
855 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400856 case android.SourceDepTag:
857 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
858 // OS/Arch variant is used.
859 var helper string
860 if ctx.Host() {
861 helper = "missing 'host_supported'?"
862 } else {
863 helper = "device module defined?"
864 }
865
866 if dep.Target().Os != ctx.Os() {
867 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
868 return
869 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
870 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
871 return
872 }
873 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 }
875
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400876 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700877 if depTag != procMacroDepTag {
878 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
879 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
880 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
881 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 }
883
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400885 linkFile := rustDep.outputFile
886 if !linkFile.Valid() {
887 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
888 depName, ctx.ModuleName())
889 return
890 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700892 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
893 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 }
895 }
896
Ivan Lozano89435d12020-07-31 11:01:18 -0400897 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700898 //Handle C dependencies
899 if _, ok := ccDep.(*Module); !ok {
900 if ccDep.Module().Target().Os != ctx.Os() {
901 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
902 return
903 }
904 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
905 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
906 return
907 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700908 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400909 linkObject := ccDep.OutputFile()
910 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500911
Ivan Lozano2093af22020-08-25 12:48:19 -0400912 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
914 }
915
916 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700917 switch {
918 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -0400919 if cc.IsWholeStaticLib(depTag) {
920 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
921 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500922 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
923 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400924 } else {
925 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 -0500926 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500927 }
928
929 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
930 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400931 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500932 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
933
Colin Cross0de8a1e2020-09-18 14:15:30 -0700934 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
935 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
936 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
937 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
938 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700939 directStaticLibDeps = append(directStaticLibDeps, ccDep)
940 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700941 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400943 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700944 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
945 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
946 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
947 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
948 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949 directSharedLibDeps = append(directSharedLibDeps, ccDep)
950 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
951 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800952 case cc.IsHeaderDepTag(depTag):
953 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
954 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
955 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
956 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700957 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400958 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700959 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400960 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961 }
962
963 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700964 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
965 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400966 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700968 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400969
970 if srcDep, ok := dep.(android.SourceFileProducer); ok {
971 switch depTag {
972 case android.SourceDepTag:
973 // These are usually genrules which don't have per-target variants.
974 directSrcDeps = append(directSrcDeps, srcDep)
975 }
976 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700977 })
978
979 var rlibDepFiles RustLibraries
980 for _, dep := range directRlibDeps {
981 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
982 }
983 var dylibDepFiles RustLibraries
984 for _, dep := range directDylibDeps {
985 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
986 }
987 var procMacroDepFiles RustLibraries
988 for _, dep := range directProcMacroDeps {
989 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
990 }
991
992 var staticLibDepFiles android.Paths
993 for _, dep := range directStaticLibDeps {
994 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
995 }
996
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500997 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998 var sharedLibDepFiles android.Paths
999 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001000 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1001 if dep.Toc().Valid() {
1002 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1003 } else {
1004 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1005 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001006 }
1007
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001008 var srcProviderDepFiles android.Paths
1009 for _, dep := range directSrcProvidersDeps {
1010 srcs, _ := dep.OutputFiles("")
1011 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1012 }
1013 for _, dep := range directSrcDeps {
1014 srcs := dep.Srcs()
1015 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1016 }
1017
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1019 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1020 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001021 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001022 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1023 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001024 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025
1026 // Dedup exported flags from dependencies
1027 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001028 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001030 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1031 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1032 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001033
1034 return depPaths
1035}
1036
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001037func (mod *Module) InstallInData() bool {
1038 if mod.compiler == nil {
1039 return false
1040 }
1041 return mod.compiler.inData()
1042}
1043
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044func linkPathFromFilePath(filepath android.Path) string {
1045 return strings.Split(filepath.String(), filepath.Base())[0]
1046}
Ivan Lozanod648c432020-02-06 12:05:10 -05001047
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1049 ctx := &depsContext{
1050 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052
1053 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001054 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001055
Ivan Lozano2b081132020-09-08 12:46:52 -04001056 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001057 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001058 stdLinkage = "rlib-std"
1059 }
1060
1061 rlibDepVariations := commonDepVariations
1062 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1063 rlibDepVariations = append(rlibDepVariations,
1064 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1065 }
1066
Ivan Lozano52767be2019-10-18 14:49:46 -07001067 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001068 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001069 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001070 rlibDepTag, deps.Rlibs...)
1071 actx.AddVariationDependencies(
1072 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001073 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001074 dylibDepTag, deps.Dylibs...)
1075
Ivan Lozano042504f2020-08-18 14:31:23 -04001076 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1077 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001078 if autoDep.depTag == rlibDepTag {
1079 actx.AddVariationDependencies(
1080 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1081 autoDep.depTag, deps.Rustlibs...)
1082 } else {
1083 actx.AddVariationDependencies(
1084 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1085 autoDep.depTag, deps.Rustlibs...)
1086 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001087 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001088 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001089 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001090 actx.AddVariationDependencies(
1091 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1092 rlibDepTag, deps.Stdlibs...)
1093 } else {
1094 actx.AddVariationDependencies(
1095 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1096 dylibDepTag, deps.Stdlibs...)
1097 }
1098 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001099 actx.AddVariationDependencies(append(commonDepVariations,
1100 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001101 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001102 actx.AddVariationDependencies(append(commonDepVariations,
1103 blueprint.Variation{Mutator: "link", Variation: "static"}),
Ivan Lozano63bb7682021-03-23 15:53:44 -04001104 cc.StaticDepTag(false), deps.StaticLibs...)
1105 actx.AddVariationDependencies(append(commonDepVariations,
1106 blueprint.Variation{Mutator: "link", Variation: "static"}),
1107 cc.StaticDepTag(true), deps.WholeStaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001108
Zach Johnson3df4e632020-11-06 11:56:27 -08001109 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1110
Colin Cross565cafd2020-09-25 18:47:38 -07001111 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001112 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001113 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001114 }
1115 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001116 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001117 }
1118
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001119 if mod.sourceProvider != nil {
1120 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1121 bindgen.Properties.Custom_bindgen != "" {
1122 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1123 bindgen.Properties.Custom_bindgen)
1124 }
1125 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001126 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001127 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001128}
1129
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001130func BeginMutator(ctx android.BottomUpMutatorContext) {
1131 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1132 mod.beginMutator(ctx)
1133 }
1134}
1135
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001136func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1137 ctx := &baseModuleContext{
1138 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001139 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001140
1141 mod.begin(ctx)
1142}
1143
Ivan Lozanoffee3342019-08-27 12:03:00 -07001144func (mod *Module) Name() string {
1145 name := mod.ModuleBase.Name()
1146 if p, ok := mod.compiler.(interface {
1147 Name(string) string
1148 }); ok {
1149 name = p.Name(name)
1150 }
1151 return name
1152}
1153
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001154func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001155 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001156 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001157 }
1158}
1159
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001160var _ android.HostToolProvider = (*Module)(nil)
1161
1162func (mod *Module) HostToolPath() android.OptionalPath {
1163 if !mod.Host() {
1164 return android.OptionalPath{}
1165 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001166 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1167 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001168 }
1169 return android.OptionalPath{}
1170}
1171
Jiyong Park99644e92020-11-17 22:21:02 +09001172var _ android.ApexModule = (*Module)(nil)
1173
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001174func (mod *Module) minSdkVersion() string {
1175 return String(mod.Properties.Min_sdk_version)
1176}
1177
Jiyong Park45bf82e2020-12-15 22:29:02 +09001178var _ android.ApexModule = (*Module)(nil)
1179
1180// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001181func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001182 minSdkVersion := mod.minSdkVersion()
1183 if minSdkVersion == "apex_inherit" {
1184 return nil
1185 }
1186 if minSdkVersion == "" {
1187 return fmt.Errorf("min_sdk_version is not specificed")
1188 }
1189
1190 // Not using nativeApiLevelFromUser because the context here is not
1191 // necessarily a native context.
1192 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1193 if err != nil {
1194 return err
1195 }
1196
1197 if ver.GreaterThan(sdkVersion) {
1198 return fmt.Errorf("newer SDK(%v)", ver)
1199 }
Jiyong Park99644e92020-11-17 22:21:02 +09001200 return nil
1201}
1202
Jiyong Park45bf82e2020-12-15 22:29:02 +09001203// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001204func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1205 depTag := ctx.OtherModuleDependencyTag(dep)
1206
1207 if ccm, ok := dep.(*cc.Module); ok {
1208 if ccm.HasStubsVariants() {
1209 if cc.IsSharedDepTag(depTag) {
1210 // dynamic dep to a stubs lib crosses APEX boundary
1211 return false
1212 }
1213 if cc.IsRuntimeDepTag(depTag) {
1214 // runtime dep to a stubs lib also crosses APEX boundary
1215 return false
1216 }
1217
1218 if cc.IsHeaderDepTag(depTag) {
1219 return false
1220 }
1221 }
1222 if mod.Static() && cc.IsSharedDepTag(depTag) {
1223 // shared_lib dependency from a static lib is considered as crossing
1224 // the APEX boundary because the dependency doesn't actually is
1225 // linked; the dependency is used only during the compilation phase.
1226 return false
1227 }
1228 }
1229
1230 if depTag == procMacroDepTag {
1231 return false
1232 }
1233
1234 return true
1235}
1236
1237// Overrides ApexModule.IsInstallabeToApex()
1238func (mod *Module) IsInstallableToApex() bool {
1239 if mod.compiler != nil {
1240 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1241 return true
1242 }
1243 if _, ok := mod.compiler.(*binaryDecorator); ok {
1244 return true
1245 }
1246 }
1247 return false
1248}
1249
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001250// If a library file has a "lib" prefix, extract the library name without the prefix.
1251func libNameFromFilePath(filepath android.Path) (string, bool) {
1252 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1253 if strings.HasPrefix(libName, "lib") {
1254 libName = libName[3:]
1255 return libName, true
1256 }
1257 return "", false
1258}
1259
Ivan Lozanoffee3342019-08-27 12:03:00 -07001260var Bool = proptools.Bool
1261var BoolDefault = proptools.BoolDefault
1262var String = proptools.String
1263var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001264
1265var _ android.OutputFileProducer = (*Module)(nil)