blob: dc23abb8d905401ad0a693ffff679de03b9700ee [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 {
323 compilerFlags(ctx ModuleContext, flags Flags) Flags
324 compilerProps() []interface{}
325 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
326 compilerDeps(ctx DepsContext, deps Deps) Deps
327 crateName() string
328
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800329 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200330 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700331 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400332
333 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400334
335 Disabled() bool
336 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400337
Ivan Lozanodd055472020-09-28 13:22:45 -0400338 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500339 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400340}
341
Matthew Maurerbb3add12020-06-25 09:34:12 -0700342type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700343 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400344 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700345}
346
347type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400348 linkDirs []string
349 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700350}
351
Matthew Maurerbb3add12020-06-25 09:34:12 -0700352func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
353 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
354}
355
Ivan Lozano2093af22020-08-25 12:48:19 -0400356func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
357 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
358}
359
Colin Cross0de8a1e2020-09-18 14:15:30 -0700360func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
361 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700362 LinkDirs: flagExporter.linkDirs,
363 LinkObjects: flagExporter.linkObjects,
364 })
365}
366
Matthew Maurerbb3add12020-06-25 09:34:12 -0700367var _ exportedFlagsProducer = (*flagExporter)(nil)
368
369func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700370 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700371}
372
Colin Cross0de8a1e2020-09-18 14:15:30 -0700373type FlagExporterInfo struct {
374 Flags []string
375 LinkDirs []string // TODO: this should be android.Paths
376 LinkObjects []string // TODO: this should be android.Paths
377}
378
379var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
380
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400381func (mod *Module) isCoverageVariant() bool {
382 return mod.coverage.Properties.IsCoverageVariant
383}
384
385var _ cc.Coverage = (*Module)(nil)
386
387func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
388 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
389}
390
391func (mod *Module) PreventInstall() {
392 mod.Properties.PreventInstall = true
393}
394
395func (mod *Module) HideFromMake() {
396 mod.Properties.HideFromMake = true
397}
398
399func (mod *Module) MarkAsCoverageVariant(coverage bool) {
400 mod.coverage.Properties.IsCoverageVariant = coverage
401}
402
403func (mod *Module) EnableCoverageIfNeeded() {
404 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700405}
406
407func defaultsFactory() android.Module {
408 return DefaultsFactory()
409}
410
411type Defaults struct {
412 android.ModuleBase
413 android.DefaultsModuleBase
414}
415
416func DefaultsFactory(props ...interface{}) android.Module {
417 module := &Defaults{}
418
419 module.AddProperties(props...)
420 module.AddProperties(
421 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500422 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400423 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700424 &BaseCompilerProperties{},
425 &BinaryCompilerProperties{},
426 &LibraryCompilerProperties{},
427 &ProcMacroCompilerProperties{},
428 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400429 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700430 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400431 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400432 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200433 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500434 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700435 )
436
437 android.InitDefaultsModule(module)
438 return module
439}
440
441func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700442 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443}
444
Ivan Lozano183a3212019-10-18 14:18:45 -0700445func (mod *Module) CcLibrary() bool {
446 if mod.compiler != nil {
447 if _, ok := mod.compiler.(*libraryDecorator); ok {
448 return true
449 }
450 }
451 return false
452}
453
454func (mod *Module) CcLibraryInterface() bool {
455 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400456 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
457 // VariantIs{Static,Shared} is set.
458 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700459 return true
460 }
461 }
462 return false
463}
464
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800465func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700466 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700467 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800468 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700469 }
470 }
471 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
472}
473
474func (mod *Module) SetStatic() {
475 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700476 if library, ok := mod.compiler.(libraryInterface); ok {
477 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700478 return
479 }
480 }
481 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
482}
483
484func (mod *Module) SetShared() {
485 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700486 if library, ok := mod.compiler.(libraryInterface); ok {
487 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700488 return
489 }
490 }
491 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
492}
493
Ivan Lozano183a3212019-10-18 14:18:45 -0700494func (mod *Module) BuildStaticVariant() bool {
495 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700496 if library, ok := mod.compiler.(libraryInterface); ok {
497 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700498 }
499 }
500 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
501}
502
503func (mod *Module) BuildSharedVariant() bool {
504 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700505 if library, ok := mod.compiler.(libraryInterface); ok {
506 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700507 }
508 }
509 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
510}
511
Ivan Lozano183a3212019-10-18 14:18:45 -0700512func (mod *Module) Module() android.Module {
513 return mod
514}
515
Ivan Lozano183a3212019-10-18 14:18:45 -0700516func (mod *Module) OutputFile() android.OptionalPath {
517 return mod.outputFile
518}
519
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400520func (mod *Module) CoverageFiles() android.Paths {
521 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800522 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400523 }
524 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
525}
526
Jiyong Park459feca2020-12-15 11:02:21 +0900527func (mod *Module) installable(apexInfo android.ApexInfo) bool {
528 // The apex variant is not installable because it is included in the APEX and won't appear
529 // in the system partition as a standalone file.
530 if !apexInfo.IsForPlatform() {
531 return false
532 }
533
534 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
535}
536
Ivan Lozano183a3212019-10-18 14:18:45 -0700537var _ cc.LinkableInterface = (*Module)(nil)
538
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539func (mod *Module) Init() android.Module {
540 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500541 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542
543 if mod.compiler != nil {
544 mod.AddProperties(mod.compiler.compilerProps()...)
545 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400546 if mod.coverage != nil {
547 mod.AddProperties(mod.coverage.props()...)
548 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200549 if mod.clippy != nil {
550 mod.AddProperties(mod.clippy.props()...)
551 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400552 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700553 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400554 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500555 if mod.sanitize != nil {
556 mod.AddProperties(mod.sanitize.props()...)
557 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400558
Ivan Lozanoffee3342019-08-27 12:03:00 -0700559 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900560 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700561
562 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563 return mod
564}
565
566func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
567 return &Module{
568 hod: hod,
569 multilib: multilib,
570 }
571}
572func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
573 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400574 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200575 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500576 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700577 return module
578}
579
580type ModuleContext interface {
581 android.ModuleContext
582 ModuleContextIntf
583}
584
585type BaseModuleContext interface {
586 android.BaseModuleContext
587 ModuleContextIntf
588}
589
590type DepsContext interface {
591 android.BottomUpMutatorContext
592 ModuleContextIntf
593}
594
595type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200596 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700597 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598}
599
600type depsContext struct {
601 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602}
603
604type moduleContext struct {
605 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606}
607
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200608type baseModuleContext struct {
609 android.BaseModuleContext
610}
611
612func (ctx *moduleContext) RustModule() *Module {
613 return ctx.Module().(*Module)
614}
615
616func (ctx *moduleContext) toolchain() config.Toolchain {
617 return ctx.RustModule().toolchain(ctx)
618}
619
620func (ctx *depsContext) RustModule() *Module {
621 return ctx.Module().(*Module)
622}
623
624func (ctx *depsContext) toolchain() config.Toolchain {
625 return ctx.RustModule().toolchain(ctx)
626}
627
628func (ctx *baseModuleContext) RustModule() *Module {
629 return ctx.Module().(*Module)
630}
631
632func (ctx *baseModuleContext) toolchain() config.Toolchain {
633 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400634}
635
636func (mod *Module) nativeCoverage() bool {
637 return mod.compiler != nil && mod.compiler.nativeCoverage()
638}
639
Ivan Lozanoffee3342019-08-27 12:03:00 -0700640func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
641 if mod.cachedToolchain == nil {
642 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
643 }
644 return mod.cachedToolchain
645}
646
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200647func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
648 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
649}
650
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
652}
653
654func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
655 ctx := &moduleContext{
656 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700658
Jiyong Park99644e92020-11-17 22:21:02 +0900659 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
660 if !apexInfo.IsForPlatform() {
661 mod.hideApexVariantFromMake = true
662 }
663
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500665 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
666
667 // Differentiate static libraries that are vendor available
668 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500669 mod.Properties.SubName += cc.VendorSuffix
670 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
671 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500672 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673
674 if !toolchain.Supported() {
675 // This toolchain's unsupported, there's nothing to do for this mod.
676 return
677 }
678
679 deps := mod.depsToPaths(ctx)
680 flags := Flags{
681 Toolchain: toolchain,
682 }
683
684 if mod.compiler != nil {
685 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400686 }
687 if mod.coverage != nil {
688 flags, deps = mod.coverage.flags(ctx, flags, deps)
689 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200690 if mod.clippy != nil {
691 flags, deps = mod.clippy.flags(ctx, flags, deps)
692 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500693 if mod.sanitize != nil {
694 flags, deps = mod.sanitize.flags(ctx, flags, deps)
695 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400696
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200697 // SourceProvider needs to call GenerateSource() before compiler calls
698 // compile() so it can provide the source. A SourceProvider has
699 // multiple variants (e.g. source, rlib, dylib). Only the "source"
700 // variant is responsible for effectively generating the source. The
701 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400702 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200703 if mod.compiler.(libraryInterface).source() {
704 mod.sourceProvider.GenerateSource(ctx, deps)
705 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
706 } else {
707 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
708 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700709 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200710 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400711 }
712
713 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700714 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400715
Ivan Lozanoffee3342019-08-27 12:03:00 -0700716 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900717
718 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
719 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200720 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400721 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700722 }
723}
724
725func (mod *Module) deps(ctx DepsContext) Deps {
726 deps := Deps{}
727
728 if mod.compiler != nil {
729 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400730 }
731 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700732 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733 }
734
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400735 if mod.coverage != nil {
736 deps = mod.coverage.deps(ctx, deps)
737 }
738
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500739 if mod.sanitize != nil {
740 deps = mod.sanitize.deps(ctx, deps)
741 }
742
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
744 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700745 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
747 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
748 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
749
750 return deps
751
752}
753
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754type dependencyTag struct {
755 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800756 name string
757 library bool
758 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759}
760
Jiyong Park65b62242020-11-25 12:44:59 +0900761// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
762// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
763func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800764 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900765}
766
767var _ android.InstallNeededDependencyTag = dependencyTag{}
768
Ivan Lozanoffee3342019-08-27 12:03:00 -0700769var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400770 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
771 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
772 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800773 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400774 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200775 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700776)
777
Jiyong Park99644e92020-11-17 22:21:02 +0900778func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
779 tag, ok := depTag.(dependencyTag)
780 return ok && tag == dylibDepTag
781}
782
Matthew Maurer0f003b12020-06-29 14:34:06 -0700783type autoDep struct {
784 variation string
785 depTag dependencyTag
786}
787
788var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200789 rlibVariation = "rlib"
790 dylibVariation = "dylib"
791 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
792 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700793)
794
795type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500796 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700797}
798
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400799func (mod *Module) begin(ctx BaseModuleContext) {
800 if mod.coverage != nil {
801 mod.coverage.begin(ctx)
802 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500803 if mod.sanitize != nil {
804 mod.sanitize.begin(ctx)
805 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400806}
807
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
809 var depPaths PathDeps
810
811 directRlibDeps := []*Module{}
812 directDylibDeps := []*Module{}
813 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700814 directSharedLibDeps := [](cc.LinkableInterface){}
815 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400816 directSrcProvidersDeps := []*Module{}
817 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818
819 ctx.VisitDirectDeps(func(dep android.Module) {
820 depName := ctx.OtherModuleName(dep)
821 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400822 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700824
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825 switch depTag {
826 case dylibDepTag:
827 dylib, ok := rustDep.compiler.(libraryInterface)
828 if !ok || !dylib.dylib() {
829 ctx.ModuleErrorf("mod %q not an dylib library", depName)
830 return
831 }
832 directDylibDeps = append(directDylibDeps, rustDep)
833 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
834 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400835
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836 rlib, ok := rustDep.compiler.(libraryInterface)
837 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400838 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839 return
840 }
841 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400842 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700843 case procMacroDepTag:
844 directProcMacroDeps = append(directProcMacroDeps, rustDep)
845 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400846 case android.SourceDepTag:
847 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
848 // OS/Arch variant is used.
849 var helper string
850 if ctx.Host() {
851 helper = "missing 'host_supported'?"
852 } else {
853 helper = "device module defined?"
854 }
855
856 if dep.Target().Os != ctx.Os() {
857 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
858 return
859 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
860 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
861 return
862 }
863 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700864 }
865
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400866 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700867 if depTag != procMacroDepTag {
868 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
869 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
870 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
871 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872 }
873
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400875 linkFile := rustDep.outputFile
876 if !linkFile.Valid() {
877 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
878 depName, ctx.ModuleName())
879 return
880 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700882 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
883 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 }
885 }
886
Ivan Lozano89435d12020-07-31 11:01:18 -0400887 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700888 //Handle C dependencies
889 if _, ok := ccDep.(*Module); !ok {
890 if ccDep.Module().Target().Os != ctx.Os() {
891 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
892 return
893 }
894 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
895 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
896 return
897 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700898 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400899 linkObject := ccDep.OutputFile()
900 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500901
Ivan Lozano2093af22020-08-25 12:48:19 -0400902 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700903 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
904 }
905
906 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700907 switch {
908 case cc.IsStaticDepTag(depTag):
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500909 // Only pass -lstatic for rlibs as it results in dylib bloat.
910 if lib, ok := ctx.Module().(*Module).compiler.(libraryInterface); ok && lib.rlib() {
911 // Link cc static libraries using "-lstatic" so rustc can reason about how to handle these
912 // (for example, bundling them into rlibs).
913 //
914 // rustc does not support linking libraries with the "-l" flag unless they are prefixed by "lib".
915 // If we need to link a library that isn't prefixed by "lib", we'll just link to it directly through
916 // linkObjects; such a library may need to be redeclared by static dependents.
917 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
918 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
919 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500920 }
921
922 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
923 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400924 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500925 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
926
Colin Cross0de8a1e2020-09-18 14:15:30 -0700927 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
928 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
929 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
930 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
931 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700932 directStaticLibDeps = append(directStaticLibDeps, ccDep)
933 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700934 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400936 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700937 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
938 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
939 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
940 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
941 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942 directSharedLibDeps = append(directSharedLibDeps, ccDep)
943 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
944 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800945 case cc.IsHeaderDepTag(depTag):
946 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
947 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
948 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
949 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700950 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400951 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700952 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400953 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700954 }
955
956 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700957 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
958 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400959 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700960 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400962
963 if srcDep, ok := dep.(android.SourceFileProducer); ok {
964 switch depTag {
965 case android.SourceDepTag:
966 // These are usually genrules which don't have per-target variants.
967 directSrcDeps = append(directSrcDeps, srcDep)
968 }
969 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700970 })
971
972 var rlibDepFiles RustLibraries
973 for _, dep := range directRlibDeps {
974 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
975 }
976 var dylibDepFiles RustLibraries
977 for _, dep := range directDylibDeps {
978 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
979 }
980 var procMacroDepFiles RustLibraries
981 for _, dep := range directProcMacroDeps {
982 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
983 }
984
985 var staticLibDepFiles android.Paths
986 for _, dep := range directStaticLibDeps {
987 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
988 }
989
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500990 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700991 var sharedLibDepFiles android.Paths
992 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500993 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
994 if dep.Toc().Valid() {
995 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
996 } else {
997 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
998 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700999 }
1000
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001001 var srcProviderDepFiles android.Paths
1002 for _, dep := range directSrcProvidersDeps {
1003 srcs, _ := dep.OutputFiles("")
1004 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1005 }
1006 for _, dep := range directSrcDeps {
1007 srcs := dep.Srcs()
1008 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1009 }
1010
Ivan Lozanoffee3342019-08-27 12:03:00 -07001011 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1012 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1013 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001014 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1016 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001017 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018
1019 // Dedup exported flags from dependencies
1020 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001021 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001022 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001023 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1024 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1025 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026
1027 return depPaths
1028}
1029
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001030func (mod *Module) InstallInData() bool {
1031 if mod.compiler == nil {
1032 return false
1033 }
1034 return mod.compiler.inData()
1035}
1036
Ivan Lozanoffee3342019-08-27 12:03:00 -07001037func linkPathFromFilePath(filepath android.Path) string {
1038 return strings.Split(filepath.String(), filepath.Base())[0]
1039}
Ivan Lozanod648c432020-02-06 12:05:10 -05001040
Ivan Lozanoffee3342019-08-27 12:03:00 -07001041func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1042 ctx := &depsContext{
1043 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001045
1046 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001047 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001048
Ivan Lozano2b081132020-09-08 12:46:52 -04001049 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001050 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001051 stdLinkage = "rlib-std"
1052 }
1053
1054 rlibDepVariations := commonDepVariations
1055 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1056 rlibDepVariations = append(rlibDepVariations,
1057 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1058 }
1059
Ivan Lozano52767be2019-10-18 14:49:46 -07001060 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001061 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001062 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001063 rlibDepTag, deps.Rlibs...)
1064 actx.AddVariationDependencies(
1065 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001066 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001067 dylibDepTag, deps.Dylibs...)
1068
Ivan Lozano042504f2020-08-18 14:31:23 -04001069 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1070 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001071 if autoDep.depTag == rlibDepTag {
1072 actx.AddVariationDependencies(
1073 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1074 autoDep.depTag, deps.Rustlibs...)
1075 } else {
1076 actx.AddVariationDependencies(
1077 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1078 autoDep.depTag, deps.Rustlibs...)
1079 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001080 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001081 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001082 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001083 actx.AddVariationDependencies(
1084 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1085 rlibDepTag, deps.Stdlibs...)
1086 } else {
1087 actx.AddVariationDependencies(
1088 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1089 dylibDepTag, deps.Stdlibs...)
1090 }
1091 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001092 actx.AddVariationDependencies(append(commonDepVariations,
1093 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001094 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001095 actx.AddVariationDependencies(append(commonDepVariations,
1096 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001097 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001098
Zach Johnson3df4e632020-11-06 11:56:27 -08001099 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1100
Colin Cross565cafd2020-09-25 18:47:38 -07001101 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001102 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001103 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001104 }
1105 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001106 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001107 }
1108
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001109 if mod.sourceProvider != nil {
1110 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1111 bindgen.Properties.Custom_bindgen != "" {
1112 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1113 bindgen.Properties.Custom_bindgen)
1114 }
1115 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001116 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001117 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001118}
1119
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001120func BeginMutator(ctx android.BottomUpMutatorContext) {
1121 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1122 mod.beginMutator(ctx)
1123 }
1124}
1125
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001126func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1127 ctx := &baseModuleContext{
1128 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001129 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001130
1131 mod.begin(ctx)
1132}
1133
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134func (mod *Module) Name() string {
1135 name := mod.ModuleBase.Name()
1136 if p, ok := mod.compiler.(interface {
1137 Name(string) string
1138 }); ok {
1139 name = p.Name(name)
1140 }
1141 return name
1142}
1143
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001144func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001145 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001146 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001147 }
1148}
1149
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001150var _ android.HostToolProvider = (*Module)(nil)
1151
1152func (mod *Module) HostToolPath() android.OptionalPath {
1153 if !mod.Host() {
1154 return android.OptionalPath{}
1155 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001156 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1157 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001158 }
1159 return android.OptionalPath{}
1160}
1161
Jiyong Park99644e92020-11-17 22:21:02 +09001162var _ android.ApexModule = (*Module)(nil)
1163
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001164func (mod *Module) minSdkVersion() string {
1165 return String(mod.Properties.Min_sdk_version)
1166}
1167
Jiyong Park45bf82e2020-12-15 22:29:02 +09001168var _ android.ApexModule = (*Module)(nil)
1169
1170// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001171func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001172 minSdkVersion := mod.minSdkVersion()
1173 if minSdkVersion == "apex_inherit" {
1174 return nil
1175 }
1176 if minSdkVersion == "" {
1177 return fmt.Errorf("min_sdk_version is not specificed")
1178 }
1179
1180 // Not using nativeApiLevelFromUser because the context here is not
1181 // necessarily a native context.
1182 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1183 if err != nil {
1184 return err
1185 }
1186
1187 if ver.GreaterThan(sdkVersion) {
1188 return fmt.Errorf("newer SDK(%v)", ver)
1189 }
Jiyong Park99644e92020-11-17 22:21:02 +09001190 return nil
1191}
1192
Jiyong Park45bf82e2020-12-15 22:29:02 +09001193// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001194func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1195 depTag := ctx.OtherModuleDependencyTag(dep)
1196
1197 if ccm, ok := dep.(*cc.Module); ok {
1198 if ccm.HasStubsVariants() {
1199 if cc.IsSharedDepTag(depTag) {
1200 // dynamic dep to a stubs lib crosses APEX boundary
1201 return false
1202 }
1203 if cc.IsRuntimeDepTag(depTag) {
1204 // runtime dep to a stubs lib also crosses APEX boundary
1205 return false
1206 }
1207
1208 if cc.IsHeaderDepTag(depTag) {
1209 return false
1210 }
1211 }
1212 if mod.Static() && cc.IsSharedDepTag(depTag) {
1213 // shared_lib dependency from a static lib is considered as crossing
1214 // the APEX boundary because the dependency doesn't actually is
1215 // linked; the dependency is used only during the compilation phase.
1216 return false
1217 }
1218 }
1219
1220 if depTag == procMacroDepTag {
1221 return false
1222 }
1223
1224 return true
1225}
1226
1227// Overrides ApexModule.IsInstallabeToApex()
1228func (mod *Module) IsInstallableToApex() bool {
1229 if mod.compiler != nil {
1230 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1231 return true
1232 }
1233 if _, ok := mod.compiler.(*binaryDecorator); ok {
1234 return true
1235 }
1236 }
1237 return false
1238}
1239
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001240// If a library file has a "lib" prefix, extract the library name without the prefix.
1241func libNameFromFilePath(filepath android.Path) (string, bool) {
1242 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1243 if strings.HasPrefix(libName, "lib") {
1244 libName = libName[3:]
1245 return libName, true
1246 }
1247 return "", false
1248}
1249
Ivan Lozanoffee3342019-08-27 12:03:00 -07001250var Bool = proptools.Bool
1251var BoolDefault = proptools.BoolDefault
1252var String = proptools.String
1253var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001254
1255var _ android.OutputFileProducer = (*Module)(nil)