blob: 99ff028d606d03c0edc71c9f8bb50e613b30c17d [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
Colin Crossc511bc52020-04-07 16:50:32 +0000259func (mod *Module) AlwaysSdk() bool {
260 return false
261}
262
Jiyong Park2286afd2020-06-16 21:58:53 +0900263func (mod *Module) IsSdkVariant() bool {
264 return false
265}
266
Colin Cross1348ce32020-10-01 13:37:16 -0700267func (mod *Module) SplitPerApiLevel() bool {
268 return false
269}
270
Ivan Lozanoffee3342019-08-27 12:03:00 -0700271type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400272 Dylibs []string
273 Rlibs []string
274 Rustlibs []string
275 Stdlibs []string
276 ProcMacros []string
277 SharedLibs []string
278 StaticLibs []string
279 WholeStaticLibs []string
280 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700281
282 CrtBegin, CrtEnd string
283}
284
285type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500286 DyLibs RustLibraries
287 RLibs RustLibraries
288 SharedLibs android.Paths
289 SharedLibDeps android.Paths
290 StaticLibs android.Paths
291 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500292
293 // depFlags and depLinkFlags are rustc and linker (clang) flags.
294 depFlags []string
295 depLinkFlags []string
296
297 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
298 // Both of these are exported and propagate to dependencies.
299 linkDirs []string
300 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700301
Ivan Lozano45901ed2020-07-24 16:05:01 -0400302 // Used by bindgen modules which call clang
303 depClangFlags []string
304 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400305 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400306 depSystemIncludePaths android.Paths
307
Ivan Lozanof1c84332019-09-20 11:00:37 -0700308 CrtBegin android.OptionalPath
309 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700310
311 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500312 SrcDeps android.Paths
313 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700314}
315
316type RustLibraries []RustLibrary
317
318type RustLibrary struct {
319 Path android.Path
320 CrateName string
321}
322
323type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100324 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700325 compilerFlags(ctx ModuleContext, flags Flags) Flags
326 compilerProps() []interface{}
327 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
328 compilerDeps(ctx DepsContext, deps Deps) Deps
329 crateName() string
330
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100331 // Output directory in which source-generated code from dependencies is
332 // copied. This is equivalent to Cargo's OUT_DIR variable.
333 CargoOutDir() android.OptionalPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800334 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200335 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700336 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400337
338 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400339
340 Disabled() bool
341 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400342
Ivan Lozanodd055472020-09-28 13:22:45 -0400343 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500344 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400345}
346
Matthew Maurerbb3add12020-06-25 09:34:12 -0700347type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700348 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400349 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700350}
351
352type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400353 linkDirs []string
354 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700355}
356
Matthew Maurerbb3add12020-06-25 09:34:12 -0700357func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
358 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
359}
360
Ivan Lozano2093af22020-08-25 12:48:19 -0400361func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
362 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
363}
364
Colin Cross0de8a1e2020-09-18 14:15:30 -0700365func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
366 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700367 LinkDirs: flagExporter.linkDirs,
368 LinkObjects: flagExporter.linkObjects,
369 })
370}
371
Matthew Maurerbb3add12020-06-25 09:34:12 -0700372var _ exportedFlagsProducer = (*flagExporter)(nil)
373
374func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700375 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700376}
377
Colin Cross0de8a1e2020-09-18 14:15:30 -0700378type FlagExporterInfo struct {
379 Flags []string
380 LinkDirs []string // TODO: this should be android.Paths
381 LinkObjects []string // TODO: this should be android.Paths
382}
383
384var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
385
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400386func (mod *Module) isCoverageVariant() bool {
387 return mod.coverage.Properties.IsCoverageVariant
388}
389
390var _ cc.Coverage = (*Module)(nil)
391
392func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
393 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
394}
395
396func (mod *Module) PreventInstall() {
397 mod.Properties.PreventInstall = true
398}
399
400func (mod *Module) HideFromMake() {
401 mod.Properties.HideFromMake = true
402}
403
404func (mod *Module) MarkAsCoverageVariant(coverage bool) {
405 mod.coverage.Properties.IsCoverageVariant = coverage
406}
407
408func (mod *Module) EnableCoverageIfNeeded() {
409 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410}
411
412func defaultsFactory() android.Module {
413 return DefaultsFactory()
414}
415
416type Defaults struct {
417 android.ModuleBase
418 android.DefaultsModuleBase
419}
420
421func DefaultsFactory(props ...interface{}) android.Module {
422 module := &Defaults{}
423
424 module.AddProperties(props...)
425 module.AddProperties(
426 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500427 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400428 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429 &BaseCompilerProperties{},
430 &BinaryCompilerProperties{},
431 &LibraryCompilerProperties{},
432 &ProcMacroCompilerProperties{},
433 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400434 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700435 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400436 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400437 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200438 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500439 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440 )
441
442 android.InitDefaultsModule(module)
443 return module
444}
445
446func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700447 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700448}
449
Ivan Lozano183a3212019-10-18 14:18:45 -0700450func (mod *Module) CcLibrary() bool {
451 if mod.compiler != nil {
452 if _, ok := mod.compiler.(*libraryDecorator); ok {
453 return true
454 }
455 }
456 return false
457}
458
459func (mod *Module) CcLibraryInterface() bool {
460 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400461 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
462 // VariantIs{Static,Shared} is set.
463 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700464 return true
465 }
466 }
467 return false
468}
469
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800470func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700471 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700472 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800473 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700474 }
475 }
476 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
477}
478
479func (mod *Module) SetStatic() {
480 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700481 if library, ok := mod.compiler.(libraryInterface); ok {
482 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700483 return
484 }
485 }
486 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
487}
488
489func (mod *Module) SetShared() {
490 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700491 if library, ok := mod.compiler.(libraryInterface); ok {
492 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700493 return
494 }
495 }
496 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
497}
498
Ivan Lozano183a3212019-10-18 14:18:45 -0700499func (mod *Module) BuildStaticVariant() bool {
500 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700501 if library, ok := mod.compiler.(libraryInterface); ok {
502 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700503 }
504 }
505 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
506}
507
508func (mod *Module) BuildSharedVariant() bool {
509 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700510 if library, ok := mod.compiler.(libraryInterface); ok {
511 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700512 }
513 }
514 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
515}
516
Ivan Lozano183a3212019-10-18 14:18:45 -0700517func (mod *Module) Module() android.Module {
518 return mod
519}
520
Ivan Lozano183a3212019-10-18 14:18:45 -0700521func (mod *Module) OutputFile() android.OptionalPath {
522 return mod.outputFile
523}
524
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400525func (mod *Module) CoverageFiles() android.Paths {
526 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800527 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400528 }
529 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
530}
531
Jiyong Park459feca2020-12-15 11:02:21 +0900532func (mod *Module) installable(apexInfo android.ApexInfo) bool {
533 // The apex variant is not installable because it is included in the APEX and won't appear
534 // in the system partition as a standalone file.
535 if !apexInfo.IsForPlatform() {
536 return false
537 }
538
539 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
540}
541
Ivan Lozano183a3212019-10-18 14:18:45 -0700542var _ cc.LinkableInterface = (*Module)(nil)
543
Ivan Lozanoffee3342019-08-27 12:03:00 -0700544func (mod *Module) Init() android.Module {
545 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500546 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547
548 if mod.compiler != nil {
549 mod.AddProperties(mod.compiler.compilerProps()...)
550 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400551 if mod.coverage != nil {
552 mod.AddProperties(mod.coverage.props()...)
553 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200554 if mod.clippy != nil {
555 mod.AddProperties(mod.clippy.props()...)
556 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400557 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700558 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400559 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500560 if mod.sanitize != nil {
561 mod.AddProperties(mod.sanitize.props()...)
562 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400563
Ivan Lozanoffee3342019-08-27 12:03:00 -0700564 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900565 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700566
567 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568 return mod
569}
570
571func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
572 return &Module{
573 hod: hod,
574 multilib: multilib,
575 }
576}
577func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
578 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400579 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200580 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500581 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700582 return module
583}
584
585type ModuleContext interface {
586 android.ModuleContext
587 ModuleContextIntf
588}
589
590type BaseModuleContext interface {
591 android.BaseModuleContext
592 ModuleContextIntf
593}
594
595type DepsContext interface {
596 android.BottomUpMutatorContext
597 ModuleContextIntf
598}
599
600type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200601 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700603}
604
605type depsContext struct {
606 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607}
608
609type moduleContext struct {
610 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611}
612
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200613type baseModuleContext struct {
614 android.BaseModuleContext
615}
616
617func (ctx *moduleContext) RustModule() *Module {
618 return ctx.Module().(*Module)
619}
620
621func (ctx *moduleContext) toolchain() config.Toolchain {
622 return ctx.RustModule().toolchain(ctx)
623}
624
625func (ctx *depsContext) RustModule() *Module {
626 return ctx.Module().(*Module)
627}
628
629func (ctx *depsContext) toolchain() config.Toolchain {
630 return ctx.RustModule().toolchain(ctx)
631}
632
633func (ctx *baseModuleContext) RustModule() *Module {
634 return ctx.Module().(*Module)
635}
636
637func (ctx *baseModuleContext) toolchain() config.Toolchain {
638 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400639}
640
641func (mod *Module) nativeCoverage() bool {
642 return mod.compiler != nil && mod.compiler.nativeCoverage()
643}
644
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
646 if mod.cachedToolchain == nil {
647 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
648 }
649 return mod.cachedToolchain
650}
651
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200652func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
653 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
654}
655
Ivan Lozanoffee3342019-08-27 12:03:00 -0700656func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
657}
658
659func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
660 ctx := &moduleContext{
661 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663
Jiyong Park99644e92020-11-17 22:21:02 +0900664 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
665 if !apexInfo.IsForPlatform() {
666 mod.hideApexVariantFromMake = true
667 }
668
Ivan Lozanoffee3342019-08-27 12:03:00 -0700669 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500670 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
671
672 // Differentiate static libraries that are vendor available
673 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500674 mod.Properties.SubName += cc.VendorSuffix
675 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
676 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500677 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678
679 if !toolchain.Supported() {
680 // This toolchain's unsupported, there's nothing to do for this mod.
681 return
682 }
683
684 deps := mod.depsToPaths(ctx)
685 flags := Flags{
686 Toolchain: toolchain,
687 }
688
689 if mod.compiler != nil {
690 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400691 }
692 if mod.coverage != nil {
693 flags, deps = mod.coverage.flags(ctx, flags, deps)
694 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200695 if mod.clippy != nil {
696 flags, deps = mod.clippy.flags(ctx, flags, deps)
697 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500698 if mod.sanitize != nil {
699 flags, deps = mod.sanitize.flags(ctx, flags, deps)
700 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400701
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200702 // SourceProvider needs to call GenerateSource() before compiler calls
703 // compile() so it can provide the source. A SourceProvider has
704 // multiple variants (e.g. source, rlib, dylib). Only the "source"
705 // variant is responsible for effectively generating the source. The
706 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400707 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200708 if mod.compiler.(libraryInterface).source() {
709 mod.sourceProvider.GenerateSource(ctx, deps)
710 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
711 } else {
712 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
713 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700714 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200715 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400716 }
717
718 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100719 mod.compiler.initialize(ctx)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700720 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400721
Ivan Lozanoffee3342019-08-27 12:03:00 -0700722 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900723
724 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
725 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200726 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400727 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700728 }
729}
730
731func (mod *Module) deps(ctx DepsContext) Deps {
732 deps := Deps{}
733
734 if mod.compiler != nil {
735 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400736 }
737 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700738 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739 }
740
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400741 if mod.coverage != nil {
742 deps = mod.coverage.deps(ctx, deps)
743 }
744
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500745 if mod.sanitize != nil {
746 deps = mod.sanitize.deps(ctx, deps)
747 }
748
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
750 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700751 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
753 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
754 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400755 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756 return deps
757
758}
759
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760type dependencyTag struct {
761 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800762 name string
763 library bool
764 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700765}
766
Jiyong Park65b62242020-11-25 12:44:59 +0900767// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
768// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
769func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800770 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900771}
772
773var _ android.InstallNeededDependencyTag = dependencyTag{}
774
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400776 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
777 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
778 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800779 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400780 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200781 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782)
783
Jiyong Park99644e92020-11-17 22:21:02 +0900784func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
785 tag, ok := depTag.(dependencyTag)
786 return ok && tag == dylibDepTag
787}
788
Matthew Maurer0f003b12020-06-29 14:34:06 -0700789type autoDep struct {
790 variation string
791 depTag dependencyTag
792}
793
794var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200795 rlibVariation = "rlib"
796 dylibVariation = "dylib"
797 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
798 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700799)
800
801type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500802 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700803}
804
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400805func (mod *Module) begin(ctx BaseModuleContext) {
806 if mod.coverage != nil {
807 mod.coverage.begin(ctx)
808 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500809 if mod.sanitize != nil {
810 mod.sanitize.begin(ctx)
811 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400812}
813
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
815 var depPaths PathDeps
816
817 directRlibDeps := []*Module{}
818 directDylibDeps := []*Module{}
819 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700820 directSharedLibDeps := [](cc.LinkableInterface){}
821 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400822 directSrcProvidersDeps := []*Module{}
823 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824
825 ctx.VisitDirectDeps(func(dep android.Module) {
826 depName := ctx.OtherModuleName(dep)
827 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400828 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700830
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831 switch depTag {
832 case dylibDepTag:
833 dylib, ok := rustDep.compiler.(libraryInterface)
834 if !ok || !dylib.dylib() {
835 ctx.ModuleErrorf("mod %q not an dylib library", depName)
836 return
837 }
838 directDylibDeps = append(directDylibDeps, rustDep)
839 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
840 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400841
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842 rlib, ok := rustDep.compiler.(libraryInterface)
843 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400844 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845 return
846 }
847 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400848 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 case procMacroDepTag:
850 directProcMacroDeps = append(directProcMacroDeps, rustDep)
851 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400852 case android.SourceDepTag:
853 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
854 // OS/Arch variant is used.
855 var helper string
856 if ctx.Host() {
857 helper = "missing 'host_supported'?"
858 } else {
859 helper = "device module defined?"
860 }
861
862 if dep.Target().Os != ctx.Os() {
863 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
864 return
865 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
866 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
867 return
868 }
869 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700870 }
871
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400872 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700873 if depTag != procMacroDepTag {
874 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
875 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
876 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
877 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 }
879
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400881 linkFile := rustDep.outputFile
882 if !linkFile.Valid() {
883 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
884 depName, ctx.ModuleName())
885 return
886 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700888 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
889 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 }
891 }
892
Ivan Lozano89435d12020-07-31 11:01:18 -0400893 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700894 //Handle C dependencies
895 if _, ok := ccDep.(*Module); !ok {
896 if ccDep.Module().Target().Os != ctx.Os() {
897 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
898 return
899 }
900 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
901 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
902 return
903 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700904 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400905 linkObject := ccDep.OutputFile()
906 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500907
Ivan Lozano2093af22020-08-25 12:48:19 -0400908 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
910 }
911
912 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700913 switch {
914 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -0400915 if cc.IsWholeStaticLib(depTag) {
916 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
917 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500918 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
919 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400920 } else {
921 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 -0500922 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500923 }
924
925 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
926 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400927 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500928 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
929
Colin Cross0de8a1e2020-09-18 14:15:30 -0700930 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
931 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
932 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
933 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
934 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935 directStaticLibDeps = append(directStaticLibDeps, ccDep)
936 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700937 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400939 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700940 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
941 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
942 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
943 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
944 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945 directSharedLibDeps = append(directSharedLibDeps, ccDep)
946 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
947 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800948 case cc.IsHeaderDepTag(depTag):
949 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
950 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
951 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
952 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700953 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400954 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700955 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400956 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957 }
958
959 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700960 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
961 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400962 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700964 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400965
966 if srcDep, ok := dep.(android.SourceFileProducer); ok {
967 switch depTag {
968 case android.SourceDepTag:
969 // These are usually genrules which don't have per-target variants.
970 directSrcDeps = append(directSrcDeps, srcDep)
971 }
972 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700973 })
974
975 var rlibDepFiles RustLibraries
976 for _, dep := range directRlibDeps {
977 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
978 }
979 var dylibDepFiles RustLibraries
980 for _, dep := range directDylibDeps {
981 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
982 }
983 var procMacroDepFiles RustLibraries
984 for _, dep := range directProcMacroDeps {
985 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
986 }
987
988 var staticLibDepFiles android.Paths
989 for _, dep := range directStaticLibDeps {
990 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
991 }
992
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500993 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994 var sharedLibDepFiles android.Paths
995 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500996 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
997 if dep.Toc().Valid() {
998 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
999 } else {
1000 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1001 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002 }
1003
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001004 var srcProviderDepFiles android.Paths
1005 for _, dep := range directSrcProvidersDeps {
1006 srcs, _ := dep.OutputFiles("")
1007 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1008 }
1009 for _, dep := range directSrcDeps {
1010 srcs := dep.Srcs()
1011 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1012 }
1013
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1015 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1016 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001017 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1019 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001020 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001021
1022 // Dedup exported flags from dependencies
1023 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001024 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001026 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1027 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1028 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029
1030 return depPaths
1031}
1032
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001033func (mod *Module) InstallInData() bool {
1034 if mod.compiler == nil {
1035 return false
1036 }
1037 return mod.compiler.inData()
1038}
1039
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040func linkPathFromFilePath(filepath android.Path) string {
1041 return strings.Split(filepath.String(), filepath.Base())[0]
1042}
Ivan Lozanod648c432020-02-06 12:05:10 -05001043
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1045 ctx := &depsContext{
1046 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048
1049 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001050 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001051
Ivan Lozano2b081132020-09-08 12:46:52 -04001052 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001053 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001054 stdLinkage = "rlib-std"
1055 }
1056
1057 rlibDepVariations := commonDepVariations
1058 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1059 rlibDepVariations = append(rlibDepVariations,
1060 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1061 }
1062
Ivan Lozano52767be2019-10-18 14:49:46 -07001063 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001064 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001065 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001066 rlibDepTag, deps.Rlibs...)
1067 actx.AddVariationDependencies(
1068 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001069 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001070 dylibDepTag, deps.Dylibs...)
1071
Ivan Lozano042504f2020-08-18 14:31:23 -04001072 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1073 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001074 if autoDep.depTag == rlibDepTag {
1075 actx.AddVariationDependencies(
1076 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1077 autoDep.depTag, deps.Rustlibs...)
1078 } else {
1079 actx.AddVariationDependencies(
1080 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1081 autoDep.depTag, deps.Rustlibs...)
1082 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001083 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001084 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001085 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001086 actx.AddVariationDependencies(
1087 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1088 rlibDepTag, deps.Stdlibs...)
1089 } else {
1090 actx.AddVariationDependencies(
1091 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1092 dylibDepTag, deps.Stdlibs...)
1093 }
1094 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001095 actx.AddVariationDependencies(append(commonDepVariations,
1096 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001097 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001098 actx.AddVariationDependencies(append(commonDepVariations,
1099 blueprint.Variation{Mutator: "link", Variation: "static"}),
Ivan Lozano63bb7682021-03-23 15:53:44 -04001100 cc.StaticDepTag(false), deps.StaticLibs...)
1101 actx.AddVariationDependencies(append(commonDepVariations,
1102 blueprint.Variation{Mutator: "link", Variation: "static"}),
1103 cc.StaticDepTag(true), deps.WholeStaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001104
Zach Johnson3df4e632020-11-06 11:56:27 -08001105 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1106
Colin Cross565cafd2020-09-25 18:47:38 -07001107 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001108 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001109 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001110 }
1111 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001112 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001113 }
1114
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001115 if mod.sourceProvider != nil {
1116 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1117 bindgen.Properties.Custom_bindgen != "" {
1118 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1119 bindgen.Properties.Custom_bindgen)
1120 }
1121 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001122 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001123 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001124}
1125
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001126func BeginMutator(ctx android.BottomUpMutatorContext) {
1127 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1128 mod.beginMutator(ctx)
1129 }
1130}
1131
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001132func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1133 ctx := &baseModuleContext{
1134 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001135 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001136
1137 mod.begin(ctx)
1138}
1139
Ivan Lozanoffee3342019-08-27 12:03:00 -07001140func (mod *Module) Name() string {
1141 name := mod.ModuleBase.Name()
1142 if p, ok := mod.compiler.(interface {
1143 Name(string) string
1144 }); ok {
1145 name = p.Name(name)
1146 }
1147 return name
1148}
1149
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001150func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001151 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001152 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001153 }
1154}
1155
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001156var _ android.HostToolProvider = (*Module)(nil)
1157
1158func (mod *Module) HostToolPath() android.OptionalPath {
1159 if !mod.Host() {
1160 return android.OptionalPath{}
1161 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001162 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1163 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001164 }
1165 return android.OptionalPath{}
1166}
1167
Jiyong Park99644e92020-11-17 22:21:02 +09001168var _ android.ApexModule = (*Module)(nil)
1169
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001170func (mod *Module) minSdkVersion() string {
1171 return String(mod.Properties.Min_sdk_version)
1172}
1173
Jiyong Park45bf82e2020-12-15 22:29:02 +09001174var _ android.ApexModule = (*Module)(nil)
1175
1176// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001177func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001178 minSdkVersion := mod.minSdkVersion()
1179 if minSdkVersion == "apex_inherit" {
1180 return nil
1181 }
1182 if minSdkVersion == "" {
1183 return fmt.Errorf("min_sdk_version is not specificed")
1184 }
1185
1186 // Not using nativeApiLevelFromUser because the context here is not
1187 // necessarily a native context.
1188 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1189 if err != nil {
1190 return err
1191 }
1192
1193 if ver.GreaterThan(sdkVersion) {
1194 return fmt.Errorf("newer SDK(%v)", ver)
1195 }
Jiyong Park99644e92020-11-17 22:21:02 +09001196 return nil
1197}
1198
Jiyong Park45bf82e2020-12-15 22:29:02 +09001199// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001200func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1201 depTag := ctx.OtherModuleDependencyTag(dep)
1202
1203 if ccm, ok := dep.(*cc.Module); ok {
1204 if ccm.HasStubsVariants() {
1205 if cc.IsSharedDepTag(depTag) {
1206 // dynamic dep to a stubs lib crosses APEX boundary
1207 return false
1208 }
1209 if cc.IsRuntimeDepTag(depTag) {
1210 // runtime dep to a stubs lib also crosses APEX boundary
1211 return false
1212 }
1213
1214 if cc.IsHeaderDepTag(depTag) {
1215 return false
1216 }
1217 }
1218 if mod.Static() && cc.IsSharedDepTag(depTag) {
1219 // shared_lib dependency from a static lib is considered as crossing
1220 // the APEX boundary because the dependency doesn't actually is
1221 // linked; the dependency is used only during the compilation phase.
1222 return false
1223 }
1224 }
1225
1226 if depTag == procMacroDepTag {
1227 return false
1228 }
1229
1230 return true
1231}
1232
1233// Overrides ApexModule.IsInstallabeToApex()
1234func (mod *Module) IsInstallableToApex() bool {
1235 if mod.compiler != nil {
1236 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1237 return true
1238 }
1239 if _, ok := mod.compiler.(*binaryDecorator); ok {
1240 return true
1241 }
1242 }
1243 return false
1244}
1245
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001246// If a library file has a "lib" prefix, extract the library name without the prefix.
1247func libNameFromFilePath(filepath android.Path) (string, bool) {
1248 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1249 if strings.HasPrefix(libName, "lib") {
1250 libName = libName[3:]
1251 return libName, true
1252 }
1253 return "", false
1254}
1255
Ivan Lozanoffee3342019-08-27 12:03:00 -07001256var Bool = proptools.Bool
1257var BoolDefault = proptools.BoolDefault
1258var String = proptools.String
1259var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001260
1261var _ android.OutputFileProducer = (*Module)(nil)