blob: a02ca6073e05e59e300c3a7c6345e7c70e75eea7 [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
63}
64
65var RustInfoProvider = blueprint.NewProvider[*RustInfo]()
66
Ivan Lozanoffee3342019-08-27 12:03:00 -070067func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070068 android.RegisterModuleType("rust_defaults", defaultsFactory)
Colin Cross8a49a3d2024-05-20 12:22:27 -070069 android.PreDepsMutators(registerPreDepsMutators)
70 android.PostDepsMutators(registerPostDepsMutators)
Inseob Kim3b244062023-07-11 13:31:36 +090071 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070072 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020073 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000074 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070075}
76
Colin Cross8a49a3d2024-05-20 12:22:27 -070077func registerPreDepsMutators(ctx android.RegisterMutatorsContext) {
78 ctx.Transition("rust_libraries", &libraryTransitionMutator{})
79 ctx.Transition("rust_stdlinkage", &libstdTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070080 ctx.BottomUp("rust_begin", BeginMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070081}
82
83func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070084 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070085}
86
Ivan Lozanoffee3342019-08-27 12:03:00 -070087type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040088 GlobalRustFlags []string // Flags that apply globally to rust
89 GlobalLinkFlags []string // Flags that apply globally to linker
90 RustFlags []string // Flags that apply to rust
91 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020092 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070093 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070094 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040095 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020096 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070097 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070098}
99
100type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -0500101 AndroidMkRlibs []string `blueprint:"mutated"`
102 AndroidMkDylibs []string `blueprint:"mutated"`
103 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -0500104 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +0000105 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -0400106
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900107 ImageVariation string `blueprint:"mutated"`
108 VndkVersion string `blueprint:"mutated"`
109 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -0500110
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400111 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
112 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
113 // appended before SubName.
114 RustSubName string `blueprint:"mutated"`
115
Ivan Lozano6a884432020-12-02 09:15:16 -0500116 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +0000117 ProductVariantNeeded bool `blueprint:"mutated"`
118 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500119 CoreVariantNeeded bool `blueprint:"mutated"`
120 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -0700121 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -0800122 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500123 ExtraVariants []string `blueprint:"mutated"`
124
Ivan Lozanoa2268632021-07-22 10:52:06 -0400125 // Allows this module to use non-APEX version of libraries. Useful
126 // for building binaries that are started before APEXes are activated.
127 Bootstrap *bool
128
Ivan Lozano1921e802021-05-20 13:39:16 -0400129 // Used by vendor snapshot to record dependencies from snapshot modules.
130 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900131 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400132 SnapshotRlibs []string `blueprint:"mutated"`
133 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400134
Matthew Maurerc6868382021-07-13 14:12:37 -0700135 // Make this module available when building for ramdisk.
136 // On device without a dedicated recovery partition, the module is only
137 // available after switching root into
138 // /first_stage_ramdisk. To expose the module before switching root, install
139 // the recovery variant instead.
140 Ramdisk_available *bool
141
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500142 // Make this module available when building for vendor ramdisk.
143 // On device without a dedicated recovery partition, the module is only
144 // available after switching root into
145 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800146 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500147 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400148
Ivan Lozano1921e802021-05-20 13:39:16 -0400149 // Normally Soong uses the directory structure to decide which modules
150 // should be included (framework) or excluded (non-framework) from the
151 // different snapshots (vendor, recovery, etc.), but this property
152 // allows a partner to exclude a module normally thought of as a
153 // framework module from the vendor snapshot.
154 Exclude_from_vendor_snapshot *bool
155
156 // Normally Soong uses the directory structure to decide which modules
157 // should be included (framework) or excluded (non-framework) from the
158 // different snapshots (vendor, recovery, etc.), but this property
159 // allows a partner to exclude a module normally thought of as a
160 // framework module from the recovery snapshot.
161 Exclude_from_recovery_snapshot *bool
162
Matthew Maurer460ee942021-02-11 12:31:46 -0800163 // Make this module available when building for recovery
164 Recovery_available *bool
165
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000166 // The API level that this module is built against. The APIs of this API level will be
167 // visible at build time, but use of any APIs newer than min_sdk_version will render the
168 // module unloadable on older devices. In the future it will be possible to weakly-link new
169 // APIs, making the behavior match Java: such modules will load on older devices, but
170 // calling new APIs on devices that do not support them will result in a crash.
171 //
172 // This property has the same behavior as sdk_version does for Java modules. For those
173 // familiar with Android Gradle, the property behaves similarly to how compileSdkVersion
174 // does for Java code.
175 //
176 // In addition, setting this property causes two variants to be built, one for the platform
177 // and one for apps.
178 Sdk_version *string
179
180 // Minimum OS API level supported by this C or C++ module. This property becomes the value
181 // of the __ANDROID_API__ macro. When the C or C++ module is included in an APEX or an APK,
182 // this property is also used to ensure that the min_sdk_version of the containing module is
183 // not older (i.e. less) than this module's min_sdk_version. When not set, this property
184 // defaults to the value of sdk_version. When this is set to "apex_inherit", this tracks
185 // min_sdk_version of the containing APEX. When the module
186 // is not built for an APEX, "apex_inherit" defaults to sdk_version.
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500187 Min_sdk_version *string
188
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000189 // Variant is an SDK variant created by sdkMutator
190 IsSdkVariant bool `blueprint:"mutated"`
191
192 // Set by factories of module types that can only be referenced from variants compiled against
193 // the SDK.
194 AlwaysSdk bool `blueprint:"mutated"`
195
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900196 HideFromMake bool `blueprint:"mutated"`
197 PreventInstall bool `blueprint:"mutated"`
198
199 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700200}
201
202type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700203 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700204
Ivan Lozano6a884432020-12-02 09:15:16 -0500205 VendorProperties cc.VendorProperties
206
Ivan Lozanoffee3342019-08-27 12:03:00 -0700207 Properties BaseProperties
208
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000209 hod android.HostOrDeviceSupported
210 multilib android.Multilib
211 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700212
Ivan Lozano6a884432020-12-02 09:15:16 -0500213 makeLinkType string
214
Yi Kong46c6e592022-01-20 22:55:00 +0800215 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700216 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400217 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200218 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500219 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700220 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400221 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700222 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400223
Ivan Lozano0a468a42024-05-13 21:03:34 -0400224 exportedLinkDirs []string
225
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400226 // Output file to be installed, may be stripped or unstripped.
227 outputFile android.OptionalPath
228
Sasha Smundaka76acba2022-04-18 20:12:56 -0700229 // Cross-reference input file
230 kytheFiles android.Paths
231
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400232 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900233
234 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500235
236 // For apex variants, this is set as apex.min_sdk_version
237 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700238
Colin Crossa14fb6a2024-10-23 16:57:06 -0700239 transitiveAndroidMkSharedLibs depset.DepSet[string]
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000240
241 // Shared flags among stubs build rules of this module
242 sharedFlags cc.SharedFlags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243}
244
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500245func (mod *Module) Header() bool {
246 //TODO: If Rust libraries provide header variants, this needs to be updated.
247 return false
248}
249
250func (mod *Module) SetPreventInstall() {
251 mod.Properties.PreventInstall = true
252}
253
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500254func (mod *Module) SetHideFromMake() {
255 mod.Properties.HideFromMake = true
256}
257
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900258func (mod *Module) HiddenFromMake() bool {
259 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400260}
261
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500262func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500263 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
264 // nil since we need compiler to actually sanitize.
265 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500266}
267
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500268func (mod *Module) IsPrebuilt() bool {
269 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
270 return true
271 }
272 return false
273}
274
Ivan Lozano52767be2019-10-18 14:49:46 -0700275func (mod *Module) SelectedStl() string {
276 return ""
277}
278
Ivan Lozano2b262972019-11-21 12:30:50 -0800279func (mod *Module) NonCcVariants() bool {
280 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400281 if library, ok := mod.compiler.(libraryInterface); ok {
282 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800283 }
284 }
285 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
286}
287
Ivan Lozano52767be2019-10-18 14:49:46 -0700288func (mod *Module) Static() bool {
289 if mod.compiler != nil {
290 if library, ok := mod.compiler.(libraryInterface); ok {
291 return library.static()
292 }
293 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400294 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700295}
296
297func (mod *Module) Shared() bool {
298 if mod.compiler != nil {
299 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400300 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 }
302 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400303 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700304}
305
Ivan Lozanod7586b62021-04-01 09:49:36 -0400306func (mod *Module) Dylib() bool {
307 if mod.compiler != nil {
308 if library, ok := mod.compiler.(libraryInterface); ok {
309 return library.dylib()
310 }
311 }
312 return false
313}
314
Ivan Lozanod106efe2023-09-21 23:30:26 -0400315func (mod *Module) Source() bool {
316 if mod.compiler != nil {
317 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
318 return library.source()
319 }
320 }
321 return false
322}
323
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400324func (mod *Module) RlibStd() bool {
325 if mod.compiler != nil {
326 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
327 return library.rlibStd()
328 }
329 }
330 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
331}
332
Ivan Lozanod7586b62021-04-01 09:49:36 -0400333func (mod *Module) Rlib() bool {
334 if mod.compiler != nil {
335 if library, ok := mod.compiler.(libraryInterface); ok {
336 return library.rlib()
337 }
338 }
339 return false
340}
341
342func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400343 if binary, ok := mod.compiler.(binaryInterface); ok {
344 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400345 }
346 return false
347}
348
Justin Yun5e035862021-06-29 20:50:37 +0900349func (mod *Module) StaticExecutable() bool {
350 if !mod.Binary() {
351 return false
352 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400353 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900354}
355
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000356func (mod *Module) ApexExclude() bool {
357 if mod.compiler != nil {
358 if library, ok := mod.compiler.(libraryInterface); ok {
359 return library.apexExclude()
360 }
361 }
362 return false
363}
364
Ivan Lozanod7586b62021-04-01 09:49:36 -0400365func (mod *Module) Object() bool {
366 // Rust has no modules which produce only object files.
367 return false
368}
369
Ivan Lozano52767be2019-10-18 14:49:46 -0700370func (mod *Module) Toc() android.OptionalPath {
371 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400372 if lib, ok := mod.compiler.(libraryInterface); ok {
373 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700374 }
375 }
376 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
377}
378
Colin Crossc511bc52020-04-07 16:50:32 +0000379func (mod *Module) UseSdk() bool {
380 return false
381}
382
Ivan Lozanod7586b62021-04-01 09:49:36 -0400383func (mod *Module) RelativeInstallPath() string {
384 if mod.compiler != nil {
385 return mod.compiler.relativeInstallPath()
386 }
387 return ""
388}
389
Ivan Lozano52767be2019-10-18 14:49:46 -0700390func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500391 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700392}
393
Jiyong Park7d55b612021-06-11 17:22:09 +0900394func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400395 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900396}
397
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400398func (mod *Module) SubName() string {
399 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700400}
401
Ivan Lozanof1868af2022-04-12 13:08:36 -0400402func (mod *Module) IsVndkPrebuiltLibrary() bool {
403 // Rust modules do not provide VNDK prebuilts
404 return false
405}
406
407func (mod *Module) IsVendorPublicLibrary() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000408 // Rust modules do not currently support vendor_public_library
409 return false
Ivan Lozanof1868af2022-04-12 13:08:36 -0400410}
411
412func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
413 // Rust modules to not provide Sdk variants
414 return false
415}
416
Colin Cross127bb8b2020-12-16 16:46:01 -0800417func (c *Module) IsVndkPrivate() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000418 // Rust modules do not currently support VNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800419 return false
420}
421
422func (c *Module) IsLlndk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000423 // Rust modules do not currently support LLNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800424 return false
425}
426
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400427func (mod *Module) KernelHeadersDecorator() bool {
428 return false
429}
430
Colin Cross1f3f1302021-04-26 18:37:44 -0700431func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000432 // Rust modules do not currently support LLNDK variants
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400433 return false
434}
435
Colin Cross5271fea2021-04-27 13:06:04 -0700436func (m *Module) NeedsVendorPublicLibraryVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000437 // Rust modules do not currently support vendor_public_library
Colin Cross5271fea2021-04-27 13:06:04 -0700438 return false
439}
440
Ivan Lozanod7586b62021-04-01 09:49:36 -0400441func (mod *Module) HasLlndkStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000442 // Rust modules do not currently support LLNDK stubs
Ivan Lozanod7586b62021-04-01 09:49:36 -0400443 return false
444}
445
Ivan Lozano52767be2019-10-18 14:49:46 -0700446func (mod *Module) SdkVersion() string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000447 return String(mod.Properties.Sdk_version)
Ivan Lozano52767be2019-10-18 14:49:46 -0700448}
449
Colin Crossc511bc52020-04-07 16:50:32 +0000450func (mod *Module) AlwaysSdk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000451 return mod.Properties.AlwaysSdk
Colin Crossc511bc52020-04-07 16:50:32 +0000452}
453
Jiyong Park2286afd2020-06-16 21:58:53 +0900454func (mod *Module) IsSdkVariant() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000455 return mod.Properties.IsSdkVariant
Jiyong Park2286afd2020-06-16 21:58:53 +0900456}
457
Colin Cross1348ce32020-10-01 13:37:16 -0700458func (mod *Module) SplitPerApiLevel() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000459 return cc.CanUseSdk(mod) && mod.IsCrt()
Colin Cross1348ce32020-10-01 13:37:16 -0700460}
461
Sasha Smundaka76acba2022-04-18 20:12:56 -0700462func (mod *Module) XrefRustFiles() android.Paths {
463 return mod.kytheFiles
464}
465
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400467 Dylibs []string
468 Rlibs []string
469 Rustlibs []string
470 Stdlibs []string
471 ProcMacros []string
472 SharedLibs []string
473 StaticLibs []string
474 WholeStaticLibs []string
475 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700476
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400477 // Used for data dependencies adjacent to tests
478 DataLibs []string
479 DataBins []string
480
Colin Crossfe605e12022-01-23 20:46:16 -0800481 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700482}
483
484type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700485 DyLibs RustLibraries
486 RLibs RustLibraries
487 SharedLibs android.Paths
488 SharedLibDeps android.Paths
489 StaticLibs android.Paths
490 ProcMacros RustLibraries
491 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000492 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500493
494 // depFlags and depLinkFlags are rustc and linker (clang) flags.
495 depFlags []string
496 depLinkFlags []string
497
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000498 // track cc static-libs that have Rlib dependencies
Ivan Lozano610eb1a2025-02-12 21:36:49 +0000499 reexportedCcRlibDeps []cc.RustRlibDep
500 reexportedWholeCcRlibDeps []cc.RustRlibDep
501 ccRlibDeps []cc.RustRlibDep
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000502
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400503 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500504 // Both of these are exported and propagate to dependencies.
Ivan Lozano1f10f682024-11-08 16:16:50 +0000505 linkDirs []string
506 rustLibObjects []string
507 staticLibObjects []string
508 wholeStaticLibObjects []string
509 sharedLibObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700510
Ivan Lozano0a468a42024-05-13 21:03:34 -0400511 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
512 // cc_library_static dependants of rlibs.
513 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
514 exportedLinkDirs []string
515
Ivan Lozano45901ed2020-07-24 16:05:01 -0400516 // Used by bindgen modules which call clang
517 depClangFlags []string
518 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400519 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400520 depSystemIncludePaths android.Paths
521
Colin Crossfe605e12022-01-23 20:46:16 -0800522 CrtBegin android.Paths
523 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700524
525 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500526 SrcDeps android.Paths
527 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700528
529 directImplementationDeps android.Paths
530 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531}
532
533type RustLibraries []RustLibrary
534
535type RustLibrary struct {
536 Path android.Path
537 CrateName string
538}
539
Matthew Maurerbb3add12020-06-25 09:34:12 -0700540type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000541 exportLinkDirs(...string)
Ivan Lozano1f10f682024-11-08 16:16:50 +0000542 exportRustLibs(...string)
543 exportStaticLibs(...string)
544 exportWholeStaticLibs(...string)
545 exportSharedLibs(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700546}
547
Sasha Smundaka76acba2022-04-18 20:12:56 -0700548type xref interface {
549 XrefRustFiles() android.Paths
550}
551
Matthew Maurerbb3add12020-06-25 09:34:12 -0700552type flagExporter struct {
Ivan Lozano1f10f682024-11-08 16:16:50 +0000553 linkDirs []string
554 ccLinkDirs []string
555 rustLibPaths []string
556 staticLibObjects []string
557 sharedLibObjects []string
558 wholeStaticLibObjects []string
Ivan Lozano610eb1a2025-02-12 21:36:49 +0000559 wholeRustRlibDeps []cc.RustRlibDep
Matthew Maurerbb3add12020-06-25 09:34:12 -0700560}
561
Wen-yi Chu41326c12023-09-22 03:58:59 +0000562func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
563 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700564}
565
Ivan Lozano1f10f682024-11-08 16:16:50 +0000566func (flagExporter *flagExporter) exportRustLibs(flags ...string) {
567 flagExporter.rustLibPaths = android.FirstUniqueStrings(append(flagExporter.rustLibPaths, flags...))
568}
569
570func (flagExporter *flagExporter) exportStaticLibs(flags ...string) {
571 flagExporter.staticLibObjects = android.FirstUniqueStrings(append(flagExporter.staticLibObjects, flags...))
572}
573
574func (flagExporter *flagExporter) exportSharedLibs(flags ...string) {
575 flagExporter.sharedLibObjects = android.FirstUniqueStrings(append(flagExporter.sharedLibObjects, flags...))
576}
577
578func (flagExporter *flagExporter) exportWholeStaticLibs(flags ...string) {
579 flagExporter.wholeStaticLibObjects = android.FirstUniqueStrings(append(flagExporter.wholeStaticLibObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400580}
581
Ivan Lozano7fe65ca2025-02-05 02:22:33 +0000582func (flagExporter *flagExporter) setRustProvider(ctx ModuleContext) {
583 android.SetProvider(ctx, RustFlagExporterInfoProvider, RustFlagExporterInfo{
Ivan Lozano1f10f682024-11-08 16:16:50 +0000584 LinkDirs: flagExporter.linkDirs,
585 RustLibObjects: flagExporter.rustLibPaths,
586 StaticLibObjects: flagExporter.staticLibObjects,
587 WholeStaticLibObjects: flagExporter.wholeStaticLibObjects,
588 SharedLibPaths: flagExporter.sharedLibObjects,
Ivan Lozano610eb1a2025-02-12 21:36:49 +0000589 WholeRustRlibDeps: flagExporter.wholeRustRlibDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700590 })
591}
592
Matthew Maurerbb3add12020-06-25 09:34:12 -0700593var _ exportedFlagsProducer = (*flagExporter)(nil)
594
595func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700596 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700597}
598
Ivan Lozano7fe65ca2025-02-05 02:22:33 +0000599type RustFlagExporterInfo struct {
Ivan Lozano1f10f682024-11-08 16:16:50 +0000600 Flags []string
601 LinkDirs []string
602 RustLibObjects []string
603 StaticLibObjects []string
604 WholeStaticLibObjects []string
605 SharedLibPaths []string
Ivan Lozano610eb1a2025-02-12 21:36:49 +0000606 WholeRustRlibDeps []cc.RustRlibDep
Colin Cross0de8a1e2020-09-18 14:15:30 -0700607}
608
Ivan Lozano7fe65ca2025-02-05 02:22:33 +0000609var RustFlagExporterInfoProvider = blueprint.NewProvider[RustFlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700610
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400611func (mod *Module) isCoverageVariant() bool {
612 return mod.coverage.Properties.IsCoverageVariant
613}
614
615var _ cc.Coverage = (*Module)(nil)
616
Colin Crosse1a85552024-06-14 12:17:37 -0700617func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400618 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
619}
620
Ivan Lozanod7586b62021-04-01 09:49:36 -0400621func (mod *Module) VndkVersion() string {
622 return mod.Properties.VndkVersion
623}
624
Ivan Lozano0a468a42024-05-13 21:03:34 -0400625func (mod *Module) ExportedCrateLinkDirs() []string {
626 return mod.exportedLinkDirs
627}
628
Ivan Lozanod7586b62021-04-01 09:49:36 -0400629func (mod *Module) PreventInstall() bool {
630 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400631}
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000632func (c *Module) ForceDisableSanitizers() {
633 c.sanitize.Properties.ForceDisable = true
634}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400635
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400636func (mod *Module) MarkAsCoverageVariant(coverage bool) {
637 mod.coverage.Properties.IsCoverageVariant = coverage
638}
639
640func (mod *Module) EnableCoverageIfNeeded() {
641 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700642}
643
644func defaultsFactory() android.Module {
645 return DefaultsFactory()
646}
647
648type Defaults struct {
649 android.ModuleBase
650 android.DefaultsModuleBase
651}
652
653func DefaultsFactory(props ...interface{}) android.Module {
654 module := &Defaults{}
655
656 module.AddProperties(props...)
657 module.AddProperties(
658 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800659 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500660 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100661 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400662 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663 &BaseCompilerProperties{},
664 &BinaryCompilerProperties{},
665 &LibraryCompilerProperties{},
666 &ProcMacroCompilerProperties{},
667 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400668 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700669 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400670 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400671 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200672 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500673 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000674 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675 )
676
677 android.InitDefaultsModule(module)
678 return module
679}
680
681func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700682 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683}
684
Ivan Lozano183a3212019-10-18 14:18:45 -0700685func (mod *Module) CcLibrary() bool {
686 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500687 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700688 return true
689 }
690 }
691 return false
692}
693
694func (mod *Module) CcLibraryInterface() bool {
695 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400696 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
697 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000698 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700699 return true
700 }
701 }
702 return false
703}
704
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400705func (mod *Module) RustLibraryInterface() bool {
706 if mod.compiler != nil {
707 if _, ok := mod.compiler.(libraryInterface); ok {
708 return true
709 }
710 }
711 return false
712}
713
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500714func (mod *Module) IsFuzzModule() bool {
715 if _, ok := mod.compiler.(*fuzzDecorator); ok {
716 return true
717 }
718 return false
719}
720
721func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
722 return mod.FuzzModule
723}
724
725func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
726 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
727 return fuzzer.fuzzPackagedModule
728 }
729 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
730}
731
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000732func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500733 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
734 return fuzzer.sharedLibraries
735 }
736 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
737}
738
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400739func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400740 if mod.compiler != nil {
741 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400742 }
743 return nil
744}
745
Ivan Lozano183a3212019-10-18 14:18:45 -0700746func (mod *Module) SetStatic() {
747 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700748 if library, ok := mod.compiler.(libraryInterface); ok {
749 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700750 return
751 }
752 }
753 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
754}
755
756func (mod *Module) SetShared() {
757 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700758 if library, ok := mod.compiler.(libraryInterface); ok {
759 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700760 return
761 }
762 }
763 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
764}
765
Ivan Lozano183a3212019-10-18 14:18:45 -0700766func (mod *Module) BuildStaticVariant() bool {
767 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700768 if library, ok := mod.compiler.(libraryInterface); ok {
769 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700770 }
771 }
772 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
773}
774
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400775func (mod *Module) BuildRlibVariant() bool {
776 if mod.compiler != nil {
777 if library, ok := mod.compiler.(libraryInterface); ok {
778 return library.buildRlib()
779 }
780 }
781 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
782}
783
Ivan Lozano183a3212019-10-18 14:18:45 -0700784func (mod *Module) BuildSharedVariant() bool {
785 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700786 if library, ok := mod.compiler.(libraryInterface); ok {
787 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700788 }
789 }
790 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
791}
792
Ivan Lozano183a3212019-10-18 14:18:45 -0700793func (mod *Module) Module() android.Module {
794 return mod
795}
796
Ivan Lozano183a3212019-10-18 14:18:45 -0700797func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400798 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700799}
800
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400801func (mod *Module) CoverageFiles() android.Paths {
802 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800803 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400804 }
805 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
806}
807
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400808// Rust does not produce gcno files, and therefore does not produce a coverage archive.
809func (mod *Module) CoverageOutputFile() android.OptionalPath {
810 return android.OptionalPath{}
811}
812
813func (mod *Module) IsNdk(config android.Config) bool {
814 return false
815}
816
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000817func (mod *Module) IsStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000818 if lib, ok := mod.compiler.(libraryInterface); ok {
819 return lib.BuildStubs()
820 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000821 return false
822}
823
Spandan Das10c41362024-12-03 01:33:09 +0000824func (mod *Module) HasStubsVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000825 if lib, ok := mod.compiler.(libraryInterface); ok {
826 return lib.HasStubsVariants()
827 }
Spandan Das10c41362024-12-03 01:33:09 +0000828 return false
829}
830
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000831func (mod *Module) ApexSdkVersion() android.ApiLevel {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000832 return mod.apexSdkVersion
833}
834
835func (mod *Module) RustApexExclude() bool {
836 return mod.ApexExclude()
837}
838
839func (mod *Module) getSharedFlags() *cc.SharedFlags {
840 shared := &mod.sharedFlags
841 if shared.FlagsMap == nil {
842 shared.NumSharedFlags = 0
843 shared.FlagsMap = make(map[string]string)
844 }
845 return shared
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000846}
847
848func (mod *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000849 name := mod.BaseModuleName()
850 if versioned, ok := mod.compiler.(cc.VersionedInterface); ok {
851 name = versioned.ImplementationModuleName(name)
852 }
853 return name
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000854}
855
856func (mod *Module) Multilib() string {
857 return mod.Arch().ArchType.Multilib
858}
859
860func (mod *Module) IsCrt() bool {
861 // Rust does not currently provide any crt modules.
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400862 return false
863}
864
Jiyong Park459feca2020-12-15 11:02:21 +0900865func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900866 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900867 return false
868 }
869
Jiyong Park459feca2020-12-15 11:02:21 +0900870 // The apex variant is not installable because it is included in the APEX and won't appear
871 // in the system partition as a standalone file.
872 if !apexInfo.IsForPlatform() {
873 return false
874 }
875
Jiyong Parke54f07e2021-04-07 15:08:04 +0900876 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900877}
878
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500879func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800880 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
881 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500882}
883
Ivan Lozano183a3212019-10-18 14:18:45 -0700884var _ cc.LinkableInterface = (*Module)(nil)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000885var _ cc.VersionedLinkableInterface = (*Module)(nil)
Ivan Lozano183a3212019-10-18 14:18:45 -0700886
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887func (mod *Module) Init() android.Module {
888 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500889 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890
Yi Kong46c6e592022-01-20 22:55:00 +0800891 if mod.afdo != nil {
892 mod.AddProperties(mod.afdo.props()...)
893 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 if mod.compiler != nil {
895 mod.AddProperties(mod.compiler.compilerProps()...)
896 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400897 if mod.coverage != nil {
898 mod.AddProperties(mod.coverage.props()...)
899 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200900 if mod.clippy != nil {
901 mod.AddProperties(mod.clippy.props()...)
902 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400903 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700904 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400905 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500906 if mod.sanitize != nil {
907 mod.AddProperties(mod.sanitize.props()...)
908 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400909
Ivan Lozanoffee3342019-08-27 12:03:00 -0700910 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900911 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912
913 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914 return mod
915}
916
917func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
918 return &Module{
919 hod: hod,
920 multilib: multilib,
921 }
922}
923func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
924 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800925 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400926 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200927 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500928 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700929 return module
930}
931
932type ModuleContext interface {
933 android.ModuleContext
934 ModuleContextIntf
935}
936
937type BaseModuleContext interface {
938 android.BaseModuleContext
939 ModuleContextIntf
940}
941
942type DepsContext interface {
943 android.BottomUpMutatorContext
944 ModuleContextIntf
945}
946
947type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200948 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950}
951
952type depsContext struct {
953 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700954}
955
956type moduleContext struct {
957 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700958}
959
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200960type baseModuleContext struct {
961 android.BaseModuleContext
962}
963
964func (ctx *moduleContext) RustModule() *Module {
965 return ctx.Module().(*Module)
966}
967
968func (ctx *moduleContext) toolchain() config.Toolchain {
969 return ctx.RustModule().toolchain(ctx)
970}
971
972func (ctx *depsContext) RustModule() *Module {
973 return ctx.Module().(*Module)
974}
975
976func (ctx *depsContext) toolchain() config.Toolchain {
977 return ctx.RustModule().toolchain(ctx)
978}
979
980func (ctx *baseModuleContext) RustModule() *Module {
981 return ctx.Module().(*Module)
982}
983
984func (ctx *baseModuleContext) toolchain() config.Toolchain {
985 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400986}
987
988func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700989 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
990 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
991 return false
992 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400993 return mod.compiler != nil && mod.compiler.nativeCoverage()
994}
995
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000996func (mod *Module) SetStl(s string) {
997 // STL is a CC concept; do nothing for Rust
998}
999
1000func (mod *Module) SetSdkVersion(s string) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001001 mod.Properties.Sdk_version = StringPtr(s)
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001002}
1003
1004func (mod *Module) SetMinSdkVersion(s string) {
1005 mod.Properties.Min_sdk_version = StringPtr(s)
1006}
1007
1008func (mod *Module) VersionedInterface() cc.VersionedInterface {
1009 if _, ok := mod.compiler.(cc.VersionedInterface); ok {
1010 return mod.compiler.(cc.VersionedInterface)
1011 }
1012 return nil
1013}
1014
Ivan Lozanod7586b62021-04-01 09:49:36 -04001015func (mod *Module) EverInstallable() bool {
1016 return mod.compiler != nil &&
1017 // Check to see whether the module is actually ever installable.
1018 mod.compiler.everInstallable()
1019}
1020
1021func (mod *Module) Installable() *bool {
1022 return mod.Properties.Installable
1023}
1024
Ivan Lozano872d5792022-03-23 17:31:39 -04001025func (mod *Module) ProcMacro() bool {
1026 if pm, ok := mod.compiler.(procMacroInterface); ok {
1027 return pm.ProcMacro()
1028 }
1029 return false
1030}
1031
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
1033 if mod.cachedToolchain == nil {
1034 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
1035 }
1036 return mod.cachedToolchain
1037}
1038
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +02001039func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
1040 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
1041}
1042
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1044}
1045
1046func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
1047 ctx := &moduleContext{
1048 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001049 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050
Colin Crossff694a82023-12-13 15:54:49 -08001051 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +09001052 if !apexInfo.IsForPlatform() {
1053 mod.hideApexVariantFromMake = true
1054 }
1055
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -05001057 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
1058
Ivan Lozanof1868af2022-04-12 13:08:36 -04001059 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -07001060
Ivan Lozanoffee3342019-08-27 12:03:00 -07001061 if !toolchain.Supported() {
1062 // This toolchain's unsupported, there's nothing to do for this mod.
1063 return
1064 }
1065
1066 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001067 // Export linkDirs for CC rust generatedlibs
1068 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
1069 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
1070
Ivan Lozanoffee3342019-08-27 12:03:00 -07001071 flags := Flags{
1072 Toolchain: toolchain,
1073 }
1074
Ivan Lozano67eada32021-09-23 11:50:33 -04001075 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +08001076 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -05001077 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +08001078 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001079 if mod.compiler != nil {
1080 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -04001081 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +00001082 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001083 }
1084 if mod.coverage != nil {
1085 flags, deps = mod.coverage.flags(ctx, flags, deps)
1086 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +02001087 if mod.clippy != nil {
1088 flags, deps = mod.clippy.flags(ctx, flags, deps)
1089 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001090 if mod.sanitize != nil {
1091 flags, deps = mod.sanitize.flags(ctx, flags, deps)
1092 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001093
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001094 // SourceProvider needs to call GenerateSource() before compiler calls
1095 // compile() so it can provide the source. A SourceProvider has
1096 // multiple variants (e.g. source, rlib, dylib). Only the "source"
1097 // variant is responsible for effectively generating the source. The
1098 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001099 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001100 if mod.compiler.(libraryInterface).source() {
1101 mod.sourceProvider.GenerateSource(ctx, deps)
1102 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1103 } else {
Yu Liu727204c2025-01-23 20:58:32 +00001104 sourceMod := actx.GetDirectDepProxyWithTag(mod.Name(), sourceDepTag)
1105 sourceLib := android.OtherModuleProviderOrDefault(ctx, sourceMod, RustInfoProvider).SourceProviderInfo
1106 mod.sourceProvider.setOutputFiles(sourceLib.Srcs)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001107 }
Colin Crossa6182ab2024-08-21 10:47:44 -07001108 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001109 }
1110
1111 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001112 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001113 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001114 if ctx.Failed() {
1115 return
1116 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001117 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -07001118 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001119 if buildOutput.kytheFile != nil {
1120 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1121 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001122 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001123
Dan Albert06feee92021-03-19 15:06:02 -07001124 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
1125
Colin Crossff694a82023-12-13 15:54:49 -08001126 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -04001127 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001128 // If the module has been specifically configure to not be installed then
1129 // hide from make as otherwise it will break when running inside make as the
1130 // output path to install will not be specified. Not all uninstallable
1131 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001132 // side dependencies. In particular, proc-macros need to be captured in the
1133 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001134 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001135 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001136 } else if !mod.installable(apexInfo) {
1137 mod.SkipInstall()
1138 }
1139
1140 // Still call install though, the installs will be stored as PackageSpecs to allow
1141 // using the outputs in a genrule.
1142 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001143 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001144 if ctx.Failed() {
1145 return
1146 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001147 // Export your own directory as a linkDir
1148 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1149
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001150 }
Chris Wailes74be7642021-07-22 16:20:28 -07001151
Colin Crossb614cd42024-10-11 12:52:21 -07001152 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1153 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1154 })
1155
Chris Wailes74be7642021-07-22 16:20:28 -07001156 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001157 }
Wei Lia1aa2972024-06-21 13:08:51 -07001158
Yu Liuf6f85492025-01-13 21:02:36 +00001159 linkableInfo := cc.CreateCommonLinkableInfo(ctx, mod)
Yu Liu8024b922024-12-20 23:31:32 +00001160 linkableInfo.Static = mod.Static()
1161 linkableInfo.Shared = mod.Shared()
1162 linkableInfo.CrateName = mod.CrateName()
1163 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001164 if lib, ok := mod.compiler.(cc.VersionedInterface); ok {
1165 linkableInfo.StubsVersion = lib.StubsVersion()
1166 }
1167
Yu Liu8024b922024-12-20 23:31:32 +00001168 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1169
1170 rustInfo := &RustInfo{
1171 AndroidMkSuffix: mod.AndroidMkSuffix(),
1172 RustSubName: mod.Properties.RustSubName,
1173 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
1174 }
1175 if mod.compiler != nil {
1176 rustInfo.CompilerInfo = &CompilerInfo{
1177 NoStdlibs: mod.compiler.noStdlibs(),
1178 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1179 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1180 }
1181 if lib, ok := mod.compiler.(libraryInterface); ok {
1182 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1183 Dylib: lib.dylib(),
1184 Rlib: lib.rlib(),
1185 }
1186 }
1187 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1188 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1189 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1190 }
1191 }
1192 }
1193 if mod.sourceProvider != nil {
Yu Liu727204c2025-01-23 20:58:32 +00001194 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1195 Srcs: mod.sourceProvider.Srcs(),
1196 }
Yu Liu8024b922024-12-20 23:31:32 +00001197 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu727204c2025-01-23 20:58:32 +00001198 rustInfo.SourceProviderInfo.ProtobufDecoratorInfo = &ProtobufDecoratorInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001199 }
1200 }
1201 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001202
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001203 ccInfo := &cc.CcInfo{
1204 IsPrebuilt: mod.IsPrebuilt(),
1205 }
1206
Ivan Lozano9587f452025-01-08 03:17:19 +00001207 // Define the linker info if compiler != nil because Rust currently
1208 // does compilation and linking in one step. If this changes in the future,
1209 // move this as appropriate.
Cole Faustc9b88c92025-02-06 17:58:26 -08001210 baseCompilerProps := mod.compiler.baseCompilerProps()
Ivan Lozano9587f452025-01-08 03:17:19 +00001211 ccInfo.LinkerInfo = &cc.LinkerInfo{
Cole Faustc9b88c92025-02-06 17:58:26 -08001212 WholeStaticLibs: baseCompilerProps.Whole_static_libs.GetOrDefault(ctx, nil),
1213 StaticLibs: baseCompilerProps.Static_libs.GetOrDefault(ctx, nil),
1214 SharedLibs: baseCompilerProps.Shared_libs.GetOrDefault(ctx, nil),
Ivan Lozano9587f452025-01-08 03:17:19 +00001215 }
1216
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001217 android.SetProvider(ctx, cc.CcInfoProvider, ccInfo)
1218
mrziwang0cbd3b02024-06-20 16:39:25 -07001219 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001220
1221 buildComplianceMetadataInfo(ctx, mod, deps)
Cole Faust58eef4f2025-01-29 15:14:17 -08001222
1223 moduleInfoJSON := ctx.ModuleInfoJSON()
1224 if mod.compiler != nil {
1225 mod.compiler.moduleInfoJSON(ctx, moduleInfoJSON)
1226 }
mrziwang0cbd3b02024-06-20 16:39:25 -07001227}
1228
1229func (mod *Module) setOutputFiles(ctx ModuleContext) {
1230 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1231 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1232 } else if mod.OutputFile().Valid() {
1233 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1234 } else {
1235 ctx.SetOutputFiles(android.Paths{}, "")
1236 }
1237 if mod.compiler != nil {
1238 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1239 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001240}
1241
Wei Lia1aa2972024-06-21 13:08:51 -07001242func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1243 // Dump metadata that can not be done in android/compliance-metadata.go
1244 metadataInfo := ctx.ComplianceMetadataInfo()
1245 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1246 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1247
1248 // Static libs
Yu Liu727204c2025-01-23 20:58:32 +00001249 staticDeps := ctx.GetDirectDepsProxyWithTag(rlibDepTag)
Wei Lia1aa2972024-06-21 13:08:51 -07001250 staticDepNames := make([]string, 0, len(staticDeps))
1251 for _, dep := range staticDeps {
1252 staticDepNames = append(staticDepNames, dep.Name())
1253 }
Yu Liu727204c2025-01-23 20:58:32 +00001254 ccStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(false))
Wei Lia1aa2972024-06-21 13:08:51 -07001255 for _, dep := range ccStaticDeps {
1256 staticDepNames = append(staticDepNames, dep.Name())
1257 }
1258
1259 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1260 // C static libraries
1261 for _, dep := range deps.StaticLibs {
1262 staticDepPaths = append(staticDepPaths, dep.String())
1263 }
1264 // Rust static libraries
1265 for _, dep := range deps.RLibs {
1266 staticDepPaths = append(staticDepPaths, dep.Path.String())
1267 }
1268 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1269 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1270
1271 // C Whole static libs
Yu Liu727204c2025-01-23 20:58:32 +00001272 ccWholeStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(true))
Wei Lia1aa2972024-06-21 13:08:51 -07001273 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1274 for _, dep := range ccStaticDeps {
1275 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1276 }
1277 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1278}
1279
Ivan Lozanoffee3342019-08-27 12:03:00 -07001280func (mod *Module) deps(ctx DepsContext) Deps {
1281 deps := Deps{}
1282
1283 if mod.compiler != nil {
1284 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001285 }
1286 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001287 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001288 }
1289
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001290 if mod.coverage != nil {
1291 deps = mod.coverage.deps(ctx, deps)
1292 }
1293
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001294 if mod.sanitize != nil {
1295 deps = mod.sanitize.deps(ctx, deps)
1296 }
1297
Ivan Lozanoffee3342019-08-27 12:03:00 -07001298 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1299 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001300 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001301 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1302 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1303 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001304 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001305 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001306 return deps
1307
1308}
1309
Ivan Lozanoffee3342019-08-27 12:03:00 -07001310type dependencyTag struct {
1311 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001312 name string
1313 library bool
1314 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001315 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001316}
1317
Jiyong Park65b62242020-11-25 12:44:59 +09001318// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1319// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1320func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001321 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001322}
1323
1324var _ android.InstallNeededDependencyTag = dependencyTag{}
1325
Colin Cross65cb3142021-12-10 23:05:02 +00001326func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1327 if d.library && d.dynamic {
1328 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1329 }
1330 return nil
1331}
1332
Yu Liuc8884602024-03-15 18:48:38 +00001333func (d dependencyTag) PropagateAconfigValidation() bool {
1334 return d == rlibDepTag || d == sourceDepTag
1335}
1336
1337var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1338
Colin Cross65cb3142021-12-10 23:05:02 +00001339var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1340
Ivan Lozanoffee3342019-08-27 12:03:00 -07001341var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001342 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1343 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001344 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001345 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001346 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001347 dataLibDepTag = dependencyTag{name: "data lib"}
1348 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001349)
1350
Jiyong Park99644e92020-11-17 22:21:02 +09001351func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1352 tag, ok := depTag.(dependencyTag)
1353 return ok && tag == dylibDepTag
1354}
1355
Jiyong Park94e22fd2021-04-08 18:19:15 +09001356func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1357 tag, ok := depTag.(dependencyTag)
1358 return ok && tag == rlibDepTag
1359}
1360
Matthew Maurer0f003b12020-06-29 14:34:06 -07001361type autoDep struct {
1362 variation string
1363 depTag dependencyTag
1364}
1365
1366var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001367 sourceVariation = "source"
1368 rlibVariation = "rlib"
1369 dylibVariation = "dylib"
1370 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1371 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001372)
1373
1374type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001375 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001376}
1377
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001378func (mod *Module) begin(ctx BaseModuleContext) {
1379 if mod.coverage != nil {
1380 mod.coverage.begin(ctx)
1381 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001382 if mod.sanitize != nil {
1383 mod.sanitize.begin(ctx)
1384 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001385
1386 if mod.UseSdk() && mod.IsSdkVariant() {
1387 sdkVersion := ""
1388 if ctx.Device() {
1389 sdkVersion = mod.SdkVersion()
1390 }
1391 version, err := cc.NativeApiLevelFromUser(ctx, sdkVersion)
1392 if err != nil {
1393 ctx.PropertyErrorf("sdk_version", err.Error())
1394 mod.Properties.Sdk_version = nil
1395 } else {
1396 mod.Properties.Sdk_version = StringPtr(version.String())
1397 }
1398 }
1399
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001400}
1401
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001402func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001403 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001404 return p.prebuilt()
1405 }
1406 return nil
1407}
1408
Kiyoung Kim37693d02024-04-04 09:56:15 +09001409func (mod *Module) Symlinks() []string {
1410 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1411 return nil
1412}
1413
Yu Liu8024b922024-12-20 23:31:32 +00001414func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1415 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001416 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001417 if rustInfo.SnapshotInfo != nil {
1418 baseName := linkableInfo.BaseModuleName
1419 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001420 }
1421 }
Yu Liu8024b922024-12-20 23:31:32 +00001422 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001423}
1424
Yu Liu8024b922024-12-20 23:31:32 +00001425func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001426 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001427 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1428 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001429 }
1430 }
1431}
Andrew Walbran52533232024-03-19 11:36:04 +00001432
Ivan Lozanoffee3342019-08-27 12:03:00 -07001433func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1434 var depPaths PathDeps
1435
Yu Liu8024b922024-12-20 23:31:32 +00001436 directRlibDeps := []*cc.LinkableInfo{}
1437 directDylibDeps := []*cc.LinkableInfo{}
1438 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001439 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001440 directStaticLibDeps := [](*cc.LinkableInfo){}
1441 directSrcProvidersDeps := []*android.ModuleProxy{}
1442 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001444 // For the dependency from platform to apex, use the latest stubs
1445 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001446 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001447 if !apexInfo.IsForPlatform() {
1448 mod.apexSdkVersion = apexInfo.MinSdkVersion
1449 }
1450
1451 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1452 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1453 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1454 // (b/144430859)
1455 mod.apexSdkVersion = android.FutureApiLevel
1456 }
1457
Spandan Das604f3762023-03-16 22:51:40 +00001458 skipModuleList := map[string]bool{}
1459
Colin Crossa14fb6a2024-10-23 16:57:06 -07001460 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001461 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001462
Yu Liu8024b922024-12-20 23:31:32 +00001463 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001464 depName := ctx.OtherModuleName(dep)
1465 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001466 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1467
Spandan Das604f3762023-03-16 22:51:40 +00001468 if _, exists := skipModuleList[depName]; exists {
1469 return
1470 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001471
1472 if depTag == android.DarwinUniversalVariantTag {
1473 return
1474 }
1475
Yu Liu8024b922024-12-20 23:31:32 +00001476 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1477 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1478 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1479 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1480 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001481 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001482 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001483
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001484 switch {
1485 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001486 dylib := rustInfo.CompilerInfo.LibraryInfo
1487 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001488 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1489 return
1490 }
Yu Liu8024b922024-12-20 23:31:32 +00001491 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001492 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001493 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1494
Colin Crossb614cd42024-10-11 12:52:21 -07001495 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1496 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1497 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1498 }
1499
Yu Liu8024b922024-12-20 23:31:32 +00001500 if !rustInfo.CompilerInfo.NoStdlibs {
1501 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1502 if ctx.Device() {
1503 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1504 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001505 if rustDepStdLinkage != modStdLinkage {
1506 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1507 return
1508 }
1509 }
1510
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001511 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001512 rlib := rustInfo.CompilerInfo.LibraryInfo
1513 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001514 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001515 return
1516 }
Yu Liu8024b922024-12-20 23:31:32 +00001517 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001518 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001519 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1520
Ivan Lozano0a468a42024-05-13 21:03:34 -04001521 // rust_ffi rlibs may export include dirs, so collect those here.
1522 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1523 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001524 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001525
Colin Crossb614cd42024-10-11 12:52:21 -07001526 // rlibs are not installed, so don't add the output file to directImplementationDeps
1527 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1528 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1529 }
1530
Yu Liu8024b922024-12-20 23:31:32 +00001531 if !rustInfo.CompilerInfo.NoStdlibs {
1532 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1533 if ctx.Device() {
1534 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1535 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001536 if rustDepStdLinkage != modStdLinkage {
1537 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1538 return
1539 }
1540 }
1541
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001542 if !mod.Rlib() {
1543 depPaths.ccRlibDeps = append(depPaths.ccRlibDeps, exportedInfo.RustRlibDeps...)
1544 } else {
1545 // rlibs need to reexport these
1546 depPaths.reexportedCcRlibDeps = append(depPaths.reexportedCcRlibDeps, exportedInfo.RustRlibDeps...)
1547 }
1548
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001549 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001550 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001551 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001552 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001553 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001554
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001555 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001556 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001557 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001558 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001559 case cc.IsStaticDepTag(depTag):
1560 // Rust FFI rlibs should not be declared in a Rust modules
1561 // "static_libs" list as we can't handle them properly at the
1562 // moment (for example, they only produce an rlib-std variant).
1563 // Instead, a normal rust_library variant should be used.
1564 ctx.PropertyErrorf("static_libs",
1565 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1566 depName)
1567
Paul Duffind5cf92e2021-07-09 17:38:55 +01001568 }
1569
Yu Liu8024b922024-12-20 23:31:32 +00001570 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001571
Paul Duffind5cf92e2021-07-09 17:38:55 +01001572 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001573 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1574 // OS/Arch variant is used.
1575 var helper string
1576 if ctx.Host() {
1577 helper = "missing 'host_supported'?"
1578 } else {
1579 helper = "device module defined?"
1580 }
1581
Yu Liu8024b922024-12-20 23:31:32 +00001582 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001583 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1584 return
Yu Liu8024b922024-12-20 23:31:32 +00001585 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001586 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1587 return
1588 }
Yu Liu8024b922024-12-20 23:31:32 +00001589 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001590 }
1591
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001592 exportedRustInfo, _ := android.OtherModuleProvider(ctx, dep, RustFlagExporterInfoProvider)
Ivan Lozano7fe65ca2025-02-05 02:22:33 +00001593 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, RustFlagExporterInfoProvider)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001594 //Append the dependencies exported objects, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001595 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001596 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001597 depPaths.rustLibObjects = append(depPaths.rustLibObjects, exportedInfo.RustLibObjects...)
1598 depPaths.sharedLibObjects = append(depPaths.sharedLibObjects, exportedInfo.SharedLibPaths...)
1599 depPaths.staticLibObjects = append(depPaths.staticLibObjects, exportedInfo.StaticLibObjects...)
1600 depPaths.wholeStaticLibObjects = append(depPaths.wholeStaticLibObjects, exportedInfo.WholeStaticLibObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001601 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001602
1603 depPaths.reexportedWholeCcRlibDeps = append(depPaths.reexportedWholeCcRlibDeps, exportedRustInfo.WholeRustRlibDeps...)
1604 if !mod.Rlib() {
1605 depPaths.ccRlibDeps = append(depPaths.ccRlibDeps, exportedRustInfo.WholeRustRlibDeps...)
1606 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001607 }
1608
Ivan Lozanoffee3342019-08-27 12:03:00 -07001609 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001610 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001611 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001612 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001613 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001614 }
1615 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001616
Ivan Lozanod106efe2023-09-21 23:30:26 -04001617 if depTag == sourceDepTag {
1618 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001619 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001620 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001621 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1622 }
1623 }
1624 }
Yu Liu8024b922024-12-20 23:31:32 +00001625 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001626 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001627 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1628 if !hasRustInfo {
1629 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001630 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1631 return
1632 }
Yu Liu8024b922024-12-20 23:31:32 +00001633 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001634 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1635 return
1636 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001637 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001638 ccLibPath := linkableInfo.OutputFile
1639 if !ccLibPath.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001640 if !ctx.Config().AllowMissingDependencies() {
1641 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1642 } else {
1643 ctx.AddMissingDependencies([]string{depName})
1644 }
1645 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001646 }
1647
Ivan Lozano1f10f682024-11-08 16:16:50 +00001648 linkPath := linkPathFromFilePath(ccLibPath.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001649
Ivan Lozanoffee3342019-08-27 12:03:00 -07001650 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001651 switch {
1652 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001653 if cc.IsWholeStaticLib(depTag) {
1654 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1655 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001656 if mod.Binary() {
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001657 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1658 // final linkage, pass the args directly to the linker to handle these cases.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001659 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", ccLibPath.Path().String(), "-Wl,--no-whole-archive"}...)
1660 } else if libName, ok := libNameFromFilePath(ccLibPath.Path()); ok {
1661 depPaths.depFlags = append(depPaths.depFlags, "-lstatic:+whole-archive="+libName)
1662 depPaths.depLinkFlags = append(depPaths.depLinkFlags, ccLibPath.Path().String())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001663 } else {
1664 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 -05001665 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001666 }
1667
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001668 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001669 if cc.IsWholeStaticLib(depTag) {
1670 // Add whole staticlibs to wholeStaticLibObjects to propagate to Rust all dependents.
1671 depPaths.wholeStaticLibObjects = append(depPaths.wholeStaticLibObjects, ccLibPath.String())
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001672
1673 // We also propagate forward whole-static'd cc staticlibs with rust_ffi_rlib dependencies
1674 // We don't need to check a hypothetical exportedRustInfo.WholeRustRlibDeps because we
1675 // wouldn't expect a rust_ffi_rlib to be listed in `static_libs` (Soong explicitly disallows this)
1676 depPaths.reexportedWholeCcRlibDeps = append(depPaths.reexportedWholeCcRlibDeps, exportedInfo.RustRlibDeps...)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001677 } else {
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001678 // If not whole_static, add to staticLibObjects, which only propagate through rlibs to their dependents.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001679 depPaths.staticLibObjects = append(depPaths.staticLibObjects, ccLibPath.String())
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001680
1681 if mod.Rlib() {
1682 // rlibs propagate their inherited rust_ffi_rlibs forward.
1683 depPaths.reexportedCcRlibDeps = append(depPaths.reexportedCcRlibDeps, exportedInfo.RustRlibDeps...)
1684 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001685 }
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001686
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001687 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001688 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1689 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1690 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1691 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001692
1693 if !mod.Rlib() {
1694 // rlibs don't need to build the generated static library, so they don't need to track these.
1695 depPaths.ccRlibDeps = append(depPaths.ccRlibDeps, exportedInfo.RustRlibDeps...)
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001696 }
1697
Yu Liu8024b922024-12-20 23:31:32 +00001698 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001699
1700 // Record baseLibName for snapshots.
1701 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1702
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001703 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001704 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001705 // For the shared lib dependencies, we may link to the stub variant
1706 // of the dependency depending on the context (e.g. if this
1707 // dependency crosses the APEX boundaries).
1708 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1709
Colin Crossb614cd42024-10-11 12:52:21 -07001710 if !sharedLibraryInfo.IsStubs {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001711 // TODO(b/362509506): remove this additional check once all apex_exclude uses are switched to stubs.
1712 if !linkableInfo.RustApexExclude {
1713 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1714 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1715 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1716 }
Colin Crossb614cd42024-10-11 12:52:21 -07001717 }
1718 }
1719
Jiyong Park7d55b612021-06-11 17:22:09 +09001720 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1721 // object (either from stub or non-stub) to use.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001722 ccLibPath = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1723 if !ccLibPath.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001724 if !ctx.Config().AllowMissingDependencies() {
1725 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1726 } else {
1727 ctx.AddMissingDependencies([]string{depName})
1728 }
1729 return
1730 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001731 linkPath = linkPathFromFilePath(ccLibPath.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001732
Ivan Lozanoffee3342019-08-27 12:03:00 -07001733 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001734 depPaths.sharedLibObjects = append(depPaths.sharedLibObjects, ccLibPath.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001735 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1736 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1737 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1738 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001739 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001740
1741 // Record baseLibName for snapshots.
1742 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1743
Cole Faustb6e6f992023-08-17 17:42:26 -07001744 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001745 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001746 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001747 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001748 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1749 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1750 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001751 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001752 case depTag == cc.CrtBeginDepTag:
Ivan Lozano1f10f682024-11-08 16:16:50 +00001753 depPaths.CrtBegin = append(depPaths.CrtBegin, ccLibPath.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001754 case depTag == cc.CrtEndDepTag:
Ivan Lozano1f10f682024-11-08 16:16:50 +00001755 depPaths.CrtEnd = append(depPaths.CrtEnd, ccLibPath.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001756 }
1757
Ivan Lozano1f10f682024-11-08 16:16:50 +00001758 // Make sure shared dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001759 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1760 lib.exportLinkDirs(linkPath)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001761 lib.exportSharedLibs(ccLibPath.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001762 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001763 } else {
1764 switch {
1765 case depTag == cc.CrtBeginDepTag:
1766 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1767 case depTag == cc.CrtEndDepTag:
1768 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1769 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001770 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001771
Yu Liuc41eae52025-01-14 01:03:08 +00001772 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoProvider); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001773 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001774 // These are usually genrules which don't have per-target variants.
1775 directSrcDeps = append(directSrcDeps, srcDep)
1776 }
1777 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001778 })
1779
Colin Crossa14fb6a2024-10-23 16:57:06 -07001780 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001781
1782 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001783 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001784 for _, dep := range directRlibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001785 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001786 if alias, aliased := aliases[crateName]; aliased {
1787 crateName = alias
1788 }
Yu Liu8024b922024-12-20 23:31:32 +00001789 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001790 }
1791 var dylibDepFiles RustLibraries
1792 for _, dep := range directDylibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001793 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001794 if alias, aliased := aliases[crateName]; aliased {
1795 crateName = alias
1796 }
Yu Liu8024b922024-12-20 23:31:32 +00001797 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001798 }
1799 var procMacroDepFiles RustLibraries
1800 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001801 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001802 if alias, aliased := aliases[crateName]; aliased {
1803 crateName = alias
1804 }
Yu Liu8024b922024-12-20 23:31:32 +00001805 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001806 }
1807
Colin Cross004bd3f2023-10-02 11:39:17 -07001808 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001809 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001810 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001811 }
1812
Colin Cross004bd3f2023-10-02 11:39:17 -07001813 var sharedLibFiles android.Paths
1814 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001815 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001816 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001817 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001818 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001819 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001820 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001821 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001822 }
1823
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001824 var srcProviderDepFiles android.Paths
1825 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001826 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001827 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1828 }
1829 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001830 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001831 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1832 }
1833
Wen-yi Chu41326c12023-09-22 03:58:59 +00001834 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1835 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001836 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1837 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1838 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001839 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001840 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001841
1842 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001843 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001844 depPaths.rustLibObjects = android.FirstUniqueStrings(depPaths.rustLibObjects)
1845 depPaths.staticLibObjects = android.FirstUniqueStrings(depPaths.staticLibObjects)
1846 depPaths.wholeStaticLibObjects = android.FirstUniqueStrings(depPaths.wholeStaticLibObjects)
1847 depPaths.sharedLibObjects = android.FirstUniqueStrings(depPaths.sharedLibObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001848 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001849 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1850 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1851 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoad7ba592025-01-23 01:23:43 +00001852 depPaths.depLinkFlags = android.FirstUniqueStrings(depPaths.depLinkFlags)
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001853 depPaths.reexportedCcRlibDeps = android.FirstUniqueFunc(depPaths.reexportedCcRlibDeps, cc.EqRustRlibDeps)
Ivan Lozano610eb1a2025-02-12 21:36:49 +00001854 depPaths.reexportedWholeCcRlibDeps = android.FirstUniqueFunc(depPaths.reexportedWholeCcRlibDeps, cc.EqRustRlibDeps)
Ivan Lozano85f00cf2025-02-11 20:19:19 +00001855 depPaths.ccRlibDeps = android.FirstUniqueFunc(depPaths.ccRlibDeps, cc.EqRustRlibDeps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001856
1857 return depPaths
1858}
1859
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001860func (mod *Module) InstallInData() bool {
1861 if mod.compiler == nil {
1862 return false
1863 }
1864 return mod.compiler.inData()
1865}
1866
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001867func (mod *Module) InstallInRamdisk() bool {
1868 return mod.InRamdisk()
1869}
1870
1871func (mod *Module) InstallInVendorRamdisk() bool {
1872 return mod.InVendorRamdisk()
1873}
1874
1875func (mod *Module) InstallInRecovery() bool {
1876 return mod.InRecovery()
1877}
1878
Wen-yi Chu41326c12023-09-22 03:58:59 +00001879func linkPathFromFilePath(filepath android.Path) string {
1880 return strings.Split(filepath.String(), filepath.Base())[0]
1881}
1882
Spandan Das604f3762023-03-16 22:51:40 +00001883// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1884func (r *Module) usePublicApi() bool {
1885 return r.Device() && r.UseSdk()
1886}
1887
1888// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1889func (r *Module) useVendorApi() bool {
1890 return r.Device() && (r.InVendor() || r.InProduct())
1891}
1892
Ivan Lozanoffee3342019-08-27 12:03:00 -07001893func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1894 ctx := &depsContext{
1895 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001896 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001897
1898 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001899 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001900
1901 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001902 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001903 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001904
Ivan Lozano2b081132020-09-08 12:46:52 -04001905 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001906 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001907 stdLinkage = "rlib-std"
1908 }
1909
1910 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001911
Ivan Lozano2b081132020-09-08 12:46:52 -04001912 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1913 rlibDepVariations = append(rlibDepVariations,
1914 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1915 }
1916
Ivan Lozano1921e802021-05-20 13:39:16 -04001917 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001918 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001919 for _, lib := range deps.Rlibs {
1920 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001921 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001922 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001923
1924 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001925 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001926
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001927 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001928 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001929 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001930
Ivan Lozano1921e802021-05-20 13:39:16 -04001931 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001932 if deps.Rustlibs != nil {
1933 if !mod.compiler.Disabled() {
1934 for _, lib := range deps.Rustlibs {
1935 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1936 if autoDep.depTag == rlibDepTag {
1937 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001938 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1939
Ivan Lozanod106efe2023-09-21 23:30:26 -04001940 } else {
1941 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1942 // Check for the existence of the dylib deptag variant. Select it if available,
1943 // otherwise select the rlib variant.
1944 autoDepVariations := append(commonDepVariations,
1945 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001946 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1947 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001948
Ivan Lozanod106efe2023-09-21 23:30:26 -04001949 } else {
1950 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001951 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1952
Ivan Lozanod106efe2023-09-21 23:30:26 -04001953 }
1954 }
1955 }
1956 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1957 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001958 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001959 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001960
Ivan Lozano0a468a42024-05-13 21:03:34 -04001961 // Only add rustlib dependencies if they're source providers themselves.
1962 // This is used to track which crate names need to be added to the source generated
1963 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001964 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001965 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001966 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001967 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001968 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001969 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001970
Ivan Lozano1921e802021-05-20 13:39:16 -04001971 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001972 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001973 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001974 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001975 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001976 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001977 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001978 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001979 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001980 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1981
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001982 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001983 }
1984 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001985
1986 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001987 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001988 name, version := cc.StubsLibNameAndVersion(lib)
1989
1990 variations := []blueprint.Variation{
1991 {Mutator: "link", Variation: "shared"},
1992 }
Spandan Dasff665182024-09-11 18:48:44 +00001993 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001994 }
1995
1996 for _, lib := range deps.WholeStaticLibs {
1997 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001998
1999 actx.AddVariationDependencies([]blueprint.Variation{
2000 {Mutator: "link", Variation: "static"},
2001 }, depTag, lib)
2002 }
2003
2004 for _, lib := range deps.StaticLibs {
2005 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04002006
2007 actx.AddVariationDependencies([]blueprint.Variation{
2008 {Mutator: "link", Variation: "static"},
2009 }, depTag, lib)
2010 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07002011
Zach Johnson3df4e632020-11-06 11:56:27 -08002012 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
2013
Colin Cross565cafd2020-09-25 18:47:38 -07002014 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08002015 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09002016 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07002017 }
Colin Crossfe605e12022-01-23 20:46:16 -08002018 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09002019 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07002020 }
2021
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04002022 if mod.sourceProvider != nil {
2023 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
2024 bindgen.Properties.Custom_bindgen != "" {
2025 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
2026 bindgen.Properties.Custom_bindgen)
2027 }
2028 }
Ivan Lozano1921e802021-05-20 13:39:16 -04002029
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04002030 actx.AddVariationDependencies([]blueprint.Variation{
2031 {Mutator: "link", Variation: "shared"},
2032 }, dataLibDepTag, deps.DataLibs...)
2033
2034 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
2035
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07002036 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07002037 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05002038
2039 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07002040}
2041
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002042func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07002043 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002044 mod.beginMutator(ctx)
2045 }
2046}
2047
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002048func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
2049 ctx := &baseModuleContext{
2050 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002051 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002052
2053 mod.begin(ctx)
2054}
2055
Ivan Lozanoffee3342019-08-27 12:03:00 -07002056func (mod *Module) Name() string {
2057 name := mod.ModuleBase.Name()
2058 if p, ok := mod.compiler.(interface {
2059 Name(string) string
2060 }); ok {
2061 name = p.Name(name)
2062 }
2063 return name
2064}
2065
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02002066func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04002067 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02002068 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04002069 }
2070}
2071
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002072var _ android.HostToolProvider = (*Module)(nil)
2073
2074func (mod *Module) HostToolPath() android.OptionalPath {
2075 if !mod.Host() {
2076 return android.OptionalPath{}
2077 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07002078 if binary, ok := mod.compiler.(*binaryDecorator); ok {
2079 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04002080 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
2081 // Even though proc-macros aren't strictly "tools", since they target the compiler
2082 // and act as compiler plugins, we treat them similarly.
2083 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002084 }
2085 return android.OptionalPath{}
2086}
2087
Jiyong Park99644e92020-11-17 22:21:02 +09002088var _ android.ApexModule = (*Module)(nil)
2089
Ivan Lozano24cf0362024-10-04 16:02:38 +00002090// If a module is marked for exclusion from apexes, don't provide apex variants.
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002091// TODO(b/362509506): remove this once all apex_exclude usages are removed.
Ivan Lozano24cf0362024-10-04 16:02:38 +00002092func (m *Module) CanHaveApexVariants() bool {
2093 if m.ApexExclude() {
2094 return false
2095 } else {
2096 return m.ApexModuleBase.CanHaveApexVariants()
2097 }
2098}
2099
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002100func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002101 return String(mod.Properties.Min_sdk_version)
2102}
2103
Jiyong Park45bf82e2020-12-15 22:29:02 +09002104// Implements android.ApexModule
Yu Liudf0b8392025-02-12 18:27:03 +00002105func (mod *Module) MinSdkVersionSupported(ctx android.BaseModuleContext) android.ApiLevel {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002106 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002107 if minSdkVersion == "apex_inherit" {
Yu Liudf0b8392025-02-12 18:27:03 +00002108 return android.MinApiLevel
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002109 }
2110
Yu Liudf0b8392025-02-12 18:27:03 +00002111 if minSdkVersion == "" {
2112 return android.NoneApiLevel
2113 }
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002114 // Not using nativeApiLevelFromUser because the context here is not
2115 // necessarily a native context.
Yu Liudf0b8392025-02-12 18:27:03 +00002116 ver, err := android.ApiLevelFromUserWithConfig(ctx.Config(), minSdkVersion)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002117 if err != nil {
Yu Liudf0b8392025-02-12 18:27:03 +00002118 return android.NoneApiLevel
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002119 }
2120
Yu Liudf0b8392025-02-12 18:27:03 +00002121 return ver
Jiyong Park99644e92020-11-17 22:21:02 +09002122}
2123
Jiyong Park45bf82e2020-12-15 22:29:02 +09002124// Implements android.ApexModule
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002125func (mod *Module) AlwaysRequiresPlatformApexVariant() bool {
2126 // stub libraries and native bridge libraries are always available to platform
2127 // TODO(b/362509506): remove the ApexExclude() check once all apex_exclude uses are switched to stubs.
2128 return mod.IsStubs() || mod.Target().NativeBridge == android.NativeBridgeEnabled || mod.ApexExclude()
2129}
2130
2131// Implements android.ApexModule
Yu Liuf1806032025-02-07 00:23:34 +00002132type RustDepInSameApexChecker struct {
2133 Static bool
2134 HasStubsVariants bool
2135 ApexExclude bool
2136 Host bool
2137}
2138
2139func (mod *Module) GetDepInSameApexChecker() android.DepInSameApexChecker {
2140 return RustDepInSameApexChecker{
2141 Static: mod.Static(),
2142 HasStubsVariants: mod.HasStubsVariants(),
2143 ApexExclude: mod.ApexExclude(),
2144 Host: mod.Host(),
2145 }
2146}
2147
2148func (r RustDepInSameApexChecker) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07002149 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09002150 return false
2151 }
2152
Yu Liuf1806032025-02-07 00:23:34 +00002153 if r.Static && cc.IsSharedDepTag(depTag) {
Colin Cross8acea3e2024-12-12 14:53:30 -08002154 // shared_lib dependency from a static lib is considered as crossing
2155 // the APEX boundary because the dependency doesn't actually is
2156 // linked; the dependency is used only during the compilation phase.
2157 return false
2158 }
2159
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002160 if depTag == cc.StubImplDepTag {
2161 // We don't track from an implementation library to its stubs.
2162 return false
2163 }
2164
2165 if cc.ExcludeInApexDepTag(depTag) {
2166 return false
2167 }
2168
2169 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
Yu Liuf1806032025-02-07 00:23:34 +00002170 if r.ApexExclude {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002171 return false
2172 }
2173
Jiyong Park99644e92020-11-17 22:21:02 +09002174 return true
2175}
2176
Yu Liuf1806032025-02-07 00:23:34 +00002177func (r RustDepInSameApexChecker) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
2178 if r.Host {
Colin Cross1cea5302024-12-03 16:40:08 -08002179 return false
2180 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002181 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
Yu Liuf1806032025-02-07 00:23:34 +00002182 if r.ApexExclude {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002183 return false
2184 }
2185
Yu Liuf1806032025-02-07 00:23:34 +00002186 if r.HasStubsVariants {
Colin Crossbd930bc2025-02-03 12:17:42 -08002187 if cc.IsSharedDepTag(depTag) && !cc.IsExplicitImplSharedDepTag(depTag) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002188 // dynamic dep to a stubs lib crosses APEX boundary
2189 return false
2190 }
2191 if cc.IsRuntimeDepTag(depTag) {
2192 // runtime dep to a stubs lib also crosses APEX boundary
2193 return false
2194 }
2195 if cc.IsHeaderDepTag(depTag) {
2196 return false
2197 }
2198 }
2199 return true
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002200}
2201
Jiyong Park99644e92020-11-17 22:21:02 +09002202// Overrides ApexModule.IsInstallabeToApex()
2203func (mod *Module) IsInstallableToApex() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002204 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2205 if mod.ApexExclude() {
2206 return false
2207 }
2208
Jiyong Park99644e92020-11-17 22:21:02 +09002209 if mod.compiler != nil {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002210 if lib, ok := mod.compiler.(libraryInterface); ok {
2211 return (lib.shared() || lib.dylib()) && !lib.BuildStubs()
Jiyong Park99644e92020-11-17 22:21:02 +09002212 }
2213 if _, ok := mod.compiler.(*binaryDecorator); ok {
2214 return true
2215 }
2216 }
2217 return false
2218}
2219
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05002220// If a library file has a "lib" prefix, extract the library name without the prefix.
2221func libNameFromFilePath(filepath android.Path) (string, bool) {
2222 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
2223 if strings.HasPrefix(libName, "lib") {
2224 libName = libName[3:]
2225 return libName, true
2226 }
2227 return "", false
2228}
2229
Sasha Smundaka76acba2022-04-18 20:12:56 -07002230func kytheExtractRustFactory() android.Singleton {
2231 return &kytheExtractRustSingleton{}
2232}
2233
2234type kytheExtractRustSingleton struct {
2235}
2236
2237func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2238 var xrefTargets android.Paths
2239 ctx.VisitAllModules(func(module android.Module) {
2240 if rustModule, ok := module.(xref); ok {
2241 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
2242 }
2243 })
2244 if len(xrefTargets) > 0 {
2245 ctx.Phony("xref_rust", xrefTargets...)
2246 }
2247}
2248
Jihoon Kangf78a8902022-09-01 22:47:07 +00002249func (c *Module) Partition() string {
2250 return ""
2251}
2252
Ivan Lozanoffee3342019-08-27 12:03:00 -07002253var Bool = proptools.Bool
2254var BoolDefault = proptools.BoolDefault
2255var String = proptools.String
2256var StringPtr = proptools.StringPtr