blob: d8a044423c30638835aa156d9760f57ff5814117 [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 {
Yu Liu727204c2025-01-23 20:58:32 +000052 Srcs android.Paths
Yu Liu8024b922024-12-20 23:31:32 +000053 ProtobufDecoratorInfo *ProtobufDecoratorInfo
54}
55
56type RustInfo struct {
57 AndroidMkSuffix string
58 RustSubName string
59 TransitiveAndroidMkSharedLibs depset.DepSet[string]
60 CompilerInfo *CompilerInfo
61 SnapshotInfo *cc.SnapshotInfo
62 SourceProviderInfo *SourceProviderInfo
Yu Liu2a815b62025-02-21 20:46:25 +000063 XrefRustFiles android.Paths
Yu Liu8024b922024-12-20 23:31:32 +000064}
65
66var RustInfoProvider = blueprint.NewProvider[*RustInfo]()
67
Ivan Lozanoffee3342019-08-27 12:03:00 -070068func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070069 android.RegisterModuleType("rust_defaults", defaultsFactory)
Colin Cross8a49a3d2024-05-20 12:22:27 -070070 android.PreDepsMutators(registerPreDepsMutators)
71 android.PostDepsMutators(registerPostDepsMutators)
Inseob Kim3b244062023-07-11 13:31:36 +090072 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070073 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020074 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000075 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070076}
77
Colin Cross8a49a3d2024-05-20 12:22:27 -070078func registerPreDepsMutators(ctx android.RegisterMutatorsContext) {
79 ctx.Transition("rust_libraries", &libraryTransitionMutator{})
80 ctx.Transition("rust_stdlinkage", &libstdTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070081 ctx.BottomUp("rust_begin", BeginMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070082}
83
84func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070085 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070086}
87
Ivan Lozanoffee3342019-08-27 12:03:00 -070088type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040089 GlobalRustFlags []string // Flags that apply globally to rust
90 GlobalLinkFlags []string // Flags that apply globally to linker
91 RustFlags []string // Flags that apply to rust
92 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020093 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070094 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070095 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040096 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020097 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070098 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070099}
100
101type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -0500102 AndroidMkRlibs []string `blueprint:"mutated"`
103 AndroidMkDylibs []string `blueprint:"mutated"`
104 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -0500105 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +0000106 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -0400107
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900108 ImageVariation string `blueprint:"mutated"`
109 VndkVersion string `blueprint:"mutated"`
110 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -0500111
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400112 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
113 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
114 // appended before SubName.
115 RustSubName string `blueprint:"mutated"`
116
Ivan Lozano6a884432020-12-02 09:15:16 -0500117 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +0000118 ProductVariantNeeded bool `blueprint:"mutated"`
119 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500120 CoreVariantNeeded bool `blueprint:"mutated"`
121 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -0700122 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -0800123 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500124 ExtraVariants []string `blueprint:"mutated"`
125
Ivan Lozanoa2268632021-07-22 10:52:06 -0400126 // Allows this module to use non-APEX version of libraries. Useful
127 // for building binaries that are started before APEXes are activated.
128 Bootstrap *bool
129
Ivan Lozano1921e802021-05-20 13:39:16 -0400130 // Used by vendor snapshot to record dependencies from snapshot modules.
131 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900132 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400133 SnapshotRlibs []string `blueprint:"mutated"`
134 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400135
Matthew Maurerc6868382021-07-13 14:12:37 -0700136 // Make this module available when building for ramdisk.
137 // On device without a dedicated recovery partition, the module is only
138 // available after switching root into
139 // /first_stage_ramdisk. To expose the module before switching root, install
140 // the recovery variant instead.
141 Ramdisk_available *bool
142
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500143 // Make this module available when building for vendor ramdisk.
144 // On device without a dedicated recovery partition, the module is only
145 // available after switching root into
146 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800147 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500148 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400149
Ivan Lozano1921e802021-05-20 13:39:16 -0400150 // Normally Soong uses the directory structure to decide which modules
151 // should be included (framework) or excluded (non-framework) from the
152 // different snapshots (vendor, recovery, etc.), but this property
153 // allows a partner to exclude a module normally thought of as a
154 // framework module from the vendor snapshot.
155 Exclude_from_vendor_snapshot *bool
156
157 // Normally Soong uses the directory structure to decide which modules
158 // should be included (framework) or excluded (non-framework) from the
159 // different snapshots (vendor, recovery, etc.), but this property
160 // allows a partner to exclude a module normally thought of as a
161 // framework module from the recovery snapshot.
162 Exclude_from_recovery_snapshot *bool
163
Matthew Maurer460ee942021-02-11 12:31:46 -0800164 // Make this module available when building for recovery
165 Recovery_available *bool
166
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000167 // The API level that this module is built against. The APIs of this API level will be
168 // visible at build time, but use of any APIs newer than min_sdk_version will render the
169 // module unloadable on older devices. In the future it will be possible to weakly-link new
170 // APIs, making the behavior match Java: such modules will load on older devices, but
171 // calling new APIs on devices that do not support them will result in a crash.
172 //
173 // This property has the same behavior as sdk_version does for Java modules. For those
174 // familiar with Android Gradle, the property behaves similarly to how compileSdkVersion
175 // does for Java code.
176 //
177 // In addition, setting this property causes two variants to be built, one for the platform
178 // and one for apps.
179 Sdk_version *string
180
181 // Minimum OS API level supported by this C or C++ module. This property becomes the value
182 // of the __ANDROID_API__ macro. When the C or C++ module is included in an APEX or an APK,
183 // this property is also used to ensure that the min_sdk_version of the containing module is
184 // not older (i.e. less) than this module's min_sdk_version. When not set, this property
185 // defaults to the value of sdk_version. When this is set to "apex_inherit", this tracks
186 // min_sdk_version of the containing APEX. When the module
187 // is not built for an APEX, "apex_inherit" defaults to sdk_version.
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500188 Min_sdk_version *string
189
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000190 // Variant is an SDK variant created by sdkMutator
191 IsSdkVariant bool `blueprint:"mutated"`
192
193 // Set by factories of module types that can only be referenced from variants compiled against
194 // the SDK.
195 AlwaysSdk bool `blueprint:"mutated"`
196
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900197 HideFromMake bool `blueprint:"mutated"`
198 PreventInstall bool `blueprint:"mutated"`
199
200 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700201}
202
203type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700204 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700205
Ivan Lozano6a884432020-12-02 09:15:16 -0500206 VendorProperties cc.VendorProperties
207
Ivan Lozanoffee3342019-08-27 12:03:00 -0700208 Properties BaseProperties
209
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000210 hod android.HostOrDeviceSupported
211 multilib android.Multilib
212 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700213
Ivan Lozano6a884432020-12-02 09:15:16 -0500214 makeLinkType string
215
Yi Kong46c6e592022-01-20 22:55:00 +0800216 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700217 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400218 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200219 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500220 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400222 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700223 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400224
Ivan Lozano0a468a42024-05-13 21:03:34 -0400225 exportedLinkDirs []string
226
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400227 // Output file to be installed, may be stripped or unstripped.
228 outputFile android.OptionalPath
229
Sasha Smundaka76acba2022-04-18 20:12:56 -0700230 // Cross-reference input file
231 kytheFiles android.Paths
232
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400233 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900234
235 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500236
237 // For apex variants, this is set as apex.min_sdk_version
238 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700239
Colin Crossa14fb6a2024-10-23 16:57:06 -0700240 transitiveAndroidMkSharedLibs depset.DepSet[string]
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000241
242 // Shared flags among stubs build rules of this module
243 sharedFlags cc.SharedFlags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700244}
245
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500246func (mod *Module) Header() bool {
247 //TODO: If Rust libraries provide header variants, this needs to be updated.
248 return false
249}
250
251func (mod *Module) SetPreventInstall() {
252 mod.Properties.PreventInstall = true
253}
254
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500255func (mod *Module) SetHideFromMake() {
256 mod.Properties.HideFromMake = true
257}
258
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900259func (mod *Module) HiddenFromMake() bool {
260 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400261}
262
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500263func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500264 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
265 // nil since we need compiler to actually sanitize.
266 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500267}
268
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500269func (mod *Module) IsPrebuilt() bool {
270 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
271 return true
272 }
273 return false
274}
275
Ivan Lozano52767be2019-10-18 14:49:46 -0700276func (mod *Module) SelectedStl() string {
277 return ""
278}
279
Ivan Lozano2b262972019-11-21 12:30:50 -0800280func (mod *Module) NonCcVariants() bool {
281 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400282 if library, ok := mod.compiler.(libraryInterface); ok {
283 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800284 }
285 }
286 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
287}
288
Ivan Lozano52767be2019-10-18 14:49:46 -0700289func (mod *Module) Static() bool {
290 if mod.compiler != nil {
291 if library, ok := mod.compiler.(libraryInterface); ok {
292 return library.static()
293 }
294 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400295 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700296}
297
298func (mod *Module) Shared() bool {
299 if mod.compiler != nil {
300 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400301 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700302 }
303 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400304 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700305}
306
Ivan Lozanod7586b62021-04-01 09:49:36 -0400307func (mod *Module) Dylib() bool {
308 if mod.compiler != nil {
309 if library, ok := mod.compiler.(libraryInterface); ok {
310 return library.dylib()
311 }
312 }
313 return false
314}
315
Ivan Lozanod106efe2023-09-21 23:30:26 -0400316func (mod *Module) Source() bool {
317 if mod.compiler != nil {
318 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
319 return library.source()
320 }
321 }
322 return false
323}
324
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400325func (mod *Module) RlibStd() bool {
326 if mod.compiler != nil {
327 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
328 return library.rlibStd()
329 }
330 }
331 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
332}
333
Ivan Lozanod7586b62021-04-01 09:49:36 -0400334func (mod *Module) Rlib() bool {
335 if mod.compiler != nil {
336 if library, ok := mod.compiler.(libraryInterface); ok {
337 return library.rlib()
338 }
339 }
340 return false
341}
342
343func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400344 if binary, ok := mod.compiler.(binaryInterface); ok {
345 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400346 }
347 return false
348}
349
Justin Yun5e035862021-06-29 20:50:37 +0900350func (mod *Module) StaticExecutable() bool {
351 if !mod.Binary() {
352 return false
353 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400354 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900355}
356
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000357func (mod *Module) ApexExclude() bool {
358 if mod.compiler != nil {
359 if library, ok := mod.compiler.(libraryInterface); ok {
360 return library.apexExclude()
361 }
362 }
363 return false
364}
365
Ivan Lozanod7586b62021-04-01 09:49:36 -0400366func (mod *Module) Object() bool {
367 // Rust has no modules which produce only object files.
368 return false
369}
370
Ivan Lozano52767be2019-10-18 14:49:46 -0700371func (mod *Module) Toc() android.OptionalPath {
372 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400373 if lib, ok := mod.compiler.(libraryInterface); ok {
374 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700375 }
376 }
377 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
378}
379
Colin Crossc511bc52020-04-07 16:50:32 +0000380func (mod *Module) UseSdk() bool {
381 return false
382}
383
Ivan Lozanod7586b62021-04-01 09:49:36 -0400384func (mod *Module) RelativeInstallPath() string {
385 if mod.compiler != nil {
386 return mod.compiler.relativeInstallPath()
387 }
388 return ""
389}
390
Ivan Lozano52767be2019-10-18 14:49:46 -0700391func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500392 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700393}
394
Jiyong Park7d55b612021-06-11 17:22:09 +0900395func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400396 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900397}
398
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400399func (mod *Module) SubName() string {
400 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700401}
402
Ivan Lozanof1868af2022-04-12 13:08:36 -0400403func (mod *Module) IsVndkPrebuiltLibrary() bool {
404 // Rust modules do not provide VNDK prebuilts
405 return false
406}
407
408func (mod *Module) IsVendorPublicLibrary() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000409 // Rust modules do not currently support vendor_public_library
410 return false
Ivan Lozanof1868af2022-04-12 13:08:36 -0400411}
412
413func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
414 // Rust modules to not provide Sdk variants
415 return false
416}
417
Colin Cross127bb8b2020-12-16 16:46:01 -0800418func (c *Module) IsVndkPrivate() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000419 // Rust modules do not currently support VNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800420 return false
421}
422
423func (c *Module) IsLlndk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000424 // Rust modules do not currently support LLNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800425 return false
426}
427
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400428func (mod *Module) KernelHeadersDecorator() bool {
429 return false
430}
431
Colin Cross1f3f1302021-04-26 18:37:44 -0700432func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000433 // Rust modules do not currently support LLNDK variants
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400434 return false
435}
436
Colin Cross5271fea2021-04-27 13:06:04 -0700437func (m *Module) NeedsVendorPublicLibraryVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000438 // Rust modules do not currently support vendor_public_library
Colin Cross5271fea2021-04-27 13:06:04 -0700439 return false
440}
441
Ivan Lozanod7586b62021-04-01 09:49:36 -0400442func (mod *Module) HasLlndkStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000443 // Rust modules do not currently support LLNDK stubs
Ivan Lozanod7586b62021-04-01 09:49:36 -0400444 return false
445}
446
Ivan Lozano52767be2019-10-18 14:49:46 -0700447func (mod *Module) SdkVersion() string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000448 return String(mod.Properties.Sdk_version)
Ivan Lozano52767be2019-10-18 14:49:46 -0700449}
450
Colin Crossc511bc52020-04-07 16:50:32 +0000451func (mod *Module) AlwaysSdk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000452 return mod.Properties.AlwaysSdk
Colin Crossc511bc52020-04-07 16:50:32 +0000453}
454
Jiyong Park2286afd2020-06-16 21:58:53 +0900455func (mod *Module) IsSdkVariant() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000456 return mod.Properties.IsSdkVariant
Jiyong Park2286afd2020-06-16 21:58:53 +0900457}
458
Colin Cross1348ce32020-10-01 13:37:16 -0700459func (mod *Module) SplitPerApiLevel() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000460 return cc.CanUseSdk(mod) && mod.IsCrt()
Colin Cross1348ce32020-10-01 13:37:16 -0700461}
462
Sasha Smundaka76acba2022-04-18 20:12:56 -0700463func (mod *Module) XrefRustFiles() android.Paths {
464 return mod.kytheFiles
465}
466
Ivan Lozanoffee3342019-08-27 12:03:00 -0700467type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400468 Dylibs []string
469 Rlibs []string
470 Rustlibs []string
471 Stdlibs []string
472 ProcMacros []string
473 SharedLibs []string
474 StaticLibs []string
475 WholeStaticLibs []string
476 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700477
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400478 // Used for data dependencies adjacent to tests
479 DataLibs []string
480 DataBins []string
481
Colin Crossfe605e12022-01-23 20:46:16 -0800482 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700483}
484
485type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700486 DyLibs RustLibraries
487 RLibs RustLibraries
488 SharedLibs android.Paths
489 SharedLibDeps android.Paths
490 StaticLibs android.Paths
491 ProcMacros RustLibraries
492 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000493 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500494
495 // depFlags and depLinkFlags are rustc and linker (clang) flags.
496 depFlags []string
497 depLinkFlags []string
498
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000499 // track cc static-libs that have Rlib dependencies
Ivan Lozano610eb1a2025-02-12 21:36:49 +0000500 reexportedCcRlibDeps []cc.RustRlibDep
501 reexportedWholeCcRlibDeps []cc.RustRlibDep
502 ccRlibDeps []cc.RustRlibDep
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000503
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400504 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500505 // Both of these are exported and propagate to dependencies.
Ivan Lozano1f10f682024-11-08 16:16:50 +0000506 linkDirs []string
507 rustLibObjects []string
508 staticLibObjects []string
509 wholeStaticLibObjects []string
510 sharedLibObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700511
Ivan Lozano0a468a42024-05-13 21:03:34 -0400512 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
513 // cc_library_static dependants of rlibs.
514 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
515 exportedLinkDirs []string
516
Ivan Lozano45901ed2020-07-24 16:05:01 -0400517 // Used by bindgen modules which call clang
518 depClangFlags []string
519 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400520 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400521 depSystemIncludePaths android.Paths
522
Colin Crossfe605e12022-01-23 20:46:16 -0800523 CrtBegin android.Paths
524 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700525
526 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500527 SrcDeps android.Paths
528 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700529
530 directImplementationDeps android.Paths
531 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700532}
533
534type RustLibraries []RustLibrary
535
536type RustLibrary struct {
537 Path android.Path
538 CrateName string
539}
540
Matthew Maurerbb3add12020-06-25 09:34:12 -0700541type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000542 exportLinkDirs(...string)
Ivan Lozano1f10f682024-11-08 16:16:50 +0000543 exportRustLibs(...string)
544 exportStaticLibs(...string)
545 exportWholeStaticLibs(...string)
546 exportSharedLibs(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700547}
548
Sasha Smundaka76acba2022-04-18 20:12:56 -0700549type xref interface {
550 XrefRustFiles() android.Paths
551}
552
Matthew Maurerbb3add12020-06-25 09:34:12 -0700553type flagExporter struct {
Ivan Lozano1f10f682024-11-08 16:16:50 +0000554 linkDirs []string
555 ccLinkDirs []string
556 rustLibPaths []string
557 staticLibObjects []string
558 sharedLibObjects []string
559 wholeStaticLibObjects []string
Ivan Lozano610eb1a2025-02-12 21:36:49 +0000560 wholeRustRlibDeps []cc.RustRlibDep
Matthew Maurerbb3add12020-06-25 09:34:12 -0700561}
562
Wen-yi Chu41326c12023-09-22 03:58:59 +0000563func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
564 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700565}
566
Ivan Lozano1f10f682024-11-08 16:16:50 +0000567func (flagExporter *flagExporter) exportRustLibs(flags ...string) {
568 flagExporter.rustLibPaths = android.FirstUniqueStrings(append(flagExporter.rustLibPaths, flags...))
569}
570
571func (flagExporter *flagExporter) exportStaticLibs(flags ...string) {
572 flagExporter.staticLibObjects = android.FirstUniqueStrings(append(flagExporter.staticLibObjects, flags...))
573}
574
575func (flagExporter *flagExporter) exportSharedLibs(flags ...string) {
576 flagExporter.sharedLibObjects = android.FirstUniqueStrings(append(flagExporter.sharedLibObjects, flags...))
577}
578
579func (flagExporter *flagExporter) exportWholeStaticLibs(flags ...string) {
580 flagExporter.wholeStaticLibObjects = android.FirstUniqueStrings(append(flagExporter.wholeStaticLibObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400581}
582
Ivan Lozano7fe65ca2025-02-05 02:22:33 +0000583func (flagExporter *flagExporter) setRustProvider(ctx ModuleContext) {
584 android.SetProvider(ctx, RustFlagExporterInfoProvider, RustFlagExporterInfo{
Ivan Lozano1f10f682024-11-08 16:16:50 +0000585 LinkDirs: flagExporter.linkDirs,
586 RustLibObjects: flagExporter.rustLibPaths,
587 StaticLibObjects: flagExporter.staticLibObjects,
588 WholeStaticLibObjects: flagExporter.wholeStaticLibObjects,
589 SharedLibPaths: flagExporter.sharedLibObjects,
Ivan Lozano610eb1a2025-02-12 21:36:49 +0000590 WholeRustRlibDeps: flagExporter.wholeRustRlibDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700591 })
592}
593
Matthew Maurerbb3add12020-06-25 09:34:12 -0700594var _ exportedFlagsProducer = (*flagExporter)(nil)
595
596func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700597 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700598}
599
Ivan Lozano7fe65ca2025-02-05 02:22:33 +0000600type RustFlagExporterInfo struct {
Ivan Lozano1f10f682024-11-08 16:16:50 +0000601 Flags []string
602 LinkDirs []string
603 RustLibObjects []string
604 StaticLibObjects []string
605 WholeStaticLibObjects []string
606 SharedLibPaths []string
Ivan Lozano610eb1a2025-02-12 21:36:49 +0000607 WholeRustRlibDeps []cc.RustRlibDep
Colin Cross0de8a1e2020-09-18 14:15:30 -0700608}
609
Ivan Lozano7fe65ca2025-02-05 02:22:33 +0000610var RustFlagExporterInfoProvider = blueprint.NewProvider[RustFlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700611
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400612func (mod *Module) isCoverageVariant() bool {
613 return mod.coverage.Properties.IsCoverageVariant
614}
615
616var _ cc.Coverage = (*Module)(nil)
617
Colin Crosse1a85552024-06-14 12:17:37 -0700618func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400619 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
620}
621
Ivan Lozanod7586b62021-04-01 09:49:36 -0400622func (mod *Module) VndkVersion() string {
623 return mod.Properties.VndkVersion
624}
625
Ivan Lozano0a468a42024-05-13 21:03:34 -0400626func (mod *Module) ExportedCrateLinkDirs() []string {
627 return mod.exportedLinkDirs
628}
629
Ivan Lozanod7586b62021-04-01 09:49:36 -0400630func (mod *Module) PreventInstall() bool {
631 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400632}
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000633func (c *Module) ForceDisableSanitizers() {
634 c.sanitize.Properties.ForceDisable = true
635}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400636
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400637func (mod *Module) MarkAsCoverageVariant(coverage bool) {
638 mod.coverage.Properties.IsCoverageVariant = coverage
639}
640
641func (mod *Module) EnableCoverageIfNeeded() {
642 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643}
644
645func defaultsFactory() android.Module {
646 return DefaultsFactory()
647}
648
649type Defaults struct {
650 android.ModuleBase
651 android.DefaultsModuleBase
652}
653
654func DefaultsFactory(props ...interface{}) android.Module {
655 module := &Defaults{}
656
657 module.AddProperties(props...)
658 module.AddProperties(
659 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800660 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500661 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100662 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400663 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664 &BaseCompilerProperties{},
665 &BinaryCompilerProperties{},
666 &LibraryCompilerProperties{},
667 &ProcMacroCompilerProperties{},
668 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400669 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700670 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400671 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400672 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200673 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500674 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000675 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700676 )
677
678 android.InitDefaultsModule(module)
679 return module
680}
681
682func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700683 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700684}
685
Ivan Lozano183a3212019-10-18 14:18:45 -0700686func (mod *Module) CcLibrary() bool {
687 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500688 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700689 return true
690 }
691 }
692 return false
693}
694
695func (mod *Module) CcLibraryInterface() bool {
696 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400697 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
698 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000699 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700700 return true
701 }
702 }
703 return false
704}
705
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400706func (mod *Module) RustLibraryInterface() bool {
707 if mod.compiler != nil {
708 if _, ok := mod.compiler.(libraryInterface); ok {
709 return true
710 }
711 }
712 return false
713}
714
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500715func (mod *Module) IsFuzzModule() bool {
716 if _, ok := mod.compiler.(*fuzzDecorator); ok {
717 return true
718 }
719 return false
720}
721
722func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
723 return mod.FuzzModule
724}
725
726func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
727 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
728 return fuzzer.fuzzPackagedModule
729 }
730 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
731}
732
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000733func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500734 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
735 return fuzzer.sharedLibraries
736 }
737 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
738}
739
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400740func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400741 if mod.compiler != nil {
742 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400743 }
744 return nil
745}
746
Ivan Lozano183a3212019-10-18 14:18:45 -0700747func (mod *Module) SetStatic() {
748 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700749 if library, ok := mod.compiler.(libraryInterface); ok {
750 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700751 return
752 }
753 }
754 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
755}
756
757func (mod *Module) SetShared() {
758 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700759 if library, ok := mod.compiler.(libraryInterface); ok {
760 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700761 return
762 }
763 }
764 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
765}
766
Ivan Lozano183a3212019-10-18 14:18:45 -0700767func (mod *Module) BuildStaticVariant() bool {
768 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700769 if library, ok := mod.compiler.(libraryInterface); ok {
770 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700771 }
772 }
773 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
774}
775
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400776func (mod *Module) BuildRlibVariant() bool {
777 if mod.compiler != nil {
778 if library, ok := mod.compiler.(libraryInterface); ok {
779 return library.buildRlib()
780 }
781 }
782 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
783}
784
Ivan Lozano183a3212019-10-18 14:18:45 -0700785func (mod *Module) BuildSharedVariant() bool {
786 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700787 if library, ok := mod.compiler.(libraryInterface); ok {
788 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700789 }
790 }
791 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
792}
793
Ivan Lozano183a3212019-10-18 14:18:45 -0700794func (mod *Module) Module() android.Module {
795 return mod
796}
797
Ivan Lozano183a3212019-10-18 14:18:45 -0700798func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400799 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700800}
801
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400802func (mod *Module) CoverageFiles() android.Paths {
803 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800804 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400805 }
806 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
807}
808
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400809// Rust does not produce gcno files, and therefore does not produce a coverage archive.
810func (mod *Module) CoverageOutputFile() android.OptionalPath {
811 return android.OptionalPath{}
812}
813
814func (mod *Module) IsNdk(config android.Config) bool {
815 return false
816}
817
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000818func (mod *Module) IsStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000819 if lib, ok := mod.compiler.(libraryInterface); ok {
820 return lib.BuildStubs()
821 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000822 return false
823}
824
Spandan Das10c41362024-12-03 01:33:09 +0000825func (mod *Module) HasStubsVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000826 if lib, ok := mod.compiler.(libraryInterface); ok {
827 return lib.HasStubsVariants()
828 }
Spandan Das10c41362024-12-03 01:33:09 +0000829 return false
830}
831
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000832func (mod *Module) ApexSdkVersion() android.ApiLevel {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000833 return mod.apexSdkVersion
834}
835
836func (mod *Module) RustApexExclude() bool {
837 return mod.ApexExclude()
838}
839
840func (mod *Module) getSharedFlags() *cc.SharedFlags {
841 shared := &mod.sharedFlags
842 if shared.FlagsMap == nil {
843 shared.NumSharedFlags = 0
844 shared.FlagsMap = make(map[string]string)
845 }
846 return shared
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000847}
848
Yu Liu0a37d422025-02-13 02:05:00 +0000849func (mod *Module) ImplementationModuleNameForMake() string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000850 name := mod.BaseModuleName()
851 if versioned, ok := mod.compiler.(cc.VersionedInterface); ok {
852 name = versioned.ImplementationModuleName(name)
853 }
854 return name
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000855}
856
857func (mod *Module) Multilib() string {
858 return mod.Arch().ArchType.Multilib
859}
860
861func (mod *Module) IsCrt() bool {
862 // Rust does not currently provide any crt modules.
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400863 return false
864}
865
Jiyong Park459feca2020-12-15 11:02:21 +0900866func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900867 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900868 return false
869 }
870
Jiyong Park459feca2020-12-15 11:02:21 +0900871 // The apex variant is not installable because it is included in the APEX and won't appear
872 // in the system partition as a standalone file.
873 if !apexInfo.IsForPlatform() {
874 return false
875 }
876
Jiyong Parke54f07e2021-04-07 15:08:04 +0900877 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900878}
879
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500880func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800881 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
882 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500883}
884
Ivan Lozano183a3212019-10-18 14:18:45 -0700885var _ cc.LinkableInterface = (*Module)(nil)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000886var _ cc.VersionedLinkableInterface = (*Module)(nil)
Ivan Lozano183a3212019-10-18 14:18:45 -0700887
Ivan Lozanoffee3342019-08-27 12:03:00 -0700888func (mod *Module) Init() android.Module {
889 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500890 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891
Yi Kong46c6e592022-01-20 22:55:00 +0800892 if mod.afdo != nil {
893 mod.AddProperties(mod.afdo.props()...)
894 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700895 if mod.compiler != nil {
896 mod.AddProperties(mod.compiler.compilerProps()...)
897 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400898 if mod.coverage != nil {
899 mod.AddProperties(mod.coverage.props()...)
900 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200901 if mod.clippy != nil {
902 mod.AddProperties(mod.clippy.props()...)
903 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400904 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700905 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400906 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500907 if mod.sanitize != nil {
908 mod.AddProperties(mod.sanitize.props()...)
909 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400910
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900912 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913
914 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915 return mod
916}
917
918func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
919 return &Module{
920 hod: hod,
921 multilib: multilib,
922 }
923}
924func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
925 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800926 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400927 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200928 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500929 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930 return module
931}
932
933type ModuleContext interface {
934 android.ModuleContext
935 ModuleContextIntf
936}
937
938type BaseModuleContext interface {
939 android.BaseModuleContext
940 ModuleContextIntf
941}
942
943type DepsContext interface {
944 android.BottomUpMutatorContext
945 ModuleContextIntf
946}
947
948type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200949 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951}
952
953type depsContext struct {
954 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955}
956
957type moduleContext struct {
958 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700959}
960
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200961type baseModuleContext struct {
962 android.BaseModuleContext
963}
964
965func (ctx *moduleContext) RustModule() *Module {
966 return ctx.Module().(*Module)
967}
968
969func (ctx *moduleContext) toolchain() config.Toolchain {
970 return ctx.RustModule().toolchain(ctx)
971}
972
973func (ctx *depsContext) RustModule() *Module {
974 return ctx.Module().(*Module)
975}
976
977func (ctx *depsContext) toolchain() config.Toolchain {
978 return ctx.RustModule().toolchain(ctx)
979}
980
981func (ctx *baseModuleContext) RustModule() *Module {
982 return ctx.Module().(*Module)
983}
984
985func (ctx *baseModuleContext) toolchain() config.Toolchain {
986 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400987}
988
989func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700990 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
991 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
992 return false
993 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400994 return mod.compiler != nil && mod.compiler.nativeCoverage()
995}
996
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000997func (mod *Module) SetStl(s string) {
998 // STL is a CC concept; do nothing for Rust
999}
1000
1001func (mod *Module) SetSdkVersion(s string) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001002 mod.Properties.Sdk_version = StringPtr(s)
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001003}
1004
1005func (mod *Module) SetMinSdkVersion(s string) {
1006 mod.Properties.Min_sdk_version = StringPtr(s)
1007}
1008
1009func (mod *Module) VersionedInterface() cc.VersionedInterface {
1010 if _, ok := mod.compiler.(cc.VersionedInterface); ok {
1011 return mod.compiler.(cc.VersionedInterface)
1012 }
1013 return nil
1014}
1015
Ivan Lozanod7586b62021-04-01 09:49:36 -04001016func (mod *Module) EverInstallable() bool {
1017 return mod.compiler != nil &&
1018 // Check to see whether the module is actually ever installable.
1019 mod.compiler.everInstallable()
1020}
1021
1022func (mod *Module) Installable() *bool {
1023 return mod.Properties.Installable
1024}
1025
Ivan Lozano872d5792022-03-23 17:31:39 -04001026func (mod *Module) ProcMacro() bool {
1027 if pm, ok := mod.compiler.(procMacroInterface); ok {
1028 return pm.ProcMacro()
1029 }
1030 return false
1031}
1032
Ivan Lozanoffee3342019-08-27 12:03:00 -07001033func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
1034 if mod.cachedToolchain == nil {
1035 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
1036 }
1037 return mod.cachedToolchain
1038}
1039
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +02001040func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
1041 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
1042}
1043
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1045}
1046
1047func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
1048 ctx := &moduleContext{
1049 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051
Colin Crossff694a82023-12-13 15:54:49 -08001052 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +09001053 if !apexInfo.IsForPlatform() {
1054 mod.hideApexVariantFromMake = true
1055 }
1056
Ivan Lozanoffee3342019-08-27 12:03:00 -07001057 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -05001058 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
1059
Ivan Lozanof1868af2022-04-12 13:08:36 -04001060 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -07001061
Ivan Lozanoffee3342019-08-27 12:03:00 -07001062 if !toolchain.Supported() {
1063 // This toolchain's unsupported, there's nothing to do for this mod.
1064 return
1065 }
1066
1067 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001068 // Export linkDirs for CC rust generatedlibs
1069 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
1070 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
1071
Ivan Lozanoffee3342019-08-27 12:03:00 -07001072 flags := Flags{
1073 Toolchain: toolchain,
1074 }
1075
Ivan Lozano67eada32021-09-23 11:50:33 -04001076 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +08001077 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -05001078 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +08001079 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001080 if mod.compiler != nil {
1081 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -04001082 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +00001083 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001084 }
1085 if mod.coverage != nil {
1086 flags, deps = mod.coverage.flags(ctx, flags, deps)
1087 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +02001088 if mod.clippy != nil {
1089 flags, deps = mod.clippy.flags(ctx, flags, deps)
1090 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001091 if mod.sanitize != nil {
1092 flags, deps = mod.sanitize.flags(ctx, flags, deps)
1093 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001094
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001095 // SourceProvider needs to call GenerateSource() before compiler calls
1096 // compile() so it can provide the source. A SourceProvider has
1097 // multiple variants (e.g. source, rlib, dylib). Only the "source"
1098 // variant is responsible for effectively generating the source. The
1099 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001100 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001101 if mod.compiler.(libraryInterface).source() {
1102 mod.sourceProvider.GenerateSource(ctx, deps)
1103 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1104 } else {
Yu Liu727204c2025-01-23 20:58:32 +00001105 sourceMod := actx.GetDirectDepProxyWithTag(mod.Name(), sourceDepTag)
1106 sourceLib := android.OtherModuleProviderOrDefault(ctx, sourceMod, RustInfoProvider).SourceProviderInfo
1107 mod.sourceProvider.setOutputFiles(sourceLib.Srcs)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001108 }
Colin Crossa6182ab2024-08-21 10:47:44 -07001109 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001110 }
1111
1112 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001113 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001114 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001115 if ctx.Failed() {
1116 return
1117 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001118 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -07001119 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001120 if buildOutput.kytheFile != nil {
1121 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1122 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001123 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001124
Dan Albert06feee92021-03-19 15:06:02 -07001125 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
1126
Colin Crossff694a82023-12-13 15:54:49 -08001127 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -04001128 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001129 // If the module has been specifically configure to not be installed then
1130 // hide from make as otherwise it will break when running inside make as the
1131 // output path to install will not be specified. Not all uninstallable
1132 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001133 // side dependencies. In particular, proc-macros need to be captured in the
1134 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001135 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001136 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001137 } else if !mod.installable(apexInfo) {
1138 mod.SkipInstall()
1139 }
1140
1141 // Still call install though, the installs will be stored as PackageSpecs to allow
1142 // using the outputs in a genrule.
1143 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001144 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001145 if ctx.Failed() {
1146 return
1147 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001148 // Export your own directory as a linkDir
1149 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1150
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001151 }
Chris Wailes74be7642021-07-22 16:20:28 -07001152
Colin Crossb614cd42024-10-11 12:52:21 -07001153 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1154 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1155 })
1156
Chris Wailes74be7642021-07-22 16:20:28 -07001157 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001158 }
Wei Lia1aa2972024-06-21 13:08:51 -07001159
Yu Liuf6f85492025-01-13 21:02:36 +00001160 linkableInfo := cc.CreateCommonLinkableInfo(ctx, mod)
Yu Liu8024b922024-12-20 23:31:32 +00001161 linkableInfo.Static = mod.Static()
1162 linkableInfo.Shared = mod.Shared()
1163 linkableInfo.CrateName = mod.CrateName()
1164 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001165 if lib, ok := mod.compiler.(cc.VersionedInterface); ok {
1166 linkableInfo.StubsVersion = lib.StubsVersion()
1167 }
1168
Yu Liu8024b922024-12-20 23:31:32 +00001169 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1170
1171 rustInfo := &RustInfo{
1172 AndroidMkSuffix: mod.AndroidMkSuffix(),
1173 RustSubName: mod.Properties.RustSubName,
1174 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
Yu Liu2a815b62025-02-21 20:46:25 +00001175 XrefRustFiles: mod.XrefRustFiles(),
Yu Liu8024b922024-12-20 23:31:32 +00001176 }
1177 if mod.compiler != nil {
1178 rustInfo.CompilerInfo = &CompilerInfo{
1179 NoStdlibs: mod.compiler.noStdlibs(),
1180 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1181 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1182 }
1183 if lib, ok := mod.compiler.(libraryInterface); ok {
1184 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1185 Dylib: lib.dylib(),
1186 Rlib: lib.rlib(),
1187 }
1188 }
1189 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1190 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1191 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1192 }
1193 }
1194 }
1195 if mod.sourceProvider != nil {
Yu Liu727204c2025-01-23 20:58:32 +00001196 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1197 Srcs: mod.sourceProvider.Srcs(),
1198 }
Yu Liu8024b922024-12-20 23:31:32 +00001199 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu727204c2025-01-23 20:58:32 +00001200 rustInfo.SourceProviderInfo.ProtobufDecoratorInfo = &ProtobufDecoratorInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001201 }
1202 }
1203 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001204
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001205 ccInfo := &cc.CcInfo{
1206 IsPrebuilt: mod.IsPrebuilt(),
1207 }
1208
Ivan Lozano9587f452025-01-08 03:17:19 +00001209 // Define the linker info if compiler != nil because Rust currently
1210 // does compilation and linking in one step. If this changes in the future,
1211 // move this as appropriate.
Cole Faustc9b88c92025-02-06 17:58:26 -08001212 baseCompilerProps := mod.compiler.baseCompilerProps()
Ivan Lozano9587f452025-01-08 03:17:19 +00001213 ccInfo.LinkerInfo = &cc.LinkerInfo{
Cole Faustc9b88c92025-02-06 17:58:26 -08001214 WholeStaticLibs: baseCompilerProps.Whole_static_libs.GetOrDefault(ctx, nil),
1215 StaticLibs: baseCompilerProps.Static_libs.GetOrDefault(ctx, nil),
1216 SharedLibs: baseCompilerProps.Shared_libs.GetOrDefault(ctx, nil),
Ivan Lozano9587f452025-01-08 03:17:19 +00001217 }
1218
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001219 android.SetProvider(ctx, cc.CcInfoProvider, ccInfo)
1220
mrziwang0cbd3b02024-06-20 16:39:25 -07001221 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001222
1223 buildComplianceMetadataInfo(ctx, mod, deps)
Cole Faust58eef4f2025-01-29 15:14:17 -08001224
1225 moduleInfoJSON := ctx.ModuleInfoJSON()
1226 if mod.compiler != nil {
1227 mod.compiler.moduleInfoJSON(ctx, moduleInfoJSON)
1228 }
mrziwang0cbd3b02024-06-20 16:39:25 -07001229}
1230
1231func (mod *Module) setOutputFiles(ctx ModuleContext) {
1232 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1233 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1234 } else if mod.OutputFile().Valid() {
1235 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1236 } else {
1237 ctx.SetOutputFiles(android.Paths{}, "")
1238 }
1239 if mod.compiler != nil {
1240 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1241 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001242}
1243
Wei Lia1aa2972024-06-21 13:08:51 -07001244func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1245 // Dump metadata that can not be done in android/compliance-metadata.go
1246 metadataInfo := ctx.ComplianceMetadataInfo()
1247 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1248 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1249
1250 // Static libs
Yu Liu727204c2025-01-23 20:58:32 +00001251 staticDeps := ctx.GetDirectDepsProxyWithTag(rlibDepTag)
Wei Lia1aa2972024-06-21 13:08:51 -07001252 staticDepNames := make([]string, 0, len(staticDeps))
1253 for _, dep := range staticDeps {
1254 staticDepNames = append(staticDepNames, dep.Name())
1255 }
Yu Liu727204c2025-01-23 20:58:32 +00001256 ccStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(false))
Wei Lia1aa2972024-06-21 13:08:51 -07001257 for _, dep := range ccStaticDeps {
1258 staticDepNames = append(staticDepNames, dep.Name())
1259 }
1260
1261 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1262 // C static libraries
1263 for _, dep := range deps.StaticLibs {
1264 staticDepPaths = append(staticDepPaths, dep.String())
1265 }
1266 // Rust static libraries
1267 for _, dep := range deps.RLibs {
1268 staticDepPaths = append(staticDepPaths, dep.Path.String())
1269 }
1270 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1271 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1272
1273 // C Whole static libs
Yu Liu727204c2025-01-23 20:58:32 +00001274 ccWholeStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(true))
Wei Lia1aa2972024-06-21 13:08:51 -07001275 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1276 for _, dep := range ccStaticDeps {
1277 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1278 }
1279 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1280}
1281
Ivan Lozanoffee3342019-08-27 12:03:00 -07001282func (mod *Module) deps(ctx DepsContext) Deps {
1283 deps := Deps{}
1284
1285 if mod.compiler != nil {
1286 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001287 }
1288 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001289 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001290 }
1291
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001292 if mod.coverage != nil {
1293 deps = mod.coverage.deps(ctx, deps)
1294 }
1295
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001296 if mod.sanitize != nil {
1297 deps = mod.sanitize.deps(ctx, deps)
1298 }
1299
Ivan Lozanoffee3342019-08-27 12:03:00 -07001300 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1301 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001302 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001303 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1304 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1305 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001306 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001307 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001308 return deps
1309
1310}
1311
Ivan Lozanoffee3342019-08-27 12:03:00 -07001312type dependencyTag struct {
1313 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001314 name string
1315 library bool
1316 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001317 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001318}
1319
Jiyong Park65b62242020-11-25 12:44:59 +09001320// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1321// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1322func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001323 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001324}
1325
1326var _ android.InstallNeededDependencyTag = dependencyTag{}
1327
Colin Cross65cb3142021-12-10 23:05:02 +00001328func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1329 if d.library && d.dynamic {
1330 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1331 }
1332 return nil
1333}
1334
Yu Liuc8884602024-03-15 18:48:38 +00001335func (d dependencyTag) PropagateAconfigValidation() bool {
1336 return d == rlibDepTag || d == sourceDepTag
1337}
1338
1339var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1340
Colin Cross65cb3142021-12-10 23:05:02 +00001341var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1342
Ivan Lozanoffee3342019-08-27 12:03:00 -07001343var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001344 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1345 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001346 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001347 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001348 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001349 dataLibDepTag = dependencyTag{name: "data lib"}
1350 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001351)
1352
Jiyong Park99644e92020-11-17 22:21:02 +09001353func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1354 tag, ok := depTag.(dependencyTag)
1355 return ok && tag == dylibDepTag
1356}
1357
Jiyong Park94e22fd2021-04-08 18:19:15 +09001358func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1359 tag, ok := depTag.(dependencyTag)
1360 return ok && tag == rlibDepTag
1361}
1362
Matthew Maurer0f003b12020-06-29 14:34:06 -07001363type autoDep struct {
1364 variation string
1365 depTag dependencyTag
1366}
1367
1368var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001369 sourceVariation = "source"
1370 rlibVariation = "rlib"
1371 dylibVariation = "dylib"
1372 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1373 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001374)
1375
1376type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001377 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001378}
1379
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001380func (mod *Module) begin(ctx BaseModuleContext) {
1381 if mod.coverage != nil {
1382 mod.coverage.begin(ctx)
1383 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001384 if mod.sanitize != nil {
1385 mod.sanitize.begin(ctx)
1386 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001387
1388 if mod.UseSdk() && mod.IsSdkVariant() {
1389 sdkVersion := ""
1390 if ctx.Device() {
1391 sdkVersion = mod.SdkVersion()
1392 }
1393 version, err := cc.NativeApiLevelFromUser(ctx, sdkVersion)
1394 if err != nil {
1395 ctx.PropertyErrorf("sdk_version", err.Error())
1396 mod.Properties.Sdk_version = nil
1397 } else {
1398 mod.Properties.Sdk_version = StringPtr(version.String())
1399 }
1400 }
1401
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001402}
1403
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001404func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001405 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001406 return p.prebuilt()
1407 }
1408 return nil
1409}
1410
Kiyoung Kim37693d02024-04-04 09:56:15 +09001411func (mod *Module) Symlinks() []string {
1412 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1413 return nil
1414}
1415
Yu Liu8024b922024-12-20 23:31:32 +00001416func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1417 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001418 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001419 if rustInfo.SnapshotInfo != nil {
Yu Liu0a37d422025-02-13 02:05:00 +00001420 baseName := commonInfo.BaseModuleName
Yu Liu8024b922024-12-20 23:31:32 +00001421 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001422 }
1423 }
Yu Liu8024b922024-12-20 23:31:32 +00001424 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001425}
1426
Yu Liu8024b922024-12-20 23:31:32 +00001427func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001428 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001429 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1430 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001431 }
1432 }
1433}
Andrew Walbran52533232024-03-19 11:36:04 +00001434
Ivan Lozanoffee3342019-08-27 12:03:00 -07001435func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1436 var depPaths PathDeps
1437
Yu Liu8024b922024-12-20 23:31:32 +00001438 directRlibDeps := []*cc.LinkableInfo{}
1439 directDylibDeps := []*cc.LinkableInfo{}
1440 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001441 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001442 directStaticLibDeps := [](*cc.LinkableInfo){}
1443 directSrcProvidersDeps := []*android.ModuleProxy{}
1444 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001445
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001446 // For the dependency from platform to apex, use the latest stubs
1447 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001448 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001449 if !apexInfo.IsForPlatform() {
1450 mod.apexSdkVersion = apexInfo.MinSdkVersion
1451 }
1452
1453 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1454 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1455 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1456 // (b/144430859)
1457 mod.apexSdkVersion = android.FutureApiLevel
1458 }
1459
Spandan Das604f3762023-03-16 22:51:40 +00001460 skipModuleList := map[string]bool{}
1461
Colin Crossa14fb6a2024-10-23 16:57:06 -07001462 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001463 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001464
Yu Liu8024b922024-12-20 23:31:32 +00001465 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001466 depName := ctx.OtherModuleName(dep)
1467 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001468 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1469
Spandan Das604f3762023-03-16 22:51:40 +00001470 if _, exists := skipModuleList[depName]; exists {
1471 return
1472 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001473
1474 if depTag == android.DarwinUniversalVariantTag {
1475 return
1476 }
1477
Yu Liu8024b922024-12-20 23:31:32 +00001478 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1479 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1480 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1481 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1482 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001483 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001484 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001485
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001486 switch {
1487 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001488 dylib := rustInfo.CompilerInfo.LibraryInfo
1489 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001490 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1491 return
1492 }
Yu Liu8024b922024-12-20 23:31:32 +00001493 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001494 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001495 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1496
Colin Crossb614cd42024-10-11 12:52:21 -07001497 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1498 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1499 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1500 }
1501
Yu Liu8024b922024-12-20 23:31:32 +00001502 if !rustInfo.CompilerInfo.NoStdlibs {
1503 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1504 if ctx.Device() {
1505 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1506 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001507 if rustDepStdLinkage != modStdLinkage {
1508 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1509 return
1510 }
1511 }
1512
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001513 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001514 rlib := rustInfo.CompilerInfo.LibraryInfo
1515 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001516 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001517 return
1518 }
Yu Liu8024b922024-12-20 23:31:32 +00001519 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001520 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001521 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1522
Ivan Lozano0a468a42024-05-13 21:03:34 -04001523 // rust_ffi rlibs may export include dirs, so collect those here.
1524 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1525 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001526 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001527
Colin Crossb614cd42024-10-11 12:52:21 -07001528 // rlibs are not installed, so don't add the output file to directImplementationDeps
1529 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1530 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1531 }
1532
Yu Liu8024b922024-12-20 23:31:32 +00001533 if !rustInfo.CompilerInfo.NoStdlibs {
1534 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1535 if ctx.Device() {
1536 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1537 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001538 if rustDepStdLinkage != modStdLinkage {
1539 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1540 return
1541 }
1542 }
1543
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001544 if !mod.Rlib() {
1545 depPaths.ccRlibDeps = append(depPaths.ccRlibDeps, exportedInfo.RustRlibDeps...)
1546 } else {
1547 // rlibs need to reexport these
1548 depPaths.reexportedCcRlibDeps = append(depPaths.reexportedCcRlibDeps, exportedInfo.RustRlibDeps...)
1549 }
1550
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001551 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001552 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001553 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001554 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001555 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001556
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001557 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001558 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001559 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001560 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001561 case cc.IsStaticDepTag(depTag):
1562 // Rust FFI rlibs should not be declared in a Rust modules
1563 // "static_libs" list as we can't handle them properly at the
1564 // moment (for example, they only produce an rlib-std variant).
1565 // Instead, a normal rust_library variant should be used.
1566 ctx.PropertyErrorf("static_libs",
1567 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1568 depName)
1569
Paul Duffind5cf92e2021-07-09 17:38:55 +01001570 }
1571
Yu Liu8024b922024-12-20 23:31:32 +00001572 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001573
Paul Duffind5cf92e2021-07-09 17:38:55 +01001574 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001575 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1576 // OS/Arch variant is used.
1577 var helper string
1578 if ctx.Host() {
1579 helper = "missing 'host_supported'?"
1580 } else {
1581 helper = "device module defined?"
1582 }
1583
Yu Liu8024b922024-12-20 23:31:32 +00001584 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001585 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1586 return
Yu Liu8024b922024-12-20 23:31:32 +00001587 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001588 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1589 return
1590 }
Yu Liu8024b922024-12-20 23:31:32 +00001591 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001592 }
1593
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001594 exportedRustInfo, _ := android.OtherModuleProvider(ctx, dep, RustFlagExporterInfoProvider)
Ivan Lozano7fe65ca2025-02-05 02:22:33 +00001595 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, RustFlagExporterInfoProvider)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001596 //Append the dependencies exported objects, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001597 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001598 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001599 depPaths.rustLibObjects = append(depPaths.rustLibObjects, exportedInfo.RustLibObjects...)
1600 depPaths.sharedLibObjects = append(depPaths.sharedLibObjects, exportedInfo.SharedLibPaths...)
1601 depPaths.staticLibObjects = append(depPaths.staticLibObjects, exportedInfo.StaticLibObjects...)
1602 depPaths.wholeStaticLibObjects = append(depPaths.wholeStaticLibObjects, exportedInfo.WholeStaticLibObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001603 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001604
1605 depPaths.reexportedWholeCcRlibDeps = append(depPaths.reexportedWholeCcRlibDeps, exportedRustInfo.WholeRustRlibDeps...)
1606 if !mod.Rlib() {
1607 depPaths.ccRlibDeps = append(depPaths.ccRlibDeps, exportedRustInfo.WholeRustRlibDeps...)
1608 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001609 }
1610
Ivan Lozanoffee3342019-08-27 12:03:00 -07001611 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001612 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001613 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001614 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001615 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001616 }
1617 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001618
Ivan Lozanod106efe2023-09-21 23:30:26 -04001619 if depTag == sourceDepTag {
1620 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001621 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001622 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001623 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1624 }
1625 }
1626 }
Yu Liu8024b922024-12-20 23:31:32 +00001627 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001628 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001629 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1630 if !hasRustInfo {
1631 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001632 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1633 return
1634 }
Yu Liu8024b922024-12-20 23:31:32 +00001635 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001636 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1637 return
1638 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001639 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001640 ccLibPath := linkableInfo.OutputFile
1641 if !ccLibPath.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001642 if !ctx.Config().AllowMissingDependencies() {
1643 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1644 } else {
1645 ctx.AddMissingDependencies([]string{depName})
1646 }
1647 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001648 }
1649
Ivan Lozano1f10f682024-11-08 16:16:50 +00001650 linkPath := linkPathFromFilePath(ccLibPath.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001651
Ivan Lozanoffee3342019-08-27 12:03:00 -07001652 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001653 switch {
1654 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001655 if cc.IsWholeStaticLib(depTag) {
1656 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1657 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001658 if mod.Binary() {
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001659 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1660 // final linkage, pass the args directly to the linker to handle these cases.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001661 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", ccLibPath.Path().String(), "-Wl,--no-whole-archive"}...)
1662 } else if libName, ok := libNameFromFilePath(ccLibPath.Path()); ok {
1663 depPaths.depFlags = append(depPaths.depFlags, "-lstatic:+whole-archive="+libName)
1664 depPaths.depLinkFlags = append(depPaths.depLinkFlags, ccLibPath.Path().String())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001665 } else {
1666 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 -05001667 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001668 }
1669
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001670 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001671 if cc.IsWholeStaticLib(depTag) {
1672 // Add whole staticlibs to wholeStaticLibObjects to propagate to Rust all dependents.
1673 depPaths.wholeStaticLibObjects = append(depPaths.wholeStaticLibObjects, ccLibPath.String())
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001674
1675 // We also propagate forward whole-static'd cc staticlibs with rust_ffi_rlib dependencies
1676 // We don't need to check a hypothetical exportedRustInfo.WholeRustRlibDeps because we
1677 // wouldn't expect a rust_ffi_rlib to be listed in `static_libs` (Soong explicitly disallows this)
1678 depPaths.reexportedWholeCcRlibDeps = append(depPaths.reexportedWholeCcRlibDeps, exportedInfo.RustRlibDeps...)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001679 } else {
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001680 // If not whole_static, add to staticLibObjects, which only propagate through rlibs to their dependents.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001681 depPaths.staticLibObjects = append(depPaths.staticLibObjects, ccLibPath.String())
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001682
1683 if mod.Rlib() {
1684 // rlibs propagate their inherited rust_ffi_rlibs forward.
1685 depPaths.reexportedCcRlibDeps = append(depPaths.reexportedCcRlibDeps, exportedInfo.RustRlibDeps...)
1686 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001687 }
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001688
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001689 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001690 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1691 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1692 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1693 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001694
1695 if !mod.Rlib() {
1696 // rlibs don't need to build the generated static library, so they don't need to track these.
1697 depPaths.ccRlibDeps = append(depPaths.ccRlibDeps, exportedInfo.RustRlibDeps...)
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001698 }
1699
Yu Liu8024b922024-12-20 23:31:32 +00001700 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001701
1702 // Record baseLibName for snapshots.
1703 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1704
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001705 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001706 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001707 // For the shared lib dependencies, we may link to the stub variant
1708 // of the dependency depending on the context (e.g. if this
1709 // dependency crosses the APEX boundaries).
1710 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1711
Colin Crossb614cd42024-10-11 12:52:21 -07001712 if !sharedLibraryInfo.IsStubs {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001713 // TODO(b/362509506): remove this additional check once all apex_exclude uses are switched to stubs.
1714 if !linkableInfo.RustApexExclude {
1715 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1716 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1717 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1718 }
Colin Crossb614cd42024-10-11 12:52:21 -07001719 }
1720 }
1721
Jiyong Park7d55b612021-06-11 17:22:09 +09001722 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1723 // object (either from stub or non-stub) to use.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001724 ccLibPath = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1725 if !ccLibPath.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001726 if !ctx.Config().AllowMissingDependencies() {
1727 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1728 } else {
1729 ctx.AddMissingDependencies([]string{depName})
1730 }
1731 return
1732 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001733 linkPath = linkPathFromFilePath(ccLibPath.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001734
Ivan Lozanoffee3342019-08-27 12:03:00 -07001735 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001736 depPaths.sharedLibObjects = append(depPaths.sharedLibObjects, ccLibPath.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001737 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1738 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1739 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1740 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001741 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001742
1743 // Record baseLibName for snapshots.
1744 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1745
Cole Faustb6e6f992023-08-17 17:42:26 -07001746 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001747 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001748 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001749 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001750 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1751 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1752 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001753 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001754 case depTag == cc.CrtBeginDepTag:
Ivan Lozano1f10f682024-11-08 16:16:50 +00001755 depPaths.CrtBegin = append(depPaths.CrtBegin, ccLibPath.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001756 case depTag == cc.CrtEndDepTag:
Ivan Lozano1f10f682024-11-08 16:16:50 +00001757 depPaths.CrtEnd = append(depPaths.CrtEnd, ccLibPath.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001758 }
1759
Ivan Lozano1f10f682024-11-08 16:16:50 +00001760 // Make sure shared dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001761 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1762 lib.exportLinkDirs(linkPath)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001763 lib.exportSharedLibs(ccLibPath.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001764 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001765 } else {
1766 switch {
1767 case depTag == cc.CrtBeginDepTag:
1768 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1769 case depTag == cc.CrtEndDepTag:
1770 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1771 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001772 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001773
Yu Liuc41eae52025-01-14 01:03:08 +00001774 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoProvider); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001775 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001776 // These are usually genrules which don't have per-target variants.
1777 directSrcDeps = append(directSrcDeps, srcDep)
1778 }
1779 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001780 })
1781
Colin Crossa14fb6a2024-10-23 16:57:06 -07001782 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001783
1784 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001785 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001786 for _, dep := range directRlibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001787 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001788 if alias, aliased := aliases[crateName]; aliased {
1789 crateName = alias
1790 }
Yu Liu8024b922024-12-20 23:31:32 +00001791 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001792 }
1793 var dylibDepFiles RustLibraries
1794 for _, dep := range directDylibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001795 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001796 if alias, aliased := aliases[crateName]; aliased {
1797 crateName = alias
1798 }
Yu Liu8024b922024-12-20 23:31:32 +00001799 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001800 }
1801 var procMacroDepFiles RustLibraries
1802 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001803 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001804 if alias, aliased := aliases[crateName]; aliased {
1805 crateName = alias
1806 }
Yu Liu8024b922024-12-20 23:31:32 +00001807 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001808 }
1809
Colin Cross004bd3f2023-10-02 11:39:17 -07001810 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001811 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001812 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001813 }
1814
Colin Cross004bd3f2023-10-02 11:39:17 -07001815 var sharedLibFiles android.Paths
1816 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001817 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001818 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001819 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001820 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001821 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001822 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001823 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001824 }
1825
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001826 var srcProviderDepFiles android.Paths
1827 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001828 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001829 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1830 }
1831 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001832 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001833 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1834 }
1835
Wen-yi Chu41326c12023-09-22 03:58:59 +00001836 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1837 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001838 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1839 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1840 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001841 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001842 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001843
1844 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001845 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001846 depPaths.rustLibObjects = android.FirstUniqueStrings(depPaths.rustLibObjects)
1847 depPaths.staticLibObjects = android.FirstUniqueStrings(depPaths.staticLibObjects)
1848 depPaths.wholeStaticLibObjects = android.FirstUniqueStrings(depPaths.wholeStaticLibObjects)
1849 depPaths.sharedLibObjects = android.FirstUniqueStrings(depPaths.sharedLibObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001850 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001851 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1852 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1853 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoad7ba592025-01-23 01:23:43 +00001854 depPaths.depLinkFlags = android.FirstUniqueStrings(depPaths.depLinkFlags)
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001855 depPaths.reexportedCcRlibDeps = android.FirstUniqueFunc(depPaths.reexportedCcRlibDeps, cc.EqRustRlibDeps)
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001856 depPaths.reexportedWholeCcRlibDeps = android.FirstUniqueFunc(depPaths.reexportedWholeCcRlibDeps, cc.EqRustRlibDeps)
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001857 depPaths.ccRlibDeps = android.FirstUniqueFunc(depPaths.ccRlibDeps, cc.EqRustRlibDeps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001858
1859 return depPaths
1860}
1861
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001862func (mod *Module) InstallInData() bool {
1863 if mod.compiler == nil {
1864 return false
1865 }
1866 return mod.compiler.inData()
1867}
1868
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001869func (mod *Module) InstallInRamdisk() bool {
1870 return mod.InRamdisk()
1871}
1872
1873func (mod *Module) InstallInVendorRamdisk() bool {
1874 return mod.InVendorRamdisk()
1875}
1876
1877func (mod *Module) InstallInRecovery() bool {
1878 return mod.InRecovery()
1879}
1880
Wen-yi Chu41326c12023-09-22 03:58:59 +00001881func linkPathFromFilePath(filepath android.Path) string {
1882 return strings.Split(filepath.String(), filepath.Base())[0]
1883}
1884
Spandan Das604f3762023-03-16 22:51:40 +00001885// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1886func (r *Module) usePublicApi() bool {
1887 return r.Device() && r.UseSdk()
1888}
1889
1890// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1891func (r *Module) useVendorApi() bool {
1892 return r.Device() && (r.InVendor() || r.InProduct())
1893}
1894
Ivan Lozanoffee3342019-08-27 12:03:00 -07001895func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1896 ctx := &depsContext{
1897 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001898 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001899
1900 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001901 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001902
1903 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001904 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001905 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001906
Ivan Lozano2b081132020-09-08 12:46:52 -04001907 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001908 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001909 stdLinkage = "rlib-std"
1910 }
1911
1912 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001913
Ivan Lozano2b081132020-09-08 12:46:52 -04001914 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1915 rlibDepVariations = append(rlibDepVariations,
1916 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1917 }
1918
Ivan Lozano1921e802021-05-20 13:39:16 -04001919 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001920 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001921 for _, lib := range deps.Rlibs {
1922 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001923 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001924 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001925
1926 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001927 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001928
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001929 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001930 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001931 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001932
Ivan Lozano1921e802021-05-20 13:39:16 -04001933 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001934 if deps.Rustlibs != nil {
1935 if !mod.compiler.Disabled() {
1936 for _, lib := range deps.Rustlibs {
1937 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1938 if autoDep.depTag == rlibDepTag {
1939 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001940 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1941
Ivan Lozanod106efe2023-09-21 23:30:26 -04001942 } else {
1943 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1944 // Check for the existence of the dylib deptag variant. Select it if available,
1945 // otherwise select the rlib variant.
1946 autoDepVariations := append(commonDepVariations,
1947 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001948 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1949 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001950
Ivan Lozanod106efe2023-09-21 23:30:26 -04001951 } else {
1952 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001953 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1954
Ivan Lozanod106efe2023-09-21 23:30:26 -04001955 }
1956 }
1957 }
1958 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1959 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001960 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001961 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001962
Ivan Lozano0a468a42024-05-13 21:03:34 -04001963 // Only add rustlib dependencies if they're source providers themselves.
1964 // This is used to track which crate names need to be added to the source generated
1965 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001966 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001967 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001968 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001969 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001970 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001971 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001972
Ivan Lozano1921e802021-05-20 13:39:16 -04001973 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001974 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001975 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001976 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001977 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001978 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001979 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001980 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001981 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001982 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1983
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001984 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001985 }
1986 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001987
1988 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001989 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001990 name, version := cc.StubsLibNameAndVersion(lib)
1991
1992 variations := []blueprint.Variation{
1993 {Mutator: "link", Variation: "shared"},
1994 }
Spandan Dasff665182024-09-11 18:48:44 +00001995 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001996 }
1997
1998 for _, lib := range deps.WholeStaticLibs {
1999 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04002000
2001 actx.AddVariationDependencies([]blueprint.Variation{
2002 {Mutator: "link", Variation: "static"},
2003 }, depTag, lib)
2004 }
2005
2006 for _, lib := range deps.StaticLibs {
2007 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04002008
2009 actx.AddVariationDependencies([]blueprint.Variation{
2010 {Mutator: "link", Variation: "static"},
2011 }, depTag, lib)
2012 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07002013
Zach Johnson3df4e632020-11-06 11:56:27 -08002014 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
2015
Colin Cross565cafd2020-09-25 18:47:38 -07002016 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08002017 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09002018 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07002019 }
Colin Crossfe605e12022-01-23 20:46:16 -08002020 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09002021 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07002022 }
2023
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04002024 if mod.sourceProvider != nil {
2025 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
2026 bindgen.Properties.Custom_bindgen != "" {
2027 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
2028 bindgen.Properties.Custom_bindgen)
2029 }
2030 }
Ivan Lozano1921e802021-05-20 13:39:16 -04002031
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04002032 actx.AddVariationDependencies([]blueprint.Variation{
2033 {Mutator: "link", Variation: "shared"},
2034 }, dataLibDepTag, deps.DataLibs...)
2035
2036 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
2037
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07002038 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07002039 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05002040
2041 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07002042}
2043
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002044func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07002045 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002046 mod.beginMutator(ctx)
2047 }
2048}
2049
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002050func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
2051 ctx := &baseModuleContext{
2052 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002053 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002054
2055 mod.begin(ctx)
2056}
2057
Ivan Lozanoffee3342019-08-27 12:03:00 -07002058func (mod *Module) Name() string {
2059 name := mod.ModuleBase.Name()
2060 if p, ok := mod.compiler.(interface {
2061 Name(string) string
2062 }); ok {
2063 name = p.Name(name)
2064 }
2065 return name
2066}
2067
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02002068func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04002069 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02002070 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04002071 }
2072}
2073
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002074var _ android.HostToolProvider = (*Module)(nil)
2075
2076func (mod *Module) HostToolPath() android.OptionalPath {
2077 if !mod.Host() {
2078 return android.OptionalPath{}
2079 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07002080 if binary, ok := mod.compiler.(*binaryDecorator); ok {
2081 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04002082 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
2083 // Even though proc-macros aren't strictly "tools", since they target the compiler
2084 // and act as compiler plugins, we treat them similarly.
2085 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002086 }
2087 return android.OptionalPath{}
2088}
2089
Jiyong Park99644e92020-11-17 22:21:02 +09002090var _ android.ApexModule = (*Module)(nil)
2091
Ivan Lozano24cf0362024-10-04 16:02:38 +00002092// If a module is marked for exclusion from apexes, don't provide apex variants.
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002093// TODO(b/362509506): remove this once all apex_exclude usages are removed.
Ivan Lozano24cf0362024-10-04 16:02:38 +00002094func (m *Module) CanHaveApexVariants() bool {
2095 if m.ApexExclude() {
2096 return false
2097 } else {
2098 return m.ApexModuleBase.CanHaveApexVariants()
2099 }
2100}
2101
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002102func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002103 return String(mod.Properties.Min_sdk_version)
2104}
2105
Jiyong Park45bf82e2020-12-15 22:29:02 +09002106// Implements android.ApexModule
Yu Liudf0b8392025-02-12 18:27:03 +00002107func (mod *Module) MinSdkVersionSupported(ctx android.BaseModuleContext) android.ApiLevel {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002108 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002109 if minSdkVersion == "apex_inherit" {
Yu Liudf0b8392025-02-12 18:27:03 +00002110 return android.MinApiLevel
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002111 }
2112
Yu Liudf0b8392025-02-12 18:27:03 +00002113 if minSdkVersion == "" {
2114 return android.NoneApiLevel
2115 }
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002116 // Not using nativeApiLevelFromUser because the context here is not
2117 // necessarily a native context.
Yu Liudf0b8392025-02-12 18:27:03 +00002118 ver, err := android.ApiLevelFromUserWithConfig(ctx.Config(), minSdkVersion)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002119 if err != nil {
Yu Liudf0b8392025-02-12 18:27:03 +00002120 return android.NoneApiLevel
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002121 }
2122
Yu Liudf0b8392025-02-12 18:27:03 +00002123 return ver
Jiyong Park99644e92020-11-17 22:21:02 +09002124}
2125
Jiyong Park45bf82e2020-12-15 22:29:02 +09002126// Implements android.ApexModule
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002127func (mod *Module) AlwaysRequiresPlatformApexVariant() bool {
2128 // stub libraries and native bridge libraries are always available to platform
2129 // TODO(b/362509506): remove the ApexExclude() check once all apex_exclude uses are switched to stubs.
2130 return mod.IsStubs() || mod.Target().NativeBridge == android.NativeBridgeEnabled || mod.ApexExclude()
2131}
2132
2133// Implements android.ApexModule
Yu Liuf1806032025-02-07 00:23:34 +00002134type RustDepInSameApexChecker struct {
2135 Static bool
2136 HasStubsVariants bool
2137 ApexExclude bool
2138 Host bool
2139}
2140
2141func (mod *Module) GetDepInSameApexChecker() android.DepInSameApexChecker {
2142 return RustDepInSameApexChecker{
2143 Static: mod.Static(),
2144 HasStubsVariants: mod.HasStubsVariants(),
2145 ApexExclude: mod.ApexExclude(),
2146 Host: mod.Host(),
2147 }
2148}
2149
2150func (r RustDepInSameApexChecker) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07002151 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09002152 return false
2153 }
2154
Yu Liuf1806032025-02-07 00:23:34 +00002155 if r.Static && cc.IsSharedDepTag(depTag) {
Colin Cross8acea3e2024-12-12 14:53:30 -08002156 // shared_lib dependency from a static lib is considered as crossing
2157 // the APEX boundary because the dependency doesn't actually is
2158 // linked; the dependency is used only during the compilation phase.
2159 return false
2160 }
2161
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002162 if depTag == cc.StubImplDepTag {
2163 // We don't track from an implementation library to its stubs.
2164 return false
2165 }
2166
2167 if cc.ExcludeInApexDepTag(depTag) {
2168 return false
2169 }
2170
2171 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
Yu Liuf1806032025-02-07 00:23:34 +00002172 if r.ApexExclude {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002173 return false
2174 }
2175
Jiyong Park99644e92020-11-17 22:21:02 +09002176 return true
2177}
2178
Yu Liuf1806032025-02-07 00:23:34 +00002179func (r RustDepInSameApexChecker) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
2180 if r.Host {
Colin Cross1cea5302024-12-03 16:40:08 -08002181 return false
2182 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002183 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
Yu Liuf1806032025-02-07 00:23:34 +00002184 if r.ApexExclude {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002185 return false
2186 }
2187
Yu Liuf1806032025-02-07 00:23:34 +00002188 if r.HasStubsVariants {
Colin Crossbd930bc2025-02-03 12:17:42 -08002189 if cc.IsSharedDepTag(depTag) && !cc.IsExplicitImplSharedDepTag(depTag) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002190 // dynamic dep to a stubs lib crosses APEX boundary
2191 return false
2192 }
2193 if cc.IsRuntimeDepTag(depTag) {
2194 // runtime dep to a stubs lib also crosses APEX boundary
2195 return false
2196 }
2197 if cc.IsHeaderDepTag(depTag) {
2198 return false
2199 }
2200 }
2201 return true
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002202}
2203
Jiyong Park99644e92020-11-17 22:21:02 +09002204// Overrides ApexModule.IsInstallabeToApex()
2205func (mod *Module) IsInstallableToApex() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002206 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2207 if mod.ApexExclude() {
2208 return false
2209 }
2210
Jiyong Park99644e92020-11-17 22:21:02 +09002211 if mod.compiler != nil {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002212 if lib, ok := mod.compiler.(libraryInterface); ok {
2213 return (lib.shared() || lib.dylib()) && !lib.BuildStubs()
Jiyong Park99644e92020-11-17 22:21:02 +09002214 }
2215 if _, ok := mod.compiler.(*binaryDecorator); ok {
2216 return true
2217 }
2218 }
2219 return false
2220}
2221
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05002222// If a library file has a "lib" prefix, extract the library name without the prefix.
2223func libNameFromFilePath(filepath android.Path) (string, bool) {
2224 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
2225 if strings.HasPrefix(libName, "lib") {
2226 libName = libName[3:]
2227 return libName, true
2228 }
2229 return "", false
2230}
2231
Sasha Smundaka76acba2022-04-18 20:12:56 -07002232func kytheExtractRustFactory() android.Singleton {
2233 return &kytheExtractRustSingleton{}
2234}
2235
2236type kytheExtractRustSingleton struct {
2237}
2238
2239func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2240 var xrefTargets android.Paths
Yu Liu2a815b62025-02-21 20:46:25 +00002241 ctx.VisitAllModuleProxies(func(module android.ModuleProxy) {
2242 if rustModule, ok := android.OtherModuleProvider(ctx, module, RustInfoProvider); ok {
2243 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles...)
Sasha Smundaka76acba2022-04-18 20:12:56 -07002244 }
2245 })
2246 if len(xrefTargets) > 0 {
2247 ctx.Phony("xref_rust", xrefTargets...)
2248 }
2249}
2250
Jihoon Kangf78a8902022-09-01 22:47:07 +00002251func (c *Module) Partition() string {
2252 return ""
2253}
2254
Ivan Lozanoffee3342019-08-27 12:03:00 -07002255var Bool = proptools.Bool
2256var BoolDefault = proptools.BoolDefault
2257var String = proptools.String
2258var StringPtr = proptools.StringPtr