blob: 8ebdb727c293928d3df90b0ccd8ae9d590a5154e [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 {
272 Dylibs []string
273 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700274 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400275 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276 ProcMacros []string
277 SharedLibs []string
278 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800279 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700280
281 CrtBegin, CrtEnd string
282}
283
284type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500285 DyLibs RustLibraries
286 RLibs RustLibraries
287 SharedLibs android.Paths
288 SharedLibDeps android.Paths
289 StaticLibs android.Paths
290 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500291
292 // depFlags and depLinkFlags are rustc and linker (clang) flags.
293 depFlags []string
294 depLinkFlags []string
295
296 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
297 // Both of these are exported and propagate to dependencies.
298 linkDirs []string
299 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700300
Ivan Lozano45901ed2020-07-24 16:05:01 -0400301 // Used by bindgen modules which call clang
302 depClangFlags []string
303 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400304 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400305 depSystemIncludePaths android.Paths
306
Ivan Lozanof1c84332019-09-20 11:00:37 -0700307 CrtBegin android.OptionalPath
308 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700309
310 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500311 SrcDeps android.Paths
312 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700313}
314
315type RustLibraries []RustLibrary
316
317type RustLibrary struct {
318 Path android.Path
319 CrateName string
320}
321
322type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100323 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700324 compilerFlags(ctx ModuleContext, flags Flags) Flags
325 compilerProps() []interface{}
326 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
327 compilerDeps(ctx DepsContext, deps Deps) Deps
328 crateName() string
329
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100330 // Output directory in which source-generated code from dependencies is
331 // copied. This is equivalent to Cargo's OUT_DIR variable.
332 CargoOutDir() android.OptionalPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800333 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200334 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700335 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400336
337 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400338
339 Disabled() bool
340 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400341
Ivan Lozanodd055472020-09-28 13:22:45 -0400342 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500343 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400344}
345
Matthew Maurerbb3add12020-06-25 09:34:12 -0700346type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700347 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400348 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700349}
350
351type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400352 linkDirs []string
353 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700354}
355
Matthew Maurerbb3add12020-06-25 09:34:12 -0700356func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
357 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
358}
359
Ivan Lozano2093af22020-08-25 12:48:19 -0400360func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
361 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
362}
363
Colin Cross0de8a1e2020-09-18 14:15:30 -0700364func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
365 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700366 LinkDirs: flagExporter.linkDirs,
367 LinkObjects: flagExporter.linkObjects,
368 })
369}
370
Matthew Maurerbb3add12020-06-25 09:34:12 -0700371var _ exportedFlagsProducer = (*flagExporter)(nil)
372
373func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700374 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700375}
376
Colin Cross0de8a1e2020-09-18 14:15:30 -0700377type FlagExporterInfo struct {
378 Flags []string
379 LinkDirs []string // TODO: this should be android.Paths
380 LinkObjects []string // TODO: this should be android.Paths
381}
382
383var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
384
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400385func (mod *Module) isCoverageVariant() bool {
386 return mod.coverage.Properties.IsCoverageVariant
387}
388
389var _ cc.Coverage = (*Module)(nil)
390
391func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
392 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
393}
394
395func (mod *Module) PreventInstall() {
396 mod.Properties.PreventInstall = true
397}
398
399func (mod *Module) HideFromMake() {
400 mod.Properties.HideFromMake = true
401}
402
403func (mod *Module) MarkAsCoverageVariant(coverage bool) {
404 mod.coverage.Properties.IsCoverageVariant = coverage
405}
406
407func (mod *Module) EnableCoverageIfNeeded() {
408 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700409}
410
411func defaultsFactory() android.Module {
412 return DefaultsFactory()
413}
414
415type Defaults struct {
416 android.ModuleBase
417 android.DefaultsModuleBase
418}
419
420func DefaultsFactory(props ...interface{}) android.Module {
421 module := &Defaults{}
422
423 module.AddProperties(props...)
424 module.AddProperties(
425 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500426 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400427 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428 &BaseCompilerProperties{},
429 &BinaryCompilerProperties{},
430 &LibraryCompilerProperties{},
431 &ProcMacroCompilerProperties{},
432 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400433 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700434 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400435 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400436 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200437 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500438 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700439 )
440
441 android.InitDefaultsModule(module)
442 return module
443}
444
445func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700446 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447}
448
Ivan Lozano183a3212019-10-18 14:18:45 -0700449func (mod *Module) CcLibrary() bool {
450 if mod.compiler != nil {
451 if _, ok := mod.compiler.(*libraryDecorator); ok {
452 return true
453 }
454 }
455 return false
456}
457
458func (mod *Module) CcLibraryInterface() bool {
459 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400460 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
461 // VariantIs{Static,Shared} is set.
462 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700463 return true
464 }
465 }
466 return false
467}
468
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800469func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700470 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700471 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800472 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700473 }
474 }
475 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
476}
477
478func (mod *Module) SetStatic() {
479 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700480 if library, ok := mod.compiler.(libraryInterface); ok {
481 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700482 return
483 }
484 }
485 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
486}
487
488func (mod *Module) SetShared() {
489 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700490 if library, ok := mod.compiler.(libraryInterface); ok {
491 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700492 return
493 }
494 }
495 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
496}
497
Ivan Lozano183a3212019-10-18 14:18:45 -0700498func (mod *Module) BuildStaticVariant() bool {
499 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700500 if library, ok := mod.compiler.(libraryInterface); ok {
501 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700502 }
503 }
504 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
505}
506
507func (mod *Module) BuildSharedVariant() bool {
508 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700509 if library, ok := mod.compiler.(libraryInterface); ok {
510 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700511 }
512 }
513 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
514}
515
Ivan Lozano183a3212019-10-18 14:18:45 -0700516func (mod *Module) Module() android.Module {
517 return mod
518}
519
Ivan Lozano183a3212019-10-18 14:18:45 -0700520func (mod *Module) OutputFile() android.OptionalPath {
521 return mod.outputFile
522}
523
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400524func (mod *Module) CoverageFiles() android.Paths {
525 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800526 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400527 }
528 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
529}
530
Jiyong Park459feca2020-12-15 11:02:21 +0900531func (mod *Module) installable(apexInfo android.ApexInfo) bool {
532 // The apex variant is not installable because it is included in the APEX and won't appear
533 // in the system partition as a standalone file.
534 if !apexInfo.IsForPlatform() {
535 return false
536 }
537
538 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
539}
540
Ivan Lozano183a3212019-10-18 14:18:45 -0700541var _ cc.LinkableInterface = (*Module)(nil)
542
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543func (mod *Module) Init() android.Module {
544 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500545 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546
547 if mod.compiler != nil {
548 mod.AddProperties(mod.compiler.compilerProps()...)
549 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400550 if mod.coverage != nil {
551 mod.AddProperties(mod.coverage.props()...)
552 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200553 if mod.clippy != nil {
554 mod.AddProperties(mod.clippy.props()...)
555 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400556 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700557 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400558 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500559 if mod.sanitize != nil {
560 mod.AddProperties(mod.sanitize.props()...)
561 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400562
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900564 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700565
566 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567 return mod
568}
569
570func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
571 return &Module{
572 hod: hod,
573 multilib: multilib,
574 }
575}
576func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
577 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400578 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200579 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500580 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581 return module
582}
583
584type ModuleContext interface {
585 android.ModuleContext
586 ModuleContextIntf
587}
588
589type BaseModuleContext interface {
590 android.BaseModuleContext
591 ModuleContextIntf
592}
593
594type DepsContext interface {
595 android.BottomUpMutatorContext
596 ModuleContextIntf
597}
598
599type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200600 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700601 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602}
603
604type depsContext struct {
605 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606}
607
608type moduleContext struct {
609 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610}
611
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200612type baseModuleContext struct {
613 android.BaseModuleContext
614}
615
616func (ctx *moduleContext) RustModule() *Module {
617 return ctx.Module().(*Module)
618}
619
620func (ctx *moduleContext) toolchain() config.Toolchain {
621 return ctx.RustModule().toolchain(ctx)
622}
623
624func (ctx *depsContext) RustModule() *Module {
625 return ctx.Module().(*Module)
626}
627
628func (ctx *depsContext) toolchain() config.Toolchain {
629 return ctx.RustModule().toolchain(ctx)
630}
631
632func (ctx *baseModuleContext) RustModule() *Module {
633 return ctx.Module().(*Module)
634}
635
636func (ctx *baseModuleContext) toolchain() config.Toolchain {
637 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400638}
639
640func (mod *Module) nativeCoverage() bool {
641 return mod.compiler != nil && mod.compiler.nativeCoverage()
642}
643
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
645 if mod.cachedToolchain == nil {
646 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
647 }
648 return mod.cachedToolchain
649}
650
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200651func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
652 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
653}
654
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
656}
657
658func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
659 ctx := &moduleContext{
660 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700661 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662
Jiyong Park99644e92020-11-17 22:21:02 +0900663 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
664 if !apexInfo.IsForPlatform() {
665 mod.hideApexVariantFromMake = true
666 }
667
Ivan Lozanoffee3342019-08-27 12:03:00 -0700668 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500669 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
670
671 // Differentiate static libraries that are vendor available
672 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500673 mod.Properties.SubName += cc.VendorSuffix
674 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
675 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500676 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700677
678 if !toolchain.Supported() {
679 // This toolchain's unsupported, there's nothing to do for this mod.
680 return
681 }
682
683 deps := mod.depsToPaths(ctx)
684 flags := Flags{
685 Toolchain: toolchain,
686 }
687
688 if mod.compiler != nil {
689 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400690 }
691 if mod.coverage != nil {
692 flags, deps = mod.coverage.flags(ctx, flags, deps)
693 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200694 if mod.clippy != nil {
695 flags, deps = mod.clippy.flags(ctx, flags, deps)
696 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500697 if mod.sanitize != nil {
698 flags, deps = mod.sanitize.flags(ctx, flags, deps)
699 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400700
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200701 // SourceProvider needs to call GenerateSource() before compiler calls
702 // compile() so it can provide the source. A SourceProvider has
703 // multiple variants (e.g. source, rlib, dylib). Only the "source"
704 // variant is responsible for effectively generating the source. The
705 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400706 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200707 if mod.compiler.(libraryInterface).source() {
708 mod.sourceProvider.GenerateSource(ctx, deps)
709 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
710 } else {
711 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
712 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700713 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200714 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400715 }
716
717 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100718 mod.compiler.initialize(ctx)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400720
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900722
723 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
724 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200725 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400726 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700727 }
728}
729
730func (mod *Module) deps(ctx DepsContext) Deps {
731 deps := Deps{}
732
733 if mod.compiler != nil {
734 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400735 }
736 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700737 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738 }
739
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400740 if mod.coverage != nil {
741 deps = mod.coverage.deps(ctx, deps)
742 }
743
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500744 if mod.sanitize != nil {
745 deps = mod.sanitize.deps(ctx, deps)
746 }
747
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
749 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700750 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
752 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
753 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
754
755 return deps
756
757}
758
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759type dependencyTag struct {
760 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800761 name string
762 library bool
763 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764}
765
Jiyong Park65b62242020-11-25 12:44:59 +0900766// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
767// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
768func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800769 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900770}
771
772var _ android.InstallNeededDependencyTag = dependencyTag{}
773
Ivan Lozanoffee3342019-08-27 12:03:00 -0700774var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400775 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
776 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
777 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800778 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400779 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200780 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781)
782
Jiyong Park99644e92020-11-17 22:21:02 +0900783func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
784 tag, ok := depTag.(dependencyTag)
785 return ok && tag == dylibDepTag
786}
787
Matthew Maurer0f003b12020-06-29 14:34:06 -0700788type autoDep struct {
789 variation string
790 depTag dependencyTag
791}
792
793var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200794 rlibVariation = "rlib"
795 dylibVariation = "dylib"
796 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
797 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700798)
799
800type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500801 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700802}
803
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400804func (mod *Module) begin(ctx BaseModuleContext) {
805 if mod.coverage != nil {
806 mod.coverage.begin(ctx)
807 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500808 if mod.sanitize != nil {
809 mod.sanitize.begin(ctx)
810 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400811}
812
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
814 var depPaths PathDeps
815
816 directRlibDeps := []*Module{}
817 directDylibDeps := []*Module{}
818 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700819 directSharedLibDeps := [](cc.LinkableInterface){}
820 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400821 directSrcProvidersDeps := []*Module{}
822 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823
824 ctx.VisitDirectDeps(func(dep android.Module) {
825 depName := ctx.OtherModuleName(dep)
826 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400827 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700829
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 switch depTag {
831 case dylibDepTag:
832 dylib, ok := rustDep.compiler.(libraryInterface)
833 if !ok || !dylib.dylib() {
834 ctx.ModuleErrorf("mod %q not an dylib library", depName)
835 return
836 }
837 directDylibDeps = append(directDylibDeps, rustDep)
838 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
839 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400840
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841 rlib, ok := rustDep.compiler.(libraryInterface)
842 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400843 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 return
845 }
846 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400847 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700848 case procMacroDepTag:
849 directProcMacroDeps = append(directProcMacroDeps, rustDep)
850 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400851 case android.SourceDepTag:
852 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
853 // OS/Arch variant is used.
854 var helper string
855 if ctx.Host() {
856 helper = "missing 'host_supported'?"
857 } else {
858 helper = "device module defined?"
859 }
860
861 if dep.Target().Os != ctx.Os() {
862 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
863 return
864 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
865 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
866 return
867 }
868 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869 }
870
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400871 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700872 if depTag != procMacroDepTag {
873 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
874 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
875 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
876 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700877 }
878
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400880 linkFile := rustDep.outputFile
881 if !linkFile.Valid() {
882 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
883 depName, ctx.ModuleName())
884 return
885 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700886 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700887 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
888 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 }
890 }
891
Ivan Lozano89435d12020-07-31 11:01:18 -0400892 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700893 //Handle C dependencies
894 if _, ok := ccDep.(*Module); !ok {
895 if ccDep.Module().Target().Os != ctx.Os() {
896 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
897 return
898 }
899 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
900 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
901 return
902 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700903 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400904 linkObject := ccDep.OutputFile()
905 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500906
Ivan Lozano2093af22020-08-25 12:48:19 -0400907 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700908 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
909 }
910
911 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700912 switch {
913 case cc.IsStaticDepTag(depTag):
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500914 // Only pass -lstatic for rlibs as it results in dylib bloat.
915 if lib, ok := ctx.Module().(*Module).compiler.(libraryInterface); ok && lib.rlib() {
916 // Link cc static libraries using "-lstatic" so rustc can reason about how to handle these
917 // (for example, bundling them into rlibs).
918 //
919 // rustc does not support linking libraries with the "-l" flag unless they are prefixed by "lib".
920 // If we need to link a library that isn't prefixed by "lib", we'll just link to it directly through
921 // linkObjects; such a library may need to be redeclared by static dependents.
922 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
923 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
924 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500925 }
926
927 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
928 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400929 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500930 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
931
Colin Cross0de8a1e2020-09-18 14:15:30 -0700932 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
933 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
934 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
935 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
936 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937 directStaticLibDeps = append(directStaticLibDeps, ccDep)
938 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700939 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400941 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700942 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
943 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
944 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
945 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
946 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700947 directSharedLibDeps = append(directSharedLibDeps, ccDep)
948 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
949 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800950 case cc.IsHeaderDepTag(depTag):
951 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
952 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
953 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
954 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700955 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400956 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700957 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400958 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700959 }
960
961 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700962 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
963 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400964 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700965 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700966 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400967
968 if srcDep, ok := dep.(android.SourceFileProducer); ok {
969 switch depTag {
970 case android.SourceDepTag:
971 // These are usually genrules which don't have per-target variants.
972 directSrcDeps = append(directSrcDeps, srcDep)
973 }
974 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700975 })
976
977 var rlibDepFiles RustLibraries
978 for _, dep := range directRlibDeps {
979 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
980 }
981 var dylibDepFiles RustLibraries
982 for _, dep := range directDylibDeps {
983 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
984 }
985 var procMacroDepFiles RustLibraries
986 for _, dep := range directProcMacroDeps {
987 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
988 }
989
990 var staticLibDepFiles android.Paths
991 for _, dep := range directStaticLibDeps {
992 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
993 }
994
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500995 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700996 var sharedLibDepFiles android.Paths
997 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500998 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
999 if dep.Toc().Valid() {
1000 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1001 } else {
1002 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1003 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001004 }
1005
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001006 var srcProviderDepFiles android.Paths
1007 for _, dep := range directSrcProvidersDeps {
1008 srcs, _ := dep.OutputFiles("")
1009 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1010 }
1011 for _, dep := range directSrcDeps {
1012 srcs := dep.Srcs()
1013 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1014 }
1015
Ivan Lozanoffee3342019-08-27 12:03:00 -07001016 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1017 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1018 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001019 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1021 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001022 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001023
1024 // Dedup exported flags from dependencies
1025 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001026 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001028 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1029 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1030 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031
1032 return depPaths
1033}
1034
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001035func (mod *Module) InstallInData() bool {
1036 if mod.compiler == nil {
1037 return false
1038 }
1039 return mod.compiler.inData()
1040}
1041
Ivan Lozanoffee3342019-08-27 12:03:00 -07001042func linkPathFromFilePath(filepath android.Path) string {
1043 return strings.Split(filepath.String(), filepath.Base())[0]
1044}
Ivan Lozanod648c432020-02-06 12:05:10 -05001045
Ivan Lozanoffee3342019-08-27 12:03:00 -07001046func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1047 ctx := &depsContext{
1048 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001049 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050
1051 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001052 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001053
Ivan Lozano2b081132020-09-08 12:46:52 -04001054 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001055 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001056 stdLinkage = "rlib-std"
1057 }
1058
1059 rlibDepVariations := commonDepVariations
1060 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1061 rlibDepVariations = append(rlibDepVariations,
1062 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1063 }
1064
Ivan Lozano52767be2019-10-18 14:49:46 -07001065 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001066 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001067 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001068 rlibDepTag, deps.Rlibs...)
1069 actx.AddVariationDependencies(
1070 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001071 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001072 dylibDepTag, deps.Dylibs...)
1073
Ivan Lozano042504f2020-08-18 14:31:23 -04001074 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1075 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001076 if autoDep.depTag == rlibDepTag {
1077 actx.AddVariationDependencies(
1078 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1079 autoDep.depTag, deps.Rustlibs...)
1080 } else {
1081 actx.AddVariationDependencies(
1082 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1083 autoDep.depTag, deps.Rustlibs...)
1084 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001085 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001086 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001087 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001088 actx.AddVariationDependencies(
1089 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1090 rlibDepTag, deps.Stdlibs...)
1091 } else {
1092 actx.AddVariationDependencies(
1093 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1094 dylibDepTag, deps.Stdlibs...)
1095 }
1096 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001097 actx.AddVariationDependencies(append(commonDepVariations,
1098 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001099 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001100 actx.AddVariationDependencies(append(commonDepVariations,
1101 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001102 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001103
Zach Johnson3df4e632020-11-06 11:56:27 -08001104 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1105
Colin Cross565cafd2020-09-25 18:47:38 -07001106 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001107 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001108 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001109 }
1110 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001111 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001112 }
1113
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001114 if mod.sourceProvider != nil {
1115 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1116 bindgen.Properties.Custom_bindgen != "" {
1117 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1118 bindgen.Properties.Custom_bindgen)
1119 }
1120 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001121 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001122 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001123}
1124
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001125func BeginMutator(ctx android.BottomUpMutatorContext) {
1126 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1127 mod.beginMutator(ctx)
1128 }
1129}
1130
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001131func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1132 ctx := &baseModuleContext{
1133 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001134 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001135
1136 mod.begin(ctx)
1137}
1138
Ivan Lozanoffee3342019-08-27 12:03:00 -07001139func (mod *Module) Name() string {
1140 name := mod.ModuleBase.Name()
1141 if p, ok := mod.compiler.(interface {
1142 Name(string) string
1143 }); ok {
1144 name = p.Name(name)
1145 }
1146 return name
1147}
1148
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001149func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001150 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001151 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001152 }
1153}
1154
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001155var _ android.HostToolProvider = (*Module)(nil)
1156
1157func (mod *Module) HostToolPath() android.OptionalPath {
1158 if !mod.Host() {
1159 return android.OptionalPath{}
1160 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001161 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1162 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001163 }
1164 return android.OptionalPath{}
1165}
1166
Jiyong Park99644e92020-11-17 22:21:02 +09001167var _ android.ApexModule = (*Module)(nil)
1168
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001169func (mod *Module) minSdkVersion() string {
1170 return String(mod.Properties.Min_sdk_version)
1171}
1172
Jiyong Park45bf82e2020-12-15 22:29:02 +09001173var _ android.ApexModule = (*Module)(nil)
1174
1175// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001176func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001177 minSdkVersion := mod.minSdkVersion()
1178 if minSdkVersion == "apex_inherit" {
1179 return nil
1180 }
1181 if minSdkVersion == "" {
1182 return fmt.Errorf("min_sdk_version is not specificed")
1183 }
1184
1185 // Not using nativeApiLevelFromUser because the context here is not
1186 // necessarily a native context.
1187 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1188 if err != nil {
1189 return err
1190 }
1191
1192 if ver.GreaterThan(sdkVersion) {
1193 return fmt.Errorf("newer SDK(%v)", ver)
1194 }
Jiyong Park99644e92020-11-17 22:21:02 +09001195 return nil
1196}
1197
Jiyong Park45bf82e2020-12-15 22:29:02 +09001198// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001199func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1200 depTag := ctx.OtherModuleDependencyTag(dep)
1201
1202 if ccm, ok := dep.(*cc.Module); ok {
1203 if ccm.HasStubsVariants() {
1204 if cc.IsSharedDepTag(depTag) {
1205 // dynamic dep to a stubs lib crosses APEX boundary
1206 return false
1207 }
1208 if cc.IsRuntimeDepTag(depTag) {
1209 // runtime dep to a stubs lib also crosses APEX boundary
1210 return false
1211 }
1212
1213 if cc.IsHeaderDepTag(depTag) {
1214 return false
1215 }
1216 }
1217 if mod.Static() && cc.IsSharedDepTag(depTag) {
1218 // shared_lib dependency from a static lib is considered as crossing
1219 // the APEX boundary because the dependency doesn't actually is
1220 // linked; the dependency is used only during the compilation phase.
1221 return false
1222 }
1223 }
1224
1225 if depTag == procMacroDepTag {
1226 return false
1227 }
1228
1229 return true
1230}
1231
1232// Overrides ApexModule.IsInstallabeToApex()
1233func (mod *Module) IsInstallableToApex() bool {
1234 if mod.compiler != nil {
1235 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1236 return true
1237 }
1238 if _, ok := mod.compiler.(*binaryDecorator); ok {
1239 return true
1240 }
1241 }
1242 return false
1243}
1244
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001245// If a library file has a "lib" prefix, extract the library name without the prefix.
1246func libNameFromFilePath(filepath android.Path) (string, bool) {
1247 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1248 if strings.HasPrefix(libName, "lib") {
1249 libName = libName[3:]
1250 return libName, true
1251 }
1252 return "", false
1253}
1254
Ivan Lozanoffee3342019-08-27 12:03:00 -07001255var Bool = proptools.Bool
1256var BoolDefault = proptools.BoolDefault
1257var String = proptools.String
1258var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001259
1260var _ android.OutputFileProducer = (*Module)(nil)