blob: a4c1afd45f2f261d87bac1cdbbe1d8b4f14bd0fb [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 (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "fmt"
Wei Lia1aa2972024-06-21 13:08:51 -070019 "strconv"
Chris Parsons637458d2023-09-19 20:09:00 +000020 "strings"
21
Sasha Smundaka76acba2022-04-18 20:12:56 -070022 "android/soong/bloaty"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090023
Ivan Lozanoffee3342019-08-27 12:03:00 -070024 "github.com/google/blueprint"
Colin Crossa14fb6a2024-10-23 16:57:06 -070025 "github.com/google/blueprint/depset"
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020030 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070031 "android/soong/fuzz"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "android/soong/rust/config"
33)
34
35var pctx = android.NewPackageContext("android/soong/rust")
36
Yu Liu8024b922024-12-20 23:31:32 +000037type LibraryInfo struct {
38 Rlib bool
39 Dylib bool
40}
41
42type CompilerInfo struct {
43 StdLinkageForDevice RustLinkage
44 StdLinkageForNonDevice RustLinkage
45 NoStdlibs bool
46 LibraryInfo *LibraryInfo
47}
48
49type ProtobufDecoratorInfo struct{}
50
51type SourceProviderInfo struct {
52 ProtobufDecoratorInfo *ProtobufDecoratorInfo
53}
54
55type RustInfo struct {
56 AndroidMkSuffix string
57 RustSubName string
58 TransitiveAndroidMkSharedLibs depset.DepSet[string]
59 CompilerInfo *CompilerInfo
60 SnapshotInfo *cc.SnapshotInfo
61 SourceProviderInfo *SourceProviderInfo
62}
63
64var RustInfoProvider = blueprint.NewProvider[*RustInfo]()
65
Ivan Lozanoffee3342019-08-27 12:03:00 -070066func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070067 android.RegisterModuleType("rust_defaults", defaultsFactory)
Colin Cross8a49a3d2024-05-20 12:22:27 -070068 android.PreDepsMutators(registerPreDepsMutators)
69 android.PostDepsMutators(registerPostDepsMutators)
Inseob Kim3b244062023-07-11 13:31:36 +090070 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070071 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020072 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000073 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070074}
75
Colin Cross8a49a3d2024-05-20 12:22:27 -070076func registerPreDepsMutators(ctx android.RegisterMutatorsContext) {
77 ctx.Transition("rust_libraries", &libraryTransitionMutator{})
78 ctx.Transition("rust_stdlinkage", &libstdTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070079 ctx.BottomUp("rust_begin", BeginMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070080}
81
82func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070083 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070084}
85
Ivan Lozanoffee3342019-08-27 12:03:00 -070086type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040087 GlobalRustFlags []string // Flags that apply globally to rust
88 GlobalLinkFlags []string // Flags that apply globally to linker
89 RustFlags []string // Flags that apply to rust
90 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020091 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070092 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070093 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040094 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020095 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070096 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070097}
98
99type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -0500100 AndroidMkRlibs []string `blueprint:"mutated"`
101 AndroidMkDylibs []string `blueprint:"mutated"`
102 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -0500103 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +0000104 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -0400105
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900106 ImageVariation string `blueprint:"mutated"`
107 VndkVersion string `blueprint:"mutated"`
108 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -0500109
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400110 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
111 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
112 // appended before SubName.
113 RustSubName string `blueprint:"mutated"`
114
Ivan Lozano6a884432020-12-02 09:15:16 -0500115 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +0000116 ProductVariantNeeded bool `blueprint:"mutated"`
117 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500118 CoreVariantNeeded bool `blueprint:"mutated"`
119 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -0700120 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -0800121 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500122 ExtraVariants []string `blueprint:"mutated"`
123
Ivan Lozanoa2268632021-07-22 10:52:06 -0400124 // Allows this module to use non-APEX version of libraries. Useful
125 // for building binaries that are started before APEXes are activated.
126 Bootstrap *bool
127
Ivan Lozano1921e802021-05-20 13:39:16 -0400128 // Used by vendor snapshot to record dependencies from snapshot modules.
129 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900130 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400131 SnapshotRlibs []string `blueprint:"mutated"`
132 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400133
Matthew Maurerc6868382021-07-13 14:12:37 -0700134 // Make this module available when building for ramdisk.
135 // On device without a dedicated recovery partition, the module is only
136 // available after switching root into
137 // /first_stage_ramdisk. To expose the module before switching root, install
138 // the recovery variant instead.
139 Ramdisk_available *bool
140
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500141 // Make this module available when building for vendor ramdisk.
142 // On device without a dedicated recovery partition, the module is only
143 // available after switching root into
144 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800145 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500146 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400147
Ivan Lozano1921e802021-05-20 13:39:16 -0400148 // Normally Soong uses the directory structure to decide which modules
149 // should be included (framework) or excluded (non-framework) from the
150 // different snapshots (vendor, recovery, etc.), but this property
151 // allows a partner to exclude a module normally thought of as a
152 // framework module from the vendor snapshot.
153 Exclude_from_vendor_snapshot *bool
154
155 // Normally Soong uses the directory structure to decide which modules
156 // should be included (framework) or excluded (non-framework) from the
157 // different snapshots (vendor, recovery, etc.), but this property
158 // allows a partner to exclude a module normally thought of as a
159 // framework module from the recovery snapshot.
160 Exclude_from_recovery_snapshot *bool
161
Matthew Maurer460ee942021-02-11 12:31:46 -0800162 // Make this module available when building for recovery
163 Recovery_available *bool
164
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500165 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
166 Min_sdk_version *string
167
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900168 HideFromMake bool `blueprint:"mutated"`
169 PreventInstall bool `blueprint:"mutated"`
170
171 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172}
173
174type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700175 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700176
Ivan Lozano6a884432020-12-02 09:15:16 -0500177 VendorProperties cc.VendorProperties
178
Ivan Lozanoffee3342019-08-27 12:03:00 -0700179 Properties BaseProperties
180
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000181 hod android.HostOrDeviceSupported
182 multilib android.Multilib
183 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700184
Ivan Lozano6a884432020-12-02 09:15:16 -0500185 makeLinkType string
186
Yi Kong46c6e592022-01-20 22:55:00 +0800187 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700188 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400189 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200190 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500191 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700192 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400193 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700194 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400195
Ivan Lozano0a468a42024-05-13 21:03:34 -0400196 exportedLinkDirs []string
197
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400198 // Output file to be installed, may be stripped or unstripped.
199 outputFile android.OptionalPath
200
Sasha Smundaka76acba2022-04-18 20:12:56 -0700201 // Cross-reference input file
202 kytheFiles android.Paths
203
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400204 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900205
206 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500207
208 // For apex variants, this is set as apex.min_sdk_version
209 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700210
Colin Crossa14fb6a2024-10-23 16:57:06 -0700211 transitiveAndroidMkSharedLibs depset.DepSet[string]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700212}
213
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500214func (mod *Module) Header() bool {
215 //TODO: If Rust libraries provide header variants, this needs to be updated.
216 return false
217}
218
219func (mod *Module) SetPreventInstall() {
220 mod.Properties.PreventInstall = true
221}
222
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500223func (mod *Module) SetHideFromMake() {
224 mod.Properties.HideFromMake = true
225}
226
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900227func (mod *Module) HiddenFromMake() bool {
228 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400229}
230
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500231func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500232 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
233 // nil since we need compiler to actually sanitize.
234 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500235}
236
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500237func (mod *Module) IsPrebuilt() bool {
238 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
239 return true
240 }
241 return false
242}
243
Ivan Lozano52767be2019-10-18 14:49:46 -0700244func (mod *Module) SelectedStl() string {
245 return ""
246}
247
Ivan Lozano2b262972019-11-21 12:30:50 -0800248func (mod *Module) NonCcVariants() bool {
249 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400250 if library, ok := mod.compiler.(libraryInterface); ok {
251 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800252 }
253 }
254 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
255}
256
Ivan Lozano52767be2019-10-18 14:49:46 -0700257func (mod *Module) Static() bool {
258 if mod.compiler != nil {
259 if library, ok := mod.compiler.(libraryInterface); ok {
260 return library.static()
261 }
262 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400263 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700264}
265
266func (mod *Module) Shared() bool {
267 if mod.compiler != nil {
268 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400269 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700270 }
271 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400272 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700273}
274
Ivan Lozanod7586b62021-04-01 09:49:36 -0400275func (mod *Module) Dylib() bool {
276 if mod.compiler != nil {
277 if library, ok := mod.compiler.(libraryInterface); ok {
278 return library.dylib()
279 }
280 }
281 return false
282}
283
Ivan Lozanod106efe2023-09-21 23:30:26 -0400284func (mod *Module) Source() bool {
285 if mod.compiler != nil {
286 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
287 return library.source()
288 }
289 }
290 return false
291}
292
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400293func (mod *Module) RlibStd() bool {
294 if mod.compiler != nil {
295 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
296 return library.rlibStd()
297 }
298 }
299 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
300}
301
Ivan Lozanod7586b62021-04-01 09:49:36 -0400302func (mod *Module) Rlib() bool {
303 if mod.compiler != nil {
304 if library, ok := mod.compiler.(libraryInterface); ok {
305 return library.rlib()
306 }
307 }
308 return false
309}
310
311func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400312 if binary, ok := mod.compiler.(binaryInterface); ok {
313 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400314 }
315 return false
316}
317
Justin Yun5e035862021-06-29 20:50:37 +0900318func (mod *Module) StaticExecutable() bool {
319 if !mod.Binary() {
320 return false
321 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400322 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900323}
324
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000325func (mod *Module) ApexExclude() bool {
326 if mod.compiler != nil {
327 if library, ok := mod.compiler.(libraryInterface); ok {
328 return library.apexExclude()
329 }
330 }
331 return false
332}
333
Ivan Lozanod7586b62021-04-01 09:49:36 -0400334func (mod *Module) Object() bool {
335 // Rust has no modules which produce only object files.
336 return false
337}
338
Ivan Lozano52767be2019-10-18 14:49:46 -0700339func (mod *Module) Toc() android.OptionalPath {
340 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400341 if lib, ok := mod.compiler.(libraryInterface); ok {
342 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700343 }
344 }
345 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
346}
347
Colin Crossc511bc52020-04-07 16:50:32 +0000348func (mod *Module) UseSdk() bool {
349 return false
350}
351
Ivan Lozanod7586b62021-04-01 09:49:36 -0400352func (mod *Module) RelativeInstallPath() string {
353 if mod.compiler != nil {
354 return mod.compiler.relativeInstallPath()
355 }
356 return ""
357}
358
Ivan Lozano52767be2019-10-18 14:49:46 -0700359func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500360 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700361}
362
Jiyong Park7d55b612021-06-11 17:22:09 +0900363func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400364 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900365}
366
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400367func (mod *Module) SubName() string {
368 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700369}
370
Ivan Lozanof1868af2022-04-12 13:08:36 -0400371func (mod *Module) IsVndkPrebuiltLibrary() bool {
372 // Rust modules do not provide VNDK prebuilts
373 return false
374}
375
376func (mod *Module) IsVendorPublicLibrary() bool {
377 return mod.VendorProperties.IsVendorPublicLibrary
378}
379
380func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
381 // Rust modules to not provide Sdk variants
382 return false
383}
384
Colin Cross127bb8b2020-12-16 16:46:01 -0800385func (c *Module) IsVndkPrivate() bool {
386 return false
387}
388
389func (c *Module) IsLlndk() bool {
390 return false
391}
392
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400393func (mod *Module) KernelHeadersDecorator() bool {
394 return false
395}
396
Colin Cross1f3f1302021-04-26 18:37:44 -0700397func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400398 return false
399}
400
Colin Cross5271fea2021-04-27 13:06:04 -0700401func (m *Module) NeedsVendorPublicLibraryVariants() bool {
402 return false
403}
404
Ivan Lozanod7586b62021-04-01 09:49:36 -0400405func (mod *Module) HasLlndkStubs() bool {
406 return false
407}
408
Ivan Lozano52767be2019-10-18 14:49:46 -0700409func (mod *Module) SdkVersion() string {
410 return ""
411}
412
Colin Crossc511bc52020-04-07 16:50:32 +0000413func (mod *Module) AlwaysSdk() bool {
414 return false
415}
416
Jiyong Park2286afd2020-06-16 21:58:53 +0900417func (mod *Module) IsSdkVariant() bool {
418 return false
419}
420
Colin Cross1348ce32020-10-01 13:37:16 -0700421func (mod *Module) SplitPerApiLevel() bool {
422 return false
423}
424
Sasha Smundaka76acba2022-04-18 20:12:56 -0700425func (mod *Module) XrefRustFiles() android.Paths {
426 return mod.kytheFiles
427}
428
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400430 Dylibs []string
431 Rlibs []string
432 Rustlibs []string
433 Stdlibs []string
434 ProcMacros []string
435 SharedLibs []string
436 StaticLibs []string
437 WholeStaticLibs []string
438 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700439
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400440 // Used for data dependencies adjacent to tests
441 DataLibs []string
442 DataBins []string
443
Colin Crossfe605e12022-01-23 20:46:16 -0800444 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445}
446
447type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700448 DyLibs RustLibraries
449 RLibs RustLibraries
450 SharedLibs android.Paths
451 SharedLibDeps android.Paths
452 StaticLibs android.Paths
453 ProcMacros RustLibraries
454 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000455 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500456
457 // depFlags and depLinkFlags are rustc and linker (clang) flags.
458 depFlags []string
459 depLinkFlags []string
460
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400461 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500462 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000463 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700464 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700465
Ivan Lozano0a468a42024-05-13 21:03:34 -0400466 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
467 // cc_library_static dependants of rlibs.
468 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
469 exportedLinkDirs []string
470
Ivan Lozano45901ed2020-07-24 16:05:01 -0400471 // Used by bindgen modules which call clang
472 depClangFlags []string
473 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400474 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400475 depSystemIncludePaths android.Paths
476
Colin Crossfe605e12022-01-23 20:46:16 -0800477 CrtBegin android.Paths
478 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700479
480 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500481 SrcDeps android.Paths
482 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700483
484 directImplementationDeps android.Paths
485 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700486}
487
488type RustLibraries []RustLibrary
489
490type RustLibrary struct {
491 Path android.Path
492 CrateName string
493}
494
Matthew Maurerbb3add12020-06-25 09:34:12 -0700495type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000496 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700497 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700498}
499
Sasha Smundaka76acba2022-04-18 20:12:56 -0700500type xref interface {
501 XrefRustFiles() android.Paths
502}
503
Matthew Maurerbb3add12020-06-25 09:34:12 -0700504type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000505 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400506 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700507 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700508}
509
Wen-yi Chu41326c12023-09-22 03:58:59 +0000510func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
511 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700512}
513
Colin Cross004bd3f2023-10-02 11:39:17 -0700514func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
515 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400516}
517
Colin Cross0de8a1e2020-09-18 14:15:30 -0700518func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800519 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700520 LinkDirs: flagExporter.linkDirs,
521 LinkObjects: flagExporter.linkObjects,
522 })
523}
524
Matthew Maurerbb3add12020-06-25 09:34:12 -0700525var _ exportedFlagsProducer = (*flagExporter)(nil)
526
527func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700528 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700529}
530
Colin Cross0de8a1e2020-09-18 14:15:30 -0700531type FlagExporterInfo struct {
532 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000533 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700534 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700535}
536
Colin Crossbc7d76c2023-12-12 16:39:03 -0800537var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700538
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400539func (mod *Module) isCoverageVariant() bool {
540 return mod.coverage.Properties.IsCoverageVariant
541}
542
543var _ cc.Coverage = (*Module)(nil)
544
Colin Crosse1a85552024-06-14 12:17:37 -0700545func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400546 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
547}
548
Ivan Lozanod7586b62021-04-01 09:49:36 -0400549func (mod *Module) VndkVersion() string {
550 return mod.Properties.VndkVersion
551}
552
Ivan Lozano0a468a42024-05-13 21:03:34 -0400553func (mod *Module) ExportedCrateLinkDirs() []string {
554 return mod.exportedLinkDirs
555}
556
Ivan Lozanod7586b62021-04-01 09:49:36 -0400557func (mod *Module) PreventInstall() bool {
558 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400559}
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000560func (c *Module) ForceDisableSanitizers() {
561 c.sanitize.Properties.ForceDisable = true
562}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400563
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400564func (mod *Module) MarkAsCoverageVariant(coverage bool) {
565 mod.coverage.Properties.IsCoverageVariant = coverage
566}
567
568func (mod *Module) EnableCoverageIfNeeded() {
569 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700570}
571
572func defaultsFactory() android.Module {
573 return DefaultsFactory()
574}
575
576type Defaults struct {
577 android.ModuleBase
578 android.DefaultsModuleBase
579}
580
581func DefaultsFactory(props ...interface{}) android.Module {
582 module := &Defaults{}
583
584 module.AddProperties(props...)
585 module.AddProperties(
586 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800587 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500588 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100589 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400590 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700591 &BaseCompilerProperties{},
592 &BinaryCompilerProperties{},
593 &LibraryCompilerProperties{},
594 &ProcMacroCompilerProperties{},
595 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400596 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700597 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400598 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400599 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200600 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500601 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000602 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700603 )
604
605 android.InitDefaultsModule(module)
606 return module
607}
608
609func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700610 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611}
612
Ivan Lozano183a3212019-10-18 14:18:45 -0700613func (mod *Module) CcLibrary() bool {
614 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500615 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700616 return true
617 }
618 }
619 return false
620}
621
622func (mod *Module) CcLibraryInterface() bool {
623 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400624 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
625 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000626 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700627 return true
628 }
629 }
630 return false
631}
632
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400633func (mod *Module) RustLibraryInterface() bool {
634 if mod.compiler != nil {
635 if _, ok := mod.compiler.(libraryInterface); ok {
636 return true
637 }
638 }
639 return false
640}
641
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500642func (mod *Module) IsFuzzModule() bool {
643 if _, ok := mod.compiler.(*fuzzDecorator); ok {
644 return true
645 }
646 return false
647}
648
649func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
650 return mod.FuzzModule
651}
652
653func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
654 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
655 return fuzzer.fuzzPackagedModule
656 }
657 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
658}
659
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000660func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500661 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
662 return fuzzer.sharedLibraries
663 }
664 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
665}
666
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400667func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400668 if mod.compiler != nil {
669 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400670 }
671 return nil
672}
673
Ivan Lozano183a3212019-10-18 14:18:45 -0700674func (mod *Module) SetStatic() {
675 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700676 if library, ok := mod.compiler.(libraryInterface); ok {
677 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700678 return
679 }
680 }
681 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
682}
683
684func (mod *Module) SetShared() {
685 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700686 if library, ok := mod.compiler.(libraryInterface); ok {
687 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700688 return
689 }
690 }
691 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
692}
693
Ivan Lozano183a3212019-10-18 14:18:45 -0700694func (mod *Module) BuildStaticVariant() bool {
695 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700696 if library, ok := mod.compiler.(libraryInterface); ok {
697 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700698 }
699 }
700 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
701}
702
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400703func (mod *Module) BuildRlibVariant() bool {
704 if mod.compiler != nil {
705 if library, ok := mod.compiler.(libraryInterface); ok {
706 return library.buildRlib()
707 }
708 }
709 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
710}
711
Ivan Lozano183a3212019-10-18 14:18:45 -0700712func (mod *Module) BuildSharedVariant() bool {
713 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700714 if library, ok := mod.compiler.(libraryInterface); ok {
715 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700716 }
717 }
718 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
719}
720
Ivan Lozano183a3212019-10-18 14:18:45 -0700721func (mod *Module) Module() android.Module {
722 return mod
723}
724
Ivan Lozano183a3212019-10-18 14:18:45 -0700725func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400726 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700727}
728
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400729func (mod *Module) CoverageFiles() android.Paths {
730 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800731 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400732 }
733 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
734}
735
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400736// Rust does not produce gcno files, and therefore does not produce a coverage archive.
737func (mod *Module) CoverageOutputFile() android.OptionalPath {
738 return android.OptionalPath{}
739}
740
741func (mod *Module) IsNdk(config android.Config) bool {
742 return false
743}
744
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000745func (mod *Module) IsStubs() bool {
746 return false
747}
748
Spandan Das10c41362024-12-03 01:33:09 +0000749func (mod *Module) HasStubsVariants() bool {
750 return false
751}
752
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000753func (mod *Module) ApexSdkVersion() android.ApiLevel {
754 panic(fmt.Errorf("ApexSdkVersion called on unsupported Rust module: %q", mod.BaseModuleName()))
755}
756
757func (mod *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
758 return mod.Name()
759}
760
761func (mod *Module) Multilib() string {
762 return mod.Arch().ArchType.Multilib
763}
764
765func (mod *Module) IsCrt() bool {
766 // Rust does not currently provide any crt modules.
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400767 return false
768}
769
Jiyong Park459feca2020-12-15 11:02:21 +0900770func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900771 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900772 return false
773 }
774
Jiyong Park459feca2020-12-15 11:02:21 +0900775 // The apex variant is not installable because it is included in the APEX and won't appear
776 // in the system partition as a standalone file.
777 if !apexInfo.IsForPlatform() {
778 return false
779 }
780
Jiyong Parke54f07e2021-04-07 15:08:04 +0900781 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900782}
783
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500784func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800785 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
786 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500787}
788
Ivan Lozano183a3212019-10-18 14:18:45 -0700789var _ cc.LinkableInterface = (*Module)(nil)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000790var _ cc.VersionedLinkableInterface = (*Module)(nil)
Ivan Lozano183a3212019-10-18 14:18:45 -0700791
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792func (mod *Module) Init() android.Module {
793 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500794 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795
Yi Kong46c6e592022-01-20 22:55:00 +0800796 if mod.afdo != nil {
797 mod.AddProperties(mod.afdo.props()...)
798 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 if mod.compiler != nil {
800 mod.AddProperties(mod.compiler.compilerProps()...)
801 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400802 if mod.coverage != nil {
803 mod.AddProperties(mod.coverage.props()...)
804 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200805 if mod.clippy != nil {
806 mod.AddProperties(mod.clippy.props()...)
807 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400808 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700809 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400810 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500811 if mod.sanitize != nil {
812 mod.AddProperties(mod.sanitize.props()...)
813 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400814
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900816 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817
818 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 return mod
820}
821
822func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
823 return &Module{
824 hod: hod,
825 multilib: multilib,
826 }
827}
828func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
829 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800830 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400831 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200832 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500833 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834 return module
835}
836
837type ModuleContext interface {
838 android.ModuleContext
839 ModuleContextIntf
840}
841
842type BaseModuleContext interface {
843 android.BaseModuleContext
844 ModuleContextIntf
845}
846
847type DepsContext interface {
848 android.BottomUpMutatorContext
849 ModuleContextIntf
850}
851
852type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200853 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855}
856
857type depsContext struct {
858 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859}
860
861type moduleContext struct {
862 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863}
864
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200865type baseModuleContext struct {
866 android.BaseModuleContext
867}
868
869func (ctx *moduleContext) RustModule() *Module {
870 return ctx.Module().(*Module)
871}
872
873func (ctx *moduleContext) toolchain() config.Toolchain {
874 return ctx.RustModule().toolchain(ctx)
875}
876
877func (ctx *depsContext) RustModule() *Module {
878 return ctx.Module().(*Module)
879}
880
881func (ctx *depsContext) toolchain() config.Toolchain {
882 return ctx.RustModule().toolchain(ctx)
883}
884
885func (ctx *baseModuleContext) RustModule() *Module {
886 return ctx.Module().(*Module)
887}
888
889func (ctx *baseModuleContext) toolchain() config.Toolchain {
890 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400891}
892
893func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700894 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
895 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
896 return false
897 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400898 return mod.compiler != nil && mod.compiler.nativeCoverage()
899}
900
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000901func (mod *Module) SetStl(s string) {
902 // STL is a CC concept; do nothing for Rust
903}
904
905func (mod *Module) SetSdkVersion(s string) {
906 panic(fmt.Errorf("SetSdkVersion called on unsupported Rust module: %q", mod.BaseModuleName()))
907}
908
909func (mod *Module) SetMinSdkVersion(s string) {
910 mod.Properties.Min_sdk_version = StringPtr(s)
911}
912
913func (mod *Module) VersionedInterface() cc.VersionedInterface {
914 if _, ok := mod.compiler.(cc.VersionedInterface); ok {
915 return mod.compiler.(cc.VersionedInterface)
916 }
917 return nil
918}
919
Ivan Lozanod7586b62021-04-01 09:49:36 -0400920func (mod *Module) EverInstallable() bool {
921 return mod.compiler != nil &&
922 // Check to see whether the module is actually ever installable.
923 mod.compiler.everInstallable()
924}
925
926func (mod *Module) Installable() *bool {
927 return mod.Properties.Installable
928}
929
Ivan Lozano872d5792022-03-23 17:31:39 -0400930func (mod *Module) ProcMacro() bool {
931 if pm, ok := mod.compiler.(procMacroInterface); ok {
932 return pm.ProcMacro()
933 }
934 return false
935}
936
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
938 if mod.cachedToolchain == nil {
939 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
940 }
941 return mod.cachedToolchain
942}
943
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200944func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
945 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
946}
947
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
949}
950
951func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
952 ctx := &moduleContext{
953 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700954 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955
Colin Crossff694a82023-12-13 15:54:49 -0800956 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900957 if !apexInfo.IsForPlatform() {
958 mod.hideApexVariantFromMake = true
959 }
960
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500962 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
963
Ivan Lozanof1868af2022-04-12 13:08:36 -0400964 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700965
Ivan Lozanoffee3342019-08-27 12:03:00 -0700966 if !toolchain.Supported() {
967 // This toolchain's unsupported, there's nothing to do for this mod.
968 return
969 }
970
971 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400972 // Export linkDirs for CC rust generatedlibs
973 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
974 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
975
Ivan Lozanoffee3342019-08-27 12:03:00 -0700976 flags := Flags{
977 Toolchain: toolchain,
978 }
979
Ivan Lozano67eada32021-09-23 11:50:33 -0400980 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800981 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500982 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800983 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700984 if mod.compiler != nil {
985 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400986 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +0000987 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400988 }
989 if mod.coverage != nil {
990 flags, deps = mod.coverage.flags(ctx, flags, deps)
991 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200992 if mod.clippy != nil {
993 flags, deps = mod.clippy.flags(ctx, flags, deps)
994 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500995 if mod.sanitize != nil {
996 flags, deps = mod.sanitize.flags(ctx, flags, deps)
997 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400998
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200999 // SourceProvider needs to call GenerateSource() before compiler calls
1000 // compile() so it can provide the source. A SourceProvider has
1001 // multiple variants (e.g. source, rlib, dylib). Only the "source"
1002 // variant is responsible for effectively generating the source. The
1003 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001004 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001005 if mod.compiler.(libraryInterface).source() {
1006 mod.sourceProvider.GenerateSource(ctx, deps)
1007 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1008 } else {
1009 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
1010 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -07001011 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001012 }
Colin Crossa6182ab2024-08-21 10:47:44 -07001013 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001014 }
1015
1016 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001017 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001018 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001019 if ctx.Failed() {
1020 return
1021 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001022 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -07001023 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001024 if buildOutput.kytheFile != nil {
1025 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1026 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001027 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001028
Dan Albert06feee92021-03-19 15:06:02 -07001029 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
1030
Colin Crossff694a82023-12-13 15:54:49 -08001031 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -04001032 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001033 // If the module has been specifically configure to not be installed then
1034 // hide from make as otherwise it will break when running inside make as the
1035 // output path to install will not be specified. Not all uninstallable
1036 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001037 // side dependencies. In particular, proc-macros need to be captured in the
1038 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001039 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001040 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001041 } else if !mod.installable(apexInfo) {
1042 mod.SkipInstall()
1043 }
1044
1045 // Still call install though, the installs will be stored as PackageSpecs to allow
1046 // using the outputs in a genrule.
1047 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001048 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001049 if ctx.Failed() {
1050 return
1051 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001052 // Export your own directory as a linkDir
1053 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1054
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001055 }
Chris Wailes74be7642021-07-22 16:20:28 -07001056
Colin Crossb614cd42024-10-11 12:52:21 -07001057 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1058 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1059 })
1060
Chris Wailes74be7642021-07-22 16:20:28 -07001061 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001062 }
Wei Lia1aa2972024-06-21 13:08:51 -07001063
Yu Liu8024b922024-12-20 23:31:32 +00001064 linkableInfo := cc.CreateCommonLinkableInfo(mod)
1065 linkableInfo.Static = mod.Static()
1066 linkableInfo.Shared = mod.Shared()
1067 linkableInfo.CrateName = mod.CrateName()
1068 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
1069 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1070
1071 rustInfo := &RustInfo{
1072 AndroidMkSuffix: mod.AndroidMkSuffix(),
1073 RustSubName: mod.Properties.RustSubName,
1074 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
1075 }
1076 if mod.compiler != nil {
1077 rustInfo.CompilerInfo = &CompilerInfo{
1078 NoStdlibs: mod.compiler.noStdlibs(),
1079 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1080 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1081 }
1082 if lib, ok := mod.compiler.(libraryInterface); ok {
1083 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1084 Dylib: lib.dylib(),
1085 Rlib: lib.rlib(),
1086 }
1087 }
1088 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1089 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1090 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1091 }
1092 }
1093 }
1094 if mod.sourceProvider != nil {
1095 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1096 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1097 ProtobufDecoratorInfo: &ProtobufDecoratorInfo{},
1098 }
1099 }
1100 }
1101 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001102
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001103 ccInfo := &cc.CcInfo{
1104 IsPrebuilt: mod.IsPrebuilt(),
1105 }
1106
1107 android.SetProvider(ctx, cc.CcInfoProvider, ccInfo)
1108
mrziwang0cbd3b02024-06-20 16:39:25 -07001109 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001110
1111 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001112}
1113
1114func (mod *Module) setOutputFiles(ctx ModuleContext) {
1115 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1116 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1117 } else if mod.OutputFile().Valid() {
1118 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1119 } else {
1120 ctx.SetOutputFiles(android.Paths{}, "")
1121 }
1122 if mod.compiler != nil {
1123 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1124 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001125}
1126
Wei Lia1aa2972024-06-21 13:08:51 -07001127func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1128 // Dump metadata that can not be done in android/compliance-metadata.go
1129 metadataInfo := ctx.ComplianceMetadataInfo()
1130 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1131 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1132
1133 // Static libs
1134 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1135 staticDepNames := make([]string, 0, len(staticDeps))
1136 for _, dep := range staticDeps {
1137 staticDepNames = append(staticDepNames, dep.Name())
1138 }
1139 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1140 for _, dep := range ccStaticDeps {
1141 staticDepNames = append(staticDepNames, dep.Name())
1142 }
1143
1144 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1145 // C static libraries
1146 for _, dep := range deps.StaticLibs {
1147 staticDepPaths = append(staticDepPaths, dep.String())
1148 }
1149 // Rust static libraries
1150 for _, dep := range deps.RLibs {
1151 staticDepPaths = append(staticDepPaths, dep.Path.String())
1152 }
1153 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1154 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1155
1156 // C Whole static libs
1157 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1158 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1159 for _, dep := range ccStaticDeps {
1160 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1161 }
1162 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1163}
1164
Ivan Lozanoffee3342019-08-27 12:03:00 -07001165func (mod *Module) deps(ctx DepsContext) Deps {
1166 deps := Deps{}
1167
1168 if mod.compiler != nil {
1169 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001170 }
1171 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001172 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001173 }
1174
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001175 if mod.coverage != nil {
1176 deps = mod.coverage.deps(ctx, deps)
1177 }
1178
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001179 if mod.sanitize != nil {
1180 deps = mod.sanitize.deps(ctx, deps)
1181 }
1182
Ivan Lozanoffee3342019-08-27 12:03:00 -07001183 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1184 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001185 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001186 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1187 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1188 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001189 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001190 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001191 return deps
1192
1193}
1194
Ivan Lozanoffee3342019-08-27 12:03:00 -07001195type dependencyTag struct {
1196 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001197 name string
1198 library bool
1199 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001200 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001201}
1202
Jiyong Park65b62242020-11-25 12:44:59 +09001203// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1204// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1205func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001206 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001207}
1208
1209var _ android.InstallNeededDependencyTag = dependencyTag{}
1210
Colin Cross65cb3142021-12-10 23:05:02 +00001211func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1212 if d.library && d.dynamic {
1213 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1214 }
1215 return nil
1216}
1217
Yu Liuc8884602024-03-15 18:48:38 +00001218func (d dependencyTag) PropagateAconfigValidation() bool {
1219 return d == rlibDepTag || d == sourceDepTag
1220}
1221
1222var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1223
Colin Cross65cb3142021-12-10 23:05:02 +00001224var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1225
Ivan Lozanoffee3342019-08-27 12:03:00 -07001226var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001227 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1228 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001229 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001230 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001231 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001232 dataLibDepTag = dependencyTag{name: "data lib"}
1233 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001234)
1235
Jiyong Park99644e92020-11-17 22:21:02 +09001236func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1237 tag, ok := depTag.(dependencyTag)
1238 return ok && tag == dylibDepTag
1239}
1240
Jiyong Park94e22fd2021-04-08 18:19:15 +09001241func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1242 tag, ok := depTag.(dependencyTag)
1243 return ok && tag == rlibDepTag
1244}
1245
Matthew Maurer0f003b12020-06-29 14:34:06 -07001246type autoDep struct {
1247 variation string
1248 depTag dependencyTag
1249}
1250
1251var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001252 sourceVariation = "source"
1253 rlibVariation = "rlib"
1254 dylibVariation = "dylib"
1255 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1256 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001257)
1258
1259type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001260 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001261}
1262
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001263func (mod *Module) begin(ctx BaseModuleContext) {
1264 if mod.coverage != nil {
1265 mod.coverage.begin(ctx)
1266 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001267 if mod.sanitize != nil {
1268 mod.sanitize.begin(ctx)
1269 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001270}
1271
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001272func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001273 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001274 return p.prebuilt()
1275 }
1276 return nil
1277}
1278
Kiyoung Kim37693d02024-04-04 09:56:15 +09001279func (mod *Module) Symlinks() []string {
1280 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1281 return nil
1282}
1283
Yu Liu8024b922024-12-20 23:31:32 +00001284func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1285 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001286 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001287 if rustInfo.SnapshotInfo != nil {
1288 baseName := linkableInfo.BaseModuleName
1289 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001290 }
1291 }
Yu Liu8024b922024-12-20 23:31:32 +00001292 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001293}
1294
Yu Liu8024b922024-12-20 23:31:32 +00001295func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001296 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001297 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1298 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001299 }
1300 }
1301}
Andrew Walbran52533232024-03-19 11:36:04 +00001302
Ivan Lozanoffee3342019-08-27 12:03:00 -07001303func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1304 var depPaths PathDeps
1305
Yu Liu8024b922024-12-20 23:31:32 +00001306 directRlibDeps := []*cc.LinkableInfo{}
1307 directDylibDeps := []*cc.LinkableInfo{}
1308 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001309 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001310 directStaticLibDeps := [](*cc.LinkableInfo){}
1311 directSrcProvidersDeps := []*android.ModuleProxy{}
1312 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001313
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001314 // For the dependency from platform to apex, use the latest stubs
1315 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001316 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001317 if !apexInfo.IsForPlatform() {
1318 mod.apexSdkVersion = apexInfo.MinSdkVersion
1319 }
1320
1321 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1322 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1323 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1324 // (b/144430859)
1325 mod.apexSdkVersion = android.FutureApiLevel
1326 }
1327
Spandan Das604f3762023-03-16 22:51:40 +00001328 skipModuleList := map[string]bool{}
1329
Colin Crossa14fb6a2024-10-23 16:57:06 -07001330 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001331 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001332
Yu Liu8024b922024-12-20 23:31:32 +00001333 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001334 depName := ctx.OtherModuleName(dep)
1335 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001336 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1337
Spandan Das604f3762023-03-16 22:51:40 +00001338 if _, exists := skipModuleList[depName]; exists {
1339 return
1340 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001341
1342 if depTag == android.DarwinUniversalVariantTag {
1343 return
1344 }
1345
Yu Liu8024b922024-12-20 23:31:32 +00001346 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1347 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1348 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1349 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1350 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001351 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001352 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001353
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001354 switch {
1355 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001356 dylib := rustInfo.CompilerInfo.LibraryInfo
1357 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001358 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1359 return
1360 }
Yu Liu8024b922024-12-20 23:31:32 +00001361 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001362 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001363 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1364
Colin Crossb614cd42024-10-11 12:52:21 -07001365 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1366 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1367 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1368 }
1369
Yu Liu8024b922024-12-20 23:31:32 +00001370 if !rustInfo.CompilerInfo.NoStdlibs {
1371 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1372 if ctx.Device() {
1373 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1374 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001375 if rustDepStdLinkage != modStdLinkage {
1376 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1377 return
1378 }
1379 }
1380
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001381 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001382 rlib := rustInfo.CompilerInfo.LibraryInfo
1383 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001384 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001385 return
1386 }
Yu Liu8024b922024-12-20 23:31:32 +00001387 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001388 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001389 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1390
Ivan Lozano0a468a42024-05-13 21:03:34 -04001391 // rust_ffi rlibs may export include dirs, so collect those here.
1392 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1393 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001394 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001395
Colin Crossb614cd42024-10-11 12:52:21 -07001396 // rlibs are not installed, so don't add the output file to directImplementationDeps
1397 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1398 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1399 }
1400
Yu Liu8024b922024-12-20 23:31:32 +00001401 if !rustInfo.CompilerInfo.NoStdlibs {
1402 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1403 if ctx.Device() {
1404 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1405 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001406 if rustDepStdLinkage != modStdLinkage {
1407 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1408 return
1409 }
1410 }
1411
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001412 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001413 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001414 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001415 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001416 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001417
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001418 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001419 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001420 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001421 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001422 case cc.IsStaticDepTag(depTag):
1423 // Rust FFI rlibs should not be declared in a Rust modules
1424 // "static_libs" list as we can't handle them properly at the
1425 // moment (for example, they only produce an rlib-std variant).
1426 // Instead, a normal rust_library variant should be used.
1427 ctx.PropertyErrorf("static_libs",
1428 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1429 depName)
1430
Paul Duffind5cf92e2021-07-09 17:38:55 +01001431 }
1432
Yu Liu8024b922024-12-20 23:31:32 +00001433 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001434
Paul Duffind5cf92e2021-07-09 17:38:55 +01001435 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001436 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1437 // OS/Arch variant is used.
1438 var helper string
1439 if ctx.Host() {
1440 helper = "missing 'host_supported'?"
1441 } else {
1442 helper = "device module defined?"
1443 }
1444
Yu Liu8024b922024-12-20 23:31:32 +00001445 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001446 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1447 return
Yu Liu8024b922024-12-20 23:31:32 +00001448 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001449 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1450 return
1451 }
Yu Liu8024b922024-12-20 23:31:32 +00001452 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001453 }
1454
Ivan Lozano0a468a42024-05-13 21:03:34 -04001455 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001456 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001457 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001458 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1459 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001460 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001461 }
1462
Ivan Lozanoffee3342019-08-27 12:03:00 -07001463 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001464 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001465 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001466 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001467 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001468 }
1469 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001470
Ivan Lozanod106efe2023-09-21 23:30:26 -04001471 if depTag == sourceDepTag {
1472 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001473 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001474 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001475 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1476 }
1477 }
1478 }
Yu Liu8024b922024-12-20 23:31:32 +00001479 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001480 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001481 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1482 if !hasRustInfo {
1483 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001484 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1485 return
1486 }
Yu Liu8024b922024-12-20 23:31:32 +00001487 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001488 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1489 return
1490 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001491 }
Yu Liu8024b922024-12-20 23:31:32 +00001492 linkObject := linkableInfo.OutputFile
Ivan Lozano2093af22020-08-25 12:48:19 -04001493 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001494 if !ctx.Config().AllowMissingDependencies() {
1495 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1496 } else {
1497 ctx.AddMissingDependencies([]string{depName})
1498 }
1499 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001500 }
1501
Wen-yi Chu41326c12023-09-22 03:58:59 +00001502 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001503
Ivan Lozanoffee3342019-08-27 12:03:00 -07001504 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001505 switch {
1506 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001507 if cc.IsWholeStaticLib(depTag) {
1508 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1509 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001510 if mod.Binary() {
1511 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1512 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1513 // final linkage, pass the args directly to the linker to handle these cases.
1514 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1515 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001516 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001517 } else {
1518 ctx.ModuleErrorf("'%q' cannot be listed as a whole_static_library in Rust modules unless the output is prefixed by 'lib'", depName, ctx.ModuleName())
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001519 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001520 }
1521
1522 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1523 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001524 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001525 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1526
Colin Cross313aa542023-12-13 13:47:44 -08001527 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001528 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1529 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1530 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1531 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Yu Liu8024b922024-12-20 23:31:32 +00001532 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001533
1534 // Record baseLibName for snapshots.
1535 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1536
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001537 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001538 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001539 // For the shared lib dependencies, we may link to the stub variant
1540 // of the dependency depending on the context (e.g. if this
1541 // dependency crosses the APEX boundaries).
1542 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1543
Colin Crossb614cd42024-10-11 12:52:21 -07001544 if !sharedLibraryInfo.IsStubs {
1545 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1546 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1547 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1548 }
1549 }
1550
Jiyong Park7d55b612021-06-11 17:22:09 +09001551 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1552 // object (either from stub or non-stub) to use.
1553 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001554 if !linkObject.Valid() {
1555 if !ctx.Config().AllowMissingDependencies() {
1556 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1557 } else {
1558 ctx.AddMissingDependencies([]string{depName})
1559 }
1560 return
1561 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001562 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001563
Ivan Lozanoffee3342019-08-27 12:03:00 -07001564 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001565 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001566 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1567 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1568 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1569 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001570 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001571
1572 // Record baseLibName for snapshots.
1573 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1574
Cole Faustb6e6f992023-08-17 17:42:26 -07001575 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001576 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001577 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001578 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001579 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1580 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1581 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001582 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001583 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001584 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001585 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001586 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001587 }
1588
1589 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001590 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1591 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001592 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001593 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001594 } else {
1595 switch {
1596 case depTag == cc.CrtBeginDepTag:
1597 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1598 case depTag == cc.CrtEndDepTag:
1599 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1600 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001601 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001602
Yu Liu8024b922024-12-20 23:31:32 +00001603 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoKey); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001604 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001605 // These are usually genrules which don't have per-target variants.
1606 directSrcDeps = append(directSrcDeps, srcDep)
1607 }
1608 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001609 })
1610
Colin Crossa14fb6a2024-10-23 16:57:06 -07001611 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001612
1613 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001614 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001615 for _, dep := range directRlibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001616 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001617 if alias, aliased := aliases[crateName]; aliased {
1618 crateName = alias
1619 }
Yu Liu8024b922024-12-20 23:31:32 +00001620 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001621 }
1622 var dylibDepFiles RustLibraries
1623 for _, dep := range directDylibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001624 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001625 if alias, aliased := aliases[crateName]; aliased {
1626 crateName = alias
1627 }
Yu Liu8024b922024-12-20 23:31:32 +00001628 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001629 }
1630 var procMacroDepFiles RustLibraries
1631 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001632 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001633 if alias, aliased := aliases[crateName]; aliased {
1634 crateName = alias
1635 }
Yu Liu8024b922024-12-20 23:31:32 +00001636 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001637 }
1638
Colin Cross004bd3f2023-10-02 11:39:17 -07001639 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001640 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001641 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001642 }
1643
Colin Cross004bd3f2023-10-02 11:39:17 -07001644 var sharedLibFiles android.Paths
1645 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001646 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001647 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001648 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001649 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001650 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001651 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001652 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001653 }
1654
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001655 var srcProviderDepFiles android.Paths
1656 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001657 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001658 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1659 }
1660 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001661 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001662 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1663 }
1664
Wen-yi Chu41326c12023-09-22 03:58:59 +00001665 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1666 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001667 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1668 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1669 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001670 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001671 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001672
1673 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001674 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001675 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001676 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001677 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1678 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1679 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001680
1681 return depPaths
1682}
1683
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001684func (mod *Module) InstallInData() bool {
1685 if mod.compiler == nil {
1686 return false
1687 }
1688 return mod.compiler.inData()
1689}
1690
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001691func (mod *Module) InstallInRamdisk() bool {
1692 return mod.InRamdisk()
1693}
1694
1695func (mod *Module) InstallInVendorRamdisk() bool {
1696 return mod.InVendorRamdisk()
1697}
1698
1699func (mod *Module) InstallInRecovery() bool {
1700 return mod.InRecovery()
1701}
1702
Wen-yi Chu41326c12023-09-22 03:58:59 +00001703func linkPathFromFilePath(filepath android.Path) string {
1704 return strings.Split(filepath.String(), filepath.Base())[0]
1705}
1706
Spandan Das604f3762023-03-16 22:51:40 +00001707// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1708func (r *Module) usePublicApi() bool {
1709 return r.Device() && r.UseSdk()
1710}
1711
1712// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1713func (r *Module) useVendorApi() bool {
1714 return r.Device() && (r.InVendor() || r.InProduct())
1715}
1716
Ivan Lozanoffee3342019-08-27 12:03:00 -07001717func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1718 ctx := &depsContext{
1719 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001720 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001721
1722 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001723 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001724
1725 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001726 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001727 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001728
Ivan Lozano2b081132020-09-08 12:46:52 -04001729 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001730 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001731 stdLinkage = "rlib-std"
1732 }
1733
1734 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001735
Ivan Lozano2b081132020-09-08 12:46:52 -04001736 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1737 rlibDepVariations = append(rlibDepVariations,
1738 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1739 }
1740
Ivan Lozano1921e802021-05-20 13:39:16 -04001741 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001742 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001743 for _, lib := range deps.Rlibs {
1744 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001745 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001746 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001747
1748 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001749 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001750
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001751 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001752 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001753 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001754
Ivan Lozano1921e802021-05-20 13:39:16 -04001755 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001756 if deps.Rustlibs != nil {
1757 if !mod.compiler.Disabled() {
1758 for _, lib := range deps.Rustlibs {
1759 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1760 if autoDep.depTag == rlibDepTag {
1761 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001762 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1763
Ivan Lozanod106efe2023-09-21 23:30:26 -04001764 } else {
1765 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1766 // Check for the existence of the dylib deptag variant. Select it if available,
1767 // otherwise select the rlib variant.
1768 autoDepVariations := append(commonDepVariations,
1769 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001770 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1771 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001772
Ivan Lozanod106efe2023-09-21 23:30:26 -04001773 } else {
1774 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001775 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1776
Ivan Lozanod106efe2023-09-21 23:30:26 -04001777 }
1778 }
1779 }
1780 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1781 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001782 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001783 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001784
Ivan Lozano0a468a42024-05-13 21:03:34 -04001785 // Only add rustlib dependencies if they're source providers themselves.
1786 // This is used to track which crate names need to be added to the source generated
1787 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001788 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001789 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001790 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001791 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001792 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001793 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001794
Ivan Lozano1921e802021-05-20 13:39:16 -04001795 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001796 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001797 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001798 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001799 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001800 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001801 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001802 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001803 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001804 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1805
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001806 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001807 }
1808 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001809
1810 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001811 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001812 name, version := cc.StubsLibNameAndVersion(lib)
1813
1814 variations := []blueprint.Variation{
1815 {Mutator: "link", Variation: "shared"},
1816 }
Spandan Dasff665182024-09-11 18:48:44 +00001817 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001818 }
1819
1820 for _, lib := range deps.WholeStaticLibs {
1821 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001822
1823 actx.AddVariationDependencies([]blueprint.Variation{
1824 {Mutator: "link", Variation: "static"},
1825 }, depTag, lib)
1826 }
1827
1828 for _, lib := range deps.StaticLibs {
1829 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001830
1831 actx.AddVariationDependencies([]blueprint.Variation{
1832 {Mutator: "link", Variation: "static"},
1833 }, depTag, lib)
1834 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001835
Zach Johnson3df4e632020-11-06 11:56:27 -08001836 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1837
Colin Cross565cafd2020-09-25 18:47:38 -07001838 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001839 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001840 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001841 }
Colin Crossfe605e12022-01-23 20:46:16 -08001842 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001843 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001844 }
1845
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001846 if mod.sourceProvider != nil {
1847 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1848 bindgen.Properties.Custom_bindgen != "" {
1849 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1850 bindgen.Properties.Custom_bindgen)
1851 }
1852 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001853
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001854 actx.AddVariationDependencies([]blueprint.Variation{
1855 {Mutator: "link", Variation: "shared"},
1856 }, dataLibDepTag, deps.DataLibs...)
1857
1858 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1859
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001860 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001861 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001862
1863 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001864}
1865
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001866func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001867 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001868 mod.beginMutator(ctx)
1869 }
1870}
1871
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001872func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1873 ctx := &baseModuleContext{
1874 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001875 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001876
1877 mod.begin(ctx)
1878}
1879
Ivan Lozanoffee3342019-08-27 12:03:00 -07001880func (mod *Module) Name() string {
1881 name := mod.ModuleBase.Name()
1882 if p, ok := mod.compiler.(interface {
1883 Name(string) string
1884 }); ok {
1885 name = p.Name(name)
1886 }
1887 return name
1888}
1889
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001890func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001891 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001892 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001893 }
1894}
1895
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001896var _ android.HostToolProvider = (*Module)(nil)
1897
1898func (mod *Module) HostToolPath() android.OptionalPath {
1899 if !mod.Host() {
1900 return android.OptionalPath{}
1901 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001902 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1903 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001904 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1905 // Even though proc-macros aren't strictly "tools", since they target the compiler
1906 // and act as compiler plugins, we treat them similarly.
1907 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001908 }
1909 return android.OptionalPath{}
1910}
1911
Jiyong Park99644e92020-11-17 22:21:02 +09001912var _ android.ApexModule = (*Module)(nil)
1913
Ivan Lozano24cf0362024-10-04 16:02:38 +00001914// If a module is marked for exclusion from apexes, don't provide apex variants.
1915// TODO(b/362509506): remove this once stubs are properly supported by rust_ffi targets.
1916func (m *Module) CanHaveApexVariants() bool {
1917 if m.ApexExclude() {
1918 return false
1919 } else {
1920 return m.ApexModuleBase.CanHaveApexVariants()
1921 }
1922}
1923
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001924func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001925 return String(mod.Properties.Min_sdk_version)
1926}
1927
Jiyong Park45bf82e2020-12-15 22:29:02 +09001928// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001929func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001930 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001931 if minSdkVersion == "apex_inherit" {
1932 return nil
1933 }
1934 if minSdkVersion == "" {
1935 return fmt.Errorf("min_sdk_version is not specificed")
1936 }
1937
1938 // Not using nativeApiLevelFromUser because the context here is not
1939 // necessarily a native context.
1940 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1941 if err != nil {
1942 return err
1943 }
1944
1945 if ver.GreaterThan(sdkVersion) {
1946 return fmt.Errorf("newer SDK(%v)", ver)
1947 }
Jiyong Park99644e92020-11-17 22:21:02 +09001948 return nil
1949}
1950
Jiyong Park45bf82e2020-12-15 22:29:02 +09001951// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001952func (mod *Module) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07001953 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001954 return false
1955 }
1956
Colin Cross8acea3e2024-12-12 14:53:30 -08001957 if mod.Static() && cc.IsSharedDepTag(depTag) {
1958 // shared_lib dependency from a static lib is considered as crossing
1959 // the APEX boundary because the dependency doesn't actually is
1960 // linked; the dependency is used only during the compilation phase.
1961 return false
1962 }
1963
Jiyong Park99644e92020-11-17 22:21:02 +09001964 return true
1965}
1966
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001967func (mod *Module) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
1968 return !mod.ApexExclude()
1969}
1970
Jiyong Park99644e92020-11-17 22:21:02 +09001971// Overrides ApexModule.IsInstallabeToApex()
1972func (mod *Module) IsInstallableToApex() bool {
1973 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001974 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001975 return true
1976 }
1977 if _, ok := mod.compiler.(*binaryDecorator); ok {
1978 return true
1979 }
1980 }
1981 return false
1982}
1983
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001984// If a library file has a "lib" prefix, extract the library name without the prefix.
1985func libNameFromFilePath(filepath android.Path) (string, bool) {
1986 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1987 if strings.HasPrefix(libName, "lib") {
1988 libName = libName[3:]
1989 return libName, true
1990 }
1991 return "", false
1992}
1993
Sasha Smundaka76acba2022-04-18 20:12:56 -07001994func kytheExtractRustFactory() android.Singleton {
1995 return &kytheExtractRustSingleton{}
1996}
1997
1998type kytheExtractRustSingleton struct {
1999}
2000
2001func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2002 var xrefTargets android.Paths
2003 ctx.VisitAllModules(func(module android.Module) {
2004 if rustModule, ok := module.(xref); ok {
2005 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
2006 }
2007 })
2008 if len(xrefTargets) > 0 {
2009 ctx.Phony("xref_rust", xrefTargets...)
2010 }
2011}
2012
Jihoon Kangf78a8902022-09-01 22:47:07 +00002013func (c *Module) Partition() string {
2014 return ""
2015}
2016
Ivan Lozanoffee3342019-08-27 12:03:00 -07002017var Bool = proptools.Bool
2018var BoolDefault = proptools.BoolDefault
2019var String = proptools.String
2020var StringPtr = proptools.StringPtr