blob: 861090bcd190b3ff0e1955813f77401450bb9cb3 [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
409func (mod *Module) StubsVersion() string {
410 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
411}
412
Ivan Lozano52767be2019-10-18 14:49:46 -0700413func (mod *Module) SdkVersion() string {
414 return ""
415}
416
Colin Crossc511bc52020-04-07 16:50:32 +0000417func (mod *Module) AlwaysSdk() bool {
418 return false
419}
420
Jiyong Park2286afd2020-06-16 21:58:53 +0900421func (mod *Module) IsSdkVariant() bool {
422 return false
423}
424
Colin Cross1348ce32020-10-01 13:37:16 -0700425func (mod *Module) SplitPerApiLevel() bool {
426 return false
427}
428
Sasha Smundaka76acba2022-04-18 20:12:56 -0700429func (mod *Module) XrefRustFiles() android.Paths {
430 return mod.kytheFiles
431}
432
Ivan Lozanoffee3342019-08-27 12:03:00 -0700433type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400434 Dylibs []string
435 Rlibs []string
436 Rustlibs []string
437 Stdlibs []string
438 ProcMacros []string
439 SharedLibs []string
440 StaticLibs []string
441 WholeStaticLibs []string
442 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400444 // Used for data dependencies adjacent to tests
445 DataLibs []string
446 DataBins []string
447
Colin Crossfe605e12022-01-23 20:46:16 -0800448 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449}
450
451type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700452 DyLibs RustLibraries
453 RLibs RustLibraries
454 SharedLibs android.Paths
455 SharedLibDeps android.Paths
456 StaticLibs android.Paths
457 ProcMacros RustLibraries
458 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000459 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500460
461 // depFlags and depLinkFlags are rustc and linker (clang) flags.
462 depFlags []string
463 depLinkFlags []string
464
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400465 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500466 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000467 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700468 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700469
Ivan Lozano0a468a42024-05-13 21:03:34 -0400470 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
471 // cc_library_static dependants of rlibs.
472 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
473 exportedLinkDirs []string
474
Ivan Lozano45901ed2020-07-24 16:05:01 -0400475 // Used by bindgen modules which call clang
476 depClangFlags []string
477 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400478 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400479 depSystemIncludePaths android.Paths
480
Colin Crossfe605e12022-01-23 20:46:16 -0800481 CrtBegin android.Paths
482 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700483
484 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500485 SrcDeps android.Paths
486 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700487
488 directImplementationDeps android.Paths
489 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700490}
491
492type RustLibraries []RustLibrary
493
494type RustLibrary struct {
495 Path android.Path
496 CrateName string
497}
498
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000500 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700501 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700502}
503
Sasha Smundaka76acba2022-04-18 20:12:56 -0700504type xref interface {
505 XrefRustFiles() android.Paths
506}
507
Matthew Maurerbb3add12020-06-25 09:34:12 -0700508type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000509 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400510 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700511 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700512}
513
Wen-yi Chu41326c12023-09-22 03:58:59 +0000514func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
515 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700516}
517
Colin Cross004bd3f2023-10-02 11:39:17 -0700518func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
519 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400520}
521
Colin Cross0de8a1e2020-09-18 14:15:30 -0700522func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800523 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700524 LinkDirs: flagExporter.linkDirs,
525 LinkObjects: flagExporter.linkObjects,
526 })
527}
528
Matthew Maurerbb3add12020-06-25 09:34:12 -0700529var _ exportedFlagsProducer = (*flagExporter)(nil)
530
531func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700532 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700533}
534
Colin Cross0de8a1e2020-09-18 14:15:30 -0700535type FlagExporterInfo struct {
536 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000537 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700538 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700539}
540
Colin Crossbc7d76c2023-12-12 16:39:03 -0800541var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700542
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400543func (mod *Module) isCoverageVariant() bool {
544 return mod.coverage.Properties.IsCoverageVariant
545}
546
547var _ cc.Coverage = (*Module)(nil)
548
Colin Crosse1a85552024-06-14 12:17:37 -0700549func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400550 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
551}
552
Ivan Lozanod7586b62021-04-01 09:49:36 -0400553func (mod *Module) VndkVersion() string {
554 return mod.Properties.VndkVersion
555}
556
Ivan Lozano0a468a42024-05-13 21:03:34 -0400557func (mod *Module) ExportedCrateLinkDirs() []string {
558 return mod.exportedLinkDirs
559}
560
Ivan Lozanod7586b62021-04-01 09:49:36 -0400561func (mod *Module) PreventInstall() bool {
562 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400563}
564
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400565func (mod *Module) MarkAsCoverageVariant(coverage bool) {
566 mod.coverage.Properties.IsCoverageVariant = coverage
567}
568
569func (mod *Module) EnableCoverageIfNeeded() {
570 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571}
572
573func defaultsFactory() android.Module {
574 return DefaultsFactory()
575}
576
577type Defaults struct {
578 android.ModuleBase
579 android.DefaultsModuleBase
580}
581
582func DefaultsFactory(props ...interface{}) android.Module {
583 module := &Defaults{}
584
585 module.AddProperties(props...)
586 module.AddProperties(
587 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800588 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500589 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100590 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400591 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700592 &BaseCompilerProperties{},
593 &BinaryCompilerProperties{},
594 &LibraryCompilerProperties{},
595 &ProcMacroCompilerProperties{},
596 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400597 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700598 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400599 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400600 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200601 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500602 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000603 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604 )
605
606 android.InitDefaultsModule(module)
607 return module
608}
609
610func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700611 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700612}
613
Ivan Lozano183a3212019-10-18 14:18:45 -0700614func (mod *Module) CcLibrary() bool {
615 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500616 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700617 return true
618 }
619 }
620 return false
621}
622
623func (mod *Module) CcLibraryInterface() bool {
624 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400625 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
626 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000627 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700628 return true
629 }
630 }
631 return false
632}
633
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400634func (mod *Module) RustLibraryInterface() bool {
635 if mod.compiler != nil {
636 if _, ok := mod.compiler.(libraryInterface); ok {
637 return true
638 }
639 }
640 return false
641}
642
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500643func (mod *Module) IsFuzzModule() bool {
644 if _, ok := mod.compiler.(*fuzzDecorator); ok {
645 return true
646 }
647 return false
648}
649
650func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
651 return mod.FuzzModule
652}
653
654func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
655 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
656 return fuzzer.fuzzPackagedModule
657 }
658 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
659}
660
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000661func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500662 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
663 return fuzzer.sharedLibraries
664 }
665 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
666}
667
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400668func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400669 if mod.compiler != nil {
670 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400671 }
672 return nil
673}
674
Ivan Lozano183a3212019-10-18 14:18:45 -0700675func (mod *Module) SetStatic() {
676 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700677 if library, ok := mod.compiler.(libraryInterface); ok {
678 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700679 return
680 }
681 }
682 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
683}
684
685func (mod *Module) SetShared() {
686 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700687 if library, ok := mod.compiler.(libraryInterface); ok {
688 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700689 return
690 }
691 }
692 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
693}
694
Ivan Lozano183a3212019-10-18 14:18:45 -0700695func (mod *Module) BuildStaticVariant() bool {
696 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700697 if library, ok := mod.compiler.(libraryInterface); ok {
698 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700699 }
700 }
701 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
702}
703
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400704func (mod *Module) BuildRlibVariant() bool {
705 if mod.compiler != nil {
706 if library, ok := mod.compiler.(libraryInterface); ok {
707 return library.buildRlib()
708 }
709 }
710 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
711}
712
Ivan Lozano183a3212019-10-18 14:18:45 -0700713func (mod *Module) BuildSharedVariant() bool {
714 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700715 if library, ok := mod.compiler.(libraryInterface); ok {
716 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700717 }
718 }
719 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
720}
721
Ivan Lozano183a3212019-10-18 14:18:45 -0700722func (mod *Module) Module() android.Module {
723 return mod
724}
725
Ivan Lozano183a3212019-10-18 14:18:45 -0700726func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400727 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700728}
729
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400730func (mod *Module) CoverageFiles() android.Paths {
731 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800732 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400733 }
734 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
735}
736
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400737// Rust does not produce gcno files, and therefore does not produce a coverage archive.
738func (mod *Module) CoverageOutputFile() android.OptionalPath {
739 return android.OptionalPath{}
740}
741
742func (mod *Module) IsNdk(config android.Config) bool {
743 return false
744}
745
Spandan Das10c41362024-12-03 01:33:09 +0000746func (mod *Module) HasStubsVariants() bool {
747 return false
748}
749
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400750func (mod *Module) IsStubs() bool {
751 return false
752}
753
Jiyong Park459feca2020-12-15 11:02:21 +0900754func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900755 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900756 return false
757 }
758
Jiyong Park459feca2020-12-15 11:02:21 +0900759 // The apex variant is not installable because it is included in the APEX and won't appear
760 // in the system partition as a standalone file.
761 if !apexInfo.IsForPlatform() {
762 return false
763 }
764
Jiyong Parke54f07e2021-04-07 15:08:04 +0900765 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900766}
767
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500768func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800769 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
770 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500771}
772
Ivan Lozano183a3212019-10-18 14:18:45 -0700773var _ cc.LinkableInterface = (*Module)(nil)
774
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775func (mod *Module) Init() android.Module {
776 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500777 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778
Yi Kong46c6e592022-01-20 22:55:00 +0800779 if mod.afdo != nil {
780 mod.AddProperties(mod.afdo.props()...)
781 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782 if mod.compiler != nil {
783 mod.AddProperties(mod.compiler.compilerProps()...)
784 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400785 if mod.coverage != nil {
786 mod.AddProperties(mod.coverage.props()...)
787 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200788 if mod.clippy != nil {
789 mod.AddProperties(mod.clippy.props()...)
790 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400791 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700792 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400793 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500794 if mod.sanitize != nil {
795 mod.AddProperties(mod.sanitize.props()...)
796 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400797
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900799 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800
801 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802 return mod
803}
804
805func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
806 return &Module{
807 hod: hod,
808 multilib: multilib,
809 }
810}
811func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
812 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800813 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400814 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200815 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500816 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 return module
818}
819
820type ModuleContext interface {
821 android.ModuleContext
822 ModuleContextIntf
823}
824
825type BaseModuleContext interface {
826 android.BaseModuleContext
827 ModuleContextIntf
828}
829
830type DepsContext interface {
831 android.BottomUpMutatorContext
832 ModuleContextIntf
833}
834
835type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200836 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700837 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700838}
839
840type depsContext struct {
841 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842}
843
844type moduleContext struct {
845 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846}
847
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200848type baseModuleContext struct {
849 android.BaseModuleContext
850}
851
852func (ctx *moduleContext) RustModule() *Module {
853 return ctx.Module().(*Module)
854}
855
856func (ctx *moduleContext) toolchain() config.Toolchain {
857 return ctx.RustModule().toolchain(ctx)
858}
859
860func (ctx *depsContext) RustModule() *Module {
861 return ctx.Module().(*Module)
862}
863
864func (ctx *depsContext) toolchain() config.Toolchain {
865 return ctx.RustModule().toolchain(ctx)
866}
867
868func (ctx *baseModuleContext) RustModule() *Module {
869 return ctx.Module().(*Module)
870}
871
872func (ctx *baseModuleContext) toolchain() config.Toolchain {
873 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400874}
875
876func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700877 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
878 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
879 return false
880 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400881 return mod.compiler != nil && mod.compiler.nativeCoverage()
882}
883
Ivan Lozanod7586b62021-04-01 09:49:36 -0400884func (mod *Module) EverInstallable() bool {
885 return mod.compiler != nil &&
886 // Check to see whether the module is actually ever installable.
887 mod.compiler.everInstallable()
888}
889
890func (mod *Module) Installable() *bool {
891 return mod.Properties.Installable
892}
893
Ivan Lozano872d5792022-03-23 17:31:39 -0400894func (mod *Module) ProcMacro() bool {
895 if pm, ok := mod.compiler.(procMacroInterface); ok {
896 return pm.ProcMacro()
897 }
898 return false
899}
900
Ivan Lozanoffee3342019-08-27 12:03:00 -0700901func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
902 if mod.cachedToolchain == nil {
903 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
904 }
905 return mod.cachedToolchain
906}
907
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200908func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
909 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
910}
911
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
913}
914
915func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
916 ctx := &moduleContext{
917 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700918 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919
Colin Crossff694a82023-12-13 15:54:49 -0800920 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900921 if !apexInfo.IsForPlatform() {
922 mod.hideApexVariantFromMake = true
923 }
924
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500926 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
927
Ivan Lozanof1868af2022-04-12 13:08:36 -0400928 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700929
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930 if !toolchain.Supported() {
931 // This toolchain's unsupported, there's nothing to do for this mod.
932 return
933 }
934
935 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400936 // Export linkDirs for CC rust generatedlibs
937 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
938 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
939
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940 flags := Flags{
941 Toolchain: toolchain,
942 }
943
Ivan Lozano67eada32021-09-23 11:50:33 -0400944 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800945 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500946 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800947 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948 if mod.compiler != nil {
949 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400950 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +0000951 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400952 }
953 if mod.coverage != nil {
954 flags, deps = mod.coverage.flags(ctx, flags, deps)
955 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200956 if mod.clippy != nil {
957 flags, deps = mod.clippy.flags(ctx, flags, deps)
958 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500959 if mod.sanitize != nil {
960 flags, deps = mod.sanitize.flags(ctx, flags, deps)
961 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400962
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200963 // SourceProvider needs to call GenerateSource() before compiler calls
964 // compile() so it can provide the source. A SourceProvider has
965 // multiple variants (e.g. source, rlib, dylib). Only the "source"
966 // variant is responsible for effectively generating the source. The
967 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400968 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200969 if mod.compiler.(libraryInterface).source() {
970 mod.sourceProvider.GenerateSource(ctx, deps)
971 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
972 } else {
973 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
974 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700975 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200976 }
Colin Crossa6182ab2024-08-21 10:47:44 -0700977 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400978 }
979
980 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100981 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700982 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400983 if ctx.Failed() {
984 return
985 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700986 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -0700987 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700988 if buildOutput.kytheFile != nil {
989 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
990 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400991 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900992
Dan Albert06feee92021-03-19 15:06:02 -0700993 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
994
Colin Crossff694a82023-12-13 15:54:49 -0800995 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400996 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900997 // If the module has been specifically configure to not be installed then
998 // hide from make as otherwise it will break when running inside make as the
999 // output path to install will not be specified. Not all uninstallable
1000 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001001 // side dependencies. In particular, proc-macros need to be captured in the
1002 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001003 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001004 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001005 } else if !mod.installable(apexInfo) {
1006 mod.SkipInstall()
1007 }
1008
1009 // Still call install though, the installs will be stored as PackageSpecs to allow
1010 // using the outputs in a genrule.
1011 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001012 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001013 if ctx.Failed() {
1014 return
1015 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001016 // Export your own directory as a linkDir
1017 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1018
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001019 }
Chris Wailes74be7642021-07-22 16:20:28 -07001020
Colin Crossb614cd42024-10-11 12:52:21 -07001021 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1022 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1023 })
1024
Chris Wailes74be7642021-07-22 16:20:28 -07001025 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026 }
Wei Lia1aa2972024-06-21 13:08:51 -07001027
Yu Liu8024b922024-12-20 23:31:32 +00001028 linkableInfo := cc.CreateCommonLinkableInfo(mod)
1029 linkableInfo.Static = mod.Static()
1030 linkableInfo.Shared = mod.Shared()
1031 linkableInfo.CrateName = mod.CrateName()
1032 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
1033 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1034
1035 rustInfo := &RustInfo{
1036 AndroidMkSuffix: mod.AndroidMkSuffix(),
1037 RustSubName: mod.Properties.RustSubName,
1038 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
1039 }
1040 if mod.compiler != nil {
1041 rustInfo.CompilerInfo = &CompilerInfo{
1042 NoStdlibs: mod.compiler.noStdlibs(),
1043 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1044 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1045 }
1046 if lib, ok := mod.compiler.(libraryInterface); ok {
1047 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1048 Dylib: lib.dylib(),
1049 Rlib: lib.rlib(),
1050 }
1051 }
1052 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1053 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1054 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1055 }
1056 }
1057 }
1058 if mod.sourceProvider != nil {
1059 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1060 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1061 ProtobufDecoratorInfo: &ProtobufDecoratorInfo{},
1062 }
1063 }
1064 }
1065 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001066
mrziwang0cbd3b02024-06-20 16:39:25 -07001067 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001068
1069 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001070}
1071
1072func (mod *Module) setOutputFiles(ctx ModuleContext) {
1073 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1074 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1075 } else if mod.OutputFile().Valid() {
1076 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1077 } else {
1078 ctx.SetOutputFiles(android.Paths{}, "")
1079 }
1080 if mod.compiler != nil {
1081 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1082 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001083}
1084
Wei Lia1aa2972024-06-21 13:08:51 -07001085func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1086 // Dump metadata that can not be done in android/compliance-metadata.go
1087 metadataInfo := ctx.ComplianceMetadataInfo()
1088 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1089 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1090
1091 // Static libs
1092 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1093 staticDepNames := make([]string, 0, len(staticDeps))
1094 for _, dep := range staticDeps {
1095 staticDepNames = append(staticDepNames, dep.Name())
1096 }
1097 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1098 for _, dep := range ccStaticDeps {
1099 staticDepNames = append(staticDepNames, dep.Name())
1100 }
1101
1102 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1103 // C static libraries
1104 for _, dep := range deps.StaticLibs {
1105 staticDepPaths = append(staticDepPaths, dep.String())
1106 }
1107 // Rust static libraries
1108 for _, dep := range deps.RLibs {
1109 staticDepPaths = append(staticDepPaths, dep.Path.String())
1110 }
1111 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1112 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1113
1114 // C Whole static libs
1115 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1116 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1117 for _, dep := range ccStaticDeps {
1118 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1119 }
1120 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1121}
1122
Ivan Lozanoffee3342019-08-27 12:03:00 -07001123func (mod *Module) deps(ctx DepsContext) Deps {
1124 deps := Deps{}
1125
1126 if mod.compiler != nil {
1127 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001128 }
1129 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001130 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001131 }
1132
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001133 if mod.coverage != nil {
1134 deps = mod.coverage.deps(ctx, deps)
1135 }
1136
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001137 if mod.sanitize != nil {
1138 deps = mod.sanitize.deps(ctx, deps)
1139 }
1140
Ivan Lozanoffee3342019-08-27 12:03:00 -07001141 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1142 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001143 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001144 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1145 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1146 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001147 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001148 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001149 return deps
1150
1151}
1152
Ivan Lozanoffee3342019-08-27 12:03:00 -07001153type dependencyTag struct {
1154 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001155 name string
1156 library bool
1157 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001158 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001159}
1160
Jiyong Park65b62242020-11-25 12:44:59 +09001161// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1162// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1163func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001164 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001165}
1166
1167var _ android.InstallNeededDependencyTag = dependencyTag{}
1168
Colin Cross65cb3142021-12-10 23:05:02 +00001169func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1170 if d.library && d.dynamic {
1171 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1172 }
1173 return nil
1174}
1175
Yu Liuc8884602024-03-15 18:48:38 +00001176func (d dependencyTag) PropagateAconfigValidation() bool {
1177 return d == rlibDepTag || d == sourceDepTag
1178}
1179
1180var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1181
Colin Cross65cb3142021-12-10 23:05:02 +00001182var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1183
Ivan Lozanoffee3342019-08-27 12:03:00 -07001184var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001185 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1186 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001187 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001188 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001189 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001190 dataLibDepTag = dependencyTag{name: "data lib"}
1191 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001192)
1193
Jiyong Park99644e92020-11-17 22:21:02 +09001194func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1195 tag, ok := depTag.(dependencyTag)
1196 return ok && tag == dylibDepTag
1197}
1198
Jiyong Park94e22fd2021-04-08 18:19:15 +09001199func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1200 tag, ok := depTag.(dependencyTag)
1201 return ok && tag == rlibDepTag
1202}
1203
Matthew Maurer0f003b12020-06-29 14:34:06 -07001204type autoDep struct {
1205 variation string
1206 depTag dependencyTag
1207}
1208
1209var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001210 sourceVariation = "source"
1211 rlibVariation = "rlib"
1212 dylibVariation = "dylib"
1213 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1214 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001215)
1216
1217type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001218 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001219}
1220
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001221func (mod *Module) begin(ctx BaseModuleContext) {
1222 if mod.coverage != nil {
1223 mod.coverage.begin(ctx)
1224 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001225 if mod.sanitize != nil {
1226 mod.sanitize.begin(ctx)
1227 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001228}
1229
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001230func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001231 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001232 return p.prebuilt()
1233 }
1234 return nil
1235}
1236
Kiyoung Kim37693d02024-04-04 09:56:15 +09001237func (mod *Module) Symlinks() []string {
1238 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1239 return nil
1240}
1241
Yu Liu8024b922024-12-20 23:31:32 +00001242func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1243 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001244 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001245 if rustInfo.SnapshotInfo != nil {
1246 baseName := linkableInfo.BaseModuleName
1247 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001248 }
1249 }
Yu Liu8024b922024-12-20 23:31:32 +00001250 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001251}
1252
Yu Liu8024b922024-12-20 23:31:32 +00001253func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001254 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001255 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1256 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001257 }
1258 }
1259}
Andrew Walbran52533232024-03-19 11:36:04 +00001260
Ivan Lozanoffee3342019-08-27 12:03:00 -07001261func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1262 var depPaths PathDeps
1263
Yu Liu8024b922024-12-20 23:31:32 +00001264 directRlibDeps := []*cc.LinkableInfo{}
1265 directDylibDeps := []*cc.LinkableInfo{}
1266 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001267 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001268 directStaticLibDeps := [](*cc.LinkableInfo){}
1269 directSrcProvidersDeps := []*android.ModuleProxy{}
1270 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001271
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001272 // For the dependency from platform to apex, use the latest stubs
1273 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001274 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001275 if !apexInfo.IsForPlatform() {
1276 mod.apexSdkVersion = apexInfo.MinSdkVersion
1277 }
1278
1279 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1280 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1281 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1282 // (b/144430859)
1283 mod.apexSdkVersion = android.FutureApiLevel
1284 }
1285
Spandan Das604f3762023-03-16 22:51:40 +00001286 skipModuleList := map[string]bool{}
1287
Colin Crossa14fb6a2024-10-23 16:57:06 -07001288 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001289 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001290
Yu Liu8024b922024-12-20 23:31:32 +00001291 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292 depName := ctx.OtherModuleName(dep)
1293 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001294 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1295
Spandan Das604f3762023-03-16 22:51:40 +00001296 if _, exists := skipModuleList[depName]; exists {
1297 return
1298 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001299
1300 if depTag == android.DarwinUniversalVariantTag {
1301 return
1302 }
1303
Yu Liu8024b922024-12-20 23:31:32 +00001304 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1305 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1306 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1307 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1308 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001309 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001310 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001311
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001312 switch {
1313 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001314 dylib := rustInfo.CompilerInfo.LibraryInfo
1315 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001316 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1317 return
1318 }
Yu Liu8024b922024-12-20 23:31:32 +00001319 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001320 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001321 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1322
Colin Crossb614cd42024-10-11 12:52:21 -07001323 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1324 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1325 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1326 }
1327
Yu Liu8024b922024-12-20 23:31:32 +00001328 if !rustInfo.CompilerInfo.NoStdlibs {
1329 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1330 if ctx.Device() {
1331 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1332 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001333 if rustDepStdLinkage != modStdLinkage {
1334 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1335 return
1336 }
1337 }
1338
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001339 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001340 rlib := rustInfo.CompilerInfo.LibraryInfo
1341 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001342 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001343 return
1344 }
Yu Liu8024b922024-12-20 23:31:32 +00001345 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001346 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001347 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1348
Ivan Lozano0a468a42024-05-13 21:03:34 -04001349 // rust_ffi rlibs may export include dirs, so collect those here.
1350 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1351 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001352 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001353
Colin Crossb614cd42024-10-11 12:52:21 -07001354 // rlibs are not installed, so don't add the output file to directImplementationDeps
1355 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1356 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1357 }
1358
Yu Liu8024b922024-12-20 23:31:32 +00001359 if !rustInfo.CompilerInfo.NoStdlibs {
1360 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1361 if ctx.Device() {
1362 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1363 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001364 if rustDepStdLinkage != modStdLinkage {
1365 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1366 return
1367 }
1368 }
1369
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001370 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001371 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001372 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001373 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001374 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001375
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001376 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001377 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001378 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001379 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001380 case cc.IsStaticDepTag(depTag):
1381 // Rust FFI rlibs should not be declared in a Rust modules
1382 // "static_libs" list as we can't handle them properly at the
1383 // moment (for example, they only produce an rlib-std variant).
1384 // Instead, a normal rust_library variant should be used.
1385 ctx.PropertyErrorf("static_libs",
1386 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1387 depName)
1388
Paul Duffind5cf92e2021-07-09 17:38:55 +01001389 }
1390
Yu Liu8024b922024-12-20 23:31:32 +00001391 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001392
Paul Duffind5cf92e2021-07-09 17:38:55 +01001393 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001394 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1395 // OS/Arch variant is used.
1396 var helper string
1397 if ctx.Host() {
1398 helper = "missing 'host_supported'?"
1399 } else {
1400 helper = "device module defined?"
1401 }
1402
Yu Liu8024b922024-12-20 23:31:32 +00001403 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001404 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1405 return
Yu Liu8024b922024-12-20 23:31:32 +00001406 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001407 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1408 return
1409 }
Yu Liu8024b922024-12-20 23:31:32 +00001410 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001411 }
1412
Ivan Lozano0a468a42024-05-13 21:03:34 -04001413 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001414 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001415 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001416 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1417 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001418 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001419 }
1420
Ivan Lozanoffee3342019-08-27 12:03:00 -07001421 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001422 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001423 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001424 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001425 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001426 }
1427 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001428
Ivan Lozanod106efe2023-09-21 23:30:26 -04001429 if depTag == sourceDepTag {
1430 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001431 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001432 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001433 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1434 }
1435 }
1436 }
Yu Liu8024b922024-12-20 23:31:32 +00001437 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001438 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001439 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1440 if !hasRustInfo {
1441 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001442 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1443 return
1444 }
Yu Liu8024b922024-12-20 23:31:32 +00001445 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001446 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1447 return
1448 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001449 }
Yu Liu8024b922024-12-20 23:31:32 +00001450 linkObject := linkableInfo.OutputFile
Ivan Lozano2093af22020-08-25 12:48:19 -04001451 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001452 if !ctx.Config().AllowMissingDependencies() {
1453 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1454 } else {
1455 ctx.AddMissingDependencies([]string{depName})
1456 }
1457 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001458 }
1459
Wen-yi Chu41326c12023-09-22 03:58:59 +00001460 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001461
Ivan Lozanoffee3342019-08-27 12:03:00 -07001462 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001463 switch {
1464 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001465 if cc.IsWholeStaticLib(depTag) {
1466 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1467 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001468 if mod.Binary() {
1469 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1470 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1471 // final linkage, pass the args directly to the linker to handle these cases.
1472 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1473 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001474 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001475 } else {
1476 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 -05001477 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001478 }
1479
1480 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1481 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001482 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001483 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1484
Colin Cross313aa542023-12-13 13:47:44 -08001485 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001486 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1487 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1488 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1489 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Yu Liu8024b922024-12-20 23:31:32 +00001490 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001491
1492 // Record baseLibName for snapshots.
1493 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1494
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001495 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001496 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001497 // For the shared lib dependencies, we may link to the stub variant
1498 // of the dependency depending on the context (e.g. if this
1499 // dependency crosses the APEX boundaries).
1500 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1501
Colin Crossb614cd42024-10-11 12:52:21 -07001502 if !sharedLibraryInfo.IsStubs {
1503 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1504 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1505 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1506 }
1507 }
1508
Jiyong Park7d55b612021-06-11 17:22:09 +09001509 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1510 // object (either from stub or non-stub) to use.
1511 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001512 if !linkObject.Valid() {
1513 if !ctx.Config().AllowMissingDependencies() {
1514 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1515 } else {
1516 ctx.AddMissingDependencies([]string{depName})
1517 }
1518 return
1519 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001520 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001521
Ivan Lozanoffee3342019-08-27 12:03:00 -07001522 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001523 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001524 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1525 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1526 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1527 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001528 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001529
1530 // Record baseLibName for snapshots.
1531 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1532
Cole Faustb6e6f992023-08-17 17:42:26 -07001533 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001534 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001535 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001536 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001537 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1538 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1539 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001540 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001541 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001542 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001543 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001544 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001545 }
1546
1547 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001548 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1549 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001550 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001551 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001552 } else {
1553 switch {
1554 case depTag == cc.CrtBeginDepTag:
1555 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1556 case depTag == cc.CrtEndDepTag:
1557 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1558 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001559 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001560
Yu Liu8024b922024-12-20 23:31:32 +00001561 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoKey); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001562 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001563 // These are usually genrules which don't have per-target variants.
1564 directSrcDeps = append(directSrcDeps, srcDep)
1565 }
1566 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001567 })
1568
Colin Crossa14fb6a2024-10-23 16:57:06 -07001569 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001570
1571 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001572 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001573 for _, dep := range directRlibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001574 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001575 if alias, aliased := aliases[crateName]; aliased {
1576 crateName = alias
1577 }
Yu Liu8024b922024-12-20 23:31:32 +00001578 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001579 }
1580 var dylibDepFiles RustLibraries
1581 for _, dep := range directDylibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001582 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001583 if alias, aliased := aliases[crateName]; aliased {
1584 crateName = alias
1585 }
Yu Liu8024b922024-12-20 23:31:32 +00001586 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001587 }
1588 var procMacroDepFiles RustLibraries
1589 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001590 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001591 if alias, aliased := aliases[crateName]; aliased {
1592 crateName = alias
1593 }
Yu Liu8024b922024-12-20 23:31:32 +00001594 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001595 }
1596
Colin Cross004bd3f2023-10-02 11:39:17 -07001597 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001598 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001599 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001600 }
1601
Colin Cross004bd3f2023-10-02 11:39:17 -07001602 var sharedLibFiles android.Paths
1603 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001604 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001605 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001606 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001607 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001608 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001609 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001610 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001611 }
1612
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001613 var srcProviderDepFiles android.Paths
1614 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001615 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001616 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1617 }
1618 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001619 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001620 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1621 }
1622
Wen-yi Chu41326c12023-09-22 03:58:59 +00001623 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1624 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001625 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1626 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1627 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001628 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001629 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001630
1631 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001632 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001633 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001634 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001635 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1636 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1637 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoad7ba592025-01-23 01:23:43 +00001638 depPaths.depLinkFlags = android.FirstUniqueStrings(depPaths.depLinkFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001639
1640 return depPaths
1641}
1642
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001643func (mod *Module) InstallInData() bool {
1644 if mod.compiler == nil {
1645 return false
1646 }
1647 return mod.compiler.inData()
1648}
1649
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001650func (mod *Module) InstallInRamdisk() bool {
1651 return mod.InRamdisk()
1652}
1653
1654func (mod *Module) InstallInVendorRamdisk() bool {
1655 return mod.InVendorRamdisk()
1656}
1657
1658func (mod *Module) InstallInRecovery() bool {
1659 return mod.InRecovery()
1660}
1661
Wen-yi Chu41326c12023-09-22 03:58:59 +00001662func linkPathFromFilePath(filepath android.Path) string {
1663 return strings.Split(filepath.String(), filepath.Base())[0]
1664}
1665
Spandan Das604f3762023-03-16 22:51:40 +00001666// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1667func (r *Module) usePublicApi() bool {
1668 return r.Device() && r.UseSdk()
1669}
1670
1671// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1672func (r *Module) useVendorApi() bool {
1673 return r.Device() && (r.InVendor() || r.InProduct())
1674}
1675
Ivan Lozanoffee3342019-08-27 12:03:00 -07001676func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1677 ctx := &depsContext{
1678 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001679 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001680
1681 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001682 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001683
1684 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001685 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001686 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001687
Ivan Lozano2b081132020-09-08 12:46:52 -04001688 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001689 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001690 stdLinkage = "rlib-std"
1691 }
1692
1693 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001694
Ivan Lozano2b081132020-09-08 12:46:52 -04001695 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1696 rlibDepVariations = append(rlibDepVariations,
1697 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1698 }
1699
Ivan Lozano1921e802021-05-20 13:39:16 -04001700 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001701 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001702 for _, lib := range deps.Rlibs {
1703 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001704 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001705 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001706
1707 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001708 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001709
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001710 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001711 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001712 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001713
Ivan Lozano1921e802021-05-20 13:39:16 -04001714 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001715 if deps.Rustlibs != nil {
1716 if !mod.compiler.Disabled() {
1717 for _, lib := range deps.Rustlibs {
1718 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1719 if autoDep.depTag == rlibDepTag {
1720 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001721 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1722
Ivan Lozanod106efe2023-09-21 23:30:26 -04001723 } else {
1724 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1725 // Check for the existence of the dylib deptag variant. Select it if available,
1726 // otherwise select the rlib variant.
1727 autoDepVariations := append(commonDepVariations,
1728 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001729 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1730 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001731
Ivan Lozanod106efe2023-09-21 23:30:26 -04001732 } else {
1733 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001734 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1735
Ivan Lozanod106efe2023-09-21 23:30:26 -04001736 }
1737 }
1738 }
1739 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1740 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001741 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001742 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001743
Ivan Lozano0a468a42024-05-13 21:03:34 -04001744 // Only add rustlib dependencies if they're source providers themselves.
1745 // This is used to track which crate names need to be added to the source generated
1746 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001747 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001748 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001749 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001750 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001751 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001752 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001753
Ivan Lozano1921e802021-05-20 13:39:16 -04001754 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001755 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001756 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001757 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001758 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001759 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001760 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001761 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001762 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001763 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1764
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001765 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001766 }
1767 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001768
1769 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001770 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001771 name, version := cc.StubsLibNameAndVersion(lib)
1772
1773 variations := []blueprint.Variation{
1774 {Mutator: "link", Variation: "shared"},
1775 }
Spandan Dasff665182024-09-11 18:48:44 +00001776 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001777 }
1778
1779 for _, lib := range deps.WholeStaticLibs {
1780 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001781
1782 actx.AddVariationDependencies([]blueprint.Variation{
1783 {Mutator: "link", Variation: "static"},
1784 }, depTag, lib)
1785 }
1786
1787 for _, lib := range deps.StaticLibs {
1788 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001789
1790 actx.AddVariationDependencies([]blueprint.Variation{
1791 {Mutator: "link", Variation: "static"},
1792 }, depTag, lib)
1793 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001794
Zach Johnson3df4e632020-11-06 11:56:27 -08001795 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1796
Colin Cross565cafd2020-09-25 18:47:38 -07001797 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001798 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001799 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001800 }
Colin Crossfe605e12022-01-23 20:46:16 -08001801 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001802 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001803 }
1804
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001805 if mod.sourceProvider != nil {
1806 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1807 bindgen.Properties.Custom_bindgen != "" {
1808 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1809 bindgen.Properties.Custom_bindgen)
1810 }
1811 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001812
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001813 actx.AddVariationDependencies([]blueprint.Variation{
1814 {Mutator: "link", Variation: "shared"},
1815 }, dataLibDepTag, deps.DataLibs...)
1816
1817 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1818
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001819 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001820 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001821
1822 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001823}
1824
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001825func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001826 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001827 mod.beginMutator(ctx)
1828 }
1829}
1830
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001831func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1832 ctx := &baseModuleContext{
1833 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001834 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001835
1836 mod.begin(ctx)
1837}
1838
Ivan Lozanoffee3342019-08-27 12:03:00 -07001839func (mod *Module) Name() string {
1840 name := mod.ModuleBase.Name()
1841 if p, ok := mod.compiler.(interface {
1842 Name(string) string
1843 }); ok {
1844 name = p.Name(name)
1845 }
1846 return name
1847}
1848
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001849func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001850 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001851 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001852 }
1853}
1854
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001855var _ android.HostToolProvider = (*Module)(nil)
1856
1857func (mod *Module) HostToolPath() android.OptionalPath {
1858 if !mod.Host() {
1859 return android.OptionalPath{}
1860 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001861 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1862 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001863 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1864 // Even though proc-macros aren't strictly "tools", since they target the compiler
1865 // and act as compiler plugins, we treat them similarly.
1866 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001867 }
1868 return android.OptionalPath{}
1869}
1870
Jiyong Park99644e92020-11-17 22:21:02 +09001871var _ android.ApexModule = (*Module)(nil)
1872
Ivan Lozano24cf0362024-10-04 16:02:38 +00001873// If a module is marked for exclusion from apexes, don't provide apex variants.
1874// TODO(b/362509506): remove this once stubs are properly supported by rust_ffi targets.
1875func (m *Module) CanHaveApexVariants() bool {
1876 if m.ApexExclude() {
1877 return false
1878 } else {
1879 return m.ApexModuleBase.CanHaveApexVariants()
1880 }
1881}
1882
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001883func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001884 return String(mod.Properties.Min_sdk_version)
1885}
1886
Jiyong Park45bf82e2020-12-15 22:29:02 +09001887// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001888func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001889 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001890 if minSdkVersion == "apex_inherit" {
1891 return nil
1892 }
1893 if minSdkVersion == "" {
1894 return fmt.Errorf("min_sdk_version is not specificed")
1895 }
1896
1897 // Not using nativeApiLevelFromUser because the context here is not
1898 // necessarily a native context.
1899 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1900 if err != nil {
1901 return err
1902 }
1903
1904 if ver.GreaterThan(sdkVersion) {
1905 return fmt.Errorf("newer SDK(%v)", ver)
1906 }
Jiyong Park99644e92020-11-17 22:21:02 +09001907 return nil
1908}
1909
Jiyong Park45bf82e2020-12-15 22:29:02 +09001910// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001911func (mod *Module) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07001912 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001913 return false
1914 }
1915
Colin Cross8acea3e2024-12-12 14:53:30 -08001916 if mod.Static() && cc.IsSharedDepTag(depTag) {
1917 // shared_lib dependency from a static lib is considered as crossing
1918 // the APEX boundary because the dependency doesn't actually is
1919 // linked; the dependency is used only during the compilation phase.
1920 return false
1921 }
1922
Jiyong Park99644e92020-11-17 22:21:02 +09001923 return true
1924}
1925
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001926func (mod *Module) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
1927 return !mod.ApexExclude()
1928}
1929
Jiyong Park99644e92020-11-17 22:21:02 +09001930// Overrides ApexModule.IsInstallabeToApex()
1931func (mod *Module) IsInstallableToApex() bool {
1932 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001933 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001934 return true
1935 }
1936 if _, ok := mod.compiler.(*binaryDecorator); ok {
1937 return true
1938 }
1939 }
1940 return false
1941}
1942
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001943// If a library file has a "lib" prefix, extract the library name without the prefix.
1944func libNameFromFilePath(filepath android.Path) (string, bool) {
1945 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1946 if strings.HasPrefix(libName, "lib") {
1947 libName = libName[3:]
1948 return libName, true
1949 }
1950 return "", false
1951}
1952
Sasha Smundaka76acba2022-04-18 20:12:56 -07001953func kytheExtractRustFactory() android.Singleton {
1954 return &kytheExtractRustSingleton{}
1955}
1956
1957type kytheExtractRustSingleton struct {
1958}
1959
1960func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1961 var xrefTargets android.Paths
1962 ctx.VisitAllModules(func(module android.Module) {
1963 if rustModule, ok := module.(xref); ok {
1964 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1965 }
1966 })
1967 if len(xrefTargets) > 0 {
1968 ctx.Phony("xref_rust", xrefTargets...)
1969 }
1970}
1971
Jihoon Kangf78a8902022-09-01 22:47:07 +00001972func (c *Module) Partition() string {
1973 return ""
1974}
1975
Ivan Lozanoffee3342019-08-27 12:03:00 -07001976var Bool = proptools.Bool
1977var BoolDefault = proptools.BoolDefault
1978var String = proptools.String
1979var StringPtr = proptools.StringPtr